| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import type { CostDay, SpendCaps } from "@g1t/contracts"; |
| 5 | |
| 6 | import { |
| 7 | daySeries, |
| 8 | daysBetween, |
| 9 | marginPercent, |
| 10 | marginTone, |
| 11 | parseBucket, |
| 12 | parseCostSettings, |
| 13 | parseMapping, |
| 14 | parseRange, |
| 15 | percentLabel, |
| 16 | spendBanner, |
| 17 | spendRows, |
| 18 | unitDollars, |
| 19 | } from "./costs.ts"; |
| 20 | |
| 21 | const day = (d: string, bucket: string, cf: number, own: number, value: number, cash: number): CostDay => ({ |
| 22 | day: d, |
| 23 | bucket, |
| 24 | cfCostMicros: cf, |
| 25 | ownCostMicros: own, |
| 26 | valueMicros: value, |
| 27 | cashMicros: cash, |
| 28 | }); |
| 29 | |
| 30 | test("every day of the range is there, even an empty one", () => { |
| 31 | assert.deepEqual(daysBetween("2026-09-29", "2026-10-02"), ["2026-09-29", "2026-09-30", "2026-10-01", "2026-10-02"]); |
| 32 | assert.deepEqual(daysBetween("2026-10-02", "2026-10-01"), []); |
| 33 | }); |
| 34 | |
| 35 | test("all of g1t is money in against every cost; a product is its value against its cost", () => { |
| 36 | const rows = [ |
| 37 | day("2026-10-01", "sandboxes", 2_000_000, 1_900_000, 2_400_000, 1_000_000), |
| 38 | day("2026-10-01", "platform", 500_000, 0, 20_000_000, 20_000_000), |
| 39 | // Models: no Cloudflare bill, g1t's own cost. |
| 40 | day("2026-10-01", "models", 0, 100_000, 120_000, 120_000), |
| 41 | ]; |
| 42 | const all = daySeries(rows, "2026-10-01", "2026-10-02", null); |
| 43 | assert.deepEqual(all, [ |
| 44 | { day: "2026-10-01", revenueMicros: 21_120_000, costMicros: 2_600_000 }, |
| 45 | { day: "2026-10-02", revenueMicros: 0, costMicros: 0 }, |
| 46 | ]); |
| 47 | const sandboxes = daySeries(rows, "2026-10-01", "2026-10-01", "sandboxes"); |
| 48 | assert.deepEqual(sandboxes, [{ day: "2026-10-01", revenueMicros: 2_400_000, costMicros: 2_000_000 }]); |
| 49 | }); |
| 50 | |
| 51 | test("margins read against the floor", () => { |
| 52 | assert.equal(marginPercent(1_200_000, 1_000_000)?.toFixed(2), "16.67"); |
| 53 | assert.equal(marginPercent(0, 10), null); |
| 54 | assert.equal(marginTone(-3, 10), "danger"); |
| 55 | assert.equal(marginTone(12, 10), "warn"); |
| 56 | assert.equal(marginTone(16.7, 10), "mint"); |
| 57 | assert.equal(marginTone(null, 10), undefined); |
| 58 | assert.equal(percentLabel(-4), "−4.0%"); |
| 59 | assert.equal(percentLabel(12.34, { signed: true }), "+12.3%"); |
| 60 | assert.equal(percentLabel(null), "—"); |
| 61 | }); |
| 62 | |
| 63 | test("unit costs keep the places they need", () => { |
| 64 | assert.equal(unitDollars(150_000), "$0.150"); |
| 65 | assert.equal(unitDollars(16.44), "$0.0000164"); |
| 66 | assert.equal(unitDollars(2_500_000), "$2.50"); |
| 67 | assert.equal(unitDollars(0), "$0"); |
| 68 | }); |
| 69 | |
| 70 | test("the range and product come from the address, within bounds", () => { |
| 71 | assert.equal(parseRange("7"), 7); |
| 72 | assert.equal(parseRange("365"), 30); |
| 73 | assert.equal(parseRange(null), 30); |
| 74 | assert.equal(parseBucket("git", ["git", "sandboxes"]), "git"); |
| 75 | assert.equal(parseBucket("nope", ["git"]), null); |
| 76 | }); |
| 77 | |
| 78 | const form = (entries: Record<string, string>) => { |
| 79 | const data = new FormData(); |
| 80 | for (const [key, value] of Object.entries(entries)) data.set(key, value); |
| 81 | return data; |
| 82 | }; |
| 83 | |
| 84 | test("the guardrails form is checked", () => { |
| 85 | const good = parseCostSettings( |
| 86 | form({ |
| 87 | autoApply: "on", |
| 88 | autoApplyPercent: "25", |
| 89 | noticeDays: "14", |
| 90 | marginFloorPercent: "10", |
| 91 | alertDays: "3", |
| 92 | anomalyFactor: "1", |
| 93 | minDailyCost: "0.10", |
| 94 | anomalyFloor: "1", |
| 95 | }), |
| 96 | ); |
| 97 | assert.ok(good.ok); |
| 98 | assert.equal(good.ok && good.value.minDailyCostMicros, 100_000); |
| 99 | assert.equal(good.ok && good.value.autoApply, true); |
| 100 | const off = parseCostSettings(form({ autoApplyPercent: "25", noticeDays: "14", marginFloorPercent: "10", alertDays: "3", anomalyFactor: "1", minDailyCost: "0", anomalyFloor: "1" })); |
| 101 | assert.ok(off.ok && !off.value.autoApply); |
| 102 | assert.equal(parseCostSettings(form({ autoApplyPercent: "250" })).ok, false); |
| 103 | assert.equal(parseCostSettings(form({ autoApplyPercent: "25", noticeDays: "1.5" })).ok, false); |
| 104 | }); |
| 105 | |
| 106 | test("a mapping names Cloudflare's product and meter and one of g1t's products", () => { |
| 107 | const mapped = parseMapping(form({ product: "Artifacts", meter: "", bucket: "git", priceMeter: "git_operations", ownMeter: "git_operations", scaleToOwn: "on", driftPercent: "15" })); |
| 108 | assert.deepEqual(mapped, { |
| 109 | ok: true, |
| 110 | value: { product: "artifacts", meter: "*", bucket: "git", priceMeter: "git_operations", ownMeter: "git_operations", scaleToOwn: true, driftPercent: 15, note: "" }, |
| 111 | }); |
| 112 | assert.deepEqual(parseMapping(form({ product: "r2", meter: "class_a", remove: "1" })), { ok: true, value: { product: "r2", meter: "class_a", remove: true } }); |
| 113 | assert.equal(parseMapping(form({ product: "r2", meter: "x y", bucket: "git" })).ok, false); |
| 114 | assert.equal(parseMapping(form({ product: "r2", meter: "*", bucket: "" })).ok, false); |
| 115 | assert.equal(parseMapping(form({ product: "r2", meter: "*", bucket: "git", driftPercent: "-1" })).ok, false); |
| 116 | }); |
| 117 | |
| 118 | const caps = (over: Partial<SpendCaps> = {}): SpendCaps => ({ |
| 119 | day: "2026-10-06", |
| 120 | month: "2026-10", |
| 121 | todayMicros: 20_000_000, |
| 122 | dailyCapMicros: 75_000_000, |
| 123 | tripped: false, |
| 124 | trippedAt: null, |
| 125 | liftedBy: null, |
| 126 | liftedAt: null, |
| 127 | liftNote: null, |
| 128 | monthBuckets: [ |
| 129 | { bucket: "comped", title: "Comped (g1t's own)", micros: 40_000_000 }, |
| 130 | { bucket: "trial", title: "Trial pool", micros: 5_000_000 }, |
| 131 | ], |
| 132 | comped: [{ account: "ws_flagon-io", name: "flagon-io", usedMicros: 40_000_000, ceilingMicros: 150_000_000, defaultCeiling: true, level: 0 }], |
| 133 | freeTierMicros: 1_000_000, |
| 134 | fixedMonthlyMicros: 30_000_000, |
| 135 | revenueMicros: 0, |
| 136 | ...over, |
| 137 | }); |
| 138 | |
| 139 | test("the spend bar shows only when a cap stops work", () => { |
| 140 | assert.equal(spendBanner(caps()), null); |
| 141 | assert.ok((spendBanner(caps({ tripped: true, todayMicros: 80_000_000 })) ?? "").startsWith("The daily breaker is open ($80.00 of $75.00 today)")); |
| 142 | const usedUp = caps({ comped: [{ account: "ws_flagon-io", name: "flagon-io", usedMicros: 150_000_000, ceilingMicros: 150_000_000, defaultCeiling: true, level: 100 }] }); |
| 143 | assert.ok((spendBanner(usedUp) ?? "").startsWith("Flagon-io used its $150.00 monthly budget")); |
| 144 | // No budget set to zero is ever "used up". |
| 145 | assert.equal(spendBanner(caps({ comped: [{ account: "a", name: "a", usedMicros: 9, ceilingMicros: 0, defaultCeiling: false, level: 0 }] })), null); |
| 146 | }); |
| 147 | |
| 148 | test("what g1t paid this month adds every bucket, the free tier and subscriptions", () => { |
| 149 | const { rows, totalMicros } = spendRows(caps()); |
| 150 | assert.deepEqual(rows.map((r) => r.key), ["comped", "trial", "free", "fixed"]); |
| 151 | assert.equal(totalMicros, 76_000_000); |
| 152 | }); |