|
| 1 | +package handoff |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/forjd/aid/internal/git" |
| 8 | + resumepkg "github.com/forjd/aid/internal/resume" |
| 9 | + "github.com/forjd/aid/internal/store" |
| 10 | +) |
| 11 | + |
| 12 | +func TestBuild(t *testing.T) { |
| 13 | + nextAction := "inspect retry middleware" |
| 14 | + snapshot := Build( |
| 15 | + "feat/auth", |
| 16 | + git.WorktreeStatus{Dirty: true, Changed: 2, Untracked: 1}, |
| 17 | + resumepkg.Bundle{ |
| 18 | + ActiveTask: taskPointer(handoffTask(1, "feat/auth", store.TaskInProgress, "Fix refresh retry path")), |
| 19 | + Notes: []store.Note{ |
| 20 | + handoffNote(1, "Retry still fails after 401"), |
| 21 | + }, |
| 22 | + Decisions: []store.Decision{ |
| 23 | + handoffDecision(1, "Use a single refresh retry"), |
| 24 | + }, |
| 25 | + RecentCommits: []store.Commit{ |
| 26 | + handoffCommit("abc123456789", "fix: refresh retry path"), |
| 27 | + }, |
| 28 | + OpenQuestions: []string{ |
| 29 | + "Which retry path is still failing?", |
| 30 | + "Should the API return a clearer auth error?", |
| 31 | + }, |
| 32 | + NextAction: &nextAction, |
| 33 | + }, |
| 34 | + []store.Task{ |
| 35 | + handoffTask(1, "feat/auth", store.TaskInProgress, "Fix refresh retry path"), |
| 36 | + handoffTask(2, "feat/auth", store.TaskOpen, "Review retry logging"), |
| 37 | + handoffTask(3, "feat/auth", store.TaskDone, "Ignore completed work"), |
| 38 | + }, |
| 39 | + ) |
| 40 | + |
| 41 | + want := "Branch: feat/auth\n" + |
| 42 | + "Worktree: dirty (2 changed, 1 untracked)\n" + |
| 43 | + "Active task: Fix refresh retry path\n" + |
| 44 | + "Open tasks:\n" + |
| 45 | + "- Fix refresh retry path [in_progress]\n" + |
| 46 | + "- Review retry logging [open]\n" + |
| 47 | + "Recent notes:\n" + |
| 48 | + "- Retry still fails after 401\n" + |
| 49 | + "Key decisions:\n" + |
| 50 | + "- Use a single refresh retry\n" + |
| 51 | + "Recent commits:\n" + |
| 52 | + "- abc1234 fix: refresh retry path\n" + |
| 53 | + "Open questions:\n" + |
| 54 | + "- Which retry path is still failing?\n" + |
| 55 | + "- Should the API return a clearer auth error?\n" + |
| 56 | + "- Should the current uncommitted changes be kept, finished, or discarded?\n" + |
| 57 | + "Recommended next action:\n" + |
| 58 | + "- inspect retry middleware" |
| 59 | + |
| 60 | + if snapshot.Branch != "feat/auth" { |
| 61 | + t.Fatalf("unexpected branch: %#v", snapshot) |
| 62 | + } |
| 63 | + if snapshot.Summary != want { |
| 64 | + t.Fatalf("unexpected handoff summary:\n%s", snapshot.Summary) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestBuildWithAmbiguousTaskAndCleanWorktree(t *testing.T) { |
| 69 | + snapshot := Build( |
| 70 | + "feat/auth", |
| 71 | + git.WorktreeStatus{}, |
| 72 | + resumepkg.Bundle{ActiveTaskAmbiguous: true}, |
| 73 | + nil, |
| 74 | + ) |
| 75 | + |
| 76 | + want := "Branch: feat/auth\nWorktree: clean\nActive task: ambiguous" |
| 77 | + if snapshot.Summary != want { |
| 78 | + t.Fatalf("unexpected minimal handoff summary:\n%s", snapshot.Summary) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestLimitOpenTasks(t *testing.T) { |
| 83 | + tasks := []store.Task{ |
| 84 | + handoffTask(1, "", store.TaskOpen, "One"), |
| 85 | + handoffTask(2, "", store.TaskDone, "Done"), |
| 86 | + handoffTask(3, "", store.TaskBlocked, "Two"), |
| 87 | + handoffTask(4, "", store.TaskOpen, "Three"), |
| 88 | + handoffTask(5, "", store.TaskOpen, "Four"), |
| 89 | + handoffTask(6, "", store.TaskOpen, "Five"), |
| 90 | + handoffTask(7, "", store.TaskOpen, "Six"), |
| 91 | + } |
| 92 | + |
| 93 | + limited := limitOpenTasks(tasks, 5) |
| 94 | + if len(limited) != 5 { |
| 95 | + t.Fatalf("expected 5 open tasks, got %#v", limited) |
| 96 | + } |
| 97 | + for _, task := range limited { |
| 98 | + if task.Status == store.TaskDone { |
| 99 | + t.Fatalf("did not expect done task in open task list: %#v", limited) |
| 100 | + } |
| 101 | + } |
| 102 | + if limited[4].Text != "Five" { |
| 103 | + t.Fatalf("expected limit to preserve task order, got %#v", limited) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestWorktreeLine(t *testing.T) { |
| 108 | + tests := []struct { |
| 109 | + name string |
| 110 | + status git.WorktreeStatus |
| 111 | + want string |
| 112 | + }{ |
| 113 | + {name: "clean", status: git.WorktreeStatus{}, want: "clean"}, |
| 114 | + {name: "untracked only", status: git.WorktreeStatus{Dirty: true, Untracked: 2}, want: "dirty (2 untracked)"}, |
| 115 | + {name: "changed only", status: git.WorktreeStatus{Dirty: true, Changed: 3}, want: "dirty (3 changed)"}, |
| 116 | + {name: "changed and untracked", status: git.WorktreeStatus{Dirty: true, Changed: 3, Untracked: 2}, want: "dirty (3 changed, 2 untracked)"}, |
| 117 | + } |
| 118 | + |
| 119 | + for _, test := range tests { |
| 120 | + t.Run(test.name, func(t *testing.T) { |
| 121 | + if got := worktreeLine(test.status); got != test.want { |
| 122 | + t.Fatalf("worktreeLine(%#v) = %q, want %q", test.status, got, test.want) |
| 123 | + } |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestShortSHA(t *testing.T) { |
| 129 | + if got := shortSHA("abc1234"); got != "abc1234" { |
| 130 | + t.Fatalf("expected unchanged short sha, got %q", got) |
| 131 | + } |
| 132 | + if got := shortSHA("abc123456789"); got != "abc1234" { |
| 133 | + t.Fatalf("expected shortened sha, got %q", got) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestHandoffOpenQuestions(t *testing.T) { |
| 138 | + questions := handoffOpenQuestions([]string{"One", "Two", "Three"}, git.WorktreeStatus{Dirty: true}) |
| 139 | + if len(questions) != 3 || questions[2] != "Three" { |
| 140 | + t.Fatalf("expected existing questions to be kept when already at limit, got %#v", questions) |
| 141 | + } |
| 142 | + |
| 143 | + questions = handoffOpenQuestions([]string{"One", "Two"}, git.WorktreeStatus{Dirty: true}) |
| 144 | + want := []string{"One", "Two", "Should the current uncommitted changes be kept, finished, or discarded?"} |
| 145 | + if len(questions) != len(want) { |
| 146 | + t.Fatalf("unexpected handoff questions: %#v", questions) |
| 147 | + } |
| 148 | + for i := range want { |
| 149 | + if questions[i] != want[i] { |
| 150 | + t.Fatalf("unexpected handoff questions: %#v", questions) |
| 151 | + } |
| 152 | + } |
| 153 | + questions = handoffOpenQuestions(nil, git.WorktreeStatus{}) |
| 154 | + if len(questions) != 0 { |
| 155 | + t.Fatalf("expected no questions for clean worktree, got %#v", questions) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func taskPointer(task store.Task) *store.Task { |
| 160 | + return &task |
| 161 | +} |
| 162 | + |
| 163 | +func handoffTask(id int64, branch string, status store.TaskStatus, text string) store.Task { |
| 164 | + return store.Task{ |
| 165 | + ID: id, |
| 166 | + Branch: branch, |
| 167 | + Text: text, |
| 168 | + Status: status, |
| 169 | + CreatedAt: handoffTime(), |
| 170 | + UpdatedAt: handoffTime(), |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func handoffNote(id int64, text string) store.Note { |
| 175 | + return store.Note{ID: id, Text: text, CreatedAt: handoffTime()} |
| 176 | +} |
| 177 | + |
| 178 | +func handoffDecision(id int64, text string) store.Decision { |
| 179 | + return store.Decision{ID: id, Text: text, CreatedAt: handoffTime()} |
| 180 | +} |
| 181 | + |
| 182 | +func handoffCommit(sha, summary string) store.Commit { |
| 183 | + return store.Commit{SHA: sha, Summary: summary, CommittedAt: handoffTime()} |
| 184 | +} |
| 185 | + |
| 186 | +func handoffTime() time.Time { |
| 187 | + return time.Date(2026, 4, 15, 10, 0, 0, 0, time.UTC) |
| 188 | +} |
0 commit comments