Skip to content

Leftover code and drifted duplicates across the js/ modules #107

Closed

Description

@TMHSDigital

Summary

A checklist of small leftovers and duplicates across the js/ modules. Each was read in place. One of them is a visible inconsistency: an armed "Discard game?" prompt on New Game survives a move, where an armed "Resign?" is cancelled.

Where and what

  • Two confirm-on-second-click implementations. New Game has its own (resetConfirmTimer, handleResetClick, cancelResetConfirm, js/game.js:1475); Resign and Load use confirmSecondClick (js/game.js:559). cancelConfirms() (js/game.js:584) runs on every move, undo and game end but does not know about the New Game button. Route New Game through confirmSecondClick, delete the private copy, and rename RESET_CONFIRM_MS (js/config.js:96), which times all three buttons.
  • Three ways to reach localStorage. saveToLocalStorage and loadFromLocalStorage in js/utils.js:90 and js/utils.js:108 (unused by the app), the readSetting and writeSetting closures in js/main.js:17, and raw try/catch blocks in js/game.js:1097 and js/game.js:1260. Pick one helper.
  • Helpers used only by tests. deepClone (js/utils.js:60), algebraicToCoords, generateAllLegalMoves (js/game.js:1013), canCastle and the isPromotionPending alias. Delete them or move them to the test helper, and drop them from appGlobals in eslint.config.mjs.
  • Two "can this side mate" rules in two layers. canDeliverMate in js/game.js:909 (flag fall) and hasInsufficientMaterial in js/rules.js:312. They disagree on same-shade bishops. Move the first into rules.js next to the second.
  • Redundant window exports. js/promotion.js:117 assigns three functions to window that nothing reads; js/drag.js:22 re-exports dragJustEnded, which js/game.js:159 then reads through window with a typeof guard.
  • drag.js reads game.js state variables directly (js/drag.js:97) where the architecture document says it uses functions.
  • Castling rights are re-derived from hasMoved flags in three places in js/rules.js (lines 155, 284, 404). One helper would do.
  • Stale comments and dead lines. js/pieces.js:113 says game.js adds castling through canCastle(); js/pieces.js:123 "Add functions here..."; js/game.js:8 describes a move record without its current fields; js/game.js:1716 says "basic disambiguation"; js/board.js:287 is commented-out code; the boardState parameter of updateBoardVisuals (js/board.js:223) is unused.
  • Tunables outside CONFIG. AI_DRAW_MARGIN (js/game.js:68), the low-time threshold 20000 (js/game.js:859), the button flash 1500 (js/game.js:1578), the drag threshold and click-swallow time in js/drag.js.
  • Theme values and timings duplicated between CSS and config.js (css/style.css:632, js/config.js:50, css/pieces.css:61, js/config.js:100).
  • Leftover lint ignores for Playwright output in eslint.config.mjs:135; Playwright is not a dependency.
  • The FEN download fallback uses the PGN MIME type (js/game.js:1582, application/x-chess-pgn for a .fen file).

Steps to reproduce (the confirm drift)

Against the computer: play a move, click New Game once, and let the computer reply within four seconds. The button still reads "Discard game?" and the next click discards the game. Do the same with Resign: the reply resets the button.

Acceptance criteria

  • Each box above is ticked or closed with a reason.
  • A test shows a move cancels an armed New Game prompt, matching the Resign test.
  • npm run lint and npm test pass.

Related: #30

Getting started

  • Relevant files: js/game.js, js/utils.js, js/main.js, js/rules.js, js/drag.js, js/promotion.js, eslint.config.mjs
  • How to run tests: npm ci, then npm test
  • Each box can be its own small pull request.

Filed by repo-audit/full-audit at ac3afa3. Severity: low. Effort: M. audit-key: tech-debt|js/game.js|leftovers-and-duplicates

Activity

  1. added
    help wanted
    tech-debt
    severity: low
    on Nov 15, 2026
  2. TMHSDigital commented on Nov 15, 2026

    @TMHSDigital
    OwnerAuthor

    Done in the commit that closed this issue ("Clear out leftovers and duplicates across the js/ modules"). Each box, ticked or closed with a reason:

    • Two confirm implementations. Ticked. New Game goes through confirmSecondClick; resetConfirmTimer, RESET_LABEL and cancelResetConfirm are gone, so cancelConfirms() now covers it. RESET_CONFIRM_MS is renamed CONFIRM_MS.
    • Three ways to reach localStorage. Ticked. readStored, writeStored and removeStored in utils.js are the one helper; main.js and game.js use them and saveToLocalStorage / loadFromLocalStorage are deleted.
    • Helpers used only by tests. deepClone, canCastle and the isPromotionPending alias are deleted (the tests call rules.canCastleIn directly). Kept, because app code now uses them: algebraicToCoords (reading FEN) and generateAllLegalMoves (a loaded position that is already decided).
    • Two "can this side mate" rules. Ticked. canDeliverMate is in rules.js next to hasInsufficientMaterial and now also says no for bishops that all stand on one colour.
    • Redundant window exports. The three in promotion.js are removed. dragJustEnded stays on window: it is the one documented shared flag and the drag tests read it there; game.js now reads it without the typeof guard.
    • drag.js reads game.js state. Ticked. It calls a new isOwnPiece(row, col); boardState and currentPlayer are no longer shared names in eslint.config.mjs.
    • Castling rights in three places. Ticked. hasCastlingRight(board, color, side) serves canCastleIn, positionKey and positionToFEN.
    • Stale comments and dead lines. Ticked: both comments in pieces.js, the move-record comment in game.js, the commented-out line in board.js and the unused boardState parameter of updateBoardVisuals. The "basic disambiguation" comment went with SAN disambiguation counts pinned pieces, so moves get an unneeded file or rank #99.
    • Tunables outside CONFIG. Ticked: AI_DRAW_MARGIN, AI_HURRY_BELOW_MS, NOTICE_MS, DRAG_THRESHOLD_PX, DRAG_CLICK_GUARD_MS; the low-time colour uses LOW_TIME_WARNINGS.
    • Theme values and timings in CSS and config.js. Closed with a reason: the stylesheet has to carry the defaults so the first paint is right before any script runs, and there is no build step to generate one from the other. Two new tests in tests/unit/themes.test.js fail if the copies drift apart.
    • Lint ignores for Playwright output. Closed with a reason: they mirror the entries in .gitignore, and .playwright-mcp/ is real local output of browser checks on this repository.
    • FEN download MIME type. Ticked: a .fen file is offered as text/plain.

    Verified: a new test in tests/unit/controls.test.js arms "Discard game?", lets the computer reply, and checks the button is back to "New Game" and that the next click asks again; it failed on the code before this change ("Discard game?" still showing). Further tests cover the storage helpers including blocked storage, canDeliverMate, and the file types. The perft counts and FEN round trips pass with the castling helper. Full suite, npm run lint and the format check pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedseverity: lowtech-debt

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions