|
7 | 7 | fallbackShotR2Key, |
8 | 8 | fetchFallbackArtifactShots, |
9 | 9 | FALLBACK_ARTIFACT_NAME, |
| 10 | + hasInFlightFallbackDispatch, |
10 | 11 | isGithubArtifactStorageUrl, |
11 | 12 | parseFallbackRunCorrelation, |
12 | 13 | parseZipEntries, |
@@ -349,6 +350,82 @@ describe("dispatchVisualCaptureFallback", () => { |
349 | 350 | }); |
350 | 351 | }); |
351 | 352 |
|
| 353 | +describe("hasInFlightFallbackDispatch (#4112 review fix -- avoid cancel-in-progress re-dispatch)", () => { |
| 354 | + const HEAD_SHA = "cafebabecafebabecafebabecafebabecafebabe"; |
| 355 | + |
| 356 | + function runsResponse(runs: Array<{ status?: string; display_title?: string }>): Response { |
| 357 | + return Response.json({ workflow_runs: runs }); |
| 358 | + } |
| 359 | + |
| 360 | + it("true when a QUEUED run matches this exact pr+headSha", async () => { |
| 361 | + vi.stubGlobal("fetch", async () => runsResponse([{ status: "queued", display_title: `gittensory-visual-fallback pr=7 sha=${HEAD_SHA}` }])); |
| 362 | + const inFlight = await hasInFlightFallbackDispatch({ token: "tok", repo: { owner: "acme", repo: "widgets" }, prNumber: 7, headSha: HEAD_SHA }); |
| 363 | + expect(inFlight).toBe(true); |
| 364 | + }); |
| 365 | + |
| 366 | + it("true when an IN_PROGRESS run matches (case-insensitive headSha)", async () => { |
| 367 | + vi.stubGlobal("fetch", async () => runsResponse([{ status: "in_progress", display_title: `gittensory-visual-fallback pr=7 sha=${HEAD_SHA}` }])); |
| 368 | + const inFlight = await hasInFlightFallbackDispatch({ token: "tok", repo: { owner: "acme", repo: "widgets" }, prNumber: 7, headSha: HEAD_SHA.toUpperCase() }); |
| 369 | + expect(inFlight).toBe(true); |
| 370 | + }); |
| 371 | + |
| 372 | + it("false for an empty run list", async () => { |
| 373 | + vi.stubGlobal("fetch", async () => runsResponse([])); |
| 374 | + const inFlight = await hasInFlightFallbackDispatch({ token: "tok", repo: { owner: "acme", repo: "widgets" }, prNumber: 7, headSha: HEAD_SHA }); |
| 375 | + expect(inFlight).toBe(false); |
| 376 | + }); |
| 377 | + |
| 378 | + it("false when the matching run has already COMPLETED (not queued/in_progress)", async () => { |
| 379 | + vi.stubGlobal("fetch", async () => runsResponse([{ status: "completed", display_title: `gittensory-visual-fallback pr=7 sha=${HEAD_SHA}` }])); |
| 380 | + const inFlight = await hasInFlightFallbackDispatch({ token: "tok", repo: { owner: "acme", repo: "widgets" }, prNumber: 7, headSha: HEAD_SHA }); |
| 381 | + expect(inFlight).toBe(false); |
| 382 | + }); |
| 383 | + |
| 384 | + it("false when an in-progress run exists for a DIFFERENT PR", async () => { |
| 385 | + vi.stubGlobal("fetch", async () => runsResponse([{ status: "in_progress", display_title: `gittensory-visual-fallback pr=99 sha=${HEAD_SHA}` }])); |
| 386 | + const inFlight = await hasInFlightFallbackDispatch({ token: "tok", repo: { owner: "acme", repo: "widgets" }, prNumber: 7, headSha: HEAD_SHA }); |
| 387 | + expect(inFlight).toBe(false); |
| 388 | + }); |
| 389 | + |
| 390 | + it("false when an in-progress run exists for the same PR but a DIFFERENT headSha (new push)", async () => { |
| 391 | + vi.stubGlobal("fetch", async () => runsResponse([{ status: "in_progress", display_title: `gittensory-visual-fallback pr=7 sha=${"f".repeat(40)}` }])); |
| 392 | + const inFlight = await hasInFlightFallbackDispatch({ token: "tok", repo: { owner: "acme", repo: "widgets" }, prNumber: 7, headSha: HEAD_SHA }); |
| 393 | + expect(inFlight).toBe(false); |
| 394 | + }); |
| 395 | + |
| 396 | + it("false when the run's display_title doesn't match the expected correlation shape at all", async () => { |
| 397 | + vi.stubGlobal("fetch", async () => runsResponse([{ status: "in_progress", display_title: "Manually triggered run" }])); |
| 398 | + const inFlight = await hasInFlightFallbackDispatch({ token: "tok", repo: { owner: "acme", repo: "widgets" }, prNumber: 7, headSha: HEAD_SHA }); |
| 399 | + expect(inFlight).toBe(false); |
| 400 | + }); |
| 401 | + |
| 402 | + it("false on a non-ok response", async () => { |
| 403 | + vi.stubGlobal("fetch", async () => new Response("nope", { status: 500 })); |
| 404 | + const inFlight = await hasInFlightFallbackDispatch({ token: "tok", repo: { owner: "acme", repo: "widgets" }, prNumber: 7, headSha: HEAD_SHA }); |
| 405 | + expect(inFlight).toBe(false); |
| 406 | + }); |
| 407 | + |
| 408 | + it("false (never throws) on a network failure", async () => { |
| 409 | + vi.stubGlobal("fetch", async () => { |
| 410 | + throw new Error("network down"); |
| 411 | + }); |
| 412 | + const inFlight = await hasInFlightFallbackDispatch({ token: "tok", repo: { owner: "acme", repo: "widgets" }, prNumber: 7, headSha: HEAD_SHA }); |
| 413 | + expect(inFlight).toBe(false); |
| 414 | + }); |
| 415 | + |
| 416 | + it("true when a rateLimitAdmissionKey is supplied and a match is found", async () => { |
| 417 | + vi.stubGlobal("fetch", async () => runsResponse([{ status: "queued", display_title: `gittensory-visual-fallback pr=7 sha=${HEAD_SHA}` }])); |
| 418 | + const inFlight = await hasInFlightFallbackDispatch({ |
| 419 | + token: "tok", |
| 420 | + repo: { owner: "acme", repo: "widgets" }, |
| 421 | + prNumber: 7, |
| 422 | + headSha: HEAD_SHA, |
| 423 | + rateLimitAdmissionKey: "installation:1", |
| 424 | + }); |
| 425 | + expect(inFlight).toBe(true); |
| 426 | + }); |
| 427 | +}); |
| 428 | + |
352 | 429 | describe("fetchFallbackArtifactShots", () => { |
353 | 430 | function stubSequence(handlers: Array<(input: RequestInfo | URL, init?: RequestInit) => Response | Promise<Response>>): void { |
354 | 431 | let call = 0; |
|
0 commit comments