|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import React, { useEffect } from "react"; |
| 4 | +import { act } from "react"; |
| 5 | +import { createRoot } from "react-dom/client"; |
| 6 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 7 | +import { NLEProvider, useNLEContext, type NLEContextValue } from "../../components/nle/NLEContext"; |
| 8 | +import { liveTime, usePlayerStore } from "../store/playerStore"; |
| 9 | +import { makeAdapterWindow, makeFakeIframe } from "./timelinePlayerTestHarness"; |
| 10 | +import { usePlayerHandle, type PlayerHandle } from "./usePlayerHandle"; |
| 11 | + |
| 12 | +Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true }); |
| 13 | + |
| 14 | +function renderHandle() { |
| 15 | + let latest: PlayerHandle | null = null; |
| 16 | + let context: NLEContextValue | null = null; |
| 17 | + let renders = 0; |
| 18 | + const host = document.createElement("div"); |
| 19 | + document.body.append(host); |
| 20 | + const root = createRoot(host); |
| 21 | + act(() => { |
| 22 | + root.render( |
| 23 | + <NLEProvider projectId="player-handle-test"> |
| 24 | + <HandleHarness |
| 25 | + onContext={(value) => (context = value)} |
| 26 | + onValue={(value) => (latest = value)} |
| 27 | + onRender={() => renders++} |
| 28 | + /> |
| 29 | + </NLEProvider>, |
| 30 | + ); |
| 31 | + }); |
| 32 | + if (!latest || !context) throw new Error("usePlayerHandle did not mount"); |
| 33 | + return { |
| 34 | + root, |
| 35 | + getHandle: () => latest as PlayerHandle, |
| 36 | + getContext: () => context as NLEContextValue, |
| 37 | + getRenderCount: () => renders, |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +function HandleHarness({ |
| 42 | + onContext, |
| 43 | + onValue, |
| 44 | + onRender, |
| 45 | +}: { |
| 46 | + onContext: (value: NLEContextValue) => void; |
| 47 | + onValue: (value: PlayerHandle) => void; |
| 48 | + onRender: () => void; |
| 49 | +}) { |
| 50 | + onRender(); |
| 51 | + const context = useNLEContext(); |
| 52 | + const handle = usePlayerHandle(); |
| 53 | + useEffect(() => { |
| 54 | + onContext(context); |
| 55 | + onValue(handle); |
| 56 | + }, [context, handle, onContext, onValue]); |
| 57 | + return null; |
| 58 | +} |
| 59 | + |
| 60 | +afterEach(() => { |
| 61 | + usePlayerStore.getState().reset(); |
| 62 | + document.body.innerHTML = ""; |
| 63 | +}); |
| 64 | + |
| 65 | +describe("usePlayerHandle", () => { |
| 66 | + it("refuses seek before readiness and notifies structural changes independently", () => { |
| 67 | + const { root, getHandle } = renderHandle(); |
| 68 | + const handle = getHandle(); |
| 69 | + |
| 70 | + expect(handle.ready).toBe(false); |
| 71 | + expect(handle.seek(4)).toBe(false); |
| 72 | + |
| 73 | + const structuralListener = vi.fn(); |
| 74 | + const unsubscribe = handle.subscribe(structuralListener); |
| 75 | + act(() => usePlayerStore.setState({ timelineReady: true })); |
| 76 | + expect(structuralListener).toHaveBeenCalledTimes(1); |
| 77 | + act(() => usePlayerStore.setState({ duration: 8 })); |
| 78 | + expect(structuralListener).toHaveBeenCalledTimes(2); |
| 79 | + act(() => |
| 80 | + usePlayerStore.setState({ |
| 81 | + elements: [ |
| 82 | + { |
| 83 | + id: "clip", |
| 84 | + tag: "div", |
| 85 | + start: 2, |
| 86 | + duration: 3, |
| 87 | + track: 0, |
| 88 | + selector: "#clip", |
| 89 | + sourceFile: "index.html", |
| 90 | + compositionSrc: "compositions/intro.html", |
| 91 | + }, |
| 92 | + ], |
| 93 | + }), |
| 94 | + ); |
| 95 | + expect(structuralListener).toHaveBeenCalledTimes(3); |
| 96 | + expect(getHandle().elements).toEqual([ |
| 97 | + { |
| 98 | + id: "clip", |
| 99 | + start: 2, |
| 100 | + end: 5, |
| 101 | + selector: "#clip", |
| 102 | + sourceFile: "index.html", |
| 103 | + compositionSrc: "compositions/intro.html", |
| 104 | + }, |
| 105 | + ]); |
| 106 | + |
| 107 | + unsubscribe(); |
| 108 | + act(() => root.unmount()); |
| 109 | + }); |
| 110 | + |
| 111 | + it("uses the shell player to seek a real preview adapter after readiness", async () => { |
| 112 | + const { root, getContext, getHandle } = renderHandle(); |
| 113 | + const { adapter, win } = makeAdapterWindow(); |
| 114 | + await act(async () => { |
| 115 | + getContext().iframeRef.current = makeFakeIframe(win); |
| 116 | + getContext().onIframeLoad(); |
| 117 | + await Promise.resolve(); |
| 118 | + }); |
| 119 | + |
| 120 | + expect(usePlayerStore.getState().timelineReady).toBe(true); |
| 121 | + expect(getHandle().seek(4)).toBe(true); |
| 122 | + expect(adapter.getTime()).toBe(4); |
| 123 | + expect(usePlayerStore.getState().currentTime).toBe(4); |
| 124 | + act(() => getHandle().play()); |
| 125 | + expect(getHandle().playing).toBe(true); |
| 126 | + |
| 127 | + act(() => root.unmount()); |
| 128 | + }); |
| 129 | + |
| 130 | + it("keeps playback time out of React renders and forwards live time notifications", () => { |
| 131 | + const { root, getHandle, getRenderCount } = renderHandle(); |
| 132 | + const initialRenderCount = getRenderCount(); |
| 133 | + const timeListener = vi.fn(); |
| 134 | + const structuralListener = vi.fn(); |
| 135 | + const unsubscribeTime = getHandle().subscribeTime(timeListener); |
| 136 | + const unsubscribe = getHandle().subscribe(structuralListener); |
| 137 | + |
| 138 | + act(() => { |
| 139 | + usePlayerStore.setState({ currentTime: 2.5 }); |
| 140 | + liveTime.notify(7.25); |
| 141 | + }); |
| 142 | + |
| 143 | + expect(getRenderCount()).toBe(initialRenderCount); |
| 144 | + expect(getHandle().currentTime).toBe(2.5); |
| 145 | + expect(timeListener).toHaveBeenCalledWith(7.25); |
| 146 | + expect(structuralListener).not.toHaveBeenCalled(); |
| 147 | + |
| 148 | + unsubscribeTime(); |
| 149 | + unsubscribe(); |
| 150 | + act(() => root.unmount()); |
| 151 | + }); |
| 152 | +}); |
0 commit comments