test(agents): dedupe agent and cron test scaffolds

This commit is contained in:
Peter Steinberger
2026-03-02 06:40:42 +00:00
parent 281494ae52
commit 7e29d604ba
38 changed files with 3114 additions and 4486 deletions

View File

@@ -1,46 +1,54 @@
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
const noop = () => {};
const MAIN_REQUESTER_SESSION_KEY = "agent:main:main";
const MAIN_REQUESTER_DISPLAY_KEY = "main";
let lifecycleHandler:
| ((evt: {
stream?: string;
runId: string;
data?: {
phase?: string;
startedAt?: number;
endedAt?: number;
aborted?: boolean;
error?: string;
};
}) => void)
| undefined;
type LifecycleData = {
phase?: string;
startedAt?: number;
endedAt?: number;
aborted?: boolean;
error?: string;
};
type LifecycleEvent = {
stream?: string;
runId: string;
data?: LifecycleData;
};
let lifecycleHandler: ((evt: LifecycleEvent) => void) | undefined;
const callGatewayMock = vi.fn(async (request: unknown) => {
const method = (request as { method?: string }).method;
if (method === "agent.wait") {
// Keep wait unresolved from the RPC path so lifecycle fallback logic is exercised.
return { status: "pending" };
}
return {};
});
const onAgentEventMock = vi.fn((handler: typeof lifecycleHandler) => {
lifecycleHandler = handler;
return noop;
});
const loadConfigMock = vi.fn(() => ({
agents: { defaults: { subagents: { archiveAfterMinutes: 0 } } },
}));
const loadRegistryMock = vi.fn(() => new Map());
const saveRegistryMock = vi.fn(() => {});
const announceSpy = vi.fn(async () => true);
vi.mock("../gateway/call.js", () => ({
callGateway: vi.fn(async (request: unknown) => {
const method = (request as { method?: string }).method;
if (method === "agent.wait") {
// Keep wait unresolved from the RPC path so lifecycle fallback logic is exercised.
return { status: "pending" };
}
return {};
}),
callGateway: callGatewayMock,
}));
vi.mock("../infra/agent-events.js", () => ({
onAgentEvent: vi.fn((handler: typeof lifecycleHandler) => {
lifecycleHandler = handler;
return noop;
}),
onAgentEvent: onAgentEventMock,
}));
vi.mock("../config/config.js", () => ({
loadConfig: vi.fn(() => ({
agents: { defaults: { subagents: { archiveAfterMinutes: 0 } } },
})),
loadConfig: loadConfigMock,
}));
const announceSpy = vi.fn(async () => true);
vi.mock("./subagent-announce.js", () => ({
runSubagentAnnounceFlow: announceSpy,
}));
@@ -50,8 +58,8 @@ vi.mock("../plugins/hook-runner-global.js", () => ({
}));
vi.mock("./subagent-registry.store.js", () => ({
loadSubagentRegistryFromDisk: vi.fn(() => new Map()),
saveSubagentRegistryToDisk: vi.fn(() => {}),
loadSubagentRegistryFromDisk: loadRegistryMock,
saveSubagentRegistryToDisk: saveRegistryMock,
}));
describe("subagent registry lifecycle error grace", () => {
@@ -77,21 +85,41 @@ describe("subagent registry lifecycle error grace", () => {
await Promise.resolve();
};
it("ignores transient lifecycle errors when run retries and then ends successfully", async () => {
function registerCompletionRun(runId: string, childSuffix: string, task: string) {
mod.registerSubagentRun({
runId: "run-transient-error",
childSessionKey: "agent:main:subagent:transient-error",
requesterSessionKey: "agent:main:main",
requesterDisplayKey: "main",
task: "transient error test",
runId,
childSessionKey: `agent:main:subagent:${childSuffix}`,
requesterSessionKey: MAIN_REQUESTER_SESSION_KEY,
requesterDisplayKey: MAIN_REQUESTER_DISPLAY_KEY,
task,
cleanup: "keep",
expectsCompletionMessage: true,
});
}
function emitLifecycleEvent(runId: string, data: LifecycleData) {
lifecycleHandler?.({
stream: "lifecycle",
runId: "run-transient-error",
data: { phase: "error", error: "rate limit", endedAt: 1_000 },
runId,
data,
});
}
function readFirstAnnounceOutcome() {
const announceCalls = announceSpy.mock.calls as unknown as Array<Array<unknown>>;
const first = (announceCalls[0]?.[0] ?? {}) as {
outcome?: { status?: string; error?: string };
};
return first.outcome;
}
it("ignores transient lifecycle errors when run retries and then ends successfully", async () => {
registerCompletionRun("run-transient-error", "transient-error", "transient error test");
emitLifecycleEvent("run-transient-error", {
phase: "error",
error: "rate limit",
endedAt: 1_000,
});
await flushAsync();
expect(announceSpy).not.toHaveBeenCalled();
@@ -99,46 +127,26 @@ describe("subagent registry lifecycle error grace", () => {
await vi.advanceTimersByTimeAsync(14_999);
expect(announceSpy).not.toHaveBeenCalled();
lifecycleHandler?.({
stream: "lifecycle",
runId: "run-transient-error",
data: { phase: "start", startedAt: 1_050 },
});
emitLifecycleEvent("run-transient-error", { phase: "start", startedAt: 1_050 });
await flushAsync();
await vi.advanceTimersByTimeAsync(20_000);
expect(announceSpy).not.toHaveBeenCalled();
lifecycleHandler?.({
stream: "lifecycle",
runId: "run-transient-error",
data: { phase: "end", endedAt: 1_250 },
});
emitLifecycleEvent("run-transient-error", { phase: "end", endedAt: 1_250 });
await flushAsync();
expect(announceSpy).toHaveBeenCalledTimes(1);
const announceCalls = announceSpy.mock.calls as unknown as Array<Array<unknown>>;
const first = (announceCalls[0]?.[0] ?? {}) as {
outcome?: { status?: string; error?: string };
};
expect(first.outcome?.status).toBe("ok");
expect(readFirstAnnounceOutcome()?.status).toBe("ok");
});
it("announces error when lifecycle error remains terminal after grace window", async () => {
mod.registerSubagentRun({
runId: "run-terminal-error",
childSessionKey: "agent:main:subagent:terminal-error",
requesterSessionKey: "agent:main:main",
requesterDisplayKey: "main",
task: "terminal error test",
cleanup: "keep",
expectsCompletionMessage: true,
});
registerCompletionRun("run-terminal-error", "terminal-error", "terminal error test");
lifecycleHandler?.({
stream: "lifecycle",
runId: "run-terminal-error",
data: { phase: "error", error: "fatal failure", endedAt: 2_000 },
emitLifecycleEvent("run-terminal-error", {
phase: "error",
error: "fatal failure",
endedAt: 2_000,
});
await flushAsync();
expect(announceSpy).not.toHaveBeenCalled();
@@ -147,11 +155,7 @@ describe("subagent registry lifecycle error grace", () => {
await flushAsync();
expect(announceSpy).toHaveBeenCalledTimes(1);
const announceCalls = announceSpy.mock.calls as unknown as Array<Array<unknown>>;
const first = (announceCalls[0]?.[0] ?? {}) as {
outcome?: { status?: string; error?: string };
};
expect(first.outcome?.status).toBe("error");
expect(first.outcome?.error).toBe("fatal failure");
expect(readFirstAnnounceOutcome()?.status).toBe("error");
expect(readFirstAnnounceOutcome()?.error).toBe("fatal failure");
});
});