import { describe, expect, it } from "vitest";
import { canAdminister, isDomnaEmail } from "./portfolioAdmin";

describe("isDomnaEmail", () => {
  it("identifies @domna.homes addresses as internal", () => {
    expect(isDomnaEmail("khalim@domna.homes")).toBe(true);
  });

  it("is case-insensitive on the domain", () => {
    expect(isDomnaEmail("Khalim@Domna.Homes")).toBe(true);
  });

  it("rejects look-alike domains and prefixes", () => {
    expect(isDomnaEmail("user@example.com")).toBe(false);
    expect(isDomnaEmail("user@domna.homes.attacker.com")).toBe(false);
    expect(isDomnaEmail("user@notdomna.homes")).toBe(false);
  });
});

describe("canAdminister", () => {
  it("grants admin powers to portfolio creator", () => {
    expect(canAdminister("creator")).toBe(true);
  });

  it("grants admin powers to portfolio admins and Domna employees", () => {
    expect(canAdminister("admin")).toBe(true);
    expect(canAdminister("domna")).toBe(true);
  });

  it("denies admin powers to read/write members and non-members", () => {
    expect(canAdminister("write")).toBe(false);
    expect(canAdminister("read")).toBe(false);
    expect(canAdminister("none")).toBe(false);
  });
});
