Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/sim/executor/execution/condition-join-loop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -12,6 +13,8 @@ import type { SerializedWorkflow } from '@/serializer/types'

vi.mock('@/tools', () => toolsMock)

vi.mock('@/lib/execution/isolated-vm', () => isolatedVmMock)

/** Real condition handler; every other block answers with its own name. */
vi.mock('@/executor/handlers/registry', () => ({
createBlockHandlers: () => [
Expand Down
18 changes: 10 additions & 8 deletions apps/sim/executor/execution/condition-loop-ceiling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -17,14 +22,7 @@ const { bodyRuns, conditionResult, runaway } = vi.hoisted(() => ({
runaway: { controller: new AbortController(), limit: 30_000 },
}))

/** Count conditions resolve to `return Boolean(<index> < <count>)`; 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: () => [
Expand Down Expand Up @@ -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([
Expand Down
10 changes: 2 additions & 8 deletions apps/sim/executor/execution/test-hooks.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -17,14 +18,7 @@ const { executed, inputsSeen } = vi.hoisted(() => ({
inputsSeen: [] as Array<{ blockId: string; inputs: Record<string, unknown> }>,
}))

/** A For loop's continuation check is `return Boolean(<index> < <count>)`; 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: () => [
Expand Down
9 changes: 3 additions & 6 deletions apps/sim/executor/orchestrators/loop.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)

Expand Down
9 changes: 4 additions & 5 deletions apps/sim/lib/function-execution/execute-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -42,15 +43,13 @@ import {
import { SandboxExecutionTimeoutError } from '@/lib/execution/remote-sandbox/timeout'

const {
mockExecuteInIsolatedVM,
mockValidateWorkspaceFileWriteTarget,
mockWriteWorkspaceFileByPath,
mockUploadExecutionFile,
mockMountContributors,
mockUnprovenancedMountCount,
mockRenderedMountContributors,
} = vi.hoisted(() => ({
mockExecuteInIsolatedVM: vi.fn(),
mockValidateWorkspaceFileWriteTarget: vi.fn(),
mockWriteWorkspaceFileByPath: vi.fn(),
mockUploadExecutionFile: vi.fn(),
Expand All @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions packages/testing/src/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
54 changes: 54 additions & 0 deletions packages/testing/src/mocks/isolated-vm.mock.ts
Original file line number Diff line number Diff line change
@@ -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(<index> < <count>)` 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)
}
Loading