@@ -40,8 +40,13 @@ vi.mock("../../state/environments", async (importOriginal) => ({
4040 } ) ,
4141} ) ) ;
4242
43- it ( "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" ) ;
43+ const id = EnvironmentId . make ( "voice-host" ) ;
44+ const grantKey = "t3code:voice-audio-grant:v1" ;
45+
46+ async function withVoicePanel (
47+ route : ( url : URL , request : RequestInfo | URL , init ?: RequestInit ) => Promise < Response > | null ,
48+ body : ( screen : Awaited < ReturnType < typeof render > > ) => Promise < void > ,
49+ ) {
4550 const target = new PrimaryConnectionTarget ( {
4651 environmentId : id ,
4752 label : "Laptop" ,
@@ -56,25 +61,10 @@ it("enables the chosen provider only after a successful setup and keeps its gran
5661 } ) ;
5762 using cleanup = new DisposableStack ( ) ;
5863 cleanup . defer ( ( ) => chooseAudioDestination ( null ) ) ;
59- chooseAudioDestination ( { kind : "environment" , environmentId : id } ) ;
60- const writes : unknown [ ] = [ ] ;
61- let rejectNextWrite = true ;
6264 const fetch = globalThis . fetch ;
6365 using _network = vi . spyOn ( globalThis , "fetch" ) . mockImplementation ( ( request , init ) => {
6466 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 ) ;
67+ return route ( url , request , init ) ?? fetch ( request , init ) ;
7868 } ) ;
7969 await vi . mocked ( useEnvironmentScope ) . withImplementation (
8070 ( ) => true ,
@@ -87,141 +77,169 @@ it("enables the chosen provider only after a successful setup and keeps its gran
8777 < VoiceSettingsPanel />
8878 </ SettingsScopeProvider > ,
8979 ) ;
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" ) ;
80+ await body ( screen ) ;
12881 } ,
12982 ) ;
13083 } ,
13184 ) ;
85+ }
86+
87+ it ( "enables the chosen provider only after a successful setup" , async ( ) => {
88+ chooseAudioDestination ( { kind : "environment" , environmentId : id } ) ;
89+ const writes : unknown [ ] = [ ] ;
90+ let rejectNextWrite = true ;
91+ await withVoicePanel (
92+ ( url , request , init ) => {
93+ if ( url . pathname === "/api/voice/cloud/keys" )
94+ return Promise . resolve ( Response . json ( { openai : writes . length > 0 , xai : true } ) ) ;
95+ if ( url . pathname !== "/api/voice/cloud/openai/key" ) return null ;
96+ if ( rejectNextWrite ) {
97+ rejectNextWrite = false ;
98+ return Promise . resolve ( new Response ( null , { status : 403 } ) ) ;
99+ }
100+ return new Request ( request , init ) . json ( ) . then ( ( body ) => {
101+ writes . push ( body ) ;
102+ return new Response ( null , { status : 204 } ) ;
103+ } ) ;
104+ } ,
105+ async ( screen ) => {
106+ const destination = screen . getByRole ( "combobox" , { name : "Transcribe with" } ) ;
107+ await expect . element ( destination ) . toHaveTextContent ( "Laptop" ) ;
108+ await destination . click ( ) ;
109+ await page . getByRole ( "option" , { name : "OpenAI via Laptop" } ) . click ( ) ;
110+ const dialog = page . getByRole ( "dialog" ) ;
111+ await dialog . getByRole ( "button" , { name : "Cancel" } ) . click ( ) ;
112+ await expect . element ( destination ) . toHaveTextContent ( "Laptop" ) ;
113+ expect ( writes ) . toEqual ( [ ] ) ;
114+ await destination . click ( ) ;
115+ await page . getByRole ( "option" , { name : "OpenAI via Laptop" } ) . click ( ) ;
116+ await dialog . getByRole ( "combobox" , { name : "OpenAI API key source" } ) . click ( ) ;
117+ await page . getByRole ( "option" , { name : "Work vault" } ) . click ( ) ;
118+ await dialog . getByLabelText ( "OpenAI API key reference" ) . fill ( "op://Work/Voice/key" ) ;
119+ await userEvent . keyboard ( "{Enter}" ) ;
120+ await expect . element ( dialog . getByRole ( "alert" ) ) . toBeVisible ( ) ;
121+ expect ( writes ) . toEqual ( [ ] ) ;
122+ await expect . element ( dialog . getByRole ( "button" , { name : "Save and enable" } ) ) . toBeEnabled ( ) ;
123+ await dialog . getByLabelText ( "OpenAI API key reference" ) . click ( ) ;
124+ await userEvent . keyboard ( "{Enter}" ) ;
125+ await expect . element ( dialog ) . not . toBeInTheDocument ( ) ;
126+ await expect . element ( destination ) . toHaveTextContent ( "OpenAI via Laptop" ) ;
127+ expect ( writes ) . toEqual ( [ { key : { provider : "work" , reference : "op://Work/Voice/key" } } ] ) ;
128+ } ,
129+ ) ;
132130} ) ;
133131
134- it ( "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 ) ;
132+ it ( "keeps the active provider's grant when editing its key" , async ( ) => {
133+ chooseAudioDestination ( { kind : "cloud" , provider : "openai" , environmentId : id } ) ;
134+ const grant = window . localStorage . getItem ( grantKey ) ;
151135 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" )
136+ await withVoicePanel (
137+ ( url , request , init ) => {
138+ if ( url . pathname === "/api/voice/cloud/keys" )
139+ return Promise . resolve ( Response . json ( { openai : true , xai : true } ) ) ;
140+ if ( url . pathname !== "/api/voice/cloud/openai/key" ) return null ;
167141 return new Request ( request , init ) . json ( ) . then ( ( body ) => {
168142 writes . push ( body ) ;
169143 return new Response ( null , { status : 204 } ) ;
170144 } ) ;
171- return fetch ( request , init ) ;
172- } ) ;
173- const grantKey = "t3code:voice-audio-grant:v1" ;
145+ } ,
146+ async ( screen ) => {
147+ await screen . getByRole ( "button" , { name : "Manage key" } ) . click ( ) ;
148+ const dialog = page . getByRole ( "dialog" ) ;
149+ await dialog . getByLabelText ( "OpenAI API key" , { exact : true } ) . fill ( "replacement" ) ;
150+ await userEvent . keyboard ( "{Enter}" ) ;
151+ await expect . element ( dialog ) . not . toBeInTheDocument ( ) ;
152+ expect ( writes ) . toEqual ( [ { key : "replacement" } ] ) ;
153+ expect ( window . localStorage . getItem ( grantKey ) ) . toBe ( grant ) ;
154+ } ,
155+ ) ;
156+ } ) ;
157+
158+ it ( "uses a provider's saved key without writing it, and turns dictation off" , async ( ) => {
159+ chooseAudioDestination ( { kind : "environment" , environmentId : id } ) ;
160+ await withVoicePanel (
161+ ( url ) =>
162+ url . pathname === "/api/voice/cloud/keys"
163+ ? Promise . resolve ( Response . json ( { openai : false , xai : true } ) )
164+ : null ,
165+ async ( screen ) => {
166+ const destination = screen . getByRole ( "combobox" , { name : "Transcribe with" } ) ;
167+ await destination . click ( ) ;
168+ await page . getByRole ( "option" , { name : "Grok Voice Transcribe via Laptop" } ) . click ( ) ;
169+ await page
170+ . getByRole ( "dialog" )
171+ . getByRole ( "button" , { name : "Use Grok Voice Transcribe" } )
172+ . click ( ) ;
173+ await expect . element ( destination ) . toHaveTextContent ( "Grok Voice Transcribe via Laptop" ) ;
174+ await destination . click ( ) ;
175+ await page . getByRole ( "option" , { name : "Off" , exact : true } ) . click ( ) ;
176+ await expect . element ( destination ) . toHaveTextContent ( "Off" ) ;
177+ } ,
178+ ) ;
179+ } ) ;
180+
181+ it ( "OpenRouter needs an exact model, and changing it writes no credential" , async ( ) => {
182+ chooseAudioDestination ( null ) ;
183+ const writes : unknown [ ] = [ ] ;
174184 const grantModel = ( ) => {
175185 const raw = window . localStorage . getItem ( grantKey ) ;
176186 const parsed = raw ? ( JSON . parse ( raw ) as { destination ?: { model ?: string } } ) : null ;
177187 return parsed ?. destination ?. model ?? null ;
178188 } ;
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 : / O t h e r : L i s t e n 2 / } ) ) . toBeVisible ( ) ;
201- await page . getByRole ( "option" , { name : / O t h e r : L i s t e n 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 : / A c m e : T r a n s c r i b e 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- ) ;
189+ await withVoicePanel (
190+ ( url , request , init ) => {
191+ if ( url . pathname === "/api/voice/cloud/keys" )
192+ return Promise . resolve ( Response . json ( { openai : false , xai : false , openrouter : true } ) ) ;
193+ if ( url . pathname === "/api/voice/cloud/openrouter/models" )
194+ return Promise . resolve (
195+ Response . json ( {
196+ models : [
197+ { id : "acme/transcribe-1" , name : "Acme: Transcribe 1" } ,
198+ { id : "other/listen-2" , name : "Other: Listen 2" } ,
199+ ] ,
200+ } ) ,
201+ ) ;
202+ if ( url . pathname === "/api/voice/cloud/openrouter/key" )
203+ return new Request ( request , init ) . json ( ) . then ( ( body ) => {
204+ writes . push ( body ) ;
205+ return new Response ( null , { status : 204 } ) ;
206+ } ) ;
207+ return null ;
208+ } ,
209+ async ( screen ) => {
210+ const destination = screen . getByRole ( "combobox" , { name : "Transcribe with" } ) ;
211+ await destination . click ( ) ;
212+ await page . getByRole ( "option" , { name : "OpenRouter via Laptop" } ) . click ( ) ;
213+ const dialog = page . getByRole ( "dialog" ) ;
214+ const save = dialog . getByRole ( "button" , { name : "Save and enable" } ) ;
215+ await expect . element ( save ) . toBeDisabled ( ) ;
216+ const picker = dialog . getByRole ( "combobox" , { name : "OpenRouter model" } ) ;
217+ await expect . element ( picker ) . toBeVisible ( ) ;
218+ await picker . click ( ) ;
219+ await userEvent . keyboard ( "listen" ) ;
220+ await expect . element ( page . getByRole ( "option" , { name : / O t h e r : L i s t e n 2 / } ) ) . toBeVisible ( ) ;
221+ await page . getByRole ( "option" , { name : / O t h e r : L i s t e n 2 / } ) . click ( ) ;
222+ await expect . element ( save ) . toBeEnabled ( ) ;
223+ const key = dialog . getByLabelText ( "OpenRouter API key" , { exact : true } ) ;
224+ await key . fill ( "op://not-a-literal-key" ) ;
225+ await expect . element ( save ) . toBeDisabled ( ) ;
226+ await key . fill ( "" ) ;
227+ await save . click ( ) ;
228+ await expect . element ( dialog ) . not . toBeInTheDocument ( ) ;
229+ await expect . element ( destination ) . toHaveTextContent ( "OpenRouter via Laptop" ) ;
230+ expect ( grantModel ( ) ) . toBe ( "other/listen-2" ) ;
231+ expect ( writes ) . toEqual ( [ ] ) ;
232+ await screen . getByRole ( "button" , { name : "Manage OpenRouter" } ) . click ( ) ;
233+ const current = dialog . getByRole ( "combobox" , { name : "OpenRouter model" } ) ;
234+ await expect . element ( current ) . toHaveValue ( "Other: Listen 2" ) ;
235+ await userEvent . clear ( current ) ;
236+ await userEvent . type ( current , "acme" ) ;
237+ await page . getByRole ( "option" , { name : / A c m e : T r a n s c r i b e 1 / } ) . click ( ) ;
238+ await dialog . getByRole ( "button" , { name : "Save" } ) . click ( ) ;
239+ await expect . element ( dialog ) . not . toBeInTheDocument ( ) ;
240+ await expect . element ( destination ) . toHaveTextContent ( "OpenRouter via Laptop" ) ;
241+ expect ( grantModel ( ) ) . toBe ( "acme/transcribe-1" ) ;
242+ expect ( writes ) . toEqual ( [ ] ) ;
225243 } ,
226244 ) ;
227245} ) ;
0 commit comments