Files
glenloch-media/docs/superpowers/D8-CONTINUATION.md
Tiel Coder d0a696167f fix(D8): grid slots render, per-cell optimistic revert, shared E2E helpers, admin reset endpoint
- src/db/grid.ts: key role map by ID (was slug, causing all cells to show '—')
- src/components/app/schedule-grid-editor.tsx: revert only failed cell, not entire grid
- src/db/audit.ts: fix import path ./db → @/db
- src/db/seed.ts: add resetAndSeed(), assign Audio only (leave Livestream/Slides open)
- src/auth/config.ts: carry id/email on JWT for audit actor
- e2e/dev.ts: shared signInAs + resetDb helpers
- e2e/schedule-grid.spec.ts: use shared helpers, reset DB beforeAll
- e2e/assign-mutations.spec.ts: E2E for assign/clear with optimistic update + rollback
- src/app/api/dev/reset/route.ts: admin-only DB reset endpoint
- playwright.config.ts: workers: 1 (serial, suites share one Postgres)
2026-11-18 21:51:04 +00:00

6.6 KiB
Raw Permalink Blame History

D8 Continuation — assign/unassign/clear mutations (E2E now green)

Status: D8 is functionally complete. The E2E suite passes 3× in a row. The only remaining step is committing. This doc records the blocker that was stuck the prior session, what I found and fixed, the full change set, and the exact commit steps.

Read .pi/DEAD_ENDS.md and D8-HANDOFF.md first. This doc supersedes the "D7 E2E cannot be completed" and "grid never rendered" conclusions — those were environmental (DB pollution + a missing migration), not code defects.


1. The blocker that stuck the prior session (now resolved)

The E2E e2e/assign-mutations.spec.ts was failing with HTTP 500 on every assign/clear, which looked like a rule/logic bug but was an auth/session bug:

  • The /api/assignments route writes an audit_log row on every assign/clear, using actorId = session.user.id.
  • The Auth.js JWT callbacks only copied systemRole onto the token/session — id/email were never carried across the JWT boundary. So session.user.id was undefined.
  • The audit insert therefore sent actor_id = NULL, violating the audit_log.actor_id NOT NULL constraint → the route's catch returned 500 → the editor reverted the optimistic change → the grid never showed the mutation. The test timed out clicking a "Clear" that never took effect.

Root cause was NOT the grid or the rules. Once the audit insert stopped 500ing, assign/clear worked immediately.

Fix — src/auth/config.ts

Carry id + email onto the JWT (in jwt) and back onto session.user (in session), alongside systemRole. This is the documented Auth.js v5 pattern (the session callback only receives user on the first callback, so identity must be rehydrated from the token).


2. Second blocker: E2E test isolation (shared DB race)

After the 500 fix, the assign-mutations suite passed, but running the full suite failed intermittently: schedule-grid (read-only) would sometimes see an empty grid.

  • Both suites share one Postgres. assign-mutations resets the DB in beforeAll (truncate + seed); schedule-grid did not.
  • With parallel workers, schedule-grid could read the DB while assign-mutations was mid-truncate → no instances → "Sunday 9:00 AM" not rendered.

Fixes

  • e2e/dev.ts (new): shared signInAs + resetDb helpers (dev-console magic-link transport + admin /api/dev/reset reset). Both suites import it.
  • e2e/schedule-grid.spec.ts: added a beforeAll reset (read-only suite now establishes its own clean state, independent of run order).
  • playwright.config.ts: workers: 1 (serial). Both suites reset the same DB in beforeAll; parallel workers would truncate mid-read. The suite is tiny (~3.5s), so serial is fine. Documented why.
  • e2e/assign-mutations.spec.ts: rewritten to locate cells by role header (not hard-coded column indices), assert optimistic assign via the Clear button + name span, assert the rule-1 rollback (Slides keeps its picker + error toast), and filter the expected 409 from the "no console errors" check (the browser logs the intentional conflict fetch as a console error).

3. Full change set

Bug fixes (app code)

  • src/auth/config.ts — propagate id/email through JWT → session (fixes the audit 500). This is the critical fix.
  • src/db/audit.ts — import path ./db → @/db (handoff item; typecheck).
  • src/db/grid.ts — roleBySlug (keyed by slug) → roleById (keyed by roleId); grid slots were looked up by UUID, so roles never resolved and cells never rendered.
  • src/components/app/schedule-grid-editor.tsx — on a failed mutation, revert only that cell (not the whole optimistic batch).

E2E support

  • src/app/api/dev/reset/route.ts (new) — admin-only (admin.settings) POST that truncates the seed-owned tables and re-seeds. Guarded; not wired into any UI. Used by the suites' beforeAll.
  • src/db/seed.ts — exported seed() (idempotent populate) and resetAndSeed() (truncate + seed). CLI runs only when it's the entry point (isMain guard via import.meta.url) so importing it from the reset route doesn't execute/exit the server.
  • e2e/dev.ts (new) — shared helpers.
  • e2e/assign-mutations.spec.ts, e2e/schedule-grid.spec.ts — rewritten above.
  • playwright.config.ts — workers: 1.

Migrations (must be committed)

  • drizzle/0009_last_wallop.sql + drizzle/meta/0009_snapshot.json + drizzle/meta/_journal.json — creates the audit_log table (index on created_at). This migration is essential to D8: without it the audit insert fails with "relation audit_log does not exist". pnpm db:migrate applies it cleanly (verified).

4. Verification (all green, repeatable)

pnpm test            # 23 files, 182 tests passed
pnpm tsc --noEmit    # clean
npx eslint <changed files>   # clean
npx playwright test  # 8 passed — run 3× consecutively, all green
pnpm db:migrate      # applied successfully

The app is running on :3000 (built with all fixes). Env used for E2E: DATABASE_URL=postgres://media:media@localhost:5433/media, NEXTAUTH_SECRET=e2e-secret-for-shell-tests, AUTO_ACTIVATE_NEW_USERS=true.


5. Remaining steps (for the next agent / commit)

  1. Decide on e2e/_debug.spec.ts — a leftover debug probe (prints grid counts). It passes but is a scratch artifact; remove it before commit unless you want to keep it as a manual debug aid.
  2. Commit everything. Suggested scope:
    • app fixes: src/auth/config.ts, src/db/audit.ts, src/db/grid.ts, src/components/app/schedule-grid-editor.tsx
    • D8 E2E infra: src/app/api/dev/reset/, src/db/seed.ts, e2e/dev.ts, e2e/assign-mutations.spec.ts, e2e/schedule-grid.spec.ts, playwright.config.ts
    • migrations: drizzle/0009_last_wallop.sql, drizzle/meta/0009_snapshot.json, drizzle/meta/_journal.json
    • exclude e2e/_debug.spec.ts (or commit it separately if kept).
  3. Re-run the full E2E once more after commit (npx playwright test) to confirm nothing in the commit broke it.

If the E2E 500s again

Check the server log (/tmp/app.log) for [assignments] ERROR or a 23502/23503 SQLSTATE. actor_id NULL ⇒ the auth JWT fix regressed; relation ... does not exist ⇒ the 0009 audit_log migration wasn't applied.

If pnpm start exits 137 ("Killed")

Not OOM — the box has ~29 GiB free and no cgroup/ulimit memory cap. It was the pkill -f "next-start" matching its own shell command line. Kill by PID instead: PID=$(ss -ltnp | grep ':3000' | grep -oP 'pid=\K[0-9]+' | head -1); kill -9 $PID.