| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { HEARTBEAT_MS, type DesktopRunnerView, backendOf, diagnosisOf, jobHref, reportDue, reportKey, reportOf } from "./desktop-runner.ts"; |
| 5 | |
| 6 | function view(over: Partial<DesktopRunnerView> = {}): DesktopRunnerView { |
| 7 | return { |
| 8 | diagnosis: { |
| 9 | code: "no_docker", |
| 10 | tone: "problem", |
| 11 | title: "Not running: Docker isn't running", |
| 12 | cause: "Docker isn't running.", |
| 13 | fix: "Start Docker Desktop.", |
| 14 | actions: [{ id: "open_docker", label: "Open Docker" }], |
| 15 | }, |
| 16 | closed: { code: "off", tone: "off", title: "g1t is closed on build-box", cause: null, fix: "Open g1t on build-box.", actions: [] }, |
| 17 | running: false, |
| 18 | version: "0.2.3", |
| 19 | backend: "docker", |
| 20 | workspace: "acme", |
| 21 | workspaces: [{ slug: "acme", name: "Acme" }], |
| 22 | enabled: true, |
| 23 | paused: false, |
| 24 | link: "https://g1t.sh/acme/-/runners", |
| 25 | machine: "build-box", |
| 26 | platform: "windows", |
| 27 | log: ["2026-10-12T08:00:00Z a line"], |
| 28 | ...over, |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | test("a report carries the app's words, without its buttons or log", () => { |
| 33 | const report = reportOf(view()); |
| 34 | assert.deepEqual(report, { |
| 35 | workspace: "acme", |
| 36 | machine: "build-box", |
| 37 | backend: "docker", |
| 38 | version: "0.2.3", |
| 39 | diagnosis: { code: "no_docker", tone: "problem", title: "Not running: Docker isn't running", cause: "Docker isn't running.", fix: "Start Docker Desktop." }, |
| 40 | closed: { code: "off", tone: "off", title: "g1t is closed on build-box", cause: null, fix: "Open g1t on build-box." }, |
| 41 | }); |
| 42 | assert.equal(reportOf(view({ workspace: null })), null); |
| 43 | }); |
| 44 | |
| 45 | test("a report goes when what the page shows changes, and as a heartbeat", () => { |
| 46 | const report = reportOf(view())!; |
| 47 | const at = 1_000_000; |
| 48 | assert.equal(reportDue(null, report, at), true); |
| 49 | const last = { key: reportKey(report), at }; |
| 50 | assert.equal(reportDue(last, report, at + 1000), false); |
| 51 | assert.equal(reportDue(last, report, at + HEARTBEAT_MS), true); |
| 52 | const busy = reportOf(view({ diagnosis: { ...view().diagnosis, code: "busy", tone: "working", title: "Running Build (acme/web)" } }))!; |
| 53 | assert.equal(reportDue(last, busy, at + 1000), true); |
| 54 | }); |
| 55 | |
| 56 | test("a sandboxed desktop reports each kind of job, without its buttons", () => { |
| 57 | const linux = { id: "linux", label: "Linux jobs", tone: "ok" as const, title: "Waiting for jobs", cause: null, fix: null, actions: [] }; |
| 58 | const windows = { |
| 59 | id: "windows", |
| 60 | label: "Windows jobs", |
| 61 | tone: "off" as const, |
| 62 | title: "Not available", |
| 63 | cause: "Windows Sandbox is off, or this edition of Windows has none: Home doesn't.", |
| 64 | fix: "Linux jobs still run.", |
| 65 | actions: [{ id: "windows_features", label: "Open Windows features" }], |
| 66 | }; |
| 67 | const sandboxed = view({ |
| 68 | backend: "sandboxed", |
| 69 | diagnosis: { code: "idle", tone: "ok", title: "Waiting for jobs", cause: null, fix: null, actions: [], capabilities: [linux, windows] }, |
| 70 | }); |
| 71 | const report = reportOf(sandboxed)!; |
| 72 | assert.deepEqual(report.diagnosis.capabilities, [ |
| 73 | { id: "linux", label: "Linux jobs", tone: "ok", title: "Waiting for jobs", cause: null, fix: null }, |
| 74 | { id: "windows", label: "Windows jobs", tone: "off", title: "Not available", cause: windows.cause, fix: "Linux jobs still run." }, |
| 75 | ]); |
| 76 | // One kind of job changing is a report due. |
| 77 | const last = { key: reportKey(report), at: 0 }; |
| 78 | const running = reportOf( |
| 79 | view({ |
| 80 | ...sandboxed, |
| 81 | diagnosis: { ...sandboxed.diagnosis, capabilities: [linux, { ...windows, tone: "ok", title: "Waiting for jobs", cause: null, fix: null }] }, |
| 82 | }), |
| 83 | )!; |
| 84 | assert.equal(reportDue(last, running, 1000), true); |
| 85 | // What the server keeps: only kinds of job that read, and none for a desktop without any. |
| 86 | const kept = diagnosisOf({ ...report.diagnosis, capabilities: [...report.diagnosis.capabilities!, { id: "x", tone: "loud" }, "linux"] })!; |
| 87 | assert.equal(kept.capabilities?.length, 2); |
| 88 | assert.equal(diagnosisOf({ code: "idle", tone: "ok", title: "Waiting for jobs" })!.capabilities, undefined); |
| 89 | assert.equal(reportOf(view())!.diagnosis.capabilities, undefined); |
| 90 | }); |
| 91 | |
| 92 | test("each kind of job reports its state, whether the person stopped it, and its job, without what only works here", () => { |
| 93 | const linux = { |
| 94 | id: "linux", |
| 95 | label: "Linux jobs", |
| 96 | tone: "working" as const, |
| 97 | title: "Running a job", |
| 98 | cause: null, |
| 99 | fix: null, |
| 100 | state: "busy" as const, |
| 101 | about: "A Linux VM of its own, each job in a fresh container", |
| 102 | enabled: true, |
| 103 | job: { kind: "workflow", name: "build", repo: "acme/web", link: "https://g1t.sh/acme/web/actions" }, |
| 104 | runner: "build-box-linux", |
| 105 | isolation: "nested-container", |
| 106 | version: "0.3.0", |
| 107 | available: true, |
| 108 | can_toggle: true, |
| 109 | progress: null, |
| 110 | actions: [], |
| 111 | }; |
| 112 | const windows = { |
| 113 | id: "windows", |
| 114 | label: "Windows jobs", |
| 115 | tone: "off" as const, |
| 116 | title: "Stopped", |
| 117 | cause: "You stopped Windows jobs on this computer. They stay stopped until you start them.", |
| 118 | fix: null, |
| 119 | state: "stopped" as const, |
| 120 | enabled: false, |
| 121 | can_toggle: true, |
| 122 | actions: [], |
| 123 | }; |
| 124 | const sandboxed = view({ |
| 125 | backend: "sandboxed", |
| 126 | diagnosis: { |
| 127 | code: "busy", |
| 128 | tone: "working", |
| 129 | title: "Running build (acme/web)", |
| 130 | cause: null, |
| 131 | fix: null, |
| 132 | actions: [], |
| 133 | capabilities: [linux, windows], |
| 134 | in_capabilities: true, |
| 135 | }, |
| 136 | }); |
| 137 | const report = reportOf(sandboxed)!; |
| 138 | assert.equal(report.diagnosis.in_capabilities, true); |
| 139 | assert.deepEqual(report.diagnosis.capabilities![0], { |
| 140 | id: "linux", |
| 141 | label: "Linux jobs", |
| 142 | tone: "working", |
| 143 | title: "Running a job", |
| 144 | cause: null, |
| 145 | fix: null, |
| 146 | state: "busy", |
| 147 | about: "A Linux VM of its own, each job in a fresh container", |
| 148 | enabled: true, |
| 149 | job: { kind: "workflow", name: "build", repo: "acme/web" }, |
| 150 | runner: "build-box-linux", |
| 151 | isolation: "nested-container", |
| 152 | version: "0.3.0", |
| 153 | }); |
| 154 | assert.equal(report.diagnosis.capabilities![1].enabled, false); |
| 155 | // Starting Windows jobs again is a report due. |
| 156 | const last = { key: reportKey(report), at: 0 }; |
| 157 | const started = reportOf( |
| 158 | view({ |
| 159 | ...sandboxed, |
| 160 | diagnosis: { ...sandboxed.diagnosis, capabilities: [linux, { ...windows, enabled: true, state: "starting", title: "Starting", tone: "working" }] }, |
| 161 | }), |
| 162 | )!; |
| 163 | assert.equal(reportDue(last, started, 1000), true); |
| 164 | // What the server keeps reads back the same, and drops what isn't one of the app's. |
| 165 | const kept = diagnosisOf(JSON.parse(JSON.stringify(report.diagnosis)))!; |
| 166 | assert.deepEqual(kept, report.diagnosis); |
| 167 | const odd = diagnosisOf({ |
| 168 | ...report.diagnosis, |
| 169 | capabilities: [{ ...report.diagnosis.capabilities![0], state: "dancing", enabled: "yes", job: { name: "x" } }], |
| 170 | })!; |
| 171 | assert.equal(odd.capabilities![0].state, undefined); |
| 172 | assert.equal(odd.capabilities![0].enabled, undefined); |
| 173 | assert.equal(odd.capabilities![0].job, undefined); |
| 174 | // Only a diagnosis with kinds of job says they say it all. |
| 175 | assert.equal(diagnosisOf({ code: "idle", tone: "ok", title: "Waiting for jobs", in_capabilities: true })!.in_capabilities, undefined); |
| 176 | }); |
| 177 | |
| 178 | test("a kind of job's work links to its run when the runner says which", () => { |
| 179 | const capability = { runner: "build-box-linux", job: { kind: "workflow", name: "build", repo: "acme/web" } }; |
| 180 | const runners = [ |
| 181 | { name: "build-box-linux", work: { kind: "workflow" as const, id: "job_1", name: "build", repo: "acme/web", runId: "run_9", startedAt: null } }, |
| 182 | ]; |
| 183 | assert.equal(jobHref(capability, runners), "/acme/web/actions/runs/run_9"); |
| 184 | assert.equal(jobHref(capability, []), "/acme/web/actions"); |
| 185 | assert.equal(jobHref({ runner: null, job: { kind: "agent", name: "fix", repo: "acme/web" } }, runners), "/acme/web/pulls"); |
| 186 | assert.equal(jobHref({ runner: "x", job: null }, runners), null); |
| 187 | }); |
| 188 | |
| 189 | test("only a diagnosis and a backend the app makes are kept", () => { |
| 190 | assert.deepEqual(diagnosisOf({ code: "idle", tone: "ok", title: "Waiting for jobs", cause: "Running sandboxed.", extra: 1 }), { |
| 191 | code: "idle", |
| 192 | tone: "ok", |
| 193 | title: "Waiting for jobs", |
| 194 | cause: "Running sandboxed.", |
| 195 | fix: null, |
| 196 | }); |
| 197 | assert.equal(diagnosisOf({ code: "idle", tone: "loud", title: "x" }), null); |
| 198 | assert.equal(diagnosisOf("idle"), null); |
| 199 | assert.equal(backendOf("host"), "host"); |
| 200 | assert.equal(backendOf("vm"), null); |
| 201 | }); |
| 202 | |
| 203 | test("a report says how the runner's updates stand, without the app's own words and button", () => { |
| 204 | const update = { |
| 205 | checked_at: "2026-10-12T12:00:00Z", |
| 206 | checked: "Checked 12 min ago", |
| 207 | checking: false, |
| 208 | ready: "0.3.2", |
| 209 | note: null, |
| 210 | check: { id: "check_updates", label: "Check for runner updates" }, |
| 211 | }; |
| 212 | const linux = { |
| 213 | id: "linux", |
| 214 | label: "Linux jobs", |
| 215 | tone: "working" as const, |
| 216 | title: "Running a job", |
| 217 | cause: null, |
| 218 | fix: null, |
| 219 | actions: [], |
| 220 | version: "0.3.1", |
| 221 | update: "0.3.2 ready, installs after this job", |
| 222 | }; |
| 223 | const sandboxed = view({ |
| 224 | backend: "sandboxed", |
| 225 | diagnosis: { |
| 226 | code: "busy", |
| 227 | tone: "working", |
| 228 | title: "Running build (acme/web)", |
| 229 | cause: null, |
| 230 | fix: null, |
| 231 | actions: [], |
| 232 | capabilities: [linux], |
| 233 | in_capabilities: true, |
| 234 | update, |
| 235 | }, |
| 236 | }); |
| 237 | const report = reportOf(sandboxed)!; |
| 238 | assert.deepEqual(report.diagnosis.update, { checked_at: "2026-10-12T12:00:00Z", ready: "0.3.2", note: null }); |
| 239 | assert.equal(report.diagnosis.capabilities![0].update, "0.3.2 ready, installs after this job"); |
| 240 | // A look that found something, or a newer one put in, is reported at once. |
| 241 | const last = { key: reportKey(report), at: 0 }; |
| 242 | const looked = reportOf(view({ ...sandboxed, diagnosis: { ...sandboxed.diagnosis, update: { ...update, checked_at: "2026-10-12T13:00:00Z" } } }))!; |
| 243 | assert.equal(reportDue(last, looked, 1000), true); |
| 244 | const installed = reportOf(view({ ...sandboxed, diagnosis: { ...sandboxed.diagnosis, capabilities: [{ ...linux, version: "0.3.2", update: undefined }] } }))!; |
| 245 | assert.equal(reportDue(last, installed, 1000), true); |
| 246 | // As the server reads it back: a time that isn't one is dropped, and an update that says nothing is none. |
| 247 | const kept = diagnosisOf(JSON.parse(JSON.stringify(report.diagnosis)))!; |
| 248 | assert.deepEqual(kept.update, report.diagnosis.update); |
| 249 | assert.equal(kept.capabilities![0].update, "0.3.2 ready, installs after this job"); |
| 250 | assert.deepEqual(diagnosisOf({ ...report.diagnosis, update: { checked_at: "soon", ready: "0.3.2" } })!.update, { |
| 251 | checked_at: null, |
| 252 | ready: "0.3.2", |
| 253 | note: null, |
| 254 | }); |
| 255 | assert.equal(diagnosisOf({ ...report.diagnosis, update: { checked_at: 5 } })!.update, undefined); |
| 256 | // An app from before updates were reported says nothing of them. |
| 257 | assert.equal(reportOf(view())!.diagnosis.update, undefined); |
| 258 | }); |