|
1 | 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { clearGitHubResponseCacheForTest, githubRateLimitAdmissionKeyForInstallation, latestGitHubRestRateLimitObservation } from "../../src/github/client"; |
3 | | -import { extractPreviewUrl, findPreviewUrlFromPrComments, getPreviewBuildState } from "../../src/review/visual/preview-url"; |
| 3 | +import { extractPreviewUrl, 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"'; |
@@ -166,6 +166,190 @@ describe("preview-url pagination (#7450)", () => { |
166 | 166 | await expect(getPreviewBuildState({ token: "t", repo: REPO, sha: "fail" })).resolves.toBe("absent"); |
167 | 167 | expect(failLater).toHaveBeenCalledTimes(2); |
168 | 168 | }); |
| 169 | + |
| 170 | + it("getLatestDeploymentStatus finds a deployment whose environment_url sits on page 2 of the deployments list (#7805)", async () => { |
| 171 | + // Page 1: 100 deployments none of which carry a usable status; page 2: the deployment with the real URL. |
| 172 | + const fetchMock = vi.fn(async (input: RequestInfo | URL) => { |
| 173 | + const url = String(input); |
| 174 | + if (url.includes("/deployments?")) { |
| 175 | + if (isPage2(input)) return Response.json([{ id: 777 }]); |
| 176 | + return Response.json( |
| 177 | + Array.from({ length: 100 }, (_v, i) => ({ id: i + 1 })), |
| 178 | + { headers: { link: NEXT_LINK } }, |
| 179 | + ); |
| 180 | + } |
| 181 | + // Deployment 777's statuses carry the preview URL; every page-1 deployment's statuses are empty. |
| 182 | + if (url.includes("/deployments/777/statuses")) { |
| 183 | + return Response.json([{ state: "success", environment_url: "https://p2.pages.dev/" }]); |
| 184 | + } |
| 185 | + return Response.json([]); |
| 186 | + }); |
| 187 | + vi.stubGlobal("fetch", fetchMock); |
| 188 | + await expect(getLatestDeploymentStatus({ token: "t", repo: REPO, sha: "abc" })).resolves.toEqual({ |
| 189 | + url: "https://p2.pages.dev/", |
| 190 | + failed: false, |
| 191 | + }); |
| 192 | + // Page 1 + page 2 of deployments were both fetched (the bug was that page 2 was never read). |
| 193 | + expect(fetchMock.mock.calls.some((c) => isPage2(c[0]))).toBe(true); |
| 194 | + }); |
| 195 | + |
| 196 | + it("getLatestDeploymentStatus finds a usable status on page 2 of a deployment's statuses (#7805)", async () => { |
| 197 | + const fetchMock = vi.fn(async (input: RequestInfo | URL) => { |
| 198 | + const url = String(input); |
| 199 | + if (url.includes("/deployments?")) return Response.json([{ id: 42 }]); |
| 200 | + if (url.includes("/deployments/42/statuses")) { |
| 201 | + if (isPage2(input)) return Response.json([{ state: "success", environment_url: "https://s2.workers.dev/" }]); |
| 202 | + // Page 1: 100 older statuses, none usable, advertising a next page. |
| 203 | + return Response.json( |
| 204 | + Array.from({ length: 100 }, () => ({ state: "queued" })), |
| 205 | + { headers: { link: NEXT_LINK } }, |
| 206 | + ); |
| 207 | + } |
| 208 | + return Response.json([]); |
| 209 | + }); |
| 210 | + vi.stubGlobal("fetch", fetchMock); |
| 211 | + await expect(getLatestDeploymentStatus({ token: "t", repo: REPO, sha: "abc" })).resolves.toEqual({ |
| 212 | + url: "https://s2.workers.dev/", |
| 213 | + failed: false, |
| 214 | + }); |
| 215 | + expect(fetchMock.mock.calls.some((c) => String(c[0]).includes("/statuses") && isPage2(c[0]))).toBe(true); |
| 216 | + }); |
| 217 | + |
| 218 | + it("getLatestDeploymentStatus reports failed when a deployment's latest status is a failure and none are pending (#7805)", async () => { |
| 219 | + // No usable environment_url anywhere, and the single deployment's latest (statuses[0]) is a hard failure: |
| 220 | + // sawFailure is set, sawPending stays false, so the terminal verdict is failed:true. |
| 221 | + vi.stubGlobal("fetch", async (input: RequestInfo | URL) => { |
| 222 | + const url = String(input); |
| 223 | + if (url.includes("/deployments?")) return Response.json([{ id: 5 }]); |
| 224 | + if (url.includes("/deployments/5/statuses")) return Response.json([{ state: "failure" }, { state: "success" }]); |
| 225 | + return Response.json([]); |
| 226 | + }); |
| 227 | + await expect(getLatestDeploymentStatus({ token: "t", repo: REPO, sha: "abc" })).resolves.toEqual({ |
| 228 | + url: null, |
| 229 | + failed: true, |
| 230 | + }); |
| 231 | + }); |
| 232 | + |
| 233 | + it("getLatestDeploymentStatus is NOT failed when another deployment is still pending, even if one failed (#7805)", async () => { |
| 234 | + // Two deployments: one failed, one still in_progress. sawPending being set suppresses the failed verdict, so |
| 235 | + // the caller keeps polling rather than declaring a false terminal failure. |
| 236 | + vi.stubGlobal("fetch", async (input: RequestInfo | URL) => { |
| 237 | + const url = String(input); |
| 238 | + if (url.includes("/deployments?")) return Response.json([{ id: 1 }, { id: 2 }]); |
| 239 | + if (url.includes("/deployments/1/statuses")) return Response.json([{ state: "error" }]); |
| 240 | + if (url.includes("/deployments/2/statuses")) return Response.json([{ state: "in_progress" }]); |
| 241 | + return Response.json([]); |
| 242 | + }); |
| 243 | + await expect(getLatestDeploymentStatus({ token: "t", repo: REPO, sha: "abc" })).resolves.toEqual({ |
| 244 | + url: null, |
| 245 | + failed: false, |
| 246 | + }); |
| 247 | + }); |
| 248 | + |
| 249 | + it("getLatestDeploymentStatus swallows a per-deployment statuses-fetch error and treats that deployment as URL-less (#7805)", async () => { |
| 250 | + // A statuses read throwing must not abort the whole lookup: findDeploymentUrl's .catch degrades that one |
| 251 | + // deployment to null, so the overall result is a clean "no URL yet, not failed". |
| 252 | + vi.stubGlobal("fetch", async (input: RequestInfo | URL) => { |
| 253 | + const url = String(input); |
| 254 | + if (url.includes("/deployments?")) return Response.json([{ id: 9 }]); |
| 255 | + if (url.includes("/deployments/9/statuses")) throw new Error("statuses network down"); |
| 256 | + return Response.json([]); |
| 257 | + }); |
| 258 | + await expect(getLatestDeploymentStatus({ token: "t", repo: REPO, sha: "abc" })).resolves.toEqual({ |
| 259 | + url: null, |
| 260 | + failed: false, |
| 261 | + }); |
| 262 | + }); |
| 263 | + |
| 264 | + it("getLatestDeploymentStatus skips a deployment whose id is absent, leaving no statuses to inspect (#7805)", async () => { |
| 265 | + // The id filter drops an id-less deployment entry, so there is nothing to fetch statuses for -> no URL, not |
| 266 | + // failed. (A statuses fetch for a real id is never issued here.) |
| 267 | + vi.stubGlobal("fetch", async (input: RequestInfo | URL) => { |
| 268 | + const url = String(input); |
| 269 | + if (url.includes("/deployments?")) return Response.json([{ notAnId: true }]); |
| 270 | + return Response.json([]); |
| 271 | + }); |
| 272 | + await expect(getLatestDeploymentStatus({ token: "t", repo: REPO, sha: "abc" })).resolves.toEqual({ |
| 273 | + url: null, |
| 274 | + failed: false, |
| 275 | + }); |
| 276 | + }); |
| 277 | + |
| 278 | + it("getLatestDeploymentStatus treats a non-array deployments payload as an empty page (#7805)", async () => { |
| 279 | + // Array.isArray(payload) ? ... : [] -- the deployments list read returning a non-array object degrades to an |
| 280 | + // empty page rather than throwing. |
| 281 | + vi.stubGlobal("fetch", async (input: RequestInfo | URL) => { |
| 282 | + const url = String(input); |
| 283 | + if (url.includes("/deployments?")) return Response.json({ message: "unexpected non-array deployments shape" }); |
| 284 | + return Response.json([]); |
| 285 | + }); |
| 286 | + await expect(getLatestDeploymentStatus({ token: "t", repo: REPO, sha: "abc" })).resolves.toEqual({ |
| 287 | + url: null, |
| 288 | + failed: false, |
| 289 | + }); |
| 290 | + }); |
| 291 | + |
| 292 | + it("getLatestDeploymentStatus treats a non-array statuses payload for a deployment as empty (#7805)", async () => { |
| 293 | + // The inner Array.isArray(payload) ? ... : [] guard: a deployment's /statuses returning a non-array object is |
| 294 | + // treated as "no statuses", so that deployment contributes no URL and no failed/pending signal. |
| 295 | + vi.stubGlobal("fetch", async (input: RequestInfo | URL) => { |
| 296 | + const url = String(input); |
| 297 | + if (url.includes("/deployments?")) return Response.json([{ id: 3 }]); |
| 298 | + if (url.includes("/deployments/3/statuses")) return Response.json({ message: "unexpected non-array statuses shape" }); |
| 299 | + return Response.json([]); |
| 300 | + }); |
| 301 | + await expect(getLatestDeploymentStatus({ token: "t", repo: REPO, sha: "abc" })).resolves.toEqual({ |
| 302 | + url: null, |
| 303 | + failed: false, |
| 304 | + }); |
| 305 | + }); |
| 306 | + |
| 307 | + it("getLatestDeploymentStatus builds a ref-scoped deployments query when given a ref instead of a sha (#7805)", async () => { |
| 308 | + // The selector ternary's ref arm: with params.ref (and no sha) the deployments list is queried by ref=..., and |
| 309 | + // a usable status still resolves to its environment_url. |
| 310 | + const fetchMock = vi.fn(async (input: RequestInfo | URL) => { |
| 311 | + const url = String(input); |
| 312 | + if (url.includes("/deployments?")) return Response.json([{ id: 8 }]); |
| 313 | + if (url.includes("/deployments/8/statuses")) return Response.json([{ state: "success", environment_url: "https://ref.pages.dev/" }]); |
| 314 | + return Response.json([]); |
| 315 | + }); |
| 316 | + vi.stubGlobal("fetch", fetchMock); |
| 317 | + await expect(getLatestDeploymentStatus({ token: "t", repo: REPO, ref: "feature-branch" })).resolves.toEqual({ |
| 318 | + url: "https://ref.pages.dev/", |
| 319 | + failed: false, |
| 320 | + }); |
| 321 | + // The deployments query was scoped by ref=..., not sha=... |
| 322 | + expect(fetchMock.mock.calls.some((c) => String(c[0]).includes("/deployments?ref=feature-branch"))).toBe(true); |
| 323 | + }); |
| 324 | + |
| 325 | + it("getLatestDeploymentStatus returns error:true when the deployments read fails with a non-404 status (#7805)", async () => { |
| 326 | + // A 403/5xx (not a 404 "genuinely no deployments") must surface error:true so the caller keeps polling rather |
| 327 | + // than showing a false terminal "no preview" state. |
| 328 | + vi.stubGlobal("fetch", async (input: RequestInfo | URL) => { |
| 329 | + const url = String(input); |
| 330 | + if (url.includes("/deployments?")) return new Response("forbidden", { status: 403 }); |
| 331 | + return Response.json([]); |
| 332 | + }); |
| 333 | + await expect(getLatestDeploymentStatus({ token: "t", repo: REPO, sha: "abc" })).resolves.toEqual({ |
| 334 | + url: null, |
| 335 | + failed: false, |
| 336 | + error: true, |
| 337 | + }); |
| 338 | + }); |
| 339 | + |
| 340 | + it("getLatestDeploymentStatus returns no-preview (not error) when the deployments read 404s (#7805)", async () => { |
| 341 | + // A 404 means the ref genuinely has no deployments -> a clean {url:null, failed:false}, distinct from the |
| 342 | + // non-404 error path above. |
| 343 | + vi.stubGlobal("fetch", async (input: RequestInfo | URL) => { |
| 344 | + const url = String(input); |
| 345 | + if (url.includes("/deployments?")) return new Response("not found", { status: 404 }); |
| 346 | + return Response.json([]); |
| 347 | + }); |
| 348 | + await expect(getLatestDeploymentStatus({ token: "t", repo: REPO, sha: "abc" })).resolves.toEqual({ |
| 349 | + url: null, |
| 350 | + failed: false, |
| 351 | + }); |
| 352 | + }); |
169 | 353 | }); |
170 | 354 |
|
171 | 355 | describe("extractPreviewUrl", () => { |
|
0 commit comments