import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { describe, expect, it } from "vitest"; const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const WORKFLOW_PATH = path.join(REPO_ROOT, ".gitea/workflows/loc-history.yml"); const README_PATH = path.join(REPO_ROOT, "README.md"); function read(filePath) { return fs.readFileSync(filePath, "utf8"); } describe("repository metrics publication contract", () => { it("publishes only full first-parent main history with pinned actions", () => { const workflow = read(WORKFLOW_PATH); expect(workflow).toMatch(/push:\n\s+branches:\n\s+- main/); expect(workflow).toContain("workflow_dispatch:"); expect(workflow).toContain("actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5"); expect(workflow).toContain("fetch-depth: 0"); expect(workflow).toContain("persist-credentials: false"); expect(workflow).toContain("actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020"); expect(workflow).toContain("bash scripts/ci/persist-node-toolchain.sh"); expect(workflow).toContain('if [ "$SOURCE_REF" != "refs/heads/main" ]'); expect(workflow).toContain("--ref HEAD"); expect(workflow).toContain("--label origin/main"); expect(workflow).toContain("node scripts/metrics/commit-history.mjs"); expect(workflow).toContain("--days 60"); }); it("fails closed unless the built-in Actions token is repository-scoped and non-admin", () => { const workflow = read(WORKFLOW_PATH); expect(workflow).toContain("LOC_METRICS_GITEA_TOKEN"); // Releases sit in the repository scope: `code: write` alone returned 403. expect(workflow).toMatch(/^permissions:\n {2}contents: write$/m); expect(workflow).not.toMatch(/^ {2}code: write$/m); expect(workflow).toContain("The Actions token cannot read releases"); expect(workflow.match(/LOC_METRICS_GITEA_TOKEN: \$\{\{ gitea\.token \}\}/g)).toHaveLength(2); expect(workflow).toContain("/api/v1/repos/toolctrl-labs/HiveGrid-Cloud"); expect(workflow).toContain('repository.full_name !== "toolctrl-labs/HiveGrid-Cloud"'); expect(workflow).toContain("repository.permissions?.push !== true"); expect(workflow).toContain("repository.permissions?.admin === true"); expect(workflow).toContain("AbortSignal.timeout(15_000)"); expect(workflow).not.toContain("LOC_METRICS_BOT_LOGIN"); expect(workflow).not.toContain("secrets.LOC_METRICS_GITEA_TOKEN"); }); it("publishes all four artefacts as release assets instead of a long-lived branch", () => { const workflow = read(WORKFLOW_PATH); expect(workflow).toContain('const TAG = "loc-metrics";'); expect(workflow).toContain('const FILES = ["commit-history.csv", "commit-history.svg", "loc-history.csv", "loc-history.svg"];'); expect(workflow).toContain("/releases/tags/${TAG}"); expect(workflow).toContain("target_commitish: sourceSha"); expect(workflow).toContain("/releases/${release.id}/assets"); expect(workflow).toContain('method: "DELETE"'); expect(workflow).toContain("AbortSignal.timeout(30_000)"); expect(workflow).toContain("draft: false"); }); it("keeps the repository history free of metrics commits", () => { const workflow = read(WORKFLOW_PATH); // The branch mechanism is gone for good: no checkout, no commit, no push. expect(workflow).not.toContain("refs/heads/loc-metrics"); expect(workflow).not.toContain("checkout --orphan"); expect(workflow).not.toContain("git commit"); expect(workflow).not.toMatch(/git[^\n]*push/); expect(workflow).not.toContain("GIT_ASKPASS"); expect(workflow).not.toMatch(/https?:\/\/[^\n]*LOC_METRICS_GITEA_TOKEN/); }); it("embeds both live SVGs and links their CSVs plus the local generators", () => { const readme = read(README_PATH); const assetBase = "https://git.tool-ctrl.de/toolctrl-labs/HiveGrid-Cloud/releases/download/loc-metrics"; expect(readme).toContain(`![LOC-Verlauf](${assetBase}/loc-history.svg)`); expect(readme).toContain(`${assetBase}/loc-history.csv`); expect(readme).toContain("scripts/metrics/loc-history.mjs"); expect(readme).toContain("Fork-/Code-Import vom 04.06.2026"); expect(readme).toContain(`![Commit-Aktivität](${assetBase}/commit-history.svg)`); expect(readme).toContain(`${assetBase}/commit-history.csv`); expect(readme).toContain("scripts/metrics/commit-history.mjs"); expect(readme).toContain("letzten 60 Kalendertage"); }); });