@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 935 lines 40.4 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { assert, describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Containers from "@distilled.cloud/cloudflare/containers";
5 import { destroy as destroyStack } from "alchemy/Destroy";
6 import * as Plan from "alchemy/Plan";
7 import { Stage } from "alchemy/Stage";
8 import { State, inMemoryState } from "alchemy/State";
9 import * as Cause from "effect/Cause";
10 import * as Effect from "effect/Effect";
11 import * as Exit from "effect/Exit";
12 import * as HttpClient from "effect/http/HttpClient";
13 import * as HttpClientRequest from "effect/http/HttpClientRequest";
14 import * as Layer from "effect/Layer";
15 import { MinimumLogLevel } from "effect/References";
16 import * as Schedule from "effect/Schedule";
17 import makeAsyncContainerStack from "./fixtures/async/stack.ts";
18 import AttachmentStack, {
19 attachmentStack,
20 stackName as attachmentStackName,
21 } from "./fixtures/attachment/stack.ts";
22 import EffectfulStack from "./fixtures/effectful/stack.ts";
23 import makeExternalStack from "./fixtures/external/stack.ts";
24 import makeInferredClassStack from "./fixtures/inferred/stack.ts";
25 import { DEMO_PLAIN, DEMO_SECRET } from "./fixtures/remote/object.ts";
26 import makeRemoteStack from "./fixtures/remote/stack.ts";
27
28 /** Narrows a Containers application to the scheduler-placed shape. */
29 const scheduledApplication = (app: Containers.ContainerApplicationItem) => {
30 assert("version" in app, `${app.name} is a Durable Object application`);
31 return app;
32 };
33
34 describe.concurrent.each([
35 {
36 dev: true,
37 // The user's test stage (`test_$USER` or `--stage`), so concurrent runs by
38 // different users never share containers.
39 stage: `${Test.defaultStage()}-local`,
40 // Must cover the full readiness budget (readinessRetries × 3s ≈ 3 min)
41 // when a saturated machine keeps the local container answering 500s.
42 timeout: 300_000,
43 },
44 { dev: false, stage: Test.defaultStage(), timeout: 300_000 },
45 ])(
46 "Container (dev: $dev)",
47 ({ dev, stage, timeout }) => {
48 const state = dev ? inMemoryState() : Cloudflare.state();
49 const AsyncContainerStack = makeAsyncContainerStack(state);
50 const ExternalStack = makeExternalStack(state);
51 const InferredClassStack = makeInferredClassStack(state);
52 const RemoteStack = makeRemoteStack(state);
53
54 // We need to create a new test context for each test case because the
55 // local runner stops running after the root scope is closed, which happens
56 // in an afterAll hook registered by `Test.make`.
57 const make = (remote = false) =>
58 Test.make({
59 providers: Cloudflare.providers(),
60 state: remote ? Cloudflare.state() : state,
61 stage,
62 dev,
63 });
64
65 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
66
67 // Container image build + push + worker/DO deploy comfortably exceeds the
68 // default 120s hook budget, so give every deploy/destroy plenty of room.
69 const HOOK_TIMEOUT = 600_000;
70
71 /**
72 * Effect-native container (`main`): the entrypoint Effect is bundled into a
73 * generated image. The Durable Object proxies into the container two ways —
74 * RPC (`container.ping()` / `container.readObject()`) and HTTP over its
75 * port-3000 server (`getTcpPort(3000).fetch`). The bucket tests prove the
76 * full end-to-end R2 path: a value written through the DO's NATIVE binding is
77 * read back from inside the container over its scoped HTTP token, surfaced
78 * both via RPC and via fetch.
79 */
80 describe("effectful container (main)", { tags: ["provider:cloudflare:r2", "live"] }, () => {
81 const { test, beforeAll, afterAll, deploy, destroy } = make(true);
82 const stack = beforeAll(deploy(EffectfulStack), { timeout: HOOK_TIMEOUT });
83 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(EffectfulStack), { timeout: HOOK_TIMEOUT });
84
85 test(
86 "RPC: ping round-trips into the container",
87 Effect.gen(function* () {
88 const { url } = yield* stack;
89
90 const pong = yield* fetchReady(new URL("/ping", url), "pong");
91 expect(pong).toContain("pong");
92 }).pipe(logLevel),
93 { timeout },
94 );
95
96 test(
97 "fetch: serves over its TCP port",
98 Effect.gen(function* () {
99 const { url } = yield* stack;
100
101 const hello = yield* fetchReady(new URL("/hello", url), "effectful container");
102 expect(hello).toContain("effectful container");
103 }).pipe(logLevel),
104 { timeout },
105 );
106
107 // A container is a real process — it has no workerd bindings, so a
108 // capability's only channel into it is the binding contract's `env`.
109 // The provider used to declare that contract and silently drop the
110 // values; this pins that a `Binding.Service`'s `host.bind({ env })`
111 // lands in the container's `process.env`, resolved Output and all.
112 // Same mechanism `Prisma.Connect` rides into a container.
113 test(
114 "a Binding.Service's bound env reaches the container",
115 Effect.gen(function* () {
116 const { url, bucketName } = yield* stack;
117
118 const body = yield* fetchReady(new URL("/bound-env", url), bucketName);
119 expect(JSON.parse(body)).toEqual({ value: bucketName });
120 }).pipe(logLevel),
121 { timeout },
122 );
123
124 test(
125 "RPC: reads an R2 object from inside the container",
126 Effect.gen(function* () {
127 const { url } = yield* stack;
128
129 yield* seed(url, "rpc.txt", "hello-rpc");
130 const body = yield* fetchReady(new URL("/rpc?key=rpc.txt", url), "hello-rpc");
131 expect(body).toContain("hello-rpc");
132 }).pipe(logLevel),
133 { timeout },
134 );
135
136 test(
137 "fetch: reads an R2 object from inside the container",
138 Effect.gen(function* () {
139 const { url } = yield* stack;
140
141 yield* seed(url, "fetch.txt", "hello-fetch");
142 const body = yield* fetchReady(new URL("/fetch?key=fetch.txt", url), "hello-fetch");
143 expect(body).toContain("hello-fetch");
144 }).pipe(logLevel),
145 { timeout },
146 );
147 });
148
149 /**
150 * External container (`context` / `dockerfile`): Alchemy builds the user's
151 * Dockerfile against the context directory (nginx serving a static page on
152 * port 8080) and the DO proxies a request to it. A second container builds
153 * the same server from inline `dockerfile.content`.
154 */
155 describe("external container (context/dockerfile)", () => {
156 const { test, beforeAll, afterAll, deploy, destroy } = make();
157 const stack = beforeAll(deploy(ExternalStack), { timeout: HOOK_TIMEOUT });
158 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(ExternalStack), { timeout: HOOK_TIMEOUT });
159
160 test(
161 "builds the user Dockerfile and serves it over its TCP port",
162 Effect.gen(function* () {
163 const { url } = yield* stack;
164
165 const hello = yield* fetchReady(new URL("/hello", url), "external container");
166 expect(hello).toContain("external container");
167 }).pipe(logLevel),
168 { tags: [...(dev ? ["local"] : ["live"])], timeout },
169 );
170
171 // Inline `dockerfile.content` has no user build context: the provider
172 // materializes one, which needs the Docker service in reconcile.
173 test(
174 "builds an inline Dockerfile and serves it over its TCP port",
175 Effect.gen(function* () {
176 const { url } = yield* stack;
177
178 const hello = yield* fetchReady(new URL("/inline", url), "inline dockerfile container");
179 expect(hello).toContain("inline dockerfile container");
180 }).pipe(logLevel),
181 { tags: [...(dev ? ["local"] : ["live"])], timeout },
182 );
183 });
184
185 /**
186 * Remote container (`image`): Alchemy pulls a pre-built public image and
187 * re-pushes it to Cloudflare's registry without building anything; the DO
188 * proxies a request to it.
189 */
190 describe("remote container (image)", { tags: ["provider:cloudflare:r2"] }, () => {
191 const { test, beforeAll, afterAll, deploy, destroy } = make();
192 const stack = beforeAll(deploy(RemoteStack), { timeout: HOOK_TIMEOUT });
193 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(RemoteStack), { timeout: HOOK_TIMEOUT });
194
195 test(
196 "preserves memoryMib and serves the remote image over its TCP port",
197 Effect.gen(function* () {
198 const { url, app } = yield* stack;
199
200 expect(app.configuration.memoryMib).toBe(4096);
201 expect(app.configuration.instanceType).toBeUndefined();
202 expect(app.applicationId.startsWith("dev:")).toBe(dev);
203
204 const hello = yield* fetchReady(new URL("/hello", url), "method");
205 expect(hello).toContain("method");
206 }).pipe(logLevel),
207 { tags: [...(dev ? ["local"] : ["live"])], timeout },
208 );
209
210 // A pre-built image has no Effect runtime to inject config into, so `env`
211 // is the only channel: it lands as the ContainerApplication's
212 // `environmentVariables` and shows up as plain `process.env` entries
213 // inside the image. Covers all three value shapes — a literal, a
214 // `Redacted` (encrypted in state, plain in the container), and an Output
215 // resolved from a sibling resource (the canonical shape of a database
216 // connection string).
217 test(
218 "passes env — literal, Redacted, and sibling-resource Output — into the image",
219 Effect.gen(function* () {
220 const { url, bucketName } = yield* stack;
221 const body = yield* fetchReady(new URL("/hello", url), "DEMO_PLAIN");
222 const echoed = JSON.parse(body) as { env: Record<string, string> };
223
224 expect(echoed.env.DEMO_PLAIN).toBe(DEMO_PLAIN);
225 expect(echoed.env.DEMO_SECRET).toBe(DEMO_SECRET);
226 expect(echoed.env.DEMO_BUCKET).toBe(bucketName);
227 }).pipe(logLevel),
228 { tags: [...(dev ? ["local"] : ["live"])], timeout },
229 );
230
231 // Cloudflare reports a start it has no instance for by rejecting
232 // monitor(), not start(), so monitor() must classify it the way start()
233 // does. maxInstances is not enforced locally.
234 test.skipIf(dev)(
235 "a start past maxInstances fails monitor() with NoContainerInstanceError",
236 Effect.gen(function* () {
237 const { url } = yield* stack;
238 const startCapped = (name: string) =>
239 Effect.gen(function* () {
240 const client = yield* freshConn;
241 const response = yield* client.get(new URL(`/capped?name=${name}`, url));
242 const body = yield* response.text;
243 if (response.status !== 200) {
244 return yield* Effect.fail(
245 new Error(`capped not ready: ${response.status} ${body}`),
246 );
247 }
248 return JSON.parse(body) as { phase: string; tag: string };
249 }).pipe(
250 Effect.timeout("60 seconds"),
251 Effect.retry({ schedule: readinessSchedule, times: 6 }),
252 );
253
254 // A fresh application also has no instance while it provisions, so
255 // retry until the first object holds the one instance.
256 const first = yield* startCapped("first").pipe(
257 Effect.repeat({
258 schedule: Schedule.spaced("5 seconds"),
259 until: (result) => result.tag === "running",
260 times: 18,
261 }),
262 );
263 expect(first).toEqual({ phase: "monitor", tag: "running" });
264 const second = yield* startCapped("second");
265 expect(second).toMatchObject({
266 phase: "monitor",
267 tag: "NoContainerInstanceError",
268 });
269 }).pipe(logLevel),
270 { tags: ["live"], timeout },
271 );
272
273 // The proxy pattern from #1334 (`yield* fetch(yield* HttpServerRequest)`):
274 // worker → DO fetch handler → container port, forwarding the raw incoming
275 // request. In the live variant the incoming web Request carries an
276 // https:// URL and workerd rejects TLS on container ports ("Connecting to
277 // a container using HTTPS is not currently supported"), so this pins the
278 // runtime's scheme downgrade on the container hop; dev serves http:// and
279 // just pins the passthrough itself.
280 test(
281 "proxies the raw incoming request through the DO to the container",
282 Effect.gen(function* () {
283 const { url } = yield* stack;
284
285 // The echo image reflects the request as JSON ("method" only appears
286 // in a real echo response, never in an error page).
287 const body = yield* fetchReady(new URL("/passthrough", url), "method");
288 expect(body).toContain("method");
289 }).pipe(logLevel),
290 { tags: [...(dev ? ["local"] : ["live"])], timeout },
291 );
292 });
293 /**
294 * Container bound directly on a plain async Worker's `env` (issue #953),
295 * with the Container's logical id EQUAL to the env key
296 * (`ECHO: Container("ECHO", …)`). The Worker's two halves — the
297 * `durable_object_namespace` binding and the `containers` script metadata —
298 * describe the same env entry, so they are contributed under one `sid`.
299 * Contributing them under two (the env key and the ContainerApplication's
300 * logical id) made them collide for exactly this shape, and binding rows
301 * are collapsed by sid — last write wins — so the namespace binding was
302 * silently dropped. Live, the Worker then uploads the Container declaration
303 * itself as a `json` binding (`{"_id":"Effect","op":"alchemy/EffectClass"}`)
304 * and `getContainer` dies with `t.idFromName is not a function`; in dev the
305 * local runtime holds container metadata for a class it has no namespace
306 * for and the deploy dies with
307 * `Durable Object namespace AsyncEchoObject not found`.
308 */
309 describe("async container bound on env", () => {
310 const { test, beforeAll, afterAll, deploy, destroy } = make();
311 const stack = beforeAll(deploy(AsyncContainerStack), { timeout: HOOK_TIMEOUT });
312 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(AsyncContainerStack), {
313 timeout: HOOK_TIMEOUT,
314 });
315
316 test(
317 "binds the container class as a durable object namespace",
318 Effect.gen(function* () {
319 const { url } = yield* stack;
320
321 const body = yield* fetchReady(new URL("/binding", url), "kind");
322 expect(JSON.parse(body)).toEqual({ kind: "durable_object_namespace" });
323 }).pipe(logLevel),
324 { tags: [...(dev ? ["local"] : ["live"])], timeout },
325 );
326
327 test(
328 "serves through the container-backed DO class",
329 Effect.gen(function* () {
330 const { url } = yield* stack;
331
332 // The echo image reflects the request as JSON ("method" only appears
333 // in a real echo response, never in an error page) — proof the
334 // request went Worker → DO class → container port 8080 and back.
335 const hello = yield* fetchReady(new URL("/hello", url), "method");
336 expect(hello).toContain("method");
337 }).pipe(logLevel),
338 { tags: [...(dev ? ["local"] : ["live"])], timeout },
339 );
340 });
341 /**
342 * Issue #1321 — the same sid collision as above in the shape the
343 * Containers guide documents: `className` omitted, so the env key, the
344 * Container's logical id and the DO class are all one name
345 * (`Probe: Container("Probe", …)`). Deployed and driven over HTTP: the
346 * Worker must see a real namespace and reach the container through it.
347 */
348 describe("async container with inferred class name", () => {
349 const { test, beforeAll, afterAll, deploy, destroy } = make();
350 const stack = beforeAll(deploy(InferredClassStack), { timeout: HOOK_TIMEOUT });
351 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(InferredClassStack), {
352 timeout: HOOK_TIMEOUT,
353 });
354
355 test(
356 "binds the container class as a durable object namespace",
357 Effect.gen(function* () {
358 const { url } = yield* stack;
359
360 const body = yield* fetchReady(new URL("/binding", url), "kind");
361 expect(JSON.parse(body)).toEqual({ kind: "durable_object_namespace" });
362 }).pipe(logLevel),
363 { tags: [...(dev ? ["local"] : ["live"])], timeout },
364 );
365
366 test(
367 "serves through the container-backed DO class",
368 Effect.gen(function* () {
369 const { url } = yield* stack;
370
371 const hello = yield* fetchReady(new URL("/hello", url), "method");
372 expect(hello).toContain("method");
373 }).pipe(logLevel),
374 { tags: [...(dev ? ["local"] : ["live"])], timeout },
375 );
376 });
377 },
378 { tags: ["provider:cloudflare", "provider:cloudflare:container", "provider:cloudflare:worker"] },
379 );
380
381 describe.sequential(
382 "container attachment recovery (live)",
383 {
384 tags: [
385 "provider:cloudflare",
386 "provider:cloudflare:container",
387 "provider:cloudflare:worker",
388 "live",
389 ],
390 },
391 () => {
392 const stage = Test.defaultStage();
393 const providers = Cloudflare.providers();
394 const services = Layer.effectContext(
395 AttachmentStack.pipe(Effect.map((stack) => stack.services)),
396 ).pipe(Layer.provide(Layer.succeed(Stage, stage)));
397 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
398 providers,
399 state: Cloudflare.state(),
400 stage,
401 dev: false,
402 });
403 const stack = beforeAll(deploy(AttachmentStack), { timeout: 120_000 });
404 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(AttachmentStack), { timeout: 120_000 });
405 const workerKey = { stack: attachmentStackName, stage, fqn: "AttachmentWorker" };
406 const containerKey = { stack: attachmentStackName, stage, fqn: "AttachmentContainer" };
407
408 test(
409 "preserves the live attachment when the Worker projection and cached attachment are stale",
410 Effect.gen(function* () {
411 const first = yield* stack;
412 const namespaceId = first.app.durableObjects?.namespaceId;
413 const otherNamespaceId = first.otherApp.durableObjects?.namespaceId;
414 assert(namespaceId);
415 assert(otherNamespaceId);
416 expect(otherNamespaceId).not.toBe(namespaceId);
417 const detached = yield* attachmentStack(false).pipe(
418 Effect.flatMap((stack) => Plan.make(stack).pipe(Effect.provide(stack.services))),
419 );
420 expect(detached.resources.AttachmentContainer.action).toBe("replace");
421 const state = yield* yield* State;
422 const workerRow = yield* state.get(workerKey);
423 const containerRow = yield* state.get(containerKey);
424 assert(workerRow?.status === "created" || workerRow?.status === "updated");
425 assert(containerRow?.status === "created" || containerRow?.status === "updated");
426 for (const durableObjects of [{ namespaceId: otherNamespaceId }, undefined]) {
427 yield* Effect.gen(function* () {
428 yield* state.set({
429 ...workerKey,
430 value: { ...workerRow, attr: { ...workerRow.attr, durableObjectNamespaces: {} } },
431 });
432 yield* state.set({
433 ...containerKey,
434 value: {
435 ...containerRow,
436 attr: {
437 ...containerRow.attr,
438 durableObjects,
439 hash: { ...containerRow.attr.hash, image: "stale" },
440 },
441 },
442 });
443 const plan = yield* AttachmentStack.pipe(
444 Effect.flatMap((stack) => Plan.make(stack).pipe(Effect.provide(stack.services))),
445 );
446 expect(plan.resources.AttachmentContainer.action).toBe("update");
447 const next = yield* deploy(AttachmentStack);
448 expect(next.worker.durableObjectNamespaces).toEqual(
449 first.worker.durableObjectNamespaces,
450 );
451 expect(next.app.applicationId).toBe(first.app.applicationId);
452 expect(next.app.durableObjects).toEqual({ namespaceId });
453 const observed = yield* readAttachmentApplication(
454 first.app.accountId,
455 first.app.applicationId,
456 );
457 expect(observed.durableObjects).toEqual({ namespaceId });
458 expect(observed.maxInstances).toBe(2);
459 expect(next.otherApp.applicationId).toBe(first.otherApp.applicationId);
460 const other = yield* readAttachmentApplication(
461 first.otherApp.accountId,
462 first.otherApp.applicationId,
463 );
464 expect(other.durableObjects).toEqual({ namespaceId: otherNamespaceId });
465 expect(other.maxInstances).toBe(2);
466 const response = yield* fetchReady(new URL("/hello", next.url), "method", 8);
467 expect(JSON.parse(response).method).toBe("GET");
468 }).pipe(
469 Effect.ensuring(
470 Effect.gen(function* () {
471 const current = yield* state.get(containerKey);
472 if (
473 current &&
474 "attr" in current &&
475 current.attr?.applicationId &&
476 current.attr.applicationId !== first.app.applicationId
477 ) {
478 yield* destroyStack({ stack: AttachmentStack, stage, dev: false });
479 }
480 yield* state
481 .set({ ...workerKey, value: workerRow })
482 .pipe(
483 Effect.ensuring(
484 state.set({ ...containerKey, value: containerRow }).pipe(Effect.orDie),
485 ),
486 );
487 }).pipe(Effect.orDie),
488 ),
489 );
490 }
491 }).pipe(Effect.provide(services)),
492 { timeout: 120_000, retry: 0 },
493 );
494
495 test(
496 "keeps an attached application whose bindings were never recorded",
497 Effect.gen(function* () {
498 const first = yield* stack;
499 assert(first.app.durableObjects?.namespaceId);
500 const state = yield* yield* State;
501 const containerRow = yield* state.get(containerKey);
502 assert(containerRow?.status === "created" || containerRow?.status === "updated");
503 // What a deploy that adopted the application and then failed leaves
504 // behind: the observed application, attachment included, and no
505 // recorded bindings.
506 yield* state.set({ ...containerKey, value: { ...containerRow, bindings: [] } });
507 yield* Effect.gen(function* () {
508 const plan = yield* AttachmentStack.pipe(
509 Effect.flatMap((stack) => Plan.make(stack).pipe(Effect.provide(stack.services))),
510 );
511 expect(plan.resources.AttachmentContainer.action).not.toBe("replace");
512 const next = yield* deploy(AttachmentStack);
513 expect(next.app.applicationId).toBe(first.app.applicationId);
514 }).pipe(
515 Effect.ensuring(state.set({ ...containerKey, value: containerRow }).pipe(Effect.orDie)),
516 );
517 }).pipe(Effect.provide(services)),
518 { timeout: 120_000, retry: 0 },
519 );
520
521 test(
522 "recovers an interrupted create with an unresolved Worker attachment",
523 Effect.gen(function* () {
524 const first = yield* stack;
525 const namespaceId = first.app.durableObjects?.namespaceId;
526 assert(namespaceId);
527 const state = yield* yield* State;
528 const workerRow = yield* state.get(workerKey);
529 const containerRow = yield* state.get(containerKey);
530 assert(workerRow?.status === "created" || workerRow?.status === "updated");
531 assert(containerRow?.status === "created" || containerRow?.status === "updated");
532 const interrupted = { ...containerRow, status: "creating" as const, attr: undefined };
533 yield* Effect.gen(function* () {
534 yield* state.set({
535 ...workerKey,
536 value: { ...workerRow, attr: { ...workerRow.attr, durableObjectNamespaces: {} } },
537 });
538 yield* state.set({ ...containerKey, value: interrupted });
539 const plan = yield* AttachmentStack.pipe(
540 Effect.flatMap((stack) => Plan.make(stack).pipe(Effect.provide(stack.services))),
541 );
542 expect(plan.resources.AttachmentContainer).toMatchObject({
543 action: "create",
544 state: {
545 instanceId: containerRow.instanceId,
546 attr: {
547 applicationId: first.app.applicationId,
548 applicationName: first.app.applicationName,
549 durableObjects: { namespaceId },
550 },
551 },
552 });
553 expect(yield* state.get(containerKey)).toEqual(interrupted);
554 const recovered = yield* deploy(AttachmentStack);
555 expect(recovered.app.applicationId).toBe(first.app.applicationId);
556 expect(recovered.app.durableObjects).toEqual({ namespaceId });
557 expect(recovered.worker.durableObjectNamespaces).toEqual(
558 first.worker.durableObjectNamespaces,
559 );
560 const committed = yield* state.get(containerKey);
561 assert(committed?.status === "created" || committed?.status === "updated");
562 expect(committed).toMatchObject({
563 instanceId: containerRow.instanceId,
564 attr: { applicationId: first.app.applicationId, durableObjects: { namespaceId } },
565 });
566 const observed = yield* readAttachmentApplication(
567 first.app.accountId,
568 first.app.applicationId,
569 );
570 expect(observed.durableObjects).toEqual({ namespaceId });
571 const other = yield* readAttachmentApplication(
572 first.otherApp.accountId,
573 first.otherApp.applicationId,
574 );
575 expect(other.durableObjects).toEqual(first.otherApp.durableObjects);
576 expect(recovered.otherApp.applicationId).toBe(first.otherApp.applicationId);
577 const response = yield* fetchReady(new URL("/hello", recovered.url), "method", 8);
578 expect(JSON.parse(response).method).toBe("GET");
579 }).pipe(
580 Effect.ensuring(
581 Effect.gen(function* () {
582 const current = yield* state.get(containerKey);
583 if (
584 current &&
585 "attr" in current &&
586 current.attr?.applicationId &&
587 current.attr.applicationId !== first.app.applicationId
588 ) {
589 yield* destroyStack({ stack: AttachmentStack, stage, dev: false });
590 }
591 yield* state
592 .set({ ...workerKey, value: workerRow })
593 .pipe(
594 Effect.ensuring(
595 state.set({ ...containerKey, value: containerRow }).pipe(Effect.orDie),
596 ),
597 );
598 }).pipe(Effect.orDie),
599 ),
600 );
601 }).pipe(Effect.provide(services)),
602 { timeout: 120_000, retry: 0 },
603 );
604
605 test(
606 "recovers a missing application only with its recorded exact attachment",
607 Effect.gen(function* () {
608 const first = yield* stack;
609 const namespaceId = first.app.durableObjects?.namespaceId;
610 assert(namespaceId);
611 const state = yield* yield* State;
612 const workerRow = yield* state.get(workerKey);
613 const containerRow = yield* state.get(containerKey);
614 assert(workerRow?.status === "created" || workerRow?.status === "updated");
615 assert(containerRow?.status === "created" || containerRow?.status === "updated");
616 const corruptProjection = (durableObjects: { namespaceId?: string } | undefined) =>
617 Effect.gen(function* () {
618 yield* state.set({
619 ...workerKey,
620 value: { ...workerRow, attr: { ...workerRow.attr, durableObjectNamespaces: {} } },
621 });
622 yield* state.set({
623 ...containerKey,
624 value: {
625 ...containerRow,
626 attr: {
627 ...containerRow.attr,
628 durableObjects,
629 hash: { ...containerRow.attr.hash, image: "stale" },
630 },
631 },
632 });
633 });
634 yield* Effect.gen(function* () {
635 yield* Containers.deleteContainerApplication({
636 accountId: first.app.accountId,
637 applicationId: first.app.applicationId,
638 });
639 const remaining = yield* Containers.listContainerApplications({
640 accountId: first.app.accountId,
641 }).pipe(
642 Effect.repeat({
643 schedule: Schedule.spaced("1 second"),
644 until: (apps) => apps.every((app) => app.id !== first.app.applicationId),
645 times: 8,
646 }),
647 );
648 expect(remaining.some((app) => app.id === first.app.applicationId)).toBe(false);
649 // Reconcile observes by id first; list deletion can propagate earlier.
650 const deleted = yield* Containers.getContainerApplication({
651 accountId: first.app.accountId,
652 applicationId: first.app.applicationId,
653 }).pipe(
654 Effect.tap((app) =>
655 Effect.logInfo(
656 `Deleted container application ${app.id} is absent from list but still readable by id`,
657 ),
658 ),
659 Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)),
660 Effect.repeat({
661 schedule: Schedule.spaced("1 second"),
662 until: (app) => app === undefined,
663 times: 8,
664 }),
665 );
666 expect(deleted).toBeUndefined();
667 for (const missing of [undefined, {}]) {
668 yield* corruptProjection(missing);
669 const failed = yield* deploy(AttachmentStack).pipe(Effect.exit);
670 assert(Exit.isFailure(failed));
671 expect(Cause.pretty(failed.cause)).toContain("unresolved Durable Object namespace");
672 const applications = yield* Containers.listContainerApplications({
673 accountId: first.app.accountId,
674 });
675 expect(applications.some((app) => app.name === first.app.applicationName)).toBe(false);
676 }
677 yield* corruptProjection({ namespaceId });
678 const recovered = yield* deploy(AttachmentStack);
679 expect(recovered.worker.durableObjectNamespaces).toEqual(
680 first.worker.durableObjectNamespaces,
681 );
682 expect(recovered.app.applicationId).not.toBe(first.app.applicationId);
683 expect(recovered.app.durableObjects).toEqual({ namespaceId });
684 const observed = yield* readAttachmentApplication(
685 first.app.accountId,
686 recovered.app.applicationId,
687 );
688 expect(observed.durableObjects).toEqual({ namespaceId });
689 expect(observed.maxInstances).toBe(2);
690 const other = yield* readAttachmentApplication(
691 first.otherApp.accountId,
692 first.otherApp.applicationId,
693 );
694 expect(other.durableObjects).toEqual(first.otherApp.durableObjects);
695 const response = yield* fetchReady(new URL("/hello", recovered.url), "method", 8);
696 expect(JSON.parse(response).method).toBe("GET");
697 }).pipe(
698 Effect.ensuring(
699 Effect.gen(function* () {
700 // Delete the replacement while its current identity is still persisted.
701 yield* destroyStack({ stack: AttachmentStack, stage, dev: false });
702 yield* Effect.gen(function* () {
703 const remaining = yield* Containers.listContainerApplications({
704 accountId: first.app.accountId,
705 }).pipe(
706 Effect.repeat({
707 schedule: Schedule.spaced("1 second"),
708 until: (apps) =>
709 apps.every(
710 (app) =>
711 app.name !== first.app.applicationName &&
712 app.name !== first.otherApp.applicationName,
713 ),
714 times: 8,
715 }),
716 );
717 expect(
718 remaining.some(
719 (app) =>
720 app.name === first.app.applicationName ||
721 app.name === first.otherApp.applicationName,
722 ),
723 ).toBe(false);
724 }).pipe(
725 Effect.ensuring(
726 state
727 .set({ ...workerKey, value: workerRow })
728 .pipe(
729 Effect.ensuring(
730 state.set({ ...containerKey, value: containerRow }).pipe(Effect.orDie),
731 ),
732 Effect.orDie,
733 ),
734 ),
735 );
736 }).pipe(Effect.orDie),
737 ),
738 );
739 }).pipe(Effect.provide(services)),
740 { timeout: 120_000, retry: 0 },
741 );
742
743 // Runs last: detaching recreates the application under a new id.
744 test(
745 "detaches in place an attached application whose bindings were never recorded",
746 Effect.gen(function* () {
747 // The previous case destroys the stack but restores its state rows.
748 yield* destroyStack({ stack: AttachmentStack, stage, dev: false });
749 const first = yield* deploy(AttachmentStack);
750 assert(first.app.durableObjects?.namespaceId);
751 const state = yield* yield* State;
752 const containerRow = yield* state.get(containerKey);
753 assert(containerRow?.status === "created" || containerRow?.status === "updated");
754 yield* state.set({ ...containerKey, value: { ...containerRow, bindings: [] } });
755 yield* Effect.gen(function* () {
756 // The observed attachment must not turn a detach into a replace:
757 // the new generation would re-adopt a named application by name
758 // and the old generation's delete would then remove it.
759 const plan = yield* attachmentStack(false).pipe(
760 Effect.flatMap((stack) => Plan.make(stack).pipe(Effect.provide(stack.services))),
761 );
762 expect(plan.resources.AttachmentContainer.action).toBe("update");
763 const next = yield* deploy(attachmentStack(false));
764 expect(next.app.applicationName).toBe(first.app.applicationName);
765 expect(next.app.durableObjects).toBeUndefined();
766 const observed = yield* readAttachmentApplication(
767 next.app.accountId,
768 next.app.applicationId,
769 );
770 expect(observed.durableObjects).toBeUndefined();
771 }).pipe(
772 Effect.ensuring(
773 destroyStack({ stack: AttachmentStack, stage, dev: false }).pipe(Effect.orDie),
774 ),
775 );
776 }).pipe(Effect.provide(services)),
777 { timeout: 180_000, retry: 0 },
778 );
779 },
780 );
781
782 describe.sequential(
783 "named container attachment (live)",
784 {
785 tags: [
786 "provider:cloudflare",
787 "provider:cloudflare:container",
788 "provider:cloudflare:worker",
789 "live",
790 ],
791 },
792 () => {
793 const stage = Test.defaultStage();
794 const name = `alchemy-named-attachment-${stage}`;
795 const NamedStack = attachmentStack(true, name);
796 const services = Layer.effectContext(
797 NamedStack.pipe(Effect.map((stack) => stack.services)),
798 ).pipe(Layer.provide(Layer.succeed(Stage, stage)));
799 const { test, deploy } = Test.make({
800 providers: Cloudflare.providers(),
801 state: Cloudflare.state(),
802 stage,
803 dev: false,
804 });
805
806 test(
807 "re-attaches a named application after detaching it",
808 Effect.gen(function* () {
809 yield* destroyStack({ stack: NamedStack, stage, dev: false });
810 yield* Effect.gen(function* () {
811 const first = yield* deploy(NamedStack);
812 expect(first.app.applicationName).toBe(name);
813 assert(first.app.durableObjects?.namespaceId);
814
815 const detached = yield* deploy(attachmentStack(false, name));
816 expect(detached.app.applicationName).toBe(name);
817 expect(detached.app.durableObjects).toBeUndefined();
818 const observedDetached = yield* readAttachmentApplication(
819 detached.app.accountId,
820 detached.app.applicationId,
821 );
822 expect(observedDetached.name).toBe(name);
823 expect(observedDetached.durableObjects).toBeUndefined();
824
825 const attached = yield* deploy(attachmentStack(true, name));
826 expect(attached.app.applicationName).toBe(name);
827 assert(attached.app.durableObjects?.namespaceId);
828 const observedAttached = yield* readAttachmentApplication(
829 attached.app.accountId,
830 attached.app.applicationId,
831 );
832 expect(observedAttached.name).toBe(name);
833 expect(observedAttached.durableObjects?.namespaceId).toBe(
834 attached.app.durableObjects.namespaceId,
835 );
836 }).pipe(
837 Effect.ensuring(
838 destroyStack({ stack: NamedStack, stage, dev: false }).pipe(Effect.orDie),
839 ),
840 );
841 }).pipe(Effect.provide(services)),
842 { timeout: 240_000, retry: 0 },
843 );
844 },
845 );
846
847 const readAttachmentApplication = (accountId: string, applicationId: string) =>
848 Containers.getContainerApplication({ accountId, applicationId }).pipe(
849 Effect.map(scheduledApplication),
850 Effect.retry({
851 while: (error) => error._tag === "ContainerApplicationNotFound",
852 schedule: Schedule.spaced("1 second"),
853 times: 8,
854 }),
855 );
856
857 // Note on image choice for the non-Effect (`image`/`dockerfile`) variants:
858 // stock nginx images crash-loop inside Cloudflare's container sandbox because
859 // they symlink /var/log/nginx/{access,error}.log to /dev/stdout and
860 // /dev/stderr, and opening those device paths fails with ENXIO (errno 6) — the
861 // nginx master aborts with `[emerg] could not open error log file` before it
862 // ever binds the port, so getTcpPort() can never connect. The fixtures work
863 // around this two ways: the `external` Dockerfile (which we own) replaces the
864 // log symlinks with real files, and the `remote` variant uses a server image
865 // (`mendhak/http-https-echo`) that writes to its inherited stdout fd directly
866 // instead of opening the /dev/std* paths as files.
867
868 // Cap exponential backoff at 3s — keeps the fast path snappy but stops the
869 // geometric blow-up from dominating wall time when CF edge is slow.
870 const readinessSchedule = Schedule.min([
871 Schedule.exponential("500 millis"),
872 Schedule.spaced("3 seconds"),
873 ]);
874 // ~3 min of 3s-spaced polls: a saturated full-suite run (many concurrent
875 // docker builds/pushes on this machine) can keep a dev container answering
876 // 500 well past the ~90s that 30 retries buys. Failing attempts return
877 // fast, so the extra budget only matters on the slow path.
878 const readinessRetries = 60;
879
880 // While a freshly pre-created worker propagates, Cloudflare's edge serves
881 // Alchemy's pre-create stub (200 with this body); any poll that sees it retries.
882 const DEPLOY_PLACEHOLDER = "Alchemy worker is being deployed...";
883
884 // Force `Connection: close` so each readiness attempt opens a fresh connection
885 // and can land on an edge that already has the new deploy (a pooled keep-alive
886 // socket stays pinned to one edge metal and can keep reading the stale body).
887 const freshConn = HttpClient.HttpClient.pipe(
888 Effect.map(HttpClient.mapRequest(HttpClientRequest.setHeader("connection", "close"))),
889 );
890
891 // Retry a freshly-deployed worker route until it answers 200 with a body that
892 // contains `expected` — rejecting both transient non-200s and the deploy stub.
893 // Each attempt is bounded so a worker that is hung waiting on its container
894 // surfaces as a retryable failure rather than blocking the whole test budget.
895 const fetchReady = (url: URL, expected: string, retries = readinessRetries) =>
896 Effect.gen(function* () {
897 const client = yield* freshConn;
898 return yield* client.get(url).pipe(
899 Effect.flatMap((r) =>
900 r.text.pipe(
901 Effect.flatMap((body) =>
902 r.status !== 200
903 ? Effect.fail(new Error(`Worker not ready: ${r.status} ${body}`))
904 : body.includes(DEPLOY_PLACEHOLDER) || !body.includes(expected)
905 ? Effect.fail(new Error(`not ready: got ${body}`))
906 : Effect.succeed(body),
907 ),
908 ),
909 ),
910 Effect.timeout("10 seconds"),
911 Effect.retry({ schedule: readinessSchedule, times: retries }),
912 );
913 });
914
915 // Seed a value into R2 via the worker's `/seed` route (DO native binding),
916 // retrying through cold-start until the producer accepts it (200).
917 const seed = (url: string, key: string, value: string) =>
918 Effect.gen(function* () {
919 const client = yield* freshConn;
920 return yield* client
921 .execute(
922 HttpClientRequest.put(new URL(`/seed?key=${encodeURIComponent(key)}`, url)).pipe(
923 HttpClientRequest.bodyText(value),
924 ),
925 )
926 .pipe(
927 Effect.flatMap((r) =>
928 r.status === 200
929 ? Effect.succeed(r)
930 : Effect.fail(new Error(`seed not ready: ${r.status}`)),
931 ),
932 Effect.timeout("10 seconds"),
933 Effect.retry({ schedule: readinessSchedule, times: readinessRetries }),
934 );
935 });