| 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( |
| 115 | form({ autoApplyPercent: "25", noticeDays: "14", marginFloorPercent: "10", alertDays: "3", anomalyFactor: "1", minDailyCost: "0", anomalyFloor: "1" }), |
| 116 | ); |
| 117 | assert.ok(off.ok && !off.value.autoApply); |
| 118 | assert.equal(parseCostSettings(form({ autoApplyPercent: "250" })).ok, false); |
| 119 | assert.equal(parseCostSettings(form({ autoApplyPercent: "25", noticeDays: "1.5" })).ok, false); |
| 120 | }); |
| 121 | |
| 122 | test("a mapping names Cloudflare's product and meter and one of g1t's products", () => { |
| 123 | const mapped = parseMapping( |
| 124 | form({ product: "Artifacts", meter: "", bucket: "git", priceMeter: "git_operations", ownMeter: "git_operations", scaleToOwn: "on", driftPercent: "15" }), |
| 125 | ); |
| 126 | assert.deepEqual(mapped, { |
| 127 | ok: true, |
| 128 | value: { |
| 129 | product: "artifacts", |
| 130 | meter: "*", |
| 131 | bucket: "git", |
| 132 | priceMeter: "git_operations", |
| 133 | ownMeter: "git_operations", |
| 134 | scaleToOwn: true, |
| 135 | driftPercent: 15, |
| 136 | note: "", |
| 137 | }, |
| 138 | }); |
| 139 | assert.deepEqual(parseMapping(form({ product: "r2", meter: "class_a", remove: "1" })), { |
| 140 | ok: true, |
| 141 | value: { product: "r2", meter: "class_a", remove: true }, |
| 142 | }); |
| 143 | assert.equal(parseMapping(form({ product: "r2", meter: "x y", bucket: "git" })).ok, false); |
| 144 | assert.equal(parseMapping(form({ product: "r2", meter: "*", bucket: "" })).ok, false); |
| 145 | assert.equal(parseMapping(form({ product: "r2", meter: "*", bucket: "git", driftPercent: "-1" })).ok, false); |
| 146 | }); |
| 147 | |
| 148 | const caps = (over: Partial<SpendCaps> = {}): SpendCaps => ({ |
| 149 | day: "2026-10-06", |
| 150 | month: "2026-10", |
| 151 | todayMicros: 20_000_000, |
| 152 | dailyCapMicros: 75_000_000, |
| 153 | tripped: false, |
| 154 | trippedAt: null, |
| 155 | liftedBy: null, |
| 156 | liftedAt: null, |
| 157 | liftNote: null, |
| 158 | monthBuckets: [ |
| 159 | { bucket: "comped", title: "Comped (g1t's own)", micros: 40_000_000 }, |
| 160 | { bucket: "trial", title: "Trial pool", micros: 5_000_000 }, |
| 161 | ], |
| 162 | comped: [{ account: "ws_flagon-io", name: "flagon-io", usedMicros: 40_000_000, ceilingMicros: 150_000_000, defaultCeiling: true, level: 0 }], |
| 163 | freeTierMicros: 1_000_000, |
| 164 | fixedMonthlyMicros: 30_000_000, |
| 165 | revenueMicros: 0, |
| 166 | ...over, |
| 167 | }); |
| 168 | |
| 169 | test("the spend bar shows only when a cap stops work", () => { |
| 170 | assert.equal(spendBanner(caps()), null); |
| 171 | assert.ok((spendBanner(caps({ tripped: true, todayMicros: 80_000_000 })) ?? "").startsWith("The daily breaker is open ($80.00 of $75.00 today)")); |
| 172 | const usedUp = caps({ |
| 173 | comped: [{ account: "ws_flagon-io", name: "flagon-io", usedMicros: 150_000_000, ceilingMicros: 150_000_000, defaultCeiling: true, level: 100 }], |
| 174 | }); |
| 175 | assert.ok((spendBanner(usedUp) ?? "").startsWith("Flagon-io used its $150.00 monthly budget")); |
| 176 | // No budget set to zero is ever "used up". |
| 177 | assert.equal(spendBanner(caps({ comped: [{ account: "a", name: "a", usedMicros: 9, ceilingMicros: 0, defaultCeiling: false, level: 0 }] })), null); |
| 178 | }); |
| 179 | |
| 180 | test("what g1t paid this month adds every bucket, the free tier and subscriptions", () => { |
| 181 | const { rows, totalMicros } = spendRows(caps()); |
| 182 | assert.deepEqual( |
| 183 | rows.map((r) => r.key), |
| 184 | ["comped", "trial", "free", "fixed"], |
| 185 | ); |
| 186 | assert.equal(totalMicros, 76_000_000); |
| 187 | }); |
| 188 | |
| 189 | test("charged without real money is what it cost g1t, never its price", () => { |
| 190 | const { rows } = spendRows(caps({ monthBuckets: [{ bucket: "unpaid", title: "Charged without real money", micros: 7_610_000 }] })); |
| 191 | assert.match(rows[0]!.note, /what it cost g1t, not what was charged/); |
| 192 | }); |
| 193 | |
| 194 | test("margins are a share of the price: cost plus 20% is 16.7%", () => { |
| 195 | assert.equal(percentLabel(marginOnPrice(20)), "16.7%"); |
| 196 | assert.equal(marginOnPrice(0), 0); |
| 197 | // As By product showed it: $2.99 charged for models that cost $2.49. |
| 198 | assert.equal(percentLabel(marginPercent(2_988_000, 2_490_000)), "16.7%"); |
| 199 | }); |
| 200 | |
| 201 | test("who g1t paid is the statement's all-in cost, subscriptions included", () => { |
| 202 | const subscriptions = subscriptionsOver(30_000_000, 30); |
| 203 | const paid = whoPaid({ costMicros: 2_490_000, cloudflareCostMicros: 0, modelsCostMicros: 2_490_000 }, subscriptions); |
| 204 | assert.deepEqual(paid, { totalMicros: 32_490_000, cloudflareMicros: 0, subscriptionsMicros: 30_000_000, modelsMicros: 2_490_000 }); |
| 205 | assert.equal(subscriptionsOver(30_000_000, 7), 7_000_000); |
| 206 | // From older billing, Cloudflare's part is the rest. |
| 207 | assert.equal(whoPaid({ costMicros: 3_000_000, modelsCostMicros: 1_000_000 }, 0).cloudflareMicros, 2_000_000); |
| 208 | }); |
| 209 | |
| 210 | test("a rate g1t sets shows no cost, and a weight is no money", () => { |
| 211 | assert.deepEqual(versionCells({ costMicros: 250_000, priceMicros: 250_000, basis: "rate" }), { |
| 212 | cost: "—", |
| 213 | price: "$0.250", |
| 214 | note: "g1t's own rate: no cost behind it", |
| 215 | }); |
| 216 | assert.equal(versionCells({ costMicros: 100_000, priceMicros: 100_000, basis: "weight" }).price, "×0.1"); |
| 217 | assert.deepEqual(versionCells({ costMicros: 16.44, priceMicros: 19.73, basis: "cost" }), { cost: unitDollars(16.44), price: unitDollars(19.73), note: null }); |
| 218 | // Older billing sends no basis: a cost. |
| 219 | assert.equal(versionCells({ costMicros: 1_000_000, priceMicros: 1_200_000 }).cost, "$1.00"); |
| 220 | }); |
| 221 | |
| 222 | test("a rise replaced before its date says so, rather than missing its date", () => { |
| 223 | assert.equal( |
| 224 | proposalOutcome({ status: "superseded", decidedBy: "guardrail", effectiveAt: null }), |
| 225 | "applied by guardrail, then replaced by a later measurement before it took effect; nothing was charged at it", |
| 226 | ); |
| 227 | // From before billing marked them superseded. |
| 228 | assert.match(proposalOutcome({ status: "applied", decidedBy: "guardrail", effectiveAt: null }) ?? "", /replaced by a later measurement/); |
| 229 | assert.equal(proposalOutcome({ status: "applied", decidedBy: "guardrail", effectiveAt: "2026-10-22T04:18:12.571Z" }), "applied by guardrail"); |
| 230 | assert.equal(proposalOutcome({ status: "superseded", decidedBy: null, effectiveAt: null }), "replaced by a later measurement"); |
| 231 | assert.equal(proposalOutcome({ status: "open", decidedBy: null, effectiveAt: null }), null); |
| 232 | }); |
| 233 | |
| 234 | test("the pause banner names every paused level, and who paused it when it was not a person", () => { |
| 235 | const level = (name: "compute" | "schedules" | "indexing" | "renders", paused: boolean, auto = false) => ({ |
| 236 | level: name, |
| 237 | paused, |
| 238 | note: null, |
| 239 | set_by: null, |
| 240 | set_at: null, |
| 241 | auto, |
| 242 | }); |
| 243 | const guard = (levels: ReturnType<typeof level>[]): PlatformGuard => ({ |
| 244 | levels, |
| 245 | hour: null, |
| 246 | last_hour: [], |
| 247 | month: "2026-10", |
| 248 | month_to_date: [], |
| 249 | breaches: [], |
| 250 | can_read: true, |
| 251 | auto_pause: ["schedules", "indexing"], |
| 252 | }); |
| 253 | assert.equal(pauseBanner(guard([level("compute", false), level("renders", false)])), null); |
| 254 | assert.equal(pauseBanner(guard([level("schedules", true, true)])), "Paused across g1t: schedules (by the usage watcher)."); |
| 255 | assert.equal( |
| 256 | pauseBanner(guard([level("compute", true), level("schedules", true), level("indexing", true, true)])), |
| 257 | "Paused across g1t: compute, schedules and indexing (by the usage watcher).", |
| 258 | ); |
| 259 | assert.equal(parsePauseLevel("renders"), "renders"); |
| 260 | assert.equal(parsePauseLevel("everything"), null); |
| 261 | assert.equal(count(240_000), "240k"); |
| 262 | assert.equal(count(2_000_000_000), "2.0B"); |
| 263 | assert.equal(thresholdShare(240_000, 200_000), 120); |
| 264 | assert.equal(thresholdShare(5, 0), null); |
| 265 | }); |
| 266 | |
| 267 | test("subscriptions over the range are billing's, day by day by the billing cycle", () => { |
| 268 | // Billing's accrual wins over a month's over 30 days. |
| 269 | assert.equal(subscriptionsOver(30_000_000, 30, { subscriptionsMicros: 29_032_258 }), 29_032_258); |
| 270 | assert.equal(subscriptionsOver(30_000_000, 30, {}), 30_000_000); |
| 271 | // The month view: this month's days so far, not the whole month. |
| 272 | const { rows, totalMicros } = spendRows(caps({ fixedMonthMicros: 9_000_000 })); |
| 273 | const fixed = rows.find((r) => r.key === "fixed")!; |
| 274 | assert.equal(fixed.micros, 9_000_000); |
| 275 | assert.match(fixed.note, /days so far, each its billing cycle's share of \$30\.00 a month/); |
| 276 | assert.equal(totalMicros, 55_000_000); |
| 277 | }); |
| 278 | |
| 279 | test("test-mode charges are given away, never money in", () => { |
| 280 | const parts = givenParts({ |
| 281 | usageMicros: 0, |
| 282 | plansMicros: 0, |
| 283 | costMicros: 0, |
| 284 | marginMicros: 0, |
| 285 | marginPercent: null, |
| 286 | givenUnpaidMicros: 7_610_000, |
| 287 | givenCompedMicros: 2_370_000, |
| 288 | }); |
| 289 | assert.deepEqual(parts, [ |
| 290 | ["100% discounts", 2_370_000], |
| 291 | ["charged without real money", 7_610_000], |
| 292 | ]); |
| 293 | }); |
| 294 | |
| 295 | const cycle = (over: Partial<CloudflareCycle> = {}): CloudflareCycle => ({ |
| 296 | start: "2026-09-28", |
| 297 | end: "2026-10-27", |
| 298 | days: 30, |
| 299 | daysElapsed: 12, |
| 300 | usageMicros: 292_175, |
| 301 | projectedMicros: 730_438, |
| 302 | averageDailyMicros: 24_347, |
| 303 | subscriptionsMicros: 30_000_000, |
| 304 | meters: [ |
| 305 | { |
| 306 | product: "workers", |
| 307 | meter: "workers_cpu_ms", |
| 308 | rawName: "Workers / Workers CPU ms", |
| 309 | unit: "ms", |
| 310 | quantity: 39_160_000, |
| 311 | included: 30_000_000, |
| 312 | billableQuantity: 9_160_000, |
| 313 | costMicros: 200_000, |
| 314 | basis: "list", |
| 315 | }, |
| 316 | { |
| 317 | product: "email", |
| 318 | meter: "email_service_emails_sent", |
| 319 | rawName: "Email / Emails sent", |
| 320 | unit: "Count", |
| 321 | quantity: 7, |
| 322 | included: null, |
| 323 | billableQuantity: 0, |
| 324 | costMicros: 0, |
| 325 | basis: "none", |
| 326 | }, |
| 327 | { |
| 328 | product: "kv", |
| 329 | meter: "kv_list_operations", |
| 330 | rawName: "KV list", |
| 331 | unit: "Count", |
| 332 | quantity: 0, |
| 333 | included: null, |
| 334 | billableQuantity: 0, |
| 335 | costMicros: 0, |
| 336 | basis: "none", |
| 337 | }, |
| 338 | ], |
| 339 | ...over, |
| 340 | }); |
| 341 | |
| 342 | test("the billing cycle reads as Cloudflare's Billable usage page does", () => { |
| 343 | const headline = cycleHeadline(cycle()); |
| 344 | assert.equal(headline.title, "2026-09-28 to 2026-10-27, day 12 of 30"); |
| 345 | assert.equal(headline.detail, "$0.29 so far, $0.02 a day; projected $0.73 for the cycle, and $30.00 of subscriptions"); |
| 346 | // Used with no list price: said, not hidden. Unused ones are no news. |
| 347 | assert.deepEqual( |
| 348 | unpricedMeters(cycle().meters).map((m) => m.meter), |
| 349 | ["email_service_emails_sent"], |
| 350 | ); |
| 351 | assert.equal(basisLabel("list"), "List price past the included amount"); |
| 352 | assert.equal(basisLabel("cloudflare"), "Cloudflare's cost"); |
| 353 | assert.equal(basisLabel("none"), "No list price: counted at $0"); |
| 354 | }); |
| 355 | |
| 356 | test("the last read of the bill says when it may be incomplete", () => { |
| 357 | const read = { |
| 358 | readAt: "2026-10-09T04:17:00Z", |
| 359 | since: "2026-09-28", |
| 360 | until: "2026-10-09", |
| 361 | rows: 412, |
| 362 | pages: 3, |
| 363 | consumedRows: 412, |
| 364 | pricingOnlyRows: 0, |
| 365 | costedRows: 0, |
| 366 | }; |
| 367 | assert.deepEqual(billReadNote(read), { |
| 368 | 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.", |
| 369 | warn: false, |
| 370 | }); |
| 371 | assert.equal(billReadNote({ ...read, pricingOnlyRows: 5 }).warn, true); |
| 372 | assert.equal(billReadNote({ ...read, rows: 0, pages: 1, consumedRows: 0 }).warn, true); |
| 373 | }); |