Cotton code
136 lines · 7.5 KB · TypeScriptRaw
/** * RULE (19 Nov 2026): the open host's site associations are the registry's, never typed. Each * Worker's APPLE_APP_SITE_ASSOCIATION names every iPhone app people sign in to — every app cut to phones, * development or production, never parked or planned — on its side (`<team>.<bundle id>`), and assetlinks.json every * such Android app whose certificates the registry lists; wrangler.jsonc holds what * `pnpm -s passkey:associations --write` makes, so an app cut and not written fails here. */import { readFileSync } from "node:fs";import { unstable_readConfig } from "wrangler";import { describe, expect, it } from "vitest";import { readRegistry, signsInOnAndroid as rootAndroid, signsInOnIphone as rootIphone } from "../lib/registry";import {  androidAssetLinks,  appleApps,  androidOrigins,  apkKeyHash,  appleSiteAssociation,  expectedWrangler,  HOSTS,  readRows,  readTeam,  ROOT,  withVars,  signsInOnAndroid,  signsInOnIphone,  WRANGLER,  type Row,} from "../../apps/business/scripts/site-associations";const rows = readRows();const team = readTeam();const vars = (env?: string) => (unstable_readConfig({ config: WRANGLER, env }) as unknown as { vars: Record<string, unknown> }).vars;const WORKERS = { live: vars(), dev: vars("dev") } as const;/** A synthetic certificate fingerprint: 32 bytes of `byte`. */const fingerprint = (byte: string) => Array.from({ length: 32 }, () => byte).join(":");describe("the open host's site associations", () => {  it("are in wrangler.jsonc as the registry makes them (pnpm -s passkey:associations --write)", () => {    const text = readFileSync(WRANGLER, "utf8");    expect(expectedWrangler(text, rows, team) === text, "wrangler.jsonc is stale: run pnpm -s passkey:associations --write").toBe(true);  });  it("name every iPhone app cut to phones on each Worker, the live ids on the live one and the dev ids on the dev one", () => {    const active = rows.filter(signsInOnIphone);    expect(active.length).toBeGreaterThan(0);    for (const side of ["live", "dev"] as const) {      const worker = WORKERS[side];      expect(worker.OPEN_HOST, side).toBe(HOSTS[side]);      const served = JSON.parse(String(worker.APPLE_APP_SITE_ASSOCIATION)) as { webcredentials: { apps: string[] } };      for (const row of active) {        const bundle = side === "live" ? row.ios!.live.bundleId : row.ios!.dev!.bundleId;        expect(served.webcredentials.apps, `${row.slug} is cut to phones but missing from the ${side} Worker's apple-app-site-association`).toContain(`${team}.${bundle}`);      }      expect(served.webcredentials.apps).toHaveLength(active.length);    }  });  it("fail the check when an app is cut and the file is not written again", () => {    const text = readFileSync(WRANGLER, "utf8");    const parked = rows.find((row) => row.phase === "parked" && row.ios?.live.flavour)!;    const more: Row[] = rows.map((row) => (row === parked ? { ...row, phase: "development" } : row));    expect(expectedWrangler(text, more, team) === text).toBe(false);    expect(appleSiteAssociation(more, team, "live")).toContain(`${team}.${parked.ios!.live.bundleId}"`);  });  it("pick the apps scripts/lib/registry.ts says sign in, the coordinator's eight among them, and no parked or planned app", () => {    const registry = readRegistry(ROOT).apps;    expect(rows.filter(signsInOnIphone).map((row) => row.slug)).toEqual(registry.filter(rootIphone).map((app) => app.slug));    expect(rows.filter(signsInOnAndroid).map((row) => row.slug)).toEqual(registry.filter(rootAndroid).map((app) => app.slug));    const live = appleApps(rows, team, "live");    for (const slug of ["notes", "messages", "story", "meetings", "keys", "books", "cinemas", "downloads"]) expect(live).toContain(`${team}.com.cottondb.${slug}`);    for (const row of rows.filter((one) => one.phase === "parked" || one.phase === "planned")) {      if (row.ios) expect(live, row.slug).not.toContain(`${team}.${row.ios.live.bundleId}`);    }  });  it("write assetlinks.json and the passkey origins from the certificates a row lists, and nothing for a row with none", () => {    const signed: Row[] = [      { slug: "a", phase: "development", ios: null, android: { package: "com.example.a", devPackage: "com.example.a.dev", signing: { live: [fingerprint("AB")], dev: [] } } },      { slug: "b", phase: "production", ios: null, android: { package: "com.example.b", devPackage: null } },      { slug: "c", phase: "parked", ios: null, android: { package: "com.example.c", devPackage: null, signing: { live: [fingerprint("CD")], dev: [] } } },    ];    expect(JSON.parse(androidAssetLinks(signed, "live")!)).toEqual([      { relation: ["delegate_permission/common.get_login_creds"], target: { namespace: "android_app", package_name: "com.example.a", sha256_cert_fingerprints: [fingerprint("AB")] } },    ]);    expect(androidOrigins(signed, "live")).toBe(apkKeyHash(fingerprint("AB")));    expect(apkKeyHash(fingerprint("AB"))).toBe(`android:apk-key-hash:${Buffer.alloc(32, 0xab).toString("base64url")}`);    // b has no dev app (devPackage null) and a lists no dev certificate: the dev Worker serves nothing for either.    expect(androidAssetLinks(signed, "dev")).toBeNull();    expect(() => androidAssetLinks([{ ...signed[0]!, android: { ...signed[0]!.android!, signing: { live: ["ab:cd"], dev: [] } } }], "live")).toThrow(/colon hex/);  });  /**   * RULE (LAUNCH.md, 22 Nov 2026): a passkey works in an Android app only once the open host names the app's Play   * app-signing certificate, so each app going to the stores first lists it. Days' is read from Play Console (App   * integrity › App signing key certificate, SHA-256) once Play holds a release of com.cottondb.calendar; until then it   * is owed by name. Listing it turns this red: then drop it from `owed` and run pnpm -s passkey:associations --write.   */  it("name the Play app-signing certificate of Story, Days and Words, Days' still owed from Play Console", () => {    const owed = ["story", "days", "notes"].filter((slug) => !rows.find((row) => row.slug === slug)?.android?.signing?.live.length);    expect(owed).toEqual(["days"]);  });  it("serve Android's files only once a certificate is listed: with none, each Worker leaves both vars out", () => {    for (const side of ["live", "dev"] as const) {      const listed = androidAssetLinks(rows, side);      expect(WORKERS[side].ANDROID_ASSET_LINKS ?? null, side).toBe(listed);      expect(WORKERS[side].PASSKEY_ANDROID_ORIGINS ?? null, side).toBe(androidOrigins(rows, side));    }  });});/** RULE (the Post, Sites and Data cut, 26 Nov 2026): a var written between OPEN_HOST and the managed lines never leaves the old list behind. */describe("writing the managed vars", () => {  it("replaces the old lines wherever they stand in the Worker's vars", () => {    const text = [      "      \"vars\": {",      "        \"OPEN_HOST\": \"h.example\",",      "        \"GOOGLE_BASE\": \"https://g.example\",",      "        // Written by `pnpm -s passkey:associations` from apps/registry.json; never by hand.",      "        \"APPLE_APP_SITE_ASSOCIATION\": \"old\",",      "        \"AGENT_GRANT\": \"on\",",      "      },",    ].join("\n");    const out = withVars(text, "h.example", [["APPLE_APP_SITE_ASSOCIATION", "new"]]);    expect(out.match(/APPLE_APP_SITE_ASSOCIATION/g)).toHaveLength(1);    expect(out).toContain('"APPLE_APP_SITE_ASSOCIATION": "new"');    expect(out).toContain('"GOOGLE_BASE"');    expect(out).toContain('"AGENT_GRANT"');    expect(withVars(out, "h.example", [["APPLE_APP_SITE_ASSOCIATION", "new"]])).toBe(out);  });});