#!/usr/bin/env bun /** * End-to-end check for AI merge fixes against a running dev server started with MERGEFIX_PROVIDER=mock. * * bun scripts/e2e-mergefix.ts # seeds a user + token in the local D1, uses BASE_URL * BASE_URL=http://localhost:3015 GITFOR_TOKEN=gitfor_pat_... bun scripts/e2e-mergefix.ts * * Scenario 1: a cosmetic conflict is fixed automatically; the merge commit lands on the head branch, is * marker-free, and a CI run is scheduled for it. Scenario 2: a security-looking conflict is only suggested. */ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; const BASE_URL = (process.env.BASE_URL ?? "http://localhost:3015").replace(/\/$/, ""); const POLL_MS = 1000; const FIX_TIMEOUT_MS = 120_000; const CI_TIMEOUT_MS = 60_000; /** A merge commit lists itself plus two parents in `rev-list --parents`. */ const MERGE_PARENT_FIELDS = 3; /** Two generations and one Clef decision for the single hunk. */ const EXPECTED_MIN_CALLS = 3; function sh(cmd: string[], cwd?: string): string { const result = Bun.spawnSync(cmd, { cwd, stdout: "pipe", stderr: "pipe" }); if (result.exitCode !== 0) throw new Error(`${cmd.join(" ")} failed: ${result.stderr.toString()}`); return result.stdout.toString(); } function d1(sql: string): Array> { const out = sh(["bunx", "wrangler", "d1", "execute", "gitfor", "--local", "--json", "--command", sql]); return (JSON.parse(out) as Array<{ results: Array> }>)[0]?.results ?? []; } async function sha256Hex(value: string): Promise { const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(value)); return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, "0")).join(""); } /** Local-only bootstrap: a throwaway user with a classic token, written straight into the dev D1. */ async function seedToken(): Promise { const suffix = crypto.randomUUID().slice(0, 8); const id = `e2e${suffix}`; const token = `gitfor_pat_${crypto.randomUUID().replaceAll("-", "")}`; const now = Date.now(); d1( `INSERT INTO accounts (id, name, kind, display_name, created_at) VALUES ('${id}', 'mergefix-${suffix}', 'user', 'Merge Fix E2E', ${now}); INSERT INTO users (account_id, email, password_hash, github_id, is_admin) VALUES ('${id}', 'mergefix-${suffix}@example.com', NULL, NULL, 0); INSERT INTO access_tokens (id, user_id, name, token_hash, token_prefix, scopes, expires_at, created_at) VALUES ('${id}tok', '${id}', 'e2e', '${await sha256Hex(token)}', '${token.slice(0, 16)}', 'repo', NULL, ${now});`, ); return token; } const TOKEN = process.env.GITFOR_TOKEN ?? (await seedToken()); async function api(method: string, path: string, body?: unknown, expected: number[] = [200, 201, 202]): Promise { const response = await fetch(`${BASE_URL}/api/v1${path}`, { method, headers: { authorization: `Bearer ${TOKEN}`, "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body), }); const text = await response.text(); if (!expected.includes(response.status)) throw new Error(`${method} ${path} -> ${response.status}: ${text}`); return JSON.parse(text) as T; } function assert(condition: unknown, message: string): asserts condition { if (!condition) throw new Error(`Assertion failed: ${message}`); } async function poll(what: string, timeoutMs: number, probe: () => Promise): Promise { const deadline = Date.now() + timeoutMs; while (Date.now() < deadline) { const value = await probe(); if (value !== null) return value; await Bun.sleep(POLL_MS); } throw new Error(`Timed out waiting for ${what}`); } type Fix = { status: string; error: string | null; commit_sha: string | null; files: Array> }; async function scenario(login: string, name: string, lines: { base: string; head: string; main: string }) { const repo = await api<{ id?: string; name: string }>("POST", "/repos", { name }); const work = mkdtempSync(join(tmpdir(), "mergefix-e2e-")); const url = new URL(`${BASE_URL}/${login}/${repo.name}.git`); url.username = "x"; url.password = TOKEN; try { const git = (...args: string[]) => sh(["git", "-c", "user.name=e2e", "-c", "user.email=e2e@example.com", ...args], work); git("init", "-q", "-b", "main"); mkdirSync(join(work, ".gitfor/workflows"), { recursive: true }); writeFileSync( join(work, ".gitfor/workflows/ci.yml"), "name: ci\non: [push]\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo ok\n", ); const file = (middle: string) => `header\none\n${middle}\nthree\nfooter\n`; writeFileSync(join(work, "app.txt"), file(lines.base)); git("add", "."); git("commit", "-qm", "base"); git("checkout", "-q", "-b", "feature"); writeFileSync(join(work, "app.txt"), file(lines.head)); git("commit", "-qam", "head change"); git("checkout", "-q", "main"); writeFileSync(join(work, "app.txt"), file(lines.main)); git("commit", "-qam", "main change"); git("push", "-q", url.toString(), "main", "feature"); const pull = await api<{ number: number }>("POST", `/repos/${login}/${name}/pulls`, { title: "Change line two", body: "Updates the second line.", head: "feature", base: "main", }); const before = git("rev-parse", "feature").trim(); await api("POST", `/repos/${login}/${name}/pulls/${pull.number}/ai-fix`); const fix = await poll("the AI fix", FIX_TIMEOUT_MS, async () => { const current = await api("GET", `/repos/${login}/${name}/pulls/${pull.number}/ai-fix`); return ["queued", "running", "applying"].includes(current.status) ? null : current; }); const remoteHead = () => git("ls-remote", url.toString(), "refs/heads/feature").split("\t")[0]; return { repo: name, pull, fix, before, work, url: url.toString(), git, remoteHead }; } catch (error) { rmSync(work, { recursive: true, force: true }); throw error; } } const user = await api<{ login: string }>("GET", "/user"); const stamp = Date.now().toString(36); const cosmetic = await scenario(user.login, `mergefix-e2e-${stamp}`, { base: "two", head: "two (head wording)", main: "two (main wording)", }); try { assert(cosmetic.fix.status === "applied", `expected applied, got ${cosmetic.fix.status}: ${cosmetic.fix.error}`); const sha = cosmetic.fix.commit_sha; assert(sha, "fix commit sha"); assert(cosmetic.remoteHead() === sha, "head branch moved to the fix commit"); cosmetic.git("fetch", "-q", cosmetic.url, `+refs/heads/feature:refs/remotes/check/feature`); const parents = cosmetic.git("rev-list", "--parents", "-n", "1", sha).trim().split(" "); assert( parents.length === MERGE_PARENT_FIELDS && parents[1] === cosmetic.before, "merge commit has head and base as parents", ); const content = cosmetic.git("show", `${sha}:app.txt`); assert(!/^(<{7}|={7}|>{7})/m.test(content), "no conflict markers"); assert(content.includes("two (head wording)") && content.includes("two (main wording)"), "both sides kept"); if (!process.env.GITFOR_TOKEN) { const run = await poll("a CI run for the fix commit", CI_TIMEOUT_MS, async () => { const rows = d1(`SELECT id, event, status FROM workflow_runs WHERE sha = '${sha}' LIMIT 1`); return rows[0] ?? null; }); console.log( `CI run ${String(run.id)} (${String(run.event)}, ${String(run.status)}) scheduled for ${sha.slice(0, 7)}`, ); const usage = d1( `SELECT calls FROM ai_usage a JOIN accounts c ON c.id = a.account_id WHERE c.name = '${user.login}'`, ); assert(Number(usage[0]?.calls ?? 0) >= EXPECTED_MIN_CALLS, "model usage was recorded"); } console.log(`ok: cosmetic conflict auto-fixed in ${sha.slice(0, 7)}`); } finally { rmSync(cosmetic.work, { recursive: true, force: true }); } const sensitive = await scenario(user.login, `mergefix-e2e-sec-${stamp}`, { base: "checkAccess()", head: "checkAuth(user)", main: "checkPermission(user, 'admin')", }); try { assert( sensitive.fix.status === "suggested", `expected suggested, got ${sensitive.fix.status}: ${sensitive.fix.error}`, ); const file = sensitive.fix.files[0]; assert(file?.risk === "security-sensitive" || file?.auto_apply === false, "security conflict was not auto-applied"); assert(sensitive.remoteHead() === sensitive.before, "head branch untouched"); console.log(`ok: security-sensitive conflict left as suggestions (${JSON.stringify(file?.reasons)})`); } finally { rmSync(sensitive.work, { recursive: true, force: true }); }