import { describe, test } from "node:test";
import assert from "node:assert/strict";

import { slugify, mintEntityId, normalizeProvenance } from "../src/id.js";

describe("id module (SC4)", () => {
  describe("slugify", () => {
    test("lowercases, trims, and dashes non-alphanumeric runs", () => {
      assert.equal(slugify("Current Role"), "current-role");
    });

    test("collapses repeated separators and strips edge dashes", () => {
      assert.equal(slugify("  Core Expertise Areas (5 topics)  "), "core-expertise-areas-5-topics");
    });

    test("is stable across case + whitespace variation of the same label", () => {
      assert.equal(slugify("Current Role"), slugify("  current   ROLE "));
    });
  });

  describe("mintEntityId", () => {
    test("is deterministic — same seed yields the same id", () => {
      assert.equal(
        mintEntityId({ kind: "profile-field", key: "Current Role" }),
        mintEntityId({ kind: "profile-field", key: "Current Role" }),
      );
    });

    test("is stable across label case/whitespace (id keyed on the slug)", () => {
      assert.equal(
        mintEntityId({ kind: "profile-field", key: "Current Role" }),
        mintEntityId({ kind: "profile-field", key: "  current   ROLE " }),
      );
    });

    test("different key yields a different id", () => {
      assert.notEqual(
        mintEntityId({ kind: "profile-field", key: "Current Role" }),
        mintEntityId({ kind: "profile-field", key: "Organization" }),
      );
    });

    test("different kind yields a different id (kind is part of the seed)", () => {
      assert.notEqual(
        mintEntityId({ kind: "profile-field", key: "role" }),
        mintEntityId({ kind: "expertise-area", key: "role" }),
      );
    });

    test("id is 12 lowercase hex characters", () => {
      assert.match(mintEntityId({ kind: "profile-field", key: "Name" }), /^[0-9a-f]{12}$/);
    });
  });

  describe("normalizeProvenance", () => {
    test("returns each accepted value unchanged", () => {
      assert.equal(normalizeProvenance("human"), "human");
      assert.equal(normalizeProvenance("published"), "published");
      assert.equal(normalizeProvenance("ai-draft"), "ai-draft");
    });

    test("trims and lowercases before matching", () => {
      assert.equal(normalizeProvenance("  Human "), "human");
      assert.equal(normalizeProvenance("AI-DRAFT"), "ai-draft");
    });

    test("throws on anything outside the provenance vocabulary", () => {
      assert.throws(() => normalizeProvenance("robot"));
      assert.throws(() => normalizeProvenance(""));
      assert.throws(() => normalizeProvenance("humanoid"));
    });
  });
});
