GeezerVison is showing github.com — Merge pull request #3445 from developing-today/queue/5350-2493-billin… · developing-today/code@b09c9ce
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pm-stack/aggregator/src/sources/github-billing.ts +18-3Lines changed: 18 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | // gh call and the outbox append; tests feed literals. | 23 | 23 | import type { SnapshotRecord } from "../model.ts"; | 24 | 24 | import { dayTs } from "../snapshot-records.ts"; | | 25 | +import { isIsoDate, isNonNegative } from "../validate.ts"; | 25 | 26 |
| 26 | 27 | export interface BillingUsageItem { | 27 | 28 | date?: string; | @@ -74,14 +75,28 @@ function codespacesCoreMultiplier(sku: unknown): number { | 74 | 75 | return m ? Number(m[1]) : 1; | 75 | 76 | } | 76 | 77 |
| | 78 | +/** A numeric endpoint field contributes only when it is a finite, | | 79 | + * non-negative number (#2493): a negative or non-finite quantity or | | 80 | + * amount is not usage, and a negative derived record would pass | | 81 | + * isSnapshotRecord and be stored. Anything else contributes 0, | | 82 | + * extending the existing non-number fallback. */ | | 83 | +function usageNumber(v: unknown): number { | | 84 | + return isNonNegative(v) ? v : 0; | | 85 | +} | | 86 | + | 77 | 87 | /** Per-day aggregates from raw endpoint items, oldest day first. */ | 78 | 88 | export function aggregateBillingUsage(items: BillingUsageItem[]): BillingDayAggregate[] { | 79 | 89 | const byDate = new Map<string, BillingDayAggregate>(); | 80 | 90 | for (const item of items) { | 81 | 91 | if (typeof item.date !== "string" || item.date.length < 10) continue; | 82 | 92 | const date = item.date.slice(0, 10); | | 93 | + // The bucket date must be a real calendar date (#2493): an | | 94 | + // impossible date (month 13, Feb 30) derives snapshot records | | 95 | + // that fail isSnapshotRecord, and the collector throws on ANY | | 96 | + // invalid record — discarding the whole month's collection. | | 97 | + if (!isIsoDate(date)) continue; | 83 | 98 | const day = byDate.get(date) ?? emptyDay(date); | 84 | | - const qty = typeof item.quantity === "number" ? item.quantity : 0; | | 99 | + const qty = usageNumber(item.quantity); | 85 | 100 | // Classification is case-insensitive: the endpoint's documented | 86 | 101 | // response example spells product/unitType "Actions"/"minutes" | 87 | 102 | // while live rows arrive as "actions"/"Minutes" — both must | @@ -96,8 +111,8 @@ export function aggregateBillingUsage(items: BillingUsageItem[]): BillingDayAggr | 96 | 111 | else if (product === "codespaces" && unit === "gigabytehours") | 97 | 112 | day.codespacesStorageGbHours += qty; | 98 | 113 | // Gross/net span every product the endpoint reports, known or not. | 99 | | - if (typeof item.grossAmount === "number") day.grossUsd += item.grossAmount; | 100 | | - if (typeof item.netAmount === "number") day.netUsd += item.netAmount; | | 114 | + day.grossUsd += usageNumber(item.grossAmount); | | 115 | + day.netUsd += usageNumber(item.netAmount); | 101 | 116 | byDate.set(date, day); | 102 | 117 | } | 103 | 118 | return [...byDate.values()] | |
Collapse file Copy file name to clipboardExpand all lines: pm-stack/aggregator/test/github-billing.test.ts +39Lines changed: 39 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,6 +54,45 @@ describe("aggregateBillingUsage", () => { | 54 | 54 | }); | 55 | 55 | }); | 56 | 56 |
| | 57 | +describe("aggregateBillingUsage validation (#2493)", () => { | | 58 | + test("impossible dates never create a day bucket", () => { | | 59 | + const days = aggregateBillingUsage([ | | 60 | + { date: "2026-13-01", product: "actions", unitType: "Minutes", quantity: 10, grossAmount: 1, netAmount: 1 }, | | 61 | + { date: "2026-02-30", product: "actions", unitType: "Minutes", quantity: 10, grossAmount: 1, netAmount: 1 }, | | 62 | + { date: "2026-10-03", product: "actions", unitType: "Minutes", quantity: 7, grossAmount: 0.1, netAmount: 0.1 }, | | 63 | + ]); | | 64 | + expect(days.length).toBe(1); | | 65 | + expect(days[0].date).toBe("2026-10-03"); | | 66 | + expect(days[0].actionsMinutes).toBe(7); | | 67 | + }); | | 68 | + | | 69 | + test("negative or non-finite numbers contribute 0, never usage", () => { | | 70 | + const days = aggregateBillingUsage([ | | 71 | + { date: "2026-10-04", product: "actions", unitType: "Minutes", quantity: -50, grossAmount: -2, netAmount: -2 }, | | 72 | + { date: "2026-10-04", product: "actions", unitType: "Minutes", quantity: Number.NaN, grossAmount: Number.POSITIVE_INFINITY, netAmount: 0.5 }, | | 73 | + { date: "2026-10-04", product: "actions", unitType: "Minutes", quantity: 30, grossAmount: 0.25, netAmount: 0 }, | | 74 | + ]); | | 75 | + expect(days.length).toBe(1); | | 76 | + expect(days[0].actionsMinutes).toBe(30); | | 77 | + expect(days[0].grossUsd).toBe(0.25); | | 78 | + expect(days[0].netUsd).toBe(0.5); | | 79 | + }); | | 80 | + | | 81 | + test("records derived from hostile items all pass isSnapshotRecord", () => { | | 82 | + const records = recordsFromBillingUsage( | | 83 | + [ | | 84 | + { date: "2026-13-40", product: "actions", unitType: "Minutes", quantity: 10, grossAmount: 1, netAmount: 1 }, | | 85 | + { date: "2026-10-05", product: "actions", unitType: "Minutes", quantity: -10, grossAmount: -1, netAmount: -1 }, | | 86 | + { date: "2026-10-05", product: "codespaces", sku: "Codespaces 4-core", unitType: "Hours", quantity: 1.5, grossAmount: 0.4, netAmount: 0.4 }, | | 87 | + ], | | 88 | + "test", | | 89 | + ); | | 90 | + expect(records.length).toBeGreaterThan(0); | | 91 | + for (const r of records) expect(isSnapshotRecord(r)).toBe(true); | | 92 | + expect(records.every((r) => r.value > 0)).toBe(true); | | 93 | + }); | | 94 | +}); | | 95 | + | 57 | 96 | describe("recordsFromBillingUsage", () => { | 58 | 97 | const records = recordsFromBillingUsage(ITEMS, "test"); | 59 | 98 |
| |
|