|
| 1 | +// @vitest-environment jsdom |
| 2 | +import "@testing-library/jest-dom"; |
| 3 | +import { act, cleanup, fireEvent, render } from "@testing-library/react"; |
| 4 | +import { createElement, useEffect } from "react"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ |
| 8 | + previewPlay: vi.fn(() => Promise.resolve()), |
| 9 | + previewPause: vi.fn(), |
| 10 | + latestSeekTarget: null as { |
| 11 | + timeSec: number; |
| 12 | + isSource?: boolean; |
| 13 | + requestId: number; |
| 14 | + preservePlayback?: boolean; |
| 15 | + } | null, |
| 16 | +})); |
| 17 | + |
| 18 | +vi.mock("@/contexts/ShortcutsContext", async () => { |
| 19 | + const { DEFAULT_SHORTCUTS } = await import("@/lib/shortcuts"); |
| 20 | + return { |
| 21 | + useShortcuts: () => ({ |
| 22 | + shortcuts: DEFAULT_SHORTCUTS, |
| 23 | + isMac: false, |
| 24 | + isConfigOpen: false, |
| 25 | + openConfig: vi.fn(), |
| 26 | + closeConfig: vi.fn(), |
| 27 | + setShortcuts: vi.fn(), |
| 28 | + persistShortcuts: () => Promise.resolve(true), |
| 29 | + }), |
| 30 | + }; |
| 31 | +}); |
| 32 | + |
| 33 | +vi.mock("@/contexts/I18nContext", () => ({ |
| 34 | + useI18n: () => ({ |
| 35 | + locale: "en", |
| 36 | + setLocale: vi.fn(), |
| 37 | + }), |
| 38 | + useScopedT: () => (key: string) => key, |
| 39 | +})); |
| 40 | + |
| 41 | +vi.mock("./Preview", () => ({ |
| 42 | + Preview: ({ |
| 43 | + seekTarget, |
| 44 | + onVideoElement, |
| 45 | + }: { |
| 46 | + seekTarget: { |
| 47 | + timeSec: number; |
| 48 | + isSource?: boolean; |
| 49 | + requestId: number; |
| 50 | + preservePlayback?: boolean; |
| 51 | + } | null; |
| 52 | + onVideoElement: (element: HTMLVideoElement | null) => void; |
| 53 | + }) => { |
| 54 | + mocks.latestSeekTarget = seekTarget; |
| 55 | + useEffect(() => { |
| 56 | + const element = document.createElement("video"); |
| 57 | + let paused = true; |
| 58 | + Object.defineProperty(element, "paused", { |
| 59 | + configurable: true, |
| 60 | + get: () => paused, |
| 61 | + }); |
| 62 | + element.play = vi.fn(() => { |
| 63 | + paused = false; |
| 64 | + element.dispatchEvent(new Event("play")); |
| 65 | + return mocks.previewPlay(); |
| 66 | + }); |
| 67 | + element.pause = vi.fn(() => { |
| 68 | + paused = true; |
| 69 | + element.dispatchEvent(new Event("pause")); |
| 70 | + mocks.previewPause(); |
| 71 | + }); |
| 72 | + onVideoElement(element); |
| 73 | + return () => onVideoElement(null); |
| 74 | + }, [onVideoElement]); |
| 75 | + return createElement("div", { |
| 76 | + "data-testid": "preview-mock", |
| 77 | + "data-seek-target": JSON.stringify(seekTarget), |
| 78 | + }); |
| 79 | + }, |
| 80 | +})); |
| 81 | + |
| 82 | +import { TooltipProvider } from "@/components/ui/tooltip"; |
| 83 | +import { EditorDialogsProvider } from "@/contexts/EditorDialogsContext"; |
| 84 | +import type { AxcutAsset, AxcutClip, AxcutDocument } from "@/lib/ai-edition/schema"; |
| 85 | +import { createEmptyDocument } from "@/lib/ai-edition/schema"; |
| 86 | +import { useProjectStore } from "@/lib/ai-edition/store/projectStore"; |
| 87 | +import { NewEditorShell } from "./NewEditorShell"; |
| 88 | + |
| 89 | +function makeCrossAssetDocument(): AxcutDocument { |
| 90 | + const doc = createEmptyDocument({ projectId: "p1", title: "Cross-Asset Replay" }); |
| 91 | + const asset1: AxcutAsset = { |
| 92 | + id: "a1", |
| 93 | + kind: "video", |
| 94 | + label: "a1", |
| 95 | + originalPath: "/a1.mp4", |
| 96 | + durationSec: 5, |
| 97 | + cameraTrack: null, |
| 98 | + }; |
| 99 | + const asset2: AxcutAsset = { |
| 100 | + id: "a2", |
| 101 | + kind: "video", |
| 102 | + label: "a2", |
| 103 | + originalPath: "/a2.mp4", |
| 104 | + durationSec: 5, |
| 105 | + cameraTrack: null, |
| 106 | + }; |
| 107 | + const clip1: AxcutClip = { |
| 108 | + id: "c1", |
| 109 | + assetId: "a1", |
| 110 | + sourceStartSec: 0, |
| 111 | + sourceEndSec: 5, |
| 112 | + timelineStartSec: 0, |
| 113 | + timelineEndSec: 5, |
| 114 | + wordRefs: [], |
| 115 | + origin: "user", |
| 116 | + reason: "", |
| 117 | + }; |
| 118 | + const clip2: AxcutClip = { |
| 119 | + id: "c2", |
| 120 | + assetId: "a2", |
| 121 | + sourceStartSec: 0, |
| 122 | + sourceEndSec: 5, |
| 123 | + timelineStartSec: 5, |
| 124 | + timelineEndSec: 10, |
| 125 | + wordRefs: [], |
| 126 | + origin: "user", |
| 127 | + reason: "", |
| 128 | + }; |
| 129 | + doc.assets = [asset1, asset2]; |
| 130 | + doc.timeline.clips = [clip1, clip2]; |
| 131 | + return doc; |
| 132 | +} |
| 133 | + |
| 134 | +function renderShell() { |
| 135 | + return render( |
| 136 | + <TooltipProvider> |
| 137 | + <EditorDialogsProvider> |
| 138 | + <NewEditorShell /> |
| 139 | + </EditorDialogsProvider> |
| 140 | + </TooltipProvider>, |
| 141 | + ); |
| 142 | +} |
| 143 | + |
| 144 | +describe("NewEditorShell togglePlay cross-asset replay", () => { |
| 145 | + beforeEach(() => { |
| 146 | + localStorage.clear(); |
| 147 | + mocks.previewPlay.mockClear(); |
| 148 | + mocks.previewPause.mockClear(); |
| 149 | + mocks.latestSeekTarget = null; |
| 150 | + (window as unknown as { electronAPI?: unknown }).electronAPI = { |
| 151 | + onAiEditionChatEvent: () => () => {}, |
| 152 | + setTitleBarOverlay: () => {}, |
| 153 | + setHasUnsavedChanges: () => {}, |
| 154 | + onRequestCloseConfirm: () => () => {}, |
| 155 | + onRequestSaveBeforeClose: () => () => {}, |
| 156 | + sendCloseConfirmResponse: () => {}, |
| 157 | + findRecordingCamera: () => Promise.resolve(null), |
| 158 | + preparePreviewAudioTrack: () => Promise.resolve({ success: false, path: null }), |
| 159 | + readBinaryFile: () => Promise.resolve(new Uint8Array(0)), |
| 160 | + isAppPackaged: () => false, |
| 161 | + }; |
| 162 | + Element.prototype.scrollTo = () => {}; |
| 163 | + (globalThis as unknown as { ResizeObserver?: unknown }).ResizeObserver = class { |
| 164 | + observe() {} |
| 165 | + unobserve() {} |
| 166 | + disconnect() {} |
| 167 | + }; |
| 168 | + }); |
| 169 | + |
| 170 | + afterEach(() => { |
| 171 | + cleanup(); |
| 172 | + (window as unknown as { electronAPI?: unknown }).electronAPI = undefined; |
| 173 | + }); |
| 174 | + |
| 175 | + it("preserves playback intent when replaying across assets through togglePlay", async () => { |
| 176 | + const doc = makeCrossAssetDocument(); |
| 177 | + useProjectStore.setState({ |
| 178 | + document: doc, |
| 179 | + projectId: "p1", |
| 180 | + currentTimeSec: 10, // at the end of the timeline |
| 181 | + playing: false, |
| 182 | + }); |
| 183 | + |
| 184 | + renderShell(); |
| 185 | + |
| 186 | + // Trigger replay via Space shortcut (togglePlay) |
| 187 | + await act(async () => { |
| 188 | + fireEvent.keyDown(document.body, { key: " ", code: "Space" }); |
| 189 | + }); |
| 190 | + |
| 191 | + // 1. togglePlay must have initiated a seek to 0 carrying preservePlayback: true |
| 192 | + expect(mocks.latestSeekTarget).toMatchObject({ |
| 193 | + timeSec: 0, |
| 194 | + preservePlayback: true, |
| 195 | + }); |
| 196 | + |
| 197 | + // 2. togglePlay must have called play() to start playback |
| 198 | + expect(mocks.previewPlay).toHaveBeenCalledTimes(1); |
| 199 | + expect(useProjectStore.getState().currentTimeSec).toBe(0); |
| 200 | + }); |
| 201 | +}); |
0 commit comments