refactor: replace rule.ts with a command runner based solution

This commit is contained in:
2026-04-02 09:33:03 +08:00
parent bcb31da773
commit 40788d445d
7 changed files with 505 additions and 721 deletions
+66 -43
View File
@@ -1,11 +1,11 @@
import { describe, it, expect } from 'vitest';
import { createGameContext } from '../../src/core/context';
import { registerTicTacToeRules, startTicTacToe, type TicTacToeState } from '../../src/samples/tic-tac-toe';
import { registerTicTacToeCommands, startTicTacToe, type TicTacToeState } from '../../src/samples/tic-tac-toe';
describe('Tic-Tac-Toe', () => {
function createGame() {
const game = createGameContext();
registerTicTacToeRules(game);
registerTicTacToeCommands(game);
return game;
}
@@ -13,9 +13,10 @@ describe('Tic-Tac-Toe', () => {
return game.latestContext<TicTacToeState>('tic-tac-toe')!.value;
}
it('should initialize the board and start the game', () => {
it('should initialize the board and start the game', async () => {
const game = createGame();
startTicTacToe(game);
await new Promise(resolve => setTimeout(resolve, 100));
const state = getBoardState(game);
expect(state.currentPlayer).toBe('X');
@@ -28,95 +29,117 @@ describe('Tic-Tac-Toe', () => {
expect(board.value.axes[1].name).toBe('y');
});
it('should play moves and determine a winner', () => {
it('should play moves and determine a winner', async () => {
const game = createGame();
startTicTacToe(game);
await new Promise(resolve => setTimeout(resolve, 100));
// X wins with column 0
game.dispatchCommand('play X 0 0');
game.dispatchCommand('play O 0 1');
game.dispatchCommand('play X 1 0');
game.dispatchCommand('play O 1 1');
game.dispatchCommand('play X 2 0');
game.enqueueAll([
'play X 0 0',
'play O 0 1',
'play X 1 0',
'play O 1 1',
'play X 2 0',
]);
await new Promise(resolve => setTimeout(resolve, 200));
const state = getBoardState(game);
expect(state.winner).toBe('X');
expect(state.moveCount).toBe(5);
});
it('should reject out-of-bounds moves', () => {
it('should reject out-of-bounds moves', async () => {
const game = createGame();
startTicTacToe(game);
await new Promise(resolve => setTimeout(resolve, 100));
const beforeCount = getBoardState(game).moveCount;
game.dispatchCommand('play X 5 5');
game.dispatchCommand('play X -1 0');
game.dispatchCommand('play X 3 3');
game.enqueueAll([
'play X 5 5',
'play X -1 0',
'play X 3 3',
]);
await new Promise(resolve => setTimeout(resolve, 200));
expect(getBoardState(game).moveCount).toBe(beforeCount);
});
it('should reject moves on occupied cells', () => {
it('should reject moves on occupied cells', async () => {
const game = createGame();
startTicTacToe(game);
await new Promise(resolve => setTimeout(resolve, 100));
game.dispatchCommand('play X 1 1');
game.enqueue('play X 1 1');
await new Promise(resolve => setTimeout(resolve, 100));
expect(getBoardState(game).moveCount).toBe(1);
// Try to play on the same cell
game.dispatchCommand('play O 1 1');
game.enqueue('play O 1 1');
await new Promise(resolve => setTimeout(resolve, 100));
expect(getBoardState(game).moveCount).toBe(1);
});
it('should ignore moves after game is over', () => {
it('should ignore moves after game is over', async () => {
const game = createGame();
startTicTacToe(game);
await new Promise(resolve => setTimeout(resolve, 100));
// X wins
game.dispatchCommand('play X 0 0');
game.dispatchCommand('play O 0 1');
game.dispatchCommand('play X 1 0');
game.dispatchCommand('play O 1 1');
game.dispatchCommand('play X 2 0');
game.enqueueAll([
'play X 0 0',
'play O 0 1',
'play X 1 0',
'play O 1 1',
'play X 2 0',
]);
await new Promise(resolve => setTimeout(resolve, 200));
expect(getBoardState(game).winner).toBe('X');
const moveCountAfterWin = getBoardState(game).moveCount;
// Try to play more
game.dispatchCommand('play X 2 1');
game.dispatchCommand('play O 2 2');
game.enqueueAll([
'play X 2 1',
'play O 2 2',
]);
await new Promise(resolve => setTimeout(resolve, 200));
expect(getBoardState(game).moveCount).toBe(moveCountAfterWin);
});
it('should detect a draw', () => {
it('should detect a draw', async () => {
const game = createGame();
startTicTacToe(game);
await new Promise(resolve => setTimeout(resolve, 100));
// Fill board with no winner (cat's game)
// X: (1,1), (0,2), (2,2), (1,0), (2,1)
// O: (0,0), (2,0), (0,1), (1,2)
game.dispatchCommand('play X 1 1'); // X
game.dispatchCommand('play O 0 0'); // O
game.dispatchCommand('play X 0 2'); // X
game.dispatchCommand('play O 2 0'); // O
game.dispatchCommand('play X 2 2'); // X
game.dispatchCommand('play O 0 1'); // O
game.dispatchCommand('play X 1 0'); // X
game.dispatchCommand('play O 1 2'); // O
game.dispatchCommand('play X 2 1'); // X (last move, draw)
game.enqueueAll([
'play X 1 1',
'play O 0 0',
'play X 0 2',
'play O 2 0',
'play X 2 2',
'play O 0 1',
'play X 1 0',
'play O 1 2',
'play X 2 1',
]);
await new Promise(resolve => setTimeout(resolve, 300));
const state = getBoardState(game);
expect(state.winner).toBe('draw');
expect(state.moveCount).toBe(9);
});
it('should place parts on the board region at correct positions', () => {
it('should place parts on the board region at correct positions', async () => {
const game = createGame();
startTicTacToe(game);
await new Promise(resolve => setTimeout(resolve, 100));
game.dispatchCommand('play X 1 2');
game.enqueue('play X 1 2');
await new Promise(resolve => setTimeout(resolve, 100));
const board = game.regions.get('board');
expect(board.value.children).toHaveLength(1);