Skip to content

Commit 078f48b

Browse files
committed
✅ test(providers): close final 2% coverage gap (#404)
1 parent 48efe02 commit 078f48b

3 files changed

Lines changed: 133 additions & 0 deletions

File tree

‎src/api/providers/__tests__/base-openai-compatible-provider.spec.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,54 @@ describe("BaseOpenAiCompatibleProvider", () => {
682682
}
683683
}).rejects.toThrow()
684684
})
685+
686+
it("should throw on base_resp error chunk during streaming", async () => {
687+
mockCreate.mockImplementationOnce(() => {
688+
return {
689+
[Symbol.asyncIterator]: () => ({
690+
next: vi
691+
.fn()
692+
.mockResolvedValueOnce({
693+
done: false,
694+
value: {
695+
base_resp: { status_code: 500, status_msg: "Internal Server Error" },
696+
choices: [],
697+
},
698+
})
699+
.mockResolvedValueOnce({ done: true }),
700+
}),
701+
}
702+
})
703+
704+
const stream = handler.createMessage("system prompt", [])
705+
await expect(async () => {
706+
for await (const _ of stream) {
707+
/* drain */
708+
}
709+
}).rejects.toThrow("TestProvider API Error (500): Internal Server Error")
710+
})
711+
712+
it("should throw on completePrompt base_resp error response", async () => {
713+
mockCreate.mockResolvedValueOnce({
714+
base_resp: { status_code: 403, status_msg: "Forbidden" },
715+
choices: [{ message: { content: "should not return" } }],
716+
})
717+
718+
await expect(handler.completePrompt("test prompt")).rejects.toThrow(
719+
"TestProvider API Error (403): Forbidden",
720+
)
721+
})
722+
723+
it("should throw on completePrompt base_resp with zero status_code as success path", async () => {
724+
// base_resp with status_code 0 is treated as success
725+
mockCreate.mockResolvedValueOnce({
726+
base_resp: { status_code: 0, status_msg: "OK" },
727+
choices: [{ message: { content: "ok response" } }],
728+
})
729+
730+
const result = await handler.completePrompt("test prompt")
731+
expect(result).toBe("ok response")
732+
})
685733
})
686734

687735
describe("completePrompt with reasoning", () => {

‎src/api/providers/__tests__/gemini.spec.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,29 @@ describe("GeminiHandler", () => {
157157
const result = await handler.completePrompt("Test prompt")
158158
expect(result).toBe("")
159159
})
160+
161+
it("should handle grounding metadata in completePrompt response", async () => {
162+
// Mock response with grounding metadata
163+
;(handler["client"].models.generateContent as any).mockResolvedValue({
164+
text: "Main response",
165+
candidates: [
166+
{
167+
groundingMetadata: {
168+
groundingChunks: [{ web: { uri: "https://example.com" } }],
169+
},
170+
},
171+
],
172+
})
173+
174+
const result = await handler.completePrompt("Test prompt")
175+
expect(result).toContain("Main response")
176+
})
177+
178+
it("should handle non-Error thrown in completePrompt", async () => {
179+
;(handler["client"].models.generateContent as any).mockRejectedValue("string error")
180+
181+
await expect(handler.completePrompt("Test prompt")).rejects.toBe("string error")
182+
})
160183
})
161184

162185
describe("getModel", () => {
@@ -223,6 +246,17 @@ describe("GeminiHandler", () => {
223246
})
224247

225248
describe("calculateCost", () => {
249+
it("should set cacheReadsPrice to 0 when it is undefined", () => {
250+
const infoWithoutCachePrice: ModelInfo = {
251+
...mockInfo,
252+
cacheReadsPrice: undefined,
253+
}
254+
const cost = handler.calculateCost({ info: infoWithoutCachePrice, inputTokens: 1000, outputTokens: 1000 })
255+
// Should still compute cost without cache reads
256+
expect(cost).toBeDefined()
257+
expect(cost).toBeGreaterThan(0)
258+
})
259+
226260
// Mock ModelInfo based on gemini-1.5-flash-latest pricing (per 1M tokens)
227261
// Removed 'id' and 'name' as they are not part of ModelInfo type directly
228262
const mockInfo: ModelInfo = {
@@ -367,4 +401,14 @@ describe("GeminiHandler", () => {
367401
expect(mockCaptureException).toHaveBeenCalled()
368402
})
369403
})
404+
405+
describe("getThoughtSignature and getResponseId", () => {
406+
it("should return undefined for getThoughtSignature before any request", () => {
407+
expect(handler.getThoughtSignature()).toBeUndefined()
408+
})
409+
410+
it("should return undefined for getResponseId before any request", () => {
411+
expect(handler.getResponseId()).toBeUndefined()
412+
})
413+
})
370414
})

‎src/api/providers/__tests__/openai.spec.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,11 @@ describe("OpenAiHandler", () => {
627627
const result = await handler.completePrompt("Test prompt")
628628
expect(result).toBe("")
629629
})
630+
631+
it("should handle handleOpenAIError wrapping of non-Error in completePrompt", async () => {
632+
mockCreate.mockRejectedValueOnce("non-error rejection")
633+
await expect(handler.completePrompt("Test prompt")).rejects.toThrow()
634+
})
630635
})
631636

632637
describe("getModel", () => {
@@ -1200,6 +1205,42 @@ describe("OpenAiHandler", () => {
12001205
expect.objectContaining({ path: "/models/chat/completions", signal: expect.any(AbortSignal) }),
12011206
)
12021207
})
1208+
1209+
it("should handle O4 model (routed to handleO3FamilyMessage)", async () => {
1210+
const o4Handler = new OpenAiHandler({
1211+
...o3Options,
1212+
openAiModelId: "o4-mini",
1213+
openAiStreamingEnabled: false,
1214+
})
1215+
const systemPrompt = "You are a helpful assistant."
1216+
const messages: Anthropic.Messages.MessageParam[] = [
1217+
{
1218+
role: "user",
1219+
content: "Hello!",
1220+
},
1221+
]
1222+
1223+
const stream = o4Handler.createMessage(systemPrompt, messages)
1224+
const chunks: any[] = []
1225+
for await (const chunk of stream) {
1226+
chunks.push(chunk)
1227+
}
1228+
1229+
// O4 models use developer role (same as O3 family)
1230+
expect(mockCreate).toHaveBeenCalledWith(
1231+
expect.objectContaining({
1232+
model: "o4-mini",
1233+
messages: [
1234+
{
1235+
role: "developer",
1236+
content: "Formatting re-enabled\nYou are a helpful assistant.",
1237+
},
1238+
{ role: "user", content: "Hello!" },
1239+
],
1240+
}),
1241+
expect.objectContaining({ signal: expect.any(AbortSignal) }),
1242+
)
1243+
})
12031244
})
12041245

12051246
describe("error handling in createMessage", () => {

0 commit comments

Comments
 (0)