Files
dibs/internal/tui/prompt_test.go
OverlordandClaude Bot 15a34e1f84 Add session mode: signing in and adding to the cart.
With cart_mode = "session", dibs signs in to the shop's account and puts
an item in the cart when it fires. The account's cookies are kept apart
from the watcher's and stored encrypted, with the key in the OS keyring.
Without a keyring, they're stored in a file only you can read.

An alert now goes out right away, without a cart link, and a second one
follows once the item is in the cart. If adding fails, the second alert
carries the cart link instead.

Amazon only accepts additions its page scripts have seen, so plugins can
now press a button in headless Chrome with ctx:click.

The TUI asks for one-time codes and shows the plugin's warning about
account locks in a modal. 'dibs login' signs in from a terminal, or
with --browser in a Chrome window, and 'dibs logout' deletes the cookies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 17:26:26 +02:00

79 lines
2.4 KiB
Go

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") }
}