summaryrefslogtreecommitdiff
path: root/internal/askcli
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-06-18 07:45:37 +0300
committerPaul Buetow <paul@buetow.org>2026-06-18 07:45:37 +0300
commit4ffb22e7f69f1c9c79b095d4e60bad3d97aac55b (patch)
tree2dc708cbb95975d34084eb5afd376871caa13e27 /internal/askcli
parentece7dcfd232b780f5650326c8ac2379ca70387d4 (diff)
ik0 replace test seams with dependency injection
Diffstat (limited to 'internal/askcli')
-rw-r--r--internal/askcli/command_edit.go10
-rw-r--r--internal/askcli/command_edit_test.go31
-rw-r--r--internal/askcli/command_watch.go7
-rw-r--r--internal/askcli/command_watch_test.go33
-rw-r--r--internal/askcli/dispatch.go17
5 files changed, 46 insertions, 52 deletions
diff --git a/internal/askcli/command_edit.go b/internal/askcli/command_edit.go
index a82d575..e2c2dbf 100644
--- a/internal/askcli/command_edit.go
+++ b/internal/askcli/command_edit.go
@@ -8,11 +8,11 @@ import (
"codeberg.org/snonux/hexai/internal/editor"
)
-// captureFromEditor opens the user's editor on a temporary file pre-filled with
+// editorCapture opens the user's editor on a temporary file pre-filled with
// the given initial content and returns its trimmed contents after the editor
// exits. ctx is forwarded to the editor subprocess so it can be cancelled with
-// the surrounding command. It is a variable so tests can stub it.
-var captureFromEditor = func(ctx context.Context, initial []byte) (string, error) {
+// the surrounding command.
+func editorCapture(ctx context.Context, initial []byte) (string, error) {
return editor.OpenTempAndEdit(ctx, initial)
}
@@ -23,7 +23,7 @@ func (d *Dispatcher) handleEdit(ctx context.Context, args []string, stdout, stde
if len(args) >= 2 {
return d.editTaskDescription(ctx, args[1], stdout, stderr)
}
- description, err := captureFromEditor(ctx, nil)
+ description, err := d.capture(ctx, nil)
if err != nil {
writeInfoError(stderr, err)
return 1, nil
@@ -44,7 +44,7 @@ func (d *Dispatcher) editTaskDescription(ctx context.Context, selector string, s
return code, nil
}
- description, err := captureFromEditor(ctx, []byte(tasks[0].Description))
+ description, err := d.capture(ctx, []byte(tasks[0].Description))
if err != nil {
writeInfoError(stderr, err)
return 1, nil
diff --git a/internal/askcli/command_edit_test.go b/internal/askcli/command_edit_test.go
index 4db19c1..91569c8 100644
--- a/internal/askcli/command_edit_test.go
+++ b/internal/askcli/command_edit_test.go
@@ -9,13 +9,10 @@ import (
"testing"
)
-func stubEditorCapture(t *testing.T, content string, err error) {
- t.Helper()
- old := captureFromEditor
- captureFromEditor = func(_ context.Context, initial []byte) (string, error) {
+func stubEditorCapture(d *Dispatcher, content string, err error) {
+ d.capture = func(_ context.Context, initial []byte) (string, error) {
return content, err
}
- t.Cleanup(func() { captureFromEditor = old })
}
func TestHandleEdit_Success(t *testing.T) {
@@ -26,15 +23,14 @@ func TestHandleEdit_Success(t *testing.T) {
{UUID: "existing-uuid", Alias: "0", CreatedAt: now},
},
})
- // editor.OpenTempAndEdit trims content, so mimic that here.
- stubEditorCapture(t, "Multi line\ntask description", nil)
-
var capturedArgs []string
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
capturedArgs = args
_, _ = io.WriteString(stdout, "Created task abc-123-def.")
return 0, nil
}})
+ // editor.OpenTempAndEdit trims content, so mimic that here.
+ stubEditorCapture(d, "Multi line\ntask description", nil)
var stdout, stderr bytes.Buffer
code, _ := d.Dispatch(context.Background(), []string{"edit"}, nil, &stdout, &stderr)
@@ -58,15 +54,8 @@ func TestHandleEdit_ExistingTaskModifiesDescription(t *testing.T) {
},
})
- var initialContent []byte
- old := captureFromEditor
- captureFromEditor = func(_ context.Context, initial []byte) (string, error) {
- initialContent = initial
- return "updated description", nil
- }
- t.Cleanup(func() { captureFromEditor = old })
-
var capturedArgs []string
+ var initialContent []byte
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
// First call: export to resolve selector. Subsequent: modify.
if len(args) >= 2 && args[1] == "export" {
@@ -76,6 +65,10 @@ func TestHandleEdit_ExistingTaskModifiesDescription(t *testing.T) {
capturedArgs = args
return 0, nil
}})
+ d.capture = func(_ context.Context, initial []byte) (string, error) {
+ initialContent = initial
+ return "updated description", nil
+ }
var stdout, stderr bytes.Buffer
code, _ := d.Dispatch(context.Background(), []string{"edit", "0"}, nil, &stdout, &stderr)
@@ -97,12 +90,11 @@ func TestHandleEdit_ExistingTaskModifiesDescription(t *testing.T) {
}
func TestHandleEdit_EmptyContentAborts(t *testing.T) {
- stubEditorCapture(t, "", nil)
-
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
t.Fatalf("runner should not be called on empty content")
return 0, nil
}})
+ stubEditorCapture(d, "", nil)
var stdout, stderr bytes.Buffer
code, _ := d.Dispatch(context.Background(), []string{"edit"}, nil, &stdout, &stderr)
@@ -115,12 +107,11 @@ func TestHandleEdit_EmptyContentAborts(t *testing.T) {
}
func TestHandleEdit_EditorError(t *testing.T) {
- stubEditorCapture(t, "", errors.New("boom"))
-
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
t.Fatalf("runner should not be called when editor fails")
return 0, nil
}})
+ stubEditorCapture(d, "", errors.New("boom"))
var stdout, stderr bytes.Buffer
code, _ := d.Dispatch(context.Background(), []string{"edit"}, nil, &stdout, &stderr)
diff --git a/internal/askcli/command_watch.go b/internal/askcli/command_watch.go
index 8d8e35f..0e0a678 100644
--- a/internal/askcli/command_watch.go
+++ b/internal/askcli/command_watch.go
@@ -30,7 +30,10 @@ func (t realWatchTicker) Stop() {
t.ticker.Stop()
}
-var newWatchTicker = func(interval time.Duration) watchTicker {
+// newRealWatchTicker is the default watchTicker factory wired into a
+// Dispatcher by NewDispatcher. It is injected as Dispatcher.newTicker so tests
+// can substitute a fake ticker instead of mutating package state.
+func newRealWatchTicker(interval time.Duration) watchTicker {
return realWatchTicker{ticker: time.NewTicker(interval)}
}
@@ -63,7 +66,7 @@ func (d *Dispatcher) handleWatch(ctx context.Context, args []string, stdout, std
return 1, nil
}
- ticker := newWatchTicker(watchInterval)
+ ticker := d.newTicker(watchInterval)
defer ticker.Stop()
var lastOutput []byte
diff --git a/internal/askcli/command_watch_test.go b/internal/askcli/command_watch_test.go
index abd74ff..7618060 100644
--- a/internal/askcli/command_watch_test.go
+++ b/internal/askcli/command_watch_test.go
@@ -27,14 +27,11 @@ func (t *fakeWatchTicker) Stop() {
func TestHandleWatch_ForwardsNonZeroCode(t *testing.T) {
ticks := make(chan time.Time)
- oldTicker := newWatchTicker
- newWatchTicker = func(time.Duration) watchTicker { return &fakeWatchTicker{ch: ticks} }
- t.Cleanup(func() { newWatchTicker = oldTicker })
-
ctx := context.Background()
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
return 1, nil
}})
+ d.newTicker = func(time.Duration) watchTicker { return &fakeWatchTicker{ch: ticks} }
var stdout, stderr bytes.Buffer
code, err := d.Dispatch(ctx, []string{"watch", "urgency"}, nil, &stdout, &stderr)
@@ -70,10 +67,6 @@ func TestHandleWatch_ForwardsInnerError(t *testing.T) {
func TestHandleWatch_DrawsStderrOnNonZero(t *testing.T) {
ticks := make(chan time.Time)
- oldTicker := newWatchTicker
- newWatchTicker = func(time.Duration) watchTicker { return &fakeWatchTicker{ch: ticks} }
- t.Cleanup(func() { newWatchTicker = oldTicker })
-
ctx, cancel := context.WithCancel(context.Background())
callCount := 0
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
@@ -87,6 +80,7 @@ func TestHandleWatch_DrawsStderrOnNonZero(t *testing.T) {
_, _ = io.WriteString(stdout, "[]")
return 0, nil
}})
+ d.newTicker = func(time.Duration) watchTicker { return &fakeWatchTicker{ch: ticks} }
var out bytes.Buffer
code, err := d.Dispatch(ctx, []string{"watch", "info"}, nil, &out, &bytes.Buffer{})
@@ -118,14 +112,6 @@ func TestHandleWatch_DefaultsToListAndRedrawsOnChange(t *testing.T) {
ticks := make(chan time.Time, 1)
ticks <- time.Now()
fakeTicker := &fakeWatchTicker{ch: ticks}
- oldTicker := newWatchTicker
- newWatchTicker = func(interval time.Duration) watchTicker {
- if interval != watchInterval {
- t.Fatalf("watch interval = %s, want %s", interval, watchInterval)
- }
- return fakeTicker
- }
- t.Cleanup(func() { newWatchTicker = oldTicker })
ctx, cancel := context.WithCancel(context.Background())
var calls [][]string
@@ -139,6 +125,12 @@ func TestHandleWatch_DefaultsToListAndRedrawsOnChange(t *testing.T) {
cancel()
return 0, nil
}})
+ d.newTicker = func(interval time.Duration) watchTicker {
+ if interval != watchInterval {
+ t.Fatalf("watch interval = %s, want %s", interval, watchInterval)
+ }
+ return fakeTicker
+ }
var stdout, stderr bytes.Buffer
code, err := d.Dispatch(ctx, []string{"watch"}, nil, &stdout, &stderr)
@@ -176,9 +168,6 @@ func TestHandleWatch_DoesNotRedrawUnchangedOutput(t *testing.T) {
ticks := make(chan time.Time, 1)
ticks <- time.Now()
- oldTicker := newWatchTicker
- newWatchTicker = func(time.Duration) watchTicker { return &fakeWatchTicker{ch: ticks} }
- t.Cleanup(func() { newWatchTicker = oldTicker })
ctx, cancel := context.WithCancel(context.Background())
runCount := 0
@@ -190,6 +179,7 @@ func TestHandleWatch_DoesNotRedrawUnchangedOutput(t *testing.T) {
}
return 0, nil
}})
+ d.newTicker = func(time.Duration) watchTicker { return &fakeWatchTicker{ch: ticks} }
var stdout, stderr bytes.Buffer
code, err := d.Dispatch(ctx, []string{"watch", "list"}, nil, &stdout, &stderr)
@@ -206,10 +196,6 @@ func TestHandleWatch_DoesNotRedrawUnchangedOutput(t *testing.T) {
func TestHandleWatch_ForwardsSubcommandArgs(t *testing.T) {
ticks := make(chan time.Time)
- oldTicker := newWatchTicker
- newWatchTicker = func(time.Duration) watchTicker { return &fakeWatchTicker{ch: ticks} }
- t.Cleanup(func() { newWatchTicker = oldTicker })
-
ctx, cancel := context.WithCancel(context.Background())
var gotArgs []string
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
@@ -218,6 +204,7 @@ func TestHandleWatch_ForwardsSubcommandArgs(t *testing.T) {
_, _ = io.WriteString(stdout, `[]`)
return 0, nil
}})
+ d.newTicker = func(time.Duration) watchTicker { return &fakeWatchTicker{ch: ticks} }
var stdout, stderr bytes.Buffer
code, err := d.Dispatch(ctx, []string{"watch", "ready", "limit:2"}, nil, &stdout, &stderr)
diff --git a/internal/askcli/dispatch.go b/internal/askcli/dispatch.go
index 924ca67..cdfc1d8 100644
--- a/internal/askcli/dispatch.go
+++ b/internal/askcli/dispatch.go
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
+ "time"
)
// Runner performs CLI work that would otherwise be handled by the ask CLI itself.
@@ -14,18 +15,30 @@ type Runner interface {
}
// Dispatcher translates CLI arguments into concrete subcommands and presents the output.
+//
+// newTicker is the injected factory for the watch-loop ticker. Production code
+// uses the real time.Ticker-backed factory installed by NewDispatcher; tests
+// inject a fake ticker so they can drive the watch loop without real delays.
type Dispatcher struct {
runner Runner
jsonOutput bool
+ newTicker func(time.Duration) watchTicker
+ capture func(context.Context, []byte) (string, error)
}
-// NewDispatcher creates a Dispatcher backed by the provided Runner or a default executor when nil.
+// NewDispatcher creates a Dispatcher backed by the provided Runner or a default
+// executor when nil. It wires the default real-ticker factory used by the
+// `ask watch` loop.
func NewDispatcher(runner Runner) *Dispatcher {
if runner == nil {
e := NewExecutor("ask")
runner = &e
}
- return &Dispatcher{runner: runner}
+ return &Dispatcher{
+ runner: runner,
+ newTicker: newRealWatchTicker,
+ capture: editorCapture,
+ }
}
func parseGlobalFlags(args []string) ([]string, bool) {