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 656d6892..cb7ba72f 100644 --- a/cloudflare/README.md +++ b/cloudflare/README.md @@ -388,7 +388,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: @@ -397,7 +398,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 @@ -407,6 +410,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 @@ -439,6 +444,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/dev/github.ts b/cloudflare/dev/github.ts index 2850f5d6..2fa8f408 100644 --- a/cloudflare/dev/github.ts +++ b/cloudflare/dev/github.ts @@ -85,8 +85,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[]; @@ -95,7 +106,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; /** @@ -109,9 +120,9 @@ export type Repo = { required?: string[]; /** The default branch has a merge queue, which takes a pull request only through `enqueuePullRequest` or auto-merge. */ queue?: boolean; - /** 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; /** Check runs the App wrote through `POST /check-runs`, oldest first. */ checkRuns?: { name: string; head_sha: string; conclusion: string; app: number; output: Record }[]; @@ -136,14 +147,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"; @@ -257,7 +279,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( @@ -306,13 +328,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) }] : [] }, @@ -331,7 +353,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: repo.queue === true, @@ -382,6 +404,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; @@ -424,8 +449,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); @@ -498,15 +523,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))), }, @@ -592,18 +618,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; } @@ -656,7 +679,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) }, }); @@ -664,13 +687,15 @@ export function fakeGitHub(world: World) { id: repo.id, name: repo.name, owner: { login: repo.owner, id: world.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, }); @@ -693,8 +718,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, }, @@ -748,7 +773,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); @@ -927,17 +956,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") { @@ -948,12 +971,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, @@ -966,7 +991,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); } @@ -1025,7 +1050,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 @@ -1075,7 +1100,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); @@ -1127,12 +1152,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 = pull.queued = 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, @@ -1178,10 +1207,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; @@ -1205,14 +1234,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); }; diff --git a/cloudflare/dev/seed.ts b/cloudflare/dev/seed.ts index 56d96c52..a91c3a3c 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, @@ -250,7 +252,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); @@ -272,9 +282,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]!; @@ -321,12 +332,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) => @@ -628,4 +640,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 876270c0..78157c9b 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" }, }, { @@ -123,10 +163,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 2a6331b3..148aaedf 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/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/cloudflare/test/inbox.test.ts b/cloudflare/test/inbox.test.ts index 67ce2e8b..33234a87 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,25 +71,19 @@ 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))); /** Sign pull request #2's head as the signed-in maintainer, so its jobs run on any runner. */ async function signed() { const { accountId } = (await db .prepare("SELECT accountId FROM account WHERE providerId='github' AND userId=?") .bind(user) .first<{ accountId: string }>())!; - github.signature = { isValid: true, signer: { databaseId: Number(accountId), login: `person${accountId}` } }; + repo.commits[head]!.signature = { isValid: true, signer: { databaseId: Number(accountId), login } }; } /** Open the project's activity page, which checks maintainer authority. */ function visit(session = cookie) { @@ -174,6 +172,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 }, @@ -186,6 +187,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