boardgame-core/tests/samples/boop.test.ts

565 lines
19 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
registry,
checkWinner,
isCellOccupied,
getPartAt,
placeKitten,
applyBoops,
checkGraduation,
processGraduation,
hasWinningLine,
removePieceFromBoard,
createInitialState,
BoopState,
WinnerType,
PlayerType,
getBoardRegion,
} from '@/samples/boop';
import {Entity} from "@/utils/entity";
import {createGameContext} from "@/";
import type { PromptEvent } from '@/utils/command';
function createTestContext() {
const ctx = createGameContext(registry, createInitialState);
return { registry, ctx };
}
function getState(ctx: ReturnType<typeof createTestContext>['ctx']): Entity<BoopState> {
return ctx.state;
}
function waitForPrompt(ctx: ReturnType<typeof createTestContext>['ctx']): Promise<PromptEvent> {
return new Promise(resolve => {
ctx.commands.on('prompt', resolve);
});
}
describe('Boop - helper functions', () => {
describe('isCellOccupied', () => {
it('should return false for empty cell', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
expect(isCellOccupied(state, 3, 3)).toBe(false);
});
it('should return true for occupied cell', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 3, 3, 'white');
expect(isCellOccupied(state, 3, 3)).toBe(true);
});
it('should return false for different cell', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
expect(isCellOccupied(state, 1, 1)).toBe(false);
});
});
describe('getPartAt', () => {
it('should return null for empty cell', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
expect(getPartAt(state, 2, 2)).toBeNull();
});
it('should return the part at occupied cell', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 2, 2, 'black');
const part = getPartAt(state, 2, 2);
expect(part).not.toBeNull();
if (part) {
expect(part.value.player).toBe('black');
expect(part.value.pieceType).toBe('kitten');
}
});
});
describe('placeKitten', () => {
it('should add a kitten to the board', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 2, 3, 'white');
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');
});
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);
placeKitten(state, 0, 1, 'black');
expect(state.value.whiteKittensInSupply).toBe(7);
expect(state.value.blackKittensInSupply).toBe(7);
});
it('should add piece to board region children', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 1, 1, 'white');
const board = getBoardRegion(state);
expect(board.value.children.length).toBe(1);
});
it('should generate unique IDs for pieces', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
placeKitten(state, 0, 1, 'black');
const ids = state.value.parts.map(p => p.id);
expect(new Set(ids).size).toBe(2);
});
});
describe('applyBoops', () => {
it('should boop adjacent kitten away from placed kitten', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 3, 3, 'black');
placeKitten(state, 2, 2, 'white');
expect(state.value.parts[1].value.position).toEqual([2, 2]);
applyBoops(state, 3, 3, 'kitten');
expect(state.value.parts[1].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];
whitePart.produce(p => {
p.pieceType = 'cat';
});
applyBoops(state, 3, 3, 'kitten');
expect(whitePart.value.position).toEqual([3, 3]);
});
it('should remove piece that is booped off the board', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
placeKitten(state, 1, 1, 'black');
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);
});
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');
applyBoops(state, 0, 1, 'kitten');
const whitePart = state.value.parts.find(p => p.value.player === 'white');
expect(whitePart).toBeDefined();
if (whitePart) {
expect(whitePart.value.position).toEqual([1, 1]);
}
});
it('should boop multiple adjacent pieces', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 3, 3, 'white');
placeKitten(state, 2, 2, 'black');
placeKitten(state, 2, 3, 'black');
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]);
});
it('should not boop the placed piece itself', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 3, 3, 'white');
applyBoops(state, 3, 3, 'kitten');
expect(state.value.parts[0].value.position).toEqual([3, 3]);
});
});
describe('removePieceFromBoard', () => {
it('should remove piece from parts and board children', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 2, 2, 'white');
const part = state.value.parts[0];
removePieceFromBoard(state, part);
expect(state.value.parts.length).toBe(0);
const board = getBoardRegion(state);
expect(board.value.children.length).toBe(0);
});
});
describe('checkGraduation', () => {
it('should return empty array when no kittens in a row', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
placeKitten(state, 2, 2, 'white');
const lines = checkGraduation(state, 'white');
expect(lines.length).toBe(0);
});
it('should detect horizontal line of 3 kittens', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 1, 0, 'white');
placeKitten(state, 1, 1, 'white');
placeKitten(state, 1, 2, 'white');
const lines = checkGraduation(state, 'white');
expect(lines.length).toBe(1);
expect(lines[0]).toEqual([[1, 0], [1, 1], [1, 2]]);
});
it('should detect vertical line of 3 kittens', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 2, 'white');
placeKitten(state, 1, 2, 'white');
placeKitten(state, 2, 2, 'white');
const lines = checkGraduation(state, 'white');
expect(lines.length).toBe(1);
expect(lines[0]).toEqual([[0, 2], [1, 2], [2, 2]]);
});
it('should detect diagonal line of 3 kittens', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
placeKitten(state, 1, 1, 'white');
placeKitten(state, 2, 2, 'white');
const lines = checkGraduation(state, 'white');
expect(lines.length).toBe(1);
expect(lines[0]).toEqual([[0, 0], [1, 1], [2, 2]]);
});
it('should detect anti-diagonal line of 3 kittens', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 2, 0, 'white');
placeKitten(state, 1, 1, 'white');
placeKitten(state, 0, 2, 'white');
const lines = checkGraduation(state, 'white');
expect(lines.length).toBe(1);
expect(lines[0]).toEqual([[2, 0], [1, 1], [0, 2]]);
});
it('should not detect line with mixed piece types', () => {
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[1].produce(p => {
p.pieceType = 'cat';
});
const lines = checkGraduation(state, 'white');
expect(lines.length).toBe(0);
});
});
describe('processGraduation', () => {
it('should convert kittens to cats and update supply', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 0, 0, 'white');
placeKitten(state, 0, 1, 'white');
placeKitten(state, 0, 2, 'white');
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);
});
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');
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);
});
});
describe('hasWinningLine', () => {
it('should return false for no line', () => {
expect(hasWinningLine([[0, 0], [1, 1], [3, 3]])).toBe(false);
});
it('should return true for horizontal line', () => {
expect(hasWinningLine([[0, 0], [0, 1], [0, 2]])).toBe(true);
});
it('should return true for vertical line', () => {
expect(hasWinningLine([[0, 0], [1, 0], [2, 0]])).toBe(true);
});
it('should return true for diagonal line', () => {
expect(hasWinningLine([[0, 0], [1, 1], [2, 2]])).toBe(true);
});
it('should return true for anti-diagonal line', () => {
expect(hasWinningLine([[2, 0], [1, 1], [0, 2]])).toBe(true);
});
});
describe('checkWinner', () => {
it('should return null for empty board', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
expect(checkWinner(state)).toBeNull();
});
it('should return winner when player has 3 cats in a row', () => {
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';
});
});
expect(checkWinner(state)).toBe('white');
});
it('should return draw when both players use all pieces', () => {
const { ctx } = createTestContext();
const state = getState(ctx);
for (let i = 0; i < 8; i++) {
placeKitten(state, i % 6, Math.floor(i / 6) + (i % 2), 'white');
}
for (let i = 0; i < 8; i++) {
placeKitten(state, i % 6, Math.floor(i / 6) + 3 + (i % 2), 'black');
}
const result = checkWinner(state);
expect(result === 'draw' || result === null).toBe(true);
});
});
});
describe('Boop - game flow', () => {
it('should have setup and turn commands registered', () => {
const { registry: reg } = createTestContext();
expect(reg.has('setup')).toBe(true);
expect(reg.has('turn')).toBe(true);
});
it('should setup board when setup command runs', async () => {
const { ctx } = createTestContext();
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.commands.run('setup');
const promptEvent = await promptPromise;
expect(promptEvent).not.toBeNull();
expect(promptEvent.schema.name).toBe('play');
promptEvent.reject(new Error('test end'));
const result = await runPromise;
expect(result.success).toBe(false);
});
it('should accept valid move via turn command', async () => {
const { ctx } = createTestContext();
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.commands.run<{winner: WinnerType}>('turn white');
const promptEvent = await promptPromise;
expect(promptEvent).not.toBeNull();
expect(promptEvent.schema.name).toBe('play');
promptEvent.resolve({ name: 'play', params: ['white', 2, 2], options: {}, flags: {} });
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]);
});
it('should reject move for wrong player and re-prompt', async () => {
const { ctx } = createTestContext();
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.commands.run<{winner: WinnerType}>('turn white');
const promptEvent1 = await promptPromise;
promptEvent1.resolve({ name: 'play', params: ['black', 2, 2], options: {}, flags: {} });
const promptEvent2 = await waitForPrompt(ctx);
expect(promptEvent2).not.toBeNull();
promptEvent2.resolve({ name: 'play', params: ['white', 2, 2], options: {}, flags: {} });
const result = await runPromise;
expect(result.success).toBe(true);
if (result.success) expect(result.result.winner).toBeNull();
});
it('should reject move to occupied cell and re-prompt', async () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 2, 2, 'black');
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.commands.run<{winner: WinnerType}>('turn white');
const promptEvent1 = await promptPromise;
promptEvent1.resolve({ name: 'play', params: ['white', 2, 2], options: {}, flags: {} });
const promptEvent2 = await waitForPrompt(ctx);
expect(promptEvent2).not.toBeNull();
promptEvent2.resolve({ name: 'play', params: ['white', 0, 0], options: {}, flags: {} });
const result = await runPromise;
expect(result.success).toBe(true);
if (result.success) expect(result.result.winner).toBeNull();
});
it('should reject move when kitten supply is empty', async () => {
const { ctx } = createTestContext();
const state = getState(ctx);
state.produce(s => {
s.whiteKittensInSupply = 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], 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);
});
it('should boop adjacent pieces after placement', async () => {
const { ctx } = createTestContext();
const state = getState(ctx);
let promptPromise = waitForPrompt(ctx);
let runPromise = ctx.commands.run<{winner: WinnerType}>('turn white');
let prompt = await promptPromise;
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);
promptPromise = waitForPrompt(ctx);
runPromise = ctx.commands.run<{winner: WinnerType}>('turn black');
prompt = await promptPromise;
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);
const whitePart = state.value.parts.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 () => {
const { ctx } = createTestContext();
const state = getState(ctx);
placeKitten(state, 1, 0, 'white');
placeKitten(state, 1, 1, 'white');
placeKitten(state, 1, 2, 'white');
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);
});
});