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, 03:37 UTC
| 1 | import { expect, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Fiber from "effect/Fiber"; |
| 4 | import * as Stream from "effect/Stream"; |
| 5 | |
| 6 | import { responsePayloads } from "./mcpResponsePayloads.ts"; |
| 7 | |
| 8 | it.effect("reads SSE line endings across byte-chunk boundaries without splitting CRLF", () => |
| 9 | Effect.gen(function* () { |
| 10 | for (const separator of [ |
| 11 | "\n\n", |
| 12 | "\r\r", |
| 13 | "\r\n\r\n", |
| 14 | "\r\n\n", |
| 15 | "\n\r", |
| 16 | "\n\r\n", |
| 17 | "\r\r\n", |
| 18 | "\r\n\r", |
| 19 | ]) { |
| 20 | for (const dataLineSeparator of ["\n", "\r", "\r\n"]) { |
| 21 | const bytes = new TextEncoder().encode( |
| 22 | `: keep-alive${separator}data: {"id":1,${dataLineSeparator}data: "result":{"text":"café😀"}}${separator}` + |
| 23 | 'data: {"id":2,"result":{}}\n\ndata: {"id":3}', |
| 24 | ); |
| 25 | for (let cut = 0; cut <= bytes.length; cut += 1) { |
| 26 | const response = new Response( |
| 27 | new ReadableStream<Uint8Array>({ |
| 28 | start(controller) { |
| 29 | controller.enqueue(bytes.subarray(0, cut)); |
| 30 | controller.enqueue(bytes.subarray(cut)); |
| 31 | controller.close(); |
| 32 | }, |
| 33 | }), |
| 34 | { headers: { "content-type": "text/event-stream" } }, |
| 35 | ); |
| 36 | const payloads = yield* Stream.runCollect( |
| 37 | responsePayloads(response, (cause) => ({ |
| 38 | _tag: "ResponsePayloadError" as const, |
| 39 | cause, |
| 40 | separator, |
| 41 | dataLineSeparator, |
| 42 | cut, |
| 43 | })), |
| 44 | ); |
| 45 | expect( |
| 46 | Array.from(payloads), |
| 47 | JSON.stringify({ separator, dataLineSeparator, cut }), |
| 48 | ).toEqual([ |
| 49 | { id: 1, result: { text: "café😀" } }, |
| 50 | { id: 2, result: {} }, |
| 51 | ]); |
| 52 | } |
| 53 | } |
| 54 | const response = new Response( |
| 55 | new ReadableStream<Uint8Array>({ |
| 56 | start(controller) { |
| 57 | controller.enqueue(new TextEncoder().encode(`data: {"id":4}${separator}`)); |
| 58 | }, |
| 59 | }), |
| 60 | { headers: { "content-type": "text/event-stream" } }, |
| 61 | ); |
| 62 | const payloads = yield* Stream.runCollect( |
| 63 | responsePayloads(response, (cause) => ({ |
| 64 | _tag: "ResponsePayloadError" as const, |
| 65 | cause, |
| 66 | separator, |
| 67 | })).pipe(Stream.take(1)), |
| 68 | ); |
| 69 | expect(Array.from(payloads)).toEqual([{ id: 4 }]); |
| 70 | } |
| 71 | }), |
| 72 | ); |
| 73 | |
| 74 | it.effect("reads a multi-megabyte SSE event delivered in small chunks", () => |
| 75 | Effect.gen(function* () { |
| 76 | const text = "x".repeat(8 * 1024 * 1024); |
| 77 | const bytes = new TextEncoder().encode( |
| 78 | `data: ${JSON.stringify({ id: 1, result: { text } })}\n\n`, |
| 79 | ); |
| 80 | let offset = 0; |
| 81 | const response = new Response( |
| 82 | new ReadableStream<Uint8Array>({ |
| 83 | pull(controller) { |
| 84 | if (offset === bytes.length) return controller.close(); |
| 85 | controller.enqueue(bytes.subarray(offset, offset + 256)); |
| 86 | offset = Math.min(offset + 256, bytes.length); |
| 87 | }, |
| 88 | }), |
| 89 | { headers: { "content-type": "text/event-stream" } }, |
| 90 | ); |
| 91 | const payloads = yield* Stream.runCollect( |
| 92 | responsePayloads(response, (cause) => ({ _tag: "ResponsePayloadError" as const, cause })), |
| 93 | ); |
| 94 | expect(Array.from(payloads)).toEqual([{ id: 1, result: { text } }]); |
| 95 | }), |
| 96 | ); |
| 97 | |
| 98 | it.effect("picks the SSE reader from the media type alone, whatever its case", () => |
| 99 | Effect.gen(function* () { |
| 100 | const response = new Response('data: {"id":1}\n\n', { |
| 101 | headers: { "content-type": "Text/Event-Stream; charset=utf-8" }, |
| 102 | }); |
| 103 | const payloads = yield* Stream.runCollect( |
| 104 | responsePayloads(response, (cause) => ({ _tag: "ResponsePayloadError" as const, cause })), |
| 105 | ); |
| 106 | expect(Array.from(payloads)).toEqual([{ id: 1 }]); |
| 107 | |
| 108 | const json = new Response('{"jsonrpc":"2.0","id":1,"result":{}}', { |
| 109 | headers: { |
| 110 | "content-type": 'application/json; profile="https://example.com/Text/Event-Stream"', |
| 111 | }, |
| 112 | }); |
| 113 | const jsonPayloads = yield* Stream.runCollect( |
| 114 | responsePayloads(json, (cause) => ({ _tag: "ResponsePayloadError" as const, cause })), |
| 115 | ); |
| 116 | expect(Array.from(jsonPayloads)).toEqual([{ jsonrpc: "2.0", id: 1, result: {} }]); |
| 117 | }), |
| 118 | ); |
| 119 | |
| 120 | it.effect.each(["text/event-stream", "application/json"])( |
| 121 | "releases a stalled %s response body when its consumer is interrupted", |
| 122 | (contentType) => |
| 123 | Effect.gen(function* () { |
| 124 | const reading = Promise.withResolvers<void>(); |
| 125 | let controller!: ReadableStreamDefaultController<Uint8Array>; |
| 126 | const body = new ReadableStream<Uint8Array>( |
| 127 | { |
| 128 | start(value) { |
| 129 | controller = value; |
| 130 | }, |
| 131 | pull() { |
| 132 | reading.resolve(); |
| 133 | }, |
| 134 | }, |
| 135 | { highWaterMark: 0 }, |
| 136 | ); |
| 137 | const response = new Response(body, { headers: { "content-type": contentType } }); |
| 138 | const consumer = yield* Stream.runDrain(responsePayloads(response, String)).pipe( |
| 139 | Effect.forkChild, |
| 140 | ); |
| 141 | yield* Effect.promise(() => reading.promise); |
| 142 | yield* Fiber.interrupt(consumer); |
| 143 | try { |
| 144 | expect( |
| 145 | () => controller.enqueue(new TextEncoder().encode("late response bytes")), |
| 146 | "interrupted MCP response must stop accepting body bytes", |
| 147 | ).toThrow(TypeError); |
| 148 | expect(body.locked, "interrupted MCP response must release its body").toBe(false); |
| 149 | } finally { |
| 150 | if (body.locked) controller.close(); |
| 151 | } |
| 152 | }), |
| 153 | ); |