iterate.com / cloud

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

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

// pages.mjs — which pages of a deployment answer now (worker/deployments.js `pagesNow`), for the
// person's page of a run that kept no record of them. The answers are those of a real deployment,
// asked signed out on 2026-11-11: a stack older than its Dash and its self-test page.
// Run: node test/pages.mjs
import assert from "node:assert/strict";
import { test } from "node:test";
import { pagesNow } from "../worker/deployments.js";

const site = "https://iterate.example.dev";
const answers = {
  [`${site}/keys`]: { status: 302, location: `${site}/sign-in/email?next=%2Fkeys` },
  [`${site}/members`]: { status: 303, location: `${site}/sign-in/email?next=%2Fmembers` },
  [`${site}/self-test`]: { status: 404 },
  [`${site}/mcp`]: {
    status: 401,
    authenticate: `Bearer resource_metadata="${site}/.well-known/oauth-protected-resource/mcp"`,
  },
  "https://dash.example.dev/": { status: 404 },
};
/** `fetch`, answering from `answers`; an address that is not in it does not resolve. */
const answering = (from) => async (url) => {
  const answer = from[url];
  if (!answer) throw new TypeError("fetch failed");
  return new Response(null, {
    status: answer.status,
    headers: {
      ...(answer.location ? { location: answer.location } : {}),
      ...(answer.authenticate ? { "www-authenticate": answer.authenticate } : {}),
    },
  });
};
const withFetch = async (from, run) => {
  const real = globalThis.fetch;
  globalThis.fetch = answering(from);
  try {
    return await run();
  } finally {
    globalThis.fetch = real;
  }
};

test("each page is there when it answers as that page does to a browser that is signed out", async () => {
  assert.deepEqual(await withFetch(answers, () => pagesNow("example.dev")), {
    keys: "works",
    members: "works",
    "self-test": "missing",
    mcp: "works",
    dash: "missing",
  });
});

test("the Dash is an app of the config repo: it is there when it sends a signed-out browser to the deployment's sign-in", async () => {
  const dash = (answer) =>
    withFetch({ ...answers, "https://dash.example.dev/": answer }, () =>
      pagesNow("example.dev"),
    ).then((pages) => pages.dash);
  assert.equal(
    await dash({ status: 302, location: `${site}/oauth2/auth?client_id=dash` }),
    "works",
  );
  assert.equal(
    await dash({ status: 302, location: "https://elsewhere.example/oauth2/auth" }),
    "missing",
  );
  assert.equal(await dash({ status: 200 }), "missing");
});

test("a redirect that leaves the deployment is not its sign-in, and a 401 with no Bearer is not its MCP address", async () => {
  const pages = await withFetch(
    {
      ...answers,
      [`${site}/keys`]: { status: 302, location: "https://elsewhere.example/sign-in" },
      "https://dash.example.dev/": {
        status: 302,
        location: "https://elsewhere.example/oauth2/auth",
      },
      [`${site}/mcp`]: { status: 401 },
    },
    () => pagesNow("example.dev"),
  );
  assert.equal(pages.keys, "missing");
  assert.equal(pages.dash, "missing");
  assert.equal(pages.mcp, "missing");
});

test("a deployment that does not answer has no page", async () => {
  const pages = await withFetch({}, () => pagesNow("example.dev"));
  assert.deepEqual([...new Set(Object.values(pages))], ["missing"]);
});

test("on the pretend deployment a host's stand-in answers for its pages", async () => {
  const pages = await pagesNow("example.dev", async (host) => host !== "dash.example.dev");
  assert.equal(pages["self-test"], "works");
  assert.equal(pages.dash, "missing");
});