Files
loki/scripts/responsive-audit.mjs
T
GandClaude Opus 5 caa0e41c3b chore: add a formatter (#449)
* chore: add a formatter, matching the style this repo already writes

singleQuote=false was chosen by counting this repo's own
imports, not by fleet decree. The fleet is genuinely split and the two repos
that already had a .prettierrc disagreed with each other, so there was no
standard to restore. Quote style does not cross repo boundaries; having a
gate does.

Markdown is ignored for now — prettier rewraps prose, which would bury the
real diff.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* style: format with prettier (1062 files)

Mechanical. No behaviour change. This SHA is listed in
.git-blame-ignore-revs so `git blame` skips it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* chore: teach git blame to skip the reformat

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(lint): reattach two guards the reformat detached

Both effects were one-liners, so a trailing eslint-disable-line covered the
setState call:

  useEffect(() => { setSelected(new Set()); }, [q.length]); // eslint-disable-line ...

prettier expanded the block. The comment stayed on the closing line while the
call moved above it, so the guard protected nothing and the rule its author had
deliberately silenced failed the build.

This is the second shape of the same defect — hirnli had the eslint-disable-NEXT-line
variant. Both come from the same fact: an eslint directive binds to a line
number, so reformatting can silently unbind it, which makes 'mechanical, no
behaviour change' false for any bulk reformat.

* chore(desktop): bump the runner version the reformat requires

desktop-release-drift enforces that anything built into Fleet Runner which
differs from the last released tag must carry a version ahead of that tag —
main read 0.8.15 and fleet-runner-v0.8.15 already shipped.

A whole-repo reformat changes bytes under desktop/, so the gate fires. The
alternative was adding desktop/ to .prettierignore, which would buy a quiet PR
by carving a permanent hole in the formatter's coverage — the precise shape of
silent drift this test was written to catch. Bumping is what the rule asks for
and costs one patch release whose only content is whitespace.

* chore: merge main and re-format

A whole-repo reformat conflicts with any concurrent commit, and a conflicting PR
runs no checks at all while reporting nothing. Conflicts resolved to main,
which is correct because this branch changes formatting and nothing else.

* chore: leave desktop/ out of this reformat

desktop-release-drift is right to refuse this. Everything under desktop/ is
built into Fleet Runner, so changing it means shipping: bump the version AND add
a FLEET_RUNNER_RELEASES entry, whose stated audience is 'person who installed
Fleet Runner and wants to know what changed'. For a whitespace-only build the
honest answer is 'nothing', so that release should not exist — and I should not
write release notes claiming otherwise.

I had bumped to 0.8.16 to get the gate green. That was the wrong direction:
publishing a build to three OS runners to keep a formatting PR quiet. Reverted.

desktop/ is excluded in .prettierignore with the reasoning recorded there, to be
formatted alongside the next real runner change, where the bump and the notes are
true anyway.

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-31 09:23:30 +00:00

235 lines
7.5 KiB
JavaScript

import fs from "node:fs";
import path from "node:path";
import { chromium } from "playwright";
const root = process.cwd();
const outDir = path.join(root, ".tmp", "responsive-audit");
fs.mkdirSync(outDir, { recursive: true });
function readLocalEnv() {
const envPath = path.join(root, ".env.local");
if (!fs.existsSync(envPath)) return;
const lines = fs.readFileSync(envPath, "utf8").split(/\r?\n/);
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith("#")) continue;
const idx = trimmed.indexOf("=");
if (idx === -1) continue;
const key = trimmed.slice(0, idx);
const value = trimmed.slice(idx + 1).replace(/^"|"$/g, "");
if (!process.env[key]) process.env[key] = value;
}
}
readLocalEnv();
const base = process.env.BASE ?? "http://localhost:3002";
const ownerPassword = process.env.LOCAL_AUTH_PASSWORD;
const dogfoodEmail = process.env.DOGFOOD_EMAIL;
const dogfoodPassword = process.env.DOGFOOD_PASSWORD;
const isLocal = base.includes("localhost") || base.includes("127.0.0.1");
const routes = [
"/today",
"/control",
"/loki",
"/terminal?source=server&tab=fleetcrown",
"/projects",
"/prompts",
"/activity",
"/history",
"/decisions",
"/system",
"/settings",
"/people",
"/crew",
"/goals",
"/habits",
"/events",
"/money",
"/thoughts",
];
const viewports = [
{ name: "mobile", width: 390, height: 844, isMobile: true },
{ name: "desktop", width: 1440, height: 1000, isMobile: false },
];
function slug(route) {
return route.replace(/^\//, "").replace(/\//g, "-") || "home";
}
async function login(page) {
await page.goto(`${base}/sign-in?callbackUrl=/today`, { waitUntil: "domcontentloaded" });
if (isLocal) {
const ownerTab = page.getByRole("button", { name: /owner key/i });
if (await ownerTab.count()) await ownerTab.click();
if (!ownerPassword) throw new Error("LOCAL_AUTH_PASSWORD is not configured");
await page.locator('input[type="password"]').first().fill(ownerPassword);
} else {
if (!dogfoodEmail || !dogfoodPassword)
throw new Error("DOGFOOD_EMAIL/DOGFOOD_PASSWORD required for production audit");
await page.locator('input[type="email"]').first().fill(dogfoodEmail);
await page.locator('input[type="password"]').first().fill(dogfoodPassword);
}
await page
.getByRole("button", { name: /sign in|continue|unlock/i })
.last()
.click();
await page.waitForURL((url) => !url.pathname.startsWith("/sign-in"), { timeout: 15000 });
}
async function analyze(page, viewportName) {
return page.evaluate((vpName) => {
const vw = document.documentElement.clientWidth;
const vh = window.innerHeight;
const body = document.body;
const doc = document.documentElement;
const all = [...document.querySelectorAll("*")];
const textNodes = all
.map((el) => {
const rect = el.getBoundingClientRect();
const style = window.getComputedStyle(el);
const text = (el.textContent || "").trim().replace(/\s+/g, " ");
return {
tag: el.tagName.toLowerCase(),
text: text.slice(0, 80),
size: parseFloat(style.fontSize),
visible:
rect.width > 0 &&
rect.height > 0 &&
style.visibility !== "hidden" &&
style.display !== "none",
};
})
.filter((x) => x.visible && x.text && x.size > 0 && x.size < (vpName === "mobile" ? 12 : 11))
.slice(0, 30);
const overflow = all
.map((el) => {
const rect = el.getBoundingClientRect();
const style = window.getComputedStyle(el);
const text = (el.textContent || "").trim().replace(/\s+/g, " ");
return {
tag: el.tagName.toLowerCase(),
cls: typeof el.className === "string" ? el.className.slice(0, 120) : "",
text: text.slice(0, 80),
right: Math.round(rect.right),
left: Math.round(rect.left),
width: Math.round(rect.width),
visible:
rect.width > 0 &&
rect.height > 0 &&
style.visibility !== "hidden" &&
style.display !== "none",
};
})
.filter((x) => x.visible && (x.right > vw + 2 || x.left < -2))
.slice(0, 30);
const targets = all
.filter((el) => el.matches("button,a,input,select,textarea,[role='button']"))
.map((el) => {
const rect = el.getBoundingClientRect();
const style = window.getComputedStyle(el);
const text = (
el.getAttribute("aria-label") ||
el.textContent ||
el.getAttribute("title") ||
""
)
.trim()
.replace(/\s+/g, " ");
return {
tag: el.tagName.toLowerCase(),
text: text.slice(0, 80),
width: Math.round(rect.width),
height: Math.round(rect.height),
visible:
rect.width > 0 &&
rect.height > 0 &&
style.visibility !== "hidden" &&
style.display !== "none",
};
})
.filter((x) => x.visible && vpName === "mobile" && (x.width < 36 || x.height < 36))
.slice(0, 40);
return {
viewport: vpName,
title: document.title,
url: location.pathname,
scrollHeight: Math.round(doc.scrollHeight),
viewportHeight: vh,
scrollScreens: Number((doc.scrollHeight / vh).toFixed(2)),
clientWidth: vw,
scrollWidth: Math.max(body.scrollWidth, doc.scrollWidth),
overflow,
tinyText: textNodes,
smallTargets: targets,
};
}, viewportName);
}
const browser = await chromium.launch({
headless: true,
executablePath: process.env.CHROME_PATH || "/usr/bin/google-chrome",
});
const results = [];
try {
const context = await browser.newContext({
viewport: { width: 390, height: 844 },
deviceScaleFactor: 1,
});
const page = await context.newPage();
await login(page);
await context.storageState({ path: path.join(outDir, "storage.json") });
await context.close();
for (const vp of viewports) {
const ctx = await browser.newContext({
storageState: path.join(outDir, "storage.json"),
viewport: { width: vp.width, height: vp.height },
isMobile: vp.isMobile,
deviceScaleFactor: 1,
});
const p = await ctx.newPage();
for (const route of routes) {
try {
await p.goto(`${base}${route}`, { waitUntil: "load", timeout: 20000 });
} catch (err) {
console.error(`nav failed ${route}: ${err.message?.split("\n")[0]}`);
continue;
}
// Let client-side fetches and first paint settle without waiting for SSE to go idle.
await p.waitForTimeout(1200);
await p.screenshot({
path: path.join(outDir, `${vp.name}-${slug(route)}.png`),
fullPage: true,
});
results.push(await analyze(p, vp.name));
}
await ctx.close();
}
} finally {
await browser.close();
}
fs.writeFileSync(path.join(outDir, "report.json"), JSON.stringify(results, null, 2));
for (const result of results) {
const problems = [
result.scrollWidth > result.clientWidth
? `overflow ${result.scrollWidth}/${result.clientWidth}`
: null,
result.tinyText.length ? `tinyText ${result.tinyText.length}` : null,
result.smallTargets.length ? `smallTargets ${result.smallTargets.length}` : null,
]
.filter(Boolean)
.join(", ");
console.log(
`${result.viewport.padEnd(7)} ${result.url.padEnd(12)} screens=${result.scrollScreens}${problems ? ` ${problems}` : ""}`,
);
}
console.log(`report ${path.join(outDir, "report.json")}`);