|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { |
| 5 | + executionPreprocessingMock, |
| 6 | + executionPreprocessingMockFns, |
| 7 | + LoggingSessionMock, |
| 8 | + loggingSessionMock, |
| 9 | + loggingSessionMockFns, |
| 10 | +} from '@sim/testing' |
| 11 | +import { |
| 12 | + executionLimitsMock, |
| 13 | + executionLimitsMockFns, |
| 14 | +} from '@sim/testing/mocks/execution-limits.mock' |
| 15 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 16 | + |
| 17 | +const { mockExecuteWorkflowCore, mockWasExecutionFinalizedByCore } = vi.hoisted(() => ({ |
| 18 | + mockExecuteWorkflowCore: vi.fn(), |
| 19 | + mockWasExecutionFinalizedByCore: vi.fn(), |
| 20 | +})) |
| 21 | + |
| 22 | +vi.mock('@/lib/execution/preprocessing', () => executionPreprocessingMock) |
| 23 | +vi.mock('@/lib/logs/execution/logging-session', () => loggingSessionMock) |
| 24 | +vi.mock('@/lib/core/execution-limits', () => executionLimitsMock) |
| 25 | +vi.mock('@/lib/workflows/executor/execution-core', () => ({ |
| 26 | + executeWorkflowCore: mockExecuteWorkflowCore, |
| 27 | + wasExecutionFinalizedByCore: mockWasExecutionFinalizedByCore, |
| 28 | +})) |
| 29 | +vi.mock('@/lib/workflows/executor/pause-persistence', () => ({ |
| 30 | + handlePostExecutionPauseState: vi.fn(), |
| 31 | +})) |
| 32 | +vi.mock('@/lib/logs/execution/trace-spans/trace-spans', () => ({ |
| 33 | + buildTraceSpans: vi.fn(() => ({ traceSpans: [] })), |
| 34 | +})) |
| 35 | +vi.mock('@/executor/execution/snapshot', () => ({ ExecutionSnapshot: vi.fn() })) |
| 36 | +vi.mock('@/lib/uploads/utils/user-file-base64.server', () => ({ |
| 37 | + cleanupExecutionBase64Cache: vi.fn(async () => {}), |
| 38 | +})) |
| 39 | + |
| 40 | +import * as usageReservation from '@/lib/billing/calculations/usage-reservation' |
| 41 | +import { executeWorkflowJob, type WorkflowExecutionPayload } from '@/background/workflow-execution' |
| 42 | +import { buildBlockExecutionError } from '@/executor/utils/errors' |
| 43 | +import type { SerializedBlock } from '@/serializer/types' |
| 44 | + |
| 45 | +const billingAttribution = { |
| 46 | + actorUserId: 'user-1', |
| 47 | + workspaceId: 'workspace-1', |
| 48 | + organizationId: null, |
| 49 | + billedAccountUserId: 'user-1', |
| 50 | + billingEntity: { type: 'user' as const, id: 'user-1' }, |
| 51 | + billingPeriod: { start: '2026-09-01T00:00:00.000Z', end: '2026-10-01T00:00:00.000Z' }, |
| 52 | + payerSubscription: null, |
| 53 | +} |
| 54 | + |
| 55 | +const payload: WorkflowExecutionPayload = { |
| 56 | + workflowId: 'workflow-1', |
| 57 | + principal: { |
| 58 | + version: 1, |
| 59 | + principal: { |
| 60 | + kind: 'system', |
| 61 | + serviceId: 'internal', |
| 62 | + workspaceId: 'workspace-1', |
| 63 | + workflowId: 'workflow-1', |
| 64 | + }, |
| 65 | + }, |
| 66 | + userId: 'user-1', |
| 67 | + billingAttribution, |
| 68 | + workspaceId: 'workspace-1', |
| 69 | + executionId: 'execution-1', |
| 70 | + requestId: 'request-1', |
| 71 | + triggerType: 'api', |
| 72 | +} |
| 73 | + |
| 74 | +const planPanels = { |
| 75 | + id: 'plan-panels', |
| 76 | + metadata: { id: 'function', name: 'planPanels' }, |
| 77 | +} as SerializedBlock |
| 78 | + |
| 79 | +describe('executeWorkflowJob fault vs workflow failure', () => { |
| 80 | + beforeEach(() => { |
| 81 | + vi.spyOn(usageReservation, 'refreshExecutionSlotExpiry').mockResolvedValue(true) |
| 82 | + vi.spyOn(usageReservation, 'releaseExecutionSlot').mockResolvedValue(undefined) |
| 83 | + executionLimitsMockFns.mockCreateTimeoutAbortController.mockImplementation(() => ({ |
| 84 | + signal: new AbortController().signal, |
| 85 | + cleanup: vi.fn(), |
| 86 | + abort: vi.fn(), |
| 87 | + isTimedOut: () => false, |
| 88 | + timeoutMs: 120_000, |
| 89 | + })) |
| 90 | + LoggingSessionMock.mockImplementation(function LoggingSession() { |
| 91 | + return { |
| 92 | + safeCompleteWithError: loggingSessionMockFns.mockSafeCompleteWithError, |
| 93 | + waitForPostExecution: loggingSessionMockFns.mockWaitForPostExecution, |
| 94 | + markAsFailed: loggingSessionMockFns.mockMarkAsFailed, |
| 95 | + setExecutionDeadlineAt: loggingSessionMockFns.mockSetExecutionDeadlineAt, |
| 96 | + projectDiagnosticError: loggingSessionMockFns.mockProjectDiagnosticError, |
| 97 | + } |
| 98 | + }) |
| 99 | + executionPreprocessingMockFns.mockPreprocessExecution.mockResolvedValue({ |
| 100 | + success: true, |
| 101 | + actorUserId: 'user-1', |
| 102 | + billingAttribution, |
| 103 | + workflowRecord: { |
| 104 | + id: 'workflow-1', |
| 105 | + workspaceId: 'workspace-1', |
| 106 | + userId: 'user-1', |
| 107 | + variables: {}, |
| 108 | + }, |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + it('completes the job when a block failure is recorded only after core throws', async () => { |
| 113 | + const blockError = buildBlockExecutionError({ |
| 114 | + block: planPanels, |
| 115 | + error: new Error("ValueError: Doctrine has no sheet layout for kind ''"), |
| 116 | + }) |
| 117 | + let recorded = false |
| 118 | + mockExecuteWorkflowCore.mockRejectedValue(blockError) |
| 119 | + loggingSessionMockFns.mockWaitForPostExecution.mockImplementation(async () => { |
| 120 | + recorded = true |
| 121 | + }) |
| 122 | + mockWasExecutionFinalizedByCore.mockImplementation(() => recorded) |
| 123 | + |
| 124 | + const result = await executeWorkflowJob(payload) |
| 125 | + |
| 126 | + expect(result).toMatchObject({ |
| 127 | + success: false, |
| 128 | + workflowId: 'workflow-1', |
| 129 | + executionId: 'execution-1', |
| 130 | + error: blockError.message, |
| 131 | + }) |
| 132 | + expect(loggingSessionMockFns.mockSafeCompleteWithError).not.toHaveBeenCalled() |
| 133 | + }) |
| 134 | + |
| 135 | + it('faults the job when core never recorded the failure', async () => { |
| 136 | + const setupError = new Error('Workflow state not found') |
| 137 | + mockExecuteWorkflowCore.mockRejectedValue(setupError) |
| 138 | + mockWasExecutionFinalizedByCore.mockReturnValue(false) |
| 139 | + |
| 140 | + await expect(executeWorkflowJob(payload)).rejects.toBe(setupError) |
| 141 | + expect(loggingSessionMockFns.mockSafeCompleteWithError).toHaveBeenCalled() |
| 142 | + }) |
| 143 | +}) |
0 commit comments