import { describe, it, expect, beforeEach } from "vitest";
import { RecordingConsent } from "@repo/core-testing/instrumentation";
import { getCategoriesHandler } from "@/handlers/get-categories.handler";

describe("getCategoriesHandler", () => {
  let consent: RecordingConsent;

  beforeEach(() => {
    consent = new RecordingConsent();
  });

  it("returns { categories: [] } when no consent has been recorded", () => {
    expect(getCategoriesHandler(consent)).toEqual({ categories: [] });
  });

  it("returns granted categories after grant", async () => {
    await consent.grant("analytics");
    const { categories } = getCategoriesHandler(consent);
    expect(categories).toHaveLength(1);
    expect(categories[0]!.category).toBe("analytics");
    expect(categories[0]!.state).toBe("granted");
  });

  it("reflects withdrawn state after withdraw", async () => {
    await consent.grant("marketing");
    await consent.withdraw("marketing");
    const { categories } = getCategoriesHandler(consent);
    expect(categories).toHaveLength(1);
    expect(categories[0]!.state).toBe("denied");
  });

  it("returns all categories when multiple are recorded", async () => {
    await consent.grant("necessary");
    await consent.grant("analytics");
    await consent.grant("marketing");
    const { categories } = getCategoriesHandler(consent);
    expect(categories).toHaveLength(3);
    const catNames = categories.map((c) => c.category).sort();
    expect(catNames).toEqual(["analytics", "marketing", "necessary"]);
  });
});
