|
| 1 | +/** Real multi-file Function exports admitting overwrite targets exactly as the writer upserts them. */ |
| 2 | +import { mkdtempSync } from 'node:fs' |
| 3 | +import { rm } from 'node:fs/promises' |
| 4 | +import { tmpdir } from 'node:os' |
| 5 | +import path from 'node:path' |
| 6 | +import { db } from '@sim/db' |
| 7 | +import { auditLog, organization, user, workspace, workspaceFiles } from '@sim/db/schema' |
| 8 | +import { deleteWorkspaceFixture } from '@sim/db/testing/workspace-fixtures' |
| 9 | +import { remoteSandboxMock, remoteSandboxMockFns } from '@sim/testing/mocks/remote-sandbox.mock' |
| 10 | +import { generateId } from '@sim/utils/id' |
| 11 | +import { and, eq, inArray, isNull } from 'drizzle-orm' |
| 12 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' |
| 13 | + |
| 14 | +const fixtureStorage = vi.hoisted(() => ({ root: '' })) |
| 15 | +const sandboxEnvironment = vi.hoisted(() => { |
| 16 | + const fixture = { |
| 17 | + SANDBOX_PROVIDER: 'e2b', |
| 18 | + E2B_ENABLED: 'true', |
| 19 | + E2B_API_KEY: 'integration-provider-fixture-not-a-real-key', |
| 20 | + E2B_FUNCTION_TEMPLATE_ID: 'fixture:11111111-1111-4111-8111-111111111111', |
| 21 | + E2B_FUNCTION_TEMPLATE_GENERATION: '1785792000000', |
| 22 | + } |
| 23 | + const previous = Object.entries(fixture).map(([key]) => [key, process.env[key]] as const) |
| 24 | + Object.assign(process.env, fixture) |
| 25 | + return previous |
| 26 | +}) |
| 27 | +vi.mock('@/lib/execution/remote-sandbox', () => remoteSandboxMock) |
| 28 | +vi.mock('@/lib/uploads/core/setup.server', () => ({ |
| 29 | + get UPLOAD_DIR_SERVER() { |
| 30 | + return fixtureStorage.root |
| 31 | + }, |
| 32 | +})) |
| 33 | + |
| 34 | +import { functionExecuteBodySchema } from '@/lib/api/contracts' |
| 35 | +import { executeFunctionRequest } from '@/lib/function-execution/execute-request' |
| 36 | +import { |
| 37 | + createKnowledgeAclFixtureIds, |
| 38 | + seedKnowledgeAclFixture, |
| 39 | +} from '@/lib/knowledge/__integration__/seed-source-access-fixture' |
| 40 | +import { createWorkspaceFileFolder } from '@/lib/uploads/contexts/workspace/file-folder-manager' |
| 41 | +import { uploadWorkspaceFile } from '@/lib/uploads/contexts/workspace/file-manager' |
| 42 | +import { createWorkspaceFileDelegatedPrincipal } from '@/lib/workspace-files/application/delegated-principal' |
| 43 | +import { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry' |
| 44 | + |
| 45 | +const fixtures: ReturnType<typeof createKnowledgeAclFixtureIds>[] = [] |
| 46 | + |
| 47 | +beforeAll(() => { |
| 48 | + fixtureStorage.root = mkdtempSync(path.join(tmpdir(), 'sim-function-overwrite-export-')) |
| 49 | +}) |
| 50 | + |
| 51 | +afterAll(async () => { |
| 52 | + try { |
| 53 | + for (const ids of fixtures) { |
| 54 | + await db.delete(auditLog).where(eq(auditLog.workspaceId, ids.workspaceId)) |
| 55 | + await deleteWorkspaceFixture(db, eq(workspace.id, ids.workspaceId)) |
| 56 | + await db.delete(organization).where(eq(organization.id, ids.organizationId)) |
| 57 | + await db.delete(user).where(inArray(user.id, [ids.aliceId, ids.bobId])) |
| 58 | + } |
| 59 | + await rm(fixtureStorage.root, { recursive: true, force: true }) |
| 60 | + } finally { |
| 61 | + for (const [key, value] of sandboxEnvironment) { |
| 62 | + if (value === undefined) Reflect.deleteProperty(process.env, key) |
| 63 | + else process.env[key] = value |
| 64 | + } |
| 65 | + } |
| 66 | +}) |
| 67 | + |
| 68 | +/** A workspace whose admin owns an empty `Reports` folder. */ |
| 69 | +async function seed() { |
| 70 | + const ids = createKnowledgeAclFixtureIds() |
| 71 | + fixtures.push(ids) |
| 72 | + await seedKnowledgeAclFixture(ids) |
| 73 | + const folder = await createWorkspaceFileFolder({ |
| 74 | + workspaceId: ids.workspaceId, |
| 75 | + userId: ids.aliceId, |
| 76 | + name: 'Reports', |
| 77 | + }) |
| 78 | + return { ...ids, folderId: folder.id, workflowId: generateId(), executionId: generateId() } |
| 79 | +} |
| 80 | + |
| 81 | +type Fixture = Awaited<ReturnType<typeof seed>> |
| 82 | + |
| 83 | +async function exportTwoFiles(ids: Fixture, overwritePath: string) { |
| 84 | + remoteSandboxMockFns.mockExecuteInSandbox.mockResolvedValueOnce({ |
| 85 | + result: 'done', |
| 86 | + stdout: '', |
| 87 | + sandboxId: 'fixture', |
| 88 | + exportedFiles: { |
| 89 | + '/home/user/summary.csv': 'name,total\nnew,2\n', |
| 90 | + '/home/user/notes.txt': 'fresh notes', |
| 91 | + }, |
| 92 | + }) |
| 93 | + return executeFunctionRequest( |
| 94 | + { headers: new Headers(), signal: AbortSignal.timeout(15_000) }, |
| 95 | + functionExecuteBodySchema.parse({ |
| 96 | + workspaceId: ids.workspaceId, |
| 97 | + workflowId: ids.workflowId, |
| 98 | + executionId: ids.executionId, |
| 99 | + language: 'python', |
| 100 | + code: 'print("done")', |
| 101 | + outputs: { |
| 102 | + files: [ |
| 103 | + { path: overwritePath, mode: 'overwrite', sandboxPath: '/home/user/summary.csv' }, |
| 104 | + { path: 'files/Reports/notes.txt', sandboxPath: '/home/user/notes.txt' }, |
| 105 | + ], |
| 106 | + }, |
| 107 | + }), |
| 108 | + { |
| 109 | + attributedUserId: ids.aliceId, |
| 110 | + fileAccessUserId: ids.aliceId, |
| 111 | + principal: createWorkspaceFileDelegatedPrincipal({ |
| 112 | + serviceId: 'executor', |
| 113 | + subjectUserId: ids.aliceId, |
| 114 | + workspaceId: ids.workspaceId, |
| 115 | + delegationId: generateId(), |
| 116 | + executionId: ids.executionId, |
| 117 | + }), |
| 118 | + resolvedSecretTraceRegistry: new ResolvedSecretTraceRegistry([], { |
| 119 | + userId: ids.aliceId, |
| 120 | + workspaceId: ids.workspaceId, |
| 121 | + }), |
| 122 | + } |
| 123 | + ) |
| 124 | +} |
| 125 | + |
| 126 | +function liveFilesInFolder(ids: Fixture) { |
| 127 | + return db |
| 128 | + .select({ |
| 129 | + id: workspaceFiles.id, |
| 130 | + name: workspaceFiles.originalName, |
| 131 | + size: workspaceFiles.sizeBytes, |
| 132 | + }) |
| 133 | + .from(workspaceFiles) |
| 134 | + .where( |
| 135 | + and( |
| 136 | + eq(workspaceFiles.workspaceId, ids.workspaceId), |
| 137 | + eq(workspaceFiles.folderId, ids.folderId), |
| 138 | + isNull(workspaceFiles.deletedAt) |
| 139 | + ) |
| 140 | + ) |
| 141 | +} |
| 142 | + |
| 143 | +describe('multi-file Function export with overwrite targets', () => { |
| 144 | + it('creates a missing overwrite target instead of failing after the code ran', async () => { |
| 145 | + const ids = await seed() |
| 146 | + |
| 147 | + const response = await exportTwoFiles(ids, 'files/Reports/summary.csv') |
| 148 | + const body = await response.json() |
| 149 | + |
| 150 | + expect(response.status, JSON.stringify(body)).toBe(200) |
| 151 | + expect(body.success).toBe(true) |
| 152 | + const files = await liveFilesInFolder(ids) |
| 153 | + expect(files.map((file) => file.name).sort()).toEqual(['notes.txt', 'summary.csv']) |
| 154 | + expect(files.find((file) => file.name === 'summary.csv')?.size).toBe( |
| 155 | + Buffer.byteLength('name,total\nnew,2\n') |
| 156 | + ) |
| 157 | + }) |
| 158 | + |
| 159 | + it('still replaces an existing overwrite target in place', async () => { |
| 160 | + const ids = await seed() |
| 161 | + const existing = await uploadWorkspaceFile( |
| 162 | + ids.workspaceId, |
| 163 | + ids.aliceId, |
| 164 | + Buffer.from('name,total\nold,1\nolder,0\n'), |
| 165 | + 'summary.csv', |
| 166 | + 'text/csv', |
| 167 | + { folderId: ids.folderId, exactName: true, notifyWorkspaceChange: false } |
| 168 | + ) |
| 169 | + |
| 170 | + const response = await exportTwoFiles(ids, 'files/Reports/summary.csv') |
| 171 | + const body = await response.json() |
| 172 | + |
| 173 | + expect(response.status, JSON.stringify(body)).toBe(200) |
| 174 | + const files = await liveFilesInFolder(ids) |
| 175 | + expect(files.map((file) => file.name).sort()).toEqual(['notes.txt', 'summary.csv']) |
| 176 | + expect(files.find((file) => file.name === 'summary.csv')).toEqual({ |
| 177 | + id: existing.id, |
| 178 | + name: 'summary.csv', |
| 179 | + size: Buffer.byteLength('name,total\nnew,2\n'), |
| 180 | + }) |
| 181 | + }) |
| 182 | +}) |
0 commit comments