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 { makeK2LocalAuth } from "@alchemy.run/cloudflare/K2/K2Http"; |
| 4 | import { makeReadSubscriptionClient } from "@alchemy.run/cloudflare/K2/ReadSubscriptionHttp"; |
| 5 | import { expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as k2 from "@distilled.cloud/cloudflare/k2"; |
| 8 | import { Action } from "alchemy/Action"; |
| 9 | import { createHostRuntimeContext, ServerHost } from "alchemy/Runtime/Process"; |
| 10 | import { RuntimeContext } from "alchemy/RuntimeContext"; |
| 11 | import * as Data from "effect/Data"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Layer from "effect/Layer"; |
| 14 | import * as Option from "effect/Option"; |
| 15 | import { MinimumLogLevel } from "effect/References"; |
| 16 | import * as Schedule from "effect/Schedule"; |
| 17 | import * as Schema from "effect/Schema"; |
| 18 | import * as Stream from "effect/Stream"; |
| 19 | import { waitForStreamGone } from "./k2-test-utils.ts"; |
| 20 | |
| 21 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 22 | |
| 23 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 24 | |
| 25 | const Event = Schema.Struct({ source: Schema.String, index: Schema.Number }); |
| 26 | type Event = typeof Event.Type; |
| 27 | |
| 28 | class NotConsumed extends Data.TaggedError("NotConsumed")<{ distinct: number }> {} |
| 29 | |
| 30 | const decoder = new TextDecoder(); |
| 31 | |
| 32 | /** |
| 33 | * The `*Local` layers end to end, from an Action: `WriteStreamLocal` and |
| 34 | * `StreamSinkLocal` produce typed records, and `consumeStreamRecords` |
| 35 | * (`StreamEventSourcePolling` over `ReadSubscriptionLocal`, on an in-process |
| 36 | * `ServerHost`) consumes them. The handler fails once to exercise |
| 37 | * nack → redelivery. The ReadSubscription client is then driven directly |
| 38 | * against a second subscription. |
| 39 | */ |
| 40 | test.provider( |
| 41 | "Local producers, consumeStreamRecords polling, and the ReadSubscription client", |
| 42 | (stack) => |
| 43 | Effect.gen(function* () { |
| 44 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 45 | |
| 46 | yield* stack.destroy(); |
| 47 | |
| 48 | const out = yield* stack.deploy( |
| 49 | Effect.gen(function* () { |
| 50 | const events = yield* Cloudflare.K2.Stream("LocalEvents", { http: true, schema: Event }); |
| 51 | const poller = yield* Cloudflare.K2.Subscription("Poller", { |
| 52 | stream: events, |
| 53 | startAt: "earliest", |
| 54 | }); |
| 55 | const reader = yield* Cloudflare.K2.Subscription("Reader", { |
| 56 | stream: events, |
| 57 | startAt: "earliest", |
| 58 | }); |
| 59 | |
| 60 | const Roundtrip = Action( |
| 61 | "Roundtrip", |
| 62 | Effect.gen(function* () { |
| 63 | const writer = yield* Cloudflare.K2.WriteStream(events); |
| 64 | const sink = yield* Cloudflare.K2.StreamSink(events); |
| 65 | |
| 66 | const seen = new Set<string>(); |
| 67 | let failures = 0; |
| 68 | const host = createHostRuntimeContext("Test.K2Host")("K2Host"); |
| 69 | yield* Cloudflare.K2.consumeStreamRecords( |
| 70 | events, |
| 71 | { |
| 72 | subscription: poller, |
| 73 | workerId: "alchemy-k2-poller", |
| 74 | idleBackoff: "500 millis", |
| 75 | concurrency: 2, |
| 76 | }, |
| 77 | (records) => |
| 78 | Effect.gen(function* () { |
| 79 | if (failures === 0) { |
| 80 | failures++; |
| 81 | return yield* Effect.fail("first delivery fails" as const); |
| 82 | } |
| 83 | yield* records.pipe( |
| 84 | Stream.runForEach((record) => |
| 85 | Effect.sync(() => seen.add(`${record.value.source}:${record.value.index}`)), |
| 86 | ), |
| 87 | ); |
| 88 | }), |
| 89 | ).pipe( |
| 90 | Effect.provide( |
| 91 | Layer.provideMerge( |
| 92 | Cloudflare.K2.StreamEventSourcePolling, |
| 93 | Layer.mergeAll( |
| 94 | Cloudflare.K2.ReadSubscriptionLocal, |
| 95 | Layer.succeed(ServerHost, host), |
| 96 | ), |
| 97 | ), |
| 98 | ), |
| 99 | ); |
| 100 | |
| 101 | return Effect.fn(function* () { |
| 102 | yield* writer.send([0, 1, 2, 3, 4].map((index) => ({ source: "writer", index }))); |
| 103 | yield* Stream.range(0, 19).pipe( |
| 104 | Stream.map((index): Event => ({ source: "sink", index })), |
| 105 | Stream.run(sink), |
| 106 | ); |
| 107 | |
| 108 | const { program } = yield* host.exports; |
| 109 | const drained = Effect.suspend(() => |
| 110 | seen.size >= 25 |
| 111 | ? Effect.succeed(seen.size) |
| 112 | : Effect.fail(new NotConsumed({ distinct: seen.size })), |
| 113 | ).pipe( |
| 114 | Effect.retry({ |
| 115 | schedule: Schedule.max([Schedule.spaced("1 second"), Schedule.recurs(60)]), |
| 116 | }), |
| 117 | ); |
| 118 | const distinct = yield* Effect.raceFirst( |
| 119 | drained, |
| 120 | program.pipe(Effect.andThen(Effect.never)), |
| 121 | ); |
| 122 | return { distinct, failures, seen: [...seen].sort() }; |
| 123 | }); |
| 124 | }).pipe( |
| 125 | Effect.provide( |
| 126 | Layer.mergeAll(Cloudflare.K2.WriteStreamLocal, Cloudflare.K2.StreamSinkLocal), |
| 127 | ), |
| 128 | ), |
| 129 | ); |
| 130 | |
| 131 | return { |
| 132 | roundtrip: yield* Roundtrip({}), |
| 133 | streamId: events.streamId, |
| 134 | readerId: reader.subscriptionId, |
| 135 | }; |
| 136 | }), |
| 137 | ); |
| 138 | |
| 139 | expect(out.roundtrip.distinct).toEqual(25); |
| 140 | expect(out.roundtrip.failures).toEqual(1); |
| 141 | expect(out.roundtrip.seen).toContain("writer:4"); |
| 142 | expect(out.roundtrip.seen).toContain("sink:19"); |
| 143 | |
| 144 | // Drive the client directly (current credentials) on the reader. |
| 145 | const auth = yield* makeK2LocalAuth; |
| 146 | const client = makeReadSubscriptionClient(auth, { |
| 147 | streamId: Effect.succeed(out.streamId), |
| 148 | subscriptionId: Effect.succeed(out.readerId), |
| 149 | }); |
| 150 | const consume = (workerId: string, maxRecords: number) => |
| 151 | client.consume({ workerId, maxRecords }).pipe( |
| 152 | Effect.retry({ |
| 153 | while: (e) => e._tag === "K2BatchPending", |
| 154 | schedule: Schedule.max([Schedule.spaced("500 millis"), Schedule.recurs(10)]), |
| 155 | }), |
| 156 | ); |
| 157 | |
| 158 | const first = yield* consume("reader-a", 3); |
| 159 | expect(Option.isSome(first)).toBe(true); |
| 160 | const batch = Option.getOrThrow(first); |
| 161 | expect(batch.records.length).toEqual(3); |
| 162 | expect(JSON.parse(decoder.decode(batch.records[0]!.content))).toEqual({ |
| 163 | source: "writer", |
| 164 | index: 0, |
| 165 | }); |
| 166 | expect(batch.records[0]!.headers["content-type"]).toEqual("application/json"); |
| 167 | expect(batch.records[0]!.timestamp).toBeInstanceOf(Date); |
| 168 | |
| 169 | // The same worker gets the same batch back; extend refreshes the lease. |
| 170 | const again = Option.getOrThrow(yield* consume("reader-a", 3)); |
| 171 | expect(again.batchId).toEqual(batch.batchId); |
| 172 | const extended = yield* client.extend(batch); |
| 173 | expect(extended.leasedUntil.getTime()).toBeGreaterThan(0); |
| 174 | |
| 175 | // nack → redelivered under a new batch id. |
| 176 | yield* client.nack(batch); |
| 177 | const redelivered = Option.getOrThrow(yield* consume("reader-a", 3)); |
| 178 | expect(redelivered.batchId).not.toEqual(batch.batchId); |
| 179 | expect(redelivered.records.map((r) => decoder.decode(r.content))).toEqual( |
| 180 | batch.records.map((r) => decoder.decode(r.content)), |
| 181 | ); |
| 182 | |
| 183 | // ack advances the subscription; acking twice succeeds; extending an |
| 184 | // acknowledged batch is K2LeaseLost. |
| 185 | yield* client.ack(redelivered); |
| 186 | yield* client.ack(redelivered); |
| 187 | const lost = yield* client.extend(redelivered).pipe(Effect.flip); |
| 188 | expect(lost._tag).toEqual("K2LeaseLost"); |
| 189 | |
| 190 | // Drain the rest; an empty subscription consumes None. |
| 191 | const rest = Option.getOrThrow(yield* consume("reader-a", 1000)); |
| 192 | expect(rest.records.length).toEqual(22); |
| 193 | yield* client.ack(rest); |
| 194 | const empty = yield* consume("reader-a", 10); |
| 195 | expect(Option.isNone(empty)).toBe(true); |
| 196 | |
| 197 | yield* stack.destroy(); |
| 198 | yield* waitForStreamGone(accountId, out.streamId); |
| 199 | }).pipe(logLevel), |
| 200 | { tags: ["provider:cloudflare", "provider:cloudflare:k2", "live"], timeout: 120_000 }, |
| 201 | ); |
| 202 | |
| 203 | // Deploy-time half of `consumeStreamRecords` without `subscription`: it |
| 204 | // declares a Subscription owned by the consuming host. |
| 205 | test.provider( |
| 206 | "consumeStreamRecords creates a host-owned subscription", |
| 207 | (stack) => |
| 208 | Effect.gen(function* () { |
| 209 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 210 | |
| 211 | yield* stack.destroy(); |
| 212 | |
| 213 | const host = createHostRuntimeContext("Test.K2Host")("K2Host"); |
| 214 | const out = yield* stack.deploy( |
| 215 | Effect.gen(function* () { |
| 216 | const events = yield* Cloudflare.K2.Stream("AutoEvents"); |
| 217 | yield* Cloudflare.K2.consumeStreamRecords(events, { startAt: "earliest" }, (records) => |
| 218 | Stream.runDrain(records), |
| 219 | ).pipe( |
| 220 | Effect.provide( |
| 221 | Layer.provideMerge( |
| 222 | Cloudflare.K2.StreamEventSourcePolling, |
| 223 | Layer.mergeAll( |
| 224 | Cloudflare.K2.ReadSubscriptionLocal, |
| 225 | Layer.succeed(ServerHost, host), |
| 226 | // A real host provides its runtime context; outputs the |
| 227 | // client reads are bound into the host's env. |
| 228 | Layer.succeed(RuntimeContext, host), |
| 229 | ), |
| 230 | ), |
| 231 | ), |
| 232 | ); |
| 233 | return { streamId: events.streamId }; |
| 234 | }), |
| 235 | ); |
| 236 | |
| 237 | const subscriptions = yield* k2.listSubscriptions({ streamId: out.streamId }); |
| 238 | expect(subscriptions.length).toEqual(1); |
| 239 | expect(subscriptions[0]?.startAt.type).toEqual("earliest"); |
| 240 | |
| 241 | yield* stack.destroy(); |
| 242 | yield* waitForStreamGone(accountId, out.streamId); |
| 243 | }).pipe(logLevel), |
| 244 | { tags: ["provider:cloudflare", "provider:cloudflare:k2", "live"], timeout: 120_000 }, |
| 245 | ); |