diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-19 08:58:38 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-19 08:58:38 +0200 |
| commit | 31394385e72dd3a317585838ed1696076043cc60 (patch) | |
| tree | 79c0c43b514df611fdcd15c899c8d1048cd01ab0 /internal/hexaiaction | |
| parent | 934266d5bbefbc33c95e933b6ef02875539bb72f (diff) | |
Inject runner dependencies across CLI, action, and LSP
Diffstat (limited to 'internal/hexaiaction')
| -rw-r--r-- | internal/hexaiaction/run.go | 34 | ||||
| -rw-r--r-- | internal/hexaiaction/run_seam_test.go | 35 |
2 files changed, 67 insertions, 2 deletions
diff --git a/internal/hexaiaction/run.go b/internal/hexaiaction/run.go index 84cb9b1..f34a4cd 100644 --- a/internal/hexaiaction/run.go +++ b/internal/hexaiaction/run.go @@ -64,10 +64,18 @@ type actionClient interface { type actionClientFactory func(cfg appconfig.App) (actionClient, error) +type actionConfigLoader func(context.Context, *log.Logger) appconfig.App + +type actionStatusSink interface { + SetLLMStart(provider, model string) error +} + // Runner executes action requests with injectable dependencies for testability. type Runner struct { chooseAction actionChooser newClient actionClientFactory + loadConfig actionConfigLoader + statusSink actionStatusSink } // NewRunner builds a Runner with production dependencies. @@ -75,6 +83,8 @@ func NewRunner() *Runner { return &Runner{ chooseAction: chooseActionFromConfig, newClient: defaultActionClientFactory, + loadConfig: loadActionConfig, + statusSink: tmuxActionStatusSink{}, } } @@ -91,6 +101,16 @@ func defaultActionClientFactory(cfg appconfig.App) (actionClient, error) { return llmutils.NewClientFromApp(cfg) } +type tmuxActionStatusSink struct{} + +func (tmuxActionStatusSink) SetLLMStart(provider, model string) error { + return tmux.SetStatus(tmux.FormatLLMStartStatus(provider, model)) +} + +func loadActionConfig(ctx context.Context, logger *log.Logger) appconfig.App { + return appconfig.LoadWithOptions(logger, appconfig.LoadOptions{ConfigPath: configPathFromContext(ctx)}) +} + type actionPlan struct { fallback string run func(context.Context) (string, error) @@ -127,6 +147,8 @@ func Run(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer) error { func (r *Runner) Run(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer) error { chooser := chooseActionFromConfig newClient := defaultActionClientFactory + loadConfig := loadActionConfig + statusSink := actionStatusSink(tmuxActionStatusSink{}) if r != nil { if r.chooseAction != nil { chooser = r.chooseAction @@ -134,10 +156,16 @@ func (r *Runner) Run(ctx context.Context, stdin io.Reader, stdout, stderr io.Wri if r.newClient != nil { newClient = r.newClient } + if r.loadConfig != nil { + loadConfig = r.loadConfig + } + if r.statusSink != nil { + statusSink = r.statusSink + } } logger := log.New(stderr, "hexai-tmux-action ", log.LstdFlags|log.Lmsgprefix) - cfg := appconfig.LoadWithOptions(logger, appconfig.LoadOptions{ConfigPath: configPathFromContext(ctx)}) + cfg := loadConfig(ctx, logger) if cfg.StatsWindowMinutes > 0 { stats.SetWindow(time.Duration(cfg.StatsWindowMinutes) * time.Minute) } @@ -159,7 +187,9 @@ func (r *Runner) Run(ctx context.Context, stdin io.Reader, stdout, stderr io.Wri if primaryModel == "" { primaryModel = cli.DefaultModel() } - _ = tmux.SetStatus(tmux.FormatLLMStartStatus(cli.Name(), primaryModel)) + if statusSink != nil { + _ = statusSink.SetLLMStart(cli.Name(), primaryModel) + } var client chatDoer = cli parts, err := ParseInput(stdin) if err != nil { diff --git a/internal/hexaiaction/run_seam_test.go b/internal/hexaiaction/run_seam_test.go index affd68e..8fb8533 100644 --- a/internal/hexaiaction/run_seam_test.go +++ b/internal/hexaiaction/run_seam_test.go @@ -3,6 +3,7 @@ package hexaiaction import ( "bytes" "context" + "log" "testing" "codeberg.org/snonux/hexai/internal/appconfig" @@ -17,6 +18,17 @@ func (llmFake) Chat(_ context.Context, _ []llm.Message, _ ...llm.RequestOption) func (llmFake) Name() string { return "fake" } func (llmFake) DefaultModel() string { return "model" } +type recordingActionStatusSink struct { + provider string + model string +} + +func (s *recordingActionStatusSink) SetLLMStart(provider, model string) error { + s.provider = provider + s.model = model + return nil +} + func TestRun_WithSeams_SkipAndRewrite(t *testing.T) { // Isolate from user config to avoid environment-dependent behavior/logging. t.Setenv("XDG_CONFIG_HOME", t.TempDir()) @@ -48,3 +60,26 @@ func TestRun_WithSeams_SkipAndRewrite(t *testing.T) { t.Fatalf("expected non-empty rewrite output") } } + +func TestRun_WithInjectedConfigAndStatusSink(t *testing.T) { + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + sink := &recordingActionStatusSink{} + runner := NewRunner() + runner.loadConfig = func(context.Context, *log.Logger) appconfig.App { return appconfig.Load(nil) } + runner.newClient = func(_ appconfig.App) (actionClient, error) { return llmFake{}, nil } + runner.chooseAction = func(_ appconfig.App) (actionChoice, error) { + return actionChoice{kind: ActionSkip}, nil + } + runner.statusSink = sink + + var out bytes.Buffer + if err := runner.Run(context.Background(), bytes.NewBufferString("selection"), &out, &bytes.Buffer{}); err != nil { + t.Fatalf("Run: %v", err) + } + if out.String() != "selection" { + t.Fatalf("unexpected output %q", out.String()) + } + if sink.provider != "fake" || sink.model == "" { + t.Fatalf("unexpected status sink values: provider=%q model=%q", sink.provider, sink.model) + } +} |
