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

document-input.test.ts

64 lines1.9 KB

1import { describe, expect, it } from 'vitest'
2import { createMistralText } from '../src/adapters/text'
3import type { DocumentPart } from '@tanstack/ai'
4​
5// `convertContentPartToMistral` is private; reach it directly so the test
6// exercises the document mapping without standing up an HTTP mock. The
7// `messageToWire` step then snake-cases `documentUrl` -> `document_url`.
8const convertContentPart = (
9 adapter: ReturnType<typeof createMistralText>,
10 part: DocumentPart,
11): unknown =>
12 (
13 adapter as unknown as {
14 convertContentPartToMistral: (p: DocumentPart) => unknown
15 }
16 ).convertContentPartToMistral(part)
17​
18describe('mistral document content parts', () => {
19 const adapter = createMistralText('mistral-small-latest', 'test-key')
20​
21 it('maps a hosted-URL document to a document_url part', () => {
22 const part: DocumentPart = {
23 type: 'document',
24 source: { type: 'url', value: 'https://example.com/contract.pdf' },
25 }
26​
27 expect(convertContentPart(adapter, part)).toEqual({
28 type: 'document_url',
29 documentUrl: 'https://example.com/contract.pdf',
30 })
31 })
32​
33 it('wraps inline base64 document bytes in a data: URL', () => {
34 const part: DocumentPart = {
35 type: 'document',
36 source: {
37 type: 'data',
38 value: 'JVBERi0xLjQ=',
39 mimeType: 'application/pdf',
40 },
41 }
42​
43 expect(convertContentPart(adapter, part)).toEqual({
44 type: 'document_url',
45 documentUrl: 'data:application/pdf;base64,JVBERi0xLjQ=',
46 })
47 })
48​
49 it('passes through a data: URL that is already formatted', () => {
50 const part: DocumentPart = {
51 type: 'document',
52 source: {
53 type: 'data',
54 value: 'data:application/pdf;base64,JVBERi0xLjQ=',
55 mimeType: 'application/pdf',
56 },
57 }
58​
59 expect(convertContentPart(adapter, part)).toEqual({
60 type: 'document_url',
61 documentUrl: 'data:application/pdf;base64,JVBERi0xLjQ=',
62 })
63 })
64})