Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

19.1 KB
Raw Blame
// @vitest-environment jsdom
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { toastText } from "@/i18n/toastText";
import type { AxcutDocument, AxcutTranscript } from "../schema";
import { answerMcpHostRequest } from "./mcpDocumentHost";
import { useProjectStore } from "./projectStore";
import { useTranscriptionStore, whenTranscriptionIdle } from "./transcriptionStore";

const bridgeMocks = vi.hoisted(() => ({
	save: vi.fn(),
}));

const transcribeMocks = vi.hoisted(() => ({
	transcribeAsset: vi.fn(),
}));

const toastMocks = vi.hoisted(() => ({
	success: vi.fn(),
	error: vi.fn(),
	info: vi.fn(),
}));

vi.mock("@/native/client", () => ({
	nativeBridgeClient: { aiEdition: { save: bridgeMocks.save } },
}));

vi.mock("../document/transcribe", async (importOriginal) => {
	const actual = await importOriginal<typeof import("../document/transcribe")>();
	return { ...actual, transcribeAsset: transcribeMocks.transcribeAsset };
});

vi.mock("sonner", () => ({
	toast: { success: toastMocks.success, error: toastMocks.error, info: toastMocks.info },
}));

function asset(id: string, extra: Record<string, unknown> = {}) {
	return {
		id,
		kind: "video" as const,
		label: `${id}.mp4`,
		originalPath: `/tmp/${id}.mp4`,
		cameraTrack: null,
		...extra,
	};
}

function makeDoc(assetIds: string[], projectId = "proj_1"): AxcutDocument {
	return {
		schemaVersion: 9,
		project: {
			id: projectId,
			title: "Test",
			createdAt: "2026-06-25T10:00:00.000Z",
			updatedAt: "2026-06-25T10:00:00.000Z",
			primaryAssetId: assetIds[0],
		},
		assets: assetIds.map((id) => asset(id)),
		transcript: null,
		transcripts: [],
		timeline: {
			clips: [],
			gaps: [],
			trimRanges: [],
			muteRanges: [],
			speedRanges: [],
			captionRanges: [],
		},
		annotations: [],
		zoomRanges: [],
		legacyEditor: null,
	} as unknown as AxcutDocument;
}

function transcriptFor(assetId: string): AxcutTranscript {
	return {
		assetId,
		language: "en",
		segments: [
			{ id: "seg_1", kind: "speech", startSec: 0, endSec: 1, text: "hello", wordIds: ["word_1"] },
		],
		words: [{ id: "word_1", segmentId: "seg_1", startSec: 0, endSec: 1, text: "hello" }],
	};
}

/** A promise plus a 0-arg release, so a mocked run can be held open mid-flight. */
function deferred(): { promise: Promise<void>; release: () => void } {
	let release: () => void = () => {
		// Replaced synchronously by the executor below, before this can be called.
	};
	const promise = new Promise<void>((resolve) => {
		release = () => resolve();
	});
	return { promise, release: () => release() };
}

/** Loads a document into the project store the way `loadProject` would. */
function loadDocument(document: AxcutDocument) {
	useProjectStore.setState({
		projectId: document.project.id,
		document,
		status: "ready",
		error: null,
		dirty: false,
	});
}

describe("useTranscriptionStore", () => {
	beforeEach(() => {
		useTranscriptionStore.getState().reset();
		useProjectStore.getState().clear();
		bridgeMocks.save.mockReset();
		// The bridge echoes back whatever it was handed, like a successful save.
		bridgeMocks.save.mockImplementation(async (document: AxcutDocument) => ({
			success: true,
			document,
		}));
		transcribeMocks.transcribeAsset.mockReset();
		toastMocks.success.mockReset();
		toastMocks.error.mockReset();
		toastMocks.info.mockReset();
		// biome-ignore lint/suspicious/noExplicitAny: test-only stub of the preload bridge
		(window as any).electronAPI = { stt: { transcribe: vi.fn() } };
	});

	afterEach(() => {
		vi.clearAllMocks();
		// biome-ignore lint/suspicious/noExplicitAny: test-only stub of the preload bridge
		delete (window as any).electronAPI;
	});

	it("transcribes every asset that has no transcript, one at a time", async () => {
		let inFlight = 0;
		let maxInFlight = 0;
		transcribeMocks.transcribeAsset.mockImplementation(
			async (_doc: AxcutDocument, assetId: string) => {
				inFlight += 1;
				maxInFlight = Math.max(maxInFlight, inFlight);
				await Promise.resolve();
				inFlight -= 1;
				return transcriptFor(assetId);
			},
		);
		loadDocument(makeDoc(["asset_1", "asset_2"]));

		useTranscriptionStore.getState().sync(useProjectStore.getState().document);
		await whenTranscriptionIdle();

		expect(maxInFlight).toBe(1);
		expect(transcribeMocks.transcribeAsset).toHaveBeenCalledTimes(2);
		expect(
			useProjectStore
				.getState()
				.document?.transcripts.map((t) => t.assetId)
				.sort(),
		).toEqual(["asset_1", "asset_2"]);
		expect(useTranscriptionStore.getState().jobs).toEqual({});
		// The background pass stays quiet on success.
		expect(toastMocks.success).not.toHaveBeenCalled();
	});

	it("does not re-enqueue an asset whose transcript it just wrote", async () => {
		transcribeMocks.transcribeAsset.mockImplementation(
			async (_doc: AxcutDocument, assetId: string) => transcriptFor(assetId),
		);
		loadDocument(makeDoc(["asset_1"]));

		const { sync } = useTranscriptionStore.getState();
		sync(useProjectStore.getState().document);
		await whenTranscriptionIdle();

		// Every document change re-runs sync in the shell — this is the loop guard.
		sync(useProjectStore.getState().document);
		sync(useProjectStore.getState().document);
		await whenTranscriptionIdle();

		expect(transcribeMocks.transcribeAsset).toHaveBeenCalledTimes(1);
		expect(useTranscriptionStore.getState().jobs).toEqual({});
	});

	it("remembers a no-audio verdict on the asset and never retries it by itself", async () => {
		transcribeMocks.transcribeAsset.mockRejectedValue(
			new Error("No audio track found in this video."),
		);
		loadDocument(makeDoc(["asset_1"]));

		const { sync } = useTranscriptionStore.getState();
		sync(useProjectStore.getState().document);
		await whenTranscriptionIdle();

		const job = useTranscriptionStore.getState().jobs.asset_1;
		expect(job?.status).toBe("failed");
		expect(job?.failure?.kind).toBe("no-audio");
		expect(useProjectStore.getState().document?.assets[0].transcriptionFailure?.kind).toBe(
			"no-audio",
		);
		// Silence is an expected outcome, not an incident.
		expect(toastMocks.error).not.toHaveBeenCalled();

		sync(useProjectStore.getState().document);
		await whenTranscriptionIdle();
		expect(transcribeMocks.transcribeAsset).toHaveBeenCalledTimes(1);
	});

	it("skips an asset that already carries a persisted failure on a fresh load", async () => {
		const doc = makeDoc(["asset_1"]);
		loadDocument({
			...doc,
			assets: [
				asset("asset_1", { transcriptionFailure: { kind: "no-audio", message: "silent" } }),
			] as AxcutDocument["assets"],
		});

		useTranscriptionStore.getState().sync(useProjectStore.getState().document);
		await whenTranscriptionIdle();

		expect(transcribeMocks.transcribeAsset).not.toHaveBeenCalled();
		expect(useTranscriptionStore.getState().jobs).toEqual({});
	});

	it("keeps a transient failure in memory only, and toasts it", async () => {
		transcribeMocks.transcribeAsset.mockRejectedValue(new Error("whisper-server exited"));
		loadDocument(makeDoc(["asset_1"]));

		useTranscriptionStore.getState().sync(useProjectStore.getState().document);
		await whenTranscriptionIdle();

		expect(useTranscriptionStore.getState().jobs.asset_1?.failure?.kind).toBe("error");
		expect(useProjectStore.getState().document?.assets[0].transcriptionFailure).toBeUndefined();
		expect(toastMocks.error).toHaveBeenCalledTimes(1);
	});

	it("stops the queue on an engine failure instead of failing each asset in turn", async () => {
		// The model download died / whisper-server didn't come up: that verdict is
		// about the engine, so the remaining assets inherit it rather than each
		// spending a full retry budget and stacking an identical toast.
		transcribeMocks.transcribeAsset.mockRejectedValue(new Error("whisper-server exited"));
		loadDocument(makeDoc(["asset_1", "asset_2", "asset_3"]));

		useTranscriptionStore.getState().sync(useProjectStore.getState().document);
		await whenTranscriptionIdle();

		expect(transcribeMocks.transcribeAsset).toHaveBeenCalledTimes(1);
		const jobs = useTranscriptionStore.getState().jobs;
		expect(Object.values(jobs).map((j) => j.status)).toEqual(["failed", "failed", "failed"]);
		expect(jobs.asset_3?.failure?.message).toBe("whisper-server exited");
		expect(toastMocks.error).toHaveBeenCalledTimes(1);
	});

	// The verdict is about the FILE, so a run the user asked for by hand answers
	// it as information rather than as a broken job — issue #628 showed a red
	// "Transcription failed" carrying ffmpeg's stderr on a muted screen recording.
	it("answers a hand-asked run on a silent media without calling it a failure", async () => {
		transcribeMocks.transcribeAsset.mockRejectedValue(
			new Error("No decodable audio in /rec.mp4: Output file #0 does not contain any stream"),
		);
		loadDocument(makeDoc(["asset_1"]));

		// The background pass discovers the silence first, and says nothing about it.
		useTranscriptionStore.getState().sync(useProjectStore.getState().document);
		await whenTranscriptionIdle();
		expect(useTranscriptionStore.getState().jobs.asset_1?.failure?.kind).toBe("no-audio");
		expect(toastMocks.info).not.toHaveBeenCalled();

		// Asking by hand deserves an answer — an informational one. `request`
		// resolves as soon as the job settles, which is before the run has finished
		// writing the verdict and reporting it, so wait for the queue too.
		await useTranscriptionStore.getState().request("asset_1", "auto");
		await whenTranscriptionIdle();

		expect(toastMocks.error).not.toHaveBeenCalled();
		expect(toastMocks.info).toHaveBeenCalledTimes(1);
		// The COPY, not just the count: the point of the toast is that it says the
		// file has no audio. Resolved through the same helper the store uses, so a
		// reworded string stays a translation change rather than a test failure.
		expect(toastMocks.info).toHaveBeenCalledWith(
			toastText("editor", "mediaStage.noAudioTrackHint"),
		);
	});

	it("request() re-runs a failed asset and clears the remembered verdict", async () => {
		transcribeMocks.transcribeAsset.mockRejectedValueOnce(
			new Error("No audio track found in this video."),
		);
		transcribeMocks.transcribeAsset.mockImplementation(
			async (_doc: AxcutDocument, assetId: string) => transcriptFor(assetId),
		);
		loadDocument(makeDoc(["asset_1"]));

		useTranscriptionStore.getState().sync(useProjectStore.getState().document);
		await whenTranscriptionIdle();
		expect(useProjectStore.getState().document?.assets[0].transcriptionFailure?.kind).toBe(
			"no-audio",
		);

		await useTranscriptionStore.getState().request("asset_1", "fr");

		expect(transcribeMocks.transcribeAsset).toHaveBeenLastCalledWith(
			expect.anything(),
			"asset_1",
			expect.objectContaining({ language: "fr" }),
		);
		expect(useProjectStore.getState().document?.transcripts).toHaveLength(1);
		expect(useProjectStore.getState().document?.assets[0].transcriptionFailure).toBeNull();
		expect(useTranscriptionStore.getState().jobs).toEqual({});
		// A run the user asked for reports back.
		expect(toastMocks.success).toHaveBeenCalledTimes(1);
	});

	it("drops the queue when another project is loaded", async () => {
		transcribeMocks.transcribeAsset.mockImplementation(
			async (_doc: AxcutDocument, assetId: string) => transcriptFor(assetId),
		);
		loadDocument(makeDoc(["asset_1"]));
		useTranscriptionStore.getState().sync(useProjectStore.getState().document);
		await whenTranscriptionIdle();

		const other = makeDoc(["asset_9"], "proj_2");
		loadDocument(other);
		useTranscriptionStore.getState().sync(other);
		expect(useTranscriptionStore.getState().projectId).toBe("proj_2");
		await whenTranscriptionIdle();

		expect(useProjectStore.getState().document?.transcripts.map((t) => t.assetId)).toEqual([
			"asset_9",
		]);
	});

	it("forgets a job when its asset leaves the document", async () => {
		transcribeMocks.transcribeAsset.mockRejectedValue(new Error("whisper-server exited"));
		loadDocument(makeDoc(["asset_1", "asset_2"]));
		useTranscriptionStore.getState().sync(useProjectStore.getState().document);
		await whenTranscriptionIdle();
		expect(Object.keys(useTranscriptionStore.getState().jobs)).toEqual(["asset_1", "asset_2"]);

		const doc = useProjectStore.getState().document as AxcutDocument;
		const pruned = { ...doc, assets: doc.assets.filter((a) => a.id === "asset_1") };
		loadDocument(pruned);
		useTranscriptionStore.getState().sync(pruned);

		expect(Object.keys(useTranscriptionStore.getState().jobs)).toEqual(["asset_1"]);
	});

	it("runs no background pass without a local STT engine", async () => {
		// biome-ignore lint/suspicious/noExplicitAny: test-only stub of the preload bridge
		delete (window as any).electronAPI;
		loadDocument(makeDoc(["asset_1"]));

		useTranscriptionStore.getState().sync(useProjectStore.getState().document);
		await whenTranscriptionIdle();

		expect(transcribeMocks.transcribeAsset).not.toHaveBeenCalled();
		expect(useTranscriptionStore.getState().jobs).toEqual({});
	});

	it("lets a manual request supersede the background run of the same asset", async () => {
		// The background pass is mid-run on asset_1 (auto language) when the user
		// asks for French from the media card. The outgoing run must neither win
		// the race nor delete the request that replaced it.
		const languages: string[] = [];
		const firstRun = deferred();
		transcribeMocks.transcribeAsset.mockImplementation(
			async (
				_doc: AxcutDocument,
				assetId: string,
				options: { language?: string; signal?: AbortSignal },
			) => {
				languages.push(options.language ?? "auto");
				if (languages.length === 1) {
					await firstRun.promise;
					throw new DOMException("Aborted", "AbortError");
				}
				return { ...transcriptFor(assetId), language: options.language ?? "auto" };
			},
		);
		loadDocument(makeDoc(["asset_1"]));
		useTranscriptionStore.getState().sync(useProjectStore.getState().document);
		await Promise.resolve();
		expect(useTranscriptionStore.getState().jobs.asset_1?.status).toBe("running");

		const requested = useTranscriptionStore.getState().request("asset_1", "fr");
		expect(useTranscriptionStore.getState().jobs.asset_1?.status).toBe("queued");
		firstRun.release();
		await requested;
		await whenTranscriptionIdle();

		expect(languages).toEqual(["auto", "fr"]);
		expect(useProjectStore.getState().document?.transcripts).toHaveLength(1);
		expect(useProjectStore.getState().document?.transcripts[0].language).toBe("fr");
		expect(useTranscriptionStore.getState().jobs).toEqual({});
	});

	it("requestTimelineTranscripts covers the timeline's media, skipping the silent ones", async () => {
		// The pane button used to target `primaryAssetId` only — which in a
		// recording project is the (often silent) screen capture, leaving the
		// talking clip next to it untranscribable from there.
		// The background pass is off here so the assertions see only what the
		// button itself asked for.
		// biome-ignore lint/suspicious/noExplicitAny: test-only stub of the preload bridge
		delete (window as any).electronAPI;
		transcribeMocks.transcribeAsset.mockImplementation(
			async (_doc: AxcutDocument, assetId: string) => transcriptFor(assetId),
		);
		const doc = makeDoc(["silent", "voice", "offTimeline"]);
		loadDocument({
			...doc,
			assets: [
				asset("silent", { transcriptionFailure: { kind: "no-audio", message: "silent" } }),
				asset("voice"),
				asset("offTimeline"),
			],
			timeline: {
				...doc.timeline,
				clips: ["silent", "voice"].map((assetId, i) => ({
					id: `clip_${i}`,
					assetId,
					sourceStartSec: 0,
					sourceEndSec: 10,
					timelineStartSec: i * 10,
					timelineEndSec: i * 10 + 10,
					wordRefs: [],
					origin: "user",
					reason: "",
				})),
			},
		} as unknown as AxcutDocument);

		await useTranscriptionStore.getState().requestTimelineTranscripts();

		expect(transcribeMocks.transcribeAsset.mock.calls.map((c) => c[1])).toEqual(["voice"]);
		expect(useProjectStore.getState().document?.transcripts.map((t) => t.assetId)).toEqual([
			"voice",
		]);
	});

	it("waits for the background run instead of transcribing the same asset twice", async () => {
		const firstRun = deferred();
		transcribeMocks.transcribeAsset.mockImplementation(
			async (_doc: AxcutDocument, assetId: string) => {
				await firstRun.promise;
				return transcriptFor(assetId);
			},
		);
		loadDocument(makeDoc(["asset_1"]));
		useTranscriptionStore.getState().sync(useProjectStore.getState().document);
		await Promise.resolve();
		expect(useTranscriptionStore.getState().jobs.asset_1?.status).toBe("running");

		const requested = useTranscriptionStore.getState().requestTimelineTranscripts();
		firstRun.release();
		await requested;

		expect(transcribeMocks.transcribeAsset).toHaveBeenCalledTimes(1);
		expect(useProjectStore.getState().document?.transcripts).toHaveLength(1);
	});

	it("ensureTranscript waits for the editor's own run, and the MCP host reports it", async () => {
		const firstRun = deferred();
		transcribeMocks.transcribeAsset.mockImplementation(
			async (_doc: AxcutDocument, assetId: string) => {
				await firstRun.promise;
				return transcriptFor(assetId);
			},
		);
		loadDocument(makeDoc(["asset_1"]));
		useTranscriptionStore.getState().sync(useProjectStore.getState().document);
		await Promise.resolve();
		expect(useTranscriptionStore.getState().jobs.asset_1?.status).toBe("running");

		const answered = answerMcpHostRequest({
			requestId: "r",
			op: "transcribe",
			projectId: "proj_1",
			assetId: "asset_1",
		});
		firstRun.release();

		await expect(answered).resolves.toEqual({ status: "transcribed", language: "en", words: 1 });
		expect(transcribeMocks.transcribeAsset).toHaveBeenCalledTimes(1);
	});

	it("ensureTranscript keeps a transcript unless a language is asked for", async () => {
		transcribeMocks.transcribeAsset.mockImplementation(
			async (_doc: AxcutDocument, assetId: string) => transcriptFor(assetId),
		);
		loadDocument({ ...makeDoc(["asset_1"]), transcripts: [transcriptFor("asset_1")] });

		await useTranscriptionStore.getState().ensureTranscript("asset_1");
		expect(transcribeMocks.transcribeAsset).not.toHaveBeenCalled();

		await useTranscriptionStore.getState().ensureTranscript("asset_1", "fr");
		expect(transcribeMocks.transcribeAsset).toHaveBeenCalledWith(
			expect.anything(),
			"asset_1",
			expect.objectContaining({ language: "fr" }),
		);
	});

	it("the MCP host reports the failure of the run it asked for", async () => {
		transcribeMocks.transcribeAsset.mockRejectedValue(new Error("No decodable audio in x"));
		loadDocument(makeDoc(["asset_1"]));

		await expect(
			answerMcpHostRequest({
				requestId: "r",
				op: "transcribe",
				projectId: "proj_1",
				assetId: "asset_1",
			}),
		).resolves.toMatchObject({ status: "failed", failure: { kind: "no-audio" } });
		await expect(
			answerMcpHostRequest({
				requestId: "r",
				op: "transcribe",
				projectId: "another",
				assetId: "asset_1",
			}),
		).resolves.toEqual({ status: "unavailable" });
	});

	it("still honours a manual request without the auto pass", async () => {
		// biome-ignore lint/suspicious/noExplicitAny: test-only stub of the preload bridge
		delete (window as any).electronAPI;
		transcribeMocks.transcribeAsset.mockImplementation(
			async (_doc: AxcutDocument, assetId: string) => transcriptFor(assetId),
		);
		loadDocument(makeDoc(["asset_1"]));

		await useTranscriptionStore.getState().request("asset_1");

		expect(useProjectStore.getState().document?.transcripts).toHaveLength(1);
	});
});