-
-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Expand file tree
/
Copy pathTwelveLabsEmbedding.test.ts
More file actions
59 lines (45 loc) · 2.48 KB
/
Copy pathTwelveLabsEmbedding.test.ts
File metadata and controls
59 lines (45 loc) · 2.48 KB
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
const mockPost = jest.fn()
jest.mock('axios', () => ({ post: (...args: any[]) => mockPost(...args) }))
jest.mock('../../../src/utils', () => ({
getBaseClasses: jest.fn().mockReturnValue(['Embeddings']),
getCredentialData: jest.fn(),
getCredentialParam: jest.fn()
}))
import { getCredentialData, getCredentialParam } from '../../../src/utils'
const { nodeClass: TwelveLabsEmbedding } = require('./TwelveLabsEmbedding')
describe('TwelveLabsEmbedding', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('builds a Marengo embedder with the credential api key and default model', async () => {
;(getCredentialData as jest.Mock).mockResolvedValue({ twelveLabsApiKey: 'tl-key' })
;(getCredentialParam as jest.Mock).mockImplementation((key, data) => data[key])
const node = new TwelveLabsEmbedding()
const model = await node.init({ credential: 'cred-1', inputs: { modelName: 'marengo3.0' } }, '', {})
expect(model.apiKey).toBe('tl-key')
expect(model.model).toBe('marengo3.0')
})
it('returns the 512-dim float vector from the /embed response', async () => {
;(getCredentialData as jest.Mock).mockResolvedValue({ twelveLabsApiKey: 'tl-key' })
;(getCredentialParam as jest.Mock).mockImplementation((key, data) => data[key])
const vector = Array.from({ length: 512 }, (_, i) => i / 512)
mockPost.mockResolvedValue({ data: { text_embedding: { segments: [{ float: vector }] } } })
const node = new TwelveLabsEmbedding()
const model = await node.init({ credential: 'cred-1', inputs: {} }, '', {})
const result = await model.embedQuery('a man walking on the beach')
expect(result).toHaveLength(512)
expect(mockPost).toHaveBeenCalledWith(
'https://api.twelvelabs.io/v1.3/embed',
expect.anything(),
expect.objectContaining({ headers: { 'x-api-key': 'tl-key' } })
)
})
it('throws when the response has no embedding', async () => {
;(getCredentialData as jest.Mock).mockResolvedValue({ twelveLabsApiKey: 'tl-key' })
;(getCredentialParam as jest.Mock).mockImplementation((key, data) => data[key])
mockPost.mockResolvedValue({ data: { text_embedding: { segments: [] } } })
const node = new TwelveLabsEmbedding()
const model = await node.init({ credential: 'cred-1', inputs: {} }, '', {})
await expect(model.embedQuery('hi')).rejects.toThrow('did not return a text embedding')
})
})