feat(cli): agent-first WeKnora CLI
The `weknora` command-line client for the WeKnora RAG server. Full command surface: kb / doc / chunk / session / message / agent / model / search / chat / auth / profile / link / api / mcp / skills / config / doctor / schema / exit-codes. Agent-first wire contract: JSON envelope by default, typed error codes mapped to stable exit codes, error.retry_argv / retryable / partial-failure status, global --dry-run preview, the exit-10 confirmation protocol for writes, and control/ANSI/Bidi input hygiene. Stateless env-credential auth (WEKNORA_API_KEY / WEKNORA_TOKEN + WEKNORA_HOST) for headless/CI/agent use. A curated read-only MCP tool surface (`mcp serve`) and embedded Agent Skills (`skills install`). Extensive unit + contract-golden tests and CI.
This commit is contained in:
+86
-26
@@ -78,14 +78,12 @@ func TestAPI_GetSuccess_JSON(t *testing.T) {
|
||||
if err := runAPI(context.Background(), &Options{}, &cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, cli, "GET", "/api/v1/foo", false); err != nil {
|
||||
t.Fatalf("runAPI: %v", err)
|
||||
}
|
||||
// v0.7 envelope: {ok:true, data:{status, headers, body}}
|
||||
// The server response is passed through directly under envelope.data — no
|
||||
// {status, headers, body} wrapper — so consumers project at the server's
|
||||
// own depth (e.g. .data.value here).
|
||||
var env struct {
|
||||
OK bool `json:"ok"`
|
||||
Data struct {
|
||||
Status int `json:"status"`
|
||||
Headers map[string]string `json:"headers"`
|
||||
Body map[string]any `json:"body"`
|
||||
} `json:"data"`
|
||||
OK bool `json:"ok"`
|
||||
Data map[string]any `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(out.Bytes(), &env); err != nil {
|
||||
t.Fatalf("decode envelope JSON: %v\n%s", err, out.String())
|
||||
@@ -93,15 +91,55 @@ func TestAPI_GetSuccess_JSON(t *testing.T) {
|
||||
if !env.OK {
|
||||
t.Errorf("envelope ok: want true, got false")
|
||||
}
|
||||
got := env.Data
|
||||
if got.Status != 200 {
|
||||
t.Errorf("status: want 200, got %d", got.Status)
|
||||
if v, ok := env.Data["value"]; !ok || v.(float64) != 42 {
|
||||
t.Errorf("data.value: want 42 directly under .data (no .body nesting), got %v", env.Data)
|
||||
}
|
||||
if got.Headers["Content-Type"] != "application/json" {
|
||||
t.Errorf("Content-Type header missing: %v", got.Headers)
|
||||
}
|
||||
|
||||
// TestAPI_InlineDataBody verifies -d/--data sends an inline JSON body and
|
||||
// auto-promotes the method to POST.
|
||||
func TestAPI_InlineDataBody(t *testing.T) {
|
||||
_, _ = iostreams.SetForTest(t)
|
||||
var seenBody []byte
|
||||
var seenMethod string
|
||||
cli, stop := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
seenMethod = r.Method
|
||||
seenBody, _ = io.ReadAll(r.Body)
|
||||
_, _ = w.Write([]byte(`{"id":"new"}`))
|
||||
})
|
||||
defer stop()
|
||||
|
||||
opts := &Options{Data: `{"name":"foo"}`}
|
||||
if err := runAPI(context.Background(), opts, &cmdutil.FormatOptions{Mode: cmdutil.FormatText}, cli, resolveMethod(opts), "/api/v1/things", false); err != nil {
|
||||
t.Fatalf("runAPI: %v", err)
|
||||
}
|
||||
if v, ok := got.Body["value"]; !ok || v.(float64) != 42 {
|
||||
t.Errorf("body.value: want 42, got %v", got.Body)
|
||||
if seenMethod != http.MethodPost {
|
||||
t.Errorf("inline -d should auto-promote to POST, got %s", seenMethod)
|
||||
}
|
||||
if string(seenBody) != `{"name":"foo"}` {
|
||||
t.Errorf("server received body %q, want the inline --data JSON", seenBody)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAPI_InlineDataMalformed_RejectedAsInput pins that a malformed -d body is
|
||||
// rejected as input.invalid_argument before any request.
|
||||
func TestAPI_InlineDataMalformed_RejectedAsInput(t *testing.T) {
|
||||
_, _ = iostreams.SetForTest(t)
|
||||
called := false
|
||||
cli, stop := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
})
|
||||
defer stop()
|
||||
|
||||
opts := &Options{Data: `{bad`}
|
||||
err := runAPI(context.Background(), opts, &cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, cli, "POST", "/api/v1/things", false)
|
||||
var typed *cmdutil.Error
|
||||
if !errors.As(err, &typed) || typed.Code != cmdutil.CodeInputInvalidArgument {
|
||||
t.Errorf("want input.invalid_argument for malformed -d, got %v", err)
|
||||
}
|
||||
if called {
|
||||
t.Error("server must not be called with a malformed inline body")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,6 +168,33 @@ func TestAPI_PostWithStdinInput(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestAPI_MalformedInputJSON_RejectedAsInput pins that a non-JSON --input body
|
||||
// is rejected with input.invalid_argument (exit 5) at the boundary, not the
|
||||
// confusing network.error (exit 7, "retryable") the SDK's marshal step
|
||||
// produced. The server must never be called.
|
||||
func TestAPI_MalformedInputJSON_RejectedAsInput(t *testing.T) {
|
||||
_, _ = iostreams.SetForTest(t)
|
||||
called := false
|
||||
cli, stop := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
})
|
||||
defer stop()
|
||||
|
||||
opts := &Options{Input: "-", StdinReader: strings.NewReader(`{bad json`)}
|
||||
err := runAPI(context.Background(), opts, &cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, cli, "POST", "/api/v1/things", false)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error for malformed --input JSON")
|
||||
}
|
||||
var typed *cmdutil.Error
|
||||
if !errors.As(err, &typed) || typed.Code != cmdutil.CodeInputInvalidArgument {
|
||||
t.Errorf("want input.invalid_argument, got %v", err)
|
||||
}
|
||||
if called {
|
||||
t.Error("server must not be called with a malformed body")
|
||||
}
|
||||
}
|
||||
|
||||
// TestAPI_InputFile verifies --input <file> reads the request body from disk.
|
||||
func TestAPI_InputFile(t *testing.T) {
|
||||
_, _ = iostreams.SetForTest(t)
|
||||
@@ -418,14 +483,12 @@ func TestAPI_PaginateNoMetadataPassesThrough(t *testing.T) {
|
||||
if called != 1 {
|
||||
t.Errorf("called %d times, want 1 (non-paginated response)", called)
|
||||
}
|
||||
// H3a: fallback must produce an envelope, not raw passthrough.
|
||||
// The non-paginated fallback must emit the SAME flat shape as a single
|
||||
// call: the server response directly under .data (no {status,headers,body}
|
||||
// wrapper), so --paginate and single-call project identically.
|
||||
var env struct {
|
||||
OK bool `json:"ok"`
|
||||
Data struct {
|
||||
Status int `json:"status"`
|
||||
Headers map[string]string `json:"headers"`
|
||||
Body map[string]any `json:"body"`
|
||||
} `json:"data"`
|
||||
OK bool `json:"ok"`
|
||||
Data map[string]any `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(out.Bytes(), &env); err != nil {
|
||||
t.Fatalf("non-paginated fallback must emit envelope JSON, unmarshal failed: %v\n%s", err, out.String())
|
||||
@@ -433,11 +496,8 @@ func TestAPI_PaginateNoMetadataPassesThrough(t *testing.T) {
|
||||
if !env.OK {
|
||||
t.Errorf("envelope ok: want true, got false")
|
||||
}
|
||||
if env.Data.Status != 200 {
|
||||
t.Errorf("envelope data.status: want 200, got %d", env.Data.Status)
|
||||
}
|
||||
if v, ok := env.Data.Body["hello"]; !ok || v.(string) != "world" {
|
||||
t.Errorf("envelope data.body: want {hello:world}, got %v", env.Data.Body)
|
||||
if v, ok := env.Data["hello"]; !ok || v.(string) != "world" {
|
||||
t.Errorf("fallback must pass the body through flat under .data (.data.hello), got %v", env.Data)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user