|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | +import type { ForkReferenceResolver } from '@/ee/workspace-forking/lib/remap/remap-references' |
| 6 | +import type { WorkflowState } from '@/stores/workflows/workflow/types' |
| 7 | + |
| 8 | +const { mockResolveBinding } = vi.hoisted(() => ({ mockResolveBinding: vi.fn() })) |
| 9 | + |
| 10 | +vi.mock('@/lib/workflows/custom-blocks/operations', () => ({ |
| 11 | + resolveCustomBlockToolBinding: mockResolveBinding, |
| 12 | +})) |
| 13 | + |
| 14 | +import { collectForkCustomBlockReconfigs } from '@/ee/workspace-forking/lib/mapping/custom-block-reconfigs' |
| 15 | + |
| 16 | +const PROD = 'custom_block_prod01' |
| 17 | +const UAT = 'custom_block_uat0001' |
| 18 | + |
| 19 | +function stateWith(blocks: Record<string, { type: string; name: string }>): WorkflowState { |
| 20 | + return { |
| 21 | + blocks: Object.fromEntries( |
| 22 | + Object.entries(blocks).map(([id, b]) => [ |
| 23 | + id, |
| 24 | + { id, type: b.type, name: b.name, position: { x: 0, y: 0 }, subBlocks: {}, outputs: {} }, |
| 25 | + ]) |
| 26 | + ), |
| 27 | + edges: [], |
| 28 | + loops: {}, |
| 29 | + parallels: {}, |
| 30 | + } as unknown as WorkflowState |
| 31 | +} |
| 32 | + |
| 33 | +const baseParams = { |
| 34 | + items: [{ sourceWorkflowId: 'wf-src', targetWorkflowId: 'wf-tgt' }], |
| 35 | + sourceStates: new Map([ |
| 36 | + ['wf-src', stateWith({ 'blk-cb': { type: UAT, name: 'Invoice Parser' } })], |
| 37 | + ]), |
| 38 | + resolveTargetBlockId: (_t: string, sourceBlockId: string) => `tgt-${sourceBlockId}`, |
| 39 | + targetWorkspaceId: 'ws-parent', |
| 40 | +} |
| 41 | + |
| 42 | +/** Maps the placed UAT block onto the PROD block, i.e. a real repoint. */ |
| 43 | +const swapResolver: ForkReferenceResolver = (kind, sourceId) => |
| 44 | + kind === 'custom-block' && sourceId === UAT ? PROD : null |
| 45 | + |
| 46 | +describe('collectForkCustomBlockReconfigs', () => { |
| 47 | + beforeEach(() => { |
| 48 | + vi.clearAllMocks() |
| 49 | + mockResolveBinding.mockResolvedValue({ |
| 50 | + workflowId: 'wf-prod-impl', |
| 51 | + inputFields: [ |
| 52 | + { id: 'field-a', name: 'Invoice URL', type: 'string' }, |
| 53 | + { id: 'field-b', name: 'Options', type: 'object' }, |
| 54 | + ], |
| 55 | + requiredInputIds: ['field-a'], |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + it('offers every input of the target block, keyed by the field id the canvas reads', async () => { |
| 60 | + const out = await collectForkCustomBlockReconfigs({ ...baseParams, resolve: swapResolver }) |
| 61 | + |
| 62 | + expect(out).toHaveLength(2) |
| 63 | + expect(out.map((f) => f.subBlockKey)).toEqual(['field-a', 'field-b']) |
| 64 | + expect(out[0]).toMatchObject({ |
| 65 | + parentKind: 'custom-block', |
| 66 | + // Joined to its own mapping row on (parentKind, parentSourceId) — the SOURCE type is |
| 67 | + // exactly what the mapping entry's sourceId is. |
| 68 | + parentSourceId: UAT, |
| 69 | + targetWorkflowId: 'wf-tgt', |
| 70 | + targetBlockId: 'tgt-blk-cb', |
| 71 | + title: 'Invoice URL', |
| 72 | + fieldType: 'string', |
| 73 | + required: true, |
| 74 | + }) |
| 75 | + expect(out[1].required).toBe(false) |
| 76 | + }) |
| 77 | + |
| 78 | + it('never seeds the source value — it belongs to a different block', async () => { |
| 79 | + const out = await collectForkCustomBlockReconfigs({ ...baseParams, resolve: swapResolver }) |
| 80 | + |
| 81 | + expect(out.every((f) => f.currentValue === '' && f.sourceValue === '')).toBe(true) |
| 82 | + }) |
| 83 | + |
| 84 | + it('offers nothing when the type does not change', async () => { |
| 85 | + // No mapping, or an explicit identity mapping: the field ids still describe this block, so |
| 86 | + // its values carry across and there is nothing to re-pick. |
| 87 | + expect( |
| 88 | + await collectForkCustomBlockReconfigs({ ...baseParams, resolve: () => null }) |
| 89 | + ).toHaveLength(0) |
| 90 | + expect( |
| 91 | + await collectForkCustomBlockReconfigs({ ...baseParams, resolve: (_k, id) => id }) |
| 92 | + ).toHaveLength(0) |
| 93 | + expect(mockResolveBinding).not.toHaveBeenCalled() |
| 94 | + }) |
| 95 | + |
| 96 | + it('ignores non-custom blocks entirely', async () => { |
| 97 | + const out = await collectForkCustomBlockReconfigs({ |
| 98 | + ...baseParams, |
| 99 | + sourceStates: new Map([['wf-src', stateWith({ a: { type: 'agent', name: 'Agent 1' } })]]), |
| 100 | + resolve: swapResolver, |
| 101 | + }) |
| 102 | + |
| 103 | + expect(out).toHaveLength(0) |
| 104 | + }) |
| 105 | + |
| 106 | + it('resolves each distinct target type once, not once per placement', async () => { |
| 107 | + const out = await collectForkCustomBlockReconfigs({ |
| 108 | + ...baseParams, |
| 109 | + sourceStates: new Map([ |
| 110 | + [ |
| 111 | + 'wf-src', |
| 112 | + stateWith({ |
| 113 | + 'blk-1': { type: UAT, name: 'Parse A' }, |
| 114 | + 'blk-2': { type: UAT, name: 'Parse B' }, |
| 115 | + }), |
| 116 | + ], |
| 117 | + ]), |
| 118 | + resolve: swapResolver, |
| 119 | + }) |
| 120 | + |
| 121 | + // Two placements, each independently configurable, but one schema lookup. |
| 122 | + expect(mockResolveBinding).toHaveBeenCalledTimes(1) |
| 123 | + expect(new Set(out.map((f) => f.targetBlockId))).toEqual(new Set(['tgt-blk-1', 'tgt-blk-2'])) |
| 124 | + }) |
| 125 | + |
| 126 | + it('drops the fields rather than failing the whole diff when a target will not resolve', async () => { |
| 127 | + // An unresolvable target is already a sync blocker via the mapping's own existence check; |
| 128 | + // throwing here would hide every other finding in the diff. |
| 129 | + mockResolveBinding.mockResolvedValue(null) |
| 130 | + |
| 131 | + expect( |
| 132 | + await collectForkCustomBlockReconfigs({ ...baseParams, resolve: swapResolver }) |
| 133 | + ).toHaveLength(0) |
| 134 | + }) |
| 135 | + |
| 136 | + it('survives a binding lookup that throws', async () => { |
| 137 | + mockResolveBinding.mockRejectedValue(new Error('deployment read failed')) |
| 138 | + |
| 139 | + expect( |
| 140 | + await collectForkCustomBlockReconfigs({ ...baseParams, resolve: swapResolver }) |
| 141 | + ).toHaveLength(0) |
| 142 | + }) |
| 143 | +}) |
0 commit comments