166 lines · 7.6 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { parsePipeline } from "../config/parse-pipeline.ts"; |
| 4 | import type { ParseResult, Pipeline } from "../config/pipeline.ts"; |
| 5 | |
| 6 | function valid(result: ParseResult): Pipeline { |
| 7 | if (result.kind !== "valid") assert.fail(`expected a valid pipeline, got ${JSON.stringify(result.problems)}`); |
| 8 | return result.pipeline; |
| 9 | } |
| 10 | |
| 11 | function problems(result: ParseResult): string[] { |
| 12 | if (result.kind !== "invalid") assert.fail("expected problems"); |
| 13 | return result.problems.map((problem) => `${problem.path}: ${problem.message}`); |
| 14 | } |
| 15 | |
| 16 | const DESIGN_EXAMPLE = ` |
| 17 | on: |
| 18 | push: { branches: [main, "tasks/*"] } |
| 19 | pull_request: {} |
| 20 | manual: {} |
| 21 | |
| 22 | jobs: |
| 23 | test: |
| 24 | image: node |
| 25 | size: standard-2 |
| 26 | cache: { key: package-lock.json, paths: [node_modules] } |
| 27 | steps: |
| 28 | - run: npm ci |
| 29 | - run: npm test |
| 30 | |
| 31 | build: |
| 32 | image: node |
| 33 | needs: [test] |
| 34 | steps: |
| 35 | - run: npm run build |
| 36 | artifacts: [build/] |
| 37 | |
| 38 | deploy: |
| 39 | image: node |
| 40 | needs: [build] |
| 41 | environment: production |
| 42 | steps: |
| 43 | - run: npx wrangler deploy |
| 44 | `; |
| 45 | |
| 46 | test("the pipeline from the design page parses", () => { |
| 47 | const pipeline = valid(parsePipeline(DESIGN_EXAMPLE)); |
| 48 | assert.deepEqual(pipeline.triggers, { push: { branches: ["main", "tasks/*"] }, pullRequest: { branches: [] }, manual: true }); |
| 49 | assert.deepEqual([...pipeline.jobs.keys()], ["test", "build", "deploy"]); |
| 50 | const test = pipeline.jobs.get("test")!; |
| 51 | assert.deepEqual(test.image, { kind: "built-in", name: "node", version: null }); |
| 52 | assert.deepEqual(test.cache, { key: "package-lock.json", paths: ["node_modules"] }); |
| 53 | assert.deepEqual(test.steps, [ |
| 54 | { name: "npm ci", run: "npm ci" }, |
| 55 | { name: "npm test", run: "npm test" }, |
| 56 | ]); |
| 57 | assert.equal(test.timeoutMinutes, 60); |
| 58 | assert.deepEqual(pipeline.jobs.get("build")!.artifacts, ["build/"]); |
| 59 | assert.equal(pipeline.jobs.get("deploy")!.environment, "production"); |
| 60 | }); |
| 61 | |
| 62 | test("a pipeline without on runs on every push, pull request and manual start", () => { |
| 63 | const pipeline = valid(parsePipeline("jobs:\n test:\n steps: [make test]\n")); |
| 64 | assert.deepEqual(pipeline.triggers, { push: { branches: [] }, pullRequest: { branches: [] }, manual: true }); |
| 65 | const job = pipeline.jobs.get("test")!; |
| 66 | assert.deepEqual(job.image, { kind: "built-in", name: "base", version: null }); |
| 67 | assert.equal(job.size, "standard-2"); |
| 68 | }); |
| 69 | |
| 70 | test("on lists only the triggers it names", () => { |
| 71 | const pipeline = valid(parsePipeline("on:\n push:\njobs:\n a:\n steps: [x]\n")); |
| 72 | assert.deepEqual(pipeline.triggers, { push: { branches: [] }, pullRequest: null, manual: false }); |
| 73 | }); |
| 74 | |
| 75 | test("a step is named after the first line of its command", () => { |
| 76 | const pipeline = valid(parsePipeline(`jobs:\n a:\n steps:\n - |\n npm ci\n npm test\n - name: Lint\n run: npm run lint\n - ${"x".repeat(100)}\n`)); |
| 77 | const [multi, named, long] = pipeline.jobs.get("a")!.steps; |
| 78 | assert.equal(multi.name, "npm ci"); |
| 79 | assert.equal(named.name, "Lint"); |
| 80 | assert.equal(long.name.length, 80); |
| 81 | }); |
| 82 | |
| 83 | test("images: built-in and dated built-in; an unknown image is refused", () => { |
| 84 | const pipeline = valid(parsePipeline("jobs:\n a:\n image: rust@2026-12\n steps: [x]\n b:\n image: refs/android\n steps: [x]\n")); |
| 85 | assert.deepEqual(pipeline.jobs.get("a")!.image, { kind: "built-in", name: "rust", version: "2026-12" }); |
| 86 | assert.deepEqual(pipeline.jobs.get("b")!.image, { kind: "built-in", name: "android", version: null }); |
| 87 | assert.deepEqual(problems(parsePipeline("jobs:\n c:\n image: rust-wasm\n steps: [x]\n")).map((problem) => problem.split(":")[0]), ["jobs.c.image"]); |
| 88 | }); |
| 89 | |
| 90 | test("a job names the secrets it gets, in secret-name form", () => { |
| 91 | const pipeline = valid(parsePipeline("jobs:\n a:\n secrets: [NPM_TOKEN, DEPLOY_KEY]\n steps: [x]\n")); |
| 92 | assert.deepEqual(pipeline.jobs.get("a")!.secrets, ["NPM_TOKEN", "DEPLOY_KEY"]); |
| 93 | assert.deepEqual(problems(parsePipeline("jobs:\n a:\n secrets: [npm-token]\n steps: [x]\n")).map((problem) => problem.split(":")[0]), ["jobs.a.secrets"]); |
| 94 | }); |
| 95 | |
| 96 | test("every problem is reported with its path, not only the first", () => { |
| 97 | const found = problems( |
| 98 | parsePipeline(` |
| 99 | color: blue |
| 100 | jobs: |
| 101 | test: |
| 102 | image: rust-wasm@2026-12 |
| 103 | size: huge |
| 104 | timeout_minutes: 0 |
| 105 | steps: [] |
| 106 | "bad name!": |
| 107 | steps: [x] |
| 108 | ok: |
| 109 | image: refs/cobol |
| 110 | stesp: [x] |
| 111 | `), |
| 112 | ); |
| 113 | assert.deepEqual(found, [ |
| 114 | "color: Unknown key \"color\".", |
| 115 | "jobs.test.image: Only built-in images have dated versions.", |
| 116 | "jobs.test.size: Must be one of lite, standard-1, standard-2, standard-3, standard-4.", |
| 117 | "jobs.test.steps: Must be a list with at least one step.", |
| 118 | "jobs.test.timeout_minutes: Must be a whole number of minutes from 1 to 360.", |
| 119 | "jobs.bad name!: A job name uses letters, digits, - and _, and is at most 64 characters.", |
| 120 | "jobs.ok.stesp: Unknown key \"stesp\".", |
| 121 | "jobs.ok.image: There is no built-in image named \"cobol\".", |
| 122 | "jobs.ok.steps: Must be a list with at least one step.", |
| 123 | ]); |
| 124 | }); |
| 125 | |
| 126 | test("needs must name other jobs and must not form a cycle", () => { |
| 127 | assert.deepEqual(problems(parsePipeline("jobs:\n a:\n needs: [missing, a]\n steps: [x]\n")), [ |
| 128 | "jobs.a.needs[0]: No job is named \"missing\".", |
| 129 | "jobs.a.needs[1]: A job cannot need itself.", |
| 130 | ]); |
| 131 | assert.deepEqual(problems(parsePipeline("jobs:\n a:\n needs: c\n steps: [x]\n b:\n needs: a\n steps: [x]\n c:\n needs: b\n steps: [x]\n")), [ |
| 132 | "jobs.a.needs: These jobs need each other in a cycle: a → c → b → a.", |
| 133 | ]); |
| 134 | }); |
| 135 | |
| 136 | test("YAML that does not parse, a duplicate key, and a file that is not a mapping are invalid", () => { |
| 137 | assert.equal(parsePipeline("jobs: [unclosed").kind, "invalid"); |
| 138 | assert.equal(parsePipeline("jobs:\n a:\n steps: [x]\n a:\n steps: [y]\n").kind, "invalid"); |
| 139 | assert.deepEqual(problems(parsePipeline("- just a list")), [": The file must be a mapping with a jobs key."]); |
| 140 | assert.deepEqual(problems(parsePipeline("on: {}\n")), ["jobs: Must be a mapping with at least one job."]); |
| 141 | }); |
| 142 | |
| 143 | test("on is a key, not the YAML 1.1 boolean", () => { |
| 144 | const pipeline = valid(parsePipeline("on:\n manual: true\njobs:\n a:\n steps: [x]\n")); |
| 145 | assert.equal(pipeline.triggers.manual, true); |
| 146 | }); |
| 147 | |
| 148 | test("a file over 64 KB is refused before parsing", () => { |
| 149 | assert.deepEqual(problems(parsePipeline(`# ${"x".repeat(64 * 1024)}\njobs: {}`)), [": The file is larger than 64 KB."]); |
| 150 | }); |
| 151 | |
| 152 | test("an agent job names a hosted agent and an optional prompt, and takes no container keys", () => { |
| 153 | const pipeline = valid(parsePipeline("jobs:\n test:\n steps: [npm test]\n review:\n agent: '@reviewer'\n prompt: Check the retry logic.\n needs: test\n")); |
| 154 | const review = pipeline.jobs.get("review")!; |
| 155 | assert.deepEqual(review.agent, { handle: "reviewer", prompt: "Check the retry logic." }); |
| 156 | assert.deepEqual(review.needs, ["test"]); |
| 157 | assert.equal(review.timeoutMinutes, 20); |
| 158 | assert.deepEqual(review.steps, [{ name: "@reviewer", run: "Check the retry logic." }]); |
| 159 | assert.equal(pipeline.jobs.get("test")!.agent, null); |
| 160 | assert.deepEqual(problems(parsePipeline("jobs:\n review:\n agent: reviewer\n image: node\n steps: [ls]\n")), [ |
| 161 | "jobs.review.image: An agent job takes only agent, prompt, needs and timeout_minutes.", |
| 162 | "jobs.review.steps: An agent job takes only agent, prompt, needs and timeout_minutes.", |
| 163 | ]); |
| 164 | assert.deepEqual(problems(parsePipeline("jobs:\n review:\n agent: Not A Handle\n")), ["jobs.review.agent: Must be the handle of a hosted agent, such as reviewer."]); |
| 165 | assert.ok(problems(parsePipeline("jobs:\n test:\n prompt: hi\n steps: [ls]\n")).includes("jobs.test.prompt: Only an agent job takes a prompt. Add agent, or remove prompt.")); |
| 166 | }); |