Files
better-auth/docs/lib/ai-chat/error.test.ts
T
2026-08-10 15:11:39 +00:00

59 lines
1.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
CHAT_ERROR_MESSAGE,
CHAT_RATE_LIMIT_MESSAGE,
getChatErrorMessage,
} from "./error";
describe("getChatErrorMessage", () => {
it("returns a recoverable message without exposing internal errors", () => {
expect(
getChatErrorMessage(new Error("Gateway request failed: secret")),
).toBe(CHAT_ERROR_MESSAGE);
});
it("explains when the request was rate limited", () => {
const errors = [
"Rate limit exceeded",
"rate_limit_error",
"Too Many Requests",
"RESOURCE_EXCEEDED",
"RESOURCE_EXHAUSTED",
"Quota exceeded",
"quota_exhausted",
"Request failed with status 429",
"HTTP 429",
];
for (const error of errors) {
expect(getChatErrorMessage(new Error(error))).toBe(
CHAT_RATE_LIMIT_MESSAGE,
);
}
});
it("does not mistake unrelated quota or numeric details for rate limits", () => {
const errors = [
"Moderation blocked due to output quota policy",
"Unexpected value 429 in response",
];
for (const error of errors) {
expect(getChatErrorMessage(new Error(error))).toBe(CHAT_ERROR_MESSAGE);
}
});
it("preserves sanitized errors received from the stream", () => {
expect(getChatErrorMessage(new Error(CHAT_ERROR_MESSAGE))).toBe(
CHAT_ERROR_MESSAGE,
);
expect(getChatErrorMessage(new Error(CHAT_RATE_LIMIT_MESSAGE))).toBe(
CHAT_RATE_LIMIT_MESSAGE,
);
});
it("handles non-Error values", () => {
expect(getChatErrorMessage(undefined)).toBe(CHAT_ERROR_MESSAGE);
});
});