forked from sttts/slagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_sequence_test.go
More file actions
343 lines (321 loc) Β· 10.3 KB
/
event_sequence_test.go
File metadata and controls
343 lines (321 loc) Β· 10.3 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package slagent
import (
"bytes"
"fmt"
"io"
"strings"
"testing"
)
// turnEvent represents a single event from the Claude stream-JSON protocol,
// mapped to the Turn method calls that readTurn would make.
type turnEvent struct {
kind string // "text", "thinking", "tool", "tool_done", "tool_error", "status", "question"
text string // text content or tool name
detail string // tool detail or question prefix
}
// formatToolForTerminal mirrors session.go's formatTool for the test harness.
// Each emoji takes 2 terminal columns + 1 space separator.
func formatToolForTerminal(name, detail string) string {
icons := map[string]string{
"Read": "π", "Glob": "π", "Grep": "π", "Bash": "π»",
"Agent": "π€", "Edit": "π", "Write": "π",
"WebSearch": "π", "WebFetch": "π",
}
icon := icons[name]
if icon == "" {
icon = "π§"
}
if detail != "" {
return icon + " " + name + ": " + detail
}
return icon + " " + name
}
func TestEventSequences(t *testing.T) {
tests := []struct {
name string
events []turnEvent
// Slack expectations (text message)
wantSlack string // substring in Slack text message
wantSlackPrefix string // Slack text message starts with
wantSlackSuffix string // Slack text message ends with
wantSlackNoText bool // no Slack text message
// Slack expectations (activity message)
wantSlackActivity string // substring in Slack activity message
// Terminal expectations (ANSI stripped)
wantTerminal []string // substrings that must appear in terminal output (in order)
}{
{
name: "pure text response",
events: []turnEvent{
{kind: "text", text: "Hello "},
{kind: "text", text: "world!"},
},
wantSlackPrefix: "> π¦ Hello",
wantSlack: "world!",
wantTerminal: []string{"π€ Claude:", "Hello ", "world!"},
},
{
name: "text with markdown converted to mrkdwn",
events: []turnEvent{
{kind: "text", text: "# Plan\n**bold** text"},
},
wantSlack: "*bold*", // **bold** β *bold*
wantTerminal: []string{"# Plan", "**bold** text"},
},
{
name: "thinking then text",
events: []turnEvent{
{kind: "thinking", text: "Let me analyze..."},
{kind: "text", text: "Here is my analysis."},
},
wantSlack: "analysis",
wantTerminal: []string{"π Let me analyze...", "Here is my analysis."},
},
{
name: "tool running then done then text",
events: []turnEvent{
{kind: "tool", text: "Read", detail: "main.go"},
{kind: "tool_done", text: "Read", detail: "main.go"},
{kind: "text", text: "I read the file."},
},
wantSlack: "I read the file",
wantTerminal: []string{"π Read: main.go", "I read the file"},
},
{
name: "multiple tools then text",
events: []turnEvent{
{kind: "tool", text: "Read", detail: "a.go"},
{kind: "tool_done", text: "Read", detail: "a.go"},
{kind: "tool", text: "Grep", detail: "pattern"},
{kind: "tool_done", text: "Grep", detail: "pattern"},
{kind: "text", text: "Found the code."},
},
wantSlack: "Found the code",
wantTerminal: []string{"π Read: a.go", "π Grep: pattern", "Found the code"},
},
{
name: "text before tool (text flushed before activity)",
events: []turnEvent{
{kind: "text", text: "Let me check. "},
{kind: "tool", text: "Read", detail: "main.go"},
{kind: "tool_done", text: "Read", detail: "main.go"},
{kind: "text", text: "Done."},
},
wantSlack: "Done",
wantTerminal: []string{"Let me check.", "π Read: main.go", "Done."},
},
{
name: "free-text question: text then AskUserQuestion",
events: []turnEvent{
{kind: "text", text: "What do you mean by Sandbox?"},
{kind: "question", text: "<@U123>: "},
},
wantSlackPrefix: "> π¦ <@U123>: ",
wantSlack: "Sandbox",
wantSlackSuffix: " β",
wantTerminal: []string{"What do you mean by Sandbox?"},
},
{
name: "free-text question: text without trailing ?",
events: []turnEvent{
{kind: "text", text: "Please tell me more."},
{kind: "question", text: "<@U123>: "},
},
wantSlackPrefix: "> π¦ <@U123>: ",
wantSlackSuffix: " β",
wantTerminal: []string{"Please tell me more."},
},
{
name: "free-text question: multi-line text",
events: []turnEvent{
{kind: "text", text: "I have a few questions:\n- What is the scope?\n- What is the timeline?"},
{kind: "question", text: "<@U123>: "},
},
wantSlackPrefix: "> π¦ <@U123>: ",
wantSlack: "scope",
wantSlackSuffix: " β",
wantTerminal: []string{"scope", "timeline"},
},
{
name: "free-text question: no owner",
events: []turnEvent{
{kind: "text", text: "What do you want?"},
{kind: "question", text: ""},
},
wantSlackPrefix: "> π¦ What",
wantSlackSuffix: " β",
wantTerminal: []string{"What do you want?"},
},
{
name: "thinking then tool then question",
events: []turnEvent{
{kind: "thinking", text: "analyzing..."},
{kind: "tool", text: "Read", detail: "go.mod"},
{kind: "tool_done", text: "Read", detail: "go.mod"},
{kind: "text", text: "Could you clarify the requirements?"},
{kind: "question", text: "<@U123>: "},
},
wantSlackPrefix: "> π¦ <@U123>: ",
wantSlackSuffix: " β",
wantTerminal: []string{"π analyzing...", "π Read: go.mod", "clarify the requirements"},
},
{
name: "only thinking, no text",
events: []turnEvent{
{kind: "thinking", text: "pondering..."},
},
wantSlackNoText: true,
wantSlackActivity: ":claude:",
wantTerminal: []string{"π pondering..."},
},
{
name: "tool error",
events: []turnEvent{
{kind: "tool", text: "Bash", detail: "go build"},
{kind: "tool_error", text: "Bash", detail: "go build"},
{kind: "text", text: "Build failed."},
},
wantSlack: "Build failed",
wantTerminal: []string{"π» Bash: go build", "Build failed"},
},
{
name: "status line in activity",
events: []turnEvent{
{kind: "status", text: "compiling..."},
{kind: "text", text: "Done."},
},
wantSlack: "Done",
wantTerminal: []string{"Done."},
},
{
name: "question replaces only last ?",
events: []turnEvent{
{kind: "text", text: "Do you want A? Or B?"},
{kind: "question", text: "<@U123>: "},
},
wantSlack: "want A?",
wantSlackSuffix: "Or B β",
wantTerminal: []string{"Do you want A? Or B?"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set up Slack mock
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.client(), "C_TEST", WithInstanceID("fox_face"))
thread.Resume("1700000001.000000")
turn := thread.NewTurn()
// Set up terminal UI with captured output
var termBuf bytes.Buffer
ui := &testUI{w: &termBuf}
ui.startResponse()
toolSeq := 0
// Replay events through both Slack turn and terminal UI
for _, ev := range tt.events {
switch ev.kind {
case "text":
ui.streamText(ev.text)
turn.Text(ev.text)
case "thinking":
ui.thinking(ev.text)
turn.Thinking(ev.text)
case "tool":
toolSeq++
ui.toolActivity(formatToolForTerminal(ev.text, ev.detail))
turn.Tool(toolID(toolSeq), ev.text, ToolRunning, ev.detail)
case "tool_done":
turn.Tool(toolID(toolSeq), ev.text, ToolDone, ev.detail)
case "tool_error":
turn.Tool(toolID(toolSeq), ev.text, ToolError, ev.detail)
case "status":
turn.Status(ev.text)
case "question":
turn.MarkQuestion(ev.text)
}
}
ui.endResponse()
turn.Finish()
// === Check Slack output ===
active := mock.activeMessages()
var textMsg, activityMsg *mockMessage
for i, m := range active {
if m.Text == "activity" {
activityMsg = &active[i]
} else {
textMsg = &active[i]
}
}
if tt.wantSlackNoText {
if textMsg != nil {
t.Errorf("slack: expected no text message, got: %q", textMsg.blockText())
}
} else if tt.wantSlack != "" || tt.wantSlackPrefix != "" || tt.wantSlackSuffix != "" {
if textMsg == nil {
t.Fatal("slack: expected text message, got none")
}
content := textMsg.blockText()
if tt.wantSlack != "" && !strings.Contains(content, tt.wantSlack) {
t.Errorf("slack: text should contain %q, got: %q", tt.wantSlack, content)
}
if tt.wantSlackPrefix != "" && !strings.HasPrefix(content, tt.wantSlackPrefix) {
t.Errorf("slack: text should start with %q, got: %q", tt.wantSlackPrefix, content)
}
if tt.wantSlackSuffix != "" && !strings.HasSuffix(content, tt.wantSlackSuffix) {
t.Errorf("slack: text should end with %q, got: %q", tt.wantSlackSuffix, content)
}
}
if tt.wantSlackActivity != "" {
if activityMsg == nil {
t.Fatal("slack: expected activity message, got none")
}
content := activityMsg.blockText()
if !strings.Contains(content, tt.wantSlackActivity) {
t.Errorf("slack: activity should contain %q, got: %q", tt.wantSlackActivity, content)
}
}
// === Check terminal output ===
termOut := stripANSI(termBuf.String())
for _, want := range tt.wantTerminal {
if !strings.Contains(termOut, want) {
t.Errorf("terminal: should contain %q, got: %q", want, termOut)
}
}
})
}
}
// stripANSI removes ANSI escape sequences from a string.
func stripANSI(s string) string {
var out strings.Builder
i := 0
for i < len(s) {
if s[i] == '\033' && i+1 < len(s) && s[i+1] == '[' {
// Skip until the final byte of the escape sequence
j := i + 2
for j < len(s) && s[j] >= 0x20 && s[j] <= 0x3F {
j++
}
if j < len(s) {
j++ // skip final byte
}
i = j
} else {
out.WriteByte(s[i])
i++
}
}
return out.String()
}
func toolID(seq int) string {
return "t" + string(rune('0'+seq))
}
// testUI is a minimal terminal UI for event sequence tests.
type testUI struct {
w io.Writer
streaming bool
}
func (u *testUI) startResponse() { fmt.Fprint(u.w, "π€ Claude: "); u.streaming = true }
func (u *testUI) streamText(text string) { fmt.Fprint(u.w, text) }
func (u *testUI) endResponse() { if u.streaming { fmt.Fprintln(u.w); u.streaming = false }; fmt.Fprintln(u.w) }
func (u *testUI) toolActivity(s string) { if u.streaming { fmt.Fprintln(u.w); u.streaming = false }; fmt.Fprintln(u.w, " "+s) }
func (u *testUI) thinking(text string) { if u.streaming { fmt.Fprintln(u.w); u.streaming = false }; fmt.Fprintln(u.w, " π "+strings.TrimRight(text, "\n")) }