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" [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(`[build]\nrun_sh = "make"`)).toBeNull(); }); test("returns null on invalid TOML", () => { expect(parseCiConfig("image = [unclosed")).toBeNull(); }); test("excludes reserved table names from steps", () => { const cfg = parseCiConfig(` image = "alpine" [on] push = ["main"] [variables] [variables.FOO] default = "bar" [step1] run_sh = "echo hi" `); expect(cfg).not.toBeNull(); expect(cfg!.steps.map((s) => s.name)).toEqual(["step1"]); }); test("preserves step order", () => { const cfg = parseCiConfig(` image = "alpine" [setup] run_sh = "apt install" [compile] run_sh = "make" [test] run_sh = "./test.sh" `); expect(cfg!.steps.map((s) => s.name)).toEqual([ "setup", "compile", "test", ]); }); 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"] `); 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(["/root/.npm"]); }); test("parses on.push as array", () => { const cfg = parseCiConfig(` image = "alpine" [on] push = ["main", "develop"] `); expect(cfg!.on?.push).toEqual(["main", "develop"]); }); test("parses on.tag and on.manual", () => { const cfg = parseCiConfig(` image = "alpine" [on] tag = true manual = true `); 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" `); 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" [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/"]); }); }); describe("shouldTriggerPush", () => { function cfg(push: string[] | boolean) { return parseCiConfig( `image="alpine"\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"')!; 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[on]\ntag=true')!; expect(shouldTriggerTag(c)).toBe(true); }); test("returns false when on.tag = false", () => { const c = parseCiConfig('image="alpine"\n[on]\ntag=false')!; expect(shouldTriggerTag(c)).toBe(false); }); test("returns false when on.tag is absent", () => { const c = parseCiConfig('image="alpine"')!; 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"] `); 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"] `); 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"] `); 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"] `); expect(validateCiConfig(cfg)).toBeNull(); }); test("ignores cache when nothing is cloned", () => { const cfg = parse(` image = "alpine" cache = ["/anything"] `); expect(validateCiConfig(cfg)).toBeNull(); }); });