diff --git a/apps/sim/executor/execution/condition-join-loop.test.ts b/apps/sim/executor/execution/condition-join-loop.test.ts index 2552774177d..19470e0d6da 100644 --- a/apps/sim/executor/execution/condition-join-loop.test.ts +++ b/apps/sim/executor/execution/condition-join-loop.test.ts @@ -3,6 +3,7 @@ import { createSerializedBlock, createSerializedWorkflow, } from '@sim/testing/factories/serialized-block.factory' +import { isolatedVmMock } from '@sim/testing/mocks/isolated-vm.mock' import { toolsMock, toolsMockFns } from '@sim/testing/mocks/tools.mock' import { beforeEach, describe, expect, it, vi } from 'vitest' import { BlockType } from '@/executor/constants' @@ -12,6 +13,9 @@ import type { SerializedWorkflow } from '@/serializer/types' vi.mock('@/tools', () => toolsMock) +/** The loop's continuation check runs as plain JavaScript instead of through the Redis-leased isolate. */ +vi.mock('@/lib/execution/isolated-vm', () => isolatedVmMock) + /** Real condition handler; every other block answers with its own name. */ vi.mock('@/executor/handlers/registry', () => ({ createBlockHandlers: () => [ diff --git a/apps/sim/executor/execution/condition-loop-ceiling.test.ts b/apps/sim/executor/execution/condition-loop-ceiling.test.ts index 5b2a175a47e..12017ae3b12 100644 --- a/apps/sim/executor/execution/condition-loop-ceiling.test.ts +++ b/apps/sim/executor/execution/condition-loop-ceiling.test.ts @@ -3,6 +3,7 @@ import { createSerializedBlock, createSerializedWorkflow, } from '@sim/testing/factories/serialized-block.factory' +import { isolatedVmMock, isolatedVmMockFns } from '@sim/testing/mocks/isolated-vm.mock' import { beforeEach, describe, expect, it, vi } from 'vitest' import { BlockType, DEFAULTS } from '@/executor/constants' import { DAGExecutor } from '@/executor/execution/executor' @@ -17,14 +18,7 @@ const { bodyRuns, conditionResult, runaway } = vi.hoisted(() => ({ runaway: { controller: new AbortController(), limit: 30_000 }, })) -/** Count conditions resolve to `return Boolean( < )`; user conditions stay opaque. */ -vi.mock('@/lib/execution/isolated-vm', () => ({ - executeInIsolatedVM: vi.fn(async ({ code }: { code: string }) => { - const indexCheck = /^return Boolean\((\d+) < (\d+)\)$/.exec(code) - if (indexCheck) return { result: Number(indexCheck[1]) < Number(indexCheck[2]) } - return { result: bodyRuns.count < conditionResult.stopAfter } - }), -})) +vi.mock('@/lib/execution/isolated-vm', () => isolatedVmMock) vi.mock('@/executor/handlers/registry', () => ({ createBlockHandlers: () => [ @@ -80,6 +74,12 @@ describe('condition-driven loop iteration ceiling', () => { bodyRuns.count = 0 conditionResult.stopAfter = Number.POSITIVE_INFINITY runaway.controller = new AbortController() + /** Count conditions resolve to `return Boolean( < )`; user conditions stay opaque. */ + isolatedVmMockFns.mockExecuteInIsolatedVM.mockImplementation(async ({ code }) => { + const indexCheck = /^return Boolean\((\d+) < (\d+)\)$/.exec(code) + if (indexCheck) return { result: Number(indexCheck[1]) < Number(indexCheck[2]) } + return { result: bodyRuns.count < conditionResult.stopAfter } + }) }) it.each([ diff --git a/apps/sim/executor/execution/test-hooks.test.ts b/apps/sim/executor/execution/test-hooks.test.ts index ec68532c0c5..2d878a4faa9 100644 --- a/apps/sim/executor/execution/test-hooks.test.ts +++ b/apps/sim/executor/execution/test-hooks.test.ts @@ -1,5 +1,5 @@ import type { SessionPrincipal } from '@sim/auth/principal' -import { createSerializedBlock, createSerializedWorkflow } from '@sim/testing' +import { createSerializedBlock, createSerializedWorkflow, isolatedVmMock } from '@sim/testing' import { beforeEach, describe, expect, it, vi } from 'vitest' import { createMockChannel, @@ -18,13 +18,7 @@ const { executed, inputsSeen } = vi.hoisted(() => ({ })) /** A For loop's continuation check is `return Boolean( < )`; nothing here needs a real isolate. */ -vi.mock('@/lib/execution/isolated-vm', () => ({ - executeInIsolatedVM: vi.fn(async ({ code }: { code: string }) => { - const indexCheck = /^return Boolean\((\d+) < (\d+)\)$/.exec(code) - if (!indexCheck) throw new Error(`Unexpected isolated-vm code in test: ${code}`) - return { result: Number(indexCheck[1]) < Number(indexCheck[2]) } - }), -})) +vi.mock('@/lib/execution/isolated-vm', () => isolatedVmMock) vi.mock('@/executor/handlers/registry', () => ({ createBlockHandlers: () => [ diff --git a/apps/sim/executor/orchestrators/loop.test.ts b/apps/sim/executor/orchestrators/loop.test.ts index 5796776d209..a30dd7d0341 100644 --- a/apps/sim/executor/orchestrators/loop.test.ts +++ b/apps/sim/executor/orchestrators/loop.test.ts @@ -1,4 +1,5 @@ import { createLogger } from '@sim/logger' +import { isolatedVmMock, isolatedVmMockFns } from '@sim/testing/mocks/isolated-vm.mock' import { storageServiceMockFns } from '@sim/testing/mocks/storage-service.mock' import { uploadsMock } from '@sim/testing/mocks/uploads.mock' import { beforeEach, describe, expect, it, vi } from 'vitest' @@ -13,17 +14,13 @@ import { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-tr const mockUploadFile = storageServiceMockFns.mockUploadFile -const { mockExecuteInIsolatedVM } = vi.hoisted(() => ({ - mockExecuteInIsolatedVM: vi.fn(), -})) +const mockExecuteInIsolatedVM = isolatedVmMockFns.mockExecuteInIsolatedVM const mockLogger = vi.mocked(createLogger).mock.results[ vi.mocked(createLogger).mock.calls.findIndex(([name]) => name === 'LoopOrchestrator') ].value -vi.mock('@/lib/execution/isolated-vm', () => ({ - executeInIsolatedVM: mockExecuteInIsolatedVM, -})) +vi.mock('@/lib/execution/isolated-vm', () => isolatedVmMock) vi.mock('@/lib/uploads', () => uploadsMock) diff --git a/apps/sim/lib/function-execution/execute-request.test.ts b/apps/sim/lib/function-execution/execute-request.test.ts index 3ce4f43f4c7..67e4ef06db5 100644 --- a/apps/sim/lib/function-execution/execute-request.test.ts +++ b/apps/sim/lib/function-execution/execute-request.test.ts @@ -11,6 +11,7 @@ import { workflowsUtilsMock, } from '@sim/testing' import { encryptionMock, encryptionMockFns } from '@sim/testing/mocks/encryption.mock' +import { isolatedVmMock, isolatedVmMockFns } from '@sim/testing/mocks/isolated-vm.mock' import { remoteSandboxMock, remoteSandboxMockFns } from '@sim/testing/mocks/remote-sandbox.mock' import { storageServiceMockFns } from '@sim/testing/mocks/storage-service.mock' import { uploadsMock } from '@sim/testing/mocks/uploads.mock' @@ -41,8 +42,9 @@ import { } from '@/lib/execution/remote-sandbox/output-limits' import { SandboxExecutionTimeoutError } from '@/lib/execution/remote-sandbox/timeout' +const mockExecuteInIsolatedVM = isolatedVmMockFns.mockExecuteInIsolatedVM + const { - mockExecuteInIsolatedVM, mockValidateWorkspaceFileWriteTarget, mockWriteWorkspaceFileByPath, mockUploadExecutionFile, @@ -50,7 +52,6 @@ const { mockUnprovenancedMountCount, mockRenderedMountContributors, } = vi.hoisted(() => ({ - mockExecuteInIsolatedVM: vi.fn(), mockValidateWorkspaceFileWriteTarget: vi.fn(), mockWriteWorkspaceFileByPath: vi.fn(), mockUploadExecutionFile: vi.fn(), @@ -61,9 +62,7 @@ const { vi.mock('@/lib/core/security/encryption', () => encryptionMock) -vi.mock('@/lib/execution/isolated-vm', () => ({ - executeInIsolatedVM: mockExecuteInIsolatedVM, -})) +vi.mock('@/lib/execution/isolated-vm', () => isolatedVmMock) vi.mock('@/lib/execution/remote-sandbox', () => remoteSandboxMock) diff --git a/packages/testing/src/mocks/index.ts b/packages/testing/src/mocks/index.ts index 2d1cfc32285..ca9f765cbd5 100644 --- a/packages/testing/src/mocks/index.ts +++ b/packages/testing/src/mocks/index.ts @@ -408,6 +408,8 @@ export { MockConflictingPendingInvitationError, MockGrantlessInvitationError, } from './invitations-send.mock' +// Isolated VM mocks (for @/lib/execution/isolated-vm) +export { isolatedVmMock, isolatedVmMockFns } from './isolated-vm.mock' export { kbConnectorsQueriesMock, kbConnectorsQueriesMockFns, diff --git a/packages/testing/src/mocks/isolated-vm.mock.ts b/packages/testing/src/mocks/isolated-vm.mock.ts new file mode 100644 index 00000000000..330a33c1b9b --- /dev/null +++ b/packages/testing/src/mocks/isolated-vm.mock.ts @@ -0,0 +1,38 @@ +import { vi } from 'vitest' + +/** + * Runs the request's code as plain JavaScript in the test process. The real + * `executeInIsolatedVM` leases an isolate through Redis, which unit tests do not have; for the + * executor's own checks (a loop's `return Boolean( < )` continuation, a condition's + * expression) plain evaluation gives the same answer. + */ +async function runAsPlainJavaScript({ code }: { code: string }): Promise<{ result: unknown }> { + return { result: new Function(code)() } +} + +/** + * Controllable mock functions for `@/lib/execution/isolated-vm`. `mockExecuteInIsolatedVM` + * defaults to plain evaluation; a test that drives isolate results itself overrides it. It is a + * bare `vi.fn()` given that default, like `toolsMockFns.mockExecuteTool`, so tests read its calls + * without restating the request type. + * + * @example + * ```ts + * isolatedVmMockFns.mockExecuteInIsolatedVM.mockResolvedValueOnce({ result: false }) + * ``` + */ +export const isolatedVmMockFns = { + mockExecuteInIsolatedVM: vi.fn().mockImplementation(runAsPlainJavaScript), +} + +/** + * Static mock module for `@/lib/execution/isolated-vm`. + * + * @example + * ```ts + * vi.mock('@/lib/execution/isolated-vm', () => isolatedVmMock) + * ``` + */ +export const isolatedVmMock = { + executeInIsolatedVM: isolatedVmMockFns.mockExecuteInIsolatedVM, +}