|
1 | 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { clearGitHubResponseCacheForTest, githubRateLimitAdmissionKeyForInstallation, latestGitHubRestRateLimitObservation } from "../../src/github/client"; |
3 | | -import { extractPreviewUrl, findPreviewUrlFromPrComments, getLatestDeploymentStatus, getPreviewBuildState } from "../../src/review/visual/preview-url"; |
| 3 | +import { extractPreviewUrl, findPreviewUrlFromChecks, findPreviewUrlFromPrComments, getLatestDeploymentStatus, getPreviewBuildState } from "../../src/review/visual/preview-url"; |
4 | 4 |
|
5 | 5 | /** GitHub's `Link` header for a page that advertises a next page (the exact shape findAcrossPages walks). */ |
6 | 6 | const NEXT_LINK = '<https://api.github.com/resource?per_page=100&page=99>; rel="next", <https://api.github.com/resource?per_page=100&page=99>; rel="last"'; |
@@ -52,6 +52,57 @@ describe("preview-url GitHub reads", () => { |
52 | 52 | }); |
53 | 53 | }); |
54 | 54 |
|
| 55 | +describe("findPreviewUrlFromChecks pagination (#7779)", () => { |
| 56 | + const isStatus = (input: RequestInfo | URL) => /\/status\b/.test(String(input)); |
| 57 | + const isCheckRuns = (input: RequestInfo | URL) => /\/check-runs\b/.test(String(input)); |
| 58 | + |
| 59 | + it("follows Link: rel=next and finds a preview check-run on page 2", async () => { |
| 60 | + // Page 1 carries only an unrelated check-run; the Cloudflare Workers Builds check-run (with the preview |
| 61 | + // link) is on page 2 -- the exact >100-check-runs case a page-1-only read used to miss. |
| 62 | + const fetchMock = vi.fn(async (input: RequestInfo | URL) => { |
| 63 | + if (isStatus(input)) return Response.json({ statuses: [] }); |
| 64 | + if (isCheckRuns(input) && isPage2(input)) { |
| 65 | + return Response.json({ check_runs: [{ status: "completed", conclusion: "success", details_url: "https://pr-1.app.workers.dev" }] }); |
| 66 | + } |
| 67 | + // page 1: an unrelated passing check with no preview link, advertising a next page |
| 68 | + return Response.json({ check_runs: [{ status: "completed", conclusion: "success", details_url: "https://ci.example.com/run/1" }] }, { headers: { link: NEXT_LINK } }); |
| 69 | + }); |
| 70 | + vi.stubGlobal("fetch", fetchMock); |
| 71 | + |
| 72 | + await expect(findPreviewUrlFromChecks({ token: "t", repo: REPO, sha: "abc123" })).resolves.toBe("https://pr-1.app.workers.dev"); |
| 73 | + // Proves it actually walked to page 2 (the whole point of the fix) rather than stopping at page 1. |
| 74 | + expect(fetchMock.mock.calls.some((c) => isCheckRuns(c[0] as RequestInfo | URL) && isPage2(c[0] as RequestInfo | URL))).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + it("returns null when every page advertises more but none carries check_runs (bounded walk)", async () => { |
| 78 | + // Payloads with no `check_runs` field exercise the empty-page fallback; the always-next Link can't loop |
| 79 | + // unboundedly (findAcrossPages caps the walk), so this resolves to null instead of hanging. |
| 80 | + const fetchMock = vi.fn(async (input: RequestInfo | URL) => { |
| 81 | + if (isStatus(input)) return Response.json({ statuses: [] }); |
| 82 | + return Response.json({}, { headers: { link: NEXT_LINK } }); |
| 83 | + }); |
| 84 | + vi.stubGlobal("fetch", fetchMock); |
| 85 | + |
| 86 | + await expect(findPreviewUrlFromChecks({ token: "t", repo: REPO, sha: "abc123" })).resolves.toBeNull(); |
| 87 | + }); |
| 88 | + |
| 89 | + it("skips a failed check-run and still finds the preview link on page 1 without an extra request", async () => { |
| 90 | + const fetchMock = vi.fn(async (input: RequestInfo | URL) => { |
| 91 | + if (isStatus(input)) return Response.json({ statuses: [] }); |
| 92 | + return Response.json({ |
| 93 | + check_runs: [ |
| 94 | + { status: "completed", conclusion: "failure", details_url: "https://pr-9.app.workers.dev" }, // failed → skipped, its URL must NOT win |
| 95 | + { status: "completed", conclusion: "success", details_url: "https://pr-real.app.workers.dev" }, |
| 96 | + ], |
| 97 | + }); |
| 98 | + }); |
| 99 | + vi.stubGlobal("fetch", fetchMock); |
| 100 | + |
| 101 | + await expect(findPreviewUrlFromChecks({ token: "t", repo: REPO, sha: "abc123" })).resolves.toBe("https://pr-real.app.workers.dev"); |
| 102 | + expect(fetchMock.mock.calls.some((c) => isPage2(c[0] as RequestInfo | URL))).toBe(false); |
| 103 | + }); |
| 104 | +}); |
| 105 | + |
55 | 106 | describe("preview-url pagination (#7450)", () => { |
56 | 107 | it("findPreviewUrlFromPrComments follows Link: rel=next and finds the bot comment on page 2", async () => { |
57 | 108 | const page1 = Array.from({ length: 100 }, (_v, i) => ({ user: { login: `user${i}` }, body: "just chatter" })); |
|
0 commit comments