test(agents): share notify heartbeat wake helper

This commit is contained in:
Vincent Koc
2026-04-12 16:43:23 +01:00
parent 4938b2cc43
commit 156ee544ed

View File

@@ -232,6 +232,19 @@ async function startBackgroundCommand(tool: ExecToolInstance, command: string) {
return requireRunningSessionId(result);
}
async function expectNotifyOnExitWake(tool: ExecToolInstance, expected: Record<string, unknown>) {
const wakeHandler = vi.fn().mockResolvedValue({ status: "skipped", reason: "disabled" });
const dispose = setHeartbeatWakeHandler(
wakeHandler as unknown as Parameters<typeof setHeartbeatWakeHandler>[0],
);
try {
await startBackgroundCommand(tool, echoAfterDelay("notify"));
await expect.poll(() => wakeHandler.mock.calls[0]?.[0], NOTIFY_POLL_OPTIONS).toEqual(expected);
} finally {
dispose();
}
}
async function drainNotifyEvents(sessionKey = DEFAULT_NOTIFY_SESSION_KEY) {
return await drainFormattedSystemEvents({
cfg: notifyCfg,
@@ -606,42 +619,16 @@ describe("exec notifyOnExit", () => {
});
it("scopes notifyOnExit heartbeat wake to the exec session key", async () => {
const tool = createNotifyOnExitExecTool();
const wakeHandler = vi.fn().mockResolvedValue({ status: "skipped", reason: "disabled" });
const dispose = setHeartbeatWakeHandler(
wakeHandler as unknown as Parameters<typeof setHeartbeatWakeHandler>[0],
);
try {
const _sessionId = await startBackgroundCommand(tool, echoAfterDelay("notify"));
await expect
.poll(() => wakeHandler.mock.calls[0]?.[0], NOTIFY_POLL_OPTIONS)
.toMatchObject({
reason: "exec-event",
sessionKey: DEFAULT_NOTIFY_SESSION_KEY,
});
} finally {
dispose();
}
await expectNotifyOnExitWake(createNotifyOnExitExecTool(), {
reason: "exec-event",
sessionKey: DEFAULT_NOTIFY_SESSION_KEY,
});
});
it("keeps notifyOnExit heartbeat wake unscoped for non-agent session keys", async () => {
const tool = createNotifyOnExitExecTool({ sessionKey: "global" });
const wakeHandler = vi.fn().mockResolvedValue({ status: "skipped", reason: "disabled" });
const dispose = setHeartbeatWakeHandler(
wakeHandler as unknown as Parameters<typeof setHeartbeatWakeHandler>[0],
);
try {
const _sessionId = await startBackgroundCommand(tool, echoAfterDelay("notify"));
await expect
.poll(() => wakeHandler.mock.calls[0]?.[0], NOTIFY_POLL_OPTIONS)
.toEqual({
reason: "exec-event",
});
} finally {
dispose();
}
await expectNotifyOnExitWake(createNotifyOnExitExecTool({ sessionKey: "global" }), {
reason: "exec-event",
});
});
it.each<NotifyNoopCase>(NOOP_NOTIFY_CASES)("$label", runNotifyNoopCase);