Skip to content

frontend: burn down four diagram/server TypeScript issues - #730

Merged
bpowers merged 2 commits into
mainfrom
frontend-issue-burndown-2026-06
Jun 8, 2026
Merged

bpowers merged 2 commits into
mainfrom
frontend-issue-burndown-2026-06

Conversation

@bpowers

@bpowers bpowers commented Jun 8, 2026 •

Copy link
Copy Markdown
Owner

Fixes #720, fixes #710, fixes #728, fixes #691

Closes a batch of frontend/TypeScript issues surfaced during the recent React component audit and the deploy-risk audit. Each fix is test-driven; the changes were run through an adversarial sub-agent review and iterated until no material findings remained.

Fixes

#720 — adjustFlows crashes on a degenerate self-loop flow

adjustFlows (src/diagram/drawing/Flow.tsx) called defined(otherEnd) and threw when handed a flow whose first and last points are both attached to the same stock. Such a flow can't be drawn in the editor but can arrive via imported models (XMILE/Vensim) or programmatic patches (MCP/API), crashing the canvas render/interaction path. It now early-returns the flow unchanged when there is no "other end" to anchor the valve fraction against (defense at the render boundary).

  • Test: flow-routing.test.ts — "degenerate self-loop flow" (reproduces the crash, verifies graceful no-op).

#710 — Menu anchors Radix to a stale getBoundingClientRect snapshot

Menu.tsx positioned the dropdown via a position:fixed proxy span placed at a rect memoized on anchorEl identity, so it never re-measured when the anchor moved (scroll/resize/reflow) and the menu detached from its trigger. A new useAnchorRect(anchorEl, open) hook re-measures (via useLayoutEffect) on capture-phase scroll, resize, and ResizeObserver while open, and bails out (returns the prior reference) when the rect is unchanged so a stationary anchor doesn't force a re-render on every scroll tick.

  • Test: menu.test.tsx — initial measure, scroll-follow, resize-follow, listener-detach-on-close + reopen re-measure.

#728 — Snapshotter modelErrors appends use non-functional setState

The two snapshot-image error handlers in Editor.tsx appended to modelErrors with a read-then-write setState({ ... latest.current.state.modelErrors ... }), which is clobber-prone under concurrent appends. Converted both to the functional updater form, matching the file's other concurrent-safe append sites. (The Snapshotter UI is currently commented out, so this is dead code today; the fix prevents it being re-enabled in a broken state — no test is reachable.)

#691 — Empty-body requests return 500 instead of 400

After the body-parser 1→2 / Express 4→5 upgrades, req.body is undefined (not {}) for empty-body or wrong-Content-Type requests. POST /api/projects and PATCH /api/user dereferenced req.body without guarding, so malformed/probe traffic threw a TypeError that Express 5 surfaced as a generic 500 (with stack noise that masks real 500s) instead of the intended 400. The body validation is extracted into pure, dependency-free validators (src/server/api-validation.ts) wired into both handlers.

  • Test: api-body-validation.test.ts — undefined/null/empty/wrong-type/falsy/extra-key cases for both validators (kept out of api.ts so the unit tests don't pull the DB/protobuf/engine-WASM graph).

Verification

  • Full pre-commit hook (Rust fmt/clippy/test, WASM build, TS lint/typecheck/test, pysimlin) passes on both commits.
  • Diagram suite: 1111 tests pass. Server suite: 111 tests pass.
  • Adversarial sub-agent review found no Critical/Important issues; its two Minor findings (Menu re-render guard, FCIS validator decoupling) and a Nit were addressed.

Deliberately deferred (not in this PR)

These related frontend/TS issues were evaluated and intentionally left for focused follow-ups:

bpowers added 2 commits June 8, 2026 13:59
Three latent frontend issues surfaced during the React component audit:

- adjustFlows (Flow.tsx) threw via defined(otherEnd) on a degenerate
  self-loop flow whose endpoints both attach to the same stock -- a shape
  that can't be drawn in the editor but arrives via imported models or
  programmatic patches, crashing the canvas. It now returns the flow
  unchanged when there is no other endpoint to anchor the valve against.

- Menu (components/Menu.tsx) anchored Radix to a position:fixed proxy
  holding a getBoundingClientRect snapshot memoized on anchorEl identity,
  so the menu detached from its trigger if the anchor moved while open. A
  new useAnchorRect hook re-measures on capture-phase scroll, resize, and
  ResizeObserver, bailing out when the rect is unchanged so a stationary
  anchor does not force a re-render per scroll tick.

- The (currently dead) Snapshotter error-append handlers in Editor.tsx
  used a non-functional setState read-then-write; converted both to the
  functional updater form matching the other concurrent-safe append sites
  so they cannot clobber a concurrent append if the Snapshotter is
  re-enabled.

Fixes #720
Fixes #710
Fixes #728
After the body-parser 1->2 upgrade (with Express 4->5), req.body is
undefined -- not {} -- for empty-body or wrong-Content-Type requests. The
POST /api/projects and PATCH /api/user handlers dereferenced req.body
without guarding, so a malformed request threw a TypeError that Express 5
surfaced as a generic 500 instead of the intended 400, logging a stack
that masks real 500s in monitoring.

Extract the body validation into pure, dependency-free validators
(api-validation.ts) that treat a missing/undefined body as a bad request,
wire them into the two handlers, and unit-test them in isolation. Keeping
them out of api.ts means the validator tests do not pull the DB/protobuf/
engine-WASM surface into their module graph.

Fixes #691
@codecov

codecov Bot commented Jun 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.61%. Comparing base (090c254) to head (ab2bb8e).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #730   +/-   ##
=======================================
  Coverage   90.61%   90.61%           
=======================================
  Files         224      224           
  Lines      135889   135889           
=======================================
+ Hits       123132   123133    +1     
+ Misses      12757    12756    -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@claude

claude Bot commented Jun 8, 2026

Copy link
Copy Markdown

Review

Reviewed all four fixes. No blocking issues found.

Notes (not blocking)

adjustFlows early return discards adjusted points — src/diagram/drawing/Flow.tsx:566-568. The early return uses return flow rather than return { ...flow, points }, so the .map()-computed point adjustments (both endpoints follow the moved stock since both have attachedToUid === stock.uid) are discarded. For a degenerate self-loop, dragging the stock visually detaches the flow from it. This is consistent with the comment ("Leave it unchanged rather than throwing") and the test (expect(newFlow.points).toEqual(flow.points)), so it appears intentional as a safe-default; flagging in case the intent was to keep the endpoints attached to the moving stock.

What I verified

  • adjustFlows is private; only caller is UpdateCloudAndFlow:804, which the new test exercises end-to-end.
  • setState in Editor.tsx is the custom class-parity merge helper that accepts both partial and functional-updater forms (Editor.tsx:370-378).
  • Menu hook: capture-phase listener on window is the right choice for catching non-bubbling scroll events from nested scroll containers; rect identity-preserving setState prevents per-tick re-renders on stationary anchors; cleanup symmetric.
  • Validators: behavior preserved for non-empty bodies; the typeof body !== 'object' || body === null guard is what closes the undefined/null -> TypeError -> 500 path under body-parser 2 / Express 5.

Overall correctness verdict

Correct. The patch fixes the four targeted issues with appropriate test coverage and does not introduce regressions in the paths I traced.

@bpowers
bpowers merged commit cde353c into main Jun 8, 2026
15 checks passed
@bpowers
bpowers deleted the frontend-issue-burndown-2026-06 branch June 8, 2026 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment