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

main
ts 256 lines 8.92 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as aisearch from "@distilled.cloud/cloudflare/aisearch";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import { stageSuffix } from "../../names.ts";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // The scoped API token the test harness mints propagates eventually-
17 // consistently across Cloudflare's edge — ride out 403 blips
18 // (`Forbidden`, declared in the distilled error union) on the test's
19 // own out-of-band verification calls.
20 const getNamespace = (accountId: string, name: string) =>
21 aisearch.readNamespace({ accountId, name }).pipe(
22 Effect.retry({
23 while: (e) => e._tag === "Forbidden",
24 schedule: Schedule.exponential("500 millis"),
25 times: 8,
26 }),
27 );
28
29 // A freshly created namespace can lag on the read path — ride out
30 // `NamespaceNotFound` (Cloudflare error code 7063) with a bounded retry
31 // when the namespace is expected to exist.
32 const waitForNamespace = (accountId: string, name: string) =>
33 getNamespace(accountId, name).pipe(
34 Effect.retry({
35 while: (e) => e._tag === "NamespaceNotFound",
36 schedule: Schedule.exponential("500 millis"),
37 times: 8,
38 }),
39 );
40
41 const expectGone = (accountId: string, name: string) =>
42 getNamespace(accountId, name).pipe(
43 Effect.flatMap(() => Effect.fail({ _tag: "NamespaceNotDeleted" } as const)),
44 // A missing namespace surfaces as `NamespaceNotFound` (Cloudflare
45 // error code 7063) — that's the success condition here.
46 Effect.catchTag("NamespaceNotFound", () => Effect.void),
47 Effect.retry({
48 while: (e) => e._tag === "NamespaceNotDeleted",
49 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
50 }),
51 );
52
53 // Namespace reads are served eventually-consistently — poll (bounded)
54 // until the live description converges to the expected value.
55 const expectDescription = (accountId: string, name: string, expected: string | undefined) =>
56 getNamespace(accountId, name).pipe(
57 Effect.repeat({
58 schedule: Schedule.spaced("2 seconds"),
59 until: (ns) => (ns.description ?? undefined) === expected,
60 times: 30,
61 }),
62 Effect.map((ns) => expect(ns.description ?? undefined).toEqual(expected)),
63 );
64
65 const program = (props?: Cloudflare.AI.SearchNamespaceProps) =>
66 Effect.gen(function* () {
67 const namespace = yield* Cloudflare.AI.SearchNamespace("Namespace", {
68 ...props,
69 });
70 return { namespace };
71 });
72
73 test.provider(
74 "create, update description in place, and delete a namespace",
75 (stack) =>
76 Effect.gen(function* () {
77 const { accountId } = yield* yield* CloudflareEnvironment;
78
79 yield* stack.destroy();
80
81 // Create — engine-generated name, no description.
82 const initial = yield* stack.deploy(program());
83
84 expect(initial.namespace.name).toBeTruthy();
85 expect(initial.namespace.accountId).toEqual(accountId);
86 expect(initial.namespace.description).toBeUndefined();
87
88 const live = yield* waitForNamespace(accountId, initial.namespace.name);
89 expect(live.name).toEqual(initial.namespace.name);
90
91 // Update the description in place — same namespace (same name and
92 // creation timestamp).
93 const updated = yield* stack.deploy(
94 program({ description: "alchemy aisearch namespace test" }),
95 );
96
97 expect(updated.namespace.name).toEqual(initial.namespace.name);
98 expect(updated.namespace.description).toEqual("alchemy aisearch namespace test");
99
100 yield* expectDescription(
101 accountId,
102 updated.namespace.name,
103 "alchemy aisearch namespace test",
104 );
105
106 // Redeploying identical props is a no-op (still the same namespace).
107 const noop = yield* stack.deploy(program({ description: "alchemy aisearch namespace test" }));
108 expect(noop.namespace.name).toEqual(initial.namespace.name);
109
110 // Clearing the description converges back to unset.
111 const cleared = yield* stack.deploy(program());
112 expect(cleared.namespace.name).toEqual(initial.namespace.name);
113 expect(cleared.namespace.description).toBeUndefined();
114
115 yield* expectDescription(accountId, cleared.namespace.name, undefined);
116
117 yield* stack.destroy();
118
119 yield* expectGone(accountId, initial.namespace.name);
120
121 // Destroy again — delete must be idempotent (already gone).
122 yield* stack.destroy();
123 }).pipe(logLevel),
124 {
125 tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"],
126 timeout: 240_000,
127 },
128 );
129
130 test.provider(
131 "changing the name triggers a replacement",
132 (stack) =>
133 Effect.gen(function* () {
134 const { accountId } = yield* yield* CloudflareEnvironment;
135 const user = stageSuffix();
136 const nameA = `${user}-alch-ais-ns-a`;
137 const nameB = `${user}-alch-ais-ns-b`;
138
139 yield* stack.destroy();
140
141 const initial = yield* stack.deploy(program({ name: nameA }));
142 expect(initial.namespace.name).toEqual(nameA);
143
144 const replaced = yield* stack.deploy(program({ name: nameB }));
145
146 // The name is the namespace's identity — a new physical namespace
147 // exists and the old one was deleted as part of the replacement.
148 expect(replaced.namespace.name).toEqual(nameB);
149
150 const live = yield* waitForNamespace(accountId, nameB);
151 expect(live.name).toEqual(nameB);
152
153 yield* expectGone(accountId, nameA);
154
155 yield* stack.destroy();
156
157 yield* expectGone(accountId, nameB);
158 }).pipe(logLevel),
159 {
160 tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"],
161 timeout: 240_000,
162 },
163 );
164
165 // Canonical `list()` test (account-scoped collection): deploy a real
166 // namespace, resolve the provider from context via the typed
167 // `findProvider`, call `list()`, and assert the deployed namespace
168 // appears in the exhaustively-paginated result.
169 test.provider(
170 "list enumerates the deployed namespace",
171 (stack) =>
172 Effect.gen(function* () {
173 yield* stack.destroy();
174
175 const deployed = yield* stack.deploy(program());
176
177 const provider = yield* Provider.findProvider(Cloudflare.AI.SearchNamespace);
178 const all = yield* provider.list();
179
180 expect(all.some((ns) => ns.name === deployed.namespace.name)).toBe(true);
181
182 yield* stack.destroy();
183 }).pipe(logLevel),
184 {
185 tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"],
186 timeout: 240_000,
187 },
188 );
189
190 test.provider(
191 "adopts the reserved default namespace without deleting it on teardown",
192 (stack) =>
193 Effect.gen(function* () {
194 const { accountId } = yield* yield* CloudflareEnvironment;
195
196 yield* stack.destroy();
197
198 // The account-provided `default` namespace always exists; adopting it
199 // is bindable but must never be created or torn down.
200 const adopted = yield* stack.deploy(program({ name: "default" }));
201 expect(adopted.namespace.name).toEqual("default");
202
203 const live = yield* getNamespace(accountId, "default");
204 expect(live.name).toEqual("default");
205
206 yield* stack.destroy();
207
208 // Destroy is a no-op for the default namespace — it must still exist.
209 const stillThere = yield* getNamespace(accountId, "default");
210 expect(stillThere.name).toEqual("default");
211 }).pipe(logLevel),
212 {
213 tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"],
214 timeout: 120_000,
215 },
216 );
217
218 test.provider(
219 "recreates after out-of-band delete",
220 (stack) =>
221 Effect.gen(function* () {
222 const { accountId } = yield* yield* CloudflareEnvironment;
223
224 yield* stack.destroy();
225
226 const initial = yield* stack.deploy(program());
227
228 // Delete the namespace out-of-band. A redeploy with identical props
229 // is a planner no-op, so change a mutable prop to force reconcile —
230 // it must observe the namespace as missing and recreate it instead
231 // of failing on a 404.
232 yield* aisearch.deleteNamespace({ accountId, name: initial.namespace.name }).pipe(
233 Effect.retry({
234 while: (e) => e._tag === "Forbidden",
235 schedule: Schedule.exponential("500 millis"),
236 times: 8,
237 }),
238 );
239 yield* expectGone(accountId, initial.namespace.name);
240
241 const healed = yield* stack.deploy(
242 program({ description: "healed after out-of-band delete" }),
243 );
244
245 expect(healed.namespace.name).toEqual(initial.namespace.name);
246 yield* expectDescription(accountId, healed.namespace.name, "healed after out-of-band delete");
247
248 yield* stack.destroy();
249
250 yield* expectGone(accountId, healed.namespace.name);
251 }).pipe(logLevel),
252 {
253 tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"],
254 timeout: 240_000,
255 },
256 );