Cotton code
105 lines · 4.6 KB · TypeScriptRaw
/** * The two Workers one wrangler.jsonc deploys, read the way wrangler reads them * (inheritance and all): the live one (`pnpm run deploy`, cottondb on * cottondb.com) and the dev one (`pnpm run * deploy:dev`, cottondb-dev on its workers.dev host alone). The dev * Worker binds what the live one binds, by the same names, but never the live * one's buckets or hosts. */import { join } from "node:path";import { unstable_readConfig } from "wrangler";import { describe, expect, it } from "vitest";import { APP } from "./dev-worker.ts";import { deployArgs } from "../deploy";import { devDeployVars, devHost } from "../dev-server";/** What these tests read of a resolved config (wrangler's own type leans on the Workers types, which the scripts do not load). */interface Worker {  name: string;  routes: unknown[];  workers_dev: boolean;  vars: Record<string, unknown>;  define?: Record<string, string>;  durable_objects: { bindings: { name: string; class_name: string }[] };  migrations: unknown[];  r2_buckets: { binding: string; bucket_name: string }[];  services: { binding: string }[];  images?: { binding: string };  ai?: { binding: string };}const read = (env?: string) => unstable_readConfig({ config: join(APP, "wrangler.jsonc"), env }) as unknown as Worker;const live = read();const dev = read("dev");/** Every binding a Worker declares, by name. */function bindings(config: Worker): string[] {  return [    ...config.durable_objects.bindings.map((b) => b.name),    ...config.r2_buckets.map((b) => b.binding),    ...config.services.map((b) => b.binding),    ...(config.images ? [config.images.binding] : []),    ...(config.ai ? [config.ai.binding] : []),  ].sort();}describe("the shared spaces' off switch (src/space/switch.ts)", () => {  it("is built shut on the live Worker and open on dev alone, and no var names it", () => {    expect(live.define ?? {}).not.toHaveProperty("__SHARED_SPACES__");    expect(dev.define).toEqual(expect.objectContaining({ __SHARED_SPACES__: '"on"' }));    for (const config of [live, dev]) for (const name of Object.keys(config.vars)) expect(name).not.toMatch(/SHARED_SPACES/);  });});describe("the dev Worker", () => {  it("is its own Worker, on its workers.dev host and no custom domain", () => {    expect(dev.name).toBe("cottondb-dev");    expect(dev.routes).toEqual([]);    expect(dev.workers_dev).toBe(true);    // Its host is a setting (config/dev-server.json), passed at the dev deploy, never written in wrangler.jsonc.    expect(dev.vars.OPEN_HOST).toBeUndefined();    expect(dev.vars.GOOGLE_BASE).toBeUndefined();    expect(deployArgs("dev", devDeployVars())).toEqual(["deploy", "--env", "dev", "--var", `OPEN_HOST:${devHost()}`, ...(devDeployVars().GOOGLE_BASE ? ["--var", `GOOGLE_BASE:${devDeployVars().GOOGLE_BASE}`] : [])]);    expect(deployArgs(undefined)).toEqual(["deploy"]);    expect(dev.vars.ENV_LABEL).toBe("dev");  });  it("binds what the live Worker binds, by the same names, but the site", () => {    expect(bindings(dev)).toEqual(bindings(live).filter((name) => name !== "SITE"));    expect(dev.durable_objects.bindings).toEqual(live.durable_objects.bindings);    expect(dev.migrations).toEqual(live.migrations);  });  it("never touches the live Worker's buckets", () => {    const buckets = (config: Worker) => Object.fromEntries(config.r2_buckets.map((b) => [b.binding, b.bucket_name]));    expect(buckets(dev)).toEqual({ PHOTOS: "cotton-dev-photos-apac", MEDIA: "cotton-dev-media-apac" });    for (const bucket of Object.values(buckets(dev))) expect(Object.values(buckets(live))).not.toContain(bucket);  });  it("signs in by the fixed code, kept a secret, as the live Worker keeps its own", () => {    expect(dev.vars.OTP_MODE).toBe("fixed");    for (const config of [live, dev]) {      for (const secret of ["OTP_FIXED_CODE", "OTP_TEST_CODES", "TWILIO_ACCOUNT_SID", "TWILIO_AUTH_TOKEN", "TWILIO_VERIFY_SID"]) expect(config.vars).not.toHaveProperty(secret);    }  });});describe("the live Worker", () => {  it("keeps its name and its one host", () => {    expect(live.name).toBe("cottondb");    expect(live.routes).toEqual([{ pattern: "cottondb.com", custom_domain: true }]);    expect(live.vars.OPEN_HOST).toBe("cottondb.com");    expect(live.vars.ENV_LABEL).toBe("production");  });  it("sends every code through Twilio, never the fixed one (owner, 13 Nov 2026)", () => {    expect(live.vars.OTP_MODE).toBe("twilio");  });  it("gives a new key or connection what it got before allows until the owner flips it (ADMIN KEYS 3); dev is narrow", () => {    expect(live.vars.KEY_DEFAULT).toBe("today");    expect(dev.vars.KEY_DEFAULT).toBe("narrow");  });});