Skip to content

Commit 74f2385

Browse files
committed
test(gateway): drive terminal command-run cases through the scheduler
The three command delete/retain/backoff cases invoked the run via cron.run(job.id, "force"/"due"), which now resolves to operator origin and is deliberately non-consuming: it records the outcome but never deletes a deleteAfterRun one-shot, applies error backoff, or otherwise mutates scheduler-owned state (#83538/#83933). The public facade cannot request watcher-terminal/timer semantics by design, so these cases were asserting scheduler-owning outcomes against a non-owning run. Drive them through the real scheduler tick instead (cron.start() + onTimer, origin=timer) — the actual production path for scheduled command jobs. The successful one-shot is deleted even when announce delivery fails, the required-delivery failure retains the one-shot with backoff, and the recurring command error applies backoff. Assertions are unchanged.
1 parent 0f47438 commit 74f2385

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

‎src/gateway/server-cron.test.ts‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,9 @@ describe("buildGatewayCronService", () => {
13591359
});
13601360

13611361
it("keeps command execution errors on backoff when announce delivery also fails", async () => {
1362+
const baseMs = Date.parse("2026-07-14T12:00:00.000Z");
1363+
vi.useFakeTimers();
1364+
vi.setSystemTime(baseMs);
13621365
const cfg = createCronConfig("server-cron-command-execution-failure");
13631366
loadConfigMock.mockReturnValue(cfg);
13641367
const deliveryError = "Channel is required (no configured channels detected)";
@@ -1374,7 +1377,7 @@ describe("buildGatewayCronService", () => {
13741377
name: "failed-headless-command",
13751378
enabled: true,
13761379
deleteAfterRun: false,
1377-
schedule: { kind: "every", everyMs: 20_000, anchorMs: Date.now() },
1380+
schedule: { kind: "every", everyMs: 20_000, anchorMs: baseMs },
13781381
sessionTarget: "isolated",
13791382
wakeMode: "next-heartbeat",
13801383
payload: {
@@ -1383,14 +1386,16 @@ describe("buildGatewayCronService", () => {
13831386
},
13841387
});
13851388

1386-
const dueAtMs = job.state.nextRunAtMs;
1387-
expect(dueAtMs).toBeTypeOf("number");
1388-
const nowSpy = vi.spyOn(Date, "now").mockReturnValue(dueAtMs ?? 0);
1389-
try {
1390-
await state.cron.run(job.id, "due");
1391-
} finally {
1392-
nowSpy.mockRestore();
1393-
}
1389+
// Drive a real scheduled tick (origin=timer): the scheduler owns state, so
1390+
// a failed recurring run applies error backoff. The public run path is
1391+
// operator-only and never reschedules (#83538). Fake timers advance to the
1392+
// due slot to fire onTimer; real timers then let the spawned command drain.
1393+
await state.cron.start();
1394+
const dueAtMs = job.state.nextRunAtMs ?? 0;
1395+
vi.setSystemTime(dueAtMs);
1396+
await vi.advanceTimersByTimeAsync(dueAtMs - baseMs);
1397+
vi.useRealTimers();
1398+
await vi.waitFor(() => expect(state.cron.getJob(job.id)?.state.lastRunStatus).toBe("error"));
13941399

13951400
const updated = state.cron.getJob(job.id);
13961401
expect(updated?.state.lastRunStatus).toBe("error");
@@ -1403,6 +1408,7 @@ describe("buildGatewayCronService", () => {
14031408
);
14041409
} finally {
14051410
state.cron.stop();
1411+
vi.useRealTimers();
14061412
}
14071413
});
14081414

@@ -1432,7 +1438,11 @@ describe("buildGatewayCronService", () => {
14321438
delivery: { mode: "announce", bestEffort: false },
14331439
});
14341440

1435-
await state.cron.run(job.id, "force");
1441+
// Drive a real scheduled tick (origin=timer): a required-delivery failure
1442+
// errors the run, so the scheduler retains the one-shot (no delete on
1443+
// error) and applies backoff. A public operator run would not (#83538).
1444+
await state.cron.start();
1445+
await vi.waitFor(() => expect(state.cron.getJob(job.id)?.state.lastRunStatus).toBe("error"));
14361446

14371447
const updated = state.cron.getJob(job.id);
14381448
expect(updated?.state.lastRunStatus).toBe("error");
@@ -1515,7 +1525,12 @@ describe("buildGatewayCronService", () => {
15151525
},
15161526
});
15171527

1518-
await state.cron.run(job.id, "force");
1528+
// Drive a real scheduled tick (origin=timer): the scheduler consumes the
1529+
// one-shot, so a successful deleteAfterRun job is deleted even when announce
1530+
// delivery fails (delivery failure never demotes an ok run). A public
1531+
// operator run would preserve it instead (#83538/#83933).
1532+
await state.cron.start();
1533+
await vi.waitFor(() => expect(state.cron.getJob(job.id)).toBeUndefined());
15191534

15201535
expect(state.cron.getJob(job.id)).toBeUndefined();
15211536
} finally {

0 commit comments

Comments
 (0)