Files
template-app/test/module-schedules.test.ts
T
2026-11-14 09:56:45 +00:00

36 lines
1.6 KiB
TypeScript

// SPDX-License-Identifier: LicenseRef-ConusAI-Template-1.0
// Module cron comes from the manifests (TM-R3): a module declares `schedules` beside its `jobs`, and
// `apps/worker/src/schedules.ts` names no module, so adding one edits nothing under apps/.
import { readFileSync } from "node:fs"
import { appModules } from "@app/modules"
import { buildRegistry, defineModule } from "@conusai-kit/core"
import { describe, expect, it } from "vitest"
import { z } from "zod"
import { appSchedules } from "../apps/worker/src/schedules.js"
describe("the worker's module cron", () => {
it("is every schedule the manifests declare, each with its module's id", () => {
const schedules = appSchedules({})
expect(schedules.map((schedule) => schedule.module)).toEqual(expect.arrayContaining(["files", "account"]))
const registry = buildRegistry([...appModules])
for (const schedule of schedules) expect(registry.jobs[schedule.job]?.module).toBe(schedule.module)
})
it("names no module in the scaffold's file", () => {
const source = readFileSync(new URL("../apps/worker/src/schedules.ts", import.meta.url), "utf8")
expect(source).not.toMatch(/@app\/modules\/|_SCHEDULES|defineSchedules/)
})
it("refuses a schedule of a job the module does not declare, at boot", () => {
const module = defineModule({
id: "reports",
version: "0.1.0",
kit: "^0.1.0",
jobs: { "reports.weekly": { payload: z.null(), handler: async () => undefined } },
schedules: [{ job: "reports.nightly", cron: "0 2 * * *", scope: null }],
})
expect(() => buildRegistry([module])).toThrow(/reports\.nightly/)
})
})