Skip to content

Commit 7abc16c

Browse files
committed
Give the audits one seam to the app, and widen the disclosure audit
targets.js supplies the list of screens, the three roles, signIn and visit -- replacing 21 hand-copied signIn functions, seven copies of the role predicates and six of the targets-file read. Four of the copies had drifted: one did not sign out first, so a second role silently reused the first one's session, and one waited on networkidle, which never settles on the slowest screens here. The targets file regenerates when it is older than config/routes.rb, so an audit cannot run against a stale list of screens. TARGETS_CMD overrides the command that builds it; that one line is the whole Rails dependency, which is what makes the rest of the suite portable. disclosure-audit is the first onto it: five hardcoded pages become 150 screens and 5 reveals become 19. Widening it produced one finding, and it was the audit's fault. A trigger inside a collapsed accordion on /partners/profile/edit has a zero-size rect at the origin, so its left edge is 0 and matches no grid column. An element with no box cannot be misaligned. Confirmed by measuring before changing anything, and the check still reports a real misalignment when one is planted. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 248167f commit 7abc16c

3 files changed

Lines changed: 144 additions & 22 deletions

File tree

‎bin/design/README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,18 @@ screens that a single-role pass had never visited.
293293
One definition of each check, with the scope as an argument: a second script for the thorough run
294294
would be a copy, and a copy drifts.
295295

296+
`targets.js` is the seam between the audits and the application, and the only file that knows this
297+
is a Rails app. It supplies the list of screens, the three roles, `signIn` and `visit`.
298+
299+
It replaced **21 hand-copied `signIn` functions**, seven copies of the role predicates and six of the
300+
targets-file read. Four of the copies had drifted: one did not sign out first, so a second role
301+
silently reused the first one's session, and one waited on `networkidle`, which never settles on the
302+
slowest screens here.
303+
304+
The targets file regenerates itself when it is missing or older than `config/routes.rb`, so an audit
305+
cannot quietly run against a stale list of screens. `TARGETS_CMD` overrides the command that builds
306+
it — that one line is the whole Rails dependency.
307+
296308
`audit-selftest.js` tests the audits, by breaking a page on purpose and by leaving it alone on
297309
purpose.
298310

‎bin/design/disclosure-audit.js‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,14 @@
2020
//
2121
// Usage: pw bin/design/disclosure-audit.js
2222
const { chromium } = require("playwright");
23+
const { targets, signIn, visit, RUNS } = require("./targets");
2324

24-
const BASE = process.env.BASE_URL || "http://127.0.0.1:3000";
25-
const PASSWORD = process.env.SEED_PASSWORD || "password!";
26-
27-
const PAGES = {
28-
bank: ["/distributions/new", "/manage/edit", "/partner_groups/new", "/partners/new"],
29-
super: ["/admin/users/new"]
30-
};
31-
const ROLES = { bank: "org_admin1@example.com", super: "superadmin@example.com" };
32-
33-
async function signIn(page, email) {
34-
await page.goto(BASE + "/users/sign_out", { waitUntil: "domcontentloaded" }).catch(() => {});
35-
await page.goto(BASE + "/users/sign_in", { waitUntil: "domcontentloaded" });
36-
await page.fill("#user_email", email);
37-
await page.fill("#user_password", PASSWORD);
38-
await Promise.all([page.waitForNavigation(), page.click("input[type=submit], button[type=submit]")]);
39-
}
25+
/*
26+
* **Every screen, not a list of five.** This used to name four bank pages and one admin page, and
27+
* a conditionally revealed field on any other screen was invisible to it -- which is the same
28+
* blindness that let the tooltip audit report 0 defects while there were 14. The check already
29+
* skips a page with no reveals on it, so widening costs nothing but time and can only find more.
30+
*/
4031

4132
const COLLECT = () => {
4233
// A reveal is an element some control points at with aria-controls, plus the two legacy shapes
@@ -115,7 +106,12 @@ const COLLECT = () => {
115106
triggerX: m.t ? m.t.x : null,
116107
// Only meaningful where the trigger is actually laid out on a grid: several forms are a
117108
// plain stacked card, where there are no column lines to land on.
118-
triggerOnColumn: (trigger && trigger.closest("main .grid"))
109+
//
110+
// **And only where it is laid out at all.** A trigger inside a collapsed accordion has a
111+
// zero-size rect at the origin, so its left edge is 0 and matches no column -- reported as
112+
// "trigger left edge 0 is on no grid column" the first time this audit was widened beyond
113+
// the five pages it used to know about. An element with no box cannot be misaligned.
114+
triggerOnColumn: (trigger && trigger.offsetParent !== null && trigger.closest("main .grid"))
119115
? [...columns].some((c) => Math.abs(c - m.t.x) <= 1) : null,
120116
// A radio or checkbox has its label beside it, so the revealed field should line up with
121117
// that label. A select's label sits above it at the same left edge, so it should not.
@@ -128,13 +124,17 @@ const COLLECT = () => {
128124
const browser = await chromium.launch();
129125
let found = 0, defects = 0;
130126

131-
for (const [role, paths] of Object.entries(PAGES)) {
127+
let visited = 0;
128+
for (const [email, wants] of RUNS) {
132129
const ctx = await browser.newContext({ viewport: { width: 1440, height: 1200 } });
133130
const page = await ctx.newPage();
134-
await signIn(page, ROLES[role]);
131+
await signIn(page, email);
135132

136-
for (const path of paths) {
137-
await page.goto(BASE + path, { waitUntil: "networkidle" }).catch(() => {});
133+
for (const { path } of targets().filter((t) => wants(t.path))) {
134+
// `visit` waits for `load`, never `networkidle`: three form pages here take longer than the
135+
// 30s idle timeout, and a page an audit gives up on is a page it reports as clean.
136+
if (!await visit(page, path)) continue;
137+
visited++;
138138
const reveals = await page.evaluate(COLLECT);
139139
if (!reveals.length) continue;
140140

@@ -169,6 +169,7 @@ const COLLECT = () => {
169169
}
170170

171171
await browser.close();
172-
console.log(`\n${found} conditional reveals, ${defects} with a defect.`);
172+
// The page count is part of the finding: "0 defects" means nothing without it.
173+
console.log(`\n${visited} screens, ${found} conditional reveals, ${defects} with a defect.`);
173174
process.exit(defects ? 1 : 0);
174175
})();

‎bin/design/targets.js‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Which screens exist, who can see them, and how to sign in.
2+
//
3+
// **This is the seam between the audits and the application.** Everything above it is a rule about
4+
// design -- is this control named, is this focus ring visible. Everything below it is Rails: how to
5+
// ask the router for a list of screens, and what the sign-in form looks like. An audit that reaches
6+
// past this file to talk to the app directly is an audit that cannot be pointed at another app.
7+
//
8+
// It replaces three things that were copied by hand across the suite: **21 copies of `signIn`**,
9+
// seven of the role predicates, and six of the targets-file read. Four of the `signIn` copies had
10+
// drifted -- one did not sign out first, so a second role silently reused the first one's session;
11+
// one waited on `networkidle`, which times out on the slowest screens in this app and has already
12+
// caused two audits to give up mid-run and report what they had as though it were the whole app.
13+
//
14+
// **Why a generated file rather than each audit shelling out to Rails.** The file is the contract:
15+
// a list of `{path, controller, action}`. Any framework can produce it -- `urls.py`, `route:list
16+
// --json`, a walk of an app directory -- and no audit needs to know which one did. Shelling out
17+
// would put `bin/rails` in twenty files and cost a Rails boot each time.
18+
//
19+
// Staleness is the obvious objection to a generated file, so it regenerates itself: if the file is
20+
// missing, or older than `config/routes.rb`, it is rebuilt before it is read.
21+
const fs = require("fs");
22+
const path = require("path");
23+
const { execSync } = require("child_process");
24+
25+
const BASE = process.env.BASE_URL || "http://127.0.0.1:3000";
26+
const PASSWORD = process.env.SEED_PASSWORD || "password!";
27+
28+
const TARGETS_FILE = process.env.TARGETS || "/tmp/targets.json";
29+
const ROOT = path.resolve(__dirname, "../..");
30+
// The one Rails-shaped line in this file. Override it and the rest works unchanged.
31+
const GENERATE = process.env.TARGETS_CMD ||
32+
"bin/rails runner bin/design/route-targets.rb";
33+
const ROUTES_FILE = path.join(ROOT, "config/routes.rb");
34+
35+
function stale() {
36+
if (!fs.existsSync(TARGETS_FILE)) return true;
37+
if (!fs.existsSync(ROUTES_FILE)) return false;
38+
return fs.statSync(TARGETS_FILE).mtimeMs < fs.statSync(ROUTES_FILE).mtimeMs;
39+
}
40+
41+
let cached = null;
42+
43+
// Every screen the router knows about, with a real record id substituted for `:id`.
44+
function targets() {
45+
if (cached) return cached;
46+
if (stale()) {
47+
process.stderr.write("targets.json is missing or older than config/routes.rb; regenerating\n");
48+
fs.writeFileSync(TARGETS_FILE, execSync(GENERATE, { cwd: ROOT, maxBuffer: 32 * 1024 * 1024 }));
49+
}
50+
cached = JSON.parse(fs.readFileSync(TARGETS_FILE, "utf8"));
51+
return cached;
52+
}
53+
54+
/*
55+
* Who can see what.
56+
*
57+
* `/partners/...` is the partner portal, except `/partners/12`, which is a bank user looking at a
58+
* partner record -- the two live under one prefix and are different applications.
59+
*/
60+
const PARTNER = (p) => (p.startsWith("/partners/") && !/^\/partners\/\d+/.test(p)) || p === "/partners/profile";
61+
const ADMIN = (p) => p.startsWith("/admin");
62+
const BANK = (p) => !ADMIN(p) && !PARTNER(p);
63+
64+
// The three passes an audit makes if it wants to see the whole app.
65+
const RUNS = [
66+
["org_admin1@example.com", BANK],
67+
["verified@example.com", PARTNER],
68+
["superadmin@example.com", ADMIN]
69+
];
70+
71+
/*
72+
* Sign in as somebody.
73+
*
74+
* **Signs out first**, because these run several roles through one browser context and a stale
75+
* session means the second role silently audits the first role's pages.
76+
*
77+
* Fields are found by `name`, not by `id`: the ids come from Devise's form builder and change if
78+
* the form is ever rebuilt, and one copy of this function was already selecting on `#user_email`
79+
* while another used the name.
80+
*
81+
* Waits for the URL to stop being the sign-in page rather than for `networkidle`, which never
82+
* settles on the slowest screens here.
83+
*/
84+
async function signIn(page, email, password = PASSWORD) {
85+
await page.goto(BASE + "/users/sign_out", { waitUntil: "domcontentloaded" }).catch(() => {});
86+
await page.goto(BASE + "/users/sign_in", { waitUntil: "domcontentloaded" });
87+
await page.fill('input[name="user[email]"]', email);
88+
await page.fill('input[name="user[password]"]', password);
89+
await page.click('form[action="/users/sign_in"] button[type="submit"], input[type=submit]');
90+
await page.waitForURL((u) => !u.pathname.includes("/sign_in"), { timeout: 60000 });
91+
}
92+
93+
/*
94+
* Go to a screen and wait for it to be usable.
95+
*
96+
* `domcontentloaded` plus `load`, never `networkidle`: three form pages in this app take longer
97+
* than the 30-second idle timeout, and an audit that gives up on a page reports it as clean.
98+
* Returns null for anything that did not load, so a caller can tell "not checked" from "fine".
99+
*/
100+
async function visit(page, urlPath, { timeout = 60000 } = {}) {
101+
const res = await page.goto(BASE + urlPath, { waitUntil: "domcontentloaded", timeout })
102+
.catch(() => null);
103+
if (!res || res.status() >= 400) return null;
104+
await page.waitForLoadState("load", { timeout: 15000 }).catch(() => {});
105+
await page.waitForTimeout(120);
106+
return res;
107+
}
108+
109+
module.exports = { BASE, PASSWORD, targets, signIn, visit, RUNS, PARTNER, ADMIN, BANK };

0 commit comments

Comments
 (0)