65 lines
2.6 KiB
TypeScript
65 lines
2.6 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { Chess, INITIAL_FEN } from '../src/shared/chess';
|
|
import { StockfishPool } from '../src/server/stockfish';
|
|
|
|
test('StockfishPool bot moves, MultiPV perspective normalization, cancellation, and full-game analysis', async () => {
|
|
const pool = new StockfishPool(2);
|
|
try {
|
|
await pool.warmup();
|
|
|
|
// 1. Bot Level 1 and Level 8 return legal UCI moves
|
|
const c = new Chess(INITIAL_FEN);
|
|
const lv1Move = await pool.getBotMove(c.fen(), 1);
|
|
assert.ok(c.legalMoves().some((m) => m.uci === lv1Move), `Illegal lv1 move: ${lv1Move}`);
|
|
|
|
const lv8Move = await pool.getBotMove(c.fen(), 8);
|
|
assert.ok(c.legalMoves().some((m) => m.uci === lv8Move), `Illegal lv8 move: ${lv8Move}`);
|
|
|
|
// 2. MultiPV on Black-to-move position where White is up a Queen -> scoreCp MUST be positive (White's perspective)!
|
|
const whiteUpQueenBlackToMove = 'rnb1kbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1';
|
|
const lines = await pool.analyzePositionMultiPv(whiteUpQueenBlackToMove, 8, 2);
|
|
assert.ok(lines.length >= 1);
|
|
assert.ok(
|
|
lines[0]!.scoreCp !== null && lines[0]!.scoreCp > 400,
|
|
`Expected White-perspective positive cp > 400, got ${lines[0]!.scoreCp}`
|
|
);
|
|
|
|
// 3. Cancellation works cleanly
|
|
const ac = new AbortController();
|
|
const cancelPromise = pool.analyzePositionMultiPv(INITIAL_FEN, 18, 3, ac.signal);
|
|
setTimeout(() => ac.abort(), 30);
|
|
const cancelledLines = await cancelPromise;
|
|
assert.ok(Array.isArray(cancelledLines));
|
|
|
|
// 4. Full-game analysis on Scholar's Mate
|
|
const gameChess = new Chess(INITIAL_FEN);
|
|
const uciList = ['e2e4', 'e7e5', 'd1h5', 'b8c6', 'f1c4', 'g8f6', 'h5f7'];
|
|
const sanList: string[] = [];
|
|
for (const u of uciList) {
|
|
const mv = gameChess.moveUci(u);
|
|
assert.ok(mv);
|
|
sanList.push(mv.san);
|
|
}
|
|
|
|
let sawProgress = false;
|
|
const report = await pool.analyzeFullGame('test-scholar', INITIAL_FEN, uciList, sanList, 10, (done, total) => {
|
|
assert.ok(done <= total);
|
|
sawProgress = true;
|
|
});
|
|
|
|
assert.equal(sawProgress, true);
|
|
assert.equal(report.status, 'done');
|
|
assert.equal(report.plies.length, 7);
|
|
assert.ok(report.whiteSummary && report.blackSummary);
|
|
// 3... Nf6?? allows 4. Qxf7# -> blunder!
|
|
const nf6Ply = report.plies[5]!;
|
|
assert.equal(nf6Ply.moveSan, 'Nf6');
|
|
assert.equal(nf6Ply.classification, 'blunder');
|
|
// 4. Qxf7# delivers mate -> 100% win percent after
|
|
const matePly = report.plies[6]!;
|
|
assert.equal(matePly.winPercentAfter, 100);
|
|
} finally {
|
|
pool.shutdown();
|
|
}
|
|
});
|