2026-05-19 16:05:09 +08:00
|
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
2026-05-27 23:40:14 +08:00
|
|
|
import { generateEmbeddingActivity } from './ai'
|
2026-06-01 23:16:22 +08:00
|
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-06-01 23:16:22 +08:00
|
|
|
vi.mock('@shumai/core/src/s3/s3', () => ({
|
2026-05-27 23:40:14 +08:00
|
|
|
s3Service: {
|
|
|
|
|
getObject: vi.fn(),
|
|
|
|
|
putObject: vi.fn(),
|
|
|
|
|
headObject: vi.fn(),
|
|
|
|
|
listObjects: vi.fn(),
|
2026-05-19 16:05:09 +08:00
|
|
|
},
|
|
|
|
|
}))
|
|
|
|
|
|
2026-05-28 14:55:59 +08:00
|
|
|
const mockEmbedContent = vi.fn().mockResolvedValue({
|
|
|
|
|
embeddings: [
|
|
|
|
|
{
|
|
|
|
|
values: [0.1, 0.2, 0.3],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
vi.mock('@google/genai', () => {
|
2026-05-27 23:40:14 +08:00
|
|
|
return {
|
2026-05-28 14:55:59 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
|
|
|
GoogleGenAI: class {
|
|
|
|
|
models = {
|
|
|
|
|
embedContent: mockEmbedContent,
|
|
|
|
|
}
|
2026-05-27 23:40:14 +08:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-05-27 23:40:14 +08:00
|
|
|
describe('AI Activities', () => {
|
2026-05-19 16:05:09 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks()
|
2026-05-28 15:21:44 +08:00
|
|
|
process.env.GEMINI_API_KEY = 'test-key'
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
2026-05-27 23:40:14 +08:00
|
|
|
it('should generate image embedding', async () => {
|
|
|
|
|
vi.mocked(s3Service.getObject).mockResolvedValue({
|
|
|
|
|
buffer: Buffer.from('test-image'),
|
|
|
|
|
contentType: 'image/png',
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mock getObject return value needs broad any casting
|
|
|
|
|
} as any)
|
|
|
|
|
|
|
|
|
|
const context = {
|
|
|
|
|
agent: { config: { provider: 'google', model: 'gemini' } },
|
|
|
|
|
asset: { id: 'a1', mediaType: 'image/png', storageKey: { key: 'test.png' } },
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const res = await generateEmbeddingActivity({
|
|
|
|
|
teamId: 't1',
|
|
|
|
|
assetId: 'a1',
|
|
|
|
|
context,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
2026-05-27 23:40:14 +08:00
|
|
|
expect(res.embeddings.length).toBe(1)
|
|
|
|
|
expect(res.embeddings[0].embedding).toEqual([0.1, 0.2, 0.3])
|
|
|
|
|
expect(s3Service.getObject).toHaveBeenCalledWith('shumai', 'test.png')
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
})
|