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

main
ts 172 lines 6.08 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import { Action } from "alchemy/Action";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7
8 const { test } = Test.make({ providers: Cloudflare.providers() });
9
10 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
11
12 interface User {
13 id: string;
14 name: string;
15 }
16
17 // Binding a D1 database inside an Action via `QueryDatabaseLocal` — the local
18 // (current-credentials) implementation of the `QueryDatabase` binding. Exercises
19 // both the binding client (exec/prepare/run/all) and the accessor mechanism
20 // (`yield* database.databaseId` resolved at apply time).
21 test.provider(
22 "QueryDatabaseLocal: seed and query a database from an Action",
23 (stack) =>
24 Effect.gen(function* () {
25 yield* stack.destroy();
26
27 const out = yield* stack.deploy(
28 Effect.gen(function* () {
29 const database = yield* Cloudflare.D1.Database("SeedDatabase");
30
31 const Seed = Action(
32 "Seed",
33 Effect.gen(function* () {
34 const db = yield* Cloudflare.D1.QueryDatabase(database);
35 // Accessor — resolved at apply time against the tracker.
36 const databaseId = yield* database.databaseId;
37
38 return Effect.fn(function* () {
39 yield* db.exec("CREATE TABLE IF NOT EXISTS users (id TEXT PRIMARY KEY, name TEXT)");
40 yield* db.prepare("DELETE FROM users").run();
41 yield* db
42 .prepare("INSERT INTO users (id, name) VALUES (?, ?)")
43 .bind("1", "Ada")
44 .run();
45 yield* db
46 .prepare("INSERT INTO users (id, name) VALUES (?, ?)")
47 .bind("2", "Grace")
48 .run();
49
50 const rows = yield* db
51 .prepare("SELECT id, name FROM users ORDER BY id")
52 .all<User>();
53
54 const first = yield* db
55 .prepare("SELECT name FROM users WHERE id = ?")
56 .bind("1")
57 .first<string>("name");
58
59 return {
60 databaseId: yield* databaseId,
61 users: rows.results,
62 first,
63 };
64 });
65 }).pipe(Effect.provide(Cloudflare.D1.QueryDatabaseLocal)),
66 );
67
68 return yield* Seed({});
69 }),
70 );
71
72 expect(out.databaseId).toBeTruthy();
73 expect(out.users).toEqual([
74 { id: "1", name: "Ada" },
75 { id: "2", name: "Grace" },
76 ]);
77 expect(out.first).toBe("Ada");
78
79 yield* stack.destroy();
80 }).pipe(logLevel),
81 {
82 tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"],
83 timeout: 120_000,
84 },
85 );
86
87 // Native D1 bind values (number/null/boolean/binary) must round-trip, not just
88 // strings: the HTTP API binds verbatim, so the Local shim mirrors native D1
89 // (booleans -> 1/0, binary -> BLOB) and Distilled models params as unknown[].
90 test.provider(
91 "QueryDatabaseLocal: binds non-string parameters (single + batch)",
92 (stack) =>
93 Effect.gen(function* () {
94 yield* stack.destroy();
95
96 const out = yield* stack.deploy(
97 Effect.gen(function* () {
98 const database = yield* Cloudflare.D1.Database("TypedBindDatabase");
99
100 const Query = Action(
101 "QueryTypedBind",
102 Effect.gen(function* () {
103 const db = yield* Cloudflare.D1.QueryDatabase(database);
104 return Effect.fn(function* () {
105 // Single statement: number, null, boolean (-> 1/0) via SELECT ?.
106 const num = yield* db.prepare("SELECT ? AS value").bind(42).first<number>("value");
107 const nul = yield* db.prepare("SELECT ? AS value").bind(null).first<null>("value");
108 const boolTrue = yield* db
109 .prepare("SELECT ? AS value")
110 .bind(true)
111 .first<number>("value");
112 const boolFalse = yield* db
113 .prepare("SELECT ? AS value")
114 .bind(false)
115 .first<number>("value");
116
117 // Batch: mixed-type params across statements round-trip too.
118 yield* db.exec(
119 "CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY, qty INTEGER, note TEXT)",
120 );
121 yield* db.prepare("DELETE FROM items").run();
122 yield* db.batch([
123 db
124 .prepare("INSERT INTO items (id, qty, note) VALUES (?, ?, ?)")
125 .bind(1, 10, null),
126 db
127 .prepare("INSERT INTO items (id, qty, note) VALUES (?, ?, ?)")
128 .bind(2, 20, "second"),
129 ]);
130 const rows = yield* db
131 .prepare("SELECT id, qty, note FROM items ORDER BY id")
132 .all<{ id: number; qty: number; note: string | null }>();
133
134 // Binary (BLOB): bind a Uint8Array, read the byte length back.
135 const blobLen = yield* db
136 .prepare("SELECT length(?) AS len")
137 .bind(new Uint8Array([1, 2, 3, 4]))
138 .first<number>("len");
139
140 return {
141 num,
142 nul,
143 boolTrue,
144 boolFalse,
145 rows: rows.results,
146 blobLen,
147 };
148 });
149 }).pipe(Effect.provide(Cloudflare.D1.QueryDatabaseLocal)),
150 );
151
152 return yield* Query({});
153 }),
154 );
155
156 expect(out.num).toBe(42);
157 expect(out.nul).toBeNull();
158 expect(out.boolTrue).toBe(1);
159 expect(out.boolFalse).toBe(0);
160 expect(out.rows).toEqual([
161 { id: 1, qty: 10, note: null },
162 { id: 2, qty: 20, note: "second" },
163 ]);
164 expect(out.blobLen).toBe(4);
165
166 yield* stack.destroy();
167 }).pipe(logLevel),
168 {
169 tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"],
170 timeout: 120_000,
171 },
172 );