d54a7a5834
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.
78 lines
2.5 KiB
Go
78 lines
2.5 KiB
Go
package search
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
|
|
"github.com/Tencent/WeKnora/cli/internal/iostreams"
|
|
sdk "github.com/Tencent/WeKnora/client"
|
|
)
|
|
|
|
type fakeSessionsSearchSvc struct {
|
|
pages map[int][]sdk.Session
|
|
total int
|
|
err error
|
|
calls []int
|
|
}
|
|
|
|
func (f *fakeSessionsSearchSvc) GetSessionsByTenant(_ context.Context, page, pageSize int) ([]sdk.Session, int, error) {
|
|
f.calls = append(f.calls, page)
|
|
if f.err != nil {
|
|
return nil, 0, f.err
|
|
}
|
|
return f.pages[page], f.total, nil
|
|
}
|
|
|
|
func TestSessionsSearch_TitleAndDescription(t *testing.T) {
|
|
out, _ := iostreams.SetForTest(t)
|
|
svc := &fakeSessionsSearchSvc{
|
|
pages: map[int][]sdk.Session{1: {
|
|
{ID: "s1", Title: "Design review", UpdatedAt: "2026-05-12"},
|
|
{ID: "s2", Title: "Random", Description: "with design notes", UpdatedAt: "2026-05-11"},
|
|
{ID: "s3", Title: "Marketing", UpdatedAt: "2026-05-10"},
|
|
}},
|
|
total: 3,
|
|
}
|
|
require.NoError(t, runSessionsSearch(context.Background(), &SessionsSearchOptions{Query: "design", Limit: 20}, svc))
|
|
got := out.String()
|
|
assert.Contains(t, got, "s1")
|
|
assert.Contains(t, got, "s2")
|
|
assert.NotContains(t, got, "s3")
|
|
}
|
|
|
|
func TestSessionsSearch_NoMatches(t *testing.T) {
|
|
out, _ := iostreams.SetForTest(t)
|
|
svc := &fakeSessionsSearchSvc{
|
|
pages: map[int][]sdk.Session{1: {{Title: "foo"}}},
|
|
total: 1,
|
|
}
|
|
require.NoError(t, runSessionsSearch(context.Background(), &SessionsSearchOptions{Query: "missing", Limit: 20}, svc))
|
|
assert.Contains(t, out.String(), "(no matches)")
|
|
}
|
|
|
|
func TestSessionsSearch_PaginatesAndStopsAtLimit(t *testing.T) {
|
|
_, _ = iostreams.SetForTest(t)
|
|
page1 := make([]sdk.Session, sessionsPageSize)
|
|
for i := range page1 {
|
|
page1[i] = sdk.Session{ID: "m", Title: "needle"}
|
|
}
|
|
svc := &fakeSessionsSearchSvc{pages: map[int][]sdk.Session{1: page1}, total: 1000}
|
|
require.NoError(t, runSessionsSearch(context.Background(), &SessionsSearchOptions{Query: "needle", Limit: 5}, svc))
|
|
assert.Equal(t, []int{1}, svc.calls, "stops paging when limit reached")
|
|
}
|
|
|
|
func TestSessionsSearch_NetworkError(t *testing.T) {
|
|
_, _ = iostreams.SetForTest(t)
|
|
svc := &fakeSessionsSearchSvc{err: errors.New("HTTP error 500: internal")}
|
|
err := runSessionsSearch(context.Background(), &SessionsSearchOptions{Query: "x", Limit: 20}, svc)
|
|
require.Error(t, err)
|
|
var typed *cmdutil.Error
|
|
require.ErrorAs(t, err, &typed)
|
|
assert.Equal(t, cmdutil.CodeServerError, typed.Code)
|
|
}
|