@@ -2,9 +2,11 @@ import assert from "node:assert/strict";
22import test from "node:test" ;
33
44import {
5+ currentPullRequestForSelection ,
56 projectReviewFilesChangedBody ,
67 retainLatestByKey ,
78 reviewDiffWorkspaceBranch ,
9+ shouldReplaceRetainedPullRequest ,
810} from "./projectReviewDisplay.ts" ;
911
1012test ( "retainLatestByKey keeps the previous value when shouldReplace is false" , ( ) => {
@@ -35,6 +37,89 @@ test("retainLatestByKey takes a new key immediately", () => {
3537 assert . deepEqual ( retained , { files : [ ] } ) ;
3638} ) ;
3739
40+ test ( "an explicit selected review does not fall back to another identity" , ( ) => {
41+ const selected = { id : "pr-a" } ;
42+ const branchReview = { id : "pr-branch" } ;
43+
44+ assert . equal (
45+ currentPullRequestForSelection ( {
46+ fallback : branchReview ,
47+ pullRequests : [ selected , branchReview ] ,
48+ selectedPullRequestId : "pr-a" ,
49+ } ) ,
50+ selected ,
51+ ) ;
52+ assert . equal (
53+ currentPullRequestForSelection ( {
54+ fallback : branchReview ,
55+ pullRequests : [ branchReview ] ,
56+ selectedPullRequestId : "pr-a" ,
57+ } ) ,
58+ null ,
59+ ) ;
60+ assert . equal (
61+ currentPullRequestForSelection ( {
62+ fallback : branchReview ,
63+ pullRequests : [ branchReview ] ,
64+ selectedPullRequestId : null ,
65+ } ) ,
66+ branchReview ,
67+ ) ;
68+ } ) ;
69+
70+ test ( "retained review identity stays aligned with the diff-query identity across fetch phases" , ( ) => {
71+ const reviewA = { id : "pr-a" } ;
72+ const renderedCache = { current : { key : "repo:pr-a" , value : reviewA } } ;
73+ const diffQueryCache = { current : { key : "repo:pr-a" , value : reviewA } } ;
74+
75+ const renderedDuringFetch = retainLatestByKey (
76+ renderedCache ,
77+ "repo:pr-a" ,
78+ currentPullRequestForSelection ( {
79+ fallback : { id : "pr-branch" } ,
80+ pullRequests : [ ] ,
81+ selectedPullRequestId : "pr-a" ,
82+ } ) ,
83+ ( next ) => shouldReplaceRetainedPullRequest ( next , true ) ,
84+ ) ;
85+ const diffDuringFetch = retainLatestByKey (
86+ diffQueryCache ,
87+ "repo:pr-a" ,
88+ currentPullRequestForSelection ( {
89+ fallback : { id : "pr-branch" } ,
90+ pullRequests : [ ] ,
91+ selectedPullRequestId : "pr-a" ,
92+ } ) ,
93+ ( next ) => shouldReplaceRetainedPullRequest ( next , true ) ,
94+ ) ;
95+ assert . equal ( renderedDuringFetch , reviewA ) ;
96+ assert . equal ( diffDuringFetch , reviewA ) ;
97+ assert . equal ( renderedDuringFetch . id , diffDuringFetch . id ) ;
98+
99+ const renderedAfterComplete = retainLatestByKey (
100+ renderedCache ,
101+ "repo:pr-a" ,
102+ currentPullRequestForSelection ( {
103+ fallback : { id : "pr-branch" } ,
104+ pullRequests : [ ] ,
105+ selectedPullRequestId : "pr-a" ,
106+ } ) ,
107+ ( next ) => shouldReplaceRetainedPullRequest ( next , false ) ,
108+ ) ;
109+ const diffAfterComplete = retainLatestByKey (
110+ diffQueryCache ,
111+ "repo:pr-a" ,
112+ currentPullRequestForSelection ( {
113+ fallback : { id : "pr-branch" } ,
114+ pullRequests : [ ] ,
115+ selectedPullRequestId : "pr-a" ,
116+ } ) ,
117+ ( next ) => shouldReplaceRetainedPullRequest ( next , false ) ,
118+ ) ;
119+ assert . equal ( renderedAfterComplete , null ) ;
120+ assert . equal ( diffAfterComplete , null ) ;
121+ } ) ;
122+
38123test ( "review files stay mounted when a populated diff races an unavailable snapshot" , ( ) => {
39124 assert . equal (
40125 projectReviewFilesChangedBody ( {
0 commit comments