The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | |
| 5 | import * as CodexError from "../errors.ts"; |
| 6 | import * as Shared from "./shared.ts"; |
| 7 | |
| 8 | const decodeNestedNumberPayload = Schema.decodeUnknownEffect( |
| 9 | Schema.Struct({ profile: Schema.Struct({ token: Schema.Number }) }), |
| 10 | ); |
| 11 | const encodeUnknownJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown)); |
| 12 | |
| 13 | it.effect("preserves schema decode diagnostics without deriving the message from the cause", () => |
| 14 | Effect.gen(function* () { |
| 15 | const error = yield* Shared.decodeOptionalPayload("thread/start", Schema.String, 42).pipe( |
| 16 | Effect.flip, |
| 17 | ); |
| 18 | |
| 19 | assert.instanceOf(error, CodexError.CodexAppServerRequestError); |
| 20 | assert.equal(error.code, -32602); |
| 21 | assert.equal(error.method, "thread/start"); |
| 22 | assert.equal(error.operation, "decode-payload"); |
| 23 | assert.equal( |
| 24 | error.message, |
| 25 | "Invalid payload for method 'thread/start' during 'decode-payload'", |
| 26 | ); |
| 27 | assert.isTrue(Schema.isSchemaError(error.cause)); |
| 28 | |
| 29 | const protocolError = error.toProtocolError(); |
| 30 | assert.equal(protocolError.code, -32602); |
| 31 | assert.equal(protocolError.message, error.message); |
| 32 | assert.property(protocolError, "data"); |
| 33 | assert.notProperty(protocolError, "method"); |
| 34 | assert.notProperty(protocolError, "operation"); |
| 35 | assert.notProperty(protocolError, "cause"); |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | it.effect("does not invent a cause when a method has no payload schema", () => |
| 40 | Effect.gen(function* () { |
| 41 | const secret = "unexpected-payload-secret"; |
| 42 | const error = yield* Shared.decodeOptionalPayload<never, never>("initialized", undefined, { |
| 43 | token: secret, |
| 44 | }).pipe(Effect.flip); |
| 45 | |
| 46 | assert.equal(error.method, "initialized"); |
| 47 | assert.equal(error.operation, "decode-payload"); |
| 48 | assert.equal(error.payloadKind, "object"); |
| 49 | assert.deepEqual(error.data, { payloadKind: "object" }); |
| 50 | assert.isUndefined(error.cause); |
| 51 | assert.notInclude(error.message, secret); |
| 52 | assert.notInclude(encodeUnknownJson(error.toProtocolError()), secret); |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | it.effect("keeps invalid payload values only in the exact schema cause", () => |
| 57 | Effect.gen(function* () { |
| 58 | const secret = "codex-schema-payload-secret"; |
| 59 | const cause = yield* decodeNestedNumberPayload({ profile: { token: secret } }).pipe( |
| 60 | Effect.flip, |
| 61 | ); |
| 62 | const error = CodexError.CodexAppServerRequestError.invalidPayload( |
| 63 | "thread/start", |
| 64 | "decode-payload", |
| 65 | cause, |
| 66 | ); |
| 67 | const { cause: directCause, ...directDiagnostics } = error; |
| 68 | |
| 69 | assert.strictEqual(directCause, cause); |
| 70 | assert.equal(error.method, "thread/start"); |
| 71 | assert.equal(error.operation, "decode-payload"); |
| 72 | assert.equal(error.maximumPathDepth, 2); |
| 73 | assert.isAbove(error.issueCount ?? 0, 0); |
| 74 | assert.include(error.issueKinds ?? [], "Pointer"); |
| 75 | assert.notInclude(error.message, secret); |
| 76 | assert.notInclude(encodeUnknownJson(directDiagnostics), secret); |
| 77 | assert.notInclude(encodeUnknownJson(error.toProtocolError()), secret); |
| 78 | }), |
| 79 | ); |
| 80 | |
| 81 | it.effect("retains the request-handler error as the internal error cause", () => |
| 82 | Effect.gen(function* () { |
| 83 | const rootCause = new Error("socket closed"); |
| 84 | const source = new CodexError.CodexAppServerTransportError({ |
| 85 | operation: "read-input-stream", |
| 86 | cause: rootCause, |
| 87 | }); |
| 88 | const error = yield* Shared.runHandler( |
| 89 | (_payload: void) => Effect.fail(source), |
| 90 | undefined, |
| 91 | "thread/start", |
| 92 | ).pipe(Effect.flip); |
| 93 | |
| 94 | assert.equal(error.code, -32603); |
| 95 | assert.equal(error.method, "thread/start"); |
| 96 | assert.equal(error.operation, "handle-request"); |
| 97 | assert.equal( |
| 98 | error.message, |
| 99 | "Codex App Server request handler failed for method 'thread/start'", |
| 100 | ); |
| 101 | assert.strictEqual(error.cause, source); |
| 102 | assert.strictEqual(source.cause, rootCause); |
| 103 | assert.notInclude(error.message, source.message); |
| 104 | }), |
| 105 | ); |