The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | decodeWithSchema, |
| 3 | encodeRecords, |
| 4 | fromBase64, |
| 5 | fromConsumedRecords, |
| 6 | toBase64, |
| 7 | toProduceRecords, |
| 8 | wireSize, |
| 9 | } from "@alchemy.run/cloudflare/K2/K2Codec"; |
| 10 | import type { EncodedRecord } from "@alchemy.run/cloudflare/K2/K2Types"; |
| 11 | import { |
| 12 | makeStreamSink, |
| 13 | MAX_REQUEST_BYTES, |
| 14 | packStreamSinkBatches, |
| 15 | } from "@alchemy.run/cloudflare/K2/StreamSinkBatch"; |
| 16 | import type { WriteStreamClient } from "@alchemy.run/cloudflare/K2/WriteStream"; |
| 17 | import { |
| 18 | type K2StreamBinding, |
| 19 | sendViaBinding, |
| 20 | } from "@alchemy.run/cloudflare/K2/WriteStreamBinding"; |
| 21 | import { makeWriteStreamClient } from "@alchemy.run/cloudflare/K2/WriteStreamClient"; |
| 22 | import { describe, expect, it } from "@alchemy.run/test"; |
| 23 | import * as k2 from "@distilled.cloud/cloudflare/k2"; |
| 24 | import { RuntimeContext } from "alchemy/RuntimeContext"; |
| 25 | import * as Effect from "effect/Effect"; |
| 26 | import * as Schema from "effect/Schema"; |
| 27 | import * as Stream from "effect/Stream"; |
| 28 | |
| 29 | /** |
| 30 | * Pure tests of K2 record encoding, `StreamSink` packing and retries, and |
| 31 | * the native binding's error mapping. Live delivery is covered by |
| 32 | * `WriteStream.test.ts` and `ReadSubscription.test.ts`. |
| 33 | */ |
| 34 | |
| 35 | const Order = Schema.Struct({ id: Schema.Number }); |
| 36 | |
| 37 | const record = (bytes: number): EncodedRecord => ({ content: new Uint8Array(bytes) }); |
| 38 | |
| 39 | const fakeClient = (failures: ReadonlyArray<k2.ProduceError["_tag"]> = []) => { |
| 40 | const sent: EncodedRecord[][] = []; |
| 41 | let attempt = 0; |
| 42 | const client: WriteStreamClient<{ id: number }> = makeWriteStreamClient(Order, (records) => |
| 43 | Effect.suspend((): Effect.Effect<void, k2.K2Unavailable | k2.K2AppendOutcomeUnknown> => { |
| 44 | const tag = failures[attempt++]; |
| 45 | if (tag === "K2Unavailable") { |
| 46 | return Effect.fail(new k2.K2Unavailable({ code: 10211, message: "unavailable" })); |
| 47 | } |
| 48 | if (tag === "K2AppendOutcomeUnknown") { |
| 49 | return Effect.fail(new k2.K2AppendOutcomeUnknown({ code: 10212, message: "maybe stored" })); |
| 50 | } |
| 51 | sent.push([...records]); |
| 52 | return Effect.void; |
| 53 | }), |
| 54 | ); |
| 55 | return { client, sent, attempts: () => attempt }; |
| 56 | }; |
| 57 | |
| 58 | const run = <A>(stream: Stream.Stream<A>, client: WriteStreamClient<A>) => |
| 59 | stream.pipe( |
| 60 | Stream.run(makeStreamSink(client)), |
| 61 | Effect.provideService(RuntimeContext, {} as never), |
| 62 | ); |
| 63 | |
| 64 | describe("Cloudflare K2 records and StreamSink", { tags: ["unit", "local"] }, () => { |
| 65 | it("round-trips bytes through standard base64", () => { |
| 66 | const bytes = Uint8Array.from({ length: 70_000 }, (_, i) => i % 256); |
| 67 | expect(fromBase64(toBase64(bytes))).toEqual(bytes); |
| 68 | expect(toBase64(new TextEncoder().encode("hi?"))).toEqual("aGk/"); |
| 69 | }); |
| 70 | |
| 71 | it("encodes strings as UTF-8 and drops empty headers", () => { |
| 72 | const [a, b] = encodeRecords([ |
| 73 | { content: "é", headers: {} }, |
| 74 | { content: new ArrayBuffer(2), headers: { k: "v" } }, |
| 75 | ]); |
| 76 | expect(a!.content).toEqual(new Uint8Array([0xc3, 0xa9])); |
| 77 | expect(a!.headers).toBeUndefined(); |
| 78 | expect(b!.content.byteLength).toEqual(2); |
| 79 | expect(toProduceRecords([b!])).toEqual([{ content: "AAA=", headers: { k: "v" } }]); |
| 80 | }); |
| 81 | |
| 82 | it.effect("decodes consumed JSON records with a schema", () => |
| 83 | Effect.gen(function* () { |
| 84 | const consumed = fromConsumedRecords([ |
| 85 | { timestampMs: 1000, content: toBase64(new TextEncoder().encode('{"id":7}')) }, |
| 86 | ]); |
| 87 | expect(consumed[0]!.timestamp.getTime()).toEqual(1000); |
| 88 | expect(consumed[0]!.headers).toEqual({}); |
| 89 | const [decoded] = yield* decodeWithSchema(Order)(consumed); |
| 90 | expect(decoded!.value).toEqual({ id: 7 }); |
| 91 | |
| 92 | const bad = fromConsumedRecords([ |
| 93 | { timestampMs: 0, content: toBase64(new TextEncoder().encode("not json")) }, |
| 94 | ]); |
| 95 | const error = yield* decodeWithSchema(Order)(bad).pipe(Effect.flip); |
| 96 | expect(error._tag).toEqual("K2SchemaError"); |
| 97 | }), |
| 98 | ); |
| 99 | |
| 100 | it("packs requests under 5 MB of wire size, preserving order", () => { |
| 101 | const records = Array.from({ length: 12 }, () => record(1_000_000)); |
| 102 | const batches = packStreamSinkBatches(records); |
| 103 | expect(batches.map((b) => b.length)).toEqual([3, 3, 3, 3]); |
| 104 | for (const batch of batches) { |
| 105 | expect(batch.reduce((sum, r) => sum + wireSize(r), 0)).toBeLessThanOrEqual(MAX_REQUEST_BYTES); |
| 106 | } |
| 107 | expect(batches.flat()).toEqual(records); |
| 108 | }); |
| 109 | |
| 110 | it("ships an oversized record alone", () => { |
| 111 | const huge = record(MAX_REQUEST_BYTES); |
| 112 | expect(packStreamSinkBatches([record(1), huge, record(1)]).map((b) => b.length)).toEqual([ |
| 113 | 1, 1, 1, |
| 114 | ]); |
| 115 | }); |
| 116 | |
| 117 | it.effect("sends one request per chunk with JSON headers", () => |
| 118 | Effect.gen(function* () { |
| 119 | const { client, sent } = fakeClient(); |
| 120 | yield* run( |
| 121 | Stream.fromIterable([1, 2, 3, 4, 5].map((id) => ({ id }))).pipe(Stream.rechunk(2)), |
| 122 | client, |
| 123 | ); |
| 124 | expect(sent.map((b) => b.length)).toEqual([2, 2, 1]); |
| 125 | expect(sent[0]![0]!.headers).toEqual({ "content-type": "application/json" }); |
| 126 | expect(new TextDecoder().decode(sent[2]![0]!.content)).toEqual('{"id":5}'); |
| 127 | }), |
| 128 | ); |
| 129 | |
| 130 | // `it.live`: the retry schedule sleeps on the real clock. |
| 131 | it.live("retries K2Unavailable (not stored) a bounded number of times", () => |
| 132 | Effect.gen(function* () { |
| 133 | const { client, sent, attempts } = fakeClient(["K2Unavailable", "K2Unavailable"]); |
| 134 | yield* run(Stream.make({ id: 1 }), client); |
| 135 | expect(attempts()).toEqual(3); |
| 136 | expect(sent.length).toEqual(1); |
| 137 | }), |
| 138 | ); |
| 139 | |
| 140 | it.effect("never retries K2AppendOutcomeUnknown (may have been stored)", () => |
| 141 | Effect.gen(function* () { |
| 142 | const { client, attempts } = fakeClient(["K2AppendOutcomeUnknown"]); |
| 143 | const error = yield* run(Stream.make({ id: 1 }), client).pipe(Effect.flip); |
| 144 | expect(error._tag).toEqual("K2AppendOutcomeUnknown"); |
| 145 | expect(attempts()).toEqual(1); |
| 146 | }), |
| 147 | ); |
| 148 | |
| 149 | it.effect("maps native binding results onto the HTTP API's typed tags", () => |
| 150 | Effect.gen(function* () { |
| 151 | const binding = ( |
| 152 | result: Awaited<ReturnType<K2StreamBinding["send"]>> | Error, |
| 153 | ): K2StreamBinding => ({ |
| 154 | send: () => (result instanceof Error ? Promise.reject(result) : Promise.resolve(result)), |
| 155 | }); |
| 156 | const records = [{ content: new Uint8Array([1]) }]; |
| 157 | yield* sendViaBinding(binding({ success: true }), records); |
| 158 | |
| 159 | const failure = (code: number) => |
| 160 | sendViaBinding( |
| 161 | binding({ success: false, error: { code, message: "nope", retryable: false } }), |
| 162 | records, |
| 163 | ).pipe( |
| 164 | Effect.flip, |
| 165 | Effect.map((e) => e._tag), |
| 166 | ); |
| 167 | expect(yield* failure(10200)).toEqual("K2StreamNotFound"); |
| 168 | expect(yield* failure(10204)).toEqual("K2InvalidRequest"); |
| 169 | expect(yield* failure(10206)).toEqual("K2RequestTooLarge"); |
| 170 | expect(yield* failure(10207)).toEqual("K2RecordTooLarge"); |
| 171 | expect(yield* failure(10211)).toEqual("K2Unavailable"); |
| 172 | expect(yield* failure(10212)).toEqual("K2AppendOutcomeUnknown"); |
| 173 | expect(yield* failure(99999)).toEqual("UnknownCloudflareError"); |
| 174 | |
| 175 | // A rejected RPC means the append outcome is unknown. |
| 176 | const rejected = yield* sendViaBinding(binding(new Error("rpc reset")), records).pipe( |
| 177 | Effect.flip, |
| 178 | ); |
| 179 | expect(rejected._tag).toEqual("K2AppendOutcomeUnknown"); |
| 180 | }), |
| 181 | ); |
| 182 | }); |