import { expect, test } from "bun:test"; import { forEachDialect } from "@sttracker/db/testing"; import { ensureBackupScheduled, nextBackupAt } from "../src/boot.ts"; test("nextBackupAt is the next 03:00 local time", () => { const before = new Date(2026, 9, 30, 2, 59); expect(nextBackupAt(before).getTime()).toBe(new Date(2026, 9, 30, 3, 0).getTime()); const after = new Date(2026, 9, 30, 3, 0); expect(nextBackupAt(after).getTime()).toBe(new Date(2026, 9, 31, 3, 0).getTime()); }); forEachDialect("boot", (getDb) => { test("ensureBackupScheduled queues one backup job under lockKey backup", async () => { const db = getDb(); expect(await ensureBackupScheduled(db)).not.toBeNull(); expect(await ensureBackupScheduled(db)).toBeNull(); const jobs = await db.k.selectFrom("jobs").selectAll().where("type", "=", "backup").execute(); expect(jobs).toHaveLength(1); expect(jobs[0]).toMatchObject({ lock_key: "backup", status: "queued" }); expect(new Date(jobs[0]!.run_at).getHours()).toBe(3); }); });