@@ -94,4 +94,70 @@ describe('installMainProcessFailureObservers', () => {
9494 await vi . waitFor ( ( ) => expect ( app . relaunch ) . toHaveBeenCalledOnce ( ) )
9595 expect ( app . exit ) . toHaveBeenCalledWith ( 1 )
9696 } )
97+
98+ function emitAppEvent ( event : string ) {
99+ const handler = vi . mocked ( app . on ) . mock . calls . find ( ( [ name ] ) => name === event ) ?. [ 1 ]
100+ if ( ! handler ) throw new Error ( `No ${ event } listener` )
101+ ; ( handler as ( ) => void ) ( )
102+ }
103+
104+ async function settlePrompt ( ) {
105+ for ( let turn = 0 ; turn < 5 ; turn ++ ) await Promise . resolve ( )
106+ }
107+
108+ it ( 'exits without prompting when the failure arrives once the quit is committed' , ( ) => {
109+ const events = { filePath : '/tmp/events.log' , record : vi . fn ( ) }
110+ const { handlers, source } = createProcessSource ( )
111+ installMainProcessFailureObservers ( { events, getWindow : ( ) => null , processSource : source } )
112+
113+ emitAppEvent ( 'before-quit' )
114+ emitAppEvent ( 'will-quit' )
115+ handlers . get ( 'uncaughtException' ) ?.( new Error ( 'during shutdown' ) as never )
116+
117+ expect ( events . record ) . toHaveBeenCalledWith ( 'main_uncaught_exception' , { valueType : 'Error' } )
118+ expect ( dialog . showMessageBox ) . not . toHaveBeenCalled ( )
119+ expect ( app . relaunch ) . not . toHaveBeenCalled ( )
120+ expect ( app . exit ) . toHaveBeenCalledWith ( 1 )
121+ } )
122+
123+ it ( 'still offers recovery after a quit request another listener cancelled' , ( ) => {
124+ vi . mocked ( dialog . showMessageBox ) . mockImplementationOnce ( ( ) => new Promise ( ( ) => { } ) )
125+ const events = { filePath : '/tmp/events.log' , record : vi . fn ( ) }
126+ const { handlers, source } = createProcessSource ( )
127+ installMainProcessFailureObservers ( { events, getWindow : ( ) => null , processSource : source } )
128+
129+ emitAppEvent ( 'before-quit' )
130+ handlers . get ( 'uncaughtException' ) ?.( new Error ( 'after a cancelled quit' ) as never )
131+
132+ expect ( dialog . showMessageBox ) . toHaveBeenCalledOnce ( )
133+ const [ options ] = vi . mocked ( dialog . showMessageBox ) . mock . calls [ 0 ] as unknown as [
134+ { signal ?: AbortSignal } ,
135+ ]
136+ expect ( options . signal ?. aborted ) . toBe ( false )
137+ expect ( app . exit ) . not . toHaveBeenCalled ( )
138+ } )
139+
140+ it ( 'answers an open recovery prompt as Quit when a quit is requested' , async ( ) => {
141+ vi . mocked ( dialog . showMessageBox ) . mockImplementationOnce (
142+ ( options : { cancelId ?: number ; signal ?: AbortSignal } ) =>
143+ new Promise ( ( resolve ) => {
144+ options . signal ?. addEventListener ( 'abort' , ( ) =>
145+ resolve ( { response : options . cancelId ?? 0 , checkboxChecked : false } )
146+ )
147+ } )
148+ )
149+ const events = { filePath : '/tmp/events.log' , record : vi . fn ( ) }
150+ const { handlers, source } = createProcessSource ( )
151+ installMainProcessFailureObservers ( { events, getWindow : ( ) => null , processSource : source } )
152+
153+ handlers . get ( 'uncaughtException' ) ?.( new Error ( 'while running' ) as never )
154+ expect ( dialog . showMessageBox ) . toHaveBeenCalledOnce ( )
155+ expect ( app . exit ) . not . toHaveBeenCalled ( )
156+
157+ emitAppEvent ( 'before-quit' )
158+ await settlePrompt ( )
159+
160+ expect ( app . exit ) . toHaveBeenCalledWith ( 1 )
161+ expect ( app . relaunch ) . not . toHaveBeenCalled ( )
162+ } )
97163} )
0 commit comments