import { describe, expect, it } from "vitest"; import { allAccepted, allRejected, hasConsent, parseConsent, readConsentCookie, serializeConsent, CONSENT_VERSION, } from "@/lib/consent"; describe("consentement aux témoins", () => { it("sérialise et relit un consentement", () => { const c = allAccepted(); expect(parseConsent(serializeConsent(c))).toMatchObject({ analytics: true, marketing: true, necessary: true, }); }); it("ignore une valeur corrompue ou d'une autre version", () => { expect(parseConsent("%7Bnope")).toBeNull(); expect( parseConsent( encodeURIComponent( JSON.stringify({ version: CONSENT_VERSION + 1, analytics: true, marketing: true }), ), ), ).toBeNull(); expect( parseConsent( encodeURIComponent(JSON.stringify({ version: CONSENT_VERSION, analytics: "oui" })), ), ).toBeNull(); }); it("lit le témoin dans un en-tête Cookie", () => { const header = `foo=1; phi_consent=${serializeConsent(allRejected())}; bar=2`; expect(readConsentCookie(header)).toMatchObject({ analytics: false, marketing: false }); expect(readConsentCookie("foo=1")).toBeNull(); }); it("les témoins nécessaires sont toujours autorisés, les autres jamais sans choix", () => { expect(hasConsent(null, "necessary")).toBe(true); expect(hasConsent(null, "analytics")).toBe(false); expect(hasConsent(allAccepted(), "marketing")).toBe(true); }); });