- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/pipeline/test/pipeline.test.ts?version=0&format=json
337 linesFirst import 100%Last change First import in version 0, 1 version changed it
You're reading version 0. The newest is 318, and 9 versions since then changed this file.
1// The pipeline file's shape (abso issue 147): read from YAML or JSON, and
2// refused with the place and why for cycles, unknown jobs, secrets a draft
3// could reach, and keys nobody knows.
5import assert from "node:assert/strict";
6import { describe, it } from "node:test";
7import { parse } from "yaml";
8import { type Checked, checkPipelines, type Problem, readPipelineFile } from "../pipeline.ts";
10/** The draft pipeline file, comments included. */
11const DRAFT = `defaults: { image: node, size: standard } # image: a menu or \`dockerfile: path\`
12pipelines:
13 ci:
14 on: [publish, draft] # every version of shared, and an agent's draft before it publishes
15 jobs:
16 setup: { run: npm ci, cache: [package-lock.json] }
17 lint: { needs: [setup], run: npx biome ci . }
18 test: { needs: [setup], run: npm test, size: large }
19 rust: { needs: [setup], image: node+rust, run: cargo nextest run }
20 bundle: { needs: [lint, test], run: npm run build, keep: ["dist/**"] }
21 required: [lint, test] # these passing makes a version green
22 release:
23 on: [green]
24 only: shared # never a draft, so secrets can't leak through one
25 jobs:
26 site:
27 uses: deploy
28 from: ci.bundle
29 worker: avcs-website
30 staging: always
31 live: { steps: [0, 10, 50, 100], hold: 2m, approve: auto, stop_if: errors_rise }
32 secrets: [CLOUDFLARE_API_TOKEN]
33 tell: { needs: [site], uses: notify, to: [issue, watchers] }
34 nightly:
35 on: [{ every: "day 06:00 UTC" }]
36 jobs:
37 restore-drill: { run: node ops/restore-drill.ts }
38`;
40const ok = (c: Checked) => {
41 assert.ok(c.ok, c.ok ? "" : JSON.stringify(c.problems, null, 2));
42 return c.file;
43};
44const problems = (c: Checked): readonly Problem[] => {
45 assert.ok(!c.ok, "the file was accepted");
46 return c.problems;
47};
48/** Asserts some problem is at `at` and matches `message`. */
49const has = (ps: readonly Problem[], at: string, message: RegExp) =>
50 assert.ok(
51 ps.some((p) => p.at === at && message.test(p.message)),
52 `no problem at ${at} matching ${message}; got ${JSON.stringify(ps, null, 2)}`,
53 );
55/** A file with one pipeline `p` of the given jobs. */
56const one = (jobs: Record<string, unknown>, rest: Record<string, unknown> = {}) => ({
57 pipelines: { p: { on: ["publish"], jobs, ...rest } },
58});
60describe("reading the file", () => {
61 it("reads the draft from YAML, with every pipeline, job and setting", () => {
62 const file = ok(readPipelineFile(DRAFT, "abso.yml"));
63 assert.deepEqual(file.defaults, { image: "node", size: "standard" });
64 assert.deepEqual(
65 file.pipelines.map((p) => p.name),
66 ["ci", "release", "nightly"],
67 );
68 const [ci, release, nightly] = file.pipelines;
69 assert.deepEqual(ci!.on, ["publish", "draft"]);
70 assert.deepEqual(ci!.required, ["lint", "test"]);
71 assert.deepEqual(release!.only, ["shared"]);
72 assert.deepEqual(nightly!.on, [{ every: "day 06:00 UTC" }]);
73 const site = release!.jobs.find((j) => j.name === "site")!;
74 assert.equal(site.uses, "deploy");
75 assert.equal(site.from, "ci.bundle");
76 assert.deepEqual(site.secrets, ["CLOUDFLARE_API_TOKEN"]);
77 assert.equal(site.with.worker, "avcs-website");
78 assert.deepEqual(ci!.jobs.find((j) => j.name === "rust")!.image, "node+rust");
79 });
81 it("reads the same file from JSON to the same result", () => {
82 const yaml = ok(readPipelineFile(DRAFT, "abso.yml"));
83 const json = ok(readPipelineFile(JSON.stringify(parse(DRAFT)), "avcs.json"));
84 assert.deepEqual(json, yaml);
85 });
87 it("puts each job after the jobs it needs", () => {
88 const file = ok(
89 checkPipelines(
90 one({
91 d: { needs: ["b", "c"], run: "d" },
92 c: { needs: ["a"], run: "c" },
93 b: { needs: ["a"], run: "b" },
94 a: { run: "a" },
95 }),
96 ),
97 );
98 const names = file.pipelines[0]!.jobs.map((j) => j.name);
99 for (const [before, after] of [
100 ["a", "b"],
101 ["a", "c"],
102 ["b", "d"],
103 ["c", "d"],
104 ])
105 assert.ok(names.indexOf(before!) < names.indexOf(after!), names.join(","));
106 });
108 it("reads on as a key in YAML, not as true", () => {
109 ok(readPipelineFile("pipelines:\n p:\n on: publish\n jobs:\n a: { run: x }\n", "abso.yaml"));
110 });
112 it("says where YAML or JSON breaks, and refuses a name it can't tell the format of", () => {
113 has(
114 problems(readPipelineFile("pipelines:\n p:\n on: [publish\n", "abso.yml")),
115 "file",
116 /isn't valid YAML at line \d/,
117 );
118 has(problems(readPipelineFile("{ pipelines: ", "avcs.json")), "file", /isn't valid JSON/);
119 has(problems(readPipelineFile("pipelines: {}", "pipeline.txt")), "file", /format can't be told/);
120 });
122 it("refuses a key given twice in YAML", () => {
123 has(
124 problems(
125 readPipelineFile("pipelines:\n p:\n on: publish\n on: draft\n jobs: { a: { run: x } }\n", "abso.yml"),
126 ),
127 "file",
128 /isn't valid YAML/,
129 );
130 });
132 it("leaves avcs.json's own CI keys alone, but not in abso.yml", () => {
133 const doc = { setup: "npm ci", check: "npm test", whole: ["package-lock.json"], ...one({ a: { run: "x" } }) };
134 ok(readPipelineFile(JSON.stringify(doc), "avcs.json"));
135 ok(readPipelineFile(JSON.stringify(doc), "some/folder/avcs.json"));
136 has(problems(readPipelineFile(JSON.stringify(doc), "abso.yml")), "setup", /isn't a key here/);
137 });
138});
140describe("refusing", () => {
141 it("a cycle in needs, naming its jobs once", () => {
142 const ps = problems(
143 checkPipelines(
144 one({
145 a: { needs: ["c"], run: "a" },
146 b: { needs: ["a"], run: "b" },
147 c: { needs: ["b"], run: "c" },
148 d: { run: "d" },
149 }),
150 ),
151 );
152 has(ps, "pipelines.p.jobs", /cycle: a → c → b → a/);
153 assert.equal(ps.filter((p) => /cycle/.test(p.message)).length, 1);
154 });
156 it("a job that needs itself", () => {
157 has(
158 problems(checkPipelines(one({ a: { needs: ["a"], run: "a" } }))),
159 "pipelines.p.jobs.a.needs[0]",
160 /the job itself/,
161 );
162 });
164 it("needs, required and from naming what doesn't exist", () => {
165 const ps = problems(
166 checkPipelines({
167 pipelines: {
168 p: {
169 on: ["publish"],
170 jobs: { a: { needs: ["nope"], run: "a" }, b: { needs: ["q.c"], run: "b" } },
171 required: ["a", "missing"],
172 },
173 q: {
174 on: ["green"],
175 jobs: {
176 c: { run: "c" },
177 d: { run: "d", from: "p.ghost" },
178 e: { run: "e", from: "none.c" },
179 f: { run: "f", from: "p.a" },
180 g: { run: "g", from: "justone" },
181 },
182 },
183 },
184 }),
185 );
186 has(ps, "pipelines.p.jobs.a.needs[0]", /job "nope", which this pipeline doesn't have/);
187 has(
188 ps,
189 "pipelines.p.jobs.b.needs[0]",
190 /reaches only jobs in this pipeline; take another pipeline's files with from/,
191 );
192 has(ps, "pipelines.p.required[1]", /job "missing"/);
193 has(ps, "pipelines.q.jobs.d.from", /job "ghost", which pipeline "p" doesn't have/);
194 has(ps, "pipelines.q.jobs.e.from", /pipeline "none", which doesn't exist/);
195 has(ps, "pipelines.q.jobs.f.from", /keeps no files/);
196 has(ps, "pipelines.q.jobs.g.from", /must be <pipeline>.<job>/);
197 });
199 it("secrets in a pipeline a draft can start", () => {
200 const deploy = { uses: "deploy", worker: "w", secrets: ["CLOUDFLARE_API_TOKEN"] };
201 const ps = problems(checkPipelines(one({ ship: deploy }, { on: ["publish", "draft"], only: "shared" })));
202 has(ps, "pipelines.p.on", /includes draft, but ship holds secrets/);
203 assert.equal(ps.length, 1, JSON.stringify(ps));
204 });
206 it("secrets in a pipeline that isn't only shared", () => {
207 const deploy = { uses: "deploy", worker: "w", secrets: ["CLOUDFLARE_API_TOKEN"] };
208 has(problems(checkPipelines(one({ ship: deploy }, { on: ["green"] }))), "pipelines.p.only", /must be shared/);
209 has(
210 problems(checkPipelines(one({ ship: deploy }, { on: ["green"], only: ["shared", "rename-59"] }))),
211 "pipelines.p.only",
212 /must be shared/,
213 );
214 ok(checkPipelines(one({ ship: deploy }, { on: ["green"], only: ["shared"] })));
215 // Without secrets, a draft may run anything.
216 ok(checkPipelines(one({ a: { run: "npm test" } }, { on: ["draft"] })));
217 });
219 it("keys it doesn't know, at every level", () => {
220 const ps = problems(
221 checkPipelines({
222 defaults: { image: "node", colour: "blue" },
223 pipelines: { p: { on: ["publish"], when: "always", jobs: { a: { run: "x", timeout: 5, worker: "w" } } } },
224 extra: 1,
225 }),
226 );
227 has(ps, "extra", /isn't a key here/);
228 has(ps, "defaults.colour", /isn't a key here/);
229 has(ps, "pipelines.p.when", /isn't a key here \(known: on, only, jobs, required\)/);
230 has(ps, "pipelines.p.jobs.a.timeout", /isn't a key here/);
231 // worker is deploy's own setting, not a run job's.
232 has(ps, "pipelines.p.jobs.a.worker", /isn't a key here/);
233 });
235 it("a job that runs nothing, or two things, or an unknown step", () => {
236 const ps = problems(
237 checkPipelines(
238 one({
239 a: { needs: [] },
240 b: { run: "x", uses: "notify" },
241 c: { uses: "launch-rockets" },
242 d: { uses: "deploy" },
243 e: { run: " " },
244 }),
245 ),
246 );
247 has(ps, "pipelines.p.jobs.a", /needs run \(a command\) or uses/);
248 has(ps, "pipelines.p.jobs.b", /both run and uses/);
249 has(ps, "pipelines.p.jobs.c.uses", /must be one of deploy, preview, notify/);
250 has(ps, "pipelines.p.jobs.d.worker", /is missing/);
251 has(ps, "pipelines.p.jobs.e.run", /must be a command/);
252 });
254 it("triggers, names, sizes, machines and secret names it can't use", () => {
255 const ps = problems(
256 checkPipelines({
257 defaults: { image: "node", dockerfile: "Dockerfile" },
258 pipelines: {
259 p: { on: ["publish", "sometimes", { every: "" }], jobs: { a: { run: "x", size: "huge" } } },
260 Bad: { on: [], jobs: {} },
261 s: { on: "green", only: "shared", jobs: { "x y": { uses: "deploy", worker: "w", secrets: ["token"] } } },
262 t: { jobs: { a: { run: "x" } } },
263 },
264 }),
265 );
266 has(ps, "defaults", /both image and dockerfile/);
267 has(ps, "pipelines.p.on[1]", /must be one of publish, draft, green, land, hand/);
268 has(ps, "pipelines.p.on[2]", /every/);
269 has(ps, "pipelines.p.jobs.a.size", /must be one of small, standard, large, not "huge"/);
270 has(ps, "pipelines.Bad", /isn't a usable name/);
271 has(ps, "pipelines.Bad.on", /is empty/);
272 has(ps, "pipelines.Bad.jobs", /has no jobs/);
273 has(ps, "pipelines.s.jobs.x y", /isn't a usable name/);
274 has(ps, "pipelines.s.jobs.x y.secrets[0]", /isn't a secret name/);
275 has(ps, "pipelines.t.on", /is missing/);
276 });
278 it("a file with no pipelines, or that isn't a map", () => {
279 has(problems(checkPipelines({})), "pipelines", /is missing/);
280 has(problems(checkPipelines({ pipelines: {} })), "pipelines", /has no pipelines/);
281 has(problems(checkPipelines([1, 2])), "file", /must be a map/);
282 has(problems(readPipelineFile("just words", "abso.yml")), "file", /must be a map/);
283 });
284});
286describe("avcs.json's shorthand for a ci pipeline (abso issue 162)", () => {
287 it("reads abso's own avcs.json as ci: setup, then check, both required, on publish", async () => {
288 const { readFileSync } = await import("node:fs");
289 const text = readFileSync(new URL("../../../avcs.json", import.meta.url), "utf8");
290 const own = JSON.parse(text);
291 const [ci, ...rest] = ok(readPipelineFile(text, "avcs.json")).pipelines;
292 assert.equal(rest.length, 0);
293 assert.equal(ci!.name, "ci");
294 assert.deepEqual(ci!.on, ["publish"]);
295 assert.deepEqual(
296 ci!.jobs.map((j) => [j.name, j.run, j.needs]),
297 [
298 ["setup", own.setup, []],
299 ["check", own.check, ["setup"]],
300 ],
301 );
302 assert.deepEqual(ci!.required, ["setup", "check"]);
303 });
305 it("runs build before check, as today's job does, keeping its out folder", () => {
306 const doc = { setup: "npm ci", check: "npm test", build: "npm run build", out: "site/dist" };
307 const [ci] = ok(readPipelineFile(JSON.stringify(doc), "avcs.json")).pipelines;
308 assert.deepEqual(
309 ci!.jobs.map((j) => [j.name, j.needs, j.keep]),
310 [
311 ["setup", [], []],
312 ["build", ["setup"], ["site/dist/**"]],
313 ["check", ["build"], []],
314 ],
315 );
316 const [only] = ok(readPipelineFile(JSON.stringify({ build: "vite build" }), "avcs.json")).pipelines;
317 assert.deepEqual(only!.jobs[0]!.keep, ["dist/**"]);
318 });
320 it("is only read from avcs.json with no pipelines beside it", () => {
321 const both = { setup: "npm ci", check: "npm test", ...one({ a: { run: "x" } }) };
322 assert.deepEqual(
323 ok(readPipelineFile(JSON.stringify(both), "avcs.json")).pipelines.map((p) => p.name),
324 ["p"],
325 );
326 has(problems(readPipelineFile("setup: npm ci\ncheck: npm test\n", "abso.yml")), "setup", /isn't a key here/);
327 });
329 it("counts empty commands as none, and refuses commands and folders it can't use", () => {
330 has(problems(readPipelineFile(JSON.stringify({ setup: "", check: " " }), "avcs.json")), "pipelines", /is missing/);
331 const ps = problems(
332 readPipelineFile(JSON.stringify({ check: ["npm", "test"], build: "b", out: "../up" }), "avcs.json"),
333 );
334 has(ps, "check", /must be a command/);
335 has(ps, "out", /must be a folder under the project/);
336 });
337});