fix: promotion dialog only fires for genuine pawn promotions

The "Promote pawn" dialog popped for any pawn "moved" toward the last
rank: the commit paths checked piece type + destination rank but never
the pawn's SOURCE rank. With the phantom layer now filling ranks 7-8
with tappable phantom pieces, tapping one (which falls through to the
real-move handler) while a real pawn was armed triggered the dialog for
a move no pawn could make — and for any phantom type, not just pawns.

Root cause: incomplete promotion detection, duplicated in Game.svelte
`onCommit` and the server's `isPromotionRequired`. Replaced with one
shared `isPromotionMove(piece, from, to)` — pawn, from the rank adjacent
to promotion, to the promotion rank, at most one file over — used by
both. 7 unit tests in packages/shared.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
claude (blind_chess)
2026-05-18 21:45:42 -04:00
parent 5d995eb428
commit c01244c850
5 changed files with 77 additions and 13 deletions
+8 -8
View File
@@ -9,7 +9,7 @@
import { pieceGlyph } from './pieces.js';
import { phantoms } from './stores/phantoms.svelte.js';
import { phantomDrag } from './stores/phantom-drag.svelte.js';
import type { PromotionType, Square } from '@blind-chess/shared';
import { isPromotionMove, type PromotionType, type Square } from '@blind-chess/shared';
interface Props { gameId: string; }
let { gameId }: Props = $props();
@@ -40,13 +40,13 @@
function onCommit(from: Square, to: Square) {
const piece = game.state.view?.pieces[from];
if (!piece) return;
// Promotion check (white pawn to rank 8, black pawn to rank 1).
if (piece.type === 'p') {
const rank = to[1];
if ((piece.color === 'w' && rank === '8') || (piece.color === 'b' && rank === '1')) {
pendingPromotion = { from, to };
return;
}
// A pawn promotes only from the rank adjacent to its promotion rank
// isPromotionMove checks the source rank, destination rank, and file delta,
// so a pawn elsewhere "moved" toward the last rank no longer pops this
// dialog (which a click on a phantom-occupied back-rank square could do).
if (isPromotionMove(piece, from, to)) {
pendingPromotion = { from, to };
return;
}
game.commit(from, to);
}