
























@@ -74,6 +74,28 @@ describe("CronService restart catch-up", () => {
7474};
7575}
767677+function expectQueuedSystemEvent(
78+enqueueSystemEvent: ReturnType<typeof vi.fn>,
79+expectedText: string,
80+) {
81+expect(enqueueSystemEvent).toHaveBeenCalledTimes(1);
82+const [text, options] = enqueueSystemEvent.mock.calls[0] ?? [];
83+expect(text).toBe(expectedText);
84+expect((options as { agentId?: string } | undefined)?.agentId).toBeUndefined();
85+}
86+87+function expectInterruptedJobEvent(
88+onEvent: ReturnType<typeof vi.fn>,
89+expected: { jobId: string; runAtMs: number },
90+) {
91+const event = onEvent.mock.calls
92+.map(([evt]) => evt as CronEvent)
93+.find((evt) => evt.action === "finished" && evt.jobId === expected.jobId);
94+expect(event?.status).toBe("error");
95+expect(event?.error).toBe("cron: job interrupted by gateway restart");
96+expect(event?.runAtMs).toBe(expected.runAtMs);
97+}
98+7799async function withRestartedCron(
78100jobs: unknown[],
79101run: (params: {
@@ -130,10 +152,7 @@ describe("CronService restart catch-up", () => {
130152},
131153],
132154async ({ cron, enqueueSystemEvent, requestHeartbeat }) => {
133-expect(enqueueSystemEvent).toHaveBeenCalledWith(
134-"digest now",
135-expect.objectContaining({ agentId: undefined }),
136-);
155+expectQueuedSystemEvent(enqueueSystemEvent, "digest now");
137156expect(requestHeartbeat).toHaveBeenCalled();
138157139158const listedJobs = await cron.list({ includeDisabled: true });
@@ -217,9 +236,13 @@ describe("CronService restart catch-up", () => {
217236},
218237],
219238async ({ cron, enqueueSystemEvent, requestHeartbeat, onEvent }) => {
220-expect(noopLogger.warn).toHaveBeenCalledWith(
221-expect.objectContaining({ jobId: "restart-stale-running" }),
222-"cron: marking interrupted running job failed on startup",
239+const warning = vi
240+.mocked(noopLogger.warn)
241+.mock.calls.find(
242+([, message]) => message === "cron: marking interrupted running job failed on startup",
243+);
244+expect((warning?.[0] as { jobId?: string } | undefined)?.jobId).toBe(
245+"restart-stale-running",
223246);
224247225248expect(enqueueSystemEvent).not.toHaveBeenCalled();
@@ -233,15 +256,10 @@ describe("CronService restart catch-up", () => {
233256expect(updated?.state.lastRunAtMs).toBe(staleRunningAt);
234257expect(updated?.state.lastError).toBe("cron: job interrupted by gateway restart");
235258expect(updated?.state.nextRunAtMs).toBeGreaterThan(Date.parse("2025-12-13T17:00:00.000Z"));
236-expect(onEvent).toHaveBeenCalledWith(
237-expect.objectContaining({
238-action: "finished",
239-jobId: "restart-stale-running",
240-status: "error",
241-error: "cron: job interrupted by gateway restart",
242-runAtMs: staleRunningAt,
243-}),
244-);
259+expectInterruptedJobEvent(onEvent, {
260+jobId: "restart-stale-running",
261+runAtMs: staleRunningAt,
262+});
245263},
246264);
247265});
@@ -269,10 +287,7 @@ describe("CronService restart catch-up", () => {
269287},
270288],
271289async ({ cron, enqueueSystemEvent, requestHeartbeat }) => {
272-expect(enqueueSystemEvent).toHaveBeenCalledWith(
273-"catch missed slot",
274-expect.objectContaining({ agentId: undefined }),
275-);
290+expectQueuedSystemEvent(enqueueSystemEvent, "catch missed slot");
276291expect(requestHeartbeat).toHaveBeenCalled();
277292278293const listedJobs = await cron.list({ includeDisabled: true });
@@ -317,15 +332,10 @@ describe("CronService restart catch-up", () => {
317332expect(updated?.state.lastRunAtMs).toBe(staleRunningAt);
318333expect(updated?.state.nextRunAtMs).toBeUndefined();
319334expect(updated?.state.lastError).toBe("cron: job interrupted by gateway restart");
320-expect(onEvent).toHaveBeenCalledWith(
321-expect.objectContaining({
322-action: "finished",
323-jobId: "restart-stale-one-shot",
324-status: "error",
325-error: "cron: job interrupted by gateway restart",
326-runAtMs: staleRunningAt,
327-}),
328-);
335+expectInterruptedJobEvent(onEvent, {
336+jobId: "restart-stale-one-shot",
337+runAtMs: staleRunningAt,
338+});
329339},
330340);
331341});
@@ -413,10 +423,7 @@ describe("CronService restart catch-up", () => {
413423},
414424],
415425async ({ enqueueSystemEvent, requestHeartbeat }) => {
416-expect(enqueueSystemEvent).toHaveBeenCalledWith(
417-"replay after backoff elapsed",
418-expect.objectContaining({ agentId: undefined }),
419-);
426+expectQueuedSystemEvent(enqueueSystemEvent, "replay after backoff elapsed");
420427expect(requestHeartbeat).toHaveBeenCalled();
421428},
422429);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。