diff --git a/src/collections/intelligence/Missions.ts b/src/collections/intelligence/Missions.ts index f50f611b..04f59930 100644 --- a/src/collections/intelligence/Missions.ts +++ b/src/collections/intelligence/Missions.ts @@ -8,6 +8,7 @@ import { expireQueuedEntriesForMission, grantQueuedEntriesForMission, } from "@/lib/contributions"; +import { nextSaturdayStart } from "@/lib/intelligence/fridayTime"; type AssignmentRef = { id: number } | number; @@ -462,13 +463,10 @@ export const Missions: CollectionConfig = { }, }, defaultValue: () => { - const today = new Date(); - const day = today.getDay(); - const diff = (6 - day) % 7; - const nextSaturday = new Date(today); - nextSaturday.setDate(today.getDate() + diff + 7); - nextSaturday.setHours(20, 0, 0, 0); - return nextSaturday; + // The upcoming Saturday at 20:00 unit time (the coming + // weekend, not the one after it). Shared with the weekly + // generate-mission bin so both agree on the target date. + return nextSaturdayStart(); }, required: true, }, diff --git a/src/lib/intelligence/fridayTime.ts b/src/lib/intelligence/fridayTime.ts index 26762f38..6158a500 100644 --- a/src/lib/intelligence/fridayTime.ts +++ b/src/lib/intelligence/fridayTime.ts @@ -79,3 +79,21 @@ export function parseDateKey(key: string): { year: number; month: number; day: n const [y, m, d] = key.split("-").map(Number); return { year: y, month: m, day: d }; } + +/** + * The upcoming operational Saturday at 20:00 unit time: the *nearest* Saturday + * (today itself while it is still before 20:00), not the Saturday after it. If + * today is Saturday and 20:00 has already passed, the following Saturday is + * used. Shared by the Missions `startDateTime` default and the weekly + * generate-mission bin so a new main op lands on the coming weekend instead of + * skipping it. Computed in unit time so a UTC deploy container does not shift + * the date by a day (the trap the Friday open-slot notice hit). + */ +export function nextSaturdayStart(now: Date = new Date()): Date { + const { year, month, day } = zonedParts(now); + const weekday = zonedWeekday(now); // 0 = Sunday .. 6 = Saturday + const daysUntilSaturday = (6 - weekday + 7) % 7; + const candidate = wallTimeToInstant(year, month, day + daysUntilSaturday, 20, 0); + if (candidate.getTime() > now.getTime()) return candidate; + return wallTimeToInstant(year, month, day + daysUntilSaturday + 7, 20, 0); +} diff --git a/src/scripts/generateNextMission.ts b/src/scripts/generateNextMission.ts index 5b243fb1..8e63cf10 100644 --- a/src/scripts/generateNextMission.ts +++ b/src/scripts/generateNextMission.ts @@ -1,6 +1,7 @@ import type { Field, SanitizedConfig } from "payload"; import { getPayload } from "payload"; import { Missions } from "@/collections/intelligence/Missions"; +import { nextSaturdayStart, zonedDateKey } from "@/lib/intelligence/fridayTime"; import { createBinLogger } from "./lib/binFileLogger"; /** @@ -82,19 +83,11 @@ export const script = async (config: SanitizedConfig) => { } // Same logic as the Missions collection startDateTime default: the upcoming - // Saturday at 20:00, always at least a week out. - const today = new Date(); - const day = today.getDay(); - const diff = (6 - day) % 7; - const nextSaturday = new Date(today); - nextSaturday.setDate(today.getDate() + diff + 7); - nextSaturday.setHours(20, 0, 0, 0); + // Saturday at 20:00 unit time (the coming weekend, not the Saturday after + // it). Computed in unit time so a UTC container does not shift the date. + const nextSaturday = nextSaturdayStart(); - const dateStamp = [ - nextSaturday.getFullYear(), - String(nextSaturday.getMonth() + 1).padStart(2, "0"), - String(nextSaturday.getDate()).padStart(2, "0"), - ].join(""); + const dateStamp = zonedDateKey(nextSaturday).replace(/-/g, ""); // The source may itself be a previously generated clone whose codeName // already ends in a datestamp — strip it so reruns derive a stable diff --git a/tests/int/next-saturday-start.int.spec.ts b/tests/int/next-saturday-start.int.spec.ts new file mode 100644 index 00000000..2e1613cd --- /dev/null +++ b/tests/int/next-saturday-start.int.spec.ts @@ -0,0 +1,48 @@ +import { describe, expect, it } from "vitest"; +import { + nextSaturdayStart, + wallTimeToInstant, + zonedDateKey, + zonedParts, +} from "@/lib/intelligence/fridayTime"; + +// 2026-10-10 is a Saturday, so: Oct 6 = Tuesday, Oct 9 = Friday, Oct 11 = Sunday. +const et = (y: number, m: number, d: number, h: number, min = 0): Date => + wallTimeToInstant(y, m, d, h, min); + +describe("nextSaturdayStart", () => { + it("returns the coming Saturday for every day of the week before 20:00", () => { + const cases: Array<[Date, string]> = [ + [et(2026, 10, 6, 10), "2026-10-10"], // Tuesday -> the same coming Saturday + [et(2026, 10, 9, 23), "2026-10-10"], // Friday night + [et(2026, 10, 11, 0, 30), "2026-10-17"], // Sunday just after midnight + ]; + for (const [now, expectedKey] of cases) { + const result = nextSaturdayStart(now); + expect(zonedDateKey(result)).toBe(expectedKey); + expect(zonedParts(result).hour).toBe(20); + expect(result.getTime()).toBeGreaterThan(now.getTime()); + } + }); + + it("uses today when it is Saturday but 20:00 has not passed yet", () => { + const result = nextSaturdayStart(et(2026, 10, 10, 15)); + expect(zonedDateKey(result)).toBe("2026-10-10"); + expect(zonedParts(result).hour).toBe(20); + }); + + it("rolls to the following Saturday once today's 20:00 has passed", () => { + const result = nextSaturdayStart(et(2026, 10, 10, 21)); + expect(zonedDateKey(result)).toBe("2026-10-17"); + expect(zonedParts(result).hour).toBe(20); + }); + + it("never skips a week when today is midweek", () => { + // Regression: the old "+7" always landed on the Saturday AFTER the coming + // one, so a Tuesday generation jumped 11 days out instead of 4. + const now = et(2026, 10, 6, 12); + const daysOut = + (nextSaturdayStart(now).getTime() - now.getTime()) / (24 * 60 * 60 * 1000); + expect(daysOut).toBeLessThan(7); + }); +});