Skip to content

Commit 76db356

Browse files
committed
test(knowledge): seed filled projection rows so the fan-out test holds on any provisioned database
1 parent c157ef1 commit 76db356

1 file changed

Lines changed: 26 additions & 12 deletions

File tree

‎apps/sim/lib/knowledge/__integration__/connector-lease-pages.integration.ts‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
user,
1818
workspace,
1919
} from '@sim/db/schema'
20+
import { installProjectionSourceAcl } from '@sim/db/script-migrations/0021_embedding_search_connector'
2021
import { generateId } from '@sim/utils/id'
2122
import { and, eq, inArray, sql } from 'drizzle-orm'
2223
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
@@ -272,6 +273,11 @@ describe('connector lease ACL pages in PostgreSQL', () => {
272273
describe('search projection fan-out', () => {
273274
const CHUNKS = 20
274275

276+
/** The production document trigger under test, whatever an earlier suite left installed. */
277+
beforeAll(async () => {
278+
await installProjectionSourceAcl(db.$client)
279+
})
280+
275281
/** Real chunks: the installed triggers create each chunk's search and keyword projection rows. */
276282
const seedChunks = async (documents: { id: string }[]) => {
277283
const rows = documents.flatMap((entry) =>
@@ -292,18 +298,26 @@ describe('connector lease ACL pages in PostgreSQL', () => {
292298
for (let offset = 0; offset < rows.length; offset += 200)
293299
await db.insert(embedding).values(rows.slice(offset, offset + 200))
294300
/**
295-
* The keyword projection's own sync trigger ships with the Tin migration, which a database
296-
* without the Tin extension skips; write the rows it would, and its installed ACL trigger
297-
* fills them from the document as it does for every insert.
301+
* Each chunk's projection rows, filled from its document as the backfill leaves them. The
302+
* embedding insert writes the vector projection's row; the keyword projection's own sync
303+
* trigger ships with the Tin migration, which a database without Tin skips. The fixture sets
304+
* the filled state itself, whatever triggers an earlier suite left behind; what is under
305+
* test is the document trigger that rewrites these rows.
298306
*/
307+
const chunkIds = sql.join(
308+
documents.map((entry) => sql`${entry.id}`),
309+
sql`, `
310+
)
311+
await db.execute(sql`
312+
UPDATE embedding_search p SET enabled = true, connector_id = d.connector_id, acl = d.acl
313+
FROM document d WHERE d.id = p.document_id AND d.id IN (${chunkIds})`)
299314
await db.execute(sql`
300-
INSERT INTO embedding_keyword_tin (id, knowledge_base_id, document_id, enabled, content)
301-
SELECT e.id, e.knowledge_base_id, e.document_id, e.enabled, e.content FROM embedding e
302-
WHERE e.document_id IN (${sql.join(
303-
documents.map((entry) => sql`${entry.id}`),
304-
sql`, `
305-
)})
306-
ON CONFLICT (id) DO NOTHING`)
315+
INSERT INTO embedding_keyword_tin (id, knowledge_base_id, document_id, enabled, content, connector_id, acl)
316+
SELECT e.id, e.knowledge_base_id, e.document_id, true, e.content, d.connector_id, d.acl
317+
FROM embedding e JOIN document d ON d.id = e.document_id WHERE e.document_id IN (${chunkIds})
318+
ON CONFLICT (id) DO UPDATE SET enabled = true, connector_id = EXCLUDED.connector_id, acl = EXCLUDED.acl`)
319+
/** Only writes made by the code under test are counted. */
320+
await db.execute(sql`DELETE FROM lease_page_projection_writes`)
307321
await db
308322
.update(document)
309323
.set({ chunkCount: CHUNKS })
@@ -348,7 +362,7 @@ describe('connector lease ACL pages in PostgreSQL', () => {
348362
).resolves.toEqual({ updated: 30, rejected: 0 })
349363

350364
const after = [...(await projectionAcls(ids.connectorId))]
351-
expect(after.every((row) => row.acl === bob() && row.expected === bob())).toBe(true)
365+
expect(after.filter((row) => row.acl !== bob() || row.expected !== bob())).toEqual([])
352366
const perTransaction = await projectionRowsPerTransaction()
353367
expect(perTransaction.reduce((total, rows) => total + rows, 0)).toBe(2 * 30 * CHUNKS)
354368
expect(Math.max(...perTransaction)).toBeLessThanOrEqual(PROJECTION_ROW_BATCH_SIZE)
@@ -368,7 +382,7 @@ describe('connector lease ACL pages in PostgreSQL', () => {
368382

369383
const after = [...(await projectionAcls(members.connectorId))]
370384
expect(after).toHaveLength(2 * 30 * CHUNKS)
371-
expect(after.every((row) => row.acl === '' && row.expected === '')).toBe(true)
385+
expect(after.filter((row) => row.acl !== '' || row.expected !== '')).toEqual([])
372386
expect(Math.max(...(await projectionRowsPerTransaction()))).toBeLessThanOrEqual(
373387
PROJECTION_ROW_BATCH_SIZE
374388
)

0 commit comments

Comments
 (0)