@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 168 lines 5.58 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Neon from "@alchemy.run/neon";
3 import { 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 type * as HttpClientResponse from "effect/http/HttpClientResponse";
9 import * as Layer from "effect/Layer";
10 import { MinimumLogLevel } from "effect/References";
11 import * as Schedule from "effect/Schedule";
12 import Stack from "./fixtures/stack.ts";
13
14 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
15 providers: Layer.merge(Cloudflare.providers(), Neon.providers()),
16 state: Cloudflare.state(),
17 });
18
19 const HOOK_TIMEOUT = 300_000;
20 const TEST_TIMEOUT = 120_000;
21
22 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
23
24 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
25 status: number;
26 body: string;
27 }> {}
28
29 // Bounded spaced schedule — caps total wait so a genuine failure surfaces
30 // fast instead of an uncapped exponential blowing past the test timeout
31 // while riding out cold-start propagation.
32 const ready = Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(30)]);
33
34 /** Retry an HTTP call until it returns 200 (rides out cold-start 404s). */
35 const untilOk = <E, R>(eff: Effect.Effect<HttpClientResponse.HttpClientResponse, E, R>) =>
36 eff.pipe(
37 Effect.flatMap((res) =>
38 res.status === 200
39 ? Effect.succeed(res)
40 : res.text.pipe(
41 Effect.flatMap((body) => Effect.fail(new WorkerNotReady({ status: res.status, body }))),
42 ),
43 ),
44 Effect.retry({
45 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
46 schedule: ready,
47 }),
48 );
49
50 interface Meta {
51 host: string;
52 port: number;
53 user: string;
54 database: string;
55 hasConnectionString: boolean;
56 hasPassword: boolean;
57 connectionStringProtocol: string;
58 connectionStringHost: string;
59 }
60
61 /** GET `${base}/meta` and assert the Hyperdrive binding resolved at runtime. */
62 const assertMeta = (base: string) =>
63 Effect.gen(function* () {
64 const res = yield* untilOk(HttpClient.get(`${base}/meta`));
65 const meta = (yield* res.json) as unknown as Meta;
66
67 // The binding resolved to a real runtime Hyperdrive object: every field
68 // is populated and the connection string is a well-formed Postgres URI
69 // pointing at Hyperdrive's local proxy host (NOT the origin host — the
70 // proxy is what Workers connect through).
71 expect(meta.host).toBeTruthy();
72 expect(meta.port).toBeGreaterThan(0);
73 expect(meta.user).toBeTruthy();
74 expect(meta.database).toBeTruthy();
75 expect(meta.hasConnectionString).toBe(true);
76 expect(meta.hasPassword).toBe(true);
77 expect(meta.connectionStringProtocol).toMatch(/^postgres(ql)?:$/);
78 // The connection string host matches the binding's `host` (the local
79 // Hyperdrive proxy), proving the parsed URI and discrete fields agree.
80 expect(meta.connectionStringHost).toBe(meta.host);
81 return meta;
82 });
83
84 /**
85 * Deploys two Workers that bind ONE shared Hyperdrive Connection (fronting a
86 * real Neon Postgres origin) via {@link Stack} — the effect-worker
87 * (`Cloudflare.Hyperdrive.Connect`) and the async-worker (`env: { HD }`) —
88 * then drives each binding flavor over `fetch`, asserting the runtime
89 * Hyperdrive object resolves with a well-formed connection string and the
90 * discrete host/port/user/database fields.
91 *
92 * Hyperdrive needs a reachable Postgres origin; the stack provisions one via
93 * `Neon.Project`, so the binding is exercised against a real database config.
94 */
95 const stack = beforeAll(deploy(Stack), { timeout: HOOK_TIMEOUT });
96 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
97 timeout: HOOK_TIMEOUT,
98 });
99
100 // ── Effect-native binding ── `yield* Cloudflare.Hyperdrive.Connect(connection)`
101 test(
102 "effect-worker: Hyperdrive.Connect resolves the runtime binding",
103 Effect.gen(function* () {
104 const out = yield* stack;
105 yield* assertMeta(out.effectWorkerUrl);
106 }).pipe(logLevel),
107 {
108 tags: [
109 "provider:cloudflare",
110 "provider:cloudflare:hyperdrive",
111 "provider:cloudflare:worker",
112 "provider:neon",
113 "provider:neon:project",
114 "live",
115 ],
116 timeout: TEST_TIMEOUT,
117 },
118 );
119
120 // `raw` escape hatch resolves to the underlying runtime Hyperdrive object.
121 test(
122 "effect-worker: raw escape hatch exposes the runtime Hyperdrive",
123 Effect.gen(function* () {
124 const out = yield* stack;
125 const res = yield* untilOk(HttpClient.get(`${out.effectWorkerUrl}/raw`));
126 const raw = (yield* res.json) as {
127 host: string;
128 port: number;
129 user: string;
130 database: string;
131 };
132 expect(raw.host).toBeTruthy();
133 expect(raw.port).toBeGreaterThan(0);
134 expect(raw.user).toBeTruthy();
135 expect(raw.database).toBeTruthy();
136 }).pipe(logLevel),
137 {
138 tags: [
139 "provider:cloudflare",
140 "provider:cloudflare:hyperdrive",
141 "provider:cloudflare:worker",
142 "provider:neon",
143 "provider:neon:project",
144 "live",
145 ],
146 timeout: TEST_TIMEOUT,
147 },
148 );
149
150 // ── Async (non-Effect) binding ── declared on `env`, resolved by `InferEnv`.
151 test(
152 "async-worker: env.HD exposes the runtime Hyperdrive binding",
153 Effect.gen(function* () {
154 const out = yield* stack;
155 yield* assertMeta(out.asyncWorkerUrl);
156 }).pipe(logLevel),
157 {
158 tags: [
159 "provider:cloudflare",
160 "provider:cloudflare:hyperdrive",
161 "provider:cloudflare:worker",
162 "provider:neon",
163 "provider:neon:project",
164 "live",
165 ],
166 timeout: TEST_TIMEOUT,
167 },
168 );