|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { applyTargetedLayout } from '@/lib/workflows/autolayout/targeted' |
| 6 | +import type { Edge } from '@/lib/workflows/autolayout/types' |
| 7 | +import { getBlockMetrics } from '@/lib/workflows/autolayout/utils' |
| 8 | +import type { BlockState } from '@/stores/workflows/workflow/types' |
| 9 | + |
| 10 | +function createBlock(id: string, overrides: Partial<BlockState> = {}): BlockState { |
| 11 | + return { |
| 12 | + id, |
| 13 | + type: 'function', |
| 14 | + name: id, |
| 15 | + position: { x: 0, y: 0 }, |
| 16 | + subBlocks: {}, |
| 17 | + outputs: {}, |
| 18 | + enabled: true, |
| 19 | + ...overrides, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +describe('applyTargetedLayout', () => { |
| 24 | + it('shifts downstream frozen blocks when only shift sources are provided', () => { |
| 25 | + const blocks = { |
| 26 | + source: createBlock('source', { |
| 27 | + position: { x: 100, y: 100 }, |
| 28 | + }), |
| 29 | + target: createBlock('target', { |
| 30 | + position: { x: 400, y: 100 }, |
| 31 | + }), |
| 32 | + end: createBlock('end', { |
| 33 | + position: { x: 760, y: 100 }, |
| 34 | + }), |
| 35 | + } |
| 36 | + const edges: Edge[] = [ |
| 37 | + { |
| 38 | + id: 'edge-1', |
| 39 | + source: 'source', |
| 40 | + target: 'target', |
| 41 | + sourceHandle: 'source', |
| 42 | + targetHandle: 'target', |
| 43 | + }, |
| 44 | + { |
| 45 | + id: 'edge-2', |
| 46 | + source: 'target', |
| 47 | + target: 'end', |
| 48 | + sourceHandle: 'source', |
| 49 | + targetHandle: 'target', |
| 50 | + }, |
| 51 | + ] |
| 52 | + |
| 53 | + const result = applyTargetedLayout(blocks, edges, { |
| 54 | + changedBlockIds: [], |
| 55 | + shiftSourceBlockIds: ['source'], |
| 56 | + }) |
| 57 | + |
| 58 | + expect(result.source.position).toEqual({ x: 100, y: 100 }) |
| 59 | + expect(result.target.position).toEqual({ x: 530, y: 100 }) |
| 60 | + expect(result.end.position).toEqual({ x: 960, y: 100 }) |
| 61 | + }) |
| 62 | + |
| 63 | + it('places new linear blocks without moving anchors', () => { |
| 64 | + const blocks = { |
| 65 | + anchor: createBlock('anchor', { |
| 66 | + position: { x: 150, y: 150 }, |
| 67 | + }), |
| 68 | + changed: createBlock('changed', { |
| 69 | + position: { x: 0, y: 0 }, |
| 70 | + }), |
| 71 | + } |
| 72 | + const edges: Edge[] = [ |
| 73 | + { |
| 74 | + id: 'edge-1', |
| 75 | + source: 'anchor', |
| 76 | + target: 'changed', |
| 77 | + }, |
| 78 | + ] |
| 79 | + |
| 80 | + const result = applyTargetedLayout(blocks, edges, { |
| 81 | + changedBlockIds: ['changed'], |
| 82 | + }) |
| 83 | + |
| 84 | + expect(result.anchor.position).toEqual({ x: 150, y: 150 }) |
| 85 | + expect(result.changed.position.x).toBeGreaterThan(result.anchor.position.x) |
| 86 | + expect(result.changed.position.y).toBe(result.anchor.position.y) |
| 87 | + }) |
| 88 | + |
| 89 | + it('keeps root-level insertions closer to anchored blocks near the top of the canvas', () => { |
| 90 | + const blocks = { |
| 91 | + start: createBlock('start', { |
| 92 | + position: { x: 0, y: 0 }, |
| 93 | + }), |
| 94 | + changed: createBlock('changed', { |
| 95 | + position: { x: 0, y: 0 }, |
| 96 | + }), |
| 97 | + agent: createBlock('agent', { |
| 98 | + position: { x: 410.94, y: 2.33 }, |
| 99 | + }), |
| 100 | + } |
| 101 | + const edges: Edge[] = [ |
| 102 | + { |
| 103 | + id: 'edge-1', |
| 104 | + source: 'start', |
| 105 | + target: 'changed', |
| 106 | + }, |
| 107 | + { |
| 108 | + id: 'edge-2', |
| 109 | + source: 'changed', |
| 110 | + target: 'agent', |
| 111 | + }, |
| 112 | + ] |
| 113 | + |
| 114 | + const result = applyTargetedLayout(blocks, edges, { |
| 115 | + changedBlockIds: ['changed'], |
| 116 | + }) |
| 117 | + |
| 118 | + expect(result.changed.position.y).toBeLessThan(150) |
| 119 | + }) |
| 120 | + |
| 121 | + it('places new parallel children below tall anchored siblings', () => { |
| 122 | + const blocks = { |
| 123 | + parallel: createBlock('parallel', { |
| 124 | + type: 'parallel', |
| 125 | + position: { x: 200, y: 150 }, |
| 126 | + data: { width: 600, height: 500 }, |
| 127 | + layout: { measuredWidth: 600, measuredHeight: 500 }, |
| 128 | + }), |
| 129 | + existing: createBlock('existing', { |
| 130 | + position: { x: 180, y: 100 }, |
| 131 | + data: { parentId: 'parallel', extent: 'parent' }, |
| 132 | + layout: { measuredWidth: 250, measuredHeight: 220 }, |
| 133 | + height: 220, |
| 134 | + }), |
| 135 | + changed: createBlock('changed', { |
| 136 | + position: { x: 0, y: 0 }, |
| 137 | + data: { parentId: 'parallel', extent: 'parent' }, |
| 138 | + }), |
| 139 | + } |
| 140 | + |
| 141 | + const result = applyTargetedLayout(blocks, [], { |
| 142 | + changedBlockIds: ['changed'], |
| 143 | + }) |
| 144 | + |
| 145 | + const existingMetrics = getBlockMetrics(result.existing) |
| 146 | + expect(result.parallel.position).toEqual({ x: 200, y: 150 }) |
| 147 | + expect(result.changed.position.y).toBeGreaterThanOrEqual( |
| 148 | + result.existing.position.y + existingMetrics.height |
| 149 | + ) |
| 150 | + }) |
| 151 | +}) |
0 commit comments