Compare commits

...

5 Commits

Author SHA1 Message Date
hypercross 34cb828f60 test: refactor and format test files
- Update `game.test.ts` to use `_ctx` when `this` is not required
- Fix type assertion in `boop.test.ts`
- Update `command-runner.test.ts` to use `parseCommandSchema` in prompt
  tests
- Reformat `middleware.test.ts` to use double quotes and consistent
  indentation
2026-04-20 15:50:32 +08:00
hypercross 974c1a828c test: reformat and expand test coverage 2026-04-20 15:37:15 +08:00
hypercross b83ff28f60 style: format tic-tac-toe sample and boop tests 2026-04-20 15:19:27 +08:00
hypercross 0c94e6923a test: reformat tests to use double quotes and improve style 2026-04-20 15:16:03 +08:00
hypercross d5ec099540 feat(slay-the-spire-like): implement encounter system
Add logic for handling different encounter types including combat,
shop, curio, camp, and events. Includes shop item generation and
grid inventory placement for shop encounters.
2026-04-20 14:57:03 +08:00
13 changed files with 3513 additions and 2986 deletions

View File

@ -29,6 +29,12 @@ export function buildCombatState(
};
}
export function buildCombatEncounterState(
data: EncounterData<"minion" | "elite">,
): CombatEncounterState {
return { data, blocked: false };
}
function createEnemyEntities(encounter: EncounterData): EnemyEntity[] {
const enemies: EnemyEntity[] = [];
let instanceCounter = 0;

View File

@ -0,0 +1,100 @@
import { createGridInventory, placeItem } from "../grid-inventory";
import { GameItemMeta } from "../grid-inventory/types";
import { IDENTITY_TRANSFORM } from "../utils/shape-collision";
import { parseShapeString } from "../utils/parse-shape";
import { EncounterData, EncounterType, ItemData } from "../types";
import type { RNG } from "@/utils/rng";
import type {
CampEncounterState,
CombatEncounterState,
CurioEncounterState,
DialogueEncounterState,
EncounterState,
ShopEncounterState,
} from "./types";
import { buildCombatEncounterState } from "./combat";
import { buildShopEncounterState } from "./shop";
function createCurioItems(allItems: ItemData[], rng: RNG): GameItemMeta[] {
const curioItems: GameItemMeta[] = [];
const rolledIndices = new Set<number>();
for (let i = 0; i < 3 && rolledIndices.size < allItems.length; i++) {
let index: number;
do {
index = rng.nextInt(allItems.length);
} while (rolledIndices.has(index));
rolledIndices.add(index);
const itemData = allItems[index];
const shape = parseShapeString(itemData.shape);
curioItems.push({ itemData, shape });
}
return curioItems;
}
export function buildCurioEncounterState(
data: EncounterData<"curio">,
allItems: ItemData[],
rng: RNG,
): CurioEncounterState {
const items = createCurioItems(allItems, rng);
const inventory = createGridInventory<GameItemMeta>(6, 4);
for (let i = 0; i < items.length; i++) {
const meta = items[i];
placeItem(inventory, {
id: `curio-item-${i}`,
shape: meta.shape,
transform: { ...IDENTITY_TRANSFORM, offset: { x: i, y: 0 } },
meta,
});
}
return { data, items: inventory };
}
export function buildCampEncounterState(
data: EncounterData<"camp">,
): CampEncounterState {
return { data };
}
export function buildDialogueEncounterState(
data: EncounterData<"event">,
): DialogueEncounterState {
return { data, blocked: false };
}
export function buildEncounterState(
data: EncounterData<EncounterType>,
allItems: ItemData[],
rng: RNG,
idCounter: { value: number },
): EncounterState {
switch (data.type) {
case "minion":
case "elite":
return buildCombatEncounterState(
data as EncounterData<"minion" | "elite">,
);
case "shop":
return buildShopEncounterState(
data as EncounterData<"shop">,
allItems,
rng,
idCounter,
);
case "curio":
return buildCurioEncounterState(
data as EncounterData<"curio">,
allItems,
rng,
);
case "camp":
return buildCampEncounterState(data as EncounterData<"camp">);
case "event":
return buildDialogueEncounterState(data as EncounterData<"event">);
}
}

View File

@ -1,3 +1,4 @@
export { buildCombatState, buildCombatEncounterState } from "./combat";
export { buildShopEncounterState, generateInstanceId } from "./shop";
export { buildEncounterState } from "./encounter";
export { RunState, EncounterState } from "./types";
export { buildCombatState } from "./combat";
export { generateInstanceId } from "./shop";

View File

@ -1,4 +1,63 @@
import { createGridInventory, placeItem } from "../grid-inventory";
import { GameItemMeta } from "../grid-inventory/types";
import { IDENTITY_TRANSFORM } from "../utils/shape-collision";
import { parseShapeString } from "../utils/parse-shape";
import { EncounterData, ItemData } from "../types";
import type { RNG } from "@/utils/rng";
import type { ShopEncounterState } from "./types";
export function generateInstanceId(counter: { value: number }): string {
counter.value++;
return `item-${counter.value}`;
}
function createShopItems(
allItems: ItemData[],
rng: RNG,
): (GameItemMeta & { sellPrice: number })[] {
const shopItems: (GameItemMeta & { sellPrice: number })[] = [];
const rolledIndices = new Set<number>();
for (let i = 0; i < 5 && rolledIndices.size < allItems.length; i++) {
let index: number;
do {
index = rng.nextInt(allItems.length);
} while (rolledIndices.has(index));
rolledIndices.add(index);
const itemData = allItems[index];
const shape = parseShapeString(itemData.shape);
const sellPrice = Math.floor(
(rng.nextInt(5) + rng.nextInt(5) + 1) * 0.2 * itemData.price,
);
shopItems.push({ itemData, shape, sellPrice });
}
return shopItems;
}
export function buildShopEncounterState(
data: EncounterData<"shop">,
allItems: ItemData[],
rng: RNG,
idCounter: { value: number },
): ShopEncounterState {
const items = createShopItems(allItems, rng);
const inventory = createGridInventory<GameItemMeta & { sellPrice: number }>(
6,
4,
);
for (let i = 0; i < items.length; i++) {
const meta = items[i];
placeItem(inventory, {
id: generateInstanceId(idCounter),
shape: meta.shape,
transform: { ...IDENTITY_TRANSFORM, offset: { x: i, y: 0 } },
meta,
});
}
return { data, items: inventory };
}

View File

@ -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,9 +96,9 @@ 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(
const { player, row, col } = await game.prompt(
prompts.play,
(player, row, col) => {
if (player !== turnPlayer) {
@ -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;

View File

@ -1,95 +1,84 @@
import { describe, it, expect } from 'vitest';
import { describe, it, expect } from "vitest";
import {
registry,
createInitialState,
start,
TicTacToeState,
WinnerType,
PlayerType
} from '@/samples/tic-tac-toe';
import { createGameHost, GameHost } from '@/core/game-host';
import type { PromptEvent } from '@/utils/command';
import { MutableSignal } from '@/utils/mutable-signal';
import {IGameContext} from "../../src";
type TestGameHost = GameHost<TicTacToeState> & {
_context: IGameContext<TicTacToeState>;
context: IGameContext<TicTacToeState>;
}
} from "@/samples/tic-tac-toe";
import { createGameHost, GameHost } from "@/core/game-host";
import type { PromptEvent } from "@/utils/command";
function createTestHost() {
const host: TestGameHost = createGameHost(
{ registry, createInitialState, start }
);
host.context = host._context;
const host = createGameHost({ registry, createInitialState, start });
return { host };
}
function waitForPromptEvent(host: GameHost<any>): Promise<PromptEvent> {
return new Promise(resolve => {
host.context._commands.on('prompt', resolve);
return new Promise((resolve) => {
// @ts-ignore - accessing private _context for testing
host._context._commands.on("prompt", resolve);
});
}
describe('GameHost', () => {
describe('creation', () => {
it('should create host with initial state', () => {
describe("GameHost", () => {
describe("creation", () => {
it("should create host with initial state", () => {
const { host } = createTestHost();
expect(host.context._state.value.currentPlayer).toBe('X');
expect(host.context._state.value.winner).toBeNull();
expect(host.context._state.value.turn).toBe(0);
expect(Object.keys(host.context._state.value.parts).length).toBe(0);
expect(host.state.value.currentPlayer).toBe("X");
expect(host.state.value.winner).toBeNull();
expect(host.state.value.turn).toBe(0);
expect(Object.keys(host.state.value.parts).length).toBe(0);
});
it('should have status "created" by default', () => {
const { host } = createTestHost();
expect(host.status.value).toBe('created');
expect(host.status.value).toBe("created");
});
it('should have null activePromptSchema initially', () => {
it("should have null activePromptSchema initially", () => {
const { host } = createTestHost();
expect(host.activePromptSchema.value).toBeNull();
});
});
describe('tryInput', () => {
describe("tryInput", () => {
it('should return "No active prompt" when no prompt is active', () => {
const { host } = createTestHost();
const result = host.tryInput('play X 1 1');
expect(result).toBe('No active prompt');
const result = host.tryInput("play X 1 1");
expect(result).toBe("No active prompt");
});
it('should accept valid input when prompt is active', async () => {
it("should accept valid input when prompt is active", async () => {
const { host } = createTestHost();
const promptPromise = waitForPromptEvent(host);
const runPromise = host.start();
const promptEvent = await promptPromise;
expect(promptEvent.schema.name).toBe('play');
expect(host.activePromptSchema.value?.name).toBe('play');
expect(promptEvent.schema.name).toBe("play");
expect(host.activePromptSchema.value?.name).toBe("play");
const error = host.tryInput('play X 1 1');
const error = host.tryInput("play X 1 1");
expect(error).toBeNull();
// Cancel to end the game since start runs until game over
const nextPromptPromise = waitForPromptEvent(host);
const nextPrompt = await nextPromptPromise;
nextPrompt.cancel('test cleanup');
nextPrompt.cancel("test cleanup");
try {
await runPromise;
} catch (e) {
const error = e as Error;
expect(error.message).toBe('test cleanup');
expect(error.message).toBe("test cleanup");
}
});
it('should reject invalid input', async () => {
it("should reject invalid input", async () => {
const { host } = createTestHost();
const promptPromise = waitForPromptEvent(host);
@ -97,29 +86,29 @@ describe('GameHost', () => {
const promptEvent = await promptPromise;
const error = host.tryInput('invalid command');
const error = host.tryInput("invalid command");
expect(error).not.toBeNull();
promptEvent.cancel('test cleanup');
promptEvent.cancel("test cleanup");
try {
await runPromise;
} catch (e) {
const error = e as Error;
expect(error.message).toBe('test cleanup');
expect(error.message).toBe("test cleanup");
}
});
it('should return error when disposed', () => {
it("should return error when disposed", () => {
const { host } = createTestHost();
host.dispose();
const result = host.tryInput('play X 1 1');
expect(result).toBe('GameHost is disposed');
const result = host.tryInput("play X 1 1");
expect(result).toBe("GameHost is disposed");
});
});
describe('getActivePromptSchema', () => {
it('should return schema when prompt is active', async () => {
describe("getActivePromptSchema", () => {
it("should return schema when prompt is active", async () => {
const { host } = createTestHost();
const promptPromise = waitForPromptEvent(host);
@ -129,26 +118,26 @@ describe('GameHost', () => {
const schema = host.activePromptSchema.value;
expect(schema).not.toBeNull();
expect(schema?.name).toBe('play');
expect(schema?.name).toBe("play");
promptEvent.cancel('test cleanup');
promptEvent.cancel("test cleanup");
try {
await runPromise;
} catch (e) {
const error = e as Error;
expect(error.message).toBe('test cleanup');
expect(error.message).toBe("test cleanup");
}
});
it('should return null when no prompt is active', () => {
it("should return null when no prompt is active", () => {
const { host } = createTestHost();
expect(host.activePromptSchema.value).toBeNull();
});
});
describe('start', () => {
it('should reset state and run start command', async () => {
describe("start", () => {
it("should reset state and run start command", async () => {
const { host } = createTestHost();
// First setup - make one move
@ -157,20 +146,25 @@ describe('GameHost', () => {
let promptEvent = await promptPromise;
// Make a move
promptEvent.tryCommit({ name: 'play', params: ['X', 1, 1], options: {}, flags: {} });
promptEvent.tryCommit({
name: "play",
params: ["X", 1, 1],
options: {},
flags: {},
});
// Wait for next prompt (next turn) and cancel
promptPromise = waitForPromptEvent(host);
promptEvent = await promptPromise;
promptEvent.cancel('test end');
promptEvent.cancel("test end");
try {
await runPromise;
} catch (e) {
const error = e as Error;
expect(error.message).toBe('test end');
expect(error.message).toBe("test end");
}
expect(Object.keys(host.context._state.value.parts).length).toBe(1);
expect(Object.keys(host.state.value.parts).length).toBe(1);
// Setup listener before calling start
const newPromptPromise = waitForPromptEvent(host);
@ -179,15 +173,15 @@ describe('GameHost', () => {
const newRunPromise = host.start();
// State should be back to initial
expect(host.context._state.value.currentPlayer).toBe('X');
expect(host.context._state.value.winner).toBeNull();
expect(host.context._state.value.turn).toBe(0);
expect(Object.keys(host.context._state.value.parts).length).toBe(0);
expect(host.state.value.currentPlayer).toBe("X");
expect(host.state.value.winner).toBeNull();
expect(host.state.value.turn).toBe(0);
expect(Object.keys(host.state.value.parts).length).toBe(0);
// New game should be running and prompting
const newPrompt = await newPromptPromise;
expect(newPrompt.schema.name).toBe('play');
newPrompt.cancel('test end');
expect(newPrompt.schema.name).toBe("play");
newPrompt.cancel("test end");
try {
await newRunPromise;
@ -196,7 +190,7 @@ describe('GameHost', () => {
}
});
it('should cancel active prompt during start', async () => {
it("should cancel active prompt during start", async () => {
const { host } = createTestHost();
const promptPromise = waitForPromptEvent(host);
@ -212,31 +206,31 @@ describe('GameHost', () => {
await runPromise;
} catch (e) {
const error = e as Error;
expect(error.message).toContain('Cancelled');
expect(error.message).toContain("Cancelled");
}
// State should be reset
expect(host.context._state.value.currentPlayer).toBe('X');
expect(host.context._state.value.turn).toBe(0);
expect(host.state.value.currentPlayer).toBe("X");
expect(host.state.value.turn).toBe(0);
});
it('should throw error when disposed', () => {
it("should throw error when disposed", () => {
const { host } = createTestHost();
host.dispose();
expect(() => host.start()).toThrow('GameHost is disposed');
expect(() => host.start()).toThrow("GameHost is disposed");
});
});
describe('dispose', () => {
it('should change status to disposed', () => {
describe("dispose", () => {
it("should change status to disposed", () => {
const { host } = createTestHost();
host.dispose();
expect(host.status.value).toBe('disposed');
expect(host.status.value).toBe("disposed");
});
it('should cancel active prompt on dispose', async () => {
it("should cancel active prompt on dispose", async () => {
const { host } = createTestHost();
const promptPromise = waitForPromptEvent(host);
@ -251,25 +245,25 @@ describe('GameHost', () => {
await runPromise;
} catch (e) {
const error = e as Error;
expect(error.message).toContain('Cancelled');
expect(error.message).toContain("Cancelled");
}
});
it('should be idempotent', () => {
it("should be idempotent", () => {
const { host } = createTestHost();
host.dispose();
host.dispose(); // Should not throw
expect(host.status.value).toBe('disposed');
expect(host.status.value).toBe("disposed");
});
});
describe('events', () => {
it('should emit start event', async () => {
describe("events", () => {
it("should emit start event", async () => {
const { host } = createTestHost();
let setupCount = 0;
host.on('start', () => {
host.on("start", () => {
setupCount++;
});
@ -281,11 +275,11 @@ describe('GameHost', () => {
expect(setupCount).toBe(1);
// State should be running
expect(host.status.value).toBe('running');
expect(host.status.value).toBe("running");
// Cancel the background setup command
const prompt = await promptPromise;
prompt.cancel('test end');
prompt.cancel("test end");
try {
await runPromise;
@ -294,11 +288,11 @@ describe('GameHost', () => {
}
});
it('should emit dispose event', () => {
it("should emit dispose event", () => {
const { host } = createTestHost();
let disposeCount = 0;
host.on('dispose', () => {
host.on("dispose", () => {
disposeCount++;
});
@ -306,11 +300,11 @@ describe('GameHost', () => {
expect(disposeCount).toBe(1);
});
it('should allow unsubscribing from events', () => {
it("should allow unsubscribing from events", () => {
const { host } = createTestHost();
let setupCount = 0;
const unsubscribe = host.on('start', () => {
const unsubscribe = host.on("start", () => {
setupCount++;
});
@ -318,43 +312,48 @@ describe('GameHost', () => {
// No event should be emitted
// (we can't easily test this without triggering setup, but we verify unsubscribe works)
expect(typeof unsubscribe).toBe('function');
expect(typeof unsubscribe).toBe("function");
});
});
describe('reactive state', () => {
it('should have state that reflects game progress', async () => {
describe("reactive state", () => {
it("should have state that reflects game progress", async () => {
const { host } = createTestHost();
// Initial state
expect(host.context._state.value.currentPlayer).toBe('X');
expect(host.context._state.value.turn).toBe(0);
expect(host.state.value.currentPlayer).toBe("X");
expect(host.state.value.turn).toBe(0);
// Make a move
const promptPromise = waitForPromptEvent(host);
const runPromise = host.start();
const promptEvent = await promptPromise;
promptEvent.tryCommit({ name: 'play', params: ['X', 1, 1], options: {}, flags: {} });
promptEvent.tryCommit({
name: "play",
params: ["X", 1, 1],
options: {},
flags: {},
});
// Wait for next prompt and cancel
const nextPromptPromise = waitForPromptEvent(host);
const nextPrompt = await nextPromptPromise;
nextPrompt.cancel('test end');
nextPrompt.cancel("test end");
try {
await runPromise;
} catch (e) {
const error = e as Error;
expect(error.message).toBe('test end');
expect(error.message).toBe("test end");
}
expect(host.context._state.value.currentPlayer).toBe('O');
expect(host.context._state.value.turn).toBe(1);
expect(Object.keys(host.context._state.value.parts).length).toBe(1);
expect(host.state.value.currentPlayer).toBe("O");
expect(host.state.value.turn).toBe(1);
expect(Object.keys(host.state.value.parts).length).toBe(1);
});
it('should update activePromptSchema reactively', async () => {
it("should update activePromptSchema reactively", async () => {
const { host } = createTestHost();
// Initially null
@ -368,11 +367,11 @@ describe('GameHost', () => {
// Now schema should be set
expect(host.activePromptSchema.value).not.toBeNull();
expect(host.activePromptSchema.value?.name).toBe('play');
expect(host.activePromptSchema.value?.name).toBe("play");
// Cancel and wait
const cancelEvent = host.activePromptSchema.value;
host.context._commands._cancel();
// @ts-ignore - accessing private _context for testing
host._context._commands._cancel();
try {
await runPromise;
} catch {
@ -384,29 +383,30 @@ describe('GameHost', () => {
});
});
describe('full game', () => {
it('should run a complete game of tic-tac-toe with X winning diagonally', async () => {
describe("full game", () => {
it("should run a complete game of tic-tac-toe with X winning diagonally", async () => {
const { host } = createTestHost();
// Initial state
expect(host.context._state.value.currentPlayer).toBe('X');
expect(host.context._state.value.winner).toBeNull();
expect(host.context._state.value.turn).toBe(0);
expect(Object.keys(host.context._state.value.parts).length).toBe(0);
expect(host.state.value.currentPlayer).toBe("X");
expect(host.state.value.winner).toBeNull();
expect(host.state.value.turn).toBe(0);
expect(Object.keys(host.state.value.parts).length).toBe(0);
// X wins diagonally: (0,0), (1,1), (2,2)
// O plays: (0,1), (2,1)
const moves = [
'play X 0 0', // turn 1: X
'play O 0 1', // turn 2: O
'play X 1 1', // turn 3: X
'play O 2 1', // turn 4: O
'play X 2 2', // turn 5: X wins!
"play X 0 0", // turn 1: X
"play O 0 1", // turn 2: O
"play X 1 1", // turn 3: X
"play O 2 1", // turn 4: O
"play X 2 2", // turn 5: X wins!
];
// Track prompt events in a queue
const promptEvents: PromptEvent[] = [];
host.context._commands.on('prompt', (e) => {
// @ts-ignore - accessing private _context for testing
host._context._commands.on("prompt", (e) => {
promptEvents.push(e);
});
@ -416,71 +416,83 @@ describe('GameHost', () => {
for (let i = 0; i < moves.length; i++) {
// Wait until the next prompt event arrives
while (i >= promptEvents.length) {
await new Promise(r => setTimeout(r, 10));
await new Promise((r) => setTimeout(r, 10));
}
const promptEvent = promptEvents[i];
expect(promptEvent.schema.name).toBe('play');
expect(promptEvent.schema.name).toBe("play");
// Submit the move
const error = host.tryInput(moves[i]);
expect(error).toBeNull();
// Wait for the command to complete before submitting next move
await new Promise(resolve => setImmediate(resolve));
await new Promise((resolve) => setImmediate(resolve));
}
// Wait for setup to complete (game ended with winner)
try {
const finalState = await setupPromise;
expect(finalState.winner).toBe('X');
expect(finalState.winner).toBe("X");
// Final state checks
expect(host.context._state.value.winner).toBe('X');
expect(host.context._state.value.currentPlayer).toBe('X');
expect(Object.keys(host.context._state.value.parts).length).toBe(5);
expect(host.state.value.winner).toBe("X");
expect(host.state.value.currentPlayer).toBe("X");
expect(Object.keys(host.state.value.parts).length).toBe(5);
// Verify winning diagonal
const parts = Object.values(host.context._state.value.parts);
const xPieces = parts.filter(p => p.player === 'X');
const parts = Object.values(host.state.value.parts);
const xPieces = parts.filter((p: any) => p.player === "X");
expect(xPieces).toHaveLength(3);
expect(xPieces.some(p => JSON.stringify(p.position) === JSON.stringify([0, 0]))).toBe(true);
expect(xPieces.some(p => JSON.stringify(p.position) === JSON.stringify([1, 1]))).toBe(true);
expect(xPieces.some(p => JSON.stringify(p.position) === JSON.stringify([2, 2]))).toBe(true);
expect(
xPieces.some(
(p: any) => JSON.stringify(p.position) === JSON.stringify([0, 0]),
),
).toBe(true);
expect(
xPieces.some(
(p: any) => JSON.stringify(p.position) === JSON.stringify([1, 1]),
),
).toBe(true);
expect(
xPieces.some(
(p: any) => JSON.stringify(p.position) === JSON.stringify([2, 2]),
),
).toBe(true);
} catch (e) {
// If setup fails due to cancellation, check state directly
const error = e as Error;
if (!error.message.includes('Cancelled')) {
if (!error.message.includes("Cancelled")) {
throw e;
}
}
host.dispose();
expect(host.status.value).toBe('disposed');
expect(host.status.value).toBe("disposed");
});
});
describe('currentPlayer in prompt', () => {
it('should have currentPlayer in PromptEvent', async () => {
describe("currentPlayer in prompt", () => {
it("should have currentPlayer in PromptEvent", async () => {
const { host } = createTestHost();
const promptPromise = waitForPromptEvent(host);
const runPromise = host.start();
const promptEvent = await promptPromise;
expect(promptEvent.currentPlayer).toBe('X');
expect(host.activePromptPlayer.value).toBe('X');
expect(promptEvent.currentPlayer).toBe("X");
expect(host.activePromptPlayer.value).toBe("X");
promptEvent.cancel('test cleanup');
promptEvent.cancel("test cleanup");
try {
await runPromise;
} catch (e) {
const error = e as Error;
expect(error.message).toBe('test cleanup');
expect(error.message).toBe("test cleanup");
}
});
it('should update activePromptPlayer reactively', async () => {
it("should update activePromptPlayer reactively", async () => {
const { host } = createTestHost();
// Initially null
@ -490,25 +502,30 @@ describe('GameHost', () => {
let promptPromise = waitForPromptEvent(host);
let runPromise = host.start();
let promptEvent = await promptPromise;
expect(promptEvent.currentPlayer).toBe('X');
expect(host.activePromptPlayer.value).toBe('X');
expect(promptEvent.currentPlayer).toBe("X");
expect(host.activePromptPlayer.value).toBe("X");
// Make a move
promptEvent.tryCommit({ name: 'play', params: ['X', 1, 1], options: {}, flags: {} });
promptEvent.tryCommit({
name: "play",
params: ["X", 1, 1],
options: {},
flags: {},
});
// Second prompt - O's turn
promptPromise = waitForPromptEvent(host);
promptEvent = await promptPromise;
expect(promptEvent.currentPlayer).toBe('O');
expect(host.activePromptPlayer.value).toBe('O');
expect(promptEvent.currentPlayer).toBe("O");
expect(host.activePromptPlayer.value).toBe("O");
// Cancel
promptEvent.cancel('test cleanup');
promptEvent.cancel("test cleanup");
try {
await runPromise;
} catch (e) {
const error = e as Error;
expect(error.message).toBe('test cleanup');
expect(error.message).toBe("test cleanup");
}
// After prompt ends, player should be null
@ -516,35 +533,35 @@ describe('GameHost', () => {
});
});
describe('tryAnswerPrompt', () => {
it('should answer prompt with valid arguments', async () => {
describe("tryAnswerPrompt", () => {
it("should answer prompt with valid arguments", async () => {
const { host } = createTestHost();
const promptPromise = waitForPromptEvent(host);
const runPromise = host.start();
const promptEvent = await promptPromise;
expect(promptEvent.schema.name).toBe('play');
expect(promptEvent.schema.name).toBe("play");
// Use tryAnswerPrompt with the prompt def
const { prompts } = await import('@/samples/tic-tac-toe');
const error = host.tryAnswerPrompt(prompts.play, 'X', 1, 1);
const { prompts } = await import("@/samples/tic-tac-toe");
const error = host.tryAnswerPrompt(prompts.play, "X", 1, 1);
expect(error).toBeNull();
// Wait for next prompt and cancel
const nextPromptPromise = waitForPromptEvent(host);
const nextPrompt = await nextPromptPromise;
nextPrompt.cancel('test cleanup');
nextPrompt.cancel("test cleanup");
try {
await runPromise;
} catch (e) {
const error = e as Error;
expect(error.message).toBe('test cleanup');
expect(error.message).toBe("test cleanup");
}
});
it('should reject invalid arguments', async () => {
it("should reject invalid arguments", async () => {
const { host } = createTestHost();
const promptPromise = waitForPromptEvent(host);
@ -553,26 +570,26 @@ describe('GameHost', () => {
const promptEvent = await promptPromise;
// Use tryAnswerPrompt with invalid position
const { prompts } = await import('@/samples/tic-tac-toe');
const error = host.tryAnswerPrompt(prompts.play, 'X', 5, 5);
const { prompts } = await import("@/samples/tic-tac-toe");
const error = host.tryAnswerPrompt(prompts.play, "X", 5, 5);
expect(error).not.toBeNull();
promptEvent.cancel('test cleanup');
promptEvent.cancel("test cleanup");
try {
await runPromise;
} catch (e) {
const error = e as Error;
expect(error.message).toBe('test cleanup');
expect(error.message).toBe("test cleanup");
}
});
});
describe('addInterruption and clearInterruptions', () => {
it('should add interruption promise to state', async () => {
describe("addInterruption and clearInterruptions", () => {
it("should add interruption promise to state", async () => {
const { host } = createTestHost();
let resolveInterruption: () => void;
const interruptionPromise = new Promise<void>(resolve => {
const interruptionPromise = new Promise<void>((resolve) => {
resolveInterruption = resolve;
});
@ -589,7 +606,7 @@ describe('GameHost', () => {
resolveInterruption!();
// Cancel and cleanup
promptEvent.cancel('test cleanup');
promptEvent.cancel("test cleanup");
try {
await runPromise;
} catch {
@ -597,15 +614,15 @@ describe('GameHost', () => {
}
});
it('should clear all pending interruptions', async () => {
it("should clear all pending interruptions", async () => {
const { host } = createTestHost();
let resolveInterruption1: () => void;
let resolveInterruption2: () => void;
const interruptionPromise1 = new Promise<void>(resolve => {
const interruptionPromise1 = new Promise<void>((resolve) => {
resolveInterruption1 = resolve;
});
const interruptionPromise2 = new Promise<void>(resolve => {
const interruptionPromise2 = new Promise<void>((resolve) => {
resolveInterruption2 = resolve;
});
@ -621,7 +638,7 @@ describe('GameHost', () => {
const runPromise = host.start();
const promptEvent = await promptPromise;
promptEvent.cancel('test cleanup');
promptEvent.cancel("test cleanup");
try {
await runPromise;

View File

@ -1,14 +1,24 @@
import { describe, it, expect } from 'vitest';
import { createGameContext, createGameCommandRegistry, createPromptDef, IGameContext, PromptDef } from '@/core/game';
import type { PromptEvent, Command } from '@/utils/command';
import { describe, it, expect } from "vitest";
import {
createGameContext,
createGameCommandRegistry,
createPromptDef,
IGameContext,
PromptDef,
} from "@/core/game";
import type {
PromptEvent,
Command,
CommandRunnerContext,
} from "@/utils/command";
type MyState = {
score: number;
round: number;
};
describe('createGameContext', () => {
it('should create a game context with state', () => {
describe("createGameContext", () => {
it("should create a game context with state", () => {
const registry = createGameCommandRegistry();
const ctx = createGameContext(registry);
@ -16,7 +26,7 @@ describe('createGameContext', () => {
expect(ctx._state.value).toBeDefined();
});
it('should wire commands to the context', () => {
it("should wire commands to the context", () => {
const registry = createGameCommandRegistry();
const ctx = createGameContext(registry);
@ -24,7 +34,7 @@ describe('createGameContext', () => {
expect(ctx._commands.registry).toBe(registry);
});
it('should accept initial state as an object', () => {
it("should accept initial state as an object", () => {
const registry = createGameCommandRegistry<MyState>();
const ctx = createGameContext<MyState>(registry, {
score: 0,
@ -35,7 +45,7 @@ describe('createGameContext', () => {
expect(ctx._state.value.round).toBe(1);
});
it('should accept initial state as a factory function', () => {
it("should accept initial state as a factory function", () => {
const registry = createGameCommandRegistry<MyState>();
const ctx = createGameContext<MyState>(registry, () => ({
score: 10,
@ -46,24 +56,32 @@ describe('createGameContext', () => {
expect(ctx._state.value.round).toBe(3);
});
it('should forward prompt events via listener', async () => {
it("should forward prompt events via listener", async () => {
const registry = createGameCommandRegistry();
const ctx = createGameContext(registry);
registry.register('test <value>', async function (_ctx, value) {
return this.prompt({schema: 'prompt <answer>'}, () => 'ok');
registry.register("test <value>", async function (_ctx, value) {
return _ctx.prompt(
createPromptDef("prompt <answer>"),
(answer) => answer,
);
});
const promptPromise = new Promise<PromptEvent>(resolve => {
ctx._commands.on('prompt', resolve);
const promptPromise = new Promise<PromptEvent>((resolve) => {
ctx._commands.on("prompt", resolve);
});
const runPromise = ctx.run('test hello');
const runPromise = ctx.run("test hello");
const promptEvent = await promptPromise;
expect(promptEvent).not.toBeNull();
expect(promptEvent.schema.name).toBe('prompt');
expect(promptEvent.schema.name).toBe("prompt");
const error = promptEvent.tryCommit({ name: 'prompt', params: ['yes'], options: {}, flags: {} });
const error = promptEvent.tryCommit({
name: "prompt",
params: ["yes"],
options: {},
flags: {},
});
expect(error).toBeNull();
const result = await runPromise;
@ -71,41 +89,41 @@ describe('createGameContext', () => {
});
});
describe('createGameCommand', () => {
it('should run a command with access to game context', async () => {
describe("createGameCommand", () => {
it("should run a command with access to game context", async () => {
const registry = createGameCommandRegistry<{ marker: string }>();
registry.register('set-marker <id>', async function (ctx, id) {
ctx.produce(state => {
registry.register("set-marker <id>", async function (ctx, id) {
ctx.produce((state) => {
state.marker = id;
});
return id;
});
const ctx = createGameContext(registry, { marker: '' });
const ctx = createGameContext(registry, { marker: "" });
const result = await ctx.run('set-marker board');
const result = await ctx.run("set-marker board");
if (!result.success) {
console.error('Error:', result.error);
console.error("Error:", result.error);
}
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toBe('board');
expect(result.result).toBe("board");
}
expect(ctx._state.value.marker).toBe('board');
expect(ctx._state.value.marker).toBe("board");
});
it('should run a typed command with extended context', async () => {
it("should run a typed command with extended context", async () => {
const registry = createGameCommandRegistry<MyState>();
registry.register(
'add-score <amount:number>',
"add-score <amount:number>",
async function (ctx, amount) {
ctx.produce(state => {
ctx.produce((state) => {
state.score += amount;
});
return ctx.value.score;
}
},
);
const ctx = createGameContext<MyState>(registry, () => ({
@ -113,7 +131,7 @@ describe('createGameCommand', () => {
round: 1,
}));
const result = await ctx.run('add-score 5');
const result = await ctx.run("add-score 5");
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toBe(5);
@ -121,67 +139,71 @@ describe('createGameCommand', () => {
expect(ctx._state.value.score).toBe(5);
});
it('should return error for unknown command', async () => {
it("should return error for unknown command", async () => {
const registry = createGameCommandRegistry();
const ctx = createGameContext(registry);
const result = await ctx.run('nonexistent');
const result = await ctx.run("nonexistent");
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toContain('nonexistent');
expect(result.error).toContain("nonexistent");
}
});
});
describe('createPromptDef', () => {
it('should create a PromptDef with string schema', () => {
const promptDef = createPromptDef<[string, number]>('play <player> <score:number>');
describe("createPromptDef", () => {
it("should create a PromptDef with string schema", () => {
const promptDef = createPromptDef<[string, number]>(
"play <player> <score:number>",
);
expect(promptDef).toBeDefined();
expect(promptDef.schema.name).toBe('play');
expect(promptDef.schema.name).toBe("play");
expect(promptDef.schema.params).toHaveLength(2);
expect(promptDef.schema.params[0].name).toBe('player');
expect(promptDef.schema.params[1].name).toBe('score');
expect(promptDef.schema.params[0].name).toBe("player");
expect(promptDef.schema.params[1].name).toBe("score");
});
it('should create a PromptDef with CommandSchema object', () => {
it("should create a PromptDef with CommandSchema object", () => {
const schemaObj = {
name: 'test',
name: "test",
params: [],
options: {},
flags: {}
flags: {},
};
const promptDef = createPromptDef<[]>(schemaObj);
expect(promptDef.schema).toEqual(schemaObj);
});
it('should be usable with game.prompt', async () => {
it("should be usable with game.prompt", async () => {
const registry = createGameCommandRegistry<{ score: number }>();
registry.register('test-prompt', async function(ctx) {
const promptDef = createPromptDef<[number]>('input <value:number>');
const result = await ctx.prompt(
promptDef,
(value) => {
if (value < 0) throw 'Value must be positive';
registry.register("test-prompt", async function (ctx) {
const promptDef = createPromptDef<[number]>("input <value:number>");
const result = await ctx.prompt(promptDef, (value) => {
if (value < 0) throw "Value must be positive";
return value;
}
);
});
return result;
});
const ctx = createGameContext(registry, { score: 0 });
const promptPromise = new Promise<PromptEvent>(resolve => {
ctx._commands.on('prompt', resolve);
const promptPromise = new Promise<PromptEvent>((resolve) => {
ctx._commands.on("prompt", resolve);
});
const runPromise = ctx.run('test-prompt');
const runPromise = ctx.run("test-prompt");
const promptEvent = await promptPromise;
expect(promptEvent.schema.name).toBe('input');
expect(promptEvent.schema.name).toBe("input");
const error = promptEvent.tryCommit({ name: 'input', params: [42], options: {}, flags: {} });
const error = promptEvent.tryCommit({
name: "input",
params: [42],
options: {},
flags: {},
});
expect(error).toBeNull();
const result = await runPromise;

View File

@ -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 as { type: string }).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 as { type: string }).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 as { type: string }).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 as { type: string }).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 as { type: string }).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 as { type: string }).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 as { type: string }).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 as { type: string }).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 as { type: string }).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 as { type: string }).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 as { type: string }).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 as { type: string }).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 as { type: string }).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;

View File

@ -1,6 +1,7 @@
import {createGameContext} from '@/core/game';
import {registry} from '@/samples/regicide/commands';
import {createInitialState} from '@/samples/regicide/state';
import { describe, it, expect } from "vitest";
import { createGameContext } from "@/core/game";
import { registry } from "@/samples/regicide/commands";
import { createInitialState } from "@/samples/regicide/state";
import {
buildEnemyDeck,
buildTavernDeck,
@ -8,50 +9,55 @@ import {
createCard,
createEnemy,
getCardValue,
isEnemyDefeated
} from '@/samples/regicide/utils';
import {Mulberry32RNG} from '@/utils/rng';
import {CARD_VALUES, ENEMY_COUNT, FACE_CARDS, INITIAL_HAND_SIZE} from '@/samples/regicide/constants';
import {PlayerType} from '@/samples/regicide/types';
isEnemyDefeated,
} from "@/samples/regicide/utils";
import { Mulberry32RNG } from "@/utils/rng";
import {
CARD_VALUES,
ENEMY_COUNT,
FACE_CARDS,
INITIAL_HAND_SIZE,
} from "@/samples/regicide/constants";
import { PlayerType } from "@/samples/regicide/types";
describe('Regicide - Utils', () => {
describe('getCardValue', () => {
it('should return correct value for number cards', () => {
expect(getCardValue('A')).toBe(1);
expect(getCardValue('5')).toBe(5);
expect(getCardValue('10')).toBe(10);
describe("Regicide - Utils", () => {
describe("getCardValue", () => {
it("should return correct value for number cards", () => {
expect(getCardValue("A")).toBe(1);
expect(getCardValue("5")).toBe(5);
expect(getCardValue("10")).toBe(10);
});
it('should return correct value for face cards', () => {
expect(getCardValue('J')).toBe(10);
expect(getCardValue('Q')).toBe(15);
expect(getCardValue('K')).toBe(20);
it("should return correct value for face cards", () => {
expect(getCardValue("J")).toBe(10);
expect(getCardValue("Q")).toBe(15);
expect(getCardValue("K")).toBe(20);
});
});
describe('createCard', () => {
it('should create a card with correct properties', () => {
const card = createCard('spades_A', 'spades', 'A');
expect(card.id).toBe('spades_A');
expect(card.suit).toBe('spades');
expect(card.rank).toBe('A');
describe("createCard", () => {
it("should create a card with correct properties", () => {
const card = createCard("spades_A", "spades", "A");
expect(card.id).toBe("spades_A");
expect(card.suit).toBe("spades");
expect(card.rank).toBe("A");
expect(card.value).toBe(1);
});
});
describe('createEnemy', () => {
it('should create an enemy with correct HP', () => {
const enemy = createEnemy('enemy_0', 'J', 'spades');
expect(enemy.rank).toBe('J');
describe("createEnemy", () => {
it("should create an enemy with correct HP", () => {
const enemy = createEnemy("enemy_0", "J", "spades");
expect(enemy.rank).toBe("J");
expect(enemy.value).toBe(10);
expect(enemy.hp).toBe(20);
expect(enemy.maxHp).toBe(20);
});
it('should create enemy with different values for different ranks', () => {
const jEnemy = createEnemy('enemy_0', 'J', 'spades');
const qEnemy = createEnemy('enemy_1', 'Q', 'hearts');
const kEnemy = createEnemy('enemy_2', 'K', 'diamonds');
it("should create enemy with different values for different ranks", () => {
const jEnemy = createEnemy("enemy_0", "J", "spades");
const qEnemy = createEnemy("enemy_1", "Q", "hearts");
const kEnemy = createEnemy("enemy_2", "K", "diamonds");
expect(jEnemy.value).toBe(10);
expect(qEnemy.value).toBe(15);
@ -63,16 +69,30 @@ describe('Regicide - Utils', () => {
});
});
describe('createAllCards', () => {
it('should create 52 cards', () => {
describe("createAllCards", () => {
it("should create 52 cards", () => {
const cards = createAllCards();
expect(Object.keys(cards).length).toBe(52);
});
it('should have all suits and ranks', () => {
it("should have all suits and ranks", () => {
const cards = createAllCards();
const suits = ['spades', 'hearts', 'diamonds', 'clubs'];
const ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
const suits = ["spades", "hearts", "diamonds", "clubs"];
const ranks = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];
for (const suit of suits) {
for (const rank of ranks) {
@ -85,39 +105,39 @@ describe('Regicide - Utils', () => {
});
});
describe('buildEnemyDeck', () => {
it('should create 12 enemies (J/Q/K)', () => {
describe("buildEnemyDeck", () => {
it("should create 12 enemies (J/Q/K)", () => {
const rng = new Mulberry32RNG(12345);
const deck = buildEnemyDeck(rng);
expect(deck.length).toBe(12);
});
it('should have J at top, Q in middle, K at bottom', () => {
it("should have J at top, Q in middle, K at bottom", () => {
const rng = new Mulberry32RNG(12345);
const deck = buildEnemyDeck(rng);
for (let i = 0; i < 4; i++) {
expect(deck[i].rank).toBe('J');
expect(deck[i].rank).toBe("J");
}
for (let i = 4; i < 8; i++) {
expect(deck[i].rank).toBe('Q');
expect(deck[i].rank).toBe("Q");
}
for (let i = 8; i < 12; i++) {
expect(deck[i].rank).toBe('K');
expect(deck[i].rank).toBe("K");
}
});
});
describe('buildTavernDeck', () => {
it('should create 40 cards (A-10)', () => {
describe("buildTavernDeck", () => {
it("should create 40 cards (A-10)", () => {
const rng = new Mulberry32RNG(12345);
const deck = buildTavernDeck(rng);
expect(deck.length).toBe(40);
});
it('should not contain face cards', () => {
it("should not contain face cards", () => {
const rng = new Mulberry32RNG(12345);
const deck = buildTavernDeck(rng);
@ -127,9 +147,9 @@ describe('Regicide - Utils', () => {
});
});
describe('isEnemyDefeated', () => {
it('should return true when enemy HP <= 0', () => {
const enemy = createEnemy('enemy_0', 'J', 'spades');
describe("isEnemyDefeated", () => {
it("should return true when enemy HP <= 0", () => {
const enemy = createEnemy("enemy_0", "J", "spades");
expect(isEnemyDefeated(enemy)).toBe(false);
enemy.hp = 0;
@ -139,13 +159,13 @@ describe('Regicide - Utils', () => {
expect(isEnemyDefeated(enemy)).toBe(true);
});
it('should return false for null enemy', () => {
it("should return false for null enemy", () => {
expect(isEnemyDefeated(null)).toBe(false);
});
});
});
describe('Regicide - Commands', () => {
describe("Regicide - Commands", () => {
function createTestContext() {
const initialState = createInitialState();
return createGameContext(registry, initialState);
@ -157,12 +177,12 @@ describe('Regicide - Commands', () => {
const enemyDeck = buildEnemyDeck(rng);
const tavernDeck = buildTavernDeck(rng);
game.produce(state => {
game.produce((state) => {
state.cards = cards;
state.playerCount = 2;
state.currentPlayerIndex = 0;
state.enemyDeck = [...enemyDeck];
state.currentEnemy = {...enemyDeck[0]};
state.currentEnemy = { ...enemyDeck[0] };
for (const card of tavernDeck) {
state.regions.tavernDeck.childIds.push(card.id);
@ -171,8 +191,8 @@ describe('Regicide - Commands', () => {
for (let i = 0; i < 6; i++) {
const card1 = tavernDeck[i];
const card2 = tavernDeck[i + 6];
card1.regionId = 'hand_player1';
card2.regionId = 'hand_player2';
card1.regionId = "hand_player1";
card2.regionId = "hand_player2";
state.playerHands.player1.push(card1.id);
state.playerHands.player2.push(card2.id);
state.regions.hand_player1.childIds.push(card1.id);
@ -181,8 +201,8 @@ describe('Regicide - Commands', () => {
});
}
describe('play command', () => {
it('should deal damage to current enemy', async () => {
describe("play command", () => {
it("should deal damage to current enemy", async () => {
const game = createTestContext();
setupTestGame(game);
@ -195,17 +215,17 @@ describe('Regicide - Commands', () => {
expect(game.value.currentEnemy!.hp).toBe(enemyHpBefore - card.value);
});
it('should double damage for clubs suit', async () => {
it("should double damage for clubs suit", async () => {
const game = createTestContext();
setupTestGame(game);
game.produce(state => {
state.cards['clubs_5'] = createCard('clubs_5', 'clubs', '5');
state.playerHands.player1.push('clubs_5');
state.regions.hand_player1.childIds.push('clubs_5');
game.produce((state) => {
state.cards["clubs_5"] = createCard("clubs_5", "clubs", "5");
state.playerHands.player1.push("clubs_5");
state.regions.hand_player1.childIds.push("clubs_5");
});
const clubsCardId = 'clubs_5';
const clubsCardId = "clubs_5";
const enemyHpBefore = game.value.currentEnemy!.hp;
const card = game.value.cards[clubsCardId];
@ -215,79 +235,79 @@ describe('Regicide - Commands', () => {
});
});
describe('pass command', () => {
it('should allow player to pass', async () => {
describe("pass command", () => {
it("should allow player to pass", async () => {
const game = createTestContext();
setupTestGame(game);
const result = await game.run('pass player1');
const result = await game.run("pass player1");
expect(result.success).toBe(true);
});
});
describe('check-enemy command', () => {
it('should detect defeated enemy and reveal next', async () => {
describe("check-enemy command", () => {
it("should detect defeated enemy and reveal next", async () => {
const game = createTestContext();
setupTestGame(game);
const firstEnemy = game.value.currentEnemy!;
game.produce(state => {
game.produce((state) => {
state.currentEnemy!.hp = 0;
});
await game.run('check-enemy');
await game.run("check-enemy");
expect(game.value.regions.discardPile.childIds).toContain(firstEnemy.id);
expect(game.value.currentEnemy).not.toBe(firstEnemy);
});
it('should not defeat enemy if HP > 0', async () => {
it("should not defeat enemy if HP > 0", async () => {
const game = createTestContext();
setupTestGame(game);
const currentEnemyId = game.value.currentEnemy!.id;
await game.run('check-enemy');
await game.run("check-enemy");
expect(game.value.currentEnemy!.id).toBe(currentEnemyId);
});
});
describe('next-turn command', () => {
it('should switch to next player', async () => {
describe("next-turn command", () => {
it("should switch to next player", async () => {
const game = createTestContext();
setupTestGame(game);
expect(game.value.currentPlayerIndex).toBe(0);
await game.run('next-turn');
await game.run("next-turn");
expect(game.value.currentPlayerIndex).toBe(1);
});
it('should wrap around to first player', async () => {
it("should wrap around to first player", async () => {
const game = createTestContext();
setupTestGame(game);
game.produce(state => {
game.produce((state) => {
state.currentPlayerIndex = 1;
});
await game.run('next-turn');
await game.run("next-turn");
expect(game.value.currentPlayerIndex).toBe(0);
});
});
});
describe('Regicide - Game Flow', () => {
describe("Regicide - Game Flow", () => {
function createTestContext() {
const initialState = createInitialState();
return createGameContext(registry, initialState);
}
it('should complete a full turn cycle', async () => {
it("should complete a full turn cycle", async () => {
const game = createTestContext();
const cards = createAllCards();
@ -295,12 +315,12 @@ describe('Regicide - Game Flow', () => {
const enemyDeck = buildEnemyDeck(rng);
const tavernDeck = buildTavernDeck(rng);
game.produce(state => {
game.produce((state) => {
state.cards = cards;
state.playerCount = 1;
state.currentPlayerIndex = 0;
state.enemyDeck = [...enemyDeck.slice(1)];
state.currentEnemy = {...enemyDeck[0]};
state.currentEnemy = { ...enemyDeck[0] };
for (const card of tavernDeck) {
state.regions.tavernDeck.childIds.push(card.id);
@ -308,7 +328,7 @@ describe('Regicide - Game Flow', () => {
for (let i = 0; i < 6; i++) {
const card = tavernDeck[i];
card.regionId = 'hand_player1';
card.regionId = "hand_player1";
state.playerHands.player1.push(card.id);
state.regions.hand_player1.childIds.push(card.id);
}
@ -323,14 +343,14 @@ describe('Regicide - Game Flow', () => {
expect(game.value.currentEnemy!.hp).toBeLessThan(enemyHpBefore);
});
it('should win game when all enemies defeated', async () => {
it("should win game when all enemies defeated", async () => {
const game = createTestContext();
const cards = createAllCards();
const rng = new Mulberry32RNG(12345);
const tavernDeck = buildTavernDeck(rng);
game.produce(state => {
game.produce((state) => {
state.cards = cards;
state.playerCount = 1;
state.currentPlayerIndex = 0;
@ -342,12 +362,12 @@ describe('Regicide - Game Flow', () => {
}
});
game.produce(state => {
state.phase = 'victory';
game.produce((state) => {
state.phase = "victory";
state.winner = true;
});
expect(game.value.phase).toBe('victory');
expect(game.value.phase).toBe("victory");
expect(game.value.winner).toBe(true);
});
});

View File

@ -14,7 +14,7 @@ import type {
GridInventory,
InventoryItem,
} from "@/samples/slay-the-spire-like/system/grid-inventory";
import type { GameItemMeta } from "@/samples/slay-the-spire-like/system/encounter/types";
import type { GameItemMeta } from "@/samples/slay-the-spire-like/system/grid-inventory/types";
import { IDENTITY_TRANSFORM } from "@/samples/slay-the-spire-like/system/utils/shape-collision";
import { parseShapeString } from "@/samples/slay-the-spire-like/system/utils/parse-shape";
import type {

View File

@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest';
import { parseShapeString } from '@/samples/slay-the-spire-like/system/utils/parse-shape';
import { IDENTITY_TRANSFORM } from '@/samples/slay-the-spire-like/system/utils/shape-collision';
import { describe, it, expect } from "vitest";
import { parseShapeString } from "@/samples/slay-the-spire-like/system/utils/parse-shape";
import { IDENTITY_TRANSFORM } from "@/samples/slay-the-spire-like/system/utils/shape-collision";
import {
createGridInventory,
placeItem,
@ -14,12 +14,16 @@ import {
validatePlacement,
type GridInventory,
type InventoryItem,
} from '@/samples/slay-the-spire-like/system/grid-inventory';
} from "@/samples/slay-the-spire-like/system/grid-inventory";
/**
* Helper: create a test inventory item.
*/
function createTestItem(id: string, shapeStr: string, transform = IDENTITY_TRANSFORM): InventoryItem {
function createTestItem(
id: string,
shapeStr: string,
transform = IDENTITY_TRANSFORM,
): InventoryItem<Record<string, never>> {
const shape = parseShapeString(shapeStr);
return {
id,
@ -28,9 +32,9 @@ function createTestItem(id: string, shapeStr: string, transform = IDENTITY_TRANS
};
}
describe('grid-inventory', () => {
describe('createGridInventory', () => {
it('should create an empty inventory with correct dimensions', () => {
describe("grid-inventory", () => {
describe("createGridInventory", () => {
it("should create an empty inventory with correct dimensions", () => {
const inv = createGridInventory(6, 4);
expect(inv.width).toBe(6);
expect(inv.height).toBe(4);
@ -39,111 +43,117 @@ describe('grid-inventory', () => {
});
});
describe('placeItem', () => {
it('should place a single-cell item', () => {
describe("placeItem", () => {
it("should place a single-cell item", () => {
const inv = createGridInventory(6, 4);
const item = createTestItem('sword', 'o');
const item = createTestItem("sword", "o");
placeItem(inv, item);
expect(inv.items.size).toBe(1);
expect(inv.items.has('sword')).toBe(true);
expect(inv.occupiedCells.has('0,0')).toBe(true);
expect(inv.items.has("sword")).toBe(true);
expect(inv.occupiedCells.has("0,0")).toBe(true);
});
it('should place a multi-cell item', () => {
it("should place a multi-cell item", () => {
const inv = createGridInventory(6, 4);
const item = createTestItem('axe', 'oee');
const item = createTestItem("axe", "oee");
placeItem(inv, item);
expect(inv.items.size).toBe(1);
expect(inv.occupiedCells.size).toBe(3);
expect(inv.occupiedCells.has('0,0')).toBe(true);
expect(inv.occupiedCells.has('1,0')).toBe(true);
expect(inv.occupiedCells.has('2,0')).toBe(true);
expect(inv.occupiedCells.has("0,0")).toBe(true);
expect(inv.occupiedCells.has("1,0")).toBe(true);
expect(inv.occupiedCells.has("2,0")).toBe(true);
});
it('should place multiple items', () => {
it("should place multiple items", () => {
const inv = createGridInventory(6, 4);
const itemA = createTestItem('a', 'o');
const itemB = createTestItem('b', 'o', { ...IDENTITY_TRANSFORM, offset: { x: 3, y: 0 } });
const itemA = createTestItem("a", "o");
const itemB = createTestItem("b", "o", {
...IDENTITY_TRANSFORM,
offset: { x: 3, y: 0 },
});
placeItem(inv, itemA);
placeItem(inv, itemB);
expect(inv.items.size).toBe(2);
expect(inv.occupiedCells.size).toBe(2);
expect(inv.occupiedCells.has('0,0')).toBe(true);
expect(inv.occupiedCells.has('3,0')).toBe(true);
expect(inv.occupiedCells.has("0,0")).toBe(true);
expect(inv.occupiedCells.has("3,0")).toBe(true);
});
});
describe('removeItem', () => {
it('should remove an item and free its cells', () => {
describe("removeItem", () => {
it("should remove an item and free its cells", () => {
const inv = createGridInventory(6, 4);
const item = createTestItem('sword', 'oee');
const item = createTestItem("sword", "oee");
placeItem(inv, item);
removeItem(inv, 'sword');
removeItem(inv, "sword");
expect(inv.items.size).toBe(0);
expect(inv.occupiedCells.size).toBe(0);
});
it('should only free the removed item\'s cells', () => {
it("should only free the removed item's cells", () => {
const inv = createGridInventory(6, 4);
const itemA = createTestItem('a', 'o');
const itemB = createTestItem('b', 'o', { ...IDENTITY_TRANSFORM, offset: { x: 2, y: 0 } });
const itemA = createTestItem("a", "o");
const itemB = createTestItem("b", "o", {
...IDENTITY_TRANSFORM,
offset: { x: 2, y: 0 },
});
placeItem(inv, itemA);
placeItem(inv, itemB);
removeItem(inv, 'a');
removeItem(inv, "a");
expect(inv.items.size).toBe(1);
expect(inv.occupiedCells.size).toBe(1);
expect(inv.occupiedCells.has('0,0')).toBe(false);
expect(inv.occupiedCells.has('2,0')).toBe(true);
expect(inv.occupiedCells.has("0,0")).toBe(false);
expect(inv.occupiedCells.has("2,0")).toBe(true);
});
it('should do nothing for non-existent item', () => {
it("should do nothing for non-existent item", () => {
const inv = createGridInventory(6, 4);
removeItem(inv, 'nonexistent');
removeItem(inv, "nonexistent");
expect(inv.items.size).toBe(0);
});
});
describe('validatePlacement', () => {
it('should return valid for empty board', () => {
describe("validatePlacement", () => {
it("should return valid for empty board", () => {
const inv = createGridInventory(6, 4);
const shape = parseShapeString('o');
const shape = parseShapeString("o");
const result = validatePlacement(inv, shape, IDENTITY_TRANSFORM);
expect(result).toEqual({ valid: true });
});
it('should return invalid for out of bounds', () => {
it("should return invalid for out of bounds", () => {
const inv = createGridInventory(6, 4);
const shape = parseShapeString('o');
const shape = parseShapeString("o");
const result = validatePlacement(inv, shape, {
...IDENTITY_TRANSFORM,
offset: { x: 6, y: 0 },
});
expect(result).toEqual({ valid: false, reason: '超出边界' });
expect(result).toEqual({ valid: false, reason: "超出边界" });
});
it('should return invalid for collision with existing item', () => {
it("should return invalid for collision with existing item", () => {
const inv = createGridInventory(6, 4);
const existing = createTestItem('a', 'oee');
const existing = createTestItem("a", "oee");
placeItem(inv, existing);
const shape = parseShapeString('o');
const shape = parseShapeString("o");
const result = validatePlacement(inv, shape, IDENTITY_TRANSFORM);
expect(result).toEqual({ valid: false, reason: '与已有物品重叠' });
expect(result).toEqual({ valid: false, reason: "与已有物品重叠" });
});
it('should return valid when there is room nearby', () => {
it("should return valid when there is room nearby", () => {
const inv = createGridInventory(6, 4);
const existing = createTestItem('a', 'o');
const existing = createTestItem("a", "o");
placeItem(inv, existing);
const shape = parseShapeString('o');
const shape = parseShapeString("o");
const result = validatePlacement(inv, shape, {
...IDENTITY_TRANSFORM,
offset: { x: 1, y: 0 },
@ -152,160 +162,169 @@ describe('grid-inventory', () => {
});
});
describe('moveItem', () => {
it('should move item to a new position', () => {
describe("moveItem", () => {
it("should move item to a new position", () => {
const inv = createGridInventory(6, 4);
const item = createTestItem('sword', 'o');
const item = createTestItem("sword", "o");
placeItem(inv, item);
const result = moveItem(inv, 'sword', {
const result = moveItem(inv, "sword", {
...IDENTITY_TRANSFORM,
offset: { x: 5, y: 3 },
});
expect(result).toEqual({ success: true });
expect(inv.occupiedCells.has('0,0')).toBe(false);
expect(inv.occupiedCells.has('5,3')).toBe(true);
expect(inv.occupiedCells.has("0,0")).toBe(false);
expect(inv.occupiedCells.has("5,3")).toBe(true);
expect(item.transform.offset).toEqual({ x: 5, y: 3 });
});
it('should reject move that goes out of bounds', () => {
it("should reject move that goes out of bounds", () => {
const inv = createGridInventory(6, 4);
const item = createTestItem('sword', 'o');
const item = createTestItem("sword", "o");
placeItem(inv, item);
const result = moveItem(inv, 'sword', {
const result = moveItem(inv, "sword", {
...IDENTITY_TRANSFORM,
offset: { x: 6, y: 0 },
});
expect(result).toEqual({ success: false, reason: '超出边界' });
expect(inv.occupiedCells.has('0,0')).toBe(true);
expect(result).toEqual({ success: false, reason: "超出边界" });
expect(inv.occupiedCells.has("0,0")).toBe(true);
expect(item.transform.offset).toEqual({ x: 0, y: 0 });
});
it('should reject move that collides with another item', () => {
it("should reject move that collides with another item", () => {
const inv = createGridInventory(6, 4);
const itemA = createTestItem('a', 'o');
const itemB = createTestItem('b', 'o', { ...IDENTITY_TRANSFORM, offset: { x: 2, y: 0 } });
const itemA = createTestItem("a", "o");
const itemB = createTestItem("b", "o", {
...IDENTITY_TRANSFORM,
offset: { x: 2, y: 0 },
});
placeItem(inv, itemA);
placeItem(inv, itemB);
const result = moveItem(inv, 'b', {
const result = moveItem(inv, "b", {
...IDENTITY_TRANSFORM,
offset: { x: 0, y: 0 },
});
expect(result).toEqual({ success: false, reason: '与已有物品重叠' });
expect(inv.occupiedCells.has('2,0')).toBe(true);
expect(result).toEqual({ success: false, reason: "与已有物品重叠" });
expect(inv.occupiedCells.has("2,0")).toBe(true);
});
it('should return error for non-existent item', () => {
it("should return error for non-existent item", () => {
const inv = createGridInventory(6, 4);
const result = moveItem(inv, 'ghost', IDENTITY_TRANSFORM);
expect(result).toEqual({ success: false, reason: '物品不存在' });
const result = moveItem(inv, "ghost", IDENTITY_TRANSFORM);
expect(result).toEqual({ success: false, reason: "物品不存在" });
});
it('should move multi-cell item correctly', () => {
it("should move multi-cell item correctly", () => {
const inv = createGridInventory(6, 4);
// oes: cells at (0,0), (1,0), (1,1)
const item = createTestItem('axe', 'oes');
const item = createTestItem("axe", "oes");
placeItem(inv, item);
const newTransform = { ...IDENTITY_TRANSFORM, offset: { x: 3, y: 1 } };
moveItem(inv, 'axe', newTransform);
moveItem(inv, "axe", newTransform);
// Old cells should be freed
expect(inv.occupiedCells.has('0,0')).toBe(false);
expect(inv.occupiedCells.has('1,0')).toBe(false);
expect(inv.occupiedCells.has('1,1')).toBe(false);
expect(inv.occupiedCells.has("0,0")).toBe(false);
expect(inv.occupiedCells.has("1,0")).toBe(false);
expect(inv.occupiedCells.has("1,1")).toBe(false);
// New cells: (0,0)+offset(3,1)=(3,1), (1,0)+(3,1)=(4,1), (1,1)+(3,1)=(4,2)
expect(inv.occupiedCells.has('3,1')).toBe(true);
expect(inv.occupiedCells.has('4,1')).toBe(true);
expect(inv.occupiedCells.has('4,2')).toBe(true);
expect(inv.occupiedCells.has("3,1")).toBe(true);
expect(inv.occupiedCells.has("4,1")).toBe(true);
expect(inv.occupiedCells.has("4,2")).toBe(true);
});
});
describe('rotateItem', () => {
it('should rotate item by 90 degrees', () => {
describe("rotateItem", () => {
it("should rotate item by 90 degrees", () => {
const inv = createGridInventory(6, 4);
// Horizontal line: (0,0), (1,0)
const item = createTestItem('bar', 'oe', {
const item = createTestItem("bar", "oe", {
...IDENTITY_TRANSFORM,
offset: { x: 0, y: 1 }, // Place away from edge so rotation stays in bounds
});
placeItem(inv, item);
const result = rotateItem(inv, 'bar', 90);
const result = rotateItem(inv, "bar", 90);
expect(result).toEqual({ success: true });
expect(item.transform.rotation).toBe(90);
});
it('should reject rotation that goes out of bounds', () => {
it("should reject rotation that goes out of bounds", () => {
const inv = createGridInventory(3, 3);
// Item at the edge: place a 2-wide item at x=1
const item = createTestItem('bar', 'oe', {
const item = createTestItem("bar", "oe", {
...IDENTITY_TRANSFORM,
offset: { x: 1, y: 0 },
});
placeItem(inv, item);
// Rotating 90° would make it vertical starting at (1,0), going to (1,-1) -> out of bounds
const result = rotateItem(inv, 'bar', 90);
const result = rotateItem(inv, "bar", 90);
expect(result).toEqual({ success: false, reason: '超出边界' });
expect(result).toEqual({ success: false, reason: "超出边界" });
});
it('should reject rotation that collides', () => {
it("should reject rotation that collides", () => {
const inv = createGridInventory(4, 4);
const itemA = createTestItem('a', 'o');
const itemB = createTestItem('b', 'oe', { ...IDENTITY_TRANSFORM, offset: { x: 2, y: 0 } });
const itemA = createTestItem("a", "o");
const itemB = createTestItem("b", "oe", {
...IDENTITY_TRANSFORM,
offset: { x: 2, y: 0 },
});
placeItem(inv, itemA);
placeItem(inv, itemB);
// Rotating b 90° would place cells at (2,0) and (2,-1) -> (2,-1) is out of bounds
// Let's try a different scenario: rotate b 270° -> (2,0) and (2,1) which is fine
// But rotating to collide with a at (0,0)... need item close to a
const itemC = createTestItem('c', 'os', { ...IDENTITY_TRANSFORM, offset: { x: 1, y: 0 } });
const itemC = createTestItem("c", "os", {
...IDENTITY_TRANSFORM,
offset: { x: 1, y: 0 },
});
placeItem(inv, itemC);
// Rotating c 90° would give cells at (1,0) and (0,0) -> collision with a
const result = rotateItem(inv, 'c', 90);
expect(result).toEqual({ success: false, reason: '与已有物品重叠' });
const result = rotateItem(inv, "c", 90);
expect(result).toEqual({ success: false, reason: "与已有物品重叠" });
});
it('should return error for non-existent item', () => {
it("should return error for non-existent item", () => {
const inv = createGridInventory(6, 4);
const result = rotateItem(inv, 'ghost', 90);
expect(result).toEqual({ success: false, reason: '物品不存在' });
const result = rotateItem(inv, "ghost", 90);
expect(result).toEqual({ success: false, reason: "物品不存在" });
});
});
describe('flipItem', () => {
it('should flip item horizontally', () => {
describe("flipItem", () => {
it("should flip item horizontally", () => {
const inv = createGridInventory(6, 4);
const item = createTestItem('bar', 'oe');
const item = createTestItem("bar", "oe");
placeItem(inv, item);
const result = flipItem(inv, 'bar', 'x');
const result = flipItem(inv, "bar", "x");
expect(result).toEqual({ success: true });
expect(item.transform.flipX).toBe(true);
});
it('should flip item vertically', () => {
it("should flip item vertically", () => {
const inv = createGridInventory(6, 4);
const item = createTestItem('bar', 'os');
const item = createTestItem("bar", "os");
placeItem(inv, item);
const result = flipItem(inv, 'bar', 'y');
const result = flipItem(inv, "bar", "y");
expect(result).toEqual({ success: true });
expect(item.transform.flipY).toBe(true);
});
it('should reject flip that causes collision', () => {
it("should reject flip that causes collision", () => {
// oes local cells: (0,0),(1,0),(1,1). flipY: (0,1),(1,1),(1,0).
// Place flipper at offset (0,2): world cells (0,2),(1,2),(1,3).
// flipY gives local (0,1),(1,1),(1,0) + offset(0,2) = (0,3),(1,3),(1,2) — same cells rearranged.
@ -313,7 +332,10 @@ describe('grid-inventory', () => {
// Use oes at offset (0,0): cells (0,0),(1,0),(1,1). flipY: (0,1),(1,1),(1,0).
// Place blocker at (0,1) — which is NOT occupied by oes initially.
const inv = createGridInventory(4, 4);
const blocker = createTestItem('blocker', 'o', { ...IDENTITY_TRANSFORM, offset: { x: 0, y: 1 } });
const blocker = createTestItem("blocker", "o", {
...IDENTITY_TRANSFORM,
offset: { x: 0, y: 1 },
});
// oes at (0,1): cells (0,1),(1,1),(1,2). This overlaps blocker at (0,1)!
// Let me try: blocker at (1,0), flipper at offset (0,2).
// flipper oes at (0,2): (0,2),(1,2),(1,3). blocker at (1,0) — no overlap.
@ -326,30 +348,33 @@ describe('grid-inventory', () => {
// Place flipper at (0,0): cells (0,0),(1,0),(1,1). Place blocker at (0,1) — but (0,1) is not occupied.
// flipY: (0,1),(1,1),(1,0). (0,1) hits blocker!
const inv2 = createGridInventory(4, 4);
const blocker2 = createTestItem('blocker', 'o', { ...IDENTITY_TRANSFORM, offset: { x: 0, y: 1 } });
const flipper2 = createTestItem('flipper', 'oes'); // at (0,0): (0,0),(1,0),(1,1)
const blocker2 = createTestItem("blocker", "o", {
...IDENTITY_TRANSFORM,
offset: { x: 0, y: 1 },
});
const flipper2 = createTestItem("flipper", "oes"); // at (0,0): (0,0),(1,0),(1,1)
placeItem(inv2, blocker2);
placeItem(inv2, flipper2);
const result = flipItem(inv2, 'flipper', 'y');
expect(result).toEqual({ success: false, reason: '与已有物品重叠' });
const result = flipItem(inv2, "flipper", "y");
expect(result).toEqual({ success: false, reason: "与已有物品重叠" });
});
it('should return error for non-existent item', () => {
it("should return error for non-existent item", () => {
const inv = createGridInventory(6, 4);
const result = flipItem(inv, 'ghost', 'x');
expect(result).toEqual({ success: false, reason: '物品不存在' });
const result = flipItem(inv, "ghost", "x");
expect(result).toEqual({ success: false, reason: "物品不存在" });
});
});
describe('getOccupiedCellSet', () => {
it('should return a copy of occupied cells', () => {
describe("getOccupiedCellSet", () => {
it("should return a copy of occupied cells", () => {
const inv = createGridInventory(6, 4);
const item = createTestItem('a', 'oe');
const item = createTestItem("a", "oe");
placeItem(inv, item);
const cells = getOccupiedCellSet(inv);
expect(cells).toEqual(new Set(['0,0', '1,0']));
expect(cells).toEqual(new Set(["0,0", "1,0"]));
// Mutating the copy should not affect the original
cells.clear();
@ -357,50 +382,68 @@ describe('grid-inventory', () => {
});
});
describe('getItemAtCell', () => {
it('should return item at occupied cell', () => {
describe("getItemAtCell", () => {
it("should return item at occupied cell", () => {
const inv = createGridInventory(6, 4);
const item = createTestItem('sword', 'oee');
const item = createTestItem("sword", "oee");
placeItem(inv, item);
const found = getItemAtCell(inv, 1, 0);
expect(found).toBeDefined();
expect(found!.id).toBe('sword');
expect(found!.id).toBe("sword");
});
it('should return undefined for empty cell', () => {
it("should return undefined for empty cell", () => {
const inv = createGridInventory(6, 4);
const item = createTestItem('sword', 'o');
const item = createTestItem("sword", "o");
placeItem(inv, item);
const found = getItemAtCell(inv, 5, 5);
expect(found).toBeUndefined();
});
it('should return correct item when multiple items exist', () => {
it("should return correct item when multiple items exist", () => {
const inv = createGridInventory(6, 4);
const itemA = createTestItem('a', 'o');
const itemB = createTestItem('b', 'o', { ...IDENTITY_TRANSFORM, offset: { x: 3, y: 2 } });
const itemA = createTestItem("a", "o");
const itemB = createTestItem("b", "o", {
...IDENTITY_TRANSFORM,
offset: { x: 3, y: 2 },
});
placeItem(inv, itemA);
placeItem(inv, itemB);
expect(getItemAtCell(inv, 0, 0)!.id).toBe('a');
expect(getItemAtCell(inv, 3, 2)!.id).toBe('b');
expect(getItemAtCell(inv, 0, 0)!.id).toBe("a");
expect(getItemAtCell(inv, 3, 2)!.id).toBe("b");
});
});
describe('getAdjacentItems', () => {
it('should return orthogonally adjacent items', () => {
describe("getAdjacentItems", () => {
it("should return orthogonally adjacent items", () => {
const inv = createGridInventory(6, 4);
const center = createTestItem('center', 'o', {
const center = createTestItem("center", "o", {
...IDENTITY_TRANSFORM,
offset: { x: 2, y: 2 },
});
const top = createTestItem('top', 'o', { ...IDENTITY_TRANSFORM, offset: { x: 2, y: 1 } });
const left = createTestItem('left', 'o', { ...IDENTITY_TRANSFORM, offset: { x: 1, y: 2 } });
const right = createTestItem('right', 'o', { ...IDENTITY_TRANSFORM, offset: { x: 3, y: 2 } });
const bottom = createTestItem('bottom', 'o', { ...IDENTITY_TRANSFORM, offset: { x: 2, y: 3 } });
const diagonal = createTestItem('diagonal', 'o', { ...IDENTITY_TRANSFORM, offset: { x: 1, y: 1 } });
const top = createTestItem("top", "o", {
...IDENTITY_TRANSFORM,
offset: { x: 2, y: 1 },
});
const left = createTestItem("left", "o", {
...IDENTITY_TRANSFORM,
offset: { x: 1, y: 2 },
});
const right = createTestItem("right", "o", {
...IDENTITY_TRANSFORM,
offset: { x: 3, y: 2 },
});
const bottom = createTestItem("bottom", "o", {
...IDENTITY_TRANSFORM,
offset: { x: 2, y: 3 },
});
const diagonal = createTestItem("diagonal", "o", {
...IDENTITY_TRANSFORM,
offset: { x: 1, y: 1 },
});
placeItem(inv, center);
placeItem(inv, top);
@ -409,77 +452,87 @@ describe('grid-inventory', () => {
placeItem(inv, bottom);
placeItem(inv, diagonal);
const adj = getAdjacentItems(inv, 'center');
const adj = getAdjacentItems(inv, "center");
expect(adj.size).toBe(4);
expect(adj.has('top')).toBe(true);
expect(adj.has('left')).toBe(true);
expect(adj.has('right')).toBe(true);
expect(adj.has('bottom')).toBe(true);
expect(adj.has('diagonal')).toBe(false);
expect(adj.has("top")).toBe(true);
expect(adj.has("left")).toBe(true);
expect(adj.has("right")).toBe(true);
expect(adj.has("bottom")).toBe(true);
expect(adj.has("diagonal")).toBe(false);
});
it('should return empty for item with no neighbors', () => {
it("should return empty for item with no neighbors", () => {
const inv = createGridInventory(6, 4);
const item = createTestItem('alone', 'o');
const item = createTestItem("alone", "o");
placeItem(inv, item);
const adj = getAdjacentItems(inv, 'alone');
const adj = getAdjacentItems(inv, "alone");
expect(adj.size).toBe(0);
});
it('should return empty for non-existent item', () => {
it("should return empty for non-existent item", () => {
const inv = createGridInventory(6, 4);
const adj = getAdjacentItems(inv, 'ghost');
const adj = getAdjacentItems(inv, "ghost");
expect(adj.size).toBe(0);
});
it('should handle multi-cell items with multiple adjacencies', () => {
it("should handle multi-cell items with multiple adjacencies", () => {
const inv = createGridInventory(6, 4);
// Horizontal bar at (0,0)-(1,0)
const bar = createTestItem('bar', 'oe');
const bar = createTestItem("bar", "oe");
// Item above left cell
const topA = createTestItem('topA', 'o', { ...IDENTITY_TRANSFORM, offset: { x: 0, y: -1 } });
const topA = createTestItem("topA", "o", {
...IDENTITY_TRANSFORM,
offset: { x: 0, y: -1 },
});
// Item above right cell
const topB = createTestItem('topB', 'o', { ...IDENTITY_TRANSFORM, offset: { x: 1, y: -1 } });
const topB = createTestItem("topB", "o", {
...IDENTITY_TRANSFORM,
offset: { x: 1, y: -1 },
});
placeItem(inv, bar);
placeItem(inv, topA);
placeItem(inv, topB);
const adj = getAdjacentItems(inv, 'bar');
const adj = getAdjacentItems(inv, "bar");
expect(adj.size).toBe(2);
expect(adj.has('topA')).toBe(true);
expect(adj.has('topB')).toBe(true);
expect(adj.has("topA")).toBe(true);
expect(adj.has("topB")).toBe(true);
});
});
describe('integration: fill a 4x6 backpack', () => {
it('should place items fitting a slay-the-spire-like backpack', () => {
describe("integration: fill a 4x6 backpack", () => {
it("should place items fitting a slay-the-spire-like backpack", () => {
const inv = createGridInventory(4, 6);
// Sword: 1x3 horizontal at (0,0)
const sword = createTestItem('sword', 'oee');
const sword = createTestItem("sword", "oee");
// Shield: 2x2 at (0,1)
const shield = createTestItem('shield', 'oes', {
const shield = createTestItem("shield", "oes", {
...IDENTITY_TRANSFORM,
offset: { x: 0, y: 1 },
});
expect(validatePlacement(inv, sword.shape, sword.transform)).toEqual({ valid: true });
expect(validatePlacement(inv, sword.shape, sword.transform)).toEqual({
valid: true,
});
placeItem(inv, sword);
expect(validatePlacement(inv, shield.shape, shield.transform)).toEqual({ valid: true });
expect(validatePlacement(inv, shield.shape, shield.transform)).toEqual({
valid: true,
});
placeItem(inv, shield);
expect(inv.items.size).toBe(2);
expect(inv.occupiedCells.size).toBe(6); // sword(3) + shield(3)
// Adjacent items should detect each other
const adjSword = getAdjacentItems(inv, 'sword');
expect(adjSword.has('shield')).toBe(true);
const adjSword = getAdjacentItems(inv, "sword");
expect(adjSword.has("shield")).toBe(true);
const adjShield = getAdjacentItems(inv, 'shield');
expect(adjShield.has('sword')).toBe(true);
const adjShield = getAdjacentItems(inv, "shield");
expect(adjShield.has("sword")).toBe(true);
});
});
});

View File

@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { parseCommandSchema } from '@/utils/command/schema-parse';
import { describe, it, expect } from "vitest";
import { parseCommandSchema } from "@/utils/command/schema-parse";
import {
createCommandRegistry,
registerCommand,
@ -10,109 +10,120 @@ import {
createCommandRunnerContext,
type CommandRegistry,
type CommandRunnerContextExport,
} from '@/utils/command/command-registry';
import type { CommandRunner, PromptEvent } from '@/utils/command/command-runner';
} from "@/utils/command/command-registry";
import type {
CommandRunner,
PromptEvent,
} from "@/utils/command/command-runner";
type TestContext = {
counter: number;
log: string[];
};
describe('CommandRegistry', () => {
it('should create an empty registry', () => {
describe("CommandRegistry", () => {
it("should create an empty registry", () => {
const registry = createCommandRegistry<TestContext>();
expect(registry.size).toBe(0);
});
it('should register a command', () => {
it("should register a command", () => {
const registry = createCommandRegistry<TestContext>();
const runner: CommandRunner<TestContext, number> = {
schema: parseCommandSchema('add <a> <b>'),
schema: parseCommandSchema("add <a> <b>"),
run: async function (cmd) {
return Number(cmd.params[0]) + Number(cmd.params[1]);
},
};
registerCommand(registry, runner);
expect(registry.size).toBe(1);
expect(hasCommand(registry, 'add')).toBe(true);
expect(hasCommand(registry, "add")).toBe(true);
});
it('should unregister a command', () => {
it("should unregister a command", () => {
const registry = createCommandRegistry<TestContext>();
const runner: CommandRunner<TestContext> = {
schema: parseCommandSchema('remove'),
schema: parseCommandSchema("remove"),
run: async () => {},
};
registerCommand(registry, runner);
expect(hasCommand(registry, 'remove')).toBe(true);
unregisterCommand(registry, 'remove');
expect(hasCommand(registry, 'remove')).toBe(false);
expect(hasCommand(registry, "remove")).toBe(true);
unregisterCommand(registry, "remove");
expect(hasCommand(registry, "remove")).toBe(false);
});
it('should get a command runner', () => {
it("should get a command runner", () => {
const registry = createCommandRegistry<TestContext>();
const runner: CommandRunner<TestContext> = {
schema: parseCommandSchema('get'),
schema: parseCommandSchema("get"),
run: async () => {},
};
registerCommand(registry, runner);
const retrieved = getCommand(registry, 'get');
const retrieved = getCommand(registry, "get");
expect(retrieved).toBe(runner);
});
it('should return undefined for unknown command', () => {
it("should return undefined for unknown command", () => {
const registry = createCommandRegistry<TestContext>();
const retrieved = getCommand(registry, 'unknown');
const retrieved = getCommand(registry, "unknown");
expect(retrieved).toBeUndefined();
});
});
describe('runCommand', () => {
it('should run a command successfully', async () => {
describe("runCommand", () => {
it("should run a command successfully", async () => {
const registry = createCommandRegistry<TestContext>();
const runner: CommandRunner<TestContext, number> = {
schema: parseCommandSchema('add <a> <b>'),
schema: parseCommandSchema("add <a> <b>"),
run: async function (cmd) {
return Number(cmd.params[0]) + Number(cmd.params[1]);
},
};
registerCommand(registry, runner);
const result = await runCommand(registry, { counter: 0, log: [] }, 'add 1 2');
const result = await runCommand(
registry,
{ counter: 0, log: [] },
"add 1 2",
);
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toBe(3);
}
});
it('should fail for unknown command', async () => {
it("should fail for unknown command", async () => {
const registry = createCommandRegistry<TestContext>();
const result = await runCommand(registry, { counter: 0, log: [] }, 'unknown');
const result = await runCommand(
registry,
{ counter: 0, log: [] },
"unknown",
);
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toContain('Unknown command');
expect(result.error).toContain("Unknown command");
}
});
it('should fail for invalid command params', async () => {
it("should fail for invalid command params", async () => {
const registry = createCommandRegistry<TestContext>();
const runner: CommandRunner<TestContext> = {
schema: parseCommandSchema('add <a> <b>'),
schema: parseCommandSchema("add <a> <b>"),
run: async () => {},
};
registerCommand(registry, runner);
const result = await runCommand(registry, { counter: 0, log: [] }, 'add 1');
const result = await runCommand(registry, { counter: 0, log: [] }, "add 1");
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toContain('参数不足');
expect(result.error).toContain("参数不足");
}
});
it('should access context via this.context', async () => {
it("should access context via this.context", async () => {
const registry = createCommandRegistry<TestContext>();
const runner: CommandRunner<TestContext, number> = {
schema: parseCommandSchema('increment'),
schema: parseCommandSchema("increment"),
run: async function () {
this.context.counter++;
return this.context.counter;
@ -121,30 +132,30 @@ describe('runCommand', () => {
registerCommand(registry, runner);
const ctx = { counter: 0, log: [] };
await runCommand(registry, ctx, 'increment');
await runCommand(registry, ctx, "increment");
expect(ctx.counter).toBe(1);
});
it('should handle async errors', async () => {
it("should handle async errors", async () => {
const registry = createCommandRegistry<TestContext>();
const runner: CommandRunner<TestContext> = {
schema: parseCommandSchema('fail'),
schema: parseCommandSchema("fail"),
run: async () => {
throw new Error('Something went wrong');
throw new Error("Something went wrong");
},
};
registerCommand(registry, runner);
const result = await runCommand(registry, { counter: 0, log: [] }, 'fail');
const result = await runCommand(registry, { counter: 0, log: [] }, "fail");
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toBe('Something went wrong');
expect(result.error).toBe("Something went wrong");
}
});
});
describe('CommandRunnerContext', () => {
it('should create a runner context', () => {
describe("CommandRunnerContext", () => {
it("should create a runner context", () => {
const registry = createCommandRegistry<TestContext>();
const ctx = { counter: 0, log: [] };
const runnerCtx = createCommandRunnerContext(registry, ctx);
@ -152,10 +163,10 @@ describe('CommandRunnerContext', () => {
expect(runnerCtx.context).toBe(ctx);
});
it('should run commands via runner context', async () => {
it("should run commands via runner context", async () => {
const registry = createCommandRegistry<TestContext>();
const runner: CommandRunner<TestContext, string> = {
schema: parseCommandSchema('greet <name>'),
schema: parseCommandSchema("greet <name>"),
run: async function (cmd) {
this.context.log.push(`Hello, ${cmd.params[0]}!`);
return `Hello, ${cmd.params[0]}!`;
@ -165,19 +176,19 @@ describe('CommandRunnerContext', () => {
const ctx = { counter: 0, log: [] };
const runnerCtx = createCommandRunnerContext(registry, ctx);
const result = await runnerCtx.run('greet World');
const result = await runnerCtx.run("greet World");
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toBe('Hello, World!');
expect(result.result).toBe("Hello, World!");
}
expect(ctx.log).toEqual(['Hello, World!']);
expect(ctx.log).toEqual(["Hello, World!"]);
});
it('should allow commands to call other commands via this.run', async () => {
it("should allow commands to call other commands via this.run", async () => {
const registry = createCommandRegistry<TestContext>();
const addRunner: CommandRunner<TestContext, number> = {
schema: parseCommandSchema('add <a> <b>'),
schema: parseCommandSchema("add <a> <b>"),
run: async function (cmd) {
return Number(cmd.params[0]) + Number(cmd.params[1]);
},
@ -186,12 +197,12 @@ describe('CommandRunnerContext', () => {
registerCommand(registry, addRunner);
const multiplyRunner: CommandRunner<TestContext, number> = {
schema: parseCommandSchema('multiply <a> <b>'),
schema: parseCommandSchema("multiply <a> <b>"),
run: async function (cmd) {
const a = Number(cmd.params[0]);
const b = Number(cmd.params[1]);
const addResult = await this.run(`add ${a} ${a}`);
if (!addResult.success) throw new Error('add failed');
if (!addResult.success) throw new Error("add failed");
return (addResult.result as number) * b;
},
};
@ -199,18 +210,18 @@ describe('CommandRunnerContext', () => {
registerCommand(registry, multiplyRunner);
const ctx = { counter: 0, log: [] };
const result = await runCommand(registry, ctx, 'multiply 3 4');
const result = await runCommand(registry, ctx, "multiply 3 4");
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toBe(24);
}
});
it('should allow commands to call other commands via this.runParsed', async () => {
it("should allow commands to call other commands via this.runParsed", async () => {
const registry = createCommandRegistry<TestContext>();
const doubleRunner: CommandRunner<TestContext, number> = {
schema: parseCommandSchema('double <n>'),
schema: parseCommandSchema("double <n>"),
run: async function (cmd) {
return Number(cmd.params[0]) * 2;
},
@ -219,11 +230,16 @@ describe('CommandRunnerContext', () => {
registerCommand(registry, doubleRunner);
const quadrupleRunner: CommandRunner<TestContext, number> = {
schema: parseCommandSchema('quadruple <n>'),
schema: parseCommandSchema("quadruple <n>"),
run: async function (cmd) {
const n = Number(cmd.params[0]);
const doubleResult = await this.runParsed({ name: 'double', params: [String(n)], options: {}, flags: {} });
if (!doubleResult.success) throw new Error('double failed');
const doubleResult = await this.runParsed({
name: "double",
params: [String(n)],
options: {},
flags: {},
});
if (!doubleResult.success) throw new Error("double failed");
return (doubleResult.result as number) * 2;
},
};
@ -231,7 +247,7 @@ describe('CommandRunnerContext', () => {
registerCommand(registry, quadrupleRunner);
const ctx = { counter: 0, log: [] };
const result = await runCommand(registry, ctx, 'quadruple 5');
const result = await runCommand(registry, ctx, "quadruple 5");
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toBe(20);
@ -239,14 +255,17 @@ describe('CommandRunnerContext', () => {
});
});
describe('prompt', () => {
it('should dispatch prompt event with string schema', async () => {
describe("prompt", () => {
it("should dispatch prompt event with string schema", async () => {
const registry = createCommandRegistry<TestContext>();
const chooseRunner: CommandRunner<TestContext, string> = {
schema: parseCommandSchema('choose'),
schema: parseCommandSchema("choose"),
run: async function () {
const result = await this.prompt('select <card>', (cmd) => cmd.params[0] as string);
const result = await this.prompt(
parseCommandSchema("select <card>"),
(cmd) => cmd.params[0] as string,
);
return result;
},
};
@ -257,27 +276,30 @@ describe('prompt', () => {
let promptEvent: PromptEvent | null = null;
const runnerCtx = createCommandRunnerContext(registry, ctx);
runnerCtx.on('prompt', (e) => {
runnerCtx.on("prompt", (e) => {
promptEvent = e;
});
const runPromise = runnerCtx.run('choose');
const runPromise = runnerCtx.run("choose");
await new Promise((r) => setTimeout(r, 0));
expect(promptEvent).not.toBeNull();
expect(promptEvent!.schema.name).toBe('select');
expect(promptEvent!.schema.name).toBe("select");
promptEvent!.cancel('test cleanup');
promptEvent!.cancel("test cleanup");
await runPromise;
});
it('should resolve prompt with valid input', async () => {
it("should resolve prompt with valid input", async () => {
const registry = createCommandRegistry<TestContext>();
const chooseRunner: CommandRunner<TestContext, string> = {
schema: parseCommandSchema('choose'),
schema: parseCommandSchema("choose"),
run: async function () {
const result = await this.prompt('select <card>', (card) => card as string);
const result = await this.prompt(
parseCommandSchema("select <card>"),
(card) => card as string,
);
this.context.log.push(`selected ${result}`);
return result;
},
@ -289,36 +311,39 @@ describe('prompt', () => {
let promptEvent: PromptEvent | null = null;
const runnerCtx = createCommandRunnerContext(registry, ctx);
runnerCtx.on('prompt', (e) => {
runnerCtx.on("prompt", (e) => {
promptEvent = e;
});
const runPromise = runnerCtx.run('choose');
const runPromise = runnerCtx.run("choose");
await new Promise((r) => setTimeout(r, 0));
expect(promptEvent).not.toBeNull();
const parsed = { name: 'select', params: ['Ace'], options: {}, flags: {} };
const parsed = { name: "select", params: ["Ace"], options: {}, flags: {} };
const error = promptEvent!.tryCommit(parsed);
expect(error).toBeNull();
const result = await runPromise;
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toBe('Ace');
expect(result.result).toBe("Ace");
}
expect(ctx.log).toEqual(['selected Ace']);
expect(ctx.log).toEqual(["selected Ace"]);
});
it('should reject prompt with invalid input', async () => {
it("should reject prompt with invalid input", async () => {
const registry = createCommandRegistry<TestContext>();
const chooseRunner: CommandRunner<TestContext, string> = {
schema: parseCommandSchema('choose'),
schema: parseCommandSchema("choose"),
run: async function () {
try {
await this.prompt('select <card>', (card) => card as string);
return 'unexpected success';
await this.prompt(
parseCommandSchema("select <card>"),
(card) => card as string,
);
return "unexpected success";
} catch (e) {
return (e as Error).message;
}
@ -331,30 +356,30 @@ describe('prompt', () => {
let promptEvent: PromptEvent | null = null;
const runnerCtx = createCommandRunnerContext(registry, ctx);
runnerCtx.on('prompt', (e) => {
runnerCtx.on("prompt", (e) => {
promptEvent = e;
});
const runPromise = runnerCtx.run('choose');
const runPromise = runnerCtx.run("choose");
await new Promise((r) => setTimeout(r, 0));
expect(promptEvent).not.toBeNull();
promptEvent!.cancel('user cancelled');
promptEvent!.cancel("user cancelled");
const result = await runPromise;
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toBe('user cancelled');
expect(result.result).toBe("user cancelled");
}
});
it('should accept CommandSchema object in prompt', async () => {
it("should accept CommandSchema object in prompt", async () => {
const registry = createCommandRegistry<TestContext>();
const schema = parseCommandSchema('pick <item>');
const schema = parseCommandSchema("pick <item>");
const pickRunner: CommandRunner<TestContext, string> = {
schema: parseCommandSchema('pick'),
schema: parseCommandSchema("pick"),
run: async function () {
const result = await this.prompt(schema, (item) => item as string);
return result;
@ -367,34 +392,45 @@ describe('prompt', () => {
let promptEvent: PromptEvent | null = null;
const runnerCtx = createCommandRunnerContext(registry, ctx);
runnerCtx.on('prompt', (e) => {
runnerCtx.on("prompt", (e) => {
promptEvent = e;
});
const runPromise = runnerCtx.run('pick');
const runPromise = runnerCtx.run("pick");
await new Promise((r) => setTimeout(r, 0));
expect(promptEvent).not.toBeNull();
expect(promptEvent!.schema.name).toBe('pick');
expect(promptEvent!.schema.name).toBe("pick");
const error = promptEvent!.tryCommit({ name: 'pick', params: ['sword'], options: {}, flags: {} });
const error = promptEvent!.tryCommit({
name: "pick",
params: ["sword"],
options: {},
flags: {},
});
expect(error).toBeNull();
const result = await runPromise;
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toBe('sword');
expect(result.result).toBe("sword");
}
});
it('should allow multiple sequential prompts', async () => {
it("should allow multiple sequential prompts", async () => {
const registry = createCommandRegistry<TestContext>();
const multiPromptRunner: CommandRunner<TestContext, string[]> = {
schema: parseCommandSchema('multi'),
schema: parseCommandSchema("multi"),
run: async function () {
const first = await this.prompt('first <a>', (a) => a as string);
const second = await this.prompt('second <b>', (b) => b as string);
const first = await this.prompt(
parseCommandSchema("first <a>"),
(a) => a as string,
);
const second = await this.prompt(
parseCommandSchema("second <b>"),
(b) => b as string,
);
return [first, second];
},
};
@ -405,48 +441,58 @@ describe('prompt', () => {
const promptEvents: PromptEvent[] = [];
const runnerCtx = createCommandRunnerContext(registry, ctx);
runnerCtx.on('prompt', (e) => {
runnerCtx.on("prompt", (e) => {
promptEvents.push(e);
});
const runPromise = runnerCtx.run('multi');
const runPromise = runnerCtx.run("multi");
await new Promise((r) => setTimeout(r, 0));
expect(promptEvents.length).toBe(1);
expect(promptEvents[0].schema.name).toBe('first');
expect(promptEvents[0].schema.name).toBe("first");
const error1 = promptEvents[0].tryCommit({ name: 'first', params: ['one'], options: {}, flags: {} });
const error1 = promptEvents[0].tryCommit({
name: "first",
params: ["one"],
options: {},
flags: {},
});
expect(error1).toBeNull();
await new Promise((r) => setTimeout(r, 0));
expect(promptEvents.length).toBe(2);
expect(promptEvents[1].schema.name).toBe('second');
expect(promptEvents[1].schema.name).toBe("second");
const error2 = promptEvents[1].tryCommit({ name: 'second', params: ['two'], options: {}, flags: {} });
const error2 = promptEvents[1].tryCommit({
name: "second",
params: ["two"],
options: {},
flags: {},
});
expect(error2).toBeNull();
const result = await runPromise;
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toEqual(['one', 'two']);
expect(result.result).toEqual(["one", "two"]);
}
});
it('should validate input with validator function', async () => {
it("should validate input with validator function", async () => {
const registry = createCommandRegistry<TestContext>();
const chooseRunner: CommandRunner<TestContext, string> = {
schema: parseCommandSchema('choose'),
schema: parseCommandSchema("choose"),
run: async function () {
const result = await this.prompt(
'select <card>',
parseCommandSchema("select <card>"),
(card) => {
const cardStr = card as string;
if (!['Ace', 'King', 'Queen'].includes(cardStr)) {
if (!["Ace", "King", "Queen"].includes(cardStr)) {
throw `Invalid card: ${cardStr}. Must be Ace, King, or Queen.`;
}
return cardStr;
}
},
);
return result;
},
@ -458,39 +504,52 @@ describe('prompt', () => {
let promptEvent: PromptEvent | null = null;
const runnerCtx = createCommandRunnerContext(registry, ctx);
runnerCtx.on('prompt', (e) => {
runnerCtx.on("prompt", (e) => {
promptEvent = e;
});
const runPromise = runnerCtx.run('choose');
const runPromise = runnerCtx.run("choose");
await new Promise((r) => setTimeout(r, 0));
expect(promptEvent).not.toBeNull();
// Try invalid input
const invalidError = promptEvent!.tryCommit({ name: 'select', params: ['Jack'], options: {}, flags: {} });
expect(invalidError).toContain('Invalid card: Jack');
const invalidError = promptEvent!.tryCommit({
name: "select",
params: ["Jack"],
options: {},
flags: {},
});
expect(invalidError).toContain("Invalid card: Jack");
// Try valid input
const validError = promptEvent!.tryCommit({ name: 'select', params: ['Ace'], options: {}, flags: {} });
const validError = promptEvent!.tryCommit({
name: "select",
params: ["Ace"],
options: {},
flags: {},
});
expect(validError).toBeNull();
const result = await runPromise;
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toBe('Ace');
expect(result.result).toBe("Ace");
}
});
it('should allow cancel with custom reason', async () => {
it("should allow cancel with custom reason", async () => {
const registry = createCommandRegistry<TestContext>();
const chooseRunner: CommandRunner<TestContext, string> = {
schema: parseCommandSchema('choose'),
schema: parseCommandSchema("choose"),
run: async function () {
try {
await this.prompt('select <card>', (cmd) => cmd.params[0] as string);
return 'unexpected success';
await this.prompt(
parseCommandSchema("select <card>"),
(cmd) => cmd.params[0] as string,
);
return "unexpected success";
} catch (e) {
return (e as Error).message;
}
@ -503,21 +562,21 @@ describe('prompt', () => {
let promptEvent: PromptEvent | null = null;
const runnerCtx = createCommandRunnerContext(registry, ctx);
runnerCtx.on('prompt', (e) => {
runnerCtx.on("prompt", (e) => {
promptEvent = e;
});
const runPromise = runnerCtx.run('choose');
const runPromise = runnerCtx.run("choose");
await new Promise((r) => setTimeout(r, 0));
expect(promptEvent).not.toBeNull();
promptEvent!.cancel('custom cancellation reason');
promptEvent!.cancel("custom cancellation reason");
const result = await runPromise;
expect(result.success).toBe(true);
if (result.success) {
expect(result.result).toBe('custom cancellation reason');
expect(result.result).toBe("custom cancellation reason");
}
});
});

View File

@ -1,27 +1,30 @@
import { describe, it, expect } from 'vitest';
import { createMiddlewareChain, type MiddlewareChain } from '@/utils/middleware';
import { describe, it, expect } from "vitest";
import {
createMiddlewareChain,
type MiddlewareChain,
} from "@/utils/middleware";
describe('createMiddlewareChain', () => {
describe('basic execution', () => {
it('should return context when no middlewares and no fallback', async () => {
describe("createMiddlewareChain", () => {
describe("basic execution", () => {
it("should return context when no middlewares and no fallback", async () => {
const chain = createMiddlewareChain<{ value: number }>();
const result = await chain.execute({ value: 42 });
expect(result).toEqual({ value: 42 });
});
it('should call fallback when no middlewares', async () => {
it("should call fallback when no middlewares", async () => {
const chain = createMiddlewareChain<{ value: number }, string>(
async ctx => `value is ${ctx.value}`
async (ctx) => `value is ${ctx.value}`,
);
const result = await chain.execute({ value: 42 });
expect(result).toBe('value is 42');
expect(result).toBe("value is 42");
});
it('should pass context to fallback', async () => {
it("should pass context to fallback", async () => {
const chain = createMiddlewareChain<{ a: number; b: number }, number>(
async ctx => ctx.a + ctx.b
async (ctx) => ctx.a + ctx.b,
);
const result = await chain.execute({ a: 3, b: 7 });
@ -29,8 +32,8 @@ describe('createMiddlewareChain', () => {
});
});
describe('single middleware', () => {
it('should execute a single middleware', async () => {
describe("single middleware", () => {
it("should execute a single middleware", async () => {
const chain = createMiddlewareChain<{ count: number }>();
chain.use(async (ctx, next) => {
ctx.count *= 2;
@ -42,9 +45,9 @@ describe('createMiddlewareChain', () => {
expect(result.count).toBe(10);
});
it('should allow middleware to modify return value', async () => {
it("should allow middleware to modify return value", async () => {
const chain = createMiddlewareChain<{ value: number }, number>(
async ctx => ctx.value
async (ctx) => ctx.value,
);
chain.use(async (ctx, next) => {
const result = await next();
@ -56,7 +59,7 @@ describe('createMiddlewareChain', () => {
expect(result).toBe(42);
});
it('should allow middleware to short-circuit without calling next', async () => {
it("should allow middleware to short-circuit without calling next", async () => {
const chain = createMiddlewareChain<{ value: number }>();
chain.use(async (_ctx, _next) => {
return { value: 999 };
@ -68,8 +71,8 @@ describe('createMiddlewareChain', () => {
});
});
describe('multiple middlewares', () => {
it('should execute middlewares in order', async () => {
describe("multiple middlewares", () => {
it("should execute middlewares in order", async () => {
const order: number[] = [];
const chain = createMiddlewareChain<{ value: number }>();
@ -91,7 +94,7 @@ describe('createMiddlewareChain', () => {
expect(order).toEqual([1, 2, 3, 4]);
});
it('should accumulate modifications through the chain', async () => {
it("should accumulate modifications through the chain", async () => {
const chain = createMiddlewareChain<{ value: number }>();
chain.use(async (ctx, next) => {
@ -112,9 +115,9 @@ describe('createMiddlewareChain', () => {
expect(result.value).toBe(5);
});
it('should allow middleware to modify result on the way back', async () => {
it("should allow middleware to modify result on the way back", async () => {
const chain = createMiddlewareChain<{ base: number }, number>(
async ctx => ctx.base
async (ctx) => ctx.base,
);
chain.use(async (_ctx, next) => {
@ -131,7 +134,7 @@ describe('createMiddlewareChain', () => {
expect(result).toBe(20);
});
it('should allow middleware to short-circuit in the middle', async () => {
it("should allow middleware to short-circuit in the middle", async () => {
const executed: number[] = [];
const chain = createMiddlewareChain<{ value: number }>();
@ -155,13 +158,14 @@ describe('createMiddlewareChain', () => {
});
});
describe('nested next calls', () => {
it('should advance index on each next call, skipping remaining middlewares', async () => {
describe("nested next calls", () => {
it("should advance index on each next call, skipping remaining middlewares", async () => {
const chain = createMiddlewareChain<{ counter: number }>();
chain.use(async (_ctx, next) => {
await next();
await next();
return undefined as unknown as { counter: number };
});
const result = await chain.execute({ counter: 0 });
@ -169,12 +173,12 @@ describe('createMiddlewareChain', () => {
expect(result).toBeUndefined();
});
it('should allow middleware to call next conditionally', async () => {
it("should allow middleware to call next conditionally", async () => {
const chain = createMiddlewareChain<{ skip: boolean; value: number }>();
chain.use(async (ctx, next) => {
if (ctx.skip) {
return { value: -1 };
return { skip: ctx.skip, value: -1 };
}
return next();
});
@ -190,33 +194,33 @@ describe('createMiddlewareChain', () => {
expect(resultB.value).toBe(10);
});
it('should handle middleware that awaits next multiple times with a fallback', async () => {
it("should handle middleware that awaits next multiple times with a fallback", async () => {
const log: string[] = [];
const chain = createMiddlewareChain<{ value: number }, string[]>(
async _ctx => log
async (_ctx) => log,
);
chain.use(async (_ctx, next) => {
log.push('before');
log.push("before");
await next();
log.push('after-first');
log.push("after-first");
await next();
log.push('after-second');
log.push("after-second");
return log;
});
chain.use(async (_ctx, next) => {
log.push('mw2');
log.push("mw2");
return next();
});
const result = await chain.execute({ value: 0 });
expect(result).toEqual(['before', 'mw2', 'after-first', 'after-second']);
expect(result).toEqual(["before", "mw2", "after-first", "after-second"]);
});
it('should return fallback result on second next call when no more middlewares remain', async () => {
it("should return fallback result on second next call when no more middlewares remain", async () => {
const chain = createMiddlewareChain<{ value: number }, number>(
async ctx => ctx.value * 10
async (ctx) => ctx.value * 10,
);
chain.use(async (_ctx, next) => {
@ -232,9 +236,9 @@ describe('createMiddlewareChain', () => {
expect(result).toBe(50);
});
it('should return fallback result on second next call when no more middlewares remain', async () => {
it("should return fallback result on second next call when no more middlewares remain", async () => {
const chain = createMiddlewareChain<{ value: number }, number>(
async ctx => ctx.value * 10
async (ctx) => ctx.value * 10,
);
chain.use(async (_ctx, next) => {
@ -251,17 +255,17 @@ describe('createMiddlewareChain', () => {
});
});
describe('async behavior', () => {
it('should handle async middlewares', async () => {
describe("async behavior", () => {
it("should handle async middlewares", async () => {
const chain = createMiddlewareChain<{ value: number }>();
chain.use(async (ctx, next) => {
await new Promise(resolve => setTimeout(resolve, 10));
await new Promise((resolve) => setTimeout(resolve, 10));
ctx.value += 1;
return next();
});
chain.use(async (ctx, next) => {
await new Promise(resolve => setTimeout(resolve, 10));
await new Promise((resolve) => setTimeout(resolve, 10));
ctx.value += 2;
return next();
});
@ -271,12 +275,12 @@ describe('createMiddlewareChain', () => {
expect(result.value).toBe(3);
});
it('should handle async fallback', async () => {
it("should handle async fallback", async () => {
const chain = createMiddlewareChain<{ value: number }, number>(
async ctx => {
await new Promise(resolve => setTimeout(resolve, 10));
async (ctx) => {
await new Promise((resolve) => setTimeout(resolve, 10));
return ctx.value * 10;
}
},
);
const result = await chain.execute({ value: 5 });
@ -285,28 +289,32 @@ describe('createMiddlewareChain', () => {
});
});
describe('error handling', () => {
it('should propagate errors from middleware', async () => {
describe("error handling", () => {
it("should propagate errors from middleware", async () => {
const chain = createMiddlewareChain<{ value: number }>();
chain.use(async () => {
throw new Error('middleware error');
throw new Error("middleware error");
});
await expect(chain.execute({ value: 1 })).rejects.toThrow('middleware error');
await expect(chain.execute({ value: 1 })).rejects.toThrow(
"middleware error",
);
});
it('should propagate errors from fallback', async () => {
it("should propagate errors from fallback", async () => {
const chain = createMiddlewareChain<{ value: number }, number>(
async () => {
throw new Error('fallback error');
}
throw new Error("fallback error");
},
);
await expect(chain.execute({ value: 1 })).rejects.toThrow('fallback error');
await expect(chain.execute({ value: 1 })).rejects.toThrow(
"fallback error",
);
});
it('should allow middleware to catch errors from downstream', async () => {
it("should allow middleware to catch errors from downstream", async () => {
const chain = createMiddlewareChain<{ value: number }>();
chain.use(async (_ctx, next) => {
@ -317,7 +325,7 @@ describe('createMiddlewareChain', () => {
}
});
chain.use(async () => {
throw new Error('downstream error');
throw new Error("downstream error");
});
const result = await chain.execute({ value: 1 });
@ -326,20 +334,20 @@ describe('createMiddlewareChain', () => {
});
});
describe('return type override', () => {
it('should support different TReturn type than TContext', async () => {
describe("return type override", () => {
it("should support different TReturn type than TContext", async () => {
const chain = createMiddlewareChain<{ name: string }, string>(
async ctx => `Hello, ${ctx.name}!`
async (ctx) => `Hello, ${ctx.name}!`,
);
const result = await chain.execute({ name: 'World' });
const result = await chain.execute({ name: "World" });
expect(result).toBe('Hello, World!');
expect(result).toBe("Hello, World!");
});
it('should allow middleware to transform return type', async () => {
it("should allow middleware to transform return type", async () => {
const chain = createMiddlewareChain<{ items: number[] }, number>(
async ctx => ctx.items.reduce((a, b) => a + b, 0)
async (ctx) => ctx.items.reduce((a, b) => a + b, 0),
);
chain.use(async (_ctx, next) => {
@ -353,8 +361,8 @@ describe('createMiddlewareChain', () => {
});
});
describe('reusability', () => {
it('should reset index on each execute call', async () => {
describe("reusability", () => {
it("should reset index on each execute call", async () => {
const chain = createMiddlewareChain<{ count: number }>();
chain.use(async (ctx, next) => {
@ -369,11 +377,11 @@ describe('createMiddlewareChain', () => {
expect(resultB.count).toBe(1);
});
it('should share middlewares across execute calls', async () => {
it("should share middlewares across execute calls", async () => {
const chain = createMiddlewareChain<{ log: string[] }>();
chain.use(async (ctx, next) => {
ctx.log.push('always');
ctx.log.push("always");
return next();
});
@ -384,17 +392,17 @@ describe('createMiddlewareChain', () => {
});
});
describe('edge cases', () => {
it('should handle empty context object', async () => {
describe("edge cases", () => {
it("should handle empty context object", async () => {
const chain = createMiddlewareChain<Record<string, never>>();
const result = await chain.execute({});
expect(result).toEqual({});
});
it('should handle middleware that returns a completely different object', async () => {
it("should handle middleware that returns a completely different object", async () => {
const chain = createMiddlewareChain<{ x: number }, { y: string }>(
async () => ({ y: 'default' })
async () => ({ y: "default" }),
);
chain.use(async (_ctx, next) => {
@ -403,12 +411,12 @@ describe('createMiddlewareChain', () => {
const result = await chain.execute({ x: 42 });
expect(result).toEqual({ y: 'default' });
expect(result).toEqual({ y: "default" });
});
it('should handle middleware that mutates context without returning', async () => {
it("should handle middleware that mutates context without returning", async () => {
const chain = createMiddlewareChain<{ value: number }>(
async ctx => ctx
async (ctx) => ctx,
);
chain.use(async (ctx, next) => {
@ -421,11 +429,12 @@ describe('createMiddlewareChain', () => {
expect(result.value).toBe(100);
});
it('should return undefined when middleware does not call next or return', async () => {
it("should return undefined when middleware does not call next or return", async () => {
const chain = createMiddlewareChain<{ value: number }>();
chain.use(async (ctx) => {
ctx.value = 100;
return undefined as unknown as { value: number };
});
const result = await chain.execute({ value: 0 });