@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 152 lines 6.25 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { isLocalId } from "@alchemy.run/cloudflare/LocalRuntime";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Effect from "effect/Effect";
6 import * as HttpClient from "effect/http/HttpClient";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import * as pathe from "pathe";
10 import { cloneFixture } from "../Utils/Fixture.ts";
11 import { expectUrlContains, expectUrlHeader } from "../Utils/Http.ts";
12
13 const { test } = Test.make({ providers: Cloudflare.providers(), dev: true });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures", "vinext-app");
18 const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp");
19
20 /**
21 * Under `alchemy dev` the site runs locally: vinext's own Vite dev server
22 * (the RSC environment in workerd behind the alchemy dev proxy) with the
23 * auto-provisioned data-cache KV emulated by the local provider.
24 */
25 describe.concurrent(
26 "Vinext dev",
27 { tags: ["provider:cloudflare", "provider:cloudflare:kv", "provider:cloudflare:website"] },
28 () => {
29 test.provider(
30 "Vinext dev: local dev server renders SSR and serves public files through ASSETS",
31 (stack) =>
32 Effect.gen(function* () {
33 yield* stack.destroy();
34
35 const rootDir = yield* cloneFixture(fixtureDir, {
36 prefix: "alchemy-vinext-dev-",
37 tempRoot,
38 entries: [".gitignore", "package.json", "vite.config.ts", "app", "public", "worker"],
39 });
40
41 const { site, cache } = yield* stack.deploy(
42 Effect.gen(function* () {
43 const site = yield* Cloudflare.Website.Vinext("VinextLocal", {
44 rootDir,
45 dev: { port: 0 },
46 memo: {
47 include: ["app/**", "public/**", "worker/**", "package.json", "vite.config.ts"],
48 },
49 });
50 // Dedupes by logical id: the data-cache namespace the Vinext
51 // resource auto-provisioned.
52 const cache = yield* Cloudflare.KV.Namespace("VinextLocalIsrCache");
53 return { site, cache };
54 }),
55 );
56
57 // Local identity: the url points at the alchemy dev proxy, and the
58 // cache namespace carries the `dev:` marker (no cloud call ran).
59 expect(site.url).toMatch(/^http:\/\/localhost:\d+/);
60 expect(isLocalId(cache.namespaceId)).toBe(true);
61
62 yield* expectUrlContains(`${site.url!}/`, "VINEXT_PAGE_MARKER", {
63 timeout: "180 seconds",
64 label: "vinext dev SSR home",
65 });
66
67 // Static asset from `public/` through the dev server.
68 yield* expectUrlContains(`${site.url!}/robots.txt`, "User-agent", {
69 timeout: "60 seconds",
70 label: "vinext dev static asset",
71 });
72
73 // The same file read by a server route through the `ASSETS` binding.
74 yield* expectUrlContains(`${site.url!}/api/asset`, "User-agent", {
75 timeout: "60 seconds",
76 label: "vinext dev ASSETS binding",
77 });
78
79 yield* stack.destroy();
80 }).pipe(logLevel),
81 { timeout: 300_000, tags: ["provider:cloudflare", "provider:cloudflare:website", "local"] },
82 );
83
84 // alchemy-run/alchemy#2231: tail consumers were dropped on the source
85 // provider's dev path.
86 test.provider(
87 "Vinext dev: a custom main serves and streams its events to tail consumers",
88 (stack) =>
89 Effect.gen(function* () {
90 yield* stack.destroy();
91
92 const rootDir = yield* cloneFixture(fixtureDir, {
93 prefix: "alchemy-vinext-tail-",
94 tempRoot,
95 entries: [".gitignore", "package.json", "vite.config.ts", "app", "public", "worker"],
96 });
97
98 const { site, consumer } = yield* stack.deploy(
99 Effect.gen(function* () {
100 const events = yield* Cloudflare.KV.Namespace("VinextTailEvents");
101 const consumer = yield* Cloudflare.Worker("VinextTailConsumer", {
102 main: pathe.resolve(import.meta.dirname, "fixtures/tail-consumer.ts"),
103 env: { EVENTS: events },
104 dev: { port: 0 },
105 });
106 const site = yield* Cloudflare.Website.Vinext("VinextCustomMain", {
107 rootDir,
108 main: "worker/custom.ts",
109 tailConsumers: [consumer],
110 streamingTailConsumers: [consumer],
111 dev: { port: 0 },
112 memo: {
113 include: ["app/**", "public/**", "worker/**", "package.json", "vite.config.ts"],
114 },
115 });
116 return { site, consumer };
117 }),
118 );
119
120 yield* expectUrlHeader(`${site.url!}/`, "x-alchemy-entry", "custom", {
121 timeout: "180 seconds",
122 label: "vinext dev custom main",
123 });
124
125 // Each poll drives the site again: the consumer may register with
126 // the dev registry after the site's first requests.
127 const client = yield* HttpClient.HttpClient;
128 const recorded = yield* Effect.gen(function* () {
129 yield* client.get(`${site.url!}/`).pipe(Effect.flatMap((res) => res.text));
130 const res = yield* client.get(`${consumer.url!}/events`);
131 return (yield* res.json) as { tail: string[]; stream: string[] };
132 }).pipe(
133 Effect.orDie,
134 Effect.repeat({
135 schedule: Schedule.spaced("1 second"),
136 until: ({ tail, stream }) =>
137 tail.some((batch) => batch.includes("vinext-custom-entry")) &&
138 stream.some((session) => session.includes("vinext-custom-entry")),
139 times: 30,
140 }),
141 );
142 expect(recorded.tail.some((batch) => batch.includes("vinext-custom-entry"))).toBe(true);
143 expect(recorded.stream.some((session) => session.includes("vinext-custom-entry"))).toBe(
144 true,
145 );
146
147 yield* stack.destroy();
148 }).pipe(logLevel),
149 { timeout: 300_000, tags: ["provider:cloudflare", "provider:cloudflare:website", "local"] },
150 );
151 },
152 );