spb/phi-conseil
Public
1import { describe, expect, it } from "vitest";2import {3 allAccepted,4 allRejected,5 hasConsent,6 parseConsent,7 readConsentCookie,8 serializeConsent,9 CONSENT_VERSION,10} from "@/lib/consent";1112describe("consentement aux témoins", () => {13 it("sérialise et relit un consentement", () => {14 const c = allAccepted();15 expect(parseConsent(serializeConsent(c))).toMatchObject({16 analytics: true,17 marketing: true,18 necessary: true,19 });20 });21 it("ignore une valeur corrompue ou d'une autre version", () => {22 expect(parseConsent("%7Bnope")).toBeNull();23 expect(24 parseConsent(25 encodeURIComponent(26 JSON.stringify({ version: CONSENT_VERSION + 1, analytics: true, marketing: true }),27 ),28 ),29 ).toBeNull();30 expect(31 parseConsent(32 encodeURIComponent(JSON.stringify({ version: CONSENT_VERSION, analytics: "oui" })),33 ),34 ).toBeNull();35 });36 it("lit le témoin dans un en-tête Cookie", () => {37 const header = `foo=1; phi_consent=${serializeConsent(allRejected())}; bar=2`;38 expect(readConsentCookie(header)).toMatchObject({ analytics: false, marketing: false });39 expect(readConsentCookie("foo=1")).toBeNull();40 });41 it("les témoins nécessaires sont toujours autorisés, les autres jamais sans choix", () => {42 expect(hasConsent(null, "necessary")).toBe(true);43 expect(hasConsent(null, "analytics")).toBe(false);44 expect(hasConsent(allAccepted(), "marketing")).toBe(true);45 });46});47