|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * Tests for Issue #3081: Race Condition between pause persistence and resume requests |
| 5 | + */ |
| 6 | +import { databaseMock, loggerMock } from '@sim/testing' |
| 7 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 8 | + |
| 9 | +vi.mock('@sim/db', () => databaseMock) |
| 10 | +vi.mock('@sim/logger', () => loggerMock) |
| 11 | + |
| 12 | +import { PauseResumeManager } from '@/lib/workflows/executor/human-in-the-loop-manager' |
| 13 | +import type { PausePoint, SerializedSnapshot } from '@/executor/types' |
| 14 | + |
| 15 | +describe('PauseResumeManager - Race Condition Fix (#3081)', () => { |
| 16 | + beforeEach(() => { |
| 17 | + vi.clearAllMocks() |
| 18 | + }) |
| 19 | + |
| 20 | + const createTestSnapshot = (): SerializedSnapshot => ({ |
| 21 | + snapshot: JSON.stringify({ |
| 22 | + workflow: { blocks: [], connections: [] }, |
| 23 | + state: { blockStates: {}, executedBlocks: [] }, |
| 24 | + }), |
| 25 | + triggerIds: [], |
| 26 | + }) |
| 27 | + |
| 28 | + const createTestPausePoints = (): PausePoint[] => [ |
| 29 | + { |
| 30 | + contextId: 'test-context', |
| 31 | + blockId: 'pause-block-1', |
| 32 | + response: {}, |
| 33 | + resumeStatus: 'paused', |
| 34 | + snapshotReady: true, |
| 35 | + registeredAt: new Date().toISOString(), |
| 36 | + }, |
| 37 | + ] |
| 38 | + |
| 39 | + describe('persistPauseResult', () => { |
| 40 | + it.concurrent('should use database transaction for atomic persistence', async () => { |
| 41 | + const mockInsert = vi.fn().mockReturnValue({ |
| 42 | + values: vi.fn().mockReturnValue({ |
| 43 | + onConflictDoUpdate: vi.fn().mockResolvedValue(undefined), |
| 44 | + }), |
| 45 | + }) |
| 46 | + |
| 47 | + const mockTransaction = vi.fn().mockImplementation(async (callback) => { |
| 48 | + const mockTx = { insert: mockInsert } |
| 49 | + return await callback(mockTx as any) |
| 50 | + }) |
| 51 | + |
| 52 | + vi.mocked(databaseMock.db.transaction).mockImplementation(mockTransaction) |
| 53 | + vi.spyOn(PauseResumeManager, 'processQueuedResumes').mockResolvedValue(undefined) |
| 54 | + |
| 55 | + await PauseResumeManager.persistPauseResult({ |
| 56 | + workflowId: 'test-workflow', |
| 57 | + executionId: 'test-execution', |
| 58 | + pausePoints: createTestPausePoints(), |
| 59 | + snapshotSeed: createTestSnapshot(), |
| 60 | + executorUserId: 'test-user', |
| 61 | + }) |
| 62 | + |
| 63 | + expect(mockTransaction).toHaveBeenCalledTimes(1) |
| 64 | + expect(mockInsert).toHaveBeenCalled() |
| 65 | + }) |
| 66 | + |
| 67 | + it.concurrent('should call processQueuedResumes after transaction', async () => { |
| 68 | + const mockInsert = vi.fn().mockReturnValue({ |
| 69 | + values: vi.fn().mockReturnValue({ |
| 70 | + onConflictDoUpdate: vi.fn().mockResolvedValue(undefined), |
| 71 | + }), |
| 72 | + }) |
| 73 | + |
| 74 | + vi.mocked(databaseMock.db.transaction).mockImplementation(async (callback) => { |
| 75 | + const mockTx = { insert: mockInsert } |
| 76 | + return await callback(mockTx as any) |
| 77 | + }) |
| 78 | + |
| 79 | + const processQueuedResumesSpy = vi |
| 80 | + .spyOn(PauseResumeManager, 'processQueuedResumes') |
| 81 | + .mockResolvedValue(undefined) |
| 82 | + |
| 83 | + await PauseResumeManager.persistPauseResult({ |
| 84 | + workflowId: 'test-workflow', |
| 85 | + executionId: 'test-execution', |
| 86 | + pausePoints: createTestPausePoints(), |
| 87 | + snapshotSeed: createTestSnapshot(), |
| 88 | + executorUserId: 'test-user', |
| 89 | + }) |
| 90 | + |
| 91 | + expect(processQueuedResumesSpy).toHaveBeenCalledWith('test-execution') |
| 92 | + }) |
| 93 | + }) |
| 94 | +}) |
0 commit comments