|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import { |
| 5 | + OpenAIEmbedder, |
| 6 | + createOpenAIEmbedder, |
| 7 | + OPENAI_COMPATIBLE_PRESETS, |
| 8 | +} from '../index'; |
| 9 | + |
| 10 | +function mockFetch(body: unknown, status = 200): typeof fetch { |
| 11 | + return vi.fn(async () => |
| 12 | + new Response(JSON.stringify(body), { |
| 13 | + status, |
| 14 | + headers: { 'content-type': 'application/json' }, |
| 15 | + }), |
| 16 | + ) as unknown as typeof fetch; |
| 17 | +} |
| 18 | + |
| 19 | +describe('OpenAIEmbedder', () => { |
| 20 | + it('requires apiKey', () => { |
| 21 | + expect(() => new OpenAIEmbedder({ apiKey: '' })).toThrow(/apiKey required/); |
| 22 | + }); |
| 23 | + |
| 24 | + it('exposes id and known dimensions for default model', () => { |
| 25 | + const e = new OpenAIEmbedder({ apiKey: 'k', fetch: mockFetch({ data: [] }) }); |
| 26 | + expect(e.id).toBe('openai'); |
| 27 | + expect(e.dimensions).toBe(1536); |
| 28 | + }); |
| 29 | + |
| 30 | + it('looks up dimensions for known Chinese models', () => { |
| 31 | + const e = new OpenAIEmbedder({ |
| 32 | + apiKey: 'k', |
| 33 | + model: 'text-embedding-v3', |
| 34 | + fetch: mockFetch({ data: [] }), |
| 35 | + }); |
| 36 | + expect(e.dimensions).toBe(1024); |
| 37 | + }); |
| 38 | + |
| 39 | + it('honours explicit dimensions override', () => { |
| 40 | + const e = new OpenAIEmbedder({ |
| 41 | + apiKey: 'k', |
| 42 | + dimensions: 256, |
| 43 | + fetch: mockFetch({ data: [] }), |
| 44 | + }); |
| 45 | + expect(e.dimensions).toBe(256); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns [] for empty input without calling fetch', async () => { |
| 49 | + const fetchImpl = vi.fn() as unknown as typeof fetch; |
| 50 | + const e = new OpenAIEmbedder({ apiKey: 'k', fetch: fetchImpl }); |
| 51 | + const out = await e.embed([]); |
| 52 | + expect(out).toEqual([]); |
| 53 | + expect(fetchImpl).not.toHaveBeenCalled(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('POSTs to the configured baseUrl with bearer auth', async () => { |
| 57 | + const fetchImpl = vi.fn(async () => |
| 58 | + new Response(JSON.stringify({ data: [{ embedding: [0.1, 0.2] }] }), { |
| 59 | + status: 200, |
| 60 | + headers: { 'content-type': 'application/json' }, |
| 61 | + }), |
| 62 | + ) as unknown as typeof fetch; |
| 63 | + |
| 64 | + const e = new OpenAIEmbedder({ |
| 65 | + apiKey: 'sk-test', |
| 66 | + baseUrl: 'https://api.siliconflow.cn/v1', |
| 67 | + model: 'BAAI/bge-m3', |
| 68 | + fetch: fetchImpl, |
| 69 | + }); |
| 70 | + const out = await e.embed(['hello']); |
| 71 | + |
| 72 | + expect(out).toEqual([[0.1, 0.2]]); |
| 73 | + const call = (fetchImpl as unknown as ReturnType<typeof vi.fn>).mock.calls[0]; |
| 74 | + expect(call[0]).toBe('https://api.siliconflow.cn/v1/embeddings'); |
| 75 | + const init = call[1] as RequestInit; |
| 76 | + expect(init.method).toBe('POST'); |
| 77 | + expect((init.headers as Record<string, string>).authorization).toBe('Bearer sk-test'); |
| 78 | + expect(JSON.parse(init.body as string)).toEqual({ model: 'BAAI/bge-m3', input: ['hello'] }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('forwards dimensions in the request body when overridden', async () => { |
| 82 | + const fetchImpl = mockFetch({ data: [{ embedding: [1, 2] }] }); |
| 83 | + const e = new OpenAIEmbedder({ |
| 84 | + apiKey: 'k', |
| 85 | + dimensions: 512, |
| 86 | + fetch: fetchImpl, |
| 87 | + }); |
| 88 | + await e.embed(['x']); |
| 89 | + const body = JSON.parse( |
| 90 | + (fetchImpl as unknown as ReturnType<typeof vi.fn>).mock.calls[0][1].body as string, |
| 91 | + ); |
| 92 | + expect(body.dimensions).toBe(512); |
| 93 | + }); |
| 94 | + |
| 95 | + it('throws a useful error on non-2xx', async () => { |
| 96 | + const fetchImpl = mockFetch({ error: 'bad key' }, 401); |
| 97 | + const e = new OpenAIEmbedder({ apiKey: 'k', fetch: fetchImpl }); |
| 98 | + await expect(e.embed(['x'])).rejects.toThrow(/401/); |
| 99 | + }); |
| 100 | + |
| 101 | + it('throws when response vector count mismatches input', async () => { |
| 102 | + const fetchImpl = mockFetch({ data: [{ embedding: [1] }] }); |
| 103 | + const e = new OpenAIEmbedder({ apiKey: 'k', fetch: fetchImpl }); |
| 104 | + await expect(e.embed(['a', 'b'])).rejects.toThrow(/expected 2 vectors/); |
| 105 | + }); |
| 106 | + |
| 107 | + it('strips trailing slashes from baseUrl', async () => { |
| 108 | + const fetchImpl = mockFetch({ data: [{ embedding: [1] }] }); |
| 109 | + const e = new OpenAIEmbedder({ |
| 110 | + apiKey: 'k', |
| 111 | + baseUrl: 'https://x.example/v1///', |
| 112 | + fetch: fetchImpl, |
| 113 | + }); |
| 114 | + await e.embed(['x']); |
| 115 | + expect( |
| 116 | + (fetchImpl as unknown as ReturnType<typeof vi.fn>).mock.calls[0][0], |
| 117 | + ).toBe('https://x.example/v1/embeddings'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('merges extra headers', async () => { |
| 121 | + const fetchImpl = mockFetch({ data: [{ embedding: [1] }] }); |
| 122 | + const e = new OpenAIEmbedder({ |
| 123 | + apiKey: 'k', |
| 124 | + fetch: fetchImpl, |
| 125 | + headers: { 'x-trace-id': 't1' }, |
| 126 | + }); |
| 127 | + await e.embed(['x']); |
| 128 | + const headers = (fetchImpl as unknown as ReturnType<typeof vi.fn>).mock |
| 129 | + .calls[0][1].headers as Record<string, string>; |
| 130 | + expect(headers['x-trace-id']).toBe('t1'); |
| 131 | + expect(headers.authorization).toBe('Bearer k'); |
| 132 | + }); |
| 133 | +}); |
| 134 | + |
| 135 | +describe('createOpenAIEmbedder presets', () => { |
| 136 | + it('maps preset names to baseUrl', () => { |
| 137 | + const e = createOpenAIEmbedder({ |
| 138 | + preset: 'dashscope', |
| 139 | + apiKey: 'k', |
| 140 | + model: 'text-embedding-v3', |
| 141 | + fetch: mockFetch({ data: [] }), |
| 142 | + }); |
| 143 | + expect(e.id).toBe('dashscope'); |
| 144 | + expect(e.dimensions).toBe(1024); |
| 145 | + }); |
| 146 | + |
| 147 | + it('exposes well-known preset URLs', () => { |
| 148 | + expect(OPENAI_COMPATIBLE_PRESETS.siliconflow).toContain('siliconflow.cn'); |
| 149 | + expect(OPENAI_COMPATIBLE_PRESETS.zhipu).toContain('bigmodel.cn'); |
| 150 | + expect(OPENAI_COMPATIBLE_PRESETS.ollama).toContain('localhost:11434'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('explicit baseUrl wins over preset', async () => { |
| 154 | + const fetchImpl = mockFetch({ data: [{ embedding: [1] }] }); |
| 155 | + const e = createOpenAIEmbedder({ |
| 156 | + preset: 'openai', |
| 157 | + baseUrl: 'https://my-proxy.example/v1', |
| 158 | + apiKey: 'k', |
| 159 | + fetch: fetchImpl, |
| 160 | + }); |
| 161 | + await e.embed(['x']); |
| 162 | + expect( |
| 163 | + (fetchImpl as unknown as ReturnType<typeof vi.fn>).mock.calls[0][0], |
| 164 | + ).toBe('https://my-proxy.example/v1/embeddings'); |
| 165 | + }); |
| 166 | +}); |
0 commit comments