refactor: transition to non child entity format

This commit is contained in:
2026-04-03 12:46:50 +08:00
parent cf7cfcd86b
commit eb0ebf5411
8 changed files with 425 additions and 478 deletions
+189 -192
View File
@@ -1,135 +1,138 @@
import { describe, it, expect } from 'vitest';
import { applyAlign, shuffle, moveToRegion, moveToRegionAll, removeFromRegion, type Region, type RegionAxis } from '@/core/region';
import { createRegion, 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<Region> {
const partEntities = parts.map(p => entity(p.id, p));
return entity('region1', {
id: 'region1',
axes: [...axes],
children: partEntities,
});
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 };
}
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);
const { region } = createTestRegion([{ name: 'x', min: 0, align: 'start' }], []);
applyAlign(region, {});
expect(region.childIds).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 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] };
const region = createTestRegion(
const { region, parts } = createTestRegion(
[{ name: 'x', min: 0, align: 'start' }, { name: 'y' }],
[part1, part2, part3]
);
applyAlign(region);
applyAlign(region, parts);
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);
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);
});
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 part1: Part = { id: 'p1', regionId: 'region1', position: [5, 100] };
const part2: Part = { id: 'p2', regionId: 'region1', position: [7, 200] };
const region = createTestRegion(
const { region, parts } = createTestRegion(
[{ name: 'x', min: 10, align: 'start' }, { name: 'y' }],
[part1, part2]
);
applyAlign(region);
applyAlign(region, parts);
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);
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);
});
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 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] };
const region = createTestRegion(
const { region, parts } = createTestRegion(
[{ name: 'x', max: 10, align: 'end' }, { name: 'y' }],
[part1, part2, part3]
);
applyAlign(region);
applyAlign(region, parts);
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);
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);
});
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 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] };
const region = createTestRegion(
const { region, parts } = createTestRegion(
[{ name: 'x', min: 0, max: 10, align: 'center' }, { name: 'y' }],
[part1, part2, part3]
);
applyAlign(region);
applyAlign(region, parts);
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);
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);
});
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 part1: Part = { id: 'p1', regionId: 'region1', position: [0, 10] };
const part2: Part = { id: 'p2', regionId: 'region1', position: [1, 20] };
const region = createTestRegion(
const { region, parts } = createTestRegion(
[{ name: 'x', min: 0, max: 10, align: 'center' }, { name: 'y' }],
[part1, part2]
);
applyAlign(region);
applyAlign(region, parts);
expect(region.value.children[0].value.position[0]).toBe(4.5);
expect(region.value.children[1].value.position[0]).toBe(5.5);
expect(parts[region.childIds[0]].position[0]).toBe(4.5);
expect(parts[region.childIds[1]].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 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] };
const region = createTestRegion(
const { region, parts } = createTestRegion(
[{ name: 'x', min: 0, align: 'start' }, { name: 'y' }],
[part1, part2, part3]
);
applyAlign(region);
applyAlign(region, parts);
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');
expect(region.childIds[0]).toBe('p2');
expect(region.childIds[1]).toBe('p3');
expect(region.childIds[2]).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 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] };
const region = createTestRegion(
const { region, parts } = createTestRegion(
[
{ name: 'x', min: 0, align: 'start' },
{ name: 'y', min: 0, align: 'start' }
@@ -137,30 +140,25 @@ describe('Region', () => {
[part1, part2, part3]
);
applyAlign(region);
applyAlign(region, parts);
const positions = region.value.children.map(c => ({
id: c.value.id,
position: c.value.position
}));
expect(region.childIds[0]).toBe('p3');
expect(parts[region.childIds[0]].position).toEqual([0, 2]);
expect(positions[0].id).toBe('p3');
expect(positions[0].position).toEqual([0, 2]);
expect(region.childIds[1]).toBe('p1');
expect(parts[region.childIds[1]].position).toEqual([1, 0]);
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]);
expect(region.childIds[2]).toBe('p2');
expect(parts[region.childIds[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 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] };
const region = createTestRegion(
const { region, parts } = createTestRegion(
[
{ name: 'x', min: 0, max: 10, align: 'start' },
{ name: 'y', min: 0, max: 10, align: 'start' }
@@ -168,55 +166,50 @@ describe('Region', () => {
[part1, part2, part3, part4]
);
applyAlign(region);
applyAlign(region, parts);
const positions = region.value.children.map(c => ({
id: c.value.id,
position: c.value.position
}));
expect(region.childIds[0]).toBe('p1');
expect(parts[region.childIds[0]].position).toEqual([0, 0]);
expect(positions[0].id).toBe('p1');
expect(positions[0].position).toEqual([0, 0]);
expect(region.childIds[1]).toBe('p4');
expect(parts[region.childIds[1]].position).toEqual([0, 1]);
expect(positions[1].id).toBe('p4');
expect(positions[1].position).toEqual([0, 1]);
expect(region.childIds[2]).toBe('p2');
expect(parts[region.childIds[2]].position).toEqual([1, 0]);
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]);
expect(region.childIds[3]).toBe('p3');
expect(parts[region.childIds[3]].position).toEqual([1, 1]);
});
});
describe('shuffle', () => {
it('should do nothing with empty region', () => {
const region = createTestRegion([], []);
const { region, parts } = createTestRegion([], []);
const rng = createRNG(42);
shuffle(region, rng);
expect(region.value.children).toHaveLength(0);
shuffle(region, parts, rng);
expect(region.childIds).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 part: Part = { id: 'p1', regionId: 'region1', position: [0, 0, 0] };
const { region, parts } = createTestRegion([], [part]);
const rng = createRNG(42);
shuffle(region, rng);
expect(region.value.children[0].value.position).toEqual([0, 0, 0]);
shuffle(region, parts, rng);
expect(parts['p1'].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 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] };
const region = createTestRegion([], [part1, part2, part3]);
const { region, parts } = createTestRegion([], [part1, part2, part3]);
const rng = createRNG(42);
const originalPositions = region.value.children.map(c => [...c.value.position]);
shuffle(region, rng);
const originalPositions = region.childIds.map(id => [...parts[id].position]);
shuffle(region, parts, rng);
const newPositions = region.value.children.map(c => c.value.position);
const newPositions = region.childIds.map(id => parts[id].position);
originalPositions.forEach(origPos => {
const found = newPositions.some(newPos =>
@@ -227,46 +220,44 @@ describe('Region', () => {
});
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 createPartsForTest = (): Part[] => [
{ id: 'p1', regionId: 'region1', position: [0, 10] },
{ id: 'p2', regionId: 'region1', position: [1, 20] },
{ id: 'p3', regionId: 'region1', position: [2, 30] },
];
const setup1 = createRegionForTest();
const setup2 = createRegionForTest();
const setup1 = createTestRegion([], createPartsForTest());
const setup2 = createTestRegion([], createPartsForTest());
const rng1 = createRNG(42);
const rng2 = createRNG(42);
shuffle(setup1, rng1);
shuffle(setup2, rng2);
shuffle(setup1.region, setup1.parts, rng1);
shuffle(setup2.region, setup2.parts, rng2);
const positions1 = setup1.value.children.map(c => c.value.position);
const positions2 = setup2.value.children.map(c => c.value.position);
const positions1 = setup1.region.childIds.map(id => setup1.parts[id].position);
const positions2 = setup2.region.childIds.map(id => setup2.parts[id].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 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] },
];
const results = new Set<string>();
for (let seed = 1; seed <= 10; seed++) {
const setup = createRegionForTest();
const setup = createTestRegion([], createPartsForTest());
const rng = createRNG(seed);
shuffle(setup, rng);
shuffle(setup.region, setup.parts, rng);
const positions = JSON.stringify(setup.value.children.map(c => c.value.position));
const positions = JSON.stringify(setup.region.childIds.map(id => setup.parts[id].position));
results.add(positions);
}
@@ -278,104 +269,110 @@ describe('Region', () => {
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 sourceRegion = createRegion('source', sourceAxes);
const targetRegion = createRegion('target', targetAxes);
const part: Part = { id: 'p1', region: sourceRegion, position: [2] };
const partEntity = entity(part.id, part);
sourceRegion.value.children.push(partEntity);
const part: Part = { id: 'p1', regionId: 'source', position: [2] };
const parts: Record<string, Part> = { p1: part };
sourceRegion.childIds.push('p1');
sourceRegion.partMap['2'] = 'p1';
expect(sourceRegion.value.children).toHaveLength(1);
expect(targetRegion.value.children).toHaveLength(0);
expect(partEntity.value.region.value.id).toBe('region1');
expect(sourceRegion.childIds).toHaveLength(1);
expect(targetRegion.childIds).toHaveLength(0);
expect(part.regionId).toBe('source');
moveToRegion(partEntity, targetRegion, [0]);
moveToRegion(part, sourceRegion, 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]);
expect(sourceRegion.childIds).toHaveLength(0);
expect(targetRegion.childIds).toHaveLength(1);
expect(part.regionId).toBe('target');
expect(part.position).toEqual([0]);
});
it('should keep existing position if no position provided', () => {
const sourceRegion = createTestRegion([{ name: 'x' }], []);
const targetRegion = createTestRegion([{ name: 'x' }], []);
const sourceRegion = createRegion('source', [{ name: 'x' }]);
const targetRegion = createRegion('target', [{ name: 'x' }]);
const part: Part = { id: 'p1', region: sourceRegion, position: [3] };
const partEntity = entity(part.id, part);
sourceRegion.value.children.push(partEntity);
const part: Part = { id: 'p1', regionId: 'source', position: [3] };
const parts: Record<string, Part> = { p1: part };
sourceRegion.childIds.push('p1');
sourceRegion.partMap['3'] = 'p1';
moveToRegion(partEntity, targetRegion);
moveToRegion(part, sourceRegion, targetRegion);
expect(partEntity.value.position).toEqual([3]);
expect(part.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 sourceRegion = createRegion('source', [{ name: 'x' }]);
const targetRegion = createRegion('target', [{ 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);
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' };
moveToRegionAll(parts, targetRegion, [[0], [1], [2]]);
moveToRegionAll([parts.p1, parts.p2, parts.p3], sourceRegion, 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]);
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]);
});
it('should keep existing positions if no positions provided', () => {
const sourceRegion = createTestRegion([{ name: 'x' }], []);
const targetRegion = createTestRegion([{ name: 'x' }], []);
const sourceRegion = createRegion('source', [{ name: 'x' }]);
const targetRegion = createRegion('target', [{ 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);
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' };
moveToRegionAll(parts, targetRegion);
moveToRegionAll([parts.p1, parts.p2], sourceRegion, targetRegion);
expect(parts[0].value.position).toEqual([5]);
expect(parts[1].value.position).toEqual([8]);
expect(parts.p1.position).toEqual([5]);
expect(parts.p2.position).toEqual([8]);
});
});
describe('removeFromRegion', () => {
it('should remove a part from its region', () => {
const region = createTestRegion([{ name: 'x' }], []);
const region = createRegion('region1', [{ name: 'x' }]);
const part: Part = { id: 'p1', region: region, position: [2] };
const partEntity = entity(part.id, part);
region.value.children.push(partEntity);
const part: Part = { id: 'p1', regionId: 'region1', position: [2] };
const parts: Record<string, Part> = { p1: part };
region.childIds.push('p1');
region.partMap['2'] = 'p1';
expect(region.value.children).toHaveLength(1);
expect(region.childIds).toHaveLength(1);
removeFromRegion(partEntity);
removeFromRegion(part, region);
expect(region.value.children).toHaveLength(0);
expect(region.childIds).toHaveLength(0);
});
it('should leave other parts unaffected', () => {
const region = createTestRegion([{ name: 'x' }], []);
const region = createRegion('region1', [{ 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);
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' };
removeFromRegion(p2);
removeFromRegion(p2, region);
expect(region.value.children).toHaveLength(2);
expect(region.value.children.map(c => c.value.id)).toEqual(['p1', 'p3']);
expect(region.childIds).toHaveLength(2);
expect(region.childIds).toEqual(['p1', 'p3']);
});
});
});
+44 -42
View File
@@ -36,7 +36,7 @@ function waitForPrompt(ctx: ReturnType<typeof createTestContext>['ctx']): Promis
}
function getParts(state: Entity<BoopState>) {
return state.value.board.value.children;
return state.value.pieces;
}
describe('Boop - helper functions', () => {
@@ -81,8 +81,8 @@ describe('Boop - helper functions', () => {
const part = getPartAt(state, 2, 2);
expect(part).not.toBeNull();
if (part) {
expect(part.value.player).toBe('black');
expect(part.value.pieceType).toBe('kitten');
expect(part.player).toBe('black');
expect(part.pieceType).toBe('kitten');
}
});
});
@@ -95,9 +95,9 @@ describe('Boop - helper functions', () => {
const parts = getParts(state);
expect(parts.length).toBe(1);
expect(parts[0].value.position).toEqual([2, 3]);
expect(parts[0].value.player).toBe('white');
expect(parts[0].value.pieceType).toBe('kitten');
expect(parts[0].position).toEqual([2, 3]);
expect(parts[0].player).toBe('white');
expect(parts[0].pieceType).toBe('kitten');
});
it('should name piece white-kitten-1', () => {
@@ -130,23 +130,23 @@ describe('Boop - helper functions', () => {
const state = getState(ctx);
placePiece(state, 0, 0, 'white', 'kitten');
expect(state.value.players.white.value.kitten.supply).toBe(7);
expect(state.value.players.black.value.kitten.supply).toBe(8);
expect(state.value.players.white.kitten.supply).toBe(7);
expect(state.value.players.black.kitten.supply).toBe(8);
placePiece(state, 0, 1, 'black', 'kitten');
expect(state.value.players.white.value.kitten.supply).toBe(7);
expect(state.value.players.black.value.kitten.supply).toBe(7);
expect(state.value.players.white.kitten.supply).toBe(7);
expect(state.value.players.black.kitten.supply).toBe(7);
});
it('should decrement the correct player cat supply', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
state.produce(s => {
s.players.white.value.cat.supply = 3;
s.players.white.cat.supply = 3;
});
placePiece(state, 0, 0, 'white', 'cat');
expect(state.value.players.white.value.cat.supply).toBe(2);
expect(state.value.players.white.cat.supply).toBe(2);
});
it('should add piece to board region children', () => {
@@ -155,7 +155,7 @@ describe('Boop - helper functions', () => {
placePiece(state, 1, 1, 'white', 'kitten');
const board = getBoardRegion(state);
expect(board.value.children.length).toBe(1);
expect(board.childIds.length).toBe(1);
});
it('should generate unique IDs for pieces', () => {
@@ -178,11 +178,11 @@ describe('Boop - helper functions', () => {
placePiece(state, 2, 2, 'white', 'kitten');
const whitePart = getParts(state)[1];
expect(whitePart.value.position).toEqual([2, 2]);
expect(whitePart.position).toEqual([2, 2]);
applyBoops(state, 3, 3, 'kitten');
expect(whitePart.value.position).toEqual([1, 1]);
expect(whitePart.position).toEqual([1, 1]);
});
it('should not boop a cat when a kitten is placed', () => {
@@ -191,13 +191,11 @@ describe('Boop - helper functions', () => {
placePiece(state, 3, 3, 'black', 'kitten');
const whitePart = getParts(state)[0];
whitePart.produce(p => {
p.pieceType = 'cat';
});
whitePart.pieceType = 'cat';
applyBoops(state, 3, 3, 'kitten');
expect(whitePart.value.position).toEqual([3, 3]);
expect(whitePart.position).toEqual([3, 3]);
});
it('should remove piece that is booped off the board', () => {
@@ -210,8 +208,8 @@ describe('Boop - helper functions', () => {
applyBoops(state, 1, 1, 'kitten');
expect(getParts(state).length).toBe(1);
expect(getParts(state)[0].value.player).toBe('black');
expect(state.value.players.white.value.kitten.supply).toBe(8);
expect(getParts(state)[0].player).toBe('black');
expect(state.value.players.white.kitten.supply).toBe(8);
});
it('should not boop piece if target cell is occupied', () => {
@@ -224,10 +222,10 @@ describe('Boop - helper functions', () => {
applyBoops(state, 0, 1, 'kitten');
const whitePart = getParts(state).find(p => p.value.player === 'white');
const whitePart = getParts(state).find(p => p.player === 'white');
expect(whitePart).toBeDefined();
if (whitePart) {
expect(whitePart.value.position).toEqual([1, 1]);
expect(whitePart.position).toEqual([1, 1]);
}
});
@@ -241,8 +239,8 @@ describe('Boop - helper functions', () => {
applyBoops(state, 3, 3, 'kitten');
expect(getParts(state)[1].value.position).toEqual([1, 1]);
expect(getParts(state)[2].value.position).toEqual([1, 3]);
expect(getParts(state)[1].position).toEqual([1, 1]);
expect(getParts(state)[2].position).toEqual([1, 3]);
});
it('should not boop the placed piece itself', () => {
@@ -253,7 +251,7 @@ describe('Boop - helper functions', () => {
applyBoops(state, 3, 3, 'kitten');
expect(getParts(state)[0].value.position).toEqual([3, 3]);
expect(getParts(state)[0].position).toEqual([3, 3]);
});
});
@@ -267,7 +265,7 @@ describe('Boop - helper functions', () => {
removePieceFromBoard(state, part);
const board = getBoardRegion(state);
expect(board.value.children.length).toBe(0);
expect(board.childIds.length).toBe(0);
});
});
@@ -343,9 +341,7 @@ describe('Boop - helper functions', () => {
placePiece(state, 0, 1, 'white', 'kitten');
placePiece(state, 0, 2, 'white', 'kitten');
getParts(state)[1].produce(p => {
p.pieceType = 'cat';
});
getParts(state)[1].pieceType = 'cat';
const lines = checkGraduation(state, 'white');
expect(lines.length).toBe(0);
@@ -367,7 +363,7 @@ describe('Boop - helper functions', () => {
processGraduation(state, 'white', lines);
expect(getParts(state).length).toBe(0);
expect(state.value.players.white.value.cat.supply).toBe(3);
expect(state.value.players.white.cat.supply).toBe(3);
});
it('should only graduate pieces on the winning lines', () => {
@@ -383,8 +379,8 @@ describe('Boop - helper functions', () => {
processGraduation(state, 'white', lines);
expect(getParts(state).length).toBe(1);
expect(getParts(state)[0].value.position).toEqual([3, 3]);
expect(state.value.players.white.value.cat.supply).toBe(3);
expect(getParts(state)[0].position).toEqual([3, 3]);
expect(state.value.players.white.cat.supply).toBe(3);
});
});
@@ -487,7 +483,7 @@ describe('Boop - game flow', () => {
expect(result.success).toBe(true);
if (result.success) expect(result.result.winner).toBeNull();
expect(getParts(ctx.state).length).toBe(1);
expect(getParts(ctx.state)[0].value.position).toEqual([2, 2]);
expect(getParts(ctx.state)[0].position).toEqual([2, 2]);
expect(getParts(ctx.state)[0].id).toBe('white-kitten-1');
});
@@ -537,7 +533,9 @@ describe('Boop - game flow', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
state.value.players.white.value.kitten.supply = 0;
state.produce(s => {
s.players.white.kitten.supply = 0;
});
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.commands.run<{winner: WinnerType}>('turn white');
@@ -575,10 +573,10 @@ describe('Boop - game flow', () => {
expect(result.success).toBe(true);
expect(getParts(state).length).toBe(2);
const whitePart = getParts(state).find(p => p.value.player === 'white');
const whitePart = getParts(state).find(p => p.player === 'white');
expect(whitePart).toBeDefined();
if (whitePart) {
expect(whitePart.value.position).not.toEqual([3, 3]);
expect(whitePart.position).not.toEqual([3, 3]);
}
});
@@ -596,14 +594,16 @@ describe('Boop - game flow', () => {
processGraduation(state, 'white', lines);
expect(getParts(state).length).toBe(0);
expect(state.value.players.white.value.cat.supply).toBe(3);
expect(state.value.players.white.cat.supply).toBe(3);
});
it('should accept placing a cat via play command', async () => {
const { ctx } = createTestContext();
const state = getState(ctx);
state.value.players.white.value.cat.supply = 3;
state.produce(s => {
s.players.white.cat.supply = 3;
});
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.commands.run<{winner: WinnerType}>('turn white');
@@ -616,15 +616,17 @@ describe('Boop - game flow', () => {
expect(result.success).toBe(true);
expect(getParts(state).length).toBe(1);
expect(getParts(state)[0].id).toBe('white-cat-1');
expect(getParts(state)[0].value.pieceType).toBe('cat');
expect(state.value.players.white.value.cat.supply).toBe(2);
expect(getParts(state)[0].pieceType).toBe('cat');
expect(state.value.players.white.cat.supply).toBe(2);
});
it('should reject placing a cat when supply is empty', async () => {
const { ctx } = createTestContext();
const state = getState(ctx);
state.value.players.white.value.cat.supply = 0;
state.produce(s => {
s.players.white.cat.supply = 0;
});
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.commands.run<{winner: WinnerType}>('turn white');
+4 -4
View File
@@ -164,8 +164,8 @@ describe('TicTacToe - helper functions', () => {
placePiece(state, 1, 1, 'X');
expect(state.value.parts.length).toBe(1);
expect(state.value.parts[0].value.position).toEqual([1, 1]);
expect(state.value.parts[0].value.player).toBe('X');
expect(state.value.parts[0].position).toEqual([1, 1]);
expect(state.value.parts[0].player).toBe('X');
});
it('should add piece to board region children', () => {
@@ -174,7 +174,7 @@ describe('TicTacToe - helper functions', () => {
placePiece(state, 0, 0, 'O');
const board = state.value.board;
expect(board.value.children.length).toBe(1);
expect(board.childIds.length).toBe(1);
});
it('should generate unique IDs for pieces', () => {
@@ -230,7 +230,7 @@ describe('TicTacToe - game flow', () => {
expect(result.success).toBe(true);
if (result.success) expect(result.result.winner).toBeNull();
expect(ctx.state.value.parts.length).toBe(1);
expect(ctx.state.value.parts[0].value.position).toEqual([1, 1]);
expect(ctx.state.value.parts[0].position).toEqual([1, 1]);
});
it('should reject move for wrong player and re-prompt', async () => {