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.

264 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import {
5 type ComputeEntitlements,
6 type ComputeKind,
7 type PlanMemory,
8 ComputeGate,
9 actualMicros,
10 alwaysPasses,
11 entitlementsKeptSeconds,
12 localRefusal,
13 onBillingError,
14 readEntitlements,
15 refusalCode,
16 refusalMessage,
17 sandboxEstimateMicros,
18} from "../../../packages/contracts/src/compute.ts";
19import { dockerFor, throughWorker, withPlanLimits } from "./egress.ts";
20
21const repo = { namespace: "acme", name: "web" };
22
23function ent(change: Partial<ComputeEntitlements> = {}): ComputeEntitlements {
24 return {
25 plan: "paid",
26 compute: true,
27 trialMicrosLeft: 0,
28 trialVerified: true,
29 firstMonth: false,
30 maxRunMinutes: 0,
31 ceilingMicros: 100_000_000,
32 exposureMicros: 0,
33 paused: null,
34 ...change,
35 };
36}
37
38/** A billing service that answers as told, and records what it was asked. */
39function billing(answers: { entitlements?: unknown; reserve?: unknown; fail?: Set<string> }) {
40 const asked: { method: string; body: Record<string, unknown> }[] = [];
41 const binding = {
42 async fetch(url: string, init?: RequestInit): Promise<Response> {
43 const method = url.split("/rpc/")[1];
44 asked.push({ method, body: JSON.parse(String(init?.body ?? "{}")) });
45 if (answers.fail?.has(method)) return new Response("down", { status: 503 });
46 if (method === "entitlements") return Response.json(answers.entitlements ?? null);
47 if (method === "reserve") return Response.json(answers.reserve ?? { ok: true, value: { id: "rsv_1", paidBy: "credit" } });
48 if (method === "settle") return Response.json({ ok: true, value: true });
49 if (method === "prices") return Response.json({ prices: [{ meter: "sandbox_second", costMicros: 20 }] });
50 return new Response("not found", { status: 404 });
51 },
52 };
53 return { binding, asked };
54}
55
56/** A plan memory that remembers nothing unless told. */
57function memory(plan: ComputeEntitlements["plan"] | null = null): PlanMemory {
58 return { get: async () => plan, put: async () => undefined };
59}
60
61const quiet = () => undefined;
62
63function wire(e: ComputeEntitlements) {
64 // As billing serialises it: camelCase.
65 return { workspace: "acme", ...e };
66}
67
68const request = (kind: ComputeKind, isPublic = false) => ({
69 workspace: "acme",
70 repo,
71 public: isPublic,
72 kind,
73 estimateMicros: 100_000,
74});
75
76// ---- Gating decisions ---------------------------------------------------------------
77
78test("a paid workspace's start is reserved and goes ahead", async () => {
79 const { binding, asked } = billing({ entitlements: wire(ent()) });
80 const admitted = await new ComputeGate(binding, memory(), quiet).admit(request("deploy"));
81 assert.equal(admitted.ok, true);
82 assert.deepEqual(admitted.ok && admitted.reservation, { id: "rsv_1", paidBy: "credit" });
83 const reserve = asked.find((call) => call.method === "reserve")!;
84 assert.deepEqual(reserve.body, { workspace: "acme", repo, public: false, kind: "deploy", estimateMicros: 100_000 });
85});
86
87test("billing's refusal is shown in its own words", async () => {
88 for (const code of ["not_paid", "trial_used", "limit", "oss_pool_empty"] as const) {
89 const { binding } = billing({
90 entitlements: wire(ent({ plan: "free", compute: false })),
91 reserve: { ok: false, error: { code, message: `Refused: ${code}. /acme/-/billing` } },
92 });
93 const admitted = await new ComputeGate(binding, memory(), quiet).admit(request("check", true));
94 assert.equal(admitted.ok, false, code);
95 assert.equal(!admitted.ok && admitted.code, code);
96 assert.equal(!admitted.ok && admitted.message, `Refused: ${code}. /acme/-/billing`);
97 }
98});
99
100test("a refusal without words gets g1t's, with what to do and where", async () => {
101 const { binding } = billing({
102 entitlements: wire(ent({ plan: "free", compute: false })),
103 reserve: { ok: false, error: { code: "not_paid", message: "" } },
104 });
105 const admitted = await new ComputeGate(binding, memory(), quiet).admit(request("deploy"));
106 assert.equal(!admitted.ok && admitted.message, refusalMessage("not_paid", "acme", "deploy"));
107 assert.match(
108 refusalMessage("not_paid", "acme", "deploy"),
109 /^Deployments need a paid workspace\. Start the \$21 plan or try it with \$5 of free usage after a card check: \/acme\/-\/billing$/,
110 );
111});
112
113test("a paused workspace starts nothing, and billing is not asked to reserve", async () => {
114 const { binding, asked } = billing({ entitlements: wire(ent({ paused: "A spend spike is waiting for an owner." })) });
115 const admitted = await new ComputeGate(binding, memory(), quiet).admit(request("workflow"));
116 assert.equal(!admitted.ok && admitted.code, "paused");
117 assert.match(!admitted.ok ? admitted.message : "", /A spend spike is waiting for an owner/);
118 assert.equal(
119 asked.some((call) => call.method === "reserve"),
120 false,
121 );
122});
123
124test("an answer that holds compute back is kept seconds, one that lets it through half a minute", () => {
125 assert.equal(entitlementsKeptSeconds(ent()), 30);
126 assert.equal(entitlementsKeptSeconds(ent({ paused: "A spend spike is waiting for an owner." })), 3);
127 assert.equal(entitlementsKeptSeconds(ent({ plan: "free", compute: false })), 3);
128 // A free workspace with compute (its trial) runs, so it is kept as long.
129 assert.equal(entitlementsKeptSeconds(ent({ plan: "free", compute: true })), 30);
130});
131
132test("internal and enterprise plans pass even when billing refuses", async () => {
133 for (const plan of ["internal", "enterprise"] as const) {
134 const { binding } = billing({
135 entitlements: wire(ent({ plan })),
136 reserve: { ok: false, error: { code: "limit", message: "Over." } },
137 });
138 const admitted = await new ComputeGate(binding, memory(), quiet).admit(request("deploy"));
139 assert.equal(admitted.ok, true, plan);
140 }
141 assert.equal(alwaysPasses("internal") && alwaysPasses("enterprise") && !alwaysPasses("paid"), true);
142});
143
144test("a pause from billing holds for every plan: g1t's own budget and its daily breaker", async () => {
145 for (const plan of ["internal", "enterprise", "paid"] as const) {
146 const message = "flagon-io's monthly budget is used up ($150.00 of $150.00 this month at cost), so new runs wait.";
147 const { binding } = billing({ entitlements: wire(ent({ plan })), reserve: { ok: false, error: { code: "paused", message } } });
148 const admitted = await new ComputeGate(binding, memory(), quiet).admit(request("workflow"));
149 assert.equal(!admitted.ok && admitted.code, "paused", plan);
150 assert.equal(!admitted.ok && admitted.message, message);
151 }
152 // Billing's pause reason reads cleanly inside g1t's sentence.
153 assert.equal(
154 refusalMessage("paused", "acme", "workflow", "Its budget is used up."),
155 "g1t paused compute for this workspace: Its budget is used up. Contact support@g1t.sh to have it looked at.",
156 );
157});
158
159test("billing down: free workspaces fail closed, paying ones go on", async () => {
160 const down = new Set(["entitlements", "reserve"]);
161 // Nothing known about the workspace: treated as free.
162 let gate = new ComputeGate(billing({ fail: down }).binding, memory(null), quiet);
163 let admitted = await gate.admit(request("deploy"));
164 assert.equal(!admitted.ok && admitted.code, "billing_unavailable");
165 // Last seen paid: goes ahead, without a reservation.
166 gate = new ComputeGate(billing({ fail: down }).binding, memory("paid"), quiet);
167 admitted = await gate.admit(request("deploy"));
168 assert.deepEqual(admitted, { ok: true, reservation: null, entitlements: null });
169 // Entitlements answer, reserve errors: the plan decides.
170 gate = new ComputeGate(
171 billing({ entitlements: wire(ent({ plan: "free", compute: false, trialMicrosLeft: 5_000_000 })), fail: new Set(["reserve"]) }).binding,
172 memory(),
173 quiet,
174 );
175 admitted = await gate.admit(request("deploy"));
176 assert.equal(admitted.ok, false);
177 gate = new ComputeGate(billing({ entitlements: wire(ent()), fail: new Set(["reserve"]) }).binding, memory(), quiet);
178 assert.equal((await gate.admit(request("deploy"))).ok, true);
179 assert.equal(onBillingError("free"), "refuse");
180 assert.equal(onBillingError(null), "refuse");
181 assert.equal(onBillingError("paid"), "allow");
182});
183
184test("an answer that is not a refusal counts as billing being down", async () => {
185 const { binding } = billing({
186 entitlements: wire(ent({ plan: "free", compute: false })),
187 reserve: { ok: false, error: { code: "invalid", message: "Unknown method" } },
188 });
189 const admitted = await new ComputeGate(binding, memory(), quiet).admit(request("deploy"));
190 assert.equal(!admitted.ok && admitted.code, "billing_unavailable");
191 assert.equal(refusalCode({ code: "payment_required" }), "not_paid");
192 assert.equal(refusalCode({ code: "conflict", reason: "trial_used" }), "trial_used");
193 assert.equal(refusalCode({ code: "invalid" }), null);
194});
195
196test("entitlements read in either case, and not at all without a plan", () => {
197 const snake = readEntitlements({ plan: "free", trial_micros_left: 5, trial_verified: true, max_run_minutes: 30, paused: "" });
198 assert.equal(snake?.trialMicrosLeft, 5);
199 assert.equal(snake?.maxRunMinutes, 30);
200 assert.equal(snake?.paused, null);
201 assert.equal(readEntitlements({ team: true })?.plan, undefined);
202 assert.equal(readEntitlements({ ok: true, value: { plan: "paid" } })?.plan, "paid");
203});
204
205test("before they try: what a free workspace can start, by kind and repository", () => {
206 const free = ent({ plan: "free", compute: false, trialVerified: false });
207 assert.equal(localRefusal(free, "deploy", false), "not_paid");
208 assert.equal(localRefusal(free, "check", true), "not_paid", "the pool needs a card check");
209 const verified = ent({ plan: "free", compute: false, trialVerified: true, trialMicrosLeft: 0 });
210 // The open-source path: public repositories' checks, workflows and queue.
211 for (const kind of ["check", "workflow", "queue"] as const) assert.equal(localRefusal(verified, kind, true), null, kind);
212 assert.equal(localRefusal(verified, "deploy", true), "trial_used");
213 assert.equal(localRefusal(verified, "check", false), "trial_used");
214 const trial = ent({ plan: "free", compute: true, trialVerified: true, trialMicrosLeft: 4_000_000 });
215 assert.equal(localRefusal(trial, "deploy", false), null);
216 assert.equal(localRefusal(ent(), "deploy", false), null);
217 assert.equal(localRefusal(ent({ paused: "Held." }), "deploy", false), "paused");
218});
219
220// ---- Estimates ------------------------------------------------------------------------
221
222test("an estimate is the sandbox for its whole time cap", () => {
223 assert.equal(sandboxEstimateMicros(60, 25), 90_000);
224 assert.equal(sandboxEstimateMicros(-5, 25), 0);
225});
226
227test("what work cost: its seconds at the sandbox price", () => {
228 assert.equal(actualMicros(600, 20), 12_000);
229 assert.equal(actualMicros(-10, 20), 0);
230});
231
232test("the gate reads the sandbox price from the price book", async () => {
233 const gate = new ComputeGate(billing({}).binding, memory(), quiet);
234 assert.equal(await gate.microsPerSecond(), 20);
235 const down = new ComputeGate(billing({ fail: new Set(["prices"]) }).binding, memory(), quiet);
236 assert.equal(await down.microsPerSecond(), 63);
237});
238
239// ---- Caps -------------------------------------------------------------------------------
240
241test("the time cap is the lower of the guardrails' and the plan's", () => {
242 const guard = { policy: { minutes: {} }, minutes: 90 };
243 assert.equal(withPlanLimits(guard, { minutes: 60 }).minutes, 60);
244 assert.equal(withPlanLimits(guard, { minutes: null }).minutes, 90);
245});
246
247// ---- Sandboxes' network ------------------------------------------------------------------
248
249test("only a job that may be given brokered secrets passes the Worker", () => {
250 assert.equal(throughWorker({ brokered: true }, undefined), true);
251 assert.equal(throughWorker({ brokered: true }, "on"), true);
252 // Everything else goes straight out.
253 assert.equal(throughWorker({ brokered: false }, undefined), false);
254 assert.equal(throughWorker({}, undefined), false);
255 assert.equal(throughWorker(null, undefined), false);
256 // The operator's switch.
257 assert.equal(throughWorker({ brokered: true }, " OFF "), false);
258});
259
260test("workflow jobs get Docker unless the operator turns it off", () => {
261 assert.equal(dockerFor(undefined), "on");
262 assert.equal(dockerFor("on"), "on");
263 assert.equal(dockerFor(" OFF "), "off");
264});