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

main
ts 160 lines 5.62 KB
Raw
1 import { assert, describe, it } from "@effect/vitest"
2 import { Cause, Context, Effect, Fiber, Option, Stream } from "effect"
3 // @ts-ignore
4 // oxlint-disable-next-line @typescript-eslint/no-unused-vars
5 import { NodeInspectSymbol } from "effect/Inspectable"
6 import * as Layer from "effect/Layer"
7 import * as RpcClient from "effect/rpc/RpcClient"
8 import type { RpcClientError } from "effect/rpc/RpcClientError"
9 import type * as RpcGroup from "effect/rpc/RpcGroup"
10 import * as RpcServer from "effect/rpc/RpcServer"
11 import * as RpcTest from "effect/rpc/RpcTest"
12 import { AuthClient, AuthLayer, TimingLayer, User, UserRpcs, UsersLayer } from "./rpc-schemas.ts"
13
14 export class UsersClient extends Context.Service<
15 UsersClient,
16 RpcClient.RpcClient<RpcGroup.Rpcs<typeof UserRpcs>, RpcClientError>
17 >()("UsersClient") {
18 static readonly layer = Layer.effect(UsersClient)(RpcClient.make(UserRpcs)).pipe(
19 Layer.provide(AuthClient)
20 )
21 static layerTest = Layer.effect(UsersClient)(RpcTest.makeClient(UserRpcs)).pipe(
22 Layer.provide([UsersLayer, AuthLayer, TimingLayer, AuthClient])
23 )
24 }
25
26 export const e2eSuite = <E>(
27 name: string,
28 layer: Layer.Layer<UsersClient | RpcServer.Protocol, E>,
29 concurrent = true
30 ) => {
31 describe(name, { concurrent, timeout: 30_000 }, () => {
32 it.effect("should get user", () =>
33 Effect.gen(function*() {
34 const client = yield* UsersClient
35 const user = yield* client.GetUser({ id: "1" })
36 assert.instanceOf(user, User)
37 assert.deepStrictEqual(user, new User({ id: "1", name: "Logged in user" }))
38 }).pipe(Effect.provide(layer)))
39
40 it.effect("should get deferred user", () =>
41 Effect.gen(function*() {
42 const client = yield* UsersClient
43 const user = yield* client.GetUserDeferred({ id: "1" })
44 assert.instanceOf(user, User)
45 assert.deepStrictEqual(user, new User({ id: "1", name: "John" }))
46 }).pipe(Effect.provide(layer)))
47
48 it.effect("completes a discarded call on its acknowledgement, and fails it when its middleware rejects it", () =>
49 Effect.gen(function*() {
50 const client = yield* UsersClient
51 assert.isUndefined(yield* client.GetUser({ id: "1" }, { discard: true }))
52 const error = yield* client.GetUser({ id: "1" }, { discard: true }).pipe(
53 RpcClient.withHeaders({ unauthorized: "true" }),
54 Effect.flip
55 )
56 assert.strictEqual(error._tag, "Unauthorized")
57 }).pipe(Effect.provide(layer)))
58
59 it.effect("nested method", () =>
60 Effect.gen(function*() {
61 const client = yield* UsersClient
62 yield* client["nested.test"]()
63 }).pipe(Effect.provide(layer)))
64
65 it.effect("should not flatten Option", () =>
66 Effect.gen(function*() {
67 const client = yield* UsersClient
68 const user = yield* client.GetUserOption({ id: "1" })
69 assert.deepStrictEqual(user, Option.some(new User({ id: "1", name: "John" })))
70 }).pipe(Effect.provide(layer)))
71
72 it.effect("headers", () =>
73 Effect.gen(function*() {
74 const client = yield* UsersClient
75 const user = yield* client.GetUser({ id: "1" })
76 assert.instanceOf(user, User)
77 assert.deepStrictEqual(user, new User({ id: "123", name: "Logged in user" }))
78 }).pipe(
79 RpcClient.withHeaders({ userId: "123" }),
80 Effect.provide(layer)
81 ))
82
83 it.live("Stream", () =>
84 Effect.gen(function*() {
85 const client = yield* UsersClient
86 const users: Array<User> = []
87 yield* client.StreamUsers({ id: "1" }).pipe(
88 Stream.take(5),
89 Stream.runForEach((user) =>
90 Effect.sync(() => {
91 users.push(user)
92 })
93 ),
94 Effect.forkChild
95 )
96
97 yield* Effect.sleep(2000)
98 assert.lengthOf(users, 5)
99
100 // test interrupts
101 const interrupts = yield* client.GetInterrupts()
102 assert.equal(interrupts, 1)
103
104 const { supportsAck } = yield* RpcServer.Protocol
105
106 // test backpressure
107 if (supportsAck) {
108 const emits = yield* client.GetEmits()
109 assert.equal(emits, 5)
110 }
111 }).pipe(Effect.provide(layer)), { timeout: 20000 })
112
113 it.effect("defect", () =>
114 Effect.gen(function*() {
115 const client = yield* UsersClient
116 const cause = yield* client.ProduceDefect().pipe(
117 Effect.sandbox,
118 Effect.flip
119 )
120 assert.deepStrictEqual(cause, Cause.die("boom"))
121 }).pipe(
122 RpcClient.withHeaders({ userId: "123" }),
123 Effect.provide(layer)
124 ))
125
126 it.live("never", () =>
127 Effect.gen(function*() {
128 const client = yield* UsersClient
129 const fiber = yield* client.Never().pipe(
130 Effect.forkChild
131 )
132 yield* Effect.sleep(500)
133 assert.isUndefined(fiber.pollUnsafe())
134
135 yield* Fiber.interrupt(fiber)
136 yield* Effect.sleep(100)
137
138 const { supportsAck } = yield* RpcServer.Protocol
139 if (supportsAck) {
140 const interrupts = yield* client.GetInterrupts()
141 assert.equal(interrupts, 1)
142 }
143 }).pipe(
144 RpcClient.withHeaders({ userId: "123" }),
145 Effect.provide(layer)
146 ))
147
148 it.effect("timing middleware", () =>
149 Effect.gen(function*() {
150 const client = yield* UsersClient
151 const result = yield* client.TimedMethod({ shouldFail: false })
152 assert.equal(result, 1)
153 yield* client.TimedMethod({ shouldFail: true }).pipe(Effect.exit)
154 const { count, defect, success } = yield* client.GetTimingMiddlewareMetrics()
155 assert.notEqual(count, 0)
156 assert.notEqual(defect, 0)
157 assert.notEqual(success, 0)
158 }).pipe(Effect.provide(layer)))
159 })
160 }