1- import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection" ;
1+ import { PrimaryConnectionTarget , RelayConnectionTarget } from "@t3tools/client-runtime/connection" ;
2+ import type * as Effect from "effect/Effect" ;
23import {
34 EnvironmentId ,
45 type EnvironmentSpeechModel ,
@@ -19,6 +20,53 @@ import { SpeechLanguageSelect } from "./SpeechLanguageSelect";
1920import { VoiceSettingsPanel } from "./VoiceSettingsPanel" ;
2021
2122vi . mock ( import ( "../../speech/useEnvironmentSpeechInput" ) , { spy : true } ) ;
23+ // The real connection runtime authorizes relay requests with a signed-in cloud session.
24+ vi . mock ( "../../connection/runtime" , async ( ) => {
25+ const [ { ManagedRelay } , rpc , { Atom } , Context , Effect , Layer ] = await Promise . all ( [
26+ import ( "@t3tools/client-runtime/relay" ) ,
27+ import ( "@t3tools/client-runtime/rpc" ) ,
28+ import ( "effect/reactivity" ) ,
29+ import ( "effect/Context" ) ,
30+ import ( "effect/Effect" ) ,
31+ import ( "effect/Layer" ) ,
32+ ] ) ;
33+ type AuthorizedHttp =
34+ import ( "@t3tools/client-runtime/authorization" ) . AuthorizedRemoteHttpEnvironment ;
35+ // Services resolve by key, and client-runtime does not export this one.
36+ class RemoteEnvironmentAuthorization extends Context . Service <
37+ RemoteEnvironmentAuthorization ,
38+ {
39+ readonly authorizeDpopHttp : ( input : {
40+ readonly expectedEnvironmentId : AuthorizedHttp [ "environmentId" ] ;
41+ } ) => Effect . Effect < AuthorizedHttp > ;
42+ }
43+ // @effect -diagnostics-next-line deterministicKeys:off
44+ > ( ) ( "@t3tools/client-runtime/authorization/service/RemoteEnvironmentAuthorization" ) { }
45+ return {
46+ connectionAtomRuntime : Atom . runtime (
47+ Layer . mergeAll (
48+ rpc . layerRemoteHttpClient ( ( input , init ) => globalThis . fetch ( input , init ) ) ,
49+ Layer . succeed ( ManagedRelay . ManagedRelayDpopSigner , {
50+ thumbprint : Effect . succeed ( "thumbprint" ) ,
51+ createProof : ( ) => Effect . succeed ( "proof" ) ,
52+ } ) ,
53+ Layer . succeed ( RemoteEnvironmentAuthorization , {
54+ authorizeDpopHttp : ( { expectedEnvironmentId } ) =>
55+ Effect . succeed ( {
56+ environmentId : expectedEnvironmentId ,
57+ label : "MacBook Pro" ,
58+ httpBaseUrl : window . location . origin ,
59+ httpAuthorization : {
60+ _tag : "Dpop" ,
61+ accessToken : "relay-access-token" ,
62+ expiresAtEpochMs : 4_102_444_800_000 ,
63+ } ,
64+ } ) ,
65+ } ) ,
66+ ) ,
67+ ) ,
68+ } ;
69+ } ) ;
2270
2371it ( "preserves the model test transcript when its model is filtered out" , async ( ) => {
2472 const target = new PrimaryConnectionTarget ( {
@@ -157,6 +205,81 @@ it("preserves the model test transcript when its model is filtered out", async (
157205 ) ;
158206} ) ;
159207
208+ it ( "loads the models of a relay-connected destination" , async ( ) => {
209+ const target = new RelayConnectionTarget ( {
210+ environmentId : EnvironmentId . make ( "voice-relay" ) ,
211+ label : "MacBook Pro" ,
212+ } ) ;
213+ const prepared = {
214+ environmentId : target . environmentId ,
215+ label : target . label ,
216+ httpBaseUrl : window . location . origin ,
217+ socketUrl : "ws://localhost" ,
218+ httpAuthorization : {
219+ _tag : "Dpop" as const ,
220+ accessToken : "stale-access-token" ,
221+ expiresAtEpochMs : 4_102_444_800_000 ,
222+ } ,
223+ target,
224+ } ;
225+ const model : EnvironmentSpeechModel = {
226+ id : "tiny" ,
227+ name : "Tiny" ,
228+ description : "" ,
229+ size : 100 ,
230+ languages : [ "en" ] ,
231+ accuracy : 1 ,
232+ speed : 1 ,
233+ recommended : false ,
234+ supportsStreaming : false ,
235+ supportsTranslation : false ,
236+ supportsLanguageDetection : false ,
237+ active : false ,
238+ state : "downloadable" ,
239+ } ;
240+ const status = {
241+ supported : true ,
242+ state : "missing-model" ,
243+ modelId : model . id ,
244+ model : model . name ,
245+ size : model . size ,
246+ supportsStreaming : false ,
247+ supportsTranslation : false ,
248+ acceleration : "cpu" ,
249+ modelUnloadTimeout : "never" ,
250+ gpuDevices : [ ] ,
251+ } satisfies EnvironmentSpeechStatus ;
252+ const authorizations : Array < string | null > = [ ] ;
253+ const fetch = globalThis . fetch ;
254+ using _network = vi . spyOn ( globalThis , "fetch" ) . mockImplementation ( ( input , init ) => {
255+ const url = new URL ( input instanceof Request ? input . url : input , window . location . href ) ;
256+ if ( url . pathname === "/api/voice/status" ) {
257+ authorizations . push ( new Headers ( init ?. headers ) . get ( "authorization" ) ) ;
258+ return Promise . resolve ( Response . json ( status ) ) ;
259+ }
260+ if ( url . pathname === "/api/voice/models" )
261+ return Promise . resolve ( Response . json ( { models : [ model ] } ) ) ;
262+ return fetch ( input , init ) ;
263+ } ) ;
264+ using notify = vi . spyOn ( toastManager , "add" ) . mockReturnValue ( "test" ) ;
265+ // Restored even when an assertion fails, unlike `withImplementation`.
266+ using _destination = vi . mocked ( useVoiceTranscriptionDestination ) . mockImplementation ( ( ) => ( {
267+ environmentId : target . environmentId ,
268+ label : target . label ,
269+ grantId : "test" ,
270+ prepared,
271+ } ) ) ;
272+ await using screen = await render (
273+ < SettingsScopeProvider search = { { } } onChange = { ( ) => { } } >
274+ < VoiceSettingsPanel />
275+ </ SettingsScopeProvider > ,
276+ ) ;
277+ await vi . waitFor ( ( ) => expect ( authorizations . length + notify . mock . calls . length ) . not . toBe ( 0 ) ) ;
278+ expect ( notify ) . not . toHaveBeenCalled ( ) ;
279+ expect ( authorizations ) . toEqual ( [ "DPoP relay-access-token" ] ) ;
280+ await expect . element ( screen . getByText ( "Tiny" , { exact : true } ) ) . toBeVisible ( ) ;
281+ } ) ;
282+
160283it ( "recovers from a failed download progress poll without notifying, but reports a failed initial load" , async ( ) => {
161284 const target = new PrimaryConnectionTarget ( {
162285 environmentId : EnvironmentId . make ( "voice-progress" ) ,
0 commit comments