@@ -23,6 +23,7 @@ import type {
2323 VcsStatusStreamEvent ,
2424} from "@t3tools/contracts" ;
2525import { GitCommandError , GitManagerError } from "@t3tools/contracts" ;
26+ import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit" ;
2627
2728import * as VcsStatusBroadcaster from "./VcsStatusBroadcaster.ts" ;
2829import * as BackgroundPolicy from "../background/BackgroundPolicy.ts" ;
@@ -72,6 +73,9 @@ function layerTestFor(state: {
7273 localInvalidationCalls : number ;
7374 remoteInvalidationCalls : number ;
7475 remoteStatusRefreshUpstreamValues ?: Array < boolean | undefined > ;
76+ remoteStatusInteractiveValues ?: Array < boolean > ;
77+ invalidateStatusKeepLookupValues ?: Array < boolean | undefined > ;
78+ upstreamFetchCount ?: number ;
7579 remoteStatusError ?: GitManagerError ;
7680 backgroundWorkEnabled ?: boolean ;
7781 automaticFetchInterval ?: Effect . Effect < Duration . Duration > ;
@@ -113,6 +117,13 @@ function layerTestFor(state: {
113117 return state . currentRemoteStatus ;
114118 } ) ,
115119 ) ,
120+ Effect . tap ( ( ) =>
121+ Effect . gen ( function * ( ) {
122+ state . remoteStatusInteractiveValues ?. push (
123+ yield * SourceControlRateLimit . Interactive ,
124+ ) ;
125+ } ) ,
126+ ) ,
116127 Effect . tap ( ( ) =>
117128 state . remoteStatusError ? Effect . fail ( state . remoteStatusError ) : Effect . void ,
118129 ) ,
@@ -126,11 +137,13 @@ function layerTestFor(state: {
126137 Effect . sync ( ( ) => {
127138 state . remoteInvalidationCalls += 1 ;
128139 } ) ,
129- invalidateStatus : ( ) =>
140+ invalidateStatus : ( _cwd , options ) =>
130141 Effect . sync ( ( ) => {
131142 state . localInvalidationCalls += 1 ;
132143 state . remoteInvalidationCalls += 1 ;
144+ state . invalidateStatusKeepLookupValues ?. push ( options ?. keepPullRequestLookups ) ;
133145 } ) ,
146+ statusUpstreamFetchCount : ( ) => Effect . sync ( ( ) => state . upstreamFetchCount ?? 0 ) ,
134147 ...( state . pullCurrentBranch ? { pullCurrentBranch : state . pullCurrentBranch } : { } ) ,
135148 } ) ,
136149 ) ,
@@ -699,6 +712,93 @@ describe("VcsStatusBroadcaster", () => {
699712 } ) . pipe ( Effect . provide ( layerTestFor ( state ) ) , Effect . scoped ) ;
700713 } ) ;
701714
715+ it . effect ( "a checkout's PR lookup is a user action that reuses cached lookups" , ( ) => {
716+ const state : Parameters < typeof layerTestFor > [ 0 ] = {
717+ currentLocalStatus : baseLocalStatus ,
718+ currentRemoteStatus : baseRemoteStatus ,
719+ localStatusCalls : 0 ,
720+ remoteStatusCalls : 0 ,
721+ localInvalidationCalls : 0 ,
722+ remoteInvalidationCalls : 0 ,
723+ remoteStatusInteractiveValues : [ ] ,
724+ invalidateStatusKeepLookupValues : [ ] ,
725+ } ;
726+
727+ return Effect . gen ( function * ( ) {
728+ const broadcaster = yield * VcsStatusBroadcaster . VcsStatusBroadcaster ;
729+ const remotePublished = yield * Deferred . make < void > ( ) ;
730+ const subscribed = yield * Deferred . make < void > ( ) ;
731+ yield * broadcaster . streamStatus ( { cwd : "/repo" , includeRemote : false } ) . pipe (
732+ Stream . runForEach ( ( event ) => {
733+ if ( event . _tag === "snapshot" ) return Deferred . succeed ( subscribed , undefined ) ;
734+ return event . _tag === "remoteUpdated" && event . remote !== null
735+ ? Deferred . succeed ( remotePublished , undefined )
736+ : Effect . void ;
737+ } ) ,
738+ Effect . forkScoped ,
739+ ) ;
740+ yield * Deferred . await ( subscribed ) ;
741+
742+ yield * broadcaster . refreshAfterCheckout ( "/repo" ) ;
743+ yield * Deferred . await ( remotePublished ) ;
744+
745+ assert . deepStrictEqual ( state . remoteStatusInteractiveValues , [ true ] ) ;
746+ assert . deepStrictEqual ( state . invalidateStatusKeepLookupValues , [ true ] ) ;
747+ } ) . pipe ( Effect . provide ( layerTestFor ( state ) ) , Effect . scoped ) ;
748+ } ) ;
749+
750+ it . effect ( "a checkout re-reads local status only when a fetch ran" , ( ) => {
751+ const state : Parameters < typeof layerTestFor > [ 0 ] = {
752+ currentLocalStatus : baseLocalStatus ,
753+ currentRemoteStatus : baseRemoteStatus ,
754+ localStatusCalls : 0 ,
755+ remoteStatusCalls : 0 ,
756+ localInvalidationCalls : 0 ,
757+ remoteInvalidationCalls : 0 ,
758+ } ;
759+
760+ return Effect . gen ( function * ( ) {
761+ const broadcaster = yield * VcsStatusBroadcaster . VcsStatusBroadcaster ;
762+ const subscribed = yield * Deferred . make < void > ( ) ;
763+ const firstRemote = yield * Deferred . make < void > ( ) ;
764+ const secondRemote = yield * Deferred . make < void > ( ) ;
765+ yield * broadcaster . streamStatus ( { cwd : "/repo" , includeRemote : false } ) . pipe (
766+ Stream . runForEach ( ( event ) => {
767+ if ( event . _tag === "snapshot" ) return Deferred . succeed ( subscribed , undefined ) ;
768+ if ( event . _tag !== "remoteUpdated" || event . remote === null ) return Effect . void ;
769+ return event . remote . pr === null
770+ ? Deferred . succeed ( firstRemote , undefined )
771+ : Deferred . succeed ( secondRemote , undefined ) ;
772+ } ) ,
773+ Effect . forkScoped ,
774+ ) ;
775+ yield * Deferred . await ( subscribed ) ;
776+ const readsBeforeCheckout = state . localStatusCalls ;
777+
778+ yield * broadcaster . refreshAfterCheckout ( "/repo" ) ;
779+ yield * Deferred . await ( firstRemote ) ;
780+ assert . equal ( state . localStatusCalls , readsBeforeCheckout + 1 ) ;
781+
782+ const fetchedLocal : VcsStatusLocalResult = {
783+ ...baseLocalStatus ,
784+ branchChanges : { baseRef : "origin/main" , insertions : 0 , deletions : 0 } ,
785+ } ;
786+ state . currentRemoteStatus = remoteStatusWithPr ;
787+ state . beforeRemoteStatus = Effect . sync ( ( ) => {
788+ state . upstreamFetchCount = ( state . upstreamFetchCount ?? 0 ) + 1 ;
789+ state . currentLocalStatus = fetchedLocal ;
790+ } ) ;
791+ yield * broadcaster . refreshAfterCheckout ( "/repo" ) ;
792+ yield * Deferred . await ( secondRemote ) ;
793+
794+ assert . equal ( state . localStatusCalls , readsBeforeCheckout + 3 ) ;
795+ assert . deepStrictEqual (
796+ ( yield * broadcaster . getStatus ( { cwd : "/repo" } ) ) . branchChanges ,
797+ fetchedLocal . branchChanges ,
798+ ) ;
799+ } ) . pipe ( Effect . provide ( layerTestFor ( state ) ) , Effect . scoped ) ;
800+ } ) ;
801+
702802 it . effect ( "an explicit refresh reads local totals after the fetch" , ( ) => {
703803 const state : Parameters < typeof layerTestFor > [ 0 ] = {
704804 currentLocalStatus : baseLocalStatus ,
0 commit comments