Open History to see the last commit
1import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection";
2import { EnvironmentId } from "@t3tools/contracts";
3import * as Option from "effect/Option";
4import { expect, it, vi } from "vite-plus/test";
5import { page, userEvent } from "vite-plus/test/browser";
6import { render } from "../../../test/render";
7import "~/index.css";
8import { chooseAudioDestination } from "../../speech/voiceConsent";
9import { useEnvironmentScope, usePreparedConnection } from "../../state/session";
10import { VoiceSettingsPanel } from "./VoiceSettingsPanel";
11import { SettingsScopeProvider } from "./SettingsScopeContext";
12
13vi.mock(import("../../state/session"), { spy: true });
14vi.mock("../../env", async (original) => ({
15 ...(await original<typeof import("../../env")>()),
16 isSecretsFeatureEnabled: true,
17}));
18vi.mock("../../state/environments", async (importOriginal) => ({
19 ...(await importOriginal<typeof import("../../state/environments")>()),
20 useEnvironments: () => ({
21 environments: [
22 {
23 environmentId: "voice-host",
24 label: "Laptop",
25 connection: { phase: "available" },
26 serverConfig: {
27 secretReferences: true,
28 settings: {
29 secretProviders: {
30 work: { kind: "one_password", label: "Work vault", config: { account: "work" } },
31 },
32 },
33 environment: {
34 platform: { os: "linux", arch: "x64" },
35 capabilities: { voiceTranscription: true, cloudVoiceTranscription: true },
36 },
37 },
38 },
39 ],
40 }),
41}));
42
43it("enables the chosen provider only after a successful setup and keeps its grant when editing the key", async () => {
44 const id = EnvironmentId.make("voice-host");
45 const target = new PrimaryConnectionTarget({
46 environmentId: id,
47 label: "Laptop",
48 httpBaseUrl: window.location.origin,
49 wsBaseUrl: "ws://localhost",
50 });
51 const prepared = Option.some({
52 ...target,
53 target,
54 socketUrl: target.wsBaseUrl,
55 httpAuthorization: null,
56 });
57 using cleanup = new DisposableStack();
58 cleanup.defer(() => chooseAudioDestination(null));
59 chooseAudioDestination({ kind: "environment", environmentId: id });
60 const writes: unknown[] = [];
61 let rejectNextWrite = true;
62 const fetch = globalThis.fetch;
63 using _network = vi.spyOn(globalThis, "fetch").mockImplementation((request, init) => {
64 const url = new URL(request instanceof Request ? request.url : request, window.location.href);
65 if (url.pathname === "/api/voice/cloud/keys")
66 return Promise.resolve(Response.json({ openai: writes.length > 0, xai: true }));
67 if (url.pathname === "/api/voice/cloud/openai/key") {
68 if (rejectNextWrite) {
69 rejectNextWrite = false;
70 return Promise.resolve(new Response(null, { status: 403 }));
71 }
72 return new Request(request, init).json().then((body) => {
73 writes.push(body);
74 return new Response(null, { status: 204 });
75 });
76 }
77 return fetch(request, init);
78 });
79 await vi.mocked(useEnvironmentScope).withImplementation(
80 () => true,
81 async () => {
82 await vi.mocked(usePreparedConnection).withImplementation(
83 () => prepared,
84 async () => {
85 await using screen = await render(
86 <SettingsScopeProvider search={{}} onChange={() => {}}>
87 <VoiceSettingsPanel />
88 </SettingsScopeProvider>,
89 );
90 const destination = screen.getByRole("combobox", { name: "Transcribe with" });
91 await expect.element(destination).toHaveTextContent("Laptop");
92 await destination.click();
93 await page.getByRole("option", { name: "OpenAI via Laptop" }).click();
94 const dialog = page.getByRole("dialog");
95 await dialog.getByRole("button", { name: "Cancel" }).click();
96 await expect.element(destination).toHaveTextContent("Laptop");
97 expect(writes).toEqual([]);
98 await destination.click();
99 await page.getByRole("option", { name: "OpenAI via Laptop" }).click();
100 await dialog.getByRole("combobox", { name: "OpenAI API key source" }).click();
101 await page.getByRole("option", { name: "Work vault" }).click();
102 await dialog.getByLabelText("OpenAI API key reference").fill("op://Work/Voice/key");
103 await userEvent.keyboard("{Enter}");
104 await expect.element(dialog.getByRole("alert")).toBeVisible();
105 expect(writes).toEqual([]);
106 await expect
107 .element(dialog.getByRole("button", { name: "Save and enable" }))
108 .toBeEnabled();
109 await dialog.getByLabelText("OpenAI API key reference").click();
110 await userEvent.keyboard("{Enter}");
111 await expect.element(dialog).not.toBeInTheDocument();
112 await expect.element(destination).toHaveTextContent("OpenAI via Laptop");
113 expect(writes).toEqual([{ key: { provider: "work", reference: "op://Work/Voice/key" } }]);
114 const grant = window.localStorage.getItem("t3code:voice-audio-grant:v1");
115 await screen.getByRole("button", { name: "Manage key" }).click();
116 await dialog.getByLabelText("OpenAI API key", { exact: true }).fill("replacement");
117 await userEvent.keyboard("{Enter}");
118 await expect.element(dialog).not.toBeInTheDocument();
119 expect(writes.at(-1)).toEqual({ key: "replacement" });
120 expect(window.localStorage.getItem("t3code:voice-audio-grant:v1")).toBe(grant);
121 await destination.click();
122 await page.getByRole("option", { name: "Grok Voice Transcribe via Laptop" }).click();
123 await dialog.getByRole("button", { name: "Use Grok Voice Transcribe" }).click();
124 await expect.element(destination).toHaveTextContent("Grok Voice Transcribe via Laptop");
125 await destination.click();
126 await page.getByRole("option", { name: "Off", exact: true }).click();
127 await expect.element(destination).toHaveTextContent("Off");
128 },
129 );
130 },
131 );
132});
133
134it("OpenRouter needs an exact model, and changing it writes no credential", async () => {
135 const id = EnvironmentId.make("voice-host");
136 const target = new PrimaryConnectionTarget({
137 environmentId: id,
138 label: "Laptop",
139 httpBaseUrl: window.location.origin,
140 wsBaseUrl: "ws://localhost",
141 });
142 const prepared = Option.some({
143 ...target,
144 target,
145 socketUrl: target.wsBaseUrl,
146 httpAuthorization: null,
147 });
148 using cleanup = new DisposableStack();
149 cleanup.defer(() => chooseAudioDestination(null));
150 chooseAudioDestination(null);
151 const writes: unknown[] = [];
152 const fetch = globalThis.fetch;
153 using _network = vi.spyOn(globalThis, "fetch").mockImplementation((request, init) => {
154 const url = new URL(request instanceof Request ? request.url : request, window.location.href);
155 if (url.pathname === "/api/voice/cloud/keys")
156 return Promise.resolve(Response.json({ openai: false, xai: false, openrouter: true }));
157 if (url.pathname === "/api/voice/cloud/openrouter/models")
158 return Promise.resolve(
159 Response.json({
160 models: [
161 { id: "acme/transcribe-1", name: "Acme: Transcribe 1" },
162 { id: "other/listen-2", name: "Other: Listen 2" },
163 ],
164 }),
165 );
166 if (url.pathname === "/api/voice/cloud/openrouter/key")
167 return new Request(request, init).json().then((body) => {
168 writes.push(body);
169 return new Response(null, { status: 204 });
170 });
171 return fetch(request, init);
172 });
173 const grantKey = "t3code:voice-audio-grant:v1";
174 const grantModel = () => {
175 const raw = window.localStorage.getItem(grantKey);
176 const parsed = raw ? (JSON.parse(raw) as { destination?: { model?: string } }) : null;
177 return parsed?.destination?.model ?? null;
178 };
179 await vi.mocked(useEnvironmentScope).withImplementation(
180 () => true,
181 async () => {
182 await vi.mocked(usePreparedConnection).withImplementation(
183 () => prepared,
184 async () => {
185 await using screen = await render(
186 <SettingsScopeProvider search={{}} onChange={() => {}}>
187 <VoiceSettingsPanel />
188 </SettingsScopeProvider>,
189 );
190 const destination = screen.getByRole("combobox", { name: "Transcribe with" });
191 await destination.click();
192 await page.getByRole("option", { name: "OpenRouter via Laptop" }).click();
193 const dialog = page.getByRole("dialog");
194 const save = dialog.getByRole("button", { name: "Save and enable" });
195 await expect.element(save).toBeDisabled();
196 const picker = dialog.getByRole("combobox", { name: "OpenRouter model" });
197 await expect.element(picker).toBeVisible();
198 await picker.click();
199 await userEvent.keyboard("listen");
200 await expect.element(page.getByRole("option", { name: /Other: Listen 2/ })).toBeVisible();
201 await page.getByRole("option", { name: /Other: Listen 2/ }).click();
202 await expect.element(save).toBeEnabled();
203 const key = dialog.getByLabelText("OpenRouter API key", { exact: true });
204 await key.fill("op://not-a-literal-key");
205 await expect.element(save).toBeDisabled();
206 await key.fill("");
207 await save.click();
208 await expect.element(dialog).not.toBeInTheDocument();
209 await expect.element(destination).toHaveTextContent("OpenRouter via Laptop");
210 expect(grantModel()).toBe("other/listen-2");
211 expect(writes).toEqual([]);
212 await screen.getByRole("button", { name: "Manage OpenRouter" }).click();
213 const current = dialog.getByRole("combobox", { name: "OpenRouter model" });
214 await expect.element(current).toHaveValue("Other: Listen 2");
215 await userEvent.clear(current);
216 await userEvent.type(current, "acme");
217 await page.getByRole("option", { name: /Acme: Transcribe 1/ }).click();
218 await dialog.getByRole("button", { name: "Save" }).click();
219 await expect.element(dialog).not.toBeInTheDocument();
220 await expect.element(destination).toHaveTextContent("OpenRouter via Laptop");
221 expect(grantModel()).toBe("acme/transcribe-1");
222 expect(writes).toEqual([]);
223 },
224 );
225 },
226 );
227});