36 lines
1.1 KiB
JavaScript
36 lines
1.1 KiB
JavaScript
|
|
import { describe, expect, it, vi, afterEach } from 'vitest';
|
||
|
|
import { embedCardImage, formatEmbeddingForPg } from '../../lib/card-embed.js';
|
||
|
|
|
||
|
|
describe('formatEmbeddingForPg', () => {
|
||
|
|
it('formats vectors for pgvector literals', () => {
|
||
|
|
expect(formatEmbeddingForPg([0.1, 0.2, 0.3])).toBe('[0.10000000,0.20000000,0.30000000]');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('embedCardImage', () => {
|
||
|
|
afterEach(() => {
|
||
|
|
vi.unstubAllGlobals();
|
||
|
|
delete process.env.AI_GATEWAY_API_KEY;
|
||
|
|
});
|
||
|
|
|
||
|
|
it('throws when AI_GATEWAY_API_KEY is missing', async () => {
|
||
|
|
await expect(embedCardImage('data:image/jpeg;base64,abc')).rejects.toThrow(
|
||
|
|
'AI_GATEWAY_API_KEY is not configured'
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('returns embedding values from the gateway response', async () => {
|
||
|
|
process.env.AI_GATEWAY_API_KEY = 'test-key';
|
||
|
|
vi.stubGlobal(
|
||
|
|
'fetch',
|
||
|
|
vi.fn(async () => ({
|
||
|
|
ok: true,
|
||
|
|
json: async () => ({ data: [{ embedding: [0.5, 0.25] }] }),
|
||
|
|
}))
|
||
|
|
);
|
||
|
|
|
||
|
|
const embedding = await embedCardImage('data:image/jpeg;base64,abc');
|
||
|
|
expect(embedding).toEqual([0.5, 0.25]);
|
||
|
|
});
|
||
|
|
});
|