95 lines · 5.2 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { mock, test } from "node:test"; |
| 3 | import { decisionIdFor } from "@refs/protocol"; |
| 4 | import { admitAgentPush } from "../decisions.ts"; |
| 5 | import type { ReceiveCommands } from "../git.ts"; |
| 6 | import type { RepoRecord } from "../repos.ts"; |
| 7 | import { d1, migrated } from "./d1.ts"; |
| 8 | |
| 9 | const base = "b".repeat(40); |
| 10 | const head = "1".repeat(40); |
| 11 | const zero = "0".repeat(40); |
| 12 | const branch = "refs/heads/tasks/7"; |
| 13 | |
| 14 | function pkt(line: string): string { |
| 15 | return (line.length + 4).toString(16).padStart(4, "0") + line; |
| 16 | } |
| 17 | |
| 18 | type Commit = { hash: string; parents: string[]; message: string }; |
| 19 | |
| 20 | function firstParents(history: ReadonlyMap<string, Commit>, ref: string): Commit[] { |
| 21 | const seen: Commit[] = []; |
| 22 | for (let commit = history.get(ref); commit; commit = history.get(commit.parents[0])) seen.push(commit); |
| 23 | return seen; |
| 24 | } |
| 25 | |
| 26 | function setup(message: string, refuseBranch: string | null = null, history = new Map<string, Commit>([[head, { hash: head, parents: [base], message }], [base, { hash: base, parents: [], message: "Start" }]]), update = { from: base, to: head }) { |
| 27 | const requests: string[] = []; |
| 28 | mock.method(globalThis, "fetch", async (_url: string, init: { body: ReadableStream<Uint8Array> }) => { |
| 29 | const body = Buffer.from(await new Response(init.body).arrayBuffer()).toString("latin1"); |
| 30 | requests.push(body); |
| 31 | const refs = [...body.matchAll(/[0-9a-f]{40} [0-9a-f]{40} (refs\/[^\s\0]+)/g)].map((match) => match[1]); |
| 32 | return new Response(pkt("unpack ok\n") + refs.map((ref) => pkt(ref === branch && refuseBranch ? `ng ${ref} ${refuseBranch}\n` : `ok ${ref}\n`)).join("") + "0000"); |
| 33 | }); |
| 34 | const handle = { |
| 35 | info: async () => ({ remote: "https://artifacts.test/refs-art" }), |
| 36 | createToken: async () => ({ plaintext: "t" }), |
| 37 | log: async ({ ref }: { ref: string }) => firstParents(history, ref), |
| 38 | [Symbol.dispose]() {}, |
| 39 | }; |
| 40 | const env = { DB: d1(migrated()), ARTIFACTS: { get: async () => handle } } as unknown as Env; |
| 41 | const repo = { id: "repo", artifacts_name: "refs-art", default_branch: "main" } as RepoRecord; |
| 42 | const commands: ReceiveCommands = { updates: [{ ref: branch, ...update }], capabilities: ["report-status", "side-band-64k", "agent=git/2"], length: 10 }; |
| 43 | const body = new Response("COMMANDS..PACKDATA").body!; |
| 44 | return { requests, admit: () => admitAgentPush(env, repo, commands, body) }; |
| 45 | } |
| 46 | |
| 47 | const commandsOf = (request: string) => [...request.matchAll(/([0-9a-f]{40}) ([0-9a-f]{40}) (refs\/[^\s\0]+)/g)].map((match) => `${match[1].slice(0, 1)} ${match[2].slice(0, 1)} ${match[3].replace(/[0-9a-f-]{36}$/, "<id>")}`); |
| 48 | |
| 49 | test("a record that does not say what it costs to reverse is refused, and the task branch never moves", async () => { |
| 50 | const { requests, admit } = setup("Queue pushes\n\n```decision\ndecision: Queue pushes.\nreason:\n - waitUntil drops work\n```\n"); |
| 51 | const { response, landed } = await admit(); |
| 52 | const text = await response.text(); |
| 53 | assert.equal(landed, false); |
| 54 | assert.match(text, /ng refs\/heads\/tasks\/7 1111111 "Queue pushes\.": to_reverse is required/); |
| 55 | assert.match(text, /\x02Fix the last commit with git commit --amend, then push again\.\n/); |
| 56 | assert.deepEqual(requests.map(commandsOf), [["0 1 refs/quarantine/<id>"], ["1 0 refs/quarantine/<id>"]]); |
| 57 | assert.ok(requests[0].endsWith("PACKDATA")); |
| 58 | assert.doesNotMatch(requests[0], /side-band/); |
| 59 | }); |
| 60 | |
| 61 | test("a commit with a record, or with none, lands and its ids are reported", async () => { |
| 62 | const { requests, admit } = setup("Queue pushes\n\n```decision\ndecision: Queue pushes.\nreason:\n - waitUntil drops work\nto_reverse: Push handling moves to a new service.\n```\n"); |
| 63 | const { response, landed } = await admit(); |
| 64 | const text = await response.text(); |
| 65 | assert.equal(landed, true); |
| 66 | assert.match(text, /ok refs\/heads\/tasks\/7\n/); |
| 67 | assert.ok(text.includes(`${decisionIdFor(head, 0)} 1111111 Queue pushes.`)); |
| 68 | assert.deepEqual(requests.map(commandsOf), [["0 1 refs/quarantine/<id>"], ["b 1 refs/heads/tasks/7", "1 0 refs/quarantine/<id>"]]); |
| 69 | |
| 70 | const none = setup("Fix typo\n"); |
| 71 | assert.equal((await none.admit()).landed, true); |
| 72 | }); |
| 73 | |
| 74 | test("a push after the task branch was merged checks only its new commits, not the default branch's history", async () => { |
| 75 | const root = "a".repeat(40); |
| 76 | const merge = "c".repeat(40); |
| 77 | const next = "d".repeat(40); |
| 78 | const history = new Map<string, Commit>([ |
| 79 | [root, { hash: root, parents: [], message: "Initial commit\n" }], |
| 80 | [base, { hash: base, parents: [root], message: "Earlier task work\n\nDecision: none\n" }], |
| 81 | [merge, { hash: merge, parents: [root, base], message: "Merge pull request #1 from tasks/7\n" }], |
| 82 | [next, { hash: next, parents: [merge], message: "More task work\n\nDecision: none\n" }], |
| 83 | ]); |
| 84 | const { admit } = setup("", null, history, { from: base, to: next }); |
| 85 | const { response, landed } = await admit(); |
| 86 | assert.equal(landed, true, await response.text()); |
| 87 | }); |
| 88 | |
| 89 | test("a branch that moved during the check refuses the push and still releases the quarantine", async () => { |
| 90 | const { requests, admit } = setup("Fix typo\n\nDecision: none\n", "stale ref"); |
| 91 | const { response, landed } = await admit(); |
| 92 | assert.equal(landed, false); |
| 93 | assert.match(await response.text(), /ng refs\/heads\/tasks\/7 fetch first/); |
| 94 | assert.equal(requests.length, 2); |
| 95 | }); |