@@ -358,4 +358,157 @@ describe("session-notification", () => {
358358 // #then - only one notification should be sent
359359 expect ( notificationCalls ) . toHaveLength ( 1 )
360360 } )
361+
362+ test ( "should trigger notification immediately on question.asked event for main session" , async ( ) => {
363+ // #given - main session is set
364+ const mainSessionID = "main-question"
365+ setMainSession ( mainSessionID )
366+
367+ const hook = createSessionNotification ( createMockPluginInput ( ) , { } )
368+
369+ // #when - question.asked event fires
370+ await hook ( {
371+ event : {
372+ type : "question.asked" ,
373+ properties : {
374+ sessionID : mainSessionID ,
375+ questions : [ { header : "Test" , question : "Test question?" } ] ,
376+ } ,
377+ } ,
378+ } )
379+
380+ // #then - notification should be sent immediately
381+ expect ( notificationCalls . length ) . toBeGreaterThanOrEqual ( 1 )
382+ expect ( notificationCalls [ 0 ] ) . toContain ( "Question" )
383+ } )
384+
385+ test ( "should not trigger question.asked notification for subagent session" , async ( ) => {
386+ // #given - a subagent session exists
387+ const subagentSessionID = "subagent-question"
388+ subagentSessions . add ( subagentSessionID )
389+
390+ const hook = createSessionNotification ( createMockPluginInput ( ) , { } )
391+
392+ // #when - question.asked event fires for subagent
393+ await hook ( {
394+ event : {
395+ type : "question.asked" ,
396+ properties : { sessionID : subagentSessionID } ,
397+ } ,
398+ } )
399+
400+ // #then - notification should NOT be sent
401+ expect ( notificationCalls ) . toHaveLength ( 0 )
402+ } )
403+
404+ test ( "should not trigger question.asked notification for non-main session" , async ( ) => {
405+ // #given - main session is set, but different session asks question
406+ const mainSessionID = "main-q"
407+ const otherSessionID = "other-q"
408+ setMainSession ( mainSessionID )
409+
410+ const hook = createSessionNotification ( createMockPluginInput ( ) , { } )
411+
412+ // #when - question.asked event fires for non-main session
413+ await hook ( {
414+ event : {
415+ type : "question.asked" ,
416+ properties : { sessionID : otherSessionID } ,
417+ } ,
418+ } )
419+
420+ // #then - notification should NOT be sent
421+ expect ( notificationCalls ) . toHaveLength ( 0 )
422+ } )
423+
424+ test ( "should trigger notification on session.error event for main session" , async ( ) => {
425+ // #given - main session is set
426+ const mainSessionID = "main-error"
427+ setMainSession ( mainSessionID )
428+
429+ const hook = createSessionNotification ( createMockPluginInput ( ) , { } )
430+
431+ // #when - session.error event fires
432+ await hook ( {
433+ event : {
434+ type : "session.error" ,
435+ properties : {
436+ sessionID : mainSessionID ,
437+ error : { message : "The final block in an assistant message cannot be thinking" } ,
438+ } ,
439+ } ,
440+ } )
441+
442+ // #then - notification should be sent immediately with error message
443+ expect ( notificationCalls . length ) . toBeGreaterThanOrEqual ( 1 )
444+ expect ( notificationCalls [ 0 ] ) . toContain ( "Error" )
445+ } )
446+
447+ test ( "should not trigger session.error notification for subagent session" , async ( ) => {
448+ // #given - a subagent session exists
449+ const subagentSessionID = "subagent-error"
450+ subagentSessions . add ( subagentSessionID )
451+
452+ const hook = createSessionNotification ( createMockPluginInput ( ) , { } )
453+
454+ // #when - session.error event fires for subagent
455+ await hook ( {
456+ event : {
457+ type : "session.error" ,
458+ properties : {
459+ sessionID : subagentSessionID ,
460+ error : { message : "Some error" } ,
461+ } ,
462+ } ,
463+ } )
464+
465+ // #then - notification should NOT be sent
466+ expect ( notificationCalls ) . toHaveLength ( 0 )
467+ } )
468+
469+ test ( "should not trigger session.error notification for non-main session" , async ( ) => {
470+ // #given - main session is set, but different session has error
471+ const mainSessionID = "main-e"
472+ const otherSessionID = "other-e"
473+ setMainSession ( mainSessionID )
474+
475+ const hook = createSessionNotification ( createMockPluginInput ( ) , { } )
476+
477+ // #when - session.error event fires for non-main session
478+ await hook ( {
479+ event : {
480+ type : "session.error" ,
481+ properties : {
482+ sessionID : otherSessionID ,
483+ error : { message : "Some error" } ,
484+ } ,
485+ } ,
486+ } )
487+
488+ // #then - notification should NOT be sent
489+ expect ( notificationCalls ) . toHaveLength ( 0 )
490+ } )
491+
492+ test ( "should handle session.error with no error message" , async ( ) => {
493+ // #given - main session is set
494+ const mainSessionID = "main-error-no-msg"
495+ setMainSession ( mainSessionID )
496+
497+ const hook = createSessionNotification ( createMockPluginInput ( ) , { } )
498+
499+ // #when - session.error event fires without error message
500+ await hook ( {
501+ event : {
502+ type : "session.error" ,
503+ properties : {
504+ sessionID : mainSessionID ,
505+ error : { } ,
506+ } ,
507+ } ,
508+ } )
509+
510+ // #then - notification should be sent with default message
511+ expect ( notificationCalls . length ) . toBeGreaterThanOrEqual ( 1 )
512+ expect ( notificationCalls [ 0 ] ) . toContain ( "Error" )
513+ } )
361514} )
0 commit comments