feat(cli): add internal/{text,compat} packages and acceptance scaffold (PR-6)
text/ gh-style display-width string helpers (Truncate / FuzzyAgo / Pluralize)
compat/ server-CLI version skew probe (kubectl) + 24h TTL cache (gh internal/update)
acceptance/contract/ helpers_test.go for in-process cobra exec + golden assert
(uses iostreams.SetForTest singleton replacement; no t.Parallel)
cmdutil/errors.go: AllCodes() + ClassifyHTTPErrorOutputs() bridge for the
contract test suite (no behavior change in error handling)
go-runewidth promoted from indirect to direct (used by text.Truncate for
display-width-aware CJK truncation).
acceptance/contract/helpers_test.go forward-references cmd.NewRootCmd which
is exported in PR-7 — that file does not compile until then by design.
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package cmdutil_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
|
||||
)
|
||||
|
||||
func TestAllCodes_NonEmpty(t *testing.T) {
|
||||
codes := cmdutil.AllCodes()
|
||||
if len(codes) == 0 {
|
||||
t.Fatal("AllCodes() should return registered codes")
|
||||
}
|
||||
// Sentinel: 至少包含这几个 v0.0 已知 code
|
||||
want := map[cmdutil.ErrorCode]bool{
|
||||
cmdutil.CodeAuthUnauthenticated: false,
|
||||
cmdutil.CodeResourceNotFound: false,
|
||||
cmdutil.CodeNetworkError: false,
|
||||
}
|
||||
for _, c := range codes {
|
||||
if _, ok := want[c]; ok {
|
||||
want[c] = true
|
||||
}
|
||||
}
|
||||
for c, ok := range want {
|
||||
if !ok {
|
||||
t.Errorf("AllCodes() missing %q", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllCodes_NoDuplicates(t *testing.T) {
|
||||
codes := cmdutil.AllCodes()
|
||||
seen := make(map[cmdutil.ErrorCode]struct{})
|
||||
for _, c := range codes {
|
||||
if _, dup := seen[c]; dup {
|
||||
t.Errorf("AllCodes() duplicate: %q", c)
|
||||
}
|
||||
seen[c] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyHTTPErrorOutputs_Subset(t *testing.T) {
|
||||
outs := cmdutil.ClassifyHTTPErrorOutputs()
|
||||
all := make(map[cmdutil.ErrorCode]struct{})
|
||||
for _, c := range cmdutil.AllCodes() {
|
||||
all[c] = struct{}{}
|
||||
}
|
||||
for _, c := range outs {
|
||||
if _, ok := all[c]; !ok {
|
||||
t.Errorf("ClassifyHTTPErrorOutputs returns %q which is not in AllCodes", c)
|
||||
}
|
||||
}
|
||||
if len(outs) < 5 {
|
||||
t.Errorf("expected at least 5 codes, got %d", len(outs))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user