@@ -472,18 +472,44 @@ func TestSession(t *testing.T) {
472472 t .Fatalf ("Failed to create session: %v" , err )
473473 }
474474
475- // Send a message that will take some time to process
476- _ , err = session .Send (copilot.MessageOptions {Prompt : "What is 1+1?" })
475+ // Set up wait for tool.execution_start BEFORE sending
476+ toolStartCh := make (chan * copilot.SessionEvent , 1 )
477+ toolStartErrCh := make (chan error , 1 )
478+ go func () {
479+ evt , err := testharness .GetNextEventOfType (session , copilot .ToolExecutionStart , 60 * time .Second )
480+ if err != nil {
481+ toolStartErrCh <- err
482+ } else {
483+ toolStartCh <- evt
484+ }
485+ }()
486+
487+ // Send a message that triggers a long-running shell command
488+ _ , err = session .Send (copilot.MessageOptions {Prompt : "run the shell command 'sleep 100' (note this works on both bash and PowerShell)" })
477489 if err != nil {
478490 t .Fatalf ("Failed to send message: %v" , err )
479491 }
480492
481- // Abort the session immediately
493+ // Wait for tool.execution_start
494+ select {
495+ case <- toolStartCh :
496+ // Tool execution has started
497+ case err := <- toolStartErrCh :
498+ t .Fatalf ("Failed waiting for tool.execution_start: %v" , err )
499+ }
500+
501+ // Abort the session
482502 err = session .Abort ()
483503 if err != nil {
484504 t .Fatalf ("Failed to abort session: %v" , err )
485505 }
486506
507+ // Wait for session.idle after abort
508+ _ , err = testharness .GetNextEventOfType (session , copilot .SessionIdle , 60 * time .Second )
509+ if err != nil {
510+ t .Fatalf ("Failed waiting for session.idle after abort: %v" , err )
511+ }
512+
487513 // The session should still be alive and usable after abort
488514 messages , err := session .GetMessages ()
489515 if err != nil {
@@ -493,15 +519,22 @@ func TestSession(t *testing.T) {
493519 t .Error ("Expected messages to exist after abort" )
494520 }
495521
496- // We should be able to send another message
497- _ , err = session .Send (copilot.MessageOptions {Prompt : "What is 2+2?" })
498- if err != nil {
499- t .Fatalf ("Failed to send message after abort: %v" , err )
522+ // Verify messages contain an abort event
523+ hasAbortEvent := false
524+ for _ , msg := range messages {
525+ if msg .Type == copilot .Abort {
526+ hasAbortEvent = true
527+ break
528+ }
529+ }
530+ if ! hasAbortEvent {
531+ t .Error ("Expected messages to contain an 'abort' event" )
500532 }
501533
502- answer , err := testharness .GetFinalAssistantMessage (session , 60 * time .Second )
534+ // We should be able to send another message
535+ answer , err := session .SendAndWait (copilot.MessageOptions {Prompt : "What is 2+2?" }, 60 * time .Second )
503536 if err != nil {
504- t .Fatalf ("Failed to get assistant message after abort: %v" , err )
537+ t .Fatalf ("Failed to send message after abort: %v" , err )
505538 }
506539
507540 if answer .Data .Content == nil || ! strings .Contains (* answer .Data .Content , "4" ) {
0 commit comments