test: stabilize gateway background tests

This commit is contained in:
Peter Steinberger
2026-04-09 10:12:53 +01:00
parent 1b24560392
commit 7497abc124
2 changed files with 20 additions and 9 deletions

View File

@@ -378,11 +378,16 @@ describe("gateway server agent", () => {
const spy = vi.mocked(agentCommand);
const calls = spy.mock.calls;
const callsBefore = calls.length;
const res = await rpcReq(ws, "agent", {
message: "/new",
sessionKey: "main",
idempotencyKey: "idem-agent-new",
});
const res = await rpcReq(
ws,
"agent",
{
message: "/new",
sessionKey: "main",
idempotencyKey: "idem-agent-new",
},
20_000,
);
expect(res.ok).toBe(true);
await vi.waitFor(() => expect(calls.length).toBeGreaterThan(callsBefore));

View File

@@ -25,6 +25,7 @@ const fetchWithSsrFGuardMock = vi.hoisted(() =>
);
const sendFailureNotificationAnnounceMock = vi.hoisted(() => vi.fn(async () => undefined));
const closeTrackedBrowserTabsForSessionsMock = vi.hoisted(() => vi.fn(async () => 0));
vi.mock("../infra/net/fetch-guard.js", () => ({
fetchWithSsrFGuard: (...args: unknown[]) =>
@@ -48,6 +49,10 @@ vi.mock("../cron/delivery.js", async () => {
};
});
vi.mock("../plugin-sdk/browser-maintenance.js", () => ({
closeTrackedBrowserTabsForSessions: closeTrackedBrowserTabsForSessionsMock,
}));
installGatewayTestHooks({ scope: "suite" });
const CRON_WAIT_TIMEOUT_MS = 3_000;
const EMPTY_CRON_STORE_CONTENT = JSON.stringify({ version: 1, jobs: [] });
@@ -246,6 +251,7 @@ describe("gateway server cron", () => {
// Keep polling helpers deterministic even if other tests left fake timers enabled.
vi.useRealTimers();
sendFailureNotificationAnnounceMock.mockClear();
closeTrackedBrowserTabsForSessionsMock.mockClear();
});
test("handles cron CRUD, normalization, and patch semantics", { timeout: 45_000 }, async () => {
@@ -692,16 +698,16 @@ describe("gateway server cron", () => {
ws,
(payload) => payload?.jobId === jobId && payload?.action === "started",
);
const finishedRun = waitForCronEvent(
ws,
(payload) => payload?.jobId === jobId && payload?.action === "finished",
);
const runRes = await rpcReq(ws, "cron.run", { id: jobId, mode: "force" }, 1_000);
expect(runRes.ok).toBe(true);
expect(runRes.payload).toEqual({ ok: true, enqueued: true, runId: expect.any(String) });
await startedRun;
expect(cronIsolatedRun).toHaveBeenCalledTimes(1);
const finishedRun = waitForCronEvent(
ws,
(payload) => payload?.jobId === jobId && payload?.action === "finished",
);
resolveRun?.({ status: "ok", summary: "background finished" });
const finishedPayload = await finishedRun;
expect(finishedPayload).toMatchObject({