|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { |
| 7 | + mockIsFeatureEnabled, |
| 8 | + mockGetTableById, |
| 9 | + mockListTables, |
| 10 | + mockQueryRows, |
| 11 | + mockGetOrCreateTableSnapshot, |
| 12 | + mockDownloadFile, |
| 13 | + mockGeneratePresignedDownloadUrl, |
| 14 | + mockHasCloudStorage, |
| 15 | + mockExecuteTool, |
| 16 | +} = vi.hoisted(() => ({ |
| 17 | + mockIsFeatureEnabled: vi.fn(), |
| 18 | + mockGetTableById: vi.fn(), |
| 19 | + mockListTables: vi.fn(), |
| 20 | + mockQueryRows: vi.fn(), |
| 21 | + mockGetOrCreateTableSnapshot: vi.fn(), |
| 22 | + mockDownloadFile: vi.fn(), |
| 23 | + mockGeneratePresignedDownloadUrl: vi.fn(), |
| 24 | + mockHasCloudStorage: vi.fn(), |
| 25 | + mockExecuteTool: vi.fn(), |
| 26 | +})) |
| 27 | + |
| 28 | +vi.mock('@/lib/core/config/feature-flags', () => ({ isFeatureEnabled: mockIsFeatureEnabled })) |
| 29 | +vi.mock('@/lib/table/service', () => ({ |
| 30 | + getTableById: mockGetTableById, |
| 31 | + listTables: mockListTables, |
| 32 | +})) |
| 33 | +vi.mock('@/lib/table/rows/service', () => ({ queryRows: mockQueryRows })) |
| 34 | +vi.mock('@/lib/table/snapshot-cache', () => ({ |
| 35 | + getOrCreateTableSnapshot: mockGetOrCreateTableSnapshot, |
| 36 | + SNAPSHOT_MAX_BYTES: 500 * 1024 * 1024, |
| 37 | +})) |
| 38 | +vi.mock('@/lib/uploads/core/storage-service', () => ({ |
| 39 | + downloadFile: mockDownloadFile, |
| 40 | + generatePresignedDownloadUrl: mockGeneratePresignedDownloadUrl, |
| 41 | + hasCloudStorage: mockHasCloudStorage, |
| 42 | +})) |
| 43 | +vi.mock('@/tools', () => ({ executeTool: mockExecuteTool })) |
| 44 | +// Workspace-file + VFS surfaces are unused on the tables-only path; stub to avoid heavy loads. |
| 45 | +vi.mock('@/lib/uploads/contexts/workspace/workspace-file-manager', () => ({ |
| 46 | + fetchWorkspaceFileBuffer: vi.fn(), |
| 47 | + findWorkspaceFileRecord: vi.fn(), |
| 48 | + getSandboxWorkspaceFilePath: vi.fn(), |
| 49 | + listWorkspaceFiles: vi.fn(), |
| 50 | +})) |
| 51 | +vi.mock('@/lib/uploads/contexts/workspace/workspace-file-folder-manager', () => ({ |
| 52 | + listWorkspaceFileFolders: vi.fn(), |
| 53 | +})) |
| 54 | +vi.mock('@/lib/copilot/vfs/path-utils', () => ({ |
| 55 | + decodeVfsPathSegments: (p: string) => p.split('/'), |
| 56 | + encodeVfsPathSegments: (s: string[]) => s.join('/'), |
| 57 | +})) |
| 58 | +vi.mock('@/lib/copilot/vfs/workflow-alias-resolver', () => ({ |
| 59 | + resolveWorkflowAliasForWorkspace: vi.fn().mockResolvedValue(null), |
| 60 | +})) |
| 61 | +vi.mock('@/lib/copilot/vfs/workflow-aliases', () => ({ |
| 62 | + isPlanAliasPath: () => false, |
| 63 | + workflowAliasSandboxPath: (p: string) => p, |
| 64 | +})) |
| 65 | + |
| 66 | +import { executeFunctionExecute } from '@/lib/copilot/tools/handlers/function-execute' |
| 67 | + |
| 68 | +const table = { |
| 69 | + id: 'tbl_1', |
| 70 | + workspaceId: 'ws_1', |
| 71 | + rowCount: 1000, |
| 72 | + schema: { columns: [{ id: 'col_name', name: 'name', type: 'string' }] }, |
| 73 | +} |
| 74 | + |
| 75 | +const context = { workspaceId: 'ws_1', userId: 'u1' } |
| 76 | + |
| 77 | +function mountedFiles() { |
| 78 | + const params = mockExecuteTool.mock.calls[0][1] as { |
| 79 | + _sandboxFiles?: Array<{ path: string; type?: string; content?: string; url?: string }> |
| 80 | + } |
| 81 | + return params._sandboxFiles ?? [] |
| 82 | +} |
| 83 | + |
| 84 | +const snapshotCacheOn = (flag: string) => Promise.resolve(flag === 'table-snapshot-cache') |
| 85 | + |
| 86 | +describe('executeFunctionExecute table mounts', () => { |
| 87 | + beforeEach(() => { |
| 88 | + vi.clearAllMocks() |
| 89 | + mockExecuteTool.mockResolvedValue({ success: true }) |
| 90 | + mockGetTableById.mockResolvedValue(table) |
| 91 | + mockIsFeatureEnabled.mockResolvedValue(false) |
| 92 | + mockQueryRows.mockResolvedValue({ rows: [{ data: { name: 'Ada' } }] }) |
| 93 | + mockHasCloudStorage.mockReturnValue(true) |
| 94 | + mockGeneratePresignedDownloadUrl.mockResolvedValue('https://s3.example/presigned?sig=abc') |
| 95 | + }) |
| 96 | + |
| 97 | + it('flag OFF: drains the table inline via queryRows (existing path)', async () => { |
| 98 | + await executeFunctionExecute({ inputTables: ['tbl_1'] }, context as never) |
| 99 | + |
| 100 | + expect(mockQueryRows).toHaveBeenCalledTimes(1) |
| 101 | + expect(mockGetOrCreateTableSnapshot).not.toHaveBeenCalled() |
| 102 | + const files = mountedFiles() |
| 103 | + expect(files[0].path).toBe('/home/user/tables/tbl_1.csv') |
| 104 | + expect(files[0].content).toBe('name\nAda') |
| 105 | + }) |
| 106 | + |
| 107 | + it('flag ON + cloud storage: mounts by presigned URL, no bytes through web', async () => { |
| 108 | + mockIsFeatureEnabled.mockImplementation(snapshotCacheOn) |
| 109 | + mockGetOrCreateTableSnapshot.mockResolvedValue({ |
| 110 | + key: 'table-snapshots/ws_1/tbl_1/v5.csv', |
| 111 | + size: 9, |
| 112 | + version: 5, |
| 113 | + }) |
| 114 | + |
| 115 | + await executeFunctionExecute({ inputTables: ['tbl_1'] }, context as never) |
| 116 | + |
| 117 | + expect(mockGetOrCreateTableSnapshot).toHaveBeenCalledTimes(1) |
| 118 | + expect(mockQueryRows).not.toHaveBeenCalled() |
| 119 | + expect(mockDownloadFile).not.toHaveBeenCalled() |
| 120 | + expect(mockGeneratePresignedDownloadUrl).toHaveBeenCalledWith( |
| 121 | + 'table-snapshots/ws_1/tbl_1/v5.csv', |
| 122 | + 'execution', |
| 123 | + expect.any(Number) |
| 124 | + ) |
| 125 | + expect(mountedFiles()[0]).toEqual({ |
| 126 | + type: 'url', |
| 127 | + path: '/home/user/tables/tbl_1.csv', |
| 128 | + url: 'https://s3.example/presigned?sig=abc', |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + it('flag ON + local storage: falls back to a buffered content mount', async () => { |
| 133 | + mockIsFeatureEnabled.mockImplementation(snapshotCacheOn) |
| 134 | + mockHasCloudStorage.mockReturnValue(false) |
| 135 | + mockGetOrCreateTableSnapshot.mockResolvedValue({ |
| 136 | + key: 'table-snapshots/ws_1/tbl_1/v5.csv', |
| 137 | + size: 9, |
| 138 | + version: 5, |
| 139 | + }) |
| 140 | + mockDownloadFile.mockResolvedValue(Buffer.from('name\nAda\n')) |
| 141 | + |
| 142 | + await executeFunctionExecute({ inputTables: ['tbl_1'] }, context as never) |
| 143 | + |
| 144 | + expect(mockGeneratePresignedDownloadUrl).not.toHaveBeenCalled() |
| 145 | + expect(mockDownloadFile).toHaveBeenCalledWith( |
| 146 | + expect.objectContaining({ key: 'table-snapshots/ws_1/tbl_1/v5.csv', context: 'execution' }) |
| 147 | + ) |
| 148 | + const file = mountedFiles()[0] |
| 149 | + expect(file.path).toBe('/home/user/tables/tbl_1.csv') |
| 150 | + expect(file.content).toBe('name\nAda\n') |
| 151 | + expect(file.type).toBeUndefined() |
| 152 | + }) |
| 153 | + |
| 154 | + it('flag ON but small table stays on the inline path', async () => { |
| 155 | + mockIsFeatureEnabled.mockImplementation(snapshotCacheOn) |
| 156 | + mockGetTableById.mockResolvedValue({ ...table, rowCount: 10 }) |
| 157 | + |
| 158 | + await executeFunctionExecute({ inputTables: ['tbl_1'] }, context as never) |
| 159 | + |
| 160 | + expect(mockGetOrCreateTableSnapshot).not.toHaveBeenCalled() |
| 161 | + expect(mockQueryRows).toHaveBeenCalledTimes(1) |
| 162 | + }) |
| 163 | + |
| 164 | + it('flag ON + cloud: throws when the snapshot exceeds the table mount limit', async () => { |
| 165 | + mockIsFeatureEnabled.mockImplementation(snapshotCacheOn) |
| 166 | + mockGetOrCreateTableSnapshot.mockResolvedValue({ |
| 167 | + key: 'table-snapshots/ws_1/tbl_1/v5.csv', |
| 168 | + size: 600 * 1024 * 1024, |
| 169 | + version: 5, |
| 170 | + }) |
| 171 | + |
| 172 | + await expect( |
| 173 | + executeFunctionExecute({ inputTables: ['tbl_1'] }, context as never) |
| 174 | + ).rejects.toThrow(/table mount limit/) |
| 175 | + expect(mockGeneratePresignedDownloadUrl).not.toHaveBeenCalled() |
| 176 | + }) |
| 177 | + |
| 178 | + it('flag ON + local: throws when the snapshot exceeds the per-file mount limit', async () => { |
| 179 | + mockIsFeatureEnabled.mockImplementation(snapshotCacheOn) |
| 180 | + mockHasCloudStorage.mockReturnValue(false) |
| 181 | + mockGetOrCreateTableSnapshot.mockResolvedValue({ |
| 182 | + key: 'table-snapshots/ws_1/tbl_1/v5.csv', |
| 183 | + size: 20 * 1024 * 1024, |
| 184 | + version: 5, |
| 185 | + }) |
| 186 | + |
| 187 | + await expect( |
| 188 | + executeFunctionExecute({ inputTables: ['tbl_1'] }, context as never) |
| 189 | + ).rejects.toThrow(/per-file mount limit/) |
| 190 | + expect(mockDownloadFile).not.toHaveBeenCalled() |
| 191 | + }) |
| 192 | + |
| 193 | + it('rejects a table that belongs to another workspace (tenant isolation)', async () => { |
| 194 | + mockGetTableById.mockResolvedValue({ ...table, workspaceId: 'ws_2' }) |
| 195 | + |
| 196 | + await expect( |
| 197 | + executeFunctionExecute({ inputTables: ['tbl_1'] }, context as never) |
| 198 | + ).rejects.toThrow(/Input table not found/) |
| 199 | + expect(mockGetOrCreateTableSnapshot).not.toHaveBeenCalled() |
| 200 | + }) |
| 201 | +}) |
0 commit comments