2026-04-01 17:48:40 +08:00
|
|
|
import { describe, it, expect } from 'vitest';
|
2026-04-03 12:46:02 +08:00
|
|
|
import { createRegion, applyAlign, shuffle, moveToRegion, moveToRegionAll, removeFromRegion, type Region, type RegionAxis } from '@/core/region';
|
2026-04-02 15:59:27 +08:00
|
|
|
import { createRNG } from '@/utils/rng';
|
|
|
|
|
import { type Part } from '@/core/part';
|
2026-04-01 17:34:21 +08:00
|
|
|
|
|
|
|
|
describe('Region', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
function createTestRegion(axes: RegionAxis[], parts: Part[]): { region: Region; parts: Record<string, Part> } {
|
|
|
|
|
const partsMap: Record<string, Part> = {};
|
|
|
|
|
for (const p of parts) {
|
|
|
|
|
partsMap[p.id] = { ...p };
|
|
|
|
|
}
|
|
|
|
|
const region = createRegion('region1', axes);
|
|
|
|
|
region.childIds = parts.map(p => p.id);
|
|
|
|
|
region.partMap = Object.fromEntries(
|
|
|
|
|
parts.map(p => [p.position.join(','), p.id])
|
|
|
|
|
);
|
|
|
|
|
return { region, parts: partsMap };
|
2026-04-01 17:34:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('applyAlign', () => {
|
|
|
|
|
it('should do nothing with empty region', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const { region } = createTestRegion([{ name: 'x', min: 0, align: 'start' }], []);
|
|
|
|
|
applyAlign(region, {});
|
|
|
|
|
expect(region.childIds).toHaveLength(0);
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
|
2026-04-01 17:48:40 +08:00
|
|
|
it('should align parts to start on first axis', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const part1: Part = { id: 'p1', regionId: 'region1', position: [5, 10] };
|
|
|
|
|
const part2: Part = { id: 'p2', regionId: 'region1', position: [7, 20] };
|
|
|
|
|
const part3: Part = { id: 'p3', regionId: 'region1', position: [2, 30] };
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const { region, parts } = createTestRegion(
|
2026-04-01 17:48:40 +08:00
|
|
|
[{ name: 'x', min: 0, align: 'start' }, { name: 'y' }],
|
2026-04-01 17:34:21 +08:00
|
|
|
[part1, part2, part3]
|
|
|
|
|
);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
applyAlign(region, parts);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(parts[region.childIds[0]].position[0]).toBe(0);
|
|
|
|
|
expect(parts[region.childIds[1]].position[0]).toBe(1);
|
|
|
|
|
expect(parts[region.childIds[2]].position[0]).toBe(2);
|
|
|
|
|
expect(parts[region.childIds[0]].position[1]).toBe(30);
|
|
|
|
|
expect(parts[region.childIds[1]].position[1]).toBe(10);
|
|
|
|
|
expect(parts[region.childIds[2]].position[1]).toBe(20);
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should align parts to start with custom min', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const part1: Part = { id: 'p1', regionId: 'region1', position: [5, 100] };
|
|
|
|
|
const part2: Part = { id: 'p2', regionId: 'region1', position: [7, 200] };
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const { region, parts } = createTestRegion(
|
2026-04-01 17:48:40 +08:00
|
|
|
[{ name: 'x', min: 10, align: 'start' }, { name: 'y' }],
|
2026-04-01 17:34:21 +08:00
|
|
|
[part1, part2]
|
|
|
|
|
);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
applyAlign(region, parts);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(parts[region.childIds[0]].position[0]).toBe(10);
|
|
|
|
|
expect(parts[region.childIds[1]].position[0]).toBe(11);
|
|
|
|
|
expect(parts[region.childIds[0]].position[1]).toBe(100);
|
|
|
|
|
expect(parts[region.childIds[1]].position[1]).toBe(200);
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
|
2026-04-01 17:48:40 +08:00
|
|
|
it('should align parts to end on first axis', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const part1: Part = { id: 'p1', regionId: 'region1', position: [2, 50] };
|
|
|
|
|
const part2: Part = { id: 'p2', regionId: 'region1', position: [4, 60] };
|
|
|
|
|
const part3: Part = { id: 'p3', regionId: 'region1', position: [1, 70] };
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const { region, parts } = createTestRegion(
|
2026-04-01 17:48:40 +08:00
|
|
|
[{ name: 'x', max: 10, align: 'end' }, { name: 'y' }],
|
2026-04-01 17:34:21 +08:00
|
|
|
[part1, part2, part3]
|
|
|
|
|
);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
applyAlign(region, parts);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(parts[region.childIds[0]].position[0]).toBe(8);
|
|
|
|
|
expect(parts[region.childIds[1]].position[0]).toBe(9);
|
|
|
|
|
expect(parts[region.childIds[2]].position[0]).toBe(10);
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
|
2026-04-01 17:48:40 +08:00
|
|
|
it('should align parts to center on first axis', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const part1: Part = { id: 'p1', regionId: 'region1', position: [0, 5] };
|
|
|
|
|
const part2: Part = { id: 'p2', regionId: 'region1', position: [1, 6] };
|
|
|
|
|
const part3: Part = { id: 'p3', regionId: 'region1', position: [2, 7] };
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const { region, parts } = createTestRegion(
|
2026-04-01 17:48:40 +08:00
|
|
|
[{ name: 'x', min: 0, max: 10, align: 'center' }, { name: 'y' }],
|
2026-04-01 17:34:21 +08:00
|
|
|
[part1, part2, part3]
|
|
|
|
|
);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
applyAlign(region, parts);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(parts[region.childIds[0]].position[0]).toBe(4);
|
|
|
|
|
expect(parts[region.childIds[1]].position[0]).toBe(5);
|
|
|
|
|
expect(parts[region.childIds[2]].position[0]).toBe(6);
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle even count center alignment', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const part1: Part = { id: 'p1', regionId: 'region1', position: [0, 10] };
|
|
|
|
|
const part2: Part = { id: 'p2', regionId: 'region1', position: [1, 20] };
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const { region, parts } = createTestRegion(
|
2026-04-01 17:48:40 +08:00
|
|
|
[{ name: 'x', min: 0, max: 10, align: 'center' }, { name: 'y' }],
|
2026-04-01 17:34:21 +08:00
|
|
|
[part1, part2]
|
|
|
|
|
);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
applyAlign(region, parts);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(parts[region.childIds[0]].position[0]).toBe(4.5);
|
|
|
|
|
expect(parts[region.childIds[1]].position[0]).toBe(5.5);
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
|
2026-04-01 17:48:40 +08:00
|
|
|
it('should sort children by position on current axis', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const part1: Part = { id: 'p1', regionId: 'region1', position: [5, 100] };
|
|
|
|
|
const part2: Part = { id: 'p2', regionId: 'region1', position: [1, 200] };
|
|
|
|
|
const part3: Part = { id: 'p3', regionId: 'region1', position: [3, 300] };
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const { region, parts } = createTestRegion(
|
2026-04-01 17:48:40 +08:00
|
|
|
[{ name: 'x', min: 0, align: 'start' }, { name: 'y' }],
|
2026-04-01 17:34:21 +08:00
|
|
|
[part1, part2, part3]
|
|
|
|
|
);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
applyAlign(region, parts);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(region.childIds[0]).toBe('p2');
|
|
|
|
|
expect(region.childIds[1]).toBe('p3');
|
|
|
|
|
expect(region.childIds[2]).toBe('p1');
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
2026-04-01 17:48:40 +08:00
|
|
|
|
|
|
|
|
it('should align on multiple axes', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const part1: Part = { id: 'p1', regionId: 'region1', position: [5, 10] };
|
|
|
|
|
const part2: Part = { id: 'p2', regionId: 'region1', position: [7, 20] };
|
|
|
|
|
const part3: Part = { id: 'p3', regionId: 'region1', position: [2, 30] };
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const { region, parts } = createTestRegion(
|
2026-04-01 17:48:40 +08:00
|
|
|
[
|
|
|
|
|
{ name: 'x', min: 0, align: 'start' },
|
|
|
|
|
{ name: 'y', min: 0, align: 'start' }
|
|
|
|
|
],
|
|
|
|
|
[part1, part2, part3]
|
|
|
|
|
);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
applyAlign(region, parts);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(region.childIds[0]).toBe('p3');
|
|
|
|
|
expect(parts[region.childIds[0]].position).toEqual([0, 2]);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(region.childIds[1]).toBe('p1');
|
|
|
|
|
expect(parts[region.childIds[1]].position).toEqual([1, 0]);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(region.childIds[2]).toBe('p2');
|
|
|
|
|
expect(parts[region.childIds[2]].position).toEqual([2, 1]);
|
2026-04-01 18:33:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should align 4 elements on rectangle corners', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const part1: Part = { id: 'p1', regionId: 'region1', position: [0, 0] };
|
|
|
|
|
const part2: Part = { id: 'p2', regionId: 'region1', position: [10, 0] };
|
|
|
|
|
const part3: Part = { id: 'p3', regionId: 'region1', position: [10, 1] };
|
|
|
|
|
const part4: Part = { id: 'p4', regionId: 'region1', position: [0, 1] };
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const { region, parts } = createTestRegion(
|
2026-04-01 18:33:09 +08:00
|
|
|
[
|
|
|
|
|
{ name: 'x', min: 0, max: 10, align: 'start' },
|
|
|
|
|
{ name: 'y', min: 0, max: 10, align: 'start' }
|
|
|
|
|
],
|
|
|
|
|
[part1, part2, part3, part4]
|
|
|
|
|
);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
applyAlign(region, parts);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(region.childIds[0]).toBe('p1');
|
|
|
|
|
expect(parts[region.childIds[0]].position).toEqual([0, 0]);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(region.childIds[1]).toBe('p4');
|
|
|
|
|
expect(parts[region.childIds[1]].position).toEqual([0, 1]);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(region.childIds[2]).toBe('p2');
|
|
|
|
|
expect(parts[region.childIds[2]].position).toEqual([1, 0]);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(region.childIds[3]).toBe('p3');
|
|
|
|
|
expect(parts[region.childIds[3]].position).toEqual([1, 1]);
|
2026-04-01 17:48:40 +08:00
|
|
|
});
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('shuffle', () => {
|
|
|
|
|
it('should do nothing with empty region', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const { region, parts } = createTestRegion([], []);
|
2026-04-01 17:34:21 +08:00
|
|
|
const rng = createRNG(42);
|
2026-04-03 12:46:02 +08:00
|
|
|
shuffle(region, parts, rng);
|
|
|
|
|
expect(region.childIds).toHaveLength(0);
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should do nothing with single part', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const part: Part = { id: 'p1', regionId: 'region1', position: [0, 0, 0] };
|
|
|
|
|
const { region, parts } = createTestRegion([], [part]);
|
2026-04-01 17:34:21 +08:00
|
|
|
const rng = createRNG(42);
|
2026-04-03 12:46:02 +08:00
|
|
|
shuffle(region, parts, rng);
|
|
|
|
|
expect(parts['p1'].position).toEqual([0, 0, 0]);
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should shuffle positions of multiple parts', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const part1: Part = { id: 'p1', regionId: 'region1', position: [0, 100] };
|
|
|
|
|
const part2: Part = { id: 'p2', regionId: 'region1', position: [1, 200] };
|
|
|
|
|
const part3: Part = { id: 'p3', regionId: 'region1', position: [2, 300] };
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const { region, parts } = createTestRegion([], [part1, part2, part3]);
|
2026-04-01 17:34:21 +08:00
|
|
|
const rng = createRNG(42);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const originalPositions = region.childIds.map(id => [...parts[id].position]);
|
|
|
|
|
shuffle(region, parts, rng);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const newPositions = region.childIds.map(id => parts[id].position);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-01 17:34:21 +08:00
|
|
|
originalPositions.forEach(origPos => {
|
2026-04-02 15:16:30 +08:00
|
|
|
const found = newPositions.some(newPos =>
|
2026-04-01 17:34:21 +08:00
|
|
|
newPos[0] === origPos[0] && newPos[1] === origPos[1]
|
|
|
|
|
);
|
|
|
|
|
expect(found).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be deterministic with same seed', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const createPartsForTest = (): Part[] => [
|
|
|
|
|
{ id: 'p1', regionId: 'region1', position: [0, 10] },
|
|
|
|
|
{ id: 'p2', regionId: 'region1', position: [1, 20] },
|
|
|
|
|
{ id: 'p3', regionId: 'region1', position: [2, 30] },
|
|
|
|
|
];
|
2026-04-01 17:34:21 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const setup1 = createTestRegion([], createPartsForTest());
|
|
|
|
|
const setup2 = createTestRegion([], createPartsForTest());
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-01 17:34:21 +08:00
|
|
|
const rng1 = createRNG(42);
|
|
|
|
|
const rng2 = createRNG(42);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
shuffle(setup1.region, setup1.parts, rng1);
|
|
|
|
|
shuffle(setup2.region, setup2.parts, rng2);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const positions1 = setup1.region.childIds.map(id => setup1.parts[id].position);
|
|
|
|
|
const positions2 = setup2.region.childIds.map(id => setup2.parts[id].position);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-01 17:34:21 +08:00
|
|
|
expect(positions1).toEqual(positions2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should produce different results with different seeds', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const createPartsForTest = (): Part[] => [
|
|
|
|
|
{ id: 'p1', regionId: 'region1', position: [0, 10] },
|
|
|
|
|
{ id: 'p2', regionId: 'region1', position: [1, 20] },
|
|
|
|
|
{ id: 'p3', regionId: 'region1', position: [2, 30] },
|
|
|
|
|
{ id: 'p4', regionId: 'region1', position: [3, 40] },
|
|
|
|
|
{ id: 'p5', regionId: 'region1', position: [4, 50] },
|
|
|
|
|
];
|
2026-04-01 17:48:40 +08:00
|
|
|
|
2026-04-01 17:34:21 +08:00
|
|
|
const results = new Set<string>();
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-01 17:34:21 +08:00
|
|
|
for (let seed = 1; seed <= 10; seed++) {
|
2026-04-03 12:46:02 +08:00
|
|
|
const setup = createTestRegion([], createPartsForTest());
|
2026-04-01 17:34:21 +08:00
|
|
|
const rng = createRNG(seed);
|
2026-04-03 12:46:02 +08:00
|
|
|
shuffle(setup.region, setup.parts, rng);
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const positions = JSON.stringify(setup.region.childIds.map(id => setup.parts[id].position));
|
2026-04-01 17:34:21 +08:00
|
|
|
results.add(positions);
|
|
|
|
|
}
|
2026-04-02 15:16:30 +08:00
|
|
|
|
2026-04-01 17:34:21 +08:00
|
|
|
expect(results.size).toBeGreaterThan(5);
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-04-02 21:58:11 +08:00
|
|
|
|
|
|
|
|
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 }];
|
2026-04-03 12:46:02 +08:00
|
|
|
const sourceRegion = createRegion('source', sourceAxes);
|
|
|
|
|
const targetRegion = createRegion('target', targetAxes);
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const part: Part = { id: 'p1', regionId: 'source', position: [2] };
|
|
|
|
|
const parts: Record<string, Part> = { p1: part };
|
|
|
|
|
sourceRegion.childIds.push('p1');
|
|
|
|
|
sourceRegion.partMap['2'] = 'p1';
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(sourceRegion.childIds).toHaveLength(1);
|
|
|
|
|
expect(targetRegion.childIds).toHaveLength(0);
|
|
|
|
|
expect(part.regionId).toBe('source');
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
moveToRegion(part, sourceRegion, targetRegion, [0]);
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(sourceRegion.childIds).toHaveLength(0);
|
|
|
|
|
expect(targetRegion.childIds).toHaveLength(1);
|
|
|
|
|
expect(part.regionId).toBe('target');
|
|
|
|
|
expect(part.position).toEqual([0]);
|
2026-04-02 21:58:11 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should keep existing position if no position provided', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const sourceRegion = createRegion('source', [{ name: 'x' }]);
|
|
|
|
|
const targetRegion = createRegion('target', [{ name: 'x' }]);
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const part: Part = { id: 'p1', regionId: 'source', position: [3] };
|
|
|
|
|
const parts: Record<string, Part> = { p1: part };
|
|
|
|
|
sourceRegion.childIds.push('p1');
|
|
|
|
|
sourceRegion.partMap['3'] = 'p1';
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
moveToRegion(part, sourceRegion, targetRegion);
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(part.position).toEqual([3]);
|
2026-04-02 21:58:11 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('moveToRegionAll', () => {
|
|
|
|
|
it('should move multiple parts to a target region', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const sourceRegion = createRegion('source', [{ name: 'x' }]);
|
|
|
|
|
const targetRegion = createRegion('target', [{ name: 'x' }]);
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const parts = {
|
|
|
|
|
p1: { id: 'p1', regionId: 'source', position: [0] } as Part,
|
|
|
|
|
p2: { id: 'p2', regionId: 'source', position: [1] } as Part,
|
|
|
|
|
p3: { id: 'p3', regionId: 'source', position: [2] } as Part,
|
|
|
|
|
};
|
|
|
|
|
sourceRegion.childIds.push('p1', 'p2', 'p3');
|
|
|
|
|
sourceRegion.partMap = { '0': 'p1', '1': 'p2', '2': 'p3' };
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
moveToRegionAll([parts.p1, parts.p2, parts.p3], sourceRegion, targetRegion, [[0], [1], [2]]);
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(sourceRegion.childIds).toHaveLength(0);
|
|
|
|
|
expect(targetRegion.childIds).toHaveLength(3);
|
|
|
|
|
expect(parts.p1.position).toEqual([0]);
|
|
|
|
|
expect(parts.p2.position).toEqual([1]);
|
|
|
|
|
expect(parts.p3.position).toEqual([2]);
|
2026-04-02 21:58:11 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should keep existing positions if no positions provided', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const sourceRegion = createRegion('source', [{ name: 'x' }]);
|
|
|
|
|
const targetRegion = createRegion('target', [{ name: 'x' }]);
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const parts = {
|
|
|
|
|
p1: { id: 'p1', regionId: 'source', position: [5] } as Part,
|
|
|
|
|
p2: { id: 'p2', regionId: 'source', position: [8] } as Part,
|
|
|
|
|
};
|
|
|
|
|
sourceRegion.childIds.push('p1', 'p2');
|
|
|
|
|
sourceRegion.partMap = { '5': 'p1', '8': 'p2' };
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
moveToRegionAll([parts.p1, parts.p2], sourceRegion, targetRegion);
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(parts.p1.position).toEqual([5]);
|
|
|
|
|
expect(parts.p2.position).toEqual([8]);
|
2026-04-02 21:58:11 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('removeFromRegion', () => {
|
|
|
|
|
it('should remove a part from its region', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const region = createRegion('region1', [{ name: 'x' }]);
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const part: Part = { id: 'p1', regionId: 'region1', position: [2] };
|
|
|
|
|
const parts: Record<string, Part> = { p1: part };
|
|
|
|
|
region.childIds.push('p1');
|
|
|
|
|
region.partMap['2'] = 'p1';
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(region.childIds).toHaveLength(1);
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
removeFromRegion(part, region);
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(region.childIds).toHaveLength(0);
|
2026-04-02 21:58:11 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should leave other parts unaffected', () => {
|
2026-04-03 12:46:02 +08:00
|
|
|
const region = createRegion('region1', [{ name: 'x' }]);
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
const p1 = { id: 'p1', regionId: 'region1', position: [0] } as Part;
|
|
|
|
|
const p2 = { id: 'p2', regionId: 'region1', position: [1] } as Part;
|
|
|
|
|
const p3 = { id: 'p3', regionId: 'region1', position: [2] } as Part;
|
|
|
|
|
region.childIds.push('p1', 'p2', 'p3');
|
|
|
|
|
region.partMap = { '0': 'p1', '1': 'p2', '2': 'p3' };
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
removeFromRegion(p2, region);
|
2026-04-02 21:58:11 +08:00
|
|
|
|
2026-04-03 12:46:02 +08:00
|
|
|
expect(region.childIds).toHaveLength(2);
|
|
|
|
|
expect(region.childIds).toEqual(['p1', 'p3']);
|
2026-04-02 21:58:11 +08:00
|
|
|
});
|
|
|
|
|
});
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|