style: format tic-tac-toe sample and boop tests
This commit is contained in:
parent
0c94e6923a
commit
b83ff28f60
|
|
@ -1,32 +1,68 @@
|
|||
import {Part} from "boardgame-core";
|
||||
import {createRegion} from "boardgame-core";
|
||||
import {createGameCommandRegistry, createPromptDef, IGameContext} from "boardgame-core";
|
||||
import { Part } from "@/core/part";
|
||||
import { createRegion } from "@/core/region";
|
||||
import {
|
||||
createGameCommandRegistry,
|
||||
createPromptDef,
|
||||
IGameContext,
|
||||
} from "@/core/game";
|
||||
|
||||
const BOARD_SIZE = 3;
|
||||
const MAX_TURNS = BOARD_SIZE * BOARD_SIZE;
|
||||
const WINNING_LINES: number[][][] = [
|
||||
[[0, 0], [0, 1], [0, 2]],
|
||||
[[1, 0], [1, 1], [1, 2]],
|
||||
[[2, 0], [2, 1], [2, 2]],
|
||||
[[0, 0], [1, 0], [2, 0]],
|
||||
[[0, 1], [1, 1], [2, 1]],
|
||||
[[0, 2], [1, 2], [2, 2]],
|
||||
[[0, 0], [1, 1], [2, 2]],
|
||||
[[0, 2], [1, 1], [2, 0]],
|
||||
[
|
||||
[0, 0],
|
||||
[0, 1],
|
||||
[0, 2],
|
||||
],
|
||||
[
|
||||
[1, 0],
|
||||
[1, 1],
|
||||
[1, 2],
|
||||
],
|
||||
[
|
||||
[2, 0],
|
||||
[2, 1],
|
||||
[2, 2],
|
||||
],
|
||||
[
|
||||
[0, 0],
|
||||
[1, 0],
|
||||
[2, 0],
|
||||
],
|
||||
[
|
||||
[0, 1],
|
||||
[1, 1],
|
||||
[2, 1],
|
||||
],
|
||||
[
|
||||
[0, 2],
|
||||
[1, 2],
|
||||
[2, 2],
|
||||
],
|
||||
[
|
||||
[0, 0],
|
||||
[1, 1],
|
||||
[2, 2],
|
||||
],
|
||||
[
|
||||
[0, 2],
|
||||
[1, 1],
|
||||
[2, 0],
|
||||
],
|
||||
];
|
||||
|
||||
export type PlayerType = 'X' | 'O';
|
||||
export type WinnerType = PlayerType | 'draw' | null;
|
||||
export type PlayerType = "X" | "O";
|
||||
export type WinnerType = PlayerType | "draw" | null;
|
||||
export type TicTacToePart = Part<{ player: PlayerType }>;
|
||||
|
||||
export function createInitialState() {
|
||||
return {
|
||||
board: createRegion('board', [
|
||||
{ name: 'x', min: 0, max: BOARD_SIZE - 1 },
|
||||
{ name: 'y', min: 0, max: BOARD_SIZE - 1 },
|
||||
board: createRegion("board", [
|
||||
{ name: "x", min: 0, max: BOARD_SIZE - 1 },
|
||||
{ name: "y", min: 0, max: BOARD_SIZE - 1 },
|
||||
]),
|
||||
parts: {} as Record<string, TicTacToePart>,
|
||||
currentPlayer: 'X' as PlayerType,
|
||||
currentPlayer: "X" as PlayerType,
|
||||
winner: null as WinnerType,
|
||||
turn: 0,
|
||||
};
|
||||
|
|
@ -36,8 +72,9 @@ export type TicTacToeGame = IGameContext<TicTacToeState>;
|
|||
export const registry = createGameCommandRegistry<TicTacToeState>();
|
||||
export const prompts = {
|
||||
play: createPromptDef<[PlayerType, number, number]>(
|
||||
'play <player> <row:number> <col:number>')
|
||||
}
|
||||
"play <player> <row:number> <col:number>",
|
||||
),
|
||||
};
|
||||
|
||||
export async function start(game: TicTacToeGame) {
|
||||
while (true) {
|
||||
|
|
@ -45,10 +82,10 @@ export async function start(game: TicTacToeGame) {
|
|||
const turnNumber = game.value.turn + 1;
|
||||
const turnOutput = await turn(game, currentPlayer, turnNumber);
|
||||
|
||||
game.produce(state => {
|
||||
game.produce((state) => {
|
||||
state.winner = turnOutput.winner;
|
||||
if (!state.winner) {
|
||||
state.currentPlayer = state.currentPlayer === 'X' ? 'O' : 'X';
|
||||
state.currentPlayer = state.currentPlayer === "X" ? "O" : "X";
|
||||
state.turn = turnNumber;
|
||||
}
|
||||
});
|
||||
|
|
@ -59,7 +96,7 @@ export async function start(game: TicTacToeGame) {
|
|||
}
|
||||
|
||||
const turn = registry.register({
|
||||
schema: 'turn <player> <turnNumber:number>',
|
||||
schema: "turn <player> <turnNumber:number>",
|
||||
async run(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) {
|
||||
const { player, row, col } = await game.prompt(
|
||||
prompts.play,
|
||||
|
|
@ -74,32 +111,41 @@ const turn = registry.register({
|
|||
return { player, row, col };
|
||||
}
|
||||
},
|
||||
game.value.currentPlayer
|
||||
game.value.currentPlayer,
|
||||
);
|
||||
|
||||
placePiece(game, row, col, turnPlayer);
|
||||
|
||||
const winner = checkWinner(game);
|
||||
if (winner) return { winner };
|
||||
if (turnNumber >= MAX_TURNS) return { winner: 'draw' as WinnerType };
|
||||
if (turnNumber >= MAX_TURNS) return { winner: "draw" as WinnerType };
|
||||
|
||||
return { winner: null };
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
function isValidMove(row: number, col: number): boolean {
|
||||
return !isNaN(row) && !isNaN(col) && row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;
|
||||
return (
|
||||
!isNaN(row) &&
|
||||
!isNaN(col) &&
|
||||
row >= 0 &&
|
||||
row < BOARD_SIZE &&
|
||||
col >= 0 &&
|
||||
col < BOARD_SIZE
|
||||
);
|
||||
}
|
||||
|
||||
export function isCellOccupied(host: TicTacToeGame, row: number, col: number): boolean {
|
||||
export function isCellOccupied(
|
||||
host: TicTacToeGame,
|
||||
row: number,
|
||||
col: number,
|
||||
): boolean {
|
||||
return !!host.value.board.partMap[`${row},${col}`];
|
||||
}
|
||||
|
||||
export function hasWinningLine(positions: number[][]): boolean {
|
||||
return WINNING_LINES.some(line =>
|
||||
line.every(([r, c]) =>
|
||||
positions.some(([pr, pc]) => pr === r && pc === c)
|
||||
)
|
||||
return WINNING_LINES.some((line) =>
|
||||
line.every(([r, c]) => positions.some(([pr, pc]) => pr === r && pc === c)),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -107,24 +153,35 @@ export function checkWinner(host: TicTacToeGame): WinnerType {
|
|||
const parts = host.value.parts;
|
||||
const partsArray = Object.values(parts);
|
||||
|
||||
const xPositions = partsArray.filter((p: TicTacToePart) => p.player === 'X').map((p: TicTacToePart) => p.position);
|
||||
const oPositions = partsArray.filter((p: TicTacToePart) => p.player === 'O').map((p: TicTacToePart) => p.position);
|
||||
const xPositions = partsArray
|
||||
.filter((p: TicTacToePart) => p.player === "X")
|
||||
.map((p: TicTacToePart) => p.position);
|
||||
const oPositions = partsArray
|
||||
.filter((p: TicTacToePart) => p.player === "O")
|
||||
.map((p: TicTacToePart) => p.position);
|
||||
|
||||
if (hasWinningLine(xPositions)) return 'X';
|
||||
if (hasWinningLine(oPositions)) return 'O';
|
||||
if (partsArray.length >= MAX_TURNS) return 'draw';
|
||||
if (hasWinningLine(xPositions)) return "X";
|
||||
if (hasWinningLine(oPositions)) return "O";
|
||||
if (partsArray.length >= MAX_TURNS) return "draw";
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function placePiece(host: TicTacToeGame, row: number, col: number, player: PlayerType) {
|
||||
export function placePiece(
|
||||
host: TicTacToeGame,
|
||||
row: number,
|
||||
col: number,
|
||||
player: PlayerType,
|
||||
) {
|
||||
const board = host.value.board;
|
||||
const moveNumber = Object.keys(host.value.parts).length + 1;
|
||||
const piece: TicTacToePart = {
|
||||
regionId: 'board', position: [row, col], player,
|
||||
id: `piece-${player}-${moveNumber}`
|
||||
}
|
||||
host.produce(state => {
|
||||
regionId: "board",
|
||||
position: [row, col],
|
||||
player,
|
||||
id: `piece-${player}-${moveNumber}`,
|
||||
};
|
||||
host.produce((state) => {
|
||||
state.parts[piece.id] = piece;
|
||||
board.childIds.push(piece.id);
|
||||
board.partMap[`${row},${col}`] = piece.id;
|
||||
|
|
|
|||
|
|
@ -1,66 +1,81 @@
|
|||
import { describe, it, expect } from 'vitest';
|
||||
import { registry, createInitialState, BoopState } from '@/samples/boop';
|
||||
import { createGameContext } from '@/core/game';
|
||||
import type { PromptEvent } from '@/utils/command';
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { registry, createInitialState, BoopState } from "@/samples/boop";
|
||||
import { createGameContext } from "@/core/game";
|
||||
import type { PromptEvent } from "@/utils/command";
|
||||
|
||||
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);
|
||||
function waitForPrompt(
|
||||
ctx: ReturnType<typeof createTestContext>["ctx"],
|
||||
): Promise<PromptEvent> {
|
||||
return new Promise((resolve) => {
|
||||
ctx._commands.on("prompt", resolve);
|
||||
});
|
||||
}
|
||||
|
||||
describe('Boop Game', () => {
|
||||
describe('Setup', () => {
|
||||
it('should create initial state correctly', () => {
|
||||
describe("Boop Game", () => {
|
||||
describe("Setup", () => {
|
||||
it("should create initial state correctly", () => {
|
||||
const state = createInitialState();
|
||||
|
||||
expect(state.currentPlayer).toBe('white');
|
||||
expect(state.currentPlayer).toBe("white");
|
||||
expect(state.winner).toBeNull();
|
||||
expect(state.regions.board).toBeDefined();
|
||||
expect(state.regions.white).toBeDefined();
|
||||
expect(state.regions.black).toBeDefined();
|
||||
|
||||
// 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');
|
||||
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);
|
||||
|
||||
// 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');
|
||||
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);
|
||||
|
||||
// All cats should be in box (regionId = '')
|
||||
whiteCats.forEach(cat => expect(cat.regionId).toBe(''));
|
||||
blackCats.forEach(cat => expect(cat.regionId).toBe(''));
|
||||
whiteCats.forEach((cat) => expect(cat.regionId).toBe(""));
|
||||
blackCats.forEach((cat) => expect(cat.regionId).toBe(""));
|
||||
|
||||
// Kittens should be in player supplies
|
||||
whiteKittens.forEach(k => expect(k.regionId).toBe('white'));
|
||||
blackKittens.forEach(k => expect(k.regionId).toBe('black'));
|
||||
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 () => {
|
||||
describe("Place and Boop Commands", () => {
|
||||
it("should place a kitten via play command", async () => {
|
||||
const { ctx } = createTestContext();
|
||||
|
||||
// Use turn command instead of setup which runs indefinitely
|
||||
const promptPromise = waitForPrompt(ctx);
|
||||
const runPromise = ctx.run('turn white');
|
||||
const runPromise = ctx.run("turn white");
|
||||
|
||||
const promptEvent = await promptPromise;
|
||||
expect(promptEvent).not.toBeNull();
|
||||
expect(promptEvent.schema.name).toBe('play');
|
||||
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: {} });
|
||||
const error = promptEvent.tryCommit({
|
||||
name: "play",
|
||||
params: ["white", 2, 2, "kitten"],
|
||||
options: {},
|
||||
flags: {},
|
||||
});
|
||||
expect(error).toBeNull();
|
||||
|
||||
const result = await runPromise;
|
||||
|
|
@ -72,29 +87,41 @@ describe('Boop Game', () => {
|
|||
expect(boardPieces.length).toBeGreaterThan(0);
|
||||
|
||||
// Should have one less kitten in supply
|
||||
const whiteSupply = state.regions.white.childIds.filter(id => state.pieces[id].type === 'kitten');
|
||||
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 () => {
|
||||
describe("Boop Mechanics", () => {
|
||||
it("should boop adjacent pieces away from placement", async () => {
|
||||
const { ctx } = createTestContext();
|
||||
|
||||
// White places at 2,2
|
||||
let promptPromise = waitForPrompt(ctx);
|
||||
let runPromise = ctx.run('turn white');
|
||||
let runPromise = ctx.run("turn white");
|
||||
let promptEvent = await promptPromise;
|
||||
let error = promptEvent.tryCommit({ name: 'play', params: ['white', 2, 2, 'kitten'], options: {}, flags: {} });
|
||||
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);
|
||||
|
||||
// Black places at 2,3, which will boop white's piece
|
||||
promptPromise = waitForPrompt(ctx);
|
||||
runPromise = ctx.run('turn black');
|
||||
runPromise = ctx.run("turn black");
|
||||
promptEvent = await promptPromise;
|
||||
error = promptEvent.tryCommit({ name: 'play', params: ['black', 2, 3, 'kitten'], options: {}, flags: {} });
|
||||
error = promptEvent.tryCommit({
|
||||
name: "play",
|
||||
params: ["black", 2, 3, "kitten"],
|
||||
options: {},
|
||||
flags: {},
|
||||
});
|
||||
expect(error).toBeNull();
|
||||
result = await runPromise;
|
||||
expect(result.success).toBe(true);
|
||||
|
|
@ -105,152 +132,186 @@ describe('Boop Game', () => {
|
|||
expect(boardPieceCount).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
|
||||
it('should handle pieces being booped off the board', async () => {
|
||||
it("should handle pieces being booped off the board", async () => {
|
||||
const { ctx } = createTestContext();
|
||||
|
||||
// White places at corner
|
||||
const promptPromise = waitForPrompt(ctx);
|
||||
const runPromise = ctx.run('turn white');
|
||||
const runPromise = ctx.run("turn white");
|
||||
const promptEvent = await promptPromise;
|
||||
const error = promptEvent.tryCommit({ name: 'play', params: ['white', 0, 0, 'kitten'], options: {}, flags: {} });
|
||||
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;
|
||||
// Verify placement
|
||||
expect(state.regions.board.partMap['0,0']).toBeDefined();
|
||||
expect(state.regions.board.partMap["0,0"]).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Full Game Flow', () => {
|
||||
it('should play a turn and switch players', async () => {
|
||||
describe("Full Game Flow", () => {
|
||||
it("should play a turn and switch players", async () => {
|
||||
const { ctx } = createTestContext();
|
||||
|
||||
// White's turn - place at 2,2
|
||||
let promptPromise = waitForPrompt(ctx);
|
||||
let runPromise = ctx.run('turn white');
|
||||
let runPromise = ctx.run("turn white");
|
||||
let prompt = await promptPromise;
|
||||
const error1 = prompt.tryCommit({ name: 'play', params: ['white', 2, 2, 'kitten'], options: {}, flags: {} });
|
||||
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;
|
||||
// Should have placed a piece
|
||||
expect(stateAfterWhite.regions.board.partMap['2,2']).toBeDefined();
|
||||
expect(stateAfterWhite.regions.board.partMap["2,2"]).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Kitten vs Cat Hierarchy', () => {
|
||||
it('should not boop cats when placing a kitten', async () => {
|
||||
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 runPromise = ctx.run("turn white");
|
||||
let prompt = await promptPromise;
|
||||
let error = prompt.tryCommit({ name: 'play', params: ['white', 2, 2, 'kitten'], options: {}, flags: {} });
|
||||
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';
|
||||
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');
|
||||
runPromise = ctx.run("turn black");
|
||||
prompt = await promptPromise;
|
||||
error = prompt.tryCommit({ name: 'play', params: ['black', 2, 3, 'kitten'], options: {}, flags: {} });
|
||||
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');
|
||||
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');
|
||||
expect(state.regions.board.partMap["2,3"]).toBe("black-kitten-1");
|
||||
});
|
||||
|
||||
it('should boop both kittens and cats when placing a cat', async () => {
|
||||
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';
|
||||
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';
|
||||
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';
|
||||
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);
|
||||
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';
|
||||
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);
|
||||
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';
|
||||
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 runPromise = ctx.run("turn white");
|
||||
const prompt = await promptPromise;
|
||||
const error = prompt.tryCommit({ name: 'play', params: ['white', 2, 2, 'cat'], options: {}, flags: {} });
|
||||
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');
|
||||
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 () => {
|
||||
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 runPromise = ctx.run("turn white");
|
||||
let prompt = await promptPromise;
|
||||
let error = prompt.tryCommit({ name: 'play', params: ['white', 2, 2, 'kitten'], options: {}, flags: {} });
|
||||
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);
|
||||
|
|
@ -261,9 +322,14 @@ describe('Boop Game', () => {
|
|||
|
||||
// Black places at 3,3
|
||||
promptPromise = waitForPrompt(ctx);
|
||||
runPromise = ctx.run('turn black');
|
||||
runPromise = ctx.run("turn black");
|
||||
prompt = await promptPromise;
|
||||
error = prompt.tryCommit({ name: 'play', params: ['black', 3, 3, 'kitten'], options: {}, flags: {} });
|
||||
error = prompt.tryCommit({
|
||||
name: "play",
|
||||
params: ["black", 3, 3, "kitten"],
|
||||
options: {},
|
||||
flags: {},
|
||||
});
|
||||
expect(error).toBeNull();
|
||||
result = await runPromise;
|
||||
expect(result.success).toBe(true);
|
||||
|
|
@ -276,294 +342,334 @@ describe('Boop Game', () => {
|
|||
expect(boardPieces.length).toBe(2);
|
||||
|
||||
// Find black's piece
|
||||
const blackPiece = boardPieces.find(([pos, id]) => state.pieces[id]?.player === 'black');
|
||||
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 () => {
|
||||
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');
|
||||
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();
|
||||
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');
|
||||
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();
|
||||
expect(state.regions.board.partMap["3,3"]).toBeDefined();
|
||||
// 4,4 should still be occupied
|
||||
expect(state.regions.board.partMap['4,4']).toBeDefined();
|
||||
expect(state.regions.board.partMap["4,4"]).toBeDefined();
|
||||
// 2,3 should have black's new piece
|
||||
expect(state.regions.board.partMap['2,3']).toBeDefined();
|
||||
expect(state.regions.board.partMap["2,3"]).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Graduation Mechanic', () => {
|
||||
it('should graduate three kittens in a row to cats', async () => {
|
||||
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'];
|
||||
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.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);
|
||||
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.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);
|
||||
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.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);
|
||||
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();
|
||||
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'
|
||||
(id) => stateBefore.pieces[id].type === "cat",
|
||||
);
|
||||
expect(catsInWhiteSupplyBefore.length).toBe(0);
|
||||
|
||||
// Run check-graduates command
|
||||
const result = await ctx._commands.run('check-graduates');
|
||||
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();
|
||||
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'
|
||||
(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'
|
||||
(id) => state.pieces[id].type === "kitten",
|
||||
);
|
||||
expect(kittensInWhiteSupply.length).toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Win Detection', () => {
|
||||
it('should detect horizontal win with three cats', async () => {
|
||||
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'];
|
||||
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.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);
|
||||
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.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);
|
||||
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.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);
|
||||
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');
|
||||
const result = await ctx._commands.run("check-win");
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(result.result).toBe('white');
|
||||
expect(result.result).toBe("white");
|
||||
}
|
||||
});
|
||||
|
||||
it('should detect vertical win with three cats', async () => {
|
||||
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'];
|
||||
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.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);
|
||||
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.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);
|
||||
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.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);
|
||||
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');
|
||||
const result = await ctx._commands.run("check-win");
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(result.result).toBe('black');
|
||||
expect(result.result).toBe("black");
|
||||
}
|
||||
});
|
||||
|
||||
it('should detect diagonal win with three cats', async () => {
|
||||
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'];
|
||||
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.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);
|
||||
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.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);
|
||||
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.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);
|
||||
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');
|
||||
const result = await ctx._commands.run("check-win");
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(result.result).toBe('white');
|
||||
expect(result.result).toBe("white");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('Placing Cats', () => {
|
||||
it('should allow placing a cat from supply', async () => {
|
||||
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';
|
||||
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 runPromise = ctx.run("turn white");
|
||||
const prompt = await promptPromise;
|
||||
const error = prompt.tryCommit({ name: 'play', params: ['white', 2, 2, 'cat'], options: {}, flags: {} });
|
||||
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');
|
||||
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');
|
||||
const whiteCatsInSupply = state.regions.white.childIds.filter(
|
||||
(id) => state.pieces[id].type === "cat",
|
||||
);
|
||||
expect(whiteCatsInSupply.length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Check Full Board', () => {
|
||||
it('should not trigger when player has fewer than 8 pieces on board', async () => {
|
||||
describe("Check Full Board", () => {
|
||||
it("should not trigger when player has fewer than 8 pieces on board", async () => {
|
||||
const { ctx } = createTestContext();
|
||||
|
||||
// White places a single kitten
|
||||
const promptPromise = waitForPrompt(ctx);
|
||||
const runPromise = ctx.run('turn white');
|
||||
const runPromise = ctx.run("turn white");
|
||||
const prompt = await promptPromise;
|
||||
const error = prompt.tryCommit({ name: 'play', params: ['white', 2, 2, 'kitten'], options: {}, flags: {} });
|
||||
const error = prompt.tryCommit({
|
||||
name: "play",
|
||||
params: ["white", 2, 2, "kitten"],
|
||||
options: {},
|
||||
flags: {},
|
||||
});
|
||||
expect(error).toBeNull();
|
||||
const result = await runPromise;
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
// check-full-board should return without prompting
|
||||
const fullBoardResult = await ctx._commands.run('check-full-board white');
|
||||
const fullBoardResult = await ctx._commands.run("check-full-board white");
|
||||
expect(fullBoardResult.success).toBe(true);
|
||||
});
|
||||
|
||||
it('should force graduation when all 8 pieces are on board', async () => {
|
||||
it("should force graduation when all 8 pieces are on board", async () => {
|
||||
const { ctx } = createTestContext();
|
||||
|
||||
// Manually place all 8 white pieces on the board
|
||||
ctx.produce(state => {
|
||||
ctx.produce((state) => {
|
||||
for (let i = 1; i <= 8; i++) {
|
||||
const piece = state.pieces[`white-kitten-${i}`];
|
||||
if (piece) {
|
||||
const row = Math.floor((i - 1) / 4);
|
||||
const col = (i - 1) % 4;
|
||||
piece.regionId = 'board';
|
||||
piece.regionId = "board";
|
||||
piece.position = [row, col];
|
||||
state.regions.board.partMap[`${row},${col}`] = piece.id;
|
||||
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== piece.id);
|
||||
state.regions.white.childIds = state.regions.white.childIds.filter(
|
||||
(id) => id !== piece.id,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
@ -574,12 +680,17 @@ describe('Boop Game', () => {
|
|||
|
||||
// Run check-full-board - should prompt for piece to graduate
|
||||
const promptPromise = waitForPrompt(ctx);
|
||||
const runPromise = ctx._commands.run('check-full-board white');
|
||||
const runPromise = ctx._commands.run("check-full-board white");
|
||||
const prompt = await promptPromise;
|
||||
expect(prompt.schema.name).toBe('choose');
|
||||
expect(prompt.schema.name).toBe("choose");
|
||||
|
||||
// Select a piece to graduate
|
||||
const error = prompt.tryCommit({ name: 'choose', params: ['white', 0, 0], options: {}, flags: {} });
|
||||
const error = prompt.tryCommit({
|
||||
name: "choose",
|
||||
params: ["white", 0, 0],
|
||||
options: {},
|
||||
flags: {},
|
||||
});
|
||||
expect(error).toBeNull();
|
||||
|
||||
const result = await runPromise;
|
||||
|
|
@ -587,65 +698,73 @@ describe('Boop Game', () => {
|
|||
|
||||
const state = ctx.value;
|
||||
// Position 0,0 should be empty (piece moved to box)
|
||||
expect(state.regions.board.partMap['0,0']).toBeUndefined();
|
||||
expect(state.regions.board.partMap["0,0"]).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should not trigger when player has a winner', async () => {
|
||||
it("should not trigger when player has a winner", async () => {
|
||||
const { ctx } = createTestContext();
|
||||
|
||||
// Set up a winning state for white
|
||||
ctx.produce(state => {
|
||||
state.winner = 'white';
|
||||
ctx.produce((state) => {
|
||||
state.winner = "white";
|
||||
for (let i = 1; i <= 8; i++) {
|
||||
const piece = state.pieces[`white-kitten-${i}`];
|
||||
if (piece) {
|
||||
const row = Math.floor((i - 1) / 4);
|
||||
const col = (i - 1) % 4;
|
||||
piece.regionId = 'board';
|
||||
piece.regionId = "board";
|
||||
piece.position = [row, col];
|
||||
state.regions.board.partMap[`${row},${col}`] = piece.id;
|
||||
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== piece.id);
|
||||
state.regions.white.childIds = state.regions.white.childIds.filter(
|
||||
(id) => id !== piece.id,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// check-full-board should return without prompting
|
||||
const fullBoardResult = await ctx._commands.run('check-full-board white');
|
||||
const fullBoardResult = await ctx._commands.run("check-full-board white");
|
||||
expect(fullBoardResult.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Start Command', () => {
|
||||
it('should run a complete game until there is a winner', async () => {
|
||||
describe("Start Command", () => {
|
||||
it("should run a complete game until there is a winner", async () => {
|
||||
const { ctx } = createTestContext();
|
||||
|
||||
// Set up a quick win scenario
|
||||
ctx.produce(state => {
|
||||
ctx.produce((state) => {
|
||||
// Place three white cats in a row
|
||||
const c1 = state.pieces['white-cat-1'];
|
||||
const c2 = state.pieces['white-cat-2'];
|
||||
const c3 = state.pieces['white-cat-3'];
|
||||
const c1 = state.pieces["white-cat-1"];
|
||||
const c2 = state.pieces["white-cat-2"];
|
||||
const c3 = state.pieces["white-cat-3"];
|
||||
|
||||
if (c1) {
|
||||
c1.type = 'cat';
|
||||
c1.regionId = 'board';
|
||||
c1.type = "cat";
|
||||
c1.regionId = "board";
|
||||
c1.position = [0, 0];
|
||||
state.regions.board.partMap['0,0'] = c1.id;
|
||||
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== c1.id);
|
||||
state.regions.board.partMap["0,0"] = c1.id;
|
||||
state.regions.white.childIds = state.regions.white.childIds.filter(
|
||||
(id) => id !== c1.id,
|
||||
);
|
||||
}
|
||||
if (c2) {
|
||||
c2.type = 'cat';
|
||||
c2.regionId = 'board';
|
||||
c2.type = "cat";
|
||||
c2.regionId = "board";
|
||||
c2.position = [0, 1];
|
||||
state.regions.board.partMap['0,1'] = c2.id;
|
||||
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== c2.id);
|
||||
state.regions.board.partMap["0,1"] = c2.id;
|
||||
state.regions.white.childIds = state.regions.white.childIds.filter(
|
||||
(id) => id !== c2.id,
|
||||
);
|
||||
}
|
||||
if (c3) {
|
||||
c3.type = 'cat';
|
||||
c3.regionId = 'board';
|
||||
c3.type = "cat";
|
||||
c3.regionId = "board";
|
||||
c3.position = [0, 2];
|
||||
state.regions.board.partMap['0,2'] = c3.id;
|
||||
state.regions.white.childIds = state.regions.white.childIds.filter(id => id !== c3.id);
|
||||
state.regions.board.partMap["0,2"] = c3.id;
|
||||
state.regions.white.childIds = state.regions.white.childIds.filter(
|
||||
(id) => id !== c3.id,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -653,11 +772,16 @@ describe('Boop Game', () => {
|
|||
// Note: start() runs indefinitely until there's a winner
|
||||
// Since we already set up a win, it should complete after one turn
|
||||
const promptPromise = waitForPrompt(ctx);
|
||||
const startPromise = ctx.run('turn white');
|
||||
const startPromise = ctx.run("turn white");
|
||||
|
||||
const prompt = await promptPromise;
|
||||
// Complete the turn
|
||||
const error = prompt.tryCommit({ name: 'play', params: ['white', 3, 3, 'kitten'], options: {}, flags: {} });
|
||||
const error = prompt.tryCommit({
|
||||
name: "play",
|
||||
params: ["white", 3, 3, "kitten"],
|
||||
options: {},
|
||||
flags: {},
|
||||
});
|
||||
expect(error).toBeNull();
|
||||
|
||||
const result = await startPromise;
|
||||
|
|
|
|||
Loading…
Reference in New Issue