@@ -20,15 +20,21 @@ import * as Option from "effect/Option";
2020import * as Queue from "effect/Queue" ;
2121import * as Schema from "effect/Schema" ;
2222import * as Stream from "effect/Stream" ;
23+ import * as SubscriptionRef from "effect/SubscriptionRef" ;
2324import * as TestClock from "effect/testing/TestClock" ;
2425import * as Socket from "effect/unstable/socket/Socket" ;
2526
2627import {
28+ AVAILABLE_CONNECTION_STATE ,
29+ ConnectionBlockedError ,
2730 ConnectionTransientError ,
2831 PrimaryConnectionTarget ,
2932 type PreparedConnection ,
3033} from "../connection/model.ts" ;
34+ import * as EnvironmentSupervisor from "../connection/supervisor.ts" ;
35+ import * as Persistence from "../platform/persistence.ts" ;
3136import * as RpcSession from "./session.ts" ;
37+ import { makeEnvironmentServerConfigState } from "../state/server.ts" ;
3238import { applyServerConfigProjection } from "../state/serverConfigProjection.ts" ;
3339
3440type SocketEventType = "open" | "message" | "close" | "error" ;
@@ -194,9 +200,10 @@ const makeFactory = Effect.fn("TestRpcSessionFactory.make")(function* (
194200
195201const awaitSocket = Effect . fn ( "TestRpcSessionFactory.awaitSocket" ) ( function * (
196202 sockets : ReadonlyArray < TestWebSocket > ,
203+ index = 0 ,
197204) {
198205 for ( let attempt = 0 ; attempt < 100 ; attempt += 1 ) {
199- const socket = sockets [ 0 ] ;
206+ const socket = sockets [ index ] ;
200207 if ( socket ) {
201208 return socket ;
202209 }
@@ -671,6 +678,161 @@ describe("RpcSessionFactory", () => {
671678 ) ,
672679 ) ;
673680
681+ it . effect . each ( [ { failure : "defect" as const } , { failure : "typed" as const } ] ) (
682+ "keeps durable config state alive after an owned $failure failure" ,
683+ ( { failure } ) =>
684+ Effect . scoped (
685+ Effect . gen ( function * ( ) {
686+ const { factory, sockets } = yield * makeFactory ( { environmentThemes : true } ) ;
687+ const firstSession = yield * factory . connect ( PREPARED ) ;
688+ const firstReady = yield * Effect . forkChild ( firstSession . ready ) ;
689+ const firstSocket = yield * awaitSocket ( sockets ) ;
690+ firstSocket . open ( ) ;
691+ yield * completeInitialConfig ( firstSocket , ENCODED_THEME_SERVER_CONFIG , {
692+ environmentThemes : true ,
693+ } ) ;
694+ yield * Fiber . join ( firstReady ) ;
695+
696+ const activeSession = yield * SubscriptionRef . make ( Option . some ( firstSession ) ) ;
697+ const supervisor = EnvironmentSupervisor . EnvironmentSupervisor . of ( {
698+ target : TARGET ,
699+ state : yield * SubscriptionRef . make ( AVAILABLE_CONNECTION_STATE ) ,
700+ session : activeSession ,
701+ prepared : yield * SubscriptionRef . make ( Option . some ( PREPARED ) ) ,
702+ connect : Effect . void ,
703+ disconnect : Effect . void ,
704+ retryNow : Effect . void ,
705+ } satisfies EnvironmentSupervisor . EnvironmentSupervisor [ "Service" ] ) ;
706+ const cache = Persistence . EnvironmentCacheStore . of ( {
707+ loadShell : ( ) => Effect . succeed ( Option . none ( ) ) ,
708+ saveShell : ( ) => Effect . void ,
709+ loadThread : ( ) => Effect . succeed ( Option . none ( ) ) ,
710+ saveThread : ( ) => Effect . void ,
711+ removeThread : ( ) => Effect . void ,
712+ loadServerConfig : ( ) => Effect . succeed ( Option . none ( ) ) ,
713+ saveServerConfig : ( ) => Effect . void ,
714+ loadVcsRefs : ( ) => Effect . succeed ( Option . none ( ) ) ,
715+ saveVcsRefs : ( ) => Effect . void ,
716+ removeVcsRefs : ( ) => Effect . void ,
717+ clearVcsRefs : ( ) => Effect . void ,
718+ clear : ( ) => Effect . void ,
719+ } ) ;
720+ const configState = yield * makeEnvironmentServerConfigState ( true ) . pipe (
721+ Effect . provideService ( EnvironmentSupervisor . EnvironmentSupervisor , supervisor ) ,
722+ Effect . provideService ( Persistence . EnvironmentCacheStore , cache ) ,
723+ ) ;
724+ const awaitConfig = ( predicate : ( config : ServerConfigType ) => boolean ) =>
725+ SubscriptionRef . changes ( configState ) . pipe (
726+ Stream . filter ( Option . isSome ) ,
727+ Stream . map ( ( projection ) => projection . value . config ) ,
728+ Stream . filter ( predicate ) ,
729+ Stream . runHead ,
730+ Effect . map ( Option . getOrThrow ) ,
731+ ) ;
732+
733+ const firstThemes = [
734+ {
735+ id : "first-theme" ,
736+ name : "First theme" ,
737+ appearance : "dark" as const ,
738+ canvas : "#111111" ,
739+ accent : "#ffffff" ,
740+ } ,
741+ ] ;
742+ const firstThemeState = yield * awaitConfig (
743+ ( config ) => config . environmentThemes ?. [ 0 ] ?. id === "first-theme" ,
744+ ) . pipe ( Effect . forkChild ) ;
745+ yield * publishConfigEvents ( firstSocket , [
746+ {
747+ version : 1 ,
748+ type : "environmentThemesUpdated" ,
749+ payload : { themes : firstThemes } ,
750+ } ,
751+ ] ) ;
752+ expect ( ( yield * Fiber . join ( firstThemeState ) ) . environmentThemes ) . toEqual ( firstThemes ) ;
753+
754+ const firstClosed = yield * firstSession . closed . pipe ( Effect . exit , Effect . forkChild ) ;
755+ const firstRequest = yield * awaitRequest ( firstSocket ) ;
756+ firstSocket . serverMessage (
757+ failure === "defect"
758+ ? encodeJson ( {
759+ _tag : "Defect" ,
760+ defect : encodeDefect ( new Error ( "config stream died" ) ) ,
761+ } )
762+ : encodeJson ( {
763+ _tag : "Exit" ,
764+ requestId : firstRequest . id ,
765+ exit : {
766+ _tag : "Failure" ,
767+ cause : [
768+ {
769+ _tag : "Fail" ,
770+ error : {
771+ _tag : "EnvironmentAuthorizationError" ,
772+ message : "config subscription rejected" ,
773+ requiredScope : "orchestration:read" ,
774+ } ,
775+ } ,
776+ ] ,
777+ } ,
778+ } ) ,
779+ ) ;
780+ const firstClosedExit = yield * Fiber . join ( firstClosed ) ;
781+ expect ( Exit . isFailure ( firstClosedExit ) ) . toBe ( true ) ;
782+ if ( failure === "typed" && Exit . isFailure ( firstClosedExit ) ) {
783+ expect ( Cause . squash ( firstClosedExit . cause ) ) . toBeInstanceOf ( ConnectionBlockedError ) ;
784+ expect ( Cause . squash ( firstClosedExit . cause ) ) . toMatchObject ( { reason : "permission" } ) ;
785+ }
786+ yield * SubscriptionRef . set ( activeSession , Option . none ( ) ) ;
787+
788+ const recoveredConfig = {
789+ ...THEME_SERVER_CONFIG ,
790+ environment : {
791+ ...THEME_SERVER_CONFIG . environment ,
792+ label : "Recovered environment" ,
793+ } ,
794+ } satisfies ServerConfigType ;
795+ const secondSession = yield * factory . connect ( PREPARED ) ;
796+ const secondReady = yield * Effect . forkChild ( secondSession . ready ) ;
797+ const secondSocket = yield * awaitSocket ( sockets , 1 ) ;
798+ secondSocket . open ( ) ;
799+ yield * completeInitialConfig ( secondSocket , encodeServerConfig ( recoveredConfig ) , {
800+ environmentThemes : true ,
801+ } ) ;
802+ yield * Fiber . join ( secondReady ) ;
803+
804+ const recoveredState = yield * awaitConfig (
805+ ( config ) => config . environment . label === "Recovered environment" ,
806+ ) . pipe ( Effect . forkChild ) ;
807+ yield * SubscriptionRef . set ( activeSession , Option . some ( secondSession ) ) ;
808+ expect ( ( yield * Fiber . join ( recoveredState ) ) . environmentThemes ) . toEqual ( firstThemes ) ;
809+
810+ const recoveredThemes = [
811+ {
812+ id : "recovered-theme" ,
813+ name : "Recovered theme" ,
814+ appearance : "dark" as const ,
815+ canvas : "#000000" ,
816+ accent : "#eeeeee" ,
817+ } ,
818+ ] ;
819+ const liveRecoveredState = yield * awaitConfig (
820+ ( config ) => config . environmentThemes ?. [ 0 ] ?. id === "recovered-theme" ,
821+ ) . pipe ( Effect . forkChild ) ;
822+ yield * publishConfigEvents ( secondSocket , [
823+ {
824+ version : 1 ,
825+ type : "environmentThemesUpdated" ,
826+ payload : { themes : recoveredThemes } ,
827+ } ,
828+ ] ) ;
829+ expect ( ( yield * Fiber . join ( liveRecoveredState ) ) . environmentThemes ) . toEqual (
830+ recoveredThemes ,
831+ ) ;
832+ } ) ,
833+ ) ,
834+ ) ;
835+
674836 it . effect . each < {
675837 readonly event : ServerConfigStreamEventType ;
676838 readonly expectedConfig : Partial < ServerConfigType > ;
0 commit comments