The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Playwright from "@alchemy.run/cloudflare-test-tools/e2e/Playwright"; |
| 2 | import { expect, test } from "@playwright/test"; |
| 3 | |
| 4 | const get = (server: { url: URL }, path: string) => fetch(new URL(path, server.url)); |
| 5 | |
| 6 | const stampOf = (body: string, prefix: string): string | undefined => |
| 7 | body.match(new RegExp(`${prefix}:(?:<!-- -->)?(\\d+)`))?.[1]; |
| 8 | |
| 9 | /** GET `path` until `predicate(stamp)` holds — bounded polling. */ |
| 10 | const pollStamp = async ( |
| 11 | server: { url: URL }, |
| 12 | path: string, |
| 13 | prefix: string, |
| 14 | predicate: (stamp: string) => boolean, |
| 15 | { times = 40, delayMs = 500 }: { times?: number; delayMs?: number } = {}, |
| 16 | ): Promise<string> => { |
| 17 | let last: string | undefined; |
| 18 | for (let i = 0; i < times; i++) { |
| 19 | const response = await get(server, path); |
| 20 | if (response.status === 200) { |
| 21 | last = stampOf(await response.text(), prefix); |
| 22 | if (last !== undefined && predicate(last)) return last; |
| 23 | } |
| 24 | await new Promise((resolve) => setTimeout(resolve, delayMs)); |
| 25 | } |
| 26 | throw new Error(`stamp never satisfied predicate (last: ${last})`); |
| 27 | }; |
| 28 | |
| 29 | for (const mode of Playwright.SERVER_METHODS) { |
| 30 | test.describe(mode, () => { |
| 31 | const it = Playwright.make(mode); |
| 32 | |
| 33 | it("caches the ISR page in KV across requests", async ({ server }) => { |
| 34 | const first = await get(server, "/isr"); |
| 35 | expect(first.status).toBe(200); |
| 36 | const stamp = stampOf(await first.text(), "isr-stamp"); |
| 37 | expect(stamp).toBeDefined(); |
| 38 | |
| 39 | // Within the (long) revalidate window the KV-cached payload serves |
| 40 | // as-is — same stamp on every hit. |
| 41 | const second = await get(server, "/isr"); |
| 42 | expect(stampOf(await second.text(), "isr-stamp")).toBe(stamp); |
| 43 | }); |
| 44 | |
| 45 | it("revalidatePath purges the writable cache (on-demand ISR)", async ({ server }) => { |
| 46 | // Prime the cache and capture the current stamp. |
| 47 | const primed = await pollStamp(server, "/isr", "isr-stamp", () => true); |
| 48 | |
| 49 | const revalidate = await fetch(new URL("/api/revalidate", server.url), { |
| 50 | method: "POST", |
| 51 | }); |
| 52 | expect(revalidate.status).toBe(200); |
| 53 | expect((await revalidate.json()) as object).toEqual({ |
| 54 | revalidated: true, |
| 55 | }); |
| 56 | |
| 57 | // The purge lands in KV; the next render produces a NEW stamp. With |
| 58 | // the read-only static-assets cache this would never change — this |
| 59 | // assertion is the proof the cache is writable. |
| 60 | const fresh = await pollStamp(server, "/isr", "isr-stamp", (s) => s !== primed); |
| 61 | expect(fresh).not.toBe(primed); |
| 62 | }); |
| 63 | |
| 64 | it("time-based revalidation regenerates through the DO queue", async ({ server }) => { |
| 65 | test.setTimeout(90_000); |
| 66 | // Prime the 2s-revalidate page. |
| 67 | const primed = await pollStamp(server, "/fast-isr", "fast-isr-stamp", () => true); |
| 68 | |
| 69 | // Let the window lapse, then keep hitting: the first stale hit |
| 70 | // enqueues a background revalidation (DO queue → self-reference |
| 71 | // fetch → KV write); a later hit serves the regenerated payload. |
| 72 | await new Promise((resolve) => setTimeout(resolve, 2_500)); |
| 73 | const fresh = await pollStamp(server, "/fast-isr", "fast-isr-stamp", (s) => s !== primed, { |
| 74 | times: 60, |
| 75 | }); |
| 76 | expect(fresh).not.toBe(primed); |
| 77 | }); |
| 78 | }); |
| 79 | } |