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

client.test.ts

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