iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / internal-packages / installer / test / routes.mjsmain at aacbc74

// routes.mjs — production's routes (wrangler.jsonc `env.prd.routes`) reach the Worker with every
// path that worker/main.js serves itself, and with no path of the wizard or the relay: those fall
// to the iterate.com mind. Run: node test/routes.mjs
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { test } from "node:test";
import { handles as serviceHandles } from "../worker/service.js";

const HOST = "cloud.iterate.com";

/** wrangler.jsonc as JSON: its comments are whole lines, and its lists end with a comma. */
const config = JSON.parse(
  readFileSync(new URL("../wrangler.jsonc", import.meta.url), "utf8")
    .replace(/^\s*\/\/.*$/gm, "")
    .replace(/,(\s*[}\]])/g, "$1"),
);
const patterns = config.env.prd.routes.map((route) => route.pattern);

/** Cloudflare's match of a route: the host, then the path WITH its query (Workers routing docs,
 *  "Matching behavior"); `*` only at the end. */
const routed = (pathAndQuery) =>
  patterns.some((pattern) => {
    const [host, ...rest] = pattern.split("/");
    const want = `/${rest.join("/")}`;
    if (host !== HOST) return false;
    return want.endsWith("*") ? pathAndQuery.startsWith(want.slice(0, -1)) : pathAndQuery === want;
  });

// Each family of paths that main.js sends to a handler of its own, and the pages of index.js that
// a person reaches from the mind's app or from a provider
const KEPT = [
  // cloud-clients `handles` (its module imports cloudflare:workers, so its two paths are named here)
  "/.well-known/iterate-cloud",
  "/clients/github/webhook",
  "/clients/google/webhook",
  "/clients/x/ask/subscribe",
  "/clients/github/token",
  // cloud-ai `handles` (direct + SecretProxy + Responses WebSocket + OpenAI voice/transcribe)
  "/api/cloud-ai",
  "/ai-ws/openai/responses",
  "/openai-voice/v1/audio/transcriptions",
  "/capnweb",
  "/capnweb/cloud.iterate.com/api/cloud-ai",
  "/capnweb/cloud.iterate.com/ai-ws/openai/responses",
  "/capnweb/api.openai.com/v1/live/sessions",
  "/capnweb/api.openai.com/v1/audio/transcriptions",
  // service.js `handles`
  "/service/installations",
  "/service/cloud-key",
  // access.js `handle`
  "/enrol",
  "/enrolled",
  "/kept",
  "/access",
  "/access/sign-in",
  // index.js: an installation's page and its consent's return
  `/i/inst_${"a".repeat(26)}/consent`,
  "/callback",
  "/api/config",
  "/assets/index-abc123.js",
  "/iterate-logo.svg",
  // the same paths as they come, with their queries
  "/.well-known/iterate-cloud?x=1",
  "/callback?code=c&state=s",
  "/enrol?domain=example.com",
  "/enrolled?domain=example.com",
  "/kept?x=1",
  "/api/config?x=1",
  "/iterate-logo.svg?v=1",
];
const FALLEN = [
  "/",
  "/?x=1",
  "/deployments",
  "/deployments?x=1",
  "/erase",
  "/connect",
  "/connect?level=full",
  "/logout",
  "/api/deployments",
  "/mcp",
  // the CLI's page after `iterate chatgpt connect`: the mind's app since 16 Nov (it opens the AI
  // step on `test=passed`)
  "/connected",
  "/connected?ai=chatgpt&domain=example.com&test=passed",
];

test("every path that the Worker serves is routed to it", () => {
  for (const path of KEPT) assert.ok(routed(path), `${path} is not routed`);
  for (const path of KEPT.filter((p) => p.startsWith("/service/")))
    assert.ok(serviceHandles(new URL(`https://${HOST}${path}`)), path);
});

test("the wizard's pages and the relay fall to the mind", () => {
  for (const path of FALLEN) assert.ok(!routed(path), `${path} is routed`);
});