Files
Enterprise-Knowledge-Base/cli/cmd/search/search_test.go
T
nullkey d54a7a5834 feat(cli): search verb-noun subtree (chunks/kb/docs/sessions)
Roadmap 3-1. Verb-noun shape borrowed from gh search (gh search repos
/ code / commits / issues / prs verified against the gh manual).

Subcommands:
- `search chunks "<q>" --kb X` — hybrid retrieval (RAG search).
- `search kb "<q>"` — case-insensitive substring match across KB names
  and descriptions; sorted by name length (shortest hits first).
- `search docs "<q>" --kb X` — pages through ListKnowledge filtering by
  title / file_name; stops once --limit matches are found.
- `search sessions "<q>"` — pages through GetSessionsByTenant filtering
  by title / description.

kb / docs / sessions are client-side filters because the server has no
fuzzy search endpoint for any of them. ListKnowledgeBases returns the
full tenant catalog in one call; the doc/session walkers chunk at 200
per request and stop early on limit.

The parent `search` command is a pure dispatcher — there is no bare-
positional form (no `weknora search "<q>"`).

Cleanups surfaced by the post-commit reviewer round:
- UX consistency: search docs's displayDocName ordered Title →
  FileName → "-", while doc list's displayName uses FileName → Title
  → ID. Same Knowledge rendered differently across commands. Aligned
  search docs on doc list's existing FileName-first convention.
- cmdutil.ResolveKBFlag(ctx, lister, raw) — extracted the
  `IsKBID ? raw : ResolveKBNameToID` block duplicated across chunks
  and docs.
- text.ContainsFold(needle, fields...) — replaces inline
  `strings.Contains(strings.ToLower(field), needle)` patterns.

37 unit tests across chunks/kb/docs/sessions plus the parent
registration smoke-test.

Roadmap: 3-1.
2026-05-14 10:57:17 +08:00

52 lines
1.5 KiB
Go

package search
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
"github.com/Tencent/WeKnora/cli/internal/iostreams"
)
// TestSearch_NoArgs_ShowsHelp: bare `weknora search` (no subcommand)
// must run cobra's Help() without erroring.
func TestSearch_NoArgs_ShowsHelp(t *testing.T) {
_, _ = iostreams.SetForTest(t)
cmd := NewCmdSearch(&cmdutil.Factory{})
cmd.SetArgs([]string{})
cmd.SilenceErrors = true
cmd.SilenceUsage = true
require.NoError(t, cmd.Execute())
}
// TestSearch_RejectsPositional: bare positional (e.g. the v0.0-style
// `weknora search "<q>" --kb X`) must error — the legacy alias was
// dropped, search is now pure dispatcher.
func TestSearch_RejectsPositional(t *testing.T) {
_, _ = iostreams.SetForTest(t)
cmd := NewCmdSearch(&cmdutil.Factory{})
cmd.SetArgs([]string{"hello", "--kb", "kb_abc"})
cmd.SilenceErrors = true
cmd.SilenceUsage = true
err := cmd.Execute()
require.Error(t, err)
}
// TestSearch_SubcommandsRegistered: ensure chunks/kb/docs/sessions are
// reachable through the parent. Smoke-test only; the subcommands' own
// tests cover behavior.
func TestSearch_SubcommandsRegistered(t *testing.T) {
f := &cmdutil.Factory{}
cmd := NewCmdSearch(f)
names := map[string]bool{}
for _, c := range cmd.Commands() {
names[c.Name()] = true
}
for _, want := range []string{"chunks", "kb", "docs", "sessions"} {
if !names[want] {
t.Errorf("missing subcommand %q", want)
}
}
}