@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, 04:45 UTC

main
ts 130 lines 4.9 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Planetscale from "@alchemy.run/planetscale";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import type { HttpClientResponse } from "effect/http/HttpClientResponse";
10 import * as Layer from "effect/Layer";
11 import { MinimumLogLevel } from "effect/References";
12 import * as Schedule from "effect/Schedule";
13 import MySQLHyperdriveWorker from "./fixtures/hyperdrive-worker.ts";
14 import type { Widget } from "./fixtures/schema.ts";
15 import { Hyperdrive, PlanetscaleDb } from "./fixtures/Stack.ts";
16
17 const providers = Layer.mergeAll(Cloudflare.providers(), Planetscale.providers());
18
19 const { test } = Test.make({
20 providers,
21 });
22
23 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
24
25 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
26 status: number;
27 body: string;
28 }> {}
29
30 const fetchReady = (req: Effect.Effect<any, any, any>) =>
31 req.pipe(
32 Effect.flatMap((res: any) =>
33 res.status >= 200 && res.status < 300
34 ? res.text.pipe(Effect.as(res))
35 : res.text.pipe(
36 Effect.flatMap((body: string) =>
37 Effect.fail(new WorkerNotReady({ status: res.status, body })),
38 ),
39 ),
40 ),
41 Effect.retry({
42 while: (e: unknown): e is WorkerNotReady =>
43 e instanceof WorkerNotReady && e.status >= 400 && e.status < 600,
44 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(20)]),
45 }),
46 ) as Effect.Effect<HttpClientResponse>;
47
48 const expectWidgetRoundTrip = (baseUrl: string, widget: Widget) =>
49 Effect.gen(function* () {
50 const initial = yield* fetchReady(HttpClient.get(`${baseUrl}/widgets`));
51 expect(initial.status).toBe(200);
52 const initialBody = (yield* initial.json) as { widgets: Widget[] };
53 expect(Array.isArray(initialBody.widgets)).toBe(true);
54
55 const insertRes = yield* fetchReady(
56 HttpClient.execute(
57 HttpClientRequest.post(`${baseUrl}/widgets`).pipe(HttpClientRequest.bodyJsonUnsafe(widget)),
58 ),
59 );
60 expect(insertRes.status).toBe(200);
61 const insertBody = (yield* insertRes.json) as { widget: Widget };
62 expect(insertBody.widget).toMatchObject(widget);
63
64 const after = yield* fetchReady(HttpClient.get(`${baseUrl}/widgets`));
65 expect(after.status).toBe(200);
66 const afterBody = (yield* after.json) as { widgets: Widget[] };
67 expect(afterBody.widgets.some((w) => w.id === widget.id)).toBe(true);
68
69 const deleteRes = yield* fetchReady(
70 HttpClient.execute(HttpClientRequest.delete(`${baseUrl}/widgets/${widget.id}`)),
71 );
72 expect(deleteRes.status).toBe(200);
73
74 const final = yield* fetchReady(HttpClient.get(`${baseUrl}/widgets`));
75 const finalBody = (yield* final.json) as { widgets: Widget[] };
76 expect(finalBody.widgets.some((w) => w.id === widget.id)).toBe(false);
77 });
78
79 describe.skipIf(!process.env.PLANETSCALE_TEST).sequential(
80 "Hyperdrive",
81 {
82 tags: [
83 "provider:cloudflare",
84 "provider:cloudflare:hyperdrive",
85 "provider:cloudflare:worker",
86 "provider:planetscale",
87 "provider:planetscale:mysql",
88 "live",
89 ],
90 },
91 () => {
92 /**
93 * End-to-end: deploy a {@link Planetscale.MySQLDatabase} + branch (which
94 * applies the fixture migrations) + password, point a
95 * {@link Cloudflare.Hyperdrive.Connection} at the password's origin, and
96 * exercise the Drizzle Effect MySQL client over real MySQL via a Worker.
97 *
98 * Validates that:
99 * - migrations applied from the fixtures dir produce the expected table
100 * - `Cloudflare.Hyperdrive.Connect(...) + Drizzle.MySQL(...)` produces
101 * a working Effect-native client at runtime (text protocol +
102 * eval-free row parsers by default on workerd)
103 * - INSERT / SELECT / DELETE round-trip through Hyperdrive to PlanetScale
104 */
105 test.provider(
106 "MySQLBranch + Hyperdrive + Drizzle round-trips through a Worker",
107 (stack) =>
108 Effect.gen(function* () {
109 yield* stack.destroy();
110
111 const { hyperdrive, worker } = yield* stack.deploy(
112 Effect.gen(function* () {
113 yield* PlanetscaleDb;
114 const hyperdrive = yield* Hyperdrive;
115 const worker = yield* MySQLHyperdriveWorker;
116 return { hyperdrive, worker };
117 }),
118 );
119
120 expect(worker.url).toBeTypeOf("string");
121 expect(hyperdrive.origin).toMatchObject({ port: 3306 });
122 const baseUrl = (worker.url as string).replace(/\/+$/, "");
123 yield* expectWidgetRoundTrip(baseUrl, { id: 1, name: "alpha" });
124
125 yield* stack.destroy();
126 }).pipe(logLevel),
127 { timeout: 900_000 },
128 );
129 },
130 );