import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

vi.mock("./cookie-consent-storage", () => ({
  readAnalyticsConsent: vi.fn()
}));

import { readAnalyticsConsent } from "./cookie-consent-storage";
import { trackEvent } from "./analytics";

const readConsent = vi.mocked(readAnalyticsConsent);

describe("trackEvent (Matomo)", () => {
  beforeEach(() => {
    readConsent.mockReset();
    readConsent.mockReturnValue("analytics-rejected");
    vi.stubGlobal("window", { _paq: [] as unknown[][] });
  });

  afterEach(() => {
    vi.unstubAllGlobals();
  });

  it("n’envoie rien sans consentement analytics", () => {
    trackEvent("recipe", "share", "x");
    expect(window._paq?.length ?? 0).toBe(0);
  });

  it("pousse un trackEvent Matomo lorsque le consentement est accepté", () => {
    readConsent.mockReturnValue("analytics-accepted");
    vi.stubGlobal("window", { _paq: [] as unknown[][] });
    trackEvent("recipe", "share", "y");
    expect(window._paq).toContainEqual(["trackEvent", "recipe", "share", "y"]);
  });
});
