@@ -277,4 +277,109 @@ describe('duplicateWorkflow ordering', () => {
277277 expect ( copiedSubBlocks . variables . value [ 0 ] . variableName ) . toBe ( 'customerName' )
278278 expect ( insertedBlocks ?. [ 0 ] . locked ) . toBe ( false )
279279 } )
280+
281+ it ( 'remaps variable assignments when duplicating an already-duplicated source (array value)' , async ( ) => {
282+ let insertedBlocks : Array < Record < string , unknown > > | null = null
283+ let insertedWorkflowValues : Record < string , unknown > | null = null
284+ const tx = createMockTx (
285+ [
286+ [
287+ {
288+ id : 'first-copy-workflow-id' ,
289+ workspaceId : 'workspace-123' ,
290+ folderId : null ,
291+ description : 'first copy' ,
292+ color : '#000000' ,
293+ variables : {
294+ 'first-copy-var-id' : {
295+ id : 'first-copy-var-id' ,
296+ workflowId : 'first-copy-workflow-id' ,
297+ name : 'customerName' ,
298+ type : 'string' ,
299+ value : 'Ada' ,
300+ } ,
301+ } ,
302+ } ,
303+ ] ,
304+ [ ] ,
305+ [ ] ,
306+ [
307+ {
308+ id : 'first-copy-block-id' ,
309+ workflowId : 'first-copy-workflow-id' ,
310+ type : 'agent' ,
311+ name : 'Agent' ,
312+ parentId : null ,
313+ extent : null ,
314+ data : { } ,
315+ subBlocks : {
316+ variables : {
317+ id : 'variables' ,
318+ type : 'variables-input' ,
319+ value : [
320+ {
321+ id : 'assignment-1' ,
322+ variableId : 'first-copy-var-id' ,
323+ variableName : 'customerName' ,
324+ type : 'string' ,
325+ value : 'Grace' ,
326+ isExisting : true ,
327+ } ,
328+ ] ,
329+ } ,
330+ } ,
331+ position : { x : 0 , y : 0 } ,
332+ enabled : true ,
333+ horizontalHandles : true ,
334+ isWide : false ,
335+ height : 0 ,
336+ advancedMode : false ,
337+ triggerMode : false ,
338+ locked : false ,
339+ createdAt : new Date ( ) ,
340+ updatedAt : new Date ( ) ,
341+ } ,
342+ ] ,
343+ [ ] ,
344+ [ ] ,
345+ ] ,
346+ ( values ) => {
347+ if ( ! insertedWorkflowValues && ! Array . isArray ( values ) ) {
348+ insertedWorkflowValues = values
349+ }
350+ } ,
351+ ( values ) => {
352+ if ( Array . isArray ( values ) ) {
353+ insertedBlocks = values as Array < Record < string , unknown > >
354+ }
355+ }
356+ )
357+
358+ mockDb . transaction . mockImplementation ( async ( callback : ( txArg : unknown ) => Promise < unknown > ) =>
359+ callback ( tx )
360+ )
361+
362+ await duplicateWorkflow ( {
363+ sourceWorkflowId : 'first-copy-workflow-id' ,
364+ userId : 'user-123' ,
365+ name : 'Duplicated Again' ,
366+ workspaceId : 'workspace-123' ,
367+ folderId : null ,
368+ requestId : 'req-second-copy' ,
369+ } )
370+
371+ expect ( insertedBlocks ) . toHaveLength ( 1 )
372+ const copiedSubBlocks = insertedBlocks ?. [ 0 ] . subBlocks as Record < string , any >
373+ expect ( Array . isArray ( copiedSubBlocks . variables . value ) ) . toBe ( true )
374+ expect ( copiedSubBlocks . variables . value ) . toHaveLength ( 1 )
375+
376+ const newVarIds = Object . keys (
377+ ( insertedWorkflowValues ?. variables as Record < string , unknown > ) || { }
378+ )
379+ expect ( newVarIds ) . toHaveLength ( 1 )
380+ const remappedVarId = copiedSubBlocks . variables . value [ 0 ] . variableId
381+ expect ( remappedVarId ) . not . toBe ( 'first-copy-var-id' )
382+ expect ( remappedVarId ) . toBe ( newVarIds [ 0 ] )
383+ expect ( copiedSubBlocks . variables . value [ 0 ] . variableName ) . toBe ( 'customerName' )
384+ } )
280385} )
0 commit comments