Switch chessboard to tap-tap interaction and fix/enhance premoving

This commit is contained in:
tyuXX 2026-11-22 19:13:24 +03:00
parent e2e085b341
commit 02b8bafb1e
6 changed files with 364 additions and 294 deletions

56
tests/premove.test.ts Normal file
View file

@ -0,0 +1,56 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { Chess, algToSq } from '../src/shared/chess';
import { computePremoveTargets, resolveQueuedPremove } from '../src/shared/premove';
test('Pawn premove targets include 1-step, 2-step from start, diagonal captures, and promotions', () => {
const c = new Chess('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1');
const d2Targets = computePremoveTargets(c, algToSq('d2'));
assert.equal(d2Targets.has(algToSq('d3')), true);
assert.equal(d2Targets.has(algToSq('d4')), true);
assert.equal(d2Targets.has(algToSq('c3')), true);
assert.equal(d2Targets.has(algToSq('e3')), true);
assert.equal(d2Targets.has(algToSq('d5')), false);
const promoChess = new Chess('4k3/2P5/8/8/8/8/8/4K3 b - - 0 1');
const c7Targets = computePremoveTargets(promoChess, algToSq('c7'));
assert.deepEqual(c7Targets.get(algToSq('c8')), ['q', 'r', 'b', 'n']);
assert.deepEqual(c7Targets.get(algToSq('b8')), ['q', 'r', 'b', 'n']);
assert.deepEqual(c7Targets.get(algToSq('d8')), ['q', 'r', 'b', 'n']);
});
test('Slider premove targets ignore blockers and resolve recapture when turn returns', () => {
// Black to move: Black bishop on g4 can take White knight on f3; White queen on d1 premoves Qxf3
const c = new Chess('r2qkbnr/ppp2ppp/2np4/4p3/4P1b1/3P1N2/PPP2PPP/RNBQKB1R b KQkq - 0 4');
const qTargets = computePremoveTargets(c, algToSq('d1'));
// Even though f3 is currently occupied by own knight and e2 is occupied by own pawn, queen can premove d1->f3 and d1->h5
assert.equal(qTargets.has(algToSq('f3')), true);
assert.equal(qTargets.has(algToSq('h5')), true);
// If Black plays Bxf3, White's queued premove d1->f3 becomes legal and resolves
c.moveUci('g4f3');
const resolved = resolveQueuedPremove(c, { from: algToSq('d1'), to: algToSq('f3') });
assert.equal(resolved, 'd1f3');
// If Black instead plays a6, f3 is still occupied by White's own knight -> premove d1->f3 is dropped (null)
c.undo();
c.moveUci('a7a6');
const dropped = resolveQueuedPremove(c, { from: algToSq('d1'), to: algToSq('f3') });
assert.equal(dropped, null);
});
test('King castling premove targets respect FEN castling rights and check resolution', () => {
const withRights = new Chess('r3k2r/8/8/8/8/8/8/R3K2R b KQkq - 0 1');
const kTargets = computePremoveTargets(withRights, algToSq('e1'));
assert.equal(kTargets.has(algToSq('g1')), true);
assert.equal(kTargets.has(algToSq('c1')), true);
const noRights = new Chess('r3k2r/8/8/8/8/8/8/R3K2R b - - 0 1');
const kNoTargets = computePremoveTargets(noRights, algToSq('e1'));
assert.equal(kNoTargets.has(algToSq('g1')), false);
assert.equal(kNoTargets.has(algToSq('c1')), false);
// If Black puts White in check and White had premoved a non-evading pawn push, resolveQueuedPremove returns null
const checkGame = new Chess('4k3/8/8/8/8/8/4P3/4K2r w - - 0 1');
assert.equal(resolveQueuedPremove(checkGame, { from: algToSq('e2'), to: algToSq('e4') }), null);
});