summaryrefslogtreecommitdiff
path: root/internal/hexaicli/testhelpers_test.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-08-17 18:52:51 +0300
committerPaul Buetow <paul@buetow.org>2025-08-17 18:52:51 +0300
commit454451105ad3522d2ac3d22136eedee4a4d034af (patch)
treeaa4b5723809c4d45bfc9094a38c01c6415582f9c /internal/hexaicli/testhelpers_test.go
parent498923e77c201ca90dc35c7934f4f7f1c9c3ccd2 (diff)
cli+lsp: refactor main packages into internal runners; add tests
- Move CLI logic to internal/hexaicli with Run/RunWithClient - Move LSP logic to internal/hexailsp with Run/RunWithFactory - Extract helpers; keep behavior identical for both binaries - Add unit tests for hexaicli (input parsing, messages, streaming) and hexailsp (factory wiring, client creation, logging settings) - Add top-of-file summaries and 'Not yet reviewed by a human' comments to all Go files - Update README with internal package docs
Diffstat (limited to 'internal/hexaicli/testhelpers_test.go')
-rw-r--r--internal/hexaicli/testhelpers_test.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/internal/hexaicli/testhelpers_test.go b/internal/hexaicli/testhelpers_test.go
new file mode 100644
index 0000000..4a25ff1
--- /dev/null
+++ b/internal/hexaicli/testhelpers_test.go
@@ -0,0 +1,63 @@
+// Summary: Test helpers for Hexai CLI tests (stdin swapping and fake LLM clients/streamers).
+// Not yet reviewed by a human
+package hexaicli
+
+import (
+ "context"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "hexai/internal/llm"
+)
+
+// setStdin sets os.Stdin from a string and returns a restore func and reader.
+func setStdin(t *testing.T, content string) (func(), *os.File) {
+ t.Helper()
+ tmpDir := t.TempDir()
+ fpath := filepath.Join(tmpDir, "stdin.txt")
+ if err := os.WriteFile(fpath, []byte(content), 0o600); err != nil {
+ t.Fatalf("write temp stdin: %v", err)
+ }
+ f, err := os.Open(fpath)
+ if err != nil {
+ t.Fatalf("open temp stdin: %v", err)
+ }
+ old := os.Stdin
+ os.Stdin = f
+ restore := func() {
+ f.Close()
+ os.Stdin = old
+ }
+ return restore, f
+}
+
+// fakeClient implements llm.Client for tests.
+type fakeClient struct {
+ name string
+ model string
+ resp string
+ gotMsgs []llm.Message
+}
+
+func (f *fakeClient) Chat(ctx context.Context, messages []llm.Message, opts ...llm.RequestOption) (string, error) {
+ f.gotMsgs = append([]llm.Message{}, messages...)
+ return f.resp, nil
+}
+func (f fakeClient) Name() string { return f.name }
+func (f fakeClient) DefaultModel() string { return f.model }
+
+// fakeStreamer implements llm.Streamer over fakeClient.
+type fakeStreamer struct {
+ fakeClient
+ chunks []string
+ sMsgs []llm.Message
+}
+
+func (s *fakeStreamer) ChatStream(ctx context.Context, messages []llm.Message, onDelta func(string), opts ...llm.RequestOption) error {
+ s.sMsgs = append([]llm.Message{}, messages...)
+ for _, c := range s.chunks {
+ onDelta(c)
+ }
+ return nil
+}