| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { HEARTBEAT_MS, type DesktopRunnerView, backendOf, diagnosisOf, reportDue, reportKey, reportOf } from "./desktop-runner.ts"; |
| 5 | |
| 6 | function view(over: Partial<DesktopRunnerView> = {}): DesktopRunnerView { |
| 7 | return { |
| 8 | diagnosis: { code: "no_docker", tone: "problem", title: "Not running: Docker isn't running", cause: "Docker isn't running.", fix: "Start Docker Desktop.", actions: [{ id: "open_docker", label: "Open Docker" }] }, |
| 9 | closed: { code: "off", tone: "off", title: "g1t is closed on build-box", cause: null, fix: "Open g1t on build-box.", actions: [] }, |
| 10 | running: false, |
| 11 | version: "0.2.3", |
| 12 | backend: "docker", |
| 13 | workspace: "acme", |
| 14 | workspaces: [{ slug: "acme", name: "Acme" }], |
| 15 | enabled: true, |
| 16 | paused: false, |
| 17 | link: "https://g1t.sh/acme/-/runners", |
| 18 | machine: "build-box", |
| 19 | platform: "windows", |
| 20 | log: ["2026-10-12T08:00:00Z a line"], |
| 21 | ...over, |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | test("a report carries the app's words, without its buttons or log", () => { |
| 26 | const report = reportOf(view()); |
| 27 | assert.deepEqual(report, { |
| 28 | workspace: "acme", |
| 29 | machine: "build-box", |
| 30 | backend: "docker", |
| 31 | version: "0.2.3", |
| 32 | diagnosis: { code: "no_docker", tone: "problem", title: "Not running: Docker isn't running", cause: "Docker isn't running.", fix: "Start Docker Desktop." }, |
| 33 | closed: { code: "off", tone: "off", title: "g1t is closed on build-box", cause: null, fix: "Open g1t on build-box." }, |
| 34 | }); |
| 35 | assert.equal(reportOf(view({ workspace: null })), null); |
| 36 | }); |
| 37 | |
| 38 | test("a report goes when what the page shows changes, and as a heartbeat", () => { |
| 39 | const report = reportOf(view())!; |
| 40 | const at = 1_000_000; |
| 41 | assert.equal(reportDue(null, report, at), true); |
| 42 | const last = { key: reportKey(report), at }; |
| 43 | assert.equal(reportDue(last, report, at + 1000), false); |
| 44 | assert.equal(reportDue(last, report, at + HEARTBEAT_MS), true); |
| 45 | const busy = reportOf(view({ diagnosis: { ...view().diagnosis, code: "busy", tone: "working", title: "Running Build (acme/web)" } }))!; |
| 46 | assert.equal(reportDue(last, busy, at + 1000), true); |
| 47 | }); |
| 48 | |
| 49 | test("only a diagnosis and a backend the app makes are kept", () => { |
| 50 | assert.deepEqual(diagnosisOf({ code: "idle", tone: "ok", title: "Waiting for jobs", cause: "Running sandboxed.", extra: 1 }), { |
| 51 | code: "idle", |
| 52 | tone: "ok", |
| 53 | title: "Waiting for jobs", |
| 54 | cause: "Running sandboxed.", |
| 55 | fix: null, |
| 56 | }); |
| 57 | assert.equal(diagnosisOf({ code: "idle", tone: "loud", title: "x" }), null); |
| 58 | assert.equal(diagnosisOf("idle"), null); |
| 59 | assert.equal(backendOf("host"), "host"); |
| 60 | assert.equal(backendOf("vm"), null); |
| 61 | }); |