refactor: update api
This commit is contained in:
@@ -1,18 +1,15 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { createGameContext } from '../../src/core/game';
|
||||
import { createCommandRegistry } from '../../src/utils/command';
|
||||
import { registerTicTacToeCommands, checkWinner, isCellOccupied, placePiece } from '../../src/samples/tic-tac-toe';
|
||||
import type { IGameContext } from '../../src/core/game';
|
||||
import {registry, checkWinner, isCellOccupied, placePiece, createInitialState} from '../../src/samples/tic-tac-toe';
|
||||
import type { TicTacToeContext } from '../../src/samples/tic-tac-toe';
|
||||
import type { Part } from '../../src/core/part';
|
||||
import {createGameContext} from "../../src";
|
||||
|
||||
function createTestContext() {
|
||||
const registry = createCommandRegistry<IGameContext>();
|
||||
registerTicTacToeCommands(registry);
|
||||
const ctx = createGameContext(registry);
|
||||
const ctx = createGameContext(registry, createInitialState);
|
||||
return { registry, ctx };
|
||||
}
|
||||
|
||||
function setupBoard(ctx: IGameContext) {
|
||||
function setupBoard(ctx: TicTacToeContext) {
|
||||
ctx.regions.add({
|
||||
id: 'board',
|
||||
axes: [
|
||||
@@ -23,7 +20,7 @@ function setupBoard(ctx: IGameContext) {
|
||||
});
|
||||
}
|
||||
|
||||
function addPiece(ctx: IGameContext, id: string, row: number, col: number) {
|
||||
function addPiece(ctx: TicTacToeContext, id: string, row: number, col: number) {
|
||||
const board = ctx.regions.get('board');
|
||||
const part: Part = {
|
||||
id,
|
||||
@@ -172,10 +169,10 @@ describe('TicTacToe - helper functions', () => {
|
||||
|
||||
describe('TicTacToe - game flow', () => {
|
||||
it('should have setup and turn commands registered', () => {
|
||||
const { registry } = createTestContext();
|
||||
const { registry: reg } = createTestContext();
|
||||
|
||||
expect(registry.has('setup')).toBe(true);
|
||||
expect(registry.has('turn')).toBe(true);
|
||||
expect(reg.has('setup')).toBe(true);
|
||||
expect(reg.has('turn')).toBe(true);
|
||||
});
|
||||
|
||||
it('should setup board when setup command runs', async () => {
|
||||
@@ -205,7 +202,6 @@ describe('TicTacToe - game flow', () => {
|
||||
|
||||
promptEvent.resolve({ name: 'play', params: ['X', 1, 1], options: {}, flags: {} });
|
||||
|
||||
// After valid non-winning move, turn command prompts again, reject to stop
|
||||
const promptEvent2 = await ctx.prompts.pop();
|
||||
promptEvent2.reject(new Error('done'));
|
||||
|
||||
@@ -229,7 +225,6 @@ describe('TicTacToe - game flow', () => {
|
||||
|
||||
promptEvent2.resolve({ name: 'play', params: ['X', 1, 1], options: {}, flags: {} });
|
||||
|
||||
// After valid non-winning move, reject next prompt
|
||||
const promptEvent3 = await ctx.prompts.pop();
|
||||
promptEvent3.reject(new Error('done'));
|
||||
|
||||
@@ -253,7 +248,6 @@ describe('TicTacToe - game flow', () => {
|
||||
|
||||
promptEvent2.resolve({ name: 'play', params: ['X', 0, 0], options: {}, flags: {} });
|
||||
|
||||
// After valid non-winning move, reject next prompt
|
||||
const promptEvent3 = await ctx.prompts.pop();
|
||||
promptEvent3.reject(new Error('done'));
|
||||
|
||||
@@ -265,7 +259,6 @@ describe('TicTacToe - game flow', () => {
|
||||
const { ctx } = createTestContext();
|
||||
setupBoard(ctx);
|
||||
|
||||
// X plays (0,0)
|
||||
let runPromise = ctx.commands.run('turn X 1');
|
||||
let prompt = await ctx.prompts.pop();
|
||||
prompt.resolve({ name: 'play', params: ['X', 0, 0], options: {}, flags: {} });
|
||||
@@ -274,7 +267,6 @@ describe('TicTacToe - game flow', () => {
|
||||
let result = await runPromise;
|
||||
expect(result.success).toBe(false);
|
||||
|
||||
// O plays (0,1)
|
||||
runPromise = ctx.commands.run('turn O 2');
|
||||
prompt = await ctx.prompts.pop();
|
||||
prompt.resolve({ name: 'play', params: ['O', 0, 1], options: {}, flags: {} });
|
||||
@@ -283,7 +275,6 @@ describe('TicTacToe - game flow', () => {
|
||||
result = await runPromise;
|
||||
expect(result.success).toBe(false);
|
||||
|
||||
// X plays (1,0)
|
||||
runPromise = ctx.commands.run('turn X 3');
|
||||
prompt = await ctx.prompts.pop();
|
||||
prompt.resolve({ name: 'play', params: ['X', 1, 0], options: {}, flags: {} });
|
||||
@@ -292,7 +283,6 @@ describe('TicTacToe - game flow', () => {
|
||||
result = await runPromise;
|
||||
expect(result.success).toBe(false);
|
||||
|
||||
// O plays (0,2)
|
||||
runPromise = ctx.commands.run('turn O 4');
|
||||
prompt = await ctx.prompts.pop();
|
||||
prompt.resolve({ name: 'play', params: ['O', 0, 2], options: {}, flags: {} });
|
||||
@@ -301,7 +291,6 @@ describe('TicTacToe - game flow', () => {
|
||||
result = await runPromise;
|
||||
expect(result.success).toBe(false);
|
||||
|
||||
// X plays (2,0) - wins with vertical line
|
||||
runPromise = ctx.commands.run('turn X 5');
|
||||
prompt = await ctx.prompts.pop();
|
||||
prompt.resolve({ name: 'play', params: ['X', 2, 0], options: {}, flags: {} });
|
||||
@@ -314,28 +303,23 @@ describe('TicTacToe - game flow', () => {
|
||||
const { ctx } = createTestContext();
|
||||
setupBoard(ctx);
|
||||
|
||||
// Pre-place 8 pieces that don't form any winning line for either player
|
||||
// Using positions that clearly don't form lines
|
||||
// X pieces at even indices, O pieces at odd indices
|
||||
const pieces = [
|
||||
{ id: 'p1', pos: [0, 0] }, // X
|
||||
{ id: 'p2', pos: [2, 2] }, // O
|
||||
{ id: 'p3', pos: [0, 2] }, // X
|
||||
{ id: 'p4', pos: [2, 0] }, // O
|
||||
{ id: 'p5', pos: [1, 0] }, // X
|
||||
{ id: 'p6', pos: [0, 1] }, // O
|
||||
{ id: 'p7', pos: [2, 1] }, // X
|
||||
{ id: 'p8', pos: [1, 2] }, // O
|
||||
{ id: 'p1', pos: [0, 0] },
|
||||
{ id: 'p2', pos: [2, 2] },
|
||||
{ id: 'p3', pos: [0, 2] },
|
||||
{ id: 'p4', pos: [2, 0] },
|
||||
{ id: 'p5', pos: [1, 0] },
|
||||
{ id: 'p6', pos: [0, 1] },
|
||||
{ id: 'p7', pos: [2, 1] },
|
||||
{ id: 'p8', pos: [1, 2] },
|
||||
];
|
||||
|
||||
for (const { id, pos } of pieces) {
|
||||
addPiece(ctx, id, pos[0], pos[1]);
|
||||
}
|
||||
|
||||
// Verify no winner before 9th move
|
||||
expect(checkWinner(ctx)).toBeNull();
|
||||
|
||||
// Now X plays (1,1) for the 9th move -> draw
|
||||
const runPromise = ctx.commands.run('turn X 9');
|
||||
const prompt = await ctx.prompts.pop();
|
||||
prompt.resolve({ name: 'play', params: ['X', 1, 1], options: {}, flags: {} });
|
||||
|
||||
Reference in New Issue
Block a user