summaryrefslogtreecommitdiff
path: root/internal/hexaicli
diff options
context:
space:
mode:
Diffstat (limited to 'internal/hexaicli')
-rw-r--r--internal/hexaicli/editor_integration_test.go83
-rw-r--r--internal/hexaicli/run_more_test.go51
2 files changed, 74 insertions, 60 deletions
diff --git a/internal/hexaicli/editor_integration_test.go b/internal/hexaicli/editor_integration_test.go
index 1bebf75..7d53d1f 100644
--- a/internal/hexaicli/editor_integration_test.go
+++ b/internal/hexaicli/editor_integration_test.go
@@ -1,51 +1,62 @@
package hexaicli
import (
- "bytes"
- "context"
- "os"
- "testing"
+ "bytes"
+ "context"
+ "os"
+ "testing"
- "codeberg.org/snonux/hexai/internal/appconfig"
- "codeberg.org/snonux/hexai/internal/editor"
- "codeberg.org/snonux/hexai/internal/llm"
+ "codeberg.org/snonux/hexai/internal/appconfig"
+ "codeberg.org/snonux/hexai/internal/editor"
+ "codeberg.org/snonux/hexai/internal/llm"
)
type cliFake struct{}
-func (cliFake) Chat(_ context.Context, _ []llm.Message, _ ...llm.RequestOption) (string, error) { return "OUT", nil }
-func (cliFake) Name() string { return "fake" }
+
+func (cliFake) Chat(_ context.Context, _ []llm.Message, _ ...llm.RequestOption) (string, error) {
+ return "OUT", nil
+}
+func (cliFake) Name() string { return "fake" }
func (cliFake) DefaultModel() string { return "m" }
-func (cliFake) CodeCompletion(context.Context, string, string, int, string, float64) ([]string, error) { return nil, nil }
+func (cliFake) CodeCompletion(context.Context, string, string, int, string, float64) ([]string, error) {
+ return nil, nil
+}
func TestRun_NoArgs_OpensEditor(t *testing.T) {
- // Seam: fake client and editor
- oldNew := newClientFromApp
- newClientFromApp = func(_ appconfig.App) (llm.Client, error) { return cliFake{}, nil }
- t.Cleanup(func(){ newClientFromApp = oldNew })
- oldRun := editor.RunEditor
- editor.RunEditor = func(_ string, path string) error { return os.WriteFile(path, []byte("PROMPT"), 0o600) }
- t.Cleanup(func(){ editor.RunEditor = oldRun })
- t.Setenv("HEXAI_EDITOR", "dummy")
+ // Seam: fake client and editor
+ oldNew := newClientFromApp
+ newClientFromApp = func(_ appconfig.App) (llm.Client, error) { return cliFake{}, nil }
+ t.Cleanup(func() { newClientFromApp = oldNew })
+ oldRun := editor.RunEditor
+ editor.RunEditor = func(_ string, path string) error { return os.WriteFile(path, []byte("PROMPT"), 0o600) }
+ t.Cleanup(func() { editor.RunEditor = oldRun })
+ t.Setenv("HEXAI_EDITOR", "dummy")
- // Provide stdin selection
- var stdout, stderr bytes.Buffer
- if err := Run(context.Background(), nil, bytes.NewBufferString("SELECTION"), &stdout, &stderr); err != nil {
- t.Fatalf("Run: %v", err)
- }
- if stdout.String() == "" { t.Fatalf("expected some output") }
+ // Provide stdin selection
+ var stdout, stderr bytes.Buffer
+ if err := Run(context.Background(), nil, bytes.NewBufferString("SELECTION"), &stdout, &stderr); err != nil {
+ t.Fatalf("Run: %v", err)
+ }
+ if stdout.String() == "" {
+ t.Fatalf("expected some output")
+ }
}
func TestRun_WithArgs_DoesNotOpenEditor(t *testing.T) {
- // Provide args; still use fake client
- oldNew := newClientFromApp
- newClientFromApp = func(_ appconfig.App) (llm.Client, error) { return cliFake{}, nil }
- t.Cleanup(func(){ newClientFromApp = oldNew })
- // Stub editor and detect if called (should not be)
- called := false
- oldRun := editor.RunEditor
- editor.RunEditor = func(_ string, _ string) error { called = true; return nil }
- t.Cleanup(func(){ editor.RunEditor = oldRun })
- var stdout, stderr bytes.Buffer
- if err := Run(context.Background(), []string{"ARG"}, bytes.NewBufferString("SEL"), &stdout, &stderr); err != nil { t.Fatalf("Run: %v", err) }
- if called { t.Fatalf("editor should not be invoked when args provided") }
+ // Provide args; still use fake client
+ oldNew := newClientFromApp
+ newClientFromApp = func(_ appconfig.App) (llm.Client, error) { return cliFake{}, nil }
+ t.Cleanup(func() { newClientFromApp = oldNew })
+ // Stub editor and detect if called (should not be)
+ called := false
+ oldRun := editor.RunEditor
+ editor.RunEditor = func(_ string, _ string) error { called = true; return nil }
+ t.Cleanup(func() { editor.RunEditor = oldRun })
+ var stdout, stderr bytes.Buffer
+ if err := Run(context.Background(), []string{"ARG"}, bytes.NewBufferString("SEL"), &stdout, &stderr); err != nil {
+ t.Fatalf("Run: %v", err)
+ }
+ if called {
+ t.Fatalf("editor should not be invoked when args provided")
+ }
}
diff --git a/internal/hexaicli/run_more_test.go b/internal/hexaicli/run_more_test.go
index ae29563..bd88d56 100644
--- a/internal/hexaicli/run_more_test.go
+++ b/internal/hexaicli/run_more_test.go
@@ -1,44 +1,47 @@
package hexaicli
import (
- "bytes"
- "context"
- "testing"
+ "bytes"
+ "context"
+ "testing"
- "codeberg.org/snonux/hexai/internal/appconfig"
- "codeberg.org/snonux/hexai/internal/llm"
+ "codeberg.org/snonux/hexai/internal/appconfig"
+ "codeberg.org/snonux/hexai/internal/llm"
)
type streamClient struct{}
func (streamClient) Chat(ctx context.Context, msgs []llm.Message, opts ...llm.RequestOption) (string, error) {
- return "X", nil
+ return "X", nil
}
func (streamClient) Name() string { return "fake" }
func (streamClient) DefaultModel() string { return "m" }
func (streamClient) ChatStream(ctx context.Context, msgs []llm.Message, onDelta func(string), opts ...llm.RequestOption) error {
- onDelta("A")
- onDelta("B")
- return nil
+ onDelta("A")
+ onDelta("B")
+ return nil
}
func TestRunChat_Streaming(t *testing.T) {
- var out, errw bytes.Buffer
- input := "hello"
- msgs := []llm.Message{{Role: "user", Content: input}}
- if err := runChat(context.Background(), streamClient{}, msgs, input, &out, &errw); err != nil {
- t.Fatalf("runChat failed: %v", err)
- }
- if out.String() != "AB" {
- t.Fatalf("unexpected stream output: %q", out.String())
- }
+ var out, errw bytes.Buffer
+ input := "hello"
+ msgs := []llm.Message{{Role: "user", Content: input}}
+ if err := runChat(context.Background(), streamClient{}, msgs, input, &out, &errw); err != nil {
+ t.Fatalf("runChat failed: %v", err)
+ }
+ if out.String() != "AB" {
+ t.Fatalf("unexpected stream output: %q", out.String())
+ }
}
func TestBuildMessagesFromConfig(t *testing.T) {
- cfg := appconfig.App{PromptCLIDefaultSystem: "DEF", PromptCLIExplainSystem: "EXP"}
- msgs := buildMessagesFromConfig(cfg, "tell me")
- if msgs[0].Content != "DEF" { t.Fatalf("default system wrong: %q", msgs[0].Content) }
- msgs = buildMessagesFromConfig(cfg, "please explain")
- if msgs[0].Content != "EXP" { t.Fatalf("explain system wrong: %q", msgs[0].Content) }
+ cfg := appconfig.App{PromptCLIDefaultSystem: "DEF", PromptCLIExplainSystem: "EXP"}
+ msgs := buildMessagesFromConfig(cfg, "tell me")
+ if msgs[0].Content != "DEF" {
+ t.Fatalf("default system wrong: %q", msgs[0].Content)
+ }
+ msgs = buildMessagesFromConfig(cfg, "please explain")
+ if msgs[0].Content != "EXP" {
+ t.Fatalf("explain system wrong: %q", msgs[0].Content)
+ }
}
-