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

main
ts 441 lines 15.3 KB
Raw
1 import { NodeHttpServer, NodeSocket, NodeSocketServer } from "@effect/platform-node"
2 import { assert, describe, it } from "@effect/vitest"
3 import { Cause, Deferred, Effect, Exit, Fiber, Layer, Ref, Schedule, Schema, Stream } from "effect"
4 import { Entity, EntityProxy, EntityProxyServer, Sharding } from "effect/cluster"
5 import { HttpClient, HttpClientRequest, HttpRouter, HttpServer } from "effect/http"
6 import type * as NetAddress from "effect/net/NetAddress"
7 import { Rpc, RpcClient, RpcGroup, RpcSerialization, RpcServer, RpcTest } from "effect/rpc"
8 import { SocketServer } from "effect/socket"
9 import { e2eSuite, UsersClient } from "./fixtures/rpc-e2e.ts"
10 import { RpcLayer, User } from "./fixtures/rpc-schemas.ts"
11
12 describe("RpcServer", () => {
13 describe("request defect isolation over HTTP", () => {
14 const Ok = Rpc.make("Ok", { success: Schema.String })
15 const serverGroup = RpcGroup.make(
16 Ok,
17 Rpc.make("Invalid", { payload: { value: Schema.String } })
18 )
19 const clientGroup = RpcGroup.make(
20 Ok,
21 Rpc.make("Missing"),
22 Rpc.make("MissingWithSchemas", { success: Schema.Number, error: Schema.String }),
23 Rpc.make("Invalid", { payload: { value: Schema.Number } })
24 )
25 const Server = HttpRouter.serve(
26 RpcServer.layer(serverGroup).pipe(
27 Layer.provide(serverGroup.toLayer({
28 Ok: () => Effect.succeed("ok"),
29 Invalid: () => Effect.void
30 })),
31 Layer.provideMerge(RpcServer.layerProtocolHttp({ path: "/rpc" }))
32 ),
33 { disableListenLog: true, disableLogger: true }
34 )
35
36 // Fingerprinted payloads require both peers to share the schema, so a
37 // mismatch is reported as a fingerprint error instead of the original
38 // diagnostic. The defect still only fails its own request.
39 const fingerprintDiagnostic = "Expected matching layout fingerprint"
40 const cases = [
41 {
42 name: "JSON",
43 serialization: RpcSerialization.layerJson,
44 invalidPayload: ["Expected string", "at [\"value\"]"]
45 },
46 {
47 name: "SchemaBinary",
48 serialization: RpcSerialization.layerSchemaBinary(),
49 invalidPayload: ["Missing key", "at [\"value\"]"]
50 },
51 {
52 name: "SchemaBinary with fingerprints",
53 serialization: RpcSerialization.layerSchemaBinary({ fingerprintPayloads: true }),
54 unknownTag: fingerprintDiagnostic,
55 invalidPayload: [fingerprintDiagnostic]
56 }
57 ]
58
59 const assertDie = (exit: Exit.Exit<unknown, unknown>, diagnostics: ReadonlyArray<string>) => {
60 assert(Exit.isFailure(exit))
61 assert.strictEqual(exit.cause.reasons.length, 1)
62 assert.strictEqual(exit.cause.reasons[0]._tag, "Die")
63 const diagnostic = String(Cause.squash(exit.cause))
64 for (const expected of diagnostics) {
65 assert.include(diagnostic, expected)
66 }
67 }
68
69 for (const { invalidPayload, name, serialization, unknownTag } of cases) {
70 const ClientProtocol = RpcClient.layerProtocolHttp({
71 url: "",
72 transformClient: HttpClient.mapRequest(HttpClientRequest.appendUrl("/rpc"))
73 }).pipe(
74 Layer.provideMerge(Server),
75 Layer.provide([NodeHttpServer.layerTest, serialization])
76 )
77
78 for (const tag of ["Missing", "MissingWithSchemas"] as const) {
79 it.effect(`${name}: unknown tag ${tag} fails only its request and the client remains reusable`, () =>
80 Effect.gen(function*() {
81 const client = yield* RpcClient.make(clientGroup)
82 assert.strictEqual(yield* client.Ok(), "ok")
83 const missing = yield* Effect.exit(client[tag]())
84 assert.strictEqual(yield* client.Ok(), "ok")
85 if (unknownTag === undefined) {
86 assert.deepStrictEqual(missing, Exit.die(`Unknown request tag: ${tag}`))
87 } else {
88 assertDie(missing, [unknownTag])
89 }
90 }).pipe(Effect.provide(ClientProtocol)))
91 }
92
93 it.effect(`${name}: an invalid payload fails only its request and the client remains reusable`, () =>
94 Effect.gen(function*() {
95 const client = yield* RpcClient.make(clientGroup)
96 assert.strictEqual(yield* client.Ok(), "ok")
97 const invalid = yield* Effect.exit(client.Invalid({ value: 42 }))
98 assert.strictEqual(yield* client.Ok(), "ok")
99 assertDie(invalid, invalidPayload)
100 }).pipe(Effect.provide(ClientProtocol)))
101 }
102 })
103
104 // http ndjson
105 const HttpProtocol = RpcServer.layerProtocolHttp({ path: "/rpc" })
106 const HttpNdjsonServer = HttpRouter.serve(
107 RpcLayer.pipe(Layer.provideMerge(HttpProtocol)),
108 { disableListenLog: true, disableLogger: true }
109 )
110 const HttpNdjsonClient = UsersClient.layer.pipe(
111 Layer.provide(
112 RpcClient.layerProtocolHttp({
113 url: "",
114 transformClient: HttpClient.mapRequest(HttpClientRequest.appendUrl("/rpc"))
115 })
116 )
117 )
118 const CustomDefectLayer = HttpNdjsonClient.pipe(
119 Layer.provideMerge(HttpNdjsonServer),
120 Layer.provide([NodeHttpServer.layerTest, RpcSerialization.layerNdjson])
121 )
122 e2eSuite(
123 "e2e http ndjson",
124 HttpNdjsonClient.pipe(
125 Layer.provideMerge(HttpNdjsonServer),
126 Layer.provide([NodeHttpServer.layerTest, RpcSerialization.layerNdjson])
127 )
128 )
129 e2eSuite(
130 "e2e http SchemaBinary",
131 HttpNdjsonClient.pipe(
132 Layer.provideMerge(HttpNdjsonServer),
133 Layer.provide([NodeHttpServer.layerTest, RpcSerialization.layerSchemaBinary()])
134 )
135 )
136 e2eSuite(
137 "e2e http jsonrpc",
138 HttpNdjsonClient.pipe(
139 Layer.provideMerge(HttpNdjsonServer),
140 Layer.provide([NodeHttpServer.layerTest, RpcSerialization.layerNdJsonRpc()])
141 )
142 )
143
144 // websocket
145 const WsProtocol = RpcServer.layerProtocolWebsocket({ path: "/rpc" })
146 const HttpWsServer = HttpRouter.serve(
147 RpcLayer.pipe(Layer.provideMerge(WsProtocol)),
148 { disableListenLog: true, disableLogger: true }
149 )
150 const HttpWsClient = UsersClient.layer.pipe(
151 Layer.provide(RpcClient.layerProtocolSocket()),
152 Layer.provide(
153 Effect.gen(function*() {
154 const server = yield* HttpServer.HttpServer
155 const address = server.address as NetAddress.InetAddress
156 return NodeSocket.layerWebSocket(`http://127.0.0.1:${address.port}/rpc`)
157 }).pipe(Layer.unwrap)
158 )
159 )
160 e2eSuite(
161 "e2e ws ndjson",
162 HttpWsClient.pipe(
163 Layer.provideMerge(HttpWsServer),
164 Layer.provide([NodeHttpServer.layerTest, RpcSerialization.layerNdjson])
165 )
166 )
167 e2eSuite(
168 "e2e ws json",
169 HttpWsClient.pipe(
170 Layer.provideMerge(HttpWsServer),
171 Layer.provide([NodeHttpServer.layerTest, RpcSerialization.layerJson])
172 )
173 )
174 e2eSuite(
175 "e2e ws SchemaBinary",
176 HttpWsClient.pipe(
177 Layer.provideMerge(HttpWsServer),
178 Layer.provide([NodeHttpServer.layerTest, RpcSerialization.layerSchemaBinary()])
179 )
180 )
181 e2eSuite(
182 "e2e ws jsonrpc",
183 HttpWsClient.pipe(
184 Layer.provideMerge(HttpWsServer),
185 Layer.provide([NodeHttpServer.layerTest, RpcSerialization.layerJsonRpc()])
186 )
187 )
188
189 // tcp
190 const TcpServer = RpcLayer.pipe(
191 Layer.provideMerge(RpcServer.layerProtocolSocketServer),
192 Layer.provideMerge(NodeSocketServer.layer({ port: 0 }))
193 )
194 const TcpClient = UsersClient.layer.pipe(
195 Layer.provide(RpcClient.layerProtocolSocket()),
196 Layer.provide(
197 Effect.gen(function*() {
198 const server = yield* SocketServer.SocketServer
199 const address = server.address as NetAddress.InetAddress
200 return NodeSocket.layerNet({ port: address.port })
201 }).pipe(Layer.unwrap)
202 )
203 )
204 e2eSuite(
205 "e2e tcp ndjson",
206 TcpClient.pipe(
207 Layer.provideMerge(TcpServer),
208 Layer.provide([NodeHttpServer.layerTest, RpcSerialization.layerNdjson])
209 )
210 )
211 e2eSuite(
212 "e2e tcp SchemaBinary",
213 TcpClient.pipe(
214 Layer.provideMerge(TcpServer),
215 Layer.provide([NodeHttpServer.layerTest, RpcSerialization.layerSchemaBinary()])
216 )
217 )
218 e2eSuite(
219 "e2e tcp jsonrpc",
220 TcpClient.pipe(
221 Layer.provideMerge(TcpServer),
222 Layer.provide([NodeHttpServer.layerTest, RpcSerialization.layerNdJsonRpc()])
223 )
224 )
225
226 // worker
227 // const WorkerClient = UsersClient.layer.pipe(
228 // Layer.provide(RpcClient.layerProtocolWorker({ size: 1 })),
229 // Layer.provide(
230 // NodeWorker.layerPlatform(() =>
231 // CP.fork(new URL("./fixtures/rpc-worker.ts", import.meta.url), {
232 // execPath: "node"
233 // })
234 // )
235 // ),
236 // Layer.merge(Layer.succeed(RpcServer.Protocol, {
237 // supportsAck: true
238 // } as any))
239 // )
240 // e2eSuite("e2e worker", WorkerClient)
241
242 describe("RpcTest", () => {
243 it.effect("works", () =>
244 Effect.gen(function*() {
245 const client = yield* UsersClient
246 const user = yield* client.GetUser({ id: "1" })
247 assert.deepStrictEqual(user, new User({ id: "1", name: "Logged in user" }))
248 }).pipe(Effect.provide(UsersClient.layerTest)))
249 })
250
251 describe("custom defect schema", () => {
252 it.effect("preserves full defect with custom schema", () =>
253 Effect.gen(function*() {
254 const client = yield* UsersClient
255 const cause = yield* client.ProduceDefectCustom().pipe(
256 Effect.sandbox,
257 Effect.flip
258 )
259 const defect = Cause.squash(cause)
260 assert.instanceOf(defect, Error)
261 assert.strictEqual(defect.name, "CustomDefect")
262 assert.strictEqual(defect.message, "detailed error")
263 assert.strictEqual(defect.stack, "Error: detailed error\n at handler.ts:1")
264 }).pipe(Effect.provide(CustomDefectLayer)))
265 })
266
267 describe("entity proxy", () => {
268 it.effect("provides handler context for generated rpc handlers", () =>
269 Effect.gen(function*() {
270 const TestEntity = Entity.make("TestEntity", [Rpc.make("NoPayload")])
271 const TestEntityRpcs = EntityProxy.toRpcGroup(TestEntity)
272 const called = yield* Deferred.make<void>()
273 const testClient = (entityId: string) => ({
274 NoPayload: Effect.fnUntraced(function*(payload: void, options?: { readonly discard?: boolean }) {
275 assert.strictEqual(entityId, "id")
276 assert.strictEqual(payload, undefined)
277 assert.strictEqual(options?.discard, true)
278 yield* Deferred.succeed(called, undefined)
279 })
280 })
281 const sharding = Sharding.Sharding.of({
282 ...({} as Sharding.Sharding["Service"]),
283 isShutdown: Effect.succeed(false),
284 makeClient: () => Effect.succeed(testClient) as never,
285 pollStorage: Effect.void
286 })
287
288 const client = yield* RpcTest.makeClient(TestEntityRpcs).pipe(
289 Effect.provide(EntityProxyServer.layerRpcHandlers(TestEntity)),
290 Effect.provideService(Sharding.Sharding, sharding)
291 )
292
293 yield* client["TestEntity.NoPayloadDiscard"]({
294 entityId: "id",
295 payload: undefined
296 })
297 yield* Deferred.await(called)
298 }))
299 })
300
301 // Asserts a failing sibling call does not affect an in-flight Ticker stream on the same connection
302 const Ticker = Rpc.make("Ticker", {
303 success: Schema.Number,
304 stream: true
305 })
306
307 const IsolationClient = RpcClient.layerProtocolSocket().pipe(
308 Layer.provide(
309 Effect.gen(function*() {
310 const server = yield* SocketServer.SocketServer
311 const address = server.address as NetAddress.InetAddress
312 return NodeSocket.layerNet({ port: address.port })
313 }).pipe(Layer.unwrap)
314 ),
315 Layer.provide(RpcSerialization.layerNdjson)
316 )
317
318 const assertTickerSurvives = Effect.fnUntraced(function*<E, R>(
319 setup: Effect.Effect<
320 {
321 readonly ticker: Stream.Stream<number, E>
322 readonly failingCall: Effect.Effect<void>
323 },
324 never,
325 R
326 >,
327 label: string
328 ) {
329 const { failingCall, ticker } = yield* setup
330
331 const received = yield* Ref.make<Array<number>>([])
332
333 const tickerFiber = yield* ticker.pipe(
334 Stream.runForEach((value) => Ref.update(received, (xs) => [...xs, value])),
335 Effect.forkChild
336 )
337
338 yield* Effect.retry(
339 Effect.flatMap(
340 Ref.get(received),
341 (xs) => xs.length >= 2 ? Effect.void : Effect.fail("not enough ticks yet")
342 ),
343 { schedule: Schedule.spaced("50 millis"), times: 200 }
344 )
345
346 const ticksBefore = (yield* Ref.get(received)).length
347 assert.isAtLeast(ticksBefore, 2)
348
349 yield* failingCall
350
351 yield* Effect.sleep("300 millis")
352
353 const ticksAfter = (yield* Ref.get(received)).length
354 const tickerStatus = tickerFiber.pollUnsafe()
355
356 yield* Fiber.interrupt(tickerFiber)
357
358 assert.isUndefined(tickerStatus, `Ticker stream must still be running after ${label}`)
359 assert.isAbove(ticksAfter, ticksBefore, `Ticker stream must keep emitting after ${label}`)
360 })
361
362 describe("unknown-tag isolation", () => {
363 const Ghost = Rpc.make("Ghost", {
364 payload: { value: Schema.String },
365 success: Schema.String
366 })
367
368 const serverGroup = RpcGroup.make(Ticker)
369 const clientGroup = RpcGroup.make(Ticker, Ghost)
370
371 const TickerHandlers = serverGroup.toLayer({
372 Ticker: () => Stream.fromSchedule(Schedule.spaced("60 millis"))
373 })
374
375 const IsolationServer = RpcServer.layer(serverGroup).pipe(
376 Layer.provide(TickerHandlers),
377 Layer.provideMerge(RpcServer.layerProtocolSocketServer),
378 Layer.provideMerge(NodeSocketServer.layer({ port: 0 })),
379 Layer.provide(RpcSerialization.layerNdjson)
380 )
381
382 it.live(
383 "an unknown request tag fails only its own request, not other in-flight streams on the same connection",
384 () =>
385 assertTickerSurvives(
386 Effect.map(RpcClient.make(clientGroup), (client) => ({
387 ticker: client.Ticker(),
388 failingCall: Effect.gen(function*() {
389 const ghostExit = yield* client.Ghost({ value: "boo" }).pipe(Effect.exit)
390 assert.isTrue(Exit.isFailure(ghostExit), "Ghost call should fail with the routing miss")
391 })
392 })),
393 "the unknown-tag failure"
394 ).pipe(Effect.provide(IsolationClient.pipe(Layer.provideMerge(IsolationServer)))),
395 { timeout: 30_000 }
396 )
397 })
398
399 describe("fatal-defect isolation", () => {
400 const Boom = Rpc.make("Boom", {
401 success: Schema.String
402 })
403
404 const group = RpcGroup.make(Ticker, Boom)
405
406 const Handlers = group.toLayer({
407 Ticker: () => Stream.fromSchedule(Schedule.spaced("60 millis")),
408 Boom: () => Effect.die("boom")
409 })
410
411 const DefectServer = RpcServer.layer(group, { disableFatalDefects: true }).pipe(
412 Layer.provide(Handlers),
413 Layer.provideMerge(RpcServer.layerProtocolSocketServer),
414 Layer.provideMerge(NodeSocketServer.layer({ port: 0 })),
415 Layer.provide(RpcSerialization.layerNdjson)
416 )
417
418 it.live(
419 "with disableFatalDefects a handler defect fails only its own request, not other in-flight streams",
420 () =>
421 assertTickerSurvives(
422 Effect.map(RpcClient.make(group), (client) => ({
423 ticker: client.Ticker(),
424 failingCall: Effect.gen(function*() {
425 const boomExit = yield* client.Boom().pipe(Effect.exit)
426 if (!Exit.isFailure(boomExit)) {
427 return assert.fail("Boom call must fail with the handler defect")
428 }
429 assert.include(
430 String(Cause.squash(boomExit.cause)),
431 "boom",
432 "the caller must receive the handler defect"
433 )
434 })
435 })),
436 "the handler defect"
437 ).pipe(Effect.provide(IsolationClient.pipe(Layer.provideMerge(DefectServer)))),
438 { timeout: 30_000 }
439 )
440 })
441 })