Skip to content

Commit 9911ba9

Browse files
committed
test(files): explain the resolver's real candidate query and fix fixtures
Mock displaySegmentKey in the central schema mock, store size_bytes on the raw fixture rows, and assert the plan of the full candidate query (id arm, display-name arm, order) rather than a reduced one.
1 parent 1d93b15 commit 9911ba9

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

‎apps/sim/lib/uploads/contexts/workspace/__integration__/file-names.integration.ts‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
workspaceFiles,
1717
} from '@sim/db/schema'
1818
import { generateId } from '@sim/utils/id'
19-
import { and, eq, inArray, isNull, sql } from 'drizzle-orm'
19+
import { and, eq, inArray, isNull, or, sql } from 'drizzle-orm'
2020
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
2121

2222
const fixtureStorage = vi.hoisted(() => ({ root: '' }))
@@ -239,9 +239,9 @@ describe('workspace file names in PostgreSQL', () => {
239239
}))
240240
for (const file of files) {
241241
await db.execute(sql`
242-
INSERT INTO ${workspaceFiles} (id, key, user_id, workspace_id, folder_id, context, original_name, content_type)
242+
INSERT INTO ${workspaceFiles} (id, key, user_id, workspace_id, folder_id, context, original_name, content_type, size_bytes)
243243
VALUES (${file.id}, ${`legacy/${file.id}`}, ${fixture.aliceId}, ${fixture.workspaceId},
244-
${file.folderId}, 'workspace', ${file.name}, 'text/plain')`)
244+
${file.folderId}, 'workspace', ${file.name}, 'text/plain', 1)`)
245245
}
246246
await db.execute(sql`
247247
INSERT INTO ${workspaceFiles} (id, key, user_id, workspace_id, context, original_name, content_type)
@@ -264,17 +264,22 @@ describe('workspace file names in PostgreSQL', () => {
264264
}
265265
expect(await resolveWorkspaceFileReference(fixture.workspaceId, 'brand-new.txt')).toBeNull()
266266

267+
// The resolver's candidate query for a miss: the id arm, the display-name arm, its order.
267268
const plan = await db.execute(
268-
sql`EXPLAIN (FORMAT JSON) SELECT id FROM ${workspaceFiles} WHERE ${and(
269-
eq(workspaceFiles.workspaceId, fixture.workspaceId),
270-
eq(workspaceFiles.context, 'workspace'),
271-
isNull(workspaceFiles.deletedAt),
272-
eq(displaySegmentKey(workspaceFiles.originalName), 'brand-new.txt')
273-
)}`
274-
)
275-
expect(JSON.stringify(plan[0]['QUERY PLAN'])).toContain(
276-
'workspace_files_workspace_display_name_idx'
269+
sql`EXPLAIN (FORMAT JSON) SELECT id, original_name, folder_id, uploaded_at
270+
FROM ${workspaceFiles} WHERE ${and(
271+
eq(workspaceFiles.workspaceId, fixture.workspaceId),
272+
eq(workspaceFiles.context, 'workspace'),
273+
isNull(workspaceFiles.deletedAt),
274+
or(
275+
inArray(workspaceFiles.id, ['brand-new.txt']),
276+
eq(displaySegmentKey(workspaceFiles.originalName), 'brand-new.txt')
277+
)
278+
)} ORDER BY ${workspaceFiles.uploadedAt}`
277279
)
280+
const scan = JSON.stringify(plan[0]['QUERY PLAN'])
281+
expect(scan).toContain('workspace_files_workspace_display_name_idx')
282+
expect(scan).not.toContain('workspace_files_workspace_active_keyset_idx')
278283
})
279284

280285
it('falls back to a short-id suffix after 20 numbered copies, including under concurrency', async () => {

‎packages/testing/src/mocks/schema.mock.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ export const schemaMock = {
8383
* shape keep working.
8484
*/
8585
foldedEmail: (column: unknown) => column,
86+
/** The schema's display-name expression; returns its column, like {@link foldedEmail}. */
87+
displaySegmentKey: (column: unknown) => column,
8688
/** Custom column type for tsvector. */
8789
tsvector: 'tsvector',
8890
/** Custom column type for bytea. */

0 commit comments

Comments
 (0)