2026-05-19 16:05:09 +08:00
import { describe , it , expect , vi , beforeEach } from 'vitest'
2026-06-03 15:48:25 +08:00
import {
generateTextEmbeddingActivity ,
extractAiMetadataActivity ,
getEmbeddingContextActivity ,
saveAssetEmbeddingsActivity ,
2026-06-18 10:57:22 +08:00
generateImageEmbeddingActivity ,
generateVideoChunkEmbeddingActivity ,
2026-06-03 15:48:25 +08:00
} from './ai'
2026-06-01 23:16:22 +08:00
import { s3Service } from '@shumai/core/src/s3/s3'
2026-06-16 09:53:28 +08:00
import { transcodeService } from '@shumai/core'
2026-06-03 15:48:25 +08:00
import { prisma , AssetType , AssetStatus } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
vi . mock ( 'fs' , async ( importOriginal ) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- importOriginal returns the raw fs module, typed dynamically as any here
const actual = await importOriginal < any >()
const mockRead = vi . fn (( path , options ) => {
if ( typeof path === 'string' && ( path . includes ( 'video-chunk' ) || path . includes ( 'out1.webp' ))) {
return Buffer . from ( 'chunk-data' )
}
return actual . readFileSync ( path , options )
})
return {
... actual ,
writeFileSync : vi.fn (),
readFileSync : mockRead ,
unlinkSync : vi.fn (),
}
})
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-06-16 09:47:27 +08:00
vi . mock ( '@shumai/core/src/transcode/transcode' , () => ({
2026-06-03 15:48:25 +08:00
transcodeService : {
extractVideoFrames : vi.fn (),
},
}))
vi . mock ( 'child_process' , () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mock functions dynamically return varying types based on test command calls
2026-06-05 23:32:27 +08:00
const execFileMock : any = vi . fn (( file : any , args : any , cb : any ) => {
2026-06-03 15:48:25 +08:00
cb ( null , '120.0\n' , '' )
})
// Setup custom promisify behavior to return { stdout, stderr }
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mock functions dynamically return varying types based on test command calls
2026-06-05 23:32:27 +08:00
execFileMock [ Symbol . for ( 'nodejs.util.promisify.custom' )] = vi . fn ( async ( file : any ) => {
if ( file === 'ffprobe' ) {
2026-06-03 15:48:25 +08:00
return { stdout : '120.0\n' , stderr : '' }
}
return { stdout : '' , stderr : '' }
})
return {
2026-06-05 23:32:27 +08:00
execFile : execFileMock ,
2026-06-03 15:48:25 +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-06-03 15:48:25 +08:00
// eslint-disable-next-line @typescript-eslint/naming-convention -- matches external library GoogleGenAI export naming convention
2026-05-28 14:55:59 +08:00
GoogleGenAI : class {
models = {
embedContent : mockEmbedContent ,
}
2026-05-27 23:40:14 +08:00
},
}
})
2026-05-19 16:05:09 +08:00
2026-06-03 15:48:25 +08:00
describe ( 'AI Activities Unit Tests' , () => {
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' ,
2026-06-03 15:48:25 +08:00
} as unknown as { buffer : Buffer ; contentType : string })
2026-05-27 23:40:14 +08:00
2026-06-18 10:57:22 +08:00
const res = await generateImageEmbeddingActivity ({
2026-05-27 23:40:14 +08:00
teamId : 't1' ,
2026-06-18 10:57:22 +08:00
assetKey : 'test.png' ,
mediaType : 'image/png' ,
2026-05-19 16:05:09 +08:00
})
2026-06-18 10:57:22 +08:00
expect ( res . embedding ). toEqual ([ 0.1 , 0.2 , 0.3 ])
2026-05-27 23:40:14 +08:00
expect ( s3Service . getObject ). toHaveBeenCalledWith ( 'shumai' , 'test.png' )
2026-05-19 16:05:09 +08:00
})
2026-06-03 15:48:25 +08:00
it ( 'should generate video embedding in chunks' , async () => {
vi . mocked ( s3Service . getObject ). mockResolvedValue ({
buffer : Buffer.from ( 'test-video' ),
contentType : 'video/mp4' ,
} as unknown as { buffer : Buffer ; contentType : string })
2026-06-18 10:57:22 +08:00
const res = await generateVideoChunkEmbeddingActivity ({
2026-06-03 15:48:25 +08:00
teamId : 't1' ,
2026-06-18 11:54:30 +08:00
chunkKey : 'files/a1/tmp-embedding-chunks/chunk-0-60.mp4' ,
2026-06-03 15:48:25 +08:00
})
2026-06-18 10:57:22 +08:00
expect ( res . embedding ). toEqual ([ 0.1 , 0.2 , 0.3 ])
2026-06-18 11:54:30 +08:00
expect ( s3Service . getObject ). toHaveBeenCalledWith (
'shumai' ,
'files/a1/tmp-embedding-chunks/chunk-0-60.mp4' ,
)
2026-06-03 15:48:25 +08:00
})
it ( 'should generate text embedding successfully' , async () => {
const res = await generateTextEmbeddingActivity ({
text : 'Search query' ,
teamId : 't1' ,
})
expect ( res . embedding ). toEqual ([ 0.1 , 0.2 , 0.3 ])
expect ( mockEmbedContent ). toHaveBeenCalledWith (
expect . objectContaining ({
model : 'gemini-embedding-2' ,
contents : [
expect . objectContaining ({
parts : [{ text : 'Search query' }],
}),
],
}),
)
})
describe ( 'extractAiMetadataActivity' , () => {
it ( 'should return existing images if they are already in S3 storage' , async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mock returned object cast as any due to partial implementation
vi . mocked ( s3Service . headObject ). mockResolvedValue ({} as any )
vi . mocked ( s3Service . listObjects ). mockResolvedValue ([
'project/ai_metadata/1.webp' ,
'project/ai_metadata/2.webp' ,
])
const res = await extractAiMetadataActivity ({
assetKey : 'project/video.mp4' ,
filePath : '/tmp/video.mp4' ,
type : 'autofill' ,
isImage : false ,
})
expect ( res ). toEqual ([ 'project/ai_metadata/1.webp' , 'project/ai_metadata/2.webp' ])
expect ( s3Service . headObject ). toHaveBeenCalledWith ( 'shumai' , 'project/ai_metadata/1.webp' )
expect ( transcodeService . extractVideoFrames ). not . toHaveBeenCalled ()
})
it ( 'should extract frames and upload to S3 if not already present' , async () => {
vi . mocked ( s3Service . headObject ). mockRejectedValue ( new Error ( 'Not Found' ))
vi . mocked ( transcodeService . extractVideoFrames ). mockResolvedValue ([ '/tmp/out1.webp' ])
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mock returned object cast as any due to partial implementation
vi . mocked ( s3Service . putObject ). mockResolvedValue ({} as any )
const res = await extractAiMetadataActivity ({
assetKey : 'project/video.mp4' ,
filePath : '/tmp/video.mp4' ,
type : 'autofill' ,
isImage : false ,
})
expect ( res ). toEqual ([ 'project/ai_metadata/out1.webp' ])
expect ( transcodeService . extractVideoFrames ). toHaveBeenCalledWith ({
inputFile : '/tmp/video.mp4' ,
outputDir : '/tmp' ,
numFrames : 30 ,
frameHeight : 720 ,
isImage : false ,
})
expect ( s3Service . putObject ). toHaveBeenCalledWith (
'shumai' ,
'project/ai_metadata/out1.webp' ,
expect . any ( Buffer ),
expect . any ( Number ),
'image/webp' ,
)
})
})
})
describe ( 'AI Database Activities Integration' , () => {
setupTestDbHooks ()
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- variables hold Prisma model output types that are difficult to type explicitly in tests
let team : any
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- variables hold Prisma model output types that are difficult to type explicitly in tests
let user : any
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- variables hold Prisma model output types that are difficult to type explicitly in tests
let asset : any
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- variables hold Prisma model output types that are difficult to type explicitly in tests
let storageKey : any
beforeEach ( async () => {
vi . restoreAllMocks ()
team = await prisma . team . create ({
data : { name : 'AI Team' },
})
user = await prisma . user . create ({
data : {
name : 'AI Agent User' ,
email : 'aiagent@shumai.ai' ,
type : 'agent' ,
},
})
// Add user as a member of the team
await prisma . teamMember . create ({
data : {
teamId : team.id ,
userId : user.id ,
role : 'editor' ,
},
})
storageKey = await prisma . storageKey . create ({
data : {
key : 'media/video.mp4' ,
},
})
asset = await prisma . asset . create ({
data : {
name : 'video.mp4' ,
type : AssetType . file ,
status : AssetStatus.uploaded ,
mediaType : 'video/mp4' ,
storageKeyId : storageKey.id ,
},
})
})
describe ( 'getEmbeddingContextActivity' , () => {
it ( 'should retrieve embedding context and throw if embedding agent not configured/enabled' , async () => {
// Expect throw since agent not created yet
await expect (
getEmbeddingContextActivity ({ teamId : team.id , assetId : asset.id }),
). rejects . toThrow ( 'embedding feature is disabled or agent not found' )
// Create embedding agent
await prisma . agent . create ({
data : {
id : user.id , // match user.id
teamId : team.id ,
type : 'embedding' ,
enabled : true ,
config : {
provider : 'openai' ,
model : 'gpt-4' ,
},
},
})
const ctx = await getEmbeddingContextActivity ({
teamId : team.id ,
assetId : asset.id ,
})
expect ( ctx . agent . id ). toBe ( user . id )
expect ( ctx . asset . id ). toBe ( asset . id )
expect ( ctx . asset . mediaType ). toBe ( 'video/mp4' )
})
2026-06-18 11:14:09 +08:00
it ( 'should parse valid positive chunk duration and fallback on invalid or negative ones' , async () => {
// Create embedding agent if not already present
const existingAgent = await prisma . agent . findUnique ({ where : { id : user.id } })
if ( ! existingAgent ) {
await prisma . agent . create ({
data : {
id : user.id ,
teamId : team.id ,
type : 'embedding' ,
enabled : true ,
config : {
provider : 'openai' ,
model : 'gpt-4' ,
},
},
})
}
// Test default/fallback
delete process . env . EMBEDDING_CHUNK_DURATION
let ctx = await getEmbeddingContextActivity ({
teamId : team.id ,
assetId : asset.id ,
})
expect ( ctx . chunkDuration ). toBe ( 60.0 )
2026-06-18 19:45:58 +08:00
expect ( ctx . chunkOverlap ). toBe ( 5.0 )
2026-06-18 11:14:09 +08:00
// Test valid positive
process . env . EMBEDDING_CHUNK_DURATION = '45.5'
2026-06-18 19:45:58 +08:00
process . env . EMBEDDING_CHUNK_OVERLAP = '10.0'
2026-06-18 11:14:09 +08:00
ctx = await getEmbeddingContextActivity ({
teamId : team.id ,
assetId : asset.id ,
})
expect ( ctx . chunkDuration ). toBe ( 45.5 )
2026-06-18 19:45:58 +08:00
expect ( ctx . chunkOverlap ). toBe ( 10.0 )
2026-06-18 11:14:09 +08:00
2026-06-18 19:45:58 +08:00
// Test negative/invalid for overlap
process . env . EMBEDDING_CHUNK_OVERLAP = '-5.0'
2026-06-18 11:14:09 +08:00
ctx = await getEmbeddingContextActivity ({
teamId : team.id ,
assetId : asset.id ,
})
2026-06-18 19:45:58 +08:00
expect ( ctx . chunkOverlap ). toBe ( 5.0 )
2026-06-18 11:14:09 +08:00
2026-06-18 19:45:58 +08:00
process . env . EMBEDDING_CHUNK_OVERLAP = 'invalid'
2026-06-18 11:14:09 +08:00
ctx = await getEmbeddingContextActivity ({
teamId : team.id ,
assetId : asset.id ,
})
2026-06-18 19:45:58 +08:00
expect ( ctx . chunkOverlap ). toBe ( 5.0 )
2026-06-18 11:14:09 +08:00
// Cleanup env
delete process . env . EMBEDDING_CHUNK_DURATION
2026-06-18 19:45:58 +08:00
delete process . env . EMBEDDING_CHUNK_OVERLAP
2026-06-18 11:14:09 +08:00
})
2026-06-03 15:48:25 +08:00
})
describe ( 'saveAssetEmbeddingsActivity' , () => {
it ( 'should save embeddings with start and end times to the database' , async () => {
const embeddings = [
{
embedding : Array ( 1536 ). fill ( 0.01 ),
startTime : 0.0 ,
endTime : 60.0 ,
},
{
embedding : Array ( 1536 ). fill ( 0.02 ),
},
]
await saveAssetEmbeddingsActivity ({
assetId : asset.id ,
embeddings ,
})
// Query raw embeddings from DB
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- db query output is a raw postgres result array typed as any
const dbEmbeddings : any [] = await prisma . $queryRaw `
SELECT id, asset_id as "assetId", start_time as "startTime", end_time as "endTime"
FROM asset_embeddings
WHERE asset_id = ${ asset . id }
ORDER BY start_time ASC NULLS LAST
`
expect ( dbEmbeddings . length ). toBe ( 2 )
expect ( dbEmbeddings [ 0 ]. startTime ). toBe ( 0.0 )
expect ( dbEmbeddings [ 0 ]. endTime ). toBe ( 60.0 )
expect ( dbEmbeddings [ 1 ]. startTime ). toBeNull ()
})
})
2026-05-19 16:05:09 +08:00
})