// : nothing from YouTube until the link is activated, // then the privacy-enhanced player in its place, focused; modified clicks and // bad ids are left to the link. import { afterAll, afterEach, beforeAll, expect, test } from "bun:test"; import { VIDEO_ID, embedUrl } from "../packages/lite-youtube/index"; import styles from "../packages/lite-youtube/lite-youtube.css"; const sleep = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms)); // happy-dom loads iframes for real by default: the suite never reaches YouTube. const settings = (window as unknown as { happyDOM: { settings: { disableIframePageLoading: boolean } } }).happyDOM.settings; const loading = settings.disableIframePageLoading; beforeAll(() => void (settings.disableIframePageLoading = true)); afterAll(() => void (settings.disableIframePageLoading = loading)); const mount = (video = "dQw4w9WgXcQ", title = "Bande-annonce : Maman, j’ai raté l’avion") => { const wrap = document.createElement("div"); wrap.innerHTML = ` L’affiche `; document.body.appendChild(wrap); const host = wrap.querySelector("pebble-lite-youtube") as HTMLElement; return { host, link: host.querySelector("a")! }; }; afterEach(async () => { document.body.textContent = ""; await sleep(0); }); test("before activation the element holds the server's link and nothing from YouTube", () => { const { host } = mount(); expect(host.querySelector("iframe")).toBeNull(); expect(host.querySelector("a")!.getAttribute("href")).toBe("https://www.youtube.com/watch?v=dQw4w9WgXcQ"); expect(document.querySelectorAll("link[rel=preconnect], script[src*=youtube]").length).toBe(0); expect(host.hasAttribute("data-playing")).toBe(false); }); test("a click replaces the link with the nocookie player, focused", () => { const { host, link } = mount(); const event = new MouseEvent("click", { bubbles: true, cancelable: true }); link.dispatchEvent(event); expect(event.defaultPrevented).toBe(true); const iframe = host.querySelector("iframe")!; expect(host.children.length).toBe(1); expect(iframe.getAttribute("src")).toBe("https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?autoplay=1&playsinline=1"); expect(iframe.getAttribute("allow")).toBe("autoplay; encrypted-media; picture-in-picture"); expect(iframe.hasAttribute("allowfullscreen")).toBe(true); expect(iframe.getAttribute("title")).toBe("Bande-annonce : Maman, j’ai raté l’avion"); expect(iframe.getAttribute("referrerpolicy")).toBe("strict-origin-when-cross-origin"); expect(host.hasAttribute("data-playing")).toBe(true); expect(document.activeElement).toBe(iframe); }); test("without a title, the poster's alt names the player", () => { const { host, link } = mount("dQw4w9WgXcQ", ""); link.click(); expect(host.querySelector("iframe")!.getAttribute("title")).toBe("L’affiche"); }); test("a modified click, or a video that is not an id, is left to the link", () => { const { host, link } = mount(); const ctrl = new MouseEvent("click", { bubbles: true, cancelable: true, ctrlKey: true }); link.dispatchEvent(ctrl); expect(ctrl.defaultPrevented).toBe(false); expect(host.querySelector("iframe")).toBeNull(); const bad = mount('x" onload="alert(1)'); const event = new MouseEvent("click", { bubbles: true, cancelable: true }); bad.link.dispatchEvent(event); expect(event.defaultPrevented).toBe(false); expect(bad.host.querySelector("iframe")).toBeNull(); }); test("ids and the embed address", () => { expect(VIDEO_ID.test("dQw4w9WgXcQ")).toBe(true); expect(VIDEO_ID.test("dQw4w9WgXc")).toBe(false); expect(VIDEO_ID.test("dQw4w9WgXc/")).toBe(false); expect(embedUrl("a_b-c")).toBe("https://www.youtube-nocookie.com/embed/a_b-c?autoplay=1&playsinline=1"); }); test("the sheet: 16:9, a 44px badge, dark defaults", () => { expect(styles).toMatch(/aspect-ratio:\s*16\s*\/\s*9/); expect(styles).toContain("44px"); expect(styles).toContain("prefers-color-scheme:dark"); });