diff --git a/packages/convex/convex/feedPagination.test.ts b/packages/convex/convex/feedPagination.test.ts index efe7dd9a89..df987bdd03 100644 --- a/packages/convex/convex/feedPagination.test.ts +++ b/packages/convex/convex/feedPagination.test.ts @@ -30,7 +30,10 @@ function makeFetchPage(tables: Record) { const acceptMain = (c: any) => !c.subagent; // Drain the feed to the end, asserting per-page invariants as we go. -async function drain(tables: Record, opts?: { limit?: number; startCursor?: string | null; maxPages?: number }) { +async function drain( + tables: Record, + opts?: { limit?: number; startCursor?: string | null; maxPages?: number; perMemberFetch?: number; perMemberWant?: number }, +) { const limit = opts?.limit ?? 5; let cursor: string | null = opts?.startCursor ?? null; const pages: any[][] = []; @@ -42,8 +45,8 @@ async function drain(tables: Record, opts?: { limit?: number; sta limit, fetchPage: makeFetchPage(tables), accept: acceptMain, - perMemberFetch: 4, - perMemberWant: 3, + perMemberFetch: opts?.perMemberFetch ?? 4, + perMemberWant: opts?.perMemberWant ?? 3, maxBatches: 2, }); pages.push(page.rows); @@ -97,6 +100,26 @@ describe("paginateTeamFeed", () => { expect(seen.size).toBe(6); }); + test("a member whose rows fit one batch keeps those past its share for the next page", async () => { + // One fetch reads all five rows and the page takes three: the other two lead the next page. + const tables = { a: member([{ t: 50 }, { t: 40 }, { t: 30 }, { t: 20 }, { t: 10 }], "a") }; + const { seen } = await drain(tables, { limit: 3, perMemberFetch: 10, perMemberWant: 3 }); + expect(seen.size).toBe(5); + }); + + test("a team with no member to read ends at once", async () => { + const page = await paginateTeamFeed({ + memberIds: [], + cursor: null, + limit: 5, + fetchPage: makeFetchPage({}), + accept: acceptMain, + perMemberFetch: 4, + perMemberWant: 3, + }); + expect(page).toEqual({ rows: [], nextCursor: null }); + }); + test("legacy numeric cursor is honored as a shared bound and upgrades to composite", async () => { const tables = { a: member([{ t: 100 }, { t: 80 }, { t: 60 }], "a"), diff --git a/packages/convex/convex/feedPagination.ts b/packages/convex/convex/feedPagination.ts index 992826cd2e..2a1d579dc3 100644 --- a/packages/convex/convex/feedPagination.ts +++ b/packages/convex/convex/feedPagination.ts @@ -29,15 +29,19 @@ export async function batchScanConversations(opts: { exhausted = true; break; } + // A scan that reaches `want` mid-batch has not examined the rest of it, + // so a short batch is the end of the index only when read to its end. + let examined = 0; for (const c of batch) { + examined++; if (accept(c)) { rows.push(c); if (rows.length >= want) break; } } - cursor = batch[batch.length - 1].updated_at; + cursor = batch[examined - 1].updated_at; oldestSeen = cursor; - if (batch.length < batchSize) { + if (examined === batch.length && batch.length < batchSize) { exhausted = true; break; } @@ -107,6 +111,8 @@ export async function paginateTeamFeed(opts: { maxBatches?: number; }): Promise<{ rows: any[]; nextCursor: string | null }> { const { memberIds, limit, fetchPage, accept, perMemberFetch, perMemberWant, maxBatches = 4 } = opts; + // No member to read (everyone hidden) is the end, not an empty page to repeat. + if (memberIds.length === 0) return { rows: [], nextCursor: null }; const { legacy, members } = parseFeedCursor(opts.cursor); const boundOf = (id: string): MemberBound => (members ? members[id] : legacy ?? undefined);