package tui import ( "context" "reflect" "slices" "testing" tea "github.com/charmbracelet/bubbletea" "github.com/coder/websocket" ) func TestHomeShortcutsDoNotFireOnPipelineDetails(t *testing.T) { for _, key := range []string{"n", "t", "q"} { m := panelModel(t) // Non-nil connection ensures the old global dispatch would open/send, // rather than merely logging a disconnected-state warning. m.conn = &websocket.Conn{} updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(key)}) after := updated.(Model) if cmd != nil || after.nextID != m.nextID || !reflect.DeepEqual(after.lines, m.lines) { t.Fatalf("home shortcut %q fired on pipeline details", key) } } } func TestHomeShortcutStillCreatesPipeline(t *testing.T) { t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir()) m := New(context.Background(), "") m.conn = &websocket.Conn{} updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'n'}}) m = updated.(Model) if cmd == nil { t.Fatal("home n should launch authoring without a form") } } func TestCommandPaletteIsScopedToPage(t *testing.T) { home := New(context.Background(), "") detail := panelModel(t) run := runInspectionModel() for _, test := range []struct { model Model allowed, blocked []string }{ {home, []string{":new", ":ping", ":logs", ":runs"}, []string{":edit", ":versions", ":submit", ":run", ":home"}}, {detail, []string{":edit", ":versions", ":run", ":runs", ":home"}, []string{":new", ":ping", ":submit"}}, {run, []string{":logs", ":runs", ":refresh", ":home"}, []string{":new", ":ping", ":edit", ":versions", ":run", ":submit"}}, } { for _, command := range test.allowed { if !slices.Contains(test.model.pageCommands(), command) { t.Fatal("missing page command", command) } } for _, command := range test.blocked { if slices.Contains(test.model.pageCommands(), command) { t.Fatal("unavailable page command shown", command) } } } detail.commandInput = newInput() detail.commandInput.SetValue(":new") if slices.Contains(detail.matches(), ":new") { t.Fatal("fuzzy filtering resurrected a hidden command") } detail = press(detail, ":") detail = press(detail, "new") detail = press(detail, "enter") if detail.pendingDraft != nil { t.Fatal("hidden palette command opened form") } } func TestCtrlCRemainsGlobal(t *testing.T) { m := panelModel(t) _, cmd := m.Update(tea.KeyMsg{Type: tea.KeyCtrlC}) if cmd == nil { t.Fatal("global quit disabled") } if _, ok := cmd().(tea.QuitMsg); !ok { t.Fatal("Ctrl+C did not quit") } } func TestQuitUsesCommandInsteadOfQ(t *testing.T) { for _, m := range []Model{New(context.Background(), ""), panelModel(t), runInspectionModel()} { _, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'q'}}) if cmd != nil { t.Fatal("q must not quit") } if !slices.Contains(m.pageCommands(), ":quit") { t.Fatal("quit command missing") } m = press(m, ":") m = press(m, "quit") _, cmd = m.Update(tea.KeyMsg{Type: tea.KeyEnter}) if cmd == nil { t.Fatal("quit command did not exit") } if _, ok := cmd().(tea.QuitMsg); !ok { t.Fatal("quit command did not return QuitMsg") } } }