iterate.com / cloud

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

cloud / internal-packages / installer / test / platform-host.mjsmain at aacbc74

// No stack on a domain named `iterate.<domain>`: that name is the platform's host of `<domain>`,
// and a stack there would serve its mind's loaded code on it. The stack's config refuses such a
// domain, and so does the wizard before it asks Cloudflare anything.
//   node test/platform-host.mjs
import assert from "node:assert/strict";
import test from "node:test";
import { MindConfig } from "../../../core/alchemy/src/config.ts";
import worker from "../worker/index.js";

const CLOUD = "https://cloud.iterate.com";
const ACCOUNT = "0123456789abcdef0123456789abcdef";
const TAKEN = ["iterate.co.uk", "iterate.eng.example.com", "Iterate.Example.com"];
const FREE = ["iterate.com", "iterate.app", "example.com", "iterate-labs.co.uk", "my.iterate.dev"];

test("a stack's config refuses a domain named iterate.<domain>, and takes iterate.com", () => {
  const config = (domain) => ({
    name: "ada",
    accountId: ACCOUNT,
    domain,
    auth: { login: { methods: { email: {} }, allow: [{ email: "ada@example.com" }] } },
    mail: true,
  });
  for (const domain of FREE)
    assert.equal(MindConfig.safeParse(config(domain)).success, true, domain);
  for (const domain of TAKEN) {
    const parsed = MindConfig.safeParse(config(domain));
    assert.equal(parsed.success, false, domain);
    assert.deepEqual(
      parsed.error.issues.map((issue) => [issue.path.join("."), issue.message]),
      [
        [
          "domain",
          "not a domain named iterate.<domain>: that name is the platform's host of <domain>",
        ],
      ],
    );
  }
});

test("the wizard starts no deploy on a domain named iterate.<domain>, and asks Cloudflare nothing", async () => {
  const rows = new Map([
    ["s:person", JSON.stringify({ level: "full", until: Date.now() + 3600_000 })],
    ["t:person", JSON.stringify({ token: "TOKEN", expiresAt: Date.now() + 3600_000 })],
  ]);
  const asked = [];
  const env = {
    CLIENT_ID: "client",
    CLIENT_SECRET: "secret",
    SESSIONS: {
      get: async (key) => rows.get(key) ?? null,
      put: async (key, value) => void rows.set(key, value),
      delete: async (key) => void rows.delete(key),
    },
    CF_SERVICE: {
      fetch: async (input, init = {}) => {
        asked.push(`${init.method || "GET"} ${new URL(input).pathname}`);
        return Response.json({ success: true, result: [] });
      },
    },
  };
  for (const zone of TAKEN.map((each) => each.toLowerCase())) {
    const answer = await worker.fetch(
      new Request(`${CLOUD}/api/deploy`, {
        method: "POST",
        headers: {
          cookie: "__Host-sid=person",
          origin: CLOUD,
          "content-type": "application/json",
        },
        body: JSON.stringify({ account: ACCOUNT, zone, zoneId: ACCOUNT }),
      }),
      env,
    );
    assert.equal(answer.status >= 400, true, zone);
    assert.match((await answer.json()).error, /^iterate does not deploy on iterate\./, zone);
  }
  assert.deepEqual(asked, []);
});