Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
fix(editor): preserve playback intent and isolate cross-source seeks
Bet4b87772477a21
diff --git a/src/components/ai-edition/NewEditorShell.togglePlay.test.tsx b/src/components/ai-edition/NewEditorShell.togglePlay.test.tsxnew file mode 100644index 000000000..3fa5fcb73--- /dev/null+++ b/src/components/ai-edition/NewEditorShell.togglePlay.test.tsx@@ -0,0 +1,222 @@+// @vitest-environment jsdom+import "@testing-library/jest-dom";+import { act, cleanup, fireEvent, render } from "@testing-library/react";+import { createElement, useEffect } from "react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++const mocks = vi.hoisted(() => ({+ previewPlay: vi.fn(() => Promise.resolve()),+ previewPause: vi.fn(),+ latestSeekTarget: null as {+ timeSec: number;+ isSource?: boolean;+ requestId: number;+ preservePlayback?: boolean;+ } | null,+}));++vi.mock("@/contexts/ShortcutsContext", async () => {+ const { DEFAULT_SHORTCUTS } = await import("@/lib/shortcuts");+ return {+ useShortcuts: () => ({+ shortcuts: DEFAULT_SHORTCUTS,+ isMac: false,+ isConfigOpen: false,+ openConfig: vi.fn(),+ closeConfig: vi.fn(),+ setShortcuts: vi.fn(),+ persistShortcuts: () => Promise.resolve(true),+ }),+ };+});++vi.mock("@/contexts/I18nContext", () => ({+ useI18n: () => ({+ locale: "en",+ setLocale: vi.fn(),+ }),+ useScopedT: () => (key: string) => key,+}));++vi.mock("./Preview", () => ({+ Preview: ({+ seekTarget,+ onVideoElement,+ }: {+ seekTarget: {+ timeSec: number;+ isSource?: boolean;+ requestId: number;+ preservePlayback?: boolean;+ } | null;+ onVideoElement: (element: HTMLVideoElement | null) => void;+ }) => {+ mocks.latestSeekTarget = seekTarget;+ useEffect(() => {+ const element = document.createElement("video");+ let paused = true;+ Object.defineProperty(element, "paused", {+ configurable: true,+ get: () => paused,+ });+ element.play = vi.fn(() => {+ paused = false;+ element.dispatchEvent(new Event("play"));+ return mocks.previewPlay();+ });+ element.pause = vi.fn(() => {+ paused = true;+ element.dispatchEvent(new Event("pause"));+ mocks.previewPause();+ });+ onVideoElement(element);+ return () => onVideoElement(null);+ }, [onVideoElement]);+ return createElement("div", {+ "data-testid": "preview-mock",+ "data-seek-target": JSON.stringify(seekTarget),+ });+ },+}));++import { TooltipProvider } from "@/components/ui/tooltip";+import { EditorDialogsProvider } from "@/contexts/EditorDialogsContext";+import type { AxcutAsset, AxcutClip, AxcutDocument } from "@/lib/ai-edition/schema";+import { createEmptyDocument } from "@/lib/ai-edition/schema";+import { useProjectStore } from "@/lib/ai-edition/store/projectStore";+import { NewEditorShell } from "./NewEditorShell";++function makeCrossAssetDocument(): AxcutDocument {+ const doc = createEmptyDocument({ projectId: "p1", title: "Cross-Asset Replay" });+ const asset1: AxcutAsset = {+ id: "a1",+ kind: "video",+ label: "a1",+ originalPath: "/a1.mp4",+ durationSec: 5,+ cameraTrack: null,+ };+ const asset2: AxcutAsset = {+ id: "a2",+ kind: "video",+ label: "a2",+ originalPath: "/a2.mp4",+ durationSec: 5,+ cameraTrack: null,+ };+ const clip1: AxcutClip = {+ id: "c1",+ assetId: "a1",+ sourceStartSec: 0,+ sourceEndSec: 5,+ timelineStartSec: 0,+ timelineEndSec: 5,+ wordRefs: [],+ origin: "user",+ reason: "",+ };+ const clip2: AxcutClip = {+ id: "c2",+ assetId: "a2",+ sourceStartSec: 0,+ sourceEndSec: 5,+ timelineStartSec: 5,+ timelineEndSec: 10,+ wordRefs: [],+ origin: "user",+ reason: "",+ };+ doc.assets = [asset1, asset2];+ doc.timeline.clips = [clip1, clip2];+ return doc;+}++function renderShell() {+ return render(+ <TooltipProvider>+ <EditorDialogsProvider>+ <NewEditorShell />+ </EditorDialogsProvider>+ </TooltipProvider>,+ );+}++describe("NewEditorShell togglePlay cross-asset replay", () => {+ beforeEach(() => {+ localStorage.clear();+ mocks.previewPlay.mockClear();+ mocks.previewPause.mockClear();+ mocks.latestSeekTarget = null;+ (window as unknown as { electronAPI?: unknown }).electronAPI = {+ onAiEditionChatEvent: () => () => {},+ setTitleBarOverlay: () => {},+ setHasUnsavedChanges: () => {},+ onRequestCloseConfirm: () => () => {},+ onRequestSaveBeforeClose: () => () => {},+ sendCloseConfirmResponse: () => {},+ findRecordingCamera: () => Promise.resolve(null),+ preparePreviewAudioTrack: () => Promise.resolve({ success: false, path: null }),+ readBinaryFile: () => Promise.resolve(new Uint8Array(0)),+ isAppPackaged: () => false,+ };+ Element.prototype.scrollTo = () => {};+ (globalThis as unknown as { ResizeObserver?: unknown }).ResizeObserver = class {+ observe() {}+ unobserve() {}+ disconnect() {}+ };+ });++ afterEach(() => {+ cleanup();+ (window as unknown as { electronAPI?: unknown }).electronAPI = undefined;+ });++ it("preserves playback intent when replaying across assets through togglePlay", async () => {+ const doc = makeCrossAssetDocument();+ useProjectStore.setState({+ document: doc,+ projectId: "p1",+ currentTimeSec: 10, // at the end of the timeline+ playing: false,+ });++ renderShell();++ // Trigger replay via Space shortcut (togglePlay)+ await act(async () => {+ fireEvent.keyDown(document.body, { key: " ", code: "Space" });+ });++ // 1. togglePlay must have initiated a seek to 0 carrying preservePlayback: true+ expect(mocks.latestSeekTarget).toMatchObject({+ timeSec: 0,+ preservePlayback: true,+ });++ // 2. togglePlay must have called play() to start playback+ expect(mocks.previewPlay).toHaveBeenCalledTimes(1);+ expect(useProjectStore.getState().currentTimeSec).toBe(0);+ });++ it("does not rewind to 0 when pressing play from an intermediate position after cross-asset seek", async () => {+ const doc = makeCrossAssetDocument();+ useProjectStore.setState({+ document: doc,+ projectId: "p1",+ currentTimeSec: 4, // in clip1 (0-5s), timeline ends at 10s+ playing: false,+ });++ renderShell();++ await act(async () => {+ fireEvent.keyDown(document.body, { key: " ", code: "Space" });+ });++ // Must NOT seek to 0+ expect(mocks.latestSeekTarget).toBeNull();+ expect(mocks.previewPlay).toHaveBeenCalledTimes(1);+ expect(useProjectStore.getState().currentTimeSec).toBe(4);+ });+});diff --git a/src/components/ai-edition/NewEditorShell.tsx b/src/components/ai-edition/NewEditorShell.tsxindex 4e1d0ac37..ea4b9904a 100644--- a/src/components/ai-edition/NewEditorShell.tsx+++ b/src/components/ai-edition/NewEditorShell.tsx@@ -85,6 +85,7 @@ interface SeekTarget {timeSec: number;isSource?: boolean;requestId: number;+ preservePlayback?: boolean;}/**@@ -540,9 +541,14 @@ export function NewEditorShell() {);const handleSeek = useCallback(- (timeSec: number) => {+ (timeSec: number, preservePlayback?: boolean) => {setCurrentTime(timeSec);- setSeekTarget({ timeSec, isSource: false, requestId: ++seekSeqRef.current });+ setSeekTarget({+ timeSec,+ isSource: false,+ requestId: ++seekSeqRef.current,+ preservePlayback,+ });},[setCurrentTime],);@@ -643,10 +649,17 @@ export function NewEditorShell() {if (videoElement.paused) {const playhead = useProjectStore.getState().currentTimeSec;const duration = totalVirtualDuration(clipsForEndedRef.current);- if (duration > 0 && playhead >= duration - 0.05) {- handleSeek(0);+ const endPos = locateVirtualPosition(clipsForEndedRef.current, duration);+ const currentPos = locateVirtualPosition(clipsForEndedRef.current, playhead);+ const hasNextClip = clipsForEndedRef.current.some((c) => c.timelineStartSec > playhead + 0.1);+ const isAtEnd =+ duration > 0 &&+ playhead >= duration - 0.05 &&+ !hasNextClip &&+ currentPos?.clip.id === endPos?.clip.id;+ if (isAtEnd) {+ handleSeek(0, true);const startPos = locateVirtualPosition(clipsForEndedRef.current, 0);- const endPos = locateVirtualPosition(clipsForEndedRef.current, duration);if (startPos && startPos.clip.assetId === endPos?.clip.assetId) {try {videoElement.currentTime = startPos.sourceTimeSec;diff --git a/src/components/ai-edition/Preview.tsx b/src/components/ai-edition/Preview.tsxindex c809a329b..2d64b4428 100644--- a/src/components/ai-edition/Preview.tsx+++ b/src/components/ai-edition/Preview.tsx@@ -50,7 +50,7 @@ interface PreviewProps {onAnnotationChange?: (id: string, patch: Partial<AxcutAnnotationRegion>) => void;onAnnotationBlurDataChange?: (id: string, blurData: BlurData) => void;onAnnotationCommit?: () => void;- seekTarget: { timeSec: number; requestId: number } | null;+ seekTarget: { timeSec: number; requestId: number; preservePlayback?: boolean } | null;onTimeChange: (sec: number) => void;onSeek: (sec: number) => void;onLoadedMetadata: (sec: number, assetId: string) => void;diff --git a/src/components/ai-edition/PreviewCanvas.tsx b/src/components/ai-edition/PreviewCanvas.tsxindex 8a0693a43..24dca18be 100644--- a/src/components/ai-edition/PreviewCanvas.tsx+++ b/src/components/ai-edition/PreviewCanvas.tsx@@ -102,7 +102,7 @@ interface PreviewCanvasProps {onAnnotationChange?: (id: string, patch: Partial<AxcutAnnotationRegion>) => void;onAnnotationBlurDataChange?: (id: string, blurData: BlurData) => void;onAnnotationCommit?: () => void;- seekTarget: { timeSec: number; requestId: number } | null;+ seekTarget: { timeSec: number; requestId: number; preservePlayback?: boolean } | null;onTimeChange: (sec: number) => void;onSeek: (sec: number) => void;onLoadedMetadata: (sec: number, assetId: string) => void;diff --git a/src/components/ai-edition/VirtualPreview.playback.test.tsx b/src/components/ai-edition/VirtualPreview.playback.test.tsxindex 4a8ffc4fb..517d0b9b6 100644--- a/src/components/ai-edition/VirtualPreview.playback.test.tsx+++ b/src/components/ai-edition/VirtualPreview.playback.test.tsx@@ -93,6 +93,9 @@ function driveVideo(element: HTMLVideoElement) {play: () => {paused = false;},+ pause: () => {+ paused = true;+ },seekTo: (next: number) => {currentTime = next;},@@ -821,4 +824,470 @@ describe("VirtualPreview replay", () => {expect(video.currentTime).toBe(2);expect(reportedTimes(onTimeChange).at(-1)).toBe(0);});++ it("restarts playback across assets when the last clip uses a different asset than the first", () => {+ const sources: VideoSource[] = [+ { id: "a1", src: "file:///tmp/a1.mp4", label: "a1" },+ { id: "a2", src: "file:///tmp/a2.mp4", label: "a2" },+ ];+ const clips = [clip("clip_1", "a1", 0, 5, 0), clip("clip_2", "a2", 0, 5, 5)];+ let currentSeekTarget: {+ timeSec: number;+ requestId: number;+ preservePlayback?: boolean;+ } | null = null;+ const onTimeChange = vi.fn();++ const view = render(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );++ // Mount first asset (a1)+ const videoEl1 = view.container.querySelector("video");+ if (!videoEl1) throw new Error("no initial <video>");+ driveVideo(videoEl1);+ act(() => {+ fireEvent.loadedMetadata(videoEl1);+ });++ // Seek to the second clip on asset a2 (e.g. 8s virtual time)+ act(() => {+ currentSeekTarget = { timeSec: 8, requestId: 1 };+ view.rerender(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );+ });++ // Now second asset (a2) mounts+ const videoEl2 = view.container.querySelector("video");+ if (!videoEl2) throw new Error("no second <video>");+ const video2 = driveVideo(videoEl2);+ act(() => {+ fireEvent.loadedMetadata(videoEl2);+ });+ video2.play();+ // Play to end of clip_2 / timeline (10s)+ video2.seekTo(5);+ video2.pause();+ act(() => {+ fireEvent.ended(videoEl2);+ });+ expect(videoEl2.paused).toBe(true);++ // Shell triggers replay: handleSeek(0, true) + play()+ act(() => {+ currentSeekTarget = { timeSec: 0, requestId: 2, preservePlayback: true };+ view.rerender(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );+ video2.play();+ fireEvent.play(videoEl2);+ });++ // Replay triggers switch back to a1+ const replayVideoEl1 = view.container.querySelector("video");+ if (!replayVideoEl1) throw new Error("no replay <video>");+ const replayVideo1 = driveVideo(replayVideoEl1);++ // Metadata loads for a1+ act(() => {+ fireEvent.loadedMetadata(replayVideoEl1);+ });++ // Replay must have automatically resumed playback on the first asset+ expect(replayVideo1.currentTime).toBe(0);+ expect(replayVideoEl1.paused).toBe(false);+ });++ it("resumes playback at 14s after seeking from B to A while paused", () => {+ const sources: VideoSource[] = [+ { id: "a1", src: "file:///tmp/a1.mp4", label: "a1" },+ { id: "a2", src: "file:///tmp/a2.mp4", label: "a2" },+ ];+ const clips = [clip("clip_1", "a1", 0, 15, 0), clip("clip_2", "a2", 0, 15, 15)];+ let currentSeekTarget: {+ timeSec: number;+ requestId: number;+ preservePlayback?: boolean;+ } | null = null;+ const onTimeChange = vi.fn();++ const view = render(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );++ const videoEl1 = view.container.querySelector("video");+ if (!videoEl1) throw new Error("no initial <video>");+ driveVideo(videoEl1);+ act(() => {+ fireEvent.loadedMetadata(videoEl1);+ });++ // Seek to clip_2 on asset a2 at 20s+ act(() => {+ currentSeekTarget = { timeSec: 20, requestId: 1 };+ view.rerender(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );+ });++ const videoEl2 = view.container.querySelector("video");+ if (!videoEl2) throw new Error("no second <video>");+ const video2 = driveVideo(videoEl2);+ act(() => {+ fireEvent.loadedMetadata(videoEl2);+ });+ video2.seekTo(5);+ video2.pause();++ // Paused in B. Click timeline at 14s (clip A)+ act(() => {+ currentSeekTarget = { timeSec: 14, requestId: 2 };+ view.rerender(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );+ });++ // rAF tick while loading+ tick();++ const videoElA = view.container.querySelector("video");+ if (!videoElA) throw new Error("no video for A");+ const videoA = driveVideo(videoElA);++ tick();++ act(() => {+ fireEvent.loadedMetadata(videoElA);+ });++ tick();++ // User waits 2s, presses Play+ act(() => {+ videoA.play();+ fireEvent.play(videoElA);+ });++ tick();++ expect(videoA.currentTime).toBe(14);+ expect(reportedTimes(onTimeChange).at(-1)).toBe(14);+ });++ it("resumes playback at 14s after cross-source seek during playback", () => {+ const sources: VideoSource[] = [+ { id: "a1", src: "file:///tmp/a1.mp4", label: "a1" },+ { id: "a2", src: "file:///tmp/a2.mp4", label: "a2" },+ ];+ const clips = [clip("clip_1", "a1", 0, 15, 0), clip("clip_2", "a2", 0, 15, 15)];+ let currentSeekTarget: {+ timeSec: number;+ requestId: number;+ preservePlayback?: boolean;+ } | null = null;+ const onTimeChange = vi.fn();++ const view = render(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );++ const videoEl1 = view.container.querySelector("video");+ if (!videoEl1) throw new Error("no initial <video>");+ driveVideo(videoEl1);+ act(() => {+ fireEvent.loadedMetadata(videoEl1);+ });++ // Start playing in clip_2 on asset a2 at 20s+ act(() => {+ currentSeekTarget = { timeSec: 20, requestId: 1, preservePlayback: true };+ view.rerender(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );+ });++ const videoEl2 = view.container.querySelector("video");+ if (!videoEl2) throw new Error("no second <video>");+ const video2 = driveVideo(videoEl2);+ act(() => {+ fireEvent.loadedMetadata(videoEl2);+ });+ video2.seekTo(5);+ video2.play();++ // WHILE PLAYING in B at 20s (source time 5):+ // Click timeline at 14s (clip A)+ act(() => {+ currentSeekTarget = { timeSec: 14, requestId: 2, preservePlayback: true };+ view.rerender(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );+ });++ // rAF loop ticks+ tick();++ const videoElA = view.container.querySelector("video");+ if (!videoElA) throw new Error("no video for A");+ const videoA = driveVideo(videoElA);++ tick();++ act(() => {+ fireEvent.loadedMetadata(videoElA);+ });++ tick();++ expect(videoA.currentTime).toBe(14);+ expect(reportedTimes(onTimeChange).at(-1)).toBe(14);+ });++ it("does not let stale loadState in tick closure block clip boundary checks or timeline end", () => {+ const sources: VideoSource[] = [+ { id: "a1", src: "file:///tmp/a1.mp4", label: "a1" },+ { id: "a2", src: "file:///tmp/a2.mp4", label: "a2" },+ ];+ const clips = [clip("clip_1", "a1", 0, 15, 0), clip("clip_2", "a2", 0, 15, 15)];+ let currentSeekTarget: {+ timeSec: number;+ requestId: number;+ preservePlayback?: boolean;+ } | null = null;+ const onTimeChange = vi.fn();++ const view = render(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );++ const videoEl1 = view.container.querySelector("video");+ if (!videoEl1) throw new Error("no initial <video>");+ driveVideo(videoEl1);+ act(() => {+ fireEvent.loadedMetadata(videoEl1);+ });++ // Seek to clip_2 on asset a2 at 28.5s+ act(() => {+ currentSeekTarget = { timeSec: 28.5, requestId: 1, preservePlayback: true };+ view.rerender(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );+ });++ tick();++ const videoEl2 = view.container.querySelector("video");+ if (!videoEl2) throw new Error("no second <video>");+ const video2 = driveVideo(videoEl2);+ act(() => {+ fireEvent.loadedMetadata(videoEl2);+ });+ video2.play();++ // Advance video2 to 15.0s (30s virtual time, timeline end)+ video2.seekTo(15);+ tick();++ // If tick was blocked by stale loadState === "loading", video2 would not be paused+ // and virtual time would not reach 30s.+ expect(video2.pauseCalls.length).toBeGreaterThan(0);+ expect(reportedTimes(onTimeChange).at(-1)).toBe(30);+ });++ it("advances across source boundary when playing from clip A into clip B without being blocked by stale loadState", () => {+ const sources: VideoSource[] = [+ { id: "a1", src: "file:///tmp/a1.mp4", label: "a1" },+ { id: "a2", src: "file:///tmp/a2.mp4", label: "a2" },+ ];+ const clips = [clip("clip_1", "a1", 0, 15, 0), clip("clip_2", "a2", 0, 15, 15)];+ let currentSeekTarget: {+ timeSec: number;+ requestId: number;+ preservePlayback?: boolean;+ } | null = null;+ const onTimeChange = vi.fn();++ const view = render(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );++ const videoEl1 = view.container.querySelector("video");+ if (!videoEl1) throw new Error("no initial <video>");+ const video1 = driveVideo(videoEl1);+ act(() => {+ fireEvent.loadedMetadata(videoEl1);+ });++ // Start playing in clip_1 at 14s+ act(() => {+ currentSeekTarget = { timeSec: 14, requestId: 1, preservePlayback: true };+ view.rerender(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );+ });++ video1.seekTo(14);+ video1.play();+ tick();++ // Play to clip_1's boundary at 15s+ video1.seekTo(15);+ tick();++ // Now asset switch happens into clip_2 (a2).+ const videoEl2 = view.container.querySelector("video");+ if (!videoEl2) throw new Error("no second <video>");+ const video2 = driveVideo(videoEl2);+ act(() => {+ fireEvent.loadedMetadata(videoEl2);+ });+ video2.play();++ // Play in clip_2 at source time 1s (virtual time 16s)+ video2.seekTo(1);+ tick();++ // Tick should process and report virtual time 16s (not blocked by stale loadState)+ expect(reportedTimes(onTimeChange).at(-1)).toBe(16);+ });++ it("advances to next clip when onWaiting is fired before onEnded at clip boundary", () => {+ const sources: VideoSource[] = [+ { id: "a1", src: "file:///tmp/a1.mp4", label: "a1" },+ { id: "a2", src: "file:///tmp/a2.mp4", label: "a2" },+ ];+ const clips = [clip("clip_1", "a1", 0, 15, 0), clip("clip_2", "a2", 0, 15, 15)];+ let currentSeekTarget: {+ timeSec: number;+ requestId: number;+ preservePlayback?: boolean;+ } | null = null;+ const onTimeChange = vi.fn();++ const view = render(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );++ const videoEl1 = view.container.querySelector("video");+ if (!videoEl1) throw new Error("no initial <video>");+ const video1 = driveVideo(videoEl1);+ act(() => {+ fireEvent.loadedMetadata(videoEl1);+ });++ // Start playing near the end of clip_1+ act(() => {+ currentSeekTarget = { timeSec: 14.9, requestId: 1, preservePlayback: true };+ view.rerender(+ <VirtualPreview+ videoSources={sources}+ clips={clips}+ seekTarget={currentSeekTarget}+ onTimeChange={onTimeChange}+ />,+ );+ });++ video1.seekTo(14.9);+ video1.play();+ tick();++ // Browser buffers at EOF: fires waiting (loadState -> loading)+ act(() => {+ fireEvent.waiting(videoEl1);+ });++ // Browser pauses and fires ended at EOF+ act(() => {+ video1.pause();+ fireEvent.ended(videoEl1);+ });++ tick();++ // Asset switch happens into clip_2 (a2)+ const videoEl2 = view.container.querySelector("video");+ if (!videoEl2) throw new Error("no second <video>");+ const video2 = driveVideo(videoEl2);+ act(() => {+ fireEvent.loadedMetadata(videoEl2);+ });+ video2.play();+ video2.seekTo(1);+ tick();++ expect(reportedTimes(onTimeChange).at(-1)).toBe(16);+ });});diff --git a/src/components/ai-edition/VirtualPreview.tsx b/src/components/ai-edition/VirtualPreview.tsxindex 481b0a690..03b7530bc 100644--- a/src/components/ai-edition/VirtualPreview.tsx+++ b/src/components/ai-edition/VirtualPreview.tsx@@ -454,7 +454,12 @@ interface VirtualPreviewProps {trimRanges?: AxcutTrimRange[];/** Play the raw timeline continuously while capturing a voiceover. */playUneditedTimeline?: boolean;- seekTarget?: { timeSec: number; isSource?: boolean; requestId: number } | null;+ seekTarget?: {+ timeSec: number;+ isSource?: boolean;+ requestId: number;+ preservePlayback?: boolean;+ } | null;onTimeChange?: (timeSec: number) => void;onLoadedMetadata?: (durationSec: number,@@ -587,6 +592,12 @@ export function VirtualPreview({const activeClipIdRef = useRef<string | null>(null);const [virtualTimeSec, setVirtualTimeSec] = useState(0);const [loadState, setLoadState] = useState<"idle" | "loading" | "ready" | "error">("idle");+ const loadStateRef = useRef(loadState);+ loadStateRef.current = loadState;+ const updateLoadState = useCallback((nextState: "idle" | "loading" | "ready" | "error") => {+ loadStateRef.current = nextState;+ setLoadState(nextState);+ }, []);const [sourceIndex, setSourceIndex] = useState(0);const virtualDurationSec = useMemo(() => totalVirtualDuration(clips), [clips]);@@ -1171,7 +1182,14 @@ export function VirtualPreview({// position rather than jumping. Note this cannot ride on `v.paused`,// the gate the rest of the tick uses — an `error` does not fire// `pause`, so a failure mid-playback leaves `paused` false.- if (recoveringRef.current || v.seeking) {+ // Same for in-flight pending seeks or loads: the element's currentTime is+ // stale or 0 and must not trigger clip boundary or trim fallbacks.+ if (+ recoveringRef.current ||+ v.seeking ||+ pendingSeekRef.current !== null ||+ loadStateRef.current === "loading"+ ) {return;}const activeSourceId = videoSourcesRef.current[sourceIndexRef.current]?.id;@@ -1233,7 +1251,10 @@ export function VirtualPreview({return;}v.pause();- updateVirtualTime(virtualDurationSecRef.current);+ const endPos = locateVirtualPosition(clipsRef.current, virtualDurationSecRef.current);+ if (activeClipIdRef.current === endPos?.clip.id) {+ updateVirtualTime(virtualDurationSecRef.current);+ }return;}}@@ -1319,7 +1340,10 @@ export function VirtualPreview({if (nextClip) seekToVirtualTimeRef.current?.(nextClip.timelineStartSec, true);else {v.pause();- updateVirtualTime(virtualDurationSecRef.current);+ const endPos = locateVirtualPosition(clipsRef.current, virtualDurationSecRef.current);+ if (activeClipIdRef.current === endPos?.clip.id) {+ updateVirtualTime(virtualDurationSecRef.current);+ }}return;}@@ -1340,7 +1364,10 @@ export function VirtualPreview({);if (!nextClip) {v.pause();- updateVirtualTime(virtualDurationSecRef.current);+ const endPos = locateVirtualPosition(clipsRef.current, virtualDurationSecRef.current);+ if (activeClipIdRef.current === endPos?.clip.id) {+ updateVirtualTime(virtualDurationSecRef.current);+ }return;}seekToVirtualTimeRef.current?.(nextClip.timelineStartSec, true);@@ -1491,8 +1518,11 @@ export function VirtualPreview({const shouldContinuePlayback = preservePlayback && (forceResume || !videoRef.current?.paused);if (isAssetSwitch) {+ if (!shouldContinuePlayback) {+ videoRef.current?.pause();+ }setSourceIndex(targetIndex);- setLoadState("loading");+ updateLoadState("loading");updateVirtualTime(position.virtualTimeSec);pendingSeekRef.current = {sourceTimeSec: position.sourceTimeSec,@@ -1522,12 +1552,12 @@ export function VirtualPreview({// A rejection here means playback never actually started, so the browser// never fired 'play' — nothing to reconcile, just avoid an unhandled// rejection warning.- void video.play().catch(() => {+ void video.play()?.catch(() => {// swallow: rejection just means playback never started});}},- [applySourceTime, clips, videoSources, sourceIndex, updateVirtualTime],+ [applySourceTime, clips, videoSources, sourceIndex, updateLoadState, updateVirtualTime],);const seekToSourceTime = useCallback(@@ -1550,52 +1580,55 @@ export function VirtualPreview({* queue the position to come back to. Everything is read through refs, so the* callback is stable and the scheduled work always sees current state.*/- const reloadActiveSource = useCallback((play: boolean, delayMs: number) => {- if (retryTimerRef.current !== null) {- window.clearTimeout(retryTimerRef.current);- retryTimerRef.current = null;- }- recoveringRef.current = true;- setLoadState("loading");- retryTimerRef.current = window.setTimeout(() => {- retryTimerRef.current = null;- const video = videoRef.current;- if (!video) return;- // Resolved HERE rather than when the error fired: the user can scrub- // during the backoff, and the position they left the playhead on is- // the one that has to come back. An asset switch queued in the- // meantime is newer intent still, so it wins outright.- if (!pendingSeekRef.current) {- const position = locateVirtualPosition(clipsRef.current, virtualTimeSecRef.current);- // `locateVirtualPosition` answers for whatever clip the playhead- // is on, which after a boundary advance can belong to a DIFFERENT- // asset — its source time would be a meaningless offset into the- // file we are about to reload. The mounted source is read from the- // same two mirrors the rAF tick uses, rather than kept in a third.- const clipIsOnThisSource =- position?.clip.assetId === videoSourcesRef.current[sourceIndexRef.current]?.id;- if (position && clipIsOnThisSource) {- // The rAF resumes against this; leaving it stale would resolve- // the next tick against the clip we were on before the failure.- activeClipIdRef.current = position.clip.id;- }- const resumeSec =- position && clipIsOnThisSource ? position.sourceTimeSec : lastGoodSourceTimeRef.current;- pendingSeekRef.current = {- sourceTimeSec: Number.isFinite(resumeSec) ? resumeSec : 0,- play,- };+ const reloadActiveSource = useCallback(+ (play: boolean, delayMs: number) => {+ if (retryTimerRef.current !== null) {+ window.clearTimeout(retryTimerRef.current);+ retryTimerRef.current = null;}- // The restore itself rides the cross-asset path `onLoadedMetadata`- // already implements — one resume path in this component, not two.- //- // load() and nothing else: per spec it unconditionally re-runs the- // media load algorithm. Re-assigning `src` first (the obvious- // alternative) starts a SECOND load that aborts the first, which- // surfaces as a spurious MEDIA_ERR_ABORTED.- video.load();- }, delayMs);- }, []);+ recoveringRef.current = true;+ updateLoadState("loading");+ retryTimerRef.current = window.setTimeout(() => {+ retryTimerRef.current = null;+ const video = videoRef.current;+ if (!video) return;+ // Resolved HERE rather than when the error fired: the user can scrub+ // during the backoff, and the position they left the playhead on is+ // the one that has to come back. An asset switch queued in the+ // meantime is newer intent still, so it wins outright.+ if (!pendingSeekRef.current) {+ const position = locateVirtualPosition(clipsRef.current, virtualTimeSecRef.current);+ // `locateVirtualPosition` answers for whatever clip the playhead+ // is on, which after a boundary advance can belong to a DIFFERENT+ // asset — its source time would be a meaningless offset into the+ // file we are about to reload. The mounted source is read from the+ // same two mirrors the rAF tick uses, rather than kept in a third.+ const clipIsOnThisSource =+ position?.clip.assetId === videoSourcesRef.current[sourceIndexRef.current]?.id;+ if (position && clipIsOnThisSource) {+ // The rAF resumes against this; leaving it stale would resolve+ // the next tick against the clip we were on before the failure.+ activeClipIdRef.current = position.clip.id;+ }+ const resumeSec =+ position && clipIsOnThisSource ? position.sourceTimeSec : lastGoodSourceTimeRef.current;+ pendingSeekRef.current = {+ sourceTimeSec: Number.isFinite(resumeSec) ? resumeSec : 0,+ play,+ };+ }+ // The restore itself rides the cross-asset path `onLoadedMetadata`+ // already implements — one resume path in this component, not two.+ //+ // load() and nothing else: per spec it unconditionally re-runs the+ // media load algorithm. Re-assigning `src` first (the obvious+ // alternative) starts a SECOND load that aborts the first, which+ // surfaces as a spurious MEDIA_ERR_ABORTED.+ video.load();+ }, delayMs);+ },+ [updateLoadState],+ );/** The element is loaded again, so the reload is over and any failure the* caller is showing can go. Note what this does NOT do: re-arm the retry@@ -1669,7 +1702,11 @@ export function VirtualPreview({if (seekTarget.isSource) {seekToSourceTimeRef.current(seekTarget.timeSec);} else {- seekToVirtualTimeRef.current?.(seekTarget.timeSec);+ seekToVirtualTimeRef.current?.(+ seekTarget.timeSec,+ seekTarget.preservePlayback,+ seekTarget.preservePlayback,+ );}}, [seekTarget]);@@ -1701,7 +1738,7 @@ export function VirtualPreview({mutedplaysInlineonLoadedMetadata={(e) => {- setLoadState("ready");+ updateLoadState("ready");markSourceLoaded();// ponytail: forward the raw duration (possibly NaN for// MediaRecorder WebMs) to the parent. handleLoadedMetadata@@ -1748,9 +1785,9 @@ export function VirtualPreview({applySourceTime(e.currentTarget, queued);}}}- onWaiting={() => setLoadState("loading")}+ onWaiting={() => updateLoadState("loading")}onCanPlay={() => {- setLoadState("ready");+ updateLoadState("ready");markSourceLoaded();}}onError={(e) => {@@ -1812,7 +1849,7 @@ export function VirtualPreview({// Latched so an expiring window cannot restart the cycle behind// the card. Only Retry or a media change lifts it.gaveUpRef.current = true;- setLoadState("error");+ updateLoadState("error");// An 'error' doesn't itself fire 'pause', so make sure the shell's// transport state (single source of truth, see NewEditorShell's// own play/pause/ended listener) actually learns playback stopped.@@ -1824,15 +1861,24 @@ export function VirtualPreview({// plain play() would hit that same boundary and pause again.// Resolve the rewind here so cross-asset restarts also carry// playback intent through the pending metadata load.+ const duration = virtualDurationSecRef.current;+ const vTime = virtualTimeSecRef.current;+ const endPos = locateVirtualPosition(clipsRef.current, duration);+ const currentPos = locateVirtualPosition(clipsRef.current, vTime);+ const hasNextClip = clipsRef.current.some((c) => c.timelineStartSec > vTime + 0.1);if (- virtualDurationSecRef.current > 0 &&- virtualTimeSecRef.current >= virtualDurationSecRef.current - 0.05+ duration > 0 &&+ vTime >= duration - 0.05 &&+ !hasNextClip &&+ currentPos?.clip.id === endPos?.clip.id) {seekToVirtualTimeRef.current?.(0, true, true);}}}onEnded={(e) => {- if (!e.currentTarget.paused) return;+ if (!e.currentTarget.paused || pendingSeekRef.current !== null) {+ return;+ }// BUG corrigé : ce handler stoppait TOUJOURS la lecture dès que le// <video> brut atteignait SA PROPRE fin de fichier — une course// avec la boucle rAF (reachedClipEnd, plus haut) qui gère@@ -1860,7 +1906,13 @@ export function VirtualPreview({if (nextClip) {seekToVirtualTime(nextClip.timelineStartSec, true, true);} else {- updateVirtualTime(virtualDurationSecRef.current);+ const endPos = locateVirtualPosition(+ clipsRef.current,+ virtualDurationSecRef.current,+ );+ if (current && current.id === endPos?.clip.id) {+ updateVirtualTime(virtualDurationSecRef.current);+ }}}}// ponytail: handleTimeUpdate is now driven by the rAF loop