@@ -20,6 +20,7 @@ import * as Scheduler from "effect/Scheduler";
2020import * as Scope from "effect/Scope" ;
2121import * as Stream from "effect/Stream" ;
2222import * as SubscriptionRef from "effect/SubscriptionRef" ;
23+ import * as Tracer from "effect/Tracer" ;
2324
2425import * as ClientCapabilities from "../platform/capabilities.ts" ;
2526import * as TokenStore from "../authorization/tokenStore.ts" ;
@@ -157,6 +158,9 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
157158 readonly beforeRegistrationRemove ?: (
158159 environmentId : EnvironmentId ,
159160 ) => Effect . Effect < void , Persistence . ConnectionPersistenceError > ;
161+ readonly beforeCredentialPut ?: (
162+ connectionId : string ,
163+ ) => Effect . Effect < void , ConnectionTransientError > ;
160164 readonly checkRoute ?: ( route : ConnectionRoute ) => RouteCheck ;
161165 readonly prepareRoute ?: ( target : ConnectionTarget ) => ConnectionBlockedError | undefined ;
162166 readonly beforePrepareRoute ?: (
@@ -337,11 +341,15 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
337341 Effect . map ( ( current ) => Option . fromUndefinedOr ( current . get ( connectionId ) ) ) ,
338342 ) ,
339343 put : ( connectionId , credential ) =>
340- Ref . update ( storedCredentials , ( current ) => {
341- const next = new Map ( current ) ;
342- next . set ( connectionId , credential ) ;
343- return next ;
344- } ) ,
344+ ( options ?. beforeCredentialPut ?.( connectionId ) ?? Effect . void ) . pipe (
345+ Effect . andThen (
346+ Ref . update ( storedCredentials , ( current ) => {
347+ const next = new Map ( current ) ;
348+ next . set ( connectionId , credential ) ;
349+ return next ;
350+ } ) ,
351+ ) ,
352+ ) ,
345353 remove : ( connectionId ) =>
346354 Ref . update ( storedCredentials , ( current ) => {
347355 const next = new Map ( current ) ;
@@ -499,6 +507,20 @@ function replaceRoutes(
499507 return [ ...others . slice ( 0 , insertAt ) , ...routes , ...others . slice ( insertAt ) ] ;
500508}
501509
510+ function registrySpanNames < A , E , R > ( effect : Effect . Effect < A , E , R > ) {
511+ const names : Array < string > = [ ] ;
512+ const tracer = Tracer . make ( {
513+ span : ( options ) => {
514+ if ( options . name . startsWith ( "EnvironmentRegistry." ) ) names . push ( options . name ) ;
515+ return new Tracer . NativeSpan ( options ) ;
516+ } ,
517+ } ) ;
518+ return effect . pipe (
519+ Effect . withTracer ( tracer ) ,
520+ Effect . map ( ( ) => names ) ,
521+ ) ;
522+ }
523+
502524function awaitConnectionState (
503525 registry : EnvironmentRegistry . EnvironmentRegistry [ "Service" ] ,
504526 environmentId : EnvironmentId ,
@@ -1430,7 +1452,18 @@ describe("EnvironmentRegistry", () => {
14301452
14311453 it . effect ( "a replaced platform bearer reaches a connected session's HTTP requests" , ( ) =>
14321454 Effect . gen ( function * ( ) {
1433- const harness = yield * makeHarness ( [ ] ) ;
1455+ let failCredentialWrites = false ;
1456+ const harness = yield * makeHarness ( [ ] , [ ] , [ ] , {
1457+ beforeCredentialPut : ( ) =>
1458+ failCredentialWrites
1459+ ? Effect . fail (
1460+ new ConnectionTransientError ( {
1461+ reason : "remote-unavailable" ,
1462+ detail : "Storage unavailable" ,
1463+ } ) ,
1464+ )
1465+ : Effect . void ,
1466+ } ) ;
14341467
14351468 yield * Effect . gen ( function * ( ) {
14361469 const registry = yield * EnvironmentRegistry . EnvironmentRegistry ;
@@ -1459,6 +1492,11 @@ describe("EnvironmentRegistry", () => {
14591492 ) ;
14601493 expect ( yield * preparedToken ) . toBe ( "old-bearer" ) ;
14611494
1495+ failCredentialWrites = true ;
1496+ yield * registry . reconcilePlatform ( [ registration ( "new-bearer" ) ] ) ;
1497+ expect ( yield * preparedToken ) . toBe ( "old-bearer" ) ;
1498+
1499+ failCredentialWrites = false ;
14621500 yield * registry . reconcilePlatform ( [ registration ( "new-bearer" ) ] ) ;
14631501
14641502 expect ( yield * preparedToken ) . toBe ( "new-bearer" ) ;
@@ -1556,7 +1594,18 @@ describe("EnvironmentRegistry", () => {
15561594 environmentId : TARGET . environmentId ,
15571595 label : "Shadowed relay environment" ,
15581596 } ) ;
1559- const harness = yield * makeHarness ( [ shadowedTarget ] ) ;
1597+ let failRemoval = true ;
1598+ const harness = yield * makeHarness ( [ shadowedTarget ] , [ ] , [ ] , {
1599+ beforeRegistrationRemove : ( ) =>
1600+ failRemoval
1601+ ? Effect . fail (
1602+ new Persistence . ConnectionPersistenceError ( {
1603+ operation : "remove-connection" ,
1604+ message : "Storage unavailable" ,
1605+ } ) ,
1606+ )
1607+ : Effect . void ,
1608+ } ) ;
15601609 const permissions = yield * makePullRequestRoutingPermissions ( {
15611610 read : Effect . succeed ( [ ] ) ,
15621611 write : ( ) => Effect . void ,
@@ -1568,6 +1617,10 @@ describe("EnvironmentRegistry", () => {
15681617 const registry = yield * EnvironmentRegistry . EnvironmentRegistry ;
15691618 yield * registry . reconcilePlatform ( [ new PrimaryConnectionRegistration ( { target : TARGET } ) ] ) ;
15701619 expect ( yield * permissions . get ( shadowedEntry ) ) . toBe ( "off" ) ;
1620+ expect ( hasRoutes ( yield * Ref . get ( harness . storedTargets ) , TARGET . environmentId ) ) . toBe ( true ) ;
1621+
1622+ failRemoval = false ;
1623+ yield * registry . reconcilePlatform ( [ new PrimaryConnectionRegistration ( { target : TARGET } ) ] ) ;
15711624
15721625 expect (
15731626 ( yield * SubscriptionRef . get ( registry . entries ) ) . get ( TARGET . environmentId ) ?. target ,
@@ -1794,8 +1847,13 @@ describe("EnvironmentRegistry", () => {
17941847 ) ;
17951848 yield * permissions . set ( entry , "read-write" ) ;
17961849
1797- yield * registry . reconcilePlatform ( [ registration ] ) ;
1850+ const repeated = yield * registrySpanNames (
1851+ registry . reconcilePlatform ( [
1852+ new PrimaryConnectionRegistration ( { target : new PrimaryConnectionTarget ( TARGET ) } ) ,
1853+ ] ) ,
1854+ ) ;
17981855
1856+ expect ( repeated ) . toEqual ( [ ] ) ;
17991857 expect ( yield * Ref . get ( harness . sessions ) ) . toHaveLength ( 1 ) ;
18001858 expect ( yield * permissions . get ( entry ) ) . toBe ( "read-write" ) ;
18011859
@@ -1810,18 +1868,30 @@ describe("EnvironmentRegistry", () => {
18101868 expect (
18111869 ( yield * SubscriptionRef . get ( registry . entries ) ) . get ( TARGET . environmentId ) ?. target ,
18121870 ) . toEqual ( TARGET ) ;
1813- yield * registry . reconcilePlatform ( [ ] ) ;
1814- expect ( ( yield * SubscriptionRef . get ( registry . entries ) ) . has ( TARGET . environmentId ) ) . toBe ( true ) ;
18151871
18161872 failWrites = false ;
18171873 yield * registry . reconcilePlatform ( [
18181874 new PrimaryConnectionRegistration ( { target : changedTarget } ) ,
18191875 ] ) ;
1876+ expect (
1877+ ( yield * SubscriptionRef . get ( registry . entries ) ) . get ( TARGET . environmentId ) ?. target ,
1878+ ) . toEqual ( changedTarget ) ;
18201879 expect ( yield * permissions . get ( entry ) ) . toBe ( "off" ) ;
18211880 yield * registry . reconcilePlatform ( [ registration ] ) ;
18221881 expect ( yield * permissions . get ( entry ) ) . toBe ( "off" ) ;
1882+
18231883 yield * permissions . set ( entry , "read-write" ) ;
1884+ failWrites = true ;
1885+ yield * registry . reconcilePlatform ( [ ] ) ;
1886+ expect ( ( yield * SubscriptionRef . get ( registry . entries ) ) . has ( TARGET . environmentId ) ) . toBe ( true ) ;
1887+ failWrites = false ;
18241888 yield * registry . reconcilePlatform ( [ ] ) ;
1889+ expect ( ( yield * SubscriptionRef . get ( registry . entries ) ) . has ( TARGET . environmentId ) ) . toBe (
1890+ false ,
1891+ ) ;
1892+ expect ( yield * permissions . get ( entry ) ) . toBe ( "off" ) ;
1893+ expect ( yield * registrySpanNames ( registry . reconcilePlatform ( [ ] ) ) ) . toEqual ( [ ] ) ;
1894+
18251895 yield * registry . reconcilePlatform ( [ registration ] ) ;
18261896 expect ( yield * permissions . get ( entry ) ) . toBe ( "off" ) ;
18271897 } ) . pipe (
0 commit comments