Skip to content

Give cross-cutting concepts one owner and enforce architecture rules in tests - #97

Merged
andreogle merged 15 commits into
mainfrom
andre/architecture-governance
Nov 28, 2026
Merged

andreogle merged 15 commits into
mainfrom
andre/architecture-governance

Conversation

@andreogle

@andreogle andreogle commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Several concepts were each owned by more than one module, and the project's architecture rules were only checked in review. This PR gives each of those concepts one owner and adds tests that fail the build when a rule is broken.

Changes

  • Safe redirects: LocalPath replaces three separate redirect checks in UserAuth, LocaleController and FallbackController. Fix: the two referer-based checks accepted //host paths, which made Phoenix raise a 500. They now fall back to /.
  • Locale: ElixirReactStarter.Locale owns the supported and default locales. Plugs.Locale resolves the request locale and owns the cookie. Before, config reads and hardcoded "en" values were spread across five modules. UserAuth now handles authentication only.
  • Session and user data: UserAuth.put_user_session/2 is the only code that writes the session token. Before, SettingsController and ConnCase also wrote it. UserJSON.data/1 replaces SharedData's private serialize_user helper.
  • Link tokens: link-token contexts are now checked against a fixed list, so a misspelt context raises instead of issuing a token nothing will accept. The two token builders are merged into one, and SHA3-256 hashing happens in one place. Stored tokens still verify.
  • Mail: all mail goes through Email.deliver/1. Fix: delivery failures used to be silently discarded, and "Sending" was logged before anything was sent. Failures are now logged at error level. The sender address comes from the new :mail_from config value.
  • Forms: TextField replaces 11 hand-written fields on 6 pages. It links each error message to its input with aria-invalid and aria-describedby; no form did this before. components/ui.ts is deleted.
  • Smaller changes:
    • The language switcher's label is translated, and it uses lucide's chevron icon instead of its own SVG.
    • syncLocale.ts moves to i18n/.
    • The two dev file-watcher scripts are merged into one.
    • Comments in config/runtime.exs and ssr.tsx point to each other, because both work out Sentry's environment and release from the same variables.

Enforcement

test/architecture_test.exs checks the Elixir source and fails the build when any of these rules break:

  • core app code never references web code;
  • web code reaches the database only through contexts;
  • only LocalPath reads Referer;
  • only Locale reads the locale config;
  • only Email calls Mailer;
  • no serializer modules outside *_json.ex.

assets/js/architecture.test.ts covers two AGENTS.md rules Biome can't express: no raw try/catch outside result.ts, and no string-literal internal URLs. assets/biome.jsonc now bans raw <input>/<label>/<textarea> in pages and layouts (use TextField), and bans @heroicons/react and @radix-ui/themes imports. docs/architecture.md lists each rule and which module owns each concept.

Verification

  • mix precommit passed on every commit, with 270 tests.
  • mix dialyzer reports 0 errors.
  • scripts/e2e passed for every flow these changes touch: login, registration, email confirmation, resend confirmation, password reset, change email, change password, delete account and locale.
  • A separate review of the diff found no security, session, stored-token or account-guessing regressions.

Records the architecture scan findings and the baseline, so each
refactoring round can be tracked against them.
UserAuth, LocaleController and FallbackController each validated
user-controlled redirect targets with their own rules. The two
referer-based ones accepted protocol-relative paths (//host), which
made Phoenix raise instead of falling back to /.

LocalPath is now the single implementation. test/architecture_test.exs
pins it, along with the domain/web and Repo layering rules (ADR 0001).
Supported locales were read from config in three modules, the default
was hardcoded in two more, and request-locale resolution lived inside
the auth plug while its cookie was written by LocaleController.

ElixirReactStarter.Locale now owns the list and the default;
Plugs.Locale owns resolution and the cookie. An architecture test keeps
the config reads in one place.
…ser shape

SettingsController and ConnCase wrote the "user_token" session key
themselves; UserAuth.put_user_session/2 now does it for all callers.
SharedData's private serialize_user/1 becomes UserJSON.data/1, per the
*_json.ex rule, which an architecture test now enforces.
Link-token contexts were free strings, so a typo silently issued a
token no verifier would accept. Both UserToken and the Accounts API now
guard against a closed list. build_email_change_token/2 folds into
build_link_token/3, and the SHA3-256 call lives in one private hash/1.
Stored token format is unchanged.
Controllers piped emails into Mailer.deliver/1 and discarded the
result, so delivery failures vanished, and the builders logged
"Sending" before anything was sent. Email.deliver/1 now sends, logs
the outcome with the recipient redacted, and returns :ok. An
architecture test keeps it the only caller of the mailer.

Also folds the repeated invalid-link redirects in AuthController and
SettingsController into one helper each.
Six pages hand-wrote the same label, input and error markup eleven
times, and none linked the error to its input. TextField owns that
markup plus aria-invalid and aria-describedby, and replaces the shared
inputClass string (components/ui.ts, deleted). A node contract test
keeps raw inputs out of pages and layouts.
LocaleSelector carried its own chevron SVG next to lucide's, and an
English-only aria-label. syncLocale moves to i18n/, beside the setup it
drives; components/ holds UI primitives only.
watch-routes.cjs and watch-ssr-pages.cjs were two copies of the same
regenerate-on-change loop, each its own dev watcher process.
watch-generated.cjs runs both generators from one table.
- Serializer rule now sees guarded definitions; every rule fails if
  its glob matches no files instead of passing vacuously.
- TextField keeps a caller's aria-describedby alongside the error id.
- Email.deliver/1 test runs in its own sync module (global log level).
- AGENTS.md JSON-view rule matches practice: data/1, index/show with
  the first JSON endpoint.
The map-literal spec allowed only an :id key, so dialyzer flagged every
caller passing a full user struct.
The sender was hardcoded in Email, so every project and environment
had to edit code to change it. It is now the :mail_from config value,
read at runtime so an environment's config can override it.
runtime.exs and ssr.tsx derive Sentry's environment and release from the
same variables in two runtimes. Each now points to the other so an edit
to one prompts the matching edit.
Drops the task pool, progress log and ADR (the history lives in the
commits) and shortens the architecture map and AGENTS.md additions to
the rules and owners agents need.
… import bans to Biome

TextField-only form fields move from the node contract test to Biome's
noRestrictedElements, and the heroicons / radix-themes bans from
AGENTS.md become noRestrictedImports entries. architecture.test.ts now
holds the two AGENTS.md rules Biome can't express: no raw try/catch
outside result.ts, and no string-literal internal URLs.
@andreogle
andreogle merged commit 291f20d into main Nov 28, 2026
5 checks passed
@andreogle
andreogle deleted the andre/architecture-governance branch November 28, 2026 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant