260 lines · 10.4 KB
#!/usr/bin/env bun
/**
 * End-to-end check of the workspace editor protocol against a running dev server.
 * Usage: BASE=http://localhost:3000 GITFOR_TOKEN=gitfor_pat_... bun run scripts/e2e-editor.ts
 * Optional: GITFOR_WRITER_TOKEN (a second user) and GITFOR_E2E_D1 (local D1 sqlite) cover protected branches.
 */
import { execFileSync } from "node:child_process";

const BASE = process.env.BASE ?? "http://localhost:3000";
const PAT = process.env.GITFOR_TOKEN ?? "";
if (!PAT) throw new Error("Set GITFOR_TOKEN to a personal access token");

const HTTP = {
  ok: 200,
  created: 201,
  noContent: 204,
  redirect: 300,
  badRequest: 400,
  unauthorized: 401,
  forbidden: 403,
  tooLarge: 413,
  conflict: 409,
  serverError: 500,
} as const;

const LFS_POINTER = `version https://git-lfs.github.com/spec/v1\noid sha256:${"a".repeat(64)}\nsize 12\n`;

function check(condition: unknown, message: string): asserts condition {
  if (!condition) throw new Error(`FAIL: ${message}`);
  console.log(`ok - ${message}`);
}

async function call(path: string, token: string, init: { method?: string; body?: unknown } = {}) {
  const response = await fetch(`${BASE}${path}`, {
    method: init.method ?? "GET",
    headers: { authorization: `Bearer ${token}`, "content-type": "application/json", origin: "null" },
    body: init.body === undefined ? undefined : JSON.stringify(init.body),
    signal: AbortSignal.timeout(60_000),
  });
  const text = await response.text();