Repository navigation
Closed
Labels
severity: mediumEdge-case bugs or meaningful maintainability costtech-debtRefactoring and maintainability
Description
Summary
The decision "is the game over, and what is the result and the status text" is written twice in game.js: once after a move, and again for a position loaded from FEN. A third derivation is in saved-game.js. The copies have already started to differ.
Where
js/game.js:637-656(playMove: checkmate and stalemate after a move)js/game.js:664-671,js/game.js:1178-1181(endIfAutomaticDraw,findDrawReason)js/game.js:1536-1557(endIfAlreadyDecided: the same outcomes for a FEN start)js/saved-game.js:120(the result derived again fromlast.isCheckmate)
Details
- "Checkmate! X wins." is built at game.js:645 and again at :1541. "Stalemate! Game is a draw." appears at :651 and :1544.
- Insufficient material and the fifty-move rule are tested in
findDrawReasonand again by hand at :1546-1554. That copy has a literalhalfmoveClock >= 100and a hand-written status instead ofDraw by ${reason}. ${color === WHITE ? 'White' : 'Black'}is repeated about 10 times in game.js, next to clock.js's ownname().
Impact
A new draw rule, a wording change, or localisation has to be made in two or three places. If one is missed, a game loaded from FEN ends differently from the same position reached by play.
Suggested fix
Each step leaves the code working:
- Tests first: pin the result and status text for mate, stalemate, insufficient material and the fifty-move rule, both after a move and when starting from FEN. Include Black-to-move FEN starts, which are untested today:
R5k1/5ppp/8/8/8/8/8/6K1 b - - 0 1should give1-0and "Checkmate! White wins.", and a Black stalemate should give1/2-1/2. - Add a pure
gameOutcome(position, player, halfmoveClock)to rules.js that returns{ result, reason }or null, with unit tests and no callers yet. - Switch
endIfAlreadyDecidedto it, with onestatusForOutcomein game.js. - Switch the end-of-move block in
playMoveandendIfAutomaticDrawto it. - Have saved-game.js use the stored
gameResult, orgameOutcome, instead of re-deriving the result. - Add
sideName(color)to utils.js and replace the repeated ternaries.
Acceptance criteria
- One place decides game-end result and status text
- Black-to-move FEN cases above are tested
- Full suite passes with unchanged coverage thresholds
Getting started
- Relevant files:
js/game.js,js/rules.js,js/saved-game.js,tests/unit/draws-promotion.test.js,tests/unit/fen.test.js - How to run tests:
npm ci && npm test
Filed by repo-audit/full-audit at 73cd279. Severity: medium. Effort: M. audit-key: tech-debt|js/game.js|endIfAlreadyDecided
Metadata
Metadata
Assignees
Labels
severity: mediumEdge-case bugs or meaningful maintainability costtech-debtRefactoring and maintainability
Activity
TMHSDigital commentedon Nov 17, 2026
Fixed in fd6dc32:
gameOutcome(position, player, draws)in rules.js now decides mate, stalemate and the draws; game.js turns it into the result and status in onestatusForOutcome/endWith, used by playMove, endIfAutomaticDraw and endIfAlreadyDecided (findDrawReason removed).sideName()in utils.js replaces the White/Black ternaries in game.js, game-ui.js and clock.js. New tests pin FEN-vs-move results and wording (including Black-to-move mate and stalemate) and cover gameOutcome directly. Full suite (565), lint, format and coverage thresholds pass.One step left as is on purpose: saved-game.js still derives a result for old saves that predate stored results from the last move's isCheckmate flag. That path reads stored flags, not a position, and is pinned by the #141 tests; every save written now carries gameResult.
Remove isCheckmate and isStalemate, left unused by gameOutcome (#140)
Decide how a game ends in one place (TMHSDigital#140)