Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Add pnpm shots: a UI PR's screenshots at three widths in both skins

Leigh12-93cc26a5f903b5
diff --git a/.gitignore b/.gitignore
index 0c17a74e7c..b516194e80 100644
--- a/.gitignore
+++ b/.gitignore
@@ -16,6 +16,7 @@ electron/resources/OpenMausBot Speech.app
electron/resources/OpenMausBot Recorder.app
release
.omb-scratch
+/shots/
.wrangler/
.dev.vars
.dev.vars.*
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 6f5eeb0eac..94da7fe21f 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -14,6 +14,8 @@ makes a PR easy to merge. Read it once before opening anything; it's short on pu
- **Keep it green.** `pnpm typecheck && pnpm test` must pass. Server changes need tests (see below).
- **UI changes follow [`DESIGN.md`](DESIGN.md).** Tokens, type, spacing, components, words and
states. Before/after screenshots in Midnight and Daylight go in the PR body; video for anything animated.
+ With `pnpm dev` running, `pnpm shots /some-preview.html?state=x --name before` saves them at 390, 768
+ and 1440 wide in both skins to `shots/`.
## Dev setup
diff --git a/package.json b/package.json
index eb638c7f89..2750e2c075 100644
--- a/package.json
+++ b/package.json
@@ -36,6 +36,7 @@
"dev:server": "node --experimental-strip-types server/dev-server.ts",
"mcp": "node --experimental-strip-types scripts/mcp-server.ts",
"control:omb": "node --experimental-strip-types scripts/control-omb.ts",
+ "shots": "node --experimental-strip-types scripts/shots.ts",
"eval": "node --experimental-strip-types evals/runners/run-evals.ts",
"dev:desktop": "node scripts/prepare-cloudflared.mjs --current && electron .",
"build": "tsc -b && tsc -p tsconfig.server.json && vite build",
diff --git a/scripts/shots.test.ts b/scripts/shots.test.ts
new file mode 100644
index 0000000000..2ae92000d4
--- /dev/null
+++ b/scripts/shots.test.ts
@@ -0,0 +1,38 @@
+// Checked-against: scripts/shots.ts, the one script these cases cover.
+import { join } from "node:path";
+import { describe, expect, it } from "vitest";
+
+import { nameFor, parseShotsArgs, shotPlan } from "./shots.ts";
+
+describe("pnpm shots", () => {
+ it("photographs the app's own page into shots/ by default", () => {
+ expect(parseShotsArgs([])).toEqual({ path: "/", name: "app", url: "http://127.0.0.1:5199", out: "shots", wait: "#root > *" });
+ });
+
+ it("names a preview's pictures after its page and state, unless --name says", () => {
+ expect(nameFor("/room-card-preview.html?state=memory&layout=phone")).toBe("room-card-preview-state-memory-layout-phone");
+ expect(parseShotsArgs(["/room-card-preview.html", "--name", "after"]).name).toBe("after");
+ });
+
+ it("refuses a path that is not the app's, a second path and a name that is not a file name", () => {
+ expect(() => parseShotsArgs(["room-card-preview.html"])).toThrow(/starts with "\/"/);
+ expect(() => parseShotsArgs(["/", "/rail"])).toThrow(/usage/);
+ expect(() => parseShotsArgs(["--name", "../before"])).toThrow(/--name/);
+ });
+
+ it("covers phone, tablet and desktop in Midnight and Daylight, each with its own file and skin in the address", () => {
+ const plan = shotPlan(parseShotsArgs(["/one-home-preview.html?state=menu", "--name", "before", "--out", "pr"]));
+ expect(plan.map(({ width, height, skin, scheme }) => `${width}x${height} ${skin} ${scheme}`)).toEqual([
+ "390x844 midnight dark", "768x1024 midnight dark", "1440x900 midnight dark",
+ "390x844 daylight light", "768x1024 daylight light", "1440x900 daylight light",
+ ]);
+ expect(plan[0]).toMatchObject({ url: "http://127.0.0.1:5199/one-home-preview.html?state=menu&skin=midnight", file: join("pr", "before-390-midnight.png"), mobile: true });
+ expect(plan[5]).toMatchObject({ url: "http://127.0.0.1:5199/one-home-preview.html?state=menu&skin=daylight", file: join("pr", "before-1440-daylight.png"), mobile: false });
+ expect(new Set(plan.map(({ file }) => file)).size).toBe(6);
+ });
+
+ it("replaces a skin the path already names rather than sending two", () => {
+ const [shot] = shotPlan(parseShotsArgs(["/?skin=pop"]));
+ expect(new URL(shot!.url).searchParams.getAll("skin")).toEqual(["midnight"]);
+ });
+});
diff --git a/scripts/shots.ts b/scripts/shots.ts
new file mode 100644
index 0000000000..c796a29da3
--- /dev/null
+++ b/scripts/shots.ts
@@ -0,0 +1,105 @@
+// The before and after pictures a UI PR carries (CONTRIBUTING.md): one page of the running `pnpm dev` app at a phone, a
+// tablet and a desktop width, in Midnight and Daylight, with a headless Chrome in a throwaway profile (OMB_CDP_CHROME,
+// else the installed Google Chrome).
+// Checked-against: scripts/verify-cloud-rail.ts, which photographs only the rail preview's own views at one size.
+//
+// pnpm shots [PATH] [--name before|after] [--url http://127.0.0.1:5199] [--out shots] [--wait SELECTOR]
+//
+// PATH is the app's own page ("/") or a preview such as "/room-card-preview.html?state=memory". It fails when the page
+// does not wear the skin it was asked for, so a picture is never mislabelled.
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join, resolve } from "node:path";
+import { pathToFileURL } from "node:url";
+import { parseArgs } from "node:util";
+
+import { openHeadlessChrome } from "./testing/headless-chrome.ts";
+
+export const SIZES = [
+ { width: 390, height: 844, mobile: true },
+ { width: 768, height: 1024, mobile: true },
+ { width: 1440, height: 900, mobile: false },
+] as const;
+export const SKINS = [
+ { id: "midnight", scheme: "dark" },
+ { id: "daylight", scheme: "light" },
+] as const;
+
+export interface ShotsOptions {
+ path: string;
+ name: string;
+ url: string;
+ out: string;
+ wait: string;
+}
+
+export interface Shot {
+ url: string;
+ file: string;
+ width: number;
+ height: number;
+ mobile: boolean;
+ skin: (typeof SKINS)[number]["id"];
+ scheme: (typeof SKINS)[number]["scheme"];
+}
+
+const USAGE = "usage: pnpm shots [PATH] [--name NAME] [--url http://127.0.0.1:5199] [--out shots] [--wait SELECTOR]";
+
+/** "/room-card-preview.html?state=memory" → "room-card-preview-state-memory"; the app's own page is "app". */
+export const nameFor = (path: string) => path.replace(/\.html\b/, "").replace(/[^a-z0-9]+/gi, "-").replace(/^-|-$/g, "").toLowerCase() || "app";
+
+export function parseShotsArgs(argv: string[]): ShotsOptions {
+ const { values, positionals } = parseArgs({
+ args: argv,
+ allowPositionals: true,
+ options: {
+ name: { type: "string" },
+ url: { type: "string", default: "http://127.0.0.1:5199" },
+ out: { type: "string", default: "shots" },
+ wait: { type: "string", default: "#root > *" },
+ },
+ });
+ if (positionals.length > 1) throw new Error(USAGE);
+ const path = positionals[0] ?? "/";
+ if (!path.startsWith("/")) throw new Error(`PATH starts with "/", such as /room-card-preview.html\n${USAGE}`);
+ if (values.name !== undefined && !/^[\w-]+$/.test(values.name)) throw new Error("--name takes letters, digits, - and _");
+ return { path, name: values.name ?? nameFor(path), url: values.url!, out: values.out!, wait: values.wait! };
+}
+
+/** Every size in every skin: the page's address with `skin=` set (the previews read it) and the picture's file. */
+export function shotPlan(options: ShotsOptions): Shot[] {
+ return SKINS.flatMap(({ id, scheme }) => SIZES.map((size) => {
+ const url = new URL(options.path, options.url);
+ url.searchParams.set("skin", id);
+ return { ...size, url: url.href, skin: id, scheme, file: join(options.out, `${options.name}-${size.width}-${id}.png`) };
+ }));
+}
+
+async function main(argv: string[]): Promise<void> {
+ const options = parseShotsArgs(argv);
+ mkdirSync(resolve(options.out), { recursive: true });
+ const profile = mkdtempSync(join(tmpdir(), "omb-shots-chrome-"));
+ const page = await openHeadlessChrome(profile);
+ try {
+ for (const shot of shotPlan(options)) {
+ // The app keeps its skin in localStorage; set before its first paint, like a person's own choice.
+ const { identifier } = await page.send("Page.addScriptToEvaluateOnNewDocument", { source: `try { localStorage.setItem("omb-skin", ${JSON.stringify(shot.skin)}) } catch {}` });
+ await page.send("Emulation.setDeviceMetricsOverride", { width: shot.width, height: shot.height, deviceScaleFactor: 2, mobile: shot.mobile });
+ await page.send("Emulation.setEmulatedMedia", { features: [{ name: "prefers-color-scheme", value: shot.scheme }] });
+ await page.evaluate("globalThis.ombShotsLeft = true");
+ await page.send("Page.navigate", { url: shot.url });
+ await page.until(`!globalThis.ombShotsLeft && document.readyState === "complete" && document.querySelector(${JSON.stringify(options.wait)})`, 30_000);
+ await page.evaluate("document.fonts.ready.then(() => new Promise((done) => requestAnimationFrame(() => requestAnimationFrame(done))))");
+ const worn = await page.evaluate("document.documentElement.dataset.skin");
+ if (worn !== shot.skin) throw new Error(`${shot.url} wears "${worn}", not ${shot.skin}`);
+ writeFileSync(resolve(shot.file), await page.capture());
+ await page.send("Page.removeScriptToEvaluateOnNewDocument", { identifier });
+ console.log(shot.file);
+ }
+ } finally {
+ page.close();
+ rmSync(profile, { recursive: true, force: true, maxRetries: 5, retryDelay: 200 });
+ }
+}
+
+if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) await main(process.argv.slice(2));