@@ -18,6 +18,7 @@ import { getWorkspaceKnowledgeBases } from '@/lib/knowledge/service'
1818 */
1919const ids = createKnowledgeAclFixtureIds ( )
2020const foreign = createKnowledgeAclFixtureIds ( )
21+ const manyBases = createKnowledgeAclFixtureIds ( )
2122const offPageId = generateId ( )
2223const emptyId = generateId ( )
2324const archivedId = generateId ( )
@@ -106,6 +107,16 @@ beforeAll(async () => {
106107 SELECT ${ baseId } || '-' || n, ${ baseId } , 'bulk.txt', 'https://fixture.invalid/bulk',
107108 1, 'text/plain', ARRAY['ws'], 1 FROM generate_series(1, 10000) AS n` )
108109 }
110+ await seedKnowledgeAclFixture ( manyBases , { connectorType : 'google_drive' } )
111+ await db . execute ( sql `INSERT INTO knowledge_base (id, workspace_id, user_id, name)
112+ SELECT ${ manyBases . knowledgeBaseId } || '-' || n, ${ manyBases . workspaceId } ,
113+ ${ manyBases . aliceId } , 'Scale fixture ' || n FROM generate_series(1, 10000) AS n` )
114+ await db . execute ( sql `INSERT INTO document
115+ (id, knowledge_base_id, filename, file_url, file_size, mime_type, acl, token_count)
116+ VALUES (${ generateId ( ) } , ${ manyBases . knowledgeBaseId } , 'first.txt',
117+ 'https://fixture.invalid/first', 1, 'text/plain', ARRAY['ws'], 13),
118+ (${ generateId ( ) } , ${ `${ manyBases . knowledgeBaseId } -10000` } , 'last.txt',
119+ 'https://fixture.invalid/last', 1, 'text/plain', ARRAY['ws'], 17)` )
109120 await db . execute ( sql `ANALYZE knowledge_base` )
110121 await db . execute ( sql `ANALYZE document` )
111122} , 60_000 )
@@ -114,7 +125,7 @@ afterAll(async () => {
114125 const reportPath = process . env . KNOWLEDGE_BASE_LIST_REPORT_PATH
115126 if ( reportPath ) writeFileSync ( reportPath , JSON . stringify ( reports , null , 2 ) )
116127 try {
117- for ( const fixture of [ ids , foreign ] ) {
128+ for ( const fixture of [ ids , foreign , manyBases ] ) {
118129 await db . delete ( workspace ) . where ( eq ( workspace . id , fixture . workspaceId ) )
119130 await db . delete ( organization ) . where ( eq ( organization . id , fixture . organizationId ) )
120131 await db . delete ( user ) . where ( inArray ( user . id , [ fixture . aliceId , fixture . bobId ] ) )
@@ -191,4 +202,34 @@ describe('knowledge base list counts on real Postgres', () => {
191202 } )
192203 expect ( archived . data . map ( ( { id } ) => id ) ) . toEqual ( [ archivedId ] )
193204 } )
205+
206+ it ( 'counts a large unpaged workspace within a fixed database round-trip budget' , async ( ) => {
207+ let documentQueries = 0
208+ const previousDebug = db . $client . options . debug
209+ db . $client . options . debug = ( _connection , query ) => {
210+ if ( query . includes ( '"document"' ) ) documentQueries ++
211+ }
212+ try {
213+ const all = await getWorkspaceKnowledgeBases ( manyBases . workspaceId , 'active' , {
214+ countsFor : access ,
215+ } )
216+ expect ( all . data ) . toHaveLength ( 10001 )
217+ expect ( all . nextCursorKeys ) . toBeNull ( )
218+ expect ( all . data . find ( ( kb ) => kb . id === manyBases . knowledgeBaseId ) ) . toMatchObject ( {
219+ docCount : 1 ,
220+ tokenCount : 13 ,
221+ } )
222+ expect ( all . data . find ( ( kb ) => kb . id === `${ manyBases . knowledgeBaseId } -10000` ) ) . toMatchObject ( {
223+ docCount : 1 ,
224+ tokenCount : 17 ,
225+ } )
226+ expect ( all . data . reduce ( ( total , kb ) => total + kb . docCount , 0 ) ) . toBe ( 2 )
227+ expect ( all . data . reduce ( ( total , kb ) => total + kb . tokenCount , 0 ) ) . toBe ( 30 )
228+ reports . push ( { unpagedBases : all . data . length , documentQueries } )
229+ expect ( documentQueries ) . toBeGreaterThan ( 0 )
230+ expect ( documentQueries ) . toBeLessThan ( 10 )
231+ } finally {
232+ db . $client . options . debug = previousDebug
233+ }
234+ } )
194235} )
0 commit comments