ChessUp/tests/chess.test.ts

135 lines
4.7 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { Chess, INITIAL_FEN } from '../src/shared/chess';
test('Perft startpos depths 1-4', () => {
const c = new Chess(INITIAL_FEN);
assert.equal(c.perft(1), 20);
assert.equal(c.perft(2), 400);
assert.equal(c.perft(3), 8902);
assert.equal(c.perft(4), 197281);
});
test('Perft Kiwipete depths 1-3', () => {
const c = new Chess('r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1');
assert.equal(c.perft(1), 48);
assert.equal(c.perft(2), 2039);
assert.equal(c.perft(3), 97862);
});
test('Perft Position 3 (EP discovered check pin) depths 1-4', () => {
const c = new Chess('8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1');
assert.equal(c.perft(1), 14);
assert.equal(c.perft(2), 191);
assert.equal(c.perft(3), 2812);
assert.equal(c.perft(4), 43238);
});
test('Perft Position 4 (promotions & castling) depths 1-3', () => {
const c = new Chess('r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1');
assert.equal(c.perft(1), 6);
assert.equal(c.perft(2), 264);
assert.equal(c.perft(3), 9467);
});
test('Perft Position 5 depths 1-3', () => {
const c = new Chess('rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8');
assert.equal(c.perft(1), 44);
assert.equal(c.perft(2), 1486);
assert.equal(c.perft(3), 62379);
});
test('Rook capture clears opponent castling right', () => {
const c = new Chess('r3k2r/8/8/8/8/8/7B/R3K2R w KQkq - 0 1');
const mv = c.moveSan('Bxh8');
//Wait h2 to h8 is blocked? Wait a1->h8 diagonal is b2..g7, h2->b8; let's use Bishop on g7 or a1
assert.equal(mv, null);
const c2 = new Chess('r3k2r/8/8/8/8/8/1B6/R3K2R w KQkq - 0 1');
const cap = c2.moveSan('Bxh8');
assert.ok(cap);
assert.equal(c2.fen().split(' ')[2], 'KQq');
});
test('SAN disambiguation, promotion, check, and checkmate symbols', () => {
// Two rooks on same file + two knights on same rank targeting f4
const c = new Chess('4k3/8/8/8/R7/8/4N1N1/R3K3 w - - 0 1');
const rMove = c.moveSan('R1a3');
assert.ok(rMove);
assert.equal(rMove.san, 'R1a3');
c.undo();
const nMove = c.moveSan('Nef4');
assert.ok(nMove);
assert.equal(nMove.san, 'Nef4');
// Promotion with checkmate
const promo = new Chess('6k1/4P3/6K1/8/8/8/8/8 w - - 0 1');
const mateMv = promo.moveUci('e7e8q');
assert.ok(mateMv);
assert.equal(mateMv.san, 'e8=Q#');
});
test('PGN export and import with comments and variations', () => {
const pgn = `[Event "Test Game"]
[White "Alice"]
[Black "Bob"]
[Result "1-0"]
1. e4 {King's pawn} e5 (1... c5 2. Nf3) 2. Qh5 Nc6 3. Bc4 Nf6 $4 4. Qxf7# 1-0`;
const parsed = Chess.fromPgn(pgn);
assert.equal(parsed.headers['White'], 'Alice');
assert.equal(parsed.moves.length, 7);
assert.equal(parsed.result, '1-0');
assert.equal(parsed.chess.isCheckmate(), true);
const exported = parsed.chess.toPgn({ White: 'Alice', Black: 'Bob', Result: '1-0' });
const reparsed = Chess.fromPgn(exported);
assert.deepEqual(
reparsed.moves.map((m) => m.uci),
parsed.moves.map((m) => m.uci)
);
});
test('Draw conditions: stalemate, insufficient material, threefold repetition with EP rule, 50-move', () => {
// Stalemate
const stale = new Chess('7k/5Q2/6K1/8/8/8/8/8 b - - 0 1');
assert.equal(stale.isStalemate(), true);
assert.deepEqual(stale.gameOutcome(), { result: '1/2-1/2', termination: 'stalemate' });
// Insufficient material: same-color bishops (c3 dark & g1 dark) vs opposite-color bishops (c3 dark & h1 light)
const sameColor = new Chess('4k3/8/8/8/8/2B5/8/4K1b1 w - - 0 1');
assert.equal(sameColor.isInsufficientMaterial(), true);
const diffColor = new Chess('4k3/8/8/8/8/2B5/8/4K2b w - - 0 1');
assert.equal(diffColor.isInsufficientMaterial(), false);
// 50-move rule
const fifty = new Chess('4k3/8/8/8/8/8/R7/4K3 w - - 100 60');
assert.equal(fifty.isFiftyMoveDraw(), true);
// Threefold repetition ignoring non-capturable EP square vs respecting capturable EP square
const rep = new Chess('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1');
// Shuffle knights back and forth
const seq = ['Nf6', 'Nf3', 'Ng8', 'Ng1', 'Nf6', 'Nf3', 'Ng8', 'Ng1'];
for (let i = 0; i < seq.length - 1; i++) {
assert.ok(rep.moveSan(seq[i]!));
assert.equal(rep.isThreefoldRepetition(), false);
}
assert.ok(rep.moveSan(seq[seq.length - 1]!));
assert.equal(rep.isThreefoldRepetition(), true);
});
test('Undo restores exact FEN and repetition count', () => {
const c = new Chess(INITIAL_FEN);
const fens: string[] = [c.fen()];
const moves = ['e2e4', 'e7e5', 'g1f3', 'b8c6', 'f1b5', 'a7a6', 'b5a4', 'g8f6', 'e1g1'];
for (const uci of moves) {
assert.ok(c.moveUci(uci));
fens.push(c.fen());
}
for (let i = moves.length - 1; i >= 0; i--) {
c.undo();
assert.equal(c.fen(), fens[i]);
}
});