Cotton code
163 lines · 9.0 KB · TypeScriptRaw
import { createHash } from "node:crypto";import { existsSync, readFileSync } from "node:fs";import path from "node:path";import { execFileSync } from "node:child_process";import sharp from "sharp";import { describe, expect, it } from "vitest";// The icons and launch screens of the four redesigned apps (scripts/draw-app-icons.mjs, scripts/gen-launch-screens.py):// every file each flavour's icon set and launch screen names is there, an icon is a 1024 square with no alpha (the// App Store refuses one), and no retired picture is left anywhere in the tree.const ROOT = path.resolve(__dirname, "../..");const FLAVOURS = path.join(ROOT, "apps/cotton/App/Flavours");/** A PNG's size and whether it carries alpha, read from its header (IHDR: width, height, bit depth, colour type). */function png(file: string): { width: number; height: number; alpha: boolean } {  const b = readFileSync(file);  expect(b.subarray(1, 4).toString("latin1"), file).toBe("PNG");  const type = b[25];  return { width: b.readUInt32BE(16), height: b.readUInt32BE(20), alpha: type === 4 || type === 6 };}type App = {  flavour: string;  set: string;  store: string;  /** The iPhone set also holds dark and tinted looks (an icon drawn here, not painted). */  looks: boolean;  /** The launch screen lays a mark (LaunchObject), and a night one. */  mark: boolean;  nightMark: boolean;  /** The icon is a wordmark: its ink spans most of the square's width (the full word, never a one-letter cut). */  wordmark?: boolean;};const APPS: App[] = [  { flavour: "story", set: "Story", store: "story", looks: true, mark: true, nightMark: false },  { flavour: "days", set: "Days", store: "days", looks: true, mark: true, nightMark: false },  { flavour: "notes", set: "Notes", store: "words", looks: true, mark: true, nightMark: true, wordmark: true },  { flavour: "messages", set: "Messages", store: "comms", looks: true, mark: true, nightMark: true },];/** * How much of the picture's width its mark spans, 0 to 1: the columns holding a pixel far from the corner's ground. * "words|" spans about three quarters of the square; the "w|" cut about half. */async function inkSpan(file: string): Promise<number> {  const { data, info } = await sharp(file).removeAlpha().raw().toBuffer({ resolveWithObject: true });  const at = (x: number, y: number) => (y * info.width + x) * info.channels;  const ground = [0, 1, 2].map((k) => data[at(4, 4) + k]);  let left = info.width, right = -1;  // Rows across the middle only, so a Dev band at the foot does not count.  for (let y = Math.round(info.height * 0.3); y < info.height * 0.7; y++) for (let x = 0; x < info.width; x++) {    const p = at(x, y);    if (Math.abs(data[p] - ground[0]) + Math.abs(data[p + 1] - ground[1]) + Math.abs(data[p + 2] - ground[2]) > 120) {      left = Math.min(left, x);      right = Math.max(right, x);    }  }  return right < 0 ? 0 : (right - left + 1) / info.width;}const contents = (dir: string) => JSON.parse(readFileSync(path.join(dir, "Contents.json"), "utf8")) as {  images: { filename?: string; size?: string; scale?: string; appearances?: { appearance: string; value: string }[] }[];};describe("each redesigned app's icons and launch screen", () => {  for (const app of APPS) {    const catalogue = path.join(FLAVOURS, app.flavour, "Assets.xcassets");    it(`${app.flavour}: the live and Dev iPhone icons are 1024 squares with no alpha, every one its set names`, () => {      for (const set of [`AppIcon${app.set}`, `AppIcon${app.set}Dev`]) {        const dir = path.join(catalogue, `${set}.appiconset`);        const images = contents(dir).images;        const looks = [...new Set(images.map((i) => i.appearances?.[0]?.value ?? "light"))].sort();        expect(looks, set).toEqual(app.looks ? ["dark", "light", "tinted"] : ["light"]);        // One 1024 per look, and nothing else: Xcode draws every size from it. A per-size set put Words' "w|" small        // cut on the owner's home screen and in App Store Connect (build 22, 22 Nov 2026).        const slots = new Set(images.map((i) => `${i.size}@${i.scale ?? "1x"}`));        expect([...slots], set).toEqual(["1024x1024@1x"]);        expect(images.length, set).toBe(slots.size * looks.length);        for (const image of images) {          const file = path.join(dir, image.filename ?? "");          expect(existsSync(file), file).toBe(true);          const px = Math.round(parseFloat(image.size ?? "0") * parseFloat(image.scale ?? "1"));          expect(png(file), file).toEqual({ width: px, height: px, alpha: false });        }      }    });    it(`${app.flavour}: its launch screen names only what its catalogue holds`, () => {      const board = readFileSync(path.join(FLAVOURS, app.flavour, "LaunchScreen.storyboard"), "utf8");      expect(existsSync(path.join(catalogue, "LaunchGround.colorset/Contents.json"))).toBe(true);      const named = [...board.matchAll(/<image name="(\w+)"/g)].map((m) => m[1]);      expect(named).toEqual(app.mark ? ["LaunchObject"] : []);      if (!app.mark) return;      const dir = path.join(catalogue, "LaunchObject.imageset");      const files = contents(dir).images.map((i) => i.filename);      const want = ["LaunchObject@2x.png", "LaunchObject@3x.png", ...(app.nightMark ? ["LaunchObject-dark@2x.png", "LaunchObject-dark@3x.png"] : [])];      expect(files.sort()).toEqual(want.sort());      for (const f of want) expect(png(path.join(dir, f)).alpha, f).toBe(true);    });    it(`${app.flavour}: the Play listing's icon is a 512 square`, () => {      const file = path.join(ROOT, `assets/store/play/${app.store}/metadata/en-US/images/icon.png`);      expect(png(file)).toMatchObject({ width: 512, height: 512 });    });    if (app.wordmark) it(`${app.flavour}: the iPhone, Dev and Play icons are the whole wordmark`, async () => {      const files = [        ...["", "-dark", "-tinted"].map((look) => path.join(catalogue, `AppIcon${app.set}.appiconset/AppIcon${app.set}-1024${look}.png`)),        path.join(catalogue, `AppIcon${app.set}Dev.appiconset/AppIcon${app.set}Dev-1024.png`),        path.join(ROOT, `assets/store/play/${app.store}/metadata/en-US/images/icon.png`),      ];      for (const file of files) expect(await inkSpan(file), file).toBeGreaterThan(0.65);    });  }});describe("retired pictures", () => {  // Comms' painted walkie-talkies on the yellow pegboard (its icon, Dev icon, launch cut-out, pegboard and Play icon),  // retired for the drawn Quote (owner, 22 Nov 2026).  const RETIRED = new Set([    "84cf8220f3e018619c7de95fafe9031f19307a4c3fbed4d7490d8e089521af4c",    "0b527a8b08a9d42c1d4fbcd329a3194d34754b87f582a211144fea6f63781f71",    "809edb38ec56dfa143bd53bfe766e097e366e65d39a5da137df85f6d39b57d7f",    "0b4cc8442d3e936312e1c82d85a33bbb6f2f7cd6a84f6509e3ff860338d1aa13",    "dcbde244023d550dc48a83f023fc584ae8024f7165f05f2c9b614a18f7324e9e",    "4bb37e1fc6e35be592278389352f37e7cbb0a4597d199ab37648ce357118c7eb",    // Story's icon, Dev icon, Play icon and feature graphic with the red full stop; Story is blue (owner, 22 Nov 2026).    "38e09c35bb06c60eb540ce566b779666978222e144916dc89473b308bb484cda",    "1a4f8214159c36b795261cf0d0d50bac907669f3aeeb258c210b1fe42a470d2e",    "39947930acb95e614baf66d6ba84222115b553cb2b9e2ca2d2f7cfc42865b6ff",    "d90fd71fc352993b63807b1a50c936c53b4e5dbfb059432c31abb0129a9222a2",    // Story's "story." wordmark (its icon, Dev icon, launch mark, Play icon and site icon), retired for the plot arc    // (owner, 25 Nov 2026).    "80566b82df6e6b2b160fd9a27bfcddefc7457043e4f1d16571ab23bc56db8bbd",    "05c78683a5290eee5286404e526d6949028c074f2f8c00a4716398054642de14",    "34b7ad1c950e4729c0487b304976c9c7dab05767334b0b4daaed778c3b13ab70",    "a35f82bd8ee899cc3b070ec3d3694f476bc0afd1fb5951ca2e81fada3c747461",    "5f6b739b2ce45298685e0f240ece20bfc6293dc1ef255f2c28ada44756f15dc3",    // Words' painted teal typewriter on cork (its icon, Dev icon, Play icon and feature graphic), retired for the    // wordmark (owner, 22 Nov 2026).    "c086b4c0f9e8b3b337af97f5ddca4dddc21d7c99f9360496a8a4b45594749431",    "d47e272d2bfe7c84d5b180c1d53dde97bd37c13740934b2ecc72a8ead50404db",    "696f14ffada8422506b2ed8a16ae58f0d5510de4624f6401ac927abf90a804f7",    "51ab311ed170373e561119d02c1ef5cef78f6411449c724fad7c460b955fb1f7",  ]);  it("no file in the tree is one of them", () => {    const files = execFileSync("git", ["ls-files", "*.png", "*.jpg", "*.jpeg", "*.webp"], { cwd: ROOT, encoding: "utf8" })      .split("\n").filter(Boolean).filter((f) => existsSync(path.join(ROOT, f)));    const left = files.filter((f) => RETIRED.has(createHash("sha256").update(readFileSync(path.join(ROOT, f))).digest("hex")));    expect(left).toEqual([]);  }, 60_000);  it("Comms launches flat, on no painted ground", () => {    const manifest = JSON.parse(readFileSync(path.join(FLAVOURS, "messages/flavour.json"), "utf8"));    expect(manifest.launch).toBeUndefined();    expect(manifest.flat).toEqual({ light: "#FAF7F2", dark: "#1A1714" });    expect(existsSync(path.join(FLAVOURS, "messages/Assets.xcassets/LaunchSurface.imageset"))).toBe(false);  });});