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

files-source.test.ts

90 lines2.5 KB

1import { describe, expect, it, vi } from 'vitest'
2import { chat } from '@tanstack/ai'
3import { createBytePlusText } from '../src/adapters/text'
4import { BYTEPLUS_CHAT_MODELS } from '../src/model-meta'
5import type { StreamChunk } from '@tanstack/ai'
6​
7// Stub the OpenAI SDK so constructing an adapter never opens a real network
8// handle (same pattern as text.test.ts).
9vi.mock('openai', () => {
10 return {
11 default: class {
12 chat = {
13 completions: {
14 create: vi.fn(),
15 },
16 }
17 },
18 }
19})
20​
21// The guard throws while the request body is being built, so the mocked
22// `create` is never reached — it exists only to fail loudly if a file source
23// ever leaks through to a network call.
24function adapterWithMockClient() {
25 const adapter = createBytePlusText(BYTEPLUS_CHAT_MODELS[0], 'test-key')
26 ;(adapter as any).client = { chat: { completions: { create: vi.fn() } } }
27 return adapter
29​
30async function collectChunks(
31 iterable: AsyncIterable<StreamChunk>,
32): Promise<Array<StreamChunk>> {
33 const chunks: Array<StreamChunk> = []
34 for await (const chunk of iterable) {
35 chunks.push(chunk)
36 }
37 return chunks
39​
40describe('byteplus file content source', () => {
41 it('rejects a foreign provider file reference in core preflight, before any request', async () => {
42 await expect(
43 collectChunks(
44 chat({
45 adapter: adapterWithMockClient(),
46 messages: [
47 {
48 role: 'user',
49 content: [
50 {
51 type: 'image',
52 source: {
53 type: 'file',
54 value: 'file-openai-abc',
55 provider: 'openai',
56 },
57 },
58 ],
59 },
60 ],
61 }),
62 ),
63 ).rejects.toThrow(/byteplus does not support provider file-handle/)
64 })
65​
66 it('rejects a byteplus-keyed file source — the provider has no Files API', async () => {
67 await expect(
68 collectChunks(
69 chat({
70 adapter: adapterWithMockClient(),
71 messages: [
72 {
73 role: 'user',
74 content: [
75 {
76 type: 'video',
77 source: {
78 type: 'file',
79 value: 'https://example.com/some-handle',
80 provider: 'byteplus',
81 },
82 },
83 ],
84 },
85 ],
86 }),
87 ),
88 ).rejects.toThrow(/byteplus does not support provider file-handle/)
89 })
90})