diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 271400e..78896db 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,7 +10,8 @@ The rules live in [AGENTS.md](AGENTS.md). Read its non-negotiables before your f - Report a bug, with steps to reproduce and no personal data. - Propose a feature. Read [VISION.md](VISION.md) first. - Run Hermit on your phone and send device data: [docs/devices.md](docs/devices.md) explains how. -- Improve the docs, or the Spanish and English copy. +- Improve the docs, or the Spanish and English copy. [How copy works](packages/copy/README.md) explains how to add a + string. - Review open pull requests. Reading and running someone's change helps too. ## Set up diff --git a/apps/mobile/preview/package.json b/apps/mobile/preview/package.json index 30ef2fb..c8f4f3f 100644 --- a/apps/mobile/preview/package.json +++ b/apps/mobile/preview/package.json @@ -7,7 +7,7 @@ "type": "module", "scripts": { "build": "node build.mjs", - "test": "node --test device-lab.test.mjs browser.test.mjs", + "test": "node --test device-lab.test.mjs browser.test.mjs pick.test.mjs", "shoot": "node shoot.mjs", "compress": "python3 -c \"import glob; from PIL import Image; [Image.open(f).convert('RGB').quantize(colors=256, dither=Image.Dither.NONE).save(f, optimize=True) for f in glob.glob('out/screens/*.png')]\"", "clips": "node clips.mjs", diff --git a/apps/mobile/preview/pick.mjs b/apps/mobile/preview/pick.mjs new file mode 100644 index 0000000..8c3f2c5 --- /dev/null +++ b/apps/mobile/preview/pick.mjs @@ -0,0 +1,16 @@ +// Dev-only and pure: the shots one run of shoot.mjs takes, so the filter can be checked without a browser. +// A shot is taken for each of its themes and languages, as a file named `${shot.file}-${theme}-${lang}.png`; +// a filter keeps only the files that contain it, as the loop in shoot.mjs always did. +export function pickShots(shots, filter) { + const picked = []; + for (const shot of shots) { + for (const theme of shot.themes) { + for (const lang of shot.langs) { + const file = `${shot.file}-${theme}-${lang}.png`; + if (filter && !file.includes(filter)) continue; + picked.push({ shot, theme, lang, file }); + } + } + } + return picked; +} diff --git a/apps/mobile/preview/pick.test.mjs b/apps/mobile/preview/pick.test.mjs new file mode 100644 index 0000000..01eaed9 --- /dev/null +++ b/apps/mobile/preview/pick.test.mjs @@ -0,0 +1,49 @@ +// #227: which shots a run takes. Pure picking, so the filter can be checked without a browser. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { pickShots } from "./pick.mjs"; + +const shots = [ + { file: "about", themes: ["light", "dark"], langs: ["es-419", "en"] }, + { file: "wallet-home", themes: ["light"], langs: ["es-419"] }, + { file: "bot-chat", themes: ["dark"], langs: ["en", "es-419"] }, +]; + +test("no filter keeps every theme and language of every shot, in order", () => { + const picked = pickShots(shots, undefined); + assert.deepEqual(picked.map((p) => p.file), [ + "about-light-es-419.png", + "about-light-en.png", + "about-dark-es-419.png", + "about-dark-en.png", + "wallet-home-light-es-419.png", + "bot-chat-dark-en.png", + "bot-chat-dark-es-419.png", + ]); + assert.equal(picked[0].shot, shots[0]); + assert.deepEqual(picked.map((p) => [p.theme, p.lang]), [ + ["light", "es-419"], + ["light", "en"], + ["dark", "es-419"], + ["dark", "en"], + ["light", "es-419"], + ["dark", "en"], + ["dark", "es-419"], + ]); +}); + +test("an empty filter is no filter, as the loop in shoot.mjs treats it", () => { + assert.equal(pickShots(shots, "").length, 7); +}); + +test("a filter keeps only the files that contain it", () => { + assert.deepEqual(pickShots(shots, "wallet").map((p) => p.file), ["wallet-home-light-es-419.png"]); + assert.equal(pickShots(shots, "about").length, 4); + // The filter matches the whole file name, theme and language included, as before. + assert.deepEqual(pickShots(shots, "dark-en").map((p) => p.file), ["about-dark-en.png", "bot-chat-dark-en.png"]); +}); + +test("a filter that matches nothing gives an empty list", () => { + assert.deepEqual(pickShots(shots, "abuot"), []); + assert.deepEqual(pickShots(shots, "nothing-matches-this"), []); +}); diff --git a/apps/mobile/preview/shoot.mjs b/apps/mobile/preview/shoot.mjs index fcb1fba..7260835 100644 --- a/apps/mobile/preview/shoot.mjs +++ b/apps/mobile/preview/shoot.mjs @@ -7,11 +7,23 @@ import { fileURLToPath } from "node:url"; import { chromium } from "playwright-core"; import { chromiumPath } from "./browser.mjs"; import { shots } from "./shots.mjs"; +import { pickShots } from "./pick.mjs"; import { audit, words } from "./audit.mjs"; const here = dirname(fileURLToPath(import.meta.url)); const dist = join(here, "dist"); const outDir = join(here, "out/screens"); + +const filter = process.argv[2]; +if (!existsSync(join(dist, "index.html"))) { + console.error("No build found. Run pnpm build first."); + process.exit(1); +} +const picks = pickShots(shots, filter); +if (!picks.length) { + console.error(`No shot matches "${filter}". Examples: ${shots.slice(0, 3).map((s) => s.file).join(", ")}.`); + process.exit(1); +} mkdirSync(outDir, { recursive: true }); const types = { ".html": "text/html", ".js": "text/javascript", ".png": "image/png", ".json": "application/json", ".ttf": "font/ttf" }; @@ -29,50 +41,43 @@ const server = createServer((req, res) => { await new Promise((r) => server.listen(0, "127.0.0.1", r)); const base = `http://127.0.0.1:${server.address().port}/`; -const filter = process.argv[2]; const browser = await chromium.launch({ executablePath: chromiumPath() }); const results = []; -for (const shot of shots) { - for (const theme of shot.themes) { - for (const lang of shot.langs) { - const file = `${shot.file}-${theme}-${lang}.png`; - if (filter && !file.includes(filter)) continue; - const ctx = await browser.newContext({ viewport: { width: 412, height: 915 }, deviceScaleFactor: 2, locale: lang, colorScheme: theme }); - const page = await ctx.newPage(); - const consoleErrors = []; - // React DOM's nesting warnings (a Pressable inside a Pressable becomes