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 { describe, expect, it } from "@alchemy.run/test"; |
| 2 | import * as Cause from "effect/Cause"; |
| 3 | import * as Data from "effect/Data"; |
| 4 | import * as Duration from "effect/Duration"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Exit from "effect/Exit"; |
| 7 | import * as Redacted from "effect/Redacted"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { |
| 10 | registerRpcErrorClasses, |
| 11 | unwrapRpcHandlers, |
| 12 | wrapRpcHandlers, |
| 13 | type RpcWrapped, |
| 14 | } from "@/Local/RpcSerialization.ts"; |
| 15 | import * as Output from "@/Output.ts"; |
| 16 | |
| 17 | class RpcRegisteredError extends Data.TaggedError("RpcRegisteredError")<{ |
| 18 | readonly message: string; |
| 19 | readonly arn: string; |
| 20 | readonly secret: Redacted.Redacted<string>; |
| 21 | }> {} |
| 22 | |
| 23 | class RpcUnregisteredError extends Data.TaggedError("RpcUnregisteredError")<{ |
| 24 | readonly message: string; |
| 25 | readonly code: number; |
| 26 | }> {} |
| 27 | |
| 28 | /** |
| 29 | * Builds a client whose wrap→unwrap path mirrors the production wire: |
| 30 | * `unwrapRpcEffectHandler` serializes its args; capnweb JSON-encodes them |
| 31 | * over the websocket; `wrapRpcEffectHandler` deserializes them on the other |
| 32 | * end. We thread `JSON.parse(JSON.stringify(...))` between the wrap and |
| 33 | * unwrap layers to model that hop. Streams skip the JSON hop because they |
| 34 | * are bridged via a real `ReadableStream`. |
| 35 | */ |
| 36 | const roundTrip = <T extends Record<string, any>>(handlers: T, streamKeys?: Array<keyof T>): T => { |
| 37 | const wrapped = wrapRpcHandlers(handlers, streamKeys); |
| 38 | const piped = Object.fromEntries( |
| 39 | Object.entries(wrapped).map(([key, value]) => { |
| 40 | if (typeof value !== "function") { |
| 41 | return [key, value]; |
| 42 | } |
| 43 | if (streamKeys?.includes(key as keyof T)) { |
| 44 | // Streams bypass JSON; the underlying ReadableStream is shipped. |
| 45 | return [key, value]; |
| 46 | } |
| 47 | const fn = value as (args: Array<unknown>) => Promise<unknown>; |
| 48 | const piped = async (args: Array<unknown>) => { |
| 49 | const wireArgs = JSON.parse(JSON.stringify(args)); |
| 50 | const result = await fn(wireArgs); |
| 51 | return JSON.parse(JSON.stringify(result)); |
| 52 | }; |
| 53 | return [key, piped]; |
| 54 | }), |
| 55 | ) as RpcWrapped<T>; |
| 56 | return unwrapRpcHandlers<T>(piped, streamKeys) as T; |
| 57 | }; |
| 58 | |
| 59 | describe("Local.RpcSerialization", { tags: ["unit", "local"] }, () => { |
| 60 | describe("argument serialization", () => { |
| 61 | it.effect("round-trips a top-level Redacted argument", () => |
| 62 | Effect.gen(function* () { |
| 63 | const handlers = { |
| 64 | echo: (s: Redacted.Redacted<string>) => Effect.succeed(Redacted.value(s)), |
| 65 | }; |
| 66 | const client = roundTrip(handlers); |
| 67 | expect(yield* client.echo(Redacted.make("hush"))).toBe("hush"); |
| 68 | }), |
| 69 | ); |
| 70 | |
| 71 | it.effect("round-trips a Redacted nested inside an object", () => |
| 72 | Effect.gen(function* () { |
| 73 | const handlers = { |
| 74 | password: (env: { password: Redacted.Redacted<string> }) => |
| 75 | Effect.succeed(Redacted.value(env.password)), |
| 76 | }; |
| 77 | const client = roundTrip(handlers); |
| 78 | expect(yield* client.password({ password: Redacted.make("hush") })).toBe("hush"); |
| 79 | }), |
| 80 | ); |
| 81 | |
| 82 | it.effect("round-trips a Duration nested inside an object", () => |
| 83 | Effect.gen(function* () { |
| 84 | const handlers = { |
| 85 | timeout: (env: { timeout: Duration.Duration }) => |
| 86 | Effect.succeed(Duration.toSeconds(env.timeout)), |
| 87 | }; |
| 88 | const client = roundTrip(handlers); |
| 89 | expect(yield* client.timeout({ timeout: Duration.seconds(15) })).toBe(15); |
| 90 | }), |
| 91 | ); |
| 92 | |
| 93 | it.effect("round-trips Duration.negativeInfinity", () => |
| 94 | Effect.gen(function* () { |
| 95 | const handlers = { |
| 96 | echo: (d: Duration.Duration) => Effect.succeed(d), |
| 97 | }; |
| 98 | const client = roundTrip(handlers); |
| 99 | const result = yield* client.echo(Duration.negativeInfinity); |
| 100 | expect(Duration.equals(result, Duration.negativeInfinity)).toBe(true); |
| 101 | }), |
| 102 | ); |
| 103 | |
| 104 | it.effect("preserves objects with their own toJSON (Date)", () => |
| 105 | Effect.gen(function* () { |
| 106 | const handlers = { |
| 107 | withDate: (env: { when: Date | string }) => Effect.succeed(String(env.when)), |
| 108 | }; |
| 109 | const client = roundTrip(handlers); |
| 110 | const d = new Date("2026-05-16T12:00:00.000Z"); |
| 111 | expect(yield* client.withDate({ when: d })).toBe(d.toISOString()); |
| 112 | }), |
| 113 | ); |
| 114 | |
| 115 | it.effect("drops function-valued args (replaced with null)", () => |
| 116 | Effect.gen(function* () { |
| 117 | const handlers = { |
| 118 | take: (arg: { cb: (() => void) | null }) => |
| 119 | Effect.succeed(arg.cb === null ? "null" : typeof arg.cb), |
| 120 | }; |
| 121 | const client = roundTrip(handlers); |
| 122 | expect(yield* client.take({ cb: () => {} })).toBe("null"); |
| 123 | }), |
| 124 | ); |
| 125 | |
| 126 | it.effect("serializes arrays element-wise", () => |
| 127 | Effect.gen(function* () { |
| 128 | const handlers = { |
| 129 | sum: (xs: ReadonlyArray<Redacted.Redacted<number>>) => |
| 130 | Effect.succeed(xs.reduce((a, x) => a + Redacted.value(x), 0)), |
| 131 | }; |
| 132 | const client = roundTrip(handlers); |
| 133 | const result = yield* client.sum([Redacted.make(1), Redacted.make(2), Redacted.make(3)]); |
| 134 | expect(result).toBe(6); |
| 135 | }), |
| 136 | ); |
| 137 | |
| 138 | it.effect("converts Output sentinel into a NamedExpr", () => |
| 139 | Effect.gen(function* () { |
| 140 | let received: unknown; |
| 141 | const handlers = { |
| 142 | take: (arg: { val: Output.Output<string> }) => |
| 143 | Effect.sync(() => { |
| 144 | received = arg.val; |
| 145 | return "ok"; |
| 146 | }), |
| 147 | }; |
| 148 | const client = roundTrip(handlers); |
| 149 | const fakeOutput = new Output.NamedExpr( |
| 150 | new Output.EffectExpr(Output.VoidExpr, () => Effect.succeed("x")), |
| 151 | "myBinding", |
| 152 | ); |
| 153 | const result = yield* client.take({ |
| 154 | val: fakeOutput as unknown as Output.Output<string>, |
| 155 | }); |
| 156 | expect(result).toBe("ok"); |
| 157 | expect(Output.isOutput(received)).toBe(true); |
| 158 | expect((received as Output.NamedExpr<unknown>).kind).toBe("NamedExpr"); |
| 159 | }), |
| 160 | ); |
| 161 | }); |
| 162 | |
| 163 | describe("effect exit serialization", () => { |
| 164 | it.effect("propagates success", () => |
| 165 | Effect.gen(function* () { |
| 166 | const handlers = { |
| 167 | ok: () => Effect.succeed(42), |
| 168 | }; |
| 169 | expect(yield* roundTrip(handlers).ok()).toBe(42); |
| 170 | }), |
| 171 | ); |
| 172 | |
| 173 | it.effect("propagates Fail (typed error)", () => |
| 174 | Effect.gen(function* () { |
| 175 | const handlers = { |
| 176 | boom: (): Effect.Effect<never, { _tag: "Boom"; msg: string }> => |
| 177 | Effect.fail({ _tag: "Boom" as const, msg: "kaboom" }), |
| 178 | }; |
| 179 | const exit = yield* Effect.exit(roundTrip(handlers).boom()); |
| 180 | expect(Exit.isFailure(exit)).toBe(true); |
| 181 | if (Exit.isFailure(exit)) { |
| 182 | expect(Cause.hasFails(exit.cause)).toBe(true); |
| 183 | const found = Cause.findErrorOption(exit.cause); |
| 184 | expect(found._tag).toBe("Some"); |
| 185 | if (found._tag === "Some") { |
| 186 | expect(found.value).toEqual({ _tag: "Boom", msg: "kaboom" }); |
| 187 | } |
| 188 | } |
| 189 | }), |
| 190 | ); |
| 191 | |
| 192 | it.effect("revives a registered tagged error class with its fields", () => |
| 193 | Effect.gen(function* () { |
| 194 | registerRpcErrorClasses(RpcRegisteredError); |
| 195 | const handlers = { |
| 196 | boom: (): Effect.Effect<never, RpcRegisteredError> => |
| 197 | Effect.fail( |
| 198 | new RpcRegisteredError({ |
| 199 | message: "did not stabilize", |
| 200 | arn: "arn:aws:ecs:us-east-1:000000000000:task-definition/app:3", |
| 201 | secret: Redacted.make("hush"), |
| 202 | }), |
| 203 | ), |
| 204 | }; |
| 205 | const exit = yield* Effect.exit(roundTrip(handlers).boom()); |
| 206 | const found = Exit.isFailure(exit) ? Cause.findErrorOption(exit.cause) : undefined; |
| 207 | expect(found?._tag).toBe("Some"); |
| 208 | if (found?._tag === "Some") { |
| 209 | const error = found.value; |
| 210 | expect(error).toBeInstanceOf(RpcRegisteredError); |
| 211 | expect(error).toBeInstanceOf(Error); |
| 212 | expect(error._tag).toBe("RpcRegisteredError"); |
| 213 | expect(error.message).toBe("did not stabilize"); |
| 214 | expect(error.arn).toBe("arn:aws:ecs:us-east-1:000000000000:task-definition/app:3"); |
| 215 | expect(Redacted.value(error.secret)).toBe("hush"); |
| 216 | } |
| 217 | }), |
| 218 | ); |
| 219 | |
| 220 | it.effect("keeps an unregistered tagged error's tag and fields for catchTag", () => |
| 221 | Effect.gen(function* () { |
| 222 | const handlers = { |
| 223 | boom: (): Effect.Effect<never, RpcUnregisteredError> => |
| 224 | Effect.fail(new RpcUnregisteredError({ message: "nope", code: 7 })), |
| 225 | }; |
| 226 | const caught = yield* roundTrip(handlers) |
| 227 | .boom() |
| 228 | .pipe(Effect.catchTag("RpcUnregisteredError", (error) => Effect.succeed(error))); |
| 229 | expect(caught._tag).toBe("RpcUnregisteredError"); |
| 230 | expect(caught.code).toBe(7); |
| 231 | expect(caught.message).toBe("nope"); |
| 232 | }), |
| 233 | ); |
| 234 | |
| 235 | it.effect("propagates Die (defect)", () => |
| 236 | Effect.gen(function* () { |
| 237 | const handlers = { |
| 238 | boom: () => Effect.die(new Error("defect")), |
| 239 | }; |
| 240 | const exit = yield* Effect.exit(roundTrip(handlers).boom()); |
| 241 | expect(Exit.isFailure(exit)).toBe(true); |
| 242 | if (Exit.isFailure(exit)) { |
| 243 | expect(Cause.hasDies(exit.cause)).toBe(true); |
| 244 | } |
| 245 | }), |
| 246 | ); |
| 247 | |
| 248 | it.effect("propagates Interrupt", () => |
| 249 | Effect.gen(function* () { |
| 250 | const handlers = { |
| 251 | boom: () => Effect.interrupt, |
| 252 | }; |
| 253 | const exit = yield* Effect.exit(roundTrip(handlers).boom()); |
| 254 | expect(Exit.isFailure(exit)).toBe(true); |
| 255 | if (Exit.isFailure(exit)) { |
| 256 | expect(Cause.hasInterrupts(exit.cause)).toBe(true); |
| 257 | } |
| 258 | }), |
| 259 | ); |
| 260 | }); |
| 261 | |
| 262 | describe("return value serialization", () => { |
| 263 | it.effect("round-trips a Redacted return value", () => |
| 264 | Effect.gen(function* () { |
| 265 | const handlers = { |
| 266 | secret: () => Effect.succeed(Redacted.make("hush")), |
| 267 | }; |
| 268 | const client = roundTrip(handlers); |
| 269 | const result = yield* client.secret(); |
| 270 | expect(Redacted.isRedacted(result)).toBe(true); |
| 271 | expect(Redacted.value(result)).toBe("hush"); |
| 272 | }), |
| 273 | ); |
| 274 | |
| 275 | it.effect("round-trips Redacted values nested in a returned object", () => |
| 276 | Effect.gen(function* () { |
| 277 | const handlers = { |
| 278 | env: () => |
| 279 | Effect.succeed({ |
| 280 | env: { TOKEN: Redacted.make("t0k3n") }, |
| 281 | plain: "value", |
| 282 | }), |
| 283 | }; |
| 284 | const client = roundTrip(handlers); |
| 285 | const result = yield* client.env(); |
| 286 | expect(Redacted.value(result.env.TOKEN)).toBe("t0k3n"); |
| 287 | expect(result.plain).toBe("value"); |
| 288 | }), |
| 289 | ); |
| 290 | }); |
| 291 | |
| 292 | describe("streams", () => { |
| 293 | it.effect("round-trips a stream when streamKeys is honored", () => |
| 294 | Effect.gen(function* () { |
| 295 | const handlers = { |
| 296 | tail: (_n: number) => Stream.fromIterable([1, 2, 3, 4]), |
| 297 | }; |
| 298 | const client = roundTrip(handlers, ["tail"] as const); |
| 299 | const out: Array<number> = []; |
| 300 | yield* Stream.runForEach(client.tail(0), (x) => |
| 301 | Effect.sync(() => { |
| 302 | out.push(x); |
| 303 | }), |
| 304 | ); |
| 305 | expect(out).toEqual([1, 2, 3, 4]); |
| 306 | }), |
| 307 | ); |
| 308 | }); |
| 309 | |
| 310 | describe("nested handlers", () => { |
| 311 | it.effect("recurses into nested handler objects", () => |
| 312 | Effect.gen(function* () { |
| 313 | const handlers = { |
| 314 | group: { |
| 315 | echo: (s: Redacted.Redacted<string>) => Effect.succeed(Redacted.value(s)), |
| 316 | }, |
| 317 | }; |
| 318 | const client = roundTrip(handlers); |
| 319 | expect(yield* client.group.echo(Redacted.make("nested"))).toBe("nested"); |
| 320 | }), |
| 321 | ); |
| 322 | }); |
| 323 | }); |