|
| 1 | +import { assert, describe, it } from "@effect/vitest"; |
| 2 | +import { WS_METHODS } from "@t3tools/contracts"; |
| 3 | +import * as Deferred from "effect/Deferred"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as Queue from "effect/Queue"; |
| 6 | +import * as Schema from "effect/Schema"; |
| 7 | +import * as Stream from "effect/Stream"; |
| 8 | +import * as TestClock from "effect/testing/TestClock"; |
| 9 | +import { Rpc, RpcGroup, RpcMessage, RpcSerialization, RpcServer } from "effect/unstable/rpc"; |
| 10 | + |
| 11 | +import { withTerminalOutputWindow } from "./OutputProtocol.ts"; |
| 12 | + |
| 13 | +describe("terminal output window", () => { |
| 14 | + for (const { tag, size, limit } of [ |
| 15 | + { tag: WS_METHODS.terminalAttach, size: 1, limit: 8 }, |
| 16 | + { tag: WS_METHODS.subscribeTerminalEvents, size: 1, limit: 8 }, |
| 17 | + { tag: WS_METHODS.terminalAttach, size: 64 * 1024, limit: 1 }, |
| 18 | + { tag: WS_METHODS.subscribeTerminalMetadata, size: 1, limit: 1 }, |
| 19 | + ]) { |
| 20 | + it.effect(`limits ${tag} with ${size}-byte values to ${limit} pending chunks`, () => |
| 21 | + Effect.gen(function* () { |
| 22 | + const group = RpcGroup.make(Rpc.make(tag, { success: Schema.String, stream: true })); |
| 23 | + const output = yield* Queue.unbounded<string>(); |
| 24 | + const responses = yield* Queue.unbounded<RpcMessage.FromServerEncoded>(); |
| 25 | + const receive = yield* Deferred.make<Parameters<RpcServer.Protocol["Service"]["run"]>[0]>(); |
| 26 | + const protocol = yield* RpcServer.Protocol.make((write) => |
| 27 | + Effect.gen(function* () { |
| 28 | + yield* Deferred.succeed(receive, write); |
| 29 | + const serialization = yield* RpcSerialization.RpcSerialization; |
| 30 | + return { |
| 31 | + disconnects: yield* Queue.unbounded<number>(), |
| 32 | + send: (_clientId, response) => Queue.offer(responses, response), |
| 33 | + end: () => Effect.void, |
| 34 | + clientIds: Effect.succeed(new Set([0])), |
| 35 | + initialMessage: Effect.succeedNone, |
| 36 | + supportsAck: true, |
| 37 | + supportsTransferables: false, |
| 38 | + supportsSpanPropagation: false, |
| 39 | + supportsNotifications: true, |
| 40 | + codecFor: serialization.codecFor, |
| 41 | + }; |
| 42 | + }), |
| 43 | + ); |
| 44 | + yield* RpcServer.make(group).pipe( |
| 45 | + Effect.provide(group.toLayerHandler(tag, () => Stream.fromQueue(output))), |
| 46 | + Effect.provideService(RpcServer.Protocol, withTerminalOutputWindow(protocol)), |
| 47 | + Effect.forkScoped, |
| 48 | + ); |
| 49 | + const write = yield* Deferred.await(receive); |
| 50 | + yield* write(0, { _tag: "Request", id: "1", tag, payload: null, headers: [] }); |
| 51 | + |
| 52 | + for (let index = 0; index < limit; index++) { |
| 53 | + const value = String(index).repeat(size); |
| 54 | + yield* Queue.offer(output, value); |
| 55 | + yield* TestClock.adjust(0); |
| 56 | + assert.equal(yield* Queue.size(responses), 1); |
| 57 | + assert.deepEqual(yield* Queue.take(responses), { |
| 58 | + _tag: "Chunk", |
| 59 | + requestId: "1", |
| 60 | + values: [value], |
| 61 | + }); |
| 62 | + } |
| 63 | + yield* Queue.offer(output, "i".repeat(size)); |
| 64 | + yield* TestClock.adjust(0); |
| 65 | + assert.equal(yield* Queue.size(responses), 0); |
| 66 | + yield* write(0, { _tag: "Ack", requestId: "1" }); |
| 67 | + const resumed = yield* Queue.take(responses); |
| 68 | + assert.equal(resumed._tag, "Chunk"); |
| 69 | + if (resumed._tag === "Chunk") assert.deepEqual(resumed.values, ["i".repeat(size)]); |
| 70 | + |
| 71 | + yield* Queue.offer(output, "j"); |
| 72 | + yield* TestClock.adjust(0); |
| 73 | + assert.equal(yield* Queue.size(responses), 0); |
| 74 | + yield* write(0, { _tag: "Interrupt", requestId: "1" }); |
| 75 | + assert.equal((yield* Queue.take(responses))._tag, "Exit"); |
| 76 | + }).pipe(Effect.provide(RpcSerialization.layerJson), Effect.scoped), |
| 77 | + ); |
| 78 | + } |
| 79 | +}); |
0 commit comments