@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 194 lines 6.82 KB
Raw
1 import {
2 makeQueueSink,
3 MAX_BATCH_BYTES,
4 MAX_BATCH_MESSAGES,
5 messageSize,
6 packQueueSinkBatches,
7 } from "@alchemy.run/cloudflare/Queues/QueueSinkBatch";
8 import { SendError, type SendMessage } from "@alchemy.run/cloudflare/Queues/QueueTypes";
9 import type { WriteQueueClient } from "@alchemy.run/cloudflare/Queues/WriteQueue";
10 import { describe, expect, it } from "@alchemy.run/test";
11 import { RuntimeContext } from "alchemy/RuntimeContext";
12 import * as Effect from "effect/Effect";
13 import * as Exit from "effect/Exit";
14 import * as Stream from "effect/Stream";
15
16 /**
17 * Pure tests of the Cloudflare `QueueSink` batching with a fake
18 * `WriteQueue` client. Live delivery is covered by `QueueSink.test.ts`.
19 */
20
21 const fakeClient = (options?: { failOnBatch?: number }) => {
22 const batches: SendMessage[][] = [];
23 const client: WriteQueueClient = {
24 raw: Effect.die("unused"),
25 send: () => Effect.die("unused"),
26 sendBatch: (messages) =>
27 Effect.suspend(() => {
28 if (batches.length === options?.failOnBatch) {
29 return Effect.fail(new SendError({ message: "rejected" }));
30 }
31 batches.push([...messages]);
32 return Effect.void;
33 }),
34 };
35 return { client, batches };
36 };
37
38 const run = <A>(stream: Stream.Stream<A>, client: WriteQueueClient) =>
39 stream.pipe(
40 Stream.run(makeQueueSink(client)),
41 Effect.provideService(RuntimeContext, {} as never),
42 );
43
44 // Synchronous only: restore before the test yields so other fibers cannot see
45 // the replacement. This exercises no_nodejs_compat without a second deployment.
46 const withBuffer = <A>(value: unknown, run: () => A): A => {
47 const descriptor = Object.getOwnPropertyDescriptor(globalThis, "Buffer");
48 try {
49 Object.defineProperty(globalThis, "Buffer", { value, configurable: true });
50 return run();
51 } finally {
52 if (descriptor) Object.defineProperty(globalThis, "Buffer", descriptor);
53 else Reflect.deleteProperty(globalThis, "Buffer");
54 }
55 };
56
57 describe("Cloudflare QueueSink batching", { tags: ["unit", "local"] }, () => {
58 it("packs at most 100 messages per batch, preserving order", () => {
59 const bodies = Array.from({ length: 250 }, (_, i) => i);
60 const batches = packQueueSinkBatches(bodies);
61 expect(batches.map((b) => b.length)).toEqual([100, 100, 50]);
62 expect(batches.flat()).toEqual(bodies);
63 });
64
65 it("packs by total size under 256 KB", () => {
66 const body = "x".repeat(100_000);
67 const batches = packQueueSinkBatches([body, body, body, body, body]);
68 expect(batches.map((b) => b.length)).toEqual([2, 2, 1]);
69 for (const batch of batches) {
70 const bytes = batch.reduce((sum, b) => sum + messageSize(b), 0);
71 expect(bytes).toBeLessThanOrEqual(MAX_BATCH_BYTES);
72 }
73 });
74
75 it("measures UTF-8 JSON bytes plus per-message overhead", () => {
76 expect(messageSize("é")).toBe(4 + 100);
77 expect(messageSize({ a: 1 })).toBe(7 + 100);
78 expect(messageSize(undefined)).toBe(100);
79 });
80
81 for (const native of [true, false]) {
82 it(`measures Unicode JSON exactly with ${native ? "native" : "web fallback"} byte length`, () => {
83 const originalBuffer = globalThis.Buffer;
84 const encoder = new TextEncoder();
85 let nativeCalls = 0;
86 const buffer = native
87 ? {
88 byteLength(value: string, encoding: "utf8") {
89 expect(encoding).toBe("utf8");
90 nativeCalls++;
91 return originalBuffer.byteLength(value, encoding);
92 },
93 }
94 : undefined;
95 withBuffer(buffer, () => {
96 const bodies = [
97 "",
98 'ASCII with "quotes", \\ and \u0000\n',
99 "é漢字🌦️",
100 "\ud800",
101 "\udfff",
102 "\ud800x\udfff",
103 "\ud800\udfff",
104 { "clé🔑": ["😀", "\ud800", "\udfff", null, 123] },
105 "東京🌦️".repeat(10_000),
106 ];
107 for (const body of bodies) {
108 expect(messageSize(body)).toBe(encoder.encode(JSON.stringify(body)).length + 100);
109 }
110 expect(nativeCalls).toBe(native ? bodies.length : 0);
111
112 let toJsonCalls = 0;
113 expect(
114 messageSize({
115 toJSON() {
116 toJsonCalls++;
117 return "é😀";
118 },
119 }),
120 ).toBe(encoder.encode(JSON.stringify("é😀")).length + 100);
121 expect(toJsonCalls).toBe(1);
122
123 const cyclic: { self?: unknown } = {};
124 cyclic.self = cyclic;
125 const callsBeforeUnsupported = nativeCalls;
126 for (const body of [
127 undefined,
128 () => undefined,
129 Symbol("body"),
130 1n,
131 cyclic,
132 {
133 toJSON() {
134 throw new Error("not serializable");
135 },
136 },
137 { toJSON: () => undefined },
138 ]) {
139 expect(messageSize(body)).toBe(100);
140 }
141 expect(nativeCalls).toBe(callsBeforeUnsupported);
142 });
143 });
144
145 it(`packs Unicode messages at the exact byte boundary with ${native ? "native" : "web fallback"} byte length`, () => {
146 withBuffer(native ? globalThis.Buffer : undefined, () => {
147 // Two messages: JSON quotes (4) + metadata (200) + 4 bytes per é pair.
148 const body = "é".repeat((MAX_BATCH_BYTES - 204) / 4);
149 expect(messageSize(body) * 2).toBe(MAX_BATCH_BYTES);
150 expect(packQueueSinkBatches([body, body, "next"]).map((batch) => batch.length)).toEqual([
151 2, 1,
152 ]);
153 expect(packQueueSinkBatches([`${body}é`, `${body}é`]).map((batch) => batch.length)).toEqual(
154 [1, 1],
155 );
156 });
157 });
158 }
159
160 it("ships an oversized message alone", () => {
161 const huge = "x".repeat(MAX_BATCH_BYTES);
162 expect(packQueueSinkBatches(["a", huge, "b"]).map((b) => b.length)).toEqual([1, 1, 1]);
163 });
164
165 it.effect("sends one sendBatch per chunk", () =>
166 Effect.gen(function* () {
167 const { client, batches } = fakeClient();
168 yield* run(Stream.fromIterable([1, 2, 3, 4, 5]).pipe(Stream.rechunk(2)), client);
169 expect(batches.map((b) => b.map((m) => m.body))).toEqual([[1, 2], [3, 4], [5]]);
170 }),
171 );
172
173 it.effect("splits an oversized chunk into consecutive batches", () =>
174 Effect.gen(function* () {
175 const { client, batches } = fakeClient();
176 const bodies = Array.from({ length: 250 }, (_, i) => ({ i }));
177 yield* run(Stream.fromIterable(bodies), client);
178 expect(batches.map((b) => b.length)).toEqual([MAX_BATCH_MESSAGES, MAX_BATCH_MESSAGES, 50]);
179 expect(batches.flat().map((m) => m.body)).toEqual(bodies);
180 }),
181 );
182
183 it.effect("fails the sink with the SendError and stops sending", () =>
184 Effect.gen(function* () {
185 const { client, batches } = fakeClient({ failOnBatch: 1 });
186 const exit = yield* run(
187 Stream.fromIterable(Array.from({ length: 250 }, (_, i) => i)),
188 client,
189 ).pipe(Effect.exit);
190 expect(Exit.isFailure(exit)).toBe(true);
191 expect(batches.length).toBe(1);
192 }),
193 );
194 });