- Bubble Tea + lipgloss with the same frame as llmgw: title bar, sidebar, bordered panel, key line, dialog overlay; keyboard only - pages: overview, machines (filter, state filter, multi-select, bulk actions), machine detail, racks with elevation, jobs, events, tags, profiles, OS images - actions: power, boot device, deploy OS, assign/unassign, tags, location, re-commission, release, cancel job, delete; confirmation for destructive ones; per-machine results - connect screen when no server is configured; URL + token saved to ~/.config/rollcall/client.json (0600), also used by the CLI - batched runes outside text fields are handled key by key - rollcall tui -dump WxH renders one screen; first Go tests for rendering and keys - web: unnamed machines show their id instead of the live image's hostname Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
159 lines
5.2 KiB
Go
159 lines
5.2 KiB
Go
package main
|
||
|
||
import (
|
||
"encoding/json"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
|
||
tea "github.com/charmbracelet/bubbletea"
|
||
"github.com/charmbracelet/x/ansi"
|
||
)
|
||
|
||
// fakeModel is a console with a small fleet loaded, no server needed.
|
||
func fakeModel(t *testing.T, w, h int) tuiModel {
|
||
t.Helper()
|
||
now := time.Now()
|
||
sp, _ := json.Marshal(DeploySpec{OS: "rocky-9", Version: "20261027-92c206", Disk: "/dev/sda"})
|
||
prog := json.RawMessage(`{"phase":"writing","pct":42,"msg":"42% of 552 MiB"}`)
|
||
job := &Job{ID: "j-1", MachineID: "m-a", Kind: "deploy", State: "running", Spec: sp, Progress: prog, Created: now}
|
||
name, os, ip := "node001", "rocky-9", "10.0.0.50"
|
||
bmc := obj{"address": "10.0.0.11", "configured": true}
|
||
m := newTUIModel(&client{base: "http://test"})
|
||
m.w, m.h = w, h
|
||
m.loaded = true
|
||
m.d = tuiData{at: now,
|
||
machines: []Summary{
|
||
{ID: "m-a", State: "deploying", NodeName: &name, OS: &os, MgmtIP: &ip, CPUs: "64", MemoryBytes: 512 << 30, Disks: 2, StorageBytes: 8 << 40, GPUs: 4, IBHCAs: 2,
|
||
BMC: &bmc, Tags: []string{"gpu", "rack-3"}, ManualTags: []string{"rack-3"}, AutoTags: []string{"gpu"}, Job: job, LastSeen: &now,
|
||
Location: Location{Building: "DC1", Room: "Hall A", Rack: "R12", Unit: 24}},
|
||
{ID: "m-b", State: "ready", CPUs: "32", MemoryBytes: 256 << 30, LastSeen: &now},
|
||
},
|
||
racks: []Rack{{Building: "DC1", Room: "Hall A", Name: "R12", Height: 42}},
|
||
jobs: []*Job{job},
|
||
events: []MachineEvent{{Event: Event{TS: now, Event: "deploy started", Detail: "job=j-1"}, MachineID: "m-a"}},
|
||
tags: []TagDef{{Name: "gpu", Match: "{{ gt .Node.GPUs 0 }}", CloudConfig: "#cloud-config\npackages: [x]", Machines: 1}},
|
||
os: []OSImage{{Name: "rocky-9", Current: &OSMeta{Version: "20261027-92c206", Size: 553 << 20, RawSize: 10 << 30, Imported: now}}},
|
||
}
|
||
return m
|
||
}
|
||
|
||
func TestTUIPagesRender(t *testing.T) {
|
||
for _, size := range [][2]int{{140, 40}, {80, 24}, {60, 16}} {
|
||
for page := 0; page < pCount; page++ {
|
||
m := fakeModel(t, size[0], size[1])
|
||
m.menu, m.inPanel = page, true
|
||
out := ansi.Strip(m.View())
|
||
lines := strings.Split(out, "\n")
|
||
if len(lines) != max(size[1], 16) {
|
||
t.Errorf("%dx%d %s: %d lines", size[0], size[1], pageNames[page], len(lines))
|
||
}
|
||
for i, l := range lines {
|
||
if w := ansi.StringWidth(l); w > max(size[0], 60) {
|
||
t.Errorf("%dx%d %s: line %d is %d wide", size[0], size[1], pageNames[page], i, w)
|
||
}
|
||
}
|
||
if !strings.Contains(out, pageLabels[page]) {
|
||
t.Errorf("%s: label missing", pageNames[page])
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestTUIMachinesListAndFilter(t *testing.T) {
|
||
m := fakeModel(t, 160, 30)
|
||
m.menu, m.inPanel = pMachines, true
|
||
out := ansi.Strip(m.View())
|
||
for _, want := range []string{"node001", "deploying 42%", "Hall A/R12 U24", "64c 512.0G 2×8.0T 4GPU 2IB", "m-b"} {
|
||
if !strings.Contains(out, want) {
|
||
t.Errorf("machines page lacks %q", want)
|
||
}
|
||
}
|
||
m.filter.SetValue("r12")
|
||
if got := len(m.filtered()); got != 1 {
|
||
t.Errorf("filter r12: %d machines, want 1", got)
|
||
}
|
||
m.filter.SetValue("")
|
||
m.stateF = "ready"
|
||
if got := m.filtered(); len(got) != 1 || got[0].ID != "m-b" {
|
||
t.Errorf("state filter: %+v", got)
|
||
}
|
||
}
|
||
|
||
func TestTUIKeysSelectionAndActions(t *testing.T) {
|
||
m := fakeModel(t, 140, 40)
|
||
press := func(s string) {
|
||
var k tea.KeyMsg
|
||
switch s {
|
||
case "enter":
|
||
k = tea.KeyMsg{Type: tea.KeyEnter}
|
||
case "esc":
|
||
k = tea.KeyMsg{Type: tea.KeyEsc}
|
||
case " ":
|
||
k = tea.KeyMsg{Type: tea.KeySpace}
|
||
default:
|
||
k = tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(s)}
|
||
}
|
||
mm, _ := m.Update(k)
|
||
m = mm.(tuiModel)
|
||
}
|
||
press("j") // Machines
|
||
press("enter")
|
||
if !m.inPanel || m.menu != pMachines {
|
||
t.Fatalf("not in the machines panel: menu=%d inPanel=%v", m.menu, m.inPanel)
|
||
}
|
||
press(" ")
|
||
press(" ")
|
||
if len(m.sel) != 2 {
|
||
t.Fatalf("selected %d, want 2", len(m.sel))
|
||
}
|
||
press("a")
|
||
if m.dlg == nil || !strings.Contains(m.dlg.title, "2 machines") {
|
||
t.Fatalf("actions dialog for the selection not open: %+v", m.dlg)
|
||
}
|
||
out := ansi.Strip(m.View())
|
||
for _, want := range []string{"Power cycle", "Deploy OS…", "Location…", "Delete record"} {
|
||
if !strings.Contains(out, want) {
|
||
t.Errorf("actions menu lacks %q", want)
|
||
}
|
||
}
|
||
// Deploy OS… opens a form with the OS image choice
|
||
for i, it := range m.dlg.menu {
|
||
if it.label == "Deploy OS…" {
|
||
m.dlg.focus = i
|
||
}
|
||
}
|
||
press("enter")
|
||
if m.dlg == nil || len(m.dlg.fields) == 0 || m.dlg.fields[0].value() != "rocky-9" {
|
||
t.Fatalf("deploy form not open with rocky-9: %+v", m.dlg)
|
||
}
|
||
press("esc")
|
||
press("esc") // clears the selection
|
||
if len(m.sel) != 0 {
|
||
t.Errorf("Esc did not clear the selection")
|
||
}
|
||
}
|
||
|
||
func TestTUIBatchedRunes(t *testing.T) {
|
||
m := fakeModel(t, 140, 40)
|
||
mm, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("jj")})
|
||
if got := mm.(tuiModel).menu; got != pRacks {
|
||
t.Errorf("\"jj\" in one message moved to page %d, want %d (Racks)", got, pRacks)
|
||
}
|
||
m.filtering = true
|
||
m.filter.Focus()
|
||
mm, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("r12")})
|
||
if got := mm.(tuiModel).filter.Value(); got != "r12" {
|
||
t.Errorf("typed into the filter: %q, want r12", got)
|
||
}
|
||
}
|
||
|
||
func TestTUIRackElevation(t *testing.T) {
|
||
m := fakeModel(t, 120, 60)
|
||
m.menu, m.inPanel, m.rackOpen = pRacks, true, 0
|
||
out := ansi.Strip(m.View())
|
||
if !strings.Contains(out, "24 │ node001") {
|
||
t.Errorf("elevation lacks node001 at U24:\n%s", out)
|
||
}
|
||
}
|