11/**
22 * Knowledge-base document totals against real PostgreSQL: the public v1 list and detail count
33 * only the documents their caller can read, and the internal list reads no document at all
4- * unless the caller asks for totals.
4+ * unless the caller asks for totals. A request without the flag is counted, since a page loaded
5+ * before the flag existed requires both totals on every row.
56 */
67import type { Principal } from '@sim/auth/principal'
78import { db } from '@sim/db'
89import { document , organization , user , workspace } from '@sim/db/schema'
9- import { createMockRequest } from '@sim/testing'
10+ import { authMock , authMockFns , createMockRequest } from '@sim/testing'
1011import { generateId } from '@sim/utils/id'
1112import { eq , inArray } from 'drizzle-orm'
1213import { afterAll , beforeAll , describe , expect , it , vi } from 'vitest'
1314
1415const caller = vi . hoisted ( ( ) => ( { userId : '' } ) )
1516
17+ vi . mock ( '@/lib/auth' , ( ) => authMock )
18+
1619vi . mock ( '@/app/api/v1/middleware' , async ( importOriginal ) => ( {
1720 ...( await importOriginal < typeof import ( '@/app/api/v1/middleware' ) > ( ) ) ,
1821 authenticateRequest : async ( ) => ( {
@@ -34,6 +37,7 @@ import {
3437 seedKnowledgeAclFixture ,
3538} from '@/lib/knowledge/__integration__/seed-source-access-fixture'
3639import { listInternalKnowledgeBases } from '@/lib/knowledge/application/knowledge-bases'
40+ import { GET as listInternalKnowledgeBasesRoute } from '@/app/api/knowledge/route'
3741import { GET as getKnowledgeBase } from '@/app/api/v1/knowledge/[id]/route'
3842import { GET as listKnowledgeBases } from '@/app/api/v1/knowledge/route'
3943
@@ -46,6 +50,10 @@ describe('knowledge-base document totals in PostgreSQL', () => {
4650 throw new Error ( 'Unexpected provider request in knowledge-base count tests' )
4751 } )
4852 caller . userId = ids . bobId
53+ authMockFns . mockGetSession . mockResolvedValue ( {
54+ user : { id : ids . bobId } ,
55+ session : { id : 'fixture-reader' } ,
56+ } )
4957 await seedKnowledgeAclFixture ( ids , { connectorType : 'google_drive' } )
5058 await db . insert ( document ) . values ( [
5159 {
@@ -134,4 +142,21 @@ describe('knowledge-base document totals in PostgreSQL', () => {
134142 ) . knowledgeBases
135143 expect ( counted ) . toMatchObject ( { docCount : 1 , tokenCount : 10 } )
136144 } )
145+
146+ it ( 'counts an internal list request that omits the flag' , async ( ) => {
147+ const list = async ( query : string ) => {
148+ const response = await listInternalKnowledgeBasesRoute (
149+ createMockRequest ( 'GET' , undefined , { } , `http://localhost/api/knowledge?${ query } ` ) ,
150+ { params : Promise . resolve ( { } ) }
151+ )
152+ expect ( response . status ) . toBe ( 200 )
153+ return ( await response . json ( ) ) . data
154+ }
155+ const workspaceQuery = `workspaceId=${ ids . workspaceId } &scope=active`
156+ expect ( await list ( workspaceQuery ) ) . toEqual ( [
157+ expect . objectContaining ( { id : ids . knowledgeBaseId , docCount : 1 , tokenCount : 10 } ) ,
158+ ] )
159+ const [ uncounted ] = await list ( `${ workspaceQuery } &includeCounts=false` )
160+ expect ( uncounted ) . not . toHaveProperty ( 'tokenCount' )
161+ } )
137162} )
0 commit comments