|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { readFileSync, readdirSync } from "node:fs"; |
| 3 | +import { repoPath } from "../helpers/repo-root"; |
| 4 | + |
| 5 | +type WorkflowStep = { |
| 6 | + name?: string; |
| 7 | + if?: string; |
| 8 | + uses?: string; |
| 9 | + with?: Record<string, unknown>; |
| 10 | + run?: string; |
| 11 | + shell?: string; |
| 12 | +}; |
| 13 | + |
| 14 | +type WorkflowJob = { |
| 15 | + needs?: string[]; |
| 16 | + strategy?: { matrix?: { include?: Array<{ os?: string }> } }; |
| 17 | + steps?: WorkflowStep[]; |
| 18 | +}; |
| 19 | + |
| 20 | +type Workflow = { jobs?: Record<string, WorkflowJob | undefined> }; |
| 21 | + |
| 22 | +function needsOf(job: WorkflowJob | undefined): string[] { |
| 23 | + if (job?.needs === undefined) return []; |
| 24 | + return typeof job.needs === "string" ? [job.needs] : job.needs; |
| 25 | +} |
| 26 | + |
| 27 | +function readWorkflow(...segments: string[]): Workflow { |
| 28 | + return Bun.YAML.parse(readFileSync(repoPath(...segments), "utf8")) as Workflow; |
| 29 | +} |
| 30 | + |
| 31 | +function triggerPaths(workflowText: string, trigger: string, until: string): string[] { |
| 32 | + const afterTrigger = workflowText.split(`${trigger}:`)[1]?.split(`${until}:`)[0]; |
| 33 | + expect(afterTrigger).toBeDefined(); |
| 34 | + return afterTrigger! |
| 35 | + .split("\n") |
| 36 | + .map(line => line.trim()) |
| 37 | + .filter(line => line.startsWith('- "')) |
| 38 | + .map(line => line.slice(3, -1)); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Contracts for the release pipeline itself. Each assertion encodes a defect that a green |
| 43 | + * workflow can still carry: a bash script silently reinterpreted by PowerShell on a Windows |
| 44 | + * runner, a checksum that records a path its verifier cannot resolve, publication that outruns |
| 45 | + * packaging, and a service gate that names one file while the implementation is a directory. |
| 46 | + */ |
| 47 | +describe("release pipeline contract", () => { |
| 48 | + const release = readWorkflow(".github", "workflows", "release.yml"); |
| 49 | + |
| 50 | + test("every multi-line script on a Windows runner declares an explicit shell", () => { |
| 51 | + for (const jobId of ["package-standalone", "package-desktop"]) { |
| 52 | + const job = release.jobs?.[jobId]; |
| 53 | + expect(job, jobId).toBeDefined(); |
| 54 | + const runsOnWindows = job!.strategy?.matrix?.include?.some(entry => entry.os === "windows-latest"); |
| 55 | + expect(runsOnWindows, `${jobId} exercises Windows`).toBe(true); |
| 56 | + for (const step of job!.steps ?? []) { |
| 57 | + // A single command line is shell-neutral; a script block implies shell-specific |
| 58 | + // syntax and must not fall back to the runner's default shell on Windows. |
| 59 | + if (typeof step.run !== "string" || !step.run.includes("\n")) continue; |
| 60 | + // Steps fenced away from Windows never meet PowerShell. |
| 61 | + if (/runner\.os\s*==\s*'(Linux|macOS)'/.test(step.if ?? "")) continue; |
| 62 | + expect(step.shell, `${jobId} / ${step.name}`).toBeDefined(); |
| 63 | + } |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + test("the release asset rename runs under bash", () => { |
| 68 | + const step = release.jobs?.["package-desktop"]?.steps |
| 69 | + ?.find(candidate => candidate.run?.includes("collect-release-assets.ts")); |
| 70 | + expect(step).toBeDefined(); |
| 71 | + // The script uses backslash continuations and "$VAR" expansion, which PowerShell does |
| 72 | + // not read the way bash does; on the Windows matrix this step is only correct under bash. |
| 73 | + expect(step!.shell).toBe("bash"); |
| 74 | + }); |
| 75 | + |
| 76 | + test("standalone checksums record bare names that resolve where the verifier runs", () => { |
| 77 | + const archive = release.jobs?.["package-standalone"]?.steps |
| 78 | + ?.find(candidate => candidate.run?.includes("sha256sum")); |
| 79 | + expect(archive).toBeDefined(); |
| 80 | + const checksumLines = archive!.run!.split("\n") |
| 81 | + .filter(line => line.includes("sha256sum") && !line.trim().startsWith("#")); |
| 82 | + expect(checksumLines.length).toBeGreaterThanOrEqual(2); |
| 83 | + for (const line of checksumLines) { |
| 84 | + const argument = /sha256sum\s+"([^"]+)"/.exec(line)?.[1]; |
| 85 | + expect(argument, line).toBeDefined(); |
| 86 | + // shasum -c resolves the recorded path relative to the verifier's working directory, |
| 87 | + // which is dist/release; any directory prefix names a file that cannot exist there. |
| 88 | + expect(argument!).not.toContain("/"); |
| 89 | + // The redirect target gets the same treatment: a bare output name is what makes the |
| 90 | + // checksum file land in the directory the upload glob scans. |
| 91 | + const output = />\s+"([^"]+)"/.exec(line)?.[1]; |
| 92 | + expect(output, line).toBeDefined(); |
| 93 | + expect(output!).not.toContain("/"); |
| 94 | + } |
| 95 | + |
| 96 | + // The bare names above only resolve end to end if the step checksums from the directory |
| 97 | + // the artifact lives in (it leaves the per-target build directory first), if the upload |
| 98 | + // glob picks the checksum file up, and if the download flattens every artifact beside |
| 99 | + // the verifier. Locking only the final shasum line would leave those joints unguarded. |
| 100 | + // YAML block scalars are dedented on parse, so the script's own lines carry no |
| 101 | + // indentation here. |
| 102 | + expect(archive!.run).toMatch(/^ *cd \.\.\/\.\.$/m); |
| 103 | + const upload = release.jobs?.["package-standalone"]?.steps |
| 104 | + ?.find(candidate => candidate.uses?.startsWith("actions/upload-artifact@")); |
| 105 | + expect(String(upload?.with?.path)).toContain("dist/ocx-*.sha256"); |
| 106 | + |
| 107 | + const download = release.jobs?.["attach-release"]?.steps |
| 108 | + ?.find(candidate => candidate.uses?.startsWith("actions/download-artifact@") |
| 109 | + && candidate.with?.pattern === "standalone-*"); |
| 110 | + expect(download?.with?.["merge-multiple"]).toBe(true); |
| 111 | + expect(download?.with?.path).toBe("dist/release"); |
| 112 | + |
| 113 | + const verify = release.jobs?.["attach-release"]?.steps |
| 114 | + ?.find(candidate => candidate.run?.includes("shasum")); |
| 115 | + expect(verify).toBeDefined(); |
| 116 | + expect(verify!.run).toContain("cd dist/release"); |
| 117 | + expect(verify!.run).toContain("shasum -a 256 -c ./*.sha256"); |
| 118 | + }); |
| 119 | + |
| 120 | + test("publication waits for both packaging jobs", () => { |
| 121 | + const publish = release.jobs?.publish; |
| 122 | + expect(publish).toBeDefined(); |
| 123 | + expect(needsOf(publish).sort()) |
| 124 | + .toEqual(["package-desktop", "package-standalone", "validate-dispatch"]); |
| 125 | + |
| 126 | + const attach = release.jobs?.["attach-release"]; |
| 127 | + expect(attach).toBeDefined(); |
| 128 | + expect(needsOf(attach).sort()).toEqual(["package-desktop", "package-standalone", "publish"]); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +describe("service lifecycle trigger coverage", () => { |
| 133 | + const lifecycleText = readFileSync(repoPath(".github", "workflows", "service-lifecycle.yml"), "utf8"); |
| 134 | + const releaseText = readFileSync(repoPath(".github", "workflows", "release.yml"), "utf8"); |
| 135 | + |
| 136 | + test("both triggers cover the service directory and the desktop shell", () => { |
| 137 | + const pushPaths = triggerPaths(lifecycleText, "push", "workflow_dispatch"); |
| 138 | + const prPaths = triggerPaths(lifecycleText, "pull_request", "push"); |
| 139 | + for (const paths of [prPaths, pushPaths]) { |
| 140 | + expect(paths).toContain("src/service.ts"); |
| 141 | + expect(paths).toContain("src/service/**"); |
| 142 | + expect(paths).toContain("desktop/**"); |
| 143 | + } |
| 144 | + expect([...prPaths].sort()).toEqual([...pushPaths].sort()); |
| 145 | + }); |
| 146 | + |
| 147 | + test("the release service gate matches every implemented service module and desktop file", () => { |
| 148 | + const gateSource = releaseText.match(/grep -Eq '(\^\([^']+\)\$)'/)?.[1]; |
| 149 | + expect(gateSource).toBeDefined(); |
| 150 | + const gate = new RegExp(gateSource!); |
| 151 | + |
| 152 | + // Derived from the tree, not restated: the service implementation is a directory, so |
| 153 | + // every module in it must satisfy the gate that demands lifecycle evidence. |
| 154 | + const serviceModules = readdirSync(repoPath("src", "service")) |
| 155 | + .filter(entry => entry.endsWith(".ts")); |
| 156 | + expect(serviceModules.length).toBeGreaterThanOrEqual(10); |
| 157 | + for (const module of serviceModules) { |
| 158 | + expect(gate.test(`src/service/${module}`), `src/service/${module}`).toBe(true); |
| 159 | + } |
| 160 | + expect(gate.test("src/service.ts")).toBe(true); |
| 161 | + |
| 162 | + const desktopSurfaces = [ |
| 163 | + ...readdirSync(repoPath("desktop", "scripts")).map(entry => `desktop/scripts/${entry}`), |
| 164 | + ...readdirSync(repoPath("desktop", "src-tauri", "src")).map(entry => `desktop/src-tauri/src/${entry}`), |
| 165 | + ]; |
| 166 | + expect(desktopSurfaces.length).toBeGreaterThanOrEqual(10); |
| 167 | + for (const path of desktopSurfaces) { |
| 168 | + expect(gate.test(path), path).toBe(true); |
| 169 | + } |
| 170 | + |
| 171 | + expect(gate.test("src/router.ts")).toBe(false); |
| 172 | + expect(gate.test("docs-site/src/pages/index.astro")).toBe(false); |
| 173 | + }); |
| 174 | +}); |
0 commit comments