cf84bf2a38
5 new leaf commands wired into the root tree:
whoami — simplified `auth status` (user_id + tenant_id only)
doctor — 4-item self-check (base_url / auth / server_version / cred_storage)
with --offline / --no-cache flags + skip cascade + summary.all_passed
防 agent 看到 envelope.ok=true 误判命令整体 success
kb list — list KBs (default updated_at desc; 0 KB → "(no knowledge bases)")
tabwriter 4-col (ID/NAME/DOCS/UPDATED), display-width truncation
kb get — show single KB details (KEY: VALUE, suppress empty fields)
context use — switch default context (writes config.current_context),
带 levenshtein distance ≤ 2 的 did-you-mean hint
Each command uses the v0.0 narrow Service interface pattern (testable via
fakes), agent.SetAgentHelp for AI-friendly hints, and ClassifyHTTPError
for stable error code mapping.
cmdutil/errors.go: 新增 CodeLocalContextNotFound for `context use`,加入 AllCodes() 注册集.
cli/cmd/root.go: NewRootCmd 改为 exported (acceptance/contract 测试需要),
注册 4 个新命令 + 1 parent group; root_test.go 跟随更新.
65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package kb
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
|
|
"github.com/Tencent/WeKnora/cli/internal/iostreams"
|
|
sdk "github.com/Tencent/WeKnora/client"
|
|
)
|
|
|
|
type fakeGetSvc struct {
|
|
kb *sdk.KnowledgeBase
|
|
err error
|
|
}
|
|
|
|
func (f *fakeGetSvc) GetKnowledgeBase(ctx context.Context, id string) (*sdk.KnowledgeBase, error) {
|
|
return f.kb, f.err
|
|
}
|
|
|
|
func TestGet_OK_Human(t *testing.T) {
|
|
out, _ := iostreams.SetForTest(t)
|
|
svc := &fakeGetSvc{kb: &sdk.KnowledgeBase{
|
|
ID: "kb1", Name: "Marketing", KnowledgeCount: 12, ChunkCount: 245,
|
|
}}
|
|
if err := runGet(context.Background(), &GetOptions{}, svc, "kb1"); err != nil {
|
|
t.Fatalf("runGet: %v", err)
|
|
}
|
|
got := out.String()
|
|
for _, want := range []string{"ID:", "kb1", "NAME:", "Marketing", "DOCS:", "12 docs", "CHUNKS:", "245 chunks"} {
|
|
if !strings.Contains(got, want) {
|
|
t.Errorf("missing %q in:\n%s", want, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGet_OK_JSON(t *testing.T) {
|
|
out, _ := iostreams.SetForTest(t)
|
|
svc := &fakeGetSvc{kb: &sdk.KnowledgeBase{ID: "kb1", Name: "Marketing"}}
|
|
if err := runGet(context.Background(), &GetOptions{JSONOut: true}, svc, "kb1"); err != nil {
|
|
t.Fatalf("runGet: %v", err)
|
|
}
|
|
got := out.String()
|
|
if !strings.Contains(got, `"ok":true`) {
|
|
t.Errorf("expected ok:true in %q", got)
|
|
}
|
|
if !strings.Contains(got, `"id":"kb1"`) {
|
|
t.Errorf("expected id field in %q", got)
|
|
}
|
|
}
|
|
|
|
func TestGet_NotFound(t *testing.T) {
|
|
_, _ = iostreams.SetForTest(t)
|
|
svc := &fakeGetSvc{err: errors.New("HTTP error 404: not found")}
|
|
err := runGet(context.Background(), &GetOptions{}, svc, "missing")
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
if !cmdutil.IsNotFound(err) {
|
|
t.Errorf("expected resource.not_found, got %v", err)
|
|
}
|
|
}
|