| 🛟 Updated. 28d5985 k33g 20h ago | 1 | package acp_test |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 📦 Turbo Core f3ade8d k33g 12h ago | 9 | "rickub.com/turbo-editors/turbo-core/acp" |
| 🛟 Updated. 28d5985 k33g 20h ago | 10 | ) |
| 11 | |
| 12 | func TestAnUpdateThatCannotBeDecodedIsCountedAndNamed(t *testing.T) { |
| 13 | session, agent := start(t, acp.Options{}) |
| 14 | id := agent.handshake() |
| 15 | waitFor(t, "ready", session.Ready) |
| 16 | |
| 17 | // input as a string, where the protocol has an object. An agent that did |
| 18 | // this would otherwise have its commands vanish without a trace. |
| 19 | agent.update(id, `{"sessionUpdate":"available_commands_update","availableCommands":[{"name":"web","input":"query"}]}`) |
| 20 | agent.update(id, `{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"still here"}}`) |
| 21 | waitFor(t, "the message after it", func() bool { |
| 22 | return textOf(session.Entries(), acp.EntryAgent) == "still here" |
| 23 | }) |
| 24 | |
| 25 | if session.Unknown() != 1 { |
| 26 | t.Errorf("Unknown() = %d, want the undecodable update counted", session.Unknown()) |
| 27 | } |
| 28 | got := session.Unreadable() |
| 29 | if !strings.HasPrefix(got, "available_commands_update: ") { |
| 30 | t.Errorf("Unreadable() = %q, want it to name the update's kind", got) |
| 31 | } |
| 32 | if len(session.Commands()) != 0 { |
| 33 | t.Error("a half-decoded command list was kept") |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestTheTraceRecordsEachLineInEachDirection(t *testing.T) { |
| 38 | path := filepath.Join(t.TempDir(), "acp.log") |
| 39 | t.Setenv(acp.TraceEnv, path) |
| 40 | |
| 41 | agent, stream := newFakeAgent(t) |
| 42 | session := acp.NewSession(acp.TraceStreamForTest(stream), acp.Agent{Name: "Bob"}, t.TempDir(), acp.Options{}) |
| 43 | agent.handshake() |
| 44 | waitFor(t, "ready", session.Ready) |
| 45 | _ = session.Close() // flushes the trace |
| 46 | |
| 47 | data, err := os.ReadFile(path) |
| 48 | if err != nil { |
| 49 | t.Fatalf("reading the trace: %v", err) |
| 50 | } |
| 51 | text := string(data) |
| 52 | |
| 53 | for _, want := range []string{"-> ", `"method":"initialize"`, "<- ", `"sessionId"`} { |
| 54 | if !strings.Contains(text, want) { |
| 55 | t.Errorf("the trace lacks %q:\n%s", want, text) |
| 56 | } |
| 57 | } |
| 58 | lines := strings.Split(strings.TrimSpace(text), "\n") |
| 59 | if len(lines) != 4 { |
| 60 | t.Errorf("the trace has %d lines, want the four messages of a handshake:\n%s", len(lines), text) |
| 61 | } |
| 62 | for _, line := range lines { |
| 63 | if !strings.Contains(line, " -> ") && !strings.Contains(line, " <- ") { |
| 64 | t.Errorf("a traced line has no direction mark: %q", line) |
| 65 | } |
| 66 | } |
| 67 | } |