Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

client.test.ts

155 lines5.3 KB

1import * as Exit from "effect/Exit";
2import * as Layer from "effect/Layer";
3import * as Path from "effect/Path";
4import * as Effect from "effect/Effect";
5import * as Ref from "effect/Ref";
6import * as Scope from "effect/Scope";
7import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
8​
9import * as NodeServices from "@effect/platform-node/NodeServices";
10import { assert, it } from "@effect/vitest";
11​
12import * as CodexClient from "./client.ts";
13​
14const mockPeerPath = Effect.map(Effect.service(Path.Path), (path) =>
15 path.join(import.meta.dirname, "../test/fixtures/codex-app-server-mock-peer.ts"),
16);
17const mockPeerArgs = (path: string) => [path];
18​
19it.layer(NodeServices.layer)("effect-codex-app-server client", (it) => {
20 const makeHandle = () =>
21 Effect.gen(function* () {
22 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
23 const path = yield* Path.Path;
24 const peerCwd = path.join(import.meta.dirname, "..");
25 const command = ChildProcess.make(process.execPath, mockPeerArgs(yield* mockPeerPath), {
26 cwd: peerCwd,
27 });
28 return yield* spawner.spawn(command);
29 });
30​
31 it.effect("initializes, handles typed server requests, and reads account and skills data", () =>
32 Effect.gen(function* () {
33 const userInputRequests = yield* Ref.make<Array<unknown>>([]);
34 const messageDeltas = yield* Ref.make<Array<unknown>>([]);
35 const handle = yield* makeHandle();
36 const scope = yield* Scope.make();
37 const clientLayer = CodexClient.layerChildProcess(handle);
38 const context = yield* Layer.buildWithScope(clientLayer, scope);
39​
40 const result = yield* Effect.gen(function* () {
41 const client = yield* CodexClient.CodexAppServerClient;
42​
43 yield* client.handleServerRequest("item/tool/requestUserInput", (payload) =>
44 Ref.update(userInputRequests, (current) => [...current, payload]).pipe(
45 Effect.as({
46 answers: {
47 approved: {
48 answers: ["yes"],
49 },
50 },
51 }),
52 ),
53 );
54​
55 yield* client.handleServerNotification("item/agentMessage/delta", (payload) =>
56 Ref.update(messageDeltas, (current) => [...current, payload]),
57 );
58​
59 const initialized = yield* client.request("initialize", {
60 clientInfo: {
61 name: "effect-codex-app-server-test",
62 title: "Effect Codex App Server Test",
63 version: "0.0.0",
64 },
65 capabilities: {
66 experimentalApi: true,
67 optOutNotificationMethods: null,
68 },
69 });
70 assert.equal(initialized.userAgent, "mock-codex-app-server");
71​
72 yield* client.notify("initialized", undefined);
73​
74 const account = yield* client.request("account/read", {});
75 assert.equal(account.requiresOpenaiAuth, false);
76 assert.deepEqual(account.account, {
77 type: "chatgpt",
78 email: "mock@example.com",
79 planType: "plus",
80 });
81​
82 const path = yield* Path.Path;
83 const peerCwd = path.join(import.meta.dirname, "..");
84 const skills = yield* client.request("skills/list", { cwds: [peerCwd] });
85 assert.equal(skills.data.length, 1);
86 assert.equal(skills.data[0]?.cwd, peerCwd);
87​
88 return {
89 account,
90 skills,
91 };
92 }).pipe(Effect.provide(context), Effect.ensuring(Scope.close(scope, Exit.void)));
93​
94 assert.equal(result.skills.data[0]?.skills.length, 0);
95 assert.deepEqual(yield* Ref.get(userInputRequests), [
96 {
97 itemId: "item-approval-1",
98 threadId: "thread-1",
99 turnId: "turn-1",
100 questions: [
101 {
102 id: "approved",
103 header: "Approve",
104 question: "Continue with the mock skills request?",
105 options: [
106 {
107 label: "yes",
108 description: "Approve the request",
109 },
110 ],
111 },
112 ],
113 },
114 ]);
115 assert.deepEqual(yield* Ref.get(messageDeltas), [
116 {
117 delta: "Mock server is ready.",
118 itemId: "item-1",
119 threadId: "thread-1",
120 turnId: "turn-1",
121 },
122 ]);
123 }),
124 );
125​
126 it.effect("initializes a command-backed app-server client", () =>
127 Effect.gen(function* () {
128 const path = yield* Path.Path;
129 const scope = yield* Scope.make();
130 const clientLayer = CodexClient.layerCommand({
131 command: "node",
132 args: mockPeerArgs(yield* mockPeerPath),
133 cwd: path.join(import.meta.dirname, ".."),
134 });
135 const context = yield* Layer.buildWithScope(clientLayer, scope);
136​
137 const initialized = yield* Effect.gen(function* () {
138 const client = yield* CodexClient.CodexAppServerClient;
139 return yield* client.request("initialize", {
140 clientInfo: {
141 name: "effect-codex-app-server-test",
142 title: "Effect Codex App Server Test",
143 version: "0.0.0",
144 },
145 capabilities: {
146 experimentalApi: true,
147 optOutNotificationMethods: null,
148 },
149 });
150 }).pipe(Effect.provide(context), Effect.ensuring(Scope.close(scope, Exit.void)));
151​
152 assert.equal(initialized.userAgent, "mock-codex-app-server");
153 }),
154 );
155});