|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import { act } from "react"; |
| 3 | +import { createRoot } from "react-dom/client"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { usePlayerStore } from "../player/store/playerStore"; |
| 6 | +import { |
| 7 | + createTimelinePerformanceFixture, |
| 8 | + type TimelinePerformanceFixtureProfile, |
| 9 | +} from "../player/lib/timelinePerformanceFixture"; |
| 10 | +import { useStudioTestHooks } from "./useStudioTestHooks"; |
| 11 | + |
| 12 | +Reflect.set(globalThis, "IS_REACT_ACT_ENVIRONMENT", true); |
| 13 | + |
| 14 | +const PROFILES: readonly TimelinePerformanceFixtureProfile[] = [ |
| 15 | + "dense-short", |
| 16 | + "long-overlap", |
| 17 | + "keyframe-heavy-expanded", |
| 18 | + "composition-heavy", |
| 19 | + "remote-unsupported", |
| 20 | +]; |
| 21 | + |
| 22 | +function Probe(): null { |
| 23 | + useStudioTestHooks({ |
| 24 | + previewIframeRef: { current: null }, |
| 25 | + buildDomSelectionFromTarget: vi.fn(), |
| 26 | + applyDomSelection: vi.fn(), |
| 27 | + }); |
| 28 | + return null; |
| 29 | +} |
| 30 | + |
| 31 | +describe("timeline performance fixture", () => { |
| 32 | + afterEach(() => { |
| 33 | + window.__studioTest = undefined; |
| 34 | + usePlayerStore.getState().reset(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("generates the expected dense-short 50k distribution", () => { |
| 38 | + const first = createTimelinePerformanceFixture({ |
| 39 | + elementCount: 50_000, |
| 40 | + profile: "dense-short", |
| 41 | + }); |
| 42 | + |
| 43 | + expect(first.summary).toEqual({ |
| 44 | + elementCount: 50_000, |
| 45 | + profile: "dense-short", |
| 46 | + duration: 120, |
| 47 | + trackCount: 1_000, |
| 48 | + keyframedElementCount: 0, |
| 49 | + expandedElementCount: 0, |
| 50 | + }); |
| 51 | + expect(new Set(first.elements.map((element) => element.track)).size).toBe(1_000); |
| 52 | + const perTrack = new Map<number, number>(); |
| 53 | + for (const element of first.elements) { |
| 54 | + perTrack.set(element.track, (perTrack.get(element.track) ?? 0) + 1); |
| 55 | + } |
| 56 | + expect(Math.max(...perTrack.values())).toBeLessThanOrEqual(128); |
| 57 | + }); |
| 58 | + |
| 59 | + it.each(PROFILES)("generates an identical 50k %s fixture", (profile) => { |
| 60 | + const first = createTimelinePerformanceFixture({ elementCount: 50_000, profile }); |
| 61 | + const second = createTimelinePerformanceFixture({ elementCount: 50_000, profile }); |
| 62 | + expect(second).toEqual(first); |
| 63 | + }); |
| 64 | + |
| 65 | + it.each(PROFILES)("builds the %s 1k scale profile", (profile) => { |
| 66 | + const fixture = createTimelinePerformanceFixture({ elementCount: 1_000, profile }); |
| 67 | + expect(fixture.elements).toHaveLength(1_000); |
| 68 | + expect(fixture.summary.elementCount).toBe(1_000); |
| 69 | + expect(fixture.summary.duration).toBeGreaterThan(0); |
| 70 | + expect(new Set(fixture.elements.map((element) => element.key)).size).toBe(1_000); |
| 71 | + if (profile === "keyframe-heavy-expanded") { |
| 72 | + expect(fixture.keyframeCache.size).toBe(1_000); |
| 73 | + expect(fixture.gsapAnimations.size).toBe(1_000); |
| 74 | + expect(fixture.expandedClipIds.size).toBe(1_000); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + it("publishes one dev-only loader that replaces fixture state atomically", () => { |
| 79 | + const host = document.createElement("div"); |
| 80 | + const root = createRoot(host); |
| 81 | + act(() => root.render(<Probe />)); |
| 82 | + const api = window.__studioTest; |
| 83 | + expect(api).toBeDefined(); |
| 84 | + if (!api) throw new Error("Expected dev Studio test API"); |
| 85 | + let notifications = 0; |
| 86 | + usePlayerStore.setState({ |
| 87 | + isPlaying: true, |
| 88 | + requestedSeekTime: 42, |
| 89 | + clipRevealRequest: { elementId: "stale", nonce: 7 }, |
| 90 | + clipManifest: [], |
| 91 | + lintFindingsByElement: new Map([["stale", { count: 1, messages: ["stale"] }]]), |
| 92 | + }); |
| 93 | + const unsubscribe = usePlayerStore.subscribe(() => { |
| 94 | + notifications += 1; |
| 95 | + }); |
| 96 | + |
| 97 | + const summary = api.loadTimelinePerformanceFixture({ |
| 98 | + elementCount: 1_000, |
| 99 | + profile: "keyframe-heavy-expanded", |
| 100 | + }); |
| 101 | + |
| 102 | + expect(summary.elementCount).toBe(1_000); |
| 103 | + expect(notifications).toBe(1); |
| 104 | + expect(usePlayerStore.getState()).toMatchObject({ |
| 105 | + isPlaying: false, |
| 106 | + requestedSeekTime: null, |
| 107 | + clipRevealRequest: null, |
| 108 | + clipManifest: null, |
| 109 | + duration: 600, |
| 110 | + timelineReady: true, |
| 111 | + }); |
| 112 | + expect(usePlayerStore.getState().lintFindingsByElement.size).toBe(0); |
| 113 | + expect(usePlayerStore.getState().elements).toHaveLength(1_000); |
| 114 | + expect(usePlayerStore.getState().expandedClipIds.size).toBe(1_000); |
| 115 | + unsubscribe(); |
| 116 | + act(() => root.unmount()); |
| 117 | + expect(window.__studioTest).toBeUndefined(); |
| 118 | + }); |
| 119 | + |
| 120 | + it("does not mutate state when the fixture request is invalid", () => { |
| 121 | + const host = document.createElement("div"); |
| 122 | + const root = createRoot(host); |
| 123 | + act(() => root.render(<Probe />)); |
| 124 | + const api = window.__studioTest; |
| 125 | + if (!api) throw new Error("Expected dev Studio test API"); |
| 126 | + const before = usePlayerStore.getState().elements; |
| 127 | + |
| 128 | + expect(() => |
| 129 | + Reflect.apply(api.loadTimelinePerformanceFixture, api, [ |
| 130 | + { elementCount: 999, profile: "dense-short" }, |
| 131 | + ]), |
| 132 | + ).toThrow("elementCount must be 1000 or 50000"); |
| 133 | + expect(usePlayerStore.getState().elements).toBe(before); |
| 134 | + expect(() => |
| 135 | + Reflect.apply(api.loadTimelinePerformanceFixture, api, [ |
| 136 | + { elementCount: 1_000, profile: "constructor" }, |
| 137 | + ]), |
| 138 | + ).toThrow("Unknown timeline performance fixture profile"); |
| 139 | + act(() => root.unmount()); |
| 140 | + }); |
| 141 | +}); |
0 commit comments