diff --git a/pm-stack/aggregator/src/saas-extract.ts b/pm-stack/aggregator/src/saas-extract.ts index c647280d1..82b32d9b5 100644 --- a/pm-stack/aggregator/src/saas-extract.ts +++ b/pm-stack/aggregator/src/saas-extract.ts @@ -884,13 +884,23 @@ export function applySupabaseExport( nowMs: number = Date.now(), ): SaasService[] { if (!doc || (doc.status !== "ok" && doc.status !== "partial")) return services; - if (!doc.measuredAt || !Array.isArray(doc.metrics)) return services; + if (typeof doc.measuredAt !== "string" || !doc.measuredAt || !Array.isArray(doc.metrics)) return services; const measuredMs = Date.parse(doc.measuredAt); if (!Number.isFinite(measuredMs)) return services; if (nowMs - measuredMs > SUPABASE_EXPORT_STALE_MS || measuredMs > nowMs + 5 * 60 * 1000) return services; const supabase = services.find((s) => s.id === "supabase"); if (!supabase) return services; - const byId = new Map(doc.metrics.filter((m) => m && typeof m.id === "string").map((m) => [m.id, m])); + const byId = new Map( + doc.metrics + .filter( + (m) => + m && + typeof m.id === "string" && + !(typeof m.used === "number" && m.used < 0) && + !(typeof m.limit === "number" && m.limit < 0), + ) + .map((m) => [m.id, m]), + ); if (!byId.size) return services; supabase.metrics = supabase.metrics.map((m) => { const live = byId.get(m.id); @@ -923,7 +933,8 @@ export function applySupabaseExport( doc.billing && doc.billing.measured !== false && typeof doc.billing.billedUsd === "number" && - Number.isFinite(doc.billing.billedUsd) + Number.isFinite(doc.billing.billedUsd) && + doc.billing.billedUsd >= 0 ) { supabase.billedUsd = doc.billing.billedUsd; } else { diff --git a/pm-stack/aggregator/test/supabase-export.test.ts b/pm-stack/aggregator/test/supabase-export.test.ts index d29975175..e3007992b 100644 --- a/pm-stack/aggregator/test/supabase-export.test.ts +++ b/pm-stack/aggregator/test/supabase-export.test.ts @@ -96,6 +96,56 @@ describe("applySupabaseExport", () => { expect(supabaseCard(unmeasured).billedUsd).toBeNull(); }); + test("negative billing is unknown without discarding valid metric measurements", () => { + const out = applySupabaseExport( + extractSaasServices(VIEW), + freshDoc({ billing: { plan: "free", billedUsd: -42.5, measured: true } }), + NOW, + ); + expect(supabaseCard(out).billedUsd).toBeNull(); + expect(supabaseCard(out).sourceClass).toBe("api"); + expect(metricOf(out, "db-size").used).toBe(1.154); + }); + + test("a non-string measuredAt is a no-op before Date.parse coercion", () => { + const base = extractSaasServices(VIEW); + const expected = structuredClone(base); + const out = applySupabaseExport( + base, + freshDoc({ measuredAt: ["2026-10-29T14:55:00Z"] as unknown as string }), + NOW, + ); + expect(out).toEqual(expected); + }); + + test("a negative used value leaves that metric at its recorded fallback", () => { + const base = extractSaasServices(VIEW); + const recordedDb = structuredClone(metricOf(base, "db-size")); + const out = applySupabaseExport( + base, + freshDoc({ + metrics: [ + { id: "db-size", used: -3.25, limit: 0.5 }, + { id: "storage", used: 0.25, limit: 1, unit: "gb" }, + ], + }), + NOW, + ); + expect(metricOf(out, "db-size")).toEqual(recordedDb); + expect(metricOf(out, "storage").used).toBe(0.25); + }); + + test("a negative limit leaves that metric at its recorded fallback", () => { + const base = extractSaasServices(VIEW); + const recordedDb = structuredClone(metricOf(base, "db-size")); + const out = applySupabaseExport( + base, + freshDoc({ metrics: [{ id: "db-size", used: 0.25, limit: -0.5 }] }), + NOW, + ); + expect(metricOf(out, "db-size")).toEqual(recordedDb); + }); + test("a stale export falls back to exactly today's card", () => { const base = extractSaasServices(VIEW); const out = applySupabaseExport(base, freshDoc({ measuredAt: "2026-10-29T12:00:00Z" }), NOW);