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.

9.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 { describe, expect, it } from "vitest";
import {
  applyLicenseHeader,
  classifyFile,
  classifyPath,
  inspectLicenseHeader,
  isLicenseCandidate,
  LICENSE_HEADERS
} from "./license-headers";

const AGPL = LICENSE_HEADERS.agpl.join("\n");
const COMMERCIAL = LICENSE_HEADERS.commercial.join("\n");

describe("classifyPath", () => {
  it("treats everything under packages/ee/ as commercial", () => {
    expect(classifyPath("packages/ee/src/plan.ts")).toEqual({
      kind: "commercial"
    });
    expect(classifyPath("packages/ee/src/a/b/c/deep.test.tsx")).toEqual({
      kind: "commercial"
    });
  });

  it("treats a `.ee.` file name anywhere as commercial", () => {
    expect(
      classifyPath(
        "apps/erp/app/modules/settings/ui/Approvals/ApprovalRuleForm.ee.tsx"
      )
    ).toEqual({ kind: "commercial" });
    expect(classifyPath("crates/x/src/thing.ee.rs")).toEqual({
      kind: "commercial"
    });
  });

  it("defaults to AGPL, and only looks at the file NAME for `.ee.`", () => {
    expect(classifyPath("apps/erp/app/root.tsx")).toEqual({ kind: "agpl" });
    expect(classifyPath("packages/eek/src/index.ts")).toEqual({
      kind: "agpl"
    });
    expect(classifyPath("apps/erp/app/x.ee/inner.ts")).toEqual({
      kind: "agpl"
    });
    expect(classifyPath("apps/erp/app/coffee.ts")).toEqual({ kind: "agpl" });
    expect(classifyPath("sst.config.ts")).toEqual({ kind: "agpl" });
  });

  it("excludes generated and third-party files, with a reason", () => {
    for (const path of [
      "sst-env.d.ts",
      "apps/erp/sst-env.d.ts",
      "packages/ee/sst-env.d.ts",
      "packages/database/src/types.ts",
      "packages/database/src/swagger-docs-schema.ts",
      "packages/ee/src/workflows/catalog/events.generated.ts",
      "packages/ee/src/paperless-parts/lib/client.ts",
      "packages/database/supabase/edge-runtime/main/index.ts"
    ]) {
      const result = classifyPath(path);
      expect(result, path).toMatchObject({ excluded: true });
      expect("reason" in result && result.reason.length > 0, path).toBe(true);
    }
    // A hand-written neighbour of a generated file is not excluded.
    expect(classifyPath("packages/ee/src/workflows/catalog/index.ts")).toEqual({
      kind: "commercial"
    });
  });
});

describe("classifyFile", () => {
  it("excludes the notification preview fixtures by their generator's marker", () => {
    const generated = `import { NotificationEmail } from "../NotificationEmail";

// Preview fixture — mirrors what getNotificationContent builds for the
// JobAssignment event. Not shipped (not exported from index.ts).
export default function JobAssignmentPreview() {}
`;
    expect(
      classifyFile(
        "packages/documents/src/email/previews/JobAssignment.tsx",
        generated
      )
    ).toMatchObject({ excluded: true });
    expect(
      classifyFile(
        "packages/documents/src/email/previews/QuoteEmail.tsx",
        'import { QuoteEmail } from "../QuoteEmail";\n'
      )
    ).toEqual({ kind: "agpl" });
  });

  it("excludes a file that says it is generated in its first lines", () => {
    expect(
      classifyFile("apps/erp/app/x.ts", "// @generated by foo\nexport {};\n")
    ).toMatchObject({ excluded: true });
    expect(
      classifyFile(
        "apps/erp/app/x.ts",
        "/* This file is auto-generated by SST. Do not edit. */\n"
      )
    ).toMatchObject({ excluded: true });
    // …but not a mention far down the file.
    const late = `${"const a = 1;\n".repeat(10)}// DO NOT EDIT below\n`;
    expect(classifyFile("apps/erp/app/x.ts", late)).toEqual({ kind: "agpl" });
  });
});

describe("isLicenseCandidate", () => {
  it("covers the roots and root-level files, with in-scope extensions only", () => {
    for (const path of [
      "apps/erp/app/root.tsx",
      "packages/utils/src/a.mjs",
      "crates/planner/src/lib.rs",
      "docs/next.config.mjs",
      "ci/src/index.ts",
      "scripts/strip-po-headers.mjs",
      "sst.config.ts",
      "lingui.config.js",
      "apps/x/y.cjs",
      "apps/x/y.jsx"
    ]) {
      expect(isLicenseCandidate(path), path).toBe(true);
    }
    for (const path of [
      "contrib/building/examples/a.ts",
      ".claude/skills/x/a.mjs",
      ".github/scripts/a.js",
      "patches/a.ts",
      "apps/erp/app/styles.css",
      "packages/database/supabase/migrations/1.sql",
      "docs/content/a.mdx",
      "apps/erp/package.json"
    ]) {
      expect(isLicenseCandidate(path), path).toBe(false);
    }
  });
});

describe("applyLicenseHeader", () => {
  it("puts the header first, then exactly one blank line", () => {
    expect(applyLicenseHeader('import a from "a";\n', "agpl")).toBe(
      `${AGPL}\n\nimport a from "a";\n`
    );
    expect(applyLicenseHeader("\n\n\nexport {};\n", "commercial")).toBe(
      `${COMMERCIAL}\n\nexport {};\n`
    );
  });

  it("keeps a shebang on line 1", () => {
    expect(
      applyLicenseHeader("#!/usr/bin/env node\nconsole.log(1);\n", "agpl")
    ).toBe(`#!/usr/bin/env node\n${AGPL}\n\nconsole.log(1);\n`);
    expect(
      applyLicenseHeader("#!/usr/bin/env node\n\nconsole.log(1);\n", "agpl")
    ).toBe(`#!/usr/bin/env node\n${AGPL}\n\nconsole.log(1);\n`);
  });

  it('goes above "use client" / "use server" directives', () => {
    expect(applyLicenseHeader('"use client";\n\nexport {};\n', "agpl")).toBe(
      `${AGPL}\n\n"use client";\n\nexport {};\n`
    );
    expect(applyLicenseHeader("'use server';\nexport {};\n", "agpl")).toBe(
      `${AGPL}\n\n'use server';\nexport {};\n`
    );
  });

  it("goes above @ts-nocheck, triple-slash references and biome-ignore", () => {
    const src =
      '// @ts-nocheck\n/// <reference types="node" />\n// biome-ignore-all lint/x: y\nexport {};\n';
    expect(applyLicenseHeader(src, "commercial")).toBe(
      `${COMMERCIAL}\n\n${src}`
    );
  });

  it("goes above Rust `//!` docs and `#![...]` attributes (not a shebang)", () => {
    const src = "//! Crate docs.\n#![deny(missing_docs)]\n\npub fn f() {}\n";
    expect(applyLicenseHeader(src, "agpl")).toBe(`${AGPL}\n\n${src}`);
    const attrFirst = "#![allow(dead_code)]\nfn f() {}\n";
    expect(applyLicenseHeader(attrFirst, "agpl")).toBe(
      `${AGPL}\n\n${attrFirst}`
    );
  });

  it("is idempotent", () => {
    for (const src of [
      "export {};\n",
      "#!/usr/bin/env node\nrun();\n",
      '"use client";\n',
      "//! docs\nfn f() {}\n",
      "no trailing newline",
      "a\r\nb\r\n",
      ""
    ]) {
      for (const kind of ["agpl", "commercial"] as const) {
        const once = applyLicenseHeader(src, kind);
        expect(applyLicenseHeader(once, kind), JSON.stringify(src)).toBe(once);
        expect(inspectLicenseHeader(once, kind)).toBe("ok");
      }
    }
  });

  it("replaces the other license's header (a file moved into packages/ee)", () => {
    const agpl = applyLicenseHeader("export const a = 1;\n", "agpl");
    expect(inspectLicenseHeader(agpl, "commercial")).toBe("wrong-license");
    const moved = applyLicenseHeader(agpl, "commercial");
    expect(moved).toBe(`${COMMERCIAL}\n\nexport const a = 1;\n`);
    expect(applyLicenseHeader(moved, "agpl")).toBe(agpl);
  });

  it("normalizes spacing and collapses a doubled header", () => {
    expect(applyLicenseHeader(`${AGPL}\nexport {};\n`, "agpl")).toBe(
      `${AGPL}\n\nexport {};\n`
    );
    expect(applyLicenseHeader(`${AGPL}\n\n\n\nexport {};\n`, "agpl")).toBe(
      `${AGPL}\n\nexport {};\n`
    );
    expect(
      applyLicenseHeader(`${COMMERCIAL}\n\n${AGPL}\n\nexport {};\n`, "agpl")
    ).toBe(`${AGPL}\n\nexport {};\n`);
    expect(inspectLicenseHeader(`${AGPL}\nexport {};\n`, "agpl")).toBe(
      "malformed"
    );
  });

  it("never strips the file's own comment that follows a header", () => {
    const src = `${AGPL}\n// What this module does.\n\nexport {};\n`;
    expect(applyLicenseHeader(src, "agpl")).toBe(
      `${AGPL}\n\n// What this module does.\n\nexport {};\n`
    );
  });

  it("preserves CRLF line endings and the (absent) trailing newline", () => {
    const crlf = "#!/usr/bin/env node\r\nimport a from 'a';\r\nrun(a);\r\n";
    const fixed = applyLicenseHeader(crlf, "agpl");
    expect(fixed).toBe(
      `#!/usr/bin/env node\r\n${LICENSE_HEADERS.agpl.join("\r\n")}\r\n\r\nimport a from 'a';\r\nrun(a);\r\n`
    );
    expect(fixed.replace(/\r\n/g, "")).not.toContain("\n");
    expect(applyLicenseHeader(fixed, "agpl")).toBe(fixed);
    expect(applyLicenseHeader(fixed, "commercial")).toBe(
      `#!/usr/bin/env node\r\n${LICENSE_HEADERS.commercial.join("\r\n")}\r\n\r\nimport a from 'a';\r\nrun(a);\r\n`
    );
    expect(applyLicenseHeader("export {}", "agpl")).toBe(
      `${AGPL}\n\nexport {}`
    );
  });

  it("keeps a byte-order mark first", () => {
    expect(applyLicenseHeader("export {};\n", "agpl")).toBe(
      `${AGPL}\n\nexport {};\n`
    );
  });

  it("leaves a file with a third-party notice untouched", () => {
    for (const src of [
      "// SPDX-License-Identifier: MIT\nexport {};\n",
      "/**\n * @license\n * Copyright 2024 Mozilla Foundation\n */\n",
      "// Copyright (c) 2019 Someone Else\nexport {};\n"
    ]) {
      expect(inspectLicenseHeader(src, "agpl")).toBe("foreign");
      expect(applyLicenseHeader(src, "agpl")).toBe(src);
    }
  });
});

describe("inspectLicenseHeader", () => {
  it("reports missing, ok and wrong-license", () => {
    expect(inspectLicenseHeader("export {};\n", "agpl")).toBe("missing");
    expect(inspectLicenseHeader(`${AGPL}\n\nexport {};\n`, "agpl")).toBe("ok");
    expect(inspectLicenseHeader(`${AGPL}\n\nexport {};\n`, "commercial")).toBe(
      "wrong-license"
    );
  });
});