Open History to see the last commit
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
28}
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
38}
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 threadId: 'thread-1',
46 adapter: adapterWithMockClient(),
47 messages: [
48 {
49 role: 'user',
50 content: [
51 {
52 type: 'image',
53 source: {
54 type: 'file',
55 value: 'file-openai-abc',
56 provider: 'openai',
57 },
58 },
59 ],
60 },
61 ],
62 }),
63 ),
64 ).rejects.toThrow(/byteplus does not support provider file-handle/)
65 })
66
67 it('rejects a byteplus-keyed file source — the provider has no Files API', async () => {
68 await expect(
69 collectChunks(
70 chat({
71 threadId: 'thread-1',
72 adapter: adapterWithMockClient(),
73 messages: [
74 {
75 role: 'user',
76 content: [
77 {
78 type: 'video',
79 source: {
80 type: 'file',
81 value: 'https://example.com/some-handle',
82 provider: 'byteplus',
83 },
84 },
85 ],
86 },
87 ],
88 }),
89 ),
90 ).rejects.toThrow(/byteplus does not support provider file-handle/)
91 })
92})