2026-04-01 23:58:07 +08:00
|
|
|
import { describe, it, expect } from 'vitest';
|
|
|
|
|
import { createGameContext } from '../../src/core/context';
|
2026-04-02 09:33:03 +08:00
|
|
|
import { registerTicTacToeCommands, startTicTacToe, type TicTacToeState } from '../../src/samples/tic-tac-toe';
|
2026-04-01 23:58:07 +08:00
|
|
|
|
|
|
|
|
describe('Tic-Tac-Toe', () => {
|
|
|
|
|
function createGame() {
|
|
|
|
|
const game = createGameContext();
|
2026-04-02 09:33:03 +08:00
|
|
|
registerTicTacToeCommands(game);
|
2026-04-01 23:58:07 +08:00
|
|
|
return game;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getBoardState(game: ReturnType<typeof createGame>) {
|
|
|
|
|
return game.latestContext<TicTacToeState>('tic-tac-toe')!.value;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
it('should initialize the board and start the game', async () => {
|
2026-04-01 23:58:07 +08:00
|
|
|
const game = createGame();
|
|
|
|
|
startTicTacToe(game);
|
2026-04-02 09:33:03 +08:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
2026-04-01 23:58:07 +08:00
|
|
|
|
|
|
|
|
const state = getBoardState(game);
|
|
|
|
|
expect(state.currentPlayer).toBe('X');
|
|
|
|
|
expect(state.winner).toBeNull();
|
|
|
|
|
expect(state.moveCount).toBe(0);
|
|
|
|
|
|
|
|
|
|
const board = game.regions.get('board');
|
|
|
|
|
expect(board.value.axes).toHaveLength(2);
|
|
|
|
|
expect(board.value.axes[0].name).toBe('x');
|
|
|
|
|
expect(board.value.axes[1].name).toBe('y');
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
it('should play moves and determine a winner', async () => {
|
2026-04-01 23:58:07 +08:00
|
|
|
const game = createGame();
|
|
|
|
|
startTicTacToe(game);
|
2026-04-02 09:33:03 +08:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
2026-04-01 23:58:07 +08:00
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
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));
|
2026-04-01 23:58:07 +08:00
|
|
|
|
|
|
|
|
const state = getBoardState(game);
|
|
|
|
|
expect(state.winner).toBe('X');
|
|
|
|
|
expect(state.moveCount).toBe(5);
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
it('should reject out-of-bounds moves', async () => {
|
2026-04-01 23:58:07 +08:00
|
|
|
const game = createGame();
|
|
|
|
|
startTicTacToe(game);
|
2026-04-02 09:33:03 +08:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
2026-04-01 23:58:07 +08:00
|
|
|
|
|
|
|
|
const beforeCount = getBoardState(game).moveCount;
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
game.enqueueAll([
|
|
|
|
|
'play X 5 5',
|
|
|
|
|
'play X -1 0',
|
|
|
|
|
'play X 3 3',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 200));
|
2026-04-01 23:58:07 +08:00
|
|
|
|
|
|
|
|
expect(getBoardState(game).moveCount).toBe(beforeCount);
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
it('should reject moves on occupied cells', async () => {
|
2026-04-01 23:58:07 +08:00
|
|
|
const game = createGame();
|
|
|
|
|
startTicTacToe(game);
|
2026-04-02 09:33:03 +08:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
2026-04-01 23:58:07 +08:00
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
game.enqueue('play X 1 1');
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
2026-04-01 23:58:07 +08:00
|
|
|
expect(getBoardState(game).moveCount).toBe(1);
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
game.enqueue('play O 1 1');
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
2026-04-01 23:58:07 +08:00
|
|
|
expect(getBoardState(game).moveCount).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
it('should ignore moves after game is over', async () => {
|
2026-04-01 23:58:07 +08:00
|
|
|
const game = createGame();
|
|
|
|
|
startTicTacToe(game);
|
2026-04-02 09:33:03 +08:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
|
|
|
|
|
|
|
|
|
game.enqueueAll([
|
|
|
|
|
'play X 0 0',
|
|
|
|
|
'play O 0 1',
|
|
|
|
|
'play X 1 0',
|
|
|
|
|
'play O 1 1',
|
|
|
|
|
'play X 2 0',
|
|
|
|
|
]);
|
2026-04-01 23:58:07 +08:00
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 200));
|
2026-04-01 23:58:07 +08:00
|
|
|
|
|
|
|
|
expect(getBoardState(game).winner).toBe('X');
|
|
|
|
|
const moveCountAfterWin = getBoardState(game).moveCount;
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
game.enqueueAll([
|
|
|
|
|
'play X 2 1',
|
|
|
|
|
'play O 2 2',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 200));
|
2026-04-01 23:58:07 +08:00
|
|
|
|
|
|
|
|
expect(getBoardState(game).moveCount).toBe(moveCountAfterWin);
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
it('should detect a draw', async () => {
|
2026-04-01 23:58:07 +08:00
|
|
|
const game = createGame();
|
|
|
|
|
startTicTacToe(game);
|
2026-04-02 09:33:03 +08:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
|
|
|
|
|
|
|
|
|
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));
|
2026-04-01 23:58:07 +08:00
|
|
|
|
|
|
|
|
const state = getBoardState(game);
|
|
|
|
|
expect(state.winner).toBe('draw');
|
|
|
|
|
expect(state.moveCount).toBe(9);
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
it('should place parts on the board region at correct positions', async () => {
|
2026-04-01 23:58:07 +08:00
|
|
|
const game = createGame();
|
|
|
|
|
startTicTacToe(game);
|
2026-04-02 09:33:03 +08:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
2026-04-01 23:58:07 +08:00
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
game.enqueue('play X 1 2');
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
2026-04-01 23:58:07 +08:00
|
|
|
|
|
|
|
|
const board = game.regions.get('board');
|
|
|
|
|
expect(board.value.children).toHaveLength(1);
|
|
|
|
|
|
|
|
|
|
const piece = board.value.children[0].value;
|
|
|
|
|
expect(piece.position).toEqual([1, 2]);
|
|
|
|
|
});
|
|
|
|
|
});
|