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) } } }