|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import * as Data from "effect/Data"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Stream from "effect/Stream"; |
| 5 | + |
| 6 | +import { responsePayloads } from "./mcpResponsePayloads.ts"; |
| 7 | + |
| 8 | +class McpResponseTestError extends Data.TaggedError("McpResponseTestError")<{ cause: unknown }> {} |
| 9 | + |
| 10 | +describe("responsePayloads", () => { |
| 11 | + it.effect.each([ |
| 12 | + "text/event-stream", |
| 13 | + "text/event-stream; charset=utf-8", |
| 14 | + "Text/Event-Stream; Charset=UTF-8", |
| 15 | + "TEXT/EVENT-STREAM", |
| 16 | + ])("reads SSE payloads with Content-Type %s", (contentType) => |
| 17 | + Effect.gen(function* () { |
| 18 | + const response = new Response( |
| 19 | + 'data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progress":1}}\n\n' + |
| 20 | + 'data: {"jsonrpc":"2.0","id":7,"result":{"text":"Keep This Case"}}\n\n', |
| 21 | + { headers: { "content-type": contentType } }, |
| 22 | + ); |
| 23 | + const payloads = yield* Stream.runCollect( |
| 24 | + responsePayloads(response, (cause) => new McpResponseTestError({ cause })), |
| 25 | + ); |
| 26 | + |
| 27 | + expect(payloads).toEqual([ |
| 28 | + { jsonrpc: "2.0", method: "notifications/progress", params: { progress: 1 } }, |
| 29 | + { jsonrpc: "2.0", id: 7, result: { text: "Keep This Case" } }, |
| 30 | + ]); |
| 31 | + }), |
| 32 | + ); |
| 33 | + |
| 34 | + it.effect.each(["application/json", 'application/json; profile="TEXT/EVENT-STREAM"'])( |
| 35 | + "reads a JSON response with Content-Type %s", |
| 36 | + (contentType) => |
| 37 | + Effect.gen(function* () { |
| 38 | + const response = new Response('{"jsonrpc":"2.0","id":7,"result":{}}', { |
| 39 | + headers: { "content-type": contentType }, |
| 40 | + }); |
| 41 | + const payloads = yield* Stream.runCollect( |
| 42 | + responsePayloads(response, (cause) => new McpResponseTestError({ cause })), |
| 43 | + ); |
| 44 | + |
| 45 | + expect(payloads).toEqual([{ jsonrpc: "2.0", id: 7, result: {} }]); |
| 46 | + }), |
| 47 | + ); |
| 48 | +}); |
0 commit comments