Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions pm-stack/aggregator/src/quota-gather-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions pm-stack/aggregator/test/quota-gather-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <model>:<window>", () => {
const entry = entryFromResult(
{
Expand Down
Loading