|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import { act } from "react"; |
| 4 | +import { afterEach, expect, it, vi } from "vitest"; |
| 5 | +import { |
| 6 | + makeAdapterWindow, |
| 7 | + makeFakeIframe, |
| 8 | + renderTimelinePlayerHarness, |
| 9 | + resetPlayerStore, |
| 10 | +} from "./timelinePlayerTestHarness"; |
| 11 | +import { usePlayerStore } from "../store/playerStore"; |
| 12 | +import { scrubMusicAtSeek } from "../lib/playbackScrub"; |
| 13 | + |
| 14 | +vi.mock("../lib/playbackScrub", async (importOriginal) => ({ |
| 15 | + ...(await importOriginal<typeof import("../lib/playbackScrub")>()), |
| 16 | + scrubMusicAtSeek: vi.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +globalThis.IS_REACT_ACT_ENVIRONMENT = true; |
| 20 | + |
| 21 | +afterEach(() => { |
| 22 | + document.body.innerHTML = ""; |
| 23 | + resetPlayerStore(); |
| 24 | + usePlayerStore.setState({ playLocked: false, loopEnabled: false, playbackRate: 1 }); |
| 25 | + vi.mocked(scrubMusicAtSeek).mockClear(); |
| 26 | +}); |
| 27 | + |
| 28 | +function readyPlayer() { |
| 29 | + const { api, root } = renderTimelinePlayerHarness(); |
| 30 | + const { adapter, win } = makeAdapterWindow(); |
| 31 | + act(() => { |
| 32 | + api.iframeRef.current = makeFakeIframe(win); |
| 33 | + api.onIframeLoad(); |
| 34 | + usePlayerStore.setState({ timelineReady: true }); |
| 35 | + }); |
| 36 | + return { api, root, adapter }; |
| 37 | +} |
| 38 | + |
| 39 | +const press = (key: string) => |
| 40 | + act(() => { |
| 41 | + window.dispatchEvent(new KeyboardEvent("keydown", { key, code: `Key${key.toUpperCase()}` })); |
| 42 | + }); |
| 43 | + |
| 44 | +it("starts nothing while the host holds playback: Play, a playback request or play backward", () => { |
| 45 | + const { api, root, adapter } = readyPlayer(); |
| 46 | + act(() => usePlayerStore.getState().setPlayLocked(true)); |
| 47 | + act(() => api.play()); |
| 48 | + act(() => usePlayerStore.getState().requestPlayback(true)); |
| 49 | + press("j"); |
| 50 | + expect(adapter.play).not.toHaveBeenCalled(); |
| 51 | + expect(usePlayerStore.getState().isPlaying).toBe(false); |
| 52 | + act(() => root.unmount()); |
| 53 | +}); |
| 54 | + |
| 55 | +it("J and L keep the chosen speed while the host holds playback", () => { |
| 56 | + const { root } = readyPlayer(); |
| 57 | + act(() => usePlayerStore.getState().setPlaybackRate(0.5)); |
| 58 | + act(() => usePlayerStore.getState().setPlayLocked(true)); |
| 59 | + press("l"); |
| 60 | + press("j"); |
| 61 | + expect(usePlayerStore.getState().playbackRate).toBe(0.5); |
| 62 | + act(() => root.unmount()); |
| 63 | +}); |
| 64 | + |
| 65 | +it("a looping film reaching its end while held stops instead of starting again", () => { |
| 66 | + const frames: FrameRequestCallback[] = []; |
| 67 | + const raf = vi.spyOn(window, "requestAnimationFrame").mockImplementation((tick) => { |
| 68 | + frames.push(tick); |
| 69 | + return frames.length; |
| 70 | + }); |
| 71 | + const { api, root, adapter } = readyPlayer(); |
| 72 | + act(() => usePlayerStore.setState({ loopEnabled: true })); |
| 73 | + act(() => api.play()); |
| 74 | + // Held with the store already reading stopped, so nothing pauses the frame loop first. |
| 75 | + act(() => usePlayerStore.setState({ playLocked: true, isPlaying: false })); |
| 76 | + adapter.seek(30); |
| 77 | + act(() => frames.at(-1)!(0)); |
| 78 | + expect(adapter.play).toHaveBeenCalledTimes(1); |
| 79 | + expect(usePlayerStore.getState().isPlaying).toBe(false); |
| 80 | + raf.mockRestore(); |
| 81 | + act(() => root.unmount()); |
| 82 | +}); |
| 83 | + |
| 84 | +it("turning the hold on before the preview has a player still clears playing", () => { |
| 85 | + const { root } = renderTimelinePlayerHarness(); |
| 86 | + act(() => usePlayerStore.setState({ isPlaying: true })); |
| 87 | + act(() => usePlayerStore.getState().setPlayLocked(true)); |
| 88 | + expect(usePlayerStore.getState().isPlaying).toBe(false); |
| 89 | + act(() => root.unmount()); |
| 90 | +}); |
| 91 | + |
| 92 | +it("a seek that would keep playing does not resume while the host holds playback", () => { |
| 93 | + const { api, root, adapter } = readyPlayer(); |
| 94 | + act(() => usePlayerStore.getState().setPlayLocked(true)); |
| 95 | + // The store still reads playing (a host that set the hold before the player caught up): only the hold stops it. |
| 96 | + act(() => usePlayerStore.setState({ isPlaying: true })); |
| 97 | + act(() => { |
| 98 | + api.seek(5, { keepPlaying: true }); |
| 99 | + }); |
| 100 | + expect(adapter.play).not.toHaveBeenCalled(); |
| 101 | + act(() => root.unmount()); |
| 102 | +}); |
| 103 | + |
| 104 | +it("a seek while the host holds playback moves the picture without scrub audio", () => { |
| 105 | + const { api, root, adapter } = readyPlayer(); |
| 106 | + act(() => { |
| 107 | + api.seek(2); |
| 108 | + }); |
| 109 | + expect(scrubMusicAtSeek).toHaveBeenCalledTimes(1); |
| 110 | + act(() => usePlayerStore.getState().setPlayLocked(true)); |
| 111 | + act(() => { |
| 112 | + api.seek(4); |
| 113 | + }); |
| 114 | + expect(adapter.getTime()).toBe(4); |
| 115 | + expect(scrubMusicAtSeek).toHaveBeenCalledTimes(1); |
| 116 | + act(() => root.unmount()); |
| 117 | +}); |
| 118 | + |
| 119 | +it("a timeline reset keeps the hold: the host owns it", () => { |
| 120 | + act(() => usePlayerStore.getState().setPlayLocked(true)); |
| 121 | + act(() => usePlayerStore.getState().reset()); |
| 122 | + expect(usePlayerStore.getState().playLocked).toBe(true); |
| 123 | +}); |
| 124 | + |
| 125 | +it("turning the hold on pauses a playing film; turning it off lets Play start again", () => { |
| 126 | + const { api, root, adapter } = readyPlayer(); |
| 127 | + act(() => api.play()); |
| 128 | + expect(usePlayerStore.getState().isPlaying).toBe(true); |
| 129 | + act(() => usePlayerStore.getState().setPlayLocked(true)); |
| 130 | + expect(adapter.pause).toHaveBeenCalled(); |
| 131 | + expect(usePlayerStore.getState().isPlaying).toBe(false); |
| 132 | + act(() => usePlayerStore.getState().setPlayLocked(false)); |
| 133 | + act(() => api.play()); |
| 134 | + expect(adapter.play).toHaveBeenCalledTimes(2); |
| 135 | + expect(usePlayerStore.getState().isPlaying).toBe(true); |
| 136 | + act(() => root.unmount()); |
| 137 | +}); |
0 commit comments