From 0cd17bfc5fa3e9776229db95d35eece45e84ac63 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 2 Nov 2026 19:43:50 -0800 Subject: [PATCH 1/6] chore(quest): claim quest/m2/worker-dev.md Quest-Claim: c04f2edf89b5868e From 1556575c778511c59ab5a65539b330f1f09a3101 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 2 Nov 2026 19:50:07 -0800 Subject: [PATCH 2/6] feat(kix): isolate seeded servers' cookies, seed raw output, and smoke-check the seeded server Co-Authored-By: Claude Opus 5.5 --- cloudflare/dev/seed.ts | 48 +++++++++++++++++--- cloudflare/dev/serve.ts | 87 ++++++++++++++++++++++++++++++------ cloudflare/package.json | 2 +- cloudflare/test/seed.test.ts | 28 ++++++++++++ 4 files changed, 146 insertions(+), 19 deletions(-) diff --git a/cloudflare/dev/seed.ts b/cloudflare/dev/seed.ts index c69cb9d6..fe7b8625 100644 --- a/cloudflare/dev/seed.ts +++ b/cloudflare/dev/seed.ts @@ -2,7 +2,9 @@ // stubbed end-to-end tests. Sign-in, enrollment, observations, and webhooks go // through the Worker's own routes against the fake GitHub, so they follow the // current code; only runners and jobs, which need a live runner, are written -// to D1 directly. +// to D1 directly, and their raw output through the store a runner's uploads use. +import { append } from "../src/jobs/output"; +import type { Runner } from "../src/jobs/store"; import { type Comment, type Pull, @@ -249,7 +251,15 @@ const form = (fields: Record) => ({ * paused), observe them, and add jobs in every state the inbox shows. * Returns the signed-in user and session cookie. */ -export async function seed(options: { fetch: Fetch; db: D1Database; world: World; origin: string; secret: string }) { +export async function seed(options: { + fetch: Fetch; + db: D1Database; + /** The Worker's `OUTPUT` bucket, for the jobs' raw output. */ + output: R2Bucket; + world: World; + origin: string; + secret: string; +}) { const { db, world, origin } = options; const post = (path: string, fields: Record, cookie: string) => { const body = form(fields); @@ -271,9 +281,10 @@ export async function seed(options: { fetch: Fetch; db: D1Database; world: World 302, "OAuth callback", ); + // The seeded server names its cookies after its port, so match the session by suffix. const cookie = cookies(callback) .split("; ") - .filter((c) => c.startsWith("better-auth.session_token=")) + .filter((c) => c.split("=")[0]!.endsWith("better-auth.session_token")) .join("; "); if (!cookie) throw new Error("Seed sign-in returned no session"); const me = world.users[0]!; @@ -320,12 +331,13 @@ export async function seed(options: { fetch: Fetch; db: D1Database; world: World "issue webhook", ); - await jobs(db, user, rocket); + await jobs({ DB: db, OUTPUT: options.output }, user, rocket); return { user, cookie }; } // Runners and jobs a live runner would have reported, in each state the inbox shows. -async function jobs(db: D1Database, user: string, repo: Repo) { +async function jobs(storage: { DB: D1Database; OUTPUT: R2Bucket }, user: string, repo: Repo) { + const db = storage.DB; const now = Date.now(); const commit = commitOf(repo); const head = (n: number) => @@ -627,4 +639,30 @@ async function jobs(db: D1Database, user: string, repo: Repo) { ]; }), ]); + + // Raw output behind the agent cards' disclosure: a transcript long enough to + // page, a short one, and one that reached the size cap. + const output: Record = { + "seed-plan-settings": [ + "$ quest guide\nRead the guide.\n", + "$ git log --oneline -3 -- src/settings\n4b1c2d0 Save settings on blur\n9e8f7a6 Add the settings page\n", + "Opening issue #7 and its five comments.\nThe reports all mention a background tab on a phone.\n", + "$ just test settings\n✓ saves on blur (12 ms)\n✓ restores a draft (8 ms)\n", + "Simulating an expired session while the tab is hidden...\nThe save request returns 401 and the draft is dropped.\n", + "Reproduced. Writing up the cause before planning the fix.\n", + ], + "seed-plan-config": [ + "$ rg -n 'config.toml' src\nsrc/config.rs:14: let text = fs::read_to_string(path)?;\n", + "An empty file parses as an error, not as defaults. Asking how it should behave.\n", + ], + "seed-iterate-cache": ["$ nix build .#cache\nbuilding '/nix/store/…-cache.drv'...\n"], + }; + for (const [job, chunks] of Object.entries(output)) { + const id = list.find((j) => j.id === job)!.runner!; + const [, name, platform, vm] = runners.find(([runner]) => runner === id)!; + const runner: Runner = { id, owner: user, name, platform, vm }; + for (const [seq, text] of chunks.entries()) await append(storage, runner, { job, attempt: 1, seq, text }, now); + } + // The cap's note, without uploading 64 MiB. + await db.prepare("UPDATE job_output_attempts SET truncated=1 WHERE job='seed-iterate-cache'").run(); } diff --git a/cloudflare/dev/serve.ts b/cloudflare/dev/serve.ts index 697c5a13..b44dc3f8 100644 --- a/cloudflare/dev/serve.ts +++ b/cloudflare/dev/serve.ts @@ -2,8 +2,10 @@ // local sign-in, for checking UI changes in a browser. It runs the bundle that // `wrangler deploy --dry-run` writes (see the `seeded` package script) on // Miniflare directly, since `wrangler dev`'s proxy hangs in some sandboxes. -// Storage is in memory, so every run starts from the same seed. +// Storage is in memory, so every run starts from the same seed. With --check, +// it checks what only this server wires up, on a free port, and exits. import { readFileSync, readdirSync } from "node:fs"; +import { type AddressInfo, createServer } from "node:net"; import { join } from "node:path"; import { parseArgs } from "node:util"; import { @@ -18,8 +20,21 @@ import { fakeGitHub } from "./github"; import { seed, world } from "./seed"; const root = join(import.meta.dirname, ".."); -const { values } = parseArgs({ options: { port: { type: "string", default: "8787" } } }); -const port = Number(values.port); +const { values } = parseArgs({ + options: { port: { type: "string", default: "8787" }, check: { type: "boolean", default: false } }, +}); + +/** A port nothing listens on, so checks run beside other seeded servers. */ +async function freePort() { + const server = createServer(); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const { port } = server.address() as AddressInfo; + await new Promise((resolve) => server.close(resolve)); + return port; +} + +const port = values.check ? await freePort() : Number(values.port); +const prefix = `seeded-${port}.`; const origin = `http://localhost:${port}`; const secret = "dev-webhook-secret"; const data = world(); @@ -33,20 +48,44 @@ const { workerOptions } = unstable_getMiniflareWorkerOptions(join(root, "wrangle // The bundle is a single ES module, and Miniflare 5 cannot convert module rules. const { modulesRules: _, ...app } = workerOptions; -// GitHub's authorize page would leave localhost, so the entry Worker sends that -// redirect straight to the callback, which the fake GitHub completes as the -// seeded maintainer. Everything else, WebSockets included, passes through. +// Browsers share localhost's cookies across ports, so seeded servers on two +// ports would sign each other out. The entry Worker gives this server's cookies +// their own names: it prefixes each cookie the app sets with COOKIES, and +// passes the app only the cookies so prefixed, without it. +// +// GitHub's authorize page would leave localhost, so the entry Worker also sends +// that redirect straight to the callback, which the fake GitHub completes as +// the seeded maintainer. Everything else, WebSockets included, passes through. const entry = `export default { async fetch(request, env) { - const response = await env.APP.fetch(request); + const sent = (request.headers.get("Cookie") ?? "") + .split(/;\\s*/) + .filter((cookie) => cookie.startsWith(env.COOKIES)) + .map((cookie) => cookie.slice(env.COOKIES.length)); + const forwarded = new Headers(request.headers); + if (sent.length) forwarded.set("Cookie", sent.join("; ")); + else forwarded.delete("Cookie"); + const response = await env.APP.fetch(new Request(request, { headers: forwarded })); + + const cookies = response.headers.getSetCookie(); const location = response.headers.get("Location"); - if (!location?.startsWith("https://github.com/login/oauth/authorize")) return response; - const callback = new URL("/api/auth/callback/github", request.url); - callback.searchParams.set("code", "fake"); - callback.searchParams.set("state", new URL(location).searchParams.get("state") ?? ""); + const authorize = location?.startsWith("https://github.com/login/oauth/authorize"); + if (!cookies.length && !authorize) return response; const headers = new Headers(response.headers); - headers.set("Location", callback.href); - return new Response(null, { status: response.status, headers }); + headers.delete("Set-Cookie"); + for (const cookie of cookies) headers.append("Set-Cookie", env.COOKIES + cookie); + if (authorize) { + const callback = new URL("/api/auth/callback/github", request.url); + callback.searchParams.set("code", "fake"); + callback.searchParams.set("state", new URL(location).searchParams.get("state") ?? ""); + headers.set("Location", callback.href); + } + return new Response(response.body, { + status: response.status, + statusText: response.statusText, + headers, + webSocket: response.webSocket, + }); }, };`; @@ -60,6 +99,7 @@ const mf = new Miniflare( modules: true, script: entry, compatibilityDate: app.compatibilityDate, + bindings: { COOKIES: prefix }, serviceBindings: { APP: "app" }, }, { @@ -121,10 +161,31 @@ for (const file of readdirSync(migrations) const { cookie } = await seed({ fetch: (path, init) => mf.dispatchFetch(`${origin}${path}`, init as never) as unknown as Promise, db, + output: (await mf.getR2Bucket("OUTPUT", "app")) as unknown as R2Bucket, world: data, origin, secret, }); + +if (values.check) { + // Over the listening socket, as a browser would reach it. + const problems: string[] = []; + const css = await fetch(`${origin}/site.css`); + if (!css.ok || !css.headers.get("Content-Type")?.startsWith("text/css")) + problems.push(`/site.css returned ${css.status} ${css.headers.get("Content-Type")}, not the built stylesheet`); + if (!cookie.startsWith(prefix)) problems.push(`the session cookie is not named after the port: ${cookie}`); + const inbox = async (sent: string) => + (await fetch(`${origin}/inbox`, { headers: { Cookie: sent }, redirect: "manual" })).status; + const own = await inbox(cookie); + if (own !== 200) problems.push(`/inbox returned ${own} with the session cookie`); + // Another server's cookie, as a browser sends every localhost port's, is not this server's session. + const foreign = await inbox(cookie.slice(prefix.length)); + if (foreign === 200) problems.push("/inbox accepted the session under another server's cookie name"); + await mf.dispose(); + for (const problem of problems) console.error(`Seeded server check failed: ${problem}`); + process.exit(problems.length ? 1 : 0); +} + const [login] = data.users.map((user) => user.login); console.log(` Seeded kix at ${origin}/inbox, signed in as ${login}. Ctrl-C stops it. diff --git a/cloudflare/package.json b/cloudflare/package.json index 7d972a3e..a58f1e22 100644 --- a/cloudflare/package.json +++ b/cloudflare/package.json @@ -7,7 +7,7 @@ "seeded": "wrangler deploy --dry-run --outdir .wrangler/seeded && bun dev/serve.ts", "build": "bun scripts/build.ts", "check": "bun run build && wrangler types --check && tsc --noEmit && prettier --check . && wrangler deploy --dry-run", - "test": "bun run build && vitest run", + "test": "bun run build && vitest run && bun run seeded --check", "fix": "prettier --write .", "types": "wrangler types", "migrate": "wrangler d1 migrations apply DB --local", diff --git a/cloudflare/test/seed.test.ts b/cloudflare/test/seed.test.ts index 2d0c8079..3dbc9b62 100644 --- a/cloudflare/test/seed.test.ts +++ b/cloudflare/test/seed.test.ts @@ -33,6 +33,7 @@ async function seeded(edit: (world: World) => void = () => {}) { const { user, cookie } = await seed({ fetch: request, db, + output: env.OUTPUT, world: data, origin, secret: env.GITHUB_WEBHOOK_SECRET, @@ -142,6 +143,33 @@ it("seeds a representative inbox through the Worker's own routes", async () => { expect(data.repos[0]!.pulls.find((p) => p.number === 12)!.closed).toBe(true); }); +it("seeds raw output that pages, ends, and reaches the cap", async () => { + const { request, cookie } = await seeded(); + const html = await (await request("/inbox", { headers: { Cookie: cookie } })).text(); + const read = async (job: string, after?: string) => { + const url = `/projects/github/example/rocket/jobs/${job}/output${after ? `?after=${after}` : ""}`; + expect(html).toContain(`data-output="${url.split("?")[0]}"`); + const response = await request(url, { headers: { Cookie: cookie } }); + expect(response.status).toBe(200); + return response.json<{ + output: { text: string }[]; + next: { attempt: number; seq: number } | null; + truncated: number[]; + }>(); + }; + // More than a page, so the card offers "Load more". + const first = await read("seed-plan-settings"); + expect(first.output).toHaveLength(4); + expect(first.next).not.toBeNull(); + const rest = await read("seed-plan-settings", `${first.next!.attempt}.${first.next!.seq}`); + expect(rest.output.at(-1)!.text).toContain("Reproduced."); + expect(rest.next).toBeNull(); + expect((await read("seed-plan-config")).truncated).toEqual([]); + expect((await read("seed-iterate-cache")).truncated).toEqual([1]); + // A card without output says so when opened. + expect((await read("seed-start-offline")).output).toEqual([]); +}); + it("lists the seeded maintainer's repositories in the project picker", async () => { const { data, request, cookie } = await seeded(); // Repositories installed after seeding, which seeding would otherwise enroll. From 91d550b435d730f4d466df8691cfbbe0a7acc1a3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 2 Nov 2026 19:53:05 -0800 Subject: [PATCH 3/6] ci(kix): run each Worker test case in its own process daily Co-Authored-By: Claude Opus 5.5 --- .github/workflows/isolated.yml | 34 ++++++++++++++ cloudflare/README.md | 16 ++++++- cloudflare/scripts/isolated.ts | 83 ++++++++++++++++++++++++++++++++++ justfile | 9 ++++ 4 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/isolated.yml create mode 100644 cloudflare/scripts/isolated.ts diff --git a/.github/workflows/isolated.yml b/.github/workflows/isolated.yml new file mode 100644 index 00000000..88623fa7 --- /dev/null +++ b/.github/workflows/isolated.yml @@ -0,0 +1,34 @@ +name: Isolated + +# Runs each Worker test case in its own process, catching a test that passes +# only on module-level state another test left behind. Too slow for every pull +# request, so it runs daily on main. +on: + schedule: + - cron: "23 6 * * *" + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: isolated-${{ github.ref }} + cancel-in-progress: true + +jobs: + worker: + runs-on: ubuntu-latest + timeout-minutes: 120 + steps: + - uses: DeterminateSystems/nix-installer-action@1d87d45818068401a10cf16bdc5f00b24994a83f + with: + determinate: false + # The build snapshots finished quests from the full history, as the Check job's does. + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + persist-credentials: false + - name: Install dependencies + run: nix develop --command just install + - name: Run each Worker test case alone + run: nix develop --command just worker-test-isolated diff --git a/cloudflare/README.md b/cloudflare/README.md index c05e060c..6e1786c2 100644 --- a/cloudflare/README.md +++ b/cloudflare/README.md @@ -366,7 +366,8 @@ projects through the Worker's own routes: ready, claimed, and blocked quests, issues (one known only from a webhook, one with a long title), pull requests with checks and reviews (one clean to merge), and a paused second project. It then adds runners and jobs that are queued, running, failed, or waiting on an agent's questions, a -tool permission, or an account limit. +tool permission, or an account limit, and raw output behind their cards: one +long enough to page, a short one, and one that reached the size cap. `dev/github.ts` fakes GitHub for every outbound request, so nothing leaves localhost and no real credential is used. It models what publication needs: @@ -375,7 +376,9 @@ request creation, so a runner's job publishes end to end. Like GitHub, it scopes each installation token to the repositories and permissions it was minted for. Notifications are off, since a push would reach a real push service. Sign in with GitHub completes as the -seeded maintainer. From a shell, sign in with a cookie jar: +seeded maintainer. Browsers share localhost's cookies across ports, so each +seeded server names its cookies after its port (`seeded-8788.…`), and servers +on different ports keep their own sessions. From a shell, sign in with a cookie jar: ```sh curl -sL -c jar -b jar -H 'Origin: http://localhost:8787' -d provider=github http://localhost:8787/sign-in @@ -385,6 +388,8 @@ Check phone and desktop widths with the browser's device emulation. The Worker tests run the same seed (`test/seed.test.ts`), so it follows schema and route changes, and later end-to-end tests can start from it. The publication tests (`test/publish.test.ts`) use the same fake with a smaller world of their own. +`just worker-test` also starts this server with `--check` on a free port, which +checks what only it wires up: the built static assets and its cookie names. ## Checks @@ -417,6 +422,13 @@ tests encrypt RFC 8291's example, verify VAPID signatures, decrypt what a subscribed device receives, and check that only decisions push, once each. No OAuth credentials or Cloudflare account are required for checks. +The Worker tests share one runtime and reset only storage between tests, so a +test could still lean on module-level state another left behind. A daily CI job +(`.github/workflows/isolated.yml`) runs each test case in its own process +instead, which is too slow for every pull request. Run it locally with +`just worker-test-isolated`, optionally with `--jobs N` and file filters such as +`inbox`. + After changing bindings, regenerate types: ```sh diff --git a/cloudflare/scripts/isolated.ts b/cloudflare/scripts/isolated.ts new file mode 100644 index 00000000..d99da1e2 --- /dev/null +++ b/cloudflare/scripts/isolated.ts @@ -0,0 +1,83 @@ +// Run each Worker test case in its own vitest process. The suite shares one +// runtime and resets only storage between tests (vitest.config.ts), so a test +// can still lean on module-level state another left behind. Each case alone is +// too slow for every PR (seconds of startup apiece), so a scheduled CI job runs +// this instead. Cases declared on one line, such as a loop's, run together. +// +// bun scripts/isolated.ts [--jobs N] [filter...] +// +// Filters keep the cases whose file path contains one of them. Run it after +// `bun run build`, as `vitest run` is. +import { spawn } from "node:child_process"; +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { availableParallelism, tmpdir } from "node:os"; +import { join, relative } from "node:path"; +import { parseArgs } from "node:util"; + +const root = join(import.meta.dirname, ".."); +const { values, positionals: filters } = parseArgs({ + options: { jobs: { type: "string", default: String(Math.max(1, Math.floor(availableParallelism() / 2))) } }, + allowPositionals: true, +}); +const jobs = Number(values.jobs); +if (!Number.isInteger(jobs) || jobs < 1) throw new Error(`--jobs must be a positive integer, not ${values.jobs}`); + +/** Run vitest with `args` in the Worker's directory, returning its exit code and combined output. */ +function vitest(args: string[]) { + return new Promise<{ code: number; output: string }>((resolve, reject) => { + const child = spawn("bun", ["x", "vitest", ...args], { cwd: root, stdio: ["ignore", "pipe", "pipe"] }); + let output = ""; + child.stdout.on("data", (chunk) => (output += chunk)); + child.stderr.on("data", (chunk) => (output += chunk)); + child.on("error", reject); + child.on("close", (code, signal) => resolve({ code: code ?? (signal ? 1 : 0), output })); + }); +} + +// Every case's location, from vitest's own collection. +const scratch = mkdtempSync(join(tmpdir(), "quest-isolated-")); +let cases: string[]; +try { + const file = join(scratch, "cases.json"); + const listed = await vitest(["list", `--json=${file}`]); + if (listed.code !== 0) { + process.stderr.write(listed.output); + throw new Error("vitest list failed"); + } + const tests = JSON.parse(readFileSync(file, "utf8")) as { file: string; location?: { line: number } }[]; + cases = [ + ...new Set( + tests.map((test) => { + if (!test.location) throw new Error(`vitest listed ${test.file} without a location`); + return `${relative(root, test.file)}:${test.location.line}`; + }), + ), + ].filter((location) => !filters.length || filters.some((filter) => location.includes(filter))); +} finally { + rmSync(scratch, { recursive: true, force: true }); +} +if (!cases.length) throw new Error("No test cases matched"); + +console.log(`Running ${cases.length} test cases, each in its own process, ${jobs} at a time.`); +const failed: string[] = []; +let done = 0; +const queue = [...cases]; +await Promise.all( + Array.from({ length: Math.min(jobs, queue.length) }, async () => { + for (let location = queue.shift(); location; location = queue.shift()) { + // A location matching no test fails too, so a stale listing cannot pass. + const { code, output } = await vitest(["run", location]); + done++; + if (code === 0) continue; + failed.push(location); + console.log(`\n✗ ${location} (${done}/${cases.length})\n${output}`); + } + }), +); + +if (failed.length) { + console.log(`\n${failed.length} of ${cases.length} test cases failed on their own:`); + for (const location of failed.sort()) console.log(` ${location}`); + process.exit(1); +} +console.log(`All ${cases.length} test cases passed on their own.`); diff --git a/justfile b/justfile index ca2f2a67..f9a6b05d 100644 --- a/justfile +++ b/justfile @@ -79,6 +79,15 @@ worker-test: _worker-test: bun run --cwd cloudflare test +# Run each Worker test case in its own process, as the scheduled Isolated job does. +# Pass `--jobs N` or file filters, for example `just worker-test-isolated inbox`. +worker-test-isolated *args: + {{gate}} _worker-test-isolated {{args}} + +_worker-test-isolated *args: + bun run --cwd cloudflare build + bun cloudflare/scripts/isolated.ts {{args}} + # Serve the Worker on localhost with seeded data and a fake GitHub, for checking UI. worker-seeded *args: bun run --cwd cloudflare seeded {{args}} From 32d7eae7807467daf8ce19e3c29e01a5a086148d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 2 Nov 2026 20:15:25 -0800 Subject: [PATCH 4/6] feat(kix): model default branches, roles, repository settings, and files per commit in the fake GitHub The fake answered as if every default branch were `main`, every repository public and MIT-licensed with every merge method, every role maintain, write, or read, and every commit holding the default branch's files. Tests that need otherwise mocked `fetch` themselves. - Honor `defaultBranch` everywhere the fake named `main`, and follow a listed default branch for `/commits/`. - Add per-login `roles` (including null for a 404), `private`, `archived`, `license`, and `mergeMethods`, which refuses a disallowed merge as GitHub does. - Answer `/contents/?ref=` from the ref's tree, written trees included. - Answer SPDX for the license asked, approving all but source-available ones. - Derive objects from the repository ID, so a rename or transfer keeps them. Co-Authored-By: Claude Opus 5.5 --- cloudflare/dev/github.ts | 155 ++++++++++++++++++++++++--------------- 1 file changed, 95 insertions(+), 60 deletions(-) diff --git a/cloudflare/dev/github.ts b/cloudflare/dev/github.ts index b988b536..8ac71c5e 100644 --- a/cloudflare/dev/github.ts +++ b/cloudflare/dev/github.ts @@ -82,8 +82,19 @@ export type Repo = { maintainers: string[]; /** Logins that can write without maintaining, whose approvals count as GitHub's writers' do. */ writers?: string[]; + /** + * Roles by login, ahead of `maintainers` and `writers`, such as admin, triage, or a custom role. + * Null is someone without access, whose permission GitHub answers 404 for. + */ + roles?: Record; /** Defaults to main. */ defaultBranch?: string; + private?: boolean; + archived?: boolean; + /** The SPDX ID of the license GitHub detects; MIT by default, none when null. */ + license?: string | null; + /** The merge methods the repository allows; all of them by default. */ + mergeMethods?: ("merge" | "squash" | "rebase")[]; /** The default branch's files. */ files: { path: string; content: string }[]; issues: Issue[]; @@ -92,7 +103,7 @@ export type Repo = { * Upstream branches and their commits. A pull request's branch that is not * listed is at a commit derived from its number; a `quest/*` branch with no * open pull request is a claim, unless it is still at a merged one's head. - * Listing `main` moves the default branch on for comparisons with it. + * Listing the default branch moves it on from the commit of `files`. */ branches: Record; /** @@ -104,9 +115,9 @@ export type Repo = { trees: Record; /** Check names the default branch's rulesets require. */ required?: string[]; - /** The default branch's other ruleset rules, as `GET /rules/branches/main` lists them. */ + /** The default branch's other ruleset rules, as `GET /rules/branches/` lists them. */ rules?: Record[]; - /** The default branch's classic protection, as `GET /branches/main/protection` returns it; none when absent. */ + /** The default branch's classic protection, as `GET /branches//protection` returns it; none when absent. */ protection?: Record; /** Rulesets by ID, as `GET /rulesets/` shows them to the App. */ rulesets?: Record; @@ -127,14 +138,25 @@ export const blob = (content: string | Uint8Array) => { const bytes = typeof content === "string" ? Buffer.from(content, "utf8") : content; return createHash("sha1").update(`blob ${bytes.length}\0`).update(bytes).digest("hex"); }; +/** The default branch's first commit, whose tree is `files`. Objects derive from the ID, which a rename keeps. */ +export const commitOf = (repo: Repo) => sha1(`commit ${repo.id}`); +const treeOf = (repo: Repo) => sha1(`tree ${repo.id}`); +const defaultOf = (repo: Repo) => repo.defaultBranch ?? "main"; +const methodsOf = (repo: Repo) => repo.mergeMethods ?? ["merge", "squash", "rebase"]; /** The default branch's commit. */ -export const commitOf = (repo: Repo) => sha1(`commit ${repo.owner}/${repo.name}`); -const treeOf = (repo: Repo) => sha1(`tree ${repo.owner}/${repo.name}`); +const tipOf = (repo: Repo) => repo.branches[defaultOf(repo)] ?? commitOf(repo); +/** A login's role: its listed one, else maintain or write as `maintainers` and `writers` say, else read. */ +const roleOf = (repo: Repo, login: string) => + repo.roles && login in repo.roles + ? repo.roles[login]! + : repo.maintainers.includes(login) + ? "maintain" + : repo.writers?.includes(login) + ? "write" + : "read"; /** A pull request's head commit: the one it ended at, or its branch's tip where the world lists one. */ export const headOf = (repo: Repo, pull: Pull) => - pull.head ?? - (pull.contributor ? {} : repo.branches)[pull.branch] ?? - sha1(`head ${repo.owner}/${repo.name}#${pull.number}`); + pull.head ?? (pull.contributor ? {} : repo.branches)[pull.branch] ?? sha1(`head ${repo.id}#${pull.number}`); // GitHub's database IDs differ from numbers; derive stable ones per repository. export const databaseId = (repo: Repo, number: number) => String(repo.id * 1000 + number); const updatedAt = "2026-10-20T12:00:00Z"; @@ -214,7 +236,7 @@ function pullTarget(repo: Repo, pull: Pull, after: unknown) { return { ...issueTarget(pull, after), isDraft: !!pull.draft, - baseRefName: "main", + baseRefName: pull.base ?? defaultOf(repo), headRefName: pull.branch, headRefOid: oid, reviews: connection( @@ -263,13 +285,13 @@ function reviewTarget(repo: Repo, pull: Pull) { for (const review of pull.reviews ?? []) if ( ["APPROVED", "CHANGES_REQUESTED", "DISMISSED"].includes(review.state) && - [...repo.maintainers, ...(repo.writers ?? [])].includes(review.author) + ["admin", "maintain", "write"].includes(roleOf(repo, review.author) ?? "") ) latest.set(review.author, review); return { state: pull.merged ? "MERGED" : pull.closed ? "CLOSED" : "OPEN", headRefOid: headOf(repo, pull), - baseRefName: pull.base ?? "main", + baseRefName: pull.base ?? defaultOf(repo), body: pull.body ?? "", author: actor(pull.author), userContentEdits: { nodes: pull.editor ? [{ editor: actor(pull.editor) }] : [] }, @@ -288,7 +310,7 @@ function holdsTarget(repo: Repo, pull: Pull) { return { id: nodeId(repo, pull), state: pull.merged ? "MERGED" : pull.closed ? "CLOSED" : "OPEN", - baseRefName: "main", + baseRefName: pull.base ?? defaultOf(repo), headRefOid: headOf(repo, pull), mergeStateStatus: mergeState(pull), isMergeQueueEnabled: false, @@ -339,6 +361,9 @@ function queueTarget(repo: Repo, pull: Pull) { }; } +/** Source-available licenses SPDX lists without OSI approval; the fake's SPDX approves every other. */ +const UNAPPROVED = new Set(["BUSL-1.1", "SSPL-1.0", "Elastic-2.0"]); + /** The fake's GitHub Actions app, which reports a run unless it names another. */ export const ACTIONS_APP = 15368; @@ -379,8 +404,8 @@ function graphql(repo: Repo, query: string, variables: Record) if (query.includes("signature {")) { const pull = repo.pulls.find((p) => p.number === variables.number); if (!pull) return { data: { repository: { pullRequest: null } } }; - const branch = repo.defaultBranch ?? "main"; - const anchor = repo.branches[branch] ?? commitOf(repo); + const branch = defaultOf(repo); + const anchor = tipOf(repo); const comparison = query.includes("compare(headRef:"); const base = comparison ? anchor : (repo.branches[pull.base ?? branch] ?? anchor); const head = comparison ? String(variables.revision) : headOf(repo, pull); @@ -453,15 +478,16 @@ function graphql(repo: Repo, query: string, variables: Record) }, }; } - if (query.includes("defaultBranchRef")) return { data: { repository: { defaultBranchRef: { name: "main" } } } }; + if (query.includes("defaultBranchRef")) + return { data: { repository: { defaultBranchRef: { name: defaultOf(repo) } } } }; if (query.includes("queue:pullRequests(")) { - const queued = repo.pulls.filter((pull) => !pull.closed && variables.base === "main"); + const queued = repo.pulls.filter((pull) => !pull.closed && variables.base === defaultOf(repo)); return { data: { repository: { ref: - variables.qualified === "refs/heads/main" - ? { target: { oid: repo.branches.main ?? commitOf(repo) } } + variables.qualified === `refs/heads/${defaultOf(repo)}` + ? { target: { oid: tipOf(repo) } } : null, queue: page(queued.map((pull) => queueTarget(repo, pull))), }, @@ -546,18 +572,15 @@ function connect(repo: Repo) { for (const tip of tips) repo.commits[tip] ??= { parents: [main], tree: treeOf(repo), message: "Work" }; } -/** A tree's `.github/workflows` files and their blob IDs: the default branch's files, then each written tree's changes. */ -function workflowsOf(repo: Repo, sha: string): Record { - const workflow = (path: string) => path.startsWith(".github/workflows/"); - if (sha === treeOf(repo)) - return Object.fromEntries(repo.files.filter((f) => workflow(f.path)).map((f) => [f.path, blob(f.content)])); +/** A tree's files and their blob IDs: `files` for the first commit's, then each written tree's changes. */ +function filesOf(repo: Repo, sha: string): Record { + if (sha === treeOf(repo)) return Object.fromEntries(repo.files.map((f) => [f.path, blob(f.content)])); const written = repo.trees[sha]; if (!written) return {}; - const files = workflowsOf(repo, written.base); + const files = filesOf(repo, written.base); for (const entry of written.entries as { path: string; sha?: string | null; content?: string }[]) - if (workflow(entry.path)) - if (entry.sha === null) delete files[entry.path]; - else files[entry.path] = entry.sha ?? blob(entry.content ?? ""); + if (entry.sha === null) delete files[entry.path]; + else files[entry.path] = entry.sha ?? blob(entry.content ?? ""); return files; } @@ -600,7 +623,7 @@ export function fakeGitHub(world: World) { draft: !!pull.draft, user: { login: pull.author }, created_at: new Date(pull.createdAt ?? Date.parse(updatedAt)).toISOString(), - base: { ref: "main" }, + base: { ref: pull.base ?? defaultOf(repo) }, head: { ref: pull.branch, sha: headOf(repo, pull), repo: headRepo(repo, pull) }, }); @@ -608,13 +631,15 @@ export function fakeGitHub(world: World) { id: repo.id, name: repo.name, owner: { login: repo.owner }, - private: false, - visibility: "public", - default_branch: repo.defaultBranch ?? "main", - archived: false, + private: !!repo.private, + visibility: repo.private ? "private" : "public", + default_branch: defaultOf(repo), + archived: !!repo.archived, disabled: false, - license: { spdx_id: "MIT" }, - allow_squash_merge: true, + license: repo.license === null ? null : { spdx_id: repo.license ?? "MIT" }, + allow_squash_merge: methodsOf(repo).includes("squash"), + allow_merge_commit: methodsOf(repo).includes("merge"), + allow_rebase_merge: methodsOf(repo).includes("rebase"), allow_auto_merge: true, }); @@ -637,8 +662,8 @@ export function fakeGitHub(world: World) { repositories: repos.map((repo) => ({ ...restRepo(repo), permissions: { - admin: false, - maintain: repo.maintainers.includes(me.login), + admin: roleOf(repo, me.login) === "admin", + maintain: ["admin", "maintain"].includes(roleOf(repo, me.login) ?? ""), push: true, pull: true, }, @@ -692,7 +717,11 @@ export function fakeGitHub(world: World) { const method = request.method; const path = url.pathname; const me = world.users[0]!; - if (url.origin === "https://spdx.org") return json({ licenseId: "MIT", isOsiApproved: true }); + const license = url.origin === "https://spdx.org" && path.match(/^\/licenses\/([^/]+)\.json$/)?.[1]; + if (license) { + const licenseId = decodeURIComponent(license); + return json({ licenseId, isOsiApproved: !UNAPPROVED.has(licenseId) }); + } if (url.origin === "https://github.com" && path === "/login/oauth/access_token") return json({ access_token: USER_TOKEN, token_type: "bearer", scope: "" }); if (url.origin !== "https://api.github.com") return missing(method, request.url); @@ -854,17 +883,11 @@ export function fakeGitHub(world: World) { const permission = tail.match(/^\/collaborators\/([^/]+)\/permission$/); if (permission) { const found = world.users.find((u) => u.login === permission[1]); - if (!found) return notFound(); - const role = repo.maintainers.includes(found.login) - ? "maintain" - : repo.writers?.includes(found.login) - ? "write" - : "read"; - return json({ - user: { id: found.id }, - permission: role === "read" ? "read" : "write", - role_name: role, - }); + const role = found && roleOf(repo, found.login); + if (!found || !role) return notFound(); + // The legacy permission is the base role's; a custom role's is read here. + const legacy = role === "admin" ? "admin" : ["maintain", "write"].includes(role) ? "write" : "read"; + return json({ user: { id: found.id }, permission: legacy, role_name: role }); } const listed = tail.match(/^\/commits\/([0-9a-f]{40})$/)?.[1]; if (listed && method === "GET") { @@ -875,12 +898,14 @@ export function fakeGitHub(world: World) { committer: found.committer ? { login: found.committer, type: actor(found.committer).__typename } : null, }); } - if (tail === "/commits/main") return json({ sha: commitOf(repo), commit: { tree: { sha: treeOf(repo) } } }); + const trunk = `/${encodeURIComponent(defaultOf(repo))}`; + if (tail === `/commits${trunk}`) + return json({ sha: tipOf(repo), commit: { tree: { sha: repo.commits[tipOf(repo)]!.tree } } }); // Read access sees whether classic protection is on and its required checks; the rest needs administration. - if (tail === "/branches/main" && method === "GET") { + if (tail === `/branches${trunk}` && method === "GET") { const checks = repo.protection?.required_status_checks; return json({ - name: "main", + name: defaultOf(repo), protection: repo.protection ? { enabled: true, @@ -893,7 +918,7 @@ export function fakeGitHub(world: World) { : { enabled: false }, }); } - if (tail === "/branches/main/protection" && method === "GET") { + if (tail === `/branches${trunk}/protection` && method === "GET") { if (!scope.permissions.administration) return forbidden(); return repo.protection ? json(repo.protection) : json({ message: "Branch not protected" }, 404); } @@ -923,7 +948,7 @@ export function fakeGitHub(world: World) { ); return json({ total_count: check_runs.length, check_runs }); } - if (tail.startsWith("/rules/branches/main") && method === "GET") { + if (tail === `/rules/branches${trunk}` && method === "GET") { const contexts = (repo.required ?? []).map((context) => ({ context })); return json([ ...(contexts.length @@ -973,7 +998,7 @@ export function fakeGitHub(world: World) { const [prefix, qualified] = String(body.head).split(":"); const owner = qualified === undefined ? repo.owner : prefix; const branch = qualified ?? prefix; - if (owner !== repo.owner || !branch || body.base !== "main" || !branches.get(branch)) + if (owner !== repo.owner || !branch || body.base !== defaultOf(repo) || !branches.get(branch)) return json({ message: "Validation Failed: head is invalid" }, 422); if (repo.pulls.some((pull) => !pull.closed && !pull.contributor && pull.branch === branch)) return json({ message: `A pull request already exists for ${body.head}.` }, 422); @@ -1025,12 +1050,16 @@ export function fakeGitHub(world: World) { const refused = pull.mergeRefusals?.shift(); if (refused) return json({ message: refused.message }, refused.status); if (body.sha !== headOf(repo, pull)) return json({ message: "Head branch was modified" }, 409); + const methods = { merge: "Merge commits", squash: "Squash merges", rebase: "Rebase merges" } as const; + const merging = (body.merge_method ?? "merge") as keyof typeof methods; + if (!methodsOf(repo).includes(merging)) + return json({ message: `${methods[merging]} are not allowed on this repository.` }, 405); if (pull.merge !== "CLEAN") return json({ message: "Pull Request is not mergeable" }, 405); pull.closed = true; pull.merged = true; pull.landing = false; pull.autoMerge = undefined; - if (pull.squash) repo.branches[pull.base ?? repo.defaultBranch ?? "main"] = pull.squash; + if (pull.squash) repo.branches[pull.base ?? defaultOf(repo)] = pull.squash; pull.mergeCommit = pull.squash ?? commitOf(repo); return json({ sha: pull.mergeCommit, @@ -1076,10 +1105,10 @@ export function fakeGitHub(world: World) { pull.labels = current.filter((label) => label !== name); return json(pull.labels.map((label) => ({ name: label }))); } - const compare = tail.match(/^\/compare\/([0-9a-f]{40})\.\.\.(main|[0-9a-f]{40})$/); + const compare = tail.match(/^\/compare\/([0-9a-f]{40})\.\.\.([^/]+)$/); if (compare && method === "GET") { const from = compare[1]!; - const base = compare[2] === "main" ? (repo.branches.main ?? commitOf(repo)) : compare[2]!; + const base = compare[2] === encodeURIComponent(defaultOf(repo)) ? tipOf(repo) : compare[2]!; if (!repo.commits[from] || !repo.commits[base]) return notFound(); const ancestors = (tip: string, seen = new Set()): Set => { if (seen.has(tip)) return seen; @@ -1103,14 +1132,20 @@ export function fakeGitHub(world: World) { } if (tail === "/contents/.github" && method === "GET") { const tree = repo.commits[url.searchParams.get("ref") ?? ""]?.tree; - const files = Object.entries(tree ? workflowsOf(repo, tree) : {}).sort(); + const files = Object.entries(tree ? filesOf(repo, tree) : {}) + .filter(([path]) => path.startsWith(".github/workflows/")) + .sort(); if (!files.length) return notFound(); const sha = sha1(`workflows ${JSON.stringify(files)}`); return json([{ name: "workflows", path: ".github/workflows", type: "dir", sha }]); } + // A file at a commit or branch, the default branch's by default. if (tail.startsWith("/contents/")) { - const file = repo.files.find((f) => f.path === decodeURIComponent(tail.slice("/contents/".length))); - return file ? json({ type: "file", path: file.path }) : notFound(); + const path = decodeURIComponent(tail.slice("/contents/".length)); + const ref = url.searchParams.get("ref") ?? defaultOf(repo); + const tree = repo.commits[repo.commits[ref] ? ref : (branches.get(ref) ?? "")]?.tree; + if (!tree) return json({ message: `No commit found for the ref ${ref}` }, 404); + return path in filesOf(repo, tree) ? json({ type: "file", path }) : notFound(); } return missing(method, request.url); }; From e06e1d094b3e80b564d7a3740eea0968370021a7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 2 Nov 2026 20:15:32 -0800 Subject: [PATCH 5/6] test(kix): run the project, inbox, jobs, and push tests against the shared GitHub fake `maintainerOf` answered GitHub with a hand-written mock and knobs. It now builds a fresh fake world per test that matches the observations it seeds, and tests state GitHub changes as world edits: roles, a closed issue or pull request, a moved head, a commit without a flake, reviews, labels and timeline, merge refusals, and allowed merge methods. Races and outages wrap the fake with `intercept` or a persistent failure. The project setup routes and the sign-in-again and link-account pages use the fake too. Webhook reads that used to fail on unmodeled requests now succeed, so the tests that relied on an unread webhook fail its read explicitly, and the world holds the issue a notified webhook names. Co-Authored-By: Claude Opus 5.5 --- cloudflare/test/inbox.test.ts | 95 ++++----- cloudflare/test/jobs.test.ts | 186 ++++++++--------- cloudflare/test/project-fixtures.ts | 296 ++++++++++------------------ cloudflare/test/projects.test.ts | 94 ++++++--- cloudflare/test/push.test.ts | 32 ++- 5 files changed, 332 insertions(+), 371 deletions(-) diff --git a/cloudflare/test/inbox.test.ts b/cloudflare/test/inbox.test.ts index 04cf7301..16fa865c 100644 --- a/cloudflare/test/inbox.test.ts +++ b/cloudflare/test/inbox.test.ts @@ -1,6 +1,7 @@ import { createExecutionContext, waitOnExecutionContext } from "cloudflare:test"; import { env } from "cloudflare:workers"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import type { Pull, Repo } from "../dev/github"; import { app } from "../src/index"; import { QUIET, cards } from "../src/inbox/activity"; import { questHref } from "../src/inbox/model"; @@ -8,8 +9,8 @@ import * as output from "../src/jobs/output"; import * as store from "../src/jobs/store"; import { INBOX_HEADER } from "../src/jobs/hub"; import { privateKey } from "./github-key"; -import { identity, oauth, origin } from "./login-fixtures"; -import { commit, delivery, head, maintainerOf, project, pull } from "./project-fixtures"; +import { origin } from "./login-fixtures"; +import { commit, delivery, failing, graphql, head, maintainerOf, project, pull, rewrite } from "./project-fixtures"; const testEnv = { ...env, GITHUB_APP_PRIVATE_KEY: privateKey }; const db = env.DB; @@ -17,11 +18,14 @@ const jobs = "/projects/github/upstream/project/jobs"; let user = ""; let cookie = ""; -let github: Awaited>["github"]; -let mock: Awaited>["mock"]; +let login = ""; +let repo: Repo; +let pr: Pull; +let github: typeof fetch; +let other: Awaited>["other"]; beforeEach(async () => { - ({ user, cookie, github, mock } = await maintainerOf()); + ({ user, cookie, login, repo, pr, github, other } = await maintainerOf()); }); afterEach(() => vi.restoreAllMocks()); const sockets: WebSocket[] = []; @@ -67,18 +71,12 @@ async function inbox(query: Record = {}, session = cookie) { expect(response.status).toBe(200); return response.text(); } -/** Sign in another GitHub user, who holds the maintain role when `maintains`. */ -async function other(maintains: boolean) { - const who = identity("github"); - const session = (await oauth(who)).session!; - vi.spyOn(globalThis, "fetch").mockImplementation(mock); - if (maintains) github.maintainers.push(who.id); - const { userId } = (await db - .prepare("SELECT userId FROM account WHERE providerId='github' AND accountId=?") - .bind(who.id) - .first<{ userId: string }>())!; - return { session, id: userId }; -} +/** The merges of #2 requested of GitHub. */ +const merges = () => + vi + .mocked(globalThis.fetch) + .mock.calls.filter(([input, init]) => init?.method === "PUT" && String(input).endsWith("/pulls/2/merge")) + .map(([, init]) => JSON.parse(String(init!.body))); /** Open the project's activity page, which checks maintainer authority. */ function visit(session = cookie) { return app.request(`${origin}/projects/github/upstream/project/state`, { headers: { Cookie: session } }, testEnv); @@ -166,6 +164,9 @@ describe("decision inbox", () => { it("coalesces webhook updates into the same item and marks it unread again", async () => { await inbox(); + // GitHub has the new issue, but cannot answer the reads these webhooks ask for. + repo.issues.push({ number: 3, title: "Crash", author: "reporter" }); + vi.mocked(globalThis.fetch).mockImplementation(failing(github, graphql, 502)); const event = { action: "synchronize", installation: { id: 5 }, @@ -178,6 +179,7 @@ describe("decision inbox", () => { expect((await delivery("pull_request", event)).status).toBe(202); const opened = { ...event, action: "opened", issue: { id: 13, number: 3 }, pull_request: undefined }; expect((await delivery("issues", opened)).status).toBe(202); + vi.mocked(globalThis.fetch).mockImplementation(github); const html = await inbox({ project: "10", item: "issues/1" }); expect(html.match(/>Fix