experience_test.go (1732B)
1 package tui 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/charmbracelet/x/ansi" 8 ) 9 10 // Every "|" of the experience git graph sits in the same terminal column as 11 // the "*" commit markers, in the fully rendered view (borders, padding, 12 // selection marker and all). 13 func TestExperienceGraphColumnsLineUp(t *testing.T) { 14 for _, w := range []int{80, 120} { 15 for sel := 0; sel < 3; sel++ { 16 m := testModel(t, w, 60) 17 m.activateTab(tabExperience) 18 m.experienceIndex = sel 19 m.syncViewport() 20 lines := strings.Split(ansi.Strip(m.View()), "\n") 21 22 starCol, stars, pipes := -1, 0, 0 23 for _, l := range lines { 24 // the graph lives inside the panel: "│ " + content 25 i := strings.Index(l, "│ ") 26 if i < 0 { 27 continue 28 } 29 content := l[i+len("│ "):] 30 col := ansi.StringWidth(l[:i+len("│ ")]) 31 cells := []rune(content) 32 if len(cells) < 3 { 33 continue 34 } 35 g := string(cells[2]) 36 switch { 37 case g == "*" && strings.TrimSpace(string(cells[:2])) == "" || g == "*" && string(cells[:2]) == "> ": 38 if starCol < 0 { 39 starCol = col + 2 40 } 41 if col+2 != starCol { 42 t.Fatalf("w=%d sel=%d: * at col %d, want %d:\n%s", w, sel, col+2, starCol, l) 43 } 44 stars++ 45 case g == "|": 46 if col+2 != starCol { 47 t.Fatalf("w=%d sel=%d: | at col %d, want %d:\n%s", w, sel, col+2, starCol, l) 48 } 49 pipes++ 50 } 51 // no stray connector anywhere else on the line 52 for j, r := range cells[:min(len(cells), 6)] { 53 if r == '|' && j != 2 { 54 t.Fatalf("w=%d sel=%d: stray | at offset %d:\n%s", w, sel, j, l) 55 } 56 } 57 } 58 if stars != len(m.experience) || pipes == 0 { 59 t.Fatalf("w=%d sel=%d: stars=%d pipes=%d", w, sel, stars, pipes) 60 } 61 } 62 } 63 }