fix: tests for boop

This commit is contained in:
hypercross 2026-04-04 21:57:25 +08:00
parent b90a4bba52
commit 28782aaf9b
1 changed files with 78 additions and 50 deletions

View File

@ -1,14 +1,23 @@
import { createGameHost } from '@/core/game-host';
import * as boop from '@/samples/boop';
import { describe, it, expect } from 'vitest';
import { registry, createInitialState, BoopState } from '@/samples/boop';
import { createGameContext } from '@/index';
import type { PromptEvent } from '@/utils/command';
function createTestHost() {
return createGameHost(boop);
function createTestContext() {
const ctx = createGameContext(registry, createInitialState());
return { registry, ctx };
}
function waitForPrompt(ctx: ReturnType<typeof createTestContext>['ctx']): Promise<PromptEvent> {
return new Promise(resolve => {
ctx._commands.on('prompt', resolve);
});
}
describe('Boop Game', () => {
describe('Setup', () => {
it('should create initial state correctly', () => {
const state = boop.createInitialState();
const state = createInitialState();
expect(state.currentPlayer).toBe('white');
expect(state.winner).toBeNull();
@ -40,17 +49,24 @@ describe('Boop Game', () => {
describe('Place and Boop Commands', () => {
it('should place a kitten via play command', async () => {
const host = createTestHost();
await host.setup('setup');
const { ctx } = createTestContext();
// Use the play command which is what the system expects
const result = host.onInput('play white 2 2 kitten');
expect(result).toBeNull();
// Use turn command instead of setup which runs indefinitely
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.run('turn white');
// Wait for async operations
await new Promise(resolve => setTimeout(resolve, 50));
const promptEvent = await promptPromise;
expect(promptEvent).not.toBeNull();
expect(promptEvent.schema.name).toBe('play');
const state = host.state.value;
// Place a kitten at position 2,2
const error = promptEvent.tryCommit({ name: 'play', params: ['white', 2, 2, 'kitten'], options: {}, flags: {} });
expect(error).toBeNull();
const result = await runPromise;
expect(result.success).toBe(true);
const state = ctx.value;
// Should have placed a piece on the board
const boardPieces = Object.keys(state.regions.board.partMap);
expect(boardPieces.length).toBeGreaterThan(0);
@ -63,32 +79,45 @@ describe('Boop Game', () => {
describe('Boop Mechanics', () => {
it('should boop adjacent pieces away from placement', async () => {
const host = createTestHost();
await host.setup('setup');
const { ctx } = createTestContext();
// White places at 2,2
host.onInput('play white 2 2 kitten');
await new Promise(resolve => setTimeout(resolve, 50));
let promptPromise = waitForPrompt(ctx);
let runPromise = ctx.run('turn white');
let promptEvent = await promptPromise;
let error = promptEvent.tryCommit({ name: 'play', params: ['white', 2, 2, 'kitten'], options: {}, flags: {} });
expect(error).toBeNull();
let result = await runPromise;
expect(result.success).toBe(true);
// Black places at 2,3, which will boop white's piece
host.onInput('play black 2 3 kitten');
await new Promise(resolve => setTimeout(resolve, 50));
promptPromise = waitForPrompt(ctx);
runPromise = ctx.run('turn black');
promptEvent = await promptPromise;
error = promptEvent.tryCommit({ name: 'play', params: ['black', 2, 3, 'kitten'], options: {}, flags: {} });
expect(error).toBeNull();
result = await runPromise;
expect(result.success).toBe(true);
const state = host.state.value;
const state = ctx.value;
// Check that pieces were placed
const boardPieceCount = Object.keys(state.regions.board.partMap).length;
expect(boardPieceCount).toBeGreaterThanOrEqual(1);
});
it('should handle pieces being booped off the board', async () => {
const host = createTestHost();
await host.setup('setup');
const { ctx } = createTestContext();
// White places at corner
host.onInput('play white 0 0 kitten');
await new Promise(resolve => setTimeout(resolve, 50));
const promptPromise = waitForPrompt(ctx);
const runPromise = ctx.run('turn white');
const promptEvent = await promptPromise;
const error = promptEvent.tryCommit({ name: 'play', params: ['white', 0, 0, 'kitten'], options: {}, flags: {} });
expect(error).toBeNull();
const result = await runPromise;
expect(result.success).toBe(true);
const state = host.state.value;
const state = ctx.value;
// Verify placement
expect(state.regions.board.partMap['0,0']).toBeDefined();
});
@ -96,21 +125,20 @@ describe('Boop Game', () => {
describe('Full Game Flow', () => {
it('should play a turn and switch players', async () => {
const host = createTestHost();
await host.setup('setup');
const { ctx } = createTestContext();
// White's turn - place at 2,2
host.onInput('play white 2 2 kitten');
await new Promise(resolve => setTimeout(resolve, 100));
let promptPromise = waitForPrompt(ctx);
let runPromise = ctx.run('turn white');
let prompt = await promptPromise;
const error1 = prompt.tryCommit({ name: 'play', params: ['white', 2, 2, 'kitten'], options: {}, flags: {} });
expect(error1).toBeNull();
let result = await runPromise;
expect(result.success).toBe(true);
const stateAfterWhite = host.state.value;
const stateAfterWhite = ctx.value;
// Should have placed a piece
expect(stateAfterWhite.regions.board.partMap['2,2']).toBeDefined();
expect(stateAfterWhite.regions.board.partMap['2,2']).toBe('white-kitten-1');
// Current player should still be white (turn hasn't completed from setup's perspective)
// But we can check if black's turn has started by trying to play as black
// This is a bit tricky, so let's just verify the board state
});
});
});