Extract + unit-test the padding-aware cell geometry (Codex nice-to-have)

Pulled the pointer→cell math out of cellAt() into a pure cellFromPoint(rect, x,
y, n) in $lib/wordsearch.js (only getBoundingClientRect stays in the component),
and covered it with vitest — including the last-column case that was drifting
under the old overflowing layout, plus clamping and a scrolled-origin rect.
11 vitest tests now; real-device testing remains the final validator.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
jay
2026-06-10 21:43:28 -04:00
parent b909b7e64b
commit 98441fae15
3 changed files with 42 additions and 8 deletions
@@ -1,6 +1,6 @@
<script>
import { getJSON } from '$lib/api.js';
import { lineFrom, matchWord } from '$lib/wordsearch.js';
import { lineFrom, matchWord, cellFromPoint } from '$lib/wordsearch.js';
let { size = 'med', onstatus } = $props();
@@ -86,12 +86,7 @@
function summary() { return { game: 'wordsearch', size, date, status, found: foundWords.length, total: words.length, ms: resultMs }; }
function cellAt(e) {
const rect = gridEl.getBoundingClientRect();
const pad = 7; // grid padding (6) + border (1)
const cw = (rect.width - 2 * pad) / n; // even 1fr columns share the inner width
const c = Math.min(n - 1, Math.max(0, Math.floor((e.clientX - rect.left - pad) / cw)));
const r = Math.min(n - 1, Math.max(0, Math.floor((e.clientY - rect.top - pad) / cw)));
return [r, c];
return cellFromPoint(gridEl.getBoundingClientRect(), e.clientX, e.clientY, n);
}
function down(e) {