|
| 1 | +import { JSDOM } from "jsdom"; |
| 2 | +import { expect, it, vi } from "vitest"; |
| 3 | +import { readFileSync } from "node:fs"; |
| 4 | +import { resolve } from "node:path"; |
| 5 | +const demoHtml = readFileSync( |
| 6 | + resolve(__dirname, "../../../../registry/examples/airbnb-deck/demo.html"), |
| 7 | + "utf8", |
| 8 | +); |
| 9 | + |
| 10 | +it("accepts only current-player sound names while preserving unlock and mute behavior", async () => { |
| 11 | + const clips: Array<{ |
| 12 | + play: ReturnType<typeof vi.fn>; |
| 13 | + pause: ReturnType<typeof vi.fn>; |
| 14 | + volume: number; |
| 15 | + currentTime: number; |
| 16 | + }> = []; |
| 17 | + const dom = new JSDOM(demoHtml, { |
| 18 | + runScripts: "dangerously", |
| 19 | + beforeParse(window) { |
| 20 | + Object.defineProperty(window, "Audio", { |
| 21 | + value: function () { |
| 22 | + const clip = { |
| 23 | + play: vi.fn(() => Promise.resolve()), |
| 24 | + pause: vi.fn(), |
| 25 | + volume: 1, |
| 26 | + currentTime: 0, |
| 27 | + }; |
| 28 | + clips.push(clip); |
| 29 | + return clip; |
| 30 | + }, |
| 31 | + }); |
| 32 | + }, |
| 33 | + }); |
| 34 | + try { |
| 35 | + const w = dom.window; |
| 36 | + const player = w.document.querySelector("hyperframes-player")!; |
| 37 | + const frame = w.document.createElement("iframe"); |
| 38 | + player.append(frame); |
| 39 | + Object.defineProperty(player, "iframeElement", { get: () => player.querySelector("iframe") }); |
| 40 | + const original = frame.contentWindow; |
| 41 | + const send = (source: MessageEventSource | null, name: unknown) => |
| 42 | + w.dispatchEvent(new w.MessageEvent("message", { source, data: { type: "hf-sfx", name } })); |
| 43 | + send(original, "advance"); |
| 44 | + expect(clips.every((clip) => clip.play.mock.calls.length === 0)).toBe(true); |
| 45 | + w.dispatchEvent(new w.Event("pointerdown")); |
| 46 | + await Promise.resolve(); |
| 47 | + clips.forEach((clip) => clip.play.mockClear()); |
| 48 | + for (const name of ["advance", "fragment", "branch-enter", "back"]) send(original, name); |
| 49 | + expect(clips.map((clip) => clip.play.mock.calls.length)).toEqual([1, 1, 1, 1]); |
| 50 | + for (const name of ["__proto__", "constructor", "unknown", ["advance"]]) send(original, name); |
| 51 | + expect(Object.hasOwn(w.Object.prototype, "currentTime")).toBe(false); |
| 52 | + send(null, "advance"); |
| 53 | + const foreign = w.document.createElement("iframe"); |
| 54 | + w.document.body.append(foreign); |
| 55 | + send(foreign.contentWindow, "advance"); |
| 56 | + expect(clips.map((clip) => clip.play.mock.calls.length)).toEqual([1, 1, 1, 1]); |
| 57 | + const replacement = w.document.createElement("iframe"); |
| 58 | + frame.replaceWith(replacement); |
| 59 | + send(original, "advance"); |
| 60 | + expect(clips[0]!.play).toHaveBeenCalledTimes(1); |
| 61 | + send(replacement.contentWindow, "advance"); |
| 62 | + expect(clips[0]!.play).toHaveBeenCalledTimes(2); |
| 63 | + const slideshow = w.document.querySelector("hyperframes-slideshow")!; |
| 64 | + slideshow.dispatchEvent(new w.CustomEvent("hf-sound", { detail: { muted: true } })); |
| 65 | + send(replacement.contentWindow, "advance"); |
| 66 | + expect(clips[0]!.play).toHaveBeenCalledTimes(2); |
| 67 | + slideshow.dispatchEvent(new w.CustomEvent("hf-sound", { detail: { muted: false } })); |
| 68 | + send(replacement.contentWindow, "advance"); |
| 69 | + expect(clips[0]!.play).toHaveBeenCalledTimes(3); |
| 70 | + expect(clips.map((clip) => clip.volume)).toEqual([0.45, 0.4, 0.4, 0.4]); |
| 71 | + } finally { |
| 72 | + dom.window.close(); |
| 73 | + } |
| 74 | +}); |
0 commit comments