Files
WeKnora/cli/cmd/root_test.go
nullkey 733bb3aaa1 refactor(cli): symmetric envelope infrastructure (supersedes e623e820)
Re-introduce the agent-first symmetric envelope deleted in commit
e623e820 (2026-05-15). Under the v0.7 constraint that AI agents are
primary consumers, the bare-JSON shape can't carry protocol channels
(_notice / risk / meta / request_id / profile) that agents need.
Errors-on-stderr and typed exit codes from e623e820 are preserved;
the envelope wraps the success/error payloads on top.

- cli/internal/output/envelope.go (new): Envelope / ErrorEnvelope /
  Meta / ErrDetail / RiskDetail structs; WriteEnvelope +
  WriteErrorEnvelope writers; PendingNotice plumbing for the open-map
  _notice channel (reserved infra; producer wiring planned for v0.8).
- cli/internal/output/envelope_test.go (new): 7 tests covering
  success / error / TTY indent / Notice / Risk shapes.
- cmdutil.Error extended with RetryCommand (directly-executable
  argv distinct from prose Hint), RetryAfterSeconds (HTTP
  Retry-After), *RiskInfo (nested level+action; ErrInternalServer-
  vs-NotFound distinction), Detail (open structured payload),
  Silent (suppress stderr emit while preserving Code for ExitCode).
- ErrorToDetail single source of envelope.error construction; reused
  by stderr PrintError, MCP StructuredContent, and batch per-item
  error path.
- WithHint / WithRetryCommand / WithRetryAfter / WithDetail /
  WithRisk builders. IsCancelled helper. AsError unwrap helper.
- defaultHint covers ~21 codes; defaultRetryCommand symmetric
  counterpart for 6 keyway codes. DefaultHint / DefaultRetryCommand
  exported wrappers for cross-package callers.
- ClassifyHTTPError tightened: rescues HTTP 500 with structured
  server-side code 1003 (ErrNotFound) into resource.not_found.
  Server's generic 1007 (ErrInternalServer) bucket stays as
  server.error — including it would mis-route validation failures
  (e.g. SQLSTATE 22001) as not-found.
- PrintError dual-mode: text/human → prose with code:msg / hint /
  retry lines; json/ndjson → envelope on stderr. Mode pinned by
  root PersistentPreRunE via SetFormatMode. resolveFormatEarly()
  scans argv before cobra dispatch so cobra-side validators
  (unknown flag, arg-count) still surface as envelope when
  --format json is in effect. Silent typed errors short-circuit
  the stderr emit. FlagError mapped to input.invalid_argument for
  the envelope (exit code stays 2 via FlagError class).
- Unknown-subcommand guard installed recursively at the root:
  parents with subcommands but no Run/RunE get a typed RunE that
  emits input.unknown_subcommand envelope with
  detail.{unknown, command_path, available[]} and
  retry_command "<parent> --help". cobra.ArbitraryArgs bypasses
  legacyArgs validation so the guard receives unmatched argv.
- cmdutil.Error.Error() returns "<code>: <message>[: <cause>]" for
  chain debugging; ErrorToDetail strips the code prefix from
  envelope.message since the separate type field carries it
  (prevents the doubled-prefix "code: code: ..." that agents would
  see).

Spec: docs/superpowers/specs/2026-05-20-weknora-cli-v0.7-design.md §0 / §4
2026-05-27 10:56:34 +08:00

164 lines
6.0 KiB
Go

package cmd
import (
"bytes"
"encoding/json"
"errors"
"strings"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
)
func TestRoot_Help(t *testing.T) {
var out bytes.Buffer
root := NewRootCmd(cmdutil.New())
root.SetArgs([]string{"--help"})
root.SetOut(&out)
require.NoError(t, root.Execute())
got := out.String()
assert.Contains(t, got, "weknora")
assert.Contains(t, got, "version")
}
func TestVersion_JSON(t *testing.T) {
var out bytes.Buffer
root := NewRootCmd(cmdutil.New())
root.SetArgs([]string{"version", "--format", "json"})
root.SetOut(&out)
require.NoError(t, root.Execute())
got := out.String()
var env struct {
OK bool `json:"ok"`
Data map[string]any `json:"data"`
}
require.NoError(t, json.Unmarshal(out.Bytes(), &env), "expected valid JSON envelope, got: %q", got)
assert.True(t, env.OK, "envelope.ok must be true")
assert.NotNil(t, env.Data, "envelope.data must be present")
assert.Contains(t, got, `"version":`)
}
// Smoke test for cmdutil.ExitCode wiring; full coverage lives in
// cli/internal/cmdutil/exit_test.go.
func TestExecute_ExitCodeSurface(t *testing.T) {
assert.Equal(t, 0, cmdutil.ExitCode(nil))
assert.Equal(t, 1, cmdutil.ExitCode(assert.AnError))
}
// TestMapCobraError_PinnedPrefixes guards against silent breakage if cobra
// changes the message format of unknown-command / required-flag / arg-count
// errors. Cobra v1.10 emits these via fmt.Errorf in args.go and command.go;
// if a future bump alters the wording, this test fails loudly so we update
// cobraFlagErrorPrefixes (or migrate to typed sentinels if cobra ever
// provides them).
func TestMapCobraError_PinnedPrefixes(t *testing.T) {
t.Run("unknown command", func(t *testing.T) {
// With installUnknownSubcommandGuard in place, unknown root-level
// subcommands now return a typed *cmdutil.Error (CodeInputUnknownSubcommand)
// rather than cobra's legacy "unknown command" text. The cobraFlagErrorPrefixes
// fallback remains for any path that bypasses the guard.
root := NewRootCmd(cmdutil.New())
root.SetArgs([]string{"bogus"})
root.SetErr(&bytes.Buffer{})
root.SetOut(&bytes.Buffer{})
err := root.Execute()
require.Error(t, err)
typed := cmdutil.AsError(err)
require.NotNil(t, typed, "expected typed *cmdutil.Error; got %T: %v", err, err)
assert.Equal(t, cmdutil.CodeInputUnknownSubcommand, typed.Code)
})
t.Run("required flag(s)", func(t *testing.T) {
// Self-contained probe - the pin must hold even before resource commands
// register their own required flags. RunE is required: without it cobra
// treats the command as a parent and skips ValidateRequiredFlags.
probe := &cobra.Command{Use: "probe", RunE: func(*cobra.Command, []string) error { return nil }}
probe.Flags().String("host", "", "")
require.NoError(t, probe.MarkFlagRequired("host"))
probe.SetErr(&bytes.Buffer{})
probe.SetOut(&bytes.Buffer{})
err := probe.Execute()
require.Error(t, err)
assert.True(t, strings.HasPrefix(err.Error(), "required flag(s)"),
"cobra required-flag prefix changed; update cobraFlagErrorPrefixes. got: %q", err.Error())
})
t.Run("accepts N arg(s) - ExactArgs", func(t *testing.T) {
probe := &cobra.Command{
Use: "probe",
Args: cobra.ExactArgs(1),
RunE: func(*cobra.Command, []string) error { return nil },
}
probe.SetArgs([]string{}) // no args, but ExactArgs(1) wants 1
probe.SetErr(&bytes.Buffer{})
probe.SetOut(&bytes.Buffer{})
err := probe.Execute()
require.Error(t, err)
assert.True(t, strings.HasPrefix(err.Error(), "accepts "),
"cobra ExactArgs prefix changed; update cobraFlagErrorPrefixes. got: %q", err.Error())
})
}
func TestMapCobraError(t *testing.T) {
t.Run("nil passes through", func(t *testing.T) {
assert.Nil(t, MapCobraError(nil))
})
t.Run("non-matching error passes through", func(t *testing.T) {
err := MapCobraError(assert.AnError)
assert.Equal(t, assert.AnError, err)
})
t.Run("unknown command wraps as FlagError", func(t *testing.T) {
err := MapCobraError(errors.New(`unknown command "bogus" for "weknora"`))
var fe *cmdutil.FlagError
assert.True(t, errors.As(err, &fe))
})
t.Run("required flag wraps as FlagError", func(t *testing.T) {
err := MapCobraError(errors.New(`required flag(s) "host" not set`))
var fe *cmdutil.FlagError
assert.True(t, errors.As(err, &fe))
})
t.Run("pflag invalid argument wraps as FlagError", func(t *testing.T) {
// pflag emits: `invalid argument "foo" for "--limit" flag`
err := MapCobraError(errors.New(`invalid argument "foo" for "--limit" flag: strconv.ParseInt: parsing "foo": invalid syntax`))
var fe *cmdutil.FlagError
assert.True(t, errors.As(err, &fe), "pflag-shaped invalid argument should become FlagError")
})
t.Run("domain invalid argument does not wrap", func(t *testing.T) {
// Domain code writing fmt.Errorf("invalid argument: ...") must NOT become FlagError.
err := MapCobraError(errors.New("invalid argument: kb id cannot be empty"))
var fe *cmdutil.FlagError
assert.False(t, errors.As(err, &fe), "domain-shaped invalid argument must not become FlagError")
})
}
// TestRoot_ProfileFlagPropagation guards the cobra → Factory wiring of the
// global --profile flag. Without this, a future refactor that disconnects
// PersistentPreRun from f.ProfileOverride would only fail e2e - the
// per-package TestFactory_ProfileOverride only proves the Factory side.
func TestRoot_ProfileFlagPropagation(t *testing.T) {
cases := []struct {
name string
args []string
want string
}{
{"no flag", []string{"version"}, ""},
{"global before subcmd", []string{"--profile", "staging", "version"}, "staging"},
{"--profile=value form", []string{"--profile=prod", "version"}, "prod"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f := cmdutil.New()
root := NewRootCmd(f)
root.SetArgs(tc.args)
root.SetOut(&bytes.Buffer{})
root.SetErr(&bytes.Buffer{})
require.NoError(t, root.Execute())
assert.Equal(t, tc.want, f.ProfileOverride)
})
}
}