package cli import ( "encoding/json" "io" "net/http" "net/http/httptest" "strings" "sync" "testing" tea "github.com/charmbracelet/bubbletea" "gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi" ) type seen struct { mu sync.Mutex reqs []string } func (s *seen) last() string { s.mu.Lock() defer s.mu.Unlock() if len(s.reqs) == 0 { return "" } return s.reqs[len(s.reqs)-1] } // adminStub answers every change with 204 and records it as "METHOD path body". func adminStub(t *testing.T) (*seen, string) { s := &seen{} srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { _, _ = w.Write([]byte("[]")) return } b, _ := io.ReadAll(r.Body) s.mu.Lock() s.reqs = append(s.reqs, r.Method+" "+r.URL.Path+" "+strings.TrimSpace(string(b))) s.mu.Unlock() w.WriteHeader(http.StatusNoContent) })) t.Cleanup(srv.Close) return s, srv.URL } // pressRun presses keys and runs, one level deep, whatever the last one // returns that is not a tick, the way the program would. func pressRun(m tea.Model, keys ...string) tea.Model { m = press(m, keys[:len(keys)-1]...) var msg tea.Msg = tea.KeyMsg{Type: tea.KeyEnter} if k := keys[len(keys)-1]; k != "enter" { msg = tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(k)} } m, cmd := m.Update(msg) if cmd != nil { if done, ok := cmd().(actionDoneMsg); ok { m, _ = m.Update(done) } } return m } func consoleWith(t *testing.T, menu int) (consoleModel, *seen) { s, url := adminStub(t) m := newConsoleModel(url, "/nonexistent/server.yaml") m.loaded, m.menu, m.inPanel = true, menu, true m.data.models = []adminapi.ModelView{{Name: "qwen", Min: 1, Max: 1, Class: "standard", Profiles: []string{"cpu"}}} m.data.nodes = []adminapi.NodeView{{ID: "n1", State: "ready"}} m.data.replicas = []adminapi.ReplicaView{{ID: "qwen-1", Model: "qwen"}} return m, s } func TestAModelsReplicasAreSetFromItsRow(t *testing.T) { m, s := consoleWith(t, cModels) var tm tea.Model = m tm = press(tm, "enter") if !strings.Contains(tm.View(), "Replicas (scale)") { t.Fatalf("no action menu:\n%s", tm.View()) } tm = press(tm, "down", "enter") f := tm.(consoleModel).formD if f == nil { t.Fatal("no form") } f.fields[1].text.SetValue("3") // A max below the min is refused in the form, and nothing is sent. f.fields[0].text.SetValue("5") tm = press(tm, "tab", "tab", "tab", "enter") if f.err == "" || s.last() != "" { t.Fatalf("a max below the min went through: %q %q", f.err, s.last()) } f.fields[0].text.SetValue("2") tm = pressRun(tm, "enter") var body adminapi.ScaleRequest got := s.last() if !strings.HasPrefix(got, "POST /api/v1/models/qwen/scale ") || json.Unmarshal([]byte(strings.SplitN(got, " ", 3)[2]), &body) != nil || body.Min != 2 || body.Max != 3 { t.Fatalf("sent %q", got) } if !strings.Contains(tm.View(), "keeps 2 to 3") { t.Errorf("the outcome was not shown:\n%s", tm.View()) } } func TestANodeIsCordonedAndAModelRemovedOnlyWhenConfirmed(t *testing.T) { m, s := consoleWith(t, cNodes) var tm tea.Model = m tm = pressRun(tm, "enter", "down", "enter") if got := s.last(); got != `POST /api/v1/nodes/n1/state {"state":"cordoned"}` { t.Fatalf("sent %q", got) } m, s = consoleWith(t, cModels) tm = m tm = press(tm, "enter") n := len(tm.(consoleModel).menuD.items) keys := []string{} for range n - 1 { keys = append(keys, "down") } tm = press(tm, append(keys, "enter")...) if tm.(consoleModel).confirmD == nil { t.Fatal("removing did not ask first") } tm = press(tm, "n") if s.last() != "" { t.Fatalf("removed though the answer was no: %q", s.last()) } tm = press(tm, "enter") tm = press(tm, append(keys, "enter")...) _ = pressRun(tm, "y") if got := s.last(); got != "DELETE /api/v1/models/qwen " { t.Fatalf("sent %q", got) } } func TestAQuotaIsAddedWithOnlyWhatIsGiven(t *testing.T) { m, s := consoleWith(t, cQuotas) var tm tea.Model = press(m, "a") f := tm.(consoleModel).formD f.fields[0].text.SetValue("team:acme/ml") f.fields[4].text.SetValue("2") for range len(f.fields) { tm = press(tm, "tab") } _ = pressRun(tm, "enter") if got := s.last(); got != `PUT /api/v1/quotas {"scope":"team:acme/ml","guarantee":{},"limit":{"gpus":2}}` { t.Fatalf("sent %q", got) } }