Files
magpie/internal/gui/route_cli_named_test.go
T
yetone 3173c4f41e gui test: a route the CLI's list doesn't name fails internal/gui too
TestEveryRouteHasACommandOrIsNamed (internal/cli) went red on main for a new GUI route six times in a week (#5946, #6162, #6677, #6724, #6786): GUI changes run internal/gui, not internal/cli. TestEveryRouteIsNamedForTheCLI reads the same HandleFunc routes and fails when one isn't a key in internal/cli/settings_parity_test.go, so the run a GUI change already does catches it. The CLI test still checks which command each route has. LESSONS: the 11-15 case and the rule that a check repeatedly missed goes where the work runs.

Run at origin/main ec5b96c44 plus this, on mica: go test -count=1 ./internal/gui ok (185s); TestEveryRouteIsNamedForTheCLI red with the quit/disconnect line taken out of notYet ("POST /api/quit/disconnect (api.go) isn't named ..."), green with it. gofmt -l clean. Not run: Linux and Windows vet (test-only file, os/filepath/regexp only).
2026-11-16 01:18:18 +08:00

48 lines
1.3 KiB
Go

package gui
import (
"os"
"path/filepath"
"regexp"
"strings"
"testing"
)
// Every /api route this package serves is named in internal/cli's route
// list (cliForRoute or notYet in settings_parity_test.go), checked here
// too: a GUI change runs this package, and a new route unnamed there left
// TestEveryRouteHasACommandOrIsNamed red on main again and again (#5946,
// #6162, #6677, #6724, #6786), each found by CI after the push. That
// test still says which command a route has; this one only says the
// route was named at all.
func TestEveryRouteIsNamedForTheCLI(t *testing.T) {
list, err := os.ReadFile(filepath.Join("..", "cli", "settings_parity_test.go"))
if err != nil {
t.Fatal(err)
}
files, err := filepath.Glob("*.go")
if err != nil {
t.Fatal(err)
}
re := regexp.MustCompile(`HandleFunc\("([A-Z]+ /api/[^"]*)"`)
n := 0
for _, f := range files {
if strings.HasSuffix(f, "_test.go") {
continue
}
b, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
for _, m := range re.FindAllStringSubmatch(string(b), -1) {
n++
if !strings.Contains(string(list), `"`+m[1]+`":`) {
t.Errorf("%s (%s) isn't named in internal/cli/settings_parity_test.go: add it to cliForRoute with its command, or to notYet with why there's none", m[1], f)
}
}
}
if n < 200 {
t.Fatalf("read %d routes", n)
}
}