AI media harness for planning, generating, and editing locally
TypeScript
Add visual video reference trimming
Bitti4746568a5303
diff --git a/docs/video-references.md b/docs/video-references.mdindex 1732b87..0b868a7 100644--- a/docs/video-references.md+++ b/docs/video-references.md@@ -60,8 +60,26 @@ decoder selection if that configuration is unavailable. This allows the webviewto use its software decoder for formats such as AV1 when hardware decoding isunavailable.-Set the clip's start and duration, and choose whether to include its sound.-Each clip must last 2–15 seconds. A generation can use up to three video clips+Source videos may be longer than 15 seconds. Import initially selects the first+15 seconds, or the entire source if it is shorter (at least 2 seconds). Import+uses the actual video frame timestamps so fragmented MP4s with missing or+placeholder header durations can still be selected correctly.++Use **Position in full video** to move the reference segment anywhere in the+source. Drag the highlighted selection to move it without changing its length,+or drag either edge to trim it. The closer ruler displays up to 30 seconds at a+time so the edges remain usable on long videos. The start and duration fields+provide precise edits; all controls stay within the source and the 2–15 second+selection limit. The handles also support arrow keys (0.1 seconds), Shift+arrow+(1 second), Home, and End.++You can also seek in the video player and choose **Start at playhead**.+**Play selection** previews only the chosen segment and stops at its end.+**Include sound** controls the selected interval's soundtrack. Changing the+selection seeks the existing player without reloading the source. The segment+and sound setting are saved with the project and used during generation.++Each selected clip must last 2–15 seconds. A generation can use up to three video clipstotaling 15 seconds, alongside up to nine images. Cite the reference in a shotjust like an image reference. Use CUDA or Vulkan with a Ref2VA transformer,such as the **References** generator. Vulkan video references requirediff --git a/src/components/workspace/ReferenceVideo.test.tsx b/src/components/workspace/ReferenceVideo.test.tsxnew file mode 100644index 0000000..cd660f3--- /dev/null+++ b/src/components/workspace/ReferenceVideo.test.tsx@@ -0,0 +1,114 @@+// @vitest-environment jsdom+import "@testing-library/jest-dom/vitest";+import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";+import { useState } from "react";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import { readMediaFileUrl } from "../../lib/persistence";+import { projectReferenceSchema, type ProjectReference } from "../../lib/project";+import { ReferenceVideo } from "./ReferenceVideo";++vi.mock("../../lib/persistence", () => ({ readMediaFileUrl: vi.fn(async () => "blob:video") }));+beforeEach(() => {+ vi.spyOn(HTMLMediaElement.prototype, "pause").mockImplementation(() => {});+ vi.spyOn(HTMLMediaElement.prototype, "play").mockResolvedValue();+ URL.revokeObjectURL = vi.fn();+});+afterEach(() => { cleanup(); vi.clearAllMocks(); vi.restoreAllMocks(); vi.unstubAllGlobals(); });+const original: ProjectReference = {+ id: "reference", kind: "video", name: "Long video", description: "", intendedUse: [], createdAt: "2026-09-18T12:00:00Z",+ sourcePath: "C:/long.mp4", video: { startSeconds: 0, durationSeconds: 15, includeAudio: true },+};+function Harness({ initial = original }: { initial?: ProjectReference }) {+ const [reference, setReference] = useState(initial);+ return <><ReferenceVideo folderPath="C:/project" reference={reference} onChange={(video) => setReference((current) => projectReferenceSchema.parse({ ...current, video }))} /><output data-testid="saved">{JSON.stringify(reference.video)}</output></>;+}+async function ready(duration = 120) {+ const video = await screen.findByLabelText("Long video video preview") as HTMLVideoElement;+ Object.defineProperty(video, "duration", { value: duration, configurable: true });+ fireEvent.loadedMetadata(video);+ return video;+}+const saved = () => JSON.parse(screen.getByTestId("saved").textContent!);++it("starts at 0–15 seconds and moves and resizes the selection without reloading the source", async () => {+ render(<Harness />);+ const video = await ready();+ expect(screen.getByText("0:00.0 – 0:15.0")).toBeInTheDocument();+ fireEvent.change(screen.getByLabelText("Clip position"), { target: { value: "60" } });+ expect(saved()).toMatchObject({ startSeconds: 60, durationSeconds: 15 });+ expect(video.currentTime).toBe(60);+ fireEvent.keyDown(screen.getByRole("slider", { name: "Selection end" }), { key: "ArrowLeft", shiftKey: true });+ expect(saved().durationSeconds).toBe(14);+ fireEvent.keyDown(screen.getByRole("slider", { name: "Selection start" }), { key: "ArrowRight", shiftKey: true });+ expect(saved()).toMatchObject({ startSeconds: 61, durationSeconds: 13 });+ expect(screen.getByLabelText("Long video video preview")).toBe(video);+ expect(readMediaFileUrl).toHaveBeenCalledTimes(1);+});++it("keeps the selection in bounds at the end of a long video", async () => {+ render(<Harness />);+ await ready(3600);+ fireEvent.keyDown(screen.getByRole("slider", { name: "Move selection" }), { key: "End" });+ expect(saved()).toMatchObject({ startSeconds: 3585, durationSeconds: 15 });+ fireEvent.change(screen.getByLabelText("Clip duration (seconds)"), { target: { value: "100" } });+ fireEvent.blur(screen.getByLabelText("Clip duration (seconds)"));+ expect(saved().durationSeconds).toBe(15);+ fireEvent.keyDown(screen.getByRole("slider", { name: "Selection start" }), { key: "End" });+ expect(saved()).toMatchObject({ startSeconds: 3598, durationSeconds: 2 });+ fireEvent.keyDown(screen.getByRole("slider", { name: "Selection end" }), { key: "ArrowRight" });+ expect(saved().durationSeconds).toBe(2);+});++it("drags the selected interval and its edges", async () => {+ vi.stubGlobal("PointerEvent", class extends MouseEvent {+ pointerId: number;+ constructor(type: string, init: PointerEventInit) { super(type, init); this.pointerId = init.pointerId ?? 1; }+ });+ render(<Harness />);+ await ready();+ const bar = screen.getByLabelText("Drag the selection or its edges");+ Object.defineProperty(bar, "setPointerCapture", { value: vi.fn() });+ vi.spyOn(bar, "getBoundingClientRect").mockReturnValue({ width: 300, left: 0 } as DOMRect);+ fireEvent.pointerDown(screen.getByRole("slider", { name: "Move selection" }), { button: 0, pointerId: 1, clientX: 75 });+ fireEvent.pointerMove(bar, { pointerId: 1, clientX: 125 });+ fireEvent.pointerUp(bar, { pointerId: 1 });+ expect(saved()).toMatchObject({ startSeconds: 5, durationSeconds: 15 });+ fireEvent.pointerDown(screen.getByRole("slider", { name: "Selection end" }), { button: 0, pointerId: 2, clientX: 200 });+ fireEvent.pointerMove(bar, { pointerId: 2, clientX: 150 });+ fireEvent.pointerUp(bar, { pointerId: 2 });+ expect(saved()).toMatchObject({ startSeconds: 5, durationSeconds: 10 });+});++it("starts the segment at the frame chosen in the video player", async () => {+ render(<Harness />);+ const video = await ready();+ video.currentTime = 42;+ fireEvent.click(screen.getByRole("button", { name: "Start at playhead" }));+ expect(saved()).toMatchObject({ startSeconds: 42, durationSeconds: 15 });+});++it("plays only the selected interval and keeps soundtrack settings", async () => {+ render(<Harness initial={{ ...original, video: { startSeconds: 30, durationSeconds: 5, includeAudio: false } }} />);+ const video = await ready();+ expect(video.muted).toBe(true);+ await act(async () => fireEvent.click(screen.getByRole("button", { name: "Play selection" })));+ expect(video.currentTime).toBe(30);+ expect(video.play).toHaveBeenCalledTimes(1);+ video.currentTime = 36;+ fireEvent.timeUpdate(video);+ expect(video.currentTime).toBe(35);+ expect(screen.getByRole("button", { name: "Play selection" })).toBeInTheDocument();+ fireEvent.click(screen.getByLabelText("Include sound"));+ expect(saved().includeAudio).toBe(true);+});++it("uses the whole shorter source and restores a saved selection on reopen", async () => {+ const view = render(<Harness />);+ await ready(8);+ expect(saved()).toMatchObject({ startSeconds: 0, durationSeconds: 8 });+ view.unmount();+ render(<Harness initial={{ ...original, video: { startSeconds: 25, durationSeconds: 8, includeAudio: true } }} />);+ const video = await ready();+ expect(video.currentTime).toBe(25);+ expect(screen.getByText("0:25.0 – 0:33.0")).toBeInTheDocument();+});diff --git a/src/components/workspace/ReferenceVideo.tsx b/src/components/workspace/ReferenceVideo.tsxindex 0e64d8c..e9bb7ae 100644--- a/src/components/workspace/ReferenceVideo.tsx+++ b/src/components/workspace/ReferenceVideo.tsx@@ -1,25 +1,187 @@-import { useEffect, useState } from "react";+import { Pause, Play } from "lucide-react";+import { useEffect, useRef, useState, type KeyboardEvent, type PointerEvent } from "react";import { readMediaFileUrl } from "../../lib/persistence";import type { ProjectReference } from "../../lib/project";+import { editReferenceVideoTrim, initialReferenceVideoTrim, normalizeReferenceVideoTrim, referenceTime, type ReferenceVideoTrim, type TrimAction } from "../../lib/referenceVideoTrim";-export function ReferenceVideo({ folderPath, reference }: { folderPath: string; reference: ProjectReference }) {+type VideoOptions = NonNullable<ProjectReference["video"]>;++function SecondsInput({ label, value, min, max, onChange }: {+ label: string; value: number; min: number; max: number; onChange: (value: number) => void;+}) {+ const [draft, setDraft] = useState(String(value));+ useEffect(() => setDraft(String(Math.round(value * 1000) / 1000)), [value]);+ return <label>{label}<input type="number" min={min} max={max} step="0.1" value={draft}+ onChange={(event) => setDraft(event.target.value)} onKeyDown={(event) => { if (event.key === "Enter") event.currentTarget.blur(); }}+ onBlur={() => {+ const parsed = Number(draft);+ const next = draft.trim() && Number.isFinite(parsed) ? Math.max(min, Math.min(max, parsed)) : value;+ setDraft(String(next)); onChange(next);+ }} /></label>;+}++export function ReferenceVideo({ folderPath, reference, onChange }: {+ folderPath: string; reference: ProjectReference; onChange: (video: VideoOptions) => void;+}) {const [url, setUrl] = useState<string | null>(null);const [error, setError] = useState<string | null>(null);+ const [sourceDuration, setSourceDuration] = useState<number | null>(null);+ const [previewing, setPreviewing] = useState(false);+ const video = useRef<HTMLVideoElement>(null);+ const latest = useRef({ reference, onChange });+ latest.current = { reference, onChange };+ const bar = useRef<HTMLDivElement>(null);+ const gesture = useRef<{ pointerId: number; action: TrimAction; x: number; width: number; trim: ReferenceVideoTrim; windowStart: number; windowDuration: number } | null>(null);+ const [dragging, setDragging] = useState(false);+ const selectionPlayback = useRef(false);+ const frameCallback = useRef<number | null>(null);useEffect(() => {let disposed = false;let owned: string | null = null;- setUrl(null); setError(null);+ setUrl(null); setError(null); setSourceDuration(null); setPreviewing(false);+ selectionPlayback.current = false;+ gesture.current = null; setDragging(false);void readMediaFileUrl(folderPath, reference, "video/mp4").then((value) => {if (disposed) { if (value) URL.revokeObjectURL(value); return; }owned = value; setUrl(value);+ if (!value) setError("The source video is unavailable.");}).catch((reason: unknown) => { if (!disposed) setError(String(reason)); });return () => { disposed = true; if (owned) URL.revokeObjectURL(owned); };- }, [folderPath, reference.sourcePath, reference.relativePath]);- if (error) return <p role="alert">{error}</p>;- if (!url) return <p>Loading video preview…</p>;- return <video className="reference-video-preview" key={`${url}:${reference.video?.startSeconds ?? 0}`}- src={url} controls preload="metadata" aria-label={`${reference.name} video preview`}- muted={reference.video?.includeAudio === false}- onLoadedMetadata={(event) => { event.currentTarget.currentTime = reference.video?.startSeconds ?? 0; }}- onError={() => setError("This computer cannot preview this video codec.")} />;+ }, [folderPath, reference.id, reference.sourcePath, reference.relativePath]);++ const trim = sourceDuration === null ? null : normalizeReferenceVideoTrim(reference.video ?? initialReferenceVideoTrim(sourceDuration), sourceDuration);+ const start = trim?.startSeconds ?? 0;+ const length = trim?.durationSeconds ?? 15;+ const end = start + length;+ // A local ruler keeps handles usable even for hour-long sources; the+ // overview slider moves this window anywhere in the source.+ const windowDuration = dragging && gesture.current ? gesture.current.windowDuration : Math.min(sourceDuration ?? 30, 30);+ const windowStart = dragging && gesture.current ? gesture.current.windowStart : Math.max(0, Math.min((sourceDuration ?? 30) - windowDuration, start - (windowDuration - length) / 2));++ const stop = () => {+ selectionPlayback.current = false;+ if (frameCallback.current !== null) video.current?.cancelVideoFrameCallback?.(frameCallback.current);+ frameCallback.current = null;+ video.current?.pause();+ setPreviewing(false);+ };+ useEffect(() => {+ const element = video.current;+ return () => { if (frameCallback.current !== null) element?.cancelVideoFrameCallback?.(frameCallback.current); };+ }, [url]);+ useEffect(() => {+ stop();+ if (video.current && sourceDuration !== null) video.current.currentTime = start;+ }, [start, length]);++ const change = (action: TrimAction, value: number, original = trim) => {+ if (!original || sourceDuration === null) return;+ const next = editReferenceVideoTrim(original, sourceDuration, action, value);+ onChange({ ...next, includeAudio: reference.video?.includeAudio ?? true });+ };+ const checkPlayback = () => {+ const element = video.current;+ if (!element || !selectionPlayback.current) return;+ const settings = latest.current.reference.video;+ const limit = (settings?.startSeconds ?? start) + (settings?.durationSeconds ?? length);+ if (element.currentTime >= limit) { stop(); element.currentTime = limit; return; }+ if (element.requestVideoFrameCallback) frameCallback.current = element.requestVideoFrameCallback(checkPlayback);+ };+ const playSelection = async () => {+ const element = video.current;+ if (!element || !trim) return;+ if (previewing) { stop(); return; }+ element.currentTime = start;+ selectionPlayback.current = true; setPreviewing(true);+ try { await element.play(); if (selectionPlayback.current) checkPlayback(); }+ catch (reason) { stop(); setError(`Could not play the selection: ${String(reason)}`); }+ };+ const beginDrag = (event: PointerEvent, action: TrimAction) => {+ if (!trim || !bar.current || event.button !== 0) return;+ event.preventDefault(); event.stopPropagation();+ const box = bar.current.getBoundingClientRect();+ if (!box.width) return;+ stop();+ gesture.current = { pointerId: event.pointerId, action, x: event.clientX, width: box.width, trim, windowStart, windowDuration };+ bar.current.setPointerCapture(event.pointerId);+ setDragging(true);+ };+ const keyboard = (event: KeyboardEvent, action: TrimAction, value: number, min: number, max: number) => {+ const step = event.shiftKey ? 1 : .1;+ const next = event.key === "Home" ? min : event.key === "End" ? max+ : event.key === "ArrowLeft" || event.key === "ArrowDown" ? value - step+ : event.key === "ArrowRight" || event.key === "ArrowUp" ? value + step : null;+ if (next !== null) { event.preventDefault(); change(action, Math.round(next * 1000) / 1000); }+ };++ return <>+ {error && <p role="alert">{error}</p>}+ {!url && !error && <p>Loading video preview…</p>}+ {url && <video ref={video} className="reference-video-preview" src={url} controls preload="metadata" aria-label={`${reference.name} video preview`}+ muted={reference.video?.includeAudio === false}+ onLoadedMetadata={(event) => {+ const duration = event.currentTarget.duration;+ try {+ const settings = latest.current.reference.video;+ const normalized = normalizeReferenceVideoTrim(settings ?? initialReferenceVideoTrim(duration), duration);+ setSourceDuration(duration);+ event.currentTarget.currentTime = normalized.startSeconds;+ if (!settings || settings.startSeconds !== normalized.startSeconds || settings.durationSeconds !== normalized.durationSeconds) {+ latest.current.onChange({ ...normalized, includeAudio: settings?.includeAudio ?? true });+ }+ } catch (reason) { setError(String(reason)); }+ }}+ onTimeUpdate={(event) => {+ if (selectionPlayback.current && event.currentTarget.currentTime >= end) { stop(); event.currentTarget.currentTime = end; }+ }}+ onPause={() => { selectionPlayback.current = false; setPreviewing(false); }}+ onError={() => setError("This computer cannot preview this video codec.")} />}+ {trim && sourceDuration !== null && <div className="reference-trim" aria-label="Reference segment editor">+ <div className="reference-trim__summary"><strong>{referenceTime(start)} – {referenceTime(end)}</strong><span>{Number(length.toFixed(2))} s selected · 15 s max</span></div>+ <label className="reference-trim__overview">Position in full video+ <input aria-label="Clip position" type="range" min="0" max={sourceDuration - length} step="any" value={start}+ disabled={sourceDuration === length} onChange={(event) => change("move", Number(event.target.value))} />+ </label>+ <div className="reference-trim__ticks"><span>0:00</span><span>Source {referenceTime(sourceDuration)}</span></div>+ <div ref={bar} className="reference-trim__bar" aria-label="Drag the selection or its edges"+ onPointerDown={(event) => {+ if (event.target !== event.currentTarget || event.button !== 0) return;+ const box = event.currentTarget.getBoundingClientRect();+ if (box.width) change("move", windowStart + (event.clientX - box.left) / box.width * windowDuration - length / 2);+ }}+ onPointerMove={(event) => {+ const drag = gesture.current;+ if (!drag || event.pointerId !== drag.pointerId) return;+ const delta = (event.clientX - drag.x) / drag.width * drag.windowDuration;+ const original = drag.action === "end" ? drag.trim.startSeconds + drag.trim.durationSeconds : drag.trim.startSeconds;+ const maximum = drag.windowStart + drag.windowDuration - (drag.action === "move" ? drag.trim.durationSeconds : 0);+ const value = Math.max(drag.windowStart, Math.min(maximum, original + delta));+ change(drag.action, Math.round(value * 1000) / 1000, drag.trim);+ }}+ onPointerUp={() => { gesture.current = null; setDragging(false); }}+ onPointerCancel={() => { gesture.current = null; setDragging(false); }}+ onLostPointerCapture={() => { gesture.current = null; setDragging(false); }}>+ <div className="reference-trim__selection" style={{ left: `${(start - windowStart) / windowDuration * 100}%`, width: `${length / windowDuration * 100}%` }}>+ <button type="button" className="reference-trim__handle" role="slider" aria-label="Selection start" aria-valuemin={Math.max(0, end - 15)} aria-valuemax={end - 2} aria-valuenow={start} aria-valuetext={referenceTime(start)}+ onPointerDown={(event) => beginDrag(event, "start")} onKeyDown={(event) => keyboard(event, "start", start, Math.max(0, end - 15), end - 2)} />+ <button type="button" className="reference-trim__move" role="slider" aria-label="Move selection" aria-valuemin={0} aria-valuemax={sourceDuration - length} aria-valuenow={start} aria-valuetext={referenceTime(start)}+ onPointerDown={(event) => beginDrag(event, "move")} onKeyDown={(event) => keyboard(event, "move", start, 0, sourceDuration - length)}>Drag</button>+ <button type="button" className="reference-trim__handle" role="slider" aria-label="Selection end" aria-valuemin={start + 2} aria-valuemax={Math.min(sourceDuration, start + 15)} aria-valuenow={end} aria-valuetext={referenceTime(end)}+ onPointerDown={(event) => beginDrag(event, "end")} onKeyDown={(event) => keyboard(event, "end", end, start + 2, Math.min(sourceDuration, start + 15))} />+ </div>+ </div>+ <div className="reference-trim__ticks"><span>{referenceTime(windowStart)}</span><span>{referenceTime(windowStart + windowDuration)}</span></div>+ <p>Move the selection or drag its edges. Use the position slider to find a part anywhere in the video.</p>+ <div className="reference-trim__numbers">+ <SecondsInput label="Clip start (seconds)" value={start} min={0} max={sourceDuration - length} onChange={(value) => change("move", value)} />+ <SecondsInput label="Clip duration (seconds)" value={length} min={2} max={Math.min(15, sourceDuration - start)} onChange={(value) => change("duration", value)} />+ </div>+ <div className="reference-trim__actions">+ <button type="button" className="secondary-button" onClick={() => void playSelection()}>{previewing ? <Pause size={14} /> : <Play size={14} />}{previewing ? "Pause selection" : "Play selection"}</button>+ <button type="button" className="secondary-button" onClick={() => change("move", video.current?.currentTime ?? start)}>Start at playhead</button>+ </div>+ <label><input type="checkbox" checked={reference.video?.includeAudio ?? true}+ onChange={(event) => onChange({ ...trim, includeAudio: event.target.checked })} /> Include sound</label>+ </div>}+ </>;}diff --git a/src/components/workspace/ReferencesView.tsx b/src/components/workspace/ReferencesView.tsxindex 6816a3f..7b218ee 100644--- a/src/components/workspace/ReferencesView.tsx+++ b/src/components/workspace/ReferencesView.tsx@@ -46,20 +46,6 @@ const referenceKindLabel = (reference: ProjectReference) => {return `${count} image${count === 1 ? "" : "s"}${isReferenceDescribed(reference) ? " + text" : ""}`;};-function ClipSecondsInput({ label, value, min, max = Number.MAX_SAFE_INTEGER, onChange }: {- label: string; value: number; min: number; max?: number; onChange: (value: number) => void;-}) {- const [draft, setDraft] = useState(String(value));- useEffect(() => setDraft(String(value)), [value]);- return <label>{label}<input type="number" min={min} max={max} step={0.1} value={draft}- onChange={(event) => setDraft(event.target.value)} onKeyDown={(event) => { if (event.key === "Enter") event.currentTarget.blur(); }}- onBlur={() => {- const parsed = Number(draft);- const next = draft.trim() && Number.isFinite(parsed) ? Math.max(min, Math.min(max, parsed)) : value;- setDraft(String(next)); onChange(next);- }} /></label>;-}-export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon, onGenerateBuiltinIcons, onRegenerateBuiltinIcon, pendingBuiltinIconIds = new Set<string>(), pendingIconIds = new Set<string>(), onOpenGenerator }: {config: ProjectConfig;folderPath: string;@@ -250,14 +236,9 @@ export function ReferencesView({ config, folderPath, onChange, onRegenerateIcon,<div className="reference-inspector__scroll">{selected ? <>{selected.kind === "video" && <section className="reference-video" aria-label="Video reference">- <ReferenceVideo folderPath={folderPath} reference={selected} />- <p>Use 2–15 seconds per clip, up to 3 clips totaling 15 seconds. Requires a Ref2VA generator. Supports CUDA and Vulkan.</p>- <ClipSecondsInput key={`${selected.id}-start`} label="Clip start (seconds)" min={0} value={selected.video?.startSeconds ?? 0}- onChange={(startSeconds) => update(selected.id, { video: { durationSeconds: 2, includeAudio: true, ...selected.video, startSeconds } })} />- <ClipSecondsInput key={`${selected.id}-duration`} label="Clip duration (seconds)" min={2} max={15} value={selected.video?.durationSeconds ?? 2}- onChange={(durationSeconds) => update(selected.id, { video: { startSeconds: 0, includeAudio: true, ...selected.video, durationSeconds } })} />- <label><input type="checkbox" checked={selected.video?.includeAudio ?? true}- onChange={(event) => update(selected.id, { video: { startSeconds: 0, durationSeconds: 2, ...selected.video, includeAudio: event.target.checked } })} /> Include sound</label>+ <ReferenceVideo key={`${selected.id}:${selected.sourcePath ?? selected.relativePath}`} folderPath={folderPath} reference={selected}+ onChange={(video) => update(selected.id, { video })} />+ <p>Long videos are welcome. Only the selected 2–15 seconds are used. Up to 3 references can total 15 seconds per generation.</p><button className="secondary-button" onClick={() => update(selected.id, { kind: "text", sourcePath: null, relativePath: null, video: undefined })}><Trash2 size={16} /> Remove video</button></section>}{(selected.kind !== "video" || showImages || hasRefmods) && <div className={`reference-detail-art${showImages || selected.iconRelativePath || (!hasRefmods && selectedPresetIcon) ? " reference-detail-art--photo" : " reference-detail-art--text"}${(!hasRefmods && selectedPresetIcon) || selected.iconRelativePath ? " reference-detail-art--preset" : ""}`}>diff --git a/src/components/workspace/ReferencesView.video.test.tsx b/src/components/workspace/ReferencesView.video.test.tsxindex 1088270..07c4cba 100644--- a/src/components/workspace/ReferencesView.video.test.tsx+++ b/src/components/workspace/ReferencesView.video.test.tsx@@ -3,16 +3,20 @@ import "@testing-library/jest-dom/vitest";import { cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";import { invoke } from "@tauri-apps/api/core";import { useState } from "react";-import { afterEach, expect, it, vi } from "vitest";+import { afterEach, beforeEach, expect, it, vi } from "vitest";import { createProjectConfig, parseProjectConfig } from "../../lib/project";import { inspectReferenceVideo } from "../../lib/referenceVideo";import { ReferencesView } from "./ReferencesView";vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() }));vi.mock("../../lib/referenceVideo", () => ({ inspectReferenceVideo: vi.fn() }));-vi.mock("./ReferenceVideo", () => ({ ReferenceVideo: () => <div>Video preview</div> }));+vi.mock("../../lib/persistence", async (importOriginal) => ({ ...await importOriginal<typeof import("../../lib/persistence")>(), readMediaFileUrl: vi.fn(async () => "blob:reference") }));vi.mock("./MediaThumbnail", () => ({ MediaThumbnail: ({ asset, posterTimeSeconds }: { asset: { sourcePath: string; name: string }; posterTimeSeconds: number }) => <img alt={`Frame from ${asset.name}`} src={`test:${asset.sourcePath}:${posterTimeSeconds}`} /> }));-afterEach(() => { cleanup(); vi.resetAllMocks(); delete (window as unknown as Record<string, unknown>).__TAURI_INTERNALS__; });+beforeEach(() => {+ vi.spyOn(HTMLMediaElement.prototype, "pause").mockImplementation(() => {});+ URL.revokeObjectURL = vi.fn();+});+afterEach(() => { cleanup(); vi.clearAllMocks(); vi.restoreAllMocks(); delete (window as unknown as Record<string, unknown>).__TAURI_INTERNALS__; });it("attaches mixed files through Add file and renders an icon with an enabled refmod", async () => {Object.assign(window, { __TAURI_INTERNALS__: {} });@@ -99,13 +103,15 @@ it("imports a video, edits its saved trim and soundtrack, then removes its attacfireEvent.click(within(picker).getByRole("button", { name: "New" }));expect(latest.references[0]).toMatchObject({ name: "Uncategorized", kind: "text", intendedUse: [] });fireEvent.click(screen.getByRole("button", { name: "Add file" }));- await screen.findByText("Video preview");+ const preview = await screen.findByLabelText("Uncategorized video preview");+ Object.defineProperty(preview, "duration", { value: 10, configurable: true });+ fireEvent.loadedMetadata(preview);expect(invoke).toHaveBeenCalledWith("choose_reference_files", { folderPath: "C:/project" });expect(screen.getByRole("img", { name: "Frame from Uncategorized" })).toHaveAttribute("src", "test:C:/motion.mp4:0");- fireEvent.change(screen.getByLabelText("Clip start (seconds)"), { target: { value: "2" } });- fireEvent.blur(screen.getByLabelText("Clip start (seconds)"));fireEvent.change(screen.getByLabelText("Clip duration (seconds)"), { target: { value: "4" } });fireEvent.blur(screen.getByLabelText("Clip duration (seconds)"));+ fireEvent.change(screen.getByLabelText("Clip start (seconds)"), { target: { value: "2" } });+ fireEvent.blur(screen.getByLabelText("Clip start (seconds)"));fireEvent.click(screen.getByLabelText("Include sound"));expect(parseProjectConfig(JSON.parse(JSON.stringify(latest))).references[0]).toMatchObject({name: "Uncategorized", intendedUse: [], kind: "video", sourcePath: "C:/motion.mp4", video: { startSeconds: 2, durationSeconds: 4, includeAudio: false },diff --git a/src/lib/referenceVideo.test.ts b/src/lib/referenceVideo.test.tsindex 3766958..248c8e5 100644--- a/src/lib/referenceVideo.test.ts+++ b/src/lib/referenceVideo.test.ts@@ -108,6 +108,23 @@ it("uploads the trimmed interval at clip-relative times and closes every frame",expect(close).toHaveBeenCalledOnce();});+it("prepares only a late selection from a long source", async () => {+ const { frames } = decoderMocks();+ const long = { ...source, durationSeconds: 0, samples: Array.from({ length: 60 }, (_, index) => ({+ timestampUs: index * 1_000_000, durationUs: 1_000_000, key: index % 5 === 0, data: new Uint8Array(1),+ })) };+ vi.mocked(demux).mockResolvedValue(long);+ const config = createProjectConfig({ name: "Long reference", prompt: "", aspectRatio: "16:9", resolution: "1080p", targetDurationSeconds: 20 });+ await prepareReferenceVideos("C:/project", {+ jobId: "job", prompt: "", frames: 120, steps: 4, seed: 1, canvasWidth: 736, canvasHeight: 416, referencePaths: [],+ referenceVideos: [{ ...input, startSeconds: 45, durationSeconds: 15 }],+ }, config, () => false, () => {});+ expect(invoke).toHaveBeenCalledWith("create_reference_video", { durationSeconds: 15, config });+ const calls = vi.mocked(invoke).mock.calls.filter(([command]) => command === "append_reference_video");+ expect(calls.map((call) => Number((call[2]?.headers as Record<string, string>)["x-reference-time"]))).toEqual(Array.from({ length: 15 }, (_, i) => i));+ expect(frames.every((frame) => frame.close.mock.calls.length === 1)).toBe(true);+});+it("releases native inputs and decoder frames after an upload failure", async () => {const { frames, close } = decoderMocks();vi.mocked(demux).mockResolvedValue(source);diff --git a/src/styles/references.css b/src/styles/references.cssindex 07af5e2..a10afff 100644--- a/src/styles/references.css+++ b/src/styles/references.css@@ -607,6 +607,27 @@border-radius: var(--radius-sm);}+.reference-trim { display: grid; gap: var(--space-2); min-width: 0; }+.reference-trim__summary { display: flex; flex-direction: column; gap: 4px; font-variant-numeric: tabular-nums; }+.reference-trim__summary span, .reference-trim__ticks { color: var(--text-muted); font-size: var(--text-xs); }+.reference-video .reference-trim__overview { display: grid; gap: 4px; }+.reference-trim__overview input { width: 100%; accent-color: var(--accent); }+.reference-trim__ticks { display: flex; justify-content: space-between; font-variant-numeric: tabular-nums; }+.reference-trim__bar { position: relative; height: 40px; margin: 8px 7px 0; border-radius: var(--radius-sm); background: var(--panel-3); touch-action: none; user-select: none; cursor: pointer; }+.reference-trim__selection { position: absolute; top: 0; bottom: 0; display: flex; border: 2px solid var(--accent); border-radius: var(--radius-sm); background: color-mix(in srgb, var(--accent) 20%, var(--panel-2)); }+.reference-trim__selection button { touch-action: none; border: 0; padding: 0; }+.reference-trim__handle { position: absolute; top: -2px; bottom: -2px; width: 14px; border-radius: 3px; background: var(--accent); cursor: ew-resize; z-index: 1; }+.reference-trim__handle:first-child { left: -7px; }+.reference-trim__handle:last-child { right: -7px; }+.reference-trim__handle::after { content: ""; position: absolute; left: 6px; top: 11px; width: 2px; height: 14px; background: var(--panel-2); }+.reference-trim__move { width: 100%; min-width: 0; overflow: hidden; font-size: var(--text-xs); color: var(--text-primary); background: transparent; cursor: grab; }+.reference-trim__move:active { cursor: grabbing; }+.reference-trim__selection button:focus-visible { outline: 2px solid var(--text-primary); outline-offset: 3px; }+.reference-trim__numbers { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2); }+.reference-trim__actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }+.reference-video .reference-trim__numbers label { display: grid; gap: 4px; min-width: 0; }+.reference-video .reference-trim__numbers input { width: 100%; min-width: 0; }+.reference-video p {color: var(--text-muted);font-size: var(--text-xs);