Keyboard support #12

Closed
opened 2026-11-21 01:41:56 +00:00 by lab-admin · 31 comments
Owner

Make the calculator fully usable from the keyboard.

Requirements

  • Digits 0-9 and . enter numbers; +, -, *, / choose add, subtract, multiply, divide; % chooses percent.
  • Enter or = computes; Escape clears (like C); Backspace deletes the last entered character.
  • Keyboard input goes through the same code paths as the buttons (no duplicated calculation logic in the page).
  • Tests that exercise the page script's key handling (for example by loading the script with a minimal DOM stub, or by extracting the key mapping into a testable function).

Acceptance criteria

  1. Typing 1, 2, +, 3, Enter shows 15.
  2. Escape resets the display to 0.
  3. Backspace turns 123 into 12.
  4. *, /, - and % select the matching operations.
Make the calculator fully usable from the keyboard. ## Requirements - Digits `0-9` and `.` enter numbers; `+`, `-`, `*`, `/` choose add, subtract, multiply, divide; `%` chooses percent. - `Enter` or `=` computes; `Escape` clears (like `C`); `Backspace` deletes the last entered character. - Keyboard input goes through the same code paths as the buttons (no duplicated calculation logic in the page). - Tests that exercise the page script's key handling (for example by loading the script with a minimal DOM stub, or by extracting the key mapping into a testable function). ## Acceptance criteria 1. Typing `1`, `2`, `+`, `3`, `Enter` shows `15`. 2. `Escape` resets the display to `0`. 3. `Backspace` turns `123` into `12`. 4. `*`, `/`, `-` and `%` select the matching operations.
Collaborator

Aira · queued

Aira picked up this task. Branch aira/12-keyboard-support.

**Aira · queued** Aira picked up this task. Branch `aira/12-keyboard-support`.
Collaborator

Aira · planning

Orchestrator thread started (composer-2.5). Planning on aira/12-keyboard-support.

T3 thread: mcp:launch:f0cb46248edbd26b8331d66f0027e85a98f438051c5a62ec324268100e8a9df5

**Aira · planning** Orchestrator thread started (composer-2.5). Planning on `aira/12-keyboard-support`. T3 thread: `mcp:launch:f0cb46248edbd26b8331d66f0027e85a98f438051c5a62ec324268100e8a9df5`
Collaborator

Aira · Plan (round 1) at 49e0a054

Added docs/plans/issue-12.md (keyboard refactor: handlePadKey, mapKeyboardKey, backspaceEntry, calculator-ui.js extraction, test plan for AC1–4); committed and pushed to aira/12-keyboard-support; npm test green (70 tests).

docs/plans/issue-12.md

Issue #12: Keyboard support

Goal

Make the calculator fully usable from the keyboard. Key presses must use the same UI handlers as on-screen buttons (digits, operators, equals, clear) without duplicating calculation or API logic. Add Backspace to delete the last character of the current entry.

Acceptance criteria

  1. Typing 1, 2, +, 3, Enter shows 15.
  2. Escape resets the display to 0.
  3. Backspace turns 123 into 12.
  4. *, /, - and % select the matching operations (multiply, divide, subtract, percent).

Design

Refactor: single keypad dispatcher

Replace the inline if/else chain in the #keypad click listener with handlePadKey(key), where key uses the same values as button[data-key] today:

key Action (existing)
0–9 appendDigit
. appendDecimal
+ commitOperand('add')
− commitOperand('subtract')
× commitOperand('multiply')
÷ commitOperand('divide')
% commitOperand('percent')
√ applyUnary('sqrt')
= evaluate()
C reset()
  • Click: resolve data-key from the clicked button and call handlePadKey(key).
  • Keyboard: map event.key and call handlePadKey (or backspaceEntry for Backspace).

Keyboard mapping

Add pure function mapKeyboardKey(eventKey) (testable in Node):

event.key Maps to
0–9, ., +, % same pad key
- −
* ×
/ ÷
= =
Enter =
Escape C
Backspace sentinel 'Backspace' (not a button data-key)

Return null for unmapped keys. On keydown, skip when ctrlKey, metaKey, or altKey is set. When a key is handled, call event.preventDefault().

Backspace (new helper)

backspaceEntry():

  • No change if freshEntry is true or entry === '0'.
  • Otherwise drop the last character of entry; if empty, set entry to '0'.
  • Update the display via setDisplay(entry) (no API call).

Script extraction (public/calculator-ui.js)

Move the inline <script> from index.html into public/calculator-ui.js so tests can load the real page logic with a minimal DOM stub:

(function (root) {
  function mapKeyboardKey(key) { /* … */ }
  function initCalculator(doc) { /* state, handlePadKey, backspaceEntry, listeners */ }
  if (typeof module !== 'undefined' && module.exports) {
    module.exports = { mapKeyboardKey, initCalculator };
  } else {
    initCalculator(document);
  }
})(typeof globalThis !== 'undefined' ? globalThis : window);

index.html loads <script src="calculator-ui.js"></script> only (no inline calculator logic).

initCalculator registers document.addEventListener('keydown', …):

  • mapped === 'Backspace' → backspaceEntry()
  • otherwise handlePadKey(mapped) when mapKeyboardKey returns a pad key.

Server / calculation layer

No changes to lib/calculator.js or server.js. evaluate() continues to call /api/calculate; keyboard Enter/= only triggers handlePadKey('=').

Test layout

File Purpose
test/calculator-ui.test.js require exports; stub document / fetch; AC1–4 and backspace behaviour.
test/ui.test.js Static checks: external script, keydown, shared handlePadKey.

Files to change

File Action
public/calculator-ui.js Create — UI state machine, mapKeyboardKey, handlePadKey, backspaceEntry, initCalculator, dual browser/Node export.
public/index.html Modify — drop inline script; reference calculator-ui.js.
test/calculator-ui.test.js Create — key mapping and display flows (AC1–4).
test/ui.test.js Modify — script tag, keyboard wiring, regressions.
docs/plans/issue-12.md Create — this plan.

No changes to lib/calculator.js, server.js, package.json, or CI workflows.

Test plan

UI logic (test/calculator-ui.test.js)

Use node:test with a minimal DOM stub (#display, #keypad, #history-list, #clear-history) and a fetch mock that returns { result: 15 } for a=12&b=3&op=add.

Case Steps Expect
AC1 (equals) handlePadKey('1'..'3'), '=' display 15
AC1 (Enter) same sequence; final step via mapKeyboardKey('Enter') → handlePadKey('=') display 15
AC2 after input, mapKeyboardKey('Escape') → handlePadKey('C') display 0
AC3 1,2,3; backspaceEntry() display 12; again → 1
AC4 mapKeyboardKey for -, *, /, % maps to −, ×, ÷, %; after digit + op, internal pendingOp is subtract / multiply / divide / percent
Map negatives mapKeyboardKey('q') null
Enter / Escape / Backspace unit asserts on return values per mapping table
Regression handlePadKey('C') display 0

Page (test/ui.test.js)

Case Assert
External script index.html includes calculator-ui.js
Keyboard calculator-ui.js defines keydown listener and mapKeyboardKey
Single dispatch handlePadKey used from click handler and keyboard path
Regression Required buttons, history hooks, /api/calculate usage unchanged

HTTP (test/server.test.js)

No new tests required; existing add/multiply/etc. cases regress the API used by AC1.

Pre-push

Run npm test locally before git push origin aira/12-keyboard-support.

Risks

Risk Mitigation
Script extraction breaks history, sqrt, or binary ops Keep handlePadKey logic equivalent to current click handler; run full test suite.
US keyboard symbols vs Unicode button labels (−, ×, ÷) Central mapKeyboardKey; explicit AC4 tests.
Backspace after choosing an operator (freshEntry) No-op when entry is fresh 0; document in tests.
preventDefault interferes with browser shortcuts Ignore modified key events; only prevent when mapping is non-null.
Tests need async evaluate Use async test callbacks and await handlePadKey('=') if evaluate is async.

Attachment: plan-12-r1.md

**Aira · Plan (round 1) at `49e0a054`** Added docs/plans/issue-12.md (keyboard refactor: handlePadKey, mapKeyboardKey, backspaceEntry, calculator-ui.js extraction, test plan for AC1–4); committed and pushed to aira/12-keyboard-support; npm test green (70 tests). <details><summary>docs/plans/issue-12.md</summary> # Issue #12: Keyboard support ## Goal Make the calculator fully usable from the keyboard. Key presses must use the same UI handlers as on-screen buttons (digits, operators, equals, clear) without duplicating calculation or API logic. Add Backspace to delete the last character of the current entry. ## Acceptance criteria 1. Typing `1`, `2`, `+`, `3`, `Enter` shows `15`. 2. `Escape` resets the display to `0`. 3. `Backspace` turns `123` into `12`. 4. `*`, `/`, `-` and `%` select the matching operations (multiply, divide, subtract, percent). ## Design ### Refactor: single keypad dispatcher Replace the inline `if/else` chain in the `#keypad` click listener with `handlePadKey(key)`, where `key` uses the same values as `button[data-key]` today: | `key` | Action (existing) | |-------|-------------------| | `0`–`9` | `appendDigit` | | `.` | `appendDecimal` | | `+` | `commitOperand('add')` | | `−` | `commitOperand('subtract')` | | `×` | `commitOperand('multiply')` | | `÷` | `commitOperand('divide')` | | `%` | `commitOperand('percent')` | | `√` | `applyUnary('sqrt')` | | `=` | `evaluate()` | | `C` | `reset()` | - **Click**: resolve `data-key` from the clicked button and call `handlePadKey(key)`. - **Keyboard**: map `event.key` and call `handlePadKey` (or `backspaceEntry` for Backspace). ### Keyboard mapping Add pure function `mapKeyboardKey(eventKey)` (testable in Node): | `event.key` | Maps to | |-------------|---------| | `0`–`9`, `.`, `+`, `%` | same pad key | | `-` | `−` | | `*` | `×` | | `/` | `÷` | | `=` | `=` | | `Enter` | `=` | | `Escape` | `C` | | `Backspace` | sentinel `'Backspace'` (not a button `data-key`) | Return `null` for unmapped keys. On `keydown`, skip when `ctrlKey`, `metaKey`, or `altKey` is set. When a key is handled, call `event.preventDefault()`. ### Backspace (new helper) `backspaceEntry()`: - No change if `freshEntry` is true or `entry === '0'`. - Otherwise drop the last character of `entry`; if empty, set `entry` to `'0'`. - Update the display via `setDisplay(entry)` (no API call). ### Script extraction (`public/calculator-ui.js`) Move the inline `<script>` from `index.html` into `public/calculator-ui.js` so tests can load the real page logic with a minimal DOM stub: ```js (function (root) { function mapKeyboardKey(key) { /* … */ } function initCalculator(doc) { /* state, handlePadKey, backspaceEntry, listeners */ } if (typeof module !== 'undefined' && module.exports) { module.exports = { mapKeyboardKey, initCalculator }; } else { initCalculator(document); } })(typeof globalThis !== 'undefined' ? globalThis : window); ``` `index.html` loads `<script src="calculator-ui.js"></script>` only (no inline calculator logic). `initCalculator` registers `document.addEventListener('keydown', …)`: - `mapped === 'Backspace'` → `backspaceEntry()` - otherwise `handlePadKey(mapped)` when `mapKeyboardKey` returns a pad key. ### Server / calculation layer No changes to `lib/calculator.js` or `server.js`. `evaluate()` continues to call `/api/calculate`; keyboard `Enter`/`=` only triggers `handlePadKey('=')`. ### Test layout | File | Purpose | |------|---------| | `test/calculator-ui.test.js` | `require` exports; stub `document` / `fetch`; AC1–4 and backspace behaviour. | | `test/ui.test.js` | Static checks: external script, `keydown`, shared `handlePadKey`. | ## Files to change | File | Action | |------|--------| | `public/calculator-ui.js` | **Create** — UI state machine, `mapKeyboardKey`, `handlePadKey`, `backspaceEntry`, `initCalculator`, dual browser/Node export. | | `public/index.html` | **Modify** — drop inline script; reference `calculator-ui.js`. | | `test/calculator-ui.test.js` | **Create** — key mapping and display flows (AC1–4). | | `test/ui.test.js` | **Modify** — script tag, keyboard wiring, regressions. | | `docs/plans/issue-12.md` | **Create** — this plan. | No changes to `lib/calculator.js`, `server.js`, `package.json`, or CI workflows. ## Test plan ### UI logic (`test/calculator-ui.test.js`) Use `node:test` with a minimal DOM stub (`#display`, `#keypad`, `#history-list`, `#clear-history`) and a `fetch` mock that returns `{ result: 15 }` for `a=12&b=3&op=add`. | Case | Steps | Expect | |------|--------|--------| | AC1 (equals) | `handlePadKey('1'..'3')`, `'='` | display `15` | | AC1 (Enter) | same sequence; final step via `mapKeyboardKey('Enter')` → `handlePadKey('=')` | display `15` | | AC2 | after input, `mapKeyboardKey('Escape')` → `handlePadKey('C')` | display `0` | | AC3 | `1`,`2`,`3`; `backspaceEntry()` | display `12`; again → `1` | | AC4 | `mapKeyboardKey` for `-`, `*`, `/`, `%` | maps to `−`, `×`, `÷`, `%`; after digit + op, internal `pendingOp` is `subtract` / `multiply` / `divide` / `percent` | | Map negatives | `mapKeyboardKey('q')` | `null` | | Enter / Escape / Backspace | unit asserts on return values | per mapping table | | Regression | `handlePadKey('C')` | display `0` | ### Page (`test/ui.test.js`) | Case | Assert | |------|--------| | External script | `index.html` includes `calculator-ui.js` | | Keyboard | `calculator-ui.js` defines `keydown` listener and `mapKeyboardKey` | | Single dispatch | `handlePadKey` used from click handler and keyboard path | | Regression | Required buttons, history hooks, `/api/calculate` usage unchanged | ### HTTP (`test/server.test.js`) No new tests required; existing add/multiply/etc. cases regress the API used by AC1. ### Pre-push Run `npm test` locally before `git push origin aira/12-keyboard-support`. ## Risks | Risk | Mitigation | |------|------------| | Script extraction breaks history, sqrt, or binary ops | Keep `handlePadKey` logic equivalent to current click handler; run full test suite. | | US keyboard symbols vs Unicode button labels (`−`, `×`, `÷`) | Central `mapKeyboardKey`; explicit AC4 tests. | | Backspace after choosing an operator (`freshEntry`) | No-op when entry is fresh `0`; document in tests. | | `preventDefault` interferes with browser shortcuts | Ignore modified key events; only prevent when mapping is non-null. | | Tests need async `evaluate` | Use `async` test callbacks and await `handlePadKey('=')` if `evaluate` is async. | </details> Attachment: [plan-12-r1.md](https://aira-lab.cluster-dev.spkey.co/attachments/e54a3707-8bf3-44a5-99f6-1be6ec8f4a56)
Collaborator

Aira · plan review

Plan pushed at 49e0a054; independent Dodik plan review started.

**Aira · plan review** Plan pushed at `49e0a054`; independent Dodik plan review started.
Collaborator

Aira · dodik-plan FAIL on 49e0a054

The plan's acceptance criteria match the task, and the keypad dispatcher matches the current button handlers and Unicode operator labels. It still fails because all UI logic moves to public/calculator-ui.js while server.js is left unchanged, and that server only serves index.html at / and returns 404 for every other path, so the browser never loads the script.

  • [blocker] Extracted UI script is never served (docs/plans/issue-12.md): server.js serves public/index.html only for GET / and returns 404 JSON for every other path. The plan moves the whole page script to public/calculator-ui.js, references it with , and states that server.js must not change. The browser request for /calculator-ui.js will 404, so keypad, history, and keyboard handlers never run. Serve public/calculator-ui.js (for example GET /calculator-ui.js with a JavaScript content type), add an HTTP test that the body is the script, and remove the no-change rule for server.js. The CI smoke check only greps HTML at / and will not catch a missing script.
  • [minor] AC4 state is not exported (docs/plans/issue-12.md): The module sketch exports only mapKeyboardKey and initCalculator, while the test plan calls handlePadKey and backspaceEntry and asserts internal pendingOp. State the return value of initCalculator (those functions plus a way to read pendingOp), or assert AC4 by recording the op query on the mocked /api/calculate request.
  • [minor] UI regression checks still target inline HTML (test/ui.test.js): test/ui.test.js reads public/index.html for /api/calculate, commitOperand(...), key === 'C' followed by reset(), refreshHistory, and the history DELETE fetch. Those strings leave index.html with the script extraction, and the C-key regex will not match a handlePadKey dispatcher. Point button markup checks at index.html and behavior checks at calculator-ui.js.
Checks performed
  • Mapped task requirements (digits, decimal, operators, Enter/=, Escape, Backspace, shared handlers, page-script tests) onto numbered criteria 1-4 plus the mapping table and test plan.
  • Compared the handlePadKey action table with the inline click handler in public/index.html, including data-key values U+2212, U+00D7, and U+00F7.
  • Read server.js: GET / serves only public/index.html; /health, /api/calculate, and /api/history are the other routes; any other path returns 404 JSON.
  • Read test/ui.test.js, test/server.test.js, and lib/calculator.js. Existing HTTP tests cover add, subtract, multiply, divide, and percent. Existing UI tests assert inline script strings inside index.html. Leaving lib/calculator.js and the calculate API unchanged matches the task.

Attachment: dodik-plan-49e0a054.json

**Aira · dodik-plan FAIL on `49e0a054`** The plan's acceptance criteria match the task, and the keypad dispatcher matches the current button handlers and Unicode operator labels. It still fails because all UI logic moves to public/calculator-ui.js while server.js is left unchanged, and that server only serves index.html at / and returns 404 for every other path, so the browser never loads the script. - [blocker] Extracted UI script is never served (docs/plans/issue-12.md): server.js serves public/index.html only for GET / and returns 404 JSON for every other path. The plan moves the whole page script to public/calculator-ui.js, references it with <script src="calculator-ui.js"></script>, and states that server.js must not change. The browser request for /calculator-ui.js will 404, so keypad, history, and keyboard handlers never run. Serve public/calculator-ui.js (for example GET /calculator-ui.js with a JavaScript content type), add an HTTP test that the body is the script, and remove the no-change rule for server.js. The CI smoke check only greps HTML at / and will not catch a missing script. - [minor] AC4 state is not exported (docs/plans/issue-12.md): The module sketch exports only mapKeyboardKey and initCalculator, while the test plan calls handlePadKey and backspaceEntry and asserts internal pendingOp. State the return value of initCalculator (those functions plus a way to read pendingOp), or assert AC4 by recording the op query on the mocked /api/calculate request. - [minor] UI regression checks still target inline HTML (test/ui.test.js): test/ui.test.js reads public/index.html for /api/calculate, commitOperand(...), key === 'C' followed by reset(), refreshHistory, and the history DELETE fetch. Those strings leave index.html with the script extraction, and the C-key regex will not match a handlePadKey dispatcher. Point button markup checks at index.html and behavior checks at calculator-ui.js. <details><summary>Checks performed</summary> - Mapped task requirements (digits, decimal, operators, Enter/=, Escape, Backspace, shared handlers, page-script tests) onto numbered criteria 1-4 plus the mapping table and test plan. - Compared the handlePadKey action table with the inline click handler in public/index.html, including data-key values U+2212, U+00D7, and U+00F7. - Read server.js: GET / serves only public/index.html; /health, /api/calculate, and /api/history are the other routes; any other path returns 404 JSON. - Read test/ui.test.js, test/server.test.js, and lib/calculator.js. Existing HTTP tests cover add, subtract, multiply, divide, and percent. Existing UI tests assert inline script strings inside index.html. Leaving lib/calculator.js and the calculate API unchanged matches the task. </details> Attachment: [dodik-plan-49e0a054.json](https://aira-lab.cluster-dev.spkey.co/attachments/0787b852-db03-44e7-9558-b018a57ad7ef)
Collaborator

Aira · planning

Dodik rejected the plan; revision round 1.

**Aira · planning** Dodik rejected the plan; revision round 1.
Collaborator

Aira · Plan (round 2) at 9fd89dbf

Revised docs/plans/issue-12.md per Dodik: GET /calculator-ui.js in server.js with HTTP test; initCalculator returns handlePadKey/backspaceEntry/getDisplayText; AC4 via mocked fetch op param; ui.test.js split (markup in index.html, behavior in calculator-ui.js). Committed and pushed.

docs/plans/issue-12.md

Issue #12: Keyboard support

Goal

Make the calculator fully usable from the keyboard. Key presses must use the same UI handlers as on-screen buttons (digits, operators, equals, clear) without duplicating calculation or API logic. Add Backspace to delete the last character of the current entry.

Acceptance criteria

  1. Typing 1, 2, +, 3, Enter shows 15.
  2. Escape resets the display to 0.
  3. Backspace turns 123 into 12.
  4. *, /, - and % select the matching operations (multiply, divide, subtract, percent).

Design

Refactor: single keypad dispatcher

Replace the inline if/else chain in the #keypad click listener with handlePadKey(key), where key uses the same values as button[data-key] today:

key Action (existing)
0–9 appendDigit
. appendDecimal
+ commitOperand('add')
− commitOperand('subtract')
× commitOperand('multiply')
÷ commitOperand('divide')
% commitOperand('percent')
√ applyUnary('sqrt')
= evaluate()
C reset()
  • Click: resolve data-key from the clicked button and call handlePadKey(key).
  • Keyboard: map event.key and call handlePadKey (or backspaceEntry for Backspace).

Keyboard mapping

Add pure function mapKeyboardKey(eventKey) (testable in Node):

event.key Maps to
0–9, ., +, % same pad key
- −
* ×
/ ÷
= =
Enter =
Escape C
Backspace sentinel 'Backspace' (not a button data-key)

Return null for unmapped keys. On keydown, skip when ctrlKey, metaKey, or altKey is set. When a key is handled, call event.preventDefault().

Backspace (new helper)

backspaceEntry():

  • No change if freshEntry is true or entry === '0'.
  • Otherwise drop the last character of entry; if empty, set entry to '0'.
  • Update the display via setDisplay(entry) (no API call).

Script extraction (public/calculator-ui.js)

Move the inline <script> from index.html into public/calculator-ui.js so tests load the same logic the browser runs.

Dual-entry pattern:

(function (root) {
  function mapKeyboardKey(key) { /* … */ }
  function initCalculator(doc) {
    /* state, handlePadKey, backspaceEntry, listeners */
    return {
      mapKeyboardKey,
      handlePadKey,
      backspaceEntry,
      getDisplayText: () => display.textContent,
    };
  }
  if (typeof module !== 'undefined' && module.exports) {
    module.exports = { mapKeyboardKey, initCalculator };
  } else {
    initCalculator(document);
  }
})(typeof globalThis !== 'undefined' ? globalThis : window);

initCalculator return value (used by Node tests): { mapKeyboardKey, handlePadKey, backspaceEntry, getDisplayText }. No exposure of pendingOp on the public test surface.

index.html loads <script src="calculator-ui.js"></script> only (no inline calculator logic).

initCalculator registers doc.addEventListener('keydown', …):

  • mapped === 'Backspace' → backspaceEntry()
  • otherwise handlePadKey(mapped) when mapKeyboardKey returns a pad key.

HTTP: serve static UI script (server.js)

Today server.js only serves GET / as HTML; any other path (including /calculator-ui.js) returns 404 JSON. That must change so the extracted script loads in the browser.

  • Add constant CALCULATOR_UI = path.join(__dirname, 'public', 'calculator-ui.js') (alongside existing INDEX).
  • GET /calculator-ui.js: 200, headers content-type: application/javascript; charset=utf-8, body fs.readFileSync(CALCULATOR_UI).
  • Keep /health, /api/calculate, /api/history, DELETE /api/history, and GET / unchanged. Unlisted paths still 404 JSON.

lib/calculator.js is unchanged. evaluate() still calls /api/calculate; keyboard Enter/= only triggers handlePadKey('=').

Test layout

File Purpose
test/calculator-ui.test.js require exports; stub document / fetch; AC1–4 and backspace behaviour.
test/server.test.js GET /calculator-ui.js returns script bytes and JS content type.
test/ui.test.js Markup in index.html; behaviour wiring in calculator-ui.js.

Files to change

File Action
public/calculator-ui.js Create — UI state machine, mapKeyboardKey, handlePadKey, backspaceEntry, initCalculator, dual browser/Node export.
public/index.html Modify — drop inline script; reference calculator-ui.js.
server.js Modify — serve GET /calculator-ui.js with application/javascript body.
test/calculator-ui.test.js Create — key mapping and display flows (AC1–4).
test/server.test.js Modify — static script route test.
test/ui.test.js Modify — split assertions between HTML and JS file; keyboard wiring.
docs/plans/issue-12.md Create — this plan.

No changes to lib/calculator.js, package.json, or CI workflows.

Test plan

UI logic (test/calculator-ui.test.js)

Use node:test with a minimal DOM stub (#display, #keypad, #history-list, #clear-history). const api = initCalculator(doc) yields api.handlePadKey, api.backspaceEntry, api.getDisplayText, api.mapKeyboardKey.

fetch mock: record each request URL; for calculate URLs, return { result: 15 } when a=12&b=3&op=add, and sensible defaults for history GET / DELETE.

Case Steps Expect
AC1 (equals) api.handlePadKey('1'..'3'), then '=' (await if async) api.getDisplayText() → 15
AC1 (Enter) same digit/operator sequence; final step api.handlePadKey(api.mapKeyboardKey('Enter')) display 15
AC2 after input, api.handlePadKey(api.mapKeyboardKey('Escape')) display 0
AC3 1,2,3; api.backspaceEntry() twice display 12, then 1
AC4 for each of -, *, /, %: handlePadKey('5'), then handlePadKey(mapKeyboardKey(key)), then handlePadKey('2'), then '=' mocked calculate URLs include op=subtract, op=multiply, op=divide, op=percent respectively (not internal pendingOp)
Map unit mapKeyboardKey for -, *, /, Enter, Escape, Backspace per mapping table
Map negatives mapKeyboardKey('q') null
Regression api.handlePadKey('C') display 0

HTTP (test/server.test.js)

Case Steps Expect
Static UI script GET /calculator-ui.js 200; content-type matches application/javascript; body contains handlePadKey (or distinct marker from file)
Regression /, /health, calculate, history existing tests unchanged

Page (test/ui.test.js)

Read public/index.html for markup only:

Case Assert
Buttons required data-key buttons still present
Script tag <script src="calculator-ui.js"> (or equivalent)
History markup #history-list, #clear-history, “Clear history”

Read public/calculator-ui.js for behaviour wiring (not index.html):

Case Assert
API /api/calculate, `commitOperand('subtract'
Clear handlePadKey (or dispatcher) routes 'C' to reset() — not key === 'C' regex on HTML
History refreshHistory, /api/history, DELETE from clear control
Keyboard keydown listener and mapKeyboardKey
Single dispatch handlePadKey used from click path and keyboard path

Pre-push

Run npm test locally before git push origin aira/12-keyboard-support.

Risks

Risk Mitigation
Script extraction breaks history, sqrt, or binary ops Keep handlePadKey logic equivalent to current click handler; run full test suite.
Browser 404 on /calculator-ui.js Explicit server route + HTTP test (CI index smoke alone only greps HTML at /).
US keyboard symbols vs Unicode button labels (−, ×, ÷) Central mapKeyboardKey; mapping unit tests + AC4 via fetch op param.
Backspace after choosing an operator (freshEntry) No-op when entry is fresh 0; document in tests.
preventDefault interferes with browser shortcuts Ignore modified key events; only prevent when mapping is non-null.
Tests need async evaluate Use async test callbacks and await handlePadKey('=') when evaluate is async.
ui.test.js regressions grep missing strings in HTML Relocate behaviour assertions to calculator-ui.js per table above.

Attachment: plan-12-r2.md

**Aira · Plan (round 2) at `9fd89dbf`** Revised docs/plans/issue-12.md per Dodik: GET /calculator-ui.js in server.js with HTTP test; initCalculator returns handlePadKey/backspaceEntry/getDisplayText; AC4 via mocked fetch op param; ui.test.js split (markup in index.html, behavior in calculator-ui.js). Committed and pushed. <details><summary>docs/plans/issue-12.md</summary> # Issue #12: Keyboard support ## Goal Make the calculator fully usable from the keyboard. Key presses must use the same UI handlers as on-screen buttons (digits, operators, equals, clear) without duplicating calculation or API logic. Add Backspace to delete the last character of the current entry. ## Acceptance criteria 1. Typing `1`, `2`, `+`, `3`, `Enter` shows `15`. 2. `Escape` resets the display to `0`. 3. `Backspace` turns `123` into `12`. 4. `*`, `/`, `-` and `%` select the matching operations (multiply, divide, subtract, percent). ## Design ### Refactor: single keypad dispatcher Replace the inline `if/else` chain in the `#keypad` click listener with `handlePadKey(key)`, where `key` uses the same values as `button[data-key]` today: | `key` | Action (existing) | |-------|-------------------| | `0`–`9` | `appendDigit` | | `.` | `appendDecimal` | | `+` | `commitOperand('add')` | | `−` | `commitOperand('subtract')` | | `×` | `commitOperand('multiply')` | | `÷` | `commitOperand('divide')` | | `%` | `commitOperand('percent')` | | `√` | `applyUnary('sqrt')` | | `=` | `evaluate()` | | `C` | `reset()` | - **Click**: resolve `data-key` from the clicked button and call `handlePadKey(key)`. - **Keyboard**: map `event.key` and call `handlePadKey` (or `backspaceEntry` for Backspace). ### Keyboard mapping Add pure function `mapKeyboardKey(eventKey)` (testable in Node): | `event.key` | Maps to | |-------------|---------| | `0`–`9`, `.`, `+`, `%` | same pad key | | `-` | `−` | | `*` | `×` | | `/` | `÷` | | `=` | `=` | | `Enter` | `=` | | `Escape` | `C` | | `Backspace` | sentinel `'Backspace'` (not a button `data-key`) | Return `null` for unmapped keys. On `keydown`, skip when `ctrlKey`, `metaKey`, or `altKey` is set. When a key is handled, call `event.preventDefault()`. ### Backspace (new helper) `backspaceEntry()`: - No change if `freshEntry` is true or `entry === '0'`. - Otherwise drop the last character of `entry`; if empty, set `entry` to `'0'`. - Update the display via `setDisplay(entry)` (no API call). ### Script extraction (`public/calculator-ui.js`) Move the inline `<script>` from `index.html` into `public/calculator-ui.js` so tests load the same logic the browser runs. Dual-entry pattern: ```js (function (root) { function mapKeyboardKey(key) { /* … */ } function initCalculator(doc) { /* state, handlePadKey, backspaceEntry, listeners */ return { mapKeyboardKey, handlePadKey, backspaceEntry, getDisplayText: () => display.textContent, }; } if (typeof module !== 'undefined' && module.exports) { module.exports = { mapKeyboardKey, initCalculator }; } else { initCalculator(document); } })(typeof globalThis !== 'undefined' ? globalThis : window); ``` **`initCalculator` return value** (used by Node tests): `{ mapKeyboardKey, handlePadKey, backspaceEntry, getDisplayText }`. No exposure of `pendingOp` on the public test surface. `index.html` loads `<script src="calculator-ui.js"></script>` only (no inline calculator logic). `initCalculator` registers `doc.addEventListener('keydown', …)`: - `mapped === 'Backspace'` → `backspaceEntry()` - otherwise `handlePadKey(mapped)` when `mapKeyboardKey` returns a pad key. ### HTTP: serve static UI script (`server.js`) Today `server.js` only serves `GET /` as HTML; any other path (including `/calculator-ui.js`) returns `404` JSON. That must change so the extracted script loads in the browser. - Add constant `CALCULATOR_UI = path.join(__dirname, 'public', 'calculator-ui.js')` (alongside existing `INDEX`). - **`GET /calculator-ui.js`**: `200`, headers `content-type: application/javascript; charset=utf-8`, body `fs.readFileSync(CALCULATOR_UI)`. - Keep `/health`, `/api/calculate`, `/api/history`, `DELETE /api/history`, and `GET /` unchanged. Unlisted paths still `404` JSON. `lib/calculator.js` is unchanged. `evaluate()` still calls `/api/calculate`; keyboard `Enter`/`=` only triggers `handlePadKey('=')`. ### Test layout | File | Purpose | |------|---------| | `test/calculator-ui.test.js` | `require` exports; stub `document` / `fetch`; AC1–4 and backspace behaviour. | | `test/server.test.js` | `GET /calculator-ui.js` returns script bytes and JS content type. | | `test/ui.test.js` | Markup in `index.html`; behaviour wiring in `calculator-ui.js`. | ## Files to change | File | Action | |------|--------| | `public/calculator-ui.js` | **Create** — UI state machine, `mapKeyboardKey`, `handlePadKey`, `backspaceEntry`, `initCalculator`, dual browser/Node export. | | `public/index.html` | **Modify** — drop inline script; reference `calculator-ui.js`. | | `server.js` | **Modify** — serve `GET /calculator-ui.js` with `application/javascript` body. | | `test/calculator-ui.test.js` | **Create** — key mapping and display flows (AC1–4). | | `test/server.test.js` | **Modify** — static script route test. | | `test/ui.test.js` | **Modify** — split assertions between HTML and JS file; keyboard wiring. | | `docs/plans/issue-12.md` | **Create** — this plan. | No changes to `lib/calculator.js`, `package.json`, or CI workflows. ## Test plan ### UI logic (`test/calculator-ui.test.js`) Use `node:test` with a minimal DOM stub (`#display`, `#keypad`, `#history-list`, `#clear-history`). `const api = initCalculator(doc)` yields `api.handlePadKey`, `api.backspaceEntry`, `api.getDisplayText`, `api.mapKeyboardKey`. **`fetch` mock**: record each request URL; for calculate URLs, return `{ result: 15 }` when `a=12&b=3&op=add`, and sensible defaults for history `GET` / `DELETE`. | Case | Steps | Expect | |------|--------|--------| | AC1 (equals) | `api.handlePadKey('1'..'3')`, then `'='` (await if async) | `api.getDisplayText()` → `15` | | AC1 (Enter) | same digit/operator sequence; final step `api.handlePadKey(api.mapKeyboardKey('Enter'))` | display `15` | | AC2 | after input, `api.handlePadKey(api.mapKeyboardKey('Escape'))` | display `0` | | AC3 | `1`,`2`,`3`; `api.backspaceEntry()` twice | display `12`, then `1` | | AC4 | for each of `-`, `*`, `/`, `%`: `handlePadKey('5')`, then `handlePadKey(mapKeyboardKey(key))`, then `handlePadKey('2')`, then `'='` | mocked calculate URLs include `op=subtract`, `op=multiply`, `op=divide`, `op=percent` respectively (not internal `pendingOp`) | | Map unit | `mapKeyboardKey` for `-`, `*`, `/`, `Enter`, `Escape`, `Backspace` | per mapping table | | Map negatives | `mapKeyboardKey('q')` | `null` | | Regression | `api.handlePadKey('C')` | display `0` | ### HTTP (`test/server.test.js`) | Case | Steps | Expect | |------|--------|--------| | Static UI script | `GET /calculator-ui.js` | `200`; `content-type` matches `application/javascript`; body contains `handlePadKey` (or distinct marker from file) | | Regression | `/`, `/health`, calculate, history | existing tests unchanged | ### Page (`test/ui.test.js`) Read **`public/index.html`** for markup only: | Case | Assert | |------|--------| | Buttons | required `data-key` buttons still present | | Script tag | `<script src="calculator-ui.js">` (or equivalent) | | History markup | `#history-list`, `#clear-history`, “Clear history” | Read **`public/calculator-ui.js`** for behaviour wiring (not `index.html`): | Case | Assert | |------|--------| | API | `/api/calculate`, `commitOperand('subtract'|'multiply'|'divide'|'percent')`, `applyUnary('sqrt')`, `setDisplay('Error')` | | Clear | `handlePadKey` (or dispatcher) routes `'C'` to `reset()` — not `key === 'C'` regex on HTML | | History | `refreshHistory`, `/api/history`, `DELETE` from clear control | | Keyboard | `keydown` listener and `mapKeyboardKey` | | Single dispatch | `handlePadKey` used from click path and keyboard path | ### Pre-push Run `npm test` locally before `git push origin aira/12-keyboard-support`. ## Risks | Risk | Mitigation | |------|------------| | Script extraction breaks history, sqrt, or binary ops | Keep `handlePadKey` logic equivalent to current click handler; run full test suite. | | Browser 404 on `/calculator-ui.js` | Explicit server route + HTTP test (CI index smoke alone only greps HTML at `/`). | | US keyboard symbols vs Unicode button labels (`−`, `×`, `÷`) | Central `mapKeyboardKey`; mapping unit tests + AC4 via fetch `op` param. | | Backspace after choosing an operator (`freshEntry`) | No-op when entry is fresh `0`; document in tests. | | `preventDefault` interferes with browser shortcuts | Ignore modified key events; only prevent when mapping is non-null. | | Tests need async `evaluate` | Use `async` test callbacks and await `handlePadKey('=')` when `evaluate` is async. | | `ui.test.js` regressions grep missing strings in HTML | Relocate behaviour assertions to `calculator-ui.js` per table above. | </details> Attachment: [plan-12-r2.md](https://aira-lab.cluster-dev.spkey.co/attachments/20104fbc-a261-4845-90a2-e128da9261f6)
Collaborator

Aira · plan review

Plan pushed at 9fd89dbf; independent Dodik plan review started.

**Aira · plan review** Plan pushed at `9fd89dbf`; independent Dodik plan review started.
Collaborator

Aira · dodik-plan PASS on 9fd89dbf

The plan covers every keyboard requirement with numbered criteria, a dispatcher that matches the current button handlers and Unicode operator keys, and tests that prove display results, operator selection via the calculate query, and the new static script route. Serving public/calculator-ui.js from server.js closes the gap that would have left the page script at 404, and calculation stays in lib/calculator.js.

Checks performed
  • Mapped digits, decimal, operators, Enter/=, Escape, Backspace, shared handlers, and page-script tests onto criteria 1-4, the mapKeyboardKey table, and the calculator-ui test cases.
  • Compared the handlePadKey table with the click handler in public/index.html; data-key values are U+2212, U+00D7, and U+00F7, and mapKeyboardKey sends -, *, and / to those keys.
  • Read server.js: only GET / serves HTML today and other paths return 404 JSON. The plan adds GET /calculator-ui.js with application/javascript and an HTTP test, and leaves /health, /api/calculate, /api/history, and GET / unchanged.
  • Read test/ui.test.js and test/server.test.js. Behavior assertions move to calculator-ui.js; markup stays in index.html. AC4 is proven by the mocked op query, and initCalculator exposes handlePadKey, backspaceEntry, and getDisplayText. npm test: 70 passing.

Attachment: dodik-plan-9fd89dbf.json

**Aira · dodik-plan PASS on `9fd89dbf`** The plan covers every keyboard requirement with numbered criteria, a dispatcher that matches the current button handlers and Unicode operator keys, and tests that prove display results, operator selection via the calculate query, and the new static script route. Serving public/calculator-ui.js from server.js closes the gap that would have left the page script at 404, and calculation stays in lib/calculator.js. <details><summary>Checks performed</summary> - Mapped digits, decimal, operators, Enter/=, Escape, Backspace, shared handlers, and page-script tests onto criteria 1-4, the mapKeyboardKey table, and the calculator-ui test cases. - Compared the handlePadKey table with the click handler in public/index.html; data-key values are U+2212, U+00D7, and U+00F7, and mapKeyboardKey sends -, *, and / to those keys. - Read server.js: only GET / serves HTML today and other paths return 404 JSON. The plan adds GET /calculator-ui.js with application/javascript and an HTTP test, and leaves /health, /api/calculate, /api/history, and GET / unchanged. - Read test/ui.test.js and test/server.test.js. Behavior assertions move to calculator-ui.js; markup stays in index.html. AC4 is proven by the mocked op query, and initCalculator exposes handlePadKey, backspaceEntry, and getDisplayText. npm test: 70 passing. </details> Attachment: [dodik-plan-9fd89dbf.json](https://aira-lab.cluster-dev.spkey.co/attachments/7e957569-aef4-4485-9b22-f03c68939bea)
Collaborator

Aira · implementing

Plan approved by Dodik at 9fd89dbf. Implementation started.

**Aira · implementing** Plan approved by Dodik at `9fd89dbf`. Implementation started.
Collaborator

Aira · ci

Ready for review at 8beb7b96 (PR #16). Waiting for CI.

Implemented keyboard support: public/calculator-ui.js with handlePadKey, mapKeyboardKey, backspaceEntry, keydown wiring; server serves GET /calculator-ui.js; index.html loads external script; tests in calculator-ui.test.js (AC1–4), server.test.js, updated ui.test.js. npm test: 81 pass.

**Aira · ci** Ready for review at `8beb7b96` (PR #16). Waiting for CI. Implemented keyboard support: public/calculator-ui.js with handlePadKey, mapKeyboardKey, backspaceEntry, keydown wiring; server serves GET /calculator-ui.js; index.html loads external script; tests in calculator-ui.test.js (AC1–4), server.test.js, updated ui.test.js. npm test: 81 pass.
Collaborator

Aira · CI passed on 8beb7b96

  • ci / test (pull_request): success
**Aira · CI passed on `8beb7b96`** - ci / test (pull_request): success
Collaborator

Aira · review

CI is green on 8beb7b96. Independent Dodik and code review started.

**Aira · review** CI is green on `8beb7b96`. Independent Dodik and code review started.
Collaborator

Aira · dodik PASS on 8beb7b96

Commit 8beb7b96 makes the calculator keyboard-usable through the same handlePadKey path as the buttons, with mapKeyboardKey and backspaceEntry covered by tests that fail if that behavior is removed. The full suite passed (81 tests, 0 skipped), and keydown checks against the live API showed 1, 2, +, 3, Enter displays 15, Escape resets to 0, Backspace turns 123 into 12, and *, /, -, and % select multiply, divide, subtract, and percent. The diff is limited to the planned UI, server, and test files; existing assertions were moved with the extracted script rather than weakened, and README did not need a change because it does not describe input behavior.

Checks performed
  • Compared git diff origin/main...HEAD (8beb7b96) with docs/plans/issue-12.md
  • AC1: keydown 1, 2, +, 3, Enter shows 15 via the real /api/calculate path, and = computes the same way
  • AC2: Escape resets the display to 0 through mapKeyboardKey to handlePadKey('C')
  • AC3: Backspace turns 123 into 12 and calls preventDefault
  • AC4: *, /, -, and % send op=multiply, op=divide, op=subtract, and op=percent
  • Digits, decimal, modifier-key ignore, and fresh-entry Backspace no-op match the plan
  • npm test: 81 passed, 0 failed, 0 skipped, 0 todo
  • No unrelated files, no skipped or weakened tests, no debug leftovers; README unchanged and not stale

Attachment: dodik-8beb7b96.json

**Aira · dodik PASS on `8beb7b96`** Commit 8beb7b96 makes the calculator keyboard-usable through the same handlePadKey path as the buttons, with mapKeyboardKey and backspaceEntry covered by tests that fail if that behavior is removed. The full suite passed (81 tests, 0 skipped), and keydown checks against the live API showed 1, 2, +, 3, Enter displays 15, Escape resets to 0, Backspace turns 123 into 12, and *, /, -, and % select multiply, divide, subtract, and percent. The diff is limited to the planned UI, server, and test files; existing assertions were moved with the extracted script rather than weakened, and README did not need a change because it does not describe input behavior. <details><summary>Checks performed</summary> - Compared git diff origin/main...HEAD (8beb7b96) with docs/plans/issue-12.md - AC1: keydown 1, 2, +, 3, Enter shows 15 via the real /api/calculate path, and = computes the same way - AC2: Escape resets the display to 0 through mapKeyboardKey to handlePadKey('C') - AC3: Backspace turns 123 into 12 and calls preventDefault - AC4: *, /, -, and % send op=multiply, op=divide, op=subtract, and op=percent - Digits, decimal, modifier-key ignore, and fresh-entry Backspace no-op match the plan - npm test: 81 passed, 0 failed, 0 skipped, 0 todo - No unrelated files, no skipped or weakened tests, no debug leftovers; README unchanged and not stale </details> Attachment: [dodik-8beb7b96.json](https://aira-lab.cluster-dev.spkey.co/attachments/4d658511-4c75-4fd9-b01b-0edec635d659)
Collaborator

Aira · reviewer FAIL on 8beb7b96

Keyboard mappings and the handlePadKey refactor meet issue #12 acceptance criteria and the full npm test suite (81/81) passes, including the new static script route. However, the global keydown listener maps Enter to equals and calls preventDefault without exempting focused buttons, which regresses keyboard activation of Clear history and keypad buttons that worked before the change.

  • [major] Enter hijacks focused button activation (public/calculator-ui.js): The document keydown handler in initCalculator (lines 155-161) maps Enter to '=' and calls event.preventDefault() for every mapped key. Unlike the old inline UI, there is no guard when event.target is a focusable button. Users who tab to Clear history or a keypad key and press Enter expect native button activation; instead the default is suppressed and handlePadKey('=') runs (evaluate when operands are set, or a no-op). Fix by skipping the global keyboard handler when the event target is (or is inside) an interactive control, or by only attaching keyboard handling when focus is on the calculator surface—not on buttons.
Checks performed
  • git diff origin/main...HEAD at 8beb7b96df
  • npm test (81 passing)
  • Compared calculator-ui.js logic to prior inline index.html script
  • Verified server GET /calculator-ui.js route and existing API/health behaviour
  • Reviewed mapKeyboardKey, backspaceEntry, and acceptance-criteria tests

Attachment: reviewer-8beb7b96.json

**Aira · reviewer FAIL on `8beb7b96`** Keyboard mappings and the handlePadKey refactor meet issue #12 acceptance criteria and the full npm test suite (81/81) passes, including the new static script route. However, the global keydown listener maps Enter to equals and calls preventDefault without exempting focused buttons, which regresses keyboard activation of Clear history and keypad buttons that worked before the change. - [major] Enter hijacks focused button activation (public/calculator-ui.js): The document keydown handler in initCalculator (lines 155-161) maps Enter to '=' and calls event.preventDefault() for every mapped key. Unlike the old inline UI, there is no guard when event.target is a focusable button. Users who tab to Clear history or a keypad key and press Enter expect native button activation; instead the default is suppressed and handlePadKey('=') runs (evaluate when operands are set, or a no-op). Fix by skipping the global keyboard handler when the event target is (or is inside) an interactive control, or by only attaching keyboard handling when focus is on the calculator surface—not on buttons. <details><summary>Checks performed</summary> - git diff origin/main...HEAD at 8beb7b96df956010b33337fe385134bac45745e0 - npm test (81 passing) - Compared calculator-ui.js logic to prior inline index.html script - Verified server GET /calculator-ui.js route and existing API/health behaviour - Reviewed mapKeyboardKey, backspaceEntry, and acceptance-criteria tests </details> Attachment: [reviewer-8beb7b96.json](https://aira-lab.cluster-dev.spkey.co/attachments/c75d66d6-6706-41a4-ab27-a80d369d935a)
Collaborator

Aira · implementing

reviewer failed on 8beb7b96; sent the findings to the orchestrator (fix round 1/6).

**Aira · implementing** reviewer failed on `8beb7b96`; sent the findings to the orchestrator (fix round 1/6).
Collaborator

Aira · ci

Ready for review at 5ba158a8 (PR #16). Waiting for CI.

Fixed reviewer finding: global keydown now returns early when event.target is inside a button so Enter/Space can activate focused keypad and Clear history controls; added regression test. npm test: 82 pass.

**Aira · ci** Ready for review at `5ba158a8` (PR #16). Waiting for CI. Fixed reviewer finding: global keydown now returns early when event.target is inside a button so Enter/Space can activate focused keypad and Clear history controls; added regression test. npm test: 82 pass.
Collaborator

Aira · CI passed on 5ba158a8

  • ci / test (pull_request): success
**Aira · CI passed on `5ba158a8`** - ci / test (pull_request): success
Collaborator

Aira · review

CI is green on 5ba158a8. Independent Dodik and code review started.

**Aira · review** CI is green on `5ba158a8`. Independent Dodik and code review started.
Collaborator

Aira · dodik FAIL on 5ba158a8

Keyboard support matches the approved plan and the four acceptance checks when focus is not on a button: the suite passes (82 tests), and the keydown path shows 1, 2, +, 3, Enter as 15, Escape as 0, Backspace on 123 as 12, and -, *, /, % as subtract, multiply, divide, and percent. The follow-up guard returns before mapping whenever the event target is a button, so those same keys are dropped after a click or Tab and the calculator is not fully usable from the keyboard in that state.

  • [major] Focused buttons swallow every keypad key (public/calculator-ui.js): The keydown listener returns before mapKeyboardKey when event.target is any button, so after a click or Tab the digits, decimal point, operators, Escape, and Backspace never reach handlePadKey or backspaceEntry. Keep that early return only for keys the focused button already activates (Enter and Space) so every other mapped key still uses the shared handlers.
Checks performed
  • Compared 5ba158a with origin/main and docs/plans/issue-12.md: script extraction, mapKeyboardKey, GET /calculator-ui.js, and tests stay in scope
  • npm test: 82 passed, 0 failed, 0 skipped; no skipped or weakened tests and no debug leftovers
  • AC1–AC4 hold through the keydown listener with lib/calculator.js: Enter and = yield 15, Escape yields 0, Backspace turns 123 into 12, and -, *, /, % request the matching operations
  • Clicks and keys share handlePadKey and backspaceEntry; README is unchanged and does not document UI behavior, as the plan specifies

Attachment: dodik-5ba158a8.json

**Aira · dodik FAIL on `5ba158a8`** Keyboard support matches the approved plan and the four acceptance checks when focus is not on a button: the suite passes (82 tests), and the keydown path shows 1, 2, +, 3, Enter as 15, Escape as 0, Backspace on 123 as 12, and -, *, /, % as subtract, multiply, divide, and percent. The follow-up guard returns before mapping whenever the event target is a button, so those same keys are dropped after a click or Tab and the calculator is not fully usable from the keyboard in that state. - [major] Focused buttons swallow every keypad key (public/calculator-ui.js): The keydown listener returns before mapKeyboardKey when event.target is any button, so after a click or Tab the digits, decimal point, operators, Escape, and Backspace never reach handlePadKey or backspaceEntry. Keep that early return only for keys the focused button already activates (Enter and Space) so every other mapped key still uses the shared handlers. <details><summary>Checks performed</summary> - Compared 5ba158a with origin/main and docs/plans/issue-12.md: script extraction, mapKeyboardKey, GET /calculator-ui.js, and tests stay in scope - npm test: 82 passed, 0 failed, 0 skipped; no skipped or weakened tests and no debug leftovers - AC1–AC4 hold through the keydown listener with lib/calculator.js: Enter and = yield 15, Escape yields 0, Backspace turns 123 into 12, and -, *, /, % request the matching operations - Clicks and keys share handlePadKey and backspaceEntry; README is unchanged and does not document UI behavior, as the plan specifies </details> Attachment: [dodik-5ba158a8.json](https://aira-lab.cluster-dev.spkey.co/attachments/875c0394-97ea-44c1-a142-bd88ec994888)
Collaborator

Aira · reviewer PASS on 5ba158a8

Issue #12 delivers keyboard support by refactoring the inline calculator script into public/calculator-ui.js with a shared handlePadKey path for clicks and keydown events, mapKeyboardKey for US-layout operator/control keys, backspaceEntry, and a server route for the extracted script. All 82 tests pass, acceptance flows for digits, Enter, Escape, Backspace, and */-/% operators are covered, and live server smoke checks confirm /calculator-ui.js is served and referenced from /. No correctness, security, or regression defects with concrete impact were identified in the diff.

Checks performed
  • git diff origin/main...HEAD at 5ba158a
  • npm test (82 passing)
  • Comparison of calculator-ui.js logic to former inline index.html script
  • Server smoke: GET /health, GET /calculator-ui.js, index references script
  • Simulated document keydown AC1 (1,2,+,3,Enter) and Escape

Attachment: reviewer-5ba158a8.json

**Aira · reviewer PASS on `5ba158a8`** Issue #12 delivers keyboard support by refactoring the inline calculator script into `public/calculator-ui.js` with a shared `handlePadKey` path for clicks and keydown events, `mapKeyboardKey` for US-layout operator/control keys, `backspaceEntry`, and a server route for the extracted script. All 82 tests pass, acceptance flows for digits, Enter, Escape, Backspace, and */-/% operators are covered, and live server smoke checks confirm `/calculator-ui.js` is served and referenced from `/`. No correctness, security, or regression defects with concrete impact were identified in the diff. <details><summary>Checks performed</summary> - git diff origin/main...HEAD at 5ba158a - npm test (82 passing) - Comparison of calculator-ui.js logic to former inline index.html script - Server smoke: GET /health, GET /calculator-ui.js, index references script - Simulated document keydown AC1 (1,2,+,3,Enter) and Escape </details> Attachment: [reviewer-5ba158a8.json](https://aira-lab.cluster-dev.spkey.co/attachments/dba40999-3463-4576-8c75-a2c03c24b0c4)
Collaborator

Aira · implementing

dodik failed on 5ba158a8; sent the findings to the orchestrator (fix round 2/6).

**Aira · implementing** dodik failed on `5ba158a8`; sent the findings to the orchestrator (fix round 2/6).
Collaborator

Aira · ci

Ready for review at 72dd0307 (PR #16). Waiting for CI.

Narrowed button-focus guard: only Enter and Space skip global keydown so native button activation works; digits/operators/Escape/Backspace still reach handlePadKey. Added test for digit key with focused button. npm test: 83 pass.

**Aira · ci** Ready for review at `72dd0307` (PR #16). Waiting for CI. Narrowed button-focus guard: only Enter and Space skip global keydown so native button activation works; digits/operators/Escape/Backspace still reach handlePadKey. Added test for digit key with focused button. npm test: 83 pass.
Collaborator

Aira · CI passed on 72dd0307

  • ci / test (pull_request): success
**Aira · CI passed on `72dd0307`** - ci / test (pull_request): success
Collaborator

Aira · review

CI is green on 72dd0307. Independent Dodik and code review started.

**Aira · review** CI is green on `72dd0307`. Independent Dodik and code review started.
Collaborator

Aira · dodik PASS on 72dd0307

Keyboard support at 72dd030 matches the approved issue #12 plan and all four acceptance criteria. Digits, decimal, operators, percent, Enter, equals, Escape, and Backspace go through mapKeyboardKey and the same handlePadKey or backspaceEntry path as the buttons; GET /calculator-ui.js serves that script. npm test reports 83 passed, 0 failed, and 0 skipped, and a keydown-listener check confirms 1, 2, +, 3, Enter shows 15, Escape resets to 0, Backspace turns 123 into 12, and -, *, /, and % select subtract, multiply, divide, and percent. The diff stays within the planned files, with no skipped or weakened tests and no debug leftovers.

Checks performed
  • git diff origin/main...HEAD is limited to docs/plans/issue-12.md, public/calculator-ui.js, public/index.html, server.js, test/calculator-ui.test.js, test/server.test.js, and test/ui.test.js
  • Acceptance criteria implemented: Enter and = evaluate via handlePadKey, Escape maps to C/reset, Backspace deletes the last character, and -, *, /, % map to subtract, multiply, divide, and percent
  • Tests cover mapKeyboardKey, the display flows for AC1–AC4, the keydown listener, and GET /calculator-ui.js; prior UI assertions were moved onto calculator-ui.js as the plan required
  • npm test: 83 passed, 0 failed, 0 skipped, 0 todo
  • Independent keydown simulation: 1 2 + 3 Enter shows 15 and requests a=12&b=3&op=add; Escape shows 0; Backspace turns 123 into 12; operator keys request the matching op; modifiers and unmapped keys are ignored
  • No debug leftovers, no test.skip or test.only, and lib/calculator.js, package.json, CI, and README are unchanged

Attachment: dodik-72dd0307.json

**Aira · dodik PASS on `72dd0307`** Keyboard support at 72dd030 matches the approved issue #12 plan and all four acceptance criteria. Digits, decimal, operators, percent, Enter, equals, Escape, and Backspace go through mapKeyboardKey and the same handlePadKey or backspaceEntry path as the buttons; GET /calculator-ui.js serves that script. npm test reports 83 passed, 0 failed, and 0 skipped, and a keydown-listener check confirms 1, 2, +, 3, Enter shows 15, Escape resets to 0, Backspace turns 123 into 12, and -, *, /, and % select subtract, multiply, divide, and percent. The diff stays within the planned files, with no skipped or weakened tests and no debug leftovers. <details><summary>Checks performed</summary> - git diff origin/main...HEAD is limited to docs/plans/issue-12.md, public/calculator-ui.js, public/index.html, server.js, test/calculator-ui.test.js, test/server.test.js, and test/ui.test.js - Acceptance criteria implemented: Enter and = evaluate via handlePadKey, Escape maps to C/reset, Backspace deletes the last character, and -, *, /, % map to subtract, multiply, divide, and percent - Tests cover mapKeyboardKey, the display flows for AC1–AC4, the keydown listener, and GET /calculator-ui.js; prior UI assertions were moved onto calculator-ui.js as the plan required - npm test: 83 passed, 0 failed, 0 skipped, 0 todo - Independent keydown simulation: 1 2 + 3 Enter shows 15 and requests a=12&b=3&op=add; Escape shows 0; Backspace turns 123 into 12; operator keys request the matching op; modifiers and unmapped keys are ignored - No debug leftovers, no test.skip or test.only, and lib/calculator.js, package.json, CI, and README are unchanged </details> Attachment: [dodik-72dd0307.json](https://aira-lab.cluster-dev.spkey.co/attachments/6d35d9fc-5bcd-47c8-8b77-eb2bf8f237dd)
Collaborator

Aira · reviewer PASS on 72dd0307

Issue #12 delivers keyboard support by extracting the existing calculator UI into public/calculator-ui.js with a shared handlePadKey dispatcher, mapKeyboardKey, and backspaceEntry, wiring document-level keydown with modifier-key guardrails and targeted Enter/Space deferral when a button is focused, and serving the script via GET /calculator-ui.js. Logic matches the former inline script for API-backed evaluation; all 83 tests pass including AC1–AC4 and the new static route.

Checks performed
  • git diff origin/main...HEAD at 72dd0307 (calculator-ui.js, index.html, server.js, tests, plan)
  • npm test — 83 passed, 0 failed
  • Manual smoke: GET /health 200, index references calculator-ui.js, GET /calculator-ui.js 200
  • Compared handlePadKey/evaluate/applyUnary/history flows to origin/main inline script
  • Reviewed keydown focus handling (Enter/Space vs digits) and mapKeyboardKey edge cases

Attachment: reviewer-72dd0307.json

**Aira · reviewer PASS on `72dd0307`** Issue #12 delivers keyboard support by extracting the existing calculator UI into `public/calculator-ui.js` with a shared `handlePadKey` dispatcher, `mapKeyboardKey`, and `backspaceEntry`, wiring document-level `keydown` with modifier-key guardrails and targeted Enter/Space deferral when a button is focused, and serving the script via `GET /calculator-ui.js`. Logic matches the former inline script for API-backed evaluation; all 83 tests pass including AC1–AC4 and the new static route. <details><summary>Checks performed</summary> - git diff origin/main...HEAD at 72dd0307 (calculator-ui.js, index.html, server.js, tests, plan) - npm test — 83 passed, 0 failed - Manual smoke: GET /health 200, index references calculator-ui.js, GET /calculator-ui.js 200 - Compared handlePadKey/evaluate/applyUnary/history flows to origin/main inline script - Reviewed keydown focus handling (Enter/Space vs digits) and mapKeyboardKey edge cases </details> Attachment: [reviewer-72dd0307.json](https://aira-lab.cluster-dev.spkey.co/attachments/5559390a-db38-4598-a111-540dd44957f1)
Collaborator

Aira · qa

Dodik and the reviewer passed on 72dd0307. Preview deployed at http://127.0.0.1:34365; QA started.

**Aira · qa** Dodik and the reviewer passed on `72dd0307`. Preview deployed at http://127.0.0.1:34365; QA started.
Collaborator

Aira · qa PASS on 72dd0307

Preview commit 72dd0307a2 serves a calculator page with the required controls, and keyboard input observed through the served page script against the live API meets every acceptance criterion: 1 2 + 3 Enter and = display 15, Escape resets to 0, Backspace turns 123 into 12, and * / - % select multiply, divide, subtract, and percent via the same calculate requests as the buttons. Invalid input returns 400, and the project tests including key handling all passed.

Checks performed

Attachment: qa-72dd0307.json

**Aira · qa PASS on `72dd0307`** Preview commit 72dd0307a21853b6d3f18e87f9f1c69a242cd343 serves a calculator page with the required controls, and keyboard input observed through the served page script against the live API meets every acceptance criterion: 1 2 + 3 Enter and = display 15, Escape resets to 0, Backspace turns 123 into 12, and * / - % select multiply, divide, subtract, and percent via the same calculate requests as the buttons. Invalid input returns 400, and the project tests including key handling all passed. <details><summary>Checks performed</summary> - GET http://127.0.0.1:34365/health -> 200 {"ok":true,"sha":"72dd0307a21853b6d3f18e87f9f1c69a242cd343"} - GET http://127.0.0.1:34365/ -> 200 text/html with buttons data-key 0-9 . + − × ÷ % = C and script calculator-ui.js - GET http://127.0.0.1:34365/calculator-ui.js -> 200 application/javascript - GET http://127.0.0.1:34365/index.html -> 404 - GET http://127.0.0.1:34365/public/index.html -> 404 - GET http://127.0.0.1:34365/missing -> 404 - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=add -> 200 {"result":15} - GET http://127.0.0.1:34365/api/calculate?a=8&b=2&op=multiply -> 200 {"result":16} - GET http://127.0.0.1:34365/api/calculate?a=8&b=2&op=divide -> 200 {"result":4} - GET http://127.0.0.1:34365/api/calculate?a=8&b=2&op=subtract -> 200 {"result":6} - GET http://127.0.0.1:34365/api/calculate?a=50&b=10&op=percent -> 200 {"result":5} - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=subtract -> 200 {"result":9} - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=multiply -> 200 {"result":36} - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=divide -> 200 {"result":4} - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=percent -> 200 {"result":0.36} - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=%2B -> 400 {"error":"Unknown operation"} - GET http://127.0.0.1:34365/api/calculate?a=8&b=2&op=* -> 400 {"error":"Unknown operation"} - GET http://127.0.0.1:34365/api/calculate?a=8&b=2&op=%2A -> 400 {"error":"Unknown operation"} - GET http://127.0.0.1:34365/api/calculate?a=8&b=2&op=/ -> 400 {"error":"Unknown operation"} - GET http://127.0.0.1:34365/api/calculate?a=8&b=2&op=%2F -> 400 {"error":"Unknown operation"} - GET http://127.0.0.1:34365/api/calculate?a=8&b=2&op=- -> 400 {"error":"Unknown operation"} - GET http://127.0.0.1:34365/api/calculate?a=50&b=10&op=%25 -> 400 {"error":"Unknown operation"} - GET http://127.0.0.1:34365/api/calculate?a=50&b=20&op=% -> 400 {"error":"Unknown operation"} - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=sub -> 400 Unknown operation - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=mul -> 400 Unknown operation - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=div -> 400 Unknown operation - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=mod -> 400 Unknown operation - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=modulo -> 400 Unknown operation - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=pct -> 400 Unknown operation - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=plus -> 400 Unknown operation - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=minus -> 400 Unknown operation - GET http://127.0.0.1:34365/api/calculate?a=12&b=3&op=times -> 400 Unknown operation - GET http://127.0.0.1:34365/api/calculate -> 400 {"error":"Unknown operation"} - GET http://127.0.0.1:34365/api/calculate?a=1&b=2 -> 400 Unknown operation - GET http://127.0.0.1:34365/api/calculate?a=1&op=add -> 400 Invalid operand - GET http://127.0.0.1:34365/api/calculate?b=2&op=add -> 400 Invalid operand - GET http://127.0.0.1:34365/api/calculate?a=&b=2&op=add -> 400 Invalid operand - GET http://127.0.0.1:34365/api/calculate?a=1&b=&op=add -> 400 Invalid operand - GET http://127.0.0.1:34365/api/calculate?a=foo&b=2&op=add -> 400 Invalid operand - GET http://127.0.0.1:34365/api/calculate?a=1&b=bar&op=add -> 400 Invalid operand - GET http://127.0.0.1:34365/api/calculate?a=1&b=2&op= -> 400 Unknown operation - GET http://127.0.0.1:34365/api/calculate?a=1&b=2&op=unknown -> 400 Unknown operation - GET http://127.0.0.1:34365/api/calculate?a=1&b=2&op=pow -> 400 Unknown operation - GET http://127.0.0.1:34365/api/calculate?a=1&b=0&op=divide -> 400 {"error":"division by zero"} - GET http://127.0.0.1:34365/api/calculate?a=-5&b=3&op=add -> 200 {"result":-2} - GET http://127.0.0.1:34365/api/calculate?a=1.5&b=2.5&op=add -> 200 {"result":4} - GET http://127.0.0.1:34365/api/calculate?a=0&b=0&op=add -> 200 {"result":0} - GET http://127.0.0.1:34365/api/calculate?a=0&b=0&op=divide -> 400 division by zero - GET http://127.0.0.1:34365/api/calculate?a=0&b=0&op=multiply -> 200 {"result":0} - GET http://127.0.0.1:34365/api/calculate?a=10&b=0&op=percent -> 200 {"result":0} - GET http://127.0.0.1:34365/api/calculate?a=1e2&b=3&op=add -> 200 {"result":103} - GET http://127.0.0.1:34365/api/calculate?a=Infinity&b=1&op=add -> 400 Invalid operand - GET http://127.0.0.1:34365/api/calculate?a=NaN&b=1&op=add -> 400 Invalid operand - GET http://127.0.0.1:34365/api/calculate?a=%20&b=2&op=add -> 400 Invalid operand - GET http://127.0.0.1:34365/api/calculate?a=1.2.3&b=1&op=add -> 400 Invalid operand - GET http://127.0.0.1:34365/api/calculate?a=--1&b=2&op=add -> 400 Invalid operand - GET http://127.0.0.1:34365/api/calculate?a=+5&b=2&op=add -> 200 {"result":7} - GET http://127.0.0.1:34365/api/calculate?a=5.&b=2&op=add -> 200 {"result":7} - GET http://127.0.0.1:34365/api/calculate?a=.5&b=2&op=add -> 200 {"result":2.5} - GET http://127.0.0.1:34365/api/calculate?a=1e308&b=1e308&op=add -> 400 {"error":"Result out of range"} - GET http://127.0.0.1:34365/api/calculate?a=9&op=sqrt -> 200 {"result":3} - GET http://127.0.0.1:34365/api/calculate?a=-1&op=sqrt -> 400 {"error":"square root of a negative number"} - POST http://127.0.0.1:34365/api/calculate?a=1&b=2&op=add -> 404 - Served script keydown 1,2,+,3,Enter -> display 15 via GET /api/calculate?a=12&b=3&op=add - Served script keydown 1,2,+,3,= -> display 15 via GET /api/calculate?a=12&b=3&op=add - Button handlePadKey 1,2,+,3,= -> display 15 via the same GET /api/calculate?a=12&b=3&op=add - Served script keydown 9,8,Escape -> display 0 - Served script keydown 1,2,3,Backspace -> display 12 - Served script keydown 6,*,7,Enter -> display 42 via GET /api/calculate?a=6&b=7&op=multiply - Served script keydown 8,/,2,Enter -> display 4 via GET /api/calculate?a=8&b=2&op=divide - Served script keydown 9,-,4,Enter -> display 5 via GET /api/calculate?a=9&b=4&op=subtract - Served script keydown 2,0,0,%,1,0,Enter -> display 20 via GET /api/calculate?a=200&b=10&op=percent - Served script keydown 1,.,5,+,2,.,5,Enter -> display 4 via GET /api/calculate?a=1.5&b=2.5&op=add - Served script keydown 8,/,0,Enter -> display Error via GET /api/calculate?a=8&b=0&op=divide (no history write) - Served script keydown .,5,*,2,Enter -> display 1 via GET /api/calculate?a=0.5&b=2&op=multiply - Served script Backspace on a single digit or on 0 leaves display 0; unmapped keys leave the entry unchanged; Enter with no operator does not call calculate - GET http://127.0.0.1:34365/api/history -> 200 items newest-first including the keyboard calculations - DELETE http://127.0.0.1:34365/api/history -> 204 - npm test -> 83 passed, 0 failed, including mapKeyboardKey and AC1-AC4 key-handling tests </details> Attachment: [qa-72dd0307.json](https://aira-lab.cluster-dev.spkey.co/attachments/8167878a-b49c-4226-b157-459075e145b9)
Collaborator

Aira · merging

QA passed on 72dd0307. Merging.

**Aira · merging** QA passed on `72dd0307`. Merging.
Collaborator

Aira · Merged PR #16

Merge commit 02216ffb7238dbf6d4e5b9a6e834dae5abea8963 from reviewed head 72dd0307a21853b6d3f18e87f9f1c69a242cd343.

Gate Verdict Summary
ci pass ci / test (pull_request)
dodik pass Keyboard support at 72dd030 matches the approved issue #12 plan and all four acceptance criteria. Digits, decimal, operators, percent, Enter, equals, Escape, an
reviewer pass Issue #12 delivers keyboard support by extracting the existing calculator UI into public/calculator-ui.js with a shared handlePadKey dispatcher, `mapKeyboar
qa pass Preview commit 72dd0307a2 serves a calculator page with the required controls, and keyboard input observed through the served page
**Aira · Merged PR #16** Merge commit `02216ffb7238dbf6d4e5b9a6e834dae5abea8963` from reviewed head `72dd0307a21853b6d3f18e87f9f1c69a242cd343`. | Gate | Verdict | Summary | |---|---|---| | ci | pass | ci / test (pull_request) | | dodik | pass | Keyboard support at 72dd030 matches the approved issue #12 plan and all four acceptance criteria. Digits, decimal, operators, percent, Enter, equals, Escape, an | | reviewer | pass | Issue #12 delivers keyboard support by extracting the existing calculator UI into `public/calculator-ui.js` with a shared `handlePadKey` dispatcher, `mapKeyboar | | qa | pass | Preview commit 72dd0307a21853b6d3f18e87f9f1c69a242cd343 serves a calculator page with the required controls, and keyboard input observed through the served page |
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
lab/web-calculator#12
No description provided.