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

main
ts 471 lines 16.5 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import {
4 STATE_STORE_SCRIPT_NAME,
5 STATE_STORE_VERSION,
6 } from "@alchemy.run/cloudflare/StateStore/Api";
7 import { readStoreCredentials } from "@alchemy.run/cloudflare/StateStore/CredentialsFile";
8 import { describe, expect } from "@alchemy.run/test";
9 import * as Test from "@alchemy.run/test/Alchemy";
10 import * as workers from "@distilled.cloud/cloudflare/workers";
11 import { CredentialsStore } from "alchemy/Auth/Credentials";
12 import { currentProfileName } from "alchemy/Auth/Profile";
13 import { StateApi } from "alchemy/State/HttpStateApi";
14 import { makeHttpStateStore } from "alchemy/State/HttpStateStore";
15 import { State } from "alchemy/State/State";
16 import * as Effect from "effect/Effect";
17 import * as HttpApiClient from "effect/http-api/HttpApiClient";
18 import * as HttpClient from "effect/http/HttpClient";
19 import * as HttpClientRequest from "effect/http/HttpClientRequest";
20 /**
21 * Live-API tests for the deployed Cloudflare State Store at
22 * `alchemy-state-store`. The State service is wired up via
23 * `Cloudflare.state()` so each test body just `yield* yield* State` and
24 * exercises the typed interface — same code path users hit in
25 * production.
26 *
27 * Tests share a single (stack, stage) pair so they can be reasoned
28 * about as a sequence: the first test wipes the namespace, the
29 * remainder build state up and tear it down.
30 */
31
32 const STACK = "alchemy_tests";
33 const STAGE = "alchemy_tests";
34 const SCRIPT_NAME = STATE_STORE_SCRIPT_NAME;
35
36 const { test } = Test.make({
37 providers: Cloudflare.providers(),
38 state: Cloudflare.state(),
39 });
40
41 const sampleState = (fqn: string, instanceId: string) => ({
42 kind: "resource" as const,
43 resourceType: "Test.Resource",
44 namespace: undefined,
45 fqn,
46 logicalId: fqn.split("/").pop()!,
47 instanceId,
48 providerVersion: 1,
49 status: "created" as const,
50 downstream: [],
51 bindings: [],
52 props: { hello: "world" },
53 attr: { id: instanceId },
54 });
55
56 const replacedState = (fqn: string) => ({
57 kind: "resource" as const,
58 resourceType: "Test.Resource",
59 namespace: undefined,
60 fqn,
61 logicalId: fqn.split("/").pop()!,
62 instanceId: "replaced-new",
63 providerVersion: 1,
64 status: "replaced" as const,
65 downstream: [],
66 bindings: [],
67 deleteFirst: false,
68 props: { hello: "world-new" },
69 attr: { id: "replaced-new" },
70 old: {
71 kind: "resource",
72 resourceType: "Test.Resource",
73 namespace: undefined,
74 fqn,
75 logicalId: fqn.split("/").pop()!,
76 instanceId: "replaced-old",
77 providerVersion: 1,
78 status: "created",
79 downstream: [],
80 bindings: [],
81 props: { hello: "world-old" },
82 attr: { id: "replaced-old" },
83 },
84 });
85
86 // These cases share a single (stack, stage) namespace and are written to be
87 // read as one sequence (first case wipes it, the rest build it up and tear it
88 // down). They must run serially under the global concurrent test config.
89 describe.sequential(
90 "State",
91 { tags: ["provider:cloudflare", "provider:cloudflare:statestore", "live"] },
92 () => {
93 test(
94 "getVersion returns the current STATE_STORE_VERSION",
95 Effect.gen(function* () {
96 const store = yield* yield* State;
97 const version = yield* store.getVersion();
98 expect(version).toBe(STATE_STORE_VERSION);
99 }),
100 { timeout: 60_000 },
101 );
102
103 test.provider(
104 "a failed Durable Object call answers as a typed StateStoreFailure",
105 () =>
106 Effect.gen(function* () {
107 // `yield* State` deploys the store at STATE_STORE_VERSION first.
108 yield* yield* State;
109 const credentials = yield* readStoreCredentials(
110 yield* CredentialsStore,
111 yield* currentProfileName,
112 SCRIPT_NAME,
113 );
114 expect(credentials).toBeDefined();
115 const client = yield* HttpApiClient.make(StateApi, {
116 baseUrl: credentials!.url,
117 transformClient: HttpClient.mapRequest(
118 HttpClientRequest.bearerToken(credentials!.authToken),
119 ),
120 });
121 // Past the 32 MiB Durable Object RPC cap, so the authorized request
122 // fails inside the Store call (#1833). Called once, without the
123 // state client's retries.
124 const error = yield* client.state
125 .setState({
126 params: { stack: STACK, stage: STAGE, fqn: "stack/scope/too-large" },
127 payload: { blob: "x".repeat(34_000_000) },
128 })
129 .pipe(Effect.flip);
130 expect(error._tag).toBe("StateStoreFailure");
131 expect(error).toMatchObject({ operation: "setState", reason: "RpcCallError" });
132 }),
133 { timeout: 120_000 },
134 );
135
136 test.provider(
137 "a request with a wrong bearer token is rejected as unauthorized",
138 () =>
139 Effect.gen(function* () {
140 yield* yield* State;
141 const { accountId } = yield* yield* CloudflareEnvironment;
142 const { subdomain } = yield* workers.getSubdomain({ accountId });
143 const store = yield* makeHttpStateStore({
144 id: "cloudflare-http",
145 url: `https://${SCRIPT_NAME}.${subdomain}.workers.dev`,
146 authToken: "not-the-state-store-token",
147 });
148 const error = yield* store
149 .get({ stack: STACK, stage: STAGE, fqn: "stack/scope/resource-a" })
150 .pipe(Effect.flip);
151 expect(error._tag).toBe("StateStoreError");
152 expect(error.message).toContain("State store rejected the request as unauthorized.");
153 expect(error.http).toEqual({ status: 401 });
154 }),
155 { timeout: 60_000 },
156 );
157
158 test(
159 "a resource whose URI-encoded FQN exceeds 100 chars round-trips",
160 Effect.gen(function* () {
161 const store = yield* yield* State;
162 const stage = `${STAGE}-long-fqn`;
163 // Each "/" encodes to "%2F", so this FQN is a ~200-char path segment.
164 const fqn = Array.from({ length: 8 }, (_, i) => `Namespace${i}/Scope${i}`).join("/");
165 expect(encodeURIComponent(fqn).length).toBeGreaterThan(100);
166 yield* store.set({ stack: STACK, stage, fqn, value: sampleState(fqn, "inst-long") });
167 const got = yield* store.get({ stack: STACK, stage, fqn });
168 expect(got?.fqn).toBe(fqn);
169 expect(yield* store.list({ stack: STACK, stage })).toEqual([fqn]);
170 yield* store.delete({ stack: STACK, stage, fqn });
171 expect(yield* store.get({ stack: STACK, stage, fqn })).toBeUndefined();
172 yield* store.deleteStack({ stack: STACK, stage });
173 }),
174 { timeout: 60_000 },
175 );
176
177 test(
178 "DELETE /state/stacks/:stack wipes the test namespace (cleanup)",
179 Effect.gen(function* () {
180 const store = yield* yield* State;
181 yield* store.deleteStack({ stack: STACK });
182 const fqns = yield* store.list({ stack: STACK, stage: STAGE });
183 expect(fqns).toEqual([]);
184 }),
185 { timeout: 60_000 },
186 );
187
188 test(
189 "PUT /resources/:fqn (setState) persists a resource",
190 Effect.gen(function* () {
191 const store = yield* yield* State;
192 const fqn = "stack/scope/resource-a";
193 const value = sampleState(fqn, "inst-a");
194 const echoed = yield* store.set({ stack: STACK, stage: STAGE, fqn, value });
195 expect(echoed.fqn).toBe(fqn);
196 expect(echoed.instanceId).toBe("inst-a");
197 }),
198 { timeout: 60_000 },
199 );
200
201 test(
202 "GET /resources/:fqn (getState) reads back the persisted value",
203 Effect.gen(function* () {
204 const store = yield* yield* State;
205 const fqn = "stack/scope/resource-a";
206 const got = yield* store.get({ stack: STACK, stage: STAGE, fqn });
207 expect(got).toBeDefined();
208 expect(got!.fqn).toBe(fqn);
209 expect((got as any).props).toEqual({ hello: "world" });
210 }),
211 { timeout: 60_000 },
212 );
213
214 test(
215 "GET /resources/:fqn returns undefined for a missing fqn",
216 Effect.gen(function* () {
217 const store = yield* yield* State;
218 const got = yield* store.get({
219 stack: STACK,
220 stage: STAGE,
221 fqn: "stack/scope/does-not-exist",
222 });
223 expect(got).toBeUndefined();
224 }),
225 { timeout: 60_000 },
226 );
227
228 test(
229 "GET /resources (listResources) returns the FQNs in the stage",
230 Effect.gen(function* () {
231 const store = yield* yield* State;
232 const fqnB = "stack/scope/resource-b";
233 yield* store.set({
234 stack: STACK,
235 stage: STAGE,
236 fqn: fqnB,
237 value: sampleState(fqnB, "inst-b"),
238 });
239 const fqns = yield* store.list({ stack: STACK, stage: STAGE });
240 expect([...fqns].sort()).toEqual(["stack/scope/resource-a", "stack/scope/resource-b"]);
241 }),
242 { timeout: 60_000 },
243 );
244
245 test(
246 "GET /stacks (listStacks) includes the registered test stack",
247 Effect.gen(function* () {
248 const store = yield* yield* State;
249 const stacks = yield* store.listStacks();
250 expect(stacks).toContain(STACK);
251 }),
252 { timeout: 60_000 },
253 );
254
255 test(
256 "GET /stacks/:stack/stages (listStages) includes the test stage",
257 Effect.gen(function* () {
258 const store = yield* yield* State;
259 const stages = yield* store.listStages(STACK);
260 expect([...stages]).toContain(STAGE);
261 }),
262 { timeout: 60_000 },
263 );
264
265 test(
266 "PUT /output (setStackOutput) persists a stack output",
267 Effect.gen(function* () {
268 const store = yield* yield* State;
269 const out = yield* store.setOutput({
270 stack: STACK,
271 stage: STAGE,
272 value: { url: "https://example.com", count: 42 },
273 });
274 expect(out).toEqual({ url: "https://example.com", count: 42 });
275 }),
276 { timeout: 60_000 },
277 );
278
279 test(
280 "GET /output (getStackOutput) reads back the persisted output",
281 Effect.gen(function* () {
282 const store = yield* yield* State;
283 const out = yield* store.getOutput({ stack: STACK, stage: STAGE });
284 expect(out).toEqual({ url: "https://example.com", count: 42 });
285 }),
286 { timeout: 60_000 },
287 );
288
289 test(
290 "GET /output returns undefined for an un-deployed stage",
291 Effect.gen(function* () {
292 const store = yield* yield* State;
293 const out = yield* store.getOutput({ stack: STACK, stage: "never-deployed" });
294 expect(out).toBeUndefined();
295 }),
296 { timeout: 60_000 },
297 );
298
299 test(
300 "GET /replaced-resources (getReplacedResources) returns status===replaced rows",
301 Effect.gen(function* () {
302 const store = yield* yield* State;
303 const fqn = "stack/scope/resource-replaced";
304 yield* store.set({ stack: STACK, stage: STAGE, fqn, value: replacedState(fqn) as any });
305 const replaced = yield* store.getReplacedResources({ stack: STACK, stage: STAGE });
306 const fqns = replaced.map((r) => (r as any).fqn);
307 expect(fqns).toContain(fqn);
308 }),
309 { timeout: 60_000 },
310 );
311
312 test(
313 "DELETE /resources/:fqn (deleteState) removes a single resource",
314 Effect.gen(function* () {
315 const store = yield* yield* State;
316 const fqn = "stack/scope/resource-a";
317 yield* store.delete({ stack: STACK, stage: STAGE, fqn });
318 const got = yield* store.get({ stack: STACK, stage: STAGE, fqn });
319 expect(got).toBeUndefined();
320 const fqns = yield* store.list({ stack: STACK, stage: STAGE });
321 expect([...fqns]).not.toContain(fqn);
322 }),
323 { timeout: 60_000 },
324 );
325
326 test(
327 "DELETE /stacks/:stack?stage=... clears one stage but leaves the stack registered",
328 Effect.gen(function* () {
329 const store = yield* yield* State;
330 yield* store.deleteStack({ stack: STACK, stage: STAGE });
331 const fqns = yield* store.list({ stack: STACK, stage: STAGE });
332 expect(fqns).toEqual([]);
333 const out = yield* store.getOutput({ stack: STACK, stage: STAGE });
334 expect(out).toBeUndefined();
335 // Per Api.ts: stage-scoped deletes do NOT unregister the stack.
336 const stacks = yield* store.listStacks();
337 expect(stacks).toContain(STACK);
338 }),
339 { timeout: 60_000 },
340 );
341
342 test(
343 "DELETE /stacks/:stack (no stage) removes the stack from listStacks",
344 Effect.gen(function* () {
345 const store = yield* yield* State;
346 yield* store.deleteStack({ stack: STACK });
347 const stacks = yield* store.listStacks();
348 expect(stacks).not.toContain(STACK);
349 }),
350 { timeout: 60_000 },
351 );
352
353 /**
354 * Stress test for transient `setState` failures (the reported 415 / etc.
355 * symptoms). 100 sequential PUTs against the same `(stack, stage, fqn)`
356 * — if the worker is racy under repeated writes (cold-start layer
357 * memoization, edge propagation, intermediary content-type munging)
358 * one of these calls should surface it.
359 */
360 test(
361 "setState 100x sequential — surfaces transient failures",
362 Effect.gen(function* () {
363 const store = yield* yield* State;
364 const stack = STACK;
365 const stage = `${STAGE}-stress-seq`;
366 const fqn = "stack/scope/stress-seq";
367
368 yield* store.deleteStack({ stack, stage });
369
370 yield* Effect.forEach(
371 Array.from({ length: 100 }, (_, i) => i),
372 (i) =>
373 store
374 .set({ stack, stage, fqn, value: sampleState(fqn, `inst-${i}`) })
375 .pipe(Effect.asVoid),
376 { discard: true },
377 );
378
379 const got = yield* store.get({ stack, stage, fqn });
380 expect(got).toBeDefined();
381 expect((got as any).instanceId).toBe("inst-99");
382
383 yield* store.deleteStack({ stack, stage });
384 }),
385 { timeout: 300_000 },
386 );
387
388 /**
389 * Same shape as the sequential stress test, but fires all 100 PUTs
390 * concurrently. Designed to catch races inside the Durable Object
391 * (`Store.getByName(stack)`) and the HttpApiBuilder layer
392 * initialization that wouldn't show up serially.
393 */
394 test(
395 "setState 100x concurrent — surfaces racy transient failures",
396 Effect.gen(function* () {
397 const store = yield* yield* State;
398 const stack = STACK;
399 const stage = `${STAGE}-stress-par`;
400
401 yield* store.deleteStack({ stack, stage });
402
403 yield* Effect.forEach(
404 Array.from({ length: 100 }, (_, i) => i),
405 (i) => {
406 const fqn = `stack/scope/stress-par-${i}`;
407 return store
408 .set({ stack, stage, fqn, value: sampleState(fqn, `inst-${i}`) })
409 .pipe(Effect.asVoid);
410 },
411 { concurrency: "unbounded", discard: true },
412 );
413
414 const fqns = yield* store.list({ stack, stage });
415 expect(fqns.length).toBe(100);
416
417 yield* store.deleteStack({ stack, stage });
418 }),
419 { timeout: 300_000 },
420 );
421
422 /**
423 * Engine-shape traffic pattern: for each resource, the apply loop reads
424 * the previous state (`GET /resources/:fqn`) and then writes the new
425 * state (`PUT /resources/:fqn`). When the engine deploys many
426 * resources in parallel, the worker sees interleaved GET+PUT pairs
427 * sharing the same connection / isolate.
428 *
429 * The 30×5 shape (30 sequential batches, 5 GET+PUT pairs in flight per
430 * batch) reproduces a transient 415 the engine surfaces under that
431 * pattern. The test fails on the first non-2xx so any regression of
432 * the underlying issue is caught immediately.
433 */
434 test(
435 "GET+PUT interleaved 30x5 — engine traffic pattern",
436 Effect.gen(function* () {
437 const store = yield* yield* State;
438 const stack = STACK;
439 const stage = `${STAGE}-interleaved`;
440
441 yield* store.deleteStack({ stack, stage });
442
443 yield* Effect.forEach(
444 Array.from({ length: 30 }, (_, b) => b),
445 (b) =>
446 Effect.forEach(
447 Array.from({ length: 5 }, (_, i) => `M${b}_${i}`),
448 (fqn) =>
449 Effect.all(
450 [
451 store.get({ stack, stage, fqn }).pipe(Effect.asVoid),
452 store
453 .set({ stack, stage, fqn, value: sampleState(fqn, `inst-${fqn}`) })
454 .pipe(Effect.asVoid),
455 ],
456 { concurrency: "unbounded", discard: true },
457 ),
458 { concurrency: "unbounded", discard: true },
459 ),
460 { discard: true },
461 );
462
463 const fqns = yield* store.list({ stack, stage });
464 expect(fqns.length).toBe(30 * 5);
465
466 yield* store.deleteStack({ stack, stage });
467 }),
468 { timeout: 300_000 },
469 );
470 },
471 );