File size: 20,687 Bytes
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
6c3af4e
 
 
 
 
d84bd10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c3af4e
 
 
d84bd10
 
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
 
 
 
 
 
 
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
 
 
 
 
 
 
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
 
 
 
 
 
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
6c3af4e
 
 
 
 
 
 
 
 
 
d84bd10
 
 
 
 
 
 
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
 
 
 
 
 
 
 
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84bd10
 
 
 
 
 
 
 
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
d84bd10
6c3af4e
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
import {
  DictationError,
  type DictationSession,
  getDictationEngine,
  resampleTo16k,
  setWorkerFactory,
  startDictation,
} from "@/modules/speechToText";
import type { WorkerResponse } from "@/modules/speechToTextWorkerProtocol";

class FakeWorker {
  static instances: FakeWorker[] = [];
  onmessage: ((event: MessageEvent) => void) | null = null;
  onerror: ((event: unknown) => void) | null = null;
  terminated = false;
  messages: unknown[] = [];

  constructor() {
    FakeWorker.instances.push(this);
  }

  postMessage(message: unknown, _transfer?: Transferable[]) {
    this.messages.push(message);
  }

  terminate() {
    this.terminated = true;
  }

  respond(response: WorkerResponse) {
    this.onmessage?.({ data: response } as MessageEvent);
  }
}

class FakeMediaStreamTrack {
  stopped = false;
  stop() {
    this.stopped = true;
  }
}

function makeMediaStream() {
  const tracks = [new FakeMediaStreamTrack(), new FakeMediaStreamTrack()];
  return {
    tracks,
    stream: {
      getTracks: () => tracks,
    } as unknown as MediaStream,
  };
}

function stubWasmSupport(getUserMedia: ReturnType<typeof vi.fn>) {
  vi.stubGlobal("Worker", FakeWorker);
  vi.stubGlobal("WebAssembly", { instantiate: vi.fn() });
  vi.stubGlobal(
    "AudioContext",
    class {
      sampleRate = 48000;
      createMediaStreamSource() {
        return { connect: vi.fn(), disconnect: vi.fn() };
      }
      createScriptProcessor() {
        return {
          onaudioprocess: null,
          connect: vi.fn(),
          disconnect: vi.fn(),
        };
      }
      close() {
        return Promise.resolve();
      }
    },
  );
  Object.defineProperty(navigator, "mediaDevices", {
    configurable: true,
    value: { getUserMedia },
  });
}

const originalMediaDevices = navigator.mediaDevices;

afterEach(() => {
  vi.unstubAllGlobals();
  Object.defineProperty(navigator, "mediaDevices", {
    configurable: true,
    value: originalMediaDevices,
  });
  delete (window as unknown as Record<string, unknown>).webkitSpeechRecognition;
  setWorkerFactory(() => {
    throw new Error("no worker factory configured");
  });
  FakeWorker.instances = [];
});

describe("getDictationEngine", () => {
  function installRecognitionFake() {
    class FakeRecognition {
      start = vi.fn();
    }
    (window as unknown as Record<string, unknown>).webkitSpeechRecognition =
      FakeRecognition;
  }

  it("reports the wasm engine when it is preferred and every piece is present", () => {
    stubWasmSupport(vi.fn());
    expect(getDictationEngine(true)).toBe("wasm");
  });

  it("falls back to web-speech when the wasm pieces are missing", () => {
    installRecognitionFake();
    expect(getDictationEngine(true)).toBe("web-speech");
  });

  it("prefers web-speech over a capable wasm path when the local model is off", () => {
    stubWasmSupport(vi.fn());
    installRecognitionFake();
    expect(getDictationEngine(false)).toBe("web-speech");
  });

  it("keeps the wasm engine when the local model is off but no recognizer exists", () => {
    // The preference reorders engines, it never empties the list: a browser
    // without `SpeechRecognition` (Firefox, for one) must still get a
    // working Dictate button with the setting off.
    stubWasmSupport(vi.fn());
    expect(getDictationEngine(false)).toBe("wasm");
  });

  it("reports no engine when neither path is available", () => {
    expect(getDictationEngine(true)).toBeNull();
    expect(getDictationEngine(false)).toBeNull();
  });
});

describe("resampleTo16k", () => {
  it("passes 16 kHz audio through untouched", () => {
    const input = new Float32Array([0.5, -0.5]);
    expect(resampleTo16k(input, 16000)).toBe(input);
  });

  it("resamples 48 kHz audio to a third of the length with interpolated values", () => {
    const input = new Float32Array([0, 1, 2, 3, 4, 5]);
    const output = resampleTo16k(input, 48000);
    expect(output.length).toBe(2);
    expect(output[0]).toBeCloseTo(0);
    expect(output[1]).toBeCloseTo(3);
  });
});

describe("startDictation with the wasm engine", () => {
  it("loads the model, streams the transcript and releases everything on stop", async () => {
    const { tracks, stream } = makeMediaStream();
    const getUserMedia = vi.fn().mockResolvedValue(stream);
    stubWasmSupport(getUserMedia);
    setWorkerFactory(() => new FakeWorker() as unknown as Worker);

    const transcripts: string[] = [];
    const progress: [number, number | undefined][] = [];

    const sessionPromise = startDictation(
      {
        onTranscript: (text) => transcripts.push(text),
        onProgress: (loaded, total) => progress.push([loaded, total]),
      },
      true,
    );

    await vi.waitFor(() => expect(FakeWorker.instances.length).toBe(1));
    const worker = FakeWorker.instances[0];
    expect(worker.messages[0]).toMatchObject({ type: "load" });

    worker.respond({ type: "progress", loaded: 1000, total: 2000 });
    worker.respond({ type: "loaded" });

    const session: DictationSession = await sessionPromise;

    worker.respond({ type: "transcript", text: "hello" });
    worker.respond({ type: "transcript", text: "hello world" });

    expect(transcripts).toEqual(["hello", "hello world"]);
    expect(progress).toEqual([[1000, 2000]]);

    await session.stop();

    expect(tracks.every((track) => track.stopped)).toBe(true);
    expect(worker.terminated).toBe(true);
    expect(worker.messages).toContainEqual({ type: "stop" });
  });

  it("reports a permission error when the microphone is denied", async () => {
    const getUserMedia = vi
      .fn()
      .mockRejectedValue(
        new DOMException("Permission denied", "NotAllowedError"),
      );
    stubWasmSupport(getUserMedia);
    setWorkerFactory(() => new FakeWorker() as unknown as Worker);

    const sessionPromise = startDictation({ onTranscript: vi.fn() }, true);
    await vi.waitFor(() => expect(FakeWorker.instances.length).toBe(1));
    FakeWorker.instances[0].respond({ type: "loaded" });

    await expect(sessionPromise).rejects.toMatchObject({ kind: "permission" });
    expect(getUserMedia).toHaveBeenCalledWith({ audio: true });
  });

  /** Minimal `SpeechRecognition` stand-in; reports whether it was started. */
  function installLocalRecognitionFake() {
    let started = false;
    class FakeRecognition {
      continuous = false;
      interimResults = false;
      lang = "";
      onresult: unknown = null;
      onerror: unknown = null;
      onend: unknown = null;
      start() {
        started = true;
      }
      stop() {}
    }
    (window as unknown as Record<string, unknown>).webkitSpeechRecognition =
      FakeRecognition;
    return () => started;
  }

  it("falls back to the browser recognizer when the local engine fails", async () => {
    const { stream } = makeMediaStream();
    stubWasmSupport(vi.fn().mockResolvedValue(stream));
    setWorkerFactory(() => new FakeWorker() as unknown as Worker);
    const started = installLocalRecognitionFake();

    const sessionPromise = startDictation({ onTranscript: vi.fn() }, true);
    await vi.waitFor(() => expect(FakeWorker.instances.length).toBe(1));
    // A 502 from `/dictation-models/` is "cannot run here", not "no engine".
    FakeWorker.instances[0].respond({
      type: "error",
      message: "model download failed",
    });

    await sessionPromise;
    expect(started()).toBe(true);
  });

  it("reports a denied microphone instead of falling back", async () => {
    stubWasmSupport(
      vi
        .fn()
        .mockRejectedValue(
          new DOMException("Permission denied", "NotAllowedError"),
        ),
    );
    setWorkerFactory(() => new FakeWorker() as unknown as Worker);
    const started = installLocalRecognitionFake();

    const sessionPromise = startDictation({ onTranscript: vi.fn() }, true);
    await vi.waitFor(() => expect(FakeWorker.instances.length).toBe(1));
    FakeWorker.instances[0].respond({ type: "loaded" });

    await expect(sessionPromise).rejects.toMatchObject({ kind: "permission" });
    // Falling back here would ask again and, in Chrome, send the audio away.
    expect(started()).toBe(false);
  });

  it("fails the load when the engine dies while permission is pending", async () => {
    const { tracks, stream } = makeMediaStream();
    let grantMicrophone: ((stream: MediaStream) => void) | undefined;
    stubWasmSupport(
      vi.fn().mockReturnValue(
        new Promise((resolve) => {
          grantMicrophone = resolve as (stream: MediaStream) => void;
        }),
      ),
    );
    setWorkerFactory(() => new FakeWorker() as unknown as Worker);
    const started = installLocalRecognitionFake();

    const sessionPromise = startDictation({ onTranscript: vi.fn() }, true);
    await vi.waitFor(() => expect(FakeWorker.instances.length).toBe(1));
    const worker = FakeWorker.instances[0];
    worker.respond({ type: "loaded" });

    // The prompt can sit open for minutes, and the transcriber is already
    // running by now. Rejecting the settled load promise would be a no-op.
    worker.respond({ type: "error", message: "the transcriber died" });
    grantMicrophone?.(stream);

    await sessionPromise;
    expect(tracks.every((track) => track.stopped)).toBe(true);
    expect(worker.terminated).toBe(true);
    // Treated as any other engine failure, so the browser recognizer takes it.
    expect(started()).toBe(true);
  });

  it("fails the load when the worker dies uncaught while permission is pending", async () => {
    const { tracks, stream } = makeMediaStream();
    let grantMicrophone: ((stream: MediaStream) => void) | undefined;
    stubWasmSupport(
      vi.fn().mockReturnValue(
        new Promise((resolve) => {
          grantMicrophone = resolve as (stream: MediaStream) => void;
        }),
      ),
    );
    setWorkerFactory(() => new FakeWorker() as unknown as Worker);
    const started = installLocalRecognitionFake();

    const sessionPromise = startDictation({ onTranscript: vi.fn() }, true);
    await vi.waitFor(() => expect(FakeWorker.instances.length).toBe(1));
    const worker = FakeWorker.instances[0];
    worker.respond({ type: "loaded" });

    // An uncaught error rather than a posted one: the worker's own try/catch
    // converts most failures, so this is the channel it cannot cover.
    worker.onerror?.(new ErrorEvent("error"));
    grantMicrophone?.(stream);

    await sessionPromise;
    expect(tracks.every((track) => track.stopped)).toBe(true);
    expect(worker.terminated).toBe(true);
    expect(started()).toBe(true);
  });

  it("ignores a transcript that arrives after stop", async () => {
    const { stream } = makeMediaStream();
    stubWasmSupport(vi.fn().mockResolvedValue(stream));
    setWorkerFactory(() => new FakeWorker() as unknown as Worker);
    const onTranscript = vi.fn();

    const sessionPromise = startDictation({ onTranscript }, true);
    await vi.waitFor(() => expect(FakeWorker.instances.length).toBe(1));
    const worker = FakeWorker.instances[0];
    worker.respond({ type: "loaded" });
    const session = await sessionPromise;

    worker.respond({ type: "transcript", text: "hello world" });
    expect(onTranscript).toHaveBeenCalledWith("hello world");

    const stopping = session.stop();
    // Both engines emit one last result after being told to stop. The caller
    // has already forgotten what it appended, so this would be appended twice.
    worker.respond({ type: "transcript", text: "hello world" });
    worker.respond({ type: "stopped" });
    await stopping;

    expect(onTranscript).toHaveBeenCalledTimes(1);
  });

  it("waits for the worker to drain before terminating it", async () => {
    const { stream } = makeMediaStream();
    stubWasmSupport(vi.fn().mockResolvedValue(stream));
    setWorkerFactory(() => new FakeWorker() as unknown as Worker);

    const sessionPromise = startDictation({ onTranscript: vi.fn() }, true);
    await vi.waitFor(() => expect(FakeWorker.instances.length).toBe(1));
    const worker = FakeWorker.instances[0];
    worker.respond({ type: "loaded" });
    const session = await sessionPromise;

    const stopping = session.stop();
    await Promise.resolve();
    // The transcriber runs synchronously inside the worker's `onmessage`, so a
    // backlog can leave it seconds behind; terminating now drops the tail.
    expect(worker.terminated).toBe(false);

    worker.respond({ type: "stopped" });
    await stopping;
    expect(worker.terminated).toBe(true);
  });

  it("reports a worker failure that happens after the engine loaded", async () => {
    const { stream } = makeMediaStream();
    stubWasmSupport(vi.fn().mockResolvedValue(stream));
    setWorkerFactory(() => new FakeWorker() as unknown as Worker);
    const onError = vi.fn();

    const sessionPromise = startDictation(
      {
        onTranscript: vi.fn(),
        onError,
      },
      true,
    );
    await vi.waitFor(() => expect(FakeWorker.instances.length).toBe(1));
    const worker = FakeWorker.instances[0];
    worker.respond({ type: "loaded" });
    await sessionPromise;

    // The load promise has settled, so rejecting it again is a no-op: without
    // a live channel the UI listens forever with the microphone open.
    worker.respond({ type: "error", message: "the transcriber died" });

    expect(onError).toHaveBeenCalledTimes(1);
    expect(onError.mock.calls[0][0]).toMatchObject({ kind: "engine" });
  });

  it("never opens the microphone when the model fails to load", async () => {
    const { tracks, stream } = makeMediaStream();
    const getUserMedia = vi.fn().mockResolvedValue(stream);
    stubWasmSupport(getUserMedia);
    setWorkerFactory(() => new FakeWorker() as unknown as Worker);

    const sessionPromise = startDictation({ onTranscript: vi.fn() }, true);
    await vi.waitFor(() => expect(FakeWorker.instances.length).toBe(1));
    const worker = FakeWorker.instances[0];
    worker.respond({ type: "error", message: "model download failed" });

    await expect(sessionPromise).rejects.toBeInstanceOf(DictationError);
    await expect(
      sessionPromise.catch((error: DictationError) => error.kind),
    ).resolves.toBe("engine");
    // The load runs first, so a failed download never lights the recording
    // indicator at all.
    expect(getUserMedia).not.toHaveBeenCalled();
    expect(tracks.every((track) => track.stopped)).toBe(false);
    expect(worker.terminated).toBe(true);
  });
});

describe("startDictation with the local model preference off", () => {
  let recognitionStarted = false;

  function installRecognitionFake(
    startImpl: () => void = () => {
      recognitionStarted = true;
    },
  ) {
    class FakeRecognition {
      continuous = false;
      interimResults = false;
      lang = "";
      onresult: unknown = null;
      onerror: unknown = null;
      onend: unknown = null;
      start = startImpl;
      stop = () => {};
    }
    (window as unknown as Record<string, unknown>).webkitSpeechRecognition =
      FakeRecognition;
  }

  beforeEach(() => {
    recognitionStarted = false;
  });

  it("never constructs the model worker on a fully wasm-capable browser", async () => {
    // The whole point of the setting: with it off, the ~51 MB model must
    // not be downloaded even where it could run.
    const { stream } = makeMediaStream();
    stubWasmSupport(vi.fn().mockResolvedValue(stream));
    setWorkerFactory(() => new FakeWorker() as unknown as Worker);
    installRecognitionFake();

    const session = await startDictation({ onTranscript: vi.fn() }, false);

    expect(FakeWorker.instances.length).toBe(0);
    expect(recognitionStarted).toBe(true);
    await session.stop();
  });

  it("does not fall back to the wasm engine when the recognizer fails", async () => {
    // A reverse fallback would start the ~51 MB download right after the
    // user turned that model off; the failure must surface instead.
    stubWasmSupport(vi.fn());
    setWorkerFactory(() => new FakeWorker() as unknown as Worker);
    installRecognitionFake(() => {
      throw new Error("recognizer refused to start");
    });

    await expect(
      startDictation({ onTranscript: vi.fn() }, false),
    ).rejects.toMatchObject({ kind: "unavailable" });
    expect(FakeWorker.instances.length).toBe(0);
  });
});

describe("startDictation with the web speech fallback", () => {
  interface RecognitionFake {
    continuous: boolean;
    interimResults: boolean;
    lang: string;
    onresult:
      | ((event: {
          resultIndex: number;
          results: ArrayLike<{ isFinal: boolean; 0: { transcript: string } }>;
        }) => void)
      | null;
    onerror: ((event: { error: string }) => void) | null;
    onend: (() => void) | null;
    start: () => void;
    stop: () => void;
    stopped: boolean;
  }

  let instance: RecognitionFake | undefined;

  function installRecognitionFake(startImpl: () => void = () => {}) {
    class FakeRecognition {
      continuous = false;
      interimResults = false;
      lang = "";
      onresult: RecognitionFake["onresult"] = null;
      onerror: RecognitionFake["onerror"] = null;
      onend: (() => void) | null = null;
      stopped = false;
      start: () => void;
      stop = () => {
        this.stopped = true;
      };
      constructor() {
        this.start = startImpl;
        instance = this as unknown as RecognitionFake;
      }
    }
    (window as unknown as Record<string, unknown>).webkitSpeechRecognition =
      FakeRecognition;
  }

  it("transcribes interim results and stops on request", async () => {
    installRecognitionFake();

    const transcripts: string[] = [];
    const session = await startDictation(
      {
        onTranscript: (text) => transcripts.push(text),
      },
      true,
    );

    expect(instance?.continuous).toBe(true);
    expect(instance?.interimResults).toBe(true);

    instance?.onresult?.({
      resultIndex: 0,
      results: [{ isFinal: false, 0: { transcript: "search for" } }],
    });
    instance?.onresult?.({
      resultIndex: 0,
      results: [
        { isFinal: true, 0: { transcript: "search for" } },
        { isFinal: false, 0: { transcript: " moonshine" } },
      ],
    });

    expect(transcripts).toEqual(["search for", "search for moonshine"]);

    await session.stop();
    expect(instance?.stopped).toBe(true);
  });

  it("reports a permission error raised before the session resolves", async () => {
    installRecognitionFake(() => {
      instance?.onerror?.({ error: "not-allowed" });
    });

    await expect(
      startDictation({ onTranscript: vi.fn() }, true),
    ).rejects.toMatchObject({ kind: "permission" });
  });

  it("reports a denial that arrives after the session resolved", async () => {
    // What a real browser does: `start()` returns, and the denial arrives on a
    // later task. Rejecting the already-resolved promise would be a no-op, so
    // without the `onError` route the user is told nothing at all.
    installRecognitionFake();
    const onError = vi.fn();

    const session = await startDictation(
      {
        onTranscript: vi.fn(),
        onError,
      },
      true,
    );

    instance?.onerror?.({ error: "not-allowed" });

    expect(onError).toHaveBeenCalledTimes(1);
    expect(onError.mock.calls[0][0]).toMatchObject({ kind: "permission" });
    await session.stop();
  });

  it("ends the session quietly when the recognizer stops on its own", async () => {
    installRecognitionFake();
    const onError = vi.fn();
    const onEnd = vi.fn();

    const session = await startDictation(
      {
        onTranscript: vi.fn(),
        onEnd,
        onError,
      },
      true,
    );

    // Chrome ends after silence even with `continuous`. The session is over,
    // but there is nothing the user needs told about.
    instance?.onend?.();

    expect(onEnd).toHaveBeenCalledTimes(1);
    expect(onError).not.toHaveBeenCalled();
    await session.stop();
  });

  it("says nothing when the recognizer ends because it was stopped", async () => {
    installRecognitionFake();
    const onError = vi.fn();
    const onEnd = vi.fn();

    const session = await startDictation(
      {
        onTranscript: vi.fn(),
        onEnd,
        onError,
      },
      true,
    );

    await session.stop();
    instance?.onend?.();

    expect(onError).not.toHaveBeenCalled();
    expect(onEnd).not.toHaveBeenCalled();
  });
});

describe("startDictation without any engine", () => {
  it("rejects with an unavailable error", async () => {
    await expect(
      startDictation({ onTranscript: vi.fn() }, true),
    ).rejects.toMatchObject({ kind: "unavailable" });
  });
});