import { describe, it, expect } from 'vitest'; import { applyAlign, shuffle, moveToRegion, moveToRegionAll, removeFromRegion, type Region, type RegionAxis } from '@/core/region'; import { createRNG } from '@/utils/rng'; import { entity, Entity } from '@/utils/entity'; import { type Part } from '@/core/part'; describe('Region', () => { function createTestRegion(axes: RegionAxis[], parts: Part[]): Entity { const partEntities = parts.map(p => entity(p.id, p)); return entity('region1', { id: 'region1', axes: [...axes], children: partEntities, }); } describe('applyAlign', () => { it('should do nothing with empty region', () => { const region = createTestRegion([{ name: 'x', min: 0, align: 'start' }], []); applyAlign(region); expect(region.value.children).toHaveLength(0); }); it('should align parts to start on first axis', () => { const part1: Part = { id: 'p1', region: null as any, position: [5, 10] }; const part2: Part = { id: 'p2', region: null as any, position: [7, 20] }; const part3: Part = { id: 'p3', region: null as any, position: [2, 30] }; const region = createTestRegion( [{ name: 'x', min: 0, align: 'start' }, { name: 'y' }], [part1, part2, part3] ); applyAlign(region); expect(region.value.children[0].value.position[0]).toBe(0); expect(region.value.children[1].value.position[0]).toBe(1); expect(region.value.children[2].value.position[0]).toBe(2); expect(region.value.children[0].value.position[1]).toBe(30); expect(region.value.children[1].value.position[1]).toBe(10); expect(region.value.children[2].value.position[1]).toBe(20); }); it('should align parts to start with custom min', () => { const part1: Part = { id: 'p1', region: null as any, position: [5, 100] }; const part2: Part = { id: 'p2', region: null as any, position: [7, 200] }; const region = createTestRegion( [{ name: 'x', min: 10, align: 'start' }, { name: 'y' }], [part1, part2] ); applyAlign(region); expect(region.value.children[0].value.position[0]).toBe(10); expect(region.value.children[1].value.position[0]).toBe(11); expect(region.value.children[0].value.position[1]).toBe(100); expect(region.value.children[1].value.position[1]).toBe(200); }); it('should align parts to end on first axis', () => { const part1: Part = { id: 'p1', region: null as any, position: [2, 50] }; const part2: Part = { id: 'p2', region: null as any, position: [4, 60] }; const part3: Part = { id: 'p3', region: null as any, position: [1, 70] }; const region = createTestRegion( [{ name: 'x', max: 10, align: 'end' }, { name: 'y' }], [part1, part2, part3] ); applyAlign(region); expect(region.value.children[0].value.position[0]).toBe(8); expect(region.value.children[1].value.position[0]).toBe(9); expect(region.value.children[2].value.position[0]).toBe(10); }); it('should align parts to center on first axis', () => { const part1: Part = { id: 'p1', region: null as any, position: [0, 5] }; const part2: Part = { id: 'p2', region: null as any, position: [1, 6] }; const part3: Part = { id: 'p3', region: null as any, position: [2, 7] }; const region = createTestRegion( [{ name: 'x', min: 0, max: 10, align: 'center' }, { name: 'y' }], [part1, part2, part3] ); applyAlign(region); expect(region.value.children[0].value.position[0]).toBe(4); expect(region.value.children[1].value.position[0]).toBe(5); expect(region.value.children[2].value.position[0]).toBe(6); }); it('should handle even count center alignment', () => { const part1: Part = { id: 'p1', region: null as any, position: [0, 10] }; const part2: Part = { id: 'p2', region: null as any, position: [1, 20] }; const region = createTestRegion( [{ name: 'x', min: 0, max: 10, align: 'center' }, { name: 'y' }], [part1, part2] ); applyAlign(region); expect(region.value.children[0].value.position[0]).toBe(4.5); expect(region.value.children[1].value.position[0]).toBe(5.5); }); it('should sort children by position on current axis', () => { const part1: Part = { id: 'p1', region: null as any, position: [5, 100] }; const part2: Part = { id: 'p2', region: null as any, position: [1, 200] }; const part3: Part = { id: 'p3', region: null as any, position: [3, 300] }; const region = createTestRegion( [{ name: 'x', min: 0, align: 'start' }, { name: 'y' }], [part1, part2, part3] ); applyAlign(region); expect(region.value.children[0].value.id).toBe('p2'); expect(region.value.children[1].value.id).toBe('p3'); expect(region.value.children[2].value.id).toBe('p1'); }); it('should align on multiple axes', () => { const part1: Part = { id: 'p1', region: null as any, position: [5, 10] }; const part2: Part = { id: 'p2', region: null as any, position: [7, 20] }; const part3: Part = { id: 'p3', region: null as any, position: [2, 30] }; const region = createTestRegion( [ { name: 'x', min: 0, align: 'start' }, { name: 'y', min: 0, align: 'start' } ], [part1, part2, part3] ); applyAlign(region); const positions = region.value.children.map(c => ({ id: c.value.id, position: c.value.position })); expect(positions[0].id).toBe('p3'); expect(positions[0].position).toEqual([0, 2]); expect(positions[1].id).toBe('p1'); expect(positions[1].position).toEqual([1, 0]); expect(positions[2].id).toBe('p2'); expect(positions[2].position).toEqual([2, 1]); }); it('should align 4 elements on rectangle corners', () => { const part1: Part = { id: 'p1', region: null as any, position: [0, 0] }; const part2: Part = { id: 'p2', region: null as any, position: [10, 0] }; const part3: Part = { id: 'p3', region: null as any, position: [10, 1] }; const part4: Part = { id: 'p4', region: null as any, position: [0, 1] }; const region = createTestRegion( [ { name: 'x', min: 0, max: 10, align: 'start' }, { name: 'y', min: 0, max: 10, align: 'start' } ], [part1, part2, part3, part4] ); applyAlign(region); const positions = region.value.children.map(c => ({ id: c.value.id, position: c.value.position })); expect(positions[0].id).toBe('p1'); expect(positions[0].position).toEqual([0, 0]); expect(positions[1].id).toBe('p4'); expect(positions[1].position).toEqual([0, 1]); expect(positions[2].id).toBe('p2'); expect(positions[2].position).toEqual([1, 0]); expect(positions[3].id).toBe('p3'); expect(positions[3].position).toEqual([1, 1]); }); }); describe('shuffle', () => { it('should do nothing with empty region', () => { const region = createTestRegion([], []); const rng = createRNG(42); shuffle(region, rng); expect(region.value.children).toHaveLength(0); }); it('should do nothing with single part', () => { const part: Part = { id: 'p1', region: null as any, position: [0, 0, 0] }; const region = createTestRegion([], [part]); const rng = createRNG(42); shuffle(region, rng); expect(region.value.children[0].value.position).toEqual([0, 0, 0]); }); it('should shuffle positions of multiple parts', () => { const part1: Part = { id: 'p1', region: null as any, position: [0, 100] }; const part2: Part = { id: 'p2', region: null as any, position: [1, 200] }; const part3: Part = { id: 'p3', region: null as any, position: [2, 300] }; const region = createTestRegion([], [part1, part2, part3]); const rng = createRNG(42); const originalPositions = region.value.children.map(c => [...c.value.position]); shuffle(region, rng); const newPositions = region.value.children.map(c => c.value.position); originalPositions.forEach(origPos => { const found = newPositions.some(newPos => newPos[0] === origPos[0] && newPos[1] === origPos[1] ); expect(found).toBe(true); }); }); it('should be deterministic with same seed', () => { const createRegionForTest = () => { const part1: Part = { id: 'p1', region: null as any, position: [0, 10] }; const part2: Part = { id: 'p2', region: null as any, position: [1, 20] }; const part3: Part = { id: 'p3', region: null as any, position: [2, 30] }; return createTestRegion([], [part1, part2, part3]); }; const setup1 = createRegionForTest(); const setup2 = createRegionForTest(); const rng1 = createRNG(42); const rng2 = createRNG(42); shuffle(setup1, rng1); shuffle(setup2, rng2); const positions1 = setup1.value.children.map(c => c.value.position); const positions2 = setup2.value.children.map(c => c.value.position); expect(positions1).toEqual(positions2); }); it('should produce different results with different seeds', () => { const createRegionForTest = () => { const part1: Part = { id: 'p1', region: null as any, position: [0, 10] }; const part2: Part = { id: 'p2', region: null as any, position: [1, 20] }; const part3: Part = { id: 'p3', region: null as any, position: [2, 30] }; const part4: Part = { id: 'p4', region: null as any, position: [3, 40] }; const part5: Part = { id: 'p5', region: null as any, position: [4, 50] }; return createTestRegion([], [part1, part2, part3, part4, part5]); }; const results = new Set(); for (let seed = 1; seed <= 10; seed++) { const setup = createRegionForTest(); const rng = createRNG(seed); shuffle(setup, rng); const positions = JSON.stringify(setup.value.children.map(c => c.value.position)); results.add(positions); } expect(results.size).toBeGreaterThan(5); }); }); describe('moveToRegion', () => { it('should move a part from one region to another', () => { const sourceAxes: RegionAxis[] = [{ name: 'x', min: 0, max: 5 }]; const targetAxes: RegionAxis[] = [{ name: 'x', min: 0, max: 5 }]; const sourceRegion = createTestRegion(sourceAxes, []); const targetRegion = createTestRegion(targetAxes, []); const part: Part = { id: 'p1', region: sourceRegion, position: [2] }; const partEntity = entity(part.id, part); sourceRegion.value.children.push(partEntity); expect(sourceRegion.value.children).toHaveLength(1); expect(targetRegion.value.children).toHaveLength(0); expect(partEntity.value.region.value.id).toBe('region1'); moveToRegion(partEntity, targetRegion, [0]); expect(sourceRegion.value.children).toHaveLength(0); expect(targetRegion.value.children).toHaveLength(1); expect(partEntity.value.region.value.id).toBe('region1'); expect(partEntity.value.position).toEqual([0]); }); it('should keep existing position if no position provided', () => { const sourceRegion = createTestRegion([{ name: 'x' }], []); const targetRegion = createTestRegion([{ name: 'x' }], []); const part: Part = { id: 'p1', region: sourceRegion, position: [3] }; const partEntity = entity(part.id, part); sourceRegion.value.children.push(partEntity); moveToRegion(partEntity, targetRegion); expect(partEntity.value.position).toEqual([3]); }); }); describe('moveToRegionAll', () => { it('should move multiple parts to a target region', () => { const sourceRegion = createTestRegion([{ name: 'x' }], []); const targetRegion = createTestRegion([{ name: 'x' }], []); const parts = [ entity('p1', { id: 'p1', region: sourceRegion, position: [0] } as Part), entity('p2', { id: 'p2', region: sourceRegion, position: [1] } as Part), entity('p3', { id: 'p3', region: sourceRegion, position: [2] } as Part), ]; sourceRegion.value.children.push(...parts); moveToRegionAll(parts, targetRegion, [[0], [1], [2]]); expect(sourceRegion.value.children).toHaveLength(0); expect(targetRegion.value.children).toHaveLength(3); expect(parts[0].value.position).toEqual([0]); expect(parts[1].value.position).toEqual([1]); expect(parts[2].value.position).toEqual([2]); }); it('should keep existing positions if no positions provided', () => { const sourceRegion = createTestRegion([{ name: 'x' }], []); const targetRegion = createTestRegion([{ name: 'x' }], []); const parts = [ entity('p1', { id: 'p1', region: sourceRegion, position: [5] } as Part), entity('p2', { id: 'p2', region: sourceRegion, position: [8] } as Part), ]; sourceRegion.value.children.push(...parts); moveToRegionAll(parts, targetRegion); expect(parts[0].value.position).toEqual([5]); expect(parts[1].value.position).toEqual([8]); }); }); describe('removeFromRegion', () => { it('should remove a part from its region', () => { const region = createTestRegion([{ name: 'x' }], []); const part: Part = { id: 'p1', region: region, position: [2] }; const partEntity = entity(part.id, part); region.value.children.push(partEntity); expect(region.value.children).toHaveLength(1); removeFromRegion(partEntity); expect(region.value.children).toHaveLength(0); }); it('should leave other parts unaffected', () => { const region = createTestRegion([{ name: 'x' }], []); const p1 = entity('p1', { id: 'p1', region: region, position: [0] } as Part); const p2 = entity('p2', { id: 'p2', region: region, position: [1] } as Part); const p3 = entity('p3', { id: 'p3', region: region, position: [2] } as Part); region.value.children.push(p1, p2, p3); removeFromRegion(p2); expect(region.value.children).toHaveLength(2); expect(region.value.children.map(c => c.value.id)).toEqual(['p1', 'p3']); }); }); });