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 { 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 k2 from "@distilled.cloud/cloudflare/k2"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import { waitForStreamGone } from "./k2-test-utils.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | test.provider( |
| 16 | "create, update in place, and delete a stream", |
| 17 | (stack) => |
| 18 | Effect.gen(function* () { |
| 19 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 20 | |
| 21 | yield* stack.destroy(); |
| 22 | |
| 23 | // Defaults: worker binding on, HTTP off, seven-day retention. |
| 24 | const created = yield* stack.deploy(Cloudflare.K2.Stream("UpdateStream")); |
| 25 | expect(created.streamName).toMatch(/^[a-z0-9_]+$/); |
| 26 | expect(created.endpoint).toEqual(`https://${created.streamId}.k2.cloudflarestorage.com`); |
| 27 | expect(created.retentionSeconds).toEqual(604_800); |
| 28 | expect(created.httpEnabled).toBe(false); |
| 29 | expect(created.workerBindingEnabled).toBe(true); |
| 30 | |
| 31 | const observed = yield* k2.getStream({ accountId, streamId: created.streamId }); |
| 32 | expect(observed.name).toEqual(created.streamName); |
| 33 | expect(observed.http.enabled).toBe(false); |
| 34 | expect(observed.workerBinding.enabled).toBe(true); |
| 35 | |
| 36 | // Retention, HTTP input, and worker binding are patched in place. |
| 37 | const updated = yield* stack.deploy( |
| 38 | Cloudflare.K2.Stream("UpdateStream", { |
| 39 | retention: "1 day", |
| 40 | http: { authentication: false, cors: ["https://example.com"] }, |
| 41 | workerBinding: false, |
| 42 | }), |
| 43 | ); |
| 44 | expect(updated.streamId).toEqual(created.streamId); |
| 45 | expect(updated.retentionSeconds).toEqual(86_400); |
| 46 | expect(updated.httpEnabled).toBe(true); |
| 47 | expect(updated.httpAuthentication).toBe(false); |
| 48 | expect(updated.corsOrigins).toEqual(["https://example.com"]); |
| 49 | expect(updated.workerBindingEnabled).toBe(false); |
| 50 | |
| 51 | const patched = yield* k2.getStream({ accountId, streamId: created.streamId }); |
| 52 | expect(patched.retentionSeconds).toEqual(86_400); |
| 53 | expect(patched.http.enabled).toBe(true); |
| 54 | expect(patched.http.authentication ?? false).toBe(false); |
| 55 | expect(patched.http.cors?.origins).toEqual(["https://example.com"]); |
| 56 | expect(patched.workerBinding.enabled).toBe(false); |
| 57 | |
| 58 | // `http: true` turns authentication on; dropping cors clears it. |
| 59 | const authenticated = yield* stack.deploy( |
| 60 | Cloudflare.K2.Stream("UpdateStream", { retention: "1 day", http: true }), |
| 61 | ); |
| 62 | expect(authenticated.streamId).toEqual(created.streamId); |
| 63 | expect(authenticated.httpAuthentication).toBe(true); |
| 64 | expect(authenticated.corsOrigins ?? []).toEqual([]); |
| 65 | expect(authenticated.workerBindingEnabled).toBe(true); |
| 66 | |
| 67 | // The provider lists the deployed stream in the read Attributes shape. |
| 68 | const provider = yield* Provider.findProvider(Cloudflare.K2.Stream); |
| 69 | const all = yield* provider.list(); |
| 70 | const listed = all.find((s) => s.streamId === created.streamId); |
| 71 | expect(listed?.streamName).toEqual(created.streamName); |
| 72 | expect(listed?.httpAuthentication).toBe(true); |
| 73 | |
| 74 | yield* stack.destroy(); |
| 75 | yield* waitForStreamGone(accountId, created.streamId); |
| 76 | }).pipe(logLevel), |
| 77 | { tags: ["provider:cloudflare", "provider:cloudflare:k2", "live"], timeout: 120_000 }, |
| 78 | ); |
| 79 | |
| 80 | test.provider( |
| 81 | "renaming a stream replaces it", |
| 82 | (stack) => |
| 83 | Effect.gen(function* () { |
| 84 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 85 | |
| 86 | yield* stack.destroy(); |
| 87 | |
| 88 | const first = yield* stack.deploy( |
| 89 | Cloudflare.K2.Stream("RenamedStream", { name: "alchemy_k2_test_rename_a" }), |
| 90 | ); |
| 91 | expect(first.streamName).toEqual("alchemy_k2_test_rename_a"); |
| 92 | |
| 93 | const second = yield* stack.deploy( |
| 94 | Cloudflare.K2.Stream("RenamedStream", { name: "alchemy_k2_test_rename_b" }), |
| 95 | ); |
| 96 | expect(second.streamName).toEqual("alchemy_k2_test_rename_b"); |
| 97 | expect(second.streamId).not.toEqual(first.streamId); |
| 98 | |
| 99 | // The old generation is deleted after the replacement. |
| 100 | yield* waitForStreamGone(accountId, first.streamId); |
| 101 | const observed = yield* k2.getStream({ accountId, streamId: second.streamId }); |
| 102 | expect(observed.name).toEqual("alchemy_k2_test_rename_b"); |
| 103 | |
| 104 | yield* stack.destroy(); |
| 105 | yield* waitForStreamGone(accountId, second.streamId); |
| 106 | }).pipe(logLevel), |
| 107 | { tags: ["provider:cloudflare", "provider:cloudflare:k2", "live"], timeout: 120_000 }, |
| 108 | ); |