diff options
| author | Paul Buetow <paul@buetow.org> | 2025-09-07 17:58:32 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-09-07 17:58:32 +0300 |
| commit | 3246ebcc5246ed357f45ac32234d5cd34922b9f3 (patch) | |
| tree | c594f2fd2ebc01689574c721f4e85e1065a124c4 /internal | |
| parent | 77e41a1018715fa5ac4e6156354710b3b224b4fc (diff) | |
test+docs: add editor tests; document HEXAI_EDITOR/EDITOR and Custom prompt; seam client in CLI for tests
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/editor/editor_test.go | 39 | ||||
| -rw-r--r-- | internal/hexaiaction/custom_action_test.go | 39 | ||||
| -rw-r--r-- | internal/hexaicli/editor_integration_test.go | 51 | ||||
| -rw-r--r-- | internal/hexaicli/run.go | 9 |
4 files changed, 134 insertions, 4 deletions
diff --git a/internal/editor/editor_test.go b/internal/editor/editor_test.go new file mode 100644 index 0000000..df6dec7 --- /dev/null +++ b/internal/editor/editor_test.go @@ -0,0 +1,39 @@ +package editor + +import ( + "os" + "path/filepath" + "testing" +) + +func TestResolve_EnvPriority(t *testing.T) { + t.Setenv("HEXAI_EDITOR", "ed1") + t.Setenv("EDITOR", "ed2") + ed, err := Resolve() + if err != nil || ed != "ed1" { + t.Fatalf("Resolve failed: %v %q", err, ed) + } + t.Setenv("HEXAI_EDITOR", "") + ed, err = Resolve() + if err != nil || ed != "ed2" { + t.Fatalf("Resolve fallback failed: %v %q", err, ed) + } +} + +func TestOpenTempAndEdit_UsesRunEditor(t *testing.T) { + old := RunEditor + t.Cleanup(func(){ RunEditor = old }) + // Ensure Resolve() succeeds + t.Setenv("HEXAI_EDITOR", "dummy") + var capturedPath string + RunEditor = func(editor, path string) error { + capturedPath = path + // simulate user writing content + return os.WriteFile(path, []byte("Hello\nWorld\n"), 0o600) + } + out, err := OpenTempAndEdit([]byte("# Start\n\n")) + if err != nil { t.Fatalf("OpenTempAndEdit: %v", err) } + if out != "Hello\nWorld" { t.Fatalf("unexpected content: %q", out) } + if filepath.Ext(capturedPath) != ".md" { t.Fatalf("expected .md suffix: %s", capturedPath) } +} + diff --git a/internal/hexaiaction/custom_action_test.go b/internal/hexaiaction/custom_action_test.go new file mode 100644 index 0000000..451a313 --- /dev/null +++ b/internal/hexaiaction/custom_action_test.go @@ -0,0 +1,39 @@ +package hexaiaction + +import ( + "bytes" + "context" + "testing" + + "codeberg.org/snonux/hexai/internal/appconfig" + "codeberg.org/snonux/hexai/internal/editor" + "codeberg.org/snonux/hexai/internal/llm" + "os" +) + +type llmFake2 struct{} +func (llmFake2) Chat(_ context.Context, _ []llm.Message, _ ...llm.RequestOption) (string, error) { return "DONE", nil } +func (llmFake2) Name() string { return "fake" } +func (llmFake2) DefaultModel() string { return "m" } + +func TestActionCustom_UsesEditorPrompt(t *testing.T) { + // Seam: choose custom, fake client, and fake editor + oldChoose := chooseActionFn + oldNew := newClientFromApp + chooseActionFn = func() (ActionKind, error) { return ActionCustom, nil } + newClientFromApp = func(_ appconfig.App) (llm.Client, error) { return llmFake2{}, nil } + t.Cleanup(func(){ chooseActionFn = oldChoose; newClientFromApp = oldNew }) + + oldRunEd := editor.RunEditor + editor.RunEditor = func(_ string, path string) error { + return os.WriteFile(path, []byte("make it done"), 0o600) + } + t.Cleanup(func(){ editor.RunEditor = oldRunEd }) + t.Setenv("HEXAI_EDITOR", "dummy") + + in := bytes.NewBufferString("some code") + var out bytes.Buffer + var errb bytes.Buffer + if err := Run(context.Background(), in, &out, &errb); err != nil { t.Fatalf("Run: %v", err) } + if out.String() == "" { t.Fatalf("expected output") } +} 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) } |
