@devries-cameron20 / t3code-1 / commits / 22936ae669
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): ignore superseded microphone refreshes in voice setup (#39552)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 20, 2026 at 08:25 UTC 22936ae66966252861a94c64d081c75f80feb2bd
3 files changed +220 -5
apps/web/src/components/chat/VoiceInputSetup.browser.test.tsx new
+60
@@ -0,0 +1,60 @@
1 +import { expect, it, vi } from "vite-plus/test";
2 +import { page } from "vite-plus/test/browser";
3 +
4 +import { act, render } from "../../../test/render";
5 +import { VoiceInputSetup } from "./VoiceInputSetup";
6 +
7 +function wizard(open: boolean) {
8 + return (
9 + <VoiceInputSetup
10 + open={open}
11 + step={1}
12 + environmentId={null}
13 + status={null}
14 + model={null}
15 + language={null}
16 + downloading={false}
17 + error={null}
18 + onOpenChange={() => {}}
19 + onRetryCatalog={null}
20 + onDownload={() => {}}
21 + onCancelDownload={() => {}}
22 + onStartRecording={() => {}}
23 + />
24 + );
25 +}
26 +
27 +it.each([
28 + { latest: "succeeds", showsError: false },
29 + { latest: "fails", showsError: true },
30 +] as const)(
31 + "ignores a superseded microphone refresh when the latest one $latest",
32 + async ({ latest, showsError }) => {
33 + await using screen = await render(wizard(false));
34 + const old = Promise.withResolvers<MediaDeviceInfo[]>();
35 + const current = Promise.withResolvers<MediaDeviceInfo[]>();
36 + const mediaDevices = navigator.mediaDevices;
37 + using _enumeration = vi
38 + .spyOn(mediaDevices, "enumerateDevices")
39 + .mockReturnValueOnce(old.promise)
40 + .mockReturnValueOnce(current.promise);
41 +
42 + await screen.rerender(wizard(true));
43 + const picker = page.getByRole("combobox", { name: "Microphone", exact: true });
44 + const alert = page.getByRole("alert").filter({ hasText: "Couldn’t list microphones" });
45 + await expect.element(picker).toBeDisabled();
46 +
47 + await act(async () => {
48 + mediaDevices.dispatchEvent(new Event("devicechange"));
49 + if (latest === "fails") current.reject(new Error("Enumeration failed"));
50 + else current.resolve([]);
51 + });
52 + await expect.element(picker).toBeEnabled();
53 +
54 + await act(async () => {
55 + if (latest === "succeeds") old.reject(new Error("Superseded enumeration failed"));
56 + else old.resolve([]);
57 + });
58 + expect(alert.query() !== null).toBe(showsError);
59 + },
60 +);
apps/web/src/components/chat/VoiceInputSetup.tsx
+10 -5
@@ -13,7 +13,7 @@ import type {
13 } from "@t3tools/contracts";
14 import { useNavigate } from "@tanstack/react-router";
15 import { CircleAlertIcon, MicIcon, RefreshCwIcon } from "lucide-react";
16 -import { useEffect, useState } from "react";
16 +import { useEffect, useRef, useState } from "react";
17
18 import { useClientSettings } from "../../hooks/useSettings";
19 import {
@@ -58,14 +58,19 @@ export function VoiceInputSetup(props: {
58 const selectedMicrophone = preferredMicrophoneId(order, microphones);
59 const [loadingMicrophones, setLoadingMicrophones] = useState(false);
60 const [microphoneError, setMicrophoneError] = useState<string | null>(null);
61 + const microphoneRefreshId = useRef(0);
62 const refreshMicrophones = useStableCallback(async () => {
63 if (!navigator.mediaDevices?.enumerateDevices) return;
64 + const requestId = ++microphoneRefreshId.current;
65 setLoadingMicrophones(true);
64 - try {
65 - setMicrophones(await listBrowserMicrophones());
66 - setMicrophoneError(null);
67 - } catch {
66 + const devices = await listBrowserMicrophones().catch(() => null);
67 + if (requestId !== microphoneRefreshId.current) return;
68 +
69 + if (devices === null) {
70 setMicrophoneError(m(msg("Couldn’t list microphones. The system default still works.")));
71 + } else {
72 + setMicrophones(devices);
73 + setMicrophoneError(null);
74 }
75 setLoadingMicrophones(false);
76 });
scripts/scenarios/voice-setup-keeps-connected-microphone.e2e.ts new
+150
@@ -0,0 +1,150 @@
1 +/// <reference lib="dom" />
2 +import type { EnvironmentSpeechModel, EnvironmentSpeechStatus } from "@t3tools/contracts";
3 +import { expect } from "e2e";
4 +
5 +import { test, WEB } from "./t3.ts";
6 +
7 +declare global {
8 + interface Window {
9 + voiceSetupDevices: {
10 + holdNext: () => void;
11 + held: Promise<void>;
12 + replaceMicrophone: () => void;
13 + releaseOld: () => Promise<void>;
14 + };
15 + }
16 +}
17 +
18 +test(
19 + "Voice setup keeps the current microphone after an older refresh finishes",
20 + WEB,
21 + async ({ t3, app, browser, screen }) => {
22 + const project = await t3.project("demo");
23 + const thread = await t3.thread({ project, title: "Voice notes", messages: ["first"] });
24 +
25 + const model = {
26 + id: "scenario-speech",
27 + name: "Scenario speech model",
28 + description: "",
29 + size: 1_048_576,
30 + languages: ["en", "fr", "de"],
31 + accuracy: 1,
32 + speed: 1,
33 + recommended: true,
34 + supportsStreaming: false,
35 + supportsLanguageDetection: true,
36 + supportsTranslation: false,
37 + active: false,
38 + state: "downloadable",
39 + } satisfies EnvironmentSpeechModel;
40 + let installed = false;
41 + await browser.route(/\/api\/voice\/status$/, (route) => {
42 + const status = {
43 + supported: true,
44 + state: installed ? "ready" : "missing-model",
45 + modelId: model.id,
46 + model: model.name,
47 + size: model.size,
48 + supportsStreaming: false,
49 + supportsTranslation: false,
50 + acceleration: "cpu",
51 + benchmarkTier: "cpu",
52 + modelUnloadTimeout: "never",
53 + gpuDevices: [],
54 + } satisfies EnvironmentSpeechStatus;
55 + return route.fulfill({ json: status });
56 + });
57 + await browser.route(/\/api\/voice\/models(?:\?|$)/, (route) =>
58 + route.fulfill({
59 + json: { models: [installed ? { ...model, active: true, state: "installed" } : model] },
60 + }),
61 + );
62 + await browser.route(/\/api\/voice\/models\/download$/, (route) => {
63 + installed = true;
64 + return route.fulfill({
65 + json: { models: [{ ...model, active: true, state: "installed" }] },
66 + });
67 + });
68 +
69 + await browser.addInitScript(() => {
70 + const oldMicrophone = { kind: "audioinput", deviceId: "desk", label: "Desk microphone" };
71 + const newMicrophone = { kind: "audioinput", deviceId: "usb", label: "USB microphone" };
72 + const { promise: held, resolve: markHeld } = Promise.withResolvers<void>();
73 + const { promise: old, resolve: release } =
74 + Promise.withResolvers<Array<typeof oldMicrophone>>();
75 + let holdNext = false;
76 + let current = oldMicrophone;
77 + const media = navigator.mediaDevices;
78 + Object.defineProperty(media, "enumerateDevices", {
79 + configurable: true,
80 + value: () => {
81 + if (!holdNext) return Promise.resolve([current]);
82 + holdNext = false;
83 + markHeld();
84 + return old;
85 + },
86 + });
87 + window.voiceSetupDevices = {
88 + holdNext: () => {
89 + holdNext = true;
90 + },
91 + held,
92 + replaceMicrophone: () => {
93 + current = newMicrophone;
94 + media.dispatchEvent(new Event("devicechange"));
95 + },
96 + releaseOld: async () => {
97 + release([oldMicrophone]);
98 + // Let enumeration and its React update settle before inspecting the picker.
99 + await new Promise<void>((resolve) =>
100 + requestAnimationFrame(() => requestAnimationFrame(() => resolve())),
101 + );
102 + },
103 + };
104 + });
105 +
106 + await t3.open(thread);
107 + const threadPath = new URL(await browser.url()).pathname;
108 + const voice = screen.getByRole("button", "Start voice input");
109 + await voice.tap();
110 + const destination = screen.getByRole("combobox", "Transcribe with");
111 + await destination.tap();
112 + // The only paired environment is the first choice after Off.
113 + await screen.getByRole("option").nth(1).tap();
114 + await app.open(threadPath);
115 + await voice.tap();
116 +
117 + const download = screen.getByRole("button", "Download model", { exact: true });
118 + await download.waitFor();
119 + await browser.evaluate(() => {
120 + window.voiceSetupDevices.holdNext();
121 + return null;
122 + });
123 + await download.tap();
124 + await screen.getByText("Voice input is ready", { exact: true }).waitFor();
125 + await browser.evaluate(async () => {
126 + await window.voiceSetupDevices.held;
127 + window.voiceSetupDevices.replaceMicrophone();
128 + return null;
129 + });
130 + await screen.getByRole("combobox", "Microphone", { exact: true }).tap();
131 + const connected = screen.getByRole("option", "USB microphone", { exact: true });
132 + await connected.waitFor();
133 +
134 + await browser.evaluate(async () => {
135 + await window.voiceSetupDevices.releaseOld();
136 + return null;
137 + });
138 + await app.screenshot("microphones-after-both-refreshes");
139 + const labels = await browser.evaluate(() =>
140 + Array.from(
141 + document.querySelectorAll('[role="listbox"] [role="option"]'),
142 + (option) => option.textContent?.trim() ?? "",
143 + ),
144 + );
145 + expect(labels, "The microphone picker lists only the current devices").toEqual([
146 + "System default",
147 + "USB microphone",
148 + ]);
149 + },
150 +);