-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
828 lines (706 loc) · 21.3 KB
/
main_test.go
File metadata and controls
828 lines (706 loc) · 21.3 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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
tea "github.com/charmbracelet/bubbletea"
)
// --- filterProjects tests ---
func newTestProjects(projectsDir string, names []string) []projectEntry {
projects := make([]projectEntry, 0, len(names))
for _, name := range names {
projects = append(projects, projectEntry{
displayName: name,
relativePath: name,
fullPath: filepath.Join(projectsDir, filepath.FromSlash(name)),
})
}
return projects
}
func projectDisplayNames(projects []projectEntry) []string {
names := make([]string, len(projects))
for i, project := range projects {
names[i] = project.displayName
}
return names
}
func assertProjectNames(t *testing.T, got []projectEntry, want []string) {
t.Helper()
gotNames := projectDisplayNames(got)
if len(gotNames) != len(want) {
t.Fatalf("expected %d projects, got %d (%v)", len(want), len(gotNames), gotNames)
}
for i, expected := range want {
if gotNames[i] != expected {
t.Errorf("index %d: expected %q, got %q (all: %v)", i, expected, gotNames[i], gotNames)
}
}
}
func TestFilterProjects_EmptySearch(t *testing.T) {
projects := newTestProjects("/tmp", []string{"alpha", "beta", "gamma"})
got := filterProjects(projects, "")
assertProjectNames(t, got, []string{"alpha", "beta", "gamma"})
}
func TestFilterProjects_MatchingSubstring(t *testing.T) {
projects := newTestProjects("/tmp", []string{"my-project", "other-thing", "project-two"})
got := filterProjects(projects, "project")
assertProjectNames(t, got, []string{"my-project", "project-two"})
}
func TestFilterProjects_NoMatch(t *testing.T) {
projects := newTestProjects("/tmp", []string{"alpha", "beta"})
got := filterProjects(projects, "zzz")
if len(got) != 0 {
t.Fatalf("expected 0 results, got %d", len(got))
}
}
func TestFilterProjects_CaseInsensitive(t *testing.T) {
projects := newTestProjects("/tmp", []string{"MyProject", "other"})
got := filterProjects(projects, "myproject")
assertProjectNames(t, got, []string{"MyProject"})
got2 := filterProjects(projects, "MYPROJECT")
assertProjectNames(t, got2, []string{"MyProject"})
}
func TestFilterProjects_EmptyInput(t *testing.T) {
got := filterProjects([]projectEntry{}, "test")
if len(got) != 0 {
t.Fatalf("expected 0 results, got %d", len(got))
}
}
// --- getProjects tests ---
func TestGetProjects_EmptyDir(t *testing.T) {
dir := t.TempDir()
projects, err := getProjects(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(projects) != 0 {
t.Fatalf("expected 0 projects, got %d", len(projects))
}
}
func TestGetProjects_WithSubdirs(t *testing.T) {
dir := t.TempDir()
names := []string{"oldest", "middle", "newest"}
baseTime := time.Now().Add(-3 * time.Hour)
for i, name := range names {
p := filepath.Join(dir, name)
os.Mkdir(p, 0755)
modTime := baseTime.Add(time.Duration(i) * time.Hour)
os.Chtimes(p, modTime, modTime)
}
projects, err := getProjects(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
assertProjectNames(t, projects, []string{"newest", "middle", "oldest"})
}
func TestGetProjects_MixedLayouts(t *testing.T) {
dir := t.TempDir()
legacyProject := filepath.Join(dir, "2026-03-08-legacy")
dateBucket := filepath.Join(dir, "2026-03-10")
nestedProject := filepath.Join(dateBucket, "nested-project")
nonBucketProject := filepath.Join(dir, "team")
nonBucketChild := filepath.Join(nonBucketProject, "ignored-child")
os.MkdirAll(legacyProject, 0755)
os.MkdirAll(nestedProject, 0755)
os.MkdirAll(nonBucketChild, 0755)
baseTime := time.Now().Add(-4 * time.Hour)
os.Chtimes(legacyProject, baseTime, baseTime)
os.Chtimes(nonBucketProject, baseTime.Add(1*time.Hour), baseTime.Add(1*time.Hour))
os.Chtimes(nestedProject, baseTime.Add(2*time.Hour), baseTime.Add(2*time.Hour))
projects, err := getProjects(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
assertProjectNames(t, projects, []string{"2026-03-10/nested-project", "team", "2026-03-08-legacy"})
if projects[0].fullPath != nestedProject {
t.Errorf("expected nested project path %q, got %q", nestedProject, projects[0].fullPath)
}
}
func TestGetProjects_IgnoresFiles(t *testing.T) {
dir := t.TempDir()
os.Mkdir(filepath.Join(dir, "project-dir"), 0755)
os.WriteFile(filepath.Join(dir, "file.txt"), []byte("hello"), 0644)
os.WriteFile(filepath.Join(dir, ".hidden"), []byte(""), 0644)
projects, err := getProjects(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
assertProjectNames(t, projects, []string{"project-dir"})
}
func TestGetProjects_CreatesNonExistentDir(t *testing.T) {
dir := filepath.Join(t.TempDir(), "nonexistent", "subdir")
projects, err := getProjects(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(projects) != 0 {
t.Fatalf("expected 0 projects, got %d", len(projects))
}
info, err := os.Stat(dir)
if err != nil {
t.Fatalf("directory was not created: %v", err)
}
if !info.IsDir() {
t.Fatal("created path is not a directory")
}
}
// --- expandPath tests ---
func TestExpandPath_NonTilde(t *testing.T) {
path := "/some/absolute/path"
got, err := expandPath(path)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != path {
t.Errorf("expected %q, got %q", path, got)
}
}
func TestExpandPath_TildeOnly(t *testing.T) {
got, err := expandPath("~")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
home, _ := os.UserHomeDir()
if got != home {
t.Errorf("expected %q, got %q", home, got)
}
}
func TestExpandPath_TildeSlash(t *testing.T) {
got, err := expandPath("~/foo/bar")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
home, _ := os.UserHomeDir()
expected := filepath.Join(home, "foo/bar")
if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}
}
func TestExpandPath_TildeUser(t *testing.T) {
_, err := expandPath("~someuser/path")
if err == nil {
t.Fatal("expected error for ~user path, got nil")
}
if !strings.Contains(err.Error(), "not supported") {
t.Errorf("unexpected error message: %v", err)
}
}
func TestExpandPath_RelativePath(t *testing.T) {
got, err := expandPath("relative/path")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != "relative/path" {
t.Errorf("expected %q, got %q", "relative/path", got)
}
}
// --- getProjectsDir tests ---
func TestGetProjectsDir_WithEnvVar(t *testing.T) {
dir := t.TempDir()
t.Setenv("TRY_PROJECTS_DIR", dir)
got, err := getProjectsDir()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != dir {
t.Errorf("expected %q, got %q", dir, got)
}
}
func TestGetProjectsDir_Default(t *testing.T) {
t.Setenv("TRY_PROJECTS_DIR", "")
got, err := getProjectsDir()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
home, _ := os.UserHomeDir()
expected := filepath.Join(home, "projects")
if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}
}
func TestGetProjectsDir_WithTildeExpansion(t *testing.T) {
t.Setenv("TRY_PROJECTS_DIR", "~/my-projects")
got, err := getProjectsDir()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
home, _ := os.UserHomeDir()
expected := filepath.Join(home, "my-projects")
if got != expected {
t.Errorf("expected %q, got %q", expected, got)
}
}
func TestGetProjectsDir_TildeUserError(t *testing.T) {
t.Setenv("TRY_PROJECTS_DIR", "~baduser/path")
_, err := getProjectsDir()
if err == nil {
t.Fatal("expected error for ~user path")
}
}
// --- initialModel tests ---
func TestInitialModel(t *testing.T) {
dir := t.TempDir()
os.Mkdir(filepath.Join(dir, "proj-a"), 0755)
os.Mkdir(filepath.Join(dir, "proj-b"), 0755)
m := initialModel(dir)
if len(m.choices) != 2 {
t.Fatalf("expected 2 choices, got %d", len(m.choices))
}
if len(m.filtered) != 2 {
t.Fatalf("expected 2 filtered, got %d", len(m.filtered))
}
if m.cursor != 0 {
t.Errorf("expected cursor 0, got %d", m.cursor)
}
if m.projectsDir != dir {
t.Errorf("expected projectsDir %q, got %q", dir, m.projectsDir)
}
if m.search != "" {
t.Errorf("expected empty search, got %q", m.search)
}
if len(m.choices) > 0 && m.choices[0].fullPath == "" {
t.Error("expected discovered projects to include a full path")
}
}
func TestInitialModel_EmptyDir(t *testing.T) {
dir := t.TempDir()
m := initialModel(dir)
if len(m.choices) != 0 {
t.Fatalf("expected 0 choices, got %d", len(m.choices))
}
if len(m.filtered) != 0 {
t.Fatalf("expected 0 filtered, got %d", len(m.filtered))
}
}
// --- model.Init tests ---
func TestModelInit(t *testing.T) {
m := model{}
cmd := m.Init()
if cmd != nil {
t.Error("expected Init() to return nil")
}
}
// --- model.Update tests ---
func newTestModel(choices []string, projectsDir string) model {
return model{
choices: newTestProjects(projectsDir, choices),
filtered: newTestProjects(projectsDir, choices),
cursor: 0,
projectsDir: projectsDir,
}
}
func TestUpdate_CtrlC(t *testing.T) {
m := newTestModel([]string{"a", "b"}, "/tmp")
_, cmd := m.Update(tea.KeyMsg{Type: tea.KeyCtrlC})
if cmd == nil {
t.Fatal("expected quit command, got nil")
}
}
func TestUpdate_CtrlQ(t *testing.T) {
m := newTestModel([]string{"a", "b"}, "/tmp")
_, cmd := m.Update(tea.KeyMsg{Type: tea.KeyCtrlQ})
if cmd == nil {
t.Fatal("expected quit command, got nil")
}
}
func TestUpdate_CtrlK_MovesUp(t *testing.T) {
m := newTestModel([]string{"a", "b", "c"}, "/tmp")
m.cursor = 2
updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyCtrlK})
if cmd != nil {
t.Error("expected nil command")
}
um := updated.(model)
if um.cursor != 1 {
t.Errorf("expected cursor 1, got %d", um.cursor)
}
}
func TestUpdate_CtrlK_AtTop(t *testing.T) {
m := newTestModel([]string{"a", "b"}, "/tmp")
m.cursor = 0
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyCtrlK})
um := updated.(model)
if um.cursor != 0 {
t.Errorf("expected cursor 0, got %d", um.cursor)
}
}
func TestUpdate_CtrlJ_MovesDown(t *testing.T) {
m := newTestModel([]string{"a", "b", "c"}, "/tmp")
m.cursor = 0
updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyCtrlJ})
if cmd != nil {
t.Error("expected nil command")
}
um := updated.(model)
if um.cursor != 1 {
t.Errorf("expected cursor 1, got %d", um.cursor)
}
}
func TestUpdate_CtrlJ_AtBottom(t *testing.T) {
m := newTestModel([]string{"a", "b"}, "/tmp")
m.cursor = 1
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyCtrlJ})
um := updated.(model)
if um.cursor != 1 {
t.Errorf("expected cursor 1, got %d", um.cursor)
}
}
func TestUpdate_Enter_SelectsProject(t *testing.T) {
dir := t.TempDir()
m := newTestModel([]string{"my-project"}, dir)
// Capture stdout to verify fmt.Printf output
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
_, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
w.Close()
os.Stdout = old
buf := make([]byte, 1024)
n, _ := r.Read(buf)
output := string(buf[:n])
if cmd == nil {
t.Fatal("expected quit command, got nil")
}
expectedPath := filepath.Join(dir, "my-project")
expectedOutput := fmt.Sprintf("cd %q", expectedPath)
if output != expectedOutput {
t.Errorf("expected output %q, got %q", expectedOutput, output)
}
}
func TestUpdate_Enter_SelectsNestedProject(t *testing.T) {
dir := t.TempDir()
m := newTestModel([]string{"2026-03-10/my-project"}, dir)
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
_, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
w.Close()
os.Stdout = old
buf := make([]byte, 1024)
n, _ := r.Read(buf)
output := string(buf[:n])
if cmd == nil {
t.Fatal("expected quit command, got nil")
}
expectedPath := filepath.Join(dir, "2026-03-10", "my-project")
expectedOutput := fmt.Sprintf("cd %q", expectedPath)
if output != expectedOutput {
t.Errorf("expected output %q, got %q", expectedOutput, output)
}
}
func TestUpdate_Enter_EmptyList(t *testing.T) {
m := newTestModel([]string{}, "/tmp")
_, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
if cmd != nil {
t.Error("expected nil command for enter on empty list")
}
}
func TestUpdate_TypeCharacter(t *testing.T) {
m := newTestModel([]string{"alpha", "beta", "gamma"}, "/tmp")
updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'a'}})
if cmd != nil {
t.Error("expected nil command")
}
um := updated.(model)
if um.search != "a" {
t.Errorf("expected search %q, got %q", "a", um.search)
}
// "a" matches alpha, beta (has 'a'), gamma (has 'a')
if len(um.filtered) != 3 {
t.Errorf("expected 3 filtered results, got %d: %v", len(um.filtered), um.filtered)
}
if um.cursor != 0 {
t.Errorf("expected cursor reset to 0, got %d", um.cursor)
}
}
func TestUpdate_TypeMultipleChars(t *testing.T) {
m := newTestModel([]string{"alpha", "beta", "gamma"}, "/tmp")
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'b'}})
um := updated.(model)
updated2, _ := um.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'e'}})
um2 := updated2.(model)
if um2.search != "be" {
t.Errorf("expected search %q, got %q", "be", um2.search)
}
if len(um2.filtered) != 1 || um2.filtered[0].displayName != "beta" {
t.Errorf("expected [beta], got %v", um2.filtered)
}
}
func TestUpdate_Backspace(t *testing.T) {
m := newTestModel([]string{"alpha", "beta"}, "/tmp")
m.search = "al"
m.filtered = filterProjects(m.choices, m.search)
updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyBackspace})
if cmd != nil {
t.Error("expected nil command")
}
um := updated.(model)
if um.search != "a" {
t.Errorf("expected search %q, got %q", "a", um.search)
}
// "a" matches both "alpha" and "beta" (contains 'a')
if len(um.filtered) != 2 {
t.Errorf("expected 2 filtered results, got %d: %v", len(um.filtered), um.filtered)
}
}
func TestUpdate_Backspace_EmptySearch(t *testing.T) {
m := newTestModel([]string{"alpha"}, "/tmp")
m.search = ""
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyBackspace})
um := updated.(model)
if um.search != "" {
t.Errorf("expected empty search, got %q", um.search)
}
}
func TestUpdate_Backspace_CursorAdjustment(t *testing.T) {
m := newTestModel([]string{"alpha", "beta", "gamma"}, "/tmp")
m.search = "alpha"
m.filtered = filterProjects(m.choices, m.search)
m.cursor = 0
// Backspace to "alph" — still only alpha matches, cursor stays 0
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyBackspace})
um := updated.(model)
if um.cursor != 0 {
t.Errorf("expected cursor 0, got %d", um.cursor)
}
}
func TestUpdate_Backspace_CursorClampToZero(t *testing.T) {
// Scenario: all items filtered out, cursor should clamp to 0
m := model{
choices: newTestProjects("/tmp", []string{"alpha"}),
filtered: []projectEntry{},
cursor: 0,
search: "z",
}
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyBackspace})
um := updated.(model)
// After backspace, search is empty, all items returned
if um.cursor != 0 {
t.Errorf("expected cursor 0, got %d", um.cursor)
}
}
func TestUpdate_CtrlN_CreatesProject(t *testing.T) {
dir := t.TempDir()
m := newTestModel([]string{}, dir)
m.search = "test-project"
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
_, cmd := m.Update(tea.KeyMsg{Type: tea.KeyCtrlN})
w.Close()
os.Stdout = old
buf := make([]byte, 1024)
n, _ := r.Read(buf)
output := string(buf[:n])
if cmd == nil {
t.Fatal("expected quit command, got nil")
}
today := time.Now().Format("2006-01-02")
expectedPath := filepath.Join(dir, today, "test-project")
if output != fmt.Sprintf("cd %q", expectedPath) {
t.Errorf("expected output %q, got %q", fmt.Sprintf("cd %q", expectedPath), output)
}
// Verify directory was created
info, err := os.Stat(expectedPath)
if err != nil {
t.Fatalf("project directory not created: %v", err)
}
if !info.IsDir() {
t.Fatal("created path is not a directory")
}
}
func TestUpdate_CtrlN_EmptySearch(t *testing.T) {
m := newTestModel([]string{}, "/tmp")
m.search = ""
_, cmd := m.Update(tea.KeyMsg{Type: tea.KeyCtrlN})
if cmd != nil {
t.Error("expected nil command when search is empty")
}
}
func TestUpdate_NonKeyMsg(t *testing.T) {
m := newTestModel([]string{"a"}, "/tmp")
// Send a non-key message (tea.WindowSizeMsg)
updated, cmd := m.Update(tea.WindowSizeMsg{Width: 80, Height: 24})
if cmd != nil {
t.Error("expected nil command")
}
um := updated.(model)
if um.cursor != 0 {
t.Errorf("model should be unchanged")
}
}
// --- model.View tests ---
func TestView_ContainsTitle(t *testing.T) {
m := newTestModel([]string{"project-a"}, "/tmp")
view := m.View()
if !strings.Contains(view, "try - Project Selector") {
t.Error("view should contain title")
}
}
func TestView_ShowsItems(t *testing.T) {
m := newTestModel([]string{"project-a", "project-b"}, "/tmp")
view := m.View()
if !strings.Contains(view, "project-a") {
t.Error("view should contain project-a")
}
if !strings.Contains(view, "project-b") {
t.Error("view should contain project-b")
}
}
func TestView_SelectedItem(t *testing.T) {
m := newTestModel([]string{"project-a", "project-b"}, "/tmp")
m.cursor = 0
view := m.View()
// The selected item should have ">" prefix
if !strings.Contains(view, "> project-a") {
t.Error("view should show > prefix for selected item")
}
}
func TestView_ShowsSearchText(t *testing.T) {
m := newTestModel([]string{"alpha"}, "/tmp")
m.search = "alp"
view := m.View()
if !strings.Contains(view, "Search: alp") {
t.Error("view should display search text")
}
}
func TestView_NoSearchText(t *testing.T) {
m := newTestModel([]string{"alpha"}, "/tmp")
m.search = ""
view := m.View()
if strings.Contains(view, "Search:") {
t.Error("view should not display Search: when search is empty")
}
}
func TestView_EmptyNoSearch(t *testing.T) {
m := newTestModel([]string{}, "/tmp")
m.filtered = []projectEntry{}
view := m.View()
if !strings.Contains(view, "No projects found.") {
t.Error("view should show 'No projects found.' when no projects and no search")
}
}
func TestView_EmptyWithSearch(t *testing.T) {
m := newTestModel([]string{}, "/tmp")
m.search = "xyz"
m.filtered = []projectEntry{}
view := m.View()
if !strings.Contains(view, "C-n") {
t.Error("view should mention C-n to create a new project when search has no results")
}
}
func TestView_HelpText(t *testing.T) {
m := newTestModel([]string{"a"}, "/tmp")
view := m.View()
if !strings.Contains(view, "C-k/C-j: navigate") {
t.Error("view should contain help text")
}
if !strings.Contains(view, "C-q: quit") {
t.Error("view should contain quit help")
}
}
func TestView_SecondItemSelected(t *testing.T) {
m := newTestModel([]string{"project-a", "project-b"}, "/tmp")
m.cursor = 1
view := m.View()
if !strings.Contains(view, "> project-b") {
t.Error("view should show > prefix for second item when cursor=1")
}
}
// --- Backspace cursor edge cases ---
func TestUpdate_Backspace_CursorExceedsFiltered(t *testing.T) {
// After backspace, filtered has fewer items than cursor position
m := model{
choices: newTestProjects("/tmp", []string{"abc", "abd", "xyz"}),
filtered: newTestProjects("/tmp", []string{"abc"}),
cursor: 2,
search: "abc",
}
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyBackspace})
um := updated.(model)
// search="ab", filtered=["abc","abd"], cursor was 2 which is >= len(filtered)=2, so clamped to 1
if um.cursor >= len(um.filtered) && len(um.filtered) > 0 {
t.Errorf("cursor %d should be < filtered length %d", um.cursor, len(um.filtered))
}
}
func TestUpdate_Backspace_FilteredBecomesEmpty(t *testing.T) {
// After backspace, search still has chars but nothing matches → cursor clamped to 0
m := model{
choices: newTestProjects("/tmp", []string{"xyz"}),
filtered: []projectEntry{},
cursor: 0,
search: "ab",
projectsDir: "/tmp",
}
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyBackspace})
um := updated.(model)
// search="a", "xyz" doesn't contain "a", filtered empty, cursor clamped to 0
if um.cursor != 0 {
t.Errorf("expected cursor 0, got %d", um.cursor)
}
}
// --- getProjects error path ---
func TestGetProjects_ReadDirError(t *testing.T) {
// Use a file where a directory is expected to trigger ReadDir error
dir := t.TempDir()
fakePath := filepath.Join(dir, "notadir")
os.WriteFile(fakePath, []byte("x"), 0644)
_, err := getProjects(fakePath)
if err == nil {
t.Fatal("expected error reading a file as directory")
}
}
// --- run() tests (extracted from main for testability) ---
func TestRun_NoArgs(t *testing.T) {
code := run([]string{"try"})
if code != 1 {
t.Errorf("expected exit code 1, got %d", code)
}
}
func TestRun_UnknownCommand(t *testing.T) {
code := run([]string{"try", "bogus"})
if code != 1 {
t.Errorf("expected exit code 1, got %d", code)
}
}
func TestRun_Init(t *testing.T) {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
code := run([]string{"try", "init"})
w.Close()
os.Stdout = old
buf := make([]byte, 4096)
n, _ := r.Read(buf)
output := string(buf[:n])
if code != 0 {
t.Errorf("expected exit code 0, got %d", code)
}
if !strings.Contains(output, "try()") {
t.Errorf("expected shell function, got: %s", output)
}
}
func TestRun_Cd_BadProjectsDir(t *testing.T) {
t.Setenv("TRY_PROJECTS_DIR", "~baduser/path")
code := run([]string{"try", "cd"})
if code != 1 {
t.Errorf("expected exit code 1 for bad projects dir, got %d", code)
}
}
func TestRun_Cd_ValidDir(t *testing.T) {
dir := t.TempDir()
t.Setenv("TRY_PROJECTS_DIR", dir)
done := make(chan int, 1)
go func() {
done <- run([]string{"try", "cd"})
}()
// Let the TUI initialize enough for coverage, then we're done
select {
case <-done:
case <-time.After(2 * time.Second):
// TUI blocks on input — coverage for setup lines was captured
}
}