package tui import ( "log/slog" "strings" "testing" tea "charm.land/bubbletea/v2" "git.security-command.org/Overlord/dibs/internal/watch" ) type idle struct{} func (idle) Statuses() []watch.Status { return nil } func (idle) Pause(string, bool) bool { return false } func (idle) PauseAll(bool) {} func (idle) CheckNow(string) bool { return false } func (idle) Reload() ([]string, error) { return nil, nil } func (idle) Solve(string) error { return nil } func newTestModel() *Model { m := New(idle{}, "/tmp/dibs.toml", NewLogHandler(slog.LevelInfo)) m.Update(tea.WindowSizeMsg{Width: 100, Height: 30}) return m } func ask(m *Model, msg promptMsg) <-chan promptReply { reply := make(chan promptReply, 1) msg.reply = reply m.Update(msg) return reply } func typeText(m *Model, s string) { for _, r := range s { m.Update(tea.KeyPressMsg{Code: r, Text: string(r)}) } } func TestModalPrompt(t *testing.T) { m := newTestModel() reply := ask(m, promptMsg{id: 1, title: "amazon ยท www.amazon.de", label: "One-time code", secret: true}) typeText(m, "123456") view := m.View().Content if !strings.Contains(view, "One-time code") { t.Error("the modal is not shown") } if strings.Contains(view, "123456") { t.Error("the secret shows as it is typed") } m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) if r := <-reply; !r.ok || r.text != "123456" { t.Errorf("reply %+v", r) } if m.modal != nil { t.Error("the modal is still open") } } func TestModalCancelAndConfirm(t *testing.T) { m := newTestModel() // Keys go to the modal, so q is typed instead of quitting. reply := ask(m, promptMsg{id: 1, title: "t", label: "code"}) typeText(m, "q") if got := m.modal.input.Value(); got != "q" { t.Errorf("typed %q", got) } m.Update(tea.KeyPressMsg{Code: tea.KeyEscape}) if r := <-reply; r.ok { t.Errorf("esc replied %+v", r) } reply = ask(m, promptMsg{id: 2, title: "Session mode for amazon", label: "Automated logins can lock the account.", confirm: true}) m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) if r := <-reply; !r.ok { t.Error("enter did not accept the warning") } // A question that timed out closes its modal, but no newer one. ask(m, promptMsg{id: 3, title: "t", label: "code"}) m.Update(closePromptMsg{id: 2}) if m.modal == nil { t.Error("an old timeout closed the modal") } m.Update(closePromptMsg{id: 3}) if m.modal != nil { t.Error("the timed-out modal is still open") } }