Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

271 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import {
5 foldTasks,
6 gigabytes,
7 shownMeters,
8 alertText,
9 alertTone,
10 cardCheckResult,
11 creditLine,
12 needsAttention,
13 usageGlance,
14 parseLimitRequest,
15 parsePrepay,
16 parseSpendLimit,
17 planStatus,
18 readDollars,
19 requestStatus,
20 share,
21 spendPath,
22 spendRange,
23} from "./billing.ts";
24
25test("a credit from g1t reads in a line, with what is left and when it expires", () => {
26 const now = new Date("2026-10-07T12:00:00Z");
27 const grant = { amountMicros: 25_000_000, leftMicros: 12_400_000, expiresAt: "2027-01-05T23:59:59Z", state: "open" as const };
28 assert.equal(creditLine(grant, now), "$25.00 credit, $12.40 left, expires Jan 5, 2027");
29 assert.equal(creditLine({ ...grant, expiresAt: "2026-12-05T23:59:59Z" }, now), "$25.00 credit, $12.40 left, expires Dec 5");
30 assert.equal(creditLine({ ...grant, expiresAt: null }, now), "$25.00 credit, $12.40 left");
31 assert.equal(creditLine({ ...grant, state: "used", leftMicros: 0 }, now), "$25.00 credit, all used");
32 assert.equal(creditLine({ ...grant, state: "revoked", leftMicros: 0 }, now), "$25.00 credit, withdrawn");
33});
34
35test("dollars are read from what people type", () => {
36 assert.equal(readDollars("$1,000"), 1_000_000_000);
37 assert.equal(readDollars(""), null);
38 assert.equal(readDollars("lots"), null);
39});
40
41test("a prepayment is $25 or more, by card up to $10,000, by bank transfer from $1,000", () => {
42 assert.deepEqual(parsePrepay({ amount: "100" }), { ok: true, value: { amountCents: 10_000, method: "card" } });
43 assert.deepEqual(parsePrepay({ amount: "100", custom: "250" }), {
44 ok: true,
45 value: { amountCents: 25_000, method: "card" },
46 });
47 assert.equal(parsePrepay({ custom: "10" }).ok, false);
48 assert.equal(parsePrepay({ custom: "20000" }).ok, false);
49 assert.equal(parsePrepay({ custom: "500", method: "bank_transfer" }).ok, false);
50 assert.deepEqual(parsePrepay({ custom: "20000", method: "bank_transfer" }), {
51 ok: true,
52 value: { amountCents: 2_000_000, method: "bank_transfer" },
53 });
54 assert.equal(parsePrepay({}).ok, false);
55});
56
57test("a limit request needs an amount, a reason and a monthly spend; an overage only a reason", () => {
58 assert.deepEqual(parseLimitRequest({ kind: "limit", amount: "5000", reason: " A launch ", expected: "3000" }), {
59 ok: true,
60 value: { kind: "limit", amountMicros: 5_000_000_000, reason: "A launch", expectedMonthlyMicros: 3_000_000_000 },
61 });
62 assert.equal(parseLimitRequest({ kind: "limit", amount: "5000", reason: "", expected: "1" }).ok, false);
63 assert.equal(parseLimitRequest({ kind: "limit", reason: "x", expected: "1" }).ok, false);
64 assert.deepEqual(parseLimitRequest({ kind: "overage", reason: "A loop ran all night" }), {
65 ok: true,
66 value: { kind: "overage", amountMicros: 0, reason: "A loop ran all night", expectedMonthlyMicros: 0 },
67 });
68});
69
70test("a spend limit is automatic, fixed, the full amount available, or the one-time raise", () => {
71 assert.deepEqual(parseSpendLimit({ mode: "automatic" }), { ok: true, value: { micros: null, useFull: false, raiseOnce: false } });
72 assert.deepEqual(parseSpendLimit({ mode: "fixed", limit: "500" }), {
73 ok: true,
74 value: { micros: 500_000_000, useFull: false, raiseOnce: false },
75 });
76 assert.equal(parseSpendLimit({ mode: "fixed", limit: "0.5" }).ok, false);
77 assert.deepEqual(parseSpendLimit({ mode: "full" }), { ok: true, value: { micros: null, useFull: true, raiseOnce: false } });
78 assert.deepEqual(parseSpendLimit({ mode: "raise" }), { ok: true, value: { micros: null, useFull: false, raiseOnce: true } });
79 assert.deepEqual(parseSpendLimit({ mode: "raise", limit: "150" }), {
80 ok: true,
81 value: { micros: 150_000_000, useFull: false, raiseOnce: true },
82 });
83});
84
85test("the spend limit's range: up to the highest ceiling plus prepaid, then once to twice it, then ask", () => {
86 // A first month at $100, nothing prepaid, the raise unused: $100 alone, $200 once.
87 const range = spendRange({ availableMicros: 100_000_000, ceilingMicros: 100_000_000, raiseOnceMicros: 200_000_000, raisedAt: null });
88 assert.deepEqual(range, { selfServeMicros: 100_000_000, raiseOnceMicros: 200_000_000, raisedAt: null });
89 assert.equal(spendPath(80_000_000, range), "self");
90 assert.equal(spendPath(100_000_000, range), "self");
91 assert.equal(spendPath(150_000_000, range), "raise");
92 assert.equal(spendPath(200_000_000, range), "raise");
93 assert.equal(spendPath(200_000_001, range), "ask");
94 // The raise used: only asking goes further.
95 const used = spendRange({ availableMicros: 200_000_000, ceilingMicros: 200_000_000, raiseOnceMicros: null, raisedAt: "2026-10-01T00:00:00Z" });
96 assert.equal(used.raiseOnceMicros, null);
97 assert.equal(spendPath(300_000_000, used), "ask");
98 // No ceiling: anything goes.
99 assert.equal(spendPath(1e12, spendRange({ availableMicros: null, ceilingMicros: null, raiseOnceMicros: null })), "self");
100 // A raise no higher than what is available is no raise.
101 assert.equal(spendRange({ availableMicros: 300_000_000, ceilingMicros: 100_000_000, raiseOnceMicros: 300_000_000 }).raiseOnceMicros, null);
102});
103
104test("the plan card says where the workspace stands", () => {
105 const free = { plan: "free" as const, trialMicrosLeft: 0, trialVerified: false, firstMonth: false };
106 assert.deepEqual(planStatus(null, free), { kind: "free", label: "Free" });
107 assert.equal(planStatus(null, { ...free, trialVerified: true, trialMicrosLeft: 5_000_000 }).kind, "trial");
108 assert.equal(planStatus(null, { ...free, trialVerified: true }).kind, "free");
109 const subscription = { feature: "plan" as const, status: "active" as const, periodEnd: null, startedBy: "a", startedAt: "" };
110 assert.deepEqual(planStatus({ on: true, subscription }, { ...free, plan: "paid", firstMonth: true }), {
111 kind: "paid",
112 label: "On the g1t plan, first month",
113 });
114 assert.equal(planStatus({ on: true, subscription: { ...subscription, status: "canceling" } }, { ...free, plan: "paid" }).kind, "canceling");
115 assert.equal(planStatus({ on: false, subscription: { ...subscription, status: "past_due" } }, free).kind, "past_due");
116 assert.equal(planStatus({ on: true, included: true, subscription: null }, free).kind, "comped");
117 assert.equal(planStatus(null, { ...free, plan: "internal" }).kind, "comped");
118 assert.equal(planStatus(null, { ...free, plan: "enterprise" }).kind, "enterprise");
119});
120
121test("the overview's Usage card reads the month by what pays", () => {
122 const usage = {
123 free: false,
124 spentMicros: 14_000_000,
125 usedMicros: 11_000_000,
126 byTask: [
127 { key: "review", micros: 2_000_000, runs: 4 },
128 { key: "implement", micros: 12_000_000, runs: 9 },
129 { key: "plan", micros: 0, runs: 1 },
130 ],
131 };
132 const entitlements = { includedMicros: 10_000_000, includedUsedMicros: 10_000_000, trialMicrosLeft: 0 };
133 const plan = usageGlance({
134 usage,
135 status: { kind: "paid", label: "" },
136 entitlements,
137 limit: { spentMicros: 4_000_000, spendLimitMicros: 200_000_000 },
138 trialMicros: 5_000_000,
139 });
140 assert.equal(plan.kind, "plan");
141 assert.deepEqual(plan.credit, { label: "Included usage", usedMicros: 10_000_000, ofMicros: 10_000_000 });
142 assert.deepEqual(plan.onDemand, { micros: 4_000_000, limitMicros: 200_000_000 });
143 // Most first, and nothing that cost nothing.
144 assert.deepEqual(
145 plan.lines.map((line) => line.label),
146 ["Making changes", "Reviews"],
147 );
148 const trial = usageGlance({
149 usage,
150 status: { kind: "trial", label: "" },
151 entitlements: { ...entitlements, trialMicrosLeft: 1_500_000 },
152 limit: null,
153 trialMicros: 5_000_000,
154 });
155 assert.deepEqual(trial.credit, { label: "Trial credit", usedMicros: 3_500_000, ofMicros: 5_000_000 });
156 assert.equal(trial.onDemand, null);
157 assert.equal(usageGlance({ usage, status: { kind: "comped", label: "" }, entitlements, limit: null, trialMicros: 0 }).kind, "comped");
158 assert.equal(usageGlance({ usage, status: { kind: "free", label: "" }, entitlements, limit: null, trialMicros: 0 }).kind, "forge");
159 // While g1t charges nothing, usage is shown at cost.
160 const beta = usageGlance({ usage: { ...usage, free: true }, status: { kind: "paid", label: "" }, entitlements, limit: null, trialMicros: 0 });
161 assert.equal(beta.kind, "beta");
162 assert.equal(beta.spentMicros, 11_000_000);
163});
164
165test("a meter's share stays between 0 and 1", () => {
166 assert.equal(share(5, 10), 0.5);
167 assert.equal(share(15, 10), 1);
168 assert.equal(share(5, 0), 0);
169 assert.equal(share(5, null), 0);
170});
171
172test("after a card check the owner reads whether the trial started", () => {
173 assert.match(cardCheckResult({ trialVerified: true, trialMicrosLeft: 5_000_000 }, 5_000_000), /[$]5 trial is ready/);
174 assert.match(cardCheckResult({ trialVerified: true, trialMicrosLeft: 0 }, 5_000_000), /Prepaid cards can still pay for the plan/);
175 assert.match(cardCheckResult({ trialVerified: false, trialMicrosLeft: 0 }, 5_000_000), /did not finish/);
176});
177
178test("alerts read as sentences", () => {
179 const alert = { meter: "included", level: 90, usedMicros: 9_000_000, limitMicros: 10_000_000, message: "" };
180 assert.equal(alertText(alert), "90% of the plan's included usage: $9.00 of $10.00.");
181 assert.equal(alertText({ ...alert, meter: "spend_limit", level: 100 }), "All of your spend limit: $9.00 of $10.00.");
182 assert.equal(alertTone(50), "ok");
183 assert.equal(alertTone(75), "warning");
184 assert.equal(alertTone(100), "stopped");
185});
186
187test("a workspace needs attention when paused, on a spike, or at 90%", () => {
188 assert.equal(needsAttention(null), false);
189 assert.equal(needsAttention({ paused: null, spike: null, alerts: [] }), false);
190 assert.equal(needsAttention({ paused: "Spend spike", spike: null, alerts: [] }), true);
191 const spike = { id: "s", status: "continued", hourMicros: 1, averageMicros: 1, detectedAt: "" };
192 assert.equal(needsAttention({ paused: null, spike, alerts: [] }), false);
193 assert.equal(needsAttention({ paused: null, spike: { ...spike, status: "open" }, alerts: [] }), true);
194 const at = (level: number) => ({ meter: "spend_limit", level, usedMicros: 0, limitMicros: 0, message: "" });
195 assert.equal(needsAttention({ paused: null, spike: null, alerts: [at(75)] }), false);
196 assert.equal(needsAttention({ paused: null, spike: null, alerts: [at(90)] }), true);
197});
198
199test("a request says where it stands", () => {
200 const request = {
201 id: "r",
202 workspace: "acme",
203 kind: "limit",
204 amountMicros: 1,
205 reason: "",
206 expectedMonthlyMicros: 0,
207 status: "open",
208 createdBy: "a",
209 createdAt: "",
210 };
211 assert.equal(requestStatus(request), "Waiting for an answer");
212 assert.equal(requestStatus({ ...request, status: "approved", decidedMicros: 2_000_000_000 }), "Approved at $2,000");
213 assert.equal(requestStatus({ ...request, status: "declined" }), "Declined");
214});
215
216const METERS = [
217 { key: "agents", label: "Agents & sandboxes", micros: 1_200_000, quantity: "4 runs" },
218 { key: "builds", label: "Builds", micros: 0, quantity: null },
219 { key: "requests", label: "Requests & CPU", micros: 0, quantity: null },
220 { key: "domains", label: "Custom domains", micros: 120_000, quantity: "1 custom domain" },
221 { key: "git_storage", label: "Git operations & storage", micros: 0, quantity: "1,200 git operations" },
222 { key: "search_scans", label: "Search & security scans", micros: 3_000, quantity: null },
223];
224
225test("the plan shows every meter, with no quota beside any of them", () => {
226 const shown = shownMeters(METERS, true);
227 assert.deepEqual(
228 shown.map((m) => m.key),
229 ["agents", "builds", "requests", "domains", "git_storage", "search_scans"],
230 );
231 for (const meter of shown) assert.ok(!/ of |apps up/i.test(`${meter.label} ${meter.quantity ?? ""}`));
232});
233
234test("a free workspace sees only what it can use, and anything it used anyway", () => {
235 assert.deepEqual(
236 shownMeters(METERS, false).map((m) => m.key),
237 ["agents", "domains", "git_storage", "search_scans"],
238 );
239});
240
241test("storage reads in powers of ten", () => {
242 assert.equal(gigabytes(1_000_000_000), "1 GB");
243 assert.equal(gigabytes(1_500_000_000), "1.5 GB");
244 assert.equal(gigabytes(500_000_000), "500 MB");
245});
246
247test("kinds of work without words of their own are one Other, listed once and last", () => {
248 const slices = [
249 { key: "check", micros: 5, runs: 50 },
250 { key: "review", micros: 3, runs: 5 },
251 { key: "other", micros: 1, runs: 7 },
252 { key: "implement", micros: 9, runs: 4 },
253 { key: "answer", micros: 2, runs: 11 },
254 ];
255 assert.deepEqual(foldTasks(slices), [
256 { key: "review", micros: 3, runs: 5 },
257 { key: "implement", micros: 9, runs: 4 },
258 { key: "other", micros: 8, runs: 68 },
259 ]);
260 assert.deepEqual(foldTasks([{ key: "plan", micros: 1, runs: 1 }]), [{ key: "plan", micros: 1, runs: 1 }]);
261});
262
263test("budget alerts keep the levels ticked, highest first", async () => {
264 const { parseBudgetAlerts } = await import("./billing.ts");
265 assert.deepEqual(parseBudgetAlerts({ alerts: ["50", "100", "75", "12"], pause: "on", webhook: "" }), {
266 ok: true,
267 value: { alerts: [100, 75, 50], pauseAtLimit: true, webhook: null },
268 });
269 assert.equal(parseBudgetAlerts({ alerts: [], webhook: "http://x.test" }).ok, false);
270 assert.deepEqual(parseBudgetAlerts({ alerts: [], webhook: "https://hooks.acme.test/g1t" }).ok, true);
271});