diff options
| author | Paul Buetow <paul@buetow.org> | 2026-06-18 08:05:32 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-06-18 08:05:32 +0300 |
| commit | a80ad2b4691d0df63f68c6977ee18444e7bb752f (patch) | |
| tree | 6edc47fcc3c929856826432cf3df3394a14934e8 /internal | |
| parent | 4ffb22e7f69f1c9c79b095d4e60bad3d97aac55b (diff) | |
ik0 replace remaining test seams with DI
Diffstat (limited to 'internal')
40 files changed, 408 insertions, 554 deletions
diff --git a/internal/askcli/command_add.go b/internal/askcli/command_add.go index ccfb034..0793693 100644 --- a/internal/askcli/command_add.go +++ b/internal/askcli/command_add.go @@ -56,7 +56,7 @@ func (d *Dispatcher) createTask(ctx context.Context, modifiers []string, descrip // problem as a warning on stderr but still report success on stdout with // exit 0 so the user does not retry and create a duplicate task. The // displayed identifier falls back to the UUID when no alias is available. - aliases, aliasErr := ensureTaskAliasesForUUIDs([]string{uuid}) + aliases, aliasErr := d.aliasCache.withDefaults().ensureTaskAliasesForUUIDs([]string{uuid}) if aliasErr != nil { fmt.Fprintf(stderr, "warning: failed to assign task alias: %v\n", aliasErr) aliases = nil diff --git a/internal/askcli/command_complete_uuids.go b/internal/askcli/command_complete_uuids.go index 5c26649..567602d 100644 --- a/internal/askcli/command_complete_uuids.go +++ b/internal/askcli/command_complete_uuids.go @@ -36,7 +36,7 @@ func (d *Dispatcher) completeTaskSelectors(ctx context.Context, args []string, s fmt.Fprintf(stderr, "error: failed to parse task data: %v\n", err) return 1, nil } - aliases, err := ensureTaskAliases(tasks) + aliases, err := d.aliasCache.withDefaults().ensureTaskAliases(tasks) if err != nil { fmt.Fprintf(stderr, "warning: failed to update task alias cache: %v\n", err) aliases = nil diff --git a/internal/askcli/command_complete_uuids_test.go b/internal/askcli/command_complete_uuids_test.go index 2c1b5fd..b7a2e80 100644 --- a/internal/askcli/command_complete_uuids_test.go +++ b/internal/askcli/command_complete_uuids_test.go @@ -13,14 +13,8 @@ import ( func TestHandleCompleteUUIDs_PrintsPendingUUIDs(t *testing.T) { dir := t.TempDir() - oldNow := nowTaskAliasCache - oldRoot := taskAliasCacheRoot - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - defer func() { - nowTaskAliasCache = oldNow - taskAliasCacheRoot = oldRoot - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { want := []string{"status:pending", "export"} @@ -30,6 +24,7 @@ func TestHandleCompleteUUIDs_PrintsPendingUUIDs(t *testing.T) { _, _ = io.WriteString(stdout, `[{"uuid":"uuid-1","description":"First task"},{"uuid":"uuid-2","description":"Second task"},{"uuid":""}]`) return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, err := d.handleCompleteUUIDs(context.Background(), nil, &stdout, &stderr) @@ -48,7 +43,7 @@ func TestHandleCompleteUUIDs_PrintsPendingUUIDs(t *testing.T) { t.Fatalf("stderr = %q, want empty", stderr.String()) } - path, err := taskAliasCachePath() + path, err := deps.taskAliasCachePath() if err != nil { t.Fatalf("taskAliasCachePath: %v", err) } @@ -82,11 +77,9 @@ func TestHandleCompleteUUIDs_ParseError(t *testing.T) { func TestHandleCompleteUUIDs_RecoverFromCorruptAliasCache(t *testing.T) { dir := t.TempDir() - oldRoot := taskAliasCacheRoot - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - defer func() { taskAliasCacheRoot = oldRoot }() + deps := testTaskAliasCacheDeps(dir, nil) - path, err := taskAliasCachePath() + path, err := deps.taskAliasCachePath() if err != nil { t.Fatalf("taskAliasCachePath: %v", err) } @@ -104,6 +97,7 @@ func TestHandleCompleteUUIDs_RecoverFromCorruptAliasCache(t *testing.T) { _, _ = io.WriteString(stdout, `[{"uuid":"uuid-1","description":"Fallback task"}]`) return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, err := d.handleCompleteUUIDs(context.Background(), nil, &stdout, &stderr) @@ -168,14 +162,8 @@ func TestTaskCompletionAliasItems_OnlyShortAliases(t *testing.T) { func TestHandleCompleteAliases_PrintsAliasesOnly(t *testing.T) { dir := t.TempDir() - oldNow := nowTaskAliasCache - oldRoot := taskAliasCacheRoot - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - defer func() { - nowTaskAliasCache = oldNow - taskAliasCacheRoot = oldRoot - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { want := []string{"status:pending", "export"} @@ -185,6 +173,7 @@ func TestHandleCompleteAliases_PrintsAliasesOnly(t *testing.T) { _, _ = io.WriteString(stdout, `[{"uuid":"uuid-1","description":"First task"},{"uuid":"uuid-2","description":"Second task"},{"uuid":""}]`) return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, err := d.handleCompleteAliases(context.Background(), nil, &stdout, &stderr) diff --git a/internal/askcli/command_delete_test.go b/internal/askcli/command_delete_test.go index 62d3e48..d86ee0b 100644 --- a/internal/askcli/command_delete_test.go +++ b/internal/askcli/command_delete_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "io" - "path/filepath" "strings" "testing" "time" @@ -12,21 +11,15 @@ import ( func TestHandleDelete_Success(t *testing.T) { dir := t.TempDir() - oldRoot := taskAliasCacheRoot - oldNow := nowTaskAliasCache - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - defer func() { - taskAliasCacheRoot = oldRoot - nowTaskAliasCache = oldNow - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) writeTaskAliasCacheForTest(t, taskAliasCache{ NextID: 1, Entries: []taskAliasCacheEntry{ - {UUID: "test-uuid-123", Alias: "0", CreatedAt: nowTaskAliasCache()}, + {UUID: "test-uuid-123", Alias: "0", CreatedAt: now}, }, - }) + }, deps) d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { if len(args) == 2 && args[0] == "uuid:test-uuid-123" && args[1] == "export" { @@ -35,6 +28,7 @@ func TestHandleDelete_Success(t *testing.T) { } return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, err := d.Dispatch(context.Background(), []string{"delete", "test-uuid-123"}, &bytes.Buffer{}, &stdout, &stderr) if code != 0 { @@ -120,21 +114,15 @@ func TestHandleDelete_PassesCorrectArgs(t *testing.T) { func TestHandleDelete_AliasSelector(t *testing.T) { dir := t.TempDir() - oldRoot := taskAliasCacheRoot - oldNow := nowTaskAliasCache - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - defer func() { - taskAliasCacheRoot = oldRoot - nowTaskAliasCache = oldNow - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) writeTaskAliasCacheForTest(t, taskAliasCache{ NextID: 1, Entries: []taskAliasCacheEntry{ - {UUID: "test-uuid-123", Alias: "0", CreatedAt: nowTaskAliasCache()}, + {UUID: "test-uuid-123", Alias: "0", CreatedAt: now}, }, - }) + }, deps) var capturedArgs []string d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { @@ -145,6 +133,7 @@ func TestHandleDelete_AliasSelector(t *testing.T) { capturedArgs = args return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, _ := d.Dispatch(context.Background(), []string{"delete", "0"}, &bytes.Buffer{}, &stdout, &stderr) diff --git a/internal/askcli/command_dep.go b/internal/askcli/command_dep.go index aa28df8..ce65aa4 100644 --- a/internal/askcli/command_dep.go +++ b/internal/askcli/command_dep.go @@ -75,7 +75,7 @@ func (d *Dispatcher) handleDepList(ctx context.Context, args []string, stdout, s if len(task.Depends) == 0 { _, _ = io.WriteString(stdout, "no dependencies\n") } else { - aliases, err := ensureTaskAliasesForUUIDs(task.Depends) + aliases, err := d.aliasCache.withDefaults().ensureTaskAliasesForUUIDs(task.Depends) if err != nil { fmt.Fprintf(stderr, "error: failed to load task aliases: %v\n", err) return 1, nil diff --git a/internal/askcli/command_dep_test.go b/internal/askcli/command_dep_test.go index 4cbcd26..fe3ab70 100644 --- a/internal/askcli/command_dep_test.go +++ b/internal/askcli/command_dep_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "io" - "path/filepath" "strings" "testing" "time" @@ -12,22 +11,16 @@ import ( func TestHandleDep_AddSuccess(t *testing.T) { dir := t.TempDir() - oldRoot := taskAliasCacheRoot - oldNow := nowTaskAliasCache - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - defer func() { - taskAliasCacheRoot = oldRoot - nowTaskAliasCache = oldNow - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) writeTaskAliasCacheForTest(t, taskAliasCache{ NextID: 2, Entries: []taskAliasCacheEntry{ - {UUID: "uuid-1", Alias: "0", CreatedAt: nowTaskAliasCache()}, - {UUID: "uuid-2", Alias: "1", CreatedAt: nowTaskAliasCache()}, + {UUID: "uuid-1", Alias: "0", CreatedAt: now}, + {UUID: "uuid-2", Alias: "1", CreatedAt: now}, }, - }) + }, deps) var capturedArgs []string d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { @@ -43,6 +36,7 @@ func TestHandleDep_AddSuccess(t *testing.T) { capturedArgs = args return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, _ := d.Dispatch(context.Background(), []string{"dep", "add", "uuid-1", "uuid-2"}, nil, &stdout, &stderr) if code != 0 { @@ -59,14 +53,8 @@ func TestHandleDep_AddSuccess(t *testing.T) { func TestHandleDep_RmSuccess(t *testing.T) { dir := t.TempDir() - oldRoot := taskAliasCacheRoot - oldNow := nowTaskAliasCache - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - defer func() { - taskAliasCacheRoot = oldRoot - nowTaskAliasCache = oldNow - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { if len(args) == 2 && args[1] == "export" { @@ -80,6 +68,7 @@ func TestHandleDep_RmSuccess(t *testing.T) { } return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, _ := d.Dispatch(context.Background(), []string{"dep", "rm", "uuid-1", "uuid-2"}, nil, &stdout, &stderr) if code != 0 { @@ -89,29 +78,24 @@ func TestHandleDep_RmSuccess(t *testing.T) { func TestHandleDep_ListSuccess(t *testing.T) { dir := t.TempDir() - oldRoot := taskAliasCacheRoot - oldNow := nowTaskAliasCache - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - defer func() { - taskAliasCacheRoot = oldRoot - nowTaskAliasCache = oldNow - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) writeTaskAliasCacheForTest(t, taskAliasCache{ NextID: 3, Entries: []taskAliasCacheEntry{ - {UUID: "dep-1", Alias: "1", CreatedAt: nowTaskAliasCache()}, - {UUID: "dep-2", Alias: "2", CreatedAt: nowTaskAliasCache()}, - {UUID: "uuid-1", Alias: "0", CreatedAt: nowTaskAliasCache()}, + {UUID: "dep-1", Alias: "1", CreatedAt: now}, + {UUID: "dep-2", Alias: "2", CreatedAt: now}, + {UUID: "uuid-1", Alias: "0", CreatedAt: now}, }, - }) + }, deps) jsonData := `[{"uuid":"uuid-1","description":"Task","status":"pending","priority":"M","tags":[],"urgency":10,"depends":["dep-1","dep-2"]}]` d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { _, _ = io.WriteString(stdout, jsonData) return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, _ := d.Dispatch(context.Background(), []string{"dep", "list", "uuid-1"}, nil, &stdout, &stderr) if code != 0 { @@ -198,22 +182,16 @@ func TestHandleDep_AcceptUUIDPrefix(t *testing.T) { func TestHandleDep_AliasSelectors(t *testing.T) { dir := t.TempDir() - oldRoot := taskAliasCacheRoot - oldNow := nowTaskAliasCache - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - defer func() { - taskAliasCacheRoot = oldRoot - nowTaskAliasCache = oldNow - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) writeTaskAliasCacheForTest(t, taskAliasCache{ NextID: 2, Entries: []taskAliasCacheEntry{ - {UUID: "uuid-1", Alias: "0", CreatedAt: nowTaskAliasCache()}, - {UUID: "uuid-2", Alias: "1", CreatedAt: nowTaskAliasCache()}, + {UUID: "uuid-1", Alias: "0", CreatedAt: now}, + {UUID: "uuid-2", Alias: "1", CreatedAt: now}, }, - }) + }, deps) var capturedArgs []string d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { @@ -229,6 +207,7 @@ func TestHandleDep_AliasSelectors(t *testing.T) { capturedArgs = args return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, _ := d.Dispatch(context.Background(), []string{"dep", "add", "0", "1"}, nil, &stdout, &stderr) diff --git a/internal/askcli/command_info.go b/internal/askcli/command_info.go index ba2e37d..6c02583 100644 --- a/internal/askcli/command_info.go +++ b/internal/askcli/command_info.go @@ -16,7 +16,7 @@ func (d *Dispatcher) handleInfo(ctx context.Context, args []string, stdout, stde return code, nil } allUUIDs := append([]string{tasks[0].UUID}, tasks[0].Depends...) - aliases, err := ensureTaskAliasesForUUIDs(allUUIDs) + aliases, err := d.aliasCache.withDefaults().ensureTaskAliasesForUUIDs(allUUIDs) if err != nil { fmt.Fprintf(stderr, "error: failed to load task aliases: %v\n", err) return 1, nil diff --git a/internal/askcli/command_info_add_test.go b/internal/askcli/command_info_add_test.go index b918518..ad82606 100644 --- a/internal/askcli/command_info_add_test.go +++ b/internal/askcli/command_info_add_test.go @@ -5,7 +5,6 @@ import ( "context" "encoding/json" "io" - "path/filepath" "strings" "testing" "time" @@ -14,22 +13,16 @@ import ( func TestHandleInfo_Success(t *testing.T) { unsetTestEnv(t, "HEXAI_DEBUG") dir := t.TempDir() - oldRoot := taskAliasCacheRoot - oldNow := nowTaskAliasCache - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - defer func() { - taskAliasCacheRoot = oldRoot - nowTaskAliasCache = oldNow - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) writeTaskAliasCacheForTest(t, taskAliasCache{ NextID: 2, Entries: []taskAliasCacheEntry{ - {UUID: "dep-1", Alias: "1", CreatedAt: nowTaskAliasCache()}, - {UUID: "test-uuid", Alias: "0", CreatedAt: nowTaskAliasCache()}, + {UUID: "dep-1", Alias: "1", CreatedAt: now}, + {UUID: "test-uuid", Alias: "0", CreatedAt: now}, }, - }) + }, deps) jsonData := `[{"uuid":"test-uuid","description":"Test task","status":"pending","priority":"H","tags":["cli","agent"],"urgency":15.0,"depends":["dep-1"],"annotations":[{"description":"Note 1","entry":"2026-03-22T10:00:00Z"}]}]` d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { @@ -39,6 +32,7 @@ func TestHandleInfo_Success(t *testing.T) { } return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, _ := d.Dispatch(context.Background(), []string{"info", "test-uuid"}, nil, &stdout, &stderr) if code != 0 { @@ -68,21 +62,15 @@ func TestHandleInfo_Success(t *testing.T) { func TestHandleInfo_Success_DebugShowsUUID(t *testing.T) { t.Setenv("HEXAI_DEBUG", "true") dir := t.TempDir() - oldRoot := taskAliasCacheRoot - oldNow := nowTaskAliasCache - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - defer func() { - taskAliasCacheRoot = oldRoot - nowTaskAliasCache = oldNow - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) writeTaskAliasCacheForTest(t, taskAliasCache{ NextID: 1, Entries: []taskAliasCacheEntry{ - {UUID: "test-uuid", Alias: "0", CreatedAt: nowTaskAliasCache()}, + {UUID: "test-uuid", Alias: "0", CreatedAt: now}, }, - }) + }, deps) jsonData := `[{"uuid":"test-uuid","description":"Test task","status":"pending","priority":"H","tags":["cli","agent"],"urgency":15.0,"depends":[],"annotations":[]}]` d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { @@ -91,6 +79,7 @@ func TestHandleInfo_Success_DebugShowsUUID(t *testing.T) { } return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, _ := d.Dispatch(context.Background(), []string{"info", "test-uuid"}, nil, &stdout, &stderr) @@ -107,21 +96,15 @@ func TestHandleInfo_Success_DebugShowsUUID(t *testing.T) { func TestHandleInfo_AssignsDependencyAliasesFromInfo(t *testing.T) { dir := t.TempDir() - oldRoot := taskAliasCacheRoot - oldNow := nowTaskAliasCache - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - defer func() { - taskAliasCacheRoot = oldRoot - nowTaskAliasCache = oldNow - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) writeTaskAliasCacheForTest(t, taskAliasCache{ NextID: 1, Entries: []taskAliasCacheEntry{ - {UUID: "test-uuid", Alias: "0", CreatedAt: nowTaskAliasCache()}, + {UUID: "test-uuid", Alias: "0", CreatedAt: now}, }, - }) + }, deps) jsonData := `[{"uuid":"test-uuid","description":"Test task","status":"pending","priority":"H","tags":["cli"],"urgency":15.0,"depends":["dep-b","dep-a"]}]` d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { @@ -130,6 +113,7 @@ func TestHandleInfo_AssignsDependencyAliasesFromInfo(t *testing.T) { } return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, _ := d.Dispatch(context.Background(), []string{"info", "test-uuid"}, nil, &stdout, &stderr) @@ -149,21 +133,15 @@ func TestHandleInfo_AssignsDependencyAliasesFromInfo(t *testing.T) { func TestHandleInfo_AliasSelector(t *testing.T) { unsetTestEnv(t, "HEXAI_DEBUG") dir := t.TempDir() - oldRoot := taskAliasCacheRoot - oldNow := nowTaskAliasCache - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - defer func() { - taskAliasCacheRoot = oldRoot - nowTaskAliasCache = oldNow - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) writeTaskAliasCacheForTest(t, taskAliasCache{ NextID: 1, Entries: []taskAliasCacheEntry{ - {UUID: "test-uuid", Alias: "0", CreatedAt: nowTaskAliasCache()}, + {UUID: "test-uuid", Alias: "0", CreatedAt: now}, }, - }) + }, deps) jsonData := `[{"uuid":"test-uuid","description":"Test task","status":"pending","priority":"H","tags":["cli"],"urgency":15.0,"depends":[]}]` d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { @@ -172,6 +150,7 @@ func TestHandleInfo_AliasSelector(t *testing.T) { } return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, _ := d.Dispatch(context.Background(), []string{"info", "0"}, nil, &stdout, &stderr) @@ -188,21 +167,15 @@ func TestHandleInfo_AliasSelector(t *testing.T) { func TestHandleInfo_JSONIncludesAliasID(t *testing.T) { dir := t.TempDir() - oldRoot := taskAliasCacheRoot - oldNow := nowTaskAliasCache - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - defer func() { - taskAliasCacheRoot = oldRoot - nowTaskAliasCache = oldNow - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) writeTaskAliasCacheForTest(t, taskAliasCache{ NextID: 1, Entries: []taskAliasCacheEntry{ - {UUID: "test-uuid", Alias: "0", CreatedAt: nowTaskAliasCache()}, + {UUID: "test-uuid", Alias: "0", CreatedAt: now}, }, - }) + }, deps) jsonData := `[{"uuid":"test-uuid","description":"Test task","status":"pending","priority":"H","tags":["cli"],"urgency":15.0,"depends":[]}]` d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { @@ -211,6 +184,7 @@ func TestHandleInfo_JSONIncludesAliasID(t *testing.T) { } return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, _ := d.Dispatch(context.Background(), []string{"--json", "info", "0"}, nil, &stdout, &stderr) @@ -247,14 +221,8 @@ func TestHandleInfo_NumericID(t *testing.T) { func TestHandleInfo_MissingUUID(t *testing.T) { unsetTestEnv(t, "HEXAI_DEBUG") dir := t.TempDir() - oldRoot := taskAliasCacheRoot - oldNow := nowTaskAliasCache - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - defer func() { - taskAliasCacheRoot = oldRoot - nowTaskAliasCache = oldNow - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) jsonData := `[{"uuid":"started-uuid","description":"Started task","status":"pending","priority":"M","start":"2026-03-26T10:00:00Z","urgency":5.0,"depends":[]}]` d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { @@ -263,6 +231,7 @@ func TestHandleInfo_MissingUUID(t *testing.T) { } return 0, nil }}) + d.aliasCache = deps var stdout, stderr bytes.Buffer code, _ := d.Dispatch(context.Background(), []string{"info"}, nil, &stdout, &stderr) if code != 0 { @@ -349,14 +318,11 @@ func TestHandleAdd_Success(t *testing.T) { // This prevents the previous bug where the user saw exit 1 and retried, // creating duplicate tasks. func TestHandleAdd_AliasAssignmentFailure(t *testing.T) { - oldRoot := taskAliasCacheRoot - taskAliasCacheRoot = func() (string, error) { return "", io.ErrUnexpectedEOF } - defer func() { taskAliasCacheRoot = oldRoot }() - d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { _, _ = io.WriteString(stdout, "Created task abc-123-def.") return 0, nil }}) + d.aliasCache = taskAliasCacheDeps{cacheRoot: func() (string, error) { return "", io.ErrUnexpectedEOF }} var stdout, stderr bytes.Buffer code, _ := d.Dispatch(context.Background(), []string{"add", "New task description"}, nil, &stdout, &stderr) diff --git a/internal/askcli/command_list.go b/internal/askcli/command_list.go index ebe26da..c1f37bb 100644 --- a/internal/askcli/command_list.go +++ b/internal/askcli/command_list.go @@ -55,7 +55,7 @@ func (d *Dispatcher) handleListWithFilters(ctx context.Context, initialFilters, } return tasks[i].Urgency > tasks[j].Urgency }) - return renderTaskList(tasks, stdout, stderr, d.jsonOutput) + return renderTaskListWithAliasLoader(tasks, stdout, stderr, d.jsonOutput, d.aliasCache.withDefaults().ensureTaskAliases) } func priorityOrder(p string) int { diff --git a/internal/askcli/command_list_test.go b/internal/askcli/command_list_test.go index c7c7b8a..82efe77 100644 --- a/internal/askcli/command_list_test.go +++ b/internal/askcli/command_list_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "io" - "path/filepath" "strings" "testing" "time" @@ -12,22 +11,16 @@ import ( func TestHandleList_Success(t *testing.T) { dir := t.TempDir() - oldRoot := taskAliasCacheRoot - oldNow := nowTaskAliasCache - taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil } - nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) } - defer func() { - taskAliasCacheRoot = oldRoot - nowTaskAliasCache = oldNow - }() + now := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) + deps := testTaskAliasCacheDeps(dir, &now) writeTaskAliasCacheForTest(t, taskAliasCache{ NextID: 2, Entries: []taskAliasCacheEntry{ - {UUID: "uuid-1", |
