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
257 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({ usage, status: { kind: "paid", label: "" }, entitlements, limit: { spentMicros: 4_000_000, spendLimitMicros: 200_000_000 }, trialMicros: 5_000_000 });
134 assert.equal(plan.kind, "plan");
135 assert.deepEqual(plan.credit, { label: "Included usage", usedMicros: 10_000_000, ofMicros: 10_000_000 });
136 assert.deepEqual(plan.onDemand, { micros: 4_000_000, limitMicros: 200_000_000 });
137 // Most first, and nothing that cost nothing.
138 assert.deepEqual(plan.lines.map((line) => line.label), ["Making changes", "Reviews"]);
139 const trial = usageGlance({ usage, status: { kind: "trial", label: "" }, entitlements: { ...entitlements, trialMicrosLeft: 1_500_000 }, limit: null, trialMicros: 5_000_000 });
140 assert.deepEqual(trial.credit, { label: "Trial credit", usedMicros: 3_500_000, ofMicros: 5_000_000 });
141 assert.equal(trial.onDemand, null);
142 assert.equal(usageGlance({ usage, status: { kind: "comped", label: "" }, entitlements, limit: null, trialMicros: 0 }).kind, "comped");
143 assert.equal(usageGlance({ usage, status: { kind: "free", label: "" }, entitlements, limit: null, trialMicros: 0 }).kind, "forge");
144 // While g1t charges nothing, usage is shown at cost.
145 const beta = usageGlance({ usage: { ...usage, free: true }, status: { kind: "paid", label: "" }, entitlements, limit: null, trialMicros: 0 });
146 assert.equal(beta.kind, "beta");
147 assert.equal(beta.spentMicros, 11_000_000);
148});
149
150test("a meter's share stays between 0 and 1", () => {
151 assert.equal(share(5, 10), 0.5);
152 assert.equal(share(15, 10), 1);
153 assert.equal(share(5, 0), 0);
154 assert.equal(share(5, null), 0);
155});
156
157test("after a card check the owner reads whether the trial started", () => {
158 assert.match(cardCheckResult({ trialVerified: true, trialMicrosLeft: 5_000_000 }, 5_000_000), /[$]5 trial is ready/);
159 assert.match(cardCheckResult({ trialVerified: true, trialMicrosLeft: 0 }, 5_000_000), /Prepaid cards can still pay for the plan/);
160 assert.match(cardCheckResult({ trialVerified: false, trialMicrosLeft: 0 }, 5_000_000), /did not finish/);
161});
162
163test("alerts read as sentences", () => {
164 const alert = { meter: "included", level: 90, usedMicros: 9_000_000, limitMicros: 10_000_000, message: "" };
165 assert.equal(alertText(alert), "90% of the plan's included usage: $9.00 of $10.00.");
166 assert.equal(alertText({ ...alert, meter: "spend_limit", level: 100 }), "All of your spend limit: $9.00 of $10.00.");
167 assert.equal(alertTone(50), "ok");
168 assert.equal(alertTone(75), "warning");
169 assert.equal(alertTone(100), "stopped");
170});
171
172test("a workspace needs attention when paused, on a spike, or at 90%", () => {
173 assert.equal(needsAttention(null), false);
174 assert.equal(needsAttention({ paused: null, spike: null, alerts: [] }), false);
175 assert.equal(needsAttention({ paused: "Spend spike", spike: null, alerts: [] }), true);
176 const spike = { id: "s", status: "continued", hourMicros: 1, averageMicros: 1, detectedAt: "" };
177 assert.equal(needsAttention({ paused: null, spike, alerts: [] }), false);
178 assert.equal(needsAttention({ paused: null, spike: { ...spike, status: "open" }, alerts: [] }), true);
179 const at = (level: number) => ({ meter: "spend_limit", level, usedMicros: 0, limitMicros: 0, message: "" });
180 assert.equal(needsAttention({ paused: null, spike: null, alerts: [at(75)] }), false);
181 assert.equal(needsAttention({ paused: null, spike: null, alerts: [at(90)] }), true);
182});
183
184test("a request says where it stands", () => {
185 const request = {
186 id: "r",
187 workspace: "acme",
188 kind: "limit",
189 amountMicros: 1,
190 reason: "",
191 expectedMonthlyMicros: 0,
192 status: "open",
193 createdBy: "a",
194 createdAt: "",
195 };
196 assert.equal(requestStatus(request), "Waiting for an answer");
197 assert.equal(requestStatus({ ...request, status: "approved", decidedMicros: 2_000_000_000 }), "Approved at $2,000");
198 assert.equal(requestStatus({ ...request, status: "declined" }), "Declined");
199});
200
201const METERS = [
202 { key: "agents", label: "Agents & sandboxes", micros: 1_200_000, quantity: "4 runs" },
203 { key: "builds", label: "Builds", micros: 0, quantity: null },
204 { key: "requests", label: "Requests & CPU", micros: 0, quantity: null },
205 { key: "domains", label: "Custom domains", micros: 120_000, quantity: "1 custom domain" },
206 { key: "git_storage", label: "Git operations & storage", micros: 0, quantity: "1,200 git operations" },
207 { key: "search_scans", label: "Search & security scans", micros: 3_000, quantity: null },
208];
209
210test("the plan shows every meter, with no quota beside any of them", () => {
211 const shown = shownMeters(METERS, true);
212 assert.deepEqual(
213 shown.map((m) => m.key),
214 ["agents", "builds", "requests", "domains", "git_storage", "search_scans"],
215 );
216 for (const meter of shown) assert.ok(!/ of |apps up/i.test(`${meter.label} ${meter.quantity ?? ""}`));
217});
218
219test("a free workspace sees only what it can use, and anything it used anyway", () => {
220 assert.deepEqual(
221 shownMeters(METERS, false).map((m) => m.key),
222 ["agents", "domains", "git_storage", "search_scans"],
223 );
224});
225
226test("storage reads in powers of ten", () => {
227 assert.equal(gigabytes(1_000_000_000), "1 GB");
228 assert.equal(gigabytes(1_500_000_000), "1.5 GB");
229 assert.equal(gigabytes(500_000_000), "500 MB");
230});
231
232test("kinds of work without words of their own are one Other, listed once and last", () => {
233 const slices = [
234 { key: "check", micros: 5, runs: 50 },
235 { key: "review", micros: 3, runs: 5 },
236 { key: "other", micros: 1, runs: 7 },
237 { key: "implement", micros: 9, runs: 4 },
238 { key: "answer", micros: 2, runs: 11 },
239 ];
240 assert.deepEqual(foldTasks(slices), [
241 { key: "review", micros: 3, runs: 5 },
242 { key: "implement", micros: 9, runs: 4 },
243 { key: "other", micros: 8, runs: 68 },
244 ]);
245 assert.deepEqual(foldTasks([{ key: "plan", micros: 1, runs: 1 }]), [{ key: "plan", micros: 1, runs: 1 }]);
246});
247
248test("budget alerts keep the levels ticked, highest first", async () => {
249 const { parseBudgetAlerts } = await import("./billing.ts");
250 assert.deepEqual(parseBudgetAlerts({ alerts: ["50", "100", "75", "12"], pause: "on", webhook: "" }), {
251 ok: true,
252 value: { alerts: [100, 75, 50], pauseAtLimit: true, webhook: null },
253 });
254 assert.equal(parseBudgetAlerts({ alerts: [], webhook: "http://x.test" }).ok, false);
255 assert.deepEqual(parseBudgetAlerts({ alerts: [], webhook: "https://hooks.acme.test/g1t" }).ok, true);
256});
257