import { existsSync, readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { describe, expect, it } from "vitest"; /** * The public website's deploy identity. * * Several tests in this folder mention the name `cotton-site` in passing — as a * path, as a bucket binding — and none of them says why it cannot change. This * one does. * * A Cloudflare Worker's name IS the thing it deploys to. Renaming it in * `wrangler.jsonc` does not rename anything: the next deploy uploads a SECOND, * empty Worker under the new name, while the custom domains, the business * Worker's service binding and the R2 bucket stay bound to the old script. cottondb.com goes on serving whatever was last deployed under * `cotton-site`, every later deploy * lands somewhere nobody is looking, and the only symptom is a site that quietly * stops changing. */ const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const CONFIG = path.join(ROOT, "apps/cotton-site/wrangler.jsonc"); const BUSINESS_CONFIG = path.join(ROOT, "apps/business/wrangler.jsonc"); /** * JSONC → JSON. Comment stripping has to know where the strings are: the config * is full of `https://…` values, and a naive `//` sweep would cut a URL in half * and leave the file unparseable. */ function stripComments(source: string): string { let out = ""; let inString = false; let index = 0; while (index < source.length) { const character = source[index]; const next = source[index + 1]; if (inString) { out += character; if (character === "\\") { out += next ?? ""; index += 2; continue; } if (character === '"') inString = false; index += 1; continue; } if (character === '"') { inString = true; out += character; index += 1; continue; } if (character === "/" && next === "/") { while (index < source.length && source[index] !== "\n") index += 1; continue; } if (character === "/" && next === "*") { index += 2; while (index < source.length && !(source[index] === "*" && source[index + 1] === "/")) { index += 1; } index += 2; continue; } out += character; index += 1; } // A trailing comma is legal in JSONC and not in JSON. return out.replace(/,(\s*[}\]])/g, "$1"); } type Wrangler = { name?: string; routes?: { pattern: string; custom_domain?: boolean }[]; d1_databases?: { binding: string; database_name: string }[]; r2_buckets?: { binding: string; bucket_name: string }[]; services?: { binding: string; service: string }[]; }; function read(file: string): Wrangler { // Loud rather than quiet: this guard reads another package's file by path, so // a move must redden here instead of silently guarding nothing. expect(existsSync(file), `${path.relative(ROOT, file)} has moved: update this guard`).toBe(true); return JSON.parse(stripComments(readFileSync(file, "utf8"))) as Wrangler; } function config(): Wrangler { return read(CONFIG); } describe("the site's Worker", () => { // RULE: The public website deploys to a Cloudflare Worker named cotton-site, and that name is never changed. // Renaming it deploys a second, empty Worker instead of renaming anything: the // custom domains and the bucket stay attached to the old script, // so the live site freezes at its last deploy and every later one lands // nowhere. The name is a deploy address, not a label. it("is named cotton-site", () => { expect(config().name).toBe("cotton-site"); }); // RULE: The website's own two addresses are attached to that one Worker, so renaming it would take them down with it. // www collapses to cottondb.com and the retired cotton.tinmen.ai is kept // attached so old links still resolve. Both are bound to the Worker BY NAME on // Cloudflare's side. it("answers www and the old host as custom domains", () => { const routes = config().routes ?? []; expect(routes.map((route) => route.pattern).sort()).toEqual([ "cotton.tinmen.ai", "www.cottondb.com", ]); for (const route of routes) { expect(route.custom_domain, route.pattern).toBe(true); } }); // RULE: cottondb.com belongs to the business Worker, which hands the site its pages over a service binding to cotton-site. // A hostname has one Worker, and a deploy that lists a custom domain takes it // from whichever Worker held it, without asking when no one is at a terminal. // The open door must keep cottondb.com (sign-in, /v1, /mcp), so the site never // lists it, and the business Worker reaches the site by this Worker's name. it("leaves cottondb.com to the business Worker, which binds it as SITE", () => { expect((config().routes ?? []).map((route) => route.pattern)).not.toContain("cottondb.com"); const business = read(BUSINESS_CONFIG); expect(business.routes).toContainEqual({ pattern: "cottondb.com", custom_domain: true }); expect(business.services).toEqual([{ binding: "SITE", service: "cotton-site" }]); }); // RULE: The site holds no database: the release-notice intake went with the old site on 8 Nov 2026. // A D1 binding left behind is a deploy that needs a database the site no // longer reads, and an invitation to start writing to it again. it("binds no database", () => { expect(config().d1_databases).toBeUndefined(); }); // RULE: The Mac app's published downloads are served from a bucket called cotton-mac-releases, which the publishing lane writes and a site deploy never touches. // A release zip is far bigger than a static asset may be, so the bytes live in // the bucket and the site only serves them. The name is shared between this // config and the publish workflow, and a rename on one side alone leaves every // Mac update link answering nothing. it("binds the Mac releases bucket as MAC_RELEASES", () => { expect(config().r2_buckets).toEqual([ expect.objectContaining({ binding: "MAC_RELEASES", bucket_name: "cotton-mac-releases" }), ]); }); // RULE: Every environment of the site names the same bucket as the live one, and no database. // A named environment inherits nothing in wrangler, so each key is redeclared // by hand — and a redeclaration that drifts is a local run writing somewhere // the live site never reads. it("redeclares the same bindings in the dev environment", () => { const parsed = JSON.parse(stripComments(readFileSync(CONFIG, "utf8"))) as { env?: { dev?: Wrangler }; }; const dev = parsed.env?.dev; expect(dev, "the dev environment is gone: update this guard").toBeTruthy(); expect(dev?.d1_databases).toBeUndefined(); expect(dev?.r2_buckets).toEqual([ expect.objectContaining({ binding: "MAC_RELEASES", bucket_name: "cotton-mac-releases" }), ]); }); });