test: reformat tests to use double quotes and improve style

This commit is contained in:
hypercross 2026-04-20 15:16:03 +08:00
parent d5ec099540
commit 0c94e6923a
5 changed files with 1561 additions and 1449 deletions

View File

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

View File

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

View File

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

View File

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

View File

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