@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 176 lines 8.06 KB
Raw
1 import * as Playwright from "@alchemy.run/cloudflare-test-tools/e2e/Playwright";
2 import { expect, test } from "@playwright/test";
3
4 const SECRET = "s3cret-from-binding";
5 const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
6
7 for (const mode of Playwright.SERVER_METHODS) {
8 test.describe(mode, () => {
9 const it = Playwright.make(mode);
10
11 it("server-renders the home page with platform.env", async ({ page, server }) => {
12 const response = await page.goto(server.url.toString());
13 expect(response?.status()).toBe(200);
14 await expect(page.locator("#secret")).toHaveText(`secret:${SECRET}`);
15 await expect(page.locator("#ctx")).toHaveText("ctx:yes");
16 await expect(page.locator("#devalued")).toHaveText("devalued:{n:1}");
17 });
18
19 it("hydrates the client-interactive counter", async ({ page, server }) => {
20 await page.goto(new URL("/counter", server.url).toString());
21 await expect(page.locator("#count")).toHaveText("count:0");
22 // wait for hydration before interacting (the effect flips the marker)
23 await expect(page.locator("#increment")).toHaveAttribute("data-hydrated", "true");
24 await page.click("#increment");
25 await expect(page.locator("#count")).toHaveText("count:1");
26 await page.click("#increment");
27 await expect(page.locator("#count")).toHaveText("count:2");
28 });
29
30 it("serves the prerendered page", async ({ server }) => {
31 const response = await server.fetch("/prerendered");
32 expect(response.status).toBe(200);
33 expect(await response.text()).toContain("this-page-is-prerendered");
34 });
35
36 it("serves static assets", async ({ server }) => {
37 const response = await server.fetch("/robots.txt");
38 expect(response.status).toBe(200);
39 expect(await response.text()).toContain("User-agent: *");
40 });
41
42 it("runs the server endpoint (cookie, uuid, node:crypto, platform.env)", async ({ server }) => {
43 const body = await server.fetchJson<{
44 uuid: string;
45 nodeUuid: string;
46 cookie: string;
47 secret: string;
48 }>("/api/hello");
49 expect(body.cookie).toBe("fixture=ok");
50 expect(body.secret).toBe(SECRET);
51 expect(body.uuid).toMatch(UUID_REGEX);
52 expect(body.nodeUuid).toMatch(UUID_REGEX);
53 });
54
55 it("honors the user vite.config.ts (kit version, user plugin, adapter override)", async ({
56 server,
57 }) => {
58 // `version.name` (a user kit option) and `virtual:fixture-marker` (a
59 // user Vite plugin) both live in the fixture's own vite.config.ts — this
60 // endpoint only works when that file is loaded natively. The config
61 // also declares a user adapter that THROWS from `adapt()`, so live
62 // builds succeeding at all proves the deploy target's adapter replaced
63 // it.
64 const body = await server.fetchJson<{ marker: string; version: string; greeting: string }>(
65 "/api/user-config",
66 );
67 expect(body.marker).toBe("user-vite-plugin-active");
68 expect(body.version).toBe("fixture-user-version");
69 expect(body.greeting).toBe("greeting-via-subpath-import");
70 });
71
72 it("runs a form action via progressive enhancement (no reload)", async ({ page, server }) => {
73 await page.goto(new URL("/form", server.url).toString());
74 await expect(page.locator("#form-result")).toHaveText("result:none");
75 // wait for hydration, then plant a marker a full-page navigation would lose
76 await expect(page.locator("#submit")).toHaveAttribute("data-hydrated", "true");
77 await page.evaluate(() => {
78 (window as { __enhanced?: boolean }).__enhanced = true;
79 });
80 await page.fill("#name", "playwright");
81 await page.click("#submit");
82 await expect(page.locator("#form-result")).toHaveText("result:hello playwright");
83 await expect(page.locator("#form-secret")).toHaveText(`secret:${SECRET}`);
84 const marker = await page.evaluate(() => (window as { __enhanced?: boolean }).__enhanced);
85 expect(marker).toBe(true);
86 });
87
88 it("runs the form action without JavaScript (plain POST re-render)", async ({
89 request,
90 server,
91 }) => {
92 // a real HTTP POST (no client-side kit runtime involved), as a
93 // JavaScript-less browser would submit it
94 const response = await request.post(new URL("/form?/greet", server.url).toString(), {
95 form: { name: "curl" },
96 headers: {
97 // kit's CSRF check requires a same-origin `origin` header on form
98 // posts; `accept: text/html` selects the no-JS HTML re-render (kit
99 // answers JSON for enhanced submissions)
100 origin: server.url.origin,
101 accept: "text/html",
102 },
103 });
104 expect(response.status()).toBe(200);
105 expect(await response.text()).toContain("hello curl");
106 });
107
108 it("round-trips cookies through the +page.server load", async ({ page, server }) => {
109 await page.goto(new URL("/cookies", server.url).toString());
110 await expect(page.locator("#visits")).toHaveText("visits:1");
111 await page.reload();
112 await expect(page.locator("#visits")).toHaveText("visits:2");
113 await page.reload();
114 await expect(page.locator("#visits")).toHaveText("visits:3");
115 });
116
117 it("serves a binary endpoint response intact", async ({ server }) => {
118 const response = await server.fetch("/api/binary");
119 expect(response.status).toBe(200);
120 expect(response.headers.get("content-type")).toBe("application/octet-stream");
121 expect(response.headers.get("x-binary-length")).toBe("256");
122 const bytes = new Uint8Array(await response.arrayBuffer());
123 expect(bytes.byteLength).toBe(256);
124 expect(bytes[0]).toBe(0);
125 expect(bytes[128]).toBe(128);
126 expect(bytes[255]).toBe(255);
127 });
128
129 it("serves a route-group page with layout data (group not in URL)", async ({
130 page,
131 server,
132 }) => {
133 const response = await page.goto(new URL("/about", server.url).toString());
134 expect(response?.status()).toBe(200);
135 await expect(page.locator("#layout-section")).toHaveText("section:marketing");
136 await expect(page.locator("#layout-secret")).toHaveText(`layout-secret:${SECRET}`);
137 await expect(page.locator("#about-marker")).toHaveText("about-inside-group");
138 // the group directory name itself is not routable
139 const literal = await server.fetch("/(marketing)/about");
140 expect(literal.status).toBe(404);
141 });
142
143 it("round-trips platform.caches (put on miss, hit on the second request)", async ({
144 server,
145 }) => {
146 // a fresh key per invocation so retries never see a warm cache
147 const key = crypto.randomUUID();
148 type CacheProbe = { supported: boolean; cached: boolean; key: string };
149 const first = await server.fetchJson<CacheProbe>(`/api/cache?key=${key}`);
150 expect(first.supported).toBe(true);
151 expect(first.cached).toBe(false);
152 expect(first.key).toBe(key);
153 const second = await server.fetchJson<CacheProbe>(`/api/cache?key=${key}`);
154 expect(second.supported).toBe(true);
155 // live runs on workerd with the real Cache API; dev serves `caches`
156 // through cloudflare-runtime's platform proxy, which round-trips too
157 // (unlike wrangler's no-op dev cache).
158 expect(second.cached).toBe(true);
159 });
160
161 it("serves real bindings, env overrides, and cf through platform", async ({ page, server }) => {
162 const response = await page.goto(new URL("/platform", server.url).toString());
163 expect(response?.status()).toBe(200);
164 // FIXTURE_KV is a real KV namespace binding — in dev the put/get pair
165 // round-trips through the platform proxy's workerd instance
166 await expect(page.locator("#kv")).toHaveText("kv:kv-round-trip");
167 // FIXTURE_OVERRIDE: the dev `env` literal wins over the proxied Text
168 // binding; live serves the binding value
169 await expect(page.locator("#override")).toHaveText(
170 mode === "dev" ? "override:literal-override" : "override:proxied-value",
171 );
172 // `cf` is populated in both modes (wrangler-parity mock in dev)
173 await expect(page.locator("#cf")).toHaveText("cf-colo:present");
174 });
175 });
176 }