Skip to content
Closed
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
4 changes: 4 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,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: () => [
Expand Down
16 changes: 8 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,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'
Expand All @@ -17,14 +18,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 +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(<index> < <count>)`; 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([
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,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,
Expand All @@ -18,13 +18,7 @@ const { executed, inputsSeen } = vi.hoisted(() => ({
}))

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

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