90 lines · 3.8 KB
/**
 * Git-push scratch sites, sweep and metering against a running dev server.
 *
 *   SITES_E2E_URL=http://localhost:3000 bun run e2e/sites-scratch-git.e2e.ts
 */
import { createScratchRepo, d1, DOMAIN, gitPush, HTTP_OK, log, runCron, send, waitForSite, WORKER } from "./helpers";

const HTTP_NOT_FOUND = 404;
const HTTP_GONE = 410;

let failures = 0;
function check(name: string, ok: boolean, detail = ""): void {
  log(`${ok ? "ok  " : "FAIL"} ${name}${ok || !detail ? "" : ` -- ${detail}`}`);
  if (!ok) failures += 1;
}

const scratch = await createScratchRepo();
const host = new URL(scratch.url).hostname;
const siteId = host.split("--")[0];
check(
  "POST /api/v1/scratch returns a remote, push token and claim token",
  scratch.pushToken.startsWith("gitfor_scratch_") &&
    scratch.claimToken.startsWith("gitfor_claim_") &&
    scratch.remote.endsWith(".git"),
);

const marker = `git-${Date.now()}`;
const pushed = await gitPush(scratch, { "index.html": `<h1>${marker}</h1>`, "_worker.js": WORKER });
check("git push with the scratch token succeeds", pushed.code === 0, pushed.output);
const live = await waitForSite(host, marker);
check("the push deploys to the scratch host", live.body.includes(marker), `${live.status} ${live.body.slice(0, 200)}`);

const previewMarker = `preview-${Date.now()}`;
const branchPush = await gitPush(scratch, { "index.html": `<h1>${previewMarker}</h1>` }, "feature");
check("pushing another branch succeeds", branchPush.code === 0, branchPush.output);
const preview = await waitForSite(`feature--${host.replace(`.${DOMAIN}`, "")}.${DOMAIN}`, previewMarker);
check(
  "each branch push deploys a preview on its own host",
  preview.body.includes(previewMarker),
  `${preview.status} ${preview.body.slice(0, 200)}`,