import { describe, expect, it } from "vitest"; import { globMatch, invalidWorkflow, isTrusted, matchesPullRequest, matchesPush, parseWorkflow, pipelineSlug, resolveAfter, triggers, } from "../src/workflow"; const job = (extra: string, steps = "steps: [{name: a, command: b}]\n") => parseWorkflow("ci.yml", `engine: cloudflare\n${steps}${extra}\n`).cloudflare; const cloudflare = (settings: string) => job(`treadle: {cloudflare: {${settings}}}`); describe("globMatch", () => { it.each([ ["main", "main", true], ["release/*", "release/1.0", true], ["release/*", "release/a/b", false], ["release/**", "release/a/b", true], ["v?", "v1", true], ["v?", "v10", false], ["a.b", "axb", false], ["{main,dev}", "{main,dev}", true], ["release-**", "release-1/x", false], ["**/main", "main", true], ["**/main", "a/b/main", true], ["v[0-9]*", "v1.2", true], ["[!a]x", "bx", true], ["[!a]x", "ax", false], ])("%s matches %s: %s", (pattern, value, expected) => { expect(globMatch(pattern, value)).toBe(expected); }); }); describe("parseWorkflow", () => { it("reads constraints and the Buildkite target", () => { const raw = [ "engine: buildkite", "when:", " - event: push", " branch: main", " - event: [push, pull_request]", " tag: ['v*']", " types: [opened, closed]", "treadle:", " buildkite:", " pipeline: treadle", " org: other", " clean_checkout: true", " after: [check.yml]", ].join("\n"); expect(parseWorkflow("ci.yml", raw)).toEqual({ name: "ci.yml", raw, engine: "buildkite", when: [ { event: ["push"], branch: ["main"], tag: [], types: [] }, { event: ["push", "pull_request"], branch: [], tag: ["v*"], types: ["opened", "closed"], }, ], buildkite: { pipeline: "treadle", org: "other", cleanCheckout: true }, after: ["check.yml"], }); }); it("reads a workflow without Buildkite settings", () => { expect(parseWorkflow("ci.yml", "engine: buildkite\n")).toMatchObject({ engine: "buildkite", when: [], buildkite: {}, after: [], }); expect(parseWorkflow("ci.yml", "")).toMatchObject({ engine: undefined, buildkite: {} }); }); it("reads a Cloudflare job", () => { const raw = [ "engine: cloudflare", "environment: {CI_MODE: fast, LEVEL: '3'}", "secrets: [API_TOKEN, OTHER]", "dependencies: [jq, 'libc6-dev=2.41-12', g++, 'tzdata=1:2025b~rc1']", "trusted:", " branch: [main, 'release/*']", "cache: true", "runs_on: {vcpu: 2, memory_mib: 8192, disk_mb: 16000}", "steps:", " - name: test", " command: pnpm test", " environment: {DEBUG: 'true'}", " - name: lint", " command: pnpm lint", "treadle:", " cloudflare:", " timeout: 1h30m", ].join("\n"); expect(parseWorkflow("ci.yml", raw).cloudflare).toEqual({ environment: { CI_MODE: "fast", LEVEL: "3" }, steps: [ { name: "test", command: "pnpm test", environment: { DEBUG: "true" } }, { name: "lint", command: "pnpm lint", environment: {} }, ], instance: { vcpu: 2, memoryMib: 8192, diskMb: 16000 }, timeoutMs: 90 * 60_000, cache: true, secrets: ["API_TOKEN", "OTHER"], injected: [], apt: ["jq", "libc6-dev=2.41-12", "g++", "tzdata=1:2025b~rc1"], }); }); it("applies the Cloudflare defaults", () => { const raw = "engine: cloudflare\nsteps:\n - {name: a, command: 'true'}\n"; expect(parseWorkflow("ci.yml", raw).cloudflare).toEqual({ environment: {}, steps: [{ name: "a", command: "true", environment: {} }], instance: "standard-1", timeoutMs: 60 * 60_000, cache: false, secrets: [], injected: [], apt: [], }); expect(parseWorkflow("ci.yml", "engine: buildkite").cloudflare).toBeUndefined(); }); it("reads injected secrets", () => { const parsed = job( [ "secrets:", " - ENV_TOKEN", " - {name: API_TOKEN, hosts: [api.example.com, '*.example.org']}", " - {name: KEY, hosts: x.example.com, headers: [X-Auth-Key]}", ].join("\n"), ); expect(parsed).toMatchObject({ secrets: ["ENV_TOKEN"], injected: [ { name: "API_TOKEN", hosts: ["api.example.com", "*.example.org"], headers: ["Authorization"], }, { name: "KEY", hosts: ["x.example.com"], headers: ["X-Auth-Key"] }, ], }); expect(job("secrets: API_TOKEN")?.secrets).toEqual(["API_TOKEN"]); }); it.each([ ["standard-4", "standard-4"], ["future-type", "future-type"], [ "{vcpu: 16, memory_mib: 65536, disk_mb: 100000}", { vcpu: 16, memoryMib: 65536, diskMb: 100000 }, ], ["{vcpu: 1, memory_mib: 1, disk_mb: 1}", { vcpu: 1, memoryMib: 1, diskMb: 1 }], ] satisfies [string, object | string][])("accepts the instance %s", (instance, expected) => { expect(job(`runs_on: ${instance}`)?.instance).toEqual(expected); }); it("keeps environment values as written", () => { const parsed = job( "environment: {CI: true, PY: 3.10, Q: 'x y'}", 'steps: [{name: a, command: b, environment: {V: 1.0, W: "0x10"}}]\n', ); expect(parsed?.environment).toEqual({ CI: "true", PY: "3.10", Q: "x y" }); expect(parsed?.steps[0]?.environment).toEqual({ V: "1.0", W: "0x10" }); }); it.each([ ["6h", 6 * 60 * 60_000], ["45m", 45 * 60_000], ["30s", 30_000], ])("accepts the timeout %s", (timeout, ms) => { expect(cloudflare(`timeout: ${timeout}`)?.timeoutMs).toBe(ms); }); it.each([ ["no steps", () => job("", ""), "steps"], ["an empty step list", () => job("", "steps: []\n"), "steps"], [ "a step without a command", () => job("", "steps: [{name: a}]"), "steps[0].command must be a string", ], ["a bad environment name", () => job("environment: {'A B': x}"), "variable name"], ["a null environment value", () => job("environment: {A: ~}"), "scalar"], ["a nested environment value", () => job("environment: {A: [x]}"), "scalar"], ["a key that is not a string", () => job("environment: {TRUE: x}"), "variable name"], [ "steps behind an alias", () => job("", "x: &s [{name: a, command: b, environment: {A: x}}]\nsteps: *s\n"), "steps[0].environment must not be behind an alias", ], ["settings that are not a map", () => job("treadle: {cloudflare: 3}"), "must be a map"], [ "an empty instance", () => job("runs_on: ''"), "runs_on must be an instance type or a custom instance", ], ["a number instance", () => job("runs_on: 4"), "runs_on"], ["a list of instances", () => job("runs_on: [standard-2]"), "runs_on"], [ "no vCPU", () => job("runs_on: {vcpu: 0, memory_mib: 1, disk_mb: 1}"), "runs_on must be an instance type or a custom instance", ], ["half a vCPU", () => job("runs_on: {vcpu: 1.5, memory_mib: 1, disk_mb: 1}"), "runs_on"], [ "a string size", () => job("runs_on: {vcpu: 1, memory_mib: '4096', disk_mb: 1}"), "runs_on", ], ["no disk", () => job("runs_on: {vcpu: 1, memory_mib: 4096}"), "runs_on"], [ "an unknown setting", () => cloudflare("timeot: 1h"), "treadle.cloudflare.timeot is not a setting", ], ["an instance setting", () => cloudflare("instance: standard-2"), "instance"], ["a timeout over 6 hours", () => cloudflare("timeout: 6h1s"), "timeout"], ["a zero timeout", () => cloudflare("timeout: 0s"), "timeout"], ["a timeout without a unit", () => cloudflare("timeout: 60"), "timeout"], ["a cache that is not true or false", () => job("cache: 'yes'"), "cache"], [ "a bad secret name", () => job("secrets: ['A B']"), "secrets[0] is not a variable name: A B", ], ["a secret that is not a list", () => job("secrets: {A: x}"), "secrets must be a list"], [ "a secret that is a number", () => job("secrets: [1]"), "secrets[0] must be a secret name or a map", ], [ "an IP address as a host", () => job("secrets: [{name: A, hosts: [192.0.2.1]}]"), "secrets[0].hosts[0] is not a hostname: 192.0.2.1", ], [ "more than 64 hosts", () => job( `secrets: [{name: A, hosts: [${Array.from({ length: 65 }, (_, i) => `h${i}.example.com`).join(", ")}]}]`, ), "secrets list more than 64 hosts", ], [ "an injected secret without hosts", () => job("secrets: [{name: A}]"), "secrets[0].hosts must name a host", ], [ "an injected secret with a bad name", () => job("secrets: [{name: 'A B', hosts: [a.example.com]}]"), "secrets[0].name is not a variable name: A B", ], [ "a host glob for every host", () => job("secrets: [{name: A, hosts: ['*']}]"), "secrets[0].hosts[0] is not a hostname: *", ], [ "a host with a port", () => job("secrets: [{name: A, hosts: ['a.example.com:8443']}]"), "secrets[0].hosts[0] is not a hostname: a.example.com:8443", ], [ "a bad header name", () => job("secrets: [{name: A, hosts: [a.example.com], headers: ['X Key']}]"), "secrets[0].headers[0] is not a header name: X Key", ], [ "an unknown secret setting", () => job("secrets: [{name: A, hosts: [a.example.com], host: b.example.com}]"), "secrets[0].host is not a setting", ], [ "a secret listed twice", () => job("secrets: [A, {name: A, hosts: [a.example.com]}]"), "secrets lists A twice", ], [ "dependencies by package manager", () => job("dependencies: {nixpkgs: [jq]}"), "dependencies", ], ["a shell command as a package", () => job("dependencies: ['jq;id']"), "dependencies"], ["an option as a package", () => job("dependencies: ['-oX=1']"), "dependencies"], ["a substitution as a package", () => job("dependencies: ['$(id)']"), "dependencies"], ["a version without a digit", () => job("dependencies: ['jq=x']"), "dependencies"], ["a package with spaces", () => job("dependencies: ['jq curl']"), "dependencies"], ["a bad trusted list", () => job("trusted: {branch: [1]}"), "trusted.branch"], ["a trusted list without a map", () => job("trusted: [main]"), "trusted"], ["a trusted map without branches", () => job("trusted: {branches: [main]}"), "trusted"], ])("rejects a Cloudflare job with %s", (_, parse, message) => { expect(parse).toThrow(message); }); it.each([ ["not YAML", "when: [", "Flow sequence"], ["not a map", "- a", "workflow must be a map"], ["a bad constraint", "when: [1]", "when[0] must be a map"], [ "a bad string list", "when:\n - event: push\n tag: [1.10]", "when[0].tag must be a string or a list of strings", ], [ "a bad pipeline", "treadle:\n buildkite:\n pipeline: 1", "treadle.buildkite.pipeline must be a string", ], [ "an empty pipeline", "treadle: {buildkite: {pipeline: ''}}", "treadle.buildkite.pipeline must not be empty", ], ["a bad after", "treadle: {after: [1]}", "treadle.after must be a string or a list"], ["treadle settings that are not a map", "treadle: 3", "treadle must be a map"], [ "Buildkite settings that are not a map", "treadle: {buildkite: 3}", "treadle.buildkite must be a map", ], ["an engine that is not a string", "engine: 1", "engine must be a string"], [ "a bad Buildkite org", "treadle: {buildkite: {org: 1}}", "treadle.buildkite.org must be a string", ], [ "a bad clean checkout", "treadle: {buildkite: {clean_checkout: 'yes'}}", "treadle.buildkite.clean_checkout must be true or false", ], ])("rejects %s", (_, raw, message) => { expect(() => parseWorkflow("ci.yml", raw)).toThrow(message); }); }); describe("invalidWorkflow", () => { it.each([ ["not YAML", "when: [", undefined, []], ["not a map", "- a", undefined, []], ["a bad when", "engine: buildkite\nwhen: 5", "buildkite", []], [ "a readable when", "engine: cloudflare\nwhen: [{event: push, branch: main}]", "cloudflare", [{ event: ["push"], branch: ["main"], tag: [], types: [] }], ], ])("keeps the error of %s", (_, raw, engine, when) => { expect(invalidWorkflow("ci.yml", raw, "bad")).toEqual({ name: "ci.yml", raw, engine, when, buildkite: {}, cloudflare: undefined, error: "bad", }); }); it.each([ ["another engine", "engine: nixery\nwhen: 5"], ["no engine", "when: 5"], ])("skips a file with %s", (_, raw) => { expect(invalidWorkflow("ci.yml", raw, "bad")).toBeUndefined(); }); }); function workflow(raw: string) { return parseWorkflow("ci.yml", raw); } describe("resolveAfter", () => { it("accepts a workflow name without its extension", () => { const workflows = resolveAfter([ workflow("engine: buildkite\ntreadle: {after: [check, lint, ci.yml, gone]}"), parseWorkflow("check.yml", "engine: buildkite"), parseWorkflow("lint.yaml", "engine: buildkite"), ]); expect(workflows[0]?.after).toEqual(["check.yml", "lint.yaml", "ci.yml", "gone"]); }); }); describe("pipelineSlug", () => { it.each([ ["treadle", "treadle"], ["Treadle", "treadle"], ["foo.nvim", "foo-nvim"], ["a__b..c", "a-b-c"], [".dotfiles-", "dotfiles"], ["über_tool", "ber-tool"], ])("turns %s into %s", (name, slug) => { expect(pipelineSlug(name)).toBe(slug); }); }); describe("matchesPush", () => { it("matches every push without constraints", () => { expect(matchesPush(workflow(""), "refs/heads/anything")).toBe(true); }); it("matches branch constraints against branch refs", () => { const wf = workflow("when:\n - event: push\n branch: [main, 'release/*']"); expect(matchesPush(wf, "refs/heads/main")).toBe(true); expect(matchesPush(wf, "refs/heads/release/1")).toBe(true); expect(matchesPush(wf, "refs/heads/dev")).toBe(false); expect(matchesPush(wf, "refs/tags/main")).toBe(false); }); it("matches tag constraints against tag refs", () => { const wf = workflow("when:\n - event: push\n tag: 'v*'"); expect(matchesPush(wf, "refs/tags/v1.0")).toBe(true); expect(matchesPush(wf, "refs/heads/v1.0")).toBe(false); }); it("requires the push event", () => { const wf = workflow("when:\n - event: pull_request\n branch: main"); expect(matchesPush(wf, "refs/heads/main")).toBe(false); }); it("matches when any constraint matches", () => { const wf = workflow( "when:\n - event: push\n branch: main\n - event: push\n branch: dev", ); expect(matchesPush(wf, "refs/heads/dev")).toBe(true); }); it("ignores paths constraints", () => { const wf = workflow("when:\n - event: push\n branch: main\n paths: ['docs/**']"); expect(matchesPush(wf, "refs/heads/main")).toBe(true); }); }); describe("matchesPullRequest", () => { const pr = "when:\n - event: pull_request\n branch: [main, 'release/*']\n"; it.each([ ["no constraints", "", "dev", "synchronize", true], ["the target branch", pr, "main", "opened", true], ["a glob of the target branch", pr, "release/1", "synchronize", true], ["another target branch", pr, "dev", "opened", false], ["the reopened default type", pr, "main", "reopened", true], ["an action outside the default types", pr, "main", "closed", false], [ "a push constraint", "when:\n - event: push\n branch: main\n", "main", "opened", false, ], ["no branch list", "when:\n - event: pull_request\n", "main", "opened", false], ["a listed type", `${pr} types: [synchronize]\n`, "main", "synchronize", true], ["an unlisted type", `${pr} types: [synchronize]\n`, "main", "opened", false], ])("with %s", (_, raw, target, action, expected) => { expect(matchesPullRequest(workflow(raw), target, action)).toBe(expected); }); }); describe("isTrusted", () => { const release = "trusted:\n branch: [main, 'release/*', '**']\n"; it.each([ ["push", undefined, "main", true], ["manual", undefined, "main", true], ["push", undefined, "dev", false], ["push", "engine: cloudflare\n", "main", true], ["push", release, "release/1", true], ["pull_request", release, "main", false], ["pull_request", release, "pull/3abc", false], ["push", "trusted:\n branch: []\n", "main", false], ["push", "trusted: [", "main", false], ["push", "trusted: main", "main", false], ] as const)("trusts a %s run with %j on %s: %s", (event, raw, branch, expected) => { expect(isTrusted(event, raw, "main", branch)).toBe(expected); }); it("uses the default branch name", () => { expect(isTrusted("push", undefined, "trunk", "trunk")).toBe(true); expect(isTrusted("push", undefined, "trunk", "main")).toBe(false); }); }); describe("triggers", () => { it("lists each event of the constraints once as an NSID", () => { const wf = parseWorkflow( "ci.yml", "when:\n - event: [push, manual]\n - event: [pull_request, push, other]\n", ); expect(triggers(wf)).toEqual([ "org.tangled.event.push", "org.tangled.ci.trigger.manual", "org.tangled.event.pullRequest", "other", ]); }); it("lists nothing for a workflow without constraints", () => { expect(triggers(parseWorkflow("ci.yml", "engine: x\n"))).toEqual([]); }); });