From b4f06c656d8d8733a9bf2e2e2a5eb2a7a48389bb Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 11 Jun 2026 00:28:27 +0300 Subject: Extract shared chatrun package to eliminate chat-runner DRY violations The CLI, LSP server and tmux code-action tool each carried near-identical chat-running logic: invoking the LLM (streaming-aware), collecting the response, and accounting sent/received bytes into the stats package. Introduce internal/chatrun with: - Invoke: streaming-aware LLM call that collects the full response and optionally mirrors chunks to a writer (nil writer = collect only). - SentBytes / Account: shared byte counting and stats.Update. Wire all three surfaces to it: - hexaicli: runChatRequest delegates to chatrun.Invoke; summarizeChatRun uses chatrun.Account. Removed the duplicated streaming/simple helpers. - hexaiaction: runOnce uses chatrun.Invoke + chatrun.Account, keeping the tmux status update local. - lsp: chatWithStats and the completion path use chatrun.SentBytes/Invoke; extracted unavailableClientError to keep chatWithStats small. chatrun has 100% test coverage; full suite passes with -race. Co-Authored-By: Claude Opus 4.8 --- internal/chatrun/chatrun_test.go | 135 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 internal/chatrun/chatrun_test.go (limited to 'internal/chatrun/chatrun_test.go') diff --git a/internal/chatrun/chatrun_test.go b/internal/chatrun/chatrun_test.go new file mode 100644 index 0000000..f2f5806 --- /dev/null +++ b/internal/chatrun/chatrun_test.go @@ -0,0 +1,135 @@ +package chatrun + +import ( + "bytes" + "context" + "errors" + "strings" + "testing" + + "codeberg.org/snonux/hexai/internal/llm" +) + +// simpleClient implements only Chatter (no streaming). +type simpleClient struct { + resp string + err error +} + +func (c simpleClient) Chat(_ context.Context, _ []llm.Message, _ ...llm.RequestOption) (string, error) { + return c.resp, c.err +} + +// streamClient implements Chatter and llm.Streamer. +type streamClient struct { + chunks []string + err error +} + +func (c streamClient) Chat(_ context.Context, _ []llm.Message, _ ...llm.RequestOption) (string, error) { + return strings.Join(c.chunks, ""), nil +} + +func (c streamClient) ChatStream(_ context.Context, _ []llm.Message, onDelta func(string), _ ...llm.RequestOption) error { + if c.err != nil { + return c.err + } + for _, chunk := range c.chunks { + onDelta(chunk) + } + return nil +} + +// errWriter always fails, to exercise the write-error paths. +type errWriter struct{ err error } + +func (w errWriter) Write([]byte) (int, error) { return 0, w.err } + +func TestInvoke_SimpleCollectsAndWrites(t *testing.T) { + var out bytes.Buffer + got, err := Invoke(context.Background(), simpleClient{resp: "hello"}, nil, nil, &out) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if got != "hello" || out.String() != "hello" { + t.Fatalf("got=%q out=%q, want hello/hello", got, out.String()) + } +} + +func TestInvoke_SimpleNilWriter(t *testing.T) { + got, err := Invoke(context.Background(), simpleClient{resp: "x"}, nil, nil, nil) + if err != nil || got != "x" { + t.Fatalf("got=%q err=%v, want x/nil", got, err) + } +} + +func TestInvoke_SimpleChatError(t *testing.T) { + _, err := Invoke(context.Background(), simpleClient{err: errors.New("boom")}, nil, nil, nil) + if err == nil || !strings.Contains(err.Error(), "boom") { + t.Fatalf("expected chat error, got %v", err) + } +} + +func TestInvoke_SimpleWriteError(t *testing.T) { + _, err := Invoke(context.Background(), simpleClient{resp: "x"}, nil, nil, errWriter{err: errors.New("wfail")}) + if err == nil || !strings.Contains(err.Error(), "wfail") { + t.Fatalf("expected write error, got %v", err) + } +} + +func TestInvoke_StreamingCollectsAndWrites(t *testing.T) { + var out bytes.Buffer + got, err := Invoke(context.Background(), streamClient{chunks: []string{"a", "b", "c"}}, nil, nil, &out) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if got != "abc" || out.String() != "abc" { + t.Fatalf("got=%q out=%q, want abc/abc", got, out.String()) + } +} + +func TestInvoke_StreamingNilWriter(t *testing.T) { + got, err := Invoke(context.Background(), streamClient{chunks: []string{"a", "b"}}, nil, nil, nil) + if err != nil || got != "ab" { + t.Fatalf("got=%q err=%v, want ab/nil", got, err) + } +} + +func TestInvoke_StreamingError(t *testing.T) { + _, err := Invoke(context.Background(), streamClient{err: errors.New("sfail")}, nil, nil, nil) + if err == nil || !strings.Contains(err.Error(), "sfail") { + t.Fatalf("expected stream error, got %v", err) + } +} + +func TestInvoke_StreamingWriteError(t *testing.T) { + // Full text must still be collected even though the writer fails. + _, err := Invoke(context.Background(), streamClient{chunks: []string{"a", "b"}}, nil, nil, errWriter{err: errors.New("wfail")}) + if err == nil || !strings.Contains(err.Error(), "wfail") { + t.Fatalf("expected write error, got %v", err) + } +} + +func TestSentBytes(t *testing.T) { + msgs := []llm.Message{ + {Role: "system", Content: "abc"}, + {Role: "user", Content: "de"}, + } + if got := SentBytes(msgs); got != 5 { + t.Fatalf("SentBytes = %d, want 5", got) + } + if got := SentBytes(nil); got != 0 { + t.Fatalf("SentBytes(nil) = %d, want 0", got) + } +} + +func TestAccount(t *testing.T) { + msgs := []llm.Message{{Role: "user", Content: "1234"}} + sent, recv := Account(context.Background(), "prov", "model", msgs, "response") + if sent != 4 { + t.Fatalf("sent = %d, want 4", sent) + } + if recv != len("response") { + t.Fatalf("recv = %d, want %d", recv, len("response")) + } +} -- cgit v1.2.3