feat: add new functions

This commit is contained in:
hyper
2026-04-02 21:58:11 +08:00
parent 11d6cbd030
commit b71ba12454
4 changed files with 315 additions and 102 deletions
+106 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { applyAlign, shuffle, type Region, type RegionAxis } from '@/core/region';
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';
@@ -273,4 +273,109 @@ describe('Region', () => {
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']);
});
});
});