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: 7 additions & 2 deletions pm-stack/aggregator/src/quota-gather-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// its raw usage on the entry and yields no windows (never zeros).
import type { QuotaCurrentEntry, QuotaCurrentWindow, SnapshotRecord } from "./model.ts";
import { safeProviderReason } from "./credential-lifecycle.ts";
import { isIsoDate } from "./validate.ts";

/** The subset of a quota-module result the mapper reads. */
export interface QuotaProviderResult {
Expand Down Expand Up @@ -628,8 +629,12 @@ export function entryFromTurso(
const periodStart =
sub?.current_billing_period_start ?? sub?.current_period_start ?? sub?.period_start ?? sub?.starts_at;
const resetsAt = normalizeResetAt(periodEnd);
const startMs = typeof periodStart === "string" ? Date.parse(periodStart) : Number.NaN;
const endMs = typeof periodEnd === "string" ? Date.parse(periodEnd) : Number.NaN;
// Gate both stamps with isIsoDate before Date.parse (#5091): Date.parse
// rolls impossible dates (2026-02-30 -> Mar 2) and parses non-ISO strings
// timezone-dependently, so a malformed period would silently publish a
// window length days off. A present-but-invalid stamp yields null.
const startMs = isIsoDate(periodStart) ? Date.parse(periodStart) : Number.NaN;
const endMs = isIsoDate(periodEnd) ? Date.parse(periodEnd) : Number.NaN;
const windowSeconds =
Number.isFinite(startMs) && Number.isFinite(endMs) && endMs > startMs ? Math.round((endMs - startMs) / 1000) : null;

Expand Down
34 changes: 34 additions & 0 deletions pm-stack/aggregator/test/quota-gather-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,40 @@ describe("entryFromTurso (#1049)", () => {
});
});

describe("entryFromTurso period stamps (#5091)", () => {
const usageDoc = { organization: { usage: { databases: 3 } } };
const at = "2026-10-29T02:30:00.000Z";
const subWith = (start: unknown, end: unknown) => ({
subscription: {
plan: "starter",
current_billing_period_start: start,
current_billing_period_end: end,
},
});
const windowSecondsOf = (sub: unknown): number | null => {
const entry = entryFromTurso(usageDoc, sub as never, { measuredAt: at });
expect(entry.state).toBe("ok");
expect(entry.windows.length).toBeGreaterThan(0);
return entry.windows[0].windowSeconds;
};
test("impossible period start yields a null window, not a rolled-over length", () => {
// 2026-02-30 does not exist; raw Date.parse rolls it to Mar 2 and would
// publish a 28-day window against the 2026-03-30 end.
expect(windowSecondsOf(subWith("2026-02-30T00:00:00Z", "2026-03-30T00:00:00Z"))).toBeNull();
});
test("impossible period end yields a null window", () => {
expect(windowSecondsOf(subWith("2026-02-01T00:00:00Z", "2026-02-30T00:00:00Z"))).toBeNull();
});
test("non-ISO period stamps yield a null window", () => {
// Raw Date.parse accepts these (timezone-dependently); the gate refuses.
expect(windowSecondsOf(subWith("February 1, 2026", "March 3, 2026"))).toBeNull();
});
test("valid ISO period stamps still compute the window", () => {
// 2026-02-01 -> 2026-03-03 is exactly 30 days.
expect(windowSecondsOf(subWith("2026-02-01T00:00:00Z", "2026-03-03T00:00:00Z"))).toBe(2_592_000);
});
});

describe("entryFromTurso negative usage (#2553)", () => {
const subDoc = {
subscription: {
Expand Down