@@ -164,6 +164,9 @@ func TestStartSession_ResumeSession(t *testing.T) {
164164 if state .sessionID != resumeID {
165165 t .Errorf ("sessionID = %q, want %q" , state .sessionID , resumeID )
166166 }
167+ if session .ID != resumeID {
168+ t .Errorf ("session.ID = %q, want %q" , session .ID , resumeID )
169+ }
167170}
168171
169172func TestRunTurn_WrongInternalType (t * testing.T ) {
@@ -314,6 +317,73 @@ func TestStopSession_NoActiveTurn(t *testing.T) {
314317 }
315318}
316319
320+ func TestStopSession_ContextDeadline (t * testing.T ) {
321+ t .Parallel ()
322+
323+ testCtx , testCancel := context .WithTimeout (context .Background (), 30 * time .Second )
324+ defer testCancel ()
325+
326+ tmpDir := t .TempDir ()
327+ script := writeOpenCodeScript (t , tmpDir , `case "$1" in
328+ export) echo '{"messages":[]}'; exit 0;;
329+ esac
330+ trap '' TERM
331+ printf '{"type":"step_start","timestamp":1000,"sessionID":"ses_abc123","part":{"id":"p1","messageID":"m1","sessionID":"ses_abc123","snapshot":"","type":"step-start"}}\n'
332+ while :; do sleep 1; done` )
333+
334+ a , _ := NewOpenCodeAdapter (map [string ]any {})
335+ session , err := a .StartSession (testCtx , domain.StartSessionParams {
336+ WorkspacePath : tmpDir ,
337+ AgentConfig : domain.AgentConfig {Command : script },
338+ })
339+ if err != nil {
340+ t .Fatalf ("StartSession() error = %v" , err )
341+ }
342+
343+ gotEvent := make (chan struct {}, 1 )
344+ resultCh := make (chan domain.TurnResult , 1 )
345+ errCh := make (chan error , 1 )
346+ go func () {
347+ result , runErr := a .RunTurn (context .Background (), session , domain.RunTurnParams {
348+ Prompt : "work" ,
349+ OnEvent : func (_ domain.AgentEvent ) {
350+ select {
351+ case gotEvent <- struct {}{}:
352+ default :
353+ }
354+ },
355+ })
356+ resultCh <- result
357+ errCh <- runErr
358+ }()
359+
360+ select {
361+ case <- gotEvent :
362+ case <- testCtx .Done ():
363+ t .Fatal ("timed out waiting for first event" )
364+ }
365+
366+ stopCtx , stopCancel := context .WithTimeout (testCtx , 50 * time .Millisecond )
367+ defer stopCancel ()
368+
369+ err = a .StopSession (stopCtx , session )
370+ if ! errors .Is (err , context .DeadlineExceeded ) {
371+ t .Fatalf ("StopSession() error = %v, want %v" , err , context .DeadlineExceeded )
372+ }
373+
374+ select {
375+ case result := <- resultCh :
376+ if result .ExitReason != domain .EventTurnCancelled {
377+ t .Errorf ("ExitReason = %q, want %q" , result .ExitReason , domain .EventTurnCancelled )
378+ }
379+ if runErr := <- errCh ; runErr != nil {
380+ t .Errorf ("RunTurn() error = %v, want nil" , runErr )
381+ }
382+ case <- testCtx .Done ():
383+ t .Fatal ("RunTurn did not return after StopSession timeout" )
384+ }
385+ }
386+
317387func TestStopSession_WrongInternalType (t * testing.T ) {
318388 t .Parallel ()
319389
0 commit comments