Compare commits
No commits in common. "34cb828f60e4a4d20bd2c93089e1fa99fb70406e" and "52b6cecd647a39ff8b52acb3f7fb708dad693c87" have entirely different histories.
34cb828f60
...
52b6cecd64
|
|
@ -29,12 +29,6 @@ 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;
|
||||
|
|
|
|||
|
|
@ -1,100 +0,0 @@
|
|||
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">);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
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";
|
||||
|
|
|
|||
|
|
@ -1,63 +1,4 @@
|
|||
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 };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,189 +1,132 @@
|
|||
import { Part } from "@/core/part";
|
||||
import { createRegion } from "@/core/region";
|
||||
import {
|
||||
createGameCommandRegistry,
|
||||
createPromptDef,
|
||||
IGameContext,
|
||||
} from "@/core/game";
|
||||
import {Part} from "boardgame-core";
|
||||
import {createRegion} from "boardgame-core";
|
||||
import {createGameCommandRegistry, createPromptDef, IGameContext} from "boardgame-core";
|
||||
|
||||
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 },
|
||||
]),
|
||||
parts: {} as Record<string, TicTacToePart>,
|
||||
currentPlayer: "X" as PlayerType,
|
||||
winner: null as WinnerType,
|
||||
turn: 0,
|
||||
};
|
||||
return {
|
||||
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,
|
||||
winner: null as WinnerType,
|
||||
turn: 0,
|
||||
};
|
||||
}
|
||||
export type TicTacToeState = ReturnType<typeof createInitialState>;
|
||||
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: createPromptDef<[PlayerType, number, number]>(
|
||||
'play <player> <row:number> <col:number>')
|
||||
}
|
||||
|
||||
export async function start(game: TicTacToeGame) {
|
||||
while (true) {
|
||||
const currentPlayer = game.value.currentPlayer;
|
||||
const turnNumber = game.value.turn + 1;
|
||||
const turnOutput = await turn(game, currentPlayer, turnNumber);
|
||||
while (true) {
|
||||
const currentPlayer = game.value.currentPlayer;
|
||||
const turnNumber = game.value.turn + 1;
|
||||
const turnOutput = await turn(game, currentPlayer, turnNumber);
|
||||
|
||||
game.produce((state) => {
|
||||
state.winner = turnOutput.winner;
|
||||
if (!state.winner) {
|
||||
state.currentPlayer = state.currentPlayer === "X" ? "O" : "X";
|
||||
state.turn = turnNumber;
|
||||
}
|
||||
});
|
||||
if (game.value.winner) break;
|
||||
}
|
||||
game.produce(state => {
|
||||
state.winner = turnOutput.winner;
|
||||
if (!state.winner) {
|
||||
state.currentPlayer = state.currentPlayer === 'X' ? 'O' : 'X';
|
||||
state.turn = turnNumber;
|
||||
}
|
||||
});
|
||||
if (game.value.winner) break;
|
||||
}
|
||||
|
||||
return game.value;
|
||||
return game.value;
|
||||
}
|
||||
|
||||
const turn = registry.register({
|
||||
schema: "turn <player> <turnNumber:number>",
|
||||
async run(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) {
|
||||
const { player, row, col } = await game.prompt(
|
||||
prompts.play,
|
||||
(player, row, col) => {
|
||||
if (player !== turnPlayer) {
|
||||
throw `Invalid player: ${player}. Expected ${turnPlayer}.`;
|
||||
} else if (!isValidMove(row, col)) {
|
||||
throw `Invalid position: (${row}, ${col}). Must be between 0 and ${BOARD_SIZE - 1}.`;
|
||||
} else if (isCellOccupied(game, row, col)) {
|
||||
throw `Cell (${row}, ${col}) is already occupied.`;
|
||||
} else {
|
||||
return { player, row, col };
|
||||
}
|
||||
},
|
||||
game.value.currentPlayer,
|
||||
);
|
||||
schema: 'turn <player> <turnNumber:number>',
|
||||
async run(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) {
|
||||
const {player, row, col} = await game.prompt(
|
||||
prompts.play,
|
||||
(player, row, col) => {
|
||||
if (player !== turnPlayer) {
|
||||
throw `Invalid player: ${player}. Expected ${turnPlayer}.`;
|
||||
} else if (!isValidMove(row, col)) {
|
||||
throw `Invalid position: (${row}, ${col}). Must be between 0 and ${BOARD_SIZE - 1}.`;
|
||||
} else if (isCellOccupied(game, row, col)) {
|
||||
throw `Cell (${row}, ${col}) is already occupied.`;
|
||||
} else {
|
||||
return { player, row, col };
|
||||
}
|
||||
},
|
||||
game.value.currentPlayer
|
||||
);
|
||||
|
||||
placePiece(game, row, col, turnPlayer);
|
||||
placePiece(game, row, col, turnPlayer);
|
||||
|
||||
const winner = checkWinner(game);
|
||||
if (winner) return { winner };
|
||||
if (turnNumber >= MAX_TURNS) return { winner: "draw" as WinnerType };
|
||||
const winner = checkWinner(game);
|
||||
if (winner) return { winner };
|
||||
if (turnNumber >= MAX_TURNS) return { winner: 'draw' as WinnerType };
|
||||
|
||||
return { winner: null };
|
||||
},
|
||||
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 {
|
||||
return !!host.value.board.partMap[`${row},${col}`];
|
||||
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)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export function checkWinner(host: TicTacToeGame): WinnerType {
|
||||
const parts = host.value.parts;
|
||||
const partsArray = Object.values(parts);
|
||||
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;
|
||||
return null;
|
||||
}
|
||||
|
||||
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) => {
|
||||
state.parts[piece.id] = piece;
|
||||
board.childIds.push(piece.id);
|
||||
board.partMap[`${row},${col}`] = piece.id;
|
||||
});
|
||||
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 => {
|
||||
state.parts[piece.id] = piece;
|
||||
board.childIds.push(piece.id);
|
||||
board.partMap[`${row},${col}`] = piece.id;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,215 +1,193 @@
|
|||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
createGameContext,
|
||||
createGameCommandRegistry,
|
||||
createPromptDef,
|
||||
IGameContext,
|
||||
PromptDef,
|
||||
} from "@/core/game";
|
||||
import type {
|
||||
PromptEvent,
|
||||
Command,
|
||||
CommandRunnerContext,
|
||||
} from "@/utils/command";
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { createGameContext, createGameCommandRegistry, createPromptDef, IGameContext, PromptDef } from '@/core/game';
|
||||
import type { PromptEvent, Command } from '@/utils/command';
|
||||
|
||||
type MyState = {
|
||||
score: number;
|
||||
round: number;
|
||||
score: number;
|
||||
round: number;
|
||||
};
|
||||
|
||||
describe("createGameContext", () => {
|
||||
it("should create a game context with state", () => {
|
||||
const registry = createGameCommandRegistry();
|
||||
const ctx = createGameContext(registry);
|
||||
describe('createGameContext', () => {
|
||||
it('should create a game context with state', () => {
|
||||
const registry = createGameCommandRegistry();
|
||||
const ctx = createGameContext(registry);
|
||||
|
||||
expect(ctx._state).not.toBeNull();
|
||||
expect(ctx._state.value).toBeDefined();
|
||||
});
|
||||
|
||||
it("should wire commands to the context", () => {
|
||||
const registry = createGameCommandRegistry();
|
||||
const ctx = createGameContext(registry);
|
||||
|
||||
expect(ctx._commands).not.toBeNull();
|
||||
expect(ctx._commands.registry).toBe(registry);
|
||||
});
|
||||
|
||||
it("should accept initial state as an object", () => {
|
||||
const registry = createGameCommandRegistry<MyState>();
|
||||
const ctx = createGameContext<MyState>(registry, {
|
||||
score: 0,
|
||||
round: 1,
|
||||
expect(ctx._state).not.toBeNull();
|
||||
expect(ctx._state.value).toBeDefined();
|
||||
});
|
||||
|
||||
expect(ctx._state.value.score).toBe(0);
|
||||
expect(ctx._state.value.round).toBe(1);
|
||||
});
|
||||
it('should wire commands to the context', () => {
|
||||
const registry = createGameCommandRegistry();
|
||||
const ctx = createGameContext(registry);
|
||||
|
||||
it("should accept initial state as a factory function", () => {
|
||||
const registry = createGameCommandRegistry<MyState>();
|
||||
const ctx = createGameContext<MyState>(registry, () => ({
|
||||
score: 10,
|
||||
round: 3,
|
||||
}));
|
||||
|
||||
expect(ctx._state.value.score).toBe(10);
|
||||
expect(ctx._state.value.round).toBe(3);
|
||||
});
|
||||
|
||||
it("should forward prompt events via listener", async () => {
|
||||
const registry = createGameCommandRegistry();
|
||||
const ctx = createGameContext(registry);
|
||||
|
||||
registry.register("test <value>", async function (_ctx, value) {
|
||||
return _ctx.prompt(
|
||||
createPromptDef("prompt <answer>"),
|
||||
(answer) => answer,
|
||||
);
|
||||
expect(ctx._commands).not.toBeNull();
|
||||
expect(ctx._commands.registry).toBe(registry);
|
||||
});
|
||||
|
||||
const promptPromise = new Promise<PromptEvent>((resolve) => {
|
||||
ctx._commands.on("prompt", resolve);
|
||||
});
|
||||
const runPromise = ctx.run("test hello");
|
||||
|
||||
const promptEvent = await promptPromise;
|
||||
expect(promptEvent).not.toBeNull();
|
||||
expect(promptEvent.schema.name).toBe("prompt");
|
||||
|
||||
const error = promptEvent.tryCommit({
|
||||
name: "prompt",
|
||||
params: ["yes"],
|
||||
options: {},
|
||||
flags: {},
|
||||
});
|
||||
expect(error).toBeNull();
|
||||
|
||||
const result = await runPromise;
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
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) => {
|
||||
state.marker = id;
|
||||
});
|
||||
return id;
|
||||
});
|
||||
|
||||
const ctx = createGameContext(registry, { marker: "" });
|
||||
|
||||
const result = await ctx.run("set-marker board");
|
||||
if (!result.success) {
|
||||
console.error("Error:", result.error);
|
||||
}
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(result.result).toBe("board");
|
||||
}
|
||||
expect(ctx._state.value.marker).toBe("board");
|
||||
});
|
||||
|
||||
it("should run a typed command with extended context", async () => {
|
||||
const registry = createGameCommandRegistry<MyState>();
|
||||
|
||||
registry.register(
|
||||
"add-score <amount:number>",
|
||||
async function (ctx, amount) {
|
||||
ctx.produce((state) => {
|
||||
state.score += amount;
|
||||
it('should accept initial state as an object', () => {
|
||||
const registry = createGameCommandRegistry<MyState>();
|
||||
const ctx = createGameContext<MyState>(registry, {
|
||||
score: 0,
|
||||
round: 1,
|
||||
});
|
||||
return ctx.value.score;
|
||||
},
|
||||
);
|
||||
|
||||
const ctx = createGameContext<MyState>(registry, () => ({
|
||||
score: 0,
|
||||
round: 1,
|
||||
}));
|
||||
expect(ctx._state.value.score).toBe(0);
|
||||
expect(ctx._state.value.round).toBe(1);
|
||||
});
|
||||
|
||||
const result = await ctx.run("add-score 5");
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(result.result).toBe(5);
|
||||
}
|
||||
expect(ctx._state.value.score).toBe(5);
|
||||
});
|
||||
it('should accept initial state as a factory function', () => {
|
||||
const registry = createGameCommandRegistry<MyState>();
|
||||
const ctx = createGameContext<MyState>(registry, () => ({
|
||||
score: 10,
|
||||
round: 3,
|
||||
}));
|
||||
|
||||
it("should return error for unknown command", async () => {
|
||||
const registry = createGameCommandRegistry();
|
||||
const ctx = createGameContext(registry);
|
||||
expect(ctx._state.value.score).toBe(10);
|
||||
expect(ctx._state.value.round).toBe(3);
|
||||
});
|
||||
|
||||
const result = await ctx.run("nonexistent");
|
||||
expect(result.success).toBe(false);
|
||||
if (!result.success) {
|
||||
expect(result.error).toContain("nonexistent");
|
||||
}
|
||||
});
|
||||
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');
|
||||
});
|
||||
|
||||
const promptPromise = new Promise<PromptEvent>(resolve => {
|
||||
ctx._commands.on('prompt', resolve);
|
||||
});
|
||||
const runPromise = ctx.run('test hello');
|
||||
|
||||
const promptEvent = await promptPromise;
|
||||
expect(promptEvent).not.toBeNull();
|
||||
expect(promptEvent.schema.name).toBe('prompt');
|
||||
|
||||
const error = promptEvent.tryCommit({ name: 'prompt', params: ['yes'], options: {}, flags: {} });
|
||||
expect(error).toBeNull();
|
||||
|
||||
const result = await runPromise;
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createPromptDef", () => {
|
||||
it("should create a PromptDef with string schema", () => {
|
||||
const promptDef = createPromptDef<[string, number]>(
|
||||
"play <player> <score:number>",
|
||||
);
|
||||
describe('createGameCommand', () => {
|
||||
it('should run a command with access to game context', async () => {
|
||||
const registry = createGameCommandRegistry<{ marker: string }>();
|
||||
|
||||
expect(promptDef).toBeDefined();
|
||||
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");
|
||||
});
|
||||
registry.register('set-marker <id>', async function (ctx, id) {
|
||||
ctx.produce(state => {
|
||||
state.marker = id;
|
||||
});
|
||||
return id;
|
||||
});
|
||||
|
||||
it("should create a PromptDef with CommandSchema object", () => {
|
||||
const schemaObj = {
|
||||
name: "test",
|
||||
params: [],
|
||||
options: {},
|
||||
flags: {},
|
||||
};
|
||||
const promptDef = createPromptDef<[]>(schemaObj);
|
||||
const ctx = createGameContext(registry, { marker: '' });
|
||||
|
||||
expect(promptDef.schema).toEqual(schemaObj);
|
||||
});
|
||||
|
||||
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";
|
||||
return value;
|
||||
});
|
||||
return result;
|
||||
const result = await ctx.run('set-marker board');
|
||||
if (!result.success) {
|
||||
console.error('Error:', result.error);
|
||||
}
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(result.result).toBe('board');
|
||||
}
|
||||
expect(ctx._state.value.marker).toBe('board');
|
||||
});
|
||||
|
||||
const ctx = createGameContext(registry, { score: 0 });
|
||||
it('should run a typed command with extended context', async () => {
|
||||
const registry = createGameCommandRegistry<MyState>();
|
||||
|
||||
const promptPromise = new Promise<PromptEvent>((resolve) => {
|
||||
ctx._commands.on("prompt", resolve);
|
||||
registry.register(
|
||||
'add-score <amount:number>',
|
||||
async function (ctx, amount) {
|
||||
ctx.produce(state => {
|
||||
state.score += amount;
|
||||
});
|
||||
return ctx.value.score;
|
||||
}
|
||||
);
|
||||
|
||||
const ctx = createGameContext<MyState>(registry, () => ({
|
||||
score: 0,
|
||||
round: 1,
|
||||
}));
|
||||
|
||||
const result = await ctx.run('add-score 5');
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(result.result).toBe(5);
|
||||
}
|
||||
expect(ctx._state.value.score).toBe(5);
|
||||
});
|
||||
const runPromise = ctx.run("test-prompt");
|
||||
|
||||
const promptEvent = await promptPromise;
|
||||
expect(promptEvent.schema.name).toBe("input");
|
||||
it('should return error for unknown command', async () => {
|
||||
const registry = createGameCommandRegistry();
|
||||
const ctx = createGameContext(registry);
|
||||
|
||||
const error = promptEvent.tryCommit({
|
||||
name: "input",
|
||||
params: [42],
|
||||
options: {},
|
||||
flags: {},
|
||||
const result = await ctx.run('nonexistent');
|
||||
expect(result.success).toBe(false);
|
||||
if (!result.success) {
|
||||
expect(result.error).toContain('nonexistent');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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.params).toHaveLength(2);
|
||||
expect(promptDef.schema.params[0].name).toBe('player');
|
||||
expect(promptDef.schema.params[1].name).toBe('score');
|
||||
});
|
||||
|
||||
it('should create a PromptDef with CommandSchema object', () => {
|
||||
const schemaObj = {
|
||||
name: 'test',
|
||||
params: [],
|
||||
options: {},
|
||||
flags: {}
|
||||
};
|
||||
const promptDef = createPromptDef<[]>(schemaObj);
|
||||
|
||||
expect(promptDef.schema).toEqual(schemaObj);
|
||||
});
|
||||
|
||||
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';
|
||||
return value;
|
||||
}
|
||||
);
|
||||
return result;
|
||||
});
|
||||
|
||||
const ctx = createGameContext(registry, { score: 0 });
|
||||
|
||||
const promptPromise = new Promise<PromptEvent>(resolve => {
|
||||
ctx._commands.on('prompt', resolve);
|
||||
});
|
||||
const runPromise = ctx.run('test-prompt');
|
||||
|
||||
const promptEvent = await promptPromise;
|
||||
expect(promptEvent.schema.name).toBe('input');
|
||||
|
||||
const error = promptEvent.tryCommit({ name: 'input', params: [42], options: {}, flags: {} });
|
||||
expect(error).toBeNull();
|
||||
|
||||
const result = await runPromise;
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(result.result).toBe(42);
|
||||
}
|
||||
});
|
||||
expect(error).toBeNull();
|
||||
|
||||
const result = await runPromise;
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(result.result).toBe(42);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,373 +1,353 @@
|
|||
import { describe, it, expect } from "vitest";
|
||||
import { createGameContext } from "@/core/game";
|
||||
import { registry } from "@/samples/regicide/commands";
|
||||
import { createInitialState } from "@/samples/regicide/state";
|
||||
import {createGameContext} from '@/core/game';
|
||||
import {registry} from '@/samples/regicide/commands';
|
||||
import {createInitialState} from '@/samples/regicide/state';
|
||||
import {
|
||||
buildEnemyDeck,
|
||||
buildTavernDeck,
|
||||
createAllCards,
|
||||
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";
|
||||
buildEnemyDeck,
|
||||
buildTavernDeck,
|
||||
createAllCards,
|
||||
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';
|
||||
|
||||
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");
|
||||
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");
|
||||
expect(enemy.value).toBe(10);
|
||||
expect(enemy.hp).toBe(20);
|
||||
expect(enemy.maxHp).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');
|
||||
expect(card.value).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
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");
|
||||
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);
|
||||
});
|
||||
|
||||
expect(jEnemy.value).toBe(10);
|
||||
expect(qEnemy.value).toBe(15);
|
||||
expect(kEnemy.value).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');
|
||||
|
||||
expect(jEnemy.hp).toBe(20);
|
||||
expect(qEnemy.hp).toBe(30);
|
||||
expect(kEnemy.hp).toBe(40);
|
||||
});
|
||||
});
|
||||
expect(jEnemy.value).toBe(10);
|
||||
expect(qEnemy.value).toBe(15);
|
||||
expect(kEnemy.value).toBe(20);
|
||||
|
||||
describe("createAllCards", () => {
|
||||
it("should create 52 cards", () => {
|
||||
const cards = createAllCards();
|
||||
expect(Object.keys(cards).length).toBe(52);
|
||||
expect(jEnemy.hp).toBe(20);
|
||||
expect(qEnemy.hp).toBe(30);
|
||||
expect(kEnemy.hp).toBe(40);
|
||||
});
|
||||
});
|
||||
|
||||
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",
|
||||
];
|
||||
describe('createAllCards', () => {
|
||||
it('should create 52 cards', () => {
|
||||
const cards = createAllCards();
|
||||
expect(Object.keys(cards).length).toBe(52);
|
||||
});
|
||||
|
||||
for (const suit of suits) {
|
||||
for (const rank of ranks) {
|
||||
const id = `${suit}_${rank}`;
|
||||
expect(cards[id]).toBeDefined();
|
||||
expect(cards[id].suit).toBe(suit);
|
||||
expect(cards[id].rank).toBe(rank);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
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'];
|
||||
|
||||
describe("buildEnemyDeck", () => {
|
||||
it("should create 12 enemies (J/Q/K)", () => {
|
||||
const rng = new Mulberry32RNG(12345);
|
||||
const deck = buildEnemyDeck(rng);
|
||||
expect(deck.length).toBe(12);
|
||||
for (const suit of suits) {
|
||||
for (const rank of ranks) {
|
||||
const id = `${suit}_${rank}`;
|
||||
expect(cards[id]).toBeDefined();
|
||||
expect(cards[id].suit).toBe(suit);
|
||||
expect(cards[id].rank).toBe(rank);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("should have J at top, Q in middle, K at bottom", () => {
|
||||
const rng = new Mulberry32RNG(12345);
|
||||
const deck = buildEnemyDeck(rng);
|
||||
describe('buildEnemyDeck', () => {
|
||||
it('should create 12 enemies (J/Q/K)', () => {
|
||||
const rng = new Mulberry32RNG(12345);
|
||||
const deck = buildEnemyDeck(rng);
|
||||
expect(deck.length).toBe(12);
|
||||
});
|
||||
|
||||
for (let i = 0; i < 4; i++) {
|
||||
expect(deck[i].rank).toBe("J");
|
||||
}
|
||||
it('should have J at top, Q in middle, K at bottom', () => {
|
||||
const rng = new Mulberry32RNG(12345);
|
||||
const deck = buildEnemyDeck(rng);
|
||||
|
||||
for (let i = 4; i < 8; i++) {
|
||||
expect(deck[i].rank).toBe("Q");
|
||||
}
|
||||
for (let i = 0; i < 4; i++) {
|
||||
expect(deck[i].rank).toBe('J');
|
||||
}
|
||||
|
||||
for (let i = 8; i < 12; i++) {
|
||||
expect(deck[i].rank).toBe("K");
|
||||
}
|
||||
});
|
||||
});
|
||||
for (let i = 4; i < 8; i++) {
|
||||
expect(deck[i].rank).toBe('Q');
|
||||
}
|
||||
|
||||
describe("buildTavernDeck", () => {
|
||||
it("should create 40 cards (A-10)", () => {
|
||||
const rng = new Mulberry32RNG(12345);
|
||||
const deck = buildTavernDeck(rng);
|
||||
expect(deck.length).toBe(40);
|
||||
for (let i = 8; i < 12; i++) {
|
||||
expect(deck[i].rank).toBe('K');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("should not contain face cards", () => {
|
||||
const rng = new Mulberry32RNG(12345);
|
||||
const deck = buildTavernDeck(rng);
|
||||
describe('buildTavernDeck', () => {
|
||||
it('should create 40 cards (A-10)', () => {
|
||||
const rng = new Mulberry32RNG(12345);
|
||||
const deck = buildTavernDeck(rng);
|
||||
expect(deck.length).toBe(40);
|
||||
});
|
||||
|
||||
for (const card of deck) {
|
||||
expect(FACE_CARDS.includes(card.rank)).toBe(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
it('should not contain face cards', () => {
|
||||
const rng = new Mulberry32RNG(12345);
|
||||
const deck = buildTavernDeck(rng);
|
||||
|
||||
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;
|
||||
expect(isEnemyDefeated(enemy)).toBe(true);
|
||||
|
||||
enemy.hp = -5;
|
||||
expect(isEnemyDefeated(enemy)).toBe(true);
|
||||
for (const card of deck) {
|
||||
expect(FACE_CARDS.includes(card.rank)).toBe(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("should return false for null enemy", () => {
|
||||
expect(isEnemyDefeated(null)).toBe(false);
|
||||
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;
|
||||
expect(isEnemyDefeated(enemy)).toBe(true);
|
||||
|
||||
enemy.hp = -5;
|
||||
expect(isEnemyDefeated(enemy)).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for null enemy', () => {
|
||||
expect(isEnemyDefeated(null)).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Regicide - Commands", () => {
|
||||
function createTestContext() {
|
||||
const initialState = createInitialState();
|
||||
return createGameContext(registry, initialState);
|
||||
}
|
||||
describe('Regicide - Commands', () => {
|
||||
function createTestContext() {
|
||||
const initialState = createInitialState();
|
||||
return createGameContext(registry, initialState);
|
||||
}
|
||||
|
||||
function setupTestGame(game: ReturnType<typeof createTestContext>) {
|
||||
const cards = createAllCards();
|
||||
const rng = new Mulberry32RNG(12345);
|
||||
const enemyDeck = buildEnemyDeck(rng);
|
||||
const tavernDeck = buildTavernDeck(rng);
|
||||
function setupTestGame(game: ReturnType<typeof createTestContext>) {
|
||||
const cards = createAllCards();
|
||||
const rng = new Mulberry32RNG(12345);
|
||||
const enemyDeck = buildEnemyDeck(rng);
|
||||
const tavernDeck = buildTavernDeck(rng);
|
||||
|
||||
game.produce((state) => {
|
||||
state.cards = cards;
|
||||
state.playerCount = 2;
|
||||
state.currentPlayerIndex = 0;
|
||||
state.enemyDeck = [...enemyDeck];
|
||||
state.currentEnemy = { ...enemyDeck[0] };
|
||||
game.produce(state => {
|
||||
state.cards = cards;
|
||||
state.playerCount = 2;
|
||||
state.currentPlayerIndex = 0;
|
||||
state.enemyDeck = [...enemyDeck];
|
||||
state.currentEnemy = {...enemyDeck[0]};
|
||||
|
||||
for (const card of tavernDeck) {
|
||||
state.regions.tavernDeck.childIds.push(card.id);
|
||||
}
|
||||
for (const card of tavernDeck) {
|
||||
state.regions.tavernDeck.childIds.push(card.id);
|
||||
}
|
||||
|
||||
for (let i = 0; i < 6; i++) {
|
||||
const card1 = tavernDeck[i];
|
||||
const card2 = tavernDeck[i + 6];
|
||||
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);
|
||||
state.regions.hand_player2.childIds.push(card2.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
for (let i = 0; i < 6; i++) {
|
||||
const card1 = tavernDeck[i];
|
||||
const card2 = tavernDeck[i + 6];
|
||||
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);
|
||||
state.regions.hand_player2.childIds.push(card2.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
describe("play command", () => {
|
||||
it("should deal damage to current enemy", async () => {
|
||||
const game = createTestContext();
|
||||
setupTestGame(game);
|
||||
describe('play command', () => {
|
||||
it('should deal damage to current enemy', async () => {
|
||||
const game = createTestContext();
|
||||
setupTestGame(game);
|
||||
|
||||
const enemyHpBefore = game.value.currentEnemy!.hp;
|
||||
const cardId = game.value.playerHands.player1[0];
|
||||
const card = game.value.cards[cardId];
|
||||
const enemyHpBefore = game.value.currentEnemy!.hp;
|
||||
const cardId = game.value.playerHands.player1[0];
|
||||
const card = game.value.cards[cardId];
|
||||
|
||||
const result = await game.run(`play player1 ${cardId}`);
|
||||
const result = await game.run(`play player1 ${cardId}`);
|
||||
|
||||
expect(game.value.currentEnemy!.hp).toBe(enemyHpBefore - card.value);
|
||||
expect(game.value.currentEnemy!.hp).toBe(enemyHpBefore - card.value);
|
||||
});
|
||||
|
||||
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');
|
||||
});
|
||||
|
||||
const clubsCardId = 'clubs_5';
|
||||
const enemyHpBefore = game.value.currentEnemy!.hp;
|
||||
const card = game.value.cards[clubsCardId];
|
||||
|
||||
await game.run(`play player1 ${clubsCardId}`);
|
||||
|
||||
expect(game.value.currentEnemy!.hp).toBe(enemyHpBefore - card.value * 2);
|
||||
});
|
||||
});
|
||||
|
||||
it("should double damage for clubs suit", async () => {
|
||||
const game = createTestContext();
|
||||
setupTestGame(game);
|
||||
describe('pass command', () => {
|
||||
it('should allow player to pass', 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");
|
||||
});
|
||||
const result = await game.run('pass player1');
|
||||
|
||||
const clubsCardId = "clubs_5";
|
||||
const enemyHpBefore = game.value.currentEnemy!.hp;
|
||||
const card = game.value.cards[clubsCardId];
|
||||
|
||||
await game.run(`play player1 ${clubsCardId}`);
|
||||
|
||||
expect(game.value.currentEnemy!.hp).toBe(enemyHpBefore - card.value * 2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("pass command", () => {
|
||||
it("should allow player to pass", async () => {
|
||||
const game = createTestContext();
|
||||
setupTestGame(game);
|
||||
|
||||
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 () => {
|
||||
const game = createTestContext();
|
||||
setupTestGame(game);
|
||||
|
||||
const firstEnemy = game.value.currentEnemy!;
|
||||
game.produce((state) => {
|
||||
state.currentEnemy!.hp = 0;
|
||||
});
|
||||
|
||||
await game.run("check-enemy");
|
||||
|
||||
expect(game.value.regions.discardPile.childIds).toContain(firstEnemy.id);
|
||||
expect(game.value.currentEnemy).not.toBe(firstEnemy);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it("should not defeat enemy if HP > 0", async () => {
|
||||
const game = createTestContext();
|
||||
setupTestGame(game);
|
||||
describe('check-enemy command', () => {
|
||||
it('should detect defeated enemy and reveal next', async () => {
|
||||
const game = createTestContext();
|
||||
setupTestGame(game);
|
||||
|
||||
const currentEnemyId = game.value.currentEnemy!.id;
|
||||
const firstEnemy = game.value.currentEnemy!;
|
||||
game.produce(state => {
|
||||
state.currentEnemy!.hp = 0;
|
||||
});
|
||||
|
||||
await game.run("check-enemy");
|
||||
await game.run('check-enemy');
|
||||
|
||||
expect(game.value.currentEnemy!.id).toBe(currentEnemyId);
|
||||
});
|
||||
});
|
||||
expect(game.value.regions.discardPile.childIds).toContain(firstEnemy.id);
|
||||
expect(game.value.currentEnemy).not.toBe(firstEnemy);
|
||||
});
|
||||
|
||||
describe("next-turn command", () => {
|
||||
it("should switch to next player", async () => {
|
||||
const game = createTestContext();
|
||||
setupTestGame(game);
|
||||
it('should not defeat enemy if HP > 0', async () => {
|
||||
const game = createTestContext();
|
||||
setupTestGame(game);
|
||||
|
||||
expect(game.value.currentPlayerIndex).toBe(0);
|
||||
const currentEnemyId = game.value.currentEnemy!.id;
|
||||
|
||||
await game.run("next-turn");
|
||||
await game.run('check-enemy');
|
||||
|
||||
expect(game.value.currentPlayerIndex).toBe(1);
|
||||
expect(game.value.currentEnemy!.id).toBe(currentEnemyId);
|
||||
});
|
||||
});
|
||||
|
||||
it("should wrap around to first player", async () => {
|
||||
const game = createTestContext();
|
||||
setupTestGame(game);
|
||||
describe('next-turn command', () => {
|
||||
it('should switch to next player', async () => {
|
||||
const game = createTestContext();
|
||||
setupTestGame(game);
|
||||
|
||||
game.produce((state) => {
|
||||
state.currentPlayerIndex = 1;
|
||||
});
|
||||
expect(game.value.currentPlayerIndex).toBe(0);
|
||||
|
||||
await game.run("next-turn");
|
||||
await game.run('next-turn');
|
||||
|
||||
expect(game.value.currentPlayerIndex).toBe(0);
|
||||
expect(game.value.currentPlayerIndex).toBe(1);
|
||||
});
|
||||
|
||||
it('should wrap around to first player', async () => {
|
||||
const game = createTestContext();
|
||||
setupTestGame(game);
|
||||
|
||||
game.produce(state => {
|
||||
state.currentPlayerIndex = 1;
|
||||
});
|
||||
|
||||
await game.run('next-turn');
|
||||
|
||||
expect(game.value.currentPlayerIndex).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Regicide - Game Flow", () => {
|
||||
function createTestContext() {
|
||||
const initialState = createInitialState();
|
||||
return createGameContext(registry, initialState);
|
||||
}
|
||||
describe('Regicide - Game Flow', () => {
|
||||
function createTestContext() {
|
||||
const initialState = createInitialState();
|
||||
return createGameContext(registry, initialState);
|
||||
}
|
||||
|
||||
it("should complete a full turn cycle", async () => {
|
||||
const game = createTestContext();
|
||||
it('should complete a full turn cycle', async () => {
|
||||
const game = createTestContext();
|
||||
|
||||
const cards = createAllCards();
|
||||
const rng = new Mulberry32RNG(12345);
|
||||
const enemyDeck = buildEnemyDeck(rng);
|
||||
const tavernDeck = buildTavernDeck(rng);
|
||||
const cards = createAllCards();
|
||||
const rng = new Mulberry32RNG(12345);
|
||||
const enemyDeck = buildEnemyDeck(rng);
|
||||
const tavernDeck = buildTavernDeck(rng);
|
||||
|
||||
game.produce((state) => {
|
||||
state.cards = cards;
|
||||
state.playerCount = 1;
|
||||
state.currentPlayerIndex = 0;
|
||||
state.enemyDeck = [...enemyDeck.slice(1)];
|
||||
state.currentEnemy = { ...enemyDeck[0] };
|
||||
game.produce(state => {
|
||||
state.cards = cards;
|
||||
state.playerCount = 1;
|
||||
state.currentPlayerIndex = 0;
|
||||
state.enemyDeck = [...enemyDeck.slice(1)];
|
||||
state.currentEnemy = {...enemyDeck[0]};
|
||||
|
||||
for (const card of tavernDeck) {
|
||||
state.regions.tavernDeck.childIds.push(card.id);
|
||||
}
|
||||
for (const card of tavernDeck) {
|
||||
state.regions.tavernDeck.childIds.push(card.id);
|
||||
}
|
||||
|
||||
for (let i = 0; i < 6; i++) {
|
||||
const card = tavernDeck[i];
|
||||
card.regionId = "hand_player1";
|
||||
state.playerHands.player1.push(card.id);
|
||||
state.regions.hand_player1.childIds.push(card.id);
|
||||
}
|
||||
for (let i = 0; i < 6; i++) {
|
||||
const card = tavernDeck[i];
|
||||
card.regionId = 'hand_player1';
|
||||
state.playerHands.player1.push(card.id);
|
||||
state.regions.hand_player1.childIds.push(card.id);
|
||||
}
|
||||
});
|
||||
|
||||
const cardId = game.value.playerHands.player1[0];
|
||||
const card = game.value.cards[cardId];
|
||||
const enemyHpBefore = game.value.currentEnemy!.hp;
|
||||
|
||||
await game.run(`play player1 ${cardId}`);
|
||||
|
||||
expect(game.value.currentEnemy!.hp).toBeLessThan(enemyHpBefore);
|
||||
});
|
||||
|
||||
const cardId = game.value.playerHands.player1[0];
|
||||
const card = game.value.cards[cardId];
|
||||
const enemyHpBefore = game.value.currentEnemy!.hp;
|
||||
it('should win game when all enemies defeated', async () => {
|
||||
const game = createTestContext();
|
||||
|
||||
await game.run(`play player1 ${cardId}`);
|
||||
const cards = createAllCards();
|
||||
const rng = new Mulberry32RNG(12345);
|
||||
const tavernDeck = buildTavernDeck(rng);
|
||||
|
||||
expect(game.value.currentEnemy!.hp).toBeLessThan(enemyHpBefore);
|
||||
});
|
||||
game.produce(state => {
|
||||
state.cards = cards;
|
||||
state.playerCount = 1;
|
||||
state.currentPlayerIndex = 0;
|
||||
state.enemyDeck = [];
|
||||
state.currentEnemy = null;
|
||||
|
||||
it("should win game when all enemies defeated", async () => {
|
||||
const game = createTestContext();
|
||||
for (const card of tavernDeck) {
|
||||
state.regions.tavernDeck.childIds.push(card.id);
|
||||
}
|
||||
});
|
||||
|
||||
const cards = createAllCards();
|
||||
const rng = new Mulberry32RNG(12345);
|
||||
const tavernDeck = buildTavernDeck(rng);
|
||||
game.produce(state => {
|
||||
state.phase = 'victory';
|
||||
state.winner = true;
|
||||
});
|
||||
|
||||
game.produce((state) => {
|
||||
state.cards = cards;
|
||||
state.playerCount = 1;
|
||||
state.currentPlayerIndex = 0;
|
||||
state.enemyDeck = [];
|
||||
state.currentEnemy = null;
|
||||
|
||||
for (const card of tavernDeck) {
|
||||
state.regions.tavernDeck.childIds.push(card.id);
|
||||
}
|
||||
expect(game.value.phase).toBe('victory');
|
||||
expect(game.value.winner).toBe(true);
|
||||
});
|
||||
|
||||
game.produce((state) => {
|
||||
state.phase = "victory";
|
||||
state.winner = true;
|
||||
});
|
||||
|
||||
expect(game.value.phase).toBe("victory");
|
||||
expect(game.value.winner).toBe(true);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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/grid-inventory/types";
|
||||
import type { GameItemMeta } from "@/samples/slay-the-spire-like/system/encounter/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 {
|
||||
|
|
|
|||
|
|
@ -1,538 +1,485 @@
|
|||
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,
|
||||
removeItem,
|
||||
moveItem,
|
||||
rotateItem,
|
||||
flipItem,
|
||||
getOccupiedCellSet,
|
||||
getItemAtCell,
|
||||
getAdjacentItems,
|
||||
validatePlacement,
|
||||
type GridInventory,
|
||||
type InventoryItem,
|
||||
} from "@/samples/slay-the-spire-like/system/grid-inventory";
|
||||
createGridInventory,
|
||||
placeItem,
|
||||
removeItem,
|
||||
moveItem,
|
||||
rotateItem,
|
||||
flipItem,
|
||||
getOccupiedCellSet,
|
||||
getItemAtCell,
|
||||
getAdjacentItems,
|
||||
validatePlacement,
|
||||
type GridInventory,
|
||||
type InventoryItem,
|
||||
} 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<Record<string, never>> {
|
||||
const shape = parseShapeString(shapeStr);
|
||||
return {
|
||||
id,
|
||||
shape,
|
||||
transform: { ...transform },
|
||||
};
|
||||
function createTestItem(id: string, shapeStr: string, transform = IDENTITY_TRANSFORM): InventoryItem {
|
||||
const shape = parseShapeString(shapeStr);
|
||||
return {
|
||||
id,
|
||||
shape,
|
||||
transform: { ...transform },
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
expect(inv.items.size).toBe(0);
|
||||
expect(inv.occupiedCells.size).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("placeItem", () => {
|
||||
it("should place a single-cell item", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
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);
|
||||
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);
|
||||
expect(inv.items.size).toBe(0);
|
||||
expect(inv.occupiedCells.size).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
it("should place a multi-cell item", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem("axe", "oee");
|
||||
placeItem(inv, item);
|
||||
describe('placeItem', () => {
|
||||
it('should place a single-cell item', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem('sword', 'o');
|
||||
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.items.size).toBe(1);
|
||||
expect(inv.items.has('sword')).toBe(true);
|
||||
expect(inv.occupiedCells.has('0,0')).toBe(true);
|
||||
});
|
||||
|
||||
it('should place a multi-cell item', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
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);
|
||||
});
|
||||
|
||||
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 } });
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
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 },
|
||||
});
|
||||
placeItem(inv, itemA);
|
||||
placeItem(inv, itemB);
|
||||
describe('removeItem', () => {
|
||||
it('should remove an item and free its cells', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem('sword', 'oee');
|
||||
placeItem(inv, item);
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
removeItem(inv, 'sword');
|
||||
|
||||
describe("removeItem", () => {
|
||||
it("should remove an item and free its cells", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem("sword", "oee");
|
||||
placeItem(inv, item);
|
||||
expect(inv.items.size).toBe(0);
|
||||
expect(inv.occupiedCells.size).toBe(0);
|
||||
});
|
||||
|
||||
removeItem(inv, "sword");
|
||||
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 } });
|
||||
placeItem(inv, itemA);
|
||||
placeItem(inv, itemB);
|
||||
|
||||
expect(inv.items.size).toBe(0);
|
||||
expect(inv.occupiedCells.size).toBe(0);
|
||||
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);
|
||||
});
|
||||
|
||||
it('should do nothing for non-existent item', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
removeItem(inv, 'nonexistent');
|
||||
expect(inv.items.size).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
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 },
|
||||
});
|
||||
placeItem(inv, itemA);
|
||||
placeItem(inv, itemB);
|
||||
describe('validatePlacement', () => {
|
||||
it('should return valid for empty board', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const shape = parseShapeString('o');
|
||||
const result = validatePlacement(inv, shape, IDENTITY_TRANSFORM);
|
||||
expect(result).toEqual({ valid: true });
|
||||
});
|
||||
|
||||
removeItem(inv, "a");
|
||||
it('should return invalid for out of bounds', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const shape = parseShapeString('o');
|
||||
const result = validatePlacement(inv, shape, {
|
||||
...IDENTITY_TRANSFORM,
|
||||
offset: { x: 6, y: 0 },
|
||||
});
|
||||
expect(result).toEqual({ valid: false, reason: '超出边界' });
|
||||
});
|
||||
|
||||
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);
|
||||
it('should return invalid for collision with existing item', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const existing = createTestItem('a', 'oee');
|
||||
placeItem(inv, existing);
|
||||
|
||||
const shape = parseShapeString('o');
|
||||
const result = validatePlacement(inv, shape, IDENTITY_TRANSFORM);
|
||||
expect(result).toEqual({ valid: false, reason: '与已有物品重叠' });
|
||||
});
|
||||
|
||||
it('should return valid when there is room nearby', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const existing = createTestItem('a', 'o');
|
||||
placeItem(inv, existing);
|
||||
|
||||
const shape = parseShapeString('o');
|
||||
const result = validatePlacement(inv, shape, {
|
||||
...IDENTITY_TRANSFORM,
|
||||
offset: { x: 1, y: 0 },
|
||||
});
|
||||
expect(result).toEqual({ valid: true });
|
||||
});
|
||||
});
|
||||
|
||||
it("should do nothing for non-existent item", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
removeItem(inv, "nonexistent");
|
||||
expect(inv.items.size).toBe(0);
|
||||
});
|
||||
});
|
||||
describe('moveItem', () => {
|
||||
it('should move item to a new position', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem('sword', 'o');
|
||||
placeItem(inv, item);
|
||||
|
||||
describe("validatePlacement", () => {
|
||||
it("should return valid for empty board", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const shape = parseShapeString("o");
|
||||
const result = validatePlacement(inv, shape, IDENTITY_TRANSFORM);
|
||||
expect(result).toEqual({ valid: true });
|
||||
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(item.transform.offset).toEqual({ x: 5, y: 3 });
|
||||
});
|
||||
|
||||
it('should reject move that goes out of bounds', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem('sword', 'o');
|
||||
placeItem(inv, item);
|
||||
|
||||
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(item.transform.offset).toEqual({ x: 0, y: 0 });
|
||||
});
|
||||
|
||||
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 } });
|
||||
placeItem(inv, itemA);
|
||||
placeItem(inv, itemB);
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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: '物品不存在' });
|
||||
});
|
||||
|
||||
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');
|
||||
placeItem(inv, item);
|
||||
|
||||
const newTransform = { ...IDENTITY_TRANSFORM, offset: { x: 3, y: 1 } };
|
||||
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);
|
||||
// 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);
|
||||
});
|
||||
});
|
||||
|
||||
it("should return invalid for out of bounds", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const shape = parseShapeString("o");
|
||||
const result = validatePlacement(inv, shape, {
|
||||
...IDENTITY_TRANSFORM,
|
||||
offset: { x: 6, y: 0 },
|
||||
});
|
||||
expect(result).toEqual({ valid: false, reason: "超出边界" });
|
||||
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', {
|
||||
...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);
|
||||
|
||||
expect(result).toEqual({ success: true });
|
||||
expect(item.transform.rotation).toBe(90);
|
||||
});
|
||||
|
||||
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', {
|
||||
...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);
|
||||
|
||||
expect(result).toEqual({ success: false, reason: '超出边界' });
|
||||
});
|
||||
|
||||
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 } });
|
||||
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 } });
|
||||
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: '与已有物品重叠' });
|
||||
});
|
||||
|
||||
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: '物品不存在' });
|
||||
});
|
||||
});
|
||||
|
||||
it("should return invalid for collision with existing item", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const existing = createTestItem("a", "oee");
|
||||
placeItem(inv, existing);
|
||||
describe('flipItem', () => {
|
||||
it('should flip item horizontally', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem('bar', 'oe');
|
||||
placeItem(inv, item);
|
||||
|
||||
const shape = parseShapeString("o");
|
||||
const result = validatePlacement(inv, shape, IDENTITY_TRANSFORM);
|
||||
expect(result).toEqual({ valid: false, reason: "与已有物品重叠" });
|
||||
const result = flipItem(inv, 'bar', 'x');
|
||||
|
||||
expect(result).toEqual({ success: true });
|
||||
expect(item.transform.flipX).toBe(true);
|
||||
});
|
||||
|
||||
it('should flip item vertically', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem('bar', 'os');
|
||||
placeItem(inv, item);
|
||||
|
||||
const result = flipItem(inv, 'bar', 'y');
|
||||
|
||||
expect(result).toEqual({ success: true });
|
||||
expect(item.transform.flipY).toBe(true);
|
||||
});
|
||||
|
||||
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.
|
||||
// Need asymmetric shape where flip changes world position.
|
||||
// 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 } });
|
||||
// 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.
|
||||
// flipY: local (0,1),(1,1),(1,0) + offset(0,2) = (0,3),(1,3),(1,2). No collision with (1,0).
|
||||
//
|
||||
// Simpler: oe shape (width=2, height=1). flipY with height=1 is identity. Use os (width=1, height=2).
|
||||
// os: (0,0),(0,1). flipY: (0,1),(0,0) — same cells.
|
||||
// Need width>1 and height>1 asymmetric shape: oes
|
||||
//
|
||||
// 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)
|
||||
placeItem(inv2, blocker2);
|
||||
placeItem(inv2, flipper2);
|
||||
|
||||
const result = flipItem(inv2, 'flipper', 'y');
|
||||
expect(result).toEqual({ success: false, reason: '与已有物品重叠' });
|
||||
});
|
||||
|
||||
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: '物品不存在' });
|
||||
});
|
||||
});
|
||||
|
||||
it("should return valid when there is room nearby", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const existing = createTestItem("a", "o");
|
||||
placeItem(inv, existing);
|
||||
describe('getOccupiedCellSet', () => {
|
||||
it('should return a copy of occupied cells', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem('a', 'oe');
|
||||
placeItem(inv, item);
|
||||
|
||||
const shape = parseShapeString("o");
|
||||
const result = validatePlacement(inv, shape, {
|
||||
...IDENTITY_TRANSFORM,
|
||||
offset: { x: 1, y: 0 },
|
||||
});
|
||||
expect(result).toEqual({ valid: true });
|
||||
});
|
||||
});
|
||||
const cells = getOccupiedCellSet(inv);
|
||||
expect(cells).toEqual(new Set(['0,0', '1,0']));
|
||||
|
||||
describe("moveItem", () => {
|
||||
it("should move item to a new position", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem("sword", "o");
|
||||
placeItem(inv, item);
|
||||
|
||||
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(item.transform.offset).toEqual({ x: 5, y: 3 });
|
||||
// Mutating the copy should not affect the original
|
||||
cells.clear();
|
||||
expect(inv.occupiedCells.size).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
it("should reject move that goes out of bounds", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem("sword", "o");
|
||||
placeItem(inv, item);
|
||||
describe('getItemAtCell', () => {
|
||||
it('should return item at occupied cell', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem('sword', 'oee');
|
||||
placeItem(inv, item);
|
||||
|
||||
const result = moveItem(inv, "sword", {
|
||||
...IDENTITY_TRANSFORM,
|
||||
offset: { x: 6, y: 0 },
|
||||
});
|
||||
const found = getItemAtCell(inv, 1, 0);
|
||||
expect(found).toBeDefined();
|
||||
expect(found!.id).toBe('sword');
|
||||
});
|
||||
|
||||
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 return undefined for empty cell', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
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', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
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');
|
||||
});
|
||||
});
|
||||
|
||||
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 },
|
||||
});
|
||||
placeItem(inv, itemA);
|
||||
placeItem(inv, itemB);
|
||||
describe('getAdjacentItems', () => {
|
||||
it('should return orthogonally adjacent items', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
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 result = moveItem(inv, "b", {
|
||||
...IDENTITY_TRANSFORM,
|
||||
offset: { x: 0, y: 0 },
|
||||
});
|
||||
placeItem(inv, center);
|
||||
placeItem(inv, top);
|
||||
placeItem(inv, left);
|
||||
placeItem(inv, right);
|
||||
placeItem(inv, bottom);
|
||||
placeItem(inv, diagonal);
|
||||
|
||||
expect(result).toEqual({ success: false, reason: "与已有物品重叠" });
|
||||
expect(inv.occupiedCells.has("2,0")).toBe(true);
|
||||
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);
|
||||
});
|
||||
|
||||
it('should return empty for item with no neighbors', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem('alone', 'o');
|
||||
placeItem(inv, item);
|
||||
|
||||
const adj = getAdjacentItems(inv, 'alone');
|
||||
expect(adj.size).toBe(0);
|
||||
});
|
||||
|
||||
it('should return empty for non-existent item', () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const adj = getAdjacentItems(inv, 'ghost');
|
||||
expect(adj.size).toBe(0);
|
||||
});
|
||||
|
||||
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');
|
||||
// Item above left cell
|
||||
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 } });
|
||||
|
||||
placeItem(inv, bar);
|
||||
placeItem(inv, topA);
|
||||
placeItem(inv, topB);
|
||||
|
||||
const adj = getAdjacentItems(inv, 'bar');
|
||||
expect(adj.size).toBe(2);
|
||||
expect(adj.has('topA')).toBe(true);
|
||||
expect(adj.has('topB')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
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: "物品不存在" });
|
||||
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');
|
||||
// Shield: 2x2 at (0,1)
|
||||
const shield = createTestItem('shield', 'oes', {
|
||||
...IDENTITY_TRANSFORM,
|
||||
offset: { x: 0, y: 1 },
|
||||
});
|
||||
|
||||
expect(validatePlacement(inv, sword.shape, sword.transform)).toEqual({ valid: true });
|
||||
placeItem(inv, sword);
|
||||
|
||||
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 adjShield = getAdjacentItems(inv, 'shield');
|
||||
expect(adjShield.has('sword')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
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");
|
||||
placeItem(inv, item);
|
||||
|
||||
const newTransform = { ...IDENTITY_TRANSFORM, offset: { x: 3, y: 1 } };
|
||||
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);
|
||||
// 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);
|
||||
});
|
||||
});
|
||||
|
||||
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", {
|
||||
...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);
|
||||
|
||||
expect(result).toEqual({ success: true });
|
||||
expect(item.transform.rotation).toBe(90);
|
||||
});
|
||||
|
||||
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", {
|
||||
...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);
|
||||
|
||||
expect(result).toEqual({ success: false, reason: "超出边界" });
|
||||
});
|
||||
|
||||
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 },
|
||||
});
|
||||
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 },
|
||||
});
|
||||
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: "与已有物品重叠" });
|
||||
});
|
||||
|
||||
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: "物品不存在" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("flipItem", () => {
|
||||
it("should flip item horizontally", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem("bar", "oe");
|
||||
placeItem(inv, item);
|
||||
|
||||
const result = flipItem(inv, "bar", "x");
|
||||
|
||||
expect(result).toEqual({ success: true });
|
||||
expect(item.transform.flipX).toBe(true);
|
||||
});
|
||||
|
||||
it("should flip item vertically", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem("bar", "os");
|
||||
placeItem(inv, item);
|
||||
|
||||
const result = flipItem(inv, "bar", "y");
|
||||
|
||||
expect(result).toEqual({ success: true });
|
||||
expect(item.transform.flipY).toBe(true);
|
||||
});
|
||||
|
||||
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.
|
||||
// Need asymmetric shape where flip changes world position.
|
||||
// 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 },
|
||||
});
|
||||
// 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.
|
||||
// flipY: local (0,1),(1,1),(1,0) + offset(0,2) = (0,3),(1,3),(1,2). No collision with (1,0).
|
||||
//
|
||||
// Simpler: oe shape (width=2, height=1). flipY with height=1 is identity. Use os (width=1, height=2).
|
||||
// os: (0,0),(0,1). flipY: (0,1),(0,0) — same cells.
|
||||
// Need width>1 and height>1 asymmetric shape: oes
|
||||
//
|
||||
// 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)
|
||||
placeItem(inv2, blocker2);
|
||||
placeItem(inv2, flipper2);
|
||||
|
||||
const result = flipItem(inv2, "flipper", "y");
|
||||
expect(result).toEqual({ success: false, reason: "与已有物品重叠" });
|
||||
});
|
||||
|
||||
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: "物品不存在" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("getOccupiedCellSet", () => {
|
||||
it("should return a copy of occupied cells", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem("a", "oe");
|
||||
placeItem(inv, item);
|
||||
|
||||
const cells = getOccupiedCellSet(inv);
|
||||
expect(cells).toEqual(new Set(["0,0", "1,0"]));
|
||||
|
||||
// Mutating the copy should not affect the original
|
||||
cells.clear();
|
||||
expect(inv.occupiedCells.size).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getItemAtCell", () => {
|
||||
it("should return item at occupied cell", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem("sword", "oee");
|
||||
placeItem(inv, item);
|
||||
|
||||
const found = getItemAtCell(inv, 1, 0);
|
||||
expect(found).toBeDefined();
|
||||
expect(found!.id).toBe("sword");
|
||||
});
|
||||
|
||||
it("should return undefined for empty cell", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
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", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
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");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAdjacentItems", () => {
|
||||
it("should return orthogonally adjacent items", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
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 },
|
||||
});
|
||||
|
||||
placeItem(inv, center);
|
||||
placeItem(inv, top);
|
||||
placeItem(inv, left);
|
||||
placeItem(inv, right);
|
||||
placeItem(inv, bottom);
|
||||
placeItem(inv, diagonal);
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
it("should return empty for item with no neighbors", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const item = createTestItem("alone", "o");
|
||||
placeItem(inv, item);
|
||||
|
||||
const adj = getAdjacentItems(inv, "alone");
|
||||
expect(adj.size).toBe(0);
|
||||
});
|
||||
|
||||
it("should return empty for non-existent item", () => {
|
||||
const inv = createGridInventory(6, 4);
|
||||
const adj = getAdjacentItems(inv, "ghost");
|
||||
expect(adj.size).toBe(0);
|
||||
});
|
||||
|
||||
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");
|
||||
// Item above left cell
|
||||
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 },
|
||||
});
|
||||
|
||||
placeItem(inv, bar);
|
||||
placeItem(inv, topA);
|
||||
placeItem(inv, topB);
|
||||
|
||||
const adj = getAdjacentItems(inv, "bar");
|
||||
expect(adj.size).toBe(2);
|
||||
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", () => {
|
||||
const inv = createGridInventory(4, 6);
|
||||
|
||||
// Sword: 1x3 horizontal at (0,0)
|
||||
const sword = createTestItem("sword", "oee");
|
||||
// Shield: 2x2 at (0,1)
|
||||
const shield = createTestItem("shield", "oes", {
|
||||
...IDENTITY_TRANSFORM,
|
||||
offset: { x: 0, y: 1 },
|
||||
});
|
||||
|
||||
expect(validatePlacement(inv, sword.shape, sword.transform)).toEqual({
|
||||
valid: true,
|
||||
});
|
||||
placeItem(inv, sword);
|
||||
|
||||
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 adjShield = getAdjacentItems(inv, "shield");
|
||||
expect(adjShield.has("sword")).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,445 +1,436 @@
|
|||
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 () => {
|
||||
const chain = createMiddlewareChain<{ value: number }>();
|
||||
const result = await chain.execute({ value: 42 });
|
||||
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 });
|
||||
expect(result).toEqual({ value: 42 });
|
||||
});
|
||||
|
||||
it('should call fallback when no middlewares', async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }, string>(
|
||||
async ctx => `value is ${ctx.value}`
|
||||
);
|
||||
const result = await chain.execute({ value: 42 });
|
||||
|
||||
expect(result).toBe('value is 42');
|
||||
});
|
||||
|
||||
it('should pass context to fallback', async () => {
|
||||
const chain = createMiddlewareChain<{ a: number; b: number }, number>(
|
||||
async ctx => ctx.a + ctx.b
|
||||
);
|
||||
const result = await chain.execute({ a: 3, b: 7 });
|
||||
|
||||
expect(result).toBe(10);
|
||||
});
|
||||
});
|
||||
|
||||
it("should call fallback when no middlewares", async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }, string>(
|
||||
async (ctx) => `value is ${ctx.value}`,
|
||||
);
|
||||
const result = await chain.execute({ value: 42 });
|
||||
describe('single middleware', () => {
|
||||
it('should execute a single middleware', async () => {
|
||||
const chain = createMiddlewareChain<{ count: number }>();
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.count *= 2;
|
||||
return next();
|
||||
});
|
||||
|
||||
expect(result).toBe("value is 42");
|
||||
const result = await chain.execute({ count: 5 });
|
||||
|
||||
expect(result.count).toBe(10);
|
||||
});
|
||||
|
||||
it('should allow middleware to modify return value', async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }, number>(
|
||||
async ctx => ctx.value
|
||||
);
|
||||
chain.use(async (ctx, next) => {
|
||||
const result = await next();
|
||||
return result * 2;
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 21 });
|
||||
|
||||
expect(result).toBe(42);
|
||||
});
|
||||
|
||||
it('should allow middleware to short-circuit without calling next', async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }>();
|
||||
chain.use(async (_ctx, _next) => {
|
||||
return { value: 999 };
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 1 });
|
||||
|
||||
expect(result.value).toBe(999);
|
||||
});
|
||||
});
|
||||
|
||||
it("should pass context to fallback", async () => {
|
||||
const chain = createMiddlewareChain<{ a: number; b: number }, number>(
|
||||
async (ctx) => ctx.a + ctx.b,
|
||||
);
|
||||
const result = await chain.execute({ a: 3, b: 7 });
|
||||
describe('multiple middlewares', () => {
|
||||
it('should execute middlewares in order', async () => {
|
||||
const order: number[] = [];
|
||||
const chain = createMiddlewareChain<{ value: number }>();
|
||||
|
||||
expect(result).toBe(10);
|
||||
});
|
||||
});
|
||||
chain.use(async (_ctx, next) => {
|
||||
order.push(1);
|
||||
const result = await next();
|
||||
order.push(4);
|
||||
return result;
|
||||
});
|
||||
chain.use(async (_ctx, next) => {
|
||||
order.push(2);
|
||||
const result = await next();
|
||||
order.push(3);
|
||||
return result;
|
||||
});
|
||||
|
||||
describe("single middleware", () => {
|
||||
it("should execute a single middleware", async () => {
|
||||
const chain = createMiddlewareChain<{ count: number }>();
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.count *= 2;
|
||||
return next();
|
||||
});
|
||||
await chain.execute({ value: 0 });
|
||||
|
||||
const result = await chain.execute({ count: 5 });
|
||||
expect(order).toEqual([1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
expect(result.count).toBe(10);
|
||||
it('should accumulate modifications through the chain', async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }>();
|
||||
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.value += 1;
|
||||
return next();
|
||||
});
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.value *= 2;
|
||||
return next();
|
||||
});
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.value += 3;
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 0 });
|
||||
|
||||
expect(result.value).toBe(5);
|
||||
});
|
||||
|
||||
it('should allow middleware to modify result on the way back', async () => {
|
||||
const chain = createMiddlewareChain<{ base: number }, number>(
|
||||
async ctx => ctx.base
|
||||
);
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
const result = await next();
|
||||
return result + 10;
|
||||
});
|
||||
chain.use(async (_ctx, next) => {
|
||||
const result = await next();
|
||||
return result * 2;
|
||||
});
|
||||
|
||||
const result = await chain.execute({ base: 5 });
|
||||
|
||||
expect(result).toBe(20);
|
||||
});
|
||||
|
||||
it('should allow middleware to short-circuit in the middle', async () => {
|
||||
const executed: number[] = [];
|
||||
const chain = createMiddlewareChain<{ value: number }>();
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
executed.push(1);
|
||||
return next();
|
||||
});
|
||||
chain.use(async () => {
|
||||
executed.push(2);
|
||||
return { value: -1 };
|
||||
});
|
||||
chain.use(async (_ctx, next) => {
|
||||
executed.push(3);
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 100 });
|
||||
|
||||
expect(result.value).toBe(-1);
|
||||
expect(executed).toEqual([1, 2]);
|
||||
});
|
||||
});
|
||||
|
||||
it("should allow middleware to modify return value", async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }, number>(
|
||||
async (ctx) => ctx.value,
|
||||
);
|
||||
chain.use(async (ctx, next) => {
|
||||
const result = await next();
|
||||
return result * 2;
|
||||
});
|
||||
describe('nested next calls', () => {
|
||||
it('should advance index on each next call, skipping remaining middlewares', async () => {
|
||||
const chain = createMiddlewareChain<{ counter: number }>();
|
||||
|
||||
const result = await chain.execute({ value: 21 });
|
||||
chain.use(async (_ctx, next) => {
|
||||
await next();
|
||||
await next();
|
||||
});
|
||||
|
||||
expect(result).toBe(42);
|
||||
const result = await chain.execute({ counter: 0 });
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
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 next();
|
||||
});
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.value += 10;
|
||||
return next();
|
||||
});
|
||||
|
||||
const resultA = await chain.execute({ skip: true, value: 0 });
|
||||
const resultB = await chain.execute({ skip: false, value: 0 });
|
||||
|
||||
expect(resultA.value).toBe(-1);
|
||||
expect(resultB.value).toBe(10);
|
||||
});
|
||||
|
||||
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
|
||||
);
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
log.push('before');
|
||||
await next();
|
||||
log.push('after-first');
|
||||
await next();
|
||||
log.push('after-second');
|
||||
return log;
|
||||
});
|
||||
chain.use(async (_ctx, next) => {
|
||||
log.push('mw2');
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 0 });
|
||||
|
||||
expect(result).toEqual(['before', 'mw2', 'after-first', 'after-second']);
|
||||
});
|
||||
|
||||
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
|
||||
);
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
await next();
|
||||
return await next();
|
||||
});
|
||||
chain.use(async (_ctx, next) => {
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 5 });
|
||||
|
||||
expect(result).toBe(50);
|
||||
});
|
||||
|
||||
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
|
||||
);
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
await next();
|
||||
return await next();
|
||||
});
|
||||
chain.use(async (_ctx, next) => {
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 5 });
|
||||
|
||||
expect(result).toBe(50);
|
||||
});
|
||||
});
|
||||
|
||||
it("should allow middleware to short-circuit without calling next", async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }>();
|
||||
chain.use(async (_ctx, _next) => {
|
||||
return { value: 999 };
|
||||
});
|
||||
describe('async behavior', () => {
|
||||
it('should handle async middlewares', async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }>();
|
||||
|
||||
const result = await chain.execute({ value: 1 });
|
||||
chain.use(async (ctx, next) => {
|
||||
await new Promise(resolve => setTimeout(resolve, 10));
|
||||
ctx.value += 1;
|
||||
return next();
|
||||
});
|
||||
chain.use(async (ctx, next) => {
|
||||
await new Promise(resolve => setTimeout(resolve, 10));
|
||||
ctx.value += 2;
|
||||
return next();
|
||||
});
|
||||
|
||||
expect(result.value).toBe(999);
|
||||
});
|
||||
});
|
||||
const result = await chain.execute({ value: 0 });
|
||||
|
||||
describe("multiple middlewares", () => {
|
||||
it("should execute middlewares in order", async () => {
|
||||
const order: number[] = [];
|
||||
const chain = createMiddlewareChain<{ value: number }>();
|
||||
expect(result.value).toBe(3);
|
||||
});
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
order.push(1);
|
||||
const result = await next();
|
||||
order.push(4);
|
||||
return result;
|
||||
});
|
||||
chain.use(async (_ctx, next) => {
|
||||
order.push(2);
|
||||
const result = await next();
|
||||
order.push(3);
|
||||
return result;
|
||||
});
|
||||
it('should handle async fallback', async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }, number>(
|
||||
async ctx => {
|
||||
await new Promise(resolve => setTimeout(resolve, 10));
|
||||
return ctx.value * 10;
|
||||
}
|
||||
);
|
||||
|
||||
await chain.execute({ value: 0 });
|
||||
const result = await chain.execute({ value: 5 });
|
||||
|
||||
expect(order).toEqual([1, 2, 3, 4]);
|
||||
expect(result).toBe(50);
|
||||
});
|
||||
});
|
||||
|
||||
it("should accumulate modifications through the chain", async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }>();
|
||||
describe('error handling', () => {
|
||||
it('should propagate errors from middleware', async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }>();
|
||||
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.value += 1;
|
||||
return next();
|
||||
});
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.value *= 2;
|
||||
return next();
|
||||
});
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.value += 3;
|
||||
return next();
|
||||
});
|
||||
chain.use(async () => {
|
||||
throw new Error('middleware error');
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 0 });
|
||||
await expect(chain.execute({ value: 1 })).rejects.toThrow('middleware error');
|
||||
});
|
||||
|
||||
expect(result.value).toBe(5);
|
||||
it('should propagate errors from fallback', async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }, number>(
|
||||
async () => {
|
||||
throw new Error('fallback error');
|
||||
}
|
||||
);
|
||||
|
||||
await expect(chain.execute({ value: 1 })).rejects.toThrow('fallback error');
|
||||
});
|
||||
|
||||
it('should allow middleware to catch errors from downstream', async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }>();
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
try {
|
||||
return await next();
|
||||
} catch {
|
||||
return { value: -1 };
|
||||
}
|
||||
});
|
||||
chain.use(async () => {
|
||||
throw new Error('downstream error');
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 1 });
|
||||
|
||||
expect(result.value).toBe(-1);
|
||||
});
|
||||
});
|
||||
|
||||
it("should allow middleware to modify result on the way back", async () => {
|
||||
const chain = createMiddlewareChain<{ base: number }, number>(
|
||||
async (ctx) => ctx.base,
|
||||
);
|
||||
describe('return type override', () => {
|
||||
it('should support different TReturn type than TContext', async () => {
|
||||
const chain = createMiddlewareChain<{ name: string }, string>(
|
||||
async ctx => `Hello, ${ctx.name}!`
|
||||
);
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
const result = await next();
|
||||
return result + 10;
|
||||
});
|
||||
chain.use(async (_ctx, next) => {
|
||||
const result = await next();
|
||||
return result * 2;
|
||||
});
|
||||
const result = await chain.execute({ name: 'World' });
|
||||
|
||||
const result = await chain.execute({ base: 5 });
|
||||
expect(result).toBe('Hello, World!');
|
||||
});
|
||||
|
||||
expect(result).toBe(20);
|
||||
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)
|
||||
);
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
const sum = await next();
|
||||
return sum * 2;
|
||||
});
|
||||
|
||||
const result = await chain.execute({ items: [1, 2, 3] });
|
||||
|
||||
expect(result).toBe(12);
|
||||
});
|
||||
});
|
||||
|
||||
it("should allow middleware to short-circuit in the middle", async () => {
|
||||
const executed: number[] = [];
|
||||
const chain = createMiddlewareChain<{ value: number }>();
|
||||
describe('reusability', () => {
|
||||
it('should reset index on each execute call', async () => {
|
||||
const chain = createMiddlewareChain<{ count: number }>();
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
executed.push(1);
|
||||
return next();
|
||||
});
|
||||
chain.use(async () => {
|
||||
executed.push(2);
|
||||
return { value: -1 };
|
||||
});
|
||||
chain.use(async (_ctx, next) => {
|
||||
executed.push(3);
|
||||
return next();
|
||||
});
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.count += 1;
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 100 });
|
||||
const resultA = await chain.execute({ count: 0 });
|
||||
const resultB = await chain.execute({ count: 0 });
|
||||
|
||||
expect(result.value).toBe(-1);
|
||||
expect(executed).toEqual([1, 2]);
|
||||
});
|
||||
});
|
||||
expect(resultA.count).toBe(1);
|
||||
expect(resultB.count).toBe(1);
|
||||
});
|
||||
|
||||
describe("nested next calls", () => {
|
||||
it("should advance index on each next call, skipping remaining middlewares", async () => {
|
||||
const chain = createMiddlewareChain<{ counter: number }>();
|
||||
it('should share middlewares across execute calls', async () => {
|
||||
const chain = createMiddlewareChain<{ log: string[] }>();
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
await next();
|
||||
await next();
|
||||
return undefined as unknown as { counter: number };
|
||||
});
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.log.push('always');
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ counter: 0 });
|
||||
await chain.execute({ log: [] });
|
||||
await chain.execute({ log: [] });
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
expect(chain).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
it("should allow middleware to call next conditionally", async () => {
|
||||
const chain = createMiddlewareChain<{ skip: boolean; value: number }>();
|
||||
describe('edge cases', () => {
|
||||
it('should handle empty context object', async () => {
|
||||
const chain = createMiddlewareChain<Record<string, never>>();
|
||||
const result = await chain.execute({});
|
||||
|
||||
chain.use(async (ctx, next) => {
|
||||
if (ctx.skip) {
|
||||
return { skip: ctx.skip, value: -1 };
|
||||
}
|
||||
return next();
|
||||
});
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.value += 10;
|
||||
return next();
|
||||
});
|
||||
expect(result).toEqual({});
|
||||
});
|
||||
|
||||
const resultA = await chain.execute({ skip: true, value: 0 });
|
||||
const resultB = await chain.execute({ skip: false, value: 0 });
|
||||
it('should handle middleware that returns a completely different object', async () => {
|
||||
const chain = createMiddlewareChain<{ x: number }, { y: string }>(
|
||||
async () => ({ y: 'default' })
|
||||
);
|
||||
|
||||
expect(resultA.value).toBe(-1);
|
||||
expect(resultB.value).toBe(10);
|
||||
chain.use(async (_ctx, next) => {
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ x: 42 });
|
||||
|
||||
expect(result).toEqual({ y: 'default' });
|
||||
});
|
||||
|
||||
it('should handle middleware that mutates context without returning', async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }>(
|
||||
async ctx => ctx
|
||||
);
|
||||
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.value = 100;
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 0 });
|
||||
|
||||
expect(result.value).toBe(100);
|
||||
});
|
||||
|
||||
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;
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 0 });
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
log.push("before");
|
||||
await next();
|
||||
log.push("after-first");
|
||||
await next();
|
||||
log.push("after-second");
|
||||
return log;
|
||||
});
|
||||
chain.use(async (_ctx, next) => {
|
||||
log.push("mw2");
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 0 });
|
||||
|
||||
expect(result).toEqual(["before", "mw2", "after-first", "after-second"]);
|
||||
});
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
await next();
|
||||
return await next();
|
||||
});
|
||||
chain.use(async (_ctx, next) => {
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 5 });
|
||||
|
||||
expect(result).toBe(50);
|
||||
});
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
await next();
|
||||
return await next();
|
||||
});
|
||||
chain.use(async (_ctx, next) => {
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 5 });
|
||||
|
||||
expect(result).toBe(50);
|
||||
});
|
||||
});
|
||||
|
||||
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));
|
||||
ctx.value += 1;
|
||||
return next();
|
||||
});
|
||||
chain.use(async (ctx, next) => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
ctx.value += 2;
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 0 });
|
||||
|
||||
expect(result.value).toBe(3);
|
||||
});
|
||||
|
||||
it("should handle async fallback", async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }, number>(
|
||||
async (ctx) => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
return ctx.value * 10;
|
||||
},
|
||||
);
|
||||
|
||||
const result = await chain.execute({ value: 5 });
|
||||
|
||||
expect(result).toBe(50);
|
||||
});
|
||||
});
|
||||
|
||||
describe("error handling", () => {
|
||||
it("should propagate errors from middleware", async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }>();
|
||||
|
||||
chain.use(async () => {
|
||||
throw new Error("middleware error");
|
||||
});
|
||||
|
||||
await expect(chain.execute({ value: 1 })).rejects.toThrow(
|
||||
"middleware error",
|
||||
);
|
||||
});
|
||||
|
||||
it("should propagate errors from fallback", async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }, number>(
|
||||
async () => {
|
||||
throw new Error("fallback error");
|
||||
},
|
||||
);
|
||||
|
||||
await expect(chain.execute({ value: 1 })).rejects.toThrow(
|
||||
"fallback error",
|
||||
);
|
||||
});
|
||||
|
||||
it("should allow middleware to catch errors from downstream", async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }>();
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
try {
|
||||
return await next();
|
||||
} catch {
|
||||
return { value: -1 };
|
||||
}
|
||||
});
|
||||
chain.use(async () => {
|
||||
throw new Error("downstream error");
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 1 });
|
||||
|
||||
expect(result.value).toBe(-1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("return type override", () => {
|
||||
it("should support different TReturn type than TContext", async () => {
|
||||
const chain = createMiddlewareChain<{ name: string }, string>(
|
||||
async (ctx) => `Hello, ${ctx.name}!`,
|
||||
);
|
||||
|
||||
const result = await chain.execute({ name: "World" });
|
||||
|
||||
expect(result).toBe("Hello, World!");
|
||||
});
|
||||
|
||||
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),
|
||||
);
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
const sum = await next();
|
||||
return sum * 2;
|
||||
});
|
||||
|
||||
const result = await chain.execute({ items: [1, 2, 3] });
|
||||
|
||||
expect(result).toBe(12);
|
||||
});
|
||||
});
|
||||
|
||||
describe("reusability", () => {
|
||||
it("should reset index on each execute call", async () => {
|
||||
const chain = createMiddlewareChain<{ count: number }>();
|
||||
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.count += 1;
|
||||
return next();
|
||||
});
|
||||
|
||||
const resultA = await chain.execute({ count: 0 });
|
||||
const resultB = await chain.execute({ count: 0 });
|
||||
|
||||
expect(resultA.count).toBe(1);
|
||||
expect(resultB.count).toBe(1);
|
||||
});
|
||||
|
||||
it("should share middlewares across execute calls", async () => {
|
||||
const chain = createMiddlewareChain<{ log: string[] }>();
|
||||
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.log.push("always");
|
||||
return next();
|
||||
});
|
||||
|
||||
await chain.execute({ log: [] });
|
||||
await chain.execute({ log: [] });
|
||||
|
||||
expect(chain).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
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 () => {
|
||||
const chain = createMiddlewareChain<{ x: number }, { y: string }>(
|
||||
async () => ({ y: "default" }),
|
||||
);
|
||||
|
||||
chain.use(async (_ctx, next) => {
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ x: 42 });
|
||||
|
||||
expect(result).toEqual({ y: "default" });
|
||||
});
|
||||
|
||||
it("should handle middleware that mutates context without returning", async () => {
|
||||
const chain = createMiddlewareChain<{ value: number }>(
|
||||
async (ctx) => ctx,
|
||||
);
|
||||
|
||||
chain.use(async (ctx, next) => {
|
||||
ctx.value = 100;
|
||||
return next();
|
||||
});
|
||||
|
||||
const result = await chain.execute({ value: 0 });
|
||||
|
||||
expect(result.value).toBe(100);
|
||||
});
|
||||
|
||||
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 });
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue