import { describe, expect, it } from "vitest";
import { normalizeRole, resolveProfileRole } from "./role";

describe("normalizeRole", () => {
  it("normalizes known role aliases", () => {
    expect(normalizeRole("tenant_member")).toBe("user");
    expect(normalizeRole("admin")).toBe("user");
    expect(normalizeRole("superadmin")).toBe("super_admin");
    expect(normalizeRole("tenantadmin")).toBe("tenant_admin");
    expect(normalizeRole("rpadmin")).toBe("rp_admin");
  });

  it("returns 'user' for unknown string values and empty string for non-strings", () => {
    expect(normalizeRole("  custom_role  ")).toBe("user");
    expect(normalizeRole(123)).toBe("");
  });
});

describe("resolveProfileRole", () => {
  it("prefers the first non-empty normalized role candidate", () => {
    expect(
      resolveProfileRole({
        role: " ",
        grade: "tenant_member",
        "custom:role": "admin",
      }),
    ).toBe("user");
  });

  it("returns an empty string when no role is present", () => {
    expect(resolveProfileRole(undefined)).toBe("");
  });
});
