import test from "node:test"; import assert from "node:assert/strict"; import { defaultSyncScheduler } from "../lib/settings"; import { defaultChannelPriority } from "../lib/channelPriority"; import { buildScheduleView } from "../jobs/syncScheduler"; import { emptySchedulerState } from "../jobs/syncSchedulerState"; import type { ChannelConfig } from "../lib/channelConfig"; import { buildSchedulerStatusPayload } from "./schedulerStatus"; // The payload is four pass-throughs and one projection, and that is the whole // point: everything it reports is either handed to it or is `buildScheduleView` // applied to what was handed to it. A second copy of the due-date rule here is // exactly what this shape exists to prevent. const config = (over: Partial = {}): ChannelConfig => ({ name: "a channel", url: "https://example.com/c", ...over }) as ChannelConfig; function inputs() { const settings = { syncScheduler: { ...defaultSyncScheduler(), enabled: true }, channelPriority: defaultChannelPriority(), }; const state = emptySchedulerState(); state.runs = [{ at: 1_000, queued: ["alpha"], skipped: [] }]; const channels = [ { slug: "alpha", config: config({ lastSyncedAt: "2026-01-01T00:00:00Z" }) }, { slug: "beta", config: config() }, ]; return { settings, now: 1_700_000_000_000, state, channels, heartbeatSeconds: 45, }; } test("now, scheduler, runs and heartbeatSeconds pass straight through", () => { const i = inputs(); const payload = buildSchedulerStatusPayload(i); assert.equal(payload.now, i.now); assert.equal(payload.scheduler, i.settings.syncScheduler); assert.equal(payload.runs, i.state.runs); // The env override is resolved by the shell, beside the heartbeat runner — // whatever it says arrives here verbatim, including 0 ("external cron only"). assert.equal(payload.heartbeatSeconds, 45); assert.equal(buildSchedulerStatusPayload({ ...i, heartbeatSeconds: 0 }).heartbeatSeconds, 0); }); test("channels is buildScheduleView's output, not a second projection", () => { const i = inputs(); const payload = buildSchedulerStatusPayload(i); assert.deepEqual( payload.channels, buildScheduleView({ channels: i.channels, scheduler: i.settings.syncScheduler, state: i.state, now: i.now, priority: i.settings.channelPriority, }), ); assert.deepEqual( payload.channels.map((c) => c.slug), ["alpha", "beta"], ); // And it really is the scheduler's own verdict: a channel that has never // synced is due now. assert.equal(payload.channels[1].overdue, true); });