g1t/apps/sudo/app/lib/costs.test.ts

114 lines4,656 bytesCodeBlame
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { CostDay } from "@g1t/contracts";
5
6import {
7 daySeries,
8 daysBetween,
9 marginPercent,
10 marginTone,
11 parseBucket,
12 parseCostSettings,
13 parseMapping,
14 parseRange,
15 percentLabel,
16 unitDollars,
17} from "./costs.ts";
18
19const day = (d: string, bucket: string, cf: number, own: number, value: number, cash: number): CostDay => ({
20 day: d,
21 bucket,
22 cfCostMicros: cf,
23 ownCostMicros: own,
24 valueMicros: value,
25 cashMicros: cash,
26});
27
28test("every day of the range is there, even an empty one", () => {
29 assert.deepEqual(daysBetween("2026-09-29", "2026-10-02"), ["2026-09-29", "2026-09-30", "2026-10-01", "2026-10-02"]);
30 assert.deepEqual(daysBetween("2026-10-02", "2026-10-01"), []);
31});
32
33test("all of g1t is money in against every cost; a product is its value against its cost", () => {
34 const rows = [
35 day("2026-10-01", "sandboxes", 2_000_000, 1_900_000, 2_400_000, 1_000_000),
36 day("2026-10-01", "platform", 500_000, 0, 20_000_000, 20_000_000),
37 // Models: no Cloudflare bill, g1t's own cost.
38 day("2026-10-01", "models", 0, 100_000, 120_000, 120_000),
39 ];
40 const all = daySeries(rows, "2026-10-01", "2026-10-02", null);
41 assert.deepEqual(all, [
42 { day: "2026-10-01", revenueMicros: 21_120_000, costMicros: 2_600_000 },
43 { day: "2026-10-02", revenueMicros: 0, costMicros: 0 },
44 ]);
45 const sandboxes = daySeries(rows, "2026-10-01", "2026-10-01", "sandboxes");
46 assert.deepEqual(sandboxes, [{ day: "2026-10-01", revenueMicros: 2_400_000, costMicros: 2_000_000 }]);
47});
48
49test("margins read against the floor", () => {
50 assert.equal(marginPercent(1_200_000, 1_000_000)?.toFixed(2), "16.67");
51 assert.equal(marginPercent(0, 10), null);
52 assert.equal(marginTone(-3, 10), "danger");
53 assert.equal(marginTone(12, 10), "warn");
54 assert.equal(marginTone(16.7, 10), "mint");
55 assert.equal(marginTone(null, 10), undefined);
56 assert.equal(percentLabel(-4), "−4.0%");
57 assert.equal(percentLabel(12.34, { signed: true }), "+12.3%");
58 assert.equal(percentLabel(null), "—");
59});
60
61test("unit costs keep the places they need", () => {
62 assert.equal(unitDollars(150_000), "$0.150");
63 assert.equal(unitDollars(16.44), "$0.0000164");
64 assert.equal(unitDollars(2_500_000), "$2.50");
65 assert.equal(unitDollars(0), "$0");
66});
67
68test("the range and product come from the address, within bounds", () => {
69 assert.equal(parseRange("7"), 7);
70 assert.equal(parseRange("365"), 30);
71 assert.equal(parseRange(null), 30);
72 assert.equal(parseBucket("git", ["git", "sandboxes"]), "git");
73 assert.equal(parseBucket("nope", ["git"]), null);
74});
75
76const form = (entries: Record<string, string>) => {
77 const data = new FormData();
78 for (const [key, value] of Object.entries(entries)) data.set(key, value);
79 return data;
80};
81
82test("the guardrails form is checked", () => {
83 const good = parseCostSettings(
84 form({
85 autoApply: "on",
86 autoApplyPercent: "25",
87 noticeDays: "14",
88 marginFloorPercent: "10",
89 alertDays: "3",
90 anomalyFactor: "1",
91 minDailyCost: "0.10",
92 anomalyFloor: "1",
93 }),
94 );
95 assert.ok(good.ok);
96 assert.equal(good.ok && good.value.minDailyCostMicros, 100_000);
97 assert.equal(good.ok && good.value.autoApply, true);
98 const off = parseCostSettings(form({ autoApplyPercent: "25", noticeDays: "14", marginFloorPercent: "10", alertDays: "3", anomalyFactor: "1", minDailyCost: "0", anomalyFloor: "1" }));
99 assert.ok(off.ok && !off.value.autoApply);
100 assert.equal(parseCostSettings(form({ autoApplyPercent: "250" })).ok, false);
101 assert.equal(parseCostSettings(form({ autoApplyPercent: "25", noticeDays: "1.5" })).ok, false);
102});
103
104test("a mapping names Cloudflare's product and meter and one of g1t's products", () => {
105 const mapped = parseMapping(form({ product: "Artifacts", meter: "", bucket: "git", priceMeter: "git_operations", ownMeter: "git_operations", scaleToOwn: "on", driftPercent: "15" }));
106 assert.deepEqual(mapped, {
107 ok: true,
108 value: { product: "artifacts", meter: "*", bucket: "git", priceMeter: "git_operations", ownMeter: "git_operations", scaleToOwn: true, driftPercent: 15, note: "" },
109 });
110 assert.deepEqual(parseMapping(form({ product: "r2", meter: "class_a", remove: "1" })), { ok: true, value: { product: "r2", meter: "class_a", remove: true } });
111 assert.equal(parseMapping(form({ product: "r2", meter: "x y", bucket: "git" })).ok, false);
112 assert.equal(parseMapping(form({ product: "r2", meter: "*", bucket: "" })).ok, false);
113 assert.equal(parseMapping(form({ product: "r2", meter: "*", bucket: "git", driftPercent: "-1" })).ok, false);
114});