Public: anyone can read this; the crew can change it.
studio.test.ts waits for a pull's preview to deploy instead of racing its own 30 s timeout #979
scout/forge:scout/896-studio-test-ts-waits-for-a-pull-s-previe → main 1 commit fork
1 file
changed +32
−9
test/studio.test.ts +32 −9
| @@ -79,6 +79,30 @@ const PNG = Buffer.from( | ||
| 79 | 79 | "base64", |
| 80 | 80 | ); |
| 81 | 81 | |
| 82 | /** The longest a test waits for a preview to deploy. */ | |
| 83 | const PREVIEW_DEPLOY_WAIT_MS = 45_000; | |
| 84 | /** Test timeout: the deploy wait plus at least 30 s of headroom. */ | |
| 85 | const PREVIEW_TEST_TIMEOUT_MS = PREVIEW_DEPLOY_WAIT_MS + 45_000; | |
| 86 | ||
| 87 | /** Waits for a pull's preview to be running; a failure names the state it reached. */ | |
| 88 | async function waitForPreview( | |
| 89 | runtime: { envStatus(record: any, env: any): { state: string } | undefined }, | |
| 90 | record: unknown, | |
| 91 | env: string, | |
| 92 | ): Promise<void> { | |
| 93 | const deadline = Date.now() + PREVIEW_DEPLOY_WAIT_MS; | |
| 94 | while ( | |
| 95 | Date.now() < deadline && | |
| 96 | runtime.envStatus(record, env)?.state !== "running" | |
| 97 | ) | |
| 98 | await Bun.sleep(100); | |
| 99 | const state = runtime.envStatus(record, env)?.state; | |
| 100 | if (state !== "running") | |
| 101 | throw new Error( | |
| 102 | `preview ${env} did not reach "running" within ${PREVIEW_DEPLOY_WAIT_MS} ms; last state: ${state ?? "none"}`, | |
| 103 | ); | |
| 104 | } | |
| 105 | ||
| 82 | 106 | /** |
| 83 | 107 | * A runtime registered for `app`, never started, answering every app's |
| 84 | 108 | * production with `stateOf` (running unless told) — the door shows only |
| @@ -2622,6 +2646,11 @@ describe("every open pull's preview on the studio page", () => { | ||
| 2622 | 2646 | }); |
| 2623 | 2647 | if (!opened.ok) throw new Error(String(opened.error)); |
| 2624 | 2648 | const pull = opened.value; |
| 2649 | await waitForPreview( | |
| 2650 | runtime, | |
| 2651 | getAppByRepo(app.db, repo.id)!, | |
| 2652 | `pull-${pull.number}`, | |
| 2653 | ); | |
| 2625 | 2654 | |
| 2626 | 2655 | const page = await (await get("/alice/widget")).text(); |
| 2627 | 2656 | expect(page).toContain("<h2>Previews</h2>"); |
| @@ -2633,7 +2662,7 @@ describe("every open pull's preview on the studio page", () => { | ||
| 2633 | 2662 | const after = await (await get("/alice/widget")).text(); |
| 2634 | 2663 | expect(after).not.toContain("<h2>Previews</h2>"); |
| 2635 | 2664 | expect(after).not.toContain(`pull-${pull.number}/open`); |
| 2636 | }, 30_000); | |
| 2665 | }, PREVIEW_TEST_TIMEOUT_MS); | |
| 2637 | 2666 | |
| 2638 | 2667 | test("a change's page names its preview's data as its own copy and gives the moment it was taken; production's card does not", async () => { |
| 2639 | 2668 | const alice = getUserByName(app.db, "alice")!; |
| @@ -2655,13 +2684,7 @@ describe("every open pull's preview on the studio page", () => { | ||
| 2655 | 2684 | const record = getAppByRepo(app.db, repo.id)!; |
| 2656 | 2685 | const env = `pull-${pull.number}` as const; |
| 2657 | 2686 | |
| 2658 | const deadline = Date.now() + 30_000; | |
| 2659 | while ( | |
| 2660 | Date.now() < deadline && | |
| 2661 | runtime.envStatus(record, env)?.state !== "running" | |
| 2662 | ) | |
| 2663 | await Bun.sleep(100); | |
| 2664 | expect(runtime.envStatus(record, env)?.state).toBe("running"); | |
| 2687 | await waitForPreview(runtime, record, env); | |
| 2665 | 2688 | |
| 2666 | 2689 | const note = "its own copy of production's data, taken"; |
| 2667 | 2690 | |
| @@ -2676,7 +2699,7 @@ describe("every open pull's preview on the studio page", () => { | ||
| 2676 | 2699 | ).text(); |
| 2677 | 2700 | expect(changePage).toContain("<h2>Preview</h2>"); |
| 2678 | 2701 | expect(changePage).toContain(note); |
| 2679 | }, 30_000); | |
| 2702 | }, PREVIEW_TEST_TIMEOUT_MS); | |
| 2680 | 2703 | }); |
| 2681 | 2704 | |
| 2682 | 2705 | /** |
new above
offline