Repository navigation
Expand file tree
/
Copy pathprojectsmodel_test.go
More file actions
491 lines (431 loc) · 17.5 KB
/
Copy pathprojectsmodel_test.go
File metadata and controls
491 lines (431 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//
// Date: 2026-06-15
// Author: Spicer Matthews (spicer@cloudmanic.com)
// Copyright: 2026 Cloudmanic Labs, LLC. All rights reserved.
//
package main
import (
"path/filepath"
"strings"
"testing"
tea "github.com/charmbracelet/bubbletea"
)
// sampleProjects returns two pre-sorted projects for the model tests.
func sampleProjects() []Project {
return []Project{
{Name: "Alpha", Description: "first one", WorkingDir: "/srv/alpha", Tabs: []ProjectTab{{Name: "run", Command: "make serve"}}},
{Name: "Bravo", Description: "second one", WorkingDir: "/srv/bravo", Tabs: []ProjectTab{{Name: "edit", Command: "vim"}, {Name: "shell"}}},
}
}
// step feeds one message to the model and returns the updated projectsModel.
func step(t *testing.T, m projectsModel, msg tea.Msg) projectsModel {
t.Helper()
updated, _ := m.Update(msg)
pm, ok := updated.(projectsModel)
if !ok {
t.Fatalf("Update returned %T, want projectsModel", updated)
}
return pm
}
// TestNewProjectsModelItems confirms each project becomes a selectable list row
// carrying its name, description, and original index.
func TestNewProjectsModelItems(t *testing.T) {
m := newProjectsModel(sampleProjects(), "/cfg/projects", "")
if len(m.list.items) != 2 {
t.Fatalf("got %d list items, want 2", len(m.list.items))
}
if !m.list.items[0].selectable || m.list.items[0].name != "Alpha" || m.list.items[0].desc != "first one" || m.list.items[0].ref != 0 {
t.Fatalf("item0 = %+v", m.list.items[0])
}
if m.list.items[1].ref != 1 {
t.Fatalf("item1 ref = %d, want 1", m.list.items[1].ref)
}
}
// groupedProjects returns four name-sorted projects spanning two named groups
// and one group-less project, the way loadProjects would hand them over.
func groupedProjects() []Project {
return []Project{
{Name: "Acme API", Group: "Acme", Tabs: []ProjectTab{{Name: "run", Command: "make"}}},
{Name: "Acme Web", Group: "Acme", Tabs: []ProjectTab{{Name: "run", Command: "make"}}},
{Name: "Loose", Tabs: []ProjectTab{{Name: "run", Command: "make"}}},
{Name: "Zeta", Group: "beta", Tabs: []ProjectTab{{Name: "run", Command: "make"}}},
}
}
// TestOrderProjectsByGroupUngrouped confirms that with no project declaring a
// group, the rows are a plain, heading-less list whose refs index straight into
// the unchanged input.
func TestOrderProjectsByGroupUngrouped(t *testing.T) {
in := sampleProjects() // neither sample sets a group
ordered, items := orderProjectsByGroup(in)
if len(ordered) != len(in) || ordered[0].Name != "Alpha" || ordered[1].Name != "Bravo" {
t.Fatalf("ungrouped order changed: %+v", ordered)
}
if len(items) != 2 {
t.Fatalf("got %d rows, want 2 (no headings)", len(items))
}
for i, it := range items {
if !it.selectable || it.ref != i {
t.Fatalf("row %d = %+v, want a selectable row with ref %d", i, it, i)
}
}
}
// TestOrderProjectsByGroupHeadings confirms that once any project sets a group,
// the rows become heading-bracketed: named groups in case-insensitive
// alphabetical order ("Acme" before "beta"), then an "Ungrouped" heading, with
// each selectable row's ref pointing at the right project in the returned order.
func TestOrderProjectsByGroupHeadings(t *testing.T) {
ordered, items := orderProjectsByGroup(groupedProjects())
// The display order interleaves groups: Acme's two, then beta's one, then the
// group-less one last.
wantOrder := []string{"Acme API", "Acme Web", "Zeta", "Loose"}
if len(ordered) != len(wantOrder) {
t.Fatalf("got %d ordered projects, want %d", len(ordered), len(wantOrder))
}
for i, name := range wantOrder {
if ordered[i].Name != name {
t.Fatalf("ordered[%d] = %q, want %q", i, ordered[i].Name, name)
}
}
// The row sequence: heading, its projects, … then the Ungrouped heading.
var headings []string
for _, it := range items {
if !it.selectable {
headings = append(headings, it.name)
continue
}
// Every selectable row's ref must resolve to the project of the same name.
if ordered[it.ref].Name != it.name {
t.Fatalf("row %q ref %d resolves to %q", it.name, it.ref, ordered[it.ref].Name)
}
}
wantHeadings := []string{"Acme", "beta", ungroupedHeading}
if strings.Join(headings, ",") != strings.Join(wantHeadings, ",") {
t.Fatalf("headings = %v, want %v", headings, wantHeadings)
}
}
// TestProjectsModelGroupedSelect confirms the grouped browser is fully navigable:
// the cursor starts on the first project under the first heading (skipping the
// heading row), and entering opens the project its ref points at.
func TestProjectsModelGroupedSelect(t *testing.T) {
m := newProjectsModel(groupedProjects(), "/cfg/projects", "")
m = step(t, m, tea.WindowSizeMsg{Width: 100, Height: 30})
// Cursor parks on the first selectable row, not the "Acme" heading.
if got := m.list.selectedIndex(); got != 0 || m.projects[got].Name != "Acme API" {
t.Fatalf("initial selection ref = %d, want 0 (Acme API)", got)
}
// Down once lands on Acme Web; once more skips the "beta" heading to Zeta.
m = step(t, m, tea.KeyMsg{Type: tea.KeyDown})
m = step(t, m, tea.KeyMsg{Type: tea.KeyDown})
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
if m.chosen == nil || m.chosen.Name != "Zeta" {
t.Fatalf("after two downs + enter, chosen = %v, want Zeta", m.chosen)
}
}
// TestProjectsModelGroupedFilterDropsHeadings confirms search is unchanged: a
// query collapses the grouped view to a single ranked list with no headings, and
// enter opens the lone match.
func TestProjectsModelGroupedFilterDropsHeadings(t *testing.T) {
m := newProjectsModel(groupedProjects(), "/cfg/projects", "")
m = step(t, m, tea.WindowSizeMsg{Width: 100, Height: 30})
m = step(t, m, tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("loose")})
for _, it := range m.list.filtered {
if !it.item.selectable {
t.Fatalf("a heading row survived filtering: %+v", it.item)
}
}
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
if m.chosen == nil || m.chosen.Name != "Loose" {
t.Fatalf("filtering to 'loose' then enter chose %v, want Loose", m.chosen)
}
}
// TestProjectsModelEnterSelects confirms pressing enter records the highlighted
// project and signals a quit.
func TestProjectsModelEnterSelects(t *testing.T) {
m := newProjectsModel(sampleProjects(), "/cfg/projects", "")
m = step(t, m, tea.WindowSizeMsg{Width: 100, Height: 30})
m = step(t, m, tea.KeyMsg{Type: tea.KeyDown}) // move to Bravo
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
if m.chosen == nil {
t.Fatal("expected a chosen project, got nil")
}
if m.chosen.Name != "Bravo" {
t.Fatalf("chosen = %q, want Bravo", m.chosen.Name)
}
}
// TestProjectsModelEscCancels confirms esc records no selection.
func TestProjectsModelEscCancels(t *testing.T) {
m := newProjectsModel(sampleProjects(), "/cfg/projects", "")
m = step(t, m, tea.KeyMsg{Type: tea.KeyEsc})
if m.chosen != nil {
t.Fatalf("esc should not choose a project, got %q", m.chosen.Name)
}
if !m.quitting {
t.Fatal("esc should set quitting")
}
}
// TestProjectsModelFilterThenSelect confirms typing narrows the list and enter
// then opens the single remaining match.
func TestProjectsModelFilterThenSelect(t *testing.T) {
m := newProjectsModel(sampleProjects(), "/cfg/projects", "")
m = step(t, m, tea.WindowSizeMsg{Width: 100, Height: 30})
m = step(t, m, tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("brav")})
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
if m.chosen == nil || m.chosen.Name != "Bravo" {
t.Fatalf("after filtering to 'brav', chosen = %v, want Bravo", m.chosen)
}
}
// TestProjectsModelMouseClickOpens confirms a left-button release over a project
// row opens it — the click counterpart to enter. With the title bar and query
// line above, the two projects sit at screen rows 4 (Alpha) and 5 (Bravo).
func TestProjectsModelMouseClickOpens(t *testing.T) {
m := newProjectsModel(sampleProjects(), "/cfg/projects", "")
m = step(t, m, tea.WindowSizeMsg{Width: 100, Height: 30})
m = step(t, m, tea.MouseMsg{
Action: tea.MouseActionRelease,
Button: tea.MouseButtonLeft,
Y: 5,
})
if m.chosen == nil || m.chosen.Name != "Bravo" {
t.Fatalf("clicking the Bravo row chose %v, want Bravo", m.chosen)
}
}
// TestProjectsModelMouseWheelMoves confirms the scroll wheel walks the highlight
// without opening anything.
func TestProjectsModelMouseWheelMoves(t *testing.T) {
m := newProjectsModel(sampleProjects(), "/cfg/projects", "")
m = step(t, m, tea.WindowSizeMsg{Width: 100, Height: 30})
m = step(t, m, tea.MouseMsg{Action: tea.MouseActionPress, Button: tea.MouseButtonWheelDown})
if m.chosen != nil {
t.Fatal("the wheel should not open a project")
}
if got := m.list.selectedIndex(); got != 1 {
t.Fatalf("after wheel down selected ref = %d, want 1 (Bravo)", got)
}
}
// TestProjectsModelCtrlGEntersBranchMode confirms ctrl+g on a selectable row
// switches to the worktree branch prompt with an empty, focused input.
func TestProjectsModelCtrlGEntersBranchMode(t *testing.T) {
m := newProjectsModel(sampleProjects(), "/cfg/projects", "dvic/")
m = step(t, m, tea.WindowSizeMsg{Width: 100, Height: 30})
m = step(t, m, tea.KeyMsg{Type: tea.KeyCtrlG})
if m.mode != modeBranch {
t.Fatalf("mode = %v, want modeBranch", m.mode)
}
if m.chosen == nil || m.chosen.Name != "Alpha" {
t.Fatalf("chosen = %v, want Alpha", m.chosen)
}
if m.branchInput.Value() != "" {
t.Fatalf("branch input = %q, want empty", m.branchInput.Value())
}
if !m.branchInput.Focused() {
t.Fatal("branch input should be focused")
}
}
// TestProjectsModelBranchEnterTyped confirms entering a bare branch name opens a
// worktree and resolves it through the configured prefix.
func TestProjectsModelBranchEnterTyped(t *testing.T) {
m := newProjectsModel(sampleProjects(), "/cfg/projects", "dvic/")
m = step(t, m, tea.KeyMsg{Type: tea.KeyCtrlG})
m = step(t, m, tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("feature")})
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
if !m.worktree {
t.Fatal("enter in branch mode should mark worktree")
}
if m.chosen == nil || m.chosen.Name != "Alpha" {
t.Fatalf("chosen = %v, want Alpha", m.chosen)
}
if m.branch != "dvic/feature" {
t.Fatalf("branch = %q, want dvic/feature", m.branch)
}
}
// TestProjectsModelBranchEnterEmpty confirms an empty branch stays empty so
// herdr can generate its native worktree branch name.
func TestProjectsModelBranchEnterEmpty(t *testing.T) {
m := newProjectsModel(sampleProjects(), "/cfg/projects", "dvic/")
m = step(t, m, tea.KeyMsg{Type: tea.KeyCtrlG})
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
if !m.worktree {
t.Fatal("enter in branch mode should mark worktree")
}
if m.branch != "" {
t.Fatalf("branch = %q, want empty", m.branch)
}
}
// TestProjectsModelBranchEscReturnsToList confirms escape backs out of the
// branch prompt without choosing a worktree.
func TestProjectsModelBranchEscReturnsToList(t *testing.T) {
m := newProjectsModel(sampleProjects(), "/cfg/projects", "dvic/")
m = step(t, m, tea.KeyMsg{Type: tea.KeyCtrlG})
m = step(t, m, tea.KeyMsg{Type: tea.KeyEsc})
if m.mode != modeList {
t.Fatalf("mode = %v, want modeList", m.mode)
}
if m.worktree {
t.Fatal("esc should not mark worktree")
}
}
// promptProjects returns a single directory-prompting project (working_dir =
// "{prompt}"), the shape of a generic any-repo template.
func promptProjects() []Project {
return []Project{{
Name: "project",
WorkingDir: "{prompt}",
Tabs: []ProjectTab{{Name: "claude", Command: "claude"}, {Name: "terminal"}},
}}
}
// TestProjectsModelPromptDirEnterAsksForPath confirms activating a "{prompt}"
// project opens the path prompt instead of quitting straight to a workspace.
func TestProjectsModelPromptDirEnterAsksForPath(t *testing.T) {
m := newProjectsModel(promptProjects(), "/cfg/projects", "")
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
if m.mode != modePath {
t.Fatalf("mode = %v, want modePath", m.mode)
}
if m.chosen == nil || m.chosen.Name != "project" {
t.Fatalf("chosen = %v, want project", m.chosen)
}
if m.pathInput.Value() != "" || !m.pathInput.Focused() {
t.Fatal("path input should be empty and focused")
}
}
// TestProjectsModelPromptDirOpensTypedPath confirms entering an existing
// directory stamps it into the chosen project, renaming it to the directory's
// basename so the workspace label says what it holds.
func TestProjectsModelPromptDirOpensTypedPath(t *testing.T) {
dir := t.TempDir()
m := newProjectsModel(promptProjects(), "/cfg/projects", "")
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
m = step(t, m, tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(dir)})
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
if m.chosen == nil || m.chosen.WorkingDir != dir {
t.Fatalf("chosen working dir = %v, want %q", m.chosen, dir)
}
if m.chosen.Name != filepath.Base(dir) {
t.Fatalf("chosen name = %q, want %q", m.chosen.Name, filepath.Base(dir))
}
if m.worktree {
t.Fatal("plain enter should not mark worktree")
}
}
// TestProjectsModelPromptDirRejectsBadPath confirms a nonexistent path shows an
// inline error and keeps the prompt up instead of opening anything.
func TestProjectsModelPromptDirRejectsBadPath(t *testing.T) {
m := newProjectsModel(promptProjects(), "/cfg/projects", "")
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
m = step(t, m, tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("/no/such/dir-for-this-test")})
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
if m.mode != modePath {
t.Fatalf("mode = %v, want modePath still", m.mode)
}
if m.pathErr == "" {
t.Fatal("expected a validation error")
}
if m.chosen == nil || m.chosen.WorkingDir != "{prompt}" {
t.Fatalf("chosen = %v, want untouched sentinel", m.chosen)
}
}
// TestProjectsModelPromptDirCtrlGThenBranch confirms the ctrl+g flow on a
// "{prompt}" project asks for the path first, then the branch, and ends marked
// as a worktree of the typed directory.
func TestProjectsModelPromptDirCtrlGThenBranch(t *testing.T) {
dir := t.TempDir()
m := newProjectsModel(promptProjects(), "/cfg/projects", "dvic/")
m = step(t, m, tea.KeyMsg{Type: tea.KeyCtrlG})
if m.mode != modePath {
t.Fatalf("mode = %v, want modePath first", m.mode)
}
m = step(t, m, tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(dir)})
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
if m.mode != modeBranch {
t.Fatalf("mode = %v, want modeBranch after path", m.mode)
}
m = step(t, m, tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("feature")})
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
if !m.worktree || m.branch != "dvic/feature" {
t.Fatalf("worktree = %v branch = %q, want true dvic/feature", m.worktree, m.branch)
}
if m.chosen == nil || m.chosen.WorkingDir != dir {
t.Fatalf("chosen working dir = %v, want %q", m.chosen, dir)
}
}
// TestProjectsModelPromptDirEscReturnsToList confirms escape backs out of the
// path prompt without choosing anything.
func TestProjectsModelPromptDirEscReturnsToList(t *testing.T) {
m := newProjectsModel(promptProjects(), "/cfg/projects", "")
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
m = step(t, m, tea.KeyMsg{Type: tea.KeyEsc})
if m.mode != modeList {
t.Fatalf("mode = %v, want modeList", m.mode)
}
if m.chosen != nil {
t.Fatalf("chosen = %v, want nil", m.chosen)
}
}
// TestProjectsModelCtrlGOnHeadingNoops confirms ctrl+g only opens branch mode
// when the highlighted row maps to a project.
func TestProjectsModelCtrlGOnHeadingNoops(t *testing.T) {
m := newProjectsModel(groupedProjects(), "/cfg/projects", "dvic/")
m.list.cursor = 0 // "Acme" heading, not selectable.
m = step(t, m, tea.KeyMsg{Type: tea.KeyCtrlG})
if m.mode != modeList {
t.Fatalf("mode = %v, want modeList", m.mode)
}
if m.chosen != nil {
t.Fatalf("chosen = %v, want nil", m.chosen)
}
}
// TestProjectsModelBranchModeIgnoresMouse confirms a click while the branch
// prompt is open cannot activate the list row underneath.
func TestProjectsModelBranchModeIgnoresMouse(t *testing.T) {
m := newProjectsModel(sampleProjects(), "/cfg/projects", "")
m = step(t, m, tea.WindowSizeMsg{Width: 100, Height: 30})
m = step(t, m, tea.KeyMsg{Type: tea.KeyCtrlG})
m = step(t, m, tea.MouseMsg{
Action: tea.MouseActionRelease,
Button: tea.MouseButtonLeft,
Y: 5,
})
if m.mode != modeBranch {
t.Fatalf("mode = %v, want modeBranch", m.mode)
}
if m.worktree {
t.Fatal("mouse in branch mode should not activate a row")
}
if m.chosen == nil || m.chosen.Name != "Alpha" {
t.Fatalf("chosen = %v, want the original Alpha target", m.chosen)
}
}
// TestProjectsModelBrowserView confirms the populated view shows the header, the
// project names, and the highlighted project's working directory in the detail
// bar.
func TestProjectsModelBrowserView(t *testing.T) {
m := newProjectsModel(sampleProjects(), "/cfg/projects", "")
m = step(t, m, tea.WindowSizeMsg{Width: 100, Height: 30})
view := m.View()
// The detail bar shows the resolved working dir, which expandedWorkingDir
// normalizes to native separators (\srv\alpha on Windows).
for _, want := range []string{"Alpha", "Bravo", filepath.Clean("/srv/alpha")} {
if !strings.Contains(view, want) {
t.Fatalf("browser view missing %q:\n%s", want, view)
}
}
}
// TestProjectsModelEmptyState confirms that with no projects the onboarding card
// renders (with the config path and docs link) and any exit key closes it.
func TestProjectsModelEmptyState(t *testing.T) {
m := newProjectsModel(nil, "/cfg/projects", "")
m = step(t, m, tea.WindowSizeMsg{Width: 100, Height: 40})
view := m.View()
for _, want := range []string{"Welcome to Herdr Plus", "/cfg/projects", docsURL} {
if !strings.Contains(view, want) {
t.Fatalf("empty-state view missing %q:\n%s", want, view)
}
}
m = step(t, m, tea.KeyMsg{Type: tea.KeyEnter})
if !m.quitting {
t.Fatal("empty-state should quit on a key press")
}
if m.chosen != nil {
t.Fatal("empty-state must never choose a project")
}
}