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
|
package acp_test
import (
"os"
"path/filepath"
"strings"
"testing"
"codeberg.org/turbo-editors/turbo-core/acp"
)
func TestAnUpdateThatCannotBeDecodedIsCountedAndNamed(t *testing.T) {
session, agent := start(t, acp.Options{})
id := agent.handshake()
waitFor(t, "ready", session.Ready)
// input as a string, where the protocol has an object. An agent that did
// this would otherwise have its commands vanish without a trace.
agent.update(id, `{"sessionUpdate":"available_commands_update","availableCommands":[{"name":"web","input":"query"}]}`)
agent.update(id, `{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"still here"}}`)
waitFor(t, "the message after it", func() bool {
return textOf(session.Entries(), acp.EntryAgent) == "still here"
})
if session.Unknown() != 1 {
t.Errorf("Unknown() = %d, want the undecodable update counted", session.Unknown())
}
got := session.Unreadable()
if !strings.HasPrefix(got, "available_commands_update: ") {
t.Errorf("Unreadable() = %q, want it to name the update's kind", got)
}
if len(session.Commands()) != 0 {
t.Error("a half-decoded command list was kept")
}
}
func TestTheTraceRecordsEachLineInEachDirection(t *testing.T) {
path := filepath.Join(t.TempDir(), "acp.log")
t.Setenv(acp.TraceEnv, path)
agent, stream := newFakeAgent(t)
session := acp.NewSession(acp.TraceStreamForTest(stream), acp.Agent{Name: "Bob"}, t.TempDir(), acp.Options{})
agent.handshake()
waitFor(t, "ready", session.Ready)
_ = session.Close() // flushes the trace
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("reading the trace: %v", err)
}
text := string(data)
for _, want := range []string{"-> ", `"method":"initialize"`, "<- ", `"sessionId"`} {
if !strings.Contains(text, want) {
t.Errorf("the trace lacks %q:\n%s", want, text)
}
}
lines := strings.Split(strings.TrimSpace(text), "\n")
if len(lines) != 4 {
t.Errorf("the trace has %d lines, want the four messages of a handshake:\n%s", len(lines), text)
}
for _, line := range lines {
if !strings.Contains(line, " -> ") && !strings.Contains(line, " <- ") {
t.Errorf("a traced line has no direction mark: %q", line)
}
}
}
|