1b20b06f5e
Output ergonomics:
- `--json` accepts a comma-separated field list (gh-parity); selects
named keys from the per-command payload. Bare `--json` keeps the
full shape.
- `--jq <expr>` evaluates a gojq expression over the JSON; pairs with
`--json field-list` so projection runs before jq.
- `--version` is a global cobra flag in addition to the `version`
subcommand; both render the same line.
- Per-command `--help` now renders the available JSON field list under
"JSON fields available via `--json id,name,...`" (field-discovery
parity with gh / kubectl `-o jsonpath`).
New commands:
- `auth token` — print the active context's credential to stdout for
shell command substitution (`WEKNORA_TOKEN=$(weknora auth token)`).
Default: raw secret, no trailing newline. `--json` emits
`{token, mode, context}`.
- `doc upload --from-url <URL>` — ingest a remote URL via the SDK
`CreateKnowledgeFromURL`. `--name` forwarded as `FileName` so the
server's known-extension heuristic upgrades crawl-mode to
file-download-mode where appropriate.
Includes the simplify post-review polish pass (field-filter unit
tests, --json/--jq compose check, agent_help copy fixes).
98 lines
2.6 KiB
Go
98 lines
2.6 KiB
Go
package linkcmd
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
|
|
"github.com/Tencent/WeKnora/cli/internal/iostreams"
|
|
"github.com/Tencent/WeKnora/cli/internal/projectlink"
|
|
)
|
|
|
|
// mkLinkFile seeds .weknora/project.yaml in dir so the unlink path has
|
|
// something to remove. Returns the absolute file path.
|
|
func mkLinkFile(t *testing.T, dir string) string {
|
|
t.Helper()
|
|
full := filepath.Join(dir, projectlink.DirName, projectlink.FileName)
|
|
if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
|
|
t.Fatalf("mkdir: %v", err)
|
|
}
|
|
if err := projectlink.Save(full, &projectlink.Project{Context: "default", KBID: "kb_abc"}); err != nil {
|
|
t.Fatalf("save: %v", err)
|
|
}
|
|
return full
|
|
}
|
|
|
|
func TestUnlink_RemovesLinkInCwd(t *testing.T) {
|
|
_, _ = iostreams.SetForTest(t)
|
|
tmp := t.TempDir()
|
|
linkPath := mkLinkFile(t, tmp)
|
|
t.Chdir(tmp)
|
|
|
|
if err := runUnlink(&UnlinkOptions{}, nil); err != nil {
|
|
t.Fatalf("runUnlink: %v", err)
|
|
}
|
|
if _, err := os.Stat(linkPath); !os.IsNotExist(err) {
|
|
t.Errorf("link file should be gone, got err=%v", err)
|
|
}
|
|
}
|
|
|
|
func TestUnlink_WalksUpFromSubdir(t *testing.T) {
|
|
_, _ = iostreams.SetForTest(t)
|
|
root := t.TempDir()
|
|
linkPath := mkLinkFile(t, root)
|
|
sub := filepath.Join(root, "deep", "nested")
|
|
if err := os.MkdirAll(sub, 0o755); err != nil {
|
|
t.Fatalf("mkdir sub: %v", err)
|
|
}
|
|
t.Chdir(sub)
|
|
|
|
if err := runUnlink(&UnlinkOptions{}, nil); err != nil {
|
|
t.Fatalf("runUnlink: %v", err)
|
|
}
|
|
if _, err := os.Stat(linkPath); !os.IsNotExist(err) {
|
|
t.Errorf("parent link file should be gone, got err=%v", err)
|
|
}
|
|
}
|
|
|
|
func TestUnlink_NoLink_Errors(t *testing.T) {
|
|
_, _ = iostreams.SetForTest(t)
|
|
tmp := t.TempDir()
|
|
t.Chdir(tmp)
|
|
|
|
err := runUnlink(&UnlinkOptions{}, nil)
|
|
if err == nil {
|
|
t.Fatal("expected error when no link present")
|
|
}
|
|
var typed *cmdutil.Error
|
|
if !errors.As(err, &typed) {
|
|
t.Fatalf("expected *cmdutil.Error, got %T", err)
|
|
}
|
|
if typed.Code != cmdutil.CodeInputInvalidArgument {
|
|
t.Errorf("expected CodeInputInvalidArgument, got %s", typed.Code)
|
|
}
|
|
if !strings.Contains(typed.Message, projectlink.DirName) {
|
|
t.Errorf("error message should name the missing path; got %q", typed.Message)
|
|
}
|
|
}
|
|
|
|
func TestUnlink_JSON_Envelope(t *testing.T) {
|
|
out, _ := iostreams.SetForTest(t)
|
|
tmp := t.TempDir()
|
|
mkLinkFile(t, tmp)
|
|
t.Chdir(tmp)
|
|
|
|
if err := runUnlink(&UnlinkOptions{}, &cmdutil.JSONOptions{}); err != nil {
|
|
t.Fatalf("runUnlink: %v", err)
|
|
}
|
|
got := out.String()
|
|
for _, want := range []string{`"ok":true`, `"project_link_path"`, projectlink.DirName} {
|
|
if !strings.Contains(got, want) {
|
|
t.Errorf("missing %q in envelope:\n%s", want, got)
|
|
}
|
|
}
|
|
}
|