import path from "node:path"; import { generateEd25519 } from "@crosscode/contracts/signing"; import { checkJob, SeenJobs } from "../src/verify"; import { config, job, NOW, tempState } from "./helpers"; const dir = tempState(); const fresh = () => new SeenJobs(path.join(dir, `seen-${Math.random()}.json`)); describe("checking a job (AGENT-1.0-PLAN ยง4)", () => { it("accepts a signed job for this server, in its window", () => { expect(checkJob(job(), config, fresh(), NOW)).toMatchObject({ ok: true }); }); it("refuses a job signed with another key", () => { expect(checkJob(job({}, generateEd25519().privateKeyPem), config, fresh(), NOW)).toMatchObject({ ok: false, status: "refused", message: expect.stringContaining("signature") }); }); it("refuses a job whose content was changed after signing", () => { const j = job(); expect(checkJob({ ...j, args: { ...j.args, church: "other" } }, config, fresh(), NOW)).toMatchObject({ ok: false, status: "refused" }); }); it("refuses a job for another server", () => { expect(checkJob(job({ serverId: "srv_other0001" }), config, fresh(), NOW)).toMatchObject({ ok: false, message: expect.stringContaining("not this server") }); }); it("reports an expired job as expired, and never runs it", () => { expect(checkJob(job(), config, fresh(), new Date("2026-11-20T12:16:00.000Z"))).toMatchObject({ ok: false, status: "expired" }); }); it("refuses a job that isn't due yet (beyond a minute of clock difference)", () => { expect(checkJob(job(), config, fresh(), new Date("2026-11-20T11:58:00.000Z"))).toMatchObject({ ok: false, status: "refused" }); expect(checkJob(job(), config, fresh(), new Date("2026-11-20T11:59:30.000Z"))).toMatchObject({ ok: true }); }); it("refuses a replayed job, also after a restart", () => { const file = path.join(dir, "seen-replay.json"); const seen = new SeenJobs(file); const j = job(); expect(checkJob(j, config, seen, NOW)).toMatchObject({ ok: true }); seen.add(j.id); expect(checkJob(j, config, seen, NOW)).toMatchObject({ ok: false, message: expect.stringContaining("replay") }); expect(checkJob(j, config, new SeenJobs(file), NOW)).toMatchObject({ ok: false, message: expect.stringContaining("replay") }); }); it("refuses an unknown type, and arguments with shell characters", () => { expect(checkJob(job({ type: "shell", args: { command: "id" } }), config, fresh(), NOW)).toMatchObject({ ok: false, message: expect.stringContaining("Unknown job type") }); expect(checkJob(job({ args: { church: "hope;reboot", products: ["events"] } }), config, fresh(), NOW)).toMatchObject({ ok: false, message: expect.stringContaining("isn't valid") }); expect(checkJob(job({ type: "adopt", args: { church: "hope", eventsDir: "/var/www/$(id)" } }), config, fresh(), NOW)).toMatchObject({ ok: false }); }); it("a central agent only runs check-ports, status and update-agent", () => { const central = { ...config, mode: "central" as const }; expect(checkJob(job(), central, fresh(), NOW)).toMatchObject({ ok: false, message: expect.stringContaining("central") }); expect(checkJob(job({ type: "check-ports", args: { ports: [20010, 20011] } }), central, fresh(), NOW)).toMatchObject({ ok: true }); }); it("refuses things that aren't jobs", () => { for (const raw of [null, "job", [], { id: "nope" }]) expect(checkJob(raw, config, fresh(), NOW)).toMatchObject({ ok: false, status: "refused" }); }); });