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