The best way to control Claude Code, Codex and more from any computer or phone

main
ts 119 lines 4.48 KB
Raw
1 import { expect } from "@alchemy.run/test";
2 import * as Data from "effect/Data";
3 import * as Effect from "effect/Effect";
4 import * as HttpClient from "effect/http/HttpClient";
5 import * as HttpClientRequest from "effect/http/HttpClientRequest";
6 import type * as HttpClientResponse from "effect/http/HttpClientResponse";
7 import * as Schedule from "effect/Schedule";
8 import type { UserRow } from "./fixtures/routes.ts";
9
10 export class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
11 status: number;
12 body: string;
13 }> {}
14
15 // Bounded spaced schedule — rides out fresh-workers.dev cold-start
16 // propagation without blowing past the test timeout.
17 export const ready = Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(45)]);
18
19 /** Retry an HTTP call until it returns 200. */
20 export const untilOk = <E, R>(eff: Effect.Effect<HttpClientResponse.HttpClientResponse, E, R>) =>
21 eff.pipe(
22 Effect.flatMap((res) =>
23 res.status === 200
24 ? Effect.succeed(res)
25 : res.text.pipe(
26 Effect.flatMap((body) => Effect.fail(new WorkerNotReady({ status: res.status, body }))),
27 ),
28 ),
29 Effect.retry({
30 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
31 schedule: ready,
32 }),
33 );
34
35 export const postJson = (url: string, body: unknown) =>
36 untilOk(
37 HttpClient.execute(HttpClientRequest.post(url).pipe(HttpClientRequest.bodyJsonUnsafe(body))),
38 ).pipe(Effect.flatMap((res) => res.json));
39
40 export const putJson = (url: string, body: unknown) =>
41 untilOk(
42 HttpClient.execute(HttpClientRequest.put(url).pipe(HttpClientRequest.bodyJsonUnsafe(body))),
43 ).pipe(Effect.flatMap((res) => res.json));
44
45 export const getJson = (url: string) =>
46 untilOk(HttpClient.get(url)).pipe(Effect.flatMap((res) => res.json));
47
48 /**
49 * Drive the dialect-agnostic `SqlClient` surface (the shared routes in
50 * `fixtures/routes.ts`) against one deployed worker. Covers, in order:
51 *
52 * - `sql.unsafe` (raw DDL) — `/init`
53 * - the `sql(table)` identifier helper — `/reset` (and every later route)
54 * - single-row `sql.insert` nested in an outer template + `sql.and` over
55 * nested fragments — `/users` (the original proxyChain regression)
56 * - multi-row `sql.insert` + `sql.literal` — `/users/batch`
57 * - `sql.update(row, omit)` — `PUT /users`
58 * - `sql.in(column, values)` — `/users/in`
59 * - `sql.or` over a nested `sql.and` — `/users/filter`
60 * - prefix-form `sql.csv` mixing string and fragment clauses — `/users/ordered`
61 * - the generic `SqlClient` service from `SQL.D1Layer` / `SQL.PostgresLayer`
62 * — `/layer/users`
63 */
64 export const exerciseSqlSurface = Effect.fn(function* (base: string) {
65 yield* postJson(`${base}/init`, {});
66 yield* postJson(`${base}/reset`, {});
67
68 // sql.insert(row) nested in the outer template, read back via sql.and.
69 const alice: UserRow = { id: 1, name: "alice", email: "alice@example.com" };
70 const created = (yield* postJson(`${base}/users`, alice)) as {
71 rows: UserRow[];
72 };
73 expect(created.rows).toEqual([alice]);
74
75 // sql.insert(rows) multi-row + sql.literal COUNT.
76 const bob: UserRow = { id: 2, name: "bob", email: "bob@example.com" };
77 const carol: UserRow = { id: 3, name: "carol", email: "carol@example.com" };
78 const batch = (yield* postJson(`${base}/users/batch`, [bob, carol])) as {
79 count: number;
80 };
81 expect(batch.count).toBe(3);
82
83 // sql.update(row, ["id"]).
84 const alicia: UserRow = {
85 id: 1,
86 name: "alicia",
87 email: "alicia@example.com",
88 };
89 const updated = (yield* putJson(`${base}/users`, alicia)) as {
90 rows: UserRow[];
91 };
92 expect(updated.rows).toEqual([alicia]);
93
94 // sql.in("id", [2, 3]).
95 const within = (yield* getJson(`${base}/users/in?ids=2,3`)) as {
96 rows: UserRow[];
97 };
98 expect(within.rows).toEqual([bob, carol]);
99
100 // sql.or([sql.and([...]), fragment]) — matches bob by name+email AND
101 // carol by id.
102 const filtered = (yield* getJson(
103 `${base}/users/filter?name=${bob.name}&email=${bob.email}&id=${carol.id}`,
104 )) as { rows: UserRow[] };
105 expect(filtered.rows).toEqual([bob, carol]);
106
107 // sql.csv("ORDER BY", [fragment, "raw string"]).
108 const ordered = (yield* getJson(`${base}/users/ordered`)) as {
109 rows: Array<{ id: number; name: string }>;
110 };
111 expect(ordered.rows.map((r) => r.name)).toEqual(["carol", "bob", "alicia"]);
112
113 // SQL.D1Layer / SQL.PostgresLayer — the generic `SqlClient` service reads
114 // the same table.
115 const viaLayer = (yield* getJson(`${base}/layer/users`)) as {
116 rows: UserRow[];
117 };
118 expect(viaLayer.rows).toEqual([alicia, bob, carol]);
119 });