Files
llmgw/internal/cli/tui_setup_test.go
T
Ilker ManapandClaude Opus 5.5 aa3efcebbe Add a standby control plane with server add, and draw the screens as windows
server add gives another machine this llmgw, a certificate, the
configuration and store address, lets it into the store through Patroni, and
starts it; --vip keeps a floating address on the leader with keepalived,
following a new /leader endpoint. Nodes learn every control plane from the
leader and notice a dead one by HTTP/2 ping instead of after the kernel's
fifteen minutes. The setup, console, node and init screens are drawn full
screen in one frame: title bar, sidebar, bordered panel, key bar, dialogs.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-03 22:29:12 +02:00

139 lines
4.3 KiB
Go

package cli
import (
"strings"
"testing"
tea "github.com/charmbracelet/bubbletea"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi"
"gitea.mynodes.xyz/ilker/llmgw/internal/preflight"
)
func press(m tea.Model, keys ...string) tea.Model {
for _, k := range keys {
var msg tea.KeyMsg
switch k {
case "enter":
msg = tea.KeyMsg{Type: tea.KeyEnter}
case "tab":
msg = tea.KeyMsg{Type: tea.KeyTab}
case "down":
msg = tea.KeyMsg{Type: tea.KeyDown}
case "esc":
msg = tea.KeyMsg{Type: tea.KeyEsc}
case "right":
msg = tea.KeyMsg{Type: tea.KeyRight}
default:
msg = tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(k)}
}
m, _ = m.Update(msg)
}
return m
}
func TestTheSetupAsksForWhatHighAvailabilityNeedsAndRefusesWhatWillNotDo(t *testing.T) {
m := tea.Model(newSetupModel("ctl", []string{"10.0.0.5"}))
m = press(m, "down", "enter")
if s := m.(setupModel); s.page != pAdmin {
t.Fatalf("high availability did not lead to the administrator: page %d", s.page)
}
// No email: refused, with a reason.
m = press(m, "tab", "tab", "enter")
if s := m.(setupModel); s.page != pAdmin || !strings.Contains(s.formErr, "email") {
t.Fatalf("went on without an administrator: %+v", s.formErr)
}
// The refusal is a dialog; a key closes it, and the cursor is back on
// the email.
if !strings.Contains(m.View(), "Not yet") {
t.Error("the refusal was not shown as a dialog")
}
m = press(m, "x", "a@example.com", "tab", "tab", "enter")
s := m.(setupModel)
if s.page != pMachines {
t.Fatalf("page %d: %s", s.page, s.formErr)
}
// This machine's own address is not a database machine.
s.db[0].addr.SetValue("10.0.0.5")
s.db[0].user.SetValue("ubuntu")
s.db[0].auth = authAgent
s.db[1].addr.SetValue("10.0.0.7")
s.db[1].user.SetValue("ubuntu")
s.db[1].auth = authPassword
if err := s.validateMachines(); !strings.Contains(err, "this machine") {
t.Errorf("this machine as a database machine: %q", err)
}
s.db[0].addr.SetValue("10.0.0.6")
if err := s.validateMachines(); !strings.Contains(err, "password") {
t.Errorf("a password login with no password: %q", err)
}
s.db[1].secret.SetValue("pw")
if err := s.validateMachines(); err != "" {
t.Errorf("a valid pair refused: %s", err)
}
cmd := s.command()
for _, want := range []string{"--admin a@example.com", "--patroni ubuntu@10.0.0.6", "--patroni ubuntu@10.0.0.7", "--no-node"} {
if !strings.Contains(cmd, want) {
t.Errorf("command lacks %q:\n%s", want, cmd)
}
}
if strings.Contains(cmd, "pw") {
t.Error("the password is in the command line")
}
}
func TestChoosingOneMachineHandsOverToInit(t *testing.T) {
m := press(tea.Model(newSetupModel("ctl", nil)), "enter").(setupModel)
if !m.single {
t.Error("one machine did not hand over")
}
}
func TestTheChecksHoldTheSetupBackOnlyForAFailure(t *testing.T) {
m := newSetupModel("ctl", nil)
m.choice, m.page = 1, pCheck
m.report = preflight.Report{Findings: []preflight.Finding{{Machine: "a", Check: "disk", Level: preflight.Fail, Fix: "more"}}}
if s := press(m, "enter").(setupModel); s.page != pCheck {
t.Error("went on past a failed check")
}
m.report.Findings[0].Level = preflight.Warn
if s := press(m, "enter").(setupModel); s.page != pPlan {
t.Error("a warning held it back")
}
}
func TestTheConsoleOffersToReplicateOnlyAStoreThatIsNot(t *testing.T) {
m := consoleModel{}
if !strings.Contains(m.items()[1].label, "Replicate") {
t.Error("a single store is not offered replication")
}
m.store = true
for _, it := range m.items() {
if strings.Contains(it.label, "Replicate") {
t.Error("a replicated store is offered replication")
}
}
// A node is asked for, then the console closes with it to do.
var mm tea.Model = consoleModel{summary: "x", loaded: true}
mm = press(mm, "enter", "ubuntu@10.0.0.9", "enter")
if a := mm.(consoleModel).action; a.kind != "node" || a.targets[0] != "ubuntu@10.0.0.9" {
t.Errorf("%+v", a)
}
_ = adminapi.Summary{}
}
func TestTheSetupIsDrawnAsWindows(t *testing.T) {
m := newSetupModel("ctl", []string{"10.0.0.5"})
m.width, m.height = 100, 30
v := m.View()
lines := strings.Split(v, "\n")
if len(lines) != 30 {
t.Errorf("%d lines for a 30-line terminal", len(lines))
}
for _, want := range []string{"llmgw setup", "Steps", "Start", "╭", "One machine", "Ctrl+C"} {
if !strings.Contains(v, want) {
t.Errorf("the screen lacks %q:\n%s", want, v)
}
}
}