@@ -27,6 +27,7 @@ function fixture() {
2727 issues : async ( ) => [ structuredClone ( issue ) ] , issue : async ( ) => structuredClone ( issue ) ,
2828 ensureComment : async ( ) => { events . push ( "comment" ) ; return 42 ; } ,
2929 findPull : async ( ) => undefined ,
30+ pull : async ( _repo , number ) => ( { number, html_url : `https://github.com/owner/repo/pull/${ number } ` , state : "open" } ) ,
3031 ensurePull : async ( ) => { events . push ( "pr" ) ; return { number : 2 , html_url : "https://github.com/owner/repo/pull/2" , state : "open" } ; } ,
3132 } ;
3233 const executor : Executor = {
@@ -475,6 +476,58 @@ test("auto-merge can be disabled independently of issue processing", async () =>
475476 await d . init ( ) ; await d . scan ( ) ; await d . tick ( ) ; await d . tick ( ) ; assert . equal ( called , false ) ; assert . equal ( d . status ( ) [ 0 ] ?. status , "done" ) ;
476477} ) ;
477478
479+ for ( const merged of [ false , true ] ) {
480+ test ( `scanning detects a manually ${ merged ? "merged" : "closed" } PR even with auto-merge disabled and its issue closed` , async ( ) => {
481+ const f = fixture ( ) ; const notifications : ReturnType < Dispatcher [ "activity" ] > [ number ] [ ] = [ ] ;
482+ const config = { ...options , autoMerge : { ...options . autoMerge , enabled : false } } ;
483+ const make = ( ) => new Dispatcher ( config , f . store , f . github , f . executor , new AbortController ( ) . signal , [ ] , ( ) => 1000 , async a => { notifications . push ( a ) ; } ) ;
484+ let d = make ( ) ; await d . init ( ) ; await d . scan ( ) ; await d . tick ( ) ;
485+ f . github . issues = async ( ) => [ ] ;
486+ f . github . issue = async ( ) => ( { ...issue , state : "closed" } ) ;
487+ f . github . pull = async ( _repo , number ) => ( { number, html_url : "https://github.com/owner/repo/pull/2" , state : "closed" , merged } ) ;
488+ await d . scan ( ) ;
489+ assert . equal ( d . status ( ) [ 0 ] ?. pr ?. state , "closed" ) ;
490+ assert . equal ( d . activity ( ) [ 0 ] ?. phase , merged ? "merged" : "pr_closed" ) ;
491+ assert . equal ( notifications . filter ( a => a . prState === "closed" ) . length , 1 ) ;
492+ assert . deepEqual ( notifications . at ( - 1 ) ?. sessionIDs , [ "ses_test" ] ) ;
493+ f . store . data = Queue . parse ( JSON . parse ( JSON . stringify ( f . store . data ) ) ) ;
494+ d = make ( ) ; await d . init ( ) ; await d . scan ( ) ; await d . tick ( ) ;
495+ assert . equal ( d . activity ( ) [ 0 ] ?. prState , "closed" ) ;
496+ assert . equal ( notifications . filter ( a => a . prState === "closed" ) . length , 1 ) ;
497+ } ) ;
498+ }
499+
500+ test ( "a failed PR state lookup does not announce closure or change saved state" , async ( ) => {
501+ const f = fixture ( ) , d = f . make ( ) ; await d . init ( ) ; await d . scan ( ) ; await d . tick ( ) ;
502+ f . github . pull = async ( ) => { throw new Error ( "GitHub unavailable" ) ; } ;
503+ await assert . rejects ( d . scan ( ) , / G i t H u b u n a v a i l a b l e / ) ;
504+ assert . equal ( d . status ( ) [ 0 ] ?. pr ?. state , "open" ) ; assert . equal ( d . activity ( ) [ 0 ] ?. prState , "open" ) ;
505+ } ) ;
506+
507+ test ( "main session IDs from every follow-up round survive restart for tab cleanup" , async ( ) => {
508+ const f = fixture ( ) ; let session = 0 ; let d = f . make ( ) ;
509+ f . executor . run = async ( _task , checkpoint ) => { await checkpoint ( { sessionID : `ses_${ ++ session } ` } ) ; } ;
510+ await d . init ( ) ; await d . scan ( ) ; await d . tick ( ) ;
511+ f . github . findPull = async ( ) => ( { number : 2 , html_url : "https://github.com/owner/repo/pull/2" , state : "open" } ) ;
512+ for ( const id of [ 50 , 60 ] ) {
513+ f . github . comments = async ( ) => [ { id, body : "Please handle one more case" , user : { login : "alice" } } ] ;
514+ await d . scan ( ) ; await d . tick ( ) ;
515+ }
516+ f . store . data = Queue . parse ( JSON . parse ( JSON . stringify ( f . store . data ) ) ) ;
517+ d = f . make ( ) ; await d . init ( ) ;
518+ assert . deepEqual ( d . activity ( ) [ 0 ] ?. sessionIDs , [ "ses_1" , "ses_2" , "ses_3" ] ) ;
519+ } ) ;
520+
521+ test ( "PR state lookup uses the exact PR number and retains merge metadata" , async ( ) => {
522+ const github = new Github ( "fake" , new AbortController ( ) . signal , ( async ( input : string | URL | Request , init ?: RequestInit ) => {
523+ assert . equal ( String ( input ) , "https://api.github.com/repos/owner/repo/pulls/17" ) ;
524+ assert . equal ( init ?. method , "GET" ) ;
525+ return new Response ( JSON . stringify ( { number : 17 , html_url : "https://github.com/owner/repo/pull/17" , state : "closed" , merged : true , merged_at : "2026-09-11T20:00:00Z" } ) ) ;
526+ } ) as typeof fetch ) ;
527+ const pr = await github . pull ( "owner/repo" , 17 ) ;
528+ assert . equal ( pr . state , "closed" ) ; assert . equal ( pr . merged , true ) ;
529+ } ) ;
530+
478531test ( "issue questions are published once, survive restart, and resume only on an authorized reply" , async ( ) => {
479532 const f = fixture ( ) ; let d = f . make ( ) ;
480533 f . github . ensureComment = async ( _repo , _number , marker ) => { f . events . push ( marker . includes ( ":question:" ) ? "question" : "comment" ) ; return marker . includes ( ":question:" ) ? 100 : 42 ; } ;
0 commit comments