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, 07:15 UTC
| 1 | import { readFile, writeFile } from "node:fs/promises"; |
| 2 | import { fileURLToPath } from "node:url"; |
| 3 | import * as Playwright from "@alchemy.run/cloudflare-test-tools/e2e/Playwright"; |
| 4 | import { expect, test } from "@playwright/test"; |
| 5 | |
| 6 | const CLIENT_MARKER_FILE = fileURLToPath(new URL("../src/client-marker.ts", import.meta.url)); |
| 7 | const SERVER_MARKER_FILE = fileURLToPath(new URL("../src/server-marker.ts", import.meta.url)); |
| 8 | |
| 9 | // Write `content` and poll `check` until the dev server reflects it, |
| 10 | // re-writing periodically: linux CI's watcher occasionally drops the change |
| 11 | // event for a file edited twice in quick succession, and a re-write (same |
| 12 | // bytes, new mtime) re-fires it. Each write is a legitimate hot update, so |
| 13 | // specs asserting update semantics (state preservation) remain valid. |
| 14 | const writeUntil = async (file: string, content: string, check: () => Promise<boolean>) => { |
| 15 | for (let attempt = 0; attempt < 12; attempt++) { |
| 16 | await writeFile(file, content, "utf8"); |
| 17 | for (let poll = 0; poll < 10; poll++) { |
| 18 | if (await check()) return; |
| 19 | await new Promise((resolve) => setTimeout(resolve, 500)); |
| 20 | } |
| 21 | } |
| 22 | throw new Error(`edit to ${file} never propagated to the dev server`); |
| 23 | }; |
| 24 | |
| 25 | for (const mode of Playwright.SERVER_METHODS) { |
| 26 | test.describe(mode, () => { |
| 27 | const it = Playwright.make(mode); |
| 28 | |
| 29 | // ---- mode-shared smoke: the fixture pulls its weight in live mode too. |
| 30 | |
| 31 | it("loads the page and hydrates the counter", async ({ page, server }) => { |
| 32 | const response = await page.goto(server.url.toString()); |
| 33 | expect(response?.status()).toBe(200); |
| 34 | await expect(page.locator("#client-marker")).toHaveText("client-marker-v1"); |
| 35 | await expect(page.locator("#counter")).toHaveText("Count is 0"); |
| 36 | await page.click("#counter"); |
| 37 | await expect(page.locator("#counter")).toHaveText("Count is 1"); |
| 38 | }); |
| 39 | |
| 40 | it("serves the worker API route", async ({ server }) => { |
| 41 | const body = await server.fetchJson<{ marker: string }>("/api/marker"); |
| 42 | expect(body.marker).toBe("server-marker-v1"); |
| 43 | }); |
| 44 | |
| 45 | it("serves static assets from public/", async ({ server }) => { |
| 46 | const response = await server.fetch("/hello.txt"); |
| 47 | expect(response.status).toBe(200); |
| 48 | expect(await response.text()).toContain("hello-from-public"); |
| 49 | }); |
| 50 | |
| 51 | // ---- dev-only: edit propagation under the Cloudflare vite plugin. |
| 52 | |
| 53 | it("applies a client-module edit as a hot update that preserves state", async ({ |
| 54 | page, |
| 55 | server, |
| 56 | }) => { |
| 57 | test.skip(mode === "live", "HMR is a dev-server behavior"); |
| 58 | |
| 59 | await page.goto(server.url.toString()); |
| 60 | await expect(page.locator("#client-marker")).toHaveText("client-marker-v1"); |
| 61 | |
| 62 | // Establish client state that only survives a HOT update: a full page |
| 63 | // reload re-runs client.ts and resets the counter to 0. |
| 64 | await page.click("#counter"); |
| 65 | await expect(page.locator("#counter")).toHaveText("Count is 1"); |
| 66 | |
| 67 | const marker = page.locator("#client-marker"); |
| 68 | const showsMarker = (value: string) => async () => (await marker.textContent()) === value; |
| 69 | const source = await readFile(CLIENT_MARKER_FILE, "utf8"); |
| 70 | const edited = source.replace('"client-marker-v1"', '"client-marker-v2"'); |
| 71 | expect(edited).not.toBe(source); |
| 72 | try { |
| 73 | await writeUntil(CLIENT_MARKER_FILE, edited, showsMarker("client-marker-v2")); |
| 74 | // The proof of real HMR: the accept callback swapped the marker while |
| 75 | // the module (and its counter state) stayed alive. |
| 76 | await expect(page.locator("#counter")).toHaveText("Count is 1"); |
| 77 | } finally { |
| 78 | // Restore (and wait for it) so later specs see a settled server. |
| 79 | await writeUntil(CLIENT_MARKER_FILE, source, showsMarker("client-marker-v1")); |
| 80 | } |
| 81 | await expect(page.locator("#counter")).toHaveText("Count is 1"); |
| 82 | }); |
| 83 | |
| 84 | it("reflects a worker-module edit on a subsequent request", async ({ server }) => { |
| 85 | test.skip(mode === "live", "edit propagation is a dev-server behavior"); |
| 86 | |
| 87 | // Bounded poll: mid-update requests may fail while the module graph |
| 88 | // invalidates; treat request errors as "not yet". |
| 89 | const servesMarker = (value: string) => async () => { |
| 90 | try { |
| 91 | const body = await server.fetchJson<{ marker: string }>("/api/marker"); |
| 92 | return body.marker === value; |
| 93 | } catch { |
| 94 | // mid-update — the module graph may be invalidating |
| 95 | return false; |
| 96 | } |
| 97 | }; |
| 98 | const expectMarker = async (value: string) => { |
| 99 | for (let attempt = 0; attempt < 60; attempt++) { |
| 100 | if (await servesMarker(value)()) return; |
| 101 | await new Promise((resolve) => setTimeout(resolve, 500)); |
| 102 | } |
| 103 | throw new Error(`timed out waiting for /api/marker to serve "${value}"`); |
| 104 | }; |
| 105 | |
| 106 | await expectMarker("server-marker-v1"); |
| 107 | const source = await readFile(SERVER_MARKER_FILE, "utf8"); |
| 108 | const edited = source.replace('"server-marker-v1"', '"server-marker-v2"'); |
| 109 | expect(edited).not.toBe(source); |
| 110 | try { |
| 111 | await writeUntil(SERVER_MARKER_FILE, edited, servesMarker("server-marker-v2")); |
| 112 | } finally { |
| 113 | // Restore (and wait for it) so later specs see a settled server. |
| 114 | await writeUntil(SERVER_MARKER_FILE, source, servesMarker("server-marker-v1")); |
| 115 | } |
| 116 | }); |
| 117 | }); |
| 118 | } |