Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions pm-stack/aggregator/test/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Loading