The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { 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 * as Workers from "@distilled.cloud/cloudflare/workers"; |
| 6 | import * as Cause from "effect/Cause"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Exit from "effect/Exit"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { |
| 12 | BuildInputs, |
| 13 | type BuildInputSet, |
| 14 | buildSecretsStack, |
| 15 | } from "./fixtures/buildsecrets/stack.ts"; |
| 16 | import { GuardWorker } from "./fixtures/native/guard-worker.ts"; |
| 17 | import { policyStack, settingsStack, TEST_SSH_PUBLIC_KEY } from "./fixtures/native/settings.ts"; |
| 18 | import { |
| 19 | directEnvStack, |
| 20 | growingStack, |
| 21 | NativeImages, |
| 22 | nativeStack, |
| 23 | } from "./fixtures/native/stack.ts"; |
| 24 | |
| 25 | /** What `/probe` reports about the running container (fixtures/native). */ |
| 26 | interface Probe { |
| 27 | wasRunning: boolean; |
| 28 | /** The image reference the object asked to start. */ |
| 29 | configured: string; |
| 30 | /** The image reference the runtime reports as running. */ |
| 31 | inspected: string | undefined; |
| 32 | /** `/etc/alpine-release` inside the container. */ |
| 33 | release: string; |
| 34 | /** Contents of the seeded file, "" when absent. */ |
| 35 | file: string; |
| 36 | /** The Durable Object storage marker, null when absent. */ |
| 37 | stored: string | null; |
| 38 | } |
| 39 | |
| 40 | interface Metadata { |
| 41 | id: string; |
| 42 | incarnation: string; |
| 43 | images: Record<string, string>; |
| 44 | stored: string | null; |
| 45 | } |
| 46 | |
| 47 | interface ExecResult { |
| 48 | stdout: string; |
| 49 | exitCode: number; |
| 50 | /** `/etc/alpine-release` of the running image, "" on non-Alpine images. */ |
| 51 | release: string; |
| 52 | /** Sorted keys of `ctx.container.images`: the ids of the class's Containers. */ |
| 53 | images: string[]; |
| 54 | } |
| 55 | |
| 56 | const EFFECT_IMAGES = ["NativeShell", "NativeTools"]; |
| 57 | const ASYNC_IMAGES = ["AsyncShell", "AsyncTools"]; |
| 58 | // The second class on each Worker shares the Shell Container. |
| 59 | const EFFECT_REVIEWER_IMAGES = ["NativeLint", "NativeShell"]; |
| 60 | const ASYNC_REVIEWER_IMAGES = ["AsyncLint", "AsyncShell"]; |
| 61 | |
| 62 | /** A failed deploy's cause mentions the typed settings conflict. */ |
| 63 | const expectSettingsConflict = (exit: Exit.Exit<unknown, unknown>) => { |
| 64 | expect(Exit.isFailure(exit)).toBe(true); |
| 65 | if (Exit.isFailure(exit)) { |
| 66 | const pretty = Cause.pretty(exit.cause); |
| 67 | expect(pretty).toContain("ContainerApplicationSettingsConflict"); |
| 68 | expect(pretty).toContain("declare different 'observability'"); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | /** GET a route; a non-2xx response fails with its status and body. */ |
| 73 | const getText = (baseUrl: string, path: string) => |
| 74 | Effect.gen(function* () { |
| 75 | const response = yield* HttpClient.get(new URL(path, baseUrl)); |
| 76 | const body = yield* response.text; |
| 77 | if (response.status < 200 || response.status >= 300) { |
| 78 | return yield* Effect.fail(new Error(`GET ${path} failed (${response.status}): ${body}`)); |
| 79 | } |
| 80 | return body; |
| 81 | }); |
| 82 | |
| 83 | const getJson = <A>(baseUrl: string, path: string) => |
| 84 | Effect.map(getText(baseUrl, path), (body) => JSON.parse(body) as A); |
| 85 | |
| 86 | /** Fail with the route that hung, so a timeout says which step stalled. */ |
| 87 | const within = (path: string, duration: `${number} seconds`) => |
| 88 | Effect.timeoutOrElse({ |
| 89 | duration, |
| 90 | orElse: () => Effect.fail(new Error(`GET ${path} timed out after ${duration}`)), |
| 91 | }); |
| 92 | |
| 93 | /** |
| 94 | * A redeploy reaches requests asynchronously (a new local workerd, or a new |
| 95 | * Worker version on Cloudflare). Wait until the object observes the deployed |
| 96 | * image set, which the fixtures expose as their `IMAGE_REVISION`. |
| 97 | */ |
| 98 | const waitForRevision = (url: string, images: object, query = "") => { |
| 99 | const expected = JSON.stringify(images); |
| 100 | return getText(url, `/revision${query}`).pipe( |
| 101 | Effect.repeat({ |
| 102 | schedule: Schedule.spaced("1 second"), |
| 103 | until: (revision) => revision === expected, |
| 104 | times: 20, |
| 105 | }), |
| 106 | within(`/revision${query}`, "30 seconds"), |
| 107 | Effect.tap((revision) => Effect.sync(() => expect(revision).toBe(expected))), |
| 108 | ); |
| 109 | }; |
| 110 | |
| 111 | /** A fresh workers.dev URL needs a few seconds before it routes requests. */ |
| 112 | const waitUntilReady = (url: string) => |
| 113 | getText(url, "/ready").pipe( |
| 114 | Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 10 }), |
| 115 | within("/ready", "25 seconds"), |
| 116 | ); |
| 117 | |
| 118 | /** |
| 119 | * Probes start, restart, and write to containers, so they are never retried. |
| 120 | * Query flags: `seed` writes the marker file and storage, `restart` destroys |
| 121 | * the running container first, `image=<name>` picks the image to start. |
| 122 | */ |
| 123 | const probe = (url: string, query = "") => |
| 124 | getJson<Probe>(url, `/probe${query}`).pipe(within(`/probe${query}`, "25 seconds")); |
| 125 | |
| 126 | const readMetadata = (url: string, query = "") => |
| 127 | getJson<Metadata>(url, `/metadata${query}`).pipe(within(`/metadata${query}`, "10 seconds")); |
| 128 | |
| 129 | /** The image keys a Worker version uploaded for one Durable Object class. */ |
| 130 | const uploadedImageKeys = Effect.fn(function* ( |
| 131 | accountId: string, |
| 132 | scriptName: string, |
| 133 | className: string, |
| 134 | ) { |
| 135 | const containers = yield* uploadedContainers(accountId, scriptName); |
| 136 | const entries = containers?.filter((container) => container.className === className) ?? []; |
| 137 | // One class has exactly one merged entry. |
| 138 | expect(entries).toHaveLength(1); |
| 139 | return Object.keys(entries[0]?.images ?? {}).sort(); |
| 140 | }); |
| 141 | |
| 142 | /** The `containers` metadata of a Worker's most recently uploaded version. */ |
| 143 | const uploadedContainers = Effect.fn(function* (accountId: string, scriptName: string) { |
| 144 | const versions = yield* Workers.listScriptVersions({ accountId, scriptName }); |
| 145 | const latest = versions.items?.toSorted((a, b) => (b.number ?? 0) - (a.number ?? 0))[0]; |
| 146 | if (!latest?.id) { |
| 147 | return yield* Effect.die(`Worker ${scriptName} has no uploaded version.`); |
| 148 | } |
| 149 | const version = yield* Workers.getScriptVersion({ |
| 150 | accountId, |
| 151 | scriptName, |
| 152 | versionId: latest.id, |
| 153 | }); |
| 154 | return version.resources.scriptRuntime?.containers; |
| 155 | }); |
| 156 | |
| 157 | const waitForApplicationDeleted = (accountId: string, applicationId: string) => |
| 158 | Containers.getContainerApplication({ accountId, applicationId }).pipe( |
| 159 | Effect.catchTag("ContainerApplicationNotFound", () => Effect.succeed(undefined)), |
| 160 | Effect.repeat({ |
| 161 | schedule: Schedule.spaced("1 second"), |
| 162 | until: (application) => application === undefined, |
| 163 | times: 8, |
| 164 | }), |
| 165 | ); |
| 166 | |
| 167 | for (const dev of [true, false]) { |
| 168 | describe( |
| 169 | `Durable Object containers (dev: ${dev})`, |
| 170 | { |
| 171 | tags: ["provider:cloudflare", "provider:cloudflare:container", dev ? "local" : "live"], |
| 172 | // The file's dev sidecar keys running Workers by FQN, so concurrent dev |
| 173 | // stacks declaring the same Worker would restart each other's instance. |
| 174 | sequential: dev, |
| 175 | }, |
| 176 | () => { |
| 177 | const { test } = Test.make({ |
| 178 | providers: Cloudflare.providers(), |
| 179 | dev, |
| 180 | stage: `${Test.defaultStage()}-native-${dev ? "local" : "live"}`, |
| 181 | }); |
| 182 | |
| 183 | // TODO: Investigate image-map propagation after same-key image updates. |
| 184 | // Live Alpine 3.21 -> 3.20 updates uploaded the correct Worker metadata, |
| 185 | // but existing DOs still exposed the old ctx.container.images map even |
| 186 | // after observing the new environment revision. Fresh DOs were mixed: |
| 187 | // the Effect fixture saw the new map, while the async fixture saw the old. |
| 188 | // abort() demonstrably recreated both original DOs (new incarnation, |
| 189 | // same DO ID and preserved storage), but refreshed only the async map. |
| 190 | // Alpine <-> Debian passed locally and live, including external-image |
| 191 | // publication. Local Alpine replacement also passed. No root cause yet; |
| 192 | // eviction alone is not a reliable remedy. Keep these assertions intact. |
| 193 | (dev ? test.provider : test.provider.todo)( |
| 194 | "preserves state across redeploys and starts each Container's replaced image", |
| 195 | (stack) => |
| 196 | Effect.gen(function* () { |
| 197 | yield* stack.destroy(); |
| 198 | |
| 199 | const initialImages = { shell: "alpine:3.21", tools: "alpine:3.20" }; |
| 200 | // Swap the two Containers' images. |
| 201 | const replacementImages = { shell: "alpine:3.20", tools: "alpine:3.21" }; |
| 202 | const stackWith = (images: typeof initialImages) => |
| 203 | nativeStack.pipe(Effect.provideService(NativeImages, images)); |
| 204 | |
| 205 | /** Seed state into each object, and check both images start. */ |
| 206 | const seed = Effect.fn(function* (url: string) { |
| 207 | const shell = yield* probe(url, "?seed"); |
| 208 | expect(shell.release).toMatch(/^3\.21\./); |
| 209 | expect(shell.file).toBe("writable"); |
| 210 | expect(shell.stored).toBe("durable"); |
| 211 | |
| 212 | const tools = yield* probe(url, "?restart&image=tools"); |
| 213 | expect(tools.release).toMatch(/^3\.20\./); |
| 214 | expect(tools.file).toBe(""); |
| 215 | expect(tools.stored).toBe("durable"); |
| 216 | |
| 217 | // Leave the shell image running with the seeded file. |
| 218 | yield* probe(url, "?restart&seed"); |
| 219 | }); |
| 220 | |
| 221 | const verifyReplacedImages = Effect.fn(function* ( |
| 222 | worker: { url: string | undefined; workerName: string }, |
| 223 | shellImage: string | undefined, |
| 224 | ) { |
| 225 | const url = worker.url!; |
| 226 | |
| 227 | // Wait for the new Worker version to reach this object. |
| 228 | const expectedRevision = JSON.stringify(replacementImages); |
| 229 | const revision = yield* getText(url, "/revision").pipe( |
| 230 | Effect.repeat({ |
| 231 | schedule: Schedule.spaced("1 second"), |
| 232 | until: (value) => value === expectedRevision, |
| 233 | times: 8, |
| 234 | }), |
| 235 | Effect.timeout("15 seconds"), |
| 236 | ); |
| 237 | expect(revision).toBe(expectedRevision); |
| 238 | |
| 239 | const existing = yield* readMetadata(url); |
| 240 | const fresh = yield* readMetadata(url, "?object=fresh-after-update"); |
| 241 | yield* Effect.logInfo("Image metadata before eviction", { |
| 242 | worker: worker.workerName, |
| 243 | expected: shellImage, |
| 244 | existing, |
| 245 | fresh, |
| 246 | }); |
| 247 | |
| 248 | // abort() fails the request by design; a new incarnation on |
| 249 | // the next read proves the object was evicted. |
| 250 | yield* getText(url, "/evict").pipe(Effect.ignore); |
| 251 | const evicted = yield* readMetadata(url).pipe( |
| 252 | Effect.retry({ |
| 253 | schedule: Schedule.spaced("1 second"), |
| 254 | times: 3, |
| 255 | }), |
| 256 | ); |
| 257 | expect(evicted.id).toBe(existing.id); |
| 258 | expect(evicted.incarnation).not.toBe(existing.incarnation); |
| 259 | expect(evicted.stored).toBe("durable"); |
| 260 | expect(fresh.id).not.toBe(existing.id); |
| 261 | expect(fresh.stored).toBeNull(); |
| 262 | |
| 263 | // Updating declarations must not replace a running container. |
| 264 | // Local hot reload restarts workerd, so only production keeps |
| 265 | // the running container. |
| 266 | const beforeRestart = yield* probe(url); |
| 267 | expect(beforeRestart.stored).toBe("durable"); |
| 268 | if (!dev) { |
| 269 | expect(beforeRestart.configured).toBe(shellImage); |
| 270 | expect(beforeRestart).toMatchObject({ |
| 271 | wasRunning: true, |
| 272 | release: expect.stringMatching(/^3\.21\./), |
| 273 | file: "writable", |
| 274 | stored: "durable", |
| 275 | }); |
| 276 | } |
| 277 | |
| 278 | // A restart picks up the Container's replaced image. |
| 279 | const afterRestart = yield* probe(url, "?restart"); |
| 280 | if (!dev) { |
| 281 | expect(afterRestart.inspected).toBe(afterRestart.configured); |
| 282 | } |
| 283 | expect(afterRestart.release).toMatch(/^3\.20\./); |
| 284 | expect(afterRestart.file).toBe(""); |
| 285 | expect(afterRestart.stored).toBe("durable"); |
| 286 | |
| 287 | const tools = yield* probe(url, "?restart&image=tools"); |
| 288 | expect(tools.release).toMatch(/^3\.21\./); |
| 289 | expect(tools.stored).toBe("durable"); |
| 290 | }); |
| 291 | |
| 292 | yield* Effect.gen(function* () { |
| 293 | const initial = yield* stack.deploy(stackWith(initialImages)); |
| 294 | const initialWorkers = [initial.worker, initial.asyncWorker]; |
| 295 | for (const worker of initialWorkers) { |
| 296 | yield* waitUntilReady(worker.url!); |
| 297 | } |
| 298 | yield* Effect.forEach(initialWorkers, (worker) => seed(worker.url!), { |
| 299 | concurrency: 2, |
| 300 | }); |
| 301 | |
| 302 | // An unchanged redeploy keeps the running container and state. |
| 303 | const unchanged = yield* stack.deploy(stackWith(initialImages)); |
| 304 | for (const worker of [unchanged.worker, unchanged.asyncWorker]) { |
| 305 | expect(yield* probe(worker.url!)).toMatchObject({ |
| 306 | wasRunning: true, |
| 307 | release: expect.stringMatching(/^3\.21\./), |
| 308 | file: "writable", |
| 309 | stored: "durable", |
| 310 | }); |
| 311 | } |
| 312 | |
| 313 | // Replacing images exercises external-image publication on update. |
| 314 | const changed = yield* stack.deploy(stackWith(replacementImages)); |
| 315 | expect(changed.application.applicationId).toBe(initial.application.applicationId); |
| 316 | expect(changed.asyncApplication.applicationId).toBe( |
| 317 | initial.asyncApplication.applicationId, |
| 318 | ); |
| 319 | // Local bindings expose Container ids; cloud bindings expose digests. |
| 320 | if (!dev) { |
| 321 | expect(changed.shell.image).not.toBe(initial.shell.image); |
| 322 | expect(changed.tools.image).not.toBe(initial.tools.image); |
| 323 | } |
| 324 | yield* Effect.all( |
| 325 | [ |
| 326 | verifyReplacedImages(changed.worker, changed.shell.image), |
| 327 | verifyReplacedImages(changed.asyncWorker, changed.asyncShell.image), |
| 328 | ], |
| 329 | { concurrency: 2 }, |
| 330 | ); |
| 331 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))); |
| 332 | }), |
| 333 | { timeout: 120_000, retry: 0 }, |
| 334 | ); |
| 335 | |
| 336 | test.provider( |
| 337 | "rejects a Durable Object-managed Container placed directly in env", |
| 338 | (stack) => |
| 339 | Effect.gen(function* () { |
| 340 | yield* stack.destroy(); |
| 341 | const exit = yield* stack.deploy(directEnvStack).pipe(Effect.exit); |
| 342 | expect(Exit.isFailure(exit)).toBe(true); |
| 343 | if (Exit.isFailure(exit)) { |
| 344 | const pretty = Cause.pretty(exit.cause); |
| 345 | expect(pretty).toContain("DurableObjectContainerBindingError"); |
| 346 | expect(pretty).toContain( |
| 347 | 'Cloudflare.DurableObject("SANDBOX", { containers: [AsyncShell] })', |
| 348 | ); |
| 349 | } |
| 350 | yield* stack.destroy(); |
| 351 | }), |
| 352 | { timeout: 60_000, retry: 0 }, |
| 353 | ); |
| 354 | |
| 355 | test.provider( |
| 356 | "rejects containers on the binding of an Effect-native Durable Object class", |
| 357 | (stack) => |
| 358 | Effect.gen(function* () { |
| 359 | yield* stack.destroy(); |
| 360 | const exit = yield* stack |
| 361 | .deploy( |
| 362 | Effect.gen(function* () { |
| 363 | return yield* GuardWorker; |
| 364 | }), |
| 365 | ) |
| 366 | .pipe(Effect.exit); |
| 367 | expect(Exit.isFailure(exit)).toBe(true); |
| 368 | if (Exit.isFailure(exit)) { |
| 369 | const pretty = Cause.pretty(exit.cause); |
| 370 | expect(pretty).toContain("DurableObjectContainersOnEffectNativeClass"); |
| 371 | expect(pretty).toContain("yield*"); |
| 372 | } |
| 373 | yield* stack.destroy(); |
| 374 | }), |
| 375 | { timeout: 120_000, retry: 0 }, |
| 376 | ); |
| 377 | |
| 378 | // Settings conflicts fail the plan, before anything is published. |
| 379 | test.provider( |
| 380 | "rejects Containers of one class that declare different application settings", |
| 381 | (stack) => |
| 382 | Effect.gen(function* () { |
| 383 | yield* stack.destroy(); |
| 384 | const conflicting = yield* stack |
| 385 | .deploy( |
| 386 | settingsStack( |
| 387 | { observability: { logs: { enabled: true } } }, |
| 388 | { observability: { logs: { enabled: false } } }, |
| 389 | ), |
| 390 | ) |
| 391 | .pipe(Effect.exit); |
| 392 | expectSettingsConflict(conflicting); |
| 393 | yield* stack.destroy(); |
| 394 | }), |
| 395 | // The deploy fails fast but returns only once its sibling Containers' |
| 396 | // images finish building and pushing, which can take minutes. |
| 397 | { timeout: 180_000, retry: 0 }, |
| 398 | ); |
| 399 | |
| 400 | if (!dev) { |
| 401 | const observe = (application: { accountId: string; applicationId: string }) => |
| 402 | Containers.getContainerApplication({ |
| 403 | accountId: application.accountId, |
| 404 | applicationId: application.applicationId, |
| 405 | }); |
| 406 | |
| 407 | test.provider( |
| 408 | "manages SSH, authorized keys, and observability declared by any of the class's Containers", |
| 409 | (stack) => |
| 410 | Effect.gen(function* () { |
| 411 | yield* stack.destroy(); |
| 412 | yield* Effect.gen(function* () { |
| 413 | // `ssh`/`authorizedKeys` on one Container, `observability` on |
| 414 | // the other: the class's application merges both. |
| 415 | const declared = yield* stack.deploy( |
| 416 | settingsStack( |
| 417 | { |
| 418 | ssh: { enabled: true, port: 22 }, |
| 419 | authorizedKeys: [{ name: "alchemy-test", publicKey: TEST_SSH_PUBLIC_KEY }], |
| 420 | }, |
| 421 | { observability: { logs: { enabled: true } } }, |
| 422 | ), |
| 423 | ); |
| 424 | const application = declared.application; |
| 425 | const withSettings = yield* observe(application); |
| 426 | expect(withSettings.id).toBe( |
| 427 | declared.worker.durableObjectNamespaces.NativeAsyncObject, |
| 428 | ); |
| 429 | expect(withSettings.configuration?.wranglerSsh).toMatchObject({ |
| 430 | enabled: true, |
| 431 | port: 22, |
| 432 | }); |
| 433 | expect( |
| 434 | withSettings.configuration?.authorizedKeys?.map((key) => key.publicKey), |
| 435 | ).toEqual([TEST_SSH_PUBLIC_KEY]); |
| 436 | expect(withSettings.observability?.logs?.enabled).toBe(true); |
| 437 | |
| 438 | // Removing declarations clears what this stack managed. |
| 439 | const removed = yield* stack.deploy(settingsStack()); |
| 440 | expect(removed.application.applicationId).toBe(application.applicationId); |
| 441 | const cleared = yield* observe(application); |
| 442 | expect(cleared.configuration?.wranglerSsh?.enabled ?? false).toBe(false); |
| 443 | expect(cleared.configuration?.authorizedKeys ?? []).toEqual([]); |
| 444 | expect(cleared.observability?.logs?.enabled ?? false).toBe(false); |
| 445 | |
| 446 | // Settings this stack never declared are left alone. |
| 447 | yield* Containers.updateContainerApplication({ |
| 448 | accountId: application.accountId, |
| 449 | applicationId: application.applicationId, |
| 450 | configuration: { wranglerSsh: { enabled: true, port: 22 } }, |
| 451 | }); |
| 452 | yield* stack.deploy(settingsStack()); |
| 453 | expect((yield* observe(application)).configuration?.wranglerSsh?.enabled).toBe( |
| 454 | true, |
| 455 | ); |
| 456 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))); |
| 457 | }), |
| 458 | { timeout: 180_000, retry: 0 }, |
| 459 | ); |
| 460 | |
| 461 | test.provider( |
| 462 | "rejects switching an application between fleet and Durable Object scheduling", |
| 463 | (stack) => |
| 464 | Effect.gen(function* () { |
| 465 | yield* stack.destroy(); |
| 466 | yield* Effect.gen(function* () { |
| 467 | const fleet = yield* stack.deploy(policyStack("default")); |
| 468 | const switched = yield* stack |
| 469 | .deploy(policyStack("durable_object")) |
| 470 | .pipe(Effect.exit); |
| 471 | expect(Exit.isFailure(switched)).toBe(true); |
| 472 | if (Exit.isFailure(switched)) { |
| 473 | expect(Cause.pretty(switched.cause)).toContain( |
| 474 | "cannot switch between fleet and Durable Object scheduling", |
| 475 | ); |
| 476 | } |
| 477 | // The rejected deploy did not touch the deployed application. |
| 478 | const observed = yield* observe(fleet); |
| 479 | expect(observed.id).toBe(fleet.applicationId); |
| 480 | expect(observed.schedulingPolicy).toBe("default"); |
| 481 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))); |
| 482 | }), |
| 483 | { timeout: 120_000, retry: 0 }, |
| 484 | ); |
| 485 | } |
| 486 | |
| 487 | // The second deploy uploads the Worker with a namespace for the new |
| 488 | // class; its container application must be created in that same deploy. |
| 489 | test.provider( |
| 490 | "a deployed Worker gains a container-backed class in one deploy", |
| 491 | (stack) => |
| 492 | Effect.gen(function* () { |
| 493 | yield* stack.destroy(); |
| 494 | |
| 495 | yield* stack.deploy(growingStack(false)); |
| 496 | const grown = yield* stack.deploy(growingStack(true)); |
| 497 | |
| 498 | const application = grown.reviewerApplication!; |
| 499 | if (!dev) { |
| 500 | const observed = yield* Containers.getContainerApplication({ |
| 501 | accountId: application.accountId, |
| 502 | applicationId: application.applicationId, |
| 503 | }); |
| 504 | expect(observed.id).toBe(grown.worker.durableObjectNamespaces.NativeAsyncReviewer); |
| 505 | expect(observed.durableObjects?.namespaceId).toBe(observed.id); |
| 506 | } |
| 507 | |
| 508 | const url = grown.worker.url!; |
| 509 | yield* waitUntilReady(url); |
| 510 | const reviewer = yield* getJson<ExecResult>(url, "/exec?class=reviewer").pipe( |
| 511 | within(`${url}/exec?class=reviewer`, "10 seconds"), |
| 512 | Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 40 }), |
| 513 | within(`${url}/exec?class=reviewer`, "120 seconds"), |
| 514 | ); |
| 515 | expect(reviewer).toEqual({ |
| 516 | stdout: "native", |
| 517 | exitCode: 7, |
| 518 | release: expect.stringMatching(/^3\.21\./), |
| 519 | images: ASYNC_REVIEWER_IMAGES, |
| 520 | }); |
| 521 | |
| 522 | yield* stack.destroy(); |
| 523 | }), |
| 524 | { timeout: 180_000, retry: 0 }, |
| 525 | ); |
| 526 | |
| 527 | for (const scenario of ["execution", "membership"] as const) { |
| 528 | test.provider( |
| 529 | scenario === "execution" |
| 530 | ? "starts each bound Container's image from Effect and async Durable Objects" |
| 531 | : "adds and removes a class's Containers without replacing its application", |
| 532 | (stack) => |
| 533 | Effect.gen(function* () { |
| 534 | yield* stack.destroy(); |
| 535 | |
| 536 | /** |
| 537 | * Exec and lifecycle routes are safe to repeat. Right after a deploy, |
| 538 | * a workers.dev route can 404 or drop a request, and a Durable Object |
| 539 | * can still be served the pre-create placeholder ("Alchemy worker is |
| 540 | * being deployed") until the new version reaches it. Each attempt is |
| 541 | * bounded and retried for up to two minutes. |
| 542 | */ |
| 543 | const call = <A>(url: string, path: string) => |
| 544 | getJson<A>(url, path).pipe( |
| 545 | within(`${url}${path}`, "10 seconds"), |
| 546 | Effect.tapError((error) => |
| 547 | Effect.logWarning(`${url}${path} attempt failed`, error), |
| 548 | ), |
| 549 | Effect.retry({ |
| 550 | schedule: Schedule.spaced("2 seconds"), |
| 551 | times: 40, |
| 552 | }), |
| 553 | within(`${url}${path}`, "120 seconds"), |
| 554 | ); |
| 555 | const exec = (url: string, path: string) => call<ExecResult>(url, path); |
| 556 | |
| 557 | const applications = yield* Effect.gen(function* () { |
| 558 | const deployed = yield* stack.deploy(nativeStack); |
| 559 | |
| 560 | if (scenario === "execution") { |
| 561 | for (const [worker, images] of [ |
| 562 | [deployed.asyncWorker, ASYNC_IMAGES], |
| 563 | [deployed.worker, EFFECT_IMAGES], |
| 564 | ] as const) { |
| 565 | const url = worker.url!; |
| 566 | yield* waitUntilReady(url); |
| 567 | // Each handle's start() starts its own Container's image. |
| 568 | expect(yield* exec(url, "/exec")).toEqual({ |
| 569 | stdout: "native", |
| 570 | exitCode: 7, |
| 571 | release: expect.stringMatching(/^3\.21\./), |
| 572 | images, |
| 573 | }); |
| 574 | expect(yield* exec(url, "/image/tools")).toEqual({ |
| 575 | stdout: "native", |
| 576 | exitCode: 7, |
| 577 | release: expect.stringMatching(/^3\.20\./), |
| 578 | images, |
| 579 | }); |
| 580 | expect(yield* exec(url, "/stdin")).toEqual({ |
| 581 | stdout: "native stdin", |
| 582 | exitCode: 0, |
| 583 | release: expect.stringMatching(/^3\.21\./), |
| 584 | images, |
| 585 | }); |
| 586 | } |
| 587 | expect(yield* exec(deployed.worker.url!, "/snapshot")).toEqual({ |
| 588 | stdout: "persisted", |
| 589 | exitCode: 0, |
| 590 | release: expect.stringMatching(/^3\.21\./), |
| 591 | images: EFFECT_IMAGES, |
| 592 | }); |
| 593 | |
| 594 | // Closing an exec's scope kills the process; stdout streams; |
| 595 | // a failed container surfaces as ContainerError from monitor(). |
| 596 | const lifecycle = (mode: string) => |
| 597 | call<Record<string, unknown>>(deployed.worker.url!, `/lifecycle/${mode}`); |
| 598 | expect(yield* lifecycle("interrupt")).toEqual({ remaining: "" }); |
| 599 | expect(yield* lifecycle("stream")).toEqual({ stdout: "ab" }); |
| 600 | expect(yield* lifecycle("monitor")).toEqual({ |
| 601 | failed: true, |
| 602 | tag: "ContainerError", |
| 603 | }); |
| 604 | // The container's outbound HTTP reaches the Worker entrypoint. |
| 605 | expect(yield* lifecycle("intercept")).toEqual({ |
| 606 | exitCode: 0, |
| 607 | stdout: "intercepted intercept.internal/hello", |
| 608 | stderr: "", |
| 609 | }); |
| 610 | |
| 611 | // A second class on each Worker shares the Shell Container |
| 612 | // and starts its own images from its own image map. |
| 613 | for (const [path, release] of [ |
| 614 | ["/reviewer/shell", /^3\.21\./], |
| 615 | ["/reviewer/lint", /^3\.19\./], |
| 616 | ] as const) { |
| 617 | expect(yield* exec(deployed.worker.url!, path)).toEqual({ |
| 618 | stdout: "native", |
| 619 | exitCode: 7, |
| 620 | release: expect.stringMatching(release), |
| 621 | images: EFFECT_REVIEWER_IMAGES, |
| 622 | }); |
| 623 | } |
| 624 | for (const [path, release] of [ |
| 625 | ["/exec?class=reviewer", /^3\.21\./], |
| 626 | ["/image/lint?class=reviewer", /^3\.19\./], |
| 627 | ] as const) { |
| 628 | expect(yield* exec(deployed.asyncWorker.url!, path)).toEqual({ |
| 629 | stdout: "native", |
| 630 | exitCode: 7, |
| 631 | release: expect.stringMatching(release), |
| 632 | images: ASYNC_REVIEWER_IMAGES, |
| 633 | }); |
| 634 | } |
| 635 | |
| 636 | // Containers own images, not applications. |
| 637 | for (const container of [ |
| 638 | deployed.shell, |
| 639 | deployed.tools, |
| 640 | deployed.lint, |
| 641 | deployed.asyncShell, |
| 642 | ]) { |
| 643 | expect(container.applicationId).toEqual( |
| 644 | dev ? expect.stringMatching(/^dev:/) : "", |
| 645 | ); |
| 646 | } |
| 647 | |
| 648 | if (dev) { |
| 649 | // Local emulation never creates a cloud application. |
| 650 | for (const application of [ |
| 651 | deployed.application, |
| 652 | deployed.reviewerApplication, |
| 653 | deployed.asyncApplication, |
| 654 | deployed.asyncReviewerApplication, |
| 655 | ]) { |
| 656 | expect(application.applicationId).toMatch(/^dev:/); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | if (!dev) { |
| 661 | // One application per class, attached to its namespace, |
| 662 | // without any fleet configuration. |
| 663 | const classes = [ |
| 664 | [deployed.application, deployed.worker, "NativeObject", EFFECT_IMAGES], |
| 665 | [ |
| 666 | deployed.reviewerApplication, |
| 667 | deployed.worker, |
| 668 | "NativeReviewer", |
| 669 | EFFECT_REVIEWER_IMAGES, |
| 670 | ], |
| 671 | [ |
| 672 | deployed.asyncApplication, |
| 673 | deployed.asyncWorker, |
| 674 | "NativeAsyncObject", |
| 675 | ASYNC_IMAGES, |
| 676 | ], |
| 677 | [ |
| 678 | deployed.asyncReviewerApplication, |
| 679 | deployed.asyncWorker, |
| 680 | "NativeAsyncReviewer", |
| 681 | ASYNC_REVIEWER_IMAGES, |
| 682 | ], |
| 683 | ] as const; |
| 684 | // Every class has its own application. |
| 685 | expect( |
| 686 | new Set(classes.map(([application]) => application.applicationId)).size, |
| 687 | ).toBe(classes.length); |
| 688 | for (const [application, worker, className, images] of classes) { |
| 689 | const observed = yield* Containers.getContainerApplication({ |
| 690 | accountId: application.accountId, |
| 691 | applicationId: application.applicationId, |
| 692 | }); |
| 693 | expect(observed.schedulingPolicy).toBe("durable_object"); |
| 694 | expect(observed.id).toBe(worker.durableObjectNamespaces[className]); |
| 695 | expect(observed.id).toBe(observed.durableObjects?.namespaceId); |
| 696 | expect(observed.name).toBe(application.applicationName); |
| 697 | const { configuration } = observed; |
| 698 | expect( |
| 699 | configuration && "image" in configuration ? configuration.image : undefined, |
| 700 | ).toBeUndefined(); |
| 701 | expect( |
| 702 | "maxInstances" in observed |
| 703 | ? (observed.maxInstances ?? undefined) |
| 704 | : undefined, |
| 705 | ).toBeUndefined(); |
| 706 | // The uploaded Worker merges the class's Containers into |
| 707 | // its one entry, keyed by Container id. |
| 708 | expect( |
| 709 | yield* uploadedImageKeys( |
| 710 | application.accountId, |
| 711 | worker.workerName, |
| 712 | className, |
| 713 | ), |
| 714 | ).toEqual(images); |
| 715 | } |
| 716 | expect(yield* exec(deployed.asyncWorker.url!, "/builtin")).toEqual({ |
| 717 | stdout: "native", |
| 718 | exitCode: 7, |
| 719 | release: "", |
| 720 | images: ASYNC_IMAGES, |
| 721 | }); |
| 722 | } |
| 723 | |
| 724 | const unchanged = yield* stack.deploy(nativeStack); |
| 725 | expect(unchanged.application.applicationId).toBe( |
| 726 | deployed.application.applicationId, |
| 727 | ); |
| 728 | expect(unchanged.asyncApplication.applicationId).toBe( |
| 729 | deployed.asyncApplication.applicationId, |
| 730 | ); |
| 731 | } else { |
| 732 | yield* waitUntilReady(deployed.asyncWorker.url!); |
| 733 | expect(yield* exec(deployed.asyncWorker.url!, "/image/tools")).toMatchObject({ |
| 734 | release: expect.stringMatching(/^3\.20\./), |
| 735 | images: ASYNC_IMAGES, |
| 736 | }); |
| 737 | |
| 738 | // Removing a Container drops its image from the class. |
| 739 | const removedImages = { shell: "alpine:3.21", tools: undefined }; |
| 740 | const removed = yield* stack.deploy( |
| 741 | nativeStack.pipe(Effect.provideService(NativeImages, removedImages)), |
| 742 | ); |
| 743 | expect(removed.asyncApplication.applicationId).toBe( |
| 744 | deployed.asyncApplication.applicationId, |
| 745 | ); |
| 746 | expect(removed.tools.image).toBeUndefined(); |
| 747 | if (!dev) { |
| 748 | expect( |
| 749 | yield* uploadedImageKeys( |
| 750 | removed.asyncApplication.accountId, |
| 751 | removed.asyncWorker.workerName, |
| 752 | "NativeAsyncObject", |
| 753 | ), |
| 754 | ).toEqual(["AsyncShell"]); |
| 755 | expect( |
| 756 | yield* uploadedImageKeys( |
| 757 | removed.application.accountId, |
| 758 | removed.worker.workerName, |
| 759 | "NativeObject", |
| 760 | ), |
| 761 | ).toEqual(["NativeShell"]); |
| 762 | } |
| 763 | // TODO: Live image-map updates reach existing Workers |
| 764 | // inconsistently: a redeploy's version can take longer than |
| 765 | // a bounded wait to serve, and existing objects can keep |
| 766 | // the old map. Live coverage asserts the uploaded metadata |
| 767 | // above; the runtime view is asserted locally. |
| 768 | if (dev) { |
| 769 | // A redeploy cuts over to the new Worker version |
| 770 | // asynchronously; wait for it before observing the class. |
| 771 | yield* waitForRevision( |
| 772 | removed.asyncWorker.url!, |
| 773 | removedImages, |
| 774 | "?object=removed", |
| 775 | ); |
| 776 | yield* waitForRevision(removed.worker.url!, removedImages); |
| 777 | expect(yield* exec(removed.asyncWorker.url!, "/exec?object=removed")).toEqual({ |
| 778 | stdout: "native", |
| 779 | exitCode: 7, |
| 780 | release: expect.stringMatching(/^3\.21\./), |
| 781 | images: ["AsyncShell"], |
| 782 | }); |
| 783 | expect(yield* exec(removed.worker.url!, "/exec")).toMatchObject({ |
| 784 | images: ["NativeShell"], |
| 785 | }); |
| 786 | } |
| 787 | |
| 788 | // Adding it back restores the image under the same key. |
| 789 | const added = yield* stack.deploy(nativeStack); |
| 790 | expect(added.asyncApplication.applicationId).toBe( |
| 791 | deployed.asyncApplication.applicationId, |
| 792 | ); |
| 793 | if (!dev) { |
| 794 | expect( |
| 795 | yield* uploadedImageKeys( |
| 796 | added.asyncApplication.accountId, |
| 797 | added.asyncWorker.workerName, |
| 798 | "NativeAsyncObject", |
| 799 | ), |
| 800 | ).toEqual(ASYNC_IMAGES); |
| 801 | } else { |
| 802 | yield* waitForRevision( |
| 803 | added.asyncWorker.url!, |
| 804 | { shell: "alpine:3.21", tools: "alpine:3.20" }, |
| 805 | "?object=added", |
| 806 | ); |
| 807 | expect( |
| 808 | yield* exec(added.asyncWorker.url!, "/image/tools?object=added"), |
| 809 | ).toEqual({ |
| 810 | stdout: "native", |
| 811 | exitCode: 7, |
| 812 | release: expect.stringMatching(/^3\.20\./), |
| 813 | images: ASYNC_IMAGES, |
| 814 | }); |
| 815 | } |
| 816 | } |
| 817 | return [deployed.application, deployed.asyncApplication]; |
| 818 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))); |
| 819 | |
| 820 | if (!dev) { |
| 821 | for (const application of applications) { |
| 822 | expect( |
| 823 | yield* waitForApplicationDeleted( |
| 824 | application.accountId, |
| 825 | application.applicationId, |
| 826 | ), |
| 827 | ).toBeUndefined(); |
| 828 | } |
| 829 | } |
| 830 | }), |
| 831 | { timeout: 300_000, retry: 0 }, |
| 832 | ); |
| 833 | } |
| 834 | |
| 835 | test.provider( |
| 836 | "builds a Container's Dockerfile with build args and BuildKit secrets", |
| 837 | (stack) => |
| 838 | Effect.gen(function* () { |
| 839 | yield* stack.destroy(); |
| 840 | |
| 841 | const withInputs = (inputs: BuildInputSet) => |
| 842 | buildSecretsStack.pipe(Effect.provideService(BuildInputs, inputs)); |
| 843 | const initial = { greeting: "hello", token: "alchemy-build-secret-value" }; |
| 844 | |
| 845 | const application = yield* Effect.gen(function* () { |
| 846 | const deployed = yield* stack.deploy(withInputs(initial)); |
| 847 | const url = deployed.worker.url!; |
| 848 | yield* waitUntilReady(url); |
| 849 | // The build saw the arg and the secret's mount. |
| 850 | expect( |
| 851 | yield* getText(url, "/build").pipe( |
| 852 | within("/build", "10 seconds"), |
| 853 | Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 40 }), |
| 854 | ), |
| 855 | ).toBe(`hello ${initial.token.length}`); |
| 856 | |
| 857 | // Secrets compare by value, so an unchanged program plans nothing. |
| 858 | const unchanged = yield* stack.plan(withInputs(initial)); |
| 859 | expect(unchanged.resources.BuildSecretsImage?.action).toBe("noop"); |
| 860 | |
| 861 | // Rotating a secret reuses the published image. |
| 862 | const rotated = yield* stack.deploy(withInputs({ ...initial, token: "rotated" })); |
| 863 | expect(rotated.image.hash?.image).toBe(deployed.image.hash?.image); |
| 864 | expect(rotated.image.image).toBe(deployed.image.image); |
| 865 | |
| 866 | // A build arg is part of the image's content hash. |
| 867 | const changed = yield* stack.deploy( |
| 868 | withInputs({ greeting: "howdy", token: "rotated" }), |
| 869 | ); |
| 870 | expect(changed.image.hash?.image).not.toBe(deployed.image.hash?.image); |
| 871 | return deployed.application; |
| 872 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))); |
| 873 | |
| 874 | if (!dev) { |
| 875 | expect( |
| 876 | yield* waitForApplicationDeleted(application.accountId, application.applicationId), |
| 877 | ).toBeUndefined(); |
| 878 | } |
| 879 | }), |
| 880 | { timeout: 300_000, retry: 0 }, |
| 881 | ); |
| 882 | }, |
| 883 | ); |
| 884 | } |