// @vitest-environment node import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import { getChangedFiles, selectSuitesForChangedFiles, writeHarnessArtifact } from "./core.mjs"; const tempDirs: string[] = []; afterEach(() => { for (const directory of tempDirs.splice(0)) { fs.rmSync(directory, { force: true, recursive: true }); } }); function createTempDir(prefix: string) { const directory = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); tempDirs.push(directory); return directory; } describe("harness changed-file helpers", () => { it("normalizes and de-duplicates explicit changed files", () => { expect(getChangedFiles({ changedFiles: ["scripts\\harness\\core.mjs", "scripts/harness/core.mjs"] })).toEqual([ "scripts/harness/core.mjs" ]); }); it("uses the injected git reader for explicit diff ranges", () => { const runGitCommandFn = vi.fn(() => ["scripts/harness/core.mjs", "README.md"]); expect(getChangedFiles({ from: "abc123", runGitCommandFn, to: "def456" })).toEqual([ "README.md", "scripts/harness/core.mjs" ]); expect(runGitCommandFn).toHaveBeenCalledWith([ "diff", "--name-only", "--diff-filter=ACMR", "abc123...def456" ]); }); it("falls back to a repo snapshot when the base ref is an all-zero sha", () => { const runGitCommandFn = vi.fn((args: string[]) => { if (args[0] === "ls-files" && args.length === 1) { return ["tracked.ts"]; } return ["untracked.ts"]; }); expect( getChangedFiles({ from: "0000000000000000000000000000000000000000", runGitCommandFn, to: "HEAD" }) ).toEqual(["tracked.ts", "untracked.ts"]); }); }); describe("selectSuitesForChangedFiles", () => { it("promotes harness control-plane changes to the PR gate", () => { expect(selectSuitesForChangedFiles(["scripts/harness/core.mjs"]).suites).toEqual(["pr"]); }); it("promotes release-risk changes to the release gate", () => { expect(selectSuitesForChangedFiles([".github/workflows/publish-packages.yml"]).suites).toEqual([ "release" ]); }); it("keeps docs stories on the docs review surfaces", () => { expect(selectSuitesForChangedFiles(["apps/docs/src/components/button.stories.tsx"]).suites).toEqual([ "static", "docs", "a11y", "docs-smoke" ]); }); it("keeps package source changes on component, docs, browser, and consumer surfaces", () => { expect(selectSuitesForChangedFiles(["packages/ui/src/components/button.tsx"]).suites).toEqual([ "component", "docs", "a11y", "docs-smoke", "consumers" ]); }); }); describe("writeHarnessArtifact", () => { it("writes the requested report into a target directory", () => { const outputDir = createTempDir("cadence-harness-artifacts-"); const outputPath = writeHarnessArtifact("selection", { suites: ["pr"] }, outputDir); expect(path.dirname(outputPath)).toBe(outputDir); expect(JSON.parse(fs.readFileSync(outputPath, "utf8"))).toEqual({ suites: ["pr"] }); }); });