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:
@@ -184,3 +184,47 @@ func ClassifyHTTPError(err error) ErrorCode {
|
||||
}
|
||||
return CodeServerError
|
||||
}
|
||||
|
||||
// AllCodes returns the registered error code set.
|
||||
// Used by acceptance/contract/errorcodes_test.go to validate that every code
|
||||
// referenced in cli/cmd/ is present here. Update this list whenever a new
|
||||
// ErrorCode constant is added above.
|
||||
func AllCodes() []ErrorCode {
|
||||
return []ErrorCode{
|
||||
// auth
|
||||
CodeAuthUnauthenticated, CodeAuthTokenExpired, CodeAuthBadCredential,
|
||||
CodeAuthForbidden, CodeAuthCrossTenantBlocked, CodeAuthTenantMismatch,
|
||||
// resource
|
||||
CodeResourceNotFound, CodeResourceAlreadyExists, CodeResourceLocked,
|
||||
// input
|
||||
CodeInputInvalidArgument, CodeInputMissingFlag,
|
||||
// server / network
|
||||
CodeServerError, CodeServerTimeout, CodeServerRateLimited,
|
||||
CodeServerIncompatibleVersion, CodeNetworkError,
|
||||
// local
|
||||
CodeLocalConfigCorrupt, CodeLocalKeychainDenied, CodeLocalFileIO,
|
||||
CodeLocalUnimplemented,
|
||||
// mcp
|
||||
CodeMCPReadonlyMode, CodeMCPToolNotAllowed, CodeMCPSchemaUnknown,
|
||||
// v0.1: context use — added in PR-7 (Task 15) when CodeLocalContextNotFound lands
|
||||
}
|
||||
}
|
||||
|
||||
// ClassifyHTTPErrorOutputs returns every code that ClassifyHTTPError can return.
|
||||
// Bridges the AST-friendly literal model with the dynamic switch inside
|
||||
// ClassifyHTTPError. errorcodes_test.go uses this to seed the "referenced codes"
|
||||
// set without trying to AST-introspect a function-call expression.
|
||||
//
|
||||
// IMPORTANT: keep in sync with the switch in ClassifyHTTPError.
|
||||
func ClassifyHTTPErrorOutputs() []ErrorCode {
|
||||
return []ErrorCode{
|
||||
CodeAuthUnauthenticated, // 401
|
||||
CodeAuthForbidden, // 403
|
||||
CodeResourceNotFound, // 404
|
||||
CodeResourceAlreadyExists, // 409
|
||||
CodeServerRateLimited, // 429
|
||||
CodeServerError, // 5xx / parse-failure / default
|
||||
CodeInputInvalidArgument, // 4xx (else)
|
||||
CodeNetworkError, // 非 HTTP error
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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