iterate.com / cloud

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

cloud / internal-packages / installer / test / deploy-steps.mjsmain at aacbc74

// deploy-steps.mjs — which step of a deploy run failed, and what the page says about it
// (src/deploy-steps.ts), on the log of a real failed run. Run: node test/deploy-steps.mjs
import assert from "node:assert/strict";
import { test } from "node:test";
import {
  runErrorOf,
  failedStepOf,
  notesOf,
  pagesOf,
  phaseOf,
  plainError,
  reportOf,
  stepsOf,
} from "../src/deploy-steps.ts";

const STEPS = ["check", "deploy", "answer", "verify", "forget"];
const STUCK =
  "Stopped: iterate could not give up its access to your Cloudflare account. Remove “Iterate Cloud” yourself.";
const phases = (state) => Object.fromEntries(STEPS.map((id) => [id, phaseOf(state, id)]));

// The shape of the run on a person's own domain, 2026-11-11: the deploy step failed at two
// resources, and the run then gave up its access, as every run does.
const failedDeploy = {
  status: "failed",
  lines: [
    "stack 269dca45d5711230ec285e2cb3b8e035e442f1f4",
    "== step check",
    "account: an account",
    "zone: example.dev",
    "== step deploy",
    "Plan: 2 to create, 1 to update",
    "[EmailRouting] fail — Forbidden: Authentication error (25.9s)",
    "[14:42:01.759] ERROR: [EmailCatchAll] fail — Forbidden: Authentication error",
    " ELIFECYCLE  Command failed with exit code 1.",
    "The deploy failed (exit 1).",
    "== step forget",
    "Cloudflare revoked the access.",
  ],
};

test("a run that failed at the deploy: that step failed, not the last one", () => {
  assert.equal(failedStepOf(failedDeploy), "deploy");
  assert.deepEqual(phases(failedDeploy), {
    check: "done",
    deploy: "failed",
    answer: "todo",
    verify: "todo",
    forget: "done",
  });
});

test("…and its error is the stack's own lines, without the clock, not 'revoked the access'", () => {
  assert.equal(
    runErrorOf(failedDeploy),
    "[EmailRouting] fail — Forbidden: Authentication error (25.9s)\n[EmailCatchAll] fail — Forbidden: Authentication error",
  );
});

test("an error of Cloudflare's API in the deploy's output is an error line", () => {
  const state = {
    status: "failed",
    lines: [
      "== step deploy",
      "✗ iterate-a-sandbox  Cloudflare.Container  failed",
      "CloudflareApiError: Unauthorized: You do not have access to Cloudflare Containers. [code: 1000]",
      "The deploy failed (exit 1).",
      "== step forget",
    ],
  };
  assert.equal(
    runErrorOf(state),
    "✗ iterate-a-sandbox  Cloudflare.Container  failed\nCloudflareApiError: Unauthorized: You do not have access to Cloudflare Containers. [code: 1000]",
  );
});

test("a deploy that ended with a part missing: the missing part first, in the run's plain words", () => {
  const state = {
    status: "failed",
    lines: [
      "== step check",
      "== step deploy",
      "== step answer",
      "iterate.example.dev answers: its issuer is https://iterate.example.dev",
      "== step verify",
      "Works: iterate answers at https://iterate.example.dev.",
      "Missing: mail to example.dev. Email Routing is off for the domain, so iterate receives no mail.",
      "== step forget",
      "Cloudflare revoked the access.",
    ],
  };
  assert.equal(failedStepOf(state), "verify");
  assert.equal(
    runErrorOf(state),
    "Missing: mail to example.dev. Email Routing is off for the domain, so iterate receives no mail.",
  );
  // after a failed deploy the run reports what is there: the missing part comes before the errors
  const reported = {
    status: "failed",
    lines: [
      "== step deploy",
      "[EmailRouting] fail — Forbidden: Authentication error (25.9s)",
      "The deploy failed (exit 1).",
      "Works: iterate answers at https://iterate.example.dev.",
      "Missing: mail to example.dev. Email Routing is off for the domain, so iterate receives no mail.",
      "== step forget",
    ],
  };
  assert.equal(
    runErrorOf(reported),
    "Missing: mail to example.dev. Email Routing is off for the domain, so iterate receives no mail.\n[EmailRouting] fail — Forbidden: Authentication error (25.9s)",
  );
});

test("a step with no error line: the last thing it said", () => {
  const state = {
    status: "failed",
    lines: [
      "== step check",
      "Cloudflare answered 403 to GET /zones/abc.",
      "== step forget",
      "Cloudflare revoked the access.",
    ],
  };
  assert.equal(failedStepOf(state), "check");
  assert.equal(runErrorOf(state), "Cloudflare answered 403 to GET /zones/abc.");
  assert.equal(phaseOf(state, "check"), "failed");
  assert.equal(phaseOf(state, "deploy"), "todo");
});

test("a run that ended before its first step: no step failed, and its one line is the error", () => {
  const state = {
    status: "failed",
    lines: [
      "The run's container stopped before the run ended.",
      "== step forget",
      "Cloudflare revoked the access.",
    ],
  };
  assert.equal(failedStepOf(state), "");
  assert.equal(runErrorOf(state), "The run's container stopped before the run ended.");
  assert.deepEqual(phases(state), {
    check: "todo",
    deploy: "todo",
    answer: "todo",
    verify: "todo",
    forget: "done",
  });
  assert.deepEqual(
    stepsOf(state).map((s) => s.id),
    ["", "forget"],
  );
});

test("a run that is running, and one that succeeded", () => {
  const running = { status: "running", lines: ["== step check", "zone: a.dev", "== step deploy"] };
  assert.equal(failedStepOf(running), null);
  assert.equal(runErrorOf(running), "");
  assert.deepEqual(phases(running), {
    check: "done",
    deploy: "now",
    answer: "todo",
    verify: "todo",
    forget: "todo",
  });
  const done = {
    status: "succeeded",
    lines: [
      "== step check",
      "== step deploy",
      "== step answer",
      "== step verify",
      "== step forget",
    ],
  };
  assert.deepEqual(phases(done), {
    check: "done",
    deploy: "done",
    answer: "done",
    verify: "done",
    forget: "done",
  });
});

// misherate.com, 2026-11-11: an earlier run left parts and its state was lost. The deploy tool
// refused them and gave its own advice, which is for whoever runs the tool, never for the page.
test("a part that exists already is said in plain words, with none of the tool's advice", () => {
  const state = {
    status: "failed",
    lines: [
      "== step check",
      "== step deploy",
      // The tool's lines as it wrote them (a run on ymobo.com without --adopt, 11 November 2026).
      "[16:20:42.677] INFO: Planning failed (11.2s)",
      "[16:20:42.692] ERROR (#1): OwnedBySomeoneElse: Cannot adopt resource 'ApexRecord' (Cloudflare.DNS.Record): it exists in the cloud but is not owned by this stack/stage/logical-id. Re-run with `--adopt` (or wrap the effect in `adopt(true)`) to take it over.",
      "    at fiber (/stack/node_modules/alchemy/src/Plan.ts:1:1)",
      "OwnedBySomeoneElse: Cannot adopt resource 'WildcardRecord' (Cloudflare.DNS.Record): it exists in the cloud but is not owned by this stack/stage/logical-id. Re-run with `--adopt` (or wrap the effect in `adopt(true)`) to take it over.",
      "OwnedBySomeoneElse: Cannot adopt resource 'EmailSending' (Cloudflare.Email.SendingSubdomain): it exists in the cloud but is not owned by this stack/stage/logical-id. Re-run with `--adopt` (or wrap the effect in `adopt(true)`) to take it over.",
      "The deploy failed (exit 1).",
      "== step forget",
    ],
  };
  const said = runErrorOf(state);
  assert.doesNotMatch(said, /adopt|OwnedBySomeoneElse|logical-id/);
  assert.match(said, /already has the DNS record of your domain/);
  assert.match(said, /already has the DNS record for every name under your domain/);
  assert.match(said, /already has Email Sending for your domain/);
  assert.equal(
    plainError("Something else failed. Re-run with `--adopt` (or wrap the effect) to go on."),
    "Something else failed.",
  );
});

// effect.ninja: the domain's names point at another service. The run stops before it changes
// anything, and the page shows the run's own words alone.
test("a run that stopped at records that point somewhere else says so, and nothing else", () => {
  const stop =
    "effect.ninja already has DNS records that point somewhere else: CNAME *.effect.ninja → cname.iterate.app. iterate needs these names. Delete these records in Cloudflare (your domain, DNS, Records) and press Try again, or choose another domain. Nothing was changed.";
  const state = {
    status: "failed",
    lines: [
      "== step check",
      "account: an account",
      "zone: effect.ninja",
      `Stopped: ${stop}`,
      "== step forget",
    ],
  };
  assert.equal(failedStepOf(state), "check");
  assert.equal(runErrorOf(state), stop);
});

// The run frees the routes that a Worker of an earlier iterate holds; a route that still stops the
// deploy is said in plain words, once, though two parts of the stack report it.
test("a route that another Worker holds: plain words, said once", () => {
  const held =
    "Cannot attach route *.example.dev/* to Worker iterate-example-dev-ingress: it is already attached to Worker iterate. Remove it from that Worker first";
  const state = {
    status: "failed",
    lines: [
      "== step check",
      "== step deploy",
      `[Ingress] fail — ${held} (2.1s)`,
      `[14:42:01.759] ERROR: [EmailCatchAll] fail — ${held}`,
      "The deploy failed (exit 1).",
      "Missing: the page for your first passkey (404).",
      "== step forget",
    ],
  };
  assert.equal(
    runErrorOf(state),
    "Missing: the page for your first passkey (404).\n*.example.dev/* is served by your Worker “iterate”, so iterate cannot serve it. In Cloudflare, open your domain, Workers Routes, and delete that route. Then press Try again.",
  );
});

test("the run's own stop at a route of somebody else's Worker is shown alone", () => {
  const stop =
    "example.dev/* is served by your Worker “shop”. iterate needs these addresses. In Cloudflare, open your domain, Workers Routes, and delete each of these routes, then press Try again. Or choose another domain. Nothing was changed.";
  assert.equal(
    runErrorOf({
      status: "failed",
      lines: ["== step check", "account: an account", `Stopped: ${stop}`, "== step forget"],
    }),
    stop,
  );
});

test("a run that worked: its notes for the person", () => {
  assert.deepEqual(
    notesOf({
      lines: [
        "== step check",
        "Removed the route example.dev/* from the Worker “iterate”, which an earlier iterate made: this deploy serves example.dev in its place.",
        "== step verify",
        "Works: iterate answers at https://iterate.example.dev.",
        "Note: An earlier iterate served example.dev from its Worker “iterate”. This deploy took over example.dev/* and *.example.dev/*. That Worker serves nothing on example.dev now: if you do not need what it kept, you can delete it in Cloudflare (Workers & Pages).",
        "== step forget",
      ],
    }),
    [
      "An earlier iterate served example.dev from its Worker “iterate”. This deploy took over example.dev/* and *.example.dev/*. That Worker serves nothing on example.dev now: if you do not need what it kept, you can delete it in Cloudflare (Workers & Pages).",
    ],
  );
  assert.deepEqual(notesOf({ lines: ["== step check"] }), []);
});

// The lines of container/check-result.sh, part 8, for a stack older than its Dash and its
// self-test page. A line about a page that is not in `PAGES` (an onboarding app, an Agents app)
// names no page.
const verified = [
  "== step verify",
  "Works: iterate answers at https://iterate.example.dev.",
  "Works: the page for your passkeys answers at https://iterate.example.dev/keys.",
  "page keys works",
  "Works: the page where you add people answers at https://iterate.example.dev/members.",
  "page members works",
  "Not there: the self-test page (https://iterate.example.dev/self-test answered 404).",
  "page self-test missing",
  "Works: the address for Claude Code answers at https://iterate.example.dev/mcp.",
  "page mcp works",
  "Not there: the onboarding app (https://onboarding.example.dev/ answered 404).",
  "page onboarding missing",
  "Works: the Agents app answers at https://agents.example.dev/",
  "page agents works",
  "Not there: the Dash (https://dash.example.dev/ answered 404).",
  "page dash missing",
];

test("the pages a run saw answer are read from its own lines, and a page no mind has now is none", () => {
  assert.deepEqual(pagesOf({ lines: verified }), {
    keys: "works",
    members: "works",
    "self-test": "missing",
    mcp: "works",
    dash: "missing",
  });
});

test("a run that looked at no page has no record, which is not a record of none", () => {
  assert.equal(pagesOf({ lines: failedDeploy.lines }), null);
  assert.equal(pagesOf({ lines: [] }), null);
});

test("a line that names no page the person's page links to is no record", () => {
  assert.equal(
    pagesOf({ lines: ["page billing works", "the page keys works", "page keys maybe"] }),
    null,
  );
});

test("a run that failed at a real part is not blamed on a page: only Missing lines are its error", () => {
  const state = {
    status: "failed",
    lines: [
      "== step verify",
      "Not there: the onboarding app (… answered 404).",
      "Missing: mail to example.dev.",
    ],
  };
  assert.equal(runErrorOf(state), "Missing: mail to example.dev.");
});

test("a run whose access still works after the revoke failed at giving it up", () => {
  const state = {
    status: "failed",
    failedAt: "forget",
    lines: [
      "== step check",
      "== step deploy",
      "== step answer",
      "== step verify",
      "Works: iterate answers at https://iterate.example.dev.",
      "== step forget",
      "Cloudflare revoked the access (200). The token still works: it gets 200.",
      STUCK,
    ],
  };
  assert.equal(failedStepOf(state), "forget");
  assert.deepEqual(phases(state), {
    check: "done",
    deploy: "done",
    answer: "done",
    verify: "done",
    forget: "failed",
  });
  assert.equal(runErrorOf(state), STUCK.slice(9));
});

test("…and after a failed deploy the access that still works is said too", () => {
  const state = { ...failedDeploy, lines: [...failedDeploy.lines, STUCK] };
  assert.equal(failedStepOf(state), "deploy");
  assert.ok(runErrorOf(state).endsWith(STUCK.slice(9)));
  assert.match(runErrorOf(state), /Forbidden: Authentication error/);
});

test("the copy for an agent: where, how it ended, what went wrong, and the whole log", () => {
  const report = reportOf(
    {
      ...failedDeploy,
      steps: [
        { id: "check", label: "Check the access to Cloudflare" },
        { id: "deploy", label: "Deploy iterate" },
        { id: "forget", label: "Give up the access to Cloudflare" },
      ],
      startedAt: Date.parse("2026-11-11T14:40:00Z"),
      endedAt: Date.parse("2026-11-11T14:42:30Z"),
    },
    "example.dev",
  );
  assert.equal(
    report,
    [
      "iterate Cloud (https://cloud.iterate.com) ran a deploy of iterate to example.dev, in my own Cloudflare account. It failed at the step “Deploy iterate”.",
      "",
      "What the page says went wrong:",
      "[EmailRouting] fail — Forbidden: Authentication error (25.9s)",
      "[EmailCatchAll] fail — Forbidden: Authentication error",
      "",
      "Started 2026-11-11T14:40:00.000Z, ended 2026-11-11T14:42:30.000Z.",
      "",
      "The deploy log:",
      "```text",
      ...failedDeploy.lines,
      "```",
    ].join("\n"),
  );
});