From 291bdb6fb038c15ad589cfa79d540ec62ad9f795 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 21:08:19 -0600 Subject: [PATCH] fix(aggregator): guard invalid provider fetchedAt --- pm-stack/aggregator/src/quota-gather-map.ts | 9 ++++---- .../aggregator/test/quota-gather-map.test.ts | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/pm-stack/aggregator/src/quota-gather-map.ts b/pm-stack/aggregator/src/quota-gather-map.ts index 5123b62f7a..42700f8642 100644 --- a/pm-stack/aggregator/src/quota-gather-map.ts +++ b/pm-stack/aggregator/src/quota-gather-map.ts @@ -175,10 +175,11 @@ export function entryFromFileExport( /** Map one provider result onto a quota-current entry. */ export function entryFromResult(result: QuotaProviderResult, tickIso: string): QuotaCurrentEntry { - const measuredAt = - typeof result.fetchedAt === "number" && Number.isFinite(result.fetchedAt) - ? new Date(result.fetchedAt).toISOString() - : tickIso; + let measuredAt = tickIso; + if (typeof result.fetchedAt === "number" && Number.isFinite(result.fetchedAt) && result.fetchedAt > 0) { + const fetchedDate = new Date(result.fetchedAt); + if (Number.isFinite(fetchedDate.getTime())) measuredAt = fetchedDate.toISOString(); + } const state: QuotaCurrentEntry["state"] = result.ok ? "ok" : result.configured ? "error" : "unconfigured"; const entry: QuotaCurrentEntry = { providerId: result.providerId, diff --git a/pm-stack/aggregator/test/quota-gather-map.test.ts b/pm-stack/aggregator/test/quota-gather-map.test.ts index 62f8fb5ad2..24a44ac882 100644 --- a/pm-stack/aggregator/test/quota-gather-map.test.ts +++ b/pm-stack/aggregator/test/quota-gather-map.test.ts @@ -69,6 +69,29 @@ describe("entryFromResult", () => { expect(balance?.pctOfLimit).toBeNull(); expect(balance?.valueLabel).toBe("12.50"); }); + test("falls back for invalid fetchedAt values without dropping a mixed batch", () => { + const tickIso = "2026-11-02T03:00:00.000Z"; + const fetchedAts = [1e16, -1e16, 0, -1, NaN, Infinity, -Infinity, undefined, 1793075143879]; + const results = fetchedAts.map((fetchedAt) => ({ ...OK_RESULT, fetchedAt })); + const entries = results.map((result) => entryFromResult(result, tickIso)); + + expect(entries).toHaveLength(results.length); + expect(entries.map((entry) => entry.measuredAt)).toEqual([ + tickIso, + tickIso, + tickIso, + tickIso, + tickIso, + tickIso, + tickIso, + tickIso, + new Date(1793075143879).toISOString(), + ]); + for (const entry of entries) { + expect(entry.windows).toHaveLength(2); + expect(entry.windows.every((window) => window.measuredAt === entry.measuredAt)).toBe(true); + } + }); test("per-model usage flattens to :", () => { const entry = entryFromResult( {