| 1 | import assert from "node:assert/strict"; |
| 2 | import test from "node:test"; |
| 3 | import { deliveryDays, deliveryTotals, duration, projectCosts, usageKey } from "./workspace-insights.ts"; |
| 4 | import type { DeliveryMetrics, UsageReport } from "@g1t/contracts"; |
| 5 | |
| 6 | test("workspace averages weight samples rather than averaging project averages", () => { |
| 7 | const empty: DeliveryMetrics = { |
| 8 | repoId: "a", |
| 9 | opened: 0, |
| 10 | merged: 0, |
| 11 | active: 0, |
| 12 | unreviewed: 0, |
| 13 | checking: 0, |
| 14 | failed: 0, |
| 15 | queued: 0, |
| 16 | leadSeconds: 0, |
| 17 | reviewSeconds: 0, |
| 18 | reviewed: 0, |
| 19 | }; |
| 20 | const total = deliveryTotals([ |
| 21 | { ...empty, merged: 1, leadSeconds: 100 }, |
| 22 | { ...empty, repoId: "b", merged: 9, leadSeconds: 9000 }, |
| 23 | ]); |
| 24 | assert.equal(total.leadSeconds / total.merged, 910); |
| 25 | assert.equal(deliveryTotals([]).merged, 0); |
| 26 | }); |
| 27 | |
| 28 | test("costs combine meters and products without conflating project keys", () => { |
| 29 | const report = { |
| 30 | products: [ |
| 31 | { |
| 32 | meters: [ |
| 33 | { |
| 34 | byProject: [ |
| 35 | { project: "a/one", micros: 3 }, |
| 36 | { project: "a/two", micros: 7 }, |
| 37 | ], |
| 38 | }, |
| 39 | ], |
| 40 | }, |
| 41 | { meters: [{ byProject: [{ project: "a/one", micros: 11 }] }] }, |
| 42 | ], |
| 43 | } as UsageReport; |
| 44 | assert.deepEqual( |
| 45 | [...projectCosts(report)], |
| 46 | [ |
| 47 | ["a/one", 14], |
| 48 | ["a/two", 7], |
| 49 | ], |
| 50 | ); |
| 51 | assert.equal(projectCosts(null).size, 0); |
| 52 | }); |
| 53 | |
| 54 | test("a project's usage is its repository's, not its slug's", () => { |
| 55 | assert.equal(usageKey({ namespace: "Acme", name: "Web-App" }), "acme/web-app"); |
| 56 | }); |
| 57 | |
| 58 | test("missing duration is distinct from a fast change", () => { |
| 59 | assert.equal(duration(null), "—"); |
| 60 | assert.equal(duration(0), "<1m"); |
| 61 | assert.equal(duration(7200), "2.0h"); |
| 62 | assert.equal(duration(172800), "2.0d"); |
| 63 | }); |
| 64 | |
| 65 | test("daily series combines batches and retains quiet days across month boundaries", () => { |
| 66 | assert.deepEqual( |
| 67 | deliveryDays("2026-09-30", "2026-10-02", [ |
| 68 | { day: "2026-10-01", merged: 2 }, |
| 69 | { day: "2026-10-01", merged: 3 }, |
| 70 | ]), |
| 71 | [ |
| 72 | { day: "2026-09-30", merged: 0 }, |
| 73 | { day: "2026-10-01", merged: 5 }, |
| 74 | { day: "2026-10-02", merged: 0 }, |
| 75 | ], |
| 76 | ); |
| 77 | }); |