import { describe, test, expect } from "bun:test"; import { parseCiConfig, shouldTriggerPush, shouldTriggerTag, validateCiConfig, } from "../src/services/ci.ts"; describe("parseCiConfig", () => { test("parses a minimal valid config", () => { const cfg = parseCiConfig(` image = "debian:latest" [[steps]] name = "build" run_sh = "make all" `); expect(cfg).not.toBeNull(); expect(cfg!.image).toBe("debian:latest"); expect(cfg!.steps).toHaveLength(1); expect(cfg!.steps[0]!.name).toBe("build"); expect(cfg!.steps[0]!.run_sh).toBe("make all"); }); test("returns null when image is missing", () => { expect( parseCiConfig(`[[steps]]\nname = "build"\nrun_sh = "make"`), ).toBeNull(); }); test("returns null on invalid TOML", () => { expect(parseCiConfig("image = [unclosed")).toBeNull(); }); test("returns null when steps are missing", () => { expect(parseCiConfig(`image = "alpine"`)).toBeNull(); }); test("returns null when a step has no name", () => { expect( parseCiConfig(`image = "alpine"\n[[steps]]\nrun_sh = "make"`), ).toBeNull(); }); test("preserves step order, including numeric names", () => { // An object keyed by step name would reorder this: JavaScript // enumerates integer-like keys first. const cfg = parseCiConfig(` image = "alpine" [[steps]] name = "setup" run_sh = "apt install" [[steps]] name = "2024" run_sh = "echo year" [[steps]] name = "test" run_sh = "./test.sh" `); expect(cfg!.steps.map((s) => s.name)).toEqual([ "setup", "2024", "test", ]); }); test("parses copy entries", () => { const cfg = parseCiConfig(` image = "alpine" [[copy]] image = "docker.io/oven/bun:1.4.0-alpine" from = "/usr/local/bin/bun" to = "/usr/local/bin" [[steps]] name = "build" run_sh = "make" `); expect(cfg!.copy).toEqual([ { image: "docker.io/oven/bun:1.4.0-alpine", from: "/usr/local/bin/bun", to: "/usr/local/bin", }, ]); }); test("returns null when a copy entry is incomplete", () => { expect( parseCiConfig(` image = "alpine" [[copy]] image = "busybox" from = "/bin/busybox" [[steps]] name = "build" run_sh = "make" `), ).toBeNull(); }); test("parses optional top-level fields", () => { const cfg = parseCiConfig(` image = "alpine" work_dir = "/ci" clone_project_to = "/ci/repo" shell_setup = "set -e" timeout = 1800 cpu_limit = 2.0 memory_limit = "1g" cache = ["/root/.npm"] [[steps]] name = "build" run_sh = "make" `); expect(cfg!.work_dir).toBe("/ci"); expect(cfg!.clone_project_to).toBe("/ci/repo"); expect(cfg!.shell_setup).toBe("set -e"); expect(cfg!.timeout).toBe(1800); expect(cfg!.cpu_limit).toBe(2.0); expect(cfg!.memory_limit).toBe("1g"); expect(cfg!.cache).toEqual([{ path: "/root/.npm" }]); }); test("parses on.push as array", () => { const cfg = parseCiConfig(` image = "alpine" [on] push = ["main", "develop"] [[steps]] name = "build" run_sh = "make" `); expect(cfg!.on?.push).toEqual(["main", "develop"]); }); test("parses on.tag and on.manual", () => { const cfg = parseCiConfig(` image = "alpine" [on] tag = true manual = true [[steps]] name = "build" run_sh = "make" `); expect(cfg!.on?.tag).toBe(true); expect(cfg!.on?.manual).toBe(true); }); test("parses variables with default and description", () => { const cfg = parseCiConfig(` image = "alpine" [variables] [variables.DEPLOY_ENV] default = "staging" description = "Target environment" [variables.VERSION] default = "1.0.0" [[steps]] name = "build" run_sh = "make" `); expect(cfg!.variables?.DEPLOY_ENV).toEqual({ default: "staging", description: "Target environment", }); expect(cfg!.variables?.VERSION?.default).toBe("1.0.0"); }); test("parses step-level fields", () => { const cfg = parseCiConfig(` image = "alpine" [[steps]] name = "test" run_sh = "./run_tests" run_if = 'test -n "\${CI_COMMIT_TAG}"' clear = true timeout = 300 publish_file = ["/dist/binary"] publish_gzip = ["/dist/"] `); const step = cfg!.steps[0]!; expect(step.run_sh).toBe("./run_tests"); expect(step.run_if).toBe('test -n "${CI_COMMIT_TAG}"'); expect(step.clear).toBe(true); expect(step.timeout).toBe(300); expect(step.publish_file).toEqual(["/dist/binary"]); expect(step.publish_gzip).toEqual(["/dist/"]); }); test("parses always and warn_on_fail", () => { const cfg = parseCiConfig(` image = "alpine" [[steps]] name = "lint" run_sh = "make lint" warn_on_fail = true [[steps]] name = "cleanup" run_sh = "rm -rf /scratch" always = true `); expect(cfg!.steps[0]!.warn_on_fail).toBe(true); expect(cfg!.steps[0]!.always).toBeUndefined(); expect(cfg!.steps[1]!.always).toBe(true); }); }); describe("shouldTriggerPush", () => { function cfg(push: string[] | boolean) { return parseCiConfig( `image="alpine"\n[[steps]]\nname="b"\n[on]\npush=${JSON.stringify(push)}`, )!; } test("matches exact branch name", () => { expect(shouldTriggerPush(cfg(["main"]), "main")).toBe(true); expect(shouldTriggerPush(cfg(["main"]), "develop")).toBe(false); }); test("wildcard * matches any branch", () => { expect(shouldTriggerPush(cfg(["*"]), "anything")).toBe(true); expect(shouldTriggerPush(cfg(["*"]), "main")).toBe(true); }); test("matches one of multiple branches", () => { const c = cfg(["main", "release"]); expect(shouldTriggerPush(c, "main")).toBe(true); expect(shouldTriggerPush(c, "release")).toBe(true); expect(shouldTriggerPush(c, "feature/x")).toBe(false); }); test("returns false when on.push is absent", () => { const c = parseCiConfig('image="alpine"\n[[steps]]\nname="b"')!; expect(shouldTriggerPush(c, "main")).toBe(false); }); test("glob prefix matching", () => { const c = cfg(["release/*"]); expect(shouldTriggerPush(c, "release/1.0")).toBe(true); expect(shouldTriggerPush(c, "main")).toBe(false); }); }); describe("shouldTriggerTag", () => { test("returns true when on.tag = true", () => { const c = parseCiConfig('image="alpine"\n[[steps]]\nname="b"\n[on]\ntag=true')!; expect(shouldTriggerTag(c)).toBe(true); }); test("returns false when on.tag = false", () => { const c = parseCiConfig('image="alpine"\n[[steps]]\nname="b"\n[on]\ntag=false')!; expect(shouldTriggerTag(c)).toBe(false); }); test("returns false when on.tag is absent", () => { const c = parseCiConfig('image="alpine"\n[[steps]]\nname="b"')!; expect(shouldTriggerTag(c)).toBe(false); }); }); describe("validateCiConfig", () => { const parse = (toml: string) => parseCiConfig(toml)!; test("accepts a cache path outside the clone directory", () => { const cfg = parse(` image = "alpine" clone_project_to = "/ci/build/project" cache = ["/ci/cache/target", "/usr/local/cargo/registry"] [[steps]] name = "build" run_sh = "make" `); expect(validateCiConfig(cfg)).toBeNull(); }); test("rejects a cache path inside the clone directory", () => { const cfg = parse(` image = "alpine" clone_project_to = "/ci/build/project" cache = ["/ci/build/project/target"] [[steps]] name = "build" run_sh = "make" `); expect(validateCiConfig(cfg)).toContain("/ci/build/project/target"); }); test("rejects a cache path equal to the clone directory", () => { const cfg = parse(` image = "alpine" clone_project_to = "/ci/build/project" cache = ["/ci/build/project"] [[steps]] name = "build" run_sh = "make" `); expect(validateCiConfig(cfg)).not.toBeNull(); }); test("does not treat a sibling prefix as inside", () => { const cfg = parse(` image = "alpine" clone_project_to = "/ci/build/project" cache = ["/ci/build/project-cache"] [[steps]] name = "build" run_sh = "make" `); expect(validateCiConfig(cfg)).toBeNull(); }); test("ignores cache when nothing is cloned", () => { const cfg = parse(` image = "alpine" cache = ["/anything"] [[steps]] name = "build" run_sh = "make" `); expect(validateCiConfig(cfg)).toBeNull(); }); }); describe("validateCiConfig, copy destinations", () => { const parse = (copyTo: string) => parseCiConfig(` image = "alpine" [[copy]] image = "busybox" from = "/bin/busybox" to = "${copyTo}" [[steps]] name = "build" run_sh = "make" `)!; test("accepts a directory destination", () => { expect(validateCiConfig(parse("/usr/local/bin"))).toBeNull(); }); test("rejects a destination that repeats the source basename", () => { // Looks like a rename, silently produces /usr/local/bin/busybox/busybox. const err = validateCiConfig(parse("/usr/local/bin/busybox")); expect(err).toContain("/usr/local/bin/busybox/busybox"); }); }); describe("cache entries", () => { test("accepts bare paths and tables in one array", () => { const cfg = parseCiConfig(` image = "alpine" cache = [ { path = "/ci/cache/target", max_size = "12g" }, "/usr/local/cargo/registry", { path = "/root/.bun/install/cache" }, ] [[steps]] name = "build" run_sh = "make" `); expect(cfg!.cache).toEqual([ { path: "/ci/cache/target", maxSize: 12 * 1024 ** 3 }, { path: "/usr/local/cargo/registry" }, { path: "/root/.bun/install/cache" }, ]); }); test("rejects a size it cannot parse", () => { // parseMemoryBytes answers 0 here, which would be a limit every cache // exceeds, wiping it after every run. expect( parseCiConfig(` image = "alpine" cache = [{ path = "/c", max_size = "12 GiB" }] [[steps]] name = "build" run_sh = "make" `), ).toBeNull(); }); test("rejects a table with no path", () => { expect( parseCiConfig(` image = "alpine" cache = [{ max_size = "1g" }] [[steps]] name = "build" run_sh = "make" `), ).toBeNull(); }); test("still rejects a capped cache inside the clone directory", () => { const cfg = parseCiConfig(` image = "alpine" clone_project_to = "/ci/build/project" cache = [{ path = "/ci/build/project/target", max_size = "1g" }] [[steps]] name = "build" run_sh = "make" `)!; expect(validateCiConfig(cfg)).toContain("/ci/build/project/target"); }); });