|
| 1 | +/** |
| 2 | + * aggregate-build-results.test.ts — covers the per-slot aggregator that |
| 3 | + * runs in the `aggregate-build-results` job of showcase_build.yml. |
| 4 | + * |
| 5 | + * The script's `run({inputDir, outputDir, githubOutput})` entrypoint is |
| 6 | + * exercised directly with temp dirs (so we never touch tracked files or |
| 7 | + * spawn subprocesses). We verify: |
| 8 | + * 1. empty INPUT_DIR → results.json = `[]`, any_success=false, no throw |
| 9 | + * 2. build-result-<x> dir missing result.json → throws naming the slot |
| 10 | + * 3. non-`build-result-*` dirs are ignored |
| 11 | + * 4. mixed success/failure → correct merged array + any_success=true |
| 12 | + * 5. GITHUB_OUTPUT receives heredoc-form `results` block + any_success |
| 13 | + */ |
| 14 | +import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; |
| 15 | +import { tmpdir } from "node:os"; |
| 16 | +import { join } from "node:path"; |
| 17 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 18 | +import { run } from "../aggregate-build-results"; |
| 19 | + |
| 20 | +function makeSlot( |
| 21 | + inputDir: string, |
| 22 | + service: string, |
| 23 | + status: "success" | "failure" | "skipped", |
| 24 | +): void { |
| 25 | + const dir = join(inputDir, `build-result-${service}`); |
| 26 | + mkdirSync(dir, { recursive: true }); |
| 27 | + writeFileSync(join(dir, "result.json"), JSON.stringify({ service, status })); |
| 28 | +} |
| 29 | + |
| 30 | +describe("aggregate-build-results.run", () => { |
| 31 | + let inputDir: string; |
| 32 | + let outputDir: string; |
| 33 | + let githubOutput: string; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + const base = mkdtempSync(join(tmpdir(), "agg-build-")); |
| 37 | + inputDir = join(base, "in"); |
| 38 | + outputDir = join(base, "out"); |
| 39 | + githubOutput = join(base, "gh_output"); |
| 40 | + mkdirSync(inputDir, { recursive: true }); |
| 41 | + // OUTPUT_DIR intentionally NOT pre-created — run() must mkdir -p. |
| 42 | + writeFileSync(githubOutput, ""); |
| 43 | + }); |
| 44 | + |
| 45 | + afterEach(() => { |
| 46 | + // Temp dirs are under os.tmpdir(); OS reaps them. No tracked files |
| 47 | + // touched, so no cleanup needed. |
| 48 | + }); |
| 49 | + |
| 50 | + it("empty INPUT_DIR → results.json=[] and any_success=false", () => { |
| 51 | + run({ inputDir, outputDir, githubOutput }); |
| 52 | + |
| 53 | + const results = JSON.parse( |
| 54 | + readFileSync(join(outputDir, "results.json"), "utf-8"), |
| 55 | + ); |
| 56 | + expect(results).toEqual([]); |
| 57 | + |
| 58 | + const gh = readFileSync(githubOutput, "utf-8"); |
| 59 | + expect(gh).toContain("any_success=false"); |
| 60 | + // Heredoc form must be used even for []. |
| 61 | + expect(gh).toMatch(/results<<(\S+)\n\[\]\n\1\n/); |
| 62 | + }); |
| 63 | + |
| 64 | + it("throws naming the slot when build-result-<x>/result.json is missing", () => { |
| 65 | + const slotDir = join(inputDir, "build-result-orphan"); |
| 66 | + mkdirSync(slotDir, { recursive: true }); |
| 67 | + // Note: NO result.json written. |
| 68 | + |
| 69 | + expect(() => run({ inputDir, outputDir, githubOutput })).toThrow( |
| 70 | + /aggregate-build-results: build-result-orphan is missing result\.json/, |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it("ignores directories that do not match build-result-*", () => { |
| 75 | + mkdirSync(join(inputDir, "some-other-artifact"), { recursive: true }); |
| 76 | + writeFileSync( |
| 77 | + join(inputDir, "some-other-artifact", "result.json"), |
| 78 | + JSON.stringify({ service: "noise", status: "success" }), |
| 79 | + ); |
| 80 | + // A file (not a directory) at top level should also be ignored. |
| 81 | + writeFileSync(join(inputDir, "build-result-not-a-dir"), "garbage"); |
| 82 | + |
| 83 | + makeSlot(inputDir, "real", "success"); |
| 84 | + |
| 85 | + run({ inputDir, outputDir, githubOutput }); |
| 86 | + |
| 87 | + const results = JSON.parse( |
| 88 | + readFileSync(join(outputDir, "results.json"), "utf-8"), |
| 89 | + ); |
| 90 | + expect(results).toEqual([{ service: "real", status: "success" }]); |
| 91 | + }); |
| 92 | + |
| 93 | + it("merges mixed success/failure correctly and sets any_success=true", () => { |
| 94 | + makeSlot(inputDir, "alpha", "success"); |
| 95 | + makeSlot(inputDir, "beta", "failure"); |
| 96 | + makeSlot(inputDir, "gamma", "skipped"); |
| 97 | + |
| 98 | + run({ inputDir, outputDir, githubOutput }); |
| 99 | + |
| 100 | + const results = JSON.parse( |
| 101 | + readFileSync(join(outputDir, "results.json"), "utf-8"), |
| 102 | + ); |
| 103 | + expect(results).toHaveLength(3); |
| 104 | + const byName = new Map<string, string>( |
| 105 | + (results as Array<{ service: string; status: string }>).map((r) => [ |
| 106 | + r.service, |
| 107 | + r.status, |
| 108 | + ]), |
| 109 | + ); |
| 110 | + expect(byName.get("alpha")).toBe("success"); |
| 111 | + expect(byName.get("beta")).toBe("failure"); |
| 112 | + expect(byName.get("gamma")).toBe("skipped"); |
| 113 | + |
| 114 | + const gh = readFileSync(githubOutput, "utf-8"); |
| 115 | + expect(gh).toContain("any_success=true"); |
| 116 | + }); |
| 117 | + |
| 118 | + it("writes results to GITHUB_OUTPUT in multi-line heredoc form", () => { |
| 119 | + makeSlot(inputDir, "alpha", "success"); |
| 120 | + makeSlot(inputDir, "beta", "failure"); |
| 121 | + |
| 122 | + run({ inputDir, outputDir, githubOutput }); |
| 123 | + |
| 124 | + const gh = readFileSync(githubOutput, "utf-8"); |
| 125 | + |
| 126 | + // The heredoc form is: |
| 127 | + // results<<EOF |
| 128 | + // <json> |
| 129 | + // EOF |
| 130 | + // The delimiter token is implementation-defined but must match on |
| 131 | + // both sides (GitHub Actions convention; commonly "EOF" or a unique |
| 132 | + // token to avoid collision with embedded payloads). |
| 133 | + const heredocRe = /results<<(\S+)\n([\s\S]*?)\n\1\n/; |
| 134 | + const match = gh.match(heredocRe); |
| 135 | + expect(match).not.toBeNull(); |
| 136 | + if (!match) return; |
| 137 | + const [, , jsonBody] = match; |
| 138 | + const parsed = JSON.parse(jsonBody); |
| 139 | + expect(Array.isArray(parsed)).toBe(true); |
| 140 | + expect(parsed).toHaveLength(2); |
| 141 | + |
| 142 | + // any_success line is still a plain key=value. |
| 143 | + expect(gh).toMatch(/any_success=true\n/); |
| 144 | + }); |
| 145 | + |
| 146 | + it("results.json has a trailing newline", () => { |
| 147 | + makeSlot(inputDir, "alpha", "success"); |
| 148 | + |
| 149 | + run({ inputDir, outputDir, githubOutput }); |
| 150 | + |
| 151 | + const raw = readFileSync(join(outputDir, "results.json"), "utf-8"); |
| 152 | + expect(raw.endsWith("\n")).toBe(true); |
| 153 | + }); |
| 154 | +}); |
0 commit comments