-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnative_test.go
More file actions
200 lines (168 loc) · 4.56 KB
/
native_test.go
File metadata and controls
200 lines (168 loc) · 4.56 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package slagent
import (
"encoding/json"
"testing"
)
func TestNativeTurnTextStreaming(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.botClient(), "C_TEST", withAPIURL(mock.apiURL()))
thread.Resume("1700000001.000000")
turn := thread.NewTurn()
// Verify we got a native turn
impl := turn.(*turnImpl)
if _, ok := impl.w.(*nativeTurn); !ok {
t.Fatal("expected nativeTurn for xoxb token")
}
turn.Text("Hello world!")
err := turn.Finish()
if err != nil {
t.Fatalf("Finish: %v", err)
}
// Should have started a stream, appended text, and stopped
mock.mu.Lock()
found := false
for _, s := range mock.streams {
if s.stopped {
found = true
if len(s.chunks) == 0 {
t.Error("no chunks appended")
}
}
}
streamCount := len(mock.streams)
mock.mu.Unlock()
if streamCount == 0 {
t.Fatal("no streams created")
}
if !found {
t.Error("no stream was stopped")
}
}
func TestNativeTurnBufferThreshold(t *testing.T) {
mock := newMockSlack()
defer mock.close()
// Small buffer size to trigger mid-stream flush
thread := NewThread(mock.botClient(), "C_TEST",
withAPIURL(mock.apiURL()),
WithBufferSize(10),
)
thread.Resume("1700000001.000000")
turn := thread.NewTurn()
turn.Text("12345678901234567890") // 20 chars > bufSize 10
// Should have flushed at least once before finish
impl := turn.(*turnImpl)
n := impl.w.(*nativeTurn)
if n.flushed == 0 {
t.Error("expected buffer flush before finish")
}
turn.Finish()
}
func TestNativeTurnThinkingAccumulation(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.botClient(), "C_TEST", withAPIURL(mock.apiURL()))
thread.Resume("1700000001.000000")
turn := thread.NewTurn()
turn.Thinking("line1\n")
turn.Thinking("line2\n")
turn.Thinking("line3\n")
turn.Finish()
// Verify thinking chunks contain accumulated text
for _, s := range mock.streams {
for _, raw := range s.chunks {
var chunk map[string]any
json.Unmarshal(raw, &chunk)
if chunk["type"] == "task_update" {
val := chunk["value"].(map[string]any)
if val["id"] == "thinking" {
details := val["details"].(string)
// Last chunk should contain all lines
if len(details) == 0 {
t.Error("thinking details empty")
}
}
}
}
}
}
func TestNativeTurnToolStatusMapping(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.botClient(), "C_TEST", withAPIURL(mock.apiURL()))
thread.Resume("1700000001.000000")
turn := thread.NewTurn()
turn.Tool("t1", "Read", ToolRunning, "main.go")
turn.Tool("t1", "Read", ToolDone, "")
turn.Tool("t2", "Write", ToolError, "permission denied")
turn.Finish()
// Verify status mapping in chunks
var statuses []string
for _, s := range mock.streams {
for _, raw := range s.chunks {
var chunk map[string]any
json.Unmarshal(raw, &chunk)
if chunk["type"] == "task_update" {
val := chunk["value"].(map[string]any)
if val["id"] != "thinking" && val["id"] != "status" {
statuses = append(statuses, val["status"].(string))
}
}
}
}
want := []string{"in_progress", "completed", "failed"}
if len(statuses) != len(want) {
t.Fatalf("statuses = %v, want %v", statuses, want)
}
for i, s := range statuses {
if s != want[i] {
t.Errorf("status[%d] = %q, want %q", i, s, want[i])
}
}
}
func TestNativeTurnLazyStart(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.botClient(), "C_TEST", withAPIURL(mock.apiURL()))
thread.Resume("1700000001.000000")
turn := thread.NewTurn()
// No content sent yet — stream should not have started
if len(mock.streams) != 0 {
t.Error("stream started before any content")
}
// Finish with no content — should not start a stream
err := turn.Finish()
if err != nil {
t.Fatalf("Finish: %v", err)
}
if len(mock.streams) != 0 {
t.Error("stream started on empty finish")
}
}
func TestNativeTurnStatus(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.botClient(), "C_TEST", withAPIURL(mock.apiURL()))
thread.Resume("1700000001.000000")
turn := thread.NewTurn()
turn.Status("searching...")
turn.Status("compiling...")
turn.Finish()
// Verify status chunks were sent
statusCount := 0
for _, s := range mock.streams {
for _, raw := range s.chunks {
var chunk map[string]any
json.Unmarshal(raw, &chunk)
if chunk["type"] == "task_update" {
val := chunk["value"].(map[string]any)
if val["id"] == "status" {
statusCount++
}
}
}
}
if statusCount != 2 {
t.Errorf("status chunks = %d, want 2", statusCount)
}
}