| 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 | marginOnPrice, |
| 10 | marginPercent, |
| 11 | marginTone, |
| 12 | parseBucket, |
| 13 | parseCostSettings, |
| 14 | parseMapping, |
| 15 | parseRange, |
| 16 | percentLabel, |
| 17 | proposalOutcome, |
| 18 | spendBanner, |
| 19 | spendRows, |
| 20 | subscriptionsOver, |
| 21 | unitDollars, |
| 22 | versionCells, |
| 23 | whoPaid, |
| 24 | } from "./costs.ts"; |
| 25 | |
| 26 | const day = (d: string, bucket: string, cf: number, own: number, value: number, cash: number): CostDay => ({ |
| 27 | day: d, |
| 28 | bucket, |
| 29 | cfCostMicros: cf, |
| 30 | ownCostMicros: own, |
| 31 | valueMicros: value, |
| 32 | cashMicros: cash, |
| 33 | }); |
| 34 | |
| 35 | test("every day of the range is there, even an empty one", () => { |
| 36 | assert.deepEqual(daysBetween("2026-09-29", "2026-10-02"), ["2026-09-29", "2026-09-30", "2026-10-01", "2026-10-02"]); |
| 37 | assert.deepEqual(daysBetween("2026-10-02", "2026-10-01"), []); |
| 38 | }); |
| 39 | |
| 40 | test("all of g1t is money in against every cost; a product is its value against its cost", () => { |
| 41 | const rows = [ |
| 42 | day("2026-10-01", "sandboxes", 2_000_000, 1_900_000, 2_400_000, 1_000_000), |
| 43 | day("2026-10-01", "platform", 500_000, 0, 20_000_000, 20_000_000), |
| 44 | // Models: no Cloudflare bill, g1t's own cost. |
| 45 | day("2026-10-01", "models", 0, 100_000, 120_000, 120_000), |
| 46 | ]; |
| 47 | const all = daySeries(rows, "2026-10-01", "2026-10-02", null); |
| 48 | assert.deepEqual(all, [ |
| 49 | { day: "2026-10-01", revenueMicros: 21_120_000, costMicros: 2_600_000 }, |
| 50 | { day: "2026-10-02", revenueMicros: 0, costMicros: 0 }, |
| 51 | ]); |
| 52 | const sandboxes = daySeries(rows, "2026-10-01", "2026-10-01", "sandboxes"); |
| 53 | assert.deepEqual(sandboxes, [{ day: "2026-10-01", revenueMicros: 2_400_000, costMicros: 2_000_000 }]); |
| 54 | }); |
| 55 | |
| 56 | test("margins read against the floor", () => { |
| 57 | assert.equal(marginPercent(1_200_000, 1_000_000)?.toFixed(2), "16.67"); |
| 58 | assert.equal(marginPercent(0, 10), null); |
| 59 | assert.equal(marginTone(-3, 10), "danger"); |
| 60 | assert.equal(marginTone(12, 10), "warn"); |
| 61 | assert.equal(marginTone(16.7, 10), "mint"); |
| 62 | assert.equal(marginTone(null, 10), undefined); |
| 63 | assert.equal(percentLabel(-4), "−4.0%"); |
| 64 | assert.equal(percentLabel(12.34, { signed: true }), "+12.3%"); |
| 65 | assert.equal(percentLabel(null), "—"); |
| 66 | }); |
| 67 | |
| 68 | test("unit costs keep the places they need", () => { |
| 69 | assert.equal(unitDollars(150_000), "$0.150"); |
| 70 | assert.equal(unitDollars(16.44), "$0.0000164"); |
| 71 | assert.equal(unitDollars(2_500_000), "$2.50"); |
| 72 | assert.equal(unitDollars(0), "$0"); |
| 73 | }); |
| 74 | |
| 75 | test("the range and product come from the address, within bounds", () => { |
| 76 | assert.equal(parseRange("7"), 7); |
| 77 | assert.equal(parseRange("365"), 30); |
| 78 | assert.equal(parseRange(null), 30); |
| 79 | assert.equal(parseBucket("git", ["git", "sandboxes"]), "git"); |
| 80 | assert.equal(parseBucket("nope", ["git"]), null); |
| 81 | }); |
| 82 | |
| 83 | const form = (entries: Record<string, string>) => { |
| 84 | const data = new FormData(); |
| 85 | for (const [key, value] of Object.entries(entries)) data.set(key, value); |
| 86 | return data; |
| 87 | }; |
| 88 | |
| 89 | test("the guardrails form is checked", () => { |
| 90 | const good = parseCostSettings( |
| 91 | form({ |
| 92 | autoApply: "on", |
| 93 | autoApplyPercent: "25", |
| 94 | noticeDays: "14", |
| 95 | marginFloorPercent: "10", |
| 96 | alertDays: "3", |
| 97 | anomalyFactor: "1", |
| 98 | minDailyCost: "0.10", |
| 99 | anomalyFloor: "1", |
| 100 | }), |
| 101 | ); |
| 102 | assert.ok(good.ok); |
| 103 | assert.equal(good.ok && good.value.minDailyCostMicros, 100_000); |
| 104 | assert.equal(good.ok && good.value.autoApply, true); |
| 105 | const off = parseCostSettings(form({ autoApplyPercent: "25", noticeDays: "14", marginFloorPercent: "10", alertDays: "3", anomalyFactor: "1", minDailyCost: "0", anomalyFloor: "1" })); |
| 106 | assert.ok(off.ok && !off.value.autoApply); |
| 107 | assert.equal(parseCostSettings(form({ autoApplyPercent: "250" })).ok, false); |
| 108 | assert.equal(parseCostSettings(form({ autoApplyPercent: "25", noticeDays: "1.5" })).ok, false); |
| 109 | }); |
| 110 | |
| 111 | test("a mapping names Cloudflare's product and meter and one of g1t's products", () => { |
| 112 | const mapped = parseMapping(form({ product: "Artifacts", meter: "", bucket: "git", priceMeter: "git_operations", ownMeter: "git_operations", scaleToOwn: "on", driftPercent: "15" })); |
| 113 | assert.deepEqual(mapped, { |
| 114 | ok: true, |
| 115 | value: { product: "artifacts", meter: "*", bucket: "git", priceMeter: "git_operations", ownMeter: "git_operations", scaleToOwn: true, driftPercent: 15, note: "" }, |
| 116 | }); |
| 117 | assert.deepEqual(parseMapping(form({ product: "r2", meter: "class_a", remove: "1" })), { ok: true, value: { product: "r2", meter: "class_a", remove: true } }); |
| 118 | assert.equal(parseMapping(form({ product: "r2", meter: "x y", bucket: "git" })).ok, false); |
| 119 | assert.equal(parseMapping(form({ product: "r2", meter: "*", bucket: "" })).ok, false); |
| 120 | assert.equal(parseMapping(form({ product: "r2", meter: "*", bucket: "git", driftPercent: "-1" })).ok, false); |
| 121 | }); |
| 122 | |
| 123 | const caps = (over: Partial<SpendCaps> = {}): SpendCaps => ({ |
| 124 | day: "2026-10-06", |
| 125 | month: "2026-10", |
| 126 | todayMicros: 20_000_000, |
| 127 | dailyCapMicros: 75_000_000, |
| 128 | tripped: false, |
| 129 | trippedAt: null, |
| 130 | liftedBy: null, |
| 131 | liftedAt: null, |
| 132 | liftNote: null, |
| 133 | monthBuckets: [ |
| 134 | { bucket: "comped", title: "Comped (g1t's own)", micros: 40_000_000 }, |
| 135 | { bucket: "trial", title: "Trial pool", micros: 5_000_000 }, |
| 136 | ], |
| 137 | comped: [{ account: "ws_flagon-io", name: "flagon-io", usedMicros: 40_000_000, ceilingMicros: 150_000_000, defaultCeiling: true, level: 0 }], |
| 138 | freeTierMicros: 1_000_000, |
| 139 | fixedMonthlyMicros: 30_000_000, |
| 140 | revenueMicros: 0, |
| 141 | ...over, |
| 142 | }); |
| 143 | |
| 144 | test("the spend bar shows only when a cap stops work", () => { |
| 145 | assert.equal(spendBanner(caps()), null); |
| 146 | assert.ok((spendBanner(caps({ tripped: true, todayMicros: 80_000_000 })) ?? "").startsWith("The daily breaker is open ($80.00 of $75.00 today)")); |
| 147 | const usedUp = caps({ comped: [{ account: "ws_flagon-io", name: "flagon-io", usedMicros: 150_000_000, ceilingMicros: 150_000_000, defaultCeiling: true, level: 100 }] }); |
| 148 | assert.ok((spendBanner(usedUp) ?? "").startsWith("Flagon-io used its $150.00 monthly budget")); |
| 149 | // No budget set to zero is ever "used up". |
| 150 | assert.equal(spendBanner(caps({ comped: [{ account: "a", name: "a", usedMicros: 9, ceilingMicros: 0, defaultCeiling: false, level: 0 }] })), null); |
| 151 | }); |
| 152 | |
| 153 | test("what g1t paid this month adds every bucket, the free tier and subscriptions", () => { |
| 154 | const { rows, totalMicros } = spendRows(caps()); |
| 155 | assert.deepEqual(rows.map((r) => r.key), ["comped", "trial", "free", "fixed"]); |
| 156 | assert.equal(totalMicros, 76_000_000); |
| 157 | }); |
| 158 | |
| 159 | test("charged without real money is what it cost g1t, never its price", () => { |
| 160 | const { rows } = spendRows(caps({ monthBuckets: [{ bucket: "unpaid", title: "Charged without real money", micros: 7_610_000 }] })); |
| 161 | assert.match(rows[0]!.note, /what it cost g1t, not what was charged/); |
| 162 | }); |
| 163 | |
| 164 | test("margins are a share of the price: cost plus 20% is 16.7%", () => { |
| 165 | assert.equal(percentLabel(marginOnPrice(20)), "16.7%"); |
| 166 | assert.equal(marginOnPrice(0), 0); |
| 167 | // As By product showed it: $2.99 charged for models that cost $2.49. |
| 168 | assert.equal(percentLabel(marginPercent(2_988_000, 2_490_000)), "16.7%"); |
| 169 | }); |
| 170 | |
| 171 | test("who g1t paid is the statement's all-in cost, subscriptions included", () => { |
| 172 | const subscriptions = subscriptionsOver(30_000_000, 30); |
| 173 | const paid = whoPaid({ costMicros: 2_490_000, cloudflareCostMicros: 0, modelsCostMicros: 2_490_000 }, subscriptions); |
| 174 | assert.deepEqual(paid, { totalMicros: 32_490_000, cloudflareMicros: 0, subscriptionsMicros: 30_000_000, modelsMicros: 2_490_000 }); |
| 175 | assert.equal(subscriptionsOver(30_000_000, 7), 7_000_000); |
| 176 | // From older billing, Cloudflare's part is the rest. |
| 177 | assert.equal(whoPaid({ costMicros: 3_000_000, modelsCostMicros: 1_000_000 }, 0).cloudflareMicros, 2_000_000); |
| 178 | }); |
| 179 | |
| 180 | test("a rate g1t sets shows no cost, and a weight is no money", () => { |
| 181 | assert.deepEqual(versionCells({ costMicros: 250_000, priceMicros: 250_000, basis: "rate" }), { |
| 182 | cost: "—", |
| 183 | price: "$0.250", |
| 184 | note: "g1t's own rate: no cost behind it", |
| 185 | }); |
| 186 | assert.equal(versionCells({ costMicros: 100_000, priceMicros: 100_000, basis: "weight" }).price, "×0.1"); |
| 187 | assert.deepEqual(versionCells({ costMicros: 16.44, priceMicros: 19.73, basis: "cost" }), { cost: unitDollars(16.44), price: unitDollars(19.73), note: null }); |
| 188 | // Older billing sends no basis: a cost. |
| 189 | assert.equal(versionCells({ costMicros: 1_000_000, priceMicros: 1_200_000 }).cost, "$1.00"); |
| 190 | }); |
| 191 | |
| 192 | test("a rise replaced before its date says so, rather than missing its date", () => { |
| 193 | assert.equal( |
| 194 | proposalOutcome({ status: "superseded", decidedBy: "guardrail", effectiveAt: null }), |
| 195 | "applied by guardrail, then replaced by a later measurement before it took effect; nothing was charged at it", |
| 196 | ); |
| 197 | // From before billing marked them superseded. |
| 198 | assert.match(proposalOutcome({ status: "applied", decidedBy: "guardrail", effectiveAt: null }) ?? "", /replaced by a later measurement/); |
| 199 | assert.equal(proposalOutcome({ status: "applied", decidedBy: "guardrail", effectiveAt: "2026-10-22T04:18:12.571Z" }), "applied by guardrail"); |
| 200 | assert.equal(proposalOutcome({ status: "superseded", decidedBy: null, effectiveAt: null }), "replaced by a later measurement"); |
| 201 | assert.equal(proposalOutcome({ status: "open", decidedBy: null, effectiveAt: null }), null); |
| 202 | }); |