/** * EACH APP CARRIES ONLY ITS OWN PICTURES, FROM ONE LIST BOTH PHONES READ. * * Books build 11 on the iPhone carried every app's paintings (105 MB of * Loom's catalogue: trips' wallets, the amps, the cheques) where Books on * Android carried its own ~10 MB (14 Nov 2026). The list of * what each app carries is `apps/loom/materials.json`: the Android build * copies `everyApp` and the app's list into its APK, and the iPhone build * (`scripts/ios-flavour-pictures.py`, a build phase of every app target) * compiles `everyApp`, `iphoneShell` and the app's list into the flavour's * bundle. These rules hold the two builds to that one list, every name to an * imageset, and each app's own ground to what it carries. A picture drawn and * not carried is caught at the draw in a debug build (`CottonMedia.image`). */import { execFileSync } from"node:child_process";import { existsSync, readdirSync, readFileSync } from"node:fs";import { dirname, resolve } from"node:path";import { fileURLToPath } from"node:url";import { describe, expect, it } from"vitest";const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../..");const MATERIALS = resolve(REPO_ROOT, "apps/loom/materials.json");const MEDIA = resolve(REPO_ROOT, "apps/loom/Sources/CottonPaper/Media.xcassets");const FLAVOURS = resolve(REPO_ROOT, "apps/cotton/App/Flavours");/** Apps no iPhone builds (Cotton Voice): their flavour folder is Android's own (apps/android/app/flavours/<slug>). */const ANDROID_ONLY = newSet( (JSON.parse(readFileSync(resolve(REPO_ROOT, "apps/registry.json"), "utf8")).apps asArray<{ slug: string; ios: unknown }>).filter((a) => a.ios === null).map((a) => a.slug),);const flavourJson = (flavour: string) => resolve(ANDROID_ONLY.has(flavour) ? resolve(REPO_ROOT, "apps/android/app/flavours") : FLAVOURS, flavour, "flavour.json");typeMaterials = { everyApp: string[]; iphoneShell: string[]; apps: Record<string, string[]> };const materials: Materials = JSON.parse(readFileSync(MATERIALS, "utf8"));const imagesets = readdirSync(MEDIA) .filter((name) => name.endsWith(".imageset")) .map((name) => name.slice(0, -".imageset".length));/** What the iPhone build carries for `flavour`, as the build phase itself answers it. */function iphoneCarries(flavour: string): string[] {return execFileSync("python3", ["scripts/ios-flavour-pictures.py", "--list", flavour], { cwd: REPO_ROOT, encoding: "utf8" }) .split("\n") .filter(Boolean);}/** A ground's painted pictures, read off `FaceGround`'s `tile`, `lamp` and `overlay` switches. */function groundPictures(ground: string): string[] {const face = readFileSync(resolve(REPO_ROOT, "apps/loom/Sources/CottonFaces/Face.swift"), "utf8");const found: string[] = [];for (const property of ["tile", "lamp", "overlay"]) {const block = face.match(newRegExp(`public var ${property}: [A-Za-z]+\\? \\{\\n([\\s\\S]*?)\\n \\}`))?.[1]; expect(block, `FaceGround.${property}`).toBeTruthy();const line = block!.split("\n").find((one) => one.includes(`case .${ground}:`));const name = line?.match(/"([a-z0-9_-]+)"/)?.[1];if (name) found.push(name); }return found;}describe("the pictures each app carries", () => {/// RULE: every name apps/loom/materials.json lists (a trailing * a prefix) is an imageset of Loom's catalogue: a renamed or removed painting fails here, as it stops both builds. it("names only imagesets Loom holds", () => {const lists = { everyApp: materials.everyApp, iphoneShell: materials.iphoneShell, ...materials.apps };for (const [owner, names] ofObject.entries(lists)) {for (const name of names) {const hits = name.endsWith("*") ? imagesets.filter((one) => one.startsWith(name.slice(0, -1))) : imagesets.filter((one) => one === name); expect(hits.length, `${owner}: ${name}`).toBeGreaterThan(0); } } });/// RULE: every app with a list is a flavour of apps/cotton/App/Flavours, and the iPhone build carries for it exactly everyApp, iphoneShell and its own list: far fewer than the whole catalogue. it("gives each listed flavour its own pictures on the iPhone", () => {for (const [flavour, own] ofObject.entries(materials.apps)) { expect(existsSync(flavourJson(flavour)), flavour).toBe(true);if (ANDROID_ONLY.has(flavour)) continue;const carried = iphoneCarries(flavour);for (const name of own.filter((one) => !one.endsWith("*"))) expect(carried, `${flavour}: ${name}`).toContain(name);for (const name of carried) expect(imagesets, name).toContain(name); expect(carried.length, flavour).toBeLessThan(imagesets.length / 2); } });/// RULE: a flavour with no list carries the whole catalogue on the iPhone (nothing is left out of an app nobody has listed yet). it("leaves an unlisted flavour the whole catalogue", () => { expect(materials.apps.cotton).toBeUndefined(); expect(iphoneCarries("cotton")).toEqual([...imagesets].sort()); });/// RULE: each listed flavour carries the painted ground it stands on (FaceGround's tile, lamp and overlay for its flavour.json ground) on both phones. it("carries each app's own ground", () => {for (const flavour ofObject.keys(materials.apps)) {const ground: string | undefined = JSON.parse(readFileSync(flavourJson(flavour), "utf8")).ground;if (!ground) continue;for (const name of groundPictures(ground)) expect(materials.apps[flavour], `${flavour} stands on ${ground}: ${name}`).toContain(name); } });/// RULE: the list is ONE: Android's flavours.json names no pictures, its build reads apps/loom/materials.json, and every Android app has a list there. it("is the one list the Android build reads", () => {const android = JSON.parse(readFileSync(resolve(REPO_ROOT, "apps/android/app/flavours.json"), "utf8"));for (const [slug, entry] ofObject.entries(android)) {if (slug === "//") continue; expect(entry, slug).not.toHaveProperty("materials"); expect(materials.apps, slug).toHaveProperty(slug); }const gradle = readFileSync(resolve(REPO_ROOT, "apps/android/app/build.gradle.kts"), "utf8"); expect(gradle).toContain('repo.resolve("apps/loom/materials.json")'); expect(gradle).not.toContain('include("act-*.imageset'); });/// RULE: the iPhone app target runs the flavour-pictures phase after its resources, so every family cloned from it (ios-spec-gate.ts) carries its own pictures. it("is compiled into every iPhone app by a build phase", () => {const spec = readFileSync(resolve(REPO_ROOT, "apps/cotton/project.yml"), "utf8");const cotton = spec.slice(spec.indexOf("\ntargets:\n Cotton:\n"));const target = cotton.slice(0, cotton.indexOf("\n CottonShareExtension:") > 0 ? cotton.indexOf("\n CottonShareExtension:") : undefined); expect(target).toMatch(/postBuildScripts:\n\s+- name: Flavour pictures\n\s+script: python3 "\$SRCROOT\/\.\.\/\.\.\/scripts\/ios-flavour-pictures\.py"/); });});