1- import { AuthStandardClientScopes , EnvironmentId } from "@t3tools/contracts" ;
1+ import {
2+ AuthStandardClientScopes ,
3+ EnvironmentId ,
4+ ORCHESTRATION_PROTOCOL_VERSION ,
5+ } from "@t3tools/contracts" ;
26import { type RelayEnvironmentConnectResponse } from "@t3tools/contracts/relay" ;
37import { describe , expect , it } from "@effect/vitest" ;
48import * as Deferred from "effect/Deferred" ;
@@ -15,6 +19,11 @@ import * as RpcHttp from "../rpc/http.ts";
1519import * as ClientCapabilities from "../platform/capabilities.ts" ;
1620import * as RemoteEnvironmentAuthorization from "./service.ts" ;
1721import * as TokenStore from "./tokenStore.ts" ;
22+ import { BearerConnectionCredential , BearerConnectionProfile } from "../connection/catalog.ts" ;
23+ import * as ConnectionCredentialStore from "../connection/credentialStore.ts" ;
24+ import { BearerConnectionTarget } from "../connection/model.ts" ;
25+ import * as ConnectionProfileStore from "../connection/profileStore.ts" ;
26+ import * as ConnectionResolver from "../connection/resolver.ts" ;
1827
1928const ENVIRONMENT_ID = EnvironmentId . make ( "environment-1" ) ;
2029const ENDPOINT = {
@@ -186,7 +195,7 @@ const makeHarness = Effect.fn("TestRemoteAuthorization.makeHarness")(function* (
186195 resetTokenCache : Effect . void ,
187196 } ) ;
188197 const layer = RemoteEnvironmentAuthorization . layer . pipe (
189- Layer . provide (
198+ Layer . provideMerge (
190199 Layer . mergeAll (
191200 RpcHttp . layerRemoteHttpClient ( fetch . fetchFn ) ,
192201 Layer . succeed ( ManagedRelay . ManagedRelayDpopSigner , signer ) ,
@@ -225,13 +234,124 @@ const makeHarness = Effect.fn("TestRemoteAuthorization.makeHarness")(function* (
225234 } ;
226235} ) ;
227236
237+ const prepareDirectBearer = Effect . fn ( "TestRemoteAuthorization.prepareDirectBearer" ) ( function * (
238+ harness : Effect . Success < ReturnType < typeof makeHarness > > ,
239+ times : number ,
240+ ) {
241+ const target = new BearerConnectionTarget ( {
242+ environmentId : ENVIRONMENT_ID ,
243+ label : DESCRIPTOR . label ,
244+ connectionId : "direct-1" ,
245+ } ) ;
246+ const entry = {
247+ target,
248+ profile : Option . some (
249+ new BearerConnectionProfile ( {
250+ connectionId : target . connectionId ,
251+ environmentId : ENVIRONMENT_ID ,
252+ label : target . label ,
253+ httpBaseUrl : ENDPOINT . httpBaseUrl ,
254+ wsBaseUrl : ENDPOINT . wsBaseUrl ,
255+ } ) ,
256+ ) ,
257+ enabled : true ,
258+ } ;
259+ const unused = ( ) => Effect . die ( "unused" ) ;
260+ const layer = ConnectionResolver . layer . pipe (
261+ Layer . provide (
262+ Layer . mergeAll (
263+ harness . layer ,
264+ Layer . succeed ( ConnectionProfileStore . ConnectionProfileStore , {
265+ get : unused ,
266+ put : unused ,
267+ remove : unused ,
268+ } ) ,
269+ Layer . succeed ( ConnectionCredentialStore . ConnectionCredentialStore , {
270+ get : ( ) => Effect . succeedSome ( new BearerConnectionCredential ( { token : "bearer-token" } ) ) ,
271+ put : unused ,
272+ remove : unused ,
273+ } ) ,
274+ Layer . succeed ( ClientCapabilities . PrimaryEnvironmentAuth , {
275+ bearerToken : Effect . succeedNone ,
276+ } ) ,
277+ Layer . succeed ( ClientCapabilities . SshEnvironmentGateway , {
278+ provision : unused ,
279+ prepare : unused ,
280+ disconnect : unused ,
281+ } ) ,
282+ ) ,
283+ ) ,
284+ ) ;
285+ const resolver = yield * ConnectionResolver . ConnectionResolver . pipe ( Effect . provide ( layer ) ) ;
286+ return yield * Effect . forEach ( Array . from ( { length : times } ) , ( ) =>
287+ resolver . prepare ( entry ) . pipe ( Effect . result ) ,
288+ ) ;
289+ } ) ;
290+
228291describe ( "RemoteEnvironmentAuthorization" , ( ) => {
229- it . effect ( "reuses a validated bearer descriptor while issuing fresh websocket tickets" , ( ) =>
292+ it . effect ( "prepares a cold direct bearer connection with one descriptor request" , ( ) =>
293+ Effect . gen ( function * ( ) {
294+ const harness = yield * makeHarness ( {
295+ responses : [
296+ Response . json ( {
297+ ...DESCRIPTOR ,
298+ orchestrationProtocolVersion : ORCHESTRATION_PROTOCOL_VERSION ,
299+ } ) ,
300+ websocketTicket ( "cold-ticket" ) ,
301+ ] ,
302+ } ) ;
303+
304+ const [ prepared ] = yield * prepareDirectBearer ( harness , 1 ) ;
305+
306+ expect ( prepared ) . toMatchObject ( {
307+ _tag : "Success" ,
308+ success : { socketUrl : expect . stringContaining ( "wsTicket=cold-ticket" ) } ,
309+ } ) ;
310+ expect ( harness . fetch . calls . map ( ( [ url ] ) => new URL ( String ( url ) ) . pathname ) ) . toEqual ( [
311+ "/.well-known/t3/environment" ,
312+ "/api/auth/websocket-ticket/rpc" ,
313+ ] ) ;
314+ } ) ,
315+ ) ;
316+
317+ it . effect ( "blocks a warm direct bearer prepare when the environment changes identity" , ( ) =>
318+ Effect . gen ( function * ( ) {
319+ const harness = yield * makeHarness ( {
320+ responses : [
321+ Response . json ( {
322+ ...DESCRIPTOR ,
323+ orchestrationProtocolVersion : ORCHESTRATION_PROTOCOL_VERSION ,
324+ } ) ,
325+ websocketTicket ( "first-ticket" ) ,
326+ Response . json ( {
327+ ...DESCRIPTOR ,
328+ environmentId : "another-environment" ,
329+ orchestrationProtocolVersion : ORCHESTRATION_PROTOCOL_VERSION ,
330+ } ) ,
331+ ] ,
332+ } ) ;
333+
334+ const [ first , second ] = yield * prepareDirectBearer ( harness , 2 ) ;
335+
336+ expect ( first ?. _tag ) . toBe ( "Success" ) ;
337+ expect ( second ) . toMatchObject ( {
338+ _tag : "Failure" ,
339+ failure : { _tag : "ConnectionBlockedError" , reason : "configuration" } ,
340+ } ) ;
341+ expect (
342+ harness . fetch . calls . filter ( ( [ url ] ) =>
343+ String ( url ) . endsWith ( "/api/auth/websocket-ticket/rpc" ) ,
344+ ) ,
345+ ) . toHaveLength ( 1 ) ;
346+ } ) ,
347+ ) ;
348+ it . effect ( "validates a fresh bearer descriptor while issuing fresh websocket tickets" , ( ) =>
230349 Effect . gen ( function * ( ) {
231350 const harness = yield * makeHarness ( {
232351 responses : [
233352 Response . json ( DESCRIPTOR ) ,
234353 websocketTicket ( "first-ticket" ) ,
354+ Response . json ( DESCRIPTOR ) ,
235355 websocketTicket ( "second-ticket" ) ,
236356 ] ,
237357 } ) ;
@@ -253,7 +373,7 @@ describe("RemoteEnvironmentAuthorization", () => {
253373 expect ( second . socketUrl ) . toContain ( "wsTicket=second-ticket" ) ;
254374 expect (
255375 harness . fetch . calls . filter ( ( [ url ] ) => String ( url ) . endsWith ( "/.well-known/t3/environment" ) ) ,
256- ) . toHaveLength ( 1 ) ;
376+ ) . toHaveLength ( 2 ) ;
257377 expect (
258378 harness . fetch . calls . filter ( ( [ url ] ) =>
259379 String ( url ) . endsWith ( "/api/auth/websocket-ticket/rpc" ) ,
0 commit comments