@@ -11,7 +11,9 @@ const issue = (number, state, labels, title = `Item ${number}`) =>
1111
1212// A fake API over a page store: `GET repos/x/y/issues?state=closed&labels=<name>` filters like GitHub
1313// (state and one label), and a DELETE removes that label from the item. Every call is recorded.
14- const fakeGh = ( items ) => {
14+ // `failures` maps `<number>/<label>` to a refusal: `stderr` is the status line `gh` writes, `stdout`
15+ // the JSON body it writes, and `remove: false` keeps the label on the item like a real refusal does.
16+ const fakeGh = ( items , failures = { } ) => {
1517 const store = items . map ( item => ( { ...item , labels : item . labels } ) )
1618 const calls = [ ]
1719 const gh = ( ...args ) => {
@@ -21,7 +23,14 @@ const fakeGh = (items) => {
2123 const m = / \/ i s s u e s \/ ( \d + ) \/ l a b e l s \/ ( .+ ) $ / . exec ( path )
2224 const item = store . find ( i => String ( i . number ) === m [ 1 ] )
2325 const name = decodeURIComponent ( m [ 2 ] )
24- item . labels = item . labels . filter ( l => l . name !== name )
26+ const failure = failures [ `${ m [ 1 ] } /${ name } ` ]
27+ if ( failure ?. remove !== false ) item . labels = item . labels . filter ( l => l . name !== name )
28+ if ( failure ) {
29+ const error = new Error ( failure . stderr )
30+ error . stderr = failure . stderr
31+ error . stdout = failure . stdout ?? ''
32+ throw error
33+ }
2534 return ''
2635 }
2736 const url = new URL ( 'https://api.github.com/' + path )
@@ -41,6 +50,15 @@ const fakeGh = (items) => {
4150
4251const run = ( gh , apply ) => main ( { gh, env : { GH_REPO : 'ThinkFlowLab/FlyBot' } , repo : 'ThinkFlowLab/FlyBot' , apply } )
4352
53+ // Capture what the run prints: the failure line and the refusal's response body are the evidence, so
54+ // the tests pin them the way the live log reads.
55+ const capture = ( fn ) => {
56+ const lines = [ ]
57+ const log = console . log
58+ console . log = ( ...args ) => lines . push ( args . join ( ' ' ) )
59+ try { return { result : fn ( ) , lines } } finally { console . log = log }
60+ }
61+
4462test ( '#738: a dry run prints what it would do and removes nothing' , ( ) => {
4563 const gh = fakeGh ( [ issue ( 602 , 'closed' , [ 'agent-working' , 'bug' , 'risk:T1' ] ) ] )
4664 const r = run ( gh , false )
@@ -131,3 +149,72 @@ test('#1401: closing an item is a trigger, and the daily sweep stays', () => {
131149 assert . match ( wf , / ^ { 2 } c a n c e l - i n - p r o g r e s s : f a l s e $ / m, 'a close must not cancel the sweep it needs' )
132150 assert . match ( wf , / r u n : n o d e \. g i t h u b \/ s c r i p t s \/ l a b e l - h y g i e n e \. m j s - - a p p l y $ / m, 'the close event runs the same --apply as the sweep' )
133151} )
152+
153+ test ( '#1418: a 422 is a refusal, not "already gone": the sweep continues and the read-back keeps the run red' , ( ) => {
154+ // The live shape from #1418: the label is still on the item after a 422 (job 122128075345, #1359).
155+ const gh = fakeGh ( [
156+ issue ( 1356 , 'closed' , [ 'agent-working' ] ) ,
157+ issue ( 1359 , 'closed' , [ 'agent-pr' ] ) ,
158+ issue ( 1400 , 'closed' , [ 'agent-working' , 'bug' ] ) ,
159+ ] , {
160+ '1356/agent-working' : {
161+ remove : false ,
162+ stderr : 'gh: Validation Failed (HTTP 422)' ,
163+ stdout : '{"message":"Validation Failed","errors":[{"resource":"Issue","field":"labels","code":"unprocessable"}]}' ,
164+ } ,
165+ } )
166+ const { result : r , lines } = capture ( ( ) => run ( gh , true ) )
167+ assert . equal ( gh . deletes ( ) . length , 3 , 'the first refusal must not abandon the items behind it' )
168+ assert . deepEqual ( gh . store . find ( i => i . number === 1356 ) . labels . map ( l => l . name ) , [ 'agent-working' ] , 'a 422 leaves its label on the item' )
169+ assert . deepEqual ( gh . store . filter ( i => i . number !== 1356 ) . map ( i => i . labels . map ( l => l . name ) ) , [ [ ] , [ 'bug' ] ] , 'the other items are still cleaned' )
170+ assert . deepEqual ( r . skipped , [ ] , 'a 422 is never reported as already gone' )
171+ assert . deepEqual ( r . failed . map ( f => [ f . number , f . name ] ) , [ [ 1356 , 'agent-working' ] ] )
172+ assert . equal ( r . ok , false , 'the label is still stale at the read-back' )
173+ const text = lines . join ( '\n' )
174+ assert . match ( text , / l a b e l - h y g i e n e : F A I L E D # 1 3 5 6 a g e n t - w o r k i n g : V a l i d a t i o n F a i l e d \( H T T P 4 2 2 \) / )
175+ assert . match ( text , / " m e s s a g e " : " V a l i d a t i o n F a i l e d " / , 'the response body is printed, not only the status line' )
176+ assert . doesNotMatch ( text , / l a b e l - h y g i e n e : s k i p # 1 3 5 6 / )
177+ assert . match ( text , / l a b e l - h y g i e n e : F A I L E D : # 1 3 5 6 \( a g e n t - w o r k i n g : / , 'the failed summary is printed before the read-back' )
178+ assert . match ( text , / S T I L L S T A L E : # 1 3 5 6 \( a g e n t - w o r k i n g \) / )
179+ } )
180+
181+ test ( '#1418: a 404 means the label is already gone: one skip line and a clean board is green' , ( ) => {
182+ const gh = fakeGh ( [
183+ issue ( 1356 , 'closed' , [ 'agent-working' ] ) ,
184+ issue ( 1359 , 'closed' , [ 'agent-pr' ] ) ,
185+ ] , {
186+ '1356/agent-working' : {
187+ stderr : 'gh: Not Found (HTTP 404)' ,
188+ stdout : '{"message":"Not Found","documentation_url":"https://docs.github.com/rest"}' ,
189+ } ,
190+ } )
191+ const { result : r , lines } = capture ( ( ) => run ( gh , true ) )
192+ assert . equal ( gh . deletes ( ) . length , 2 )
193+ assert . deepEqual ( r . skipped . map ( s => [ s . number , s . name ] ) , [ [ 1356 , 'agent-working' ] ] )
194+ assert . deepEqual ( r . failed , [ ] )
195+ assert . equal ( r . ok , true , 'a label that was gone before the call does not fail the sweep' )
196+ const text = lines . join ( '\n' )
197+ assert . match ( text , / l a b e l - h y g i e n e : s k i p # 1 3 5 6 a g e n t - w o r k i n g : a l r e a d y g o n e / )
198+ assert . doesNotMatch ( text , / S T I L L S T A L E / )
199+ } )
200+
201+ test ( '#1418: an unexpected write failure is collected, the rest is cleaned, and the run names it and exits non-zero' , ( ) => {
202+ const gh = fakeGh ( [ 1354 , 1356 , 1358 ] . map ( n => issue ( n , 'closed' , [ 'agent-working' , 'bug' ] ) ) , {
203+ '1356/agent-working' : {
204+ remove : false ,
205+ stderr : 'gh: Server Error (HTTP 500)' ,
206+ stdout : '{"message":"Server Error"}' ,
207+ } ,
208+ } )
209+ const { result : r , lines } = capture ( ( ) => run ( gh , true ) )
210+ assert . equal ( gh . deletes ( ) . length , 3 )
211+ assert . deepEqual ( r . failed . map ( f => [ f . number , f . name ] ) , [ [ 1356 , 'agent-working' ] ] )
212+ assert . deepEqual ( r . skipped , [ ] )
213+ assert . equal ( r . ok , false )
214+ assert . deepEqual ( r . after . map ( i => i . number ) , [ 1356 ] , 'the label that really failed stays on the board' )
215+ assert . deepEqual ( gh . store . filter ( i => i . number !== 1356 ) . map ( i => i . labels . map ( l => l . name ) ) , [ [ 'bug' ] , [ 'bug' ] ] )
216+ const text = lines . join ( '\n' )
217+ assert . match ( text , / F A I L E D # 1 3 5 6 a g e n t - w o r k i n g : S e r v e r E r r o r \( H T T P 5 0 0 \) / )
218+ assert . match ( text , / l a b e l - h y g i e n e : F A I L E D : # 1 3 5 6 \( a g e n t - w o r k i n g : / )
219+ assert . match ( text , / S T I L L S T A L E : # 1 3 5 6 \( a g e n t - w o r k i n g \) / )
220+ } )
0 commit comments