11/**
22 * @vitest -environment node
33 */
4+ import { loggerMock } from '@sim/testing'
45import { NextRequest } from 'next/server'
56import { beforeEach , describe , expect , it , vi } from 'vitest'
67
@@ -11,6 +12,7 @@ const {
1112 mockGetPauseContextDetail,
1213 mockGetPausedExecutionDetail,
1314 mockPreprocessExecution,
15+ mockStartResumeExecution,
1416 mockExecuteResumeJob,
1517 mockShouldExecuteInline,
1618 mockValidateWorkflowAccess,
@@ -21,6 +23,7 @@ const {
2123 mockGetPauseContextDetail : vi . fn ( ) ,
2224 mockGetPausedExecutionDetail : vi . fn ( ) ,
2325 mockPreprocessExecution : vi . fn ( ) ,
26+ mockStartResumeExecution : vi . fn ( ) ,
2427 mockExecuteResumeJob : vi . fn ( ) ,
2528 mockShouldExecuteInline : vi . fn ( ( ) => false ) ,
2629 mockValidateWorkflowAccess : vi . fn ( ) ,
@@ -58,12 +61,20 @@ vi.mock('@/lib/workflows/executor/human-in-the-loop-manager', () => ({
5861 getPausedExecutionDetail : mockGetPausedExecutionDetail ,
5962 markResumeAttemptFailed : vi . fn ( ) ,
6063 processQueuedResumes : vi . fn ( ) ,
61- startResumeExecution : vi . fn ( ) ,
64+ startResumeExecution : mockStartResumeExecution ,
6265 } ,
6366} ) )
6467
6568import { GET , POST } from '@/app/api/resume/[workflowId]/[executionId]/[contextId]/route'
6669
70+ const resumeApiLoggerCallIndex = loggerMock . createLogger . mock . calls . findIndex (
71+ ( [ name ] ) => name === 'WorkflowResumeAPI'
72+ )
73+ const resumeApiLogger = loggerMock . createLogger . mock . results [ resumeApiLoggerCallIndex ] ?. value
74+ if ( ! resumeApiLogger ) {
75+ throw new Error ( 'WorkflowResumeAPI logger mock was not initialized' )
76+ }
77+
6778const WORKFLOW_ID = 'workflow-1'
6879const EXECUTION_ID = 'execution-1'
6980const CONTEXT_ID = 'context-1'
@@ -188,6 +199,12 @@ describe('POST /api/resume/[workflowId]/[executionId]/[contextId]', () => {
188199 } )
189200 mockEnqueue . mockResolvedValue ( 'resume-job-1' )
190201 mockExecuteResumeJob . mockResolvedValue ( { success : true } )
202+ mockStartResumeExecution . mockResolvedValue ( {
203+ success : true ,
204+ status : 'completed' ,
205+ output : { } ,
206+ logs : [ ] ,
207+ } )
191208 mockShouldExecuteInline . mockReturnValue ( false )
192209 } )
193210
@@ -329,6 +346,77 @@ describe('POST /api/resume/[workflowId]/[executionId]/[contextId]', () => {
329346 )
330347 } )
331348
349+ it ( 'projects rejected resume requests for logs without changing the API error response' , async ( ) => {
350+ const secret = 'resume-request-secret-value'
351+ const message = `Resume request exposed ${ secret } __var_API_KEY __sim_code_1_binding_0`
352+ const rawError = new Error ( message )
353+ mockEnqueueOrStartResume . mockRejectedValueOnce ( rawError )
354+ const { request, context } = makeRequest ( )
355+
356+ const response = await POST ( request , context )
357+
358+ expect ( response . status ) . toBe ( 400 )
359+ expect ( await response . json ( ) ) . toEqual ( { error : message } )
360+ expect ( resumeApiLogger . error ) . toHaveBeenCalledWith ( 'Resume request failed' , {
361+ errorType : 'error' ,
362+ hasStack : true ,
363+ } )
364+ const loggerPayload = JSON . stringify ( resumeApiLogger . error . mock . calls )
365+ expect ( loggerPayload ) . not . toContain ( secret )
366+ expect ( loggerPayload ) . not . toContain ( '__var_' )
367+ expect ( loggerPayload ) . not . toContain ( '__sim_' )
368+ expect ( rawError . message ) . toBe ( message )
369+ } )
370+
371+ it ( 'projects detached resume failures without changing the started response' , async ( ) => {
372+ const secret = 'detached-resume-secret-value'
373+ const message = `Detached resume exposed ${ secret } __var_API_KEY __sim_code_2_binding_0`
374+ const rawError = new Error ( message )
375+ const pausedExecution = createPausedExecution ( )
376+ mockValidateWorkflowAccess . mockResolvedValueOnce ( {
377+ workflow : {
378+ id : WORKFLOW_ID ,
379+ workspaceId : WORKSPACE_ID ,
380+ } ,
381+ auth : {
382+ success : true ,
383+ userId : 'current-session-user' ,
384+ authType : 'session' ,
385+ workspaceId : WORKSPACE_ID ,
386+ } ,
387+ } )
388+ mockEnqueueOrStartResume . mockResolvedValueOnce ( {
389+ status : 'starting' ,
390+ resumeExecutionId : 'resume-attempt-1' ,
391+ resumeEntryId : 'resume-entry-1' ,
392+ pausedExecution,
393+ contextId : CONTEXT_ID ,
394+ resumeInput : { approved : true } ,
395+ userId : 'current-session-user' ,
396+ } )
397+ mockStartResumeExecution . mockRejectedValueOnce ( rawError )
398+ const { request, context } = makeRequest ( )
399+
400+ const response = await POST ( request , context )
401+ await Promise . resolve ( )
402+
403+ expect ( response . status ) . toBe ( 200 )
404+ expect ( await response . json ( ) ) . toEqual ( {
405+ status : 'started' ,
406+ executionId : 'resume-attempt-1' ,
407+ message : 'Resume execution started.' ,
408+ } )
409+ expect ( resumeApiLogger . error ) . toHaveBeenCalledWith ( 'Failed to start resume execution' , {
410+ errorType : 'error' ,
411+ hasStack : true ,
412+ } )
413+ const loggerPayload = JSON . stringify ( resumeApiLogger . error . mock . calls )
414+ expect ( loggerPayload ) . not . toContain ( secret )
415+ expect ( loggerPayload ) . not . toContain ( '__var_' )
416+ expect ( loggerPayload ) . not . toContain ( '__sim_' )
417+ expect ( rawError . message ) . toBe ( message )
418+ } )
419+
332420 it . each ( [
333421 { statusCode : 402 , message : 'Member usage limit reached' , retryable : false } ,
334422 { statusCode : 429 , message : 'Target concurrency full' , retryable : true } ,
0 commit comments