File size: 7,484 Bytes
ca51841 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | import { describe, it, expect, vi } from 'vitest';
import {
buildRequestConfig,
fetchModels,
streamCompletion,
transcribeAudio,
} from '../src/api.js';
// βββ buildRequestConfig ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('buildRequestConfig β dev mode (isDev=true)', () => {
it('uses /lw-proxy as urlBase', () => {
const { urlBase } = buildRequestConfig('http://10.168.21.119:8000', '', true);
expect(urlBase).toBe('/lw-proxy');
});
it('sets X-LW-Target header to the cleaned baseUrl', () => {
const { headers } = buildRequestConfig('http://10.168.21.119:8000/', '', true);
expect(headers['X-LW-Target']).toBe('http://10.168.21.119:8000');
});
it('strips trailing slash from X-LW-Target', () => {
const { headers } = buildRequestConfig('http://localhost:8000/', '', true);
expect(headers['X-LW-Target']).toBe('http://localhost:8000');
});
it('does NOT add Authorization header when apiKey is empty', () => {
const { headers } = buildRequestConfig('http://localhost:8000', '', true);
expect(headers['Authorization']).toBeUndefined();
});
it('adds Authorization header when apiKey is provided', () => {
const { headers } = buildRequestConfig('http://localhost:8000', 'sk-test', true);
expect(headers['Authorization']).toBe('Bearer sk-test');
});
});
describe('buildRequestConfig β prod mode (isDev=false)', () => {
it('uses cleaned baseUrl as urlBase', () => {
const { urlBase } = buildRequestConfig('http://10.168.21.119:8000/', '', false);
expect(urlBase).toBe('http://10.168.21.119:8000');
});
it('does NOT set X-LW-Target header', () => {
const { headers } = buildRequestConfig('http://localhost:8000', '', false);
expect(headers['X-LW-Target']).toBeUndefined();
});
it('does NOT add Authorization header when apiKey is empty', () => {
const { headers } = buildRequestConfig('http://localhost:8000', '', false);
expect(headers['Authorization']).toBeUndefined();
});
it('adds Authorization header when apiKey is provided', () => {
const { headers } = buildRequestConfig('http://localhost:8000', 'sk-key', false);
expect(headers['Authorization']).toBe('Bearer sk-key');
});
});
// βββ fetchModels ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('fetchModels (dev mode via buildRequestConfig isDev=true default in Vitest)', () => {
it('calls /lw-proxy/v1/models and sets X-LW-Target', async () => {
const captured = {};
vi.stubGlobal('fetch', vi.fn(async (url, opts) => {
captured.url = url;
captured.headers = opts?.headers ?? {};
return { ok: true, json: async () => ({ data: [{ id: 'model-a' }] }) };
}));
await fetchModels('http://10.168.21.119:8000', '');
expect(captured.url).toBe('/lw-proxy/v1/models');
expect(captured.headers['X-LW-Target']).toBe('http://10.168.21.119:8000');
vi.unstubAllGlobals();
});
it('parses OpenAI-style { data: [{id}] } response', async () => {
vi.stubGlobal('fetch', vi.fn(async () => ({
ok: true,
json: async () => ({ data: [{ id: 'AXERA-TECH/Qwen3-VL-2B-Instruct' }] }),
})));
const models = await fetchModels('http://10.168.21.119:8000', '');
expect(models).toContain('AXERA-TECH/Qwen3-VL-2B-Instruct');
vi.unstubAllGlobals();
});
it('parses flat string-array response', async () => {
vi.stubGlobal('fetch', vi.fn(async () => ({
ok: true,
json: async () => ['model-x', 'model-y'],
})));
const models = await fetchModels('http://localhost:8000', '');
expect(models).toContain('model-x');
vi.unstubAllGlobals();
});
it('does NOT add Authorization header when apiKey is empty', async () => {
const captured = {};
vi.stubGlobal('fetch', vi.fn(async (url, opts) => {
captured.headers = opts?.headers ?? {};
return { ok: true, json: async () => ({ data: [] }) };
}));
await fetchModels('http://localhost:8000', '');
expect(captured.headers['Authorization']).toBeUndefined();
vi.unstubAllGlobals();
});
it('adds Authorization header when apiKey is provided', async () => {
const captured = {};
vi.stubGlobal('fetch', vi.fn(async (url, opts) => {
captured.headers = opts?.headers ?? {};
return { ok: true, json: async () => ({ data: [] }) };
}));
await fetchModels('http://localhost:8000', 'sk-my-key');
expect(captured.headers['Authorization']).toBe('Bearer sk-my-key');
vi.unstubAllGlobals();
});
it('throws on non-OK HTTP response', async () => {
vi.stubGlobal('fetch', vi.fn(async () => ({
ok: false, status: 500, text: async () => 'Internal error',
})));
await expect(fetchModels('http://localhost:8000', '')).rejects.toThrow('500');
vi.unstubAllGlobals();
});
});
// βββ streamCompletion ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('streamCompletion (dev mode)', () => {
it('calls /lw-proxy/v1/chat/completions and sets X-LW-Target', async () => {
const captured = {};
const sseChunks = [
'data: {"choices":[{"delta":{"content":"Hi"}}]}\n\n',
'data: [DONE]\n\n',
];
let idx = 0;
const reader = {
read: vi.fn(async () =>
idx >= sseChunks.length
? { done: true }
: { done: false, value: new TextEncoder().encode(sseChunks[idx++]) }
),
};
vi.stubGlobal('fetch', vi.fn(async (url, opts) => {
captured.url = url;
captured.headers = opts?.headers ?? {};
return { ok: true, body: { getReader: () => reader } };
}));
const chunks = [];
for await (const chunk of streamCompletion('http://10.168.21.119:8000', '', 'model', [])) {
chunks.push(chunk);
}
expect(captured.url).toBe('/lw-proxy/v1/chat/completions');
expect(captured.headers['X-LW-Target']).toBe('http://10.168.21.119:8000');
expect(chunks).toContain('Hi');
vi.unstubAllGlobals();
});
});
describe('audio uploads', () => {
it('transcribeAudio posts multipart form data to /v1/audio/transcriptions', async () => {
const captured = {};
vi.stubGlobal('fetch', vi.fn(async (url, opts) => {
captured.url = url;
captured.method = opts?.method;
captured.headers = opts?.headers ?? {};
captured.body = opts?.body;
return { ok: true, json: async () => ({ text: 'hello transcript' }) };
}));
const file = new File(['audio'], 'meeting.wav', { type: 'audio/wav' });
const text = await transcribeAudio('http://localhost:8000', 'sk-key', 'audio-model', file);
expect(captured.url).toBe('/lw-proxy/v1/audio/transcriptions');
expect(captured.method).toBe('POST');
expect(captured.headers['Content-Type']).toBeUndefined();
expect(captured.headers['Authorization']).toBe('Bearer sk-key');
expect(captured.body).toBeInstanceOf(FormData);
expect(captured.body.get('model')).toBe('audio-model');
expect(captured.body.get('file').name).toBe(file.name);
expect(captured.body.get('file').type).toBe(file.type);
expect(text).toBe('hello transcript');
vi.unstubAllGlobals();
});
});
|