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 Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import { testName } from "../../names.ts"; |
| 9 | import { waitForStreamGone, waitForSubscriptionGone } 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 | const program = (startAt: "earliest" | "latest") => |
| 16 | Effect.gen(function* () { |
| 17 | const stream = yield* Cloudflare.K2.Stream("SubscriptionStream"); |
| 18 | const subscription = yield* Cloudflare.K2.Subscription("Analytics", { |
| 19 | stream, |
| 20 | startAt, |
| 21 | }); |
| 22 | return { stream, subscription }; |
| 23 | }); |
| 24 | |
| 25 | test.provider( |
| 26 | "create a subscription and replace it when startAt changes", |
| 27 | (stack) => |
| 28 | Effect.gen(function* () { |
| 29 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 30 | |
| 31 | yield* stack.destroy(); |
| 32 | |
| 33 | const first = yield* stack.deploy(program("latest")); |
| 34 | const streamId = first.stream.streamId; |
| 35 | expect(first.subscription.streamId).toEqual(streamId); |
| 36 | expect(first.subscription.startAt).toEqual("latest"); |
| 37 | |
| 38 | const observed = yield* k2.getSubscription({ |
| 39 | streamId, |
| 40 | subscriptionId: first.subscription.subscriptionId, |
| 41 | }); |
| 42 | expect(observed.name).toEqual(first.subscription.subscriptionName); |
| 43 | expect(observed.startAt.type).toEqual("latest"); |
| 44 | |
| 45 | // A redeploy with the same props is a no-op. |
| 46 | const same = yield* stack.deploy(program("latest")); |
| 47 | expect(same.subscription.subscriptionId).toEqual(first.subscription.subscriptionId); |
| 48 | |
| 49 | // startAt is immutable — the subscription is replaced on the same stream. |
| 50 | const second = yield* stack.deploy(program("earliest")); |
| 51 | expect(second.stream.streamId).toEqual(streamId); |
| 52 | expect(second.subscription.startAt).toEqual("earliest"); |
| 53 | expect(second.subscription.subscriptionId).not.toEqual(first.subscription.subscriptionId); |
| 54 | yield* waitForSubscriptionGone(streamId, first.subscription.subscriptionId); |
| 55 | |
| 56 | const replaced = yield* k2.listSubscriptions({ streamId }); |
| 57 | expect(replaced.map((s) => s.id)).toEqual([second.subscription.subscriptionId]); |
| 58 | expect(replaced[0]?.startAt.type).toEqual("earliest"); |
| 59 | |
| 60 | yield* stack.destroy(); |
| 61 | yield* waitForSubscriptionGone(streamId, second.subscription.subscriptionId); |
| 62 | yield* waitForStreamGone(accountId, streamId); |
| 63 | }).pipe(logLevel), |
| 64 | { tags: ["provider:cloudflare", "provider:cloudflare:k2", "live"], timeout: 120_000 }, |
| 65 | ); |
| 66 | |
| 67 | test.provider( |
| 68 | "an explicitly named subscription is replaced delete-first", |
| 69 | (stack) => |
| 70 | Effect.gen(function* () { |
| 71 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 72 | |
| 73 | yield* stack.destroy(); |
| 74 | |
| 75 | const named = (startAt: "earliest" | "latest") => |
| 76 | Effect.gen(function* () { |
| 77 | const stream = yield* Cloudflare.K2.Stream("NamedSubscriptionStream"); |
| 78 | const subscription = yield* Cloudflare.K2.Subscription("Named", { |
| 79 | stream, |
| 80 | name: testName("alchemy-k2-named"), |
| 81 | startAt, |
| 82 | }); |
| 83 | return { stream, subscription }; |
| 84 | }); |
| 85 | |
| 86 | const first = yield* stack.deploy(named("latest")); |
| 87 | expect(first.subscription.subscriptionName).toEqual(testName("alchemy-k2-named")); |
| 88 | |
| 89 | // K2 rejects the same name with different settings, so the engine must |
| 90 | // delete the old subscription before creating the new one. |
| 91 | const second = yield* stack.deploy(named("earliest")); |
| 92 | expect(second.subscription.subscriptionName).toEqual(testName("alchemy-k2-named")); |
| 93 | expect(second.subscription.startAt).toEqual("earliest"); |
| 94 | const observed = yield* k2.getSubscription({ |
| 95 | streamId: second.stream.streamId, |
| 96 | subscriptionId: second.subscription.subscriptionId, |
| 97 | }); |
| 98 | expect(observed.startAt.type).toEqual("earliest"); |
| 99 | |
| 100 | yield* stack.destroy(); |
| 101 | yield* waitForStreamGone(accountId, second.stream.streamId); |
| 102 | }).pipe(logLevel), |
| 103 | { tags: ["provider:cloudflare", "provider:cloudflare:k2", "live"], timeout: 120_000 }, |
| 104 | ); |