| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import type { CloudflareCycle, CostDay, SpendCaps } from "@g1t/contracts"; |
| 5 | |
| 6 | import { |
| 7 | basisLabel, |
| 8 | billReadNote, |
| 9 | cycleHeadline, |
| 10 | daySeries, |
| 11 | daysBetween, |
| 12 | givenParts, |
| 13 | marginOnPrice, |
| 14 | marginPercent, |
| 15 | marginTone, |
| 16 | parseBucket, |
| 17 | parseCostSettings, |
| 18 | parseMapping, |
| 19 | parseRange, |
| 20 | percentLabel, |
| 21 | proposalOutcome, |
| 22 | spendBanner, |
| 23 | spendRows, |
| 24 | subscriptionsOver, |
| 25 | unitDollars, |
| 26 | unpricedMeters, |
| 27 | versionCells, |
| 28 | whoPaid, |
| 29 | } from "./costs.ts"; |
| 30 | |
| 31 | const day = (d: string, bucket: string, cf: number, own: number, value: number, cash: number): CostDay => ({ |
| 32 | day: d, |
| 33 | bucket, |
| 34 | cfCostMicros: cf, |
| 35 | ownCostMicros: own, |
| 36 | valueMicros: value, |
| 37 | cashMicros: cash, |
| 38 | }); |
| 39 | |
| 40 | test("every day of the range is there, even an empty one", () => { |
| 41 | assert.deepEqual(daysBetween("2026-09-29", "2026-10-02"), ["2026-09-29", "2026-09-30", "2026-10-01", "2026-10-02"]); |
| 42 | assert.deepEqual(daysBetween("2026-10-02", "2026-10-01"), []); |
| 43 | }); |
| 44 | |
| 45 | test("all of g1t is money in against every cost; a product is its value against its cost", () => { |
| 46 | const rows = [ |
| 47 | day("2026-10-01", "sandboxes", 2_000_000, 1_900_000, 2_400_000, 1_000_000), |
| 48 | day("2026-10-01", "platform", 500_000, 0, 20_000_000, 20_000_000), |
| 49 | // Models: no Cloudflare bill, g1t's own cost. |
| 50 | day("2026-10-01", "models", 0, 100_000, 120_000, 120_000), |
| 51 | ]; |
| 52 | const all = daySeries(rows, "2026-10-01", "2026-10-02", null); |
| 53 | assert.deepEqual(all, [ |
| 54 | { day: "2026-10-01", revenueMicros: 21_120_000, costMicros: 2_600_000 }, |
| 55 | { day: "2026-10-02", revenueMicros: 0, costMicros: 0 }, |
| 56 | ]); |
| 57 | const sandboxes = daySeries(rows, "2026-10-01", "2026-10-01", "sandboxes"); |
| 58 | assert.deepEqual(sandboxes, [{ day: "2026-10-01", revenueMicros: 2_400_000, costMicros: 2_000_000 }]); |
| 59 | }); |
| 60 | |
| 61 | test("margins read against the floor", () => { |
| 62 | assert.equal(marginPercent(1_200_000, 1_000_000)?.toFixed(2), "16.67"); |
| 63 | assert.equal(marginPercent(0, 10), null); |
| 64 | assert.equal(marginTone(-3, 10), "danger"); |
| 65 | assert.equal(marginTone(12, 10), "warn"); |
| 66 | assert.equal(marginTone(16.7, 10), "mint"); |
| 67 | assert.equal(marginTone(null, 10), undefined); |
| 68 | assert.equal(percentLabel(-4), "−4.0%"); |
| 69 | assert.equal(percentLabel(12.34, { signed: true }), "+12.3%"); |
| 70 | assert.equal(percentLabel(null), "—"); |
| 71 | }); |
| 72 | |
| 73 | test("unit costs keep the places they need", () => { |
| 74 | assert.equal(unitDollars(150_000), "$0.150"); |
| 75 | assert.equal(unitDollars(16.44), "$0.0000164"); |
| 76 | assert.equal(unitDollars(2_500_000), "$2.50"); |
| 77 | assert.equal(unitDollars(0), "$0"); |
| 78 | }); |
| 79 | |
| 80 | test("the range and product come from the address, within bounds", () => { |
| 81 | assert.equal(parseRange("7"), 7); |
| 82 | assert.equal(parseRange("365"), 30); |
| 83 | assert.equal(parseRange(null), 30); |
| 84 | assert.equal(parseBucket("git", ["git", "sandboxes"]), "git"); |
| 85 | assert.equal(parseBucket("nope", ["git"]), null); |
| 86 | }); |
| 87 | |
| 88 | const form = (entries: Record<string, string>) => { |
| 89 | const data = new FormData(); |
| 90 | for (const [key, value] of Object.entries(entries)) data.set(key, value); |
| 91 | return data; |
| 92 | }; |
| 93 | |
| 94 | test("the guardrails form is checked", () => { |
| 95 | const good = parseCostSettings( |
| 96 | form({ |
| 97 | autoApply: "on", |
| 98 | autoApplyPercent: "25", |
| 99 | noticeDays: "14", |
| 100 | marginFloorPercent: "10", |
| 101 | alertDays: "3", |
| 102 | anomalyFactor: "1", |
| 103 | minDailyCost: "0.10", |
| 104 | anomalyFloor: "1", |
| 105 | }), |
| 106 | ); |
| 107 | assert.ok(good.ok); |
| 108 | assert.equal(good.ok && good.value.minDailyCostMicros, 100_000); |
| 109 | assert.equal(good.ok && good.value.autoApply, true); |
| 110 | const off = parseCostSettings(form({ autoApplyPercent: "25", noticeDays: "14", marginFloorPercent: "10", alertDays: "3", anomalyFactor: "1", minDailyCost: "0", anomalyFloor: "1" })); |
| 111 | assert.ok(off.ok && !off.value.autoApply); |
| 112 | assert.equal(parseCostSettings(form({ autoApplyPercent: "250" })).ok, false); |
| 113 | assert.equal(parseCostSettings(form({ autoApplyPercent: "25", noticeDays: "1.5" })).ok, false); |
| 114 | }); |
| 115 | |
| 116 | test("a mapping names Cloudflare's product and meter and one of g1t's products", () => { |
| 117 | const mapped = parseMapping(form({ product: "Artifacts", meter: "", bucket: "git", priceMeter: "git_operations", ownMeter: "git_operations", scaleToOwn: "on", driftPercent: "15" })); |
| 118 | assert.deepEqual(mapped, { |
| 119 | ok: true, |
| 120 | value: { product: "artifacts", meter: "*", bucket: "git", priceMeter: "git_operations", ownMeter: "git_operations", scaleToOwn: true, driftPercent: 15, note: "" }, |
| 121 | }); |
| 122 | assert.deepEqual(parseMapping(form({ product: "r2", meter: "class_a", remove: "1" })), { ok: true, value: { product: "r2", meter: "class_a", remove: true } }); |
| 123 | assert.equal(parseMapping(form({ product: "r2", meter: "x y", bucket: "git" })).ok, false); |
| 124 | assert.equal(parseMapping(form({ product: "r2", meter: "*", bucket: "" })).ok, false); |
| 125 | assert.equal(parseMapping(form({ product: "r2", meter: "*", bucket: "git", driftPercent: "-1" })).ok, false); |
| 126 | }); |
| 127 | |
| 128 | const caps = (over: Partial<SpendCaps> = {}): SpendCaps => ({ |
| 129 | day: "2026-10-06", |
| 130 | month: "2026-10", |
| 131 | todayMicros: 20_000_000, |
| 132 | dailyCapMicros: 75_000_000, |
| 133 | tripped: false, |
| 134 | trippedAt: null, |
| 135 | liftedBy: null, |
| 136 | liftedAt: null, |
| 137 | liftNote: null, |
| 138 | monthBuckets: [ |
| 139 | { bucket: "comped", title: "Comped (g1t's own)", micros: 40_000_000 }, |
| 140 | { bucket: "trial", title: "Trial pool", micros: 5_000_000 }, |
| 141 | ], |
| 142 | comped: [{ account: "ws_flagon-io", name: "flagon-io", usedMicros: 40_000_000, ceilingMicros: 150_000_000, defaultCeiling: true, level: 0 }], |
| 143 | freeTierMicros: 1_000_000, |
| 144 | fixedMonthlyMicros: 30_000_000, |
| 145 | revenueMicros: 0, |
| 146 | ...over, |
| 147 | }); |
| 148 | |
| 149 | test("the spend bar shows only when a cap stops work", () => { |
| 150 | assert.equal(spendBanner(caps()), null); |
| 151 | assert.ok((spendBanner(caps({ tripped: true, todayMicros: 80_000_000 })) ?? "").startsWith("The daily breaker is open ($80.00 of $75.00 today)")); |
| 152 | const usedUp = caps({ comped: [{ account: "ws_flagon-io", name: "flagon-io", usedMicros: 150_000_000, ceilingMicros: 150_000_000, defaultCeiling: true, level: 100 }] }); |
| 153 | assert.ok((spendBanner(usedUp) ?? "").startsWith("Flagon-io used its $150.00 monthly budget")); |
| 154 | // No budget set to zero is ever "used up". |
| 155 | assert.equal(spendBanner(caps({ comped: [{ account: "a", name: "a", usedMicros: 9, ceilingMicros: 0, defaultCeiling: false, level: 0 }] })), null); |
| 156 | }); |
| 157 | |
| 158 | test("what g1t paid this month adds every bucket, the free tier and subscriptions", () => { |
| 159 | const { rows, totalMicros } = spendRows(caps()); |
| 160 | assert.deepEqual(rows.map((r) => r.key), ["comped", "trial", "free", "fixed"]); |
| 161 | assert.equal(totalMicros, 76_000_000); |
| 162 | }); |
| 163 | |
| 164 | test("charged without real money is what it cost g1t, never its price", () => { |
| 165 | const { rows } = spendRows(caps({ monthBuckets: [{ bucket: "unpaid", title: "Charged without real money", micros: 7_610_000 }] })); |
| 166 | assert.match(rows[0]!.note, /what it cost g1t, not what was charged/); |
| 167 | }); |
| 168 | |
| 169 | test("margins are a share of the price: cost plus 20% is 16.7%", () => { |
| 170 | assert.equal(percentLabel(marginOnPrice(20)), "16.7%"); |
| 171 | assert.equal(marginOnPrice(0), 0); |
| 172 | // As By product showed it: $2.99 charged for models that cost $2.49. |
| 173 | assert.equal(percentLabel(marginPercent(2_988_000, 2_490_000)), "16.7%"); |
| 174 | }); |
| 175 | |
| 176 | test("who g1t paid is the statement's all-in cost, subscriptions included", () => { |
| 177 | const subscriptions = subscriptionsOver(30_000_000, 30); |
| 178 | const paid = whoPaid({ costMicros: 2_490_000, cloudflareCostMicros: 0, modelsCostMicros: 2_490_000 }, subscriptions); |
| 179 | assert.deepEqual(paid, { totalMicros: 32_490_000, cloudflareMicros: 0, subscriptionsMicros: 30_000_000, modelsMicros: 2_490_000 }); |
| 180 | assert.equal(subscriptionsOver(30_000_000, 7), 7_000_000); |
| 181 | // From older billing, Cloudflare's part is the rest. |
| 182 | assert.equal(whoPaid({ costMicros: 3_000_000, modelsCostMicros: 1_000_000 }, 0).cloudflareMicros, 2_000_000); |
| 183 | }); |
| 184 | |
| 185 | test("a rate g1t sets shows no cost, and a weight is no money", () => { |
| 186 | assert.deepEqual(versionCells({ costMicros: 250_000, priceMicros: 250_000, basis: "rate" }), { |
| 187 | cost: "—", |
| 188 | price: "$0.250", |
| 189 | note: "g1t's own rate: no cost behind it", |
| 190 | }); |
| 191 | assert.equal(versionCells({ costMicros: 100_000, priceMicros: 100_000, basis: "weight" }).price, "×0.1"); |
| 192 | assert.deepEqual(versionCells({ costMicros: 16.44, priceMicros: 19.73, basis: "cost" }), { cost: unitDollars(16.44), price: unitDollars(19.73), note: null }); |
| 193 | // Older billing sends no basis: a cost. |
| 194 | assert.equal(versionCells({ costMicros: 1_000_000, priceMicros: 1_200_000 }).cost, "$1.00"); |
| 195 | }); |
| 196 | |
| 197 | test("a rise replaced before its date says so, rather than missing its date", () => { |
| 198 | assert.equal( |
| 199 | proposalOutcome({ status: "superseded", decidedBy: "guardrail", effectiveAt: null }), |
| 200 | "applied by guardrail, then replaced by a later measurement before it took effect; nothing was charged at it", |
| 201 | ); |
| 202 | // From before billing marked them superseded. |
| 203 | assert.match(proposalOutcome({ status: "applied", decidedBy: "guardrail", effectiveAt: null }) ?? "", /replaced by a later measurement/); |
| 204 | assert.equal(proposalOutcome({ status: "applied", decidedBy: "guardrail", effectiveAt: "2026-10-22T04:18:12.571Z" }), "applied by guardrail"); |
| 205 | assert.equal(proposalOutcome({ status: "superseded", decidedBy: null, effectiveAt: null }), "replaced by a later measurement"); |
| 206 | assert.equal(proposalOutcome({ status: "open", decidedBy: null, effectiveAt: null }), null); |
| 207 | }); |
| 208 | |
| 209 | test("subscriptions over the range are billing's, day by day by the billing cycle", () => { |
| 210 | // Billing's accrual wins over a month's over 30 days. |
| 211 | assert.equal(subscriptionsOver(30_000_000, 30, { subscriptionsMicros: 29_032_258 }), 29_032_258); |
| 212 | assert.equal(subscriptionsOver(30_000_000, 30, {}), 30_000_000); |
| 213 | // The month view: this month's days so far, not the whole month. |
| 214 | const { rows, totalMicros } = spendRows(caps({ fixedMonthMicros: 9_000_000 })); |
| 215 | const fixed = rows.find((r) => r.key === "fixed")!; |
| 216 | assert.equal(fixed.micros, 9_000_000); |
| 217 | assert.match(fixed.note, /days so far, each its billing cycle's share of \$30\.00 a month/); |
| 218 | assert.equal(totalMicros, 55_000_000); |
| 219 | }); |
| 220 | |
| 221 | test("test-mode charges are given away, never money in", () => { |
| 222 | const parts = givenParts({ usageMicros: 0, plansMicros: 0, costMicros: 0, marginMicros: 0, marginPercent: null, givenUnpaidMicros: 7_610_000, givenCompedMicros: 2_370_000 }); |
| 223 | assert.deepEqual(parts, [ |
| 224 | ["100% discounts", 2_370_000], |
| 225 | ["charged without real money", 7_610_000], |
| 226 | ]); |
| 227 | }); |
| 228 | |
| 229 | const cycle = (over: Partial<CloudflareCycle> = {}): CloudflareCycle => ({ |
| 230 | start: "2026-09-28", |
| 231 | end: "2026-10-27", |
| 232 | days: 30, |
| 233 | daysElapsed: 12, |
| 234 | usageMicros: 292_175, |
| 235 | projectedMicros: 730_438, |
| 236 | averageDailyMicros: 24_347, |
| 237 | subscriptionsMicros: 30_000_000, |
| 238 | meters: [ |
| 239 | { product: "workers", meter: "workers_cpu_ms", rawName: "Workers / Workers CPU ms", unit: "ms", quantity: 39_160_000, included: 30_000_000, billableQuantity: 9_160_000, costMicros: 200_000, basis: "list" }, |
| 240 | { product: "email", meter: "email_service_emails_sent", rawName: "Email / Emails sent", unit: "Count", quantity: 7, included: null, billableQuantity: 0, costMicros: 0, basis: "none" }, |
| 241 | { product: "kv", meter: "kv_list_operations", rawName: "KV list", unit: "Count", quantity: 0, included: null, billableQuantity: 0, costMicros: 0, basis: "none" }, |
| 242 | ], |
| 243 | ...over, |
| 244 | }); |
| 245 | |
| 246 | test("the billing cycle reads as Cloudflare's Billable usage page does", () => { |
| 247 | const headline = cycleHeadline(cycle()); |
| 248 | assert.equal(headline.title, "2026-09-28 to 2026-10-27, day 12 of 30"); |
| 249 | assert.equal(headline.detail, "$0.29 so far, $0.02 a day; projected $0.73 for the cycle, and $30.00 of subscriptions"); |
| 250 | // Used with no list price: said, not hidden. Unused ones are no news. |
| 251 | assert.deepEqual(unpricedMeters(cycle().meters).map((m) => m.meter), ["email_service_emails_sent"]); |
| 252 | assert.equal(basisLabel("list"), "List price past the included amount"); |
| 253 | assert.equal(basisLabel("cloudflare"), "Cloudflare's cost"); |
| 254 | assert.equal(basisLabel("none"), "No list price: counted at $0"); |
| 255 | }); |
| 256 | |
| 257 | test("the last read of the bill says when it may be incomplete", () => { |
| 258 | const read = { readAt: "2026-10-09T04:17:00Z", since: "2026-09-28", until: "2026-10-09", rows: 412, pages: 3, consumedRows: 412, pricingOnlyRows: 0, costedRows: 0 }; |
| 259 | assert.deepEqual(billReadNote(read), { |
| 260 | text: "412 rows in 3 pages for 2026-09-28 to 2026-10-09; none with a cost of Cloudflare's, so the list prices apply.", |
| 261 | warn: false, |
| 262 | }); |
| 263 | assert.equal(billReadNote({ ...read, pricingOnlyRows: 5 }).warn, true); |
| 264 | assert.equal(billReadNote({ ...read, rows: 0, pages: 1, consumedRows: 0 }).warn, true); |
| 265 | }); |