Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

4.7 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { execFileSync, spawnSync } from "node:child_process";
import {
  mkdirSync,
  mkdtempSync,
  readFileSync,
  rmSync,
  writeFileSync
} from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { spdxLicenseHeader } from "../conformance/spdx-license-header";
import { LICENSE_HEADERS } from "../license-headers";
import { scanAll } from "../run";
import { listLicenseCandidates, loadLicenseFiles } from "./license-files";

const PACKAGE_DIR = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
const SCRIPT = join(PACKAGE_DIR, "src/scripts/license-headers.ts");
const TSX = join(PACKAGE_DIR, "node_modules/.bin/tsx");

let root: string;

function write(path: string, contents: string) {
  mkdirSync(dirname(join(root, path)), { recursive: true });
  writeFileSync(join(root, path), contents);
}

function git(...args: string[]) {
  execFileSync("git", args, { cwd: root, stdio: "ignore" });
}

beforeEach(() => {
  root = mkdtempSync(join(tmpdir(), "license-headers-"));
  git("init", "-q");
  write("apps/erp/app/root.tsx", '"use client";\nexport {};\n');
  write("apps/erp/app/Form.ee.tsx", "export {};\n");
  write("packages/ee/src/plan.ts", "export {};\n");
  write("scripts/run.mjs", "#!/usr/bin/env node\nconsole.log(1);\n");
  write("crates/x/src/lib.rs", "//! docs\n#![deny(missing_docs)]\n");
  write("sst.config.ts", "export default {};\n");
  write("apps/erp/sst-env.d.ts", "/* auto-generated by SST */\n");
  write("apps/erp/app/styles.css", "a {}\n");
  write("contrib/example.ts", "export {};\n");
  write(".claude/skill.mjs", "export {};\n");
  git("add", "-A");
  // Untracked: never scanned.
  write("apps/erp/app/untracked.ts", "export {};\n");
});

afterEach(() => {
  rmSync(root, { recursive: true, force: true });
});

const run = (...args: string[]) =>
  spawnSync(TSX, [SCRIPT, "--root", root, ...args], {
    encoding: "utf8",
    env: { ...process.env, INIT_CWD: root }
  });

describe("listLicenseCandidates", () => {
  it("lists tracked, in-root, in-scope files only", () => {
    expect(listLicenseCandidates(root)).toEqual([
      "apps/erp/app/Form.ee.tsx",
      "apps/erp/app/root.tsx",
      "apps/erp/sst-env.d.ts",
      "crates/x/src/lib.rs",
      "packages/ee/src/plan.ts",
      "scripts/run.mjs",
      "sst.config.ts"
    ]);
  });

  it("does not read path-excluded files", () => {
    expect(loadLicenseFiles(root).map((f) => f.file)).not.toContain(
      "apps/erp/sst-env.d.ts"
    );
  });
});

describe("license-headers script", () => {
  it("--dry-run and --check write nothing; write mode fixes; a rerun is a no-op", () => {
    const before = readFileSync(join(root, "scripts/run.mjs"), "utf8");

    const dry = run("--dry-run");
    expect(dry.status).toBe(0);
    expect(dry.stdout).toMatch(/AGPL-3\.0-only: +0 correct, 4 missing/);
    expect(dry.stdout).toMatch(/Carbon Commercial: +0 correct, 2 missing/);
    expect(dry.stdout).toMatch(/Excluded: +1/);

    const check = run("--check");
    expect(check.status).toBe(1);
    expect(check.stdout).toMatch(
      /missing +commercial +packages\/ee\/src\/plan\.ts/
    );
    expect(readFileSync(join(root, "scripts/run.mjs"), "utf8")).toBe(before);
    expect(scanAll(loadLicenseFiles(root), [spdxLicenseHeader])).toHaveLength(
      6
    );

    expect(run().status).toBe(0);
    expect(readFileSync(join(root, "scripts/run.mjs"), "utf8")).toBe(
      `#!/usr/bin/env node\n${LICENSE_HEADERS.agpl.join("\n")}\n\nconsole.log(1);\n`
    );
    expect(
      readFileSync(join(root, "apps/erp/app/Form.ee.tsx"), "utf8")
    ).toMatch(/^\/\/ SPDX-License-Identifier: LicenseRef-Carbon-Commercial\n/);
    expect(readFileSync(join(root, "apps/erp/sst-env.d.ts"), "utf8")).toBe(
      "/* auto-generated by SST */\n"
    );
    expect(readFileSync(join(root, "apps/erp/app/untracked.ts"), "utf8")).toBe(
      "export {};\n"
    );

    // The check and the fixer agree: nothing left.
    expect(scanAll(loadLicenseFiles(root), [spdxLicenseHeader])).toEqual([]);
    expect(run("--check").status).toBe(0);
    const again = run();
    expect(again.stdout).toMatch(/^0 fixed\.$/m);
  }, 30_000);

  it("limits a run to the given paths", () => {
    expect(run("sst.config.ts").status).toBe(0);
    expect(readFileSync(join(root, "sst.config.ts"), "utf8")).toMatch(
      /^\/\/ SPDX-License-Identifier: AGPL-3\.0-only\n/
    );
    expect(readFileSync(join(root, "packages/ee/src/plan.ts"), "utf8")).toBe(
      "export {};\n"
    );
  }, 30_000);
});