-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupervisor_launch_test.go
More file actions
93 lines (83 loc) · 2.36 KB
/
Copy pathsupervisor_launch_test.go
File metadata and controls
93 lines (83 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package aicliobserve
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestLaunchUnsupportedAdapter(t *testing.T) {
s := NewSupervisor().(*supervisor)
_, err := s.Launch(context.Background(), LaunchRequest{Adapter: "unknown"})
if !errors.Is(err, ErrUnsupportedCLI) {
t.Fatalf("expected unsupported error, got %v", err)
}
}
func TestLaunchAndReceiveEvents(t *testing.T) {
binDir := t.TempDir()
script := filepath.Join(binDir, "codex")
content := "#!/bin/sh\necho permission required\nsleep 1\necho agent-turn-complete\nsleep 1\n"
if err := os.WriteFile(script, []byte(content), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
s := NewSupervisor().(*supervisor)
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel()
subCtx, subCancel := context.WithCancel(context.Background())
defer subCancel()
ch, err := s.Subscribe(subCtx)
if err != nil {
t.Fatal(err)
}
resp, err := s.Launch(ctx, LaunchRequest{Adapter: AdapterCodex})
if err != nil {
t.Fatal(err)
}
if resp.Mode != ModePTYFallback {
t.Fatalf("unexpected mode: %s", resp.Mode)
}
seenStart := false
seenTerminal := false
deadline := time.After(5 * time.Second)
for !(seenStart && seenTerminal) {
select {
case e, ok := <-ch:
if !ok {
t.Fatalf("subscriber channel closed before expected events")
}
if e.SessionID != resp.SessionID {
continue
}
switch e.State {
case StateStarting:
seenStart = true
case StateExited, StateError:
seenTerminal = true
}
case <-deadline:
t.Fatalf("missing deterministic states start=%v terminal=%v", seenStart, seenTerminal)
}
}
}
func TestStopSessionKillsProcess(t *testing.T) {
binDir := t.TempDir()
script := filepath.Join(binDir, "codex")
content := "#!/bin/sh\necho started\nsleep 20\n"
if err := os.WriteFile(script, []byte(content), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", strings.Join([]string{binDir, os.Getenv("PATH")}, string(os.PathListSeparator)))
s := NewSupervisor().(*supervisor)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
resp, err := s.Launch(ctx, LaunchRequest{Adapter: AdapterCodex})
if err != nil {
t.Fatal(err)
}
if err := s.StopSession(context.Background(), resp.SessionID); err != nil {
t.Fatal(err)
}
}