mirror of
https://github.com/anomalyco/opencode.git
synced 2026-07-23 10:45:33 -04:00
61 lines
2.5 KiB
TypeScript
61 lines
2.5 KiB
TypeScript
import { describe, expect, test } from "bun:test"
|
|
import { RequestError } from "@agentclientprotocol/sdk"
|
|
import { ACPError } from "../../src/acp/error"
|
|
|
|
describe("acp errors", () => {
|
|
test("maps validation failures to invalid params", () => {
|
|
const cases: ACPError.Error[] = [
|
|
new ACPError.SessionNotFoundError({ sessionId: "ses_missing" }),
|
|
new ACPError.InvalidConfigOptionError({ configId: "temperature" }),
|
|
new ACPError.InvalidModelError({ providerId: "anthropic", modelId: "claude-missing" }),
|
|
new ACPError.InvalidEffortError({ effort: "extreme" }),
|
|
new ACPError.InvalidModeError({ mode: "turbo" }),
|
|
]
|
|
|
|
expect(cases.map((error) => ACPError.toRequestError(error).code)).toEqual([-32602, -32602, -32602, -32602, -32602])
|
|
})
|
|
|
|
test("includes safe validation details", () => {
|
|
expect(ACPError.toRequestError(new ACPError.SessionNotFoundError({ sessionId: "ses_123" }))).toMatchObject({
|
|
code: -32602,
|
|
data: { sessionId: "ses_123" },
|
|
})
|
|
expect(ACPError.toRequestError(new ACPError.InvalidModelError({ modelId: "gpt-missing" }))).toMatchObject({
|
|
code: -32602,
|
|
data: { modelId: "gpt-missing" },
|
|
})
|
|
})
|
|
|
|
test("maps auth required to the SDK auth error", () => {
|
|
const requestError = ACPError.toRequestError(new ACPError.AuthRequiredError())
|
|
|
|
expect(requestError).toBeInstanceOf(RequestError)
|
|
expect(requestError.code).toBe(-32000)
|
|
expect(requestError.message).toBe("Authentication required: provider authentication required")
|
|
expect(requestError.data).toEqual({})
|
|
})
|
|
|
|
test("maps service failures to safe internal errors", () => {
|
|
const requestError = ACPError.toRequestError(
|
|
new ACPError.ServiceFailureError({ service: "provider", safeMessage: "Provider request failed" }),
|
|
)
|
|
|
|
expect(requestError.code).toBe(-32603)
|
|
expect(requestError.message).toBe("Internal error: Provider request failed")
|
|
expect(requestError.data).toEqual({ service: "provider" })
|
|
})
|
|
|
|
test("wraps unknown defects without leaking raw details", () => {
|
|
const requestError = ACPError.toRequestError(
|
|
ACPError.fromUnknown(new Error("stack has sk-ant-secret and oauth refresh token")),
|
|
)
|
|
const serialized = JSON.stringify(requestError.toErrorResponse())
|
|
|
|
expect(requestError.code).toBe(-32603)
|
|
expect(requestError.message).toBe("Internal error: Internal service failure")
|
|
expect(serialized).not.toContain("sk-ant-secret")
|
|
expect(serialized).not.toContain("oauth refresh token")
|
|
expect(serialized).not.toContain("stack")
|
|
})
|
|
})
|