test: harden OpenAI response error handling

This commit is contained in:
2026-06-11 11:47:29 +01:00
parent 189b3372a4
commit 311f49435e
3 changed files with 55 additions and 8 deletions
+46
View File
@@ -30,6 +30,18 @@ function makeNetworkError() {
return err;
}
function makeServerError() {
const err = new Error("Service unavailable");
err.status = 503;
return err;
}
function makeTimeoutError() {
const err = new Error("timeout");
err.code = "ETIMEDOUT";
return err;
}
// --- Import ---
const { sendOpenAIResponse } = await import("../../src/openai/responses.js");
@@ -167,4 +179,38 @@ describe("sendOpenAIResponse", () => {
sendOpenAIResponse(mockClient, { input: ["x"], model: "m", maxOutputTokens: -5 })
).rejects.toHaveProperty("kind", "ValidationError");
});
it("throws ValidationError when maxOutputTokens is zero", async () => {
await expect(
sendOpenAIResponse(mockClient, { input: ["x"], model: "m", maxOutputTokens: 0 })
).rejects.toHaveProperty("kind", "ValidationError");
});
it("throws ValidationError when maxOutputTokens is a non-positive float", async () => {
await expect(
sendOpenAIResponse(mockClient, { input: ["x"], model: "m", maxOutputTokens: 0.5 })
).rejects.toHaveProperty("kind", "ValidationError");
});
it("throws ValidationError when maxOutputTokens is NaN", async () => {
await expect(
sendOpenAIResponse(mockClient, { input: ["x"], model: "m", maxOutputTokens: NaN })
).rejects.toHaveProperty("kind", "ValidationError");
});
// ===== Error mapping edge cases (2) =====
it("maps status 503 to OpenAIRequestError", async () => {
mockCreate.mockRejectedValueOnce(makeServerError());
await expect(
sendOpenAIResponse(mockClient, { input: ["x"], model: "m" })
).rejects.toHaveProperty("kind", "OpenAIRequestError");
});
it("maps timeout error (code ETIMEDOUT) to OpenAIRequestError", async () => {
mockCreate.mockRejectedValueOnce(makeTimeoutError());
await expect(
sendOpenAIResponse(mockClient, { input: ["x"], model: "m" })
).rejects.toHaveProperty("kind", "OpenAIRequestError");
});
});