|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import { act, createElement, isValidElement, type ReactNode } from "react"; |
| 4 | +import { createRoot } from "react-dom/client"; |
| 5 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import { collectRuntimeTimelinePayload } from "../../../../core/src/runtime/timeline"; |
| 7 | +import { PARITY_MODES as MODES, type ParityMode } from "../../../tests/parityModes"; |
| 8 | +import { useRenderClipContent } from "../../hooks/useRenderClipContent"; |
| 9 | +import { sealedPreviewFrame } from "../lib/sealedPreviewFrame.test-helpers"; |
| 10 | +import { usePlayerStore, type TimelineElement } from "../store/playerStore"; |
| 11 | +import { useTimelineSyncCallbacks } from "./useTimelineSyncCallbacks"; |
| 12 | + |
| 13 | +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 14 | + |
| 15 | +const BASE = `${window.location.origin}/api/projects/demo/preview/`; |
| 16 | +const FILM = ` |
| 17 | + <div data-composition-id="main" data-composition-file="index.html" data-width="1920" data-height="1080" data-duration="10"> |
| 18 | + <video id="intro" class="clip" src="media/intro.mp4" muted data-start="0" data-duration="2" data-track-index="0"></video> |
| 19 | + <img id="logo" class="clip" src="media/logo.png" data-start="2" data-duration="2" data-track-index="1"> |
| 20 | + <div id="scene" data-composition-id="scene" data-composition-src="compositions/scene.html" data-start="4" data-duration="2" data-track-index="2"></div> |
| 21 | + <div id="broll" data-composition-id="broll" data-start="6" data-duration="2" data-track-index="3"><video src="media/broll.mp4" muted></video></div> |
| 22 | + <div class="card clip" data-start="8" data-duration="1" data-track-index="4"></div> |
| 23 | + <div class="card clip" data-start="9" data-duration="1" data-track-index="5"></div> |
| 24 | + </div>`; |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + document.head.innerHTML = ""; |
| 28 | + document.body.innerHTML = ""; |
| 29 | + usePlayerStore.getState().reset(); |
| 30 | +}); |
| 31 | + |
| 32 | +/** FILM in `page`, as the preview serves it. */ |
| 33 | +function serve(page: Document) { |
| 34 | + page.head.innerHTML = `<base href="${BASE}">`; |
| 35 | + page.body.innerHTML = FILM; |
| 36 | +} |
| 37 | + |
| 38 | +/** The runtime's clip manifest for FILM, and a preview of it Studio reads directly or a sealed one. */ |
| 39 | +function previewIn(mode: ParityMode) { |
| 40 | + serve(document); |
| 41 | + const manifest = collectRuntimeTimelinePayload({ canonicalFps: 30 }); |
| 42 | + if (mode === "sealed") return { manifest, iframe: sealedPreviewFrame().iframe }; |
| 43 | + document.head.innerHTML = ""; |
| 44 | + document.body.innerHTML = ""; |
| 45 | + const iframe = document.createElement("iframe"); |
| 46 | + document.body.append(iframe); |
| 47 | + serve(iframe.contentDocument!); |
| 48 | + return { manifest, iframe }; |
| 49 | +} |
| 50 | + |
| 51 | +async function timelineOf(mode: ParityMode): Promise<TimelineElement[]> { |
| 52 | + const { manifest, iframe } = previewIn(mode); |
| 53 | + const sync = (els: TimelineElement[]) => usePlayerStore.getState().setElements(els); |
| 54 | + let callbacks: ReturnType<typeof useTimelineSyncCallbacks> | undefined; |
| 55 | + function Harness() { |
| 56 | + callbacks = useTimelineSyncCallbacks({ |
| 57 | + iframeRef: { current: iframe }, |
| 58 | + probeIntervalRef: { current: undefined }, |
| 59 | + pendingSeekRef: { current: null }, |
| 60 | + isRefreshingRef: { current: false }, |
| 61 | + getAdapter: () => null, |
| 62 | + syncTimelineElements: sync, |
| 63 | + setDuration: () => {}, |
| 64 | + setCurrentTime: () => {}, |
| 65 | + requestTimelineReady: () => {}, |
| 66 | + setIsPlaying: () => {}, |
| 67 | + attachIframeShortcutListeners: () => {}, |
| 68 | + applyPreviewAudioState: () => {}, |
| 69 | + onAdapterReady: (_iframe, _context, commit) => commit(), |
| 70 | + }); |
| 71 | + return null; |
| 72 | + } |
| 73 | + act(() => createRoot(document.createElement("div")).render(createElement(Harness))); |
| 74 | + callbacks!.processTimelineMessage(manifest); |
| 75 | + await vi.waitFor(() => expect(usePlayerStore.getState().elements).toHaveLength(6)); |
| 76 | + return usePlayerStore.getState().elements; |
| 77 | +} |
| 78 | + |
| 79 | +/** What a timeline clip's thumbnail fetches. */ |
| 80 | +function thumbnailOf(node: ReactNode): unknown { |
| 81 | + if (!isValidElement(node)) return null; |
| 82 | + const props = node.props as Record<string, unknown>; |
| 83 | + if ("thumbnail" in props) return thumbnailOf(props.thumbnail as ReactNode); |
| 84 | + if ("children" in props && !("previewUrl" in props)) |
| 85 | + return thumbnailOf(props.children as ReactNode); |
| 86 | + const { videoSrc, imageSrc, previewUrl, selector, selectorIndex, seekTime } = props; |
| 87 | + return { videoSrc, imageSrc, previewUrl, selector, selectorIndex, seekTime }; |
| 88 | +} |
| 89 | + |
| 90 | +function filmstrips(elements: TimelineElement[], activePreviewUrl: string | null) { |
| 91 | + let render: ReturnType<typeof useRenderClipContent> | undefined; |
| 92 | + function Harness() { |
| 93 | + render = useRenderClipContent({ |
| 94 | + projectIdRef: { current: "demo" }, |
| 95 | + compIdToSrc: new Map(), |
| 96 | + activePreviewUrl, |
| 97 | + effectiveTimelineDuration: 10, |
| 98 | + }); |
| 99 | + return null; |
| 100 | + } |
| 101 | + act(() => createRoot(document.createElement("div")).render(createElement(Harness))); |
| 102 | + return elements.map((element) => thumbnailOf(render!(element, { clip: "", label: "" }))); |
| 103 | +} |
| 104 | + |
| 105 | +const PREVIEW = "/api/projects/demo/preview"; |
| 106 | +const SCENE = `${PREVIEW}/comp/compositions/scene.html`; |
| 107 | + |
| 108 | +describe.each(MODES)("timeline thumbnails in a %s preview", (mode) => { |
| 109 | + it("ask each clip for its own frames", async () => { |
| 110 | + expect(filmstrips(await timelineOf(mode), null)).toEqual([ |
| 111 | + { videoSrc: `${PREVIEW}/media/intro.mp4` }, |
| 112 | + { imageSrc: `${PREVIEW}/media/logo.png` }, |
| 113 | + { previewUrl: SCENE, seekTime: 1 }, |
| 114 | + { videoSrc: `${PREVIEW}/media/broll.mp4` }, |
| 115 | + { previewUrl: PREVIEW, selector: ".card", selectorIndex: 0, seekTime: 8 }, |
| 116 | + { previewUrl: PREVIEW, selector: ".card", selectorIndex: 1, seekTime: 9 }, |
| 117 | + ]); |
| 118 | + }); |
| 119 | + |
| 120 | + it("ask a clip for its frames in the scene that is open", async () => { |
| 121 | + const strips = filmstrips(await timelineOf(mode), SCENE); |
| 122 | + |
| 123 | + expect(strips.slice(4)).toEqual([ |
| 124 | + { previewUrl: SCENE, selector: ".card", selectorIndex: 0, seekTime: 8 }, |
| 125 | + { previewUrl: SCENE, selector: ".card", selectorIndex: 1, seekTime: 9 }, |
| 126 | + ]); |
| 127 | + }); |
| 128 | +}); |
0 commit comments