Skip to content

Commit 4fec86d

Browse files
committed
fix(voiceover): reject empty takes and failed saves
1 parent d36aa7d commit 4fec86d

20 files changed

Lines changed: 90 additions & 13 deletions

File tree

‎src/components/ai-edition/v4/AddAudioLayerDialog.test.tsx‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class FakeRecorder {
3232
resumeCount = 0;
3333
started = false;
3434
stopped = false;
35+
chunk = new Blob(["audio"]);
3536
ondataavailable: ((e: { data: Blob }) => void) | null = null;
3637
onstop: (() => void) | null = null;
3738
mimeType = "audio/webm";
@@ -56,7 +57,7 @@ class FakeRecorder {
5657
stop() {
5758
this.stopped = true;
5859
this.state = "inactive";
59-
this.ondataavailable?.({ data: new Blob(["audio"]) });
60+
this.ondataavailable?.({ data: this.chunk });
6061
this.onstop?.();
6162
}
6263
}
@@ -187,6 +188,59 @@ describe("AddAudioLayerDialog", () => {
187188
unmount();
188189
expect(addAudioAsset).not.toHaveBeenCalled();
189190
});
191+
192+
it("reports an empty microphone take without saving or importing it", async () => {
193+
const { props } = renderDialog();
194+
await act(async () => {
195+
fireEvent.click(screen.getByText("audio.record"));
196+
});
197+
FakeRecorder.instances[0].chunk = new Blob([]);
198+
await act(async () => {
199+
fireEvent.click(screen.getByText("audio.stop"));
200+
});
201+
expect(toast.error).toHaveBeenCalledWith("audio.emptyRecording");
202+
expect(window.electronAPI.saveRecordedVoiceover).not.toHaveBeenCalled();
203+
expect(addAudioAsset).not.toHaveBeenCalled();
204+
expect(props.onComplete).not.toHaveBeenCalled();
205+
expect(props.onRecordingStop).toHaveBeenCalledOnce();
206+
expect(stopTrack).toHaveBeenCalledOnce();
207+
});
208+
209+
it("reports a refused save without importing a temporary blob URL", async () => {
210+
const createObjectURL = vi.fn(() => "blob:unpersisted-voiceover");
211+
vi.stubGlobal(
212+
"URL",
213+
class extends URL {
214+
static createObjectURL = createObjectURL;
215+
},
216+
);
217+
vi.mocked(window.electronAPI.saveRecordedVoiceover).mockResolvedValueOnce({
218+
success: false,
219+
message: "Disk unavailable",
220+
});
221+
const { props } = renderDialog();
222+
await act(async () => {
223+
fireEvent.click(screen.getByText("audio.record"));
224+
});
225+
await act(async () => {
226+
fireEvent.click(screen.getByText("audio.stop"));
227+
});
228+
expect(toast.error).toHaveBeenCalledWith("audio.saveFailed");
229+
expect(createObjectURL).not.toHaveBeenCalled();
230+
expect(addAudioAsset).not.toHaveBeenCalled();
231+
expect(props.onComplete).not.toHaveBeenCalled();
232+
});
233+
234+
it("does not acquire the microphone when persistence is unavailable", async () => {
235+
Object.defineProperty(window, "electronAPI", { configurable: true, value: {} });
236+
renderDialog();
237+
await act(async () => {
238+
fireEvent.click(screen.getByText("audio.record"));
239+
});
240+
expect(toast.error).toHaveBeenCalledWith("audio.recordingUnavailable");
241+
expect(navigator.mediaDevices.getUserMedia).not.toHaveBeenCalled();
242+
expect(FakeRecorder.instances).toHaveLength(0);
243+
});
190244
});
191245

192246
describe("voiceover pause and resume", () => {

‎src/components/ai-edition/v4/AddAudioLayerDialog.tsx‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,12 @@ export function AddAudioLayerDialog({
225225

226226
/** Acquire the microphone and create one recorder for the entire pauseable take. */
227227
const startRecording = useCallback(async () => {
228-
if (typeof navigator === "undefined" || !navigator.mediaDevices?.getUserMedia) {
228+
const saveRecordedVoiceover = window.electronAPI?.saveRecordedVoiceover;
229+
if (
230+
typeof navigator === "undefined" ||
231+
!navigator.mediaDevices?.getUserMedia ||
232+
!saveRecordedVoiceover
233+
) {
229234
toast.error(t("audio.recordingUnavailable"));
230235
return;
231236
}
@@ -264,21 +269,21 @@ export function AddAudioLayerDialog({
264269
setElapsedSec(0);
265270
onRecordingStop();
266271
if (discarded) return;
272+
// A device can stay live while delivering no audio packets. Do not
273+
// send an empty take to disk or disguise it as an import failure.
274+
if (blob.size === 0) {
275+
toast.error(t("audio.emptyRecording"));
276+
return;
277+
}
267278
void (async () => {
268279
try {
269280
const data = await blob.arrayBuffer();
270-
const saved = window.electronAPI?.saveRecordedVoiceover
271-
? await window.electronAPI.saveRecordedVoiceover(data)
272-
: { success: false as const };
273-
if (saved.success && saved.path) {
274-
await finishWithPath(saved.path, duration);
275-
} else {
276-
// Browser-mode fallback: no main process to persist the
277-
// blob, so the layer references the in-memory blob URL —
278-
// good for the session, gone on reload.
279-
const url = URL.createObjectURL(blob);
280-
await finishWithPath(url, duration);
281+
const saved = await saveRecordedVoiceover(data);
282+
if (!saved.success || !saved.path) {
283+
toast.error(t("audio.saveFailed"));
284+
return;
281285
}
286+
await finishWithPath(saved.path, duration);
282287
} catch (err) {
283288
toast.error(t("audio.saveFailed"), {
284289
description: err instanceof Error ? err.message : String(err),

‎src/i18n/locales/ar/timeline.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"stop": "إيقاف",
115115
"micDenied": "تم رفض الوصول إلى الميكروفون",
116116
"recordingUnavailable": "التسجيل غير متاح هنا",
117+
"emptyRecording": "لم يُسجَّل أي صوت. تحقّق من الميكروفون في إعدادات الأجهزة وحاول مرة أخرى.",
117118
"saveFailed": "تعذر حفظ التسجيل",
118119
"importFailed": "تعذر استيراد الملف الصوتي"
119120
},

‎src/i18n/locales/cs/timeline.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"stop": "Zastavit",
115115
"micDenied": "Přístup k mikrofonu byl odepřen",
116116
"recordingUnavailable": "Nahrávání zde není dostupné",
117+
"emptyRecording": "Nebyl nahrán žádný zvuk. Zkontrolujte mikrofon v nastavení zařízení a zkuste to znovu.",
117118
"saveFailed": "Nepodařilo se uložit nahrávku",
118119
"importFailed": "Nepodařilo se importovat zvukový soubor"
119120
},

‎src/i18n/locales/de/timeline.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"stop": "Stopp",
115115
"micDenied": "Zugriff auf das Mikrofon wurde verweigert",
116116
"recordingUnavailable": "Aufnahme ist hier nicht verfügbar",
117+
"emptyRecording": "Es wurde kein Ton aufgenommen. Prüfe dein Mikrofon in den Geräteeinstellungen und versuche es erneut.",
117118
"saveFailed": "Die Aufnahme konnte nicht gespeichert werden",
118119
"importFailed": "Die Audiodatei konnte nicht importiert werden"
119120
},

‎src/i18n/locales/en/timeline.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"stop": "Stop",
115115
"micDenied": "Microphone access was denied",
116116
"recordingUnavailable": "Recording is not available here",
117+
"emptyRecording": "No audio was recorded. Check your microphone in Device settings and try again.",
117118
"saveFailed": "Could not save the recording",
118119
"importFailed": "Could not import the audio file"
119120
},

‎src/i18n/locales/es/timeline.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"stop": "Detener",
115115
"micDenied": "Se denegó el acceso al micrófono",
116116
"recordingUnavailable": "La grabación no está disponible aquí",
117+
"emptyRecording": "No se ha grabado audio. Comprueba el micrófono en los ajustes de dispositivos e inténtalo de nuevo.",
117118
"saveFailed": "No se pudo guardar la grabación",
118119
"importFailed": "No se pudo importar el archivo de audio"
119120
},

‎src/i18n/locales/fr/timeline.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"stop": "Arrêter",
115115
"micDenied": "L'accès au micro a été refusé",
116116
"recordingUnavailable": "L'enregistrement n'est pas disponible ici",
117+
"emptyRecording": "Aucun son n’a été enregistré. Vérifiez votre microphone dans les réglages des appareils, puis réessayez.",
117118
"saveFailed": "Impossible d'enregistrer la capture",
118119
"importFailed": "Impossible d'importer le fichier audio"
119120
},

‎src/i18n/locales/it/timeline.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"stop": "Ferma",
115115
"micDenied": "Accesso al microfono negato",
116116
"recordingUnavailable": "La registrazione non è disponibile qui",
117+
"emptyRecording": "Non è stato registrato alcun audio. Controlla il microfono nelle impostazioni dei dispositivi e riprova.",
117118
"saveFailed": "Impossibile salvare la registrazione",
118119
"importFailed": "Impossibile importare il file audio"
119120
},

‎src/i18n/locales/ja-JP/timeline.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"stop": "停止",
115115
"micDenied": "マイクへのアクセスが拒否されました",
116116
"recordingUnavailable": "ここでは録音できません",
117+
"emptyRecording": "音声が録音されませんでした。デバイス設定でマイクを確認して、もう一度お試しください。",
117118
"saveFailed": "録音を保存できませんでした",
118119
"importFailed": "音声ファイルを読み込めませんでした"
119120
},

0 commit comments

Comments
 (0)