test(cron): improve fire-and-forget harness coverage

This commit is contained in:
Peter Steinberger
2026-02-22 11:29:15 +00:00
parent c343132dbb
commit 0a758dc710
3 changed files with 63 additions and 61 deletions

View File

@@ -1,5 +1,3 @@
import fs from "node:fs/promises";
import path from "node:path";
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it, vi } from "vitest";
import { CronService } from "./service.js"; import { CronService } from "./service.js";
import { import {
@@ -7,6 +5,7 @@ import {
createCronStoreHarness, createCronStoreHarness,
createNoopLogger, createNoopLogger,
installCronTestHooks, installCronTestHooks,
writeCronStoreSnapshot,
} from "./service.test-harness.js"; } from "./service.test-harness.js";
const noopLogger = createNoopLogger(); const noopLogger = createNoopLogger();
@@ -120,12 +119,8 @@ describe("CronService interval/cron jobs fire on time", () => {
const requestHeartbeatNow = vi.fn(); const requestHeartbeatNow = vi.fn();
const nowMs = Date.parse("2025-12-13T00:00:00.000Z"); const nowMs = Date.parse("2025-12-13T00:00:00.000Z");
await fs.mkdir(path.dirname(store.storePath), { recursive: true }); await writeCronStoreSnapshot({
await fs.writeFile( storePath: store.storePath,
store.storePath,
JSON.stringify(
{
version: 1,
jobs: [ jobs: [
{ {
id: "legacy-every", id: "legacy-every",
@@ -152,12 +147,7 @@ describe("CronService interval/cron jobs fire on time", () => {
state: { nextRunAtMs: nowMs + 60_000 }, state: { nextRunAtMs: nowMs + 60_000 },
}, },
], ],
}, });
null,
2,
),
"utf-8",
);
const cron = new CronService({ const cron = new CronService({
storePath: store.storePath, storePath: store.storePath,

View File

@@ -3,6 +3,7 @@ import os from "node:os";
import path from "node:path"; import path from "node:path";
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it, vi } from "vitest";
import { CronService } from "./service.js"; import { CronService } from "./service.js";
import { writeCronStoreSnapshot } from "./service.test-harness.js";
const noopLogger = { const noopLogger = {
debug: vi.fn(), debug: vi.fn(),
@@ -167,11 +168,8 @@ describe("CronService read ops while job is running", () => {
const requestHeartbeatNow = vi.fn(); const requestHeartbeatNow = vi.fn();
const nowMs = Date.parse("2025-12-13T00:00:00.000Z"); const nowMs = Date.parse("2025-12-13T00:00:00.000Z");
await fs.mkdir(path.dirname(store.storePath), { recursive: true }); await writeCronStoreSnapshot({
await fs.writeFile( storePath: store.storePath,
store.storePath,
JSON.stringify({
version: 1,
jobs: [ jobs: [
{ {
id: "startup-catchup", id: "startup-catchup",
@@ -187,9 +185,7 @@ describe("CronService read ops while job is running", () => {
state: { nextRunAtMs: nowMs - 60_000 }, state: { nextRunAtMs: nowMs - 60_000 },
}, },
], ],
}), });
"utf-8",
);
const isolatedRun = createDeferredIsolatedRun(); const isolatedRun = createDeferredIsolatedRun();

View File

@@ -51,6 +51,22 @@ export function createCronStoreHarness(options?: { prefix?: string }) {
return { makeStorePath }; return { makeStorePath };
} }
export async function writeCronStoreSnapshot(params: { storePath: string; jobs: CronJob[] }) {
await fs.mkdir(path.dirname(params.storePath), { recursive: true });
await fs.writeFile(
params.storePath,
JSON.stringify(
{
version: 1,
jobs: params.jobs,
},
null,
2,
),
"utf-8",
);
}
export function installCronTestHooks(options: { export function installCronTestHooks(options: {
logger: ReturnType<typeof createNoopLogger>; logger: ReturnType<typeof createNoopLogger>;
baseTimeIso?: string; baseTimeIso?: string;