import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { DEFAULT_SCAN_SCHEDULE, nextScanAt, parseScanSchedule, type ScanSchedule, scanDue, validateScanSchedule } from "./folder-scan"; const at = (iso: string) => new Date(iso); const s = (o: Partial): ScanSchedule => ({ ...DEFAULT_SCAN_SCHEDULE, tz: "Europe/Berlin", ...o }); describe("folder scan schedule", () => { it("keeps the old every-2-minutes default and never schedules manual-only", () => { assert.equal(nextScanAt(DEFAULT_SCAN_SCHEDULE, at("2026-10-27T10:00:00Z"))!.toISOString(), "2026-10-27T10:02:00.000Z"); assert.equal(nextScanAt(s({ every: "manual" }), at("2026-10-27T10:00:00Z")), null); assert.equal(scanDue(s({ every: "manual" }), null, at("2026-10-27T10:00:00Z")), false); }); it("runs daily at the local time, across a DST change", () => { const daily = s({ every: "daily", time: "03:00" }); // Berlin is UTC+1 after the switch on 25 Oct 2026. assert.equal(nextScanAt(daily, at("2026-10-27T10:00:00Z"))!.toISOString(), "2026-10-28T02:00:00.000Z"); assert.equal(nextScanAt(daily, at("2026-10-27T01:00:00Z"))!.toISOString(), "2026-10-27T02:00:00.000Z"); // Still summer time (UTC+2) before the switch. assert.equal(nextScanAt(daily, at("2026-10-23T12:00:00Z"))!.toISOString(), "2026-10-24T01:00:00.000Z"); }); it("runs hourly at the given minute, weekly on the weekday, monthly on the day", () => { assert.equal(nextScanAt(s({ every: "hourly", time: "00:15" }), at("2026-10-27T10:20:00Z"))!.toISOString(), "2026-10-27T11:15:00.000Z"); // 27 Oct 2026 is a Tuesday; next Sunday is 1 Nov. assert.equal(nextScanAt(s({ every: "weekly", weekday: 0, time: "04:30" }), at("2026-10-27T10:00:00Z"))!.toISOString(), "2026-11-01T03:30:00.000Z"); assert.equal(nextScanAt(s({ every: "monthly", monthDay: 1, time: "03:00" }), at("2026-10-27T10:00:00Z"))!.toISOString(), "2026-11-01T02:00:00.000Z"); assert.equal(nextScanAt(s({ every: "monthly", monthDay: 27, time: "03:00" }), at("2026-10-27T10:00:00Z"))!.toISOString(), "2026-11-27T02:00:00.000Z"); }); it("is due once the next slot after the last scan has passed (also after downtime)", () => { const daily = s({ every: "daily", time: "03:00" }); assert.equal(scanDue(daily, null, at("2026-10-27T10:00:00Z")), true, "never scanned"); assert.equal(scanDue(daily, at("2026-10-27T02:00:30Z"), at("2026-10-27T10:00:00Z")), false); assert.equal(scanDue(daily, at("2026-10-27T02:00:30Z"), at("2026-10-28T02:00:00Z")), true); assert.equal(scanDue(daily, at("2026-10-25T02:00:30Z"), at("2026-10-27T10:00:00Z")), true, "missed while the worker was down"); }); it("validates input and falls back to the default for broken stored values", () => { assert.deepEqual(validateScanSchedule({ every: "weekly", time: "23:59", weekday: 6, monthDay: 1, tz: "America/New_York" }), { every: "weekly", time: "23:59", weekday: 6, monthDay: 1, tz: "America/New_York", }); assert.throws(() => validateScanSchedule({ every: "yearly", time: "03:00" }), /Frequency/); assert.throws(() => validateScanSchedule({ every: "daily", time: "24:00" }), /HH:MM/); assert.throws(() => validateScanSchedule({ every: "monthly", time: "03:00", monthDay: 31 }), /1–28/); assert.throws(() => validateScanSchedule({ every: "daily", time: "03:00", tz: "Mars/Base" }), /time zone/); assert.deepEqual(parseScanSchedule({ every: "nope" }), DEFAULT_SCAN_SCHEDULE); }); });