@@ -156,6 +156,119 @@ function layerBackgroundPolicy(shouldRunScopeWork: (scope: BackgroundScope) => b
156156}
157157
158158describe ( "VcsStatusBroadcaster" , ( ) => {
159+ it . effect ( "publishes a terminal branch switch without reading remote status" , ( ) => {
160+ const state = {
161+ currentLocalStatus : baseLocalStatus ,
162+ currentRemoteStatus : remoteStatusWithPr ,
163+ localStatusCalls : 0 ,
164+ remoteStatusCalls : 0 ,
165+ localInvalidationCalls : 0 ,
166+ remoteInvalidationCalls : 0 ,
167+ } ;
168+
169+ return Effect . gen ( function * ( ) {
170+ const broadcaster = yield * VcsStatusBroadcaster . VcsStatusBroadcaster ;
171+ yield * broadcaster . getStatus ( { cwd : "/repo" } ) ;
172+ const subscribed = yield * Deferred . make < void > ( ) ;
173+ const events = yield * broadcaster . streamStatus ( { cwd : "/repo" , includeRemote : false } ) . pipe (
174+ Stream . tap ( ( event ) =>
175+ event . _tag === "snapshot" ? Deferred . succeed ( subscribed , undefined ) : Effect . void ,
176+ ) ,
177+ Stream . take ( 2 ) ,
178+ Stream . runCollect ,
179+ Effect . forkScoped ,
180+ ) ;
181+ yield * Deferred . await ( subscribed ) ;
182+
183+ state . currentLocalStatus = { ...baseLocalStatus , refName : "main" } ;
184+ const refreshed = yield * broadcaster . refreshStatus ( "/repo" , { localOnly : true } ) ;
185+ assert . equal ( refreshed . refName , "main" ) ;
186+ assert . isNull ( refreshed . pr ) ;
187+ assert . equal ( state . localInvalidationCalls , 1 ) ;
188+ assert . equal ( state . remoteStatusCalls , 1 ) ;
189+ assert . equal ( state . remoteInvalidationCalls , 1 ) ;
190+ assert . deepStrictEqual ( ( yield * Fiber . join ( events ) ) [ 1 ] , {
191+ _tag : "snapshot" ,
192+ local : state . currentLocalStatus ,
193+ remote : null ,
194+ } ) ;
195+ assert . isNull ( ( yield * broadcaster . getStatus ( { cwd : "/repo" } ) ) . pr ) ;
196+ const snapshot = yield * broadcaster
197+ . streamStatus ( { cwd : "/repo" , includeRemote : false } )
198+ . pipe ( Stream . take ( 1 ) , Stream . runCollect ) ;
199+ assert . deepStrictEqual ( snapshot [ 0 ] , {
200+ _tag : "snapshot" ,
201+ local : state . currentLocalStatus ,
202+ remote : null ,
203+ } ) ;
204+ } ) . pipe ( Effect . provide ( layerTestFor ( state ) ) ) ;
205+ } ) ;
206+
207+ it . effect (
208+ "preserves cached remote status when a local-only refresh stays on the same branch" ,
209+ ( ) => {
210+ const state = {
211+ currentLocalStatus : baseLocalStatus ,
212+ currentRemoteStatus : { ...remoteStatusWithPr , aheadCount : 2 , behindCount : 3 } ,
213+ localStatusCalls : 0 ,
214+ remoteStatusCalls : 0 ,
215+ localInvalidationCalls : 0 ,
216+ remoteInvalidationCalls : 0 ,
217+ } ;
218+
219+ return Effect . gen ( function * ( ) {
220+ const broadcaster = yield * VcsStatusBroadcaster . VcsStatusBroadcaster ;
221+ const initial = yield * broadcaster . getStatus ( { cwd : "/repo" } ) ;
222+ const refreshed = yield * broadcaster . refreshStatus ( "/repo" , { localOnly : true } ) ;
223+ assert . deepStrictEqual ( refreshed , initial ) ;
224+ assert . deepStrictEqual ( yield * broadcaster . getStatus ( { cwd : "/repo" } ) , refreshed ) ;
225+ assert . equal ( state . localInvalidationCalls , 1 ) ;
226+ assert . equal ( state . remoteInvalidationCalls , 0 ) ;
227+ assert . equal ( state . remoteStatusCalls , 1 ) ;
228+ } ) . pipe ( Effect . provide ( layerTestFor ( state ) ) ) ;
229+ } ,
230+ ) ;
231+
232+ it . effect ( "clears the previous branch's remote status after an in-flight PR refresh" , ( ) => {
233+ const remoteReadStarted = Deferred . makeUnsafe < void > ( ) ;
234+ const releaseRemoteRead = Deferred . makeUnsafe < void > ( ) ;
235+ const state = {
236+ currentLocalStatus : baseLocalStatus ,
237+ currentRemoteStatus : remoteStatusWithPr ,
238+ localStatusCalls : 0 ,
239+ remoteStatusCalls : 0 ,
240+ localInvalidationCalls : 0 ,
241+ remoteInvalidationCalls : 0 ,
242+ beforeRemoteStatus : Effect . void ,
243+ } ;
244+
245+ return Effect . gen ( function * ( ) {
246+ const broadcaster = yield * VcsStatusBroadcaster . VcsStatusBroadcaster ;
247+ yield * broadcaster . getStatus ( { cwd : "/repo" } ) ;
248+ state . beforeRemoteStatus = Deferred . succeed ( remoteReadStarted , undefined ) . pipe (
249+ Effect . andThen ( Deferred . await ( releaseRemoteRead ) ) ,
250+ ) ;
251+ const remoteRefresh = yield * broadcaster
252+ . refreshPullRequestStatus ( "/repo" )
253+ . pipe ( Effect . forkScoped ) ;
254+ yield * Deferred . await ( remoteReadStarted ) ;
255+ state . currentLocalStatus = { ...baseLocalStatus , refName : "main" } ;
256+ const localRefresh = yield * broadcaster
257+ . refreshStatus ( "/repo" , { localOnly : true } )
258+ . pipe ( Effect . forkScoped ) ;
259+ yield * Deferred . succeed ( releaseRemoteRead , undefined ) ;
260+ yield * Fiber . join ( remoteRefresh ) ;
261+ yield * Fiber . join ( localRefresh ) ;
262+
263+ const status = yield * broadcaster . getStatus ( { cwd : "/repo" } ) ;
264+ assert . equal ( status . refName , "main" ) ;
265+ assert . isNull ( status . pr ) ;
266+ assert . equal ( status . aheadCount , 0 ) ;
267+ assert . equal ( status . behindCount , 0 ) ;
268+ assert . equal ( state . remoteStatusCalls , 2 ) ;
269+ } ) . pipe ( Effect . provide ( layerTestFor ( state ) ) ) ;
270+ } ) ;
271+
159272 it . effect . skipIf ( ! symlinksSupported ) (
160273 "automatically pulls an enabled clean default branch when status detects it is behind" ,
161274 ( ) => {
0 commit comments