Files
vpp-maglev/cmd/client/tree_test.go
T
pim 76fbe2eee0 refactor(maglevc): build the CLI on the golang-cli library
Replace maglevc's hand-rolled command-tree CLI with
git.ipng.ch/ipng/golang-cli v1.3.0, mirroring the evpnc refactor. The
tree (commands.go) and the gRPC-status error unwrap (color.go) stay
app-specific; the generic parts — parse tree, completion, '?'-help, the
readline shell, one-shot dispatch, color helpers, and the watch keypress
handler — now come from the library.

- main.go: a single cli.App[grpcapi.MaglevClient] with a Connect
  callback; drops the flag/color-default/dispatch boilerplate.
- commands.go: `type node = cli.Node[grpcapi.MaglevClient]`; label() ->
  cli.Label(); dyn* gain the captured-args parameter the library's
  Dynamic signature carries; runQuit returns cli.ErrQuit.
- watch.go: keypress.WaitForKey replaces the inline cbreak helper.
- color.go: only formatError remains, reading cli.ColorEnabled().
- delete tree.go, complete.go, shell.go.
- tests use the library API; add TestTreeValid (cli.Validate).

Behavior is unchanged except labels/errors now use the library's bright
ANSI palette (was dark); escape lengths are identical so tabwriter
alignment is unaffected. maglevc additionally gains the OpenBSD readline
fix and BSD-correct watch keypress it previously lacked. Builds on linux
and openbsd.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-05 22:38:34 +02:00

203 lines
6.0 KiB
Go

// SPDX-License-Identifier: Apache-2.0
package main
import (
"strings"
"testing"
cli "git.ipng.ch/ipng/golang-cli"
)
func TestExpandPathsRoot(t *testing.T) {
root := buildTree()
lines := cli.ExpandPaths(root, "")
// Should include well-known leaf paths.
want := []string{
"show version",
"show frontends",
"show frontends <name>",
"show backends",
"show backends <name>",
"show healthchecks",
"show healthchecks <name>",
"set backend <name> pause",
"set backend <name> resume",
"set backend <name> disable",
"set backend <name> enable",
"set frontend <name> pool <pool> backend <backend> weight <weight>",
"set frontend <name> pool <pool> backend <backend> weight <weight> flush",
"watch events",
"watch events <opt>",
"config check",
"show vpp info",
"show vpp lb state",
"show vpp lb counters",
"config reload",
"sync vpp lb state",
"sync vpp lb state <name>",
"quit",
"exit",
}
paths := make(map[string]bool, len(lines))
for _, l := range lines {
paths[l.Path] = true
}
for _, w := range want {
if !paths[w] {
t.Errorf("expandPaths(root) missing %q", w)
}
}
}
func TestExpandPathsShow(t *testing.T) {
root := buildTree()
showNode, _, _ := cli.Walk(root, []string{"show"})
lines := cli.ExpandPaths(showNode, "show")
for _, l := range lines {
if !strings.HasPrefix(l.Path, "show ") {
t.Errorf("unexpected path %q: should start with 'show '", l.Path)
}
if l.Help == "" {
t.Errorf("path %q has empty help", l.Path)
}
}
// version, frontends, frontends <name>, backends, backends <name>,
// healthchecks, healthchecks <name>, vpp info, vpp lb state,
// vpp lb counters = 10 lines
if len(lines) != 10 {
t.Errorf("expected exactly 10 show subcommands, got %d", len(lines))
}
}
func TestExpandPathsNoCycles(t *testing.T) {
root := buildTree()
// watch events has a self-referencing slot; expandPaths must terminate.
watchEvents, _, _ := cli.Walk(root, []string{"watch", "events"})
lines := cli.ExpandPaths(watchEvents, "watch events")
// Should produce exactly 2 lines: "watch events" and "watch events <opt>".
if len(lines) != 2 {
t.Errorf("watch events: expected 2 lines, got %d: %v", len(lines), lines)
}
}
func TestExpandPathsSetBackendName(t *testing.T) {
root := buildTree()
// Walk to the name slot so displayPrefix carries the actual arg.
node, _, _ := cli.Walk(root, []string{"set", "backend", "mybackend"})
lines := cli.ExpandPaths(node, "set backend mybackend")
want := []string{
"set backend mybackend pause",
"set backend mybackend resume",
"set backend mybackend disable",
"set backend mybackend enable",
}
if len(lines) != len(want) {
t.Fatalf("expected %d lines, got %d: %v", len(want), len(lines), lines)
}
for i, w := range want {
if lines[i].Path != w {
t.Errorf("line %d: got %q, want %q", i, lines[i].Path, w)
}
}
}
func TestPrefixMatchCollapsedNouns(t *testing.T) {
root := buildTree()
// "sh ba" → show backends (list all) via prefix matching.
node, args, rem := cli.Walk(root, []string{"sh", "ba"})
if node.Run == nil {
t.Fatal("'sh ba' did not reach a Run node")
}
if len(args) != 0 {
t.Errorf("'sh ba' should have 0 args, got %v", args)
}
if len(rem) != 0 {
t.Errorf("'sh ba' should fully consume tokens, got remaining %v", rem)
}
// "sh ba nginx0" → show backends <name> (get specific) via slot.
node, args, rem = cli.Walk(root, []string{"sh", "ba", "nginx0"})
if node.Run == nil {
t.Fatal("'sh ba nginx0' did not reach a Run node")
}
if len(args) != 1 || args[0] != "nginx0" {
t.Errorf("'sh ba nginx0' args: got %v, want [nginx0]", args)
}
if len(rem) != 0 {
t.Errorf("'sh ba nginx0' should fully consume tokens, got remaining %v", rem)
}
// "sh fr" → show frontends (list all).
node, _, _ = cli.Walk(root, []string{"sh", "fr"})
if node.Run == nil {
t.Fatal("'sh fr' did not reach a Run node")
}
// "sh he icmp" → show healthchecks icmp (get specific).
node, args, _ = cli.Walk(root, []string{"sh", "he", "icmp"})
if node.Run == nil {
t.Fatal("'sh he icmp' did not reach a Run node")
}
if len(args) != 1 || args[0] != "icmp" {
t.Errorf("'sh he icmp' args: got %v, want [icmp]", args)
}
}
func TestWalkUnknownTokens(t *testing.T) {
root := buildTree()
// A bare unknown word leaves every token unconsumed and anchors
// the returned node at the root — callers must treat this as
// "unknown command" rather than silently showing the whole tree.
node, _, rem := cli.Walk(root, []string{"foo"})
if node != root {
t.Errorf("'foo' should leave walk at root, got %q", node.Word)
}
if len(rem) != 1 || rem[0] != "foo" {
t.Errorf("'foo' remaining: got %v, want [foo]", rem)
}
// Partial consumption: "show" matches but "bogus" doesn't. The
// returned remaining is the first unmatched token onwards so the
// caller can point at exactly what was wrong.
node, _, rem = cli.Walk(root, []string{"show", "bogus", "tail"})
if node.Word != "show" {
t.Errorf("'show bogus tail' should stop at show, got %q", node.Word)
}
if len(rem) != 2 || rem[0] != "bogus" || rem[1] != "tail" {
t.Errorf("'show bogus tail' remaining: got %v, want [bogus tail]", rem)
}
}
func TestExpandPathsWeightSlotWalk(t *testing.T) {
// Verify the weight command is fully walkable (fixes bug: setWeightValue
// and setFrontendPoolName were non-slot nodes that couldn't capture tokens).
root := buildTree()
node, args, _ := cli.Walk(root, []string{"set", "frontend", "web", "pool", "primary", "backend", "be0", "weight", "42"})
if node.Run == nil {
t.Fatal("Walk did not reach a Run node for full weight command")
}
if len(args) != 4 {
t.Errorf("expected 4 args (name, pool, backend, weight), got %d: %v", len(args), args)
}
if args[3] != "42" {
t.Errorf("args[3] (weight): got %q, want 42", args[3])
}
}
// TestTreeValid guards the command tree against authoring faults (more than one
// slot child per node, empty words, duplicate siblings, dead ends).
func TestTreeValid(t *testing.T) {
if err := cli.Validate(buildTree()); err != nil {
t.Fatalf("buildTree() has authoring faults:\n%v", err)
}
}