Files
WeKnora/cli/cmd/search/sessions_test.go
T
nullkey 26fa43e2cc fix(cli): post-audit fixes (MCP MatchCount + sessions + auth + view)
Four unrelated shipped-code drifts found during v0.5 audit cycles:

1. MCP search_chunks tool omitted MatchCount from SearchParams. Server
   fell back to its default cap; agents asking for limit:50 silently
   got fewer results. Adds MatchCount: limit to the struct literal.

2. search sessions printed UpdatedAt as raw RFC3339 while session list
   used a fuzzy "X hours ago" render — same SDK field, two human
   renderings. Switches to the shared text.FuzzyAgoStr helper for
   parity.

3. auth status --json omitted three operationally-meaningful AuthUser
   fields (username, is_active, can_access_all_tenants). Agents
   branching on can_access_all_tenants previously needed a second
   round-trip.

4. session view Long help claimed the SDK doesn't wrap session_messages;
   it does (LoadMessages / GetMessagesBefore / GetRecentMessages all
   exist in client/message.go). Rewrites the comment to be accurate.
2026-05-16 16:56:33 +08:00

98 lines
3.5 KiB
Go

package search
import (
"context"
"errors"
"testing"
"time"
"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}, nil, 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}, nil, 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}, nil, 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}, nil, svc)
require.Error(t, err)
var typed *cmdutil.Error
require.ErrorAs(t, err, &typed)
assert.Equal(t, cmdutil.CodeServerError, typed.Code)
}
// TestSessionsSearch_RendersFuzzyTime is a regression guard for the v0.5
// audit bug: `search sessions` printed UpdatedAt as the raw RFC3339 string
// while `session list` ran it through text.FuzzyAgoStr — same SDK field,
// two human renderings. Asserts the human output now renders relative time
// (and does NOT contain the RFC3339 "T" date/time separator).
func TestSessionsSearch_RendersFuzzyTime(t *testing.T) {
out, _ := iostreams.SetForTest(t)
svc := &fakeSessionsSearchSvc{
pages: map[int][]sdk.Session{1: {
{ID: "s1", Title: "needle", UpdatedAt: time.Now().Add(-2 * time.Hour).Format(time.RFC3339)},
}},
total: 1,
}
require.NoError(t, runSessionsSearch(context.Background(), &SessionsSearchOptions{Query: "needle", Limit: 10}, nil, svc))
body := out.String()
assert.Contains(t, body, "hour", "must render relative time (e.g. 'about 2 hours ago'), not raw RFC3339")
assert.NotContains(t, body, "T0", "raw RFC3339 has 'T' between date and time; fuzzyTime output should not")
}