2026-05-27 23:40:14 +08:00
|
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
|
|
|
import { agentChat } from './agent-chat'
|
2026-06-01 23:08:58 +08:00
|
|
|
import { prisma } from '@shumai/db'
|
2026-06-02 18:48:54 +08:00
|
|
|
import { setupTestDbHooks } from '@shumai/db/test'
|
2026-06-01 23:29:11 +08:00
|
|
|
import * as workflowUtils from '@shumai/workflow-core'
|
2026-05-27 23:40:14 +08:00
|
|
|
|
2026-06-01 23:29:11 +08:00
|
|
|
vi.mock('@shumai/workflow-core', async () => {
|
|
|
|
|
const actual = await vi.importActual('@shumai/workflow-core')
|
2026-05-27 23:40:14 +08:00
|
|
|
return {
|
|
|
|
|
...actual,
|
|
|
|
|
getActivities: vi.fn(),
|
|
|
|
|
executeActivity: vi.fn(),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('Agent Chat Workflow', () => {
|
|
|
|
|
setupTestDbHooks()
|
|
|
|
|
|
2026-06-03 15:48:25 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mockActivities holds mock functions cast to expected types
|
|
|
|
|
let mockActivities: any
|
|
|
|
|
|
2026-05-27 23:40:14 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks()
|
|
|
|
|
|
2026-06-03 15:48:25 +08:00
|
|
|
mockActivities = {
|
2026-05-27 23:40:14 +08:00
|
|
|
updateTaskStatusActivity: Object.assign(vi.fn(), {
|
|
|
|
|
_activityName: 'updateTaskStatusActivity',
|
|
|
|
|
}),
|
|
|
|
|
getCommentActivity: Object.assign(vi.fn(), { _activityName: 'getCommentActivity' }),
|
|
|
|
|
createCommentActivity: Object.assign(vi.fn(), { _activityName: 'createCommentActivity' }),
|
|
|
|
|
getAssetActivity: Object.assign(vi.fn(), { _activityName: 'getAssetActivity' }),
|
|
|
|
|
initializeAgentSessionActivity: Object.assign(vi.fn(), {
|
|
|
|
|
_activityName: 'initializeAgentSessionActivity',
|
|
|
|
|
}),
|
|
|
|
|
getAgentChatContextActivity: Object.assign(vi.fn(), {
|
|
|
|
|
_activityName: 'getAgentChatContextActivity',
|
|
|
|
|
}),
|
|
|
|
|
getAgentWorkerQueueActivity: Object.assign(vi.fn(), {
|
|
|
|
|
_activityName: 'getAgentWorkerQueueActivity',
|
|
|
|
|
}),
|
|
|
|
|
agentChatActivity: Object.assign(vi.fn(), { _activityName: 'agentChatActivity' }),
|
|
|
|
|
updateCommentActivity: Object.assign(vi.fn(), { _activityName: 'updateCommentActivity' }),
|
|
|
|
|
updateTaskUsageActivity: Object.assign(vi.fn(), { _activityName: 'updateTaskUsageActivity' }),
|
|
|
|
|
deleteCommentActivity: Object.assign(vi.fn(), { _activityName: 'deleteCommentActivity' }),
|
2026-08-30 15:25:13 +08:00
|
|
|
getAssetPathHierarchyActivity: Object.assign(vi.fn(), {
|
|
|
|
|
_activityName: 'getAssetPathHierarchyActivity',
|
|
|
|
|
}),
|
2026-05-30 17:09:18 +08:00
|
|
|
getAssetPathContextActivity: Object.assign(vi.fn(), {
|
|
|
|
|
_activityName: 'getAssetPathContextActivity',
|
|
|
|
|
}),
|
2026-07-11 20:54:09 +08:00
|
|
|
generateSessionNameActivity: Object.assign(vi.fn(), {
|
|
|
|
|
_activityName: 'generateSessionNameActivity',
|
|
|
|
|
}),
|
2026-07-27 11:24:48 +08:00
|
|
|
getUserTeamInfoActivity: Object.assign(vi.fn(), {
|
|
|
|
|
_activityName: 'getUserTeamInfoActivity',
|
|
|
|
|
}),
|
2026-05-27 23:40:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-03 15:48:25 +08:00
|
|
|
mockActivities.getAgentWorkerQueueActivity.mockResolvedValue('agent_queue')
|
|
|
|
|
mockActivities.updateTaskStatusActivity.mockResolvedValue({})
|
|
|
|
|
mockActivities.createCommentActivity.mockResolvedValue({ id: 'comment-placeholder-id' })
|
2026-07-11 20:54:09 +08:00
|
|
|
mockActivities.generateSessionNameActivity.mockResolvedValue(undefined)
|
2026-07-27 11:24:48 +08:00
|
|
|
mockActivities.getUserTeamInfoActivity.mockResolvedValue({ name: 'Test User', role: 'owner' })
|
2026-06-03 15:48:25 +08:00
|
|
|
mockActivities.getAssetActivity.mockResolvedValue({
|
|
|
|
|
id: 'a1',
|
|
|
|
|
project: { teamId: 't1' },
|
|
|
|
|
type: 'file',
|
2026-06-05 21:25:08 +08:00
|
|
|
name: 'test-file.png',
|
|
|
|
|
mediaType: 'image/png',
|
2026-07-20 10:22:36 +08:00
|
|
|
media: { proxyType: 'image' },
|
2026-06-03 15:48:25 +08:00
|
|
|
parentId: 'parent-folder-id',
|
|
|
|
|
})
|
2026-05-28 09:48:05 +08:00
|
|
|
mockActivities.getCommentActivity.mockResolvedValue({
|
|
|
|
|
id: 'c1',
|
2026-08-30 15:25:13 +08:00
|
|
|
creatorId: 'user-c1',
|
2026-06-03 15:48:25 +08:00
|
|
|
message: 'hello agent',
|
2026-05-28 09:48:05 +08:00
|
|
|
replyToId: null,
|
2026-06-03 15:48:25 +08:00
|
|
|
attachments: [],
|
2026-05-28 09:48:05 +08:00
|
|
|
})
|
2026-05-27 23:40:14 +08:00
|
|
|
mockActivities.initializeAgentSessionActivity.mockResolvedValue('session-123')
|
|
|
|
|
mockActivities.getAgentChatContextActivity.mockResolvedValue({ agent: { id: 'b1' } })
|
2026-08-30 15:25:13 +08:00
|
|
|
mockActivities.getAssetPathHierarchyActivity.mockResolvedValue({
|
|
|
|
|
path: 'folder/subfolder/test-file.png',
|
|
|
|
|
ancestors: [
|
|
|
|
|
{ id: 'f1', name: 'folder' },
|
|
|
|
|
{ id: 'f2', name: 'subfolder' },
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
mockActivities.getAssetPathContextActivity.mockResolvedValue('folder/subfolder/test-file.png')
|
2026-05-27 23:40:14 +08:00
|
|
|
mockActivities.agentChatActivity.mockResolvedValue({
|
2026-06-03 15:48:25 +08:00
|
|
|
text: 'Hello user, how can I help you?',
|
2026-05-27 23:40:14 +08:00
|
|
|
sessionId: 'session-123',
|
2026-06-03 15:48:25 +08:00
|
|
|
usage: { inputTokens: 10, outputTokens: 20, model: 'gemini-model' },
|
2026-05-27 23:40:14 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mockActivities contains vi.fn mock functions which are cast to expected activity proxy types
|
|
|
|
|
vi.mocked(workflowUtils.getActivities).mockReturnValue(mockActivities as any)
|
|
|
|
|
vi.mocked(workflowUtils.executeActivity).mockImplementation(async (_queue, act, ...args) => {
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- act is one of the mocked activities
|
|
|
|
|
return (act as any)(...args)
|
|
|
|
|
})
|
2026-06-03 15:48:25 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should run agent chat workflow successfully with default configurations', async () => {
|
|
|
|
|
const task = await prisma.workflowTask.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: 'chat',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
assetId: 'a1',
|
|
|
|
|
payload: {
|
|
|
|
|
projectId: 'p1',
|
2026-07-27 14:49:41 +08:00
|
|
|
agent: { userCommentId: 'c1', agentId: 'b1' },
|
2026-06-03 15:48:25 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await agentChat(task)
|
|
|
|
|
|
|
|
|
|
// Verify queue discovery
|
|
|
|
|
expect(mockActivities.getAgentWorkerQueueActivity).toHaveBeenCalled()
|
|
|
|
|
|
|
|
|
|
// Verify task status processing
|
|
|
|
|
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
|
|
|
|
taskId: task.id,
|
|
|
|
|
status: 'processing',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Verify comment context fetched
|
|
|
|
|
expect(mockActivities.getCommentActivity).toHaveBeenCalledWith('c1')
|
|
|
|
|
|
2026-08-16 15:44:45 +08:00
|
|
|
// Verify agent context fetched with the project context forwarded
|
|
|
|
|
expect(mockActivities.getAgentChatContextActivity).toHaveBeenCalledWith({
|
|
|
|
|
teamId: 't1',
|
|
|
|
|
agentId: 'b1',
|
|
|
|
|
userId: undefined,
|
|
|
|
|
projectId: 'p1',
|
|
|
|
|
})
|
|
|
|
|
|
2026-06-03 15:48:25 +08:00
|
|
|
// Verify placeholder comment created
|
|
|
|
|
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
|
|
|
|
|
assetId: 'a1',
|
|
|
|
|
message: '__CHAT__',
|
|
|
|
|
sessionId: 'pending',
|
|
|
|
|
agentId: 'b1',
|
|
|
|
|
replyToId: 'c1',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Verify session initialized since sessionId was missing
|
|
|
|
|
expect(mockActivities.initializeAgentSessionActivity).toHaveBeenCalledWith({
|
|
|
|
|
teamId: 't1',
|
|
|
|
|
agentId: 'b1',
|
|
|
|
|
userCommentId: 'c1',
|
|
|
|
|
userId: undefined,
|
|
|
|
|
})
|
|
|
|
|
|
2026-08-30 15:25:13 +08:00
|
|
|
// Verify agent chat called with structured messageContext
|
2026-06-03 15:48:25 +08:00
|
|
|
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
teamId: 't1',
|
|
|
|
|
agentId: 'b1',
|
|
|
|
|
message: 'hello agent',
|
|
|
|
|
sessionId: 'session-123',
|
|
|
|
|
folderId: 'parent-folder-id',
|
2026-08-15 10:25:46 +08:00
|
|
|
assetId: 'a1',
|
2026-08-30 15:25:13 +08:00
|
|
|
messageContext: expect.objectContaining({
|
|
|
|
|
user: { id: 'user-c1', name: 'Test User', role: 'owner' },
|
|
|
|
|
currentAsset: expect.objectContaining({
|
|
|
|
|
id: 'a1',
|
|
|
|
|
name: 'test-file.png',
|
|
|
|
|
type: 'file',
|
|
|
|
|
mediaType: 'image',
|
|
|
|
|
mimeType: 'image/png',
|
|
|
|
|
parentId: 'parent-folder-id',
|
|
|
|
|
path: 'folder/subfolder/test-file.png',
|
|
|
|
|
ancestors: [
|
|
|
|
|
{ id: 'f1', name: 'folder' },
|
|
|
|
|
{ id: 'f2', name: 'subfolder' },
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
}),
|
2026-06-03 15:48:25 +08:00
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Verify comment update
|
|
|
|
|
expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({
|
|
|
|
|
commentId: 'comment-placeholder-id',
|
|
|
|
|
message: 'Hello user, how can I help you?',
|
|
|
|
|
sessionId: 'session-123',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Verify task usage update
|
|
|
|
|
expect(mockActivities.updateTaskUsageActivity).toHaveBeenCalledWith({
|
|
|
|
|
taskId: task.id,
|
|
|
|
|
inputTokens: 10,
|
|
|
|
|
outputTokens: 20,
|
|
|
|
|
model: 'gemini-model',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Verify task completed status
|
|
|
|
|
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
|
|
|
|
taskId: task.id,
|
|
|
|
|
status: 'completed',
|
|
|
|
|
output: { sessionId: 'session-123' },
|
|
|
|
|
})
|
2026-07-11 20:54:09 +08:00
|
|
|
|
|
|
|
|
// Verify session name generation triggered
|
|
|
|
|
expect(mockActivities.generateSessionNameActivity).toHaveBeenCalledWith({
|
|
|
|
|
teamId: 't1',
|
|
|
|
|
agentId: 'b1',
|
|
|
|
|
prompt: 'hello agent',
|
|
|
|
|
sessionId: 'session-123',
|
|
|
|
|
context: expect.any(Object),
|
|
|
|
|
})
|
2026-06-03 15:48:25 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should skip session initialization if sessionId is already provided', async () => {
|
|
|
|
|
const task = await prisma.workflowTask.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: 'chat',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
assetId: 'a1',
|
|
|
|
|
payload: {
|
|
|
|
|
projectId: 'p1',
|
|
|
|
|
agent: { userCommentId: 'c1', agentId: 'b1', sessionId: 'existing-session-456' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await agentChat(task)
|
|
|
|
|
|
|
|
|
|
expect(mockActivities.initializeAgentSessionActivity).not.toHaveBeenCalled()
|
2026-06-03 16:02:06 +08:00
|
|
|
|
2026-06-03 15:48:25 +08:00
|
|
|
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
sessionId: 'existing-session-456',
|
2026-08-30 15:25:13 +08:00
|
|
|
messageContext: expect.objectContaining({
|
|
|
|
|
currentAsset: expect.objectContaining({ id: 'a1', name: 'test-file.png' }),
|
|
|
|
|
}),
|
2026-06-03 15:48:25 +08:00
|
|
|
}),
|
|
|
|
|
)
|
2026-07-11 20:54:09 +08:00
|
|
|
|
|
|
|
|
// Verify session name generation is skipped
|
|
|
|
|
expect(mockActivities.generateSessionNameActivity).not.toHaveBeenCalled()
|
2026-06-03 15:48:25 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should collect S3 keys for image attachments from the user comment', async () => {
|
|
|
|
|
mockActivities.getCommentActivity.mockResolvedValue({
|
|
|
|
|
id: 'c1',
|
|
|
|
|
message: 'look at this',
|
|
|
|
|
replyToId: null,
|
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
asset: {
|
|
|
|
|
mediaType: 'image/png',
|
2026-07-20 10:22:36 +08:00
|
|
|
media: { proxyType: 'image' },
|
2026-06-03 15:48:25 +08:00
|
|
|
storageKey: { key: 'attachments/image1.png' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
asset: {
|
|
|
|
|
mediaType: 'video/mp4', // non-image, should be ignored
|
2026-07-20 10:22:36 +08:00
|
|
|
media: { proxyType: 'video' },
|
2026-06-03 15:48:25 +08:00
|
|
|
storageKey: { key: 'attachments/video.mp4' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
asset: {
|
|
|
|
|
mediaType: 'image/jpeg',
|
2026-07-20 10:22:36 +08:00
|
|
|
media: { proxyType: 'image' },
|
2026-06-03 15:48:25 +08:00
|
|
|
storageKey: { key: 'attachments/image2.jpg' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
})
|
2026-05-27 23:40:14 +08:00
|
|
|
|
|
|
|
|
const task = await prisma.workflowTask.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: 'chat',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
assetId: 'a1',
|
2026-05-28 09:48:05 +08:00
|
|
|
payload: {
|
|
|
|
|
projectId: 'p1',
|
|
|
|
|
agent: { userCommentId: 'c1', agentId: 'b1' },
|
|
|
|
|
},
|
2026-05-27 23:40:14 +08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await agentChat(task)
|
|
|
|
|
|
2026-06-03 15:48:25 +08:00
|
|
|
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
imageUrls: ['attachments/image1.png', 'attachments/image2.jpg'],
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should format instructions with asset media info when available', async () => {
|
|
|
|
|
mockActivities.getAssetActivity.mockResolvedValue({
|
|
|
|
|
id: 'a1',
|
|
|
|
|
project: { teamId: 't1' },
|
|
|
|
|
type: 'file',
|
2026-06-05 21:25:08 +08:00
|
|
|
name: 'test-asset',
|
|
|
|
|
mediaType: 'video/mp4',
|
2026-06-03 15:48:25 +08:00
|
|
|
media: {
|
|
|
|
|
duration: 10,
|
|
|
|
|
height: 1080,
|
|
|
|
|
width: 1920,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const task = await prisma.workflowTask.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: 'chat',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
assetId: 'a1',
|
|
|
|
|
payload: {
|
|
|
|
|
projectId: 'p1',
|
|
|
|
|
agent: { userCommentId: 'c1', agentId: 'b1' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await agentChat(task)
|
|
|
|
|
|
|
|
|
|
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
2026-08-30 15:25:13 +08:00
|
|
|
messageContext: expect.objectContaining({
|
|
|
|
|
currentAsset: expect.objectContaining({
|
|
|
|
|
durationSeconds: 10,
|
|
|
|
|
}),
|
|
|
|
|
}),
|
2026-06-03 15:48:25 +08:00
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should handle errors gracefully by updating the placeholder comment with error message and marking task as failed', async () => {
|
|
|
|
|
mockActivities.agentChatActivity.mockRejectedValue(new Error('AI error: API limit reached'))
|
|
|
|
|
|
|
|
|
|
const task = await prisma.workflowTask.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: 'chat',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
assetId: 'a1',
|
|
|
|
|
payload: {
|
|
|
|
|
projectId: 'p1',
|
|
|
|
|
agent: { userCommentId: 'c1', agentId: 'b1' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await expect(agentChat(task)).rejects.toThrow('AI error: API limit reached')
|
|
|
|
|
|
|
|
|
|
// Verify placeholder updated with formatted error message
|
|
|
|
|
expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({
|
|
|
|
|
commentId: 'comment-placeholder-id',
|
|
|
|
|
message: 'AI error: API limit reached',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Verify status updated to failed
|
|
|
|
|
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
|
|
|
|
taskId: task.id,
|
|
|
|
|
status: 'failed',
|
|
|
|
|
output: { error: 'AI error: API limit reached' },
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should handle generic errors in error handling block', async () => {
|
|
|
|
|
mockActivities.agentChatActivity.mockRejectedValue(new Error('Some DB connection error'))
|
|
|
|
|
|
|
|
|
|
const task = await prisma.workflowTask.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: 'chat',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
assetId: 'a1',
|
|
|
|
|
payload: {
|
|
|
|
|
projectId: 'p1',
|
|
|
|
|
agent: { userCommentId: 'c1', agentId: 'b1' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await expect(agentChat(task)).rejects.toThrow('Some DB connection error')
|
|
|
|
|
|
|
|
|
|
// Verify placeholder updated with generic error message
|
|
|
|
|
expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({
|
|
|
|
|
commentId: 'comment-placeholder-id',
|
|
|
|
|
message: 'Sorry, I encountered an error while processing your request.',
|
|
|
|
|
})
|
2026-05-27 23:40:14 +08:00
|
|
|
})
|
2026-07-07 11:42:40 +08:00
|
|
|
|
|
|
|
|
it('should execute direct-context chatbot flow resolving file metadata and skipping comment placeholder', async () => {
|
|
|
|
|
mockActivities.getAssetActivity.mockImplementation(async (id: string) => {
|
|
|
|
|
if (id === 'file-attachment-1') {
|
2026-07-09 10:40:24 +08:00
|
|
|
return {
|
|
|
|
|
id: 'file-attachment-1',
|
|
|
|
|
name: 'attachment.png',
|
|
|
|
|
type: 'file',
|
|
|
|
|
mediaType: 'image/png',
|
2026-08-30 15:25:13 +08:00
|
|
|
media: { proxyType: 'image' },
|
2026-07-09 10:40:24 +08:00
|
|
|
projectId: 'p1',
|
|
|
|
|
}
|
2026-07-07 11:42:40 +08:00
|
|
|
}
|
|
|
|
|
if (id === 'referenced-asset-1') {
|
2026-07-09 10:40:24 +08:00
|
|
|
return {
|
|
|
|
|
id: 'referenced-asset-1',
|
|
|
|
|
name: 'ref-folder',
|
|
|
|
|
type: 'folder',
|
|
|
|
|
mediaType: null,
|
2026-08-30 15:25:13 +08:00
|
|
|
media: null,
|
2026-07-09 10:40:24 +08:00
|
|
|
projectId: 'p1',
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
id,
|
|
|
|
|
name: 'test-file.png',
|
|
|
|
|
type: 'file',
|
|
|
|
|
mediaType: 'image/png',
|
2026-08-30 15:25:13 +08:00
|
|
|
media: { proxyType: 'image' },
|
2026-07-09 10:40:24 +08:00
|
|
|
projectId: 'p1',
|
|
|
|
|
project: { teamId: 't1' },
|
2026-07-07 11:42:40 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
mockActivities.getAssetPathContextActivity.mockImplementation(async (id: string) => {
|
|
|
|
|
if (id === 'file-attachment-1') return 'attachment.png'
|
|
|
|
|
if (id === 'referenced-asset-1') return 'ref-folder'
|
|
|
|
|
return 'folder/subfolder/file.png'
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const task = await prisma.workflowTask.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: 'chat',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
assetId: 'a1',
|
|
|
|
|
payload: {
|
|
|
|
|
projectId: 'p1',
|
|
|
|
|
agent: {
|
|
|
|
|
agentId: 'b1',
|
|
|
|
|
sessionId: 'session-direct-123',
|
|
|
|
|
prompt: 'chatbot prompt',
|
|
|
|
|
attachedFiles: ['file-attachment-1'],
|
|
|
|
|
assetIds: ['referenced-asset-1'],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await agentChat(task)
|
|
|
|
|
|
|
|
|
|
// Verify placeholder comment is NOT created
|
|
|
|
|
expect(mockActivities.createCommentActivity).not.toHaveBeenCalled()
|
|
|
|
|
expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled()
|
|
|
|
|
|
2026-08-30 15:25:13 +08:00
|
|
|
// Verify messageContext is populated with attachedFiles and referencedAssets
|
2026-07-14 10:25:06 +08:00
|
|
|
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
teamId: 't1',
|
|
|
|
|
agentId: 'b1',
|
|
|
|
|
message: 'chatbot prompt',
|
|
|
|
|
imageUrls: [],
|
|
|
|
|
projectId: 'p1',
|
|
|
|
|
folderId: '',
|
|
|
|
|
sessionId: 'session-direct-123',
|
|
|
|
|
userId: undefined,
|
|
|
|
|
userCommentId: undefined,
|
|
|
|
|
context: { agent: { id: 'b1' } },
|
2026-08-30 15:25:13 +08:00
|
|
|
messageContext: expect.objectContaining({
|
|
|
|
|
attachedFiles: [
|
|
|
|
|
{
|
|
|
|
|
id: 'file-attachment-1',
|
|
|
|
|
name: 'attachment.png',
|
|
|
|
|
type: 'file',
|
|
|
|
|
mediaType: 'image',
|
|
|
|
|
mimeType: 'image/png',
|
|
|
|
|
path: 'attachment.png',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
referencedAssets: [
|
|
|
|
|
{
|
|
|
|
|
id: 'referenced-asset-1',
|
|
|
|
|
name: 'ref-folder',
|
|
|
|
|
type: 'folder',
|
|
|
|
|
mediaType: undefined,
|
|
|
|
|
mimeType: undefined,
|
|
|
|
|
path: 'ref-folder',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}),
|
2026-07-14 10:25:06 +08:00
|
|
|
}),
|
|
|
|
|
)
|
2026-07-07 11:42:40 +08:00
|
|
|
})
|
2026-07-21 15:04:38 +08:00
|
|
|
|
|
|
|
|
it('should handle version stack asset by building instructions with resolved latest version asset details', async () => {
|
|
|
|
|
mockActivities.getAssetActivity.mockImplementation(async (id: string) => {
|
|
|
|
|
if (id === 'vs-1') {
|
|
|
|
|
return {
|
|
|
|
|
id: 'v2-latest',
|
|
|
|
|
project: { teamId: 't1' },
|
|
|
|
|
type: 'file',
|
|
|
|
|
name: 'latest-video.mp4',
|
|
|
|
|
mediaType: 'video/mp4',
|
|
|
|
|
media: { proxyType: 'video', duration: 42 },
|
|
|
|
|
parentId: 'vs-1',
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const task = await prisma.workflowTask.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: 'chat',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
assetId: 'vs-1',
|
|
|
|
|
payload: {
|
|
|
|
|
projectId: 'p1',
|
|
|
|
|
agent: {
|
|
|
|
|
prompt: 'Explain this version stack video',
|
|
|
|
|
sessionId: 'session-vs-123',
|
|
|
|
|
agentId: 'b1',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await agentChat(task)
|
|
|
|
|
|
|
|
|
|
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
teamId: 't1',
|
|
|
|
|
agentId: 'b1',
|
|
|
|
|
message: 'Explain this version stack video',
|
|
|
|
|
sessionId: 'session-vs-123',
|
2026-08-30 15:25:13 +08:00
|
|
|
messageContext: expect.objectContaining({
|
|
|
|
|
currentAsset: expect.objectContaining({
|
|
|
|
|
id: 'v2-latest',
|
|
|
|
|
durationSeconds: 42,
|
|
|
|
|
}),
|
|
|
|
|
}),
|
2026-07-21 15:04:38 +08:00
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
})
|
2026-07-27 11:24:48 +08:00
|
|
|
|
|
|
|
|
it('should inject user info for 1-on-1 new chat sessions', async () => {
|
|
|
|
|
const task = await prisma.workflowTask.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: 'chat',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
assetId: 'a1',
|
|
|
|
|
payload: {
|
|
|
|
|
projectId: 'p1',
|
|
|
|
|
agent: {
|
|
|
|
|
prompt: 'Hello agent from 1-on-1 chat',
|
|
|
|
|
sessionId: 'session-new-1-on-1',
|
|
|
|
|
agentId: 'b1',
|
|
|
|
|
userId: 'user-alice',
|
|
|
|
|
isNewChat: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await agentChat(task)
|
|
|
|
|
|
|
|
|
|
expect(mockActivities.getUserTeamInfoActivity).toHaveBeenCalledWith({
|
|
|
|
|
userId: 'user-alice',
|
|
|
|
|
teamId: 't1',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
2026-08-30 15:25:13 +08:00
|
|
|
messageContext: expect.objectContaining({
|
|
|
|
|
user: { id: 'user-alice', name: 'Test User', role: 'owner' },
|
|
|
|
|
}),
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should populate comment attachments into structured messageContext.attachedFiles', async () => {
|
|
|
|
|
mockActivities.getCommentActivity.mockResolvedValue({
|
|
|
|
|
id: 'c1',
|
|
|
|
|
creatorId: 'user-c1',
|
|
|
|
|
message: 'check these attachments',
|
|
|
|
|
replyToId: null,
|
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
asset: {
|
|
|
|
|
id: 'att-doc-1',
|
|
|
|
|
name: 'brief.pdf',
|
|
|
|
|
type: 'file',
|
|
|
|
|
mediaType: 'application/pdf',
|
|
|
|
|
media: { proxyType: 'pdf' },
|
|
|
|
|
storageKey: { key: 'brief.pdf' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
asset: {
|
|
|
|
|
id: 'att-img-1',
|
|
|
|
|
name: 'mockup.png',
|
|
|
|
|
type: 'file',
|
|
|
|
|
mediaType: 'image/png',
|
|
|
|
|
media: { proxyType: 'image' },
|
|
|
|
|
storageKey: { key: 'mockup.png' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
mockActivities.getAssetPathContextActivity.mockImplementation(async (id: string) => {
|
|
|
|
|
if (id === 'att-doc-1') return 'Docs/brief.pdf'
|
|
|
|
|
if (id === 'att-img-1') return 'Designs/mockup.png'
|
|
|
|
|
return 'folder/subfolder/file.png'
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const task = await prisma.workflowTask.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: 'chat',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
assetId: 'a1',
|
|
|
|
|
payload: {
|
|
|
|
|
projectId: 'p1',
|
|
|
|
|
agent: { userCommentId: 'c1', agentId: 'b1' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await agentChat(task)
|
|
|
|
|
|
|
|
|
|
expect(mockActivities.agentChatActivity).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
imageUrls: ['mockup.png'],
|
|
|
|
|
messageContext: expect.objectContaining({
|
|
|
|
|
attachedFiles: [
|
|
|
|
|
{
|
|
|
|
|
id: 'att-doc-1',
|
|
|
|
|
name: 'brief.pdf',
|
|
|
|
|
type: 'file',
|
|
|
|
|
mediaType: 'pdf',
|
|
|
|
|
mimeType: 'application/pdf',
|
|
|
|
|
path: 'Docs/brief.pdf',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 'att-img-1',
|
|
|
|
|
name: 'mockup.png',
|
|
|
|
|
type: 'file',
|
|
|
|
|
mediaType: 'image',
|
|
|
|
|
mimeType: 'image/png',
|
|
|
|
|
path: 'Designs/mockup.png',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}),
|
2026-07-27 11:24:48 +08:00
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
})
|
2026-05-27 23:40:14 +08:00
|
|
|
})
|