| 1 | import { assert, it } from "@effect/vitest"; |
| 2 | import { classifyCodexManagedError } from "./CodexManagedErrors.ts"; |
| 3 | it("maps streamed failures to safe actionable messages and reconnect decisions", () => { |
| 4 | assert.deepEqual( |
| 5 | classifyCodexManagedError({ |
| 6 | error: { |
| 7 | code: "subscription_sharing_v2_invalid_user", |
| 8 | message: "opaque token dummy-sensitive", |
| 9 | }, |
| 10 | }), |
| 11 | { |
| 12 | message: |
| 13 | "ChatGPT could not validate this connection. Check the selected account and sharing permissions.", |
| 14 | revoke: false, |
| 15 | code: "subscription_sharing_v2_invalid_user", |
| 16 | }, |
| 17 | ); |
| 18 | assert.include( |
| 19 | classifyCodexManagedError("subscription_sharing_usage_limit_exceeded")!.message, |
| 20 | "Usage settings", |
| 21 | ); |
| 22 | assert.equal( |
| 23 | classifyCodexManagedError("subscription_sharing_usage_limit_exceeded")!.code, |
| 24 | "subscription_sharing_usage_limit_exceeded", |
| 25 | ); |
| 26 | assert.isFalse(classifyCodexManagedError("subscription_sharing_usage_unavailable")!.revoke); |
| 27 | assert.include( |
| 28 | classifyCodexManagedError("subscription_sharing_unsupported_capability")!.message, |
| 29 | "feature", |
| 30 | ); |
| 31 | assert.isUndefined(classifyCodexManagedError(undefined)); |
| 32 | assert.isUndefined(classifyCodexManagedError({ error: "unknown" })); |
| 33 | }); |
| 34 | |
| 35 | it("distinguishes unsupported tools from input items without exposing the raw response", () => { |
| 36 | const code = "subscription_sharing_unsupported_capability"; |
| 37 | for (const [detail, expected] of [ |
| 38 | ["tool 'namespace' is not supported", "tool namespace"], |
| 39 | ["input item 'additional_tools' is not supported", "input item"], |
| 40 | ]) { |
| 41 | const response = { error: { code, message: `${detail}; dummy-sensitive` } }; |
| 42 | for (const value of [response, JSON.stringify(response)]) { |
| 43 | const failure = classifyCodexManagedError(value); |
| 44 | assert.isDefined(failure); |
| 45 | assert.include(failure!.message, expected!); |
| 46 | assert.notInclude(failure!.message, "dummy-sensitive"); |
| 47 | assert.isFalse(failure!.revoke); |
| 48 | } |
| 49 | } |
| 50 | }); |
| 51 | |
| 52 | it("preserves credentials for the current subscriber and permission error codes", () => { |
| 53 | for (const code of [ |
| 54 | "subscription_sharing_invalid_user", |
| 55 | "subscription_sharing_user_not_eligible", |
| 56 | "subscription_sharing_route_not_supported", |
| 57 | "subscription_sharing_user_unavailable", |
| 58 | "chatpass_v2_scope_not_authorized", |
| 59 | "chatpass_v2_invalid_authorization_context", |
| 60 | ]) { |
| 61 | assert.strictEqual(classifyCodexManagedError({ error: { code } })?.code, code); |
| 62 | assert.isFalse(classifyCodexManagedError({ error: { code } })!.revoke); |
| 63 | } |
| 64 | }); |