Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

reasoning-request.test.ts

73 lines2.3 KB

1import { afterEach, describe, expect, it, vi } from 'vitest'
2import { resolveDebugOption } from '@tanstack/ai/adapter-internals'
3import { createMistralText } from '../src/adapters/text'
4import type { ReasoningRequest } from '@tanstack/ai'
5import type { MISTRAL_CHAT_MODELS } from '../src/model-meta'
6​
7const logger = resolveDebugOption(false)
8​
9/** Run one streaming call and return the JSON body it sent. */
10async function send(
11 model: (typeof MISTRAL_CHAT_MODELS)[number],
12 reasoning: ReasoningRequest | undefined,
13) {
14 const fetchMock = vi.fn().mockResolvedValue({
15 ok: true,
16 status: 200,
17 body: new ReadableStream({
18 start(controller) {
19 controller.enqueue(new TextEncoder().encode('data: [DONE]\n\n'))
20 controller.close()
21 },
22 }),
23 })
24 vi.stubGlobal('fetch', fetchMock)
25 const adapter = createMistralText(model, 'test-key')
26 for await (const _chunk of adapter.chatStream({
27 logger,
28 model,
29 messages: [{ role: 'user', content: 'hi' }],
30 ...(reasoning ? { reasoning } : {}),
31 })) {
32 // Drain the stream.
33 }
34 const [, init] = fetchMock.mock.calls[0] ?? []
35 return JSON.parse(init.body) as Record<string, unknown>
37​
38const on = (level: ReasoningRequest['level']): ReasoningRequest => ({
39 level,
40 summary: true,
41})
42​
43describe('Mistral chat({ reasoning }) request shape', () => {
44 afterEach(() => {
45 vi.unstubAllGlobals()
46 })
47​
48 it('effort models: reasoning_effort, and none for off', async () => {
49 expect(await send('mistral-small-latest', on('high'))).toMatchObject({
50 reasoning_effort: 'high',
51 })
52 expect(await send('mistral-small-latest', on('off'))).toMatchObject({
53 reasoning_effort: 'none',
54 })
55 })
56​
57 it.each(['magistral-medium-latest', 'magistral-small-latest'] as const)(
58 '%s: prompt_mode reasoning, and nothing for off',
59 async (model) => {
60 const high = await send(model, on('medium'))
61 expect(high.prompt_mode).toBe('reasoning')
62 expect(high).not.toHaveProperty('reasoning_effort')
63 const off = await send(model, on('off'))
64 expect(off).not.toHaveProperty('prompt_mode')
65 },
66 )
67​
68 it('sends no reasoning fields without a request', async () => {
69 const body = await send('mistral-small-latest', undefined)
70 expect(body).not.toHaveProperty('reasoning_effort')
71 expect(body).not.toHaveProperty('prompt_mode')
72 })
73})