-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtui_test.go
More file actions
1474 lines (1260 loc) · 45.4 KB
/
Copy pathtui_test.go
File metadata and controls
1474 lines (1260 loc) · 45.4 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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/foliagecp/easyjson"
"github.com/muesli/termenv"
)
func TestMain(m *testing.M) {
// Force lipgloss to emit ANSI codes in tests (no TTY by default).
lipgloss.SetColorProfile(termenv.TrueColor)
tmp, err := os.MkdirTemp("", "fg-cli-test-*")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmp)
FoliageCLIDir = tmp
NatsHubDomain = "hub"
os.Exit(m.Run())
}
// ── Fixtures ──────────────────────────────────────────────────────────────────
func makeVertexInfo(id string, outLinks, inLinks []linkId) fullVertexInfo {
body := easyjson.NewJSONObjectWithKeyValue("name", easyjson.NewJSON(id))
return fullVertexInfo{
id: id,
body: body.GetPtr(),
outLinks: outLinks,
inLinks: inLinks,
}
}
// makeLinkInfo builds a link the way the FAST path does: target and type only.
//
// It used to fill body and tags as well, which no production path does — the
// vertex read deliberately skips them. That made every test agree with a model
// state the server never produces, and is why nothing caught the tags editor
// opening blank over links that had tags. Details now come from linkDetails,
// so a test that needs them says so explicitly.
func makeLinkInfo(from, name, to, tp string) fullLinkInfo {
return fullLinkInfo{
id: linkId{from: from, name: name},
to: to,
tp: tp,
}
}
// makeModel creates a ready-to-use tuiModel with groups already built.
func makeModel(currentID string, links []displayLink, fvi *fullVertexInfo) tuiModel {
m := newTuiModel(currentID)
m.loading = false
m.links = links
m.fvi = fvi
m.grouped = buildGroupedView(links, "")
m.rCursor = 0
m.lCursor = 0
m.width = 120
m.height = 40
m.ready = true
m.bodyVP = viewport.New(40, 30)
if fvi != nil {
m.bodyVP.SetContent(m.bodyContent())
}
return m
}
// threeLinks returns 3 out-links of type "contains".
func threeLinks() []displayLink {
return []displayLink{
{info: makeLinkInfo("root", "l1", "c1", "contains"), isOut: true},
{info: makeLinkInfo("root", "l2", "c2", "contains"), isOut: true},
{info: makeLinkInfo("root", "l3", "c3", "contains"), isOut: true},
}
}
// mixedLinks returns 2 out-links of different types and 1 in-link.
func mixedLinks() []displayLink {
return []displayLink{
{info: makeLinkInfo("root", "child_a", "ca", "contains"), isOut: true},
{info: makeLinkInfo("root", "dep_b", "db", "depends_on"), isOut: true},
{info: makeLinkInfo("parent", "p_link", "root", "parent_of"), isOut: false},
}
}
func key(s string) tea.Msg { return tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(s)} }
func keyEnter() tea.Msg { return tea.KeyMsg{Type: tea.KeyEnter} }
func keyBackspace() tea.Msg { return tea.KeyMsg{Type: tea.KeyBackspace} }
func keyTab() tea.Msg { return tea.KeyMsg{Type: tea.KeyTab} }
func keyEsc() tea.Msg { return tea.KeyMsg{Type: tea.KeyEsc} }
func update(m tuiModel, msg tea.Msg) tuiModel {
next, _ := m.Update(msg)
return next.(tuiModel)
}
// ── buildGroupedView ──────────────────────────────────────────────────────────
func TestBuildGroupedView_Empty(t *testing.T) {
gv := buildGroupedView(nil, "")
if len(gv.outGroups) != 0 || len(gv.inGroups) != 0 {
t.Errorf("empty links should produce empty groups, got out=%d in=%d",
len(gv.outGroups), len(gv.inGroups))
}
if len(gv.outFlat) != 0 || len(gv.inFlat) != 0 {
t.Errorf("empty links should produce empty flat lists, got out=%d in=%d",
len(gv.outFlat), len(gv.inFlat))
}
}
func TestBuildGroupedView_GroupsByType(t *testing.T) {
links := []displayLink{
{info: makeLinkInfo("root", "a1", "x", "typeA"), isOut: true},
{info: makeLinkInfo("root", "b1", "y", "typeB"), isOut: true},
{info: makeLinkInfo("root", "a2", "z", "typeA"), isOut: true},
}
gv := buildGroupedView(links, "")
if got := len(gv.outGroups); got != 2 {
t.Fatalf("want 2 out-groups, got %d", got)
}
if gv.outGroups[0].tp != "typeA" || gv.outGroups[1].tp != "typeB" {
t.Errorf("groups should be sorted alphabetically: got %q, %q",
gv.outGroups[0].tp, gv.outGroups[1].tp)
}
if len(gv.outGroups[0].links) != 2 {
t.Errorf("typeA should have 2 links, got %d", len(gv.outGroups[0].links))
}
}
func TestBuildGroupedView_SeparatesInOut(t *testing.T) {
links := mixedLinks()
gv := buildGroupedView(links, "")
if len(gv.outGroups) != 2 {
t.Errorf("want 2 out-groups, got %d", len(gv.outGroups))
}
if len(gv.inGroups) != 1 {
t.Errorf("want 1 in-group, got %d", len(gv.inGroups))
}
}
func TestBuildGroupedView_SortsLinksWithinGroup(t *testing.T) {
links := []displayLink{
{info: makeLinkInfo("root", "zzz", "c", "t"), isOut: true},
{info: makeLinkInfo("root", "aaa", "d", "t"), isOut: true},
{info: makeLinkInfo("root", "mmm", "e", "t"), isOut: true},
}
gv := buildGroupedView(links, "")
g := gv.outGroups[0].links
if g[0].label() != "aaa" || g[1].label() != "mmm" || g[2].label() != "zzz" {
t.Errorf("links should be sorted by name: got %q, %q, %q",
g[0].label(), g[1].label(), g[2].label())
}
}
func TestBuildGroupedView_NoTypeFallback(t *testing.T) {
links := []displayLink{
{info: makeLinkInfo("root", "l1", "c1", ""), isOut: true},
}
gv := buildGroupedView(links, "")
if len(gv.outGroups) != 1 {
t.Fatalf("want 1 out-group, got %d", len(gv.outGroups))
}
if gv.outGroups[0].tp != "(no type)" {
t.Errorf("empty type should fall back to '(no type)', got %q", gv.outGroups[0].tp)
}
}
func TestBuildGroupedView_SearchFiltersByName(t *testing.T) {
links := []displayLink{
{info: makeLinkInfo("root", "alpha", "x", "t"), isOut: true},
{info: makeLinkInfo("root", "beta", "y", "t"), isOut: true},
{info: makeLinkInfo("root", "gamma", "z", "t"), isOut: true},
}
gv := buildGroupedView(links, "bet")
if len(gv.outGroups) != 1 {
t.Fatalf("search filter should keep 1 group, got %d", len(gv.outGroups))
}
if len(gv.outGroups[0].links) != 1 || gv.outGroups[0].links[0].label() != "beta" {
t.Errorf("only 'beta' should survive filter, got %v", gv.outGroups[0].links)
}
}
func TestBuildGroupedView_SearchFiltersByTarget(t *testing.T) {
links := []displayLink{
{info: makeLinkInfo("root", "link1", "hub/special-node", "t"), isOut: true},
{info: makeLinkInfo("root", "link2", "hub/other-node", "t"), isOut: true},
}
gv := buildGroupedView(links, "special")
if len(gv.outGroups[0].links) != 1 {
t.Errorf("filter by target should keep 1 link, got %d", len(gv.outGroups[0].links))
}
}
func TestBuildGroupedView_SearchRemovesEmptyGroups(t *testing.T) {
links := []displayLink{
{info: makeLinkInfo("root", "alpha", "x", "typeA"), isOut: true},
{info: makeLinkInfo("root", "beta", "y", "typeB"), isOut: true},
}
gv := buildGroupedView(links, "alpha")
if len(gv.outGroups) != 1 {
t.Errorf("group with no matching links should be removed, got %d groups", len(gv.outGroups))
}
if gv.outGroups[0].tp != "typeA" {
t.Errorf("surviving group should be typeA, got %q", gv.outGroups[0].tp)
}
}
func TestBuildGroupedView_SearchCaseInsensitive(t *testing.T) {
links := []displayLink{
{info: makeLinkInfo("root", "MyLink", "x", "t"), isOut: true},
}
gv := buildGroupedView(links, "mylink")
if len(gv.outGroups[0].links) != 1 {
t.Error("search should be case-insensitive")
}
}
// ── buildPanelFlat ────────────────────────────────────────────────────────────
func TestBuildPanelFlat_Empty(t *testing.T) {
flat := buildPanelFlat(nil)
if len(flat) != 0 {
t.Errorf("empty groups should produce empty flat, got %d", len(flat))
}
}
func TestBuildPanelFlat_OneGroupThreeLinks(t *testing.T) {
gv := buildGroupedView(threeLinks(), "")
flat := gv.outFlat
// Expected: [typeGroup, link0, link1, link2]
if len(flat) != 4 {
t.Fatalf("want 4 flat items, got %d", len(flat))
}
if flat[0].kind != flatTypeGroup {
t.Error("flat[0] should be flatTypeGroup")
}
if flat[1].kind != flatLink || flat[2].kind != flatLink || flat[3].kind != flatLink {
t.Error("flat[1..3] should be flatLink")
}
}
func TestBuildPanelFlat_CollapsedGroupHidesLinks(t *testing.T) {
gv := buildGroupedView(threeLinks(), "")
gv.outGroups[0].collapsed = true
gv.rebuildFlat()
for _, item := range gv.outFlat {
if item.kind == flatLink {
t.Error("collapsed group should have no flatLink items")
}
}
hasTypeGroup := false
for _, item := range gv.outFlat {
if item.kind == flatTypeGroup {
hasTypeGroup = true
}
}
if !hasTypeGroup {
t.Error("collapsed group should still show type group header")
}
}
func TestBuildPanelFlat_TwoDirectionsSeparate(t *testing.T) {
gv := buildGroupedView(mixedLinks(), "")
// outFlat: 2 groups → 2 headers + 2 links = 4 items
if len(gv.outFlat) != 4 {
t.Errorf("outFlat: want 4 (2 headers + 2 links), got %d", len(gv.outFlat))
}
// inFlat: 1 group → 1 header + 1 link = 2 items
if len(gv.inFlat) != 2 {
t.Errorf("inFlat: want 2 (1 header + 1 link), got %d", len(gv.inFlat))
}
}
// ── nextSelectable ────────────────────────────────────────────────────────────
func TestNextSelectable_Forward(t *testing.T) {
flat := []flatItem{
{kind: flatTypeGroup},
{kind: flatLink},
{kind: flatLink},
}
got := nextSelectable(flat, 0, +1)
if got != 1 {
t.Errorf("from 0 going +1: want 1, got %d", got)
}
}
func TestNextSelectable_Backward(t *testing.T) {
flat := []flatItem{
{kind: flatTypeGroup},
{kind: flatLink},
{kind: flatLink},
}
got := nextSelectable(flat, 1, -1)
if got != 0 {
t.Errorf("from 1 going -1: want 0, got %d", got)
}
}
func TestNextSelectable_WrapsWithinTheList(t *testing.T) {
flat := []flatItem{
{kind: flatTypeGroup},
{kind: flatLink},
}
if got := nextSelectable(flat, 1, +1); got != 0 {
t.Errorf("past the last row should wrap to the first, got %d", got)
}
if got := nextSelectable(flat, 0, -1); got != 1 {
t.Errorf("before the first row should wrap to the last, got %d", got)
}
}
// ── nextSelectable ────────────────────────────────────────────────────────────
func TestNextSelectable_EmptyList(t *testing.T) {
if got := nextSelectable(nil, 0, +1); got != 0 {
t.Errorf("want 0, got %d", got)
}
}
// ── linkForItemIn ─────────────────────────────────────────────────────────────
func TestLinkForItemIn_ReturnsLink(t *testing.T) {
gv := buildGroupedView(threeLinks(), "")
item := flatItem{kind: flatLink, groupIdx: 0, linkIdx: 0}
dl, ok := linkForItemIn(gv.outGroups, item)
if !ok {
t.Fatal("expected link to be found")
}
if dl.label() == "" {
t.Error("found link should have a label")
}
}
func TestLinkForItemIn_TypeGroupReturnsFalse(t *testing.T) {
gv := buildGroupedView(threeLinks(), "")
item := flatItem{kind: flatTypeGroup, groupIdx: 0}
_, ok := linkForItemIn(gv.outGroups, item)
if ok {
t.Error("linkForItemIn on flatTypeGroup should return false")
}
}
// ── displayLink ───────────────────────────────────────────────────────────────
func TestDisplayLink_TargetOut(t *testing.T) {
dl := displayLink{info: makeLinkInfo("root", "l", "child", ""), isOut: true}
if dl.target() != "child" {
t.Errorf("out link target: want child, got %q", dl.target())
}
}
func TestDisplayLink_TargetIn(t *testing.T) {
dl := displayLink{info: makeLinkInfo("parent", "l", "root", ""), isOut: false}
if dl.target() != "parent" {
t.Errorf("in link target: want parent, got %q", dl.target())
}
}
func TestDisplayLink_Label(t *testing.T) {
dl := displayLink{info: makeLinkInfo("root", "my_link", "child", ""), isOut: true}
if dl.label() != "my_link" {
t.Errorf("label: want my_link, got %q", dl.label())
}
}
// ── renderBodyKV ──────────────────────────────────────────────────────────────
func TestRenderBodyKV_EmptyBody(t *testing.T) {
body := easyjson.NewJSONObject()
out := renderBodyKV(body.GetPtr(), 80)
if !strings.Contains(out, "empty") {
t.Errorf("empty body should say 'empty', got: %q", out)
}
}
func TestRenderBodyKV_NilBody(t *testing.T) {
out := renderBodyKV(nil, 80)
if !strings.Contains(out, "empty") {
t.Errorf("nil body should say 'empty', got: %q", out)
}
}
func TestRenderBodyKV_StringField(t *testing.T) {
body := easyjson.NewJSONObjectWithKeyValue("name", easyjson.NewJSON("Router1"))
out := renderBodyKV(body.GetPtr(), 80)
if !strings.Contains(out, "name") {
t.Errorf("should contain key 'name', got: %q", out)
}
if !strings.Contains(out, "Router1") {
t.Errorf("should contain value 'Router1', got: %q", out)
}
}
func TestRenderBodyKV_MultipleFields(t *testing.T) {
body := easyjson.NewJSONObject()
body.SetByPath("alpha", easyjson.NewJSON("a"))
body.SetByPath("beta", easyjson.NewJSON("b"))
body.SetByPath("gamma", easyjson.NewJSON("c"))
out := renderBodyKV(body.GetPtr(), 80)
if !strings.Contains(out, "alpha") || !strings.Contains(out, "beta") || !strings.Contains(out, "gamma") {
t.Errorf("all keys should appear, got: %q", out)
}
}
// ── renderJSONValue ───────────────────────────────────────────────────────────
func TestRenderJSONValue_String(t *testing.T) {
out := renderJSONValue("hello", 80)
if !strings.Contains(out, "hello") {
t.Errorf("string value: want 'hello' in output, got %q", out)
}
}
func TestRenderJSONValue_StringTruncated(t *testing.T) {
long := strings.Repeat("x", 100)
out := renderJSONValue(long, 10)
if len(out) > 100 {
t.Errorf("long string should be truncated, got length %d", len(out))
}
if !strings.Contains(out, "…") {
t.Errorf("truncated string should end with ellipsis, got %q", out)
}
}
func TestRenderJSONValue_Number(t *testing.T) {
out := renderJSONValue(float64(42), 80)
if !strings.Contains(out, "42") {
t.Errorf("number value: want '42' in output, got %q", out)
}
}
func TestRenderJSONValue_BoolTrue(t *testing.T) {
out := renderJSONValue(true, 80)
if !strings.Contains(out, "true") {
t.Errorf("bool true: want 'true' in output, got %q", out)
}
}
func TestRenderJSONValue_BoolFalse(t *testing.T) {
out := renderJSONValue(false, 80)
if !strings.Contains(out, "false") {
t.Errorf("bool false: want 'false' in output, got %q", out)
}
}
func TestRenderJSONValue_Nil(t *testing.T) {
out := renderJSONValue(nil, 80)
if !strings.Contains(out, "null") {
t.Errorf("nil value: want 'null' in output, got %q", out)
}
}
func TestRenderJSONValue_StringSlice(t *testing.T) {
out := renderJSONValue([]interface{}{"a", "b", "c"}, 80)
if !strings.Contains(out, "a") || !strings.Contains(out, "b") {
t.Errorf("string slice: want items in output, got %q", out)
}
}
func TestRenderJSONValue_EmptySlice(t *testing.T) {
out := renderJSONValue([]interface{}{}, 80)
if !strings.Contains(out, "[]") {
t.Errorf("empty slice: want '[]', got %q", out)
}
}
func TestRenderJSONValue_Object(t *testing.T) {
v := map[string]interface{}{"key": "val"}
out := renderJSONValue(v, 80)
b, _ := json.Marshal(v)
if !strings.Contains(out, "key") {
t.Errorf("object value: want JSON with 'key', got %q (json=%s)", out, b)
}
}
// ── highlightMatches ──────────────────────────────────────────────────────────
func TestHighlightMatches_Empty(t *testing.T) {
out := highlightMatches("hello world", "")
if out != "hello world" {
t.Errorf("empty query should return unchanged text, got %q", out)
}
}
func TestHighlightMatches_SingleMatch(t *testing.T) {
out := highlightMatches("hello world", "world")
if !strings.Contains(out, "world") {
t.Errorf("match should still contain 'world', got %q", out)
}
if len(out) <= len("hello world") {
t.Errorf("output with highlight should be longer than input (ANSI codes)")
}
}
func TestHighlightMatches_CaseInsensitive(t *testing.T) {
out := highlightMatches("Hello World", "hello")
if len(out) <= len("Hello World") {
t.Errorf("case-insensitive match should add highlight codes, out len=%d", len(out))
}
}
func TestHighlightMatches_NoMatch(t *testing.T) {
out := highlightMatches("hello world", "xyz")
if out != "hello world" {
t.Errorf("no match should return unchanged, got %q", out)
}
}
func TestHighlightMatches_MultipleMatches(t *testing.T) {
out := highlightMatches("aaa bbb aaa", "aaa")
if len(out) <= len("aaa bbb aaa")+10 {
t.Logf("may have only one match highlighted, out: %q", out)
}
}
// ── stripDomain ───────────────────────────────────────────────────────────────
func TestStripDomain(t *testing.T) {
tests := []struct{ in, want string }{
{"hub/network/router", "network/router"},
{"router", "router"},
{"a/b/c", "b/c"},
{"", ""},
}
for _, c := range tests {
if got := stripDomain(c.in); got != c.want {
t.Errorf("stripDomain(%q) = %q, want %q", c.in, got, c.want)
}
}
}
// ── Navigation: h/l panel switching ──────────────────────────────────────────
// h and l now move one column at a time across three, so a single press from
// the centre reaches a side panel and a second press clamps there.
func TestNav_lFocusesOutgoing(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", mixedLinks(), &fvi)
m.focus = panelCenter
m = update(m, key("l"))
if m.focus != panelOut {
t.Errorf("l should switch focus to panelOut, got %v", m.focus)
}
}
func TestNav_hFocusesIncoming(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", mixedLinks(), &fvi)
m.focus = panelCenter
m = update(m, key("h"))
if m.focus != panelIn {
t.Errorf("h should switch focus to panelIn, got %v", m.focus)
}
}
func TestNav_ArrowLeftFocusesIncoming(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", mixedLinks(), &fvi)
m.focus = panelCenter
m = update(m, tea.KeyMsg{Type: tea.KeyLeft})
if m.focus != panelIn {
t.Errorf("← should focus incoming panel, got %v", m.focus)
}
}
func TestNav_ArrowRightFocusesOutgoing(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", mixedLinks(), &fvi)
m.focus = panelCenter
m = update(m, tea.KeyMsg{Type: tea.KeyRight})
if m.focus != panelOut {
t.Errorf("→ should focus outgoing panel, got %v", m.focus)
}
}
// ── Navigation: j/k ──────────────────────────────────────────────────────────
func TestNav_jMovesToFirstLink(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", threeLinks(), &fvi)
m.focus = panelOut
// Row 0 is the group header; j reaches the first link under it.
m = update(m, key("j"))
dl, isLink := m.cursorLink()
if !isLink {
t.Fatal("j from the group header should reach a link")
}
if dl.label() != "l1" {
t.Errorf("first link should be l1 (alpha sort), got %q", dl.label())
}
}
func TestNav_kWrapsToLast(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", threeLinks(), &fvi)
m.focus = panelOut
m = update(m, key("k"))
if m.rCursor != len(m.grouped.outFlat)-1 {
t.Errorf("k from the first row should wrap to the last, got %d", m.rCursor)
}
}
func TestNav_jjjWrapsAround(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", threeLinks(), &fvi)
m.focus = panelOut
total := len(m.grouped.outFlat)
start := m.rCursor
for i := 0; i < total; i++ {
m = update(m, key("j"))
}
if m.rCursor != start {
t.Errorf("after %d j presses should be back at start=%d, got %d", total, start, m.rCursor)
}
}
func TestNav_ArrowDown(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", threeLinks(), &fvi)
m.focus = panelOut
before := m.rCursor
m = update(m, tea.KeyMsg{Type: tea.KeyDown})
if m.rCursor == before {
t.Error("↓ should move cursor")
}
}
func TestNav_EmptyFlat_NoPanic(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", nil, &fvi)
m = update(m, key("j"))
m = update(m, key("k"))
if m.rCursor != 0 {
t.Errorf("with no links the cursor stays at 0, got %d", m.rCursor)
}
}
func TestNav_jIncomingPanel(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", mixedLinks(), &fvi)
m.focus = panelIn
before := m.lCursor
m = update(m, key("j"))
if m.lCursor == before && len(m.grouped.inFlat) > 1 {
t.Error("j in incoming panel should move lCursor")
}
// rCursor should not change
if m.rCursor != 0 {
t.Errorf("rCursor should not change when navigating incoming panel, got %d", m.rCursor)
}
}
// ── Navigation: Enter ─────────────────────────────────────────────────────────
func TestNav_EnterOnLinkNavigates(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", threeLinks(), &fvi)
m.focus = panelOut
// Move to first flatLink
m = update(m, key("j"))
if _, isLink := m.cursorLink(); !isLink {
t.Skip("cursor not on link, skipping")
}
m = update(m, keyEnter())
if len(m.history) != 1 || m.history[0] != "root" {
t.Errorf("Enter on link should push root to history, got %v", m.history)
}
if !m.loading {
t.Error("Enter on link should set loading=true")
}
}
func TestNav_EnterOnTypeGroupCollapses(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", threeLinks(), &fvi)
m.focus = panelOut
if m.grouped.outFlat[m.rCursor].kind != flatTypeGroup {
t.Fatal("the first row should be a group header")
}
before := len(m.grouped.outFlat)
m = update(m, keyEnter())
if len(m.grouped.outFlat) >= before {
t.Errorf("flat should shrink after collapse: before=%d after=%d", before, len(m.grouped.outFlat))
}
}
func TestNav_EnterWhileLoading(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", threeLinks(), &fvi)
m.loading = true
m = update(m, keyEnter())
if !m.loading {
t.Error("Enter while loading should not stop loading")
}
if len(m.history) != 0 {
t.Error("Enter while loading should not push history")
}
}
// ── Navigation: Tab ───────────────────────────────────────────────────────────
func TestNav_TabOnTypeGroupCollapses(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", threeLinks(), &fvi)
m.focus = panelOut
if m.grouped.outFlat[m.rCursor].kind != flatTypeGroup {
t.Skip("cursor not on typeGroup")
}
initialLen := len(m.grouped.outFlat)
m = update(m, keyTab())
if len(m.grouped.outFlat) >= initialLen {
t.Errorf("Tab on typeGroup should collapse: len before=%d after=%d",
initialLen, len(m.grouped.outFlat))
}
}
func TestNav_TabToggleExpandCollapse(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", threeLinks(), &fvi)
m.focus = panelOut
initialLen := len(m.grouped.outFlat)
m = update(m, keyTab()) // collapse
collapsed := len(m.grouped.outFlat)
m = update(m, keyTab()) // expand
expanded := len(m.grouped.outFlat)
if collapsed >= initialLen {
t.Errorf("first Tab should collapse: initial=%d collapsed=%d", initialLen, collapsed)
}
if expanded != initialLen {
t.Errorf("second Tab should restore: want %d, got %d", initialLen, expanded)
}
}
func TestNav_TabOnLinkTogglesParentGroup(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", threeLinks(), &fvi)
m.focus = panelOut
// Move to first flatLink
m = update(m, key("j"))
if _, isLink := m.cursorLink(); !isLink {
t.Skip("cursor not on link")
}
initialLen := len(m.grouped.outFlat)
m = update(m, keyTab())
if len(m.grouped.outFlat) >= initialLen {
t.Errorf("Tab on link should collapse parent group: initial=%d after=%d",
initialLen, len(m.grouped.outFlat))
}
}
// ── Navigation: back ─────────────────────────────────────────────────────────
func TestNav_bGoesBack(t *testing.T) {
fvi := makeVertexInfo("child", nil, nil)
m := makeModel("child", threeLinks(), &fvi)
m.history = []string{"root"}
m = update(m, key("b"))
if len(m.history) != 0 {
t.Errorf("b should pop history, got %v", m.history)
}
if !m.loading {
t.Error("b should trigger loading")
}
}
func TestNav_BackspaceGoesBack(t *testing.T) {
fvi := makeVertexInfo("child", nil, nil)
m := makeModel("child", threeLinks(), &fvi)
m.history = []string{"root"}
m = update(m, keyBackspace())
if len(m.history) != 0 {
t.Errorf("backspace should pop history, got %v", m.history)
}
}
func TestNav_bNoHistory(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", threeLinks(), &fvi)
m = update(m, key("b"))
if m.loading {
t.Error("b with empty history should not trigger loading")
}
}
// ── rawBody toggle ────────────────────────────────────────────────────────────
func TestNav_vTogglesRawBody(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", nil, &fvi)
if m.rawBody {
t.Fatal("rawBody should start false")
}
m = update(m, key("v"))
if !m.rawBody {
t.Error("v should toggle rawBody to true")
}
m = update(m, key("v"))
if m.rawBody {
t.Error("second v should toggle rawBody back to false")
}
}
// ── Refresh ───────────────────────────────────────────────────────────────────
func TestNav_rReloads(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", threeLinks(), &fvi)
initialGen := m.loadGen
m = update(m, key("r"))
if !m.loading {
t.Error("r should trigger loading")
}
if m.loadGen != initialGen+1 {
t.Errorf("r should increment loadGen: want %d, got %d", initialGen+1, m.loadGen)
}
}
// ── Search ────────────────────────────────────────────────────────────────────
func TestSearch_fEntersSearchMode(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", nil, &fvi)
m = update(m, key("f"))
if !m.searchMode {
t.Error("f should enter search mode")
}
}
func TestSearch_EscClearsQuery(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", mixedLinks(), &fvi)
m.searchQuery = "something"
m.grouped = buildGroupedView(m.links, "something")
m = update(m, keyEsc())
if m.searchQuery != "" {
t.Errorf("Esc should clear searchQuery, got %q", m.searchQuery)
}
}
func TestSearch_FilterRebuildsGroups(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", mixedLinks(), &fvi)
m.searchMode = true
for _, ch := range "child_a" {
m = update(m, key(string(ch)))
}
if m.searchQuery == "" {
t.Skip("searchQuery not updated yet")
}
total := 0
for _, g := range m.grouped.outGroups {
total += len(g.links)
}
if total != 1 {
t.Errorf("search for 'child_a' should keep 1 out-link, got %d", total)
}
}
// ── Query mode ────────────────────────────────────────────────────────────────
func TestQuery_SlashEnters(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", nil, &fvi)
m = update(m, key("/"))
if !m.queryMode {
t.Error("/ should enter query mode")
}
}
func TestQuery_EscExits(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", nil, &fvi)
m.queryMode = true
m = update(m, keyEsc())
if m.queryMode {
t.Error("Esc should exit query mode")
}
}
func TestQuery_EmptyEnterExits(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", nil, &fvi)
m.queryMode = true
m = update(m, keyEnter())
if m.queryMode {
t.Error("Enter with empty query should exit query mode without sending command")
}
}
// ── Message handling ──────────────────────────────────────────────────────────
func TestMsg_VertexInfoMsg_UpdatesState(t *testing.T) {
m := newTuiModel("root")
m.width = 120
m.height = 40
m.ready = true
m.bodyVP = viewport.New(40, 30)
m.loading = true
fvi := makeVertexInfo("child",
[]linkId{{from: "child", name: "l1"}, {from: "child", name: "l2"}},
nil,
)
m = update(m, vertexInfoMsg{id: "child", gen: 0, fvi: &fvi})
if m.currentID != "child" {
t.Errorf("currentID should update to child, got %q", m.currentID)
}
if m.linksTotal != 2 {
t.Errorf("linksTotal: want 2, got %d", m.linksTotal)
}
}
func TestMsg_VertexInfoMsg_Stale(t *testing.T) {
m := newTuiModel("root")
m.loadGen = 5
fvi := makeVertexInfo("other", nil, nil)
m = update(m, vertexInfoMsg{id: "other", gen: 2, fvi: &fvi})
if m.currentID != "root" {
t.Error("stale vertexInfoMsg should not update currentID")
}
}
func TestMsg_VertexInfoMsg_Error(t *testing.T) {
m := newTuiModel("root")
m.loading = true
m = update(m, vertexInfoMsg{id: "root", gen: 0, err: fmt.Errorf("not found")})
if m.loading {
t.Error("error should stop loading")
}
if m.errMsg == "" {
t.Error("errMsg should be set on error")
}
}
func TestMsg_LinksLoadedMsg_BuildsGroups(t *testing.T) {
fvi := makeVertexInfo("root", nil, nil)
m := makeModel("root", nil, &fvi)
m.loading = true
m.loadGen = 1
m.ready = true
m.bodyVP = viewport.New(40, 30)
links := []displayLink{
{info: makeLinkInfo("root", "l1", "c1", "contains"), isOut: true},
{info: makeLinkInfo("root", "l2", "c2", "depends"), isOut: true},
}
m = update(m, linksLoadedMsg{id: "root", gen: 1, links: links})
if m.loading {
t.Error("should not be loading after linksLoadedMsg")
}
if len(m.grouped.outGroups) != 2 {
t.Errorf("should have 2 out-groups (contains, depends), got %d", len(m.grouped.outGroups))
}
if m.rCursor != 0 {
t.Errorf("rCursor should be 0 after load, got %d", m.rCursor)
}
}
func TestMsg_LinksLoadedMsg_Stale(t *testing.T) {