diff --git a/frontend/src/app/workspace/component/hugging-face-audio-upload/hugging-face-audio-upload.component.spec.ts b/frontend/src/app/workspace/component/hugging-face-audio-upload/hugging-face-audio-upload.component.spec.ts index 41f1fec5afd..20ac721505c 100644 --- a/frontend/src/app/workspace/component/hugging-face-audio-upload/hugging-face-audio-upload.component.spec.ts +++ b/frontend/src/app/workspace/component/hugging-face-audio-upload/hugging-face-audio-upload.component.spec.ts @@ -101,6 +101,30 @@ describe("HuggingFaceAudioUploadComponent", () => { }; } + async function selectAudioFile( + name: string, + type: string, + size: number, + opts?: ReaderMockOptions + ): Promise { + const teardown = installReaderMock(opts); + try { + const input = makeFileInput(sizedFile(name, type, size)); + await component.onFileSelected({ target: input } as unknown as Event); + return input; + } finally { + teardown(); + } + } + + function expectClearedSelection(input: HTMLInputElement, errorMessage: string): void { + expect(component.errorMessage).toBe(errorMessage); + expect(component.formControl.value).toBe(""); + expect(component.fileName).toBe(""); + expect(component.hasMedia).toBe(false); + expect(input.value).toBe(""); + } + it("should create", () => { expect(component).toBeTruthy(); }); @@ -127,21 +151,15 @@ describe("HuggingFaceAudioUploadComponent", () => { expect(component.displayFileName).toBe("clip.wav"); }); - it("returns false for a leftover server path (legacy saved value)", () => { - component.formControl.setValue("/uploads/clip.wav"); + it.each<[string, string | null]>([ + ["returns false for a leftover server path (legacy saved value)", "/uploads/clip.wav"], + ["returns false for a non-audio data URL", "data:image/png;base64,AAA"], + ["returns false for a null value", null], + ])("%s", (_name, value) => { + component.formControl.setValue(value); expect(component.hasMedia).toBe(false); expect(component.previewSrc).toBe(""); }); - - it("returns false for a non-audio data URL", () => { - component.formControl.setValue("data:image/png;base64,AAA"); - expect(component.hasMedia).toBe(false); - }); - - it("returns false for a null value", () => { - component.formControl.setValue(null); - expect(component.hasMedia).toBe(false); - }); }); // ── onFileSelected ────────────────────────────────────────────────────── @@ -156,103 +174,58 @@ describe("HuggingFaceAudioUploadComponent", () => { }); it("rejects a non-audio file and resets the input", async () => { - const file = sizedFile("doc.pdf", "application/pdf", 100); - const input = makeFileInput(file); - await component.onFileSelected({ target: input } as unknown as Event); + const input = await selectAudioFile("doc.pdf", "application/pdf", 100); expect(component.errorMessage).toBe("Choose an audio file."); expect(component.hasMedia).toBe(false); expect(input.value).toBe(""); }); it("rejects a file over the size cap", async () => { - const file = sizedFile("big.wav", "audio/wav", MAX_AUDIO_BYTES + 1); - const input = makeFileInput(file); - await component.onFileSelected({ target: input } as unknown as Event); + await selectAudioFile("big.wav", "audio/wav", MAX_AUDIO_BYTES + 1); expect(component.errorMessage).toBe("Audio file is too large (max 25 MB)."); expect(component.formControl.value).toBe(""); }); it("accepts a file exactly at the size cap", async () => { - const teardown = installReaderMock(); - try { - const file = sizedFile("edge.wav", "audio/wav", MAX_AUDIO_BYTES); - const input = makeFileInput(file); - await component.onFileSelected({ target: input } as unknown as Event); - expect(component.errorMessage).toBe(""); - expect(component.formControl.value).toBe("data:audio/wav;base64,AAAA"); - } finally { - teardown(); - } + await selectAudioFile("edge.wav", "audio/wav", MAX_AUDIO_BYTES); + expect(component.errorMessage).toBe(""); + expect(component.formControl.value).toBe("data:audio/wav;base64,AAAA"); }); it("reads an audio file into a data URL and sets form + model state", async () => { - const teardown = installReaderMock(); - try { - const file = sizedFile("clip.wav", "audio/wav", 2048); - const input = makeFileInput(file); - await component.onFileSelected({ target: input } as unknown as Event); - - expect(component.formControl.value).toBe("data:audio/wav;base64,AAAA"); - expect(component.fileName).toBe("clip.wav"); - expect(component.hasMedia).toBe(true); - expect(component.formControl.dirty).toBe(true); - expect(component.formControl.touched).toBe(true); - expect((component.model as any).audioInput).toBe("data:audio/wav;base64,AAAA"); - expect(component.errorMessage).toBe(""); - } finally { - teardown(); - } + await selectAudioFile("clip.wav", "audio/wav", 2048); + + expect(component.formControl.value).toBe("data:audio/wav;base64,AAAA"); + expect(component.fileName).toBe("clip.wav"); + expect(component.hasMedia).toBe(true); + expect(component.formControl.dirty).toBe(true); + expect(component.formControl.touched).toBe(true); + expect((component.model as any).audioInput).toBe("data:audio/wav;base64,AAAA"); + expect(component.errorMessage).toBe(""); }); it("clears a previous error on a successful read", async () => { component.errorMessage = "previous failure"; - const teardown = installReaderMock(); - try { - const file = sizedFile("ok.mp3", "audio/mpeg", 2048); - const input = makeFileInput(file); - await component.onFileSelected({ target: input } as unknown as Event); - expect(component.errorMessage).toBe(""); - } finally { - teardown(); - } - }); - - it("rejects when FileReader yields a non-audio data URL", async () => { - const teardown = installReaderMock({ readerResult: "data:text/plain;base64,AAAA" }); - try { - const file = sizedFile("weird.wav", "audio/wav", 2048); - const input = makeFileInput(file); - await component.onFileSelected({ target: input } as unknown as Event); - expect(component.errorMessage).toBe("Could not read this audio file."); - expect(component.hasMedia).toBe(false); - } finally { - teardown(); - } - }); - - it("rejects when FileReader.result is not a string (ArrayBuffer)", async () => { - const teardown = installReaderMock({ readerResult: new ArrayBuffer(8) }); - try { - const file = sizedFile("clip.wav", "audio/wav", 2048); - const input = makeFileInput(file); - await component.onFileSelected({ target: input } as unknown as Event); - expect(component.errorMessage).toBe("Could not read this audio file."); - } finally { - teardown(); - } + await selectAudioFile("ok.mp3", "audio/mpeg", 2048); + expect(component.errorMessage).toBe(""); }); - it("rejects when FileReader fires onerror", async () => { - const teardown = installReaderMock({ readerError: true }); - try { - const file = sizedFile("broken.wav", "audio/wav", 2048); - const input = makeFileInput(file); - await component.onFileSelected({ target: input } as unknown as Event); - expect(component.errorMessage).toBe("Could not read this audio file."); - expect(component.hasMedia).toBe(false); - } finally { - teardown(); - } + it.each<[string, string, ReaderMockOptions]>([ + [ + "rejects when FileReader yields a non-audio data URL", + "weird.wav", + { readerResult: "data:text/plain;base64,AAAA" }, + ], + [ + "rejects when FileReader.result is not a string (ArrayBuffer)", + "clip.wav", + { readerResult: new ArrayBuffer(8) }, + ], + ["rejects when FileReader fires onerror", "broken.wav", { readerError: true }], + ])("%s", async (_name, filename, opts) => { + await selectAudioFile(filename, "audio/wav", 2048, opts); + expect(component.errorMessage).toBe("Could not read this audio file."); + expect(component.hasMedia).toBe(false); }); it("does not update the model when key is not a string", async () => { @@ -264,98 +237,60 @@ describe("HuggingFaceAudioUploadComponent", () => { model, } as any; - const teardown = installReaderMock(); - try { - const file = sizedFile("clip.wav", "audio/wav", 2048); - const input = makeFileInput(file); - await component.onFileSelected({ target: input } as unknown as Event); - expect(component.formControl.value).toBe("data:audio/wav;base64,AAAA"); - expect(model[42 as any]).toBeUndefined(); - } finally { - teardown(); - } + await selectAudioFile("clip.wav", "audio/wav", 2048); + expect(component.formControl.value).toBe("data:audio/wav;base64,AAAA"); + expect(model[42 as any]).toBeUndefined(); }); // Review feedback on #7790: with a clip already accepted, every reject path // must drop it. Leaving it behind shows an error while the