-
-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Expand file tree
/
Copy pathTwelveLabs.test.ts
More file actions
73 lines (63 loc) · 2.83 KB
/
Copy pathTwelveLabs.test.ts
File metadata and controls
73 lines (63 loc) · 2.83 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const mockPost = jest.fn()
const mockGet = jest.fn()
jest.mock('axios', () => ({ post: (...args: any[]) => mockPost(...args), get: (...args: any[]) => mockGet(...args) }))
jest.mock('../../../src/utils', () => ({
getCredentialData: jest.fn(),
getCredentialParam: jest.fn(),
handleEscapeCharacters: jest.fn((input) => input)
}))
import { getCredentialData, getCredentialParam } from '../../../src/utils'
const { nodeClass: TwelveLabsVideo } = require('./TwelveLabs')
describe('TwelveLabs Video Document Loader', () => {
beforeEach(() => {
jest.clearAllMocks()
;(getCredentialData as jest.Mock).mockResolvedValue({ twelveLabsApiKey: 'tl-key' })
;(getCredentialParam as jest.Mock).mockImplementation((key, data) => data[key])
})
it('submits an analyze task and returns the generated text as a document', async () => {
mockPost.mockResolvedValue({ data: { task_id: 'task-123', status: 'pending' } })
mockGet.mockResolvedValue({ data: { task_id: 'task-123', status: 'ready', result: { data: 'A cat plays piano.' } } })
const node = new TwelveLabsVideo()
const docs = await node.init(
{
credential: 'cred-1',
inputs: { videoUrl: 'https://example.com/v.mp4', prompt: 'Describe', modelName: 'pegasus1.5' },
outputs: { output: 'document' }
},
'',
{}
)
expect(docs).toHaveLength(1)
expect(docs[0].pageContent).toBe('A cat plays piano.')
expect(docs[0].metadata.source).toBe('https://example.com/v.mp4')
expect(mockPost).toHaveBeenCalledWith(
'https://api.twelvelabs.io/v1.3/analyze/tasks',
expect.objectContaining({
model_name: 'pegasus1.5',
video: { type: 'url', url: 'https://example.com/v.mp4' },
prompt: 'Describe'
}),
expect.objectContaining({ headers: { 'x-api-key': 'tl-key' } })
)
})
it('throws when the task fails', async () => {
mockPost.mockResolvedValue({ data: { task_id: 'task-123', status: 'pending' } })
mockGet.mockResolvedValue({ data: { task_id: 'task-123', status: 'failed' } })
const node = new TwelveLabsVideo()
await expect(
node.init(
{
credential: 'cred-1',
inputs: { videoUrl: 'https://example.com/v.mp4', prompt: 'Describe' },
outputs: { output: 'document' }
},
'',
{}
)
).rejects.toThrow('analysis task failed')
})
it('requires a video url', async () => {
const node = new TwelveLabsVideo()
await expect(node.init({ credential: 'cred-1', inputs: {}, outputs: {} }, '', {})).rejects.toThrow('Video URL is required')
})
})