2026-05-19 16:05:09 +08:00
|
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
|
|
|
import { setupTestDbHooks } from '@/db-test-hooks'
|
2026-05-21 22:51:40 +08:00
|
|
|
import { prisma } from '@/db'
|
2026-05-19 16:05:09 +08:00
|
|
|
import { aiChatActivity } from './agent'
|
2026-05-20 22:43:31 +08:00
|
|
|
import { agentService } from '@/services/agent/agent'
|
2026-05-22 09:20:49 +08:00
|
|
|
import type { SessionTreeEntry } from '@earendil-works/pi-agent-core'
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-05-20 22:43:31 +08:00
|
|
|
vi.mock('@/services/agent/agent', () => ({
|
|
|
|
|
agentService: {
|
2026-05-19 16:05:09 +08:00
|
|
|
chatWithAgent: vi.fn(),
|
|
|
|
|
},
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
describe('Agent Activities', () => {
|
|
|
|
|
setupTestDbHooks()
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should call aiChatActivity', async () => {
|
2026-05-20 22:43:31 +08:00
|
|
|
vi.mocked(agentService.chatWithAgent).mockResolvedValue({
|
2026-05-19 16:05:09 +08:00
|
|
|
text: 'AI response',
|
|
|
|
|
usage: { inputTokens: 1, outputTokens: 1, model: 'gpt-4' },
|
|
|
|
|
sessionId: 'mock-session-id',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await aiChatActivity({
|
|
|
|
|
teamId: 't1',
|
|
|
|
|
agentId: 'b1',
|
|
|
|
|
message: 'Hi',
|
|
|
|
|
imageUrls: [],
|
|
|
|
|
projectId: 'p1',
|
|
|
|
|
folderId: 'f1',
|
2026-05-21 22:51:40 +08:00
|
|
|
sessionId: 'mock-session-id',
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
2026-05-20 22:43:31 +08:00
|
|
|
expect(agentService.chatWithAgent).toHaveBeenCalledWith(
|
2026-05-19 16:05:09 +08:00
|
|
|
't1',
|
|
|
|
|
'b1',
|
|
|
|
|
'Hi',
|
|
|
|
|
[],
|
|
|
|
|
'',
|
2026-05-21 22:51:40 +08:00
|
|
|
'mock-session-id',
|
2026-05-19 16:05:09 +08:00
|
|
|
undefined,
|
|
|
|
|
)
|
|
|
|
|
})
|
2026-05-21 22:51:40 +08:00
|
|
|
|
|
|
|
|
it('should initialize a new session with chronological context (Rule 1 and Rule 2) and prefix username', async () => {
|
|
|
|
|
const team = await prisma.team.create({ data: { name: 't1' } })
|
|
|
|
|
const user1 = await prisma.user.create({
|
|
|
|
|
data: { name: 'User One', email: 'user1@example.com' },
|
|
|
|
|
})
|
2026-05-22 09:25:27 +08:00
|
|
|
const agentUser = await prisma.user.create({
|
|
|
|
|
data: { id: 'agent-user-id', name: 'Smart Agent', email: 'agent@example.com', type: 'agent' },
|
|
|
|
|
})
|
2026-05-21 22:51:40 +08:00
|
|
|
const project = await prisma.project.create({
|
|
|
|
|
data: { name: 'p1', teamId: team.id },
|
|
|
|
|
})
|
|
|
|
|
const file = await prisma.asset.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'test-file',
|
|
|
|
|
type: 'file',
|
|
|
|
|
projectId: project.id,
|
|
|
|
|
creatorId: user1.id,
|
|
|
|
|
status: 'uploaded',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-22 09:25:27 +08:00
|
|
|
// Create existing comment 1 (User)
|
2026-05-21 22:51:40 +08:00
|
|
|
const comment1 = await prisma.assetComment.create({
|
|
|
|
|
data: {
|
|
|
|
|
assetId: file.id,
|
|
|
|
|
creatorId: user1.id,
|
|
|
|
|
message: 'First comment message',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-22 09:25:27 +08:00
|
|
|
// Create existing comment 1.5 (Agent)
|
|
|
|
|
const commentAgent = await prisma.assetComment.create({
|
|
|
|
|
data: {
|
|
|
|
|
assetId: file.id,
|
|
|
|
|
creatorId: agentUser.id,
|
|
|
|
|
message: 'I am helping',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-21 22:51:40 +08:00
|
|
|
// Create triggering comment 2 (Rule 1: no reply, mentions agent)
|
|
|
|
|
const comment2 = await prisma.assetComment.create({
|
|
|
|
|
data: {
|
|
|
|
|
assetId: file.id,
|
|
|
|
|
creatorId: user1.id,
|
|
|
|
|
message: 'Hello <@agent-id>',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
vi.mocked(agentService.chatWithAgent).mockResolvedValue({
|
|
|
|
|
text: 'AI response',
|
|
|
|
|
usage: { inputTokens: 1, outputTokens: 1, model: 'gpt-4' },
|
|
|
|
|
sessionId: 'generated-session-id',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await aiChatActivity({
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
agentId: 'agent-id',
|
|
|
|
|
message: 'Hello <@agent-id>',
|
|
|
|
|
imageUrls: [],
|
|
|
|
|
projectId: project.id,
|
|
|
|
|
folderId: file.id,
|
|
|
|
|
userCommentId: comment2.id,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Verify chatWithAgent was called with a brand new sessionId (a ULID)
|
|
|
|
|
expect(agentService.chatWithAgent).toHaveBeenCalled()
|
|
|
|
|
const callArgs = vi.mocked(agentService.chatWithAgent).mock.calls[0]
|
|
|
|
|
const createdSessionId = callArgs[5]
|
|
|
|
|
expect(createdSessionId).toBeDefined()
|
|
|
|
|
expect(createdSessionId).not.toBe('generated-session-id')
|
|
|
|
|
|
2026-05-22 09:25:27 +08:00
|
|
|
// Verify session entries were populated with comment1 and commentAgent as context
|
2026-05-21 22:51:40 +08:00
|
|
|
const entries = await prisma.agentSessionEntry.findMany({
|
|
|
|
|
where: { sessionId: createdSessionId },
|
|
|
|
|
orderBy: { id: 'asc' },
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-22 09:25:27 +08:00
|
|
|
expect(entries.length).toBe(2)
|
|
|
|
|
|
|
|
|
|
// Verify User Comment Entry
|
|
|
|
|
const entryData1 = entries[0].entry as unknown as SessionTreeEntry
|
|
|
|
|
expect(entryData1.id).toBeDefined()
|
|
|
|
|
expect(entryData1.id).not.toBe(comment1.id)
|
|
|
|
|
if (entryData1.type === 'message') {
|
|
|
|
|
const msg = entryData1.message as { role: 'user'; content: { type: 'text'; text: string }[] }
|
2026-05-22 09:20:49 +08:00
|
|
|
expect(msg.content[0].text).toBe('[User One]: First comment message')
|
|
|
|
|
expect(msg.role).toBe('user')
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error('Expected entry to be a message')
|
2026-05-22 09:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify Agent Comment Entry
|
|
|
|
|
const entryData2 = entries[1].entry as unknown as SessionTreeEntry
|
|
|
|
|
expect(entryData2.id).toBeDefined()
|
|
|
|
|
expect(entryData2.id).not.toBe(commentAgent.id)
|
|
|
|
|
if (entryData2.type === 'message') {
|
|
|
|
|
const msg = entryData2.message as { role: 'user'; content: { type: 'text'; text: string }[] }
|
|
|
|
|
expect(msg.content[0].text).toBe('[Agent Message][Smart Agent]: I am helping')
|
|
|
|
|
expect(msg.role).toBe('user')
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error('Expected entry to be a message')
|
2026-05-22 09:20:49 +08:00
|
|
|
}
|
2026-05-21 22:51:40 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should support deleteCommentActivity', async () => {
|
|
|
|
|
const team = await prisma.team.create({ data: { name: 't1' } })
|
|
|
|
|
const user1 = await prisma.user.create({
|
|
|
|
|
data: { name: 'User One', email: 'user1-delete@example.com' },
|
|
|
|
|
})
|
|
|
|
|
const project = await prisma.project.create({
|
|
|
|
|
data: { name: 'p1', teamId: team.id },
|
|
|
|
|
})
|
|
|
|
|
const file = await prisma.asset.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'test-file',
|
|
|
|
|
type: 'file',
|
|
|
|
|
projectId: project.id,
|
|
|
|
|
creatorId: user1.id,
|
|
|
|
|
status: 'uploaded',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
const comment = await prisma.assetComment.create({
|
|
|
|
|
data: {
|
|
|
|
|
assetId: file.id,
|
|
|
|
|
creatorId: user1.id,
|
|
|
|
|
message: 'To be deleted',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const { deleteCommentActivity } = await import('./agent')
|
|
|
|
|
await deleteCommentActivity(comment.id)
|
|
|
|
|
|
|
|
|
|
const found = await prisma.assetComment.findUnique({
|
|
|
|
|
where: { id: comment.id },
|
|
|
|
|
})
|
|
|
|
|
expect(found).toBeNull()
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|