@@ -81,6 +81,203 @@ const remark = (
8181const noRemarks : ReadonlyArray < PullRequestComment > = [ ] ;
8282
8383describe ( "evaluatePullRequestWatch" , ( ) => {
84+ it . each ( [ false , true ] ) ( "baselines already-passed checks (required: %s)" , ( required ) => {
85+ const green = detail ( { checks : [ { ...check ( "test" , "success" ) , required } ] } ) ;
86+ const baseline = evaluatePullRequestWatch ( watch ( { awaitingBaseline : "all" } ) , green , noRemarks ) ;
87+ assert . deepEqual ( baseline . changes , [ ] ) ;
88+ assert . equal ( baseline . next . headSha , green . headSha ) ;
89+ assert . isTrue ( baseline . next . passed ) ;
90+ assert . deepEqual ( baseline . next . passedChecks , [ "test" ] ) ;
91+ assert . isFalse ( baseline . exhausted ) ;
92+ assert . deepEqual ( evaluatePullRequestWatch ( baseline . next , green , noRemarks ) . changes , [ ] ) ;
93+
94+ const pending = evaluatePullRequestWatch (
95+ baseline . next ,
96+ detail ( { checks : [ { ...check ( "test" , "pending" ) , required } ] } ) ,
97+ noRemarks ,
98+ ) ;
99+ assert . deepEqual ( pending . changes , [ ] ) ;
100+ const failed = evaluatePullRequestWatch (
101+ pending . next ,
102+ detail ( { checks : [ { ...check ( "test" , "failure" ) , required } ] } ) ,
103+ noRemarks ,
104+ ) ;
105+ assert . equal ( failed . changes [ 0 ] ?. kind , "checks-failed" ) ;
106+ const passed = evaluatePullRequestWatch ( failed . next , green , noRemarks ) ;
107+ assert . deepEqual ( passed . changes , [ { kind : "checks-passed" , count : 1 , required } ] ) ;
108+ assert . deepEqual (
109+ evaluatePullRequestWatch ( passed . next , { ...green , headSha : "bbbbbbbbbb" } , noRemarks ) . changes ,
110+ [ { kind : "checks-passed" , count : 1 , required } ] ,
111+ ) ;
112+ } ) ;
113+
114+ it ( "baselines existing failures and conflicts, then reports new ones" , ( ) => {
115+ const broken = detail ( {
116+ checks : [ check ( "lint" , "failure" ) , check ( "test" , "pending" ) ] ,
117+ mergeability : "conflicting" ,
118+ } ) ;
119+ const baseline = evaluatePullRequestWatch (
120+ watch ( { awaitingBaseline : "all" } ) ,
121+ broken ,
122+ noRemarks ,
123+ ) ;
124+ assert . deepEqual ( baseline . changes , [ ] ) ;
125+ assert . deepEqual ( baseline . next . failedChecks , [ "lint" ] ) ;
126+ assert . isFalse ( baseline . next . passed ) ;
127+ assert . isTrue ( baseline . next . conflicting ) ;
128+ assert . deepEqual ( evaluatePullRequestWatch ( baseline . next , broken , noRemarks ) . changes , [ ] ) ;
129+
130+ const failed = evaluatePullRequestWatch (
131+ baseline . next ,
132+ { ...broken , checks : [ check ( "lint" , "failure" ) , check ( "test" , "failure" ) ] } ,
133+ noRemarks ,
134+ ) ;
135+ assert . deepEqual ( failed . changes , [
136+ { kind : "checks-failed" , failed : [ check ( "test" , "failure" ) ] } ,
137+ ] ) ;
138+ const clean = evaluatePullRequestWatch ( failed . next , detail ( ) , noRemarks ) ;
139+ assert . deepEqual ( clean . changes , [ ] ) ;
140+ assert . deepEqual ( evaluatePullRequestWatch ( clean . next , broken , noRemarks ) . changes , [
141+ { kind : "checks-failed" , failed : [ check ( "lint" , "failure" ) ] } ,
142+ { kind : "conflicting" } ,
143+ ] ) ;
144+ } ) ;
145+
146+ it ( "baselines checks already awaiting approval" , ( ) => {
147+ const gated = detail ( { checks : [ { ...check ( "CI" , "action-required" ) , url : run ( 1 ) } ] } ) ;
148+ const baseline = evaluatePullRequestWatch ( watch ( { awaitingBaseline : "all" } ) , gated , noRemarks ) ;
149+ assert . deepEqual ( baseline . changes , [ ] ) ;
150+ assert . deepEqual ( evaluatePullRequestWatch ( baseline . next , gated , noRemarks ) . changes , [ ] ) ;
151+ } ) ;
152+
153+ it ( "reports remarks after the watch started while baselining checks and conflicts" , ( ) => {
154+ const green = detail ( {
155+ checks : [ check ( "test" , "success" ) ] ,
156+ mergeability : "conflicting" ,
157+ } ) ;
158+ const comments = [
159+ remark ( "reviewer" , "2026-10-02T11:00:00Z" ) ,
160+ remark ( "reviewer" , "2026-10-02T12:06:00Z" ) ,
161+ ] ;
162+ const baseline = evaluatePullRequestWatch ( watch ( { awaitingBaseline : "all" } ) , green , comments ) ;
163+ assert . deepEqual ( baseline . changes , [ { kind : "remarks" , remarks : [ comments [ 1 ] ! ] } ] ) ;
164+ assert . isTrue ( baseline . next . passed ) ;
165+ assert . isTrue ( baseline . next . conflicting ) ;
166+ assert . equal ( baseline . next . wakes , 1 ) ;
167+ assert . deepEqual ( evaluatePullRequestWatch ( baseline . next , green , comments ) . changes , [ ] ) ;
168+ } ) ;
169+
170+ it ( "reports a failure once the head SHA appears after a baseline without one" , ( ) => {
171+ const waiting = evaluatePullRequestWatch (
172+ watch ( { awaitingBaseline : "all" } ) ,
173+ detail ( { headSha : undefined , checks : [ check ( "test" , "pending" ) ] } ) ,
174+ noRemarks ,
175+ ) ;
176+ assert . deepEqual ( waiting . changes , [ ] ) ;
177+ const failed = evaluatePullRequestWatch (
178+ waiting . next ,
179+ detail ( { checks : [ check ( "test" , "failure" ) ] } ) ,
180+ noRemarks ,
181+ ) ;
182+ assert . deepEqual ( failed . changes , [
183+ { kind : "checks-failed" , failed : [ check ( "test" , "failure" ) ] } ,
184+ ] ) ;
185+ } ) ;
186+
187+ it ( "keeps a baseline taken without a head SHA once the SHA appears" , ( ) => {
188+ const green = detail ( { checks : [ check ( "test" , "success" ) ] } ) ;
189+ const baseline = evaluatePullRequestWatch (
190+ watch ( { awaitingBaseline : "all" } ) ,
191+ { ...green , headSha : undefined } ,
192+ noRemarks ,
193+ ) ;
194+ const known = evaluatePullRequestWatch ( baseline . next , green , noRemarks ) ;
195+ assert . deepEqual ( known . changes , [ ] ) ;
196+ assert . equal ( known . next . headSha , green . headSha ) ;
197+ const unknown = evaluatePullRequestWatch (
198+ known . next ,
199+ { ...green , headSha : undefined } ,
200+ noRemarks ,
201+ ) ;
202+ assert . equal ( unknown . next . headSha , green . headSha ) ;
203+ assert . deepEqual (
204+ evaluatePullRequestWatch ( unknown . next , { ...green , headSha : "bbbbbbbbbb" } , noRemarks )
205+ . changes ,
206+ [ { kind : "checks-passed" , count : 1 , required : false } ] ,
207+ ) ;
208+ } ) ;
209+
210+ it . each ( [ "success" , "failure" ] as const ) (
211+ "reports %s on checks that appear after a first read without any" ,
212+ ( status ) => {
213+ const empty = evaluatePullRequestWatch (
214+ watch ( { awaitingBaseline : "all" } ) ,
215+ detail ( { checks : [ ] } ) ,
216+ noRemarks ,
217+ ) ;
218+ assert . deepEqual ( empty . changes , [ ] ) ;
219+ assert . isUndefined ( empty . next . awaitingBaseline ) ;
220+ assert . deepEqual (
221+ evaluatePullRequestWatch ( empty . next , detail ( { checks : [ check ( "test" , status ) ] } ) , noRemarks )
222+ . changes ,
223+ status === "success"
224+ ? [ { kind : "checks-passed" , count : 1 , required : false } ]
225+ : [ { kind : "checks-failed" , failed : [ check ( "test" , "failure" ) ] } ] ,
226+ ) ;
227+ } ,
228+ ) ;
229+
230+ it ( "keeps the check baseline through a read whose approval lookup failed" , ( ) => {
231+ const bot = check ( "bot" , "success" ) ;
232+ const unavailable = { ...check ( "Workflow approval status" , "pending" ) , placeholder : true } ;
233+ const partial = evaluatePullRequestWatch (
234+ watch ( { awaitingBaseline : "all" } ) ,
235+ detail ( { checks : [ bot , unavailable ] } ) ,
236+ noRemarks ,
237+ ) ;
238+ assert . deepEqual ( partial . changes , [ ] ) ;
239+ assert . equal ( partial . next . awaitingBaseline , "checks" ) ;
240+ const approval = { ...check ( "CI" , "action-required" ) , url : run ( 1 ) } ;
241+ for ( const checks of [ [ bot ] , [ bot , approval ] ] ) {
242+ assert . deepEqual (
243+ evaluatePullRequestWatch ( partial . next , detail ( { checks } ) , noRemarks ) . changes ,
244+ [ ] ,
245+ ) ;
246+ }
247+ } ) ;
248+
249+ it ( "reports pending advisory checks finishing after a required-check baseline" , ( ) => {
250+ const initial = detail ( {
251+ checks : [ { ...check ( "test" , "success" ) , required : true } , check ( "bot" , "pending" ) ] ,
252+ } ) ;
253+ const baseline = evaluatePullRequestWatch (
254+ watch ( { awaitingBaseline : "all" } ) ,
255+ initial ,
256+ noRemarks ,
257+ ) ;
258+ assert . deepEqual ( baseline . changes , [ ] ) ;
259+ const settled = evaluatePullRequestWatch (
260+ baseline . next ,
261+ {
262+ ...initial ,
263+ checks : [ initial . checks [ 0 ] ! , check ( "bot" , "success" ) ] ,
264+ } ,
265+ noRemarks ,
266+ ) ;
267+ assert . deepEqual ( settled . changes , [ { kind : "checks-passed" , count : 2 , required : false } ] ) ;
268+ assert . deepEqual (
269+ evaluatePullRequestWatch (
270+ settled . next ,
271+ {
272+ ...initial ,
273+ checks : [ initial . checks [ 0 ] ! , check ( "bot" , "success" ) ] ,
274+ } ,
275+ noRemarks ,
276+ ) . changes ,
277+ [ ] ,
278+ ) ;
279+ } ) ;
280+
84281 it ( "waits for workflow approval once, through reads that miss it, until the run finishes" , ( ) => {
85282 const bot = check ( "bot" , "success" ) ;
86283 const approval = { ...check ( "CI" , "action-required" ) , url : run ( 1 ) } ;
@@ -571,14 +768,22 @@ describe("evaluatePullRequestWatch", () => {
571768 const unknown = detail ( {
572769 checks : [ { ...check ( "Workflow approval status" , "action-required" ) , placeholder : true } ] ,
573770 } ) ;
574- assert . deepEqual ( anHourLater ( watch ( ) , unknown ) . flat ( ) , [ ] ) ;
575- const unread = evaluate ( watch ( ) , unknown , noRemarks , 0 ) ;
771+ const fresh = watch ( { awaitingBaseline : "all" } ) ;
772+ assert . deepEqual ( anHourLater ( fresh , unknown ) . flat ( ) , [ ] ) ;
773+ const unread = evaluate ( fresh , unknown , noRemarks , 0 ) ;
576774 assert . deepEqual (
577775 evaluate ( unread . next , detail ( { checks : [ ] } ) , noRemarks , 60 * 60_000 ) . changes ,
578776 [ { kind : "no-checks" , minutes : 60 } ] ,
579777 ) ;
580778 } ) ;
581779
780+ it ( "starts the no-checks clock while a fresh watch awaits its baseline" , ( ) => {
781+ assert . deepEqual (
782+ anHourLater ( watch ( { awaitingBaseline : "all" } ) , detail ( { checks : [ ] } ) ) . flat ( ) ,
783+ [ "no-checks" ] ,
784+ ) ;
785+ } ) ;
786+
582787 it ( "never reports missing checks on a host where an empty list says nothing" , ( ) => {
583788 const silent = detail ( { capabilities : capabilities ( false ) , checks : [ ] } ) ;
584789 assert . isTrue ( anHourLater ( watch ( ) , silent ) . every ( ( kinds ) => kinds . length === 0 ) ) ;
0 commit comments