fix(cli): apply list limits after filtering and sorting (#3990)

main
LeonSGP 2026-10-08 01:28:19 +08:00 committed by GitHub
parent 7d75ce3a6f
commit cefd5920f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 120 additions and 21 deletions

View File

@ -177,6 +177,7 @@ func runList(ctx context.Context, opts *ListOptions, fopts *cmdutil.FormatOption
// non-walking path returns the first page only.
var items []sdk.Knowledge
var serverTotal int64
truncated := false
if opts.AllPages {
accum := make([]sdk.Knowledge, 0)
for page := 1; ; page++ {
@ -187,7 +188,7 @@ func runList(ctx context.Context, opts *ListOptions, fopts *cmdutil.FormatOption
serverTotal = total
accum = append(accum, chunk...)
if opts.Limit > 0 && len(accum) >= opts.Limit {
accum = accum[:opts.Limit]
truncated = int64(len(accum)) < total
break
}
if int64(len(accum)) >= total || len(chunk) == 0 {
@ -214,7 +215,6 @@ func runList(ctx context.Context, opts *ListOptions, fopts *cmdutil.FormatOption
})
// --limit applies after sort so users get the top-N most-recent items
// when combined with a single-page fetch where page_size > limit.
truncated := false
if opts.Limit > 0 && len(items) > opts.Limit {
items = items[:opts.Limit]
truncated = true

View File

@ -0,0 +1,44 @@
package doc
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
"github.com/Tencent/WeKnora/cli/internal/iostreams"
sdk "github.com/Tencent/WeKnora/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestListAllPagesSortsBeforeLimit(t *testing.T) {
out, _ := iostreams.SetForTest(t)
now := time.Now()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/api/v1/knowledge-bases/kb-test/knowledge", r.URL.Path)
_ = json.NewEncoder(w).Encode(sdk.KnowledgeListResponse{
Success: true, Total: 2,
Data: []sdk.Knowledge{
{ID: "older", UpdatedAt: now.Add(-time.Hour)},
{ID: "newer", UpdatedAt: now},
},
})
}))
defer server.Close()
require.NoError(t, runList(context.Background(), &ListOptions{PageSize: 2, Limit: 1, AllPages: true},
&cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, sdk.NewClient(server.URL), "kb-test"))
var envelope struct {
Data []sdk.Knowledge `json:"data"`
Meta struct {
HasMore bool `json:"has_more"`
} `json:"meta"`
}
require.NoError(t, json.Unmarshal(out.Bytes(), &envelope))
require.Len(t, envelope.Data, 1)
assert.Equal(t, "newer", envelope.Data[0].ID)
assert.True(t, envelope.Meta.HasMore)
}

View File

@ -115,22 +115,38 @@ func runList(ctx context.Context, opts *ListOptions, fopts *cmdutil.FormatOption
since = d
}
threshold := time.Now().Add(-since)
filterSince := func(items []sdk.Session) []sdk.Session {
if since <= 0 {
return items
}
filtered := items[:0]
for _, session := range items {
updated, err := time.Parse(time.RFC3339, session.UpdatedAt)
if err == nil && updated.After(threshold) {
filtered = append(filtered, session)
}
}
return filtered
}
var items []sdk.Session
var serverTotal int
if opts.AllPages {
accum := make([]sdk.Session, 0)
scanned := 0
for page := 1; ; page++ {
chunk, total, err := svc.GetSessionsByTenant(ctx, page, opts.PageSize)
if err != nil {
return cmdutil.WrapHTTP(err, "list sessions")
}
serverTotal = total
accum = append(accum, chunk...)
if opts.Limit > 0 && len(accum) >= opts.Limit {
accum = accum[:opts.Limit]
scanned += len(chunk)
accum = append(accum, filterSince(chunk)...)
if opts.Limit > 0 && len(accum) > opts.Limit {
break
}
if len(accum) >= total || len(chunk) == 0 {
if scanned >= total || len(chunk) == 0 {
break
}
}
@ -141,25 +157,11 @@ func runList(ctx context.Context, opts *ListOptions, fopts *cmdutil.FormatOption
return cmdutil.WrapHTTP(err, "list sessions")
}
serverTotal = total
items = chunk
items = filterSince(chunk)
}
if items == nil {
items = []sdk.Session{} // JSON [] not null
}
if since > 0 {
threshold := time.Now().Add(-since)
filtered := items[:0]
for _, s := range items {
t, err := time.Parse(time.RFC3339, s.UpdatedAt)
if err != nil {
continue // skip unparseable timestamps rather than guess
}
if t.After(threshold) {
filtered = append(filtered, s)
}
}
items = filtered
}
// --limit applies after --since so the cap reflects what the user sees.
truncated := false
if opts.Limit > 0 && len(items) > opts.Limit {

View File

@ -0,0 +1,53 @@
package sessioncmd
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
"github.com/Tencent/WeKnora/cli/internal/iostreams"
sdk "github.com/Tencent/WeKnora/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestListAllPagesLimitAppliesAfterSince(t *testing.T) {
out, _ := iostreams.SetForTest(t)
now := time.Now()
sessions := []sdk.Session{
{ID: "old", UpdatedAt: now.Add(-30 * 24 * time.Hour).Format(time.RFC3339)},
{ID: "recent-one", UpdatedAt: now.Add(-time.Hour).Format(time.RFC3339)},
{ID: "recent-two", UpdatedAt: now.Add(-2 * time.Hour).Format(time.RFC3339)},
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
page, err := strconv.Atoi(r.URL.Query().Get("page"))
assert.NoError(t, err)
assert.Equal(t, "/api/v1/sessions", r.URL.Path)
assert.Equal(t, "1", r.URL.Query().Get("page_size"))
data := []sdk.Session{}
if page >= 1 && page <= len(sessions) {
data = append(data, sessions[page-1])
}
_ = json.NewEncoder(w).Encode(sdk.SessionListResponse{Success: true, Data: data, Total: len(sessions)})
}))
defer server.Close()
require.NoError(t, runList(context.Background(), &ListOptions{PageSize: 1, Limit: 1, AllPages: true, Since: "7d"},
&cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, sdk.NewClient(server.URL)))
var envelope struct {
Data []sdk.Session `json:"data"`
Meta struct {
HasMore bool `json:"has_more"`
TotalCount int `json:"total_count"`
} `json:"meta"`
}
require.NoError(t, json.Unmarshal(out.Bytes(), &envelope))
require.Len(t, envelope.Data, 1)
assert.Equal(t, "recent-one", envelope.Data[0].ID)
assert.True(t, envelope.Meta.HasMore)
assert.Equal(t, 3, envelope.Meta.TotalCount)
}