refactor: fix boop placement

This commit is contained in:
2026-04-02 17:36:42 +08:00
parent ecb09c01a1
commit 975d363769
2 changed files with 247 additions and 149 deletions
+181 -100
View File
@@ -4,7 +4,7 @@ import {
checkWinner,
isCellOccupied,
getPartAt,
placeKitten,
placePiece,
applyBoops,
checkGraduation,
processGraduation,
@@ -35,6 +35,10 @@ function waitForPrompt(ctx: ReturnType<typeof createTestContext>['ctx']): Promis
});
}
function getParts(state: Entity<BoopState>) {
return state.value.board.value.children;
}
describe('Boop - helper functions', () => {
describe('isCellOccupied', () => {
it('should return false for empty cell', () => {
@@ -47,7 +51,7 @@ describe('Boop - helper functions', () => {
it('should return true for occupied cell', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 3, 3, 'white');
placePiece(state, 3, 3, 'white', 'kitten');
expect(isCellOccupied(state, 3, 3)).toBe(true);
});
@@ -55,7 +59,7 @@ describe('Boop - helper functions', () => {
it('should return false for different cell', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
placePiece(state, 0, 0, 'white', 'kitten');
expect(isCellOccupied(state, 1, 1)).toBe(false);
});
@@ -72,7 +76,7 @@ describe('Boop - helper functions', () => {
it('should return the part at occupied cell', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 2, 2, 'black');
placePiece(state, 2, 2, 'black', 'kitten');
const part = getPartAt(state, 2, 2);
expect(part).not.toBeNull();
@@ -83,35 +87,72 @@ describe('Boop - helper functions', () => {
});
});
describe('placeKitten', () => {
describe('placePiece', () => {
it('should add a kitten to the board', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 2, 3, 'white');
placePiece(state, 2, 3, 'white', 'kitten');
expect(state.value.parts.length).toBe(1);
expect(state.value.parts[0].value.position).toEqual([2, 3]);
expect(state.value.parts[0].value.player).toBe('white');
expect(state.value.parts[0].value.pieceType).toBe('kitten');
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');
});
it('should name piece white-kitten-1', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placePiece(state, 0, 0, 'white', 'kitten');
expect(getParts(state)[0].id).toBe('white-kitten-1');
});
it('should name piece white-kitten-2 for second white kitten', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placePiece(state, 0, 0, 'white', 'kitten');
placePiece(state, 0, 1, 'white', 'kitten');
expect(getParts(state)[1].id).toBe('white-kitten-2');
});
it('should name piece white-cat-1', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placePiece(state, 0, 0, 'white', 'cat');
expect(getParts(state)[0].id).toBe('white-cat-1');
});
it('should decrement the correct player kitten supply', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
expect(state.value.whiteKittensInSupply).toBe(7);
expect(state.value.blackKittensInSupply).toBe(8);
placePiece(state, 0, 0, 'white', 'kitten');
expect(state.value.players.white.kitten.supply).toBe(7);
expect(state.value.players.black.kitten.supply).toBe(8);
placeKitten(state, 0, 1, 'black');
expect(state.value.whiteKittensInSupply).toBe(7);
expect(state.value.blackKittensInSupply).toBe(7);
placePiece(state, 0, 1, 'black', 'kitten');
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.cat.supply = 3;
});
placePiece(state, 0, 0, 'white', 'cat');
expect(state.value.players.white.cat.supply).toBe(2);
});
it('should add piece to board region children', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 1, 1, 'white');
placePiece(state, 1, 1, 'white', 'kitten');
const board = getBoardRegion(state);
expect(board.value.children.length).toBe(1);
@@ -120,10 +161,10 @@ describe('Boop - helper functions', () => {
it('should generate unique IDs for pieces', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
placeKitten(state, 0, 1, 'black');
placePiece(state, 0, 0, 'white', 'kitten');
placePiece(state, 0, 1, 'black', 'kitten');
const ids = state.value.parts.map(p => p.id);
const ids = getParts(state).map(p => p.id);
expect(new Set(ids).size).toBe(2);
});
});
@@ -133,22 +174,23 @@ describe('Boop - helper functions', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 3, 3, 'black');
placeKitten(state, 2, 2, 'white');
placePiece(state, 3, 3, 'black', 'kitten');
placePiece(state, 2, 2, 'white', 'kitten');
expect(state.value.parts[1].value.position).toEqual([2, 2]);
const whitePart = getParts(state)[1];
expect(whitePart.value.position).toEqual([2, 2]);
applyBoops(state, 3, 3, 'kitten');
expect(state.value.parts[1].value.position).toEqual([1, 1]);
expect(whitePart.value.position).toEqual([1, 1]);
});
it('should not boop a cat when a kitten is placed', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 3, 3, 'black');
const whitePart = state.value.parts[0];
placePiece(state, 3, 3, 'black', 'kitten');
const whitePart = getParts(state)[0];
whitePart.produce(p => {
p.pieceType = 'cat';
});
@@ -162,27 +204,27 @@ describe('Boop - helper functions', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
placeKitten(state, 1, 1, 'black');
placePiece(state, 0, 0, 'white', 'kitten');
placePiece(state, 1, 1, 'black', 'kitten');
applyBoops(state, 1, 1, 'kitten');
expect(state.value.parts.length).toBe(1);
expect(state.value.parts[0].value.player).toBe('black');
expect(state.value.whiteKittensInSupply).toBe(8);
expect(getParts(state).length).toBe(1);
expect(getParts(state)[0].value.player).toBe('black');
expect(state.value.players.white.kitten.supply).toBe(8);
});
it('should not boop piece if target cell is occupied', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 1, 1, 'white');
placeKitten(state, 2, 1, 'black');
placeKitten(state, 0, 1, 'black');
placePiece(state, 1, 1, 'white', 'kitten');
placePiece(state, 2, 1, 'black', 'kitten');
placePiece(state, 0, 1, 'black', 'kitten');
applyBoops(state, 0, 1, 'kitten');
const whitePart = state.value.parts.find(p => p.value.player === 'white');
const whitePart = getParts(state).find(p => p.value.player === 'white');
expect(whitePart).toBeDefined();
if (whitePart) {
expect(whitePart.value.position).toEqual([1, 1]);
@@ -193,38 +235,37 @@ describe('Boop - helper functions', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 3, 3, 'white');
placeKitten(state, 2, 2, 'black');
placeKitten(state, 2, 3, 'black');
placePiece(state, 3, 3, 'white', 'kitten');
placePiece(state, 2, 2, 'black', 'kitten');
placePiece(state, 2, 3, 'black', 'kitten');
applyBoops(state, 3, 3, 'kitten');
expect(state.value.parts[1].value.position).toEqual([1, 1]);
expect(state.value.parts[2].value.position).toEqual([1, 3]);
expect(getParts(state)[1].value.position).toEqual([1, 1]);
expect(getParts(state)[2].value.position).toEqual([1, 3]);
});
it('should not boop the placed piece itself', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 3, 3, 'white');
placePiece(state, 3, 3, 'white', 'kitten');
applyBoops(state, 3, 3, 'kitten');
expect(state.value.parts[0].value.position).toEqual([3, 3]);
expect(getParts(state)[0].value.position).toEqual([3, 3]);
});
});
describe('removePieceFromBoard', () => {
it('should remove piece from parts and board children', () => {
it('should remove piece from board children', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 2, 2, 'white');
const part = state.value.parts[0];
placePiece(state, 2, 2, 'white', 'kitten');
const part = getParts(state)[0];
removePieceFromBoard(state, part);
expect(state.value.parts.length).toBe(0);
const board = getBoardRegion(state);
expect(board.value.children.length).toBe(0);
});
@@ -235,8 +276,8 @@ describe('Boop - helper functions', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
placeKitten(state, 2, 2, 'white');
placePiece(state, 0, 0, 'white', 'kitten');
placePiece(state, 2, 2, 'white', 'kitten');
const lines = checkGraduation(state, 'white');
expect(lines.length).toBe(0);
@@ -246,9 +287,9 @@ describe('Boop - helper functions', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 1, 0, 'white');
placeKitten(state, 1, 1, 'white');
placeKitten(state, 1, 2, 'white');
placePiece(state, 1, 0, 'white', 'kitten');
placePiece(state, 1, 1, 'white', 'kitten');
placePiece(state, 1, 2, 'white', 'kitten');
const lines = checkGraduation(state, 'white');
expect(lines.length).toBe(1);
@@ -259,9 +300,9 @@ describe('Boop - helper functions', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 2, 'white');
placeKitten(state, 1, 2, 'white');
placeKitten(state, 2, 2, 'white');
placePiece(state, 0, 2, 'white', 'kitten');
placePiece(state, 1, 2, 'white', 'kitten');
placePiece(state, 2, 2, 'white', 'kitten');
const lines = checkGraduation(state, 'white');
expect(lines.length).toBe(1);
@@ -272,9 +313,9 @@ describe('Boop - helper functions', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
placeKitten(state, 1, 1, 'white');
placeKitten(state, 2, 2, 'white');
placePiece(state, 0, 0, 'white', 'kitten');
placePiece(state, 1, 1, 'white', 'kitten');
placePiece(state, 2, 2, 'white', 'kitten');
const lines = checkGraduation(state, 'white');
expect(lines.length).toBe(1);
@@ -285,9 +326,9 @@ describe('Boop - helper functions', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 2, 0, 'white');
placeKitten(state, 1, 1, 'white');
placeKitten(state, 0, 2, 'white');
placePiece(state, 2, 0, 'white', 'kitten');
placePiece(state, 1, 1, 'white', 'kitten');
placePiece(state, 0, 2, 'white', 'kitten');
const lines = checkGraduation(state, 'white');
expect(lines.length).toBe(1);
@@ -298,11 +339,11 @@ describe('Boop - helper functions', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
placeKitten(state, 0, 1, 'white');
placeKitten(state, 0, 2, 'white');
placePiece(state, 0, 0, 'white', 'kitten');
placePiece(state, 0, 1, 'white', 'kitten');
placePiece(state, 0, 2, 'white', 'kitten');
state.value.parts[1].produce(p => {
getParts(state)[1].produce(p => {
p.pieceType = 'cat';
});
@@ -316,34 +357,34 @@ describe('Boop - helper functions', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
placeKitten(state, 0, 1, 'white');
placeKitten(state, 0, 2, 'white');
placePiece(state, 0, 0, 'white', 'kitten');
placePiece(state, 0, 1, 'white', 'kitten');
placePiece(state, 0, 2, 'white', 'kitten');
const lines = checkGraduation(state, 'white');
expect(lines.length).toBe(1);
processGraduation(state, 'white', lines);
expect(state.value.parts.length).toBe(0);
expect(state.value.whiteCatsInSupply).toBe(3);
expect(getParts(state).length).toBe(0);
expect(state.value.players.white.cat.supply).toBe(3);
});
it('should only graduate pieces on the winning lines', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
placeKitten(state, 0, 1, 'white');
placeKitten(state, 0, 2, 'white');
placeKitten(state, 3, 3, 'white');
placePiece(state, 0, 0, 'white', 'kitten');
placePiece(state, 0, 1, 'white', 'kitten');
placePiece(state, 0, 2, 'white', 'kitten');
placePiece(state, 3, 3, 'white', 'kitten');
const lines = checkGraduation(state, 'white');
processGraduation(state, 'white', lines);
expect(state.value.parts.length).toBe(1);
expect(state.value.parts[0].value.position).toEqual([3, 3]);
expect(state.value.whiteCatsInSupply).toBe(3);
expect(getParts(state).length).toBe(1);
expect(getParts(state)[0].value.position).toEqual([3, 3]);
expect(state.value.players.white.cat.supply).toBe(3);
});
});
@@ -381,15 +422,9 @@ describe('Boop - helper functions', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
placeKitten(state, 0, 1, 'white');
placeKitten(state, 0, 2, 'white');
state.value.parts.forEach(p => {
p.produce(part => {
part.pieceType = 'cat';
});
});
placePiece(state, 0, 0, 'white', 'cat');
placePiece(state, 0, 1, 'white', 'cat');
placePiece(state, 0, 2, 'white', 'cat');
expect(checkWinner(state)).toBe('white');
});
@@ -399,10 +434,10 @@ describe('Boop - helper functions', () => {
const state = getState(ctx);
for (let i = 0; i < 8; i++) {
placeKitten(state, i % 6, Math.floor(i / 6) + (i % 2), 'white');
placePiece(state, i % 6, Math.floor(i / 6) + (i % 2), 'white', 'kitten');
}
for (let i = 0; i < 8; i++) {
placeKitten(state, i % 6, Math.floor(i / 6) + 3 + (i % 2), 'black');
placePiece(state, i % 6, Math.floor(i / 6) + 3 + (i % 2), 'black', 'kitten');
}
const result = checkWinner(state);
@@ -450,8 +485,9 @@ describe('Boop - game flow', () => {
const result = await runPromise;
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([2, 2]);
expect(getParts(ctx.state).length).toBe(1);
expect(getParts(ctx.state)[0].value.position).toEqual([2, 2]);
expect(getParts(ctx.state)[0].id).toBe('white-kitten-1');
});
it('should reject move for wrong player and re-prompt', async () => {
@@ -477,7 +513,7 @@ describe('Boop - game flow', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 2, 2, 'black');
placePiece(state, 2, 2, 'black', 'kitten');
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.commands.run<{winner: WinnerType}>('turn white');
@@ -500,7 +536,7 @@ describe('Boop - game flow', () => {
const state = getState(ctx);
state.produce(s => {
s.whiteKittensInSupply = 0;
s.players.white.kitten.supply = 0;
});
const promptPromise = waitForPrompt(ctx);
@@ -528,7 +564,7 @@ describe('Boop - game flow', () => {
prompt.resolve({ name: 'play', params: ['white', 3, 3], options: {}, flags: {} });
let result = await runPromise;
expect(result.success).toBe(true);
expect(state.value.parts.length).toBe(1);
expect(getParts(state).length).toBe(1);
promptPromise = waitForPrompt(ctx);
runPromise = ctx.commands.run<{winner: WinnerType}>('turn black');
@@ -536,29 +572,74 @@ describe('Boop - game flow', () => {
prompt.resolve({ name: 'play', params: ['black', 2, 2], options: {}, flags: {} });
result = await runPromise;
expect(result.success).toBe(true);
expect(state.value.parts.length).toBe(2);
expect(getParts(state).length).toBe(2);
const whitePart = state.value.parts.find(p => p.value.player === 'white');
const whitePart = getParts(state).find(p => p.value.player === 'white');
expect(whitePart).toBeDefined();
if (whitePart) {
expect(whitePart.value.position).not.toEqual([3, 3]);
}
});
it('should graduate kittens to cats and check for cat win', async () => {
it('should graduate kittens to cats and check for cat win', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 1, 0, 'white');
placeKitten(state, 1, 1, 'white');
placeKitten(state, 1, 2, 'white');
placePiece(state, 1, 0, 'white', 'kitten');
placePiece(state, 1, 1, 'white', 'kitten');
placePiece(state, 1, 2, 'white', 'kitten');
const lines = checkGraduation(state, 'white');
expect(lines.length).toBeGreaterThanOrEqual(1);
processGraduation(state, 'white', lines);
expect(state.value.parts.length).toBe(0);
expect(state.value.whiteCatsInSupply).toBe(3);
expect(getParts(state).length).toBe(0);
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.produce(s => {
s.players.white.cat.supply = 3;
});
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.commands.run<{winner: WinnerType}>('turn white');
const promptEvent = await promptPromise;
promptEvent.resolve({ name: 'play', params: ['white', 2, 2, 'cat'], options: {}, flags: {} });
const result = await runPromise;
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.cat.supply).toBe(2);
});
it('should reject placing a cat when supply is empty', async () => {
const { ctx } = createTestContext();
const state = getState(ctx);
state.produce(s => {
s.players.white.cat.supply = 0;
});
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.commands.run<{winner: WinnerType}>('turn white');
const promptEvent1 = await promptPromise;
promptEvent1.resolve({ name: 'play', params: ['white', 0, 0, 'cat'], options: {}, flags: {} });
const promptEvent2 = await waitForPrompt(ctx);
expect(promptEvent2).not.toBeNull();
promptEvent2.reject(new Error('test end'));
const result = await runPromise;
expect(result.success).toBe(false);
});
});