Files
shumai/packages/e2e/workflow/agent-chat.test.ts
T
Yiling-J 35a0e82284 refactor(agent)!: migrate to Pi Durable and embed the agent queue (#525)
* refactor(agent)!: migrate sessions to Pi Durable

Replace patched AgentHarness storage with per-session SQLite and PostgreSQL commit mirroring. Migrate legacy history automatically and run agent queues with the main server.

BREAKING CHANGE: Remove standalone agent workers and return native Pi entry records from chat history APIs.

* fix(agent): constrain session log dialog scrolling

Keep the header visible and wrap long output while log entries and expanded records scroll within the dialog. Cover desktop and narrow viewports through the dashboard Sessions flow.

* fix(agent): hide entry IDs from session log card headers

Retain internal entry IDs in expanded raw records for debugging. Verify that card headers omit the IDs while raw JSON preserves them.

* test(e2e): handle browser wheel limits in session log checks

Verify that a normal wheel event moves the viewport before explicitly scrolling to the final record. Keep the dialog bounds, fixed header, wrapping, and expanded-record assertions.

* fix(agent): select model providers without inheriting unrelated auth

* fix(webui): prevent resize feedback when toggling agent panel

* test(agent): reproduce concurrent comment session races

* fix(agent): serialize comment sessions and deduplicate history

* docs(changelog): consolidate Pi Durable changes into one entry
2026-11-11 16:05:18 +08:00

465 lines
12 KiB
TypeScript

import type { AssistantMessage } from '@earendil-works/pi-ai'
import { initAgentWorkflows } from '@shumai/agent'
import { s3Service } from '@shumai/core/src/s3/s3'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { TaskQueueAgent, workflowService } from '@shumai/workflow-core'
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
import { mockModelPrompt } from './model-mock'
describe('Workflow E2E - agentChat', () => {
setupTestDbHooks()
beforeAll(async () => {
process.env.S3_BUCKET = 'shumai-e2e-test-bucket-chat'
process.env.GEMINI_API_KEY = 'dummy-key'
initAgentWorkflows()
mockModelPrompt(
async () =>
({
content: [{ type: 'text', text: 'E2E Agent Chat Response Success' }],
usage: { input: 10, output: 20 },
stopReason: 'stop',
}) as unknown as AssistantMessage,
)
await workflowService.startWorkers(TaskQueueAgent)
})
afterAll(async () => {
await workflowService.shutdownWorkers()
workflowService.close()
vi.restoreAllMocks()
try {
console.log('Cleaning up local E2E storage files...')
await s3Service.deletePrefix('shumai-e2e-test-bucket-chat', '')
} catch (err) {
console.error('Failed to clean up E2E storage folder:', err)
}
})
it('should run agentChat workflow with userCommentId and reply comment successfully', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Chat Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Chat Project', teamId: team.id },
})
const agentUser = await prisma.user.create({
data: {
name: 'E2E Chat Agent User',
email: 'e2e-chat-agent@shumai.ai',
type: 'agent',
},
})
await prisma.teamMember.create({
data: {
teamId: team.id,
userId: agentUser.id,
role: 'editor',
},
})
const provider = await prisma.provider.create({
data: {
name: 'google',
teamId: team.id,
config: { api: 'google-generative-ai', apiKey: 'dummy-google-api-key' },
},
})
const model = await prisma.model.create({
data: {
modelId: 'gemini',
name: 'Gemini',
providerId: provider.id,
config: {
input: ['text', 'image'],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 8192,
maxTokens: 2048,
reasoning: false,
},
},
})
await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'chat',
enabled: true,
providerId: provider.id,
modelId: model.id,
config: {
provider: 'google',
model: 'gemini',
},
},
})
const asset = await prisma.asset.create({
data: {
name: 'chat-asset.png',
type: 'file',
status: 'uploaded',
mediaType: 'image/png',
projectId: project.id,
},
})
// Create a regular user who authors the comment
const regularUser = await prisma.user.create({
data: {
name: 'Regular User',
email: 'user@example.com',
type: 'human',
},
})
await prisma.teamMember.create({
data: {
teamId: team.id,
userId: regularUser.id,
role: 'owner',
},
})
// Create AgentSession first
await prisma.agentSession.create({
data: {
id: 'chat-session-123',
agentId: agentUser.id,
userId: regularUser.id,
cwd: process.cwd(),
assetId: asset.id,
},
})
// Create the User Comment
const userComment = await prisma.assetComment.create({
data: {
assetId: asset.id,
message: 'Hello E2E Agent!',
creatorId: regularUser.id,
sessionId: 'chat-session-123',
},
})
// 2. Create Workflow Task
const task = await prisma.workflowTask.create({
data: {
type: 'chat',
status: 'pending',
assetId: asset.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
agent: {
agentId: agentUser.id,
sessionId: 'chat-session-123',
userCommentId: userComment.id,
userId: regularUser.id,
},
},
},
})
// 3. Wait for workflow to complete
console.log(
`Submitted E2E Chat Workflow Task with Comment. ID: ${task.id}. Awaiting completion...`,
)
const completedTask = await workflowService.executeWait(task, 45000)
// 4. Verification
expect(completedTask.status).toBe('completed')
// Verify reply comment in DB
const comments = await prisma.assetComment.findMany({
where: {
assetId: asset.id,
replyToId: userComment.id,
},
})
expect(comments.length).toBeGreaterThan(0)
expect(comments[0].message).toBe('E2E Agent Chat Response Success')
expect(comments[0].creatorId).toBe(agentUser.id)
}, 50000)
it('should run agentChat workflow with direct prompt successfully', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: { name: 'E2E Direct Chat Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Direct Chat Project', teamId: team.id },
})
const agentUser = await prisma.user.create({
data: {
name: 'E2E Direct Chat Agent User',
email: 'e2e-dir-chat-agent@shumai.ai',
type: 'agent',
},
})
await prisma.teamMember.create({
data: {
teamId: team.id,
userId: agentUser.id,
role: 'editor',
},
})
const provider = await prisma.provider.create({
data: {
name: 'google',
teamId: team.id,
config: { api: 'google-generative-ai', apiKey: 'dummy-google-api-key' },
},
})
const model = await prisma.model.create({
data: {
modelId: 'gemini',
name: 'Gemini',
providerId: provider.id,
config: {
input: ['text'],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 8192,
maxTokens: 2048,
reasoning: false,
},
},
})
await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'chat',
enabled: true,
providerId: provider.id,
modelId: model.id,
config: {
provider: 'google',
model: 'gemini',
},
},
})
const asset = await prisma.asset.create({
data: {
name: 'direct-chat-asset.png',
type: 'file',
status: 'uploaded',
mediaType: 'image/png',
projectId: project.id,
},
})
const regularUser = await prisma.user.create({
data: {
name: 'Regular User 2',
email: 'user2@example.com',
type: 'human',
},
})
await prisma.teamMember.create({
data: {
teamId: team.id,
userId: regularUser.id,
role: 'owner',
},
})
// Create AgentSession first
await prisma.agentSession.create({
data: {
id: 'direct-session-456',
agentId: agentUser.id,
userId: regularUser.id,
cwd: process.cwd(),
assetId: asset.id,
},
})
// 2. Create Workflow Task (without userCommentId, but with prompt/sessionId)
const task = await prisma.workflowTask.create({
data: {
type: 'chat',
status: 'pending',
assetId: asset.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
agent: {
agentId: agentUser.id,
sessionId: 'direct-session-456',
prompt: 'Direct test message',
userId: regularUser.id,
},
},
},
})
// 3. Wait for workflow to complete
console.log(`Submitted E2E Direct Chat Workflow Task. ID: ${task.id}. Awaiting completion...`)
const completedTask = await workflowService.executeWait(task, 45000)
// 4. Verification
expect(completedTask.status).toBe('completed')
expect((completedTask.output as unknown as { sessionId: string })?.sessionId).toBe(
'direct-session-456',
)
}, 50000)
it('should run agentChat workflow with version stack asset successfully', async () => {
const team = await prisma.team.create({
data: { name: 'E2E Version Stack Team' },
})
const project = await prisma.project.create({
data: { name: 'E2E Version Stack Project', teamId: team.id },
})
const agentUser = await prisma.user.create({
data: {
name: 'E2E VS Agent User',
email: 'e2e-vs-agent@shumai.ai',
type: 'agent',
},
})
await prisma.teamMember.create({
data: {
teamId: team.id,
userId: agentUser.id,
role: 'editor',
},
})
const provider = await prisma.provider.create({
data: {
name: 'google',
teamId: team.id,
config: { api: 'google-generative-ai', apiKey: 'dummy-google-api-key' },
},
})
const model = await prisma.model.create({
data: {
modelId: 'gemini',
name: 'Gemini',
providerId: provider.id,
config: {
input: ['text', 'image'],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 8192,
maxTokens: 2048,
reasoning: false,
},
},
})
await prisma.agent.create({
data: {
id: agentUser.id,
teamId: team.id,
type: 'chat',
enabled: true,
providerId: provider.id,
modelId: model.id,
config: {
provider: 'google',
model: 'gemini',
},
},
})
const versionStack = await prisma.asset.create({
data: {
name: 'VersionStack Container',
type: 'version_stack',
status: 'uploaded',
projectId: project.id,
},
})
await prisma.asset.create({
data: {
name: 'version-1.png',
type: 'file',
status: 'uploaded',
mediaType: 'image/png',
projectId: project.id,
parentId: versionStack.id,
sortIndex: 'a0',
},
})
const regularUser = await prisma.user.create({
data: {
name: 'Regular User 3',
email: 'user3@example.com',
type: 'human',
},
})
await prisma.teamMember.create({
data: {
teamId: team.id,
userId: regularUser.id,
role: 'owner',
},
})
await prisma.agentSession.create({
data: {
id: 'vs-session-789',
agentId: agentUser.id,
userId: regularUser.id,
cwd: process.cwd(),
assetId: versionStack.id,
},
})
const task = await prisma.workflowTask.create({
data: {
type: 'chat',
status: 'pending',
assetId: versionStack.id,
projectId: project.id,
teamId: team.id,
payload: {
projectId: project.id,
agent: {
agentId: agentUser.id,
sessionId: 'vs-session-789',
prompt: 'Explain this version stack file',
userId: regularUser.id,
},
},
},
})
const completedTask = await workflowService.executeWait(task, 45000)
expect(completedTask.status).toBe('completed')
expect((completedTask.output as unknown as { sessionId: string })?.sessionId).toBe(
'vs-session-789',
)
}, 50000)
})