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

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

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

  it("returns { success: true }", async () => {
    const result = await grantHandler(consent, { category: "analytics" });
    expect(result).toEqual({ success: true });
  });

  it("calls consent.grant with the given category", async () => {
    await grantHandler(consent, { category: "marketing" });
    expect(consent.grants).toHaveLength(1);
    expect(consent.grants[0]!.category).toBe("marketing");
  });

  it("forwards meta to consent.grant", async () => {
    await grantHandler(consent, {
      category: "functional",
      meta: {
        bannerVersion: "v2",
        policyVersion: "2026-01",
        method: "banner-accept",
      },
    });
    expect(consent.grants[0]!.meta).toEqual({
      bannerVersion: "v2",
      policyVersion: "2026-01",
      method: "banner-accept",
    });
  });

  it("grants without meta when omitted", async () => {
    await grantHandler(consent, { category: "necessary" });
    expect(consent.grants[0]!.meta).toBeUndefined();
  });
});
