Skip to content

Commit 3c5fcc0

Browse files
committed
fix(redis): keep unit tests off a real Redis and update integration tests for lazy channels
1 parent 529c3f9 commit 3c5fcc0

12 files changed

Lines changed: 169 additions & 38 deletions

File tree

‎apps/sim/app/api/workflows/[id]/executions/[executionId]/stream/route.test.ts‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { authMockFns, createMockRequest, workflowAuthzMockFns } from '@sim/testing'
2+
import { executionSignalMockFns } from '@sim/testing/mocks/execution-signal.mock'
23
import { beforeEach, describe, expect, it, vi } from 'vitest'
34
import type { ExecutionEventEntry } from '@/lib/execution/event-buffer'
45

5-
const { mockReadExecutionEventsState, mockReadExecutionMetaState, mockSubscribe } = vi.hoisted(
6-
() => ({
7-
mockReadExecutionEventsState: vi.fn(),
8-
mockReadExecutionMetaState: vi.fn(),
9-
mockSubscribe: vi.fn(),
10-
})
11-
)
6+
const { mockReadExecutionEventsState, mockReadExecutionMetaState } = vi.hoisted(() => ({
7+
mockReadExecutionEventsState: vi.fn(),
8+
mockReadExecutionMetaState: vi.fn(),
9+
}))
10+
const mockSubscribe = executionSignalMockFns.mockSubscribe
1211

1312
const mockAuthorizeWorkflowByWorkspacePermission =
1413
workflowAuthzMockFns.mockAuthorizeWorkflowByWorkspacePermission
@@ -19,10 +18,6 @@ vi.mock('@/lib/execution/event-buffer', () => ({
1918
readExecutionMetaState: mockReadExecutionMetaState,
2019
}))
2120

22-
vi.mock('@/lib/execution/signal', () => ({
23-
getExecutionSignalHub: () => ({ subscribe: mockSubscribe }),
24-
}))
25-
2621
import { GET } from './route'
2722

2823
const mockGetSession = authMockFns.mockGetSession
@@ -191,7 +186,7 @@ describe('execution stream reconnect route', () => {
191186
let signalHandler: ((reason: 'event') => void) | undefined
192187
mockSubscribe.mockImplementationOnce(async (_executionId, handler) => {
193188
signalHandler = handler
194-
return vi.fn()
189+
return vi.fn<() => void>()
195190
})
196191
mockReadExecutionMetaState
197192
.mockResolvedValueOnce({
@@ -253,7 +248,7 @@ describe('execution stream reconnect route', () => {
253248
let signalHandler: ((reason: 'event') => void) | undefined
254249
mockSubscribe.mockImplementationOnce(async (_executionId, handler) => {
255250
signalHandler = handler
256-
return vi.fn()
251+
return vi.fn<() => void>()
257252
})
258253
const staleActiveMeta = {
259254
status: 'found' as const,
@@ -313,7 +308,7 @@ describe('execution stream reconnect route', () => {
313308
it('fails the observer closed when its Redis signal subscription becomes unavailable', async () => {
314309
mockSubscribe.mockImplementationOnce(async (_executionId, handler) => {
315310
queueMicrotask(() => handler('unavailable'))
316-
return vi.fn()
311+
return vi.fn<() => void>()
317312
})
318313
const req = createMockRequest(
319314
'GET',

‎apps/sim/executor/execution/test-hooks.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ const { executed, inputsSeen } = vi.hoisted(() => ({
1717
inputsSeen: [] as Array<{ blockId: string; inputs: Record<string, unknown> }>,
1818
}))
1919

20+
/** A For loop's continuation check is `return Boolean(<index> < <count>)`; nothing here needs a real isolate. */
21+
vi.mock('@/lib/execution/isolated-vm', () => ({
22+
executeInIsolatedVM: vi.fn(async ({ code }: { code: string }) => {
23+
const indexCheck = /^return Boolean\((\d+) < (\d+)\)$/.exec(code)
24+
if (!indexCheck) throw new Error(`Unexpected isolated-vm code in test: ${code}`)
25+
return { result: Number(indexCheck[1]) < Number(indexCheck[2]) }
26+
}),
27+
}))
28+
2029
vi.mock('@/executor/handlers/registry', () => ({
2130
createBlockHandlers: () => [
2231
{

‎apps/sim/lib/execution/cancellation.test.ts‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
import { redisConfigMockFns } from '@sim/testing'
2+
import { executionSignalMockFns } from '@sim/testing/mocks/execution-signal.mock'
23
import { beforeEach, describe, expect, it, vi } from 'vitest'
4+
import type { ExecutionSignalHandler } from '@/lib/execution/signal'
35

46
const {
57
mockRedisEval,
68
mockRedisExists,
79
mockRedisPublish,
8-
mockSignalSubscribe,
910
mockSignalUnsubscribe,
1011
mockRecordCancellationResult,
1112
} = vi.hoisted(() => ({
1213
mockRedisEval: vi.fn(),
1314
mockRedisExists: vi.fn(),
1415
mockRedisPublish: vi.fn(),
15-
mockSignalSubscribe: vi.fn(),
1616
mockSignalUnsubscribe: vi.fn(),
1717
mockRecordCancellationResult: vi.fn(),
1818
}))
1919

2020
const mockGetRedisClient = redisConfigMockFns.mockGetRedisClient
21-
22-
vi.mock('@/lib/execution/signal', () => ({
23-
getExecutionSignalChannel: (executionId: string) => `execution:signal:${executionId}`,
24-
getExecutionSignalHub: () => ({ subscribe: mockSignalSubscribe }),
25-
LEGACY_EXECUTION_CANCEL_CHANNEL: 'execution:cancel',
26-
}))
21+
const mockSignalSubscribe = executionSignalMockFns.mockSubscribe
2722

2823
vi.mock('@/lib/core/execution-limits/metrics', () => ({
2924
recordExecutionCancellationBackendResult: mockRecordCancellationResult,
@@ -195,7 +190,7 @@ describe('subscribeToExecutionCancellation', () => {
195190
})
196191

197192
it('re-reads the durable cancellation flag after the signal connection recovers', async () => {
198-
let signalHandler: ((reason: string) => void) | undefined
193+
let signalHandler: ExecutionSignalHandler | undefined
199194
mockSignalSubscribe.mockImplementation(async (_executionId, handler) => {
200195
signalHandler = handler
201196
return mockSignalUnsubscribe
@@ -211,7 +206,7 @@ describe('subscribeToExecutionCancellation', () => {
211206
})
212207

213208
it('queues a fresh durable read when reconnect occurs during an in-flight read', async () => {
214-
let signalHandler: ((reason: string) => void) | undefined
209+
let signalHandler: ExecutionSignalHandler | undefined
215210
mockSignalSubscribe.mockImplementation(async (_executionId, handler) => {
216211
signalHandler = handler
217212
return mockSignalUnsubscribe
@@ -237,7 +232,7 @@ describe('subscribeToExecutionCancellation', () => {
237232
})
238233

239234
it('fails closed when the signal subscription cannot be restored', async () => {
240-
let signalHandler: ((reason: string) => void) | undefined
235+
let signalHandler: ExecutionSignalHandler | undefined
241236
mockSignalSubscribe.mockImplementation(async (_executionId, handler) => {
242237
signalHandler = handler
243238
return mockSignalUnsubscribe

‎apps/sim/lib/execution/signal.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const { connection, mockRedisUrl, mockSubscribe, mockUnsubscribe } = vi.hoisted(
1616
mockUnsubscribe: vi.fn(),
1717
}))
1818

19+
vi.unmock('@/lib/execution/signal')
20+
1921
vi.mock('ioredis', () => ({
2022
default: class extends EventEmitter {
2123
constructor(_url: string, options: Record<string, unknown>) {

‎apps/sim/lib/mothership/agent-cli/workbench-file-provenance.integration.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ const redis = redisUrl
2929
})
3030
: undefined
3131
redis?.on('error', () => {})
32+
/** A client whose connection has closed: every command rejects the way it does when Redis drops. */
33+
const closedRedis = redisUrl
34+
? new Redis(redisUrl, { lazyConnect: true, retryStrategy: () => null })
35+
: undefined
36+
closedRedis?.on('error', () => {})
37+
closedRedis?.disconnect()
38+
const STORAGE_FAILURE = 'Connection is closed'
3239
const receiptPrefixes = new Set<string>()
3340
const historyKeys = new Set<string>()
3441
const fixtureWorkspaceId = generateShortId()
@@ -354,10 +361,10 @@ describe.skipIf(!redisUrl)('trusted workbench byte receipts with real Redis', ()
354361
await consume(broken.observeUpload(machine, body()))
355362
expect(broken.uploadProvenance()).toEqual({ status: 'unknown' })
356363
}
357-
redisConfigMockFns.mockGetRedisClient.mockReturnValue(null)
364+
redisConfigMockFns.mockGetRedisClient.mockReturnValue(closedRedis!)
358365
const unavailable = createWorkbenchFileProvenance(scope)
359366
await expect(consume(unavailable.observeUpload(machine, body()))).rejects.toThrow(
360-
'storage is unavailable'
367+
STORAGE_FAILURE
361368
)
362369
expect(() => unavailable.uploadProvenance()).toThrow('has not finished')
363370
})
@@ -394,7 +401,7 @@ describe.skipIf(!redisUrl)('trusted workbench byte receipts with real Redis', ()
394401
await expect(reader.read()).rejects.toThrow('stopped')
395402
} else {
396403
if (outcome === 'storage failure') {
397-
redisConfigMockFns.mockGetRedisClient.mockReturnValueOnce(null)
404+
redisConfigMockFns.mockGetRedisClient.mockReturnValueOnce(closedRedis!)
398405
} else if (outcome === 'abort during persistence') {
399406
redisConfigMockFns.mockGetRedisClient.mockImplementationOnce(() => {
400407
stop.abort(new Error('stopped during persistence'))
@@ -406,7 +413,7 @@ describe.skipIf(!redisUrl)('trusted workbench byte receipts with real Redis', ()
406413
expect(await reader.read()).toEqual({ done: true, value: undefined })
407414
} else {
408415
await expect(reader.read()).rejects.toThrow(
409-
outcome === 'storage failure' ? 'storage is unavailable' : 'stopped during persistence'
416+
outcome === 'storage failure' ? STORAGE_FAILURE : 'stopped during persistence'
410417
)
411418
}
412419
}

‎apps/sim/lib/mothership/async-runs/tool-identity.integration.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ describe('Copilot tool identity with PostgreSQL', () => {
139139
if (redisUrl) {
140140
redisState.current = new Redis(redisUrl, { maxRetriesPerRequest: 1 })
141141
await redisState.current.ping()
142+
const channels = globalThis as typeof globalThis & {
143+
_toolConfirmationChannel?: { ready(): Promise<void> }
144+
_toolPermissionChannel?: { ready(): Promise<void> }
145+
}
146+
if (!channels._toolConfirmationChannel || !channels._toolPermissionChannel) {
147+
throw new Error('Tool confirmation and permission channels were not created on import')
148+
}
149+
/** The channels connect on first use; open both so no publication below precedes them. */
150+
await Promise.all([
151+
channels._toolConfirmationChannel.ready(),
152+
channels._toolPermissionChannel.ready(),
153+
])
142154
await vi.waitFor(async () => {
143155
const counts = await redisState.current?.pubsub(
144156
'NUMSUB',

‎apps/sim/lib/projects/__integration__/backfill-repair.integration.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,13 @@ describeWithRedis('Operator archive repair against the full compatible schema',
339339
const unsubscribe = mcpPubSub.onWorkflowToolsChanged(() => { throw new Error('fixture subscriber failed'); });
340340
const received = [];
341341
const unsubscribeHealthy = mcpPubSub.onWorkflowToolsChanged((event) => received.push(event));
342+
const notified = () => received.some((event) => event.serverId === '${serverId}' && event.workspaceId === 'local-env');
342343
try {
344+
await mcpPubSub.ready();
343345
await repairArchivedProjectEnvironment(client, {workspaceId:'local-env', archivedAt:'2026-05-01 00:00:00', workflowIds:['local-flow']}, 'local-subscriber');
344-
assert.ok(received.some((event) => event.serverId === '${serverId}' && event.workspaceId === 'local-env'), 'Healthy subscriber missed the archive notification');
346+
const deadline = Date.now() + 10000;
347+
while (!notified() && Date.now() < deadline) await new Promise((resolve) => setTimeout(resolve, 25));
348+
assert.ok(notified(), 'Healthy subscriber missed the archive notification');
345349
} finally {
346350
unsubscribe(); unsubscribeHealthy(); mcpPubSub.dispose(); await client.end();
347351
await Promise.all([...new Set([db.$client,dbReplica.$client])].map(client=>client.end()));
@@ -350,7 +354,7 @@ describeWithRedis('Operator archive repair against the full compatible schema',
350354
],
351355
{
352356
cwd: new URL('../../../', import.meta.url),
353-
env: { ...environment, REDIS_URL: '' },
357+
env: environment,
354358
timeout: 45000,
355359
}
356360
)

‎apps/sim/lib/workflows/executor/execution-core.test.ts‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from '@sim/testing/factories/principal.factory'
1616
import { customBlockOperationsMock } from '@sim/testing/mocks/custom-block-operations.mock'
1717
import { encryptionMock, encryptionMockFns } from '@sim/testing/mocks/encryption.mock'
18+
import { executionSignalMockFns } from '@sim/testing/mocks/execution-signal.mock'
1819
import {
1920
largeValueMetadataMock,
2021
largeValueMetadataMockFns,
@@ -108,13 +109,7 @@ vi.mock('@/lib/execution/cancellation', () => ({
108109
clearExecutionCancellation: clearExecutionCancellationMock,
109110
}))
110111

111-
const { connectExecutionSignalHubMock } = vi.hoisted(() => ({
112-
connectExecutionSignalHubMock: vi.fn(),
113-
}))
114-
115-
vi.mock('@/lib/execution/signal', () => ({
116-
connectExecutionSignalHub: connectExecutionSignalHubMock,
117-
}))
112+
const connectExecutionSignalHubMock = executionSignalMockFns.mockConnectExecutionSignalHub
118113

119114
vi.mock('@/lib/core/security/encryption', () => encryptionMock)
120115

‎apps/sim/lib/workflows/executor/human-in-the-loop-manager.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import {
1515
executionPreprocessingMock,
1616
executionPreprocessingMockFns,
1717
} from '@sim/testing/mocks/execution-preprocessing.mock'
18+
import { deliverExecutionSignal } from '@sim/testing/mocks/execution-signal.mock'
1819
import { largeValueMetadataMock } from '@sim/testing/mocks/large-value-metadata.mock'
20+
import { createMockRedis } from '@sim/testing/mocks/redis.mock'
21+
import { redisConfigMockFns, resetRedisConfigMock } from '@sim/testing/mocks/redis-config.mock'
1922
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2023
import { classifyFailure, logFailureOnce } from '@/lib/core/errors/failure-log'
2124
import { createTimeoutAbortController, getExecutionDeadlineAt } from '@/lib/core/execution-limits'
@@ -2417,6 +2420,13 @@ describe('stopping an active resume from another replica', () => {
24172420
})
24182421

24192422
it('aborts the attempt when Stop records cancellation under the attempt id', async () => {
2423+
const redis = createMockRedis()
2424+
// Runs the cancellation script's PUBLISH: the durable key write is not what wakes a live attempt.
2425+
redis.eval.mockImplementation(async (...args: unknown[]) => {
2426+
deliverExecutionSignal(String(args[4]), 'cancelled')
2427+
return null
2428+
})
2429+
redisConfigMockFns.mockGetRedisClient.mockReturnValue(redis)
24202430
let attemptSignal: AbortSignal | undefined
24212431
const runSpy = vi
24222432
.spyOn(PauseResumeManager as unknown as PauseResumeManagerInternals, 'runResumeExecution')
@@ -2450,6 +2460,7 @@ describe('stopping an active resume from another replica', () => {
24502460
expect(attemptSignal?.aborted).toBe(true)
24512461
expect(attemptSignal?.reason).toMatchObject({ name: 'AbortError', message: 'user' })
24522462
} finally {
2463+
resetRedisConfigMock()
24532464
runSpy.mockRestore()
24542465
attemptFailedSpy.mockRestore()
24552466
processSpy.mockRestore()

‎apps/sim/vitest.setup.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { databaseMock, drizzleOrmMock } from '@sim/testing/mocks/database.mock'
33
import { envMock } from '@sim/testing/mocks/env.mock'
44
import { envFlagsMock } from '@sim/testing/mocks/env-flags.mock'
55
import { environmentUtilsMock } from '@sim/testing/mocks/environment-utils.mock'
6+
import { executionSignalMock } from '@sim/testing/mocks/execution-signal.mock'
67
import { setupGlobalFetchMock } from '@sim/testing/mocks/fetch.mock'
78
import { hybridAuthMock } from '@sim/testing/mocks/hybrid-auth.mock'
89
import { loggerMock } from '@sim/testing/mocks/logger.mock'
@@ -53,6 +54,7 @@ vi.mock('@/lib/core/config/env-flags', () => envFlagsMock)
5354
vi.mock('@/lib/core/config/env', () => envMock)
5455
vi.mock('@/lib/core/utils/urls', () => urlsMock)
5556
vi.mock('@/lib/core/config/redis', () => redisConfigMock)
57+
vi.mock('@/lib/execution/signal', () => executionSignalMock)
5658
vi.mock('@/lib/environment/utils', () => environmentUtilsMock)
5759

5860
vi.mock('@/stores/terminal', () => terminalConsoleMock)

0 commit comments

Comments
 (0)