Skip to content
Source
Sign inSign up
samwillis/neonbricks
Open History to see the last commit

ai-errors.test.ts

110 lines3.2 KB

1import { describe, expect, it, vi } from "vitest";
2vi.mock("../src/server/db", () => ({ pool: {}, transaction: vi.fn() }));
3import { APICallError } from "ai";
4import { aiBuildErrorMessage } from "../src/server/ai-errors";
5import { DomainError } from "../src/server/auth";
6​
7const gatewayError = (
8 statusCode: number,
9 body: unknown,
10 message = "Gateway request failed",
11) =>
12 new APICallError({
13 url: "https://gateway.example/openai/v1/responses",
14 requestBodyValues: {},
15 statusCode,
16 responseBody: JSON.stringify(body),
17 message,
18 });
19​
20describe("AI Gateway failure messages", () => {
21 it.each([
22 [
23 429,
24 {
25 error_code: "REQUEST_LIMIT_EXCEEDED",
26 message:
27 "ai gateway account daily spend limit exceeded. Retry after the daily spend limit resets",
28 },
29 ],
30 [
31 402,
32 {
33 error: {
34 code: "insufficient_credits",
35 message: "Insufficient prepaid credits",
36 },
37 },
38 ],
39 [
40 403,
41 { error: { message: "ai gateway prepaid credit balance is too low" } },
42 ],
43 [429, { error: { code: "insufficient_quota" } }],
44 ])(
45 "shows the budget message for explicit financial exhaustion (%s)",
46 (status, body) => {
47 expect(aiBuildErrorMessage(gatewayError(status, body))).toContain(
48 "budget has been used",
49 );
50 },
51 );
52​
53 it.each([
54 {
55 error_code: "REQUEST_LIMIT_EXCEEDED",
56 message:
57 "ai gateway account token rate limit exceeded (200000 tokens per minute across all models in this region)",
58 },
59 {
60 error_code: "REQUEST_LIMIT_EXCEEDED",
61 message: "ai gateway daily token limit exceeded",
62 },
63 { error: { code: "rate_limit_exceeded", message: "Too many requests" } },
64 {},
65 ])(
66 "does not mistake a temporary or unspecified rate limit for spending exhaustion",
67 (body) => {
68 expect(aiBuildErrorMessage(gatewayError(429, body))).toBe(
69 "Neon's AI Gateway is busy. Please try again shortly.",
70 );
71 },
72 );
73​
74 it("handles structured SDK data and malformed response bodies", () => {
75 const error = new APICallError({
76 url: "https://gateway.example/v1/chat/completions",
77 requestBodyValues: {},
78 statusCode: 402,
79 data: { error: { code: "insufficient_credits" } },
80 responseBody: "not JSON",
81 message: "Gateway request failed",
82 });
83 expect(aiBuildErrorMessage(error)).toContain("budget has been used");
84 expect(aiBuildErrorMessage(gatewayError(402, null))).not.toContain(
85 "budget",
86 );
87 });
88​
89 it("preserves validation and interruption messages and hides unknown errors", () => {
90 expect(aiBuildErrorMessage(new DomainError("Pieces overlap"))).toBe(
91 "Pieces overlap",
92 );
93 expect(
94 aiBuildErrorMessage(
95 gatewayError(429, { error: { code: "insufficient_credits" } }),
96 true,
97 ),
98 ).toContain("interrupted");
99 expect(
100 aiBuildErrorMessage(
101 gatewayError(403, {
102 error: { message: "credential not authorized for this branch" },
103 }),
104 ),
105 ).not.toContain("budget");
106 expect(aiBuildErrorMessage(new Error("secret budget details"))).toBe(
107 "AI couldn't finish this build. Please try again.",
108 );
109 });
110});