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.

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