test(mcp): make checkout/reviewComments tests offline (fixture-driven) (#575)

`mcp/checkout.test.ts` and `mcp/reviewComments.test.ts` previously hit
live GitHub on every run via `acquireNewToken()`, requiring `GH_TOKEN`
or `GITHUB_APP_ID` + `GITHUB_PRIVATE_KEY` in the env. that made them:

- cred-gated — the action runtime filters `_KEY$` / `_TOKEN$` from
  subprocess env, so the husky pre-push hook (which runs
  `pnpm -r test`) blocked Pullfrog agents from pushing branches. issues
  #562, #563, #564, #566 all hit this exact blocker and never got their
  fixes pushed.
- non-deterministic and slow (network round-trips for a snapshot test).

both tests are really snapshot tests of pure formatters
(`formatFilesWithLineNumbers`, plus `parseFilePatches` /
`buildThreadBlocks` / `formatReviewThreads` for review data). the live
fetches were just an inefficient way to obtain fixtures.

changes:

1. extract a pure `formatReviewData({ review, threads, prFiles, ... })`
   from `getReviewData` in `mcp/reviewComments.ts`. `getReviewData`
   becomes thin orchestration: fetch + call formatter. preserves the
   "skip listFiles when no threads" perf optimization.

2. add `action/mcp/__fixtures__/` with checked-in JSON captures for the
   three fixture test cases (pullfrog/test-repo#1 listFiles,
   pullfrog/scratch#49 review 3485940013, pullfrog/scratch#64 review
   3531000326). ~14KB total. fixtures store only the fields the
   formatter reads — volatile fields (sha, blob_url, etc.) are dropped.

3. rewrite both test files to load the fixtures and call the pure
   formatters directly. snapshot keys updated; snapshot content
   unchanged (verified by running existing snapshots against the
   refactored tests).

4. add `action/scripts/refresh-test-fixtures.ts` to re-fetch the
   fixtures from live GitHub on demand:
   `node action/scripts/refresh-test-fixtures.ts` (with creds in
   `.env` or env). re-run when the GitHub API response shape changes
   and review the snapshot diff.

trade-off: a silent change to GitHub's `pulls.listFiles` /
`pulls.getReview` / GraphQL `reviewThreads` response shape would no
longer break this test on every push. that tradeoff is worth it: shape
drift on those endpoints is rare (years between changes), and a
dedicated cron that runs the refresh script and opens a PR on diff is
a far better signal than a flaky cred-gated pre-push hook.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
David Blass
2026-05-05 23:25:46 +00:00
committed by pullfrog[bot]
parent b6e2c61d30
commit ada5584737
9 changed files with 506 additions and 119 deletions
@@ -0,0 +1,110 @@
{
"owner": "pullfrog",
"name": "scratch",
"pullNumber": 49,
"reviewId": 3485940013,
"review": {
"body": "### This is the final PR Bugbot will review for you during this billing cycle\n\nYour free Bugbot reviews will reset on November 30\n\n<details>\n<summary>Details</summary>\n\nYour team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.\n\nTo receive Bugbot reviews on all of your PRs, visit the [Cursor dashboard](https://www.cursor.com/dashboard?tab=bugbot) to activate Pro and start your 14-day free trial.\n</details>\n\n",
"user": {
"login": "cursor[bot]"
}
},
"threads": [
{
"id": "PRRT_kwDOPaxxp85iysVl",
"path": ".github/workflows/test.yml",
"line": null,
"startLine": null,
"diffSide": "RIGHT",
"isResolved": true,
"isOutdated": true,
"comments": {
"nodes": [
{
"fullDatabaseId": "2544544046",
"body": "### Bug: GitHub Actions workflow triggered for wrong branch\n\n<!-- **High Severity** -->\n\n<!-- DESCRIPTION START -->\nThe `pull_request` trigger specifies `branches: [mainc]`, but the `push` trigger specifies `branches: [main]`. This mismatch means pull requests will only trigger tests if targeting a non-existent `mainc` branch rather than the actual `main` development branch, preventing CI from running on most pull requests.\n<!-- DESCRIPTION END -->\n\n<!-- LOCATIONS START\n.github/workflows/test.yml#L6-L7\nLOCATIONS END -->\n<a href=\"https://cursor.com/open?data=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImJ1Z2JvdC12MSJ9.eyJ2ZXJzaW9uIjoxLCJ0eXBlIjoiQlVHQk9UX0ZJWF9JTl9DVVJTT1IiLCJkYXRhIjp7InJlZGlzS2V5IjoiYnVnYm90OjllMTgyY2U2LWY0YWMtNDAwNS1hMzQ4LWIyYzJkZTk4OGM1ZSIsImVuY3J5cHRpb25LZXkiOiJmSW93NEdsUGUwYlYtd3M2UC1UNHdHT1JmMGZjakxfWVZEdC00SWNveXo0IiwiYnJhbmNoIjoiZGl2aWRlIn0sImlhdCI6MTc2MzYyMDgxOSwiZXhwIjoxNzY0MjI1NjE5fQ.BjkWsTqiNriojI5v10JcveUY2M50f9eflTNDgWAdjdW9w7E0EEY4GJfyzBrA72neco3qAlc34WipASNuEQbTD1fZvwtJY-TeNTDzoKmwA6gtwICB8t7qT87GPvcbDrdGGWdC8kW1jf-LntTmD0k7gt0AeENRAdRSiD3dbqYFN0huXHaB8f2Y48mpmLcnnUpoaaZe7By-Y0DnILyHppwx3AH75nKE_ZeAee3rQNGX4cwcHgB5emTSM93pMDQhT1vbIRYHMaFkOaW2-kDOA8H2QqxD4mT8VzY3skvxIo5HNZCvqE84NtEygHqkBv88g2EEijOPAAeskfsdp087yIzV9g\"><picture><source media=\"(prefers-color-scheme: dark)\" srcset=\"https://cursor.com/fix-in-cursor-dark.svg\"><source media=\"(prefers-color-scheme: light)\" srcset=\"https://cursor.com/fix-in-cursor-light.svg\"><img alt=\"Fix in Cursor\" src=\"https://cursor.com/fix-in-cursor.svg\"></picture></a>&nbsp;<a href=\"https://cursor.com/agents?data=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImJ1Z2JvdC12MSJ9.eyJ2ZXJzaW9uIjoxLCJ0eXBlIjoiQlVHQk9UX0ZJWF9JTl9XRUIiLCJkYXRhIjp7InJlZGlzS2V5IjoiYnVnYm90OjllMTgyY2U2LWY0YWMtNDAwNS1hMzQ4LWIyYzJkZTk4OGM1ZSIsImVuY3J5cHRpb25LZXkiOiJmSW93NEdsUGUwYlYtd3M2UC1UNHdHT1JmMGZjakxfWVZEdC00SWNveXo0IiwiYnJhbmNoIjoiZGl2aWRlIiwicmVwb093bmVyIjoicHVsbGZyb2dhaSIsInJlcG9OYW1lIjoic2NyYXRjaCIsInByTnVtYmVyIjo0OSwiY29tbWl0U2hhIjoiNThiOGJmNmQ1MWE1Mjg4OGFjNGFkNzA5YWVmYTk2MWFkZDMyNDBiMSJ9LCJpYXQiOjE3NjM2MjA4MTksImV4cCI6MTc2NDIyNTYxOX0.SFDZe8R9uwhPjS55J4i_mV2ybsSZoQYM6YzdUOava4IKy1IK2OrVkVsG3-8p4rRaMBXdDZZ4ObPbtk70KqdAiLEDKBqaFcWqELc49lr0XRKUmu4F6EhESFQOvt7MLSVDIOgee8YRlhS6xtoPDqsRiV2KGOwyLEdCeYdrYz9i1DanIswWSoMRVvkjxZ6GUBYVAUg_JsgAXoKVJ-L9Q5Ygho6acVAr5NlGeBp2f6g49GX4GfDOPeV3SORQS1CjxQVRbjI-g0rW55NIisBEl8279VwG6-dTISNbyasZOB6R3eEmC4vmyAAGJjUsMwqhMPw1oaMMmYNSbtZLDESxME9IUg\"><picture><source media=\"(prefers-color-scheme: dark)\" srcset=\"https://cursor.com/fix-in-web-dark.svg\"><source media=\"(prefers-color-scheme: light)\" srcset=\"https://cursor.com/fix-in-web-light.svg\"><img alt=\"Fix in Web\" src=\"https://cursor.com/fix-in-web.svg\"></picture></a>\n\n",
"createdAt": "2025-11-20T06:40:19Z",
"diffHunk": "@@ -0,0 +1,36 @@\n+name: Test\n+\n+on:\n+ push:\n+ branches: [main]\n+ pull_request:\n+ branches: [mainc]",
"line": null,
"startLine": null,
"originalLine": 7,
"originalStartLine": null,
"author": {
"login": "cursor"
},
"pullRequestReview": {
"databaseId": 3485940013,
"author": {
"login": "cursor"
}
},
"reactionGroups": [
{
"content": "THUMBS_UP",
"reactors": {
"nodes": []
}
},
{
"content": "THUMBS_DOWN",
"reactors": {
"nodes": []
}
},
{
"content": "LAUGH",
"reactors": {
"nodes": []
}
},
{
"content": "HOORAY",
"reactors": {
"nodes": []
}
},
{
"content": "CONFUSED",
"reactors": {
"nodes": []
}
},
{
"content": "HEART",
"reactors": {
"nodes": []
}
},
{
"content": "ROCKET",
"reactors": {
"nodes": []
}
},
{
"content": "EYES",
"reactors": {
"nodes": []
}
}
]
}
]
}
}
],
"prFiles": [
{
"filename": ".github/workflows/test.yml",
"patch": "@@ -0,0 +1,36 @@\n+name: Test\n+\n+on:\n+ push:\n+ branches: [main]\n+ pull_request:\n+ branches: [main]\n+\n+jobs:\n+ test:\n+ runs-on: ubuntu-latest\n+\n+ strategy:\n+ matrix:\n+ node-version: [22.x]\n+\n+ steps:\n+ - name: Checkout code\n+ uses: actions/checkout@v4\n+\n+ - name: Setup pnpm\n+ uses: pnpm/action-setup@v2\n+ with:\n+ version: 8\n+\n+ - name: Setup Node.js ${{ matrix.node-version }}\n+ uses: actions/setup-node@v4\n+ with:\n+ node-version: ${{ matrix.node-version }}\n+ cache: 'pnpm'\n+\n+ - name: Install dependencies\n+ run: pnpm install\n+\n+ - name: Run tests\n+ run: pnpm test"
},
{
"filename": "index.test.ts",
"patch": "@@ -1,5 +1,5 @@\n import { describe, it, expect } from 'vitest'\n-import { add } from './index.js'\n+import { add, multiply, subtract, divide } from './index.js'\n \n describe('add function', () => {\n it('should add two positive numbers correctly', () => {\n@@ -25,3 +25,51 @@ describe('add function', () => {\n expect(add(0.1, 0.2)).toBeCloseTo(0.3)\n })\n })\n+\n+describe('multiply function', () => {\n+ it('should multiply two positive numbers correctly', () => {\n+ expect(multiply(3, 4)).toBe(12)\n+ })\n+\n+ it('should multiply negative numbers correctly', () => {\n+ expect(multiply(-2, 3)).toBe(-6)\n+ expect(multiply(-2, -3)).toBe(6)\n+ })\n+\n+ it('should handle zero correctly', () => {\n+ expect(multiply(5, 0)).toBe(0)\n+ expect(multiply(0, 5)).toBe(0)\n+ })\n+})\n+\n+describe('subtract function', () => {\n+ it('should subtract two positive numbers correctly', () => {\n+ expect(subtract(10, 3)).toBe(7)\n+ })\n+\n+ it('should handle negative numbers correctly', () => {\n+ expect(subtract(5, -3)).toBe(8)\n+ expect(subtract(-5, 3)).toBe(-8)\n+ })\n+\n+ it('should handle zero correctly', () => {\n+ expect(subtract(5, 0)).toBe(5)\n+ expect(subtract(0, 5)).toBe(-5)\n+ })\n+})\n+\n+describe('divide function', () => {\n+ it('should divide two positive numbers correctly', () => {\n+ expect(divide(10, 2)).toBe(5)\n+ })\n+\n+ it('should handle negative numbers correctly', () => {\n+ expect(divide(-10, 2)).toBe(-5)\n+ expect(divide(10, -2)).toBe(-5)\n+ })\n+\n+ it('should handle decimal results correctly', () => {\n+ expect(divide(10, 3)).toBeCloseTo(3.333, 2)\n+ expect(divide(7, 2)).toBe(3.5)\n+ })\n+})"
},
{
"filename": "index.ts",
"patch": "@@ -3,11 +3,13 @@ export function add(a: number, b: number) {\n }\n \n export function multiply(a: number, b: number) {\n- // Bug: accidentally adding 1 to the result\n- return a * b + 1;\n+ return a * b;\n }\n \n export function subtract(a: number, b: number) {\n- // Bug: accidentally adding instead of subtracting\n- return a + b;\n+ return a - b;\n+}\n+\n+export function divide(a: number, b: number) {\n+ return a / b;\n }"
}
]
}
@@ -0,0 +1,14 @@
{
"owner": "pullfrog",
"name": "scratch",
"pullNumber": 64,
"reviewId": 3531000326,
"review": {
"body": "This PR looks great. The retry logic is well-implemented and the tests are comprehensive.",
"user": {
"login": "pullfrog[bot]"
}
},
"threads": [],
"prFiles": []
}
@@ -0,0 +1,67 @@
{
"owner": "pullfrog",
"name": "test-repo",
"pullNumber": 1,
"files": [
{
"sha": "a2d9c355792f1883c26d43d219db006b05781e4c",
"filename": "src/format.ts",
"status": "modified",
"additions": 12,
"deletions": 2,
"changes": 14,
"blob_url": "https://github.com/pullfrog/test-repo/blob/0311c0fb58fc7faa46e51c174394a4468f379681/src%2Fformat.ts",
"raw_url": "https://github.com/pullfrog/test-repo/raw/0311c0fb58fc7faa46e51c174394a4468f379681/src%2Fformat.ts",
"contents_url": "https://api.github.com/repos/pullfrog/test-repo/contents/src%2Fformat.ts?ref=0311c0fb58fc7faa46e51c174394a4468f379681",
"patch": "@@ -1,7 +1,17 @@\n-export function formatCurrency(amount: number) {\n- return `$${amount.toFixed(2)}`;\n+export function formatCurrency(amount: number, currency = \"USD\") {\n+ return new Intl.NumberFormat(\"en-US\", {\n+ style: \"currency\",\n+ currency,\n+ }).format(amount);\n }\n \n export function formatPercent(value: number) {\n return `${(value * 100).toFixed(1)}%`;\n }\n+\n+export function formatNumber(value: number, decimals = 2) {\n+ return new Intl.NumberFormat(\"en-US\", {\n+ minimumFractionDigits: decimals,\n+ maximumFractionDigits: decimals,\n+ }).format(value);\n+}"
},
{
"sha": "0786b9ce6870e65c644673745266e87eef057ce4",
"filename": "src/math.ts",
"status": "modified",
"additions": 5,
"deletions": 2,
"changes": 7,
"blob_url": "https://github.com/pullfrog/test-repo/blob/0311c0fb58fc7faa46e51c174394a4468f379681/src%2Fmath.ts",
"raw_url": "https://github.com/pullfrog/test-repo/raw/0311c0fb58fc7faa46e51c174394a4468f379681/src%2Fmath.ts",
"contents_url": "https://api.github.com/repos/pullfrog/test-repo/contents/src%2Fmath.ts?ref=0311c0fb58fc7faa46e51c174394a4468f379681",
"patch": "@@ -3,13 +3,16 @@ export function add(a: number, b: number) {\n }\n \n export function subtract(a: number, b: number) {\n- return a + b; // bug: should be a - b\n+ return a - b;\n }\n \n export function multiply(a: number, b: number) {\n- return a * b + 1; // bug: off by one\n+ return a * b;\n }\n \n export function divide(a: number, b: number) {\n+ if (b === 0) {\n+ throw new Error(\"division by zero\");\n+ }\n return a / b;\n }"
},
{
"sha": "cf92d8f6562c1be779506fec1049f38c9206c869",
"filename": "src/old-module.ts",
"status": "removed",
"additions": 0,
"deletions": 4,
"changes": 4,
"blob_url": "https://github.com/pullfrog/test-repo/blob/91ef1048326ef786fbcf95f29b3e2555506d2d54/src%2Fold-module.ts",
"raw_url": "https://github.com/pullfrog/test-repo/raw/91ef1048326ef786fbcf95f29b3e2555506d2d54/src%2Fold-module.ts",
"contents_url": "https://api.github.com/repos/pullfrog/test-repo/contents/src%2Fold-module.ts?ref=91ef1048326ef786fbcf95f29b3e2555506d2d54",
"patch": "@@ -1,4 +0,0 @@\n-// this module is deprecated and will be removed\n-export function legacyHelper() {\n- return \"old\";\n-}"
},
{
"sha": "a5bfb8a1be72e4f0816a5c4c83ee784a06559629",
"filename": "src/validate.ts",
"status": "added",
"additions": 11,
"deletions": 0,
"changes": 11,
"blob_url": "https://github.com/pullfrog/test-repo/blob/0311c0fb58fc7faa46e51c174394a4468f379681/src%2Fvalidate.ts",
"raw_url": "https://github.com/pullfrog/test-repo/raw/0311c0fb58fc7faa46e51c174394a4468f379681/src%2Fvalidate.ts",
"contents_url": "https://api.github.com/repos/pullfrog/test-repo/contents/src%2Fvalidate.ts?ref=0311c0fb58fc7faa46e51c174394a4468f379681",
"patch": "@@ -0,0 +1,11 @@\n+export function isPositive(n: number) {\n+ return n > 0;\n+}\n+\n+export function isInRange(value: number, min: number, max: number) {\n+ return value >= min && value <= max;\n+}\n+\n+export function isInteger(n: number) {\n+ return Number.isInteger(n);\n+}"
},
{
"sha": "5815895211d8e3355fdb77b9e216e73a248644d9",
"filename": "test/math.test.ts",
"status": "modified",
"additions": 4,
"deletions": 0,
"changes": 4,
"blob_url": "https://github.com/pullfrog/test-repo/blob/0311c0fb58fc7faa46e51c174394a4468f379681/test%2Fmath.test.ts",
"raw_url": "https://github.com/pullfrog/test-repo/raw/0311c0fb58fc7faa46e51c174394a4468f379681/test%2Fmath.test.ts",
"contents_url": "https://api.github.com/repos/pullfrog/test-repo/contents/test%2Fmath.test.ts?ref=0311c0fb58fc7faa46e51c174394a4468f379681",
"patch": "@@ -17,4 +17,8 @@ describe(\"math\", () => {\n it(\"divides\", () => {\n expect(divide(10, 2)).toBe(5);\n });\n+\n+ it(\"throws on division by zero\", () => {\n+ expect(() => divide(1, 0)).toThrow(\"division by zero\");\n+ });\n });"
}
]
}
+2 -2
View File
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`fetchAndFormatPrDiff > generates accurate TOC line numbers for pullfrog/test-repo#1 > content 1`] = ` exports[`formatFilesWithLineNumbers > generates accurate TOC line numbers for pullfrog/test-repo#1 > content 1`] = `
"## Files (5) "## Files (5)
- src/format.ts → lines 9-32 · diff-41c7b3ac268a3a1ae5c7be92f1230f600013b7170e44a693570ccbdb183ea36b - src/format.ts → lines 9-32 · diff-41c7b3ac268a3a1ae5c7be92f1230f600013b7170e44a693570ccbdb183ea36b
- src/math.ts → lines 33-55 · diff-9c6e445a719b33e276684bdf95c69e617f0303638d44cf90d61295f2720ecc63 - src/math.ts → lines 33-55 · diff-9c6e445a719b33e276684bdf95c69e617f0303638d44cf90d61295f2720ecc63
@@ -96,7 +96,7 @@ diff --git a/test/math.test.ts b/test/math.test.ts
" "
`; `;
exports[`fetchAndFormatPrDiff > generates accurate TOC line numbers for pullfrog/test-repo#1 > toc 1`] = ` exports[`formatFilesWithLineNumbers > generates accurate TOC line numbers for pullfrog/test-repo#1 > toc 1`] = `
"## Files (5) "## Files (5)
- src/format.ts → lines 9-32 · diff-41c7b3ac268a3a1ae5c7be92f1230f600013b7170e44a693570ccbdb183ea36b - src/format.ts → lines 9-32 · diff-41c7b3ac268a3a1ae5c7be92f1230f600013b7170e44a693570ccbdb183ea36b
- src/math.ts → lines 33-55 · diff-9c6e445a719b33e276684bdf95c69e617f0303638d44cf90d61295f2720ecc63 - src/math.ts → lines 33-55 · diff-9c6e445a719b33e276684bdf95c69e617f0303638d44cf90d61295f2720ecc63
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`getFormattedReviewThreads > formats body-only review > content 1`] = ` exports[`formatReviewData > formats body-only review > content 1`] = `
"# Review Threads (0) for PR #64 - Review 3531000326 by pullfrog[bot] "# Review Threads (0) for PR #64 - Review 3531000326 by pullfrog[bot]
## Review Body ## Review Body
@@ -11,9 +11,9 @@ This PR looks great. The retry logic is well-implemented and the tests are compr
" "
`; `;
exports[`getFormattedReviewThreads > formats body-only review > toc 1`] = `""`; exports[`formatReviewData > formats body-only review > toc 1`] = `""`;
exports[`getFormattedReviewThreads > formats thread blocks with TOC and correct line numbers > content 1`] = ` exports[`formatReviewData > formats thread blocks with TOC and correct line numbers > content 1`] = `
"# Review Threads (1) for PR #49 - Review 3485940013 by cursor[bot] "# Review Threads (1) for PR #49 - Review 3485940013 by cursor[bot]
## TOC ## TOC
@@ -68,4 +68,4 @@ LOCATIONS END -->
" "
`; `;
exports[`getFormattedReviewThreads > formats thread blocks with TOC and correct line numbers > toc 1`] = `"- .github/workflows/test.yml:7 → lines 25-52"`; exports[`formatReviewData > formats thread blocks with TOC and correct line numbers > toc 1`] = `"- .github/workflows/test.yml:7 → lines 25-52"`;
+42 -52
View File
@@ -1,8 +1,7 @@
import type { RestEndpointMethodTypes } from "@octokit/rest"; import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
import { acquireNewToken, createOctokit } from "../utils/github.ts"; import { type FormatFilesResult, formatFilesWithLineNumbers } from "./checkout.ts";
import { fetchAndFormatPrDiff } from "./checkout.ts";
import type { ToolContext } from "./server.ts";
/** /**
* parses TOC entries like "- src/math.ts → lines 7-42 · diff-<hex>" into structured data. * parses TOC entries like "- src/math.ts → lines 7-42 · diff-<hex>" into structured data.
@@ -22,59 +21,50 @@ function parseTocEntries(toc: string) {
return entries; return entries;
} }
async function getToken(): Promise<string> { // fixture captured by action/scripts/refresh-test-fixtures.ts. running
// prefer explicit GH_TOKEN, fall back to acquiring one via GitHub App credentials // the formatter against checked-in JSON keeps this test offline and
if (process.env.GH_TOKEN) return process.env.GH_TOKEN; // deterministic — re-fetch the fixture (with creds) when GitHub's
return await acquireNewToken(); // pulls.listFiles response shape changes, then review the snapshot diff.
type DiffFixture = {
owner: string;
name: string;
pullNumber: number;
files: Parameters<typeof formatFilesWithLineNumbers>[0];
};
function loadFixture<T>(file: string): T {
return JSON.parse(readFileSync(resolve(import.meta.dirname, "__fixtures__", file), "utf-8")) as T;
} }
describe("fetchAndFormatPrDiff", () => { describe("formatFilesWithLineNumbers", () => {
it( it("generates accurate TOC line numbers for pullfrog/test-repo#1", () => {
"generates accurate TOC line numbers for pullfrog/test-repo#1", const fx = loadFixture<DiffFixture>("pullfrog-test-repo-pr-1.diff.json");
{ timeout: 30000 }, const result: FormatFilesResult = formatFilesWithLineNumbers(fx.files);
async () => {
const token = await getToken();
const octokit = createOctokit(token);
const ctx = {
octokit,
repo: {
owner: "pullfrog",
name: "test-repo",
data: {} as RestEndpointMethodTypes["repos"]["get"]["response"]["data"],
},
} as ToolContext;
const result = await fetchAndFormatPrDiff(ctx, 1);
// verify content includes TOC at the start expect(result.content.startsWith(result.toc)).toBe(true);
expect(result.content.startsWith(result.toc)).toBe(true);
// parse TOC and validate every entry's line numbers against actual content const contentLines = result.content.split("\n");
const contentLines = result.content.split("\n"); const tocEntries = parseTocEntries(result.toc);
const tocEntries = parseTocEntries(result.toc); expect(tocEntries.length).toBeGreaterThan(0);
expect(tocEntries.length).toBeGreaterThan(0);
for (const entry of tocEntries) { for (const entry of tocEntries) {
// line numbers are 1-indexed, arrays are 0-indexed // line numbers are 1-indexed, arrays are 0-indexed
const firstLine = contentLines[entry.startLine - 1]; const firstLine = contentLines[entry.startLine - 1];
expect(firstLine).toBeDefined(); expect(firstLine).toBeDefined();
// first line of each file section should be the diff header // first line of each file section should be the diff header
expect(firstLine).toBe(`diff --git a/${entry.filename} b/${entry.filename}`); expect(firstLine).toBe(`diff --git a/${entry.filename} b/${entry.filename}`);
// endLine should be within bounds expect(entry.endLine).toBeLessThanOrEqual(contentLines.length);
expect(entry.endLine).toBeLessThanOrEqual(contentLines.length);
}
// verify adjacent files don't overlap and are contiguous
for (let i = 1; i < tocEntries.length; i++) {
const prev = tocEntries[i - 1];
const curr = tocEntries[i];
// current file starts right after previous file ends
expect(curr.startLine).toBe(prev.endLine + 1);
}
// snapshot the full output for regression detection
expect(result.toc).toMatchSnapshot("toc");
expect(result.content).toMatchSnapshot("content");
} }
);
// verify adjacent files don't overlap and are contiguous
for (let i = 1; i < tocEntries.length; i++) {
const prev = tocEntries[i - 1];
const curr = tocEntries[i];
expect(curr.startLine).toBe(prev.endLine + 1);
}
expect(result.toc).toMatchSnapshot("toc");
expect(result.content).toMatchSnapshot("content");
});
}); });
+30 -33
View File
@@ -1,43 +1,40 @@
import { Octokit } from "@octokit/rest"; import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
import { acquireNewToken } from "../utils/github.ts"; import { type FormatReviewDataInput, formatReviewData } from "./reviewComments.ts";
import { getReviewData } from "./reviewComments.ts";
async function getToken(): Promise<string> { // fixtures captured by action/scripts/refresh-test-fixtures.ts; re-run
if (process.env.GH_TOKEN) return process.env.GH_TOKEN; // (with creds) when GitHub's review/threads/listFiles response shape
return await acquireNewToken(); // changes, then review the snapshot diff.
type ReviewFixture = FormatReviewDataInput & {
owner: string;
name: string;
};
function loadFixture(file: string): ReviewFixture {
return JSON.parse(
readFileSync(resolve(import.meta.dirname, "__fixtures__", file), "utf-8")
) as ReviewFixture;
} }
describe("getFormattedReviewThreads", () => { describe("formatReviewData", () => {
it("formats thread blocks with TOC and correct line numbers", { timeout: 30000 }, async () => { it("formats thread blocks with TOC and correct line numbers", () => {
const token = await getToken(); const fx = loadFixture("pullfrog-scratch-pr-49-review-3485940013.json");
const octokit = new Octokit({ auth: token }); const result = formatReviewData(fx);
expect(result).toBeDefined();
if (!result) return;
const { formatted } = (await getReviewData({ expect(result.formatted.toc).toMatchSnapshot("toc");
octokit, expect(result.formatted.content).toMatchSnapshot("content");
owner: "pullfrog",
name: "scratch",
pullNumber: 49,
reviewId: 3485940013,
}))!;
expect(formatted.toc).toMatchSnapshot("toc");
expect(formatted.content).toMatchSnapshot("content");
}); });
it("formats body-only review", { timeout: 30000 }, async () => { it("formats body-only review", () => {
const token = await getToken(); const fx = loadFixture("pullfrog-scratch-pr-64-review-3531000326.json");
const octokit = new Octokit({ auth: token }); const result = formatReviewData(fx);
expect(result).toBeDefined();
if (!result) return;
const { formatted } = (await getReviewData({ expect(result.formatted.toc).toMatchSnapshot("toc");
octokit, expect(result.formatted.content).toMatchSnapshot("content");
owner: "pullfrog",
name: "scratch",
pullNumber: 64,
reviewId: 3531000326,
}))!;
expect(formatted.toc).toMatchSnapshot("toc");
expect(formatted.content).toMatchSnapshot("content");
}); });
}); });
+76 -28
View File
@@ -497,6 +497,67 @@ interface GetReviewDataInput {
approvedBy?: string | undefined; approvedBy?: string | undefined;
} }
// pure formatter: takes already-fetched GitHub responses and produces the
// review data the MCP tool returns. extracted from getReviewData so tests
// can drive it from checked-in fixtures without live API access.
//
// `prFiles` may be empty when `threads` is empty — callers that hit the
// network should skip the listFiles call in that case as a perf
// optimization. when both are empty and `review.body` is also empty, the
// formatter returns undefined just like getReviewData.
export interface FormatReviewDataInput {
review: ReviewResponse;
threads: ReviewThread[];
prFiles: ReviewPrFile[];
pullNumber: number;
reviewId: number;
}
export type ReviewResponse = {
body: string | null | undefined;
user: { login: string } | null | undefined;
};
export type ReviewPrFile = {
filename: string;
patch?: string | undefined;
};
export function formatReviewData(input: FormatReviewDataInput):
| {
threadBlocks: Array<{ path: string; lineRange: string; content: string[] }>;
reviewer: string;
formatted: { toc: string; content: string };
}
| undefined {
const rawReviewBody = input.review.body;
const reviewBody = rawReviewBody ? stripExistingFooter(rawReviewBody) : "";
const reviewer = input.review.user?.login ?? "unknown";
if (input.threads.length === 0 && !reviewBody) return undefined;
let threadBlocks: Array<{ path: string; lineRange: string; content: string[] }> = [];
if (input.threads.length > 0) {
const filePatchMap = new Map<string, ParsedHunk[]>();
for (const file of input.prFiles) {
if (file.patch) {
filePatchMap.set(file.filename, parseFilePatches(file.patch));
}
}
threadBlocks = buildThreadBlocks(input.threads, filePatchMap, input.reviewId);
}
const formatted = formatReviewThreads(threadBlocks, {
pullNumber: input.pullNumber,
reviewId: input.reviewId,
reviewer,
reviewBody,
});
return { threadBlocks, reviewer, formatted };
}
export async function getReviewData(input: GetReviewDataInput): Promise< export async function getReviewData(input: GetReviewDataInput): Promise<
| { | {
threadBlocks: Array<{ path: string; lineRange: string; content: string[] }>; threadBlocks: Array<{ path: string; lineRange: string; content: string[] }>;
@@ -515,38 +576,25 @@ export async function getReviewData(input: GetReviewDataInput): Promise<
getReviewThreads(input), getReviewThreads(input),
]); ]);
const rawReviewBody = review.data.body; // skip listFiles when there are no threads — prFiles is only used for
const reviewBody = rawReviewBody ? stripExistingFooter(rawReviewBody) : ""; // building thread blocks, and an empty array short-circuits below.
const reviewer = review.data.user?.login ?? "unknown"; const prFiles =
threads.length > 0
? await input.octokit.paginate(input.octokit.rest.pulls.listFiles, {
owner: input.owner,
repo: input.name,
pull_number: input.pullNumber,
per_page: 100,
})
: [];
if (threads.length === 0 && !reviewBody) return undefined; return formatReviewData({
review: review.data,
let threadBlocks: Array<{ path: string; lineRange: string; content: string[] }> = []; threads,
prFiles,
if (threads.length > 0) {
const prFiles = await input.octokit.paginate(input.octokit.rest.pulls.listFiles, {
owner: input.owner,
repo: input.name,
pull_number: input.pullNumber,
per_page: 100,
});
const filePatchMap = new Map<string, ParsedHunk[]>();
for (const file of prFiles) {
if (file.patch) {
filePatchMap.set(file.filename, parseFilePatches(file.patch));
}
}
threadBlocks = buildThreadBlocks(threads, filePatchMap, input.reviewId);
}
const formatted = formatReviewThreads(threadBlocks, {
pullNumber: input.pullNumber, pullNumber: input.pullNumber,
reviewId: input.reviewId, reviewId: input.reviewId,
reviewer,
reviewBody,
}); });
return { threadBlocks, reviewer, formatted };
} }
export function GetReviewCommentsTool(ctx: ToolContext) { export function GetReviewCommentsTool(ctx: ToolContext) {
+161
View File
@@ -0,0 +1,161 @@
#!/usr/bin/env node
/**
* refresh checked-in test fixtures for mcp/checkout.test.ts and
* mcp/reviewComments.test.ts.
*
* those tests used to hit live GitHub on every run, which made them
* cred-gated (GH_TOKEN or GITHUB_APP_ID + GITHUB_PRIVATE_KEY) and
* non-deterministic. they now read from action/mcp/__fixtures__/*.json,
* which this script regenerates on demand.
*
* run with creds set (locally via .env, or in a CI cron with secrets):
*
* GH_TOKEN=… node action/scripts/refresh-test-fixtures.ts
* # or
* GITHUB_APP_ID=… GITHUB_PRIVATE_KEY=… node action/scripts/refresh-test-fixtures.ts
*
* commit the resulting fixture changes; review the diff before merging
* (anything unexpected indicates real GitHub API drift).
*/
import { mkdirSync, writeFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { Octokit } from "@octokit/rest";
import { config as loadDotenv } from "dotenv";
import {
REVIEW_THREADS_QUERY,
type ReviewThread,
type ReviewThreadsQueryResponse,
} from "../mcp/reviewComments.ts";
import { acquireNewToken } from "../utils/github.ts";
const scriptDir = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(scriptDir, "../..");
const fixturesDir = resolve(scriptDir, "../mcp/__fixtures__");
loadDotenv({ path: resolve(repoRoot, ".env") });
type DiffFixture = {
owner: string;
name: string;
pullNumber: number;
files: unknown;
};
type ReviewFixture = {
owner: string;
name: string;
pullNumber: number;
reviewId: number;
review: { body: string | null | undefined; user: { login: string } | null | undefined };
threads: ReviewThread[];
prFiles: Array<{ filename: string; patch?: string | undefined }>;
};
const DIFF_TARGETS: Array<Pick<DiffFixture, "owner" | "name" | "pullNumber">> = [
{ owner: "pullfrog", name: "test-repo", pullNumber: 1 },
];
const REVIEW_TARGETS: Array<Pick<ReviewFixture, "owner" | "name" | "pullNumber" | "reviewId">> = [
{ owner: "pullfrog", name: "scratch", pullNumber: 49, reviewId: 3485940013 },
{ owner: "pullfrog", name: "scratch", pullNumber: 64, reviewId: 3531000326 },
];
async function getToken(): Promise<string> {
if (process.env.GH_TOKEN) return process.env.GH_TOKEN;
return await acquireNewToken();
}
async function refreshDiffFixture(
octokit: Octokit,
target: (typeof DIFF_TARGETS)[number]
): Promise<void> {
const files = await octokit.paginate(octokit.rest.pulls.listFiles, {
owner: target.owner,
repo: target.name,
pull_number: target.pullNumber,
per_page: 100,
});
const fixture: DiffFixture = { ...target, files };
const path = resolve(
fixturesDir,
`${target.owner}-${target.name}-pr-${target.pullNumber}.diff.json`
);
writeFileSync(path, `${JSON.stringify(fixture, null, 2)}\n`);
console.log(`wrote ${path}`);
}
async function refreshReviewFixture(
octokit: Octokit,
target: (typeof REVIEW_TARGETS)[number]
): Promise<void> {
const [review, threadsResp] = await Promise.all([
octokit.rest.pulls.getReview({
owner: target.owner,
repo: target.name,
pull_number: target.pullNumber,
review_id: target.reviewId,
}),
octokit.graphql<ReviewThreadsQueryResponse>(REVIEW_THREADS_QUERY, {
owner: target.owner,
name: target.name,
prNumber: target.pullNumber,
}),
]);
const allThreads = threadsResp.repository?.pullRequest?.reviewThreads?.nodes ?? [];
const threads = allThreads.filter((thread): thread is ReviewThread => {
if (!thread?.comments?.nodes) return false;
return thread.comments.nodes.some((c) => c?.pullRequestReview?.databaseId === target.reviewId);
});
// skip listFiles entirely when there are no threads — prFiles is only
// used for thread blocks, so an empty array short-circuits in the
// formatter. mirrors getReviewData's runtime perf optimization and
// keeps body-only-review fixtures small.
const prFiles =
threads.length > 0
? await octokit.paginate(octokit.rest.pulls.listFiles, {
owner: target.owner,
repo: target.name,
pull_number: target.pullNumber,
per_page: 100,
})
: [];
// strip prFiles down to the fields the formatter actually reads. keeps
// fixtures small and avoids capturing volatile fields (sha, blob_url,
// contents_url, etc.) that would churn unrelated to formatter behavior.
const trimmedFiles = prFiles.map((f) => ({
filename: f.filename,
...(f.patch ? { patch: f.patch } : {}),
}));
const fixture: ReviewFixture = {
...target,
review: {
body: review.data.body,
user: review.data.user ? { login: review.data.user.login } : null,
},
threads,
prFiles: trimmedFiles,
};
const path = resolve(
fixturesDir,
`${target.owner}-${target.name}-pr-${target.pullNumber}-review-${target.reviewId}.json`
);
writeFileSync(path, `${JSON.stringify(fixture, null, 2)}\n`);
console.log(`wrote ${path}`);
}
async function main(): Promise<void> {
const token = await getToken();
const octokit = new Octokit({ auth: token });
mkdirSync(fixturesDir, { recursive: true });
for (const t of DIFF_TARGETS) await refreshDiffFixture(octokit, t);
for (const t of REVIEW_TARGETS) await refreshReviewFixture(octokit, t);
}
await main();