|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | + |
| 7 | +const tempDirs: string[] = []; |
| 8 | + |
| 9 | +function createFakeGh(): string { |
| 10 | + const dir = mkdtempSync(join(tmpdir(), "openclaw-pr-metadata-")); |
| 11 | + const gh = join(dir, "gh"); |
| 12 | + tempDirs.push(dir); |
| 13 | + writeFileSync( |
| 14 | + gh, |
| 15 | + `#!/usr/bin/env bash |
| 16 | +set -euo pipefail |
| 17 | +
|
| 18 | +if [ "$1" = "pr" ] && [ "$2" = "view" ]; then |
| 19 | + if [[ "$*" == *changedFiles* ]]; then |
| 20 | + printf '{"number":42,"url":"https://example.test/pr/42","headRefOid":"head-a","changedFiles":%s}\n' "\${FAKE_CHANGED_FILES:-101}" |
| 21 | + else |
| 22 | + printf '{"headRefOid":"%s"}\n' "\${FAKE_HEAD_AFTER:-head-a}" |
| 23 | + fi |
| 24 | + exit 0 |
| 25 | +fi |
| 26 | +
|
| 27 | +if [ "$1" = "api" ] && [ "$2" = "--paginate" ]; then |
| 28 | + [ "$3" = 'repos/{owner}/{repo}/pulls/42/files?per_page=100' ] || { echo "unexpected endpoint: $3" >&2; exit 4; } |
| 29 | + jq -nc '[range(0; 100) | {filename: ("src/file-" + (tostring) + ".ts"), status: "modified", additions: 1, deletions: 0}]' |
| 30 | + jq -nc '[{filename: "src/file-100.ts", status: "removed", additions: 0, deletions: 1}]' |
| 31 | + exit 0 |
| 32 | +fi |
| 33 | +
|
| 34 | +echo "unexpected gh command: $*" >&2 |
| 35 | +exit 2 |
| 36 | +`, |
| 37 | + ); |
| 38 | + chmodSync(gh, 0o755); |
| 39 | + return dir; |
| 40 | +} |
| 41 | + |
| 42 | +function readPrMetadata( |
| 43 | + fakeGhDir: string, |
| 44 | + options: { changedFiles?: string; headAfter?: string } = {}, |
| 45 | +) { |
| 46 | + return spawnSync( |
| 47 | + "bash", |
| 48 | + ["-c", "set -euo pipefail; source scripts/pr-lib/worktree.sh; pr_meta_json 42"], |
| 49 | + { |
| 50 | + cwd: process.cwd(), |
| 51 | + env: { |
| 52 | + ...process.env, |
| 53 | + FAKE_CHANGED_FILES: options.changedFiles ?? "101", |
| 54 | + FAKE_HEAD_AFTER: options.headAfter ?? "head-a", |
| 55 | + PATH: `${fakeGhDir}:${process.env.PATH}`, |
| 56 | + }, |
| 57 | + encoding: "utf8", |
| 58 | + }, |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +afterEach(() => { |
| 63 | + for (const dir of tempDirs.splice(0)) { |
| 64 | + rmSync(dir, { recursive: true, force: true }); |
| 65 | + } |
| 66 | +}); |
| 67 | + |
| 68 | +describe("PR metadata", () => { |
| 69 | + it("paginates all changed files and preserves the GraphQL file shape", () => { |
| 70 | + const result = readPrMetadata(createFakeGh()); |
| 71 | + |
| 72 | + expect(result.status).toBe(0); |
| 73 | + expect(result.stderr).toBe(""); |
| 74 | + const metadata = JSON.parse(result.stdout) as { |
| 75 | + changedFiles: number; |
| 76 | + files: Array<{ |
| 77 | + path: string; |
| 78 | + additions: number; |
| 79 | + deletions: number; |
| 80 | + changeType: string; |
| 81 | + }>; |
| 82 | + }; |
| 83 | + expect(metadata.changedFiles).toBe(101); |
| 84 | + expect(metadata.files).toHaveLength(101); |
| 85 | + expect(metadata.files[0]).toEqual({ |
| 86 | + path: "src/file-0.ts", |
| 87 | + additions: 1, |
| 88 | + deletions: 0, |
| 89 | + changeType: "MODIFIED", |
| 90 | + }); |
| 91 | + expect(metadata.files[100]).toEqual({ |
| 92 | + path: "src/file-100.ts", |
| 93 | + additions: 0, |
| 94 | + deletions: 1, |
| 95 | + changeType: "DELETED", |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it("rejects incomplete paginated file metadata", () => { |
| 100 | + const result = readPrMetadata(createFakeGh(), { changedFiles: "102" }); |
| 101 | + |
| 102 | + expect(result.status).toBe(1); |
| 103 | + expect(result.stderr).toContain( |
| 104 | + "Incomplete PR file metadata for #42: expected 102 changed files, received 101 from paginated REST.", |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + it("rejects files collected while the PR head changes", () => { |
| 109 | + const result = readPrMetadata(createFakeGh(), { headAfter: "head-b" }); |
| 110 | + |
| 111 | + expect(result.status).toBe(1); |
| 112 | + expect(result.stderr).toContain( |
| 113 | + "PR head changed while collecting file metadata for #42 (started at head-a, ended at head-b). Retry review initialization.", |
| 114 | + ); |
| 115 | + }); |
| 116 | +}); |
0 commit comments