Skip to content

Commit 9384725

Browse files
PR Batch Testerclaude
andcommitted
test: cut frame-part tests to the two that guard the fix
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2361e80 commit 9384725

2 files changed

Lines changed: 13 additions & 83 deletions

File tree

‎packages/client-runtime/src/rpc/session.test.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,14 @@ describe("RpcSessionFactory", () => {
302302
}),
303303
512,
304304
);
305-
expect(parts.length).toBeGreaterThan(2);
306305

307306
for (const part of parts) {
308307
socket.serverMessage(part);
309308
yield* TestClock.adjust("10 seconds");
310309
}
311310
yield* Fiber.join(readyFiber);
312311

313-
const config = yield* session.initialConfig;
314-
expect(config).toEqual(SERVER_CONFIG);
315-
expect(socket.readyState).toBe(TestWebSocket.OPEN);
312+
expect(yield* session.initialConfig).toEqual(SERVER_CONFIG);
316313
}),
317314
);
318315

Lines changed: 12 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,14 @@
11
import { assert, it } from "@effect/vitest";
2-
import * as Effect from "effect/Effect";
3-
import * as Socket from "effect/socket/Socket";
42

5-
import {
6-
joinSocketFrameParts,
7-
SOCKET_FRAME_PART_BYTES,
8-
splitLargeSocketFrames,
9-
} from "./socketFrameParts.ts";
10-
11-
const unusedUpgrade = () => Effect.void;
12-
13-
function recordingSocket(sent: Array<string | Uint8Array>) {
14-
return Socket.make({
15-
reader: Effect.succeed({ pull: Effect.never, upgrade: unusedUpgrade }),
16-
writer: Effect.succeed({
17-
write: (chunk) =>
18-
Effect.sync(() => {
19-
if (!Socket.isCloseEvent(chunk)) sent.push(chunk);
20-
}),
21-
writeAll: (chunks) => Effect.sync(() => void sent.push(...chunks)),
22-
}),
23-
});
24-
}
25-
26-
function replayingSocket(batches: ReadonlyArray<readonly [string, ...Array<string>]>) {
27-
let next = 0;
28-
return Socket.make({
29-
reader: Effect.succeed({
30-
pull: Effect.suspend(() =>
31-
next < batches.length ? Effect.succeed(batches[next++]!) : Effect.never,
32-
),
33-
upgrade: unusedUpgrade,
34-
}),
35-
writer: Effect.succeed({ write: () => Effect.void, writeAll: () => Effect.void }),
36-
});
37-
}
38-
39-
it.effect("a large message reaches the client whole, with a frame for every part on the way", () =>
40-
Effect.gen(function* () {
41-
// Two-, three- and four-byte characters, the last as surrogate pairs that
42-
// fall across part boundaries.
43-
const message = JSON.stringify({
44-
_tag: "Chunk",
45-
values: ["é€😀x".repeat(SOCKET_FRAME_PART_BYTES / 4)],
46-
});
47-
const sent: Array<string | Uint8Array> = [];
48-
const serverWriter = yield* splitLargeSocketFrames(recordingSocket(sent)).writer;
49-
yield* serverWriter.write(message);
50-
yield* serverWriter.write('{"_tag":"Pong"}');
51-
52-
const parts = sent.filter((frame) => typeof frame === "string");
53-
assert.isAbove(parts.length, 3);
54-
const encoded = parts.map((part) => new TextEncoder().encode(part));
55-
assert.isTrue(encoded.every((bytes) => bytes.length <= SOCKET_FRAME_PART_BYTES));
56-
assert.isTrue(
57-
encoded.every((bytes, index) => new TextDecoder().decode(bytes) === parts[index]),
58-
);
59-
60-
const [first, ...rest] = parts;
61-
const reader = yield* joinSocketFrameParts(replayingSocket([[first!, ...rest]])).reader;
62-
const received = yield* reader.pull;
63-
64-
assert.strictEqual(received.length, parts.length);
65-
const held = received.slice(0, -2);
66-
assert.isTrue(held.every((frame) => JSON.parse(frame as string)._tag === "Pong"));
67-
assert.strictEqual(received.at(-2), message);
68-
assert.strictEqual(received.at(-1), '{"_tag":"Pong"}');
69-
}).pipe(Effect.scoped),
70-
);
71-
72-
it.effect("a server that predates parts still reaches the client whole", () =>
73-
Effect.gen(function* () {
74-
const message = JSON.stringify({
75-
_tag: "Chunk",
76-
values: ["x".repeat(3 * SOCKET_FRAME_PART_BYTES)],
77-
});
78-
const reader = yield* joinSocketFrameParts(replayingSocket([[message]])).reader;
79-
assert.deepStrictEqual(yield* reader.pull, [message]);
80-
}).pipe(Effect.scoped),
81-
);
3+
import { SOCKET_FRAME_PART_BYTES, splitSocketFrame } from "./socketFrameParts.ts";
4+
5+
it("splits a large frame into parts that each fit the budget and stay valid UTF-8", () => {
6+
// Surrogate pairs fall across part boundaries; splitting one would turn it into U+FFFD on the wire.
7+
const parts = splitSocketFrame("é€😀x".repeat(SOCKET_FRAME_PART_BYTES / 4));
8+
assert.isAbove(parts.length, 1);
9+
for (const part of parts) {
10+
const bytes = new TextEncoder().encode(part);
11+
assert.isAtMost(bytes.length, SOCKET_FRAME_PART_BYTES);
12+
assert.strictEqual(new TextDecoder().decode(bytes), part);
13+
}
14+
});

0 commit comments

Comments
 (0)