|
| 1 | +// fallow-ignore-file code-duplication |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { HF_COLOR_GRADING_ATTR, serializeHfColorGrading } from "../colorGrading"; |
| 4 | +import type { RuntimeTimelineLike } from "./types"; |
| 5 | + |
| 6 | +function pausedTimeline(duration: number): RuntimeTimelineLike { |
| 7 | + let time = 0; |
| 8 | + return { |
| 9 | + play: () => {}, |
| 10 | + pause: () => {}, |
| 11 | + seek: (t?: number) => (t === undefined ? time : (time = t)), |
| 12 | + totalTime: (t?: number) => (t === undefined ? time : (time = t)), |
| 13 | + time: () => time, |
| 14 | + duration: () => duration, |
| 15 | + add: () => {}, |
| 16 | + paused: () => true, |
| 17 | + timeScale: () => {}, |
| 18 | + set: () => {}, |
| 19 | + getChildren: () => [], |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +function timed<K extends keyof HTMLElementTagNameMap>( |
| 24 | + parent: Element, |
| 25 | + tag: K, |
| 26 | + start: string, |
| 27 | +): HTMLElementTagNameMap[K] { |
| 28 | + const el = document.createElement(tag); |
| 29 | + el.className = "clip"; |
| 30 | + el.setAttribute("data-start", start); |
| 31 | + el.setAttribute("data-duration", "2"); |
| 32 | + el.setAttribute("data-track-index", "1"); |
| 33 | + parent.appendChild(el); |
| 34 | + return el; |
| 35 | +} |
| 36 | + |
| 37 | +function mountRoot(): HTMLElement { |
| 38 | + const root = document.createElement("div"); |
| 39 | + root.setAttribute("data-composition-id", "main"); |
| 40 | + root.setAttribute("data-root", "true"); |
| 41 | + root.setAttribute("data-start", "0"); |
| 42 | + root.setAttribute("data-width", "1920"); |
| 43 | + root.setAttribute("data-height", "1080"); |
| 44 | + document.body.appendChild(root); |
| 45 | + window.__timelines = { main: pausedTimeline(10) }; |
| 46 | + return root; |
| 47 | +} |
| 48 | + |
| 49 | +async function evaluateRuntime(): Promise<void> { |
| 50 | + vi.resetModules(); |
| 51 | + await import("./entry"); |
| 52 | +} |
| 53 | + |
| 54 | +const visibility = (...els: HTMLElement[]) => els.map((el) => getComputedStyle(el).visibility); |
| 55 | + |
| 56 | +describe("runtime entry", () => { |
| 57 | + afterEach(() => { |
| 58 | + window.__hfRuntimeTeardown?.(); |
| 59 | + document.head.innerHTML = ""; |
| 60 | + document.body.innerHTML = ""; |
| 61 | + window.__timelines = {}; |
| 62 | + delete window.__player; |
| 63 | + delete window.__playerReady; |
| 64 | + delete window.__renderReady; |
| 65 | + delete window.__hfTimelinesBuilding; |
| 66 | + const win = window as { |
| 67 | + __hyperframeRuntimeBootstrapped?: boolean; |
| 68 | + __hfFirstPassHidden?: boolean; |
| 69 | + }; |
| 70 | + delete win.__hyperframeRuntimeBootstrapped; |
| 71 | + delete win.__hfFirstPassHidden; |
| 72 | + delete (document as { readyState?: unknown }).readyState; |
| 73 | + }); |
| 74 | + |
| 75 | + it("paints no timed clip, from script evaluation until the first visibility pass decides it", async () => { |
| 76 | + const root = mountRoot(); |
| 77 | + const current = timed(root, "div", "0"); |
| 78 | + const later = timed(root, "div", "5"); |
| 79 | + const poster = timed(root, "img", "0"); |
| 80 | + // A composition script may write visibility inline before the runtime runs. |
| 81 | + later.style.visibility = "visible"; |
| 82 | + // Readiness, which runs the first pass, waits while GSAP batches timelines. |
| 83 | + window.__hfTimelinesBuilding = true; |
| 84 | + Object.defineProperty(document, "readyState", { configurable: true, get: () => "loading" }); |
| 85 | + |
| 86 | + await evaluateRuntime(); |
| 87 | + expect(window.__player).toBeUndefined(); |
| 88 | + expect(visibility(current, later)).toEqual(["hidden", "hidden"]); |
| 89 | + |
| 90 | + delete (document as { readyState?: unknown }).readyState; |
| 91 | + document.dispatchEvent(new Event("DOMContentLoaded")); |
| 92 | + expect(window.__renderReady).toBe(false); |
| 93 | + expect(visibility(current, later, poster)).toEqual(["hidden", "hidden", "visible"]); |
| 94 | + |
| 95 | + window.__hfTimelinesBuilding = false; |
| 96 | + window.dispatchEvent(new CustomEvent("hf-timelines-built")); |
| 97 | + expect(window.__renderReady).toBe(true); |
| 98 | + expect(visibility(current, later, poster)).toEqual(["visible", "hidden", "visible"]); |
| 99 | + }); |
| 100 | + |
| 101 | + it("leaves nothing hidden when the runtime is evaluated a second time", async () => { |
| 102 | + const root = mountRoot(); |
| 103 | + const current = timed(root, "div", "0"); |
| 104 | + |
| 105 | + await evaluateRuntime(); |
| 106 | + await evaluateRuntime(); |
| 107 | + // Paused and never sought: no later pass would lift a rule the second copy added. |
| 108 | + expect(visibility(root, current)).toEqual(["visible", "visible"]); |
| 109 | + expect(document.querySelectorAll("style[data-hf-first-pass-hide]")).toHaveLength(0); |
| 110 | + }); |
| 111 | + |
| 112 | + it("grades media inside a clip once the first pass shows the clip, with no seek", async () => { |
| 113 | + const getContext = vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue(null); |
| 114 | + const scene = timed(mountRoot(), "div", "0"); |
| 115 | + // Untimed: the video inherits the scene's window, so no pass writes its visibility. |
| 116 | + const video = document.createElement("video"); |
| 117 | + video.setAttribute( |
| 118 | + HF_COLOR_GRADING_ATTR, |
| 119 | + serializeHfColorGrading({ adjust: { exposure: 0.5 } }), |
| 120 | + ); |
| 121 | + Object.defineProperty(video, "readyState", { value: HTMLMediaElement.HAVE_CURRENT_DATA }); |
| 122 | + Object.defineProperty(video, "videoWidth", { value: 640 }); |
| 123 | + Object.defineProperty(video, "videoHeight", { value: 360 }); |
| 124 | + scene.appendChild(video); |
| 125 | + |
| 126 | + await evaluateRuntime(); |
| 127 | + |
| 128 | + expect(window.__renderReady).toBe(true); |
| 129 | + expect(getContext.mock.calls.some(([type]) => String(type).startsWith("webgl"))).toBe(true); |
| 130 | + getContext.mockRestore(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments