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

535 lines
23 KiB
TypeScript
Raw Normal View History

2026-04-04 21:57:25 +08:00
import { describe, it, expect } from 'vitest';
import { registry, createInitialState, BoopState } from '@/samples/boop';
2026-04-05 10:42:38 +08:00
import { createGameContext } from '@/core/game';
2026-04-04 21:57:25 +08:00
import type { PromptEvent } from '@/utils/command';
2026-04-02 16:53:17 +08:00
2026-04-04 21:57:25 +08:00
function createTestContext() {
const ctx = createGameContext(registry, createInitialState());
return { registry, ctx };
}
function waitForPrompt(ctx: ReturnType<typeof createTestContext>['ctx']): Promise<PromptEvent> {
return new Promise(resolve => {
ctx._commands.on('prompt', resolve);
});
2026-04-02 16:53:17 +08:00
}
2026-04-04 21:53:37 +08:00
describe('Boop Game', () => {
describe('Setup', () => {
it('should create initial state correctly', () => {
2026-04-04 21:57:25 +08:00
const state = createInitialState();
2026-04-04 21:53:37 +08:00
expect(state.currentPlayer).toBe('white');
expect(state.winner).toBeNull();
expect(state.regions.board).toBeDefined();
expect(state.regions.white).toBeDefined();
expect(state.regions.black).toBeDefined();
2026-04-04 21:57:25 +08:00
2026-04-04 21:53:37 +08:00
// 8 kittens per player
const whiteKittens = Object.values(state.pieces).filter(p => p.player === 'white' && p.type === 'kitten');
const blackKittens = Object.values(state.pieces).filter(p => p.player === 'black' && p.type === 'kitten');
expect(whiteKittens.length).toBe(8);
expect(blackKittens.length).toBe(8);
2026-04-04 21:57:25 +08:00
2026-04-04 21:53:37 +08:00
// 8 cats per player (initially in box)
const whiteCats = Object.values(state.pieces).filter(p => p.player === 'white' && p.type === 'cat');
const blackCats = Object.values(state.pieces).filter(p => p.player === 'black' && p.type === 'cat');
expect(whiteCats.length).toBe(8);
expect(blackCats.length).toBe(8);
2026-04-04 21:57:25 +08:00
2026-04-04 21:53:37 +08:00
// All cats should be in box (regionId = '')
whiteCats.forEach(cat => expect(cat.regionId).toBe(''));
blackCats.forEach(cat => expect(cat.regionId).toBe(''));
2026-04-04 21:57:25 +08:00
2026-04-04 21:53:37 +08:00
// Kittens should be in player supplies
whiteKittens.forEach(k => expect(k.regionId).toBe('white'));
blackKittens.forEach(k => expect(k.regionId).toBe('black'));
});
});
describe('Place and Boop Commands', () => {
it('should place a kitten via play command', async () => {
2026-04-04 21:57:25 +08:00
const { ctx } = createTestContext();
// Use turn command instead of setup which runs indefinitely
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.run('turn white');
const promptEvent = await promptPromise;
expect(promptEvent).not.toBeNull();
expect(promptEvent.schema.name).toBe('play');
// Place a kitten at position 2,2
const error = promptEvent.tryCommit({ name: 'play', params: ['white', 2, 2, 'kitten'], options: {}, flags: {} });
expect(error).toBeNull();
const result = await runPromise;
expect(result.success).toBe(true);
2026-04-04 21:53:37 +08:00
2026-04-04 21:57:25 +08:00
const state = ctx.value;
2026-04-04 21:53:37 +08:00
// Should have placed a piece on the board
const boardPieces = Object.keys(state.regions.board.partMap);
expect(boardPieces.length).toBeGreaterThan(0);
2026-04-04 21:57:25 +08:00
2026-04-04 21:53:37 +08:00
// Should have one less kitten in supply
const whiteSupply = state.regions.white.childIds.filter(id => state.pieces[id].type === 'kitten');
expect(whiteSupply.length).toBe(7);
});
});
describe('Boop Mechanics', () => {
it('should boop adjacent pieces away from placement', async () => {
2026-04-04 21:57:25 +08:00
const { ctx } = createTestContext();
2026-04-04 21:53:37 +08:00
// White places at 2,2
2026-04-04 21:57:25 +08:00
let promptPromise = waitForPrompt(ctx);
let runPromise = ctx.run('turn white');
let promptEvent = await promptPromise;
let error = promptEvent.tryCommit({ name: 'play', params: ['white', 2, 2, 'kitten'], options: {}, flags: {} });
expect(error).toBeNull();
let result = await runPromise;
expect(result.success).toBe(true);
2026-04-04 21:53:37 +08:00
// Black places at 2,3, which will boop white's piece
2026-04-04 21:57:25 +08:00
promptPromise = waitForPrompt(ctx);
runPromise = ctx.run('turn black');
promptEvent = await promptPromise;
error = promptEvent.tryCommit({ name: 'play', params: ['black', 2, 3, 'kitten'], options: {}, flags: {} });
expect(error).toBeNull();
result = await runPromise;
expect(result.success).toBe(true);
const state = ctx.value;
2026-04-04 21:53:37 +08:00
// Check that pieces were placed
const boardPieceCount = Object.keys(state.regions.board.partMap).length;
expect(boardPieceCount).toBeGreaterThanOrEqual(1);
});
it('should handle pieces being booped off the board', async () => {
2026-04-04 21:57:25 +08:00
const { ctx } = createTestContext();
2026-04-04 21:53:37 +08:00
// White places at corner
2026-04-04 21:57:25 +08:00
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.run('turn white');
const promptEvent = await promptPromise;
const error = promptEvent.tryCommit({ name: 'play', params: ['white', 0, 0, 'kitten'], options: {}, flags: {} });
expect(error).toBeNull();
const result = await runPromise;
expect(result.success).toBe(true);
const state = ctx.value;
2026-04-04 21:53:37 +08:00
// Verify placement
expect(state.regions.board.partMap['0,0']).toBeDefined();
});
});
describe('Full Game Flow', () => {
it('should play a turn and switch players', async () => {
2026-04-04 21:57:25 +08:00
const { ctx } = createTestContext();
2026-04-04 21:53:37 +08:00
// White's turn - place at 2,2
2026-04-04 21:57:25 +08:00
let promptPromise = waitForPrompt(ctx);
let runPromise = ctx.run('turn white');
let prompt = await promptPromise;
const error1 = prompt.tryCommit({ name: 'play', params: ['white', 2, 2, 'kitten'], options: {}, flags: {} });
expect(error1).toBeNull();
let result = await runPromise;
expect(result.success).toBe(true);
const stateAfterWhite = ctx.value;
2026-04-04 21:53:37 +08:00
// Should have placed a piece
expect(stateAfterWhite.regions.board.partMap['2,2']).toBeDefined();
});
2026-04-02 16:53:17 +08:00
});
2026-04-04 22:23:15 +08:00
describe('Kitten vs Cat Hierarchy', () => {
it('should not boop cats when placing a kitten', async () => {
const { ctx } = createTestContext();
// White places a kitten at 2,2
let promptPromise = waitForPrompt(ctx);
let runPromise = ctx.run('turn white');
let prompt = await promptPromise;
let error = prompt.tryCommit({ name: 'play', params: ['white', 2, 2, 'kitten'], options: {}, flags: {} });
expect(error).toBeNull();
let result = await runPromise;
expect(result.success).toBe(true);
// Manually move white's kitten to box and replace with a cat (for testing)
ctx.produce(state => {
const whiteKitten = state.pieces['white-kitten-1'];
if (whiteKitten && whiteKitten.regionId === 'board') {
whiteKitten.type = 'cat';
}
});
// Black places a kitten at 2,3 (adjacent to the cat)
promptPromise = waitForPrompt(ctx);
runPromise = ctx.run('turn black');
prompt = await promptPromise;
error = prompt.tryCommit({ name: 'play', params: ['black', 2, 3, 'kitten'], options: {}, flags: {} });
expect(error).toBeNull();
result = await runPromise;
expect(result.success).toBe(true);
const state = ctx.value;
// White's cat should still be at 2,2 (not booped)
expect(state.regions.board.partMap['2,2']).toBe('white-kitten-1');
// Black's kitten should be at 2,3
expect(state.regions.board.partMap['2,3']).toBe('black-kitten-1');
});
it('should boop both kittens and cats when placing a cat', async () => {
const { ctx } = createTestContext();
// Manually set up: white cat at 2,3, black cat at 3,2
// First move cats to white and black supplies
ctx.produce(state => {
const whiteCat = state.pieces['white-cat-1'];
const blackCat = state.pieces['black-cat-1'];
if (whiteCat && whiteCat.regionId === '') {
whiteCat.regionId = 'white';
state.regions.white.childIds.push(whiteCat.id);
}
if (blackCat && blackCat.regionId === '') {
blackCat.regionId = 'black';
state.regions.black.childIds.push(blackCat.id);
}
});
// Now move them to the board
ctx.produce(state => {
const whiteCat = state.pieces['white-cat-1'];
const blackCat = state.pieces['black-cat-1'];
if (whiteCat && whiteCat.regionId === 'white') {
whiteCat.regionId = 'board';
whiteCat.position = [2, 3];
state.regions.board.partMap['2,3'] = whiteCat.id;
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== whiteCat.id);
}
if (blackCat && blackCat.regionId === 'black') {
blackCat.regionId = 'board';
blackCat.position = [3, 2];
state.regions.board.partMap['3,2'] = blackCat.id;
state.regions.black.childIds = state.regions.black.childIds.filter(id => id !== blackCat.id);
}
});
// Give white another cat for placement
ctx.produce(state => {
const whiteCat2 = state.pieces['white-cat-2'];
if (whiteCat2 && whiteCat2.regionId === '') {
whiteCat2.regionId = 'white';
state.regions.white.childIds.push(whiteCat2.id);
}
});
// White places a cat at 2,2 (should boop black's cat at 3,2 to 4,2)
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.run('turn white');
const prompt = await promptPromise;
const error = prompt.tryCommit({ name: 'play', params: ['white', 2, 2, 'cat'], options: {}, flags: {} });
expect(error).toBeNull();
const result = await runPromise;
expect(result.success).toBe(true);
const state = ctx.value;
// Black's cat should have been booped to 4,2
expect(state.regions.board.partMap['4,2']).toBeDefined();
const pieceAt42 = state.pieces[state.regions.board.partMap['4,2']];
expect(pieceAt42?.player).toBe('black');
expect(pieceAt42?.type).toBe('cat');
});
});
describe('Boop Obstructions', () => {
it('should boop pieces to empty positions', async () => {
const { ctx } = createTestContext();
// White places at 2,2
let promptPromise = waitForPrompt(ctx);
let runPromise = ctx.run('turn white');
let prompt = await promptPromise;
let error = prompt.tryCommit({ name: 'play', params: ['white', 2, 2, 'kitten'], options: {}, flags: {} });
expect(error).toBeNull();
let result = await runPromise;
expect(result.success).toBe(true);
// Check board has 1 piece after first placement
let state = ctx.value;
expect(Object.keys(state.regions.board.partMap).length).toBe(1);
// Black places at 3,3
promptPromise = waitForPrompt(ctx);
runPromise = ctx.run('turn black');
prompt = await promptPromise;
error = prompt.tryCommit({ name: 'play', params: ['black', 3, 3, 'kitten'], options: {}, flags: {} });
expect(error).toBeNull();
result = await runPromise;
expect(result.success).toBe(true);
state = ctx.value;
expect(Object.keys(state.regions.board.partMap).length).toBe(2);
// Verify the pieces are on the board (positions may vary due to boop)
const boardPieces = Object.entries(state.regions.board.partMap);
expect(boardPieces.length).toBe(2);
// Find black's piece
const blackPiece = boardPieces.find(([pos, id]) => state.pieces[id]?.player === 'black');
expect(blackPiece).toBeDefined();
});
it('should keep both pieces in place when boop is blocked', async () => {
const { ctx } = createTestContext();
// Setup: place white at 2,2 and 4,4, black at 3,3
await ctx._commands.run('place 2 2 white kitten');
await ctx._commands.run('place 3 3 black kitten');
await ctx._commands.run('place 4 4 white kitten');
const stateBefore = ctx.value;
// Verify setup - 3 pieces on board
const boardPiecesBefore = Object.keys(stateBefore.regions.board.partMap);
expect(boardPiecesBefore.length).toBe(3);
expect(stateBefore.regions.board.partMap['2,2']).toBeDefined();
expect(stateBefore.regions.board.partMap['3,3']).toBeDefined();
expect(stateBefore.regions.board.partMap['4,4']).toBeDefined();
// Black places at 2,3 - should try to boop piece at 3,3 to 4,4
// but 4,4 is occupied, so both should stay
await ctx._commands.run('place 2 3 black kitten');
const state = ctx.value;
// Should now have 4 pieces on board
const boardPiecesAfter = Object.keys(state.regions.board.partMap);
expect(boardPiecesAfter.length).toBe(4);
// 3,3 should still have the same piece (not booped)
expect(state.regions.board.partMap['3,3']).toBeDefined();
// 4,4 should still be occupied
expect(state.regions.board.partMap['4,4']).toBeDefined();
// 2,3 should have black's new piece
expect(state.regions.board.partMap['2,3']).toBeDefined();
});
});
describe('Graduation Mechanic', () => {
it('should graduate three kittens in a row to cats', async () => {
const { ctx } = createTestContext();
// Manually place three white kittens in a row
ctx.produce(state => {
const k1 = state.pieces['white-kitten-1'];
const k2 = state.pieces['white-kitten-2'];
const k3 = state.pieces['white-kitten-3'];
if (k1) {
k1.regionId = 'board';
k1.position = [0, 0];
state.regions.board.partMap['0,0'] = k1.id;
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== k1.id);
}
if (k2) {
k2.regionId = 'board';
k2.position = [0, 1];
state.regions.board.partMap['0,1'] = k2.id;
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== k2.id);
}
if (k3) {
k3.regionId = 'board';
k3.position = [0, 2];
state.regions.board.partMap['0,2'] = k3.id;
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== k3.id);
}
});
const stateBefore = ctx.value;
// Verify three kittens on board
expect(stateBefore.regions.board.partMap['0,0']).toBeDefined();
expect(stateBefore.regions.board.partMap['0,1']).toBeDefined();
expect(stateBefore.regions.board.partMap['0,2']).toBeDefined();
// Count cats in white supply before graduation
const catsInWhiteSupplyBefore = stateBefore.regions.white.childIds.filter(
id => stateBefore.pieces[id].type === 'cat'
);
expect(catsInWhiteSupplyBefore.length).toBe(0);
// Run check-graduates command
const result = await ctx._commands.run('check-graduates');
expect(result.success).toBe(true);
const state = ctx.value;
// The three positions on board should now be empty (kittens removed)
expect(state.regions.board.partMap['0,0']).toBeUndefined();
expect(state.regions.board.partMap['0,1']).toBeUndefined();
expect(state.regions.board.partMap['0,2']).toBeUndefined();
// White's supply should now have 3 cats (graduated)
const catsInWhiteSupply = state.regions.white.childIds.filter(
id => state.pieces[id].type === 'cat'
);
expect(catsInWhiteSupply.length).toBe(3);
// White's supply should have 5 kittens left (8 - 3 graduated)
const kittensInWhiteSupply = state.regions.white.childIds.filter(
id => state.pieces[id].type === 'kitten'
);
expect(kittensInWhiteSupply.length).toBe(5);
});
});
describe('Win Detection', () => {
it('should detect horizontal win with three cats', async () => {
const { ctx } = createTestContext();
// Manually set up a winning scenario for white
ctx.produce(state => {
const k1 = state.pieces['white-kitten-1'];
const k2 = state.pieces['white-kitten-2'];
const k3 = state.pieces['white-kitten-3'];
if (k1) {
k1.type = 'cat';
k1.regionId = 'board';
k1.position = [0, 0];
state.regions.board.partMap['0,0'] = k1.id;
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== k1.id);
}
if (k2) {
k2.type = 'cat';
k2.regionId = 'board';
k2.position = [0, 1];
state.regions.board.partMap['0,1'] = k2.id;
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== k2.id);
}
if (k3) {
k3.type = 'cat';
k3.regionId = 'board';
k3.position = [0, 2];
state.regions.board.partMap['0,2'] = k3.id;
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== k3.id);
}
});
// Run check-win command
const result = await ctx._commands.run('check-win');
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toBe('white');
}
});
it('should detect vertical win with three cats', async () => {
const { ctx } = createTestContext();
// Manually set up a vertical winning scenario for black
ctx.produce(state => {
const k1 = state.pieces['black-kitten-1'];
const k2 = state.pieces['black-kitten-2'];
const k3 = state.pieces['black-kitten-3'];
if (k1) {
k1.type = 'cat';
k1.regionId = 'board';
k1.position = [0, 0];
state.regions.board.partMap['0,0'] = k1.id;
state.regions.black.childIds = state.regions.black.childIds.filter(id => id !== k1.id);
}
if (k2) {
k2.type = 'cat';
k2.regionId = 'board';
k2.position = [1, 0];
state.regions.board.partMap['1,0'] = k2.id;
state.regions.black.childIds = state.regions.black.childIds.filter(id => id !== k2.id);
}
if (k3) {
k3.type = 'cat';
k3.regionId = 'board';
k3.position = [2, 0];
state.regions.board.partMap['2,0'] = k3.id;
state.regions.black.childIds = state.regions.black.childIds.filter(id => id !== k3.id);
}
});
// Run check-win command
const result = await ctx._commands.run('check-win');
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toBe('black');
}
});
it('should detect diagonal win with three cats', async () => {
const { ctx } = createTestContext();
// Manually set up a diagonal winning scenario for white
ctx.produce(state => {
const k1 = state.pieces['white-kitten-1'];
const k2 = state.pieces['white-kitten-2'];
const k3 = state.pieces['white-kitten-3'];
if (k1) {
k1.type = 'cat';
k1.regionId = 'board';
k1.position = [0, 0];
state.regions.board.partMap['0,0'] = k1.id;
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== k1.id);
}
if (k2) {
k2.type = 'cat';
k2.regionId = 'board';
k2.position = [1, 1];
state.regions.board.partMap['1,1'] = k2.id;
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== k2.id);
}
if (k3) {
k3.type = 'cat';
k3.regionId = 'board';
k3.position = [2, 2];
state.regions.board.partMap['2,2'] = k3.id;
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== k3.id);
}
});
// Run check-win command
const result = await ctx._commands.run('check-win');
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toBe('white');
}
});
});
describe('Placing Cats', () => {
it('should allow placing a cat from supply', async () => {
const { ctx } = createTestContext();
// Manually give a cat to white's supply
ctx.produce(state => {
const cat = state.pieces['white-cat-1'];
if (cat && cat.regionId === '') {
cat.regionId = 'white';
state.regions.white.childIds.push(cat.id);
}
});
// White places a cat at 2,2
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.run('turn white');
const prompt = await promptPromise;
const error = prompt.tryCommit({ name: 'play', params: ['white', 2, 2, 'cat'], options: {}, flags: {} });
expect(error).toBeNull();
const result = await runPromise;
expect(result.success).toBe(true);
const state = ctx.value;
// Cat should be on the board
expect(state.regions.board.partMap['2,2']).toBe('white-cat-1');
// Cat should no longer be in supply
const whiteCatsInSupply = state.regions.white.childIds.filter(id => state.pieces[id].type === 'cat');
expect(whiteCatsInSupply.length).toBe(0);
});
});
2026-04-02 16:53:17 +08:00
});