@@ -132,14 +132,17 @@ const FAMILY_SNAPSHOT: OrchestrationV2ShellSnapshot = {
132132const makeFamilyHarness = Effect . fn ( "test.makeFamilyHarness" ) ( function * ( input : {
133133 readonly cached : Option . Option < DeferredShellSnapshot > ;
134134 readonly initialVersion : string ;
135+ readonly snapshot ?: DeferredShellSnapshot ;
135136} ) {
136137 const subscribeInputs = yield * Queue . unbounded < unknown > ( ) ;
137138 const saved = yield * Queue . unbounded < { readonly verifiedServerVersion ?: string } > ( ) ;
138139 const httpLoads = yield * Ref . make ( 0 ) ;
139- const familySession = ( serverVersion : string ) : RpcSession . RpcSession => {
140- const events = Stream . make < ReadonlyArray < OrchestrationV2ShellStreamItem > > ( {
140+ const familySession = (
141+ serverVersion : string ,
142+ events = Stream . make < ReadonlyArray < OrchestrationV2ShellStreamItem > > ( {
141143 kind : "synchronized" ,
142- } ) . pipe ( Stream . concat ( Stream . never ) ) ;
144+ } ) . pipe ( Stream . concat ( Stream . never ) ) ,
145+ ) : RpcSession . RpcSession => {
143146 const client = {
144147 [ ORCHESTRATION_V2_WS_METHODS . subscribeShell ] : ( subscribeInput : unknown ) =>
145148 Stream . unwrap ( Queue . offer ( subscribeInputs , subscribeInput ) . pipe ( Effect . as ( events ) ) ) ,
@@ -181,7 +184,9 @@ const makeFamilyHarness = Effect.fn("test.makeFamilyHarness")(function* (input:
181184 } ) ;
182185 const snapshotLoader = ShellSnapshotLoader . ShellSnapshotLoader . of ( {
183186 load : ( ) =>
184- Ref . update ( httpLoads , ( count ) => count + 1 ) . pipe ( Effect . as ( Option . some ( FAMILY_SNAPSHOT ) ) ) ,
187+ Ref . update ( httpLoads , ( count ) => count + 1 ) . pipe (
188+ Effect . as ( Option . some ( input . snapshot ?? FAMILY_SNAPSHOT ) ) ,
189+ ) ,
185190 } ) ;
186191 const shellState = yield * makeEnvironmentShellState ( ) . pipe (
187192 Effect . provideService ( EnvironmentSupervisor . EnvironmentSupervisor , supervisor ) ,
@@ -194,7 +199,7 @@ const makeFamilyHarness = Effect.fn("test.makeFamilyHarness")(function* (input:
194199 Stream . runHead ,
195200 Effect . map ( Option . getOrThrow ) ,
196201 ) ;
197- return { sessions, familySession, subscribeInputs, saved, httpLoads, stateWhere } ;
202+ return { sessions, familySession, subscribeInputs, saved, httpLoads, stateWhere, shellState } ;
198203} ) ;
199204
200205describe ( "environment shell synchronization" , ( ) => {
@@ -823,6 +828,61 @@ describe("environment shell synchronization", () => {
823828 } ) ,
824829 ) ;
825830
831+ it . effect (
832+ "does not verify a replacement session with a buffered snapshot from its predecessor" ,
833+ ( ) =>
834+ Effect . gen ( function * ( ) {
835+ const links =
836+ yield * Deferred . make < ReadonlyMap < ThreadId , ReadonlyArray < ThreadPullRequestLink > > > ( ) ;
837+ const h = yield * makeFamilyHarness ( {
838+ cached : Option . none ( ) ,
839+ initialVersion : "1.0.0" ,
840+ snapshot : { ...FAMILY_SNAPSHOT , loadPullRequests : Deferred . await ( links ) } ,
841+ } ) ;
842+ yield * Queue . take ( h . subscribeInputs ) ;
843+ yield * h . stateWhere ( ( state ) => state . status === "live" ) ;
844+ const configStarted = yield * Deferred . make < void > ( ) ;
845+ const configGate = yield * Deferred . make < void > ( ) ;
846+ const replacement = h . familySession ( "2.0.0" ) ;
847+ const next = {
848+ ...replacement ,
849+ initialConfig : Deferred . succeed ( configStarted , undefined ) . pipe (
850+ Effect . andThen ( Deferred . await ( configGate ) ) ,
851+ Effect . andThen ( replacement . initialConfig ) ,
852+ ) ,
853+ } ;
854+ const events = yield * Queue . unbounded < OrchestrationV2ShellStreamItem > ( ) ;
855+ const old = h . familySession ( "1.0.0" , Stream . fromQueue ( events ) ) ;
856+ yield * SubscriptionRef . set ( h . sessions , Option . some ( old ) ) ;
857+ yield * Queue . take ( h . subscribeInputs ) ;
858+ const held = yield * Deferred . make < void > ( ) ;
859+ const release = yield * Deferred . make < void > ( ) ;
860+ const writer = yield * Effect . forkChild (
861+ h . shellState . semaphore . withPermit (
862+ Deferred . succeed ( held , undefined ) . pipe ( Effect . andThen ( Deferred . await ( release ) ) ) ,
863+ ) ,
864+ ) ;
865+ yield * Deferred . await ( held ) ;
866+ yield * Deferred . succeed ( links , new Map ( ) ) ;
867+ yield * TestClock . adjust ( "1 millis" ) ;
868+ yield * Queue . offer ( events , {
869+ kind : "snapshot" ,
870+ snapshot : { ...FAMILY_SNAPSHOT , snapshotSequence : 99 } ,
871+ } ) ;
872+ yield * TestClock . adjust ( "1 millis" ) ;
873+ yield * SubscriptionRef . set ( h . sessions , Option . some ( next ) ) ;
874+ yield * Deferred . await ( configStarted ) ;
875+ yield * Deferred . succeed ( release , undefined ) ;
876+ yield * Fiber . join ( writer ) ;
877+ yield * h . stateWhere ( ( state ) =>
878+ Option . exists ( state . snapshot , ( snapshot ) => snapshot . snapshotSequence === 99 ) ,
879+ ) ;
880+ yield * Deferred . succeed ( configGate , undefined ) ;
881+ expect ( yield * Queue . take ( h . subscribeInputs ) ) . toMatchObject ( { afterSequence : 1 } ) ;
882+ expect ( yield * Ref . get ( h . httpLoads ) ) . toBe ( 2 ) ;
883+ } ) ,
884+ ) ;
885+
826886 it . effect ( "resumes the cached snapshot on a cold start of the build that verified it" , ( ) =>
827887 Effect . gen ( function * ( ) {
828888 const cached = { ...FAMILY_SNAPSHOT , verifiedServerVersion : "1.0.0" } ;
0 commit comments