@@ -50,6 +50,8 @@ interface FakeGhScenario {
5050 } ;
5151 repositoryCloneUrls ?: Record < string , { url : string ; sshUrl : string } > ;
5252 failWith ?: GitHubCli . GitHubCliError ;
53+ /** Let this many gh calls succeed before failWith kicks in (default 0 = fail immediately). */
54+ failAfterCalls ?: number ;
5355}
5456
5557function fakeGhOutput ( stdout : string ) : VcsProcess . VcsProcessOutput {
@@ -382,7 +384,7 @@ function createGitHubCliWithFakeGh(scenario: FakeGhScenario = {}): {
382384 const args = [ ...input . args ] ;
383385 ghCalls . push ( args . join ( " " ) ) ;
384386
385- if ( scenario . failWith ) {
387+ if ( scenario . failWith && ghCalls . length > ( scenario . failAfterCalls ?? 0 ) ) {
386388 return Effect . fail ( scenario . failWith ) ;
387389 }
388390
@@ -1336,6 +1338,182 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
13361338 } ) ,
13371339 ) ;
13381340
1341+ it . effect ( "status keeps the last known PR when a later lookup fails" , ( ) =>
1342+ Effect . gen ( function * ( ) {
1343+ const repoDir = yield * makeTempDir ( "t3code-git-manager-" ) ;
1344+ yield * initRepo ( repoDir ) ;
1345+ yield * runGit ( repoDir , [ "checkout" , "-b" , "feature/pr-sticky" ] ) ;
1346+ const remoteDir = yield * createBareRemote ( ) ;
1347+ yield * runGit ( repoDir , [ "remote" , "add" , "origin" , remoteDir ] ) ;
1348+ yield * runGit ( repoDir , [ "push" , "-u" , "origin" , "feature/pr-sticky" ] ) ;
1349+
1350+ const existingPr = {
1351+ number : 214 ,
1352+ title : "Sticky PR" ,
1353+ url : "https://github.com/pingdotgg/codething-mvp/pull/214" ,
1354+ baseRefName : "main" ,
1355+ headRefName : "feature/pr-sticky" ,
1356+ } ;
1357+ const { manager } = yield * makeManager ( {
1358+ ghScenario : {
1359+ // @effect -diagnostics-next-line preferSchemaOverJson:off
1360+ prListSequence : [ JSON . stringify ( [ existingPr ] ) ] ,
1361+ failWith : new GitHubCli . GitHubCliUnavailableError ( {
1362+ command : "gh" ,
1363+ cwd : repoDir ,
1364+ cause : new Error ( "rate limited" ) ,
1365+ } ) ,
1366+ failAfterCalls : 1 ,
1367+ } ,
1368+ } ) ;
1369+
1370+ const first = yield * manager . status ( { cwd : repoDir } ) ;
1371+ expect ( first . pr ?. number ) . toBe ( 214 ) ;
1372+
1373+ // An explicit invalidation (user refresh, git action) bypasses the PR
1374+ // cache and forces a live lookup — which now fails. The badge must keep
1375+ // the last known PR instead of blanking out.
1376+ yield * manager . invalidateStatus ( repoDir ) ;
1377+ const second = yield * manager . status ( { cwd : repoDir } ) ;
1378+ expect ( second . pr ?. number ) . toBe ( 214 ) ;
1379+ } ) ,
1380+ ) ;
1381+
1382+ it . effect (
1383+ "status does not reuse a stale PR after the branch is retargeted to a different upstream" ,
1384+ ( ) =>
1385+ Effect . gen ( function * ( ) {
1386+ const repoDir = yield * makeTempDir ( "t3code-git-manager-" ) ;
1387+ yield * initRepo ( repoDir ) ;
1388+ yield * runGit ( repoDir , [ "checkout" , "-b" , "feature/pr-retarget" ] ) ;
1389+
1390+ const originRemote = yield * createBareRemote ( ) ;
1391+ yield * runGit ( repoDir , [ "remote" , "add" , "origin" , originRemote ] ) ;
1392+ yield * runGit ( repoDir , [ "push" , "-u" , "origin" , "feature/pr-retarget" ] ) ;
1393+
1394+ const existingPr = {
1395+ number : 214 ,
1396+ title : "Sticky PR" ,
1397+ url : "https://github.com/pingdotgg/codething-mvp/pull/214" ,
1398+ baseRefName : "main" ,
1399+ headRefName : "feature/pr-retarget" ,
1400+ } ;
1401+ const { manager } = yield * makeManager ( {
1402+ ghScenario : {
1403+ // @effect -diagnostics-next-line preferSchemaOverJson:off
1404+ prListSequence : [ JSON . stringify ( [ existingPr ] ) ] ,
1405+ failWith : new GitHubCli . GitHubCliUnavailableError ( {
1406+ command : "gh" ,
1407+ cwd : repoDir ,
1408+ cause : new Error ( "rate limited" ) ,
1409+ } ) ,
1410+ failAfterCalls : 1 ,
1411+ } ,
1412+ } ) ;
1413+
1414+ const first = yield * manager . status ( { cwd : repoDir } ) ;
1415+ expect ( first . pr ?. number ) . toBe ( 214 ) ;
1416+
1417+ // Retarget the branch to a different remote/upstream (e.g. the PR was
1418+ // reopened against a fork). The previously cached PR belonged to the
1419+ // old upstream and must not be shown against the new one.
1420+ const forkRemote = yield * createBareRemote ( ) ;
1421+ yield * runGit ( repoDir , [ "remote" , "add" , "fork" , forkRemote ] ) ;
1422+ yield * runGit ( repoDir , [ "push" , "fork" , "feature/pr-retarget" ] ) ;
1423+ yield * runGit ( repoDir , [
1424+ "branch" ,
1425+ "--set-upstream-to=fork/feature/pr-retarget" ,
1426+ "feature/pr-retarget" ,
1427+ ] ) ;
1428+
1429+ yield * manager . invalidateStatus ( repoDir ) ;
1430+ const second = yield * manager . status ( { cwd : repoDir } ) ;
1431+ expect ( second . pr ) . toBeNull ( ) ;
1432+ } ) ,
1433+ ) ;
1434+
1435+ it . effect ( "status keeps the last known PR when the branch gains its first upstream" , ( ) =>
1436+ Effect . gen ( function * ( ) {
1437+ const repoDir = yield * makeTempDir ( "t3code-git-manager-" ) ;
1438+ yield * initRepo ( repoDir ) ;
1439+ yield * runGit ( repoDir , [ "checkout" , "-b" , "feature/pr-sticky-first-push" ] ) ;
1440+ const remoteDir = yield * createBareRemote ( ) ;
1441+ yield * runGit ( repoDir , [ "remote" , "add" , "origin" , remoteDir ] ) ;
1442+
1443+ const existingPr = {
1444+ number : 215 ,
1445+ title : "Sticky first-push PR" ,
1446+ url : "https://github.com/pingdotgg/codething-mvp/pull/215" ,
1447+ baseRefName : "main" ,
1448+ headRefName : "feature/pr-sticky-first-push" ,
1449+ } ;
1450+ const { manager } = yield * makeManager ( {
1451+ ghScenario : {
1452+ // @effect -diagnostics-next-line preferSchemaOverJson:off
1453+ prListSequence : [ JSON . stringify ( [ existingPr ] ) ] ,
1454+ failWith : new GitHubCli . GitHubCliUnavailableError ( {
1455+ command : "gh" ,
1456+ cwd : repoDir ,
1457+ cause : new Error ( "rate limited" ) ,
1458+ } ) ,
1459+ failAfterCalls : 1 ,
1460+ } ,
1461+ } ) ;
1462+
1463+ const first = yield * manager . status ( { cwd : repoDir } ) ;
1464+ expect ( first . pr ?. number ) . toBe ( 215 ) ;
1465+
1466+ yield * runGit ( repoDir , [ "push" , "-u" , "origin" , "feature/pr-sticky-first-push" ] ) ;
1467+ yield * manager . invalidateStatus ( repoDir ) ;
1468+
1469+ const second = yield * manager . status ( { cwd : repoDir } ) ;
1470+ expect ( second . pr ?. number ) . toBe ( 215 ) ;
1471+ } ) ,
1472+ ) ;
1473+
1474+ it . effect ( "status drops the last known PR when the tracked remote is repointed" , ( ) =>
1475+ Effect . gen ( function * ( ) {
1476+ const repoDir = yield * makeTempDir ( "t3code-git-manager-" ) ;
1477+ yield * initRepo ( repoDir ) ;
1478+ yield * runGit ( repoDir , [ "checkout" , "-b" , "feature/pr-repointed" ] ) ;
1479+ const originalRemoteDir = yield * createBareRemote ( ) ;
1480+ yield * runGit ( repoDir , [ "remote" , "add" , "origin" , originalRemoteDir ] ) ;
1481+ yield * runGit ( repoDir , [ "push" , "-u" , "origin" , "feature/pr-repointed" ] ) ;
1482+
1483+ const existingPr = {
1484+ number : 216 ,
1485+ title : "Old remote PR" ,
1486+ url : "https://github.com/pingdotgg/codething-mvp/pull/216" ,
1487+ baseRefName : "main" ,
1488+ headRefName : "feature/pr-repointed" ,
1489+ } ;
1490+ const { manager } = yield * makeManager ( {
1491+ ghScenario : {
1492+ // @effect -diagnostics-next-line preferSchemaOverJson:off
1493+ prListSequence : [ JSON . stringify ( [ existingPr ] ) ] ,
1494+ failWith : new GitHubCli . GitHubCliUnavailableError ( {
1495+ command : "gh" ,
1496+ cwd : repoDir ,
1497+ cause : new Error ( "rate limited" ) ,
1498+ } ) ,
1499+ failAfterCalls : 1 ,
1500+ } ,
1501+ } ) ;
1502+
1503+ const first = yield * manager . status ( { cwd : repoDir } ) ;
1504+ expect ( first . pr ?. number ) . toBe ( 216 ) ;
1505+
1506+ const replacementRemoteDir = yield * createBareRemote ( ) ;
1507+ yield * runGit ( repoDir , [ "remote" , "add" , "replacement" , replacementRemoteDir ] ) ;
1508+ yield * runGit ( repoDir , [ "push" , "replacement" , "feature/pr-repointed" ] ) ;
1509+ yield * runGit ( repoDir , [ "remote" , "set-url" , "origin" , replacementRemoteDir ] ) ;
1510+ yield * manager . invalidateStatus ( repoDir ) ;
1511+
1512+ const second = yield * manager . status ( { cwd : repoDir } ) ;
1513+ expect ( second . pr ) . toBeNull ( ) ;
1514+ } ) ,
1515+ ) ;
1516+
13391517 it . effect ( "creates a commit when working tree is dirty" , ( ) =>
13401518 Effect . gen ( function * ( ) {
13411519 const repoDir = yield * makeTempDir ( "t3code-git-manager-" ) ;
0 commit comments