import { describe, expect, it } from "vitest";
import { checkErpKitVersion } from "./erp-kit-version";
import type { WorkspacePackage } from "./shared";

describe("checkErpKitVersion", () => {
  function erpKitPkg(version: string): WorkspacePackage[] {
    return [
      {
        relativePath: "apps/web/backend/package.json",
        manifest: { dependencies: { "@tailor-platform/erp-kit": version } },
      },
    ];
  }

  it("passes when the pin equals the installed version", () => {
    expect(checkErpKitVersion(erpKitPkg("0.29.0"), "0.29.0")).toEqual([]);
  });

  it("ignores packages that don't declare erp-kit", () => {
    expect(
      checkErpKitVersion([{ relativePath: "p/package.json", manifest: {} }], "0.29.0"),
    ).toEqual([]);
  });

  it("errors when the pin differs from the installed version", () => {
    const [finding] = checkErpKitVersion(erpKitPkg("0.27.0"), "0.29.0");
    expect(finding?.severity).toBe("error");
    expect(finding?.check).toBe("erp-kit-version");
    expect(finding?.subject).toBe("apps/web/backend/package.json");
  });

  it("errors when erp-kit is declared as a range", () => {
    const [finding] = checkErpKitVersion(erpKitPkg("^0.29.0"), "0.29.0");
    expect(finding?.severity).toBe("error");
    expect(finding?.message).toContain("must be pinned to the exact installed version");
  });

  it("warns on a non-semver specifier", () => {
    const [finding] = checkErpKitVersion(erpKitPkg("catalog:"), "0.29.0");
    expect(finding?.severity).toBe("warning");
    expect(finding?.message).toContain("catalog:");
  });

  it("treats the workspace protocol as dev-repo info, not a violation", () => {
    const [finding] = checkErpKitVersion(erpKitPkg("workspace:*"), "0.29.0");
    expect(finding?.severity).toBe("info");
    expect(finding?.message).toContain("workspace protocol");
  });
});
