Cotton code
133 lines · 5.8 KB · TypeScriptRaw
/** * THE TINMEN MEDIA WORKER IS RETIRED. Nothing may reach for it again. * * Cotton used to send every byte — a person's photo, a WhatsApp attachment — * through a shared media Worker inherited from the Tinmen monorepo, as a * multipart POST authenticated with a Supabase access token, and store the * public URL it answered with. That shape is what the offline-first media rule * replaced: * bytes land in the phone's media store first, `_media` records them, and the * upload queue mints a presigned PUT against COTTON's bucket through the server. * * The reason this is a test and not a note in AGENTS.md: the Worker is still * up, still authenticates, and still answers 200. A call to it would look like * it worked — the file would just be in the wrong bucket, invisible to the * server, undeletable by the app, and absent from the person's own phone. That * is a silent wrong answer, which is exactly the class of failure this repo * puts a guard on rather than a paragraph. * * Three markers, because the path can be reintroduced by host, by helper, or * by URL shape: *   - `tinmen-media(-dev).<account>.workers.dev` — the Worker's host shape, *     which is what a re-added endpoint (either variant) actually spells. The *     account label is a personal Cloudflare account and is NOT in the tree *     ([nothing-real-ever-goes-in-cotton-s-public-tree]), so this marker is a *     pattern over any label. NOT the bare string "tinmen-media": this file, *     and every comment explaining the retirement, has to be able to NAME the *     thing it forbids. *   - `getR2UploadUrl`         — the endpoint accessor that fed it. *   - `workers.dev/upload`     — the endpoint shape, on any host. */import { execFileSync } from "node:child_process";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)), "../..");/** * The markers, each spelled the way a reintroduction would spell it. A * `fixed` marker is matched as a string, the other as an extended regexp. */const MARKERS: ReadonlyArray<{ marker: string; fixed: boolean }> = [  { marker: "tinmen-media(-dev)?\\.[a-z0-9-]+\\.workers\\.dev", fixed: false },  { marker: "getR2UploadUrl", fixed: true },  { marker: "workers.dev/upload", fixed: true },];/** Where the apps live — the code that ships. */const ROOTS = ["apps"];/** * EMPTY, and that is the whole point (2026-08-26, issue #20). * * There used to be one entry: `packages/import/src/mediaUpload.ts`, the NODE * half of the WhatsApp importer, allowlisted on the reasoning that a * dev-machine CLI is not on the offline-first path because there is no phone * in it to hold the bytes. That reasoning was half right and therefore wrong. * Holding a local copy is the PHONE's half of the rule; the other half is where * the bytes end up and what the row says about them — COTTON's bucket, under a * key from `packages/core/src/media/keys.ts`, described by `{key,…}` — and on * that a CLI has no excuse. It mints at * `/v1/admin/workspaces/:id/media/upload-url` now, with the server's admin * token, and writes exactly the entries the app writes. * * Everything the APP used was already gone — `apps/cotton/src/lib/photoUpload.ts`, * `packages/services/src/r2.service.ts` and its export, the `initR2` wiring in * `config/initServices.ts`, `ENV.r2UploadUrl`, and the baked Worker URLs in * `apps/cotton/config/build.js`. The objects the old CLI path already wrote * were accounted for by `scripts/media-repatriate.ts`, which ran ON the hosted * server and was deleted with it in step 5l; a row still carrying a legacy URL * renders from that URL and is rewritten by whatever the Mac grows for it. * * An entry here would be a PATH RELATIVE TO THE REPO ROOT — never a prefix, so * a new file cannot slip in behind one. Adding one back needs a better reason * than the last one had. */const ALLOWED = new Set<string>([]);/** * Every file under `apps/` and `packages/` that mentions a marker. * * `git grep` rather than a hand-rolled walk: it already knows what is tracked, * so `node_modules`, build output and iOS/Android generated trees are out by * construction rather than by an ignore list that would rot. */function filesMentioning(marker: string, fixed = true): string[] {  try {    return execFileSync(      "git",      [        "grep",        fixed ? "--fixed-strings" : "--extended-regexp",        "--files-with-matches",        // A file added but not yet committed is still a file that ships.        "--untracked",        marker,        "--",        ...ROOTS,      ],      { cwd: REPO_ROOT, encoding: "utf8" },    )      .split("\n")      .map((line) => line.trim())      .filter(Boolean);  } catch (error) {    // `git grep` exits 1 when nothing matched, which is the state we want.    const status = (error as { status?: number }).status;    if (status === 1) return [];    throw error;  }}describe("the retired Tinmen media Worker", () => {  for (const { marker, fixed } of MARKERS) {    it(`is not referenced by shipped code — "${marker}"`, () => {      const offenders = filesMentioning(marker, fixed).filter(        (file) => !ALLOWED.has(file),      );      expect(offenders).toEqual([]);    });  }  it("has an allowlist that names only files that exist", () => {    // An allowlist entry for a file somebody deleted is an entry that would    // silently cover the NEXT file to take that path. It also means the    // removal above got further than the list did.    const stillThere = new Set(      MARKERS.flatMap(({ marker, fixed }) => filesMentioning(marker, fixed)),    );    const stale = [...ALLOWED].filter((file) => !stillThere.has(file));    expect(stale).toEqual([]);  });});