import { describe, it, expect } from "vitest";
import { extractGroups, isAdminGroupMember } from "@/lib/auth/authentik";

describe("isAdminGroupMember", () => {
  it("true, wenn die Admin-Gruppe enthalten ist", () => {
    expect(
      isAdminGroupMember(["wehr-x", "floriannetz-admins"], "floriannetz-admins"),
    ).toBe(true);
  });

  it("false, wenn die Admin-Gruppe fehlt", () => {
    expect(isAdminGroupMember(["wehr-x"], "floriannetz-admins")).toBe(false);
  });

  it("false bei leerer Gruppenliste", () => {
    expect(isAdminGroupMember([], "floriannetz-admins")).toBe(false);
  });
});

describe("extractGroups", () => {
  it("liest den groups-Claim", () => {
    expect(extractGroups({ sub: "1", groups: ["a", "b"] })).toEqual(["a", "b"]);
  });

  it("leeres Array, wenn kein groups-Claim vorhanden ist", () => {
    expect(extractGroups({ sub: "1" })).toEqual([]);
  });

  it("leeres Array bei undefined/null", () => {
    expect(extractGroups(undefined)).toEqual([]);
    expect(extractGroups(null)).toEqual([]);
  });

  it("defensiv: ignoriert nicht-string-Arrays", () => {
    expect(extractGroups({ groups: "kein-array" })).toEqual([]);
    expect(extractGroups({ groups: [1, 2, 3] })).toEqual([]);
  });
});
