import { describe, expect, it } from "vitest";
import { validatePermissionKey } from "./validatePermissionKey";

describe("validatePermissionKey", () => {
  it("accepts 3-part key (module:scope:command)", () => {
    expect(validatePermissionKey("user-management:role:createRole")).toBe(true);
    expect(validatePermissionKey("audit:auditEvent:logAuditEvent")).toBe(true);
  });

  it("accepts 2-part key (module:scope)", () => {
    expect(validatePermissionKey("user-management:role")).toBe(true);
    expect(validatePermissionKey("audit:auditEvent")).toBe(true);
  });

  it("accepts 1-part key (module)", () => {
    expect(validatePermissionKey("user-management")).toBe(true);
    expect(validatePermissionKey("audit")).toBe(true);
  });

  it("rejects empty string", () => {
    expect(validatePermissionKey("")).toBe(false);
  });

  it("rejects key starting with number", () => {
    expect(validatePermissionKey("1module")).toBe(false);
  });

  it("rejects key with trailing colon", () => {
    expect(validatePermissionKey("user-management:")).toBe(false);
  });

  it("rejects key with 4 parts", () => {
    expect(validatePermissionKey("a:b:c:d")).toBe(false);
  });

  it("rejects key with empty segment", () => {
    expect(validatePermissionKey("user-management::createRole")).toBe(false);
  });
});
