Files
Enterprise-Knowledge-Base/cli/internal/agent/mode_test.go
T
nullkey a39805aa89 feat(cli): scaffold weknora CLI module foundation (PR-1)
Empty cli/ Go module with Factory + Options skeleton that all v0.x commands
plug into.

Foundation packages (10 internal/):
- cmdutil: Factory(3 closure: Config/Client/Prompter) + Options + Errors
  with namespaced ErrorCode + typed predicates (IsAuthError / IsNotFound /
  IsTransient / IsAuthExpired) + Exporter + AddJSONFlags + ExitCode/PrintError
- iostreams: package singleton with TTY detection; ColorEnabled honors
  NO_COLOR / TERM=dumb on demand
- agent: mode + detect + annotations (Stripe pkg/useragent pattern;
  CLAUDECODE / CURSOR_AGENT / CODEX_* / AIDER_* / CONTINUE_* /
  OPENCODE_* / GEMINICODER env detection)
- build: ldflags-injected version/commit/date
- safepaths: separator-aware path-traversal protection
- config: yaml.v3 schema + atomic save 0600 (XDG-style)
- format: success/failure envelope (code/hint/request_id/risk/retryable/
  console_url) with typed RiskLevel
- prompt: Prompter interface + AgentPrompter (rejects in agent mode)
- secrets: file 0600 store with weknora:<context>:<key> namespace
- testutil: XDGTempDir(t) test helper

Smoke command: weknora version (-/--json). Build/test/vet matrix in
.github/workflows/cli.yml across linux x macos x windows x Go 1.24.

Factory.Client returns CodeLocalUnimplemented until PR-3 wires the SDK
adapter; PR-1 commands (version) don't need it.
2026-05-07 23:19:46 +08:00

88 lines
2.2 KiB
Go

package agent
import (
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// newTestCmd returns a cobra command with the flags ApplyAgentSugar
// expects to be present (`--agent`, `--json`, `--no-interactive`,
// `--no-progress`).
func newTestCmd() *cobra.Command {
c := &cobra.Command{Use: "x"}
c.Flags().Bool("agent", false, "")
c.Flags().Bool("json", false, "")
c.Flags().Bool("no-interactive", false, "")
c.Flags().Bool("no-progress", false, "")
return c
}
func TestApplyAgentSugar_AgentFlagSet(t *testing.T) {
c := newTestCmd()
require.NoError(t, c.Flags().Set("agent", "true"))
ApplyAgentSugar(c)
j, _ := c.Flags().GetBool("json")
assert.True(t, j)
ni, _ := c.Flags().GetBool("no-interactive")
assert.True(t, ni)
np, _ := c.Flags().GetBool("no-progress")
assert.True(t, np)
}
func TestApplyAgentSugar_NoTrigger(t *testing.T) {
clearAgentEnv(t)
c := newTestCmd()
ApplyAgentSugar(c)
j, _ := c.Flags().GetBool("json")
assert.False(t, j, "without --agent or env, --json must remain unset")
}
func TestApplyAgentSugar_ExplicitJSONFalseWins(t *testing.T) {
clearAgentEnv(t)
c := newTestCmd()
require.NoError(t, c.Flags().Set("agent", "true"))
require.NoError(t, c.Flags().Set("json", "false"))
ApplyAgentSugar(c)
j, _ := c.Flags().GetBool("json")
assert.False(t, j, "explicit --json=false must override agent sugar")
}
func TestFormatAgentGuidance(t *testing.T) {
c := &cobra.Command{Use: "x"}
assert.Equal(t, "", FormatAgentGuidance(c))
SetAgentHelp(c, " use --idempotency-key for safe retries ")
assert.Equal(t, "use --idempotency-key for safe retries", FormatAgentGuidance(c))
}
func TestSetAgentHelp_InitsAnnotations(t *testing.T) {
c := &cobra.Command{Use: "x"}
assert.Nil(t, c.Annotations)
SetAgentHelp(c, "blurb")
require.NotNil(t, c.Annotations)
assert.Equal(t, "blurb", c.Annotations[AIAgentHelpKey])
}
func TestTruthy(t *testing.T) {
cases := []struct {
in string
want bool
}{
{"", false},
{"0", false},
{"false", false},
{"FALSE", false},
{"1", true},
{"true", true},
{"yes", true},
{"anything-else", true},
}
for _, tc := range cases {
t.Run(tc.in, func(t *testing.T) {
assert.Equal(t, tc.want, truthy(tc.in))
})
}
}