165 行
5.1 KiB
TypeScript
165 行
5.1 KiB
TypeScript
|
|
import { describe, expect, it, vi } from "vitest";
|
||
|
|
|
||
|
|
import {
|
||
|
|
MINIMUM_RECORDING_MS,
|
||
|
|
PREFERRED_RECORDING_TYPES,
|
||
|
|
describeMicrophoneFailure,
|
||
|
|
isUsableRecording,
|
||
|
|
pickRecordingType,
|
||
|
|
startRecording,
|
||
|
|
} from "./voiceRecorder";
|
||
|
|
|
||
|
|
describe("pickRecordingType", () => {
|
||
|
|
it("prefers opus in webm, which is what speech providers accept most reliably", () => {
|
||
|
|
expect(pickRecordingType(() => true)).toBe("audio/webm;codecs=opus");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("falls through to whatever this WebView2 build does support", () => {
|
||
|
|
expect(pickRecordingType((type) => type === "audio/mp4")).toBe("audio/mp4");
|
||
|
|
});
|
||
|
|
|
||
|
|
/// A WebView2 without the media codec pack supports no audio type at all.
|
||
|
|
it("reports no format rather than letting MediaRecorder throw", () => {
|
||
|
|
expect(pickRecordingType(() => false)).toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
it("only ever offers audio types", () => {
|
||
|
|
for (const type of PREFERRED_RECORDING_TYPES) {
|
||
|
|
expect(type.startsWith("audio/")).toBe(true);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe("describeMicrophoneFailure", () => {
|
||
|
|
/// Each of these needs a different action from the user, so they must not
|
||
|
|
/// collapse into one message — which is what the old code did.
|
||
|
|
it("distinguishes the cases that need different actions", () => {
|
||
|
|
const denied = describeMicrophoneFailure({ name: "NotAllowedError" });
|
||
|
|
const missing = describeMicrophoneFailure({ name: "NotFoundError" });
|
||
|
|
const busy = describeMicrophoneFailure({ name: "NotReadableError" });
|
||
|
|
|
||
|
|
expect(denied).toMatch(/权限/);
|
||
|
|
expect(missing).toMatch(/没有找到/);
|
||
|
|
expect(busy).toMatch(/占用/);
|
||
|
|
expect(new Set([denied, missing, busy]).size).toBe(3);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("still says something usable for an unrecognised failure", () => {
|
||
|
|
expect(describeMicrophoneFailure(undefined)).not.toBe("");
|
||
|
|
expect(describeMicrophoneFailure(new Error("boom"))).toMatch(/WebView2/);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe("isUsableRecording", () => {
|
||
|
|
it("discards a tap on the button", () => {
|
||
|
|
expect(
|
||
|
|
isUsableRecording({ mimeType: "audio/webm", bytes: [1], durationMs: 40 }),
|
||
|
|
).toBe(false);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("discards a recording that produced no audio", () => {
|
||
|
|
expect(
|
||
|
|
isUsableRecording({
|
||
|
|
mimeType: "audio/webm",
|
||
|
|
bytes: [],
|
||
|
|
durationMs: 5000,
|
||
|
|
}),
|
||
|
|
).toBe(false);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("keeps a recording long enough to be speech", () => {
|
||
|
|
expect(
|
||
|
|
isUsableRecording({
|
||
|
|
mimeType: "audio/webm",
|
||
|
|
bytes: [1, 2],
|
||
|
|
durationMs: MINIMUM_RECORDING_MS,
|
||
|
|
}),
|
||
|
|
).toBe(true);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe("startRecording", () => {
|
||
|
|
const fakeStream = () => {
|
||
|
|
const stop = vi.fn();
|
||
|
|
return {
|
||
|
|
stream: { getTracks: () => [{ stop }] } as unknown as MediaStream,
|
||
|
|
stop,
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
const fakeRecorder = () => {
|
||
|
|
const recorder = {
|
||
|
|
ondataavailable: null as ((event: { data: Blob }) => void) | null,
|
||
|
|
onstop: null as (() => void) | null,
|
||
|
|
onerror: null as (() => void) | null,
|
||
|
|
start: vi.fn(),
|
||
|
|
stop: vi.fn(),
|
||
|
|
};
|
||
|
|
// A real MediaRecorder fires onstop asynchronously after stop().
|
||
|
|
recorder.stop.mockImplementation(() => recorder.onstop?.());
|
||
|
|
return recorder;
|
||
|
|
};
|
||
|
|
|
||
|
|
it("releases the microphone once the recording ends", async () => {
|
||
|
|
const { stream, stop: stopTrack } = fakeStream();
|
||
|
|
const recorder = fakeRecorder();
|
||
|
|
let clock = 1000;
|
||
|
|
|
||
|
|
const finish = await startRecording(
|
||
|
|
{ getUserMedia: async () => stream } as unknown as MediaDevices,
|
||
|
|
() => recorder as unknown as MediaRecorder,
|
||
|
|
() => true,
|
||
|
|
() => clock,
|
||
|
|
);
|
||
|
|
expect(recorder.start).toHaveBeenCalled();
|
||
|
|
// The track must still be live while recording, or nothing is captured.
|
||
|
|
expect(stopTrack).not.toHaveBeenCalled();
|
||
|
|
|
||
|
|
recorder.ondataavailable?.({ data: new Blob([new Uint8Array([7, 8])]) });
|
||
|
|
clock = 3000;
|
||
|
|
const recording = await finish();
|
||
|
|
|
||
|
|
// Leaving the track live is what keeps Windows' recording indicator on after
|
||
|
|
// the user has finished speaking.
|
||
|
|
expect(stopTrack).toHaveBeenCalled();
|
||
|
|
expect(recording.durationMs).toBe(2000);
|
||
|
|
expect(recording.mimeType).toBe("audio/webm;codecs=opus");
|
||
|
|
expect(recording.bytes).toEqual([7, 8]);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("releases the microphone when no codec is available", async () => {
|
||
|
|
const { stream, stop: stopTrack } = fakeStream();
|
||
|
|
const getUserMedia = vi.fn(async () => stream);
|
||
|
|
|
||
|
|
await expect(
|
||
|
|
startRecording(
|
||
|
|
{ getUserMedia } as unknown as MediaDevices,
|
||
|
|
() => fakeRecorder() as unknown as MediaRecorder,
|
||
|
|
() => false,
|
||
|
|
() => 0,
|
||
|
|
),
|
||
|
|
).rejects.toThrow(/编码器/);
|
||
|
|
|
||
|
|
// Refused before the microphone was even opened.
|
||
|
|
expect(getUserMedia).not.toHaveBeenCalled();
|
||
|
|
expect(stopTrack).not.toHaveBeenCalled();
|
||
|
|
});
|
||
|
|
|
||
|
|
it("releases the microphone when the recorder cannot be constructed", async () => {
|
||
|
|
const { stream, stop: stopTrack } = fakeStream();
|
||
|
|
|
||
|
|
await expect(
|
||
|
|
startRecording(
|
||
|
|
{ getUserMedia: async () => stream } as unknown as MediaDevices,
|
||
|
|
() => {
|
||
|
|
throw new Error("unsupported");
|
||
|
|
},
|
||
|
|
() => true,
|
||
|
|
() => 0,
|
||
|
|
),
|
||
|
|
).rejects.toThrow("unsupported");
|
||
|
|
|
||
|
|
expect(stopTrack).toHaveBeenCalled();
|
||
|
|
});
|
||
|
|
});
|