cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

main
012 / internal / ui / help_test.go
help_test.go 3.5K
1package ui23import (4 "strings"5 "testing"67 tea "charm.land/bubbletea/v2"89 "src.rest/cat/012/internal/ui/shortcuts"10 "src.rest/cat/012/internal/ui/theme"11)1213func TestShortcutsListEveryBoundCommand(t *testing.T) {14 rows := helpRows(false)15 for key, id := range keymap {16 c := commands[id]17 if c == nil {18 continue19 }20 found := false21 for _, r := range rows {22 if r.action == c.title && strings.Contains(strings.Join(r.keys, " "), theme.KeyLabel(key)) {23 found = true24 }25 }26 if !found {27 t.Errorf("%s (%s) missing from the shortcuts", c.title, theme.KeyLabel(key))28 }29 }30}3132func TestShortcutsOverlay(t *testing.T) {33 m := newModel()34 press(t, m, "<f1>")35 s, ok := m.overlay.(*shortcuts.View)36 if !ok || !strings.HasSuffix(line(m, menuLine), "HELP") {37 t.Fatalf("F1 opened %T", m.overlay)38 }39 scr := screen(m)40 for _, want := range []string{"Keyboard shortcuts", "Moving around", "Ctrl+G F5"} {41 if !strings.Contains(scr, want) {42 t.Errorf("shortcuts missing %q:\n%s", want, scr)43 }44 }45 // Taller than 20 lines, so it scrolls, and says where it is.46 if !strings.Contains(scr, "1-") {47 t.Errorf("no scroll position in footer:\n%s", scr)48 }49 press(t, m, "<down>", "<down>")50 if s.Top() != 2 {51 t.Errorf("down scrolled to %d", s.Top())52 }53 send(m, tea.MouseWheelMsg{X: 40, Y: 10, Button: tea.MouseWheelUp})54 if m.View(); s.Top() != 0 {55 t.Errorf("wheel up scrolled to %d", s.Top())56 }57 press(t, m, "<end>")58 bottom := s.Top()59 press(t, m, "<down>")60 if bottom == 0 || s.Top() != bottom {61 t.Errorf("End scrolled to %d, then Down to %d", bottom, s.Top())62 }63 press(t, m, "<esc>")64 if m.overlay != nil || m.mode != modeReady {65 t.Error("Esc did not close the shortcuts")66 }67 press(t, m, "<ctrl+/>")68 if _, ok := m.overlay.(*shortcuts.View); !ok {69 t.Error("Ctrl+/ did not open the shortcuts")70 }71 leftClick(m, 0, m.height-1)72 if m.overlay != nil {73 t.Error("clicking outside did not close the shortcuts")74 }75}7677func TestShortcutsTwoColumnsWhenWide(t *testing.T) {78 m := newModel()79 m.Update(tea.WindowSizeMsg{Width: 200, Height: 50})80 press(t, m, "<f1>")81 lines, w := m.overlay.(*shortcuts.View).Lines()82 if w < 100 || len(lines) > len(helpRows(false)) {83 t.Errorf("width %d, %d lines for %d rows", w, len(lines), len(helpRows(false)))84 }85}8687func TestFunctionListInsertsFunction(t *testing.T) {88 m := newModel()89 m.runCommand("help.functions")90 press(t, m, "iferr")91 if got := selected(t, m); !strings.HasPrefix(got, "IFERROR(") {92 t.Fatalf("selected %q", got)93 }94 if !strings.Contains(line(m, m.height-1), "Enter insert") {95 t.Errorf("status %q", line(m, m.height-1))96 }97 press(t, m, "<enter>")98 if m.mode != modeEnter || m.line.Text() != "=IFERROR(" {99 t.Errorf("mode %v buf %q", m.mode, m.line.Text())100 }101}102103// Overlays must survive screens smaller than they are.104func TestOverlaysOnTinyScreens(t *testing.T) {105 for _, size := range [][2]int{{60, 16}, {20, 6}, {3, 2}} {106 for _, keys := range [][]string{{"<alt+o>", "<right>"}, {"<ctrl+k>", "s"}, {"<f1>", "<end>"}, {"1", "<enter>", "<ctrl+q>"}, {"=su"}, {"<alt+d>", "n"}} {107 m := newModel()108 m.Update(tea.WindowSizeMsg{Width: size[0], Height: size[1]})109 press(t, m, keys...)110 m.View()111 rightClick(m, size[0]-1, size[1]-1)112 m.View()113 }114 }115}116117func TestAbout(t *testing.T) {118 m := newModel()119 m.runCommand("help.about")120 if l := line(m, contextLine); !strings.HasPrefix(l, "012 devel") || !strings.Contains(l, "Esc Close") {121 t.Errorf("about %q", l)122 }123 press(t, m, "<esc>")124 if m.overlay != nil || line(m, contextLine) != "" {125 t.Errorf("Esc left %T %q", m.overlay, line(m, contextLine))126 }127}