import assert from 'node:assert/strict'; import test from 'node:test'; import { Chess, algToSq } from '../src/shared/chess'; import { MAX_PREMOVES, type QueuedPremove, applyPremoveQueueToBoard, computePremoveTargets, resolveQueuedPremove } from '../src/shared/premove'; test('MAX_PREMOVES is 25', () => { assert.equal(MAX_PREMOVES, 25); }); 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('Chained multi-premove targets project board state across consecutive moves', () => { const c = new Chess('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1'); const queue: QueuedPremove[] = [ { from: algToSq('d2'), to: algToSq('d4') }, { from: algToSq('d4'), to: algToSq('d5') } ]; // After queueing d2->d4 and d4->d5, pawn is projected at d5 and d2 is empty const projected = applyPremoveQueueToBoard(c.board(), queue); assert.equal(projected[algToSq('d2')], null); assert.deepEqual(projected[algToSq('d5')], { color: 'w', type: 'p' }); // From d5, pawn can premove d6 (1-step), c6, e6 — NOT d7 (2-step only from start rank) const d5Targets = computePremoveTargets(c, algToSq('d5'), queue); assert.equal(d5Targets.has(algToSq('d6')), true); assert.equal(d5Targets.has(algToSq('c6')), true); assert.equal(d5Targets.has(algToSq('e6')), true); assert.equal(d5Targets.has(algToSq('d7')), false); }); 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')); 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 in premove queue moves rook on projected board and disables repeat castling', () => { 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); // Queue O-O (e1->g1): projected board places White king on g1 and White rook on f1 const queueAfterCastle: QueuedPremove[] = [{ from: algToSq('e1'), to: algToSq('g1') }]; const projected = applyPremoveQueueToBoard(withRights.board(), queueAfterCastle); assert.deepEqual(projected[algToSq('g1')], { color: 'w', type: 'k' }); assert.deepEqual(projected[algToSq('f1')], { color: 'w', type: 'r' }); assert.equal(projected[algToSq('h1')], null); // Rook on f1 can now premove along the f-file const f1Targets = computePremoveTargets(withRights, algToSq('f1'), queueAfterCastle); assert.equal(f1Targets.has(algToSq('f8')), true); // Once king moves in queue, e1->c1 castling is no longer offered even if projected back const movedBackQueue: QueuedPremove[] = [ { from: algToSq('e1'), to: algToSq('f1') }, { from: algToSq('f1'), to: algToSq('e1') } ]; const e1AfterMove = computePremoveTargets(withRights, algToSq('e1'), movedBackQueue); assert.equal(e1AfterMove.has(algToSq('g1')), false); 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); }); test('Multi-premove queue can queue 25 moves and executes sequentially until completion or illegality', () => { const c = new Chess('4k3/8/8/8/8/8/R7/4K3 b - - 0 1'); const queue: QueuedPremove[] = []; for (let i = 0; i < MAX_PREMOVES; i++) { queue.push(i % 2 === 0 ? { from: algToSq('a2'), to: algToSq('a3') } : { from: algToSq('a3'), to: algToSq('a2') }); } assert.equal(queue.length, 25); // Black plays Ke7 -> White executes premove 0 (a2->a3) -> Black plays Kd7 -> White executes premove 1 (a3->a2) assert.ok(c.moveSan('Ke7')); const uci0 = resolveQueuedPremove(c, queue[0]!); assert.equal(uci0, 'a2a3'); assert.ok(c.moveUci(uci0!)); assert.ok(c.moveSan('Kd7')); const uci1 = resolveQueuedPremove(c, queue[1]!); assert.equal(uci1, 'a3a2'); assert.ok(c.moveUci(uci1!)); });