package ui import ( "strings" "testing" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/x/ansi" "ghosttyconf/internal/config" "ghosttyconf/internal/schema" ) func newTestApp(t *testing.T, content string) *App { t.Helper() sch, err := schema.Embedded() if err != nil { t.Fatal(err) } f := &config.File{Path: "/tmp/ghosttyconf-test/config.ghostty", Lines: config.Parse([]byte(content))} a := New(sch, nil, f) a.Update(tea.WindowSizeMsg{Width: 160, Height: 45}) return a } func press(a *App, keys ...string) { for _, k := range keys { var msg tea.KeyMsg switch k { case "enter": msg = tea.KeyMsg{Type: tea.KeyEnter} case "esc": msg = tea.KeyMsg{Type: tea.KeyEsc} case "down": msg = tea.KeyMsg{Type: tea.KeyDown} case "up": msg = tea.KeyMsg{Type: tea.KeyUp} case "tab": msg = tea.KeyMsg{Type: tea.KeyTab} case "shift+tab": msg = tea.KeyMsg{Type: tea.KeyShiftTab} case "space": msg = tea.KeyMsg{Type: tea.KeySpace, Runes: []rune{' '}} default: msg = tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(k)} } a.Update(msg) } } // TestEveryEditorRenders opens the editor of every option at several // terminal sizes to catch panics and layout bugs. func TestEveryEditorRenders(t *testing.T) { a := newTestApp(t, "font-family = Hack\nkeybind = ctrl+a=new_tab\npalette = 1=#ff0000\n") sizes := [][2]int{{160, 45}, {100, 30}, {60, 16}, {40, 12}} for _, o := range a.sch.Options { for _, sz := range sizes { a.Update(tea.WindowSizeMsg{Width: sz[0], Height: sz[1]}) a.edit(o) if a.editor == nil && o.Kind != schema.KindBool { t.Fatalf("%s: no editor opened", o.Key) } _ = a.View() press(a, "down", "down") _ = a.View() for i := 0; i < 3 && a.editor != nil; i++ { press(a, "esc") } if a.editor != nil { t.Fatalf("%s: editor did not close on esc", o.Key) } _ = a.View() } } } func TestEditEnumAndUndo(t *testing.T) { a := newTestApp(t, "# keep me\ncursor-style = block\n") press(a, "/") for _, r := range "cursor-style" { press(a, string(r)) } press(a, "enter") if o := a.selected(); o == nil || o.Key != "cursor-style" { t.Fatalf("selected = %v", o) } press(a, "enter", "down", "enter") // block -> bar if v, _ := a.file.Get("cursor-style"); v != "bar" { t.Fatalf("cursor-style = %q", v) } if !a.dirty() { t.Fatal("expected unsaved changes") } press(a, "u") if v, _ := a.file.Get("cursor-style"); v != "block" { t.Fatalf("after undo cursor-style = %q", v) } if a.dirty() { t.Fatal("undo should restore the saved state") } if !strings.HasPrefix(string(a.file.Bytes()), "# keep me\n") { t.Fatal("comment lost") } } func TestToggleStepAndReset(t *testing.T) { a := newTestApp(t, "font-size = 13\n") a.selectCategory(0) // Set in Config press(a, "+", "+") if v, _ := a.file.Get("font-size"); v != "14" { t.Fatalf("font-size = %q", v) } press(a, "r") if a.file.Has("font-size") { t.Fatal("font-size should have been removed") } } func TestKeybindListAdd(t *testing.T) { a := newTestApp(t, "") a.edit(a.sch.Lookup("keybind")) press(a, "a") for _, r := range "ctrl+shift+t" { press(a, string(r)) } press(a, "tab") for _, r := range "new_tab" { press(a, string(r)) } press(a, "enter", "esc") if got := a.file.GetAll("keybind"); len(got) != 1 || got[0] != "ctrl+shift+t=new_tab" { t.Fatalf("keybind = %v", got) } } func TestKeybindManagerChangeDefault(t *testing.T) { a := newTestApp(t, "") o := a.sch.Lookup("keybind") a.edit(o) m, ok := a.editor.(*keybindManager) if !ok { t.Fatalf("editor is %T", a.editor) } // Find the built-in super+d binding and move it to super+shift+d. press(a, "/") for _, r := range "super+d" { press(a, string(r)) } press(a, "enter") b := m.selected() if b == nil || b.Trigger != "super+d" { t.Fatalf("selected = %+v", b) } press(a, "enter") // change press(a, "shift+tab") // trigger field a.Update(tea.KeyMsg{Type: tea.KeyCtrlU}) // clear it for _, r := range "super+shift+d" { press(a, string(r)) } press(a, "enter", "esc") got := strings.Join(a.file.GetAll("keybind"), " | ") if got != "super+d=unbind | super+shift+d="+b.Action { t.Fatalf("keybind = %s", got) } } func TestKeybindManagerUnbindRestore(t *testing.T) { a := newTestApp(t, "") a.edit(a.sch.Lookup("keybind")) m := a.editor.(*keybindManager) b := *m.selected() press(a, "d") if v := m.set.Values(); len(v) != 1 || !strings.HasSuffix(v[0], "=unbind") { t.Fatalf("after unbind: %v", v) } press(a, "r") if v := m.set.Values(); len(v) != 0 { t.Fatalf("after restore: %v", v) } press(a, "esc") if a.file.Has("keybind") { t.Fatalf("no change expected, got %v (binding %s)", a.file.GetAll("keybind"), b.Trigger) } } func TestKeybindRecording(t *testing.T) { a := newTestApp(t, "") a.edit(a.sch.Lookup("keybind")) press(a, "a") a.Update(tea.KeyMsg{Type: tea.KeyCtrlR}) if !a.capturing() { t.Fatal("not recording") } // ctrl+c must be recorded, not quit the program. _, cmd := a.Update(tea.KeyMsg{Type: tea.KeyCtrlC}) if a.dlg != nil || cmd != nil && a.capturing() == false { t.Fatal("ctrl+c was not captured") } press(a, "enter") if a.capturing() { t.Fatal("still recording") } for _, r := range "copy_to_clipboard" { press(a, string(r)) } press(a, "enter", "esc") if got := a.file.GetAll("keybind"); len(got) != 1 || got[0] != "ctrl+c=copy_to_clipboard" { t.Fatalf("keybind = %v", got) } } func TestUnknownKeyIsListed(t *testing.T) { a := newTestApp(t, "bogus-key = 1\n") a.selectCategory(0) if len(a.opts) != 1 || a.opts[0].Key != "bogus-key" { t.Fatalf("opts = %v", a.opts) } if !strings.Contains(a.View(), "bogus-key") { t.Fatal("unknown key not rendered") } } func TestUpdateHintShown(t *testing.T) { a := newTestApp(t, "") a.SetVersion("1.0.0") a.SetUpdate("1.0.1") header := func() string { return strings.SplitN(ansi.Strip(a.View()), "\n", 2)[0] } if h := header(); !strings.Contains(h, "ghosttyconf 1.0.1 available ยท ") || !strings.Contains(h, "options") { t.Errorf("header at 160 columns: %q", h) } // Short of space, the update outlasts the option count. a.Update(tea.WindowSizeMsg{Width: 80, Height: 24}) if h := header(); !strings.Contains(h, "ghosttyconf 1.0.1 available") || strings.Contains(h, "options") { t.Errorf("header at 80 columns: %q", h) } press(a, "?") if !strings.Contains(ansi.Strip(a.View()), "1.0.1 is available, run ghosttyconf --update") { t.Error("help does not mention the update") } a = newTestApp(t, "") if h := header(); strings.Contains(h, "available") || !strings.Contains(h, "options") { t.Errorf("header without an update: %q", h) } }