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, 04:45 UTC
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import * as Cause from "effect/Cause"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Exit from "effect/Exit"; |
| 5 | import * as Fiber from "effect/Fiber"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Logger from "effect/Logger"; |
| 8 | import * as Queue from "effect/Queue"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { Rpc, RpcClient, RpcGroup, RpcServer } from "effect/rpc"; |
| 12 | |
| 13 | import { WS_RPC_SERVER_OPTIONS } from "../ws.ts"; |
| 14 | import * as DefectReporter from "./DefectReporter.ts"; |
| 15 | |
| 16 | class TestRpcs extends RpcGroup.make( |
| 17 | Rpc.make("subscribe", { success: Schema.Number, stream: true }), |
| 18 | Rpc.make("boom", { success: Schema.Void }), |
| 19 | ) {} |
| 20 | type TestRpc = RpcGroup.Rpcs<typeof TestRpcs>; |
| 21 | |
| 22 | const withErrorLogs = <A, E, R>( |
| 23 | body: (logs: Queue.Queue<Cause.Cause<unknown>>) => Effect.Effect<A, E, R>, |
| 24 | ) => |
| 25 | Effect.gen(function* () { |
| 26 | const logs = yield* Queue.unbounded<Cause.Cause<unknown>>(); |
| 27 | const logger = Logger.make(({ cause, logLevel }) => { |
| 28 | if (logLevel === "Error") Queue.offerUnsafe(logs, cause); |
| 29 | }); |
| 30 | return yield* body(logs).pipe( |
| 31 | Effect.provide(Logger.layer([logger], { mergeWithExisting: false })), |
| 32 | ); |
| 33 | }); |
| 34 | |
| 35 | const errorMessage = (cause: Cause.Cause<unknown>) => (Cause.squash(cause) as Error).message; |
| 36 | |
| 37 | describe("DefectReporter", () => { |
| 38 | it.effect("a dying RPC handler fails alone, and its defect is logged", () => |
| 39 | withErrorLogs((logs) => |
| 40 | Effect.gen(function* () { |
| 41 | const subscription = yield* Queue.unbounded<number>(); |
| 42 | let client!: Effect.Success< |
| 43 | ReturnType<typeof RpcClient.makeNoSerialization<TestRpc, never>> |
| 44 | >; |
| 45 | const server = yield* RpcServer.makeNoSerialization(TestRpcs, { |
| 46 | ...WS_RPC_SERVER_OPTIONS, |
| 47 | onFromServer: (response) => client.write(response), |
| 48 | }).pipe( |
| 49 | Effect.provide( |
| 50 | TestRpcs.toLayer({ |
| 51 | subscribe: () => Stream.fromQueue(subscription), |
| 52 | boom: () => Effect.die(new Error("handler bug")), |
| 53 | }).pipe(Layer.provide(DefectReporter.layer)), |
| 54 | ), |
| 55 | ); |
| 56 | client = yield* RpcClient.makeNoSerialization(TestRpcs, { |
| 57 | supportsAck: true, |
| 58 | onFromClient: ({ message }) => server.write(0, message), |
| 59 | }); |
| 60 | |
| 61 | const received = yield* Queue.unbounded<number>(); |
| 62 | const sibling = yield* client.client.subscribe().pipe( |
| 63 | Stream.runForEach((value) => Queue.offer(received, value)), |
| 64 | Effect.forkScoped, |
| 65 | ); |
| 66 | yield* Queue.offer(subscription, 1); |
| 67 | assert.equal(yield* Queue.take(received), 1); |
| 68 | |
| 69 | const boom = yield* Effect.exit(client.client.boom()); |
| 70 | assert.isTrue(Exit.hasDies(boom)); |
| 71 | const logged = yield* Queue.clear(logs); |
| 72 | assert.deepEqual(logged.map(errorMessage), ["handler bug"]); |
| 73 | |
| 74 | yield* Queue.offer(subscription, 2); |
| 75 | const next = yield* Queue.take(received).pipe( |
| 76 | Effect.raceFirst(Fiber.await(sibling).pipe(Effect.as("subscription ended"))), |
| 77 | ); |
| 78 | assert.equal(next, 2); |
| 79 | assert.equal(yield* Queue.size(logs), 0); |
| 80 | }), |
| 81 | ), |
| 82 | ); |
| 83 | }); |