Files
elizaos--eliza/plugins/plugin-anthropic/__tests__/error-policy.shape.test.ts
T
wehub-resource-sync 426e9eeabd
Voice Workbench / headless workbench (mocked backends) (push) Has been cancelled
Voice Workbench / real acoustic lane (nightly, provisioned only) (push) Has been cancelled
ci / test (push) Has been cancelled
ci / lint-and-format (push) Has been cancelled
ci / build (push) Has been cancelled
ci / dev-startup (push) Has been cancelled
gitleaks / gitleaks (push) Has been cancelled
Markdown Links / Relative Markdown Links (push) Has been cancelled
Quality (Extended) / Homepage Build (PR smoke) (push) Has been cancelled
Quality (Extended) / Comment-only diff guard (push) Has been cancelled
Quality (Extended) / Format + Type Safety Ratchet (push) Has been cancelled
Quality (Extended) / Develop Gate (secret scan + UI determinism) (push) Has been cancelled
Quality (Extended) / Develop Gate (lint) (push) Has been cancelled
Chat shell gestures / Chat shell gesture + parity e2e (push) Has been cancelled
Cloud Gateway Discord / Test (push) Has been cancelled
Benchmark Bridge Tests / benchmark (bunx @biomejs/biome check packages/lifeops-bench/src, benchmark-lint) (push) Has been cancelled
Benchmark Bridge Tests / benchmark (bunx vitest run --config packages/lifeops-bench/vitest.config.ts --root packages/lifeops-bench --passWithNoTests, benchmark-tests) (push) Has been cancelled
Build Agent Image / build-and-push (push) Has been cancelled
Dev Smoke / bun run dev onboarding chat (push) Has been cancelled
Dev Smoke / Vite HMR dependency-level smoke (push) Has been cancelled
Electrobun Submodule Guard / electrobun gitlink is fetchable (push) Has been cancelled
Publish @elizaos/example-code / check_npm (push) Has been cancelled
Publish @elizaos/example-code / publish_npm (push) Has been cancelled
Publish @elizaos/plugin-elizacloud / verify_version (push) Has been cancelled
Publish @elizaos/plugin-elizacloud / publish_npm (push) Has been cancelled
Sandbox Live Smoke / Sandbox live smoke (push) Has been cancelled
Snap Build & Test / Build Snap (amd64) (push) Has been cancelled
Snap Build & Test / Build Snap (arm64) (push) Has been cancelled
Test Packaging / elizaos CLI global-install smoke (node + bun) (push) Has been cancelled
Cloud Gateway Webhook / Test (push) Has been cancelled
Cloud Tests / lint-and-types (push) Has been cancelled
Cloud Tests / unit-tests (push) Has been cancelled
Cloud Tests / integration-tests (push) Has been cancelled
Cloud Tests / e2e-tests (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Deploy Apps Worker (Product 2) / Determine environment (push) Has been cancelled
Deploy Apps Worker (Product 2) / Deploy apps worker to apps-control host (${{ needs.determine-env.outputs.environment }}) (push) Has been cancelled
Deploy Eliza Provisioning Worker / Determine environment (push) Has been cancelled
Deploy Eliza Provisioning Worker / Deploy worker to Hetzner host (${{ needs.determine-env.outputs.environment }} @ ${{ needs.determine-env.outputs.deployment_sha }}) (push) Has been cancelled
Dev Smoke / Classify changed paths (push) Has been cancelled
supply-chain / sbom (push) Has been cancelled
supply-chain / vulnerability-scan (push) Has been cancelled
Build, Push & Deploy to Phala Cloud / build-and-push (push) Has been cancelled
Test Packaging / Validate Packaging Configs (push) Has been cancelled
Test Packaging / Build & Test PyPI Package (push) Has been cancelled
Test Packaging / PyPI on Python ${{ matrix.python }} (push) Has been cancelled
Test Packaging / Pack & Test JS Tarballs (push) Has been cancelled
UI Fixture E2E / ui-fixture-e2e (push) Has been cancelled
UI Fixture E2E / fixture-e2e (push) Has been cancelled
UI Story Gate / story-gate (push) Has been cancelled
vault-ci / test (macos-latest) (push) Has been cancelled
vault-ci / test (ubuntu-latest) (push) Has been cancelled
vault-ci / test (windows-latest) (push) Has been cancelled
vault-ci / app-core wiring tests (push) Has been cancelled
verify-patches / verify patches/CHECKSUMS.sha256 (push) Has been cancelled
Voice Benchmark Smoke / voice-emotion fixture smoke (push) Has been cancelled
Voice Benchmark Smoke / voiceagentbench fixture smoke (push) Has been cancelled
Voice Benchmark Smoke / voicebench-quality unit smoke (push) Has been cancelled
Voice Benchmark Smoke / voicebench TypeScript unit (no audio) (push) Has been cancelled
Voice Benchmark Smoke / voice bench smoke summary (push) Has been cancelled
Windows CI / windows ([bun run --cwd packages/app-core test bun run --cwd packages/elizaos test bun run --cwd packages/cloud/shared test], app-and-cli) (push) Has been cancelled
Windows CI / windows ([bun run --cwd packages/scenario-runner test bun run --cwd packages/vault test bun run --cwd packages/security test bun run --cwd plugins/plugin-coding-tools test], framework-packages) (push) Has been cancelled
Windows CI / windows ([bun run --cwd plugins/plugin-elizacloud test bun run --cwd plugins/plugin-discord test bun run --cwd plugins/plugin-anthropic test bun run --cwd plugins/plugin-openai test bun run --cwd plugins/plugin-app-control test bun run --cwd plugins/pl… (push) Has been cancelled
Windows CI / windows ([node packages/scripts/run-turbo.mjs run build --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/agent --concurrency=4 node packages/scripts/run-bash-linux-only.mjs scripts/verify-riscv64-buildpaths.sh node packages/scripts/run… (push) Has been cancelled
Windows CI / windows ([node packages/scripts/run-turbo.mjs run typecheck --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/cloud-shared --concurrency=4 bun run --cwd packages/core test bun run --cwd packages/shared test], core-runtime, 75) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:43:05 +08:00

228 lines
7.9 KiB
TypeScript

/**
* Failure-path tests for the #12182 error-handling policy (#12795): provider
* credential rejection (401), invalid request (400), malformed provider JSON,
* rate-limit retry exhaustion (429), and the CLI-mode failure surfaces
* (unavailable Bun runtime, unparseable CLI JSON, non-zero-exit stream). Every
* case must surface a typed error — never a fabricated completion. The image
* handler runs the real `ai` + `@ai-sdk/anthropic` stack against a stubbed
* global fetch; no live API.
*/
import type { IAgentRuntime } from "@elizaos/core";
import { afterEach, describe, expect, it, vi } from "vitest";
import { handleImageDescription } from "../models/image";
import { generateViaCli, streamViaCli } from "../utils/claude-cli";
import { executeWithRetry, formatModelError } from "../utils/retry";
function createRuntime() {
return {
character: { name: "ErrorPolicyTester" },
emitEvent: vi.fn(async () => undefined),
getSetting: vi.fn((key: string) => {
const settings: Record<string, string> = {
ANTHROPIC_API_KEY: "test-key",
ANTHROPIC_SMALL_MODEL: "claude-test-small",
ANTHROPIC_AUTH_MODE: "apikey",
};
return settings[key] ?? null;
}),
} as unknown as IAgentRuntime;
}
function anthropicErrorResponse(status: number, type: string, message: string): Response {
return new Response(JSON.stringify({ type: "error", error: { type, message } }), {
status,
headers: { "content-type": "application/json" },
});
}
function streamOf(text: string): ReadableStream<Uint8Array> {
return new ReadableStream({
start(controller) {
if (text.length > 0) {
controller.enqueue(new TextEncoder().encode(text));
}
controller.close();
},
});
}
function fakeBun(stdout: string, stderr: string, exitCode: number) {
return {
spawn: vi.fn(() => ({
stdout: streamOf(stdout),
stderr: streamOf(stderr),
exited: Promise.resolve(exitCode),
})),
};
}
afterEach(() => {
vi.unstubAllGlobals();
vi.clearAllMocks();
});
describe("Anthropic provider failure surfaces (real SDK, stubbed transport)", () => {
it("surfaces a 401 credential rejection as a typed auth failure, not a result", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () => anthropicErrorResponse(401, "authentication_error", "invalid x-api-key"))
);
const runtime = createRuntime();
await expect(handleImageDescription(runtime, "https://example.com/image.png")).rejects.toThrow(
/IMAGE_DESCRIPTION request .* failed: Authentication failed/
);
// No fabricated { title, description } and no success usage event.
expect(
(runtime as unknown as { emitEvent: ReturnType<typeof vi.fn> }).emitEvent
).not.toHaveBeenCalled();
}, 60_000);
it("surfaces a 400 invalid-request rejection with the provider's message", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () =>
anthropicErrorResponse(400, "invalid_request_error", "max_tokens: must be positive")
)
);
await expect(
handleImageDescription(createRuntime(), "https://example.com/image.png")
).rejects.toThrow(/failed: max_tokens: must be positive/);
}, 60_000);
it("surfaces a malformed provider response body as a typed failure", async () => {
vi.stubGlobal(
"fetch",
vi.fn(
async () =>
new Response("<html>not json</html>", {
status: 200,
headers: { "content-type": "application/json" },
})
)
);
await expect(
handleImageDescription(createRuntime(), "https://example.com/image.png")
).rejects.toThrow(/IMAGE_DESCRIPTION request .* failed/);
}, 60_000);
});
describe("Anthropic retry + error translation (rate-limit / overload)", () => {
const fastRetry = { maxRetries: 2, initialDelayMs: 1, maxDelayMs: 2, backoffFactor: 2 };
it("retries a 429 then surfaces the original rate-limit error, never a value", async () => {
const rateLimit = Object.assign(new Error("rate limited"), { statusCode: 429 });
const fn = vi.fn(async () => {
throw rateLimit;
});
await expect(executeWithRetry("test op", fn, fastRetry)).rejects.toBe(rateLimit);
expect(fn).toHaveBeenCalledTimes(3);
expect(formatModelError("test op", rateLimit).message).toContain(
"Anthropic rate limited the request"
);
});
it("retries a 529 overload then surfaces a typed overload failure", async () => {
const overloaded = Object.assign(new Error("Overloaded"), { statusCode: 529 });
const fn = vi.fn(async () => {
throw overloaded;
});
await expect(executeWithRetry("test op", fn, fastRetry)).rejects.toBe(overloaded);
expect(fn).toHaveBeenCalledTimes(3);
expect(formatModelError("test op", overloaded).message).toContain("temporarily overloaded");
});
it("does not retry a non-retryable 403 and preserves the cause chain", async () => {
const forbidden = Object.assign(new Error("forbidden"), {
statusCode: 403,
data: { error: { message: "This organization has been disabled." } },
});
const fn = vi.fn(async () => {
throw forbidden;
});
await expect(executeWithRetry("test op", fn, fastRetry)).rejects.toBe(forbidden);
expect(fn).toHaveBeenCalledTimes(1);
const formatted = formatModelError("test op", forbidden);
expect(formatted.message).toContain("This organization has been disabled.");
expect(formatted.cause).toBe(forbidden);
});
});
describe("Anthropic CLI mode failure surfaces", () => {
it("throws a typed error when the Bun runtime is unavailable", async () => {
vi.stubGlobal("Bun", undefined);
await expect(
generateViaCli(createRuntime(), "hello", "claude-test", "TEXT_SMALL")
).rejects.toThrow("[Anthropic CLI] Bun runtime is required for CLI mode");
});
it("throws with cause when the CLI emits unparseable JSON", async () => {
vi.stubGlobal("Bun", fakeBun("definitely-not-json", "", 0));
const rejection = generateViaCli(createRuntime(), "hello", "claude-test", "TEXT_SMALL");
await expect(rejection).rejects.toThrow(/\[Anthropic CLI\] Failed to parse JSON/);
await rejection.catch((error: Error) => {
expect(error.cause).toBeInstanceOf(SyntaxError);
});
});
it("throws a typed error when the CLI exits non-zero", async () => {
vi.stubGlobal("Bun", fakeBun("", "boom: not logged in", 1));
await expect(
generateViaCli(createRuntime(), "hello", "claude-test", "TEXT_SMALL")
).rejects.toThrow(/claude -p failed \(exit 1\): boom: not logged in/);
});
it("surfaces a failed CLI stream as an error instead of a healthy-empty completion", async () => {
vi.stubGlobal("Bun", fakeBun("", "auth expired", 1));
const result = streamViaCli(createRuntime(), "hello", "claude-test", "TEXT_SMALL");
const consume = async () => {
const chunks: string[] = [];
for await (const chunk of result.textStream) {
chunks.push(chunk);
}
return chunks;
};
await expect(consume()).rejects.toThrow(
/\[Anthropic CLI\] claude -p stream failed \(exit 1\): auth expired/
);
// The finish reason must not read as a successful end_turn.
await expect(result.finishReason).resolves.toBe("error");
});
it("still resolves end_turn for a successful stream with a result event", async () => {
const lines = [
JSON.stringify({
type: "stream_event",
event: { delta: { type: "text_delta", text: "hi" } },
}),
JSON.stringify({
type: "result",
modelUsage: { "claude-test": { inputTokens: 2, outputTokens: 1 } },
stop_reason: "end_turn",
}),
"",
].join("\n");
vi.stubGlobal("Bun", fakeBun(lines, "", 0));
const result = streamViaCli(createRuntime(), "hello", "claude-test", "TEXT_SMALL");
const chunks: string[] = [];
for await (const chunk of result.textStream) {
chunks.push(chunk);
}
expect(chunks).toEqual(["hi"]);
await expect(result.finishReason).resolves.toBe("end_turn");
});
});