Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

fix(export): honour a cancel and never carry voice-over over

davbio0470c8da3539
diff --git a/src/core/export/__tests__/options.test.ts b/src/core/export/__tests__/options.test.ts
index 149833c..38296c1 100644
--- a/src/core/export/__tests__/options.test.ts
+++ b/src/core/export/__tests__/options.test.ts
@@ -83,6 +83,15 @@ describe('export option persistence', () => {
expect(await loadExportOptions()).toEqual(options);
});
+ it('never brings voice-over back on from storage, since it costs money', async () => {
+ await fakeBrowser.storage.local.set({ exportOptions: { ...DEFAULT_EXPORT_OPTIONS, voiceover: true } });
+ expect((await loadExportOptions()).voiceover).toBe(false);
+
+ await saveExportOptions({ ...DEFAULT_EXPORT_OPTIONS, voiceover: true });
+ const stored = await fakeBrowser.storage.local.get('exportOptions');
+ expect((stored.exportOptions as { voiceover: boolean }).voiceover).toBe(false);
+ });
+
it('survives a corrupted stored value', async () => {
await fakeBrowser.storage.local.set({ exportOptions: 'corrupted' });
expect(await loadExportOptions()).toEqual(DEFAULT_EXPORT_OPTIONS);
diff --git a/src/core/export/__tests__/video-export-encode.test.ts b/src/core/export/__tests__/video-export-encode.test.ts
index cd47dd8..71293d4 100644
--- a/src/core/export/__tests__/video-export-encode.test.ts
+++ b/src/core/export/__tests__/video-export-encode.test.ts
@@ -498,6 +498,32 @@ describe('exportGuideAsVideo voiceover', () => {
expect(rec.added).toHaveLength(157 * 2 - 10);
});
+ it('stays silent for a caller that passes no options, whatever was saved', async () => {
+ voice.settings = { ...voice.settings, exportOptions: { ...DEFAULT_EXPORT_OPTIONS, voiceover: true, cover: false } };
+ voice.clips = new Map([[0, clip(9)]]);
+ const steps = [makeStep(0), makeStep(1)];
+ await exportGuideAsVideo(guide, steps, shotsFor(steps));
+
+ expect(renderVoiceover).not.toHaveBeenCalled();
+ expect(rec.audioTracks).toBe(0);
+ });
+
+ it('stops a narrated export cancelled while the voice track is being written', async () => {
+ const controller = new AbortController();
+ voice.clips = new Map([[0, clip(2)]]);
+ const steps = [makeStep(0), makeStep(1)];
+
+ await expect(
+ exportGuideAsVideo(guide, steps, shotsFor(steps), opts({ cover: false, voiceover: true }), {
+ signal: controller.signal,
+ onMuxProgress: () => controller.abort(),
+ }),
+ ).rejects.toMatchObject({ name: 'AbortError' });
+
+ expect(rec.cancelled).toBe(1);
+ expect(rec.finalized).toBe(0);
+ });
+
it('holds a narrated step until its clip ends', async () => {
voice.clips = new Map([[0, clip(9)]]);
const steps = [makeStep(0), makeStep(1)];
diff --git a/src/core/export/options.ts b/src/core/export/options.ts
index a194ff1..33aff88 100644
--- a/src/core/export/options.ts
+++ b/src/core/export/options.ts
@@ -85,9 +85,9 @@ export function normaliseExportOptions(value: unknown): ExportOptions {
export async function loadExportOptions(): Promise<ExportOptions> {
const stored = await localStorage.get(['exportOptions']);
- return normaliseExportOptions(stored.exportOptions);
+ return { ...normaliseExportOptions(stored.exportOptions), voiceover: false };
}
export async function saveExportOptions(options: ExportOptions): Promise<void> {
- await localStorage.set({ exportOptions: options });
+ await localStorage.set({ exportOptions: { ...options, voiceover: false } });
}
diff --git a/src/core/export/video-export.ts b/src/core/export/video-export.ts
index 4165e7a..c2a3ded 100644
--- a/src/core/export/video-export.ts
+++ b/src/core/export/video-export.ts
@@ -1048,9 +1048,11 @@ export async function exportGuideAsVideo(
(buffer) => audio.add(buffer),
undefined,
controls.onMuxProgress,
+ controls.signal,
);
}
+ if (controls.signal?.aborted) throw new DOMException('Video export was aborted', 'AbortError');
await output.finalize();
} catch (error) {
await output.cancel();
diff --git a/src/core/export/voiceover/__tests__/audio.test.ts b/src/core/export/voiceover/__tests__/audio.test.ts
index d17def5..78788ea 100644
--- a/src/core/export/voiceover/__tests__/audio.test.ts
+++ b/src/core/export/voiceover/__tests__/audio.test.ts
@@ -72,6 +72,25 @@ describe('writeVoiceTrack', () => {
]);
});
+ it('stops writing as soon as the export is cancelled', async () => {
+ const controller = new AbortController();
+ let added = 0;
+ const writing = writeVoiceTrack(
+ [clip(1, 50)],
+ 3,
+ async () => {
+ added += 1;
+ controller.abort();
+ },
+ RATE,
+ undefined,
+ controller.signal,
+ );
+
+ await expect(writing).rejects.toMatchObject({ name: 'AbortError' });
+ expect(added).toBe(1);
+ });
+
it('ends on a full count, so the bar lands on 100%', async () => {
const seen: [number, number][] = [];
await writeVoiceTrack(
diff --git a/src/core/export/voiceover/audio.ts b/src/core/export/voiceover/audio.ts
index 20bf39c..4eec644 100644
--- a/src/core/export/voiceover/audio.ts
+++ b/src/core/export/voiceover/audio.ts
@@ -69,10 +69,12 @@ export async function writeVoiceTrack(
add: (buffer: AudioBuffer) => Promise<void>,
sampleRate = VOICE_SAMPLE_RATE,
onProgress?: (done: number, total: number) => void,
+ signal?: AbortSignal,
): Promise<void> {
const pieces = voiceTrackPieces(clips, totalSec, sampleRate);
let written = 0;
for (const piece of pieces) {
+ if (signal?.aborted) throw new DOMException('Voiceover was aborted', 'AbortError');
await add(piece.kind === 'clip' ? piece.buffer : silence(piece.samples, sampleRate));
onProgress?.(++written, pieces.length);
}
diff --git a/src/ui/fullview/ExportPreviewModal.tsx b/src/ui/fullview/ExportPreviewModal.tsx
index 52376e9..f278614 100644
--- a/src/ui/fullview/ExportPreviewModal.tsx
+++ b/src/ui/fullview/ExportPreviewModal.tsx
@@ -74,6 +74,8 @@ export default function ExportPreviewModal({ open, onOpenChange, guide, steps, s
const [voiceProgress, setVoiceProgress] = useState<{ done: number; total: number } | null>(null);
const [, setNarratedSeconds] = useState<number | null>(null);
const [voiceoverError, setVoiceoverError] = useState<VoiceoverSkip | null>(null);
+ const [downloadVoiceoverError, setDownloadVoiceoverError] = useState<VoiceoverSkip | null>(null);
+ const shownVoiceoverError = downloadVoiceoverError ?? voiceoverError;
useEffect(() => {
if (open) loadExportOptions().then(setOptions);
@@ -113,7 +115,9 @@ export default function ExportPreviewModal({ open, onOpenChange, guide, steps, s
);
useEffect(() => {
- if (!open) setVideoRequested(false);
+ if (open) return;
+ setVideoRequested(false);
+ setOptions((current) => (current.voiceover ? { ...current, voiceover: false } : current));
}, [open]);
useEffect(() => {
@@ -141,6 +145,7 @@ export default function ExportPreviewModal({ open, onOpenChange, guide, steps, s
setVoiceProgress(null);
setNarratedSeconds(null);
setVoiceoverError(null);
+ setDownloadVoiceoverError(null);
const timer = setTimeout(async () => {
let allClipsLanded = false;
try {
@@ -239,11 +244,13 @@ export default function ExportPreviewModal({ open, onOpenChange, guide, steps, s
signal: controller.signal,
onProgress: (encoded, frames) => setDownloadProgress(frames > 0 ? encoded / frames : 0),
});
+ if (controller.signal.aborted) return;
downloadBlob(blob, safeFilename(guide.title, extension));
} else if (format === 'video') {
const controller = new AbortController();
downloadAbort.current = controller;
setDownloadProgress(0);
+ setDownloadVoiceoverError(null);
const { exportGuideAsVideo } = await import('@/core/export/video-export');
let allClipsLanded = false;
const {
@@ -268,7 +275,8 @@ export default function ExportPreviewModal({ open, onOpenChange, guide, steps, s
onMuxProgress: (done, total) => setDownloadProgress(muxProgress(done, total, muxShare)),
},
);
- setVoiceoverError(failed ?? null);
+ if (controller.signal.aborted) return;
+ setDownloadVoiceoverError(failed ?? null);
downloadBlob(blob, safeFilename(guide.title, extension));
} else if (format === 'bundle') {
const { exportGuideAsBundle } = await import('@/core/transfer/bundle');
@@ -434,12 +442,12 @@ export default function ExportPreviewModal({ open, onOpenChange, guide, steps, s
</div>
)}
- {voiceover && voiceoverError && (
+ {voiceover && shownVoiceoverError && (
<div
className="mt-1.5 rounded-lg px-2.5 py-2 text-[10px] leading-snug text-destructive bg-destructive/10"
role="alert"
>
- {i18n.t(VOICEOVER_SKIP_MESSAGES[voiceoverError.reason])}
+ {i18n.t(VOICEOVER_SKIP_MESSAGES[shownVoiceoverError.reason])}
</div>
)}
</div>
diff --git a/src/ui/fullview/__tests__/ExportPreviewModal.test.tsx b/src/ui/fullview/__tests__/ExportPreviewModal.test.tsx
index 0b1489d..4ccb213 100644
--- a/src/ui/fullview/__tests__/ExportPreviewModal.test.tsx
+++ b/src/ui/fullview/__tests__/ExportPreviewModal.test.tsx
@@ -8,6 +8,12 @@ const exportGuideAsVideo = vi.hoisted(() => vi.fn());
const exportGuideAsHTML = vi.hoisted(() => vi.fn());
const canExportVideo = vi.hoisted(() => vi.fn());
const stored = vi.hoisted(() => ({ value: {} as Record<string, unknown> }));
+const downloadBlob = vi.hoisted(() => vi.fn());
+
+vi.mock('@/core/export/download', async (importOriginal) => ({
+ ...(await importOriginal<typeof import('@/core/export/download')>()),
+ downloadBlob,
+}));
vi.mock('@/lib/browser-api', () => ({
localStorage: { get: async () => stored.value, set: async () => {} },
@@ -182,6 +188,20 @@ describe('ExportPreviewModal voice-over controls', () => {
expect(silent()?.getAttribute('aria-pressed')).toBe('false');
});
+ it('is off again as soon as the panel is reopened, before any saved options load', async () => {
+ const { steps, screenshots } = makeGuideOf(3);
+ const props = { onOpenChange: () => {}, guide, steps, screenshots };
+ const { rerender } = render(<ExportPreviewModal open {...props} />);
+ await waitFor(() => expect(narrated()).not.toBeNull());
+ fireEvent.click(narrated() as HTMLElement);
+ await waitFor(() => expect(narrated()?.getAttribute('aria-pressed')).toBe('true'));
+
+ rerender(<ExportPreviewModal open={false} {...props} />);
+ rerender(<ExportPreviewModal open {...props} />);
+
+ expect(narrated()?.getAttribute('aria-pressed')).toBe('false');
+ });
+
it('cannot be narrated without a key', async () => {
stored.value = {};
renderModal(3);
@@ -308,6 +328,37 @@ describe('ExportPreviewModal video progress', () => {
expect(hooks()).not.toBe(preview);
});
+ it('keeps showing why the preview is silent after a download narrates fine', async () => {
+ exportGuideAsVideo.mockResolvedValueOnce({
+ blob: new Blob(['video']),
+ extension: 'mp4',
+ chapters: [],
+ voiceoverError: { reason: 'failed', detail: 'rejected' },
+ });
+ await openNarratedVideo();
+ await screen.findByText('exportPreview.voiceoverFailed');
+
+ exportGuideAsVideo.mockResolvedValueOnce({ blob: new Blob(['video']), extension: 'mp4', chapters: [] });
+ fireEvent.click(screen.getByRole('button', { name: 'exportPreview.download[exportMenu.video]' }));
+ await waitFor(() => expect(downloadBlob).toHaveBeenCalled());
+
+ expect(screen.getByText('exportPreview.voiceoverFailed')).not.toBeNull();
+ });
+
+ it('saves nothing when the download is cancelled', async () => {
+ renderModal(3);
+ await screen.findByRole('button', { name: 'exportPreview.download[exportMenu.video]' });
+ let finish!: (value: unknown) => void;
+ exportGuideAsVideo.mockImplementationOnce(() => new Promise((resolve) => (finish = resolve)));
+
+ fireEvent.click(screen.getByRole('button', { name: 'exportPreview.download[exportMenu.video]' }));
+ const cancel = await screen.findByRole('button', { name: /exportMenu\.cancelProgress/ });
+ fireEvent.click(cancel);
+ await act(async () => finish({ blob: new Blob(['video']), extension: 'mp4', chapters: [] }));
+
+ expect(downloadBlob).not.toHaveBeenCalled();
+ });
+
it('still reaches the end on a silent export, which writes no audio track', async () => {
renderModal(3);
fireEvent.click(await screen.findByRole('button', { name: 'exportPreview.modeVideo' }));