From 0103662c6c14d96d0868982f2011cf7e5f3fdb5c Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Fri, 20 Nov 2026 20:24:39 -0800 Subject: [PATCH 1/2] fix(executor): keep the condition-join loop test off the Redis-backed isolate lease --- apps/sim/executor/execution/condition-join-loop.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/apps/sim/executor/execution/condition-join-loop.test.ts b/apps/sim/executor/execution/condition-join-loop.test.ts index 2552774177d..a44f491ecb2 100644 --- a/apps/sim/executor/execution/condition-join-loop.test.ts +++ b/apps/sim/executor/execution/condition-join-loop.test.ts @@ -12,6 +12,15 @@ import type { SerializedWorkflow } from '@/serializer/types' vi.mock('@/tools', () => toolsMock) +/** 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]) } + }), +})) + /** Real condition handler; every other block answers with its own name. */ vi.mock('@/executor/handlers/registry', () => ({ createBlockHandlers: () => [ From 145e878b5a07cbf42ec15656076abbf941976fd8 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Fri, 20 Nov 2026 20:42:08 -0800 Subject: [PATCH 2/2] improvement(testing): share one isolated-vm mock across executor tests --- .../execution/condition-join-loop.test.ts | 10 +--- .../execution/condition-loop-ceiling.test.ts | 18 ++++--- .../sim/executor/execution/test-hooks.test.ts | 10 +--- apps/sim/executor/orchestrators/loop.test.ts | 9 ++-- .../execute-request.test.ts | 9 ++-- packages/testing/src/mocks/index.ts | 7 +++ .../testing/src/mocks/isolated-vm.mock.ts | 54 +++++++++++++++++++ 7 files changed, 82 insertions(+), 35 deletions(-) create mode 100644 packages/testing/src/mocks/isolated-vm.mock.ts diff --git a/apps/sim/executor/execution/condition-join-loop.test.ts b/apps/sim/executor/execution/condition-join-loop.test.ts index a44f491ecb2..196a2ac48cc 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,14 +13,7 @@ import type { SerializedWorkflow } from '@/serializer/types' vi.mock('@/tools', () => toolsMock) -/** 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) /** Real condition handler; every other block answers with its own name. */ vi.mock('@/executor/handlers/registry', () => ({ diff --git a/apps/sim/executor/execution/condition-loop-ceiling.test.ts b/apps/sim/executor/execution/condition-loop-ceiling.test.ts index 5b2a175a47e..59fabad0c57 100644 --- a/apps/sim/executor/execution/condition-loop-ceiling.test.ts +++ b/apps/sim/executor/execution/condition-loop-ceiling.test.ts @@ -3,6 +3,11 @@ import { createSerializedBlock, createSerializedWorkflow, } from '@sim/testing/factories/serialized-block.factory' +import { + evaluateLoopIndexCheck, + 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 +22,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 +78,10 @@ describe('condition-driven loop iteration ceiling', () => { bodyRuns.count = 0 conditionResult.stopAfter = Number.POSITIVE_INFINITY runaway.controller = new AbortController() + /** Count conditions evaluate; user conditions stay opaque and hold until `stopAfter`. */ + isolatedVmMockFns.mockExecuteInIsolatedVM.mockImplementation(async ({ code }) => ({ + result: evaluateLoopIndexCheck(code) ?? 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..dd47387d6ea 100644 --- a/apps/sim/executor/execution/test-hooks.test.ts +++ b/apps/sim/executor/execution/test-hooks.test.ts @@ -1,5 +1,6 @@ import type { SessionPrincipal } from '@sim/auth/principal' import { createSerializedBlock, createSerializedWorkflow } from '@sim/testing' +import { isolatedVmMock } from '@sim/testing/mocks/isolated-vm.mock' import { beforeEach, describe, expect, it, vi } from 'vitest' import { createMockChannel, @@ -17,14 +18,7 @@ const { executed, inputsSeen } = vi.hoisted(() => ({ inputsSeen: [] as Array<{ blockId: string; inputs: Record }>, })) -/** 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..50b80cae62f 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' @@ -42,7 +43,6 @@ import { import { SandboxExecutionTimeoutError } from '@/lib/execution/remote-sandbox/timeout' const { - mockExecuteInIsolatedVM, mockValidateWorkspaceFileWriteTarget, mockWriteWorkspaceFileByPath, mockUploadExecutionFile, @@ -50,7 +50,6 @@ const { mockUnprovenancedMountCount, mockRenderedMountContributors, } = vi.hoisted(() => ({ - mockExecuteInIsolatedVM: vi.fn(), mockValidateWorkspaceFileWriteTarget: vi.fn(), mockWriteWorkspaceFileByPath: vi.fn(), mockUploadExecutionFile: vi.fn(), @@ -61,9 +60,9 @@ const { vi.mock('@/lib/core/security/encryption', () => encryptionMock) -vi.mock('@/lib/execution/isolated-vm', () => ({ - executeInIsolatedVM: mockExecuteInIsolatedVM, -})) +const mockExecuteInIsolatedVM = isolatedVmMockFns.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..44eff70eb5c 100644 --- a/packages/testing/src/mocks/index.ts +++ b/packages/testing/src/mocks/index.ts @@ -330,6 +330,13 @@ export { featureFlagsMock, featureFlagsMockFns, } from './feature-flags.mock' +// Isolated VM mocks (for @/lib/execution/isolated-vm) +export { + evaluateLoopIndexCheck, + isolatedVmMock, + isolatedVmMockFns, + resetIsolatedVmMock, +} from './isolated-vm.mock' // Executor mocks - use side-effect import: import '@sim/testing/mocks/executor' export { 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..b561a7e889e --- /dev/null +++ b/packages/testing/src/mocks/isolated-vm.mock.ts @@ -0,0 +1,54 @@ +import { vi } from 'vitest' + +/** The continuation check the loop orchestrator compiles for count-driven loops. */ +const LOOP_INDEX_CHECK = /^return Boolean\((\d+) < (\d+)\)$/ + +/** + * Evaluates a loop's `return Boolean( < )` continuation check, or returns `null` + * for any other code so a test can decide what that code resolves to. + */ +export function evaluateLoopIndexCheck(code: string): boolean | null { + const match = LOOP_INDEX_CHECK.exec(code) + return match ? Number(match[1]) < Number(match[2]) : null +} + +async function executeLoopIndexCheck({ code }: { code: string }) { + const result = evaluateLoopIndexCheck(code) + if (result === null) throw new Error(`Unexpected isolated-vm code in test: ${code}`) + return { result } +} + +/** + * Controllable mock functions for `@/lib/execution/isolated-vm`. Executor tests mock it because + * the real sandbox acquires a Redis-backed lease, which the shared Redis mock answers as + * over-limit. By default only loop continuation checks evaluate; any other code throws, so a + * test that needs more must say what it resolves to. + * + * @example + * ```ts + * isolatedVmMockFns.mockExecuteInIsolatedVM.mockImplementation(async ({ code }) => ({ + * result: evaluateLoopIndexCheck(code) ?? userConditionResult, + * })) + * ``` + */ +export const isolatedVmMockFns = { + /** Untyped like a bare `vi.fn()`, so each test resolves the fields its code path reads. */ + mockExecuteInIsolatedVM: vi.fn().mockImplementation(executeLoopIndexCheck), +} + +/** + * Static mock module for `@/lib/execution/isolated-vm`. + * + * @example + * ```ts + * vi.mock('@/lib/execution/isolated-vm', () => isolatedVmMock) + * ``` + */ +export const isolatedVmMock = { + executeInIsolatedVM: isolatedVmMockFns.mockExecuteInIsolatedVM, +} + +/** Restores the default loop-check-only implementation. */ +export function resetIsolatedVmMock(): void { + isolatedVmMockFns.mockExecuteInIsolatedVM.mockReset().mockImplementation(executeLoopIndexCheck) +}