diff --git a/pm-stack/aggregator/test/events.test.ts b/pm-stack/aggregator/test/events.test.ts index 98efae9bbd..74d93bf6e6 100644 --- a/pm-stack/aggregator/test/events.test.ts +++ b/pm-stack/aggregator/test/events.test.ts @@ -188,13 +188,22 @@ describe("events store (#1739)", () => { }); test("validateStreamEvent refuses impossible calendar dates (#5154)", () => { - // Date.parse rolls these over to a neighbouring month instead of - // returning NaN; the ingest boundary must refuse them outright. - expect(validateStreamEvent({ ...base, at: "2026-02-30" }).ok).toBe(false); - expect(validateStreamEvent({ ...base, at: "2026-02-30T10:00:00Z" }).ok).toBe(false); - expect(validateStreamEvent({ ...base, at: "2026-04-31T10:00:00Z" }).ok).toBe(false); - expect(validateStreamEvent({ ...base, at: "2026-13-01T00:00:00Z" }).ok).toBe(false); - expect(validateStreamEvent({ ...base, at: "2026-02-29T10:00:00Z" }).ok).toBe(false); + // Date.parse rolls impossible dates over and accepts locale-dependent + // non-ISO forms; the ingest boundary must refuse both with its ISO error. + for (const at of [ + "2026-02-30", + "2026-02-30T10:00:00Z", + "2026-04-31T10:00:00Z", + "March 2, 2026", + "03/02/2026", + "2026-13-01T00:00:00Z", + "2026-02-29T10:00:00Z", + ]) { + expect(validateStreamEvent({ ...base, at })).toEqual({ + ok: false, + error: "event.at must be an ISO timestamp string", + }); + } // Controls: real timestamps still validate — leap date, offset, // fractional seconds, date-only. expect(validateStreamEvent({ ...base, at: "2024-02-29T10:00:00Z" }).ok).toBe(true); @@ -203,6 +212,20 @@ describe("events store (#1739)", () => { expect(validateStreamEvent({ ...base, at: "2026-02-28" }).ok).toBe(true); }); + test("legacy event files with impossible or non-ISO timestamps are quarantined", async () => { + for (const at of ["2026-02-30T10:00:00Z", "2026-04-31T10:00:00Z", "March 2, 2026", "03/02/2026"]) { + const dir = mkdtempSync(join(tmpdir(), "agg-events-invalid-at-")); + const file = join(dir, "events.json"); + const original = JSON.stringify({ version: 1, events: [{ ...base, at }] }, null, 2); + writeFileSync(file, original); + + expect(await new EventsStore(file).list()).toEqual([]); + const quarantined = readdirSync(dir).find((name) => name.startsWith("events.json.corrupt-")); + expect(quarantined).toBeDefined(); + expect(readFileSync(join(dir, quarantined!), "utf8")).toBe(original); + } + }); + test("validateStreamEvent rejects malformed records", () => { expect(validateStreamEvent({ ...base, at: "2026-11-01T10:00:00.000Z" }).ok).toBe(true); expect(validateStreamEvent({ ...base, at: "not a date" }).ok).toBe(false);