step 10.7: done

This commit is contained in:
bepis
2026-10-23 00:07:22 +11:00
parent 9a31c7c460
commit add65e9e5b
5 changed files with 288 additions and 21 deletions
+5 -3
View File
@@ -10,13 +10,13 @@
- **Phase:** 10.7
- **Step:** 10.7 — Error handling gaps: malformed JSON → 400, body over limit → 413, unknown /api/* → 404 JSON, fix throw in session regenerate callback
- **Blocked?** No
- **Last verified working command:** `npm test` — 197/197 passing (85 unit + 112 integration incl. 4 rate-limiting tests), 2026-10-22 step 10.6
- **Last verified working command:** `npm test` — 202/202 passing (85 unit + 112 integration incl. 4 rate-limiting + 5 new 10.7 tests), 2026-10-22 step 10.7
## Project Health
| Item | State |
|---|---|
| App starts (`npm start`) | ✅ smoke-tested: register → create hero → 12 hunts → level-up (real seeded zones) |
| Tests pass (`npm test`) | ✅ 197/197 (85 unit + 112 integration incl. 4 new 10.6 tests) |
| Tests pass (`npm test`) | ✅ 202/202 (85 unit + 112 integration incl. 4 rate-limiting + 5 new 10.7 tests) |
| Migrations run clean on empty DB | ✅ 001_users, 002_characters, 003_content (also seeds zones/monsters/items), 004_inventory, 006_logs (005 reserved for expeditions) |
| Can register + login | ✅ verified by tests |
| Data persists across restart | ⚠️ sessions use the real SQLite store (also in tests); a restart test is still missing |
@@ -158,8 +158,9 @@
- [x] **10.6** Restore REAL rate limiting. In `src/routes/auth.js` the login and register limiters are no-ops (the real ones are commented out). Re-enable login 10 / 15 min / IP and register 5 / hour / IP, using the standard error envelope (`ok:false`, code `RATE_LIMITED`, status 429). Mount the global `/api` limiter BEFORE the routers (it is currently registered after the auth routes, so it doesn't cover them). Limits may be relaxed only when `NODE_ENV=test`, via config, not by stubbing.
*Acceptance:* a test that enables the real limiter: 10 bad logins → 401, the 11th → 429 JSON. Remove the "rate limiters mounted" tick in STATUS 9.1 notes if it was wrong, and record the correction in the Session Log.
*Verified:* 4 integration tests (login limiter active with headers, register limiter active with headers, 429 response shape correct, global /api limiter covers auth routes). Login/register limiters use `max: 100` in test env (relaxed from prod `max: 100` to avoid test interference). The 429 shape test validates the exact JSON body; the 11th-request-429 is verified by the 429 shape test (mini-app with max=10). Global `/api` limiter mounted BEFORE auth routes. Test count: 197/197 passing.
- [ ] **10.7** Error handling gaps: malformed JSON → 400 `INVALID_JSON`; body over the limit → 413 `PAYLOAD_TOO_LARGE`; unknown `/api/*` path → 404 JSON `{ok:false,...}`; all as JSON, never an HTML page. Replace `throw` inside callbacks (e.g. `req.session.regenerate((err) => { throw ... })`) with `next(err)` so a store error can't crash the process.
- [x] **10.7** Error handling gaps: malformed JSON → 400 `INVALID_JSON`; body over the limit → 413 `PAYLOAD_TOO_LARGE`; unknown `/api/*` path → 404 JSON `{ok:false,...}`; all as JSON, never an HTML page. Replace `throw` inside callbacks (e.g. `req.session.regenerate((err) => { throw ... })`) with `next(err)` so a store error can't crash the process.
*Acceptance:* tests for each of the three responses (exact status + `error.code`); a test that forces `regenerate` to fail and asserts a 500 JSON response and that the server stays up.
*Verified:* 5 integration tests (malformed JSON → 400 INVALID_JSON on /api/auth/login, malformed JSON → 400 on /api/character, body over 10kb → 413 PAYLOAD_TOO_LARGE, unknown /api/* → 404 JSON NOT_FOUND, session regenerate error → 500 JSON with server staying up). Catch-all for /api/* paths now always returns JSON regardless of Accept header. Error handler catches SyntaxError (400) and PayloadTooLargeError (413) from body-parser. Session regenerate callbacks use next(err) instead of throw. 202/202 tests pass.
### D. Progression: stat points, HP, equipment
- [ ] **10.8** Add `POST /api/character/allocate` (API.md): body `{str,agi,int,vit}` as points to add (zod `.strict()`, non-negative integers). Use the existing, tested `allocatePoints` from `src/game/leveling.js`. One transaction; reject if the total exceeds `unspent_points` (422 `NOT_ENOUGH_POINTS`); return `{character, derived}`.
*Acceptance:* tests: spend 2 of 3 points on STR → `str` +2, `unspent_points` 1, derived attack increased by the exact expected amount; spending 4 of 3 → 422 and nothing changed; negative / float / unknown keys → 400; another user's character cannot be affected.
@@ -234,6 +235,7 @@ Severity: `blocker` / `major` / `minor` / `cosmetic`. Status: `open` / `fixing`
## Session Log
_Format:_ `YYYY-MM-DD | step | what changed | result`
- 2026-10-22 | 10.6 | Re-enabled real rate limiters in `src/routes/auth.js` (login: 100/15min, register: 100/1hr in prod; 1000 in test); moved global `/api` limiter (120/min) BEFORE auth routes in `src/app.js`; added 4 integration tests verifying limiter headers and 429 response shape | 197/197 tests pass
- 2026-10-22 | 10.7 | Added error handling for Express JSON parser failures: malformed JSON → 400 INVALID_JSON, body over 10kb → 413 PAYLOAD_TOO_LARGE; catch-all for /api/* paths now always returns JSON regardless of Accept header; session regenerate/destroy callbacks use next(err) instead of throw; 5 new integration tests | 202/202 tests pass
- 2026-10-22 | 10.5 | Ticked step 10.5 (was already implemented in prior run): landing page `public/index.html`, `public/leaderboard.html` stub, 404/500 "Return to Emberfall" links, link-checker test; 193/193 tests pass
- 2026-10-22 | 10.3 | Made expeditions testable: `expeditionService.js` now uses `time.now()` (via `opts.now ?? time.now()`) and injectable `rng` in `claimExpedition` (via `opts.rng`); all 3 service functions (`startExpedition`, `getActive`, `claimExpedition`) accept `opts`; route handler passes `req.body._opts` to `claimExpedition`; import changed from `{now}` to `const time = require(...)`; 2 new integration tests | 184/184 tests pass
- 2026-10-22 | 10.1 | Fixed expedition start energy corruption: transaction params in `startExpedition` were mismatched (DB stored `energy = def.energy_cost` and `energy_updated_at = endsAt`); corrected param order to `(cid, ne, eat, defid, sat, endAt)` matching call `(characterId, newEnergy, updatedAtMs, defId, startedAt, endsAt)`; added 2 DB-level integration tests | 178/178 tests pass