Open History to see the last commit
1import * as Exit from "effect/Exit";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Schema from "effect/Schema";
5import * as Scope from "effect/Scope";
6
7import { assert, it } from "@effect/vitest";
8
9import * as CodexClient from "./client.ts";
10import * as CodexError from "./errors.ts";
11import * as CodexReplay from "./replay.ts";
12
13const isCodexAppServerTransportError = Schema.is(CodexError.CodexAppServerTransportError);
14const decodeReplayError = Schema.decodeUnknownEffect(CodexReplay.CodexAppServerReplayError);
15const encodeReplayError = Schema.encodeUnknownEffect(CodexReplay.CodexAppServerReplayError);
16
17const initializeParams = {
18 clientInfo: {
19 name: "effect-codex-app-server-test",
20 title: "Effect Codex App Server Test",
21 version: "0.0.0",
22 },
23 capabilities: {
24 experimentalApi: true,
25 optOutNotificationMethods: null,
26 },
27} as const;
28
29const initializeResponse = {
30 userAgent: "replay-codex-app-server",
31 codexHome: "/tmp/codex-home",
32 platformFamily: "unix",
33 platformOs: "macos",
34} as const;
35
36function buildContext(transcript: CodexReplay.CodexAppServerReplayTranscript) {
37 return Effect.gen(function* () {
38 const scope = yield* Scope.make();
39 const context = yield* Layer.buildWithScope(CodexReplay.layerReplay(transcript), scope);
40 return { context, scope };
41 });
42}
43
44it.effect("replays Codex app-server frames through the real client protocol", () =>
45 Effect.gen(function* () {
46 const { context, scope } = yield* buildContext({
47 provider: "codex",
48 protocol: "codex.app-server",
49 version: "test",
50 scenario: "initialize",
51 entries: [
52 {
53 type: "expect_outbound",
54 label: "initialize",
55 frame: {
56 id: 1,
57 method: "initialize",
58 params: {
59 ...initializeParams,
60 clientInfo: {
61 ...initializeParams.clientInfo,
62 version: "older-fixture-version",
63 },
64 },
65 },
66 },
67 {
68 type: "emit_inbound",
69 label: "initialize",
70 frame: {
71 id: 1,
72 result: initializeResponse,
73 },
74 },
75 {
76 type: "expect_outbound",
77 label: "initialized",
78 frame: {
79 method: "initialized",
80 },
81 },
82 {
83 type: "runtime_exit",
84 status: "success",
85 },
86 ],
87 });
88
89 yield* Effect.gen(function* () {
90 const client = yield* CodexClient.CodexAppServerClient;
91 assert.deepEqual(yield* client.request("initialize", initializeParams), initializeResponse);
92 yield* client.notify("initialized", undefined);
93 }).pipe(Effect.provide(context), Effect.ensuring(Scope.close(scope, Exit.void)));
94 }),
95);
96
97it.effect("fails pending client requests with a schema-serializable replay mismatch", () =>
98 Effect.gen(function* () {
99 const { context, scope } = yield* buildContext({
100 provider: "codex",
101 protocol: "codex.app-server",
102 version: "test",
103 scenario: "mismatch",
104 entries: [
105 {
106 type: "expect_outbound",
107 label: "initialize",
108 frame: {
109 id: 1,
110 method: "initialize",
111 params: initializeParams,
112 },
113 },
114 {
115 type: "runtime_exit",
116 status: "success",
117 },
118 ],
119 });
120
121 const error = yield* Effect.gen(function* () {
122 const client = yield* CodexClient.CodexAppServerClient;
123 return yield* client.request("account/read", {});
124 }).pipe(Effect.provide(context), Effect.flip, Effect.ensuring(Scope.close(scope, Exit.void)));
125
126 assert.equal(error._tag, "CodexAppServerTransportError");
127 if (!isCodexAppServerTransportError(error)) {
128 throw new Error("Expected transport error.");
129 }
130
131 const replayError = yield* decodeReplayError(error.cause);
132 const encoded = yield* encodeReplayError(replayError);
133
134 assert.equal(encoded._tag, "CodexAppServerReplayFrameMismatchError");
135 if (encoded._tag !== "CodexAppServerReplayFrameMismatchError") {
136 throw new Error("Expected frame mismatch error.");
137 }
138 assert.equal(encoded.scenario, "mismatch");
139 assert.equal(encoded.cursor, 0);
140 assert.deepEqual(encoded.actual, {
141 id: 1,
142 method: "account/read",
143 params: {},
144 });
145 }),
146);