|
| 1 | +import { createReverseMappingUtility } from '../ReverseMappingUtility'; |
| 2 | +import type { TPoint } from '../../@types/tower'; |
| 3 | +import WorkspaceManager from '../../workspace/model/model'; |
| 4 | +import { |
| 5 | + createSimpleBrick, |
| 6 | + createExpressionBrick, |
| 7 | + createCompoundBrick, |
| 8 | + resetFactoryCounter, |
| 9 | +} from '../../brick/utils/brickFactory'; |
| 10 | + |
| 11 | +describe('ReverseMappingUtility', () => { |
| 12 | + let workspaceManager: WorkspaceManager; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + resetFactoryCounter(); |
| 16 | + workspaceManager = new WorkspaceManager(); |
| 17 | + }); |
| 18 | + |
| 19 | + function setupTowers() { |
| 20 | + const reverseMapper = createReverseMappingUtility(workspaceManager); |
| 21 | + |
| 22 | + // Tower 1: Simple vertical stack |
| 23 | + const root1 = createSimpleBrick({ label: 'Root Brick' }); |
| 24 | + const child1 = createSimpleBrick({ label: 'Child 1' }); |
| 25 | + const child2 = createSimpleBrick({ label: 'Child 2' }); |
| 26 | + |
| 27 | + const tower1 = workspaceManager.createTower(root1, { x: 100, y: 100 }); |
| 28 | + tower1.addBrick(root1.uuid, child1, { x: 100, y: 150 }); |
| 29 | + tower1.addBrick(child1.uuid, child2, { x: 100, y: 200 }); |
| 30 | + |
| 31 | + // Tower 2: Compound brick with argument bricks |
| 32 | + const compound = createCompoundBrick({ |
| 33 | + label: 'Compound Brick', |
| 34 | + bboxArgs: [ |
| 35 | + { w: 80, h: 30 }, |
| 36 | + { w: 60, h: 25 }, |
| 37 | + ], |
| 38 | + }); |
| 39 | + const arg1 = createExpressionBrick({ label: 'Arg 1' }); |
| 40 | + const arg2 = createExpressionBrick({ label: 'Arg 2' }); |
| 41 | + |
| 42 | + const tower2 = workspaceManager.createTower(compound, { x: 400, y: 100 }); |
| 43 | + tower2.addArgumentBrick(compound.uuid, arg1, { x: 0, y: 0 }, 0); |
| 44 | + tower2.addArgumentBrick(compound.uuid, arg2, { x: 0, y: 0 }, 1); |
| 45 | + |
| 46 | + // Tower 3: Compound brick with nested bricks |
| 47 | + const compound2 = createCompoundBrick({ |
| 48 | + label: 'Nested Brick', |
| 49 | + bboxArgs: [{ w: 70, h: 40 }], |
| 50 | + }); |
| 51 | + const nested1 = createSimpleBrick({ label: 'Nested 1' }); |
| 52 | + const nested2 = createSimpleBrick({ label: 'Nested 2' }); |
| 53 | + |
| 54 | + const tower3 = workspaceManager.createTower(compound2, { x: 700, y: 100 }); |
| 55 | + tower3.addNestedBrick(compound2.uuid, nested1, { x: 0, y: 0 }); |
| 56 | + tower3.addNestedBrick(compound2.uuid, nested2, { x: 0, y: 0 }); |
| 57 | + |
| 58 | + return { |
| 59 | + reverseMapper, |
| 60 | + brickRefs: { |
| 61 | + root1, |
| 62 | + child1, |
| 63 | + child2, |
| 64 | + compound, |
| 65 | + arg1, |
| 66 | + arg2, |
| 67 | + compound2, |
| 68 | + nested1, |
| 69 | + nested2, |
| 70 | + }, |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + it('should find a brick at a known location', () => { |
| 75 | + const { reverseMapper } = setupTowers(); |
| 76 | + const testPoint: TPoint = { x: 120, y: 110 }; // Inside root1 brick area |
| 77 | + |
| 78 | + const result = reverseMapper.findBrickAtPoint(testPoint); |
| 79 | + |
| 80 | + expect(result.brick).not.toBeNull(); |
| 81 | + expect(result.tower).not.toBeNull(); |
| 82 | + expect(result.towerNode).not.toBeNull(); |
| 83 | + expect(result.brickLocalPoint).not.toBeNull(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should return null for a point that does not intersect any brick', () => { |
| 87 | + const { reverseMapper } = setupTowers(); |
| 88 | + const testPoint: TPoint = { x: 1000, y: 1000 }; |
| 89 | + |
| 90 | + const result = reverseMapper.findBrickAtPoint(testPoint); |
| 91 | + |
| 92 | + expect(result.brick).toBeNull(); |
| 93 | + expect(result.tower).toBeNull(); |
| 94 | + expect(result.towerNode).toBeNull(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should find multiple bricks within a given area', () => { |
| 98 | + const { reverseMapper } = setupTowers(); |
| 99 | + const topLeft: TPoint = { x: 50, y: 50 }; |
| 100 | + const bottomRight: TPoint = { x: 500, y: 300 }; |
| 101 | + |
| 102 | + const results = reverseMapper.findBricksInArea(topLeft, bottomRight); |
| 103 | + |
| 104 | + expect(results.length).toBeGreaterThan(0); |
| 105 | + expect(results.every((r) => r.brick !== null)).toBe(true); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should return all connection points for a brick by ID', () => { |
| 109 | + const { reverseMapper, brickRefs } = setupTowers(); |
| 110 | + |
| 111 | + const result = reverseMapper.getBrickConnectionPoints(brickRefs.root1.uuid); |
| 112 | + |
| 113 | + expect(Array.isArray(result)).toBe(true); |
| 114 | + expect(result.length).toBeGreaterThan(0); |
| 115 | + result.forEach((conn) => { |
| 116 | + expect(conn.point).toHaveProperty('x'); |
| 117 | + expect(conn.point).toHaveProperty('y'); |
| 118 | + expect(conn.type).toMatch(/top|right|bottom|left/); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should not return connection points for a nonexistent brick', () => { |
| 123 | + const reverseMapper = createReverseMappingUtility(workspaceManager); |
| 124 | + const result = reverseMapper.getBrickConnectionPoints('nonexistent-brick-id'); |
| 125 | + expect(result.length).toBe(0); |
| 126 | + }); |
| 127 | +}); |
0 commit comments