#!/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.
*/
export {};
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 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();
return { status: response.status, headers: response.headers, body: text ? JSON.parse(text) : null };
}
const me = await call("/api/v1/user", PAT);
const name = `editor-e2e-${Date.now()}`;
const created = await call("/api/v1/repos", PAT, { method: "POST", body: { name, private: true } });
check(created.status === 201, `created ${me.body.login}/${name}`);
const repo = `/repos/${me.body.login}/${name}`;
for (const [path, content] of [
["README.md", "# hello\n"],
["src/app.ts", "export const answer = 41;\n"],
["src/old.txt", "remove me\n"],
["assets/big.bin", LFS_POINTER],