Skip to content

Commit db1bc09

Browse files
test(api): assert reasoning detail edges the review asked for
Answers three open review rows on #1545 without touching production code: - malformed reasoning updates now run valid-then-invalid for text, summary and encrypted fragments at the same type and index, asserting both the stored detail and the emitted reasoning events (the old case only covered invalid-then-valid). - the same-index text/summary test asserts the complete emitted chunk array and that the summary reaches consolidation, plus a companion case where no text arrives, so the drop in consolidateReasoningDetails is pinned from both sides. - consolidateReasoningDetails gains a mixed-input test that asserts the full output array across interleaved types and repeated items, per the repository test-path instruction in .coderabbit.yaml. The Mistral tool call id assertion is unchanged: normalizeMistralToolCallId caps ids at MAX_MISTRAL_TOOL_CALL_ID_LENGTH = 9, so toolu_123456789 normalizes to toolu1234 and the test passes as written (proven by vitest at this head, 188 tests passing).
1 parent 1e9f518 commit db1bc09

2 files changed

Lines changed: 158 additions & 1 deletion

File tree

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

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,46 @@ describe("OpenRouterHandler", () => {
16961696
expect(text).not.toContain("[object Object]")
16971697
})
16981698

1699+
it.each([
1700+
{ type: "reasoning.text", field: "text", good: "ok", emitsReasoning: true },
1701+
{ type: "reasoning.summary", field: "summary", good: "s", emitsReasoning: true },
1702+
{ type: "reasoning.encrypted", field: "data", good: "enc", emitsReasoning: false },
1703+
])(
1704+
"keeps the valid fragment when a non-string $type fragment arrives after it at the same index",
1705+
async ({ type, field, good, emitsReasoning }) => {
1706+
const handler = new OpenRouterHandler(mockOptions)
1707+
const mockCreate = vitest.fn().mockResolvedValue(
1708+
asyncStreamFrom([
1709+
{
1710+
id: "1",
1711+
choices: [
1712+
{ delta: { reasoning_details: [{ index: 0, type, [field]: good }] } },
1713+
{ delta: { reasoning_details: [{ index: 0, type, [field]: { not: "a string" } }] } },
1714+
],
1715+
},
1716+
]),
1717+
)
1718+
// The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts.
1719+
const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } }
1720+
Object.defineProperty(client, "chat", {
1721+
value: { completions: { create: mockCreate } },
1722+
configurable: true,
1723+
writable: true,
1724+
})
1725+
1726+
const chunks = await collectStream(
1727+
handler.createMessage("system", [{ role: "user" as const, content: "hi" }]),
1728+
)
1729+
// The valid fragment is the only displayable one; the non-string fragment yields nothing.
1730+
expect(chunks).toEqual(emitsReasoning ? [{ type: "reasoning", text: good }] : [])
1731+
1732+
const details = handler.getReasoningDetails()
1733+
expect(details).toHaveLength(1)
1734+
expect(details?.[0]).toMatchObject({ type, index: 0, [field]: good })
1735+
expect(JSON.stringify(details)).not.toContain("[object Object]")
1736+
},
1737+
)
1738+
16991739
it("excludes reasoning for Gemini 2.5 Pro models by default", async () => {
17001740
const handler = new OpenRouterHandler(
17011741
makeApiHandlerOptions({
@@ -2397,11 +2437,57 @@ describe("OpenRouterHandler", () => {
23972437
const chunks = await collectStream(
23982438
handler.createMessage("system", [{ role: "user" as const, content: "hi" }]),
23992439
)
2400-
expect(chunks).toContainEqual({ type: "reasoning", text: "a" })
2440+
// Both fragments stream live in arrival order (the summary is yielded from detail.summary),
2441+
// so a dropped summary is visible here rather than hidden by toContainEqual on one chunk.
2442+
expect(chunks).toEqual([
2443+
{ type: "reasoning", text: "a" },
2444+
{ type: "reasoning", text: "s" },
2445+
])
24012446

24022447
const details = handler.getReasoningDetails()
2448+
// consolidateReasoningDetails emits a summary entry only when no text arrived at the same
2449+
// index (src/api/transform/openai-format.ts:116), so the consolidated store keeps the text
2450+
// and drops the summary; asserting the drop is what makes this fail when that predicate
2451+
// narrows. The companion test below covers the no-text branch. The entry's type label
2452+
// follows the last fragment seen for the index (openai-format.ts:93-99, 105), which is why
2453+
// it reports reasoning.summary while carrying text "a" and no summary field.
24032454
expect(details).toHaveLength(1)
24042455
expect(details?.[0]?.text).toBe("a")
2456+
expect(details?.[0]?.index).toBe(0)
2457+
expect(details?.[0]?.type).toBe("reasoning.summary")
2458+
expect(details?.[0]?.summary).toBeUndefined()
2459+
})
2460+
2461+
it("keeps a reasoning summary entry when no text arrives at the same index", async () => {
2462+
const handler = new OpenRouterHandler(mockOptions)
2463+
const mockCreate = vitest.fn().mockResolvedValue(
2464+
asyncStreamFrom([
2465+
{
2466+
id: "1",
2467+
choices: [
2468+
{
2469+
delta: { reasoning_details: [{ type: "reasoning.summary", index: 0, summary: "s" }] },
2470+
},
2471+
],
2472+
},
2473+
]),
2474+
)
2475+
// The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts.
2476+
const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } }
2477+
Object.defineProperty(client, "chat", {
2478+
value: { completions: { create: mockCreate } },
2479+
configurable: true,
2480+
writable: true,
2481+
})
2482+
2483+
const chunks = await collectStream(
2484+
handler.createMessage("system", [{ role: "user" as const, content: "hi" }]),
2485+
)
2486+
expect(chunks).toEqual([{ type: "reasoning", text: "s" }])
2487+
2488+
const details = handler.getReasoningDetails()
2489+
expect(details).toHaveLength(1)
2490+
expect(details?.[0]).toMatchObject({ type: "reasoning.summary", index: 0, summary: "s" })
24052491
})
24062492

24072493
it("does not yield displayable reasoning for encrypted-only details", async () => {

‎src/api/transform/__tests__/openai-format.spec.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,77 @@ describe("consolidateReasoningDetails", () => {
13941394
expect(result).toHaveLength(1)
13951395
expect(result[0].summary).toBe("Summary part 1Summary part 2")
13961396
})
1397+
1398+
it("should consolidate interleaved item types and assert the full output array", () => {
1399+
const details: ReasoningDetail[] = [
1400+
{ type: "reasoning.summary", summary: "s1", format: "google-gemini-v1", index: 0 },
1401+
{
1402+
type: "reasoning.text",
1403+
text: "t1",
1404+
signature: "sig_1",
1405+
id: "rs_t1",
1406+
format: "google-gemini-v1",
1407+
index: 0,
1408+
},
1409+
{ type: "reasoning.encrypted", data: "e1", id: "rs_e1", format: "google-gemini-v1", index: 0 },
1410+
{
1411+
type: "reasoning.text",
1412+
text: "t2",
1413+
signature: "sig_2",
1414+
id: "rs_t2",
1415+
format: "google-gemini-v1",
1416+
index: 0,
1417+
},
1418+
{ type: "reasoning.encrypted", data: "e2", id: "rs_e2", format: "google-gemini-v1", index: 0 },
1419+
{ type: "reasoning.encrypted", data: "e3", id: "rs_e3", format: "google-gemini-v1", index: 1 },
1420+
{ type: "reasoning.summary", summary: "only summary", format: "google-gemini-v1", index: 1 },
1421+
]
1422+
1423+
const result = consolidateReasoningDetails(details)
1424+
1425+
// Full output array, not a subset: consolidated text per index, then the summary-only entry
1426+
// per index, then the last encrypted block per index. The summary interleaved at index 0 is
1427+
// dropped because text exists at that index, while the index-1 summary survives because no
1428+
// text arrived there; a single-type test cannot fail when that predicate narrows. The text
1429+
// entry takes id, format and type from the last item in the group that carries them: the
1430+
// encrypted fragment arrives last at index 0, so index 0 reports rs_e2 and the consolidated
1431+
// text block is labelled reasoning.encrypted (openai-format.ts:93-99, 105). Asserted as
1432+
// measured at this revision, because the full array is what catches that labelling.
1433+
expect(result).toEqual([
1434+
{
1435+
type: "reasoning.encrypted",
1436+
text: "t1t2",
1437+
signature: "sig_2",
1438+
id: "rs_e2",
1439+
format: "google-gemini-v1",
1440+
index: 0,
1441+
},
1442+
{
1443+
type: "reasoning.encrypted",
1444+
data: "e2",
1445+
signature: undefined,
1446+
id: "rs_e2",
1447+
format: "google-gemini-v1",
1448+
index: 0,
1449+
},
1450+
{
1451+
type: "reasoning.summary",
1452+
summary: "only summary",
1453+
signature: undefined,
1454+
id: "rs_e3",
1455+
format: "google-gemini-v1",
1456+
index: 1,
1457+
},
1458+
{
1459+
type: "reasoning.encrypted",
1460+
data: "e3",
1461+
signature: undefined,
1462+
id: "rs_e3",
1463+
format: "google-gemini-v1",
1464+
index: 1,
1465+
},
1466+
])
1467+
})
13971468
})
13981469

13991470
describe("sanitizeGeminiMessages", () => {

0 commit comments

Comments
 (0)