|
| 1 | +/** |
| 2 | + * Real PostgreSQL reads of a workspace's whole file list: which rows each scope returns, their |
| 3 | + * order, and the plan the live list's SQL gets. |
| 4 | + */ |
| 5 | +import { db } from '@sim/db' |
| 6 | +import { user, workspace, workspaceFiles } from '@sim/db/schema' |
| 7 | +import { explainCaptured } from '@sim/db/testing/explain' |
| 8 | +import { deleteWorkspaceFixture, insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures' |
| 9 | +import { uploadsSetupMock } from '@sim/testing/mocks/uploads-setup.mock' |
| 10 | +import { generateId } from '@sim/utils/id' |
| 11 | +import { eq, inArray } from 'drizzle-orm' |
| 12 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' |
| 13 | + |
| 14 | +vi.mock('@/lib/uploads/core/setup.server', () => uploadsSetupMock) |
| 15 | + |
| 16 | +import { listWorkspaceFiles } from '@/lib/uploads/contexts/workspace/file-manager' |
| 17 | +import { WORKSPACE_FILE_SEED_MAX } from '@/app/workspace/[workspaceId]/lib/seed-workspace-files' |
| 18 | + |
| 19 | +const ownerId = generateId() |
| 20 | +const workspaceId = generateId() |
| 21 | +const otherWorkspaceId = generateId() |
| 22 | +const tick = (seconds: number) => new Date(Date.UTC(2026, 0, 1, 0, 0, seconds)) |
| 23 | + |
| 24 | +type FileRow = typeof workspaceFiles.$inferInsert |
| 25 | + |
| 26 | +function fileRow(name: string, values: Partial<FileRow> = {}): FileRow { |
| 27 | + const id = `${name}-${workspaceId}` |
| 28 | + return { |
| 29 | + id, |
| 30 | + key: `workspace/${workspaceId}/${id}`, |
| 31 | + userId: ownerId, |
| 32 | + workspaceId, |
| 33 | + context: 'workspace', |
| 34 | + originalName: `${name}.txt`, |
| 35 | + contentType: 'text/plain', |
| 36 | + sizeBytes: 1, |
| 37 | + ...values, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +describe('workspace file list in PostgreSQL', () => { |
| 42 | + /** |
| 43 | + * Rows inserted, and deleted, out of id order, with uploaded_at ties inside each scope and |
| 44 | + * across them, so an order that is not (uploaded_at, id) shows up as a reordered list. |
| 45 | + */ |
| 46 | + const live = [ |
| 47 | + fileRow('live-d', { uploadedAt: tick(1) }), |
| 48 | + fileRow('live-c', { uploadedAt: tick(2) }), |
| 49 | + fileRow('live-b', { uploadedAt: tick(2) }), |
| 50 | + fileRow('live-a', { uploadedAt: tick(2) }), |
| 51 | + fileRow('live-e', { uploadedAt: tick(4) }), |
| 52 | + ] |
| 53 | + const archived = [ |
| 54 | + fileRow('gone-b', { uploadedAt: tick(2), deletedAt: tick(10) }), |
| 55 | + fileRow('gone-c', { uploadedAt: tick(3), deletedAt: tick(11) }), |
| 56 | + fileRow('gone-a', { uploadedAt: tick(3), deletedAt: tick(12) }), |
| 57 | + ] |
| 58 | + const neverListed = [ |
| 59 | + fileRow('chat-upload', { context: 'mothership', uploadedAt: tick(2) }), |
| 60 | + fileRow('execution-output', { context: 'execution', uploadedAt: tick(2) }), |
| 61 | + fileRow('other-workspace', { |
| 62 | + workspaceId: otherWorkspaceId, |
| 63 | + key: `workspace/${otherWorkspaceId}/other`, |
| 64 | + uploadedAt: tick(2), |
| 65 | + }), |
| 66 | + ] |
| 67 | + const byUploadThenId = (rows: FileRow[]) => |
| 68 | + [...rows] |
| 69 | + .sort( |
| 70 | + (a, b) => |
| 71 | + (a.uploadedAt?.getTime() ?? 0) - (b.uploadedAt?.getTime() ?? 0) || |
| 72 | + (a.id < b.id ? -1 : a.id > b.id ? 1 : 0) |
| 73 | + ) |
| 74 | + .map((row) => row.id) |
| 75 | + |
| 76 | + beforeAll(async () => { |
| 77 | + await db.insert(user).values({ |
| 78 | + id: ownerId, |
| 79 | + email: `${ownerId}@file-list.invalid`, |
| 80 | + name: 'File list fixture', |
| 81 | + emailVerified: true, |
| 82 | + createdAt: new Date(), |
| 83 | + updatedAt: new Date(), |
| 84 | + }) |
| 85 | + await insertWorkspaceFixture( |
| 86 | + db, |
| 87 | + [workspaceId, otherWorkspaceId].map((id) => ({ |
| 88 | + id, |
| 89 | + ownerId, |
| 90 | + billedAccountUserId: ownerId, |
| 91 | + name: 'File list fixture', |
| 92 | + })) |
| 93 | + ) |
| 94 | + await db.insert(workspaceFiles).values([...live, ...archived, ...neverListed]) |
| 95 | + }) |
| 96 | + |
| 97 | + afterAll(async () => { |
| 98 | + try { |
| 99 | + await db.delete(workspaceFiles).where(eq(workspaceFiles.userId, ownerId)) |
| 100 | + await deleteWorkspaceFixture(db, inArray(workspace.id, [workspaceId, otherWorkspaceId])) |
| 101 | + await db.delete(user).where(eq(user.id, ownerId)) |
| 102 | + } finally { |
| 103 | + await db.$client.end() |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + it.each([ |
| 108 | + { scope: 'active', label: 'live files', expected: byUploadThenId(live) }, |
| 109 | + { scope: 'archived', label: 'Recently Deleted files', expected: byUploadThenId(archived) }, |
| 110 | + { |
| 111 | + scope: 'all', |
| 112 | + label: 'live and deleted files together', |
| 113 | + expected: byUploadThenId([...live, ...archived]), |
| 114 | + }, |
| 115 | + ] as const)('lists $label by upload time, ties by id', async ({ scope, expected }) => { |
| 116 | + const files = await listWorkspaceFiles(workspaceId, { scope, throwOnError: true }) |
| 117 | + expect(files.map((file) => file.id)).toEqual(expected) |
| 118 | + }) |
| 119 | + |
| 120 | + /** |
| 121 | + * With every sorting plan ruled out, an ordered plan exists only if an index's predicate is |
| 122 | + * implied by the list's filter and its key gives the list's order. |
| 123 | + */ |
| 124 | + it('reads the live list, whole or bounded, in order from its workspace index', async () => { |
| 125 | + for (const limit of [undefined, WORKSPACE_FILE_SEED_MAX + 1]) { |
| 126 | + const nodes = await explainCaptured( |
| 127 | + db, |
| 128 | + () => listWorkspaceFiles(workspaceId, { limit, throwOnError: true }), |
| 129 | + { match: /from "workspace_files"/, gucs: { enable_sort: 'off' } } |
| 130 | + ) |
| 131 | + expect(nodes.map((node) => node['Node Type'])).not.toContain('Sort') |
| 132 | + expect(nodes.map((node) => node['Index Name'])).toContain( |
| 133 | + 'workspace_files_workspace_active_uploaded_idx' |
| 134 | + ) |
| 135 | + } |
| 136 | + }) |
| 137 | +}) |
0 commit comments