gunk-dev/the-valley

dashboard_select_test.go

source88a5a486b52fmain's published tip history
6.4 KiB
1package cmd
2
3import (
4 "context"
5 "strings"
6 "testing"
7 "time"
8
9 tea "github.com/charmbracelet/bubbletea"
10
11 "the-valley/flow/internal/run"
12 "the-valley/flow/internal/tmux"
13)
14
15// captureTmux is a tmux.Client that records SendKeys/SelectPane calls so tests
16// can assert on the literal text and pane id sent to the coordinator.
17type captureTmux struct {
18 tmux.Client
19 sentPane string
20 sentKeys string
21 focusPane string
22}
23
24func (c *captureTmux) SendKeys(_ context.Context, paneID, keys string) error {
25 c.sentPane = paneID
26 c.sentKeys = keys
27 return nil
28}
29
30func (c *captureTmux) SelectPane(_ context.Context, paneID string) error {
31 c.focusPane = paneID
32 return nil
33}
34
35func TestBuildRowsOneRowPerPRAndBareAgent(t *testing.T) {
36 // Sessions get no row; a bare (no-PR) agent gets its own; agents on the
37 // same PR share one row, which carries the first agent's state. Rows of the
38 // same category are most recent first, across repos.
39 states := []*run.State{
40 {ID: "s1", Type: "session", Prompt: "session"},
41 {ID: "first", Prompt: "p1", TargetRepo: strPtr("zzz"), PRURL: strPtr("https://github.com/o/zzz/pull/7"), CreatedAt: "2026-01-01T00:00:00Z"},
42 {ID: "a2", Prompt: "p2", TargetRepo: strPtr("aaa"), PRURL: strPtr("https://github.com/o/aaa/pull/9"), CreatedAt: "2026-01-01T00:01:00Z"},
43 {ID: "second", Prompt: "p3", TargetRepo: strPtr("zzz"), PRURL: strPtr("https://github.com/o/zzz/pull/7"), CreatedAt: "2026-01-01T00:02:00Z"},
44 {ID: "bare", Prompt: "bare", TargetRepo: strPtr("aaa"), Worktree: "/wt", CreatedAt: "2026-01-01T00:03:00Z"},
45 }
46 m := dashboardModel{states: states, tmuxDeps: testDashboardTmuxDeps()}
47
48 var got []string
49 for _, r := range m.buildRows() {
50 got = append(got, r.key)
51 }
52 // Both PRs are open; #7's latest activity (00:02) is newer than #9's. The
53 // exited bare agent is finished, so it sorts last.
54 want := []string{"pr:o/zzz#7", "pr:o/aaa#9", "run:bare"}
55 if strings.Join(got, ",") != strings.Join(want, ",") {
56 t.Fatalf("row keys = %v, want %v", got, want)
57 }
58 rows := m.buildRows()
59 if len(rows[0].runs) != 2 || rows[0].state().ID != "first" {
60 t.Errorf("PR #7 row runs = %d, state = %s; want 2 runs, first agent's state", len(rows[0].runs), rows[0].state().ID)
61 }
62}
63
64func TestClampCursor(t *testing.T) {
65 cases := []struct{ cursor, n, want int }{
66 {0, 0, 0},
67 {5, 0, 0},
68 {-1, 3, 0},
69 {1, 3, 1},
70 {3, 3, 2},
71 {99, 3, 2},
72 }
73 for _, c := range cases {
74 if got := clampCursor(c.cursor, c.n); got != c.want {
75 t.Errorf("clampCursor(%d, %d) = %d, want %d", c.cursor, c.n, got, c.want)
76 }
77 }
78}
79
80func TestSelectionFollowsRowWhenListChanges(t *testing.T) {
81 prevStates := []*run.State{
82 {ID: "a", Prompt: "p", TargetRepo: strPtr("r"), PRURL: strPtr("https://github.com/o/r/pull/1"), CreatedAt: "2026-01-01T00:02:00Z"},
83 {ID: "b", Prompt: "p", TargetRepo: strPtr("r"), PRURL: strPtr("https://github.com/o/r/pull/2"), CreatedAt: "2026-01-01T00:01:00Z"},
84 {ID: "c", Prompt: "p", TargetRepo: strPtr("r"), PRURL: strPtr("https://github.com/o/r/pull/3"), CreatedAt: "2026-01-01T00:00:00Z"},
85 }
86 m := dashboardModel{states: prevStates, tmuxDeps: testDashboardTmuxDeps(), cursor: 2}
87 m.syncView(time.Now())
88 if r, _ := m.selectedRow(); r.prNum != "3" {
89 t.Fatalf("selected PR #%s, want #3", r.prNum)
90 }
91
92 // PR #1 is removed; #3 still exists but shifts to index 1.
93 next, _ := m.Update(statesLoadedMsg{states: []*run.State{prevStates[1], prevStates[2]}})
94 m = next.(dashboardModel)
95 if r, _ := m.selectedRow(); r.prNum != "3" || m.cursor != 1 {
96 t.Errorf("after reload selection = #%s at %d, want PR #3 at 1", r.prNum, m.cursor)
97 }
98
99 // PR #3 is removed; the selection stays at its position, clamped.
100 next, _ = m.Update(statesLoadedMsg{states: []*run.State{prevStates[0]}})
101 m = next.(dashboardModel)
102 if r, _ := m.selectedRow(); r.prNum != "1" || m.cursor != 0 {
103 t.Errorf("after removal selection = #%s at %d, want PR #1 at 0", r.prNum, m.cursor)
104 }
105}
106
107func TestDiscussPRSendsLiteralPromptAndFocuses(t *testing.T) {
108 fake := &captureTmux{}
109 m := dashboardModel{tmux: fake}
110
111 if err := m.discussPR("583", "%7"); err != nil {
112 t.Fatalf("discussPR: %v", err)
113 }
114 if want := "WRT PR#583: "; fake.sentKeys != want {
115 t.Errorf("SendKeys keys = %q, want %q", fake.sentKeys, want)
116 }
117 if fake.sentPane != "%7" {
118 t.Errorf("SendKeys pane = %q, want %q", fake.sentPane, "%7")
119 }
120 if fake.focusPane != "%7" {
121 t.Errorf("SelectPane pane = %q, want %q", fake.focusPane, "%7")
122 }
123}
124
125func TestCoordinatorPaneFromSessionState(t *testing.T) {
126 tmpDir := t.TempDir()
127 store := run.NewHomeDirStoreFromPath(tmpDir)
128 if err := store.EnsureDirs(); err != nil {
129 t.Fatalf("EnsureDirs: %v", err)
130 }
131
132 sessionID := "20260101-0000-sess"
133 pane := "%3"
134 if err := store.Save(&run.State{ID: sessionID, Type: "session", CoordinatorPane: &pane, CreatedAt: "2026-01-01T00:00:00Z"}); err != nil {
135 t.Fatalf("Save: %v", err)
136 }
137 t.Setenv(sessionIDEnv, sessionID)
138
139 m := dashboardModel{store: store}
140 if got := m.coordinatorPane(); got != pane {
141 t.Errorf("coordinatorPane() = %q, want %q", got, pane)
142 }
143
144 // A session without a coordinator pane (older session) yields "".
145 if err := store.Save(&run.State{ID: sessionID, Type: "session", CreatedAt: "2026-01-01T00:00:00Z"}); err != nil {
146 t.Fatalf("Save: %v", err)
147 }
148 if got := m.coordinatorPane(); got != "" {
149 t.Errorf("coordinatorPane() = %q, want empty for older session", got)
150 }
151}
152
153func TestApproveSelectedPRMarksRightRun(t *testing.T) {
154 tmpDir := t.TempDir()
155 store := run.NewHomeDirStoreFromPath(tmpDir)
156 if err := store.EnsureDirs(); err != nil {
157 t.Fatalf("EnsureDirs: %v", err)
158 }
159
160 states := []*run.State{
161 {ID: "r1", Prompt: "p", Branch: "b1", TargetRepo: strPtr("r"), PRURL: strPtr("https://github.com/o/r/pull/11"), CreatedAt: "2026-01-01T00:00:00Z"},
162 {ID: "r2", Prompt: "p", Branch: "b2", TargetRepo: strPtr("r"), PRURL: strPtr("https://github.com/o/r/pull/22"), CreatedAt: "2026-01-01T00:01:00Z"},
163 }
164 for _, s := range states {
165 if err := store.Save(s); err != nil {
166 t.Fatalf("Save: %v", err)
167 }
168 }
169
170 // #22 is the more recent open PR, so it is listed first: move down to
171 // #11 and back up to #22, then approve with the 'a' key.
172 var model tea.Model = dashboardModel{store: store, states: states, tmuxDeps: testDashboardTmuxDeps()}
173 for _, k := range []string{"j", "k", "a"} {
174 model, _ = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(k)})
175 }
176
177 s22, _ := store.Load("r2")
178 if s22.Approved == nil || !*s22.Approved {
179 t.Error("PR #22 should be approved")
180 }
181 s11, _ := store.Load("r1")
182 if s11.Approved != nil {
183 t.Error("PR #11 should not be approved")
184 }
185}