Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

VoiceInputSetup.browser.test.tsx

150 lines5.0 KB

1import { expect, it, vi } from "vite-plus/test";
2import { page } from "vite-plus/test/browser";
3​
4import { __setClientSettingsForTests, getClientSettings } from "../../hooks/useSettings";
5import { act, render } from "../../../test/render";
6import { VoiceInputSetup } from "./VoiceInputSetup";
7​
8function wizard(open: boolean) {
9 return (
10 <VoiceInputSetup
11 open={open}
12 step={1}
13 environmentId={null}
14 status={null}
15 model={null}
16 language={null}
17 downloading={false}
18 error={null}
19 onOpenChange={() => {}}
20 onRetryCatalog={null}
21 onDownload={() => {}}
22 onCancelDownload={() => {}}
23 onStartRecording={() => {}}
24 />
25 );
27​
28it.each([
29 { latest: "succeeds", showsError: false },
30 { latest: "fails", showsError: true },
31] as const)(
32 "ignores a superseded microphone refresh when the latest one $latest",
33 async ({ latest, showsError }) => {
34 await using screen = await render(wizard(false));
35 const old = Promise.withResolvers<MediaDeviceInfo[]>();
36 const current = Promise.withResolvers<MediaDeviceInfo[]>();
37 const mediaDevices = navigator.mediaDevices;
38 using _enumeration = vi
39 .spyOn(mediaDevices, "enumerateDevices")
40 .mockReturnValueOnce(old.promise)
41 .mockReturnValueOnce(current.promise);
42​
43 await screen.rerender(wizard(true));
44 const picker = page.getByRole("combobox", { name: "Microphone", exact: true });
45 const alert = page.getByRole("alert").filter({ hasText: "Couldn’t list microphones" });
46 await expect.element(picker).toBeDisabled();
47​
48 await act(async () => {
49 mediaDevices.dispatchEvent(new Event("devicechange"));
50 if (latest === "fails") current.reject(new Error("Enumeration failed"));
51 else current.resolve([]);
52 });
53 await expect.element(picker).toBeEnabled();
54​
55 await act(async () => {
56 if (latest === "succeeds") old.reject(new Error("Superseded enumeration failed"));
57 else old.resolve([]);
58 });
59 expect(alert.query() !== null).toBe(showsError);
60 },
61);
62​
63it.each([
64 {
65 name: "a saved microphone",
66 settings: {
67 voiceMicrophone: "",
68 voiceMicrophones: [
69 { kind: "device" as const, deviceId: "usb-mic-1", label: "USB Mic" },
70 { kind: "default" as const },
71 ],
72 },
73 pendingLabel: "USB Mic",
74 },
75 {
76 name: "a legacy unlabelled microphone",
77 settings: {
78 voiceMicrophone: "usb-mic-1",
79 voiceMicrophones: [],
80 },
81 pendingLabel: "Saved microphone",
82 },
83] as const)(
84 "keeps $name selectable before device ids are known",
85 async ({ settings, pendingLabel }) => {
86 const previous = getClientSettings();
87 using cleanup = new DisposableStack();
88 cleanup.defer(() => __setClientSettingsForTests(previous));
89 __setClientSettingsForTests({ ...previous, ...settings });
90​
91 const pending = Promise.withResolvers<MediaDeviceInfo[]>();
92 const mediaDevices = navigator.mediaDevices;
93 using enumeration = vi
94 .spyOn(mediaDevices, "enumerateDevices")
95 .mockReturnValueOnce(pending.promise);
96​
97 await using _screen = await render(wizard(true));
98 const picker = page.getByRole("combobox", { name: "Microphone", exact: true });
99 const unplugged = page.getByText("That microphone is unplugged. Pick another one.");
100​
101 await expect.element(picker).toBeDisabled();
102 await expect.element(picker).toHaveTextContent(pendingLabel);
103 await expect.element(unplugged).not.toBeInTheDocument();
104​
105 await act(async () => {
106 pending.resolve([
107 { deviceId: "", label: "", groupId: "", kind: "audioinput" } as MediaDeviceInfo,
108 ]);
109 });
110 await expect.element(picker).toBeEnabled();
111 await expect.element(picker).toHaveTextContent(pendingLabel);
112 await expect.element(unplugged).not.toBeInTheDocument();
113​
114 await picker.click();
115 const savedOption = page.getByRole("option", { name: pendingLabel, exact: true });
116 await expect.element(savedOption).toBeVisible();
117 await expect.element(savedOption).toHaveAttribute("aria-selected", "true");
118 await savedOption.click();
119 await expect.element(savedOption).not.toBeInTheDocument();
120 await expect.element(picker).toHaveTextContent(pendingLabel);
121​
122 enumeration.mockResolvedValueOnce([
123 {
124 deviceId: "usb-mic-1",
125 label: "USB Mic (Live)",
126 groupId: "usb",
127 kind: "audioinput",
128 } as MediaDeviceInfo,
129 ]);
130 await act(async () => {
131 mediaDevices.dispatchEvent(new Event("devicechange"));
132 });
133 await expect.element(picker).toHaveTextContent("USB Mic (Live)");
134 await expect.element(unplugged).not.toBeInTheDocument();
135​
136 enumeration.mockResolvedValueOnce([
137 {
138 deviceId: "built-in",
139 label: "Built-in Mic",
140 groupId: "built-in",
141 kind: "audioinput",
142 } as MediaDeviceInfo,
143 ]);
144 await act(async () => {
145 mediaDevices.dispatchEvent(new Event("devicechange"));
146 });
147 await expect.element(picker).toHaveTextContent("System default");
148 await expect.element(unplugged).not.toBeInTheDocument();
149 },
150);