From 3246ebcc5246ed357f45ac32234d5cd34922b9f3 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 7 Sep 2025 17:58:32 +0300 Subject: test+docs: add editor tests; document HEXAI_EDITOR/EDITOR and Custom prompt; seam client in CLI for tests --- internal/hexaicli/editor_integration_test.go | 51 ++++++++++++++++++++++++++++ internal/hexaicli/run.go | 9 ++--- 2 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 internal/hexaicli/editor_integration_test.go (limited to 'internal/hexaicli') diff --git a/internal/hexaicli/editor_integration_test.go b/internal/hexaicli/editor_integration_test.go new file mode 100644 index 0000000..1bebf75 --- /dev/null +++ b/internal/hexaicli/editor_integration_test.go @@ -0,0 +1,51 @@ +package hexaicli + +import ( + "bytes" + "context" + "os" + "testing" + + "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) DefaultModel() string { return "m" } +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") + + // 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") } +} diff --git a/internal/hexaicli/run.go b/internal/hexaicli/run.go index 98e4c40..6017e51 100644 --- a/internal/hexaicli/run.go +++ b/internal/hexaicli/run.go @@ -25,7 +25,7 @@ func Run(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io. // Load configuration with a logger so file-based config is respected. logger := log.New(stderr, "hexai ", log.LstdFlags|log.Lmsgprefix) cfg := appconfig.Load(logger) - client, err := llmutils.NewClientFromApp(cfg) + client, err := newClientFromApp(cfg) if err != nil { fmt.Fprintf(stderr, logging.AnsiBase+"hexai: LLM disabled: %v"+logging.AnsiReset+"\n", err) return err @@ -154,6 +154,7 @@ func printProviderInfo(errw io.Writer, client llm.Client) { } // newClientFromConfig is kept for tests; delegates to llmutils. -func newClientFromConfig(cfg appconfig.App) (llm.Client, error) { - return llmutils.NewClientFromApp(cfg) -} +var newClientFromApp = llmutils.NewClientFromApp + +// Backcompat for tests referencing the older helper name. +func newClientFromConfig(cfg appconfig.App) (llm.Client, error) { return newClientFromApp(cfg) } -- cgit v1.2.3