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 | |
| parent | 77e41a1018715fa5ac4e6156354710b3b224b4fc (diff) | |
test+docs: add editor tests; document HEXAI_EDITOR/EDITOR and Custom prompt; seam client in CLI for tests
| -rw-r--r-- | PROJECTSTATUS.md | 2 | ||||
| -rw-r--r-- | docs/configuration.md | 6 | ||||
| -rw-r--r-- | docs/usage.md | 1 | ||||
| -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 |
7 files changed, 143 insertions, 4 deletions
diff --git a/PROJECTSTATUS.md b/PROJECTSTATUS.md index d1d865a..8709378 100644 --- a/PROJECTSTATUS.md +++ b/PROJECTSTATUS.md @@ -4,6 +4,8 @@ This documents shows future items and in progress items. Already completed ones ## Features +* [/] EDITOR support for custom action in hexai-tmux-action +* [/] EDITOR support for hexai when no args given * [ ] In-editor chat triggers should be context aware of the current file, buffer and function! * [ ] Kagi FastGPT for in-editor search - Think about an in-editor chat trigger, maybe with S> for search! diff --git a/docs/configuration.md b/docs/configuration.md index 11f23c1..e37f743 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -56,6 +56,12 @@ This is mostly useful when Helix runs in a [tmux](https://tmux.github.io/) sessi - `--tmux-percent N` split size percentage (default: `33`). - `--ui-child` internal; used by the parent process when spawning inside tmux. +Editor integration + +- Hexai tries to launch your preferred editor when needed (e.g., TUI “Custom prompt”, CLI with no args). +- Editor resolution: `HEXAI_EDITOR`, falling back to `EDITOR` when unset. +- Invocation form: `EDITOR /tmp/hexai-XXXX.md` (a temporary Markdown file). + Code action prompts - All prompts can be customized under `[prompts.code_action]` in `config.toml`. In addition to `rewrite_*`, `diagnostics_*`, `document_*`, and `go_test_*`, the following templates control the “Simplify and improve” action: diff --git a/docs/usage.md b/docs/usage.md index 925ac26..293b038 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -109,6 +109,7 @@ hexai 'install ripgrep on macOS and explain' - Choose an action with arrow keys, `j/k`, `g/G`, Enter, or hotkeys `[s] [r] [c] [t]`. - Includes: Rewrite selection, Simplify and improve, Document code, Generate Go unit test(s), Skip. + - “Custom prompt” (hotkey `[p]`) opens your editor (`$HEXAI_EDITOR` or `$EDITOR`) on a temporary `.md` file to write a free-form instruction. - Output is written to stdout by default, or to a file via `--outfile`. Input formats 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) } |
