Cotton code
52 lines · 4.4 KB · TypeScriptRaw
import { generateKeyPairSync } from "node:crypto";import { cloudflareTest } from "@cloudflare/vitest-pool-workers";import { configDefaults, defineConfig } from "vitest/config";import { boxTestTimeout, takeWorkerCap } from "../../vitest.workers";import { writePictures } from "./scripts/kit.ts";// The kit pictures (ASSETS, wrangler.jsonc `assets`) are a generated folder: laid before the pool reads the// config, so a fresh checkout's tests serve them as the deployed Worker does.writePictures();// Cotton Calling's APNs key for the tests alone, made fresh for each run: the fake APNs checks every push's// JWT against its public half (test/calling.ts). No real key is ever in a test or in the tree.const apns = generateKeyPairSync("ec", { namedCurve: "P-256" });const APNS_TEST_KEY = apns.privateKey.export({ type: "pkcs8", format: "pem" }).toString();const APNS_TEST_PUBLIC = JSON.stringify(apns.publicKey.export({ format: "jwk" }));// And its Firebase service account, the same way: an RSA key made for this run, its public half for the fake token endpoint.const fcm = generateKeyPairSync("rsa", { modulusLength: 2048 });const FCM_SERVICE_ACCOUNT = JSON.stringify({ type: "service_account", project_id: "synthetic-calling", client_email: "calling-test@synthetic-calling.iam.gserviceaccount.test", private_key: fcm.privateKey.export({ type: "pkcs8", format: "pem" }).toString() });const FCM_TEST_PUBLIC = JSON.stringify(fcm.publicKey.export({ format: "jwk" }));export default defineConfig({  // The shared spaces built on, as the dev Worker is (src/space/switch.ts; wrangler.jsonc env.dev `define`).  define: { __SHARED_SPACES__: JSON.stringify("on") },  plugins: [    cloudflareTest({      wrangler: { configPath: "./wrangler.jsonc" },      // Workers AI is remote only: the tests bind none (the calendar engine is handed a fake embedder).      remoteBindings: false,      miniflare: {        // No Workers AI under AI in the pool (it is remote only, and the pool binds nothing remote): the        // wrangler binding is moved off the name the object reads, and the calendar engine's tests hand it a fake embedder.        ai: { binding: "AI_REMOTE_ONLY" },        // The site Worker (SITE, wrangler.jsonc `services`) is not in this pool: the tests' own site answers        // a plain line naming what reached it, so a request through the Worker shows which side took it.        serviceBindings: {          SITE: async (request: Request) => new Response(`site: ${request.method} ${new URL(request.url).pathname}`, { headers: { "content-type": "text/plain; charset=utf-8", "x-test-site": "1" } }),        },        bindings: { ARTWORK: "off", ADMIN_TOKEN: "test-admin-token", OTP_MODE: "fixed", SMS_PER_NUMBER_PER_DAY: "1000", ACCOUNT_DELETE: "on", OTP_FIXED_CODE: "123456", ENV_LABEL: "test", KEY_DEFAULT: "narrow",VAULT_KEY: "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8", WINDOW_KEY: "ICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj8", SERVICE_TOKEN_KEY: "QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl8", MAIL_FETCH_KEY: "YGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn8", JOB_TOKEN_KEY: "test-job-token-key-0123456789abcdefghij", GOOGLE_CLIENT_ID: "test-client.apps.googleusercontent.com", GOOGLE_CLIENT_SECRET: "test-client-secret", STREAM_SIGNING_KEY: "test-stream-signing-key", PLANS_FAKE_SECRET: "test-plans-fake-secret", SPOTIFY_CLIENT_ID: "test-spotify-client", SPOTIFY_CLIENT_SECRET: "test-spotify-secret", TMDB_API_KEY: "test-tmdb-key", APNS_KEY: APNS_TEST_KEY, APNS_KEY_ID: "TESTKEY001", APNS_TEAM_ID: "TESTTEAM01", APNS_URL: "https://apns.test", APNS_TEST_PUBLIC, FCM_SERVICE_ACCOUNT, FCM_TEST_PUBLIC, FCM_URL: "https://fcm.test", FCM_TOKEN_URL: "https://oauth.test/token", CF_SFU_APP_ID: "synthetic-sfu-app", CF_SFU_APP_SECRET: "synthetic-sfu-secret", CF_SFU_URL: "https://sfu.test", PUBLISHER_TOKENS: JSON.stringify({ cotton: "test-publisher-token-cotton-0123456789abcdef", someone: "test-publisher-token-someone-0123456789abcdef" }), BIG_OBJECT: process.env.BIG_OBJECT ?? "", HEAVY: process.env.HEAVY ?? "", SAY: process.env.SAY ?? "" },      },    }),  ],  test: {    ...takeWorkerCap(),    // 30 s on the box, vitest's default on a Mac (`vitest.workers.ts` says why).    ...boxTestTimeout(),    globalSetup: ["../../vitest.testfiles.ts"],    include: ["test/**/*.test.ts"],    // The scripts' own tests run in plain Node under vitest.scripts.config.ts (`test:scripts`).    exclude: [...configDefaults.exclude, "scripts/**"],  },});