Skip to content

Commit 17329d4

Browse files
fix(ui): defer missing microphone errors
1 parent 23da7f2 commit 17329d4

4 files changed

Lines changed: 98 additions & 1 deletion

File tree

‎ui/src/pages/chat/chat-realtime.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import {
2020
type ChatRealtimeState,
2121
} from "./chat-realtime.ts";
2222

23+
function mediaDevice(kind: MediaDeviceKind, deviceId: string, label: string): MediaDeviceInfo {
24+
return { kind, deviceId, label, groupId: "", toJSON: () => ({}) } as MediaDeviceInfo;
25+
}
26+
2327
function createState(): ChatRealtimeState {
2428
const settings = loadSettings();
2529
const state = {
@@ -46,6 +50,7 @@ describe("chat realtime microphone selection", () => {
4650

4751
afterEach(() => {
4852
vi.restoreAllMocks();
53+
vi.unstubAllGlobals();
4954
});
5055

5156
it("keeps the selected input in memory when persistence fails and shares it across panes", async () => {
@@ -69,4 +74,61 @@ describe("chat realtime microphone selection", () => {
6974
);
7075
expect(sessionStart).toHaveBeenCalledOnce();
7176
});
77+
78+
it("does not reject a persisted input from incomplete passive discovery", async () => {
79+
vi.stubGlobal("navigator", {
80+
mediaDevices: {
81+
enumerateDevices: vi.fn(async () => [
82+
mediaDevice("audioinput", "built-in", "Built-in Microphone"),
83+
]),
84+
},
85+
});
86+
const state = createState();
87+
state.settings = { ...state.settings, gatewayUrl: "ws://passive-discovery.example" };
88+
state.selectRealtimeTalkInput("usb-mic");
89+
90+
await state.refreshRealtimeTalkInputs(false);
91+
92+
expect(state.realtimeTalkInputDeviceId).toBe("usb-mic");
93+
expect(state.realtimeTalkInputError).toBeNull();
94+
});
95+
96+
it("reports a missing persisted input after successful permissioned discovery", async () => {
97+
vi.stubGlobal("navigator", {
98+
mediaDevices: {
99+
enumerateDevices: vi.fn(async () => [
100+
mediaDevice("audioinput", "built-in", "Built-in Microphone"),
101+
]),
102+
},
103+
});
104+
const state = createState();
105+
state.settings = { ...state.settings, gatewayUrl: "ws://permissioned-discovery.example" };
106+
state.selectRealtimeTalkInput("usb-mic");
107+
108+
await state.refreshRealtimeTalkInputs(true);
109+
110+
expect(state.realtimeTalkInputDeviceId).toBe("usb-mic");
111+
expect(state.realtimeTalkInputError).toContain("The selected microphone is unavailable");
112+
});
113+
114+
it("keeps permission guidance when permissioned discovery is incomplete", async () => {
115+
vi.stubGlobal("navigator", {
116+
mediaDevices: {
117+
enumerateDevices: vi.fn(async () => [
118+
mediaDevice("audioinput", "built-in", "Built-in Microphone"),
119+
mediaDevice("audioinput", "", ""),
120+
]),
121+
getUserMedia: vi.fn(async () => {
122+
throw new DOMException("denied", "NotAllowedError");
123+
}),
124+
},
125+
});
126+
const state = createState();
127+
state.settings = { ...state.settings, gatewayUrl: "ws://blocked-discovery.example" };
128+
state.selectRealtimeTalkInput("usb-mic");
129+
130+
await state.refreshRealtimeTalkInputs(true);
131+
132+
expect(state.realtimeTalkInputError).toContain("Microphone access is blocked");
133+
});
72134
});

‎ui/src/pages/chat/chat-realtime.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ async function refreshRealtimeTalkInputs(
116116
state.realtimeTalkInputDevices = result.devices;
117117
state.realtimeTalkInputDeviceId = currentRealtimeTalkInput(state);
118118
const selectedDeviceMissing =
119+
requestPermission &&
120+
result.warning === null &&
119121
state.realtimeTalkInputDeviceId.length > 0 &&
120122
result.devices.length > 0 &&
121123
!result.devices.some((device) => device.deviceId === state.realtimeTalkInputDeviceId);

‎ui/src/pages/chat/chat-view.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,27 @@ describe("chat voice controls", () => {
18231823
expect(onRealtimeTalkInputSelect).toHaveBeenCalledWith("built-in");
18241824
});
18251825

1826+
it("keeps a persisted microphone visible before discovery can name it", () => {
1827+
const container = renderVoiceOptions({
1828+
realtimeTalkInputDeviceId: "persisted-microphone",
1829+
});
1830+
1831+
const microphone = container.querySelector<HTMLSelectElement>(
1832+
'[data-talk-select="microphone"] select',
1833+
);
1834+
expect(microphone).toBeInstanceOf(HTMLSelectElement);
1835+
expect(
1836+
Array.from(microphone?.options ?? []).map((option) => ({
1837+
label: option.textContent?.trim(),
1838+
value: option.value,
1839+
})),
1840+
).toEqual([
1841+
{ label: "System default", value: "" },
1842+
{ label: "Microphone 1", value: "persisted-microphone" },
1843+
]);
1844+
expect(microphone?.value).toBe("persisted-microphone");
1845+
});
1846+
18261847
it("shows microphone loading, empty, and error states without hiding System default", () => {
18271848
const loading = renderVoiceOptions({
18281849
realtimeTalkInputLoading: true,

‎ui/src/pages/chat/components/chat-realtime-controls.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,21 @@ function renderRealtimeTalkInputSetting(props: ChatRealtimeTalkOptionsProps) {
9898
return nothing;
9999
}
100100
const devices = props.realtimeTalkInputDevices ?? [];
101+
const selectedDeviceId = props.realtimeTalkInputDeviceId?.trim() ?? "";
102+
const selectedDeviceKnown = devices.some((device) => device.deviceId === selectedDeviceId);
101103
const options = [
102104
{ label: t("chat.composer.systemDefaultMicrophone"), value: "" },
103105
...devices.map((device) => ({ label: device.label, value: device.deviceId })),
106+
...(selectedDeviceId && !selectedDeviceKnown
107+
? [
108+
{
109+
label: t("chat.composer.microphoneFallback", {
110+
number: String(devices.length + 1),
111+
}),
112+
value: selectedDeviceId,
113+
},
114+
]
115+
: []),
104116
];
105117
const refreshLabel = `${t("common.refresh")}: ${t("chat.composer.microphoneInput")}`;
106118
return html`
@@ -109,7 +121,7 @@ function renderRealtimeTalkInputSetting(props: ChatRealtimeTalkOptionsProps) {
109121
${renderNativeTalkSelect({
110122
id: "microphone",
111123
label: t("chat.composer.microphoneInput"),
112-
value: props.realtimeTalkInputDeviceId ?? "",
124+
value: selectedDeviceId,
113125
options,
114126
onSelect: props.onRealtimeTalkInputSelect,
115127
})}

0 commit comments

Comments
 (0)