Skip to content
120 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { AgentRun, RunnerActivity, UsageReport } from "@g1t/contracts";
5
6import { cloudNow, cloudPrices, machineTime, ownWaiting, perMinute, runnerCost } from "./runners.ts";
7
8function run(id: string, status: AgentRun["status"], over: Partial<AgentRun> = {}): AgentRun {
9 return {
10 id,
11 repo: { namespace: "acme", name: "web" },
12 number: 12,
13 title: "Fix the flaky checkout test",
14 kind: "checks",
15 agent: "g1t",
16 model: null,
17 status,
18 step: null,
19 steps: [],
20 stepCount: 0,
21 startedBy: null,
22 error: null,
23 costUsd: null,
24 turns: null,
25 createdAt: "2026-10-10T10:00:00Z",
26 startedAt: "2026-10-10T10:01:00Z",
27 finishedAt: null,
28 updatedAt: "2026-10-10T10:02:00Z",
29 ...over,
30 };
31}
32
33const activity: RunnerActivity = {
34 cloud_jobs: [{ id: "job_1", name: "test (ubuntu)", run_id: "wr_1", repo: "acme/web", started_at: "2026-10-10T10:05:00Z" }],
35 cloud_jobs_queued: 2,
36 handed_over: [
37 { id: "rtk_1", run_id: "run_mine", kind: "implement", title: "On my runner", repo: "acme/web", status: "in_progress", runner_name: "mac", created_at: "2026-10-10T09:00:00Z", started_at: null },
38 { id: "rtk_2", run_id: null, kind: "checks", title: "Waiting", repo: "acme/web", status: "queued", runner_name: null, created_at: "2026-10-10T09:00:00Z", started_at: null },
39 ],
40 self_hosted_jobs_queued: 3,
41};
42
43test("g1t cloud is the running sandbox runs not handed to the workspace's runners, and the jobs in g1t's sandboxes", () => {
44 const now = cloudNow([run("run_a", "running"), run("run_mine", "running"), run("run_q", "queued")], activity, 200, 100)!;
45 assert.deepEqual(
46 now.running.map((work) => [work.kind, work.id, work.href]),
47 [
48 ["workflow", "job_1", "/acme/web/actions/runs/wr_1"],
49 ["agent", "run_a", "/acme/web/pull/12"],
50 ],
51 );
52 // One queued sandbox run and two queued jobs.
53 assert.equal(now.queued, 3);
54 assert.equal(now.more, false);
55});
56
57test("a service that does not answer leaves only its part out", () => {
58 assert.equal(cloudNow(null, null, 200, 100), null);
59 assert.equal(cloudNow([run("run_a", "running")], null, 200, 100)!.running.length, 1);
60 assert.equal(ownWaiting(null), null);
61 assert.deepEqual(ownWaiting(activity), { tasks: 1, jobs: 3 });
62 assert.equal(runnerCost(null), null);
63});
64
65test("a full list says there may be more", () => {
66 assert.equal(cloudNow([run("run_a", "running")], activity, 1, 100)!.more, true);
67});
68
69test("this month's time adds agent sandbox and sandbox time for g1t cloud, and self-hosted time for the workspace's own", () => {
70 const meter = (key: string, quantity: number, micros: number) => ({ key, label: key, product: "x", unit: "seconds", quantity, micros, daily: [], byProject: [] });
71 const report = {
72 from: "2026-10-01",
73 until: "2026-10-10",
74 free: false,
75 products: [
76 { key: "agent", label: "Agent", micros: 0, meters: [meter("agent_sandbox", 600, 1_000_000), meter("agent_models", 9, 5_000_000)] },
77 {
78 key: "sandboxes",
79 label: "Sandboxes",
80 micros: 0,
81 meters: [
82 meter("runner_2-core", 10, 40_000),
83 meter("runner_4-core", 2, 14_000),
84 meter("sandbox", 120, 200_000),
85 meter("self_hosted", 3600, 0),
86 ],
87 },
88 ],
89 } as unknown as UsageReport;
90 // Runner minutes count as their minutes; older sandbox time by the second.
91 assert.deepEqual(runnerCost(report), {
92 from: "2026-10-01",
93 until: "2026-10-10",
94 cloudSeconds: 720 + 12 * 60,
95 cloudMicros: 1_254_000,
96 ownSeconds: 3600,
97 free: false,
98 });
99});
100
101test("prices are each machine's rate a minute, from the catalogue", () => {
102 assert.deepEqual(
103 cloudPrices().map((p) => [p.machine, p.perMinuteMicros, p.title]),
104 [
105 ["2-core", 4000, "2-core: 2 vCPU, 8 GiB, 16 GB"],
106 ["4-core", 7000, "4-core: 4 vCPU, 12 GiB, 20 GB"],
107 ],
108 );
109 assert.equal(perMinute(4000), "$0.004");
110});
111
112test("machine time and prices read as people say them", () => {
113 assert.equal(machineTime(0), "0 min");
114 assert.equal(machineTime(45), "45s");
115 assert.equal(machineTime(720), "12 min");
116 assert.equal(machineTime(12_240), "3.4 h");
117 assert.equal(perMinute(1500), "$0.0015");
118 assert.equal(perMinute(870), "$0.00087");
119 assert.equal(perMinute(0), "$0");
120});