2026-05-27 23:40:14 +08:00
|
|
|
import { describe, expect, test } from 'vitest'
|
2026-05-19 16:05:09 +08:00
|
|
|
import { AgentService } from './agent'
|
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-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
describe('AgentService', () => {
|
|
|
|
|
setupTestDbHooks()
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
async function setupTestData(db: any) {
|
|
|
|
|
const team = await db.team.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'Test Team',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const provider = await db.provider.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'Google',
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
config: {
|
|
|
|
|
api: 'openai-completions',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const model = await db.model.create({
|
|
|
|
|
data: {
|
|
|
|
|
modelId: 'gemini-pro',
|
|
|
|
|
name: 'Gemini Pro',
|
|
|
|
|
providerId: provider.id,
|
|
|
|
|
config: {},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const skill = await db.skill.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'Test Skill',
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
assetId: 'asset1',
|
|
|
|
|
hash: 'hash1',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const user = await db.user.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'Bot 1',
|
|
|
|
|
email: 'bot1@shumai.ai',
|
|
|
|
|
type: 'agent',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const agent = await db.agent.create({
|
|
|
|
|
data: {
|
|
|
|
|
id: user.id,
|
2026-05-21 20:41:37 +08:00
|
|
|
teamId: team.id,
|
2026-05-19 16:05:09 +08:00
|
|
|
type: 'chat',
|
|
|
|
|
providerId: provider.id,
|
|
|
|
|
modelId: model.id,
|
|
|
|
|
soul: 'Test soul',
|
|
|
|
|
config: {
|
|
|
|
|
provider: provider.id,
|
|
|
|
|
model: model.id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await db.teamMember.create({
|
|
|
|
|
data: {
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
role: 'owner',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return { team, provider, model, skill, agent }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('create, update, delete agents', () => {
|
|
|
|
|
test('Create Agent with Provider and Model', async () => {
|
|
|
|
|
const db = prisma
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
const { team, provider, model } = await setupTestData(db)
|
|
|
|
|
|
|
|
|
|
const agent = await svc.createAgent({
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
name: 'New Agent',
|
|
|
|
|
type: 'chat',
|
|
|
|
|
enabled: true,
|
|
|
|
|
providerId: provider.id,
|
|
|
|
|
modelId: model.id,
|
|
|
|
|
thinkingLevel: 'medium',
|
|
|
|
|
systemPrompt: 'You are helpful',
|
|
|
|
|
soul: 'I am a soul',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(agent?.id).toBeDefined()
|
|
|
|
|
expect(agent?.user.name).toBe('New Agent')
|
|
|
|
|
expect(agent?.providerId).toBe(provider.id)
|
|
|
|
|
expect(agent?.modelId).toBe(model.id)
|
|
|
|
|
expect(agent?.soul).toBe('I am a soul')
|
|
|
|
|
|
|
|
|
|
const teamMember = await db.teamMember.findFirst({
|
|
|
|
|
where: { userId: agent?.id, teamId: team.id },
|
|
|
|
|
})
|
|
|
|
|
expect(teamMember).toBeDefined()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('Create Agent with Skills', async () => {
|
|
|
|
|
const db = prisma
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
const { team, provider, model, skill } = await setupTestData(db)
|
|
|
|
|
|
|
|
|
|
const agent = await svc.createAgent({
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
name: 'Skill Agent',
|
|
|
|
|
type: 'chat',
|
|
|
|
|
enabled: true,
|
|
|
|
|
providerId: provider.id,
|
|
|
|
|
modelId: model.id,
|
|
|
|
|
thinkingLevel: 'medium',
|
|
|
|
|
skills: [skill.id],
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(agent?.skills?.length).toBe(1)
|
|
|
|
|
expect(agent?.skills?.[0].skillId).toBe(skill.id)
|
|
|
|
|
})
|
|
|
|
|
|
2026-07-16 15:12:26 +08:00
|
|
|
test('Create Agent with Denied Tools', async () => {
|
|
|
|
|
const db = prisma
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
const { team, provider, model } = await setupTestData(db)
|
|
|
|
|
|
|
|
|
|
const agent = await svc.createAgent({
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
name: 'Denied Tools Agent',
|
|
|
|
|
type: 'chat',
|
|
|
|
|
enabled: true,
|
|
|
|
|
providerId: provider.id,
|
|
|
|
|
modelId: model.id,
|
|
|
|
|
thinkingLevel: 'medium',
|
|
|
|
|
deniedTools: ['bash', 'screenshot'],
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(agent?.id).toBeDefined()
|
|
|
|
|
const config = agent?.config as unknown as PrismaJson.AgentConfig
|
|
|
|
|
expect(config.deniedTools).toEqual(['bash', 'screenshot'])
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-19 16:05:09 +08:00
|
|
|
test('Create Agent validation fails if model mismatch', async () => {
|
|
|
|
|
const db = prisma
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
const { team, model } = await setupTestData(db)
|
|
|
|
|
|
|
|
|
|
const otherProvider = await db.provider.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'Other',
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
config: {
|
|
|
|
|
api: 'openai-completions',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
svc.createAgent({
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
name: 'Fail Agent',
|
|
|
|
|
type: 'chat',
|
|
|
|
|
enabled: true,
|
|
|
|
|
providerId: otherProvider.id,
|
|
|
|
|
modelId: model.id,
|
|
|
|
|
thinkingLevel: 'medium',
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toThrow('model does not belong to provider')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('Update Agent and Skills', async () => {
|
|
|
|
|
const db = prisma
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
const { agent, skill } = await setupTestData(db)
|
|
|
|
|
|
|
|
|
|
const updated = await svc.updateAgent({
|
|
|
|
|
agentId: agent.id,
|
|
|
|
|
name: 'Updated Agent',
|
|
|
|
|
type: 'autofill',
|
|
|
|
|
enabled: true,
|
|
|
|
|
providerId: agent.providerId!,
|
|
|
|
|
modelId: agent.modelId!,
|
|
|
|
|
thinkingLevel: 'high',
|
|
|
|
|
soul: 'New soul',
|
|
|
|
|
skills: [], // Disable all skills
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(updated.user.name).toBe('Updated Agent')
|
|
|
|
|
expect(updated.type).toBe('autofill')
|
|
|
|
|
expect(updated.soul).toBe('New soul')
|
|
|
|
|
expect(updated.skills?.length).toBe(0)
|
|
|
|
|
|
|
|
|
|
const reUpdated = await svc.updateAgent({
|
|
|
|
|
agentId: agent.id,
|
|
|
|
|
name: 'Updated Agent',
|
|
|
|
|
type: 'autofill',
|
|
|
|
|
enabled: true,
|
|
|
|
|
providerId: agent.providerId!,
|
|
|
|
|
modelId: agent.modelId!,
|
|
|
|
|
thinkingLevel: 'high',
|
|
|
|
|
soul: 'New soul',
|
|
|
|
|
skills: [skill.id], // Re-enable one
|
2026-07-16 15:12:26 +08:00
|
|
|
deniedTools: ['list_assets'],
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
expect(reUpdated.skills?.length).toBe(1)
|
2026-07-16 15:12:26 +08:00
|
|
|
const config = reUpdated.config as unknown as PrismaJson.AgentConfig
|
|
|
|
|
expect(config.deniedTools).toEqual(['list_assets'])
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
2026-06-03 23:24:21 +08:00
|
|
|
test('Create Agent with presigned URL extracts S3 key', async () => {
|
|
|
|
|
const db = prisma
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
const { team, provider, model } = await setupTestData(db)
|
|
|
|
|
|
2026-06-04 11:19:52 +08:00
|
|
|
const avatarPresigned =
|
|
|
|
|
'https://shumai.s3.amazonaws.com/files/01JK23456789ABCDEF01234567?AWSAccessKeyId=foo'
|
2026-06-03 23:24:21 +08:00
|
|
|
const agent = await svc.createAgent({
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
name: 'Agent with URL',
|
|
|
|
|
type: 'chat',
|
|
|
|
|
enabled: true,
|
|
|
|
|
avatar: avatarPresigned,
|
|
|
|
|
providerId: provider.id,
|
|
|
|
|
modelId: model.id,
|
2026-06-03 23:30:33 +08:00
|
|
|
thinkingLevel: 'medium',
|
2026-06-03 23:24:21 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(agent?.id).toBeDefined()
|
|
|
|
|
expect(agent?.user.image).toBe('files/01JK23456789ABCDEF01234567')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('Update Agent with presigned URL extracts S3 key', async () => {
|
|
|
|
|
const db = prisma
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
const { agent } = await setupTestData(db)
|
|
|
|
|
|
2026-06-04 11:19:52 +08:00
|
|
|
const avatarPresigned =
|
|
|
|
|
'https://shumai.s3.amazonaws.com/files/01JK23456789ABCDEF01234567?AWSAccessKeyId=foo'
|
2026-06-03 23:24:21 +08:00
|
|
|
const updated = await svc.updateAgent({
|
|
|
|
|
agentId: agent.id,
|
|
|
|
|
name: 'Updated with URL',
|
|
|
|
|
type: 'chat',
|
|
|
|
|
enabled: true,
|
|
|
|
|
avatar: avatarPresigned,
|
|
|
|
|
providerId: agent.providerId!,
|
|
|
|
|
modelId: agent.modelId!,
|
2026-06-03 23:30:33 +08:00
|
|
|
thinkingLevel: 'medium',
|
2026-06-03 23:24:21 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(updated.user.image).toBe('files/01JK23456789ABCDEF01234567')
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-19 16:05:09 +08:00
|
|
|
test('Delete Agent', async () => {
|
|
|
|
|
const db = prisma
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
const { agent } = await setupTestData(db)
|
|
|
|
|
|
|
|
|
|
await svc.deleteAgent({ agentId: agent.id })
|
|
|
|
|
|
|
|
|
|
const deletedAgent = await db.agent.findUnique({
|
|
|
|
|
where: { id: agent.id },
|
|
|
|
|
})
|
|
|
|
|
expect(deletedAgent).toBeNull()
|
|
|
|
|
|
|
|
|
|
const deletedUser = await db.user.findUnique({
|
|
|
|
|
where: { id: agent.id },
|
|
|
|
|
})
|
|
|
|
|
expect(deletedUser).toBeNull()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('List Agents', async () => {
|
|
|
|
|
const db = prisma
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
const { team } = await setupTestData(db)
|
|
|
|
|
|
|
|
|
|
const agents = await svc.listAgents({ teamId: team.id })
|
|
|
|
|
expect(agents.length).toBe(1)
|
|
|
|
|
expect(agents[0].user.name).toBe('Bot 1')
|
|
|
|
|
expect(agents[0].provider).toBeDefined()
|
|
|
|
|
expect(agents[0].modelRef).toBeDefined()
|
|
|
|
|
})
|
2026-07-28 11:21:31 +08:00
|
|
|
|
|
|
|
|
test('getSessionEntries follows parentId across shared sessions', async () => {
|
|
|
|
|
const db = prisma
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
const { agent } = await setupTestData(db)
|
|
|
|
|
|
|
|
|
|
const mainSession = await db.agentSession.create({
|
|
|
|
|
data: {
|
|
|
|
|
agentId: agent.id,
|
|
|
|
|
type: 'comment',
|
|
|
|
|
cwd: '/tmp',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const threadSession = await db.agentSession.create({
|
|
|
|
|
data: {
|
|
|
|
|
agentId: agent.id,
|
|
|
|
|
type: 'comment',
|
|
|
|
|
cwd: '/tmp',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const entry1 = await db.agentSessionEntry.create({
|
|
|
|
|
data: {
|
|
|
|
|
id: '01ENTRY0000000000000000001',
|
|
|
|
|
sessionId: mainSession.id,
|
|
|
|
|
type: 'message',
|
|
|
|
|
parentId: null,
|
|
|
|
|
data: { text: 'Main Entry 1' },
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const entry2 = await db.agentSessionEntry.create({
|
|
|
|
|
data: {
|
|
|
|
|
id: '01ENTRY0000000000000000002',
|
|
|
|
|
sessionId: threadSession.id,
|
|
|
|
|
type: 'message',
|
|
|
|
|
parentId: entry1.id,
|
|
|
|
|
data: { text: 'Thread Entry 2' },
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await db.agentSession.update({
|
|
|
|
|
where: { id: threadSession.id },
|
|
|
|
|
data: { leafId: entry2.id },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const entries = await svc.getSessionEntries({ sessionId: threadSession.id })
|
|
|
|
|
expect(entries).toHaveLength(2)
|
|
|
|
|
expect(entries[0].id).toBe(entry1.id)
|
|
|
|
|
expect(entries[1].id).toBe(entry2.id)
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
2026-07-29 10:10:07 +08:00
|
|
|
|
|
|
|
|
describe('listSessions', () => {
|
|
|
|
|
test('lists sessions for team and excludes pending id', async () => {
|
|
|
|
|
const db = prisma
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
const { team, agent } = await setupTestData(db)
|
|
|
|
|
|
|
|
|
|
const user = await db.user.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'Regular User',
|
|
|
|
|
email: 'user@shumai.ai',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const session1 = await db.agentSession.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'Session One',
|
|
|
|
|
agentId: agent.id,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
type: 'chat',
|
|
|
|
|
cwd: '/tmp',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const session2 = await db.agentSession.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: 'Session Two',
|
|
|
|
|
agentId: agent.id,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
type: 'comment',
|
|
|
|
|
cwd: '/tmp',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Pending session - should be excluded!
|
|
|
|
|
await db.agentSession.create({
|
|
|
|
|
data: {
|
|
|
|
|
id: 'pending',
|
|
|
|
|
name: 'Pending Session',
|
|
|
|
|
agentId: agent.id,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
type: 'chat',
|
|
|
|
|
cwd: '/tmp',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const res = await svc.listSessions(team.id, { first: 10 })
|
|
|
|
|
|
|
|
|
|
expect(res.data).toHaveLength(2)
|
|
|
|
|
expect(res.data.map((s) => s.id)).not.toContain('pending')
|
|
|
|
|
const sessionIds = res.data.map((s) => s.id)
|
|
|
|
|
expect(sessionIds).toContain(session1.id)
|
|
|
|
|
expect(sessionIds).toContain(session2.id)
|
|
|
|
|
|
|
|
|
|
const found = res.data.find((s) => s.id === session1.id)
|
|
|
|
|
expect(found?.name).toBe('Session One')
|
|
|
|
|
expect(found?.type).toBe('chat')
|
|
|
|
|
expect(found?.creator?.name).toBe('Regular User')
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-08-01 20:44:34 +08:00
|
|
|
|
|
|
|
|
describe('getAgent', () => {
|
|
|
|
|
test('returns agent when it exists', async () => {
|
|
|
|
|
const db = prisma
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
const { agent } = await setupTestData(db)
|
|
|
|
|
|
|
|
|
|
const result = await svc.getAgent({ agentId: agent.id })
|
|
|
|
|
|
|
|
|
|
expect(result).not.toBeNull()
|
|
|
|
|
expect(result?.id).toBe(agent.id)
|
|
|
|
|
expect(result?.type).toBe('chat')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('returns null for non-existent ID', async () => {
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
|
|
|
|
|
const result = await svc.getAgent({ agentId: 'nonexistent-id' })
|
|
|
|
|
|
|
|
|
|
expect(result).toBeNull()
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-08-16 11:16:14 +08:00
|
|
|
|
|
|
|
|
describe('agent permissions', () => {
|
|
|
|
|
test('createAgent with custom permission and updateAgentPermission', async () => {
|
|
|
|
|
const db = prisma
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
const { team, provider, model } = await setupTestData(db)
|
|
|
|
|
|
|
|
|
|
const agent = await svc.createAgent({
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
name: 'Owner Only Agent',
|
|
|
|
|
type: 'chat',
|
|
|
|
|
enabled: true,
|
|
|
|
|
thinkingLevel: 'off',
|
|
|
|
|
permission: 'owner',
|
|
|
|
|
providerId: provider.id,
|
|
|
|
|
modelId: model.id,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(agent?.permission).toBe('owner')
|
|
|
|
|
|
|
|
|
|
const updated = await svc.updateAgentPermission(agent!.id, 'editor')
|
|
|
|
|
expect(updated.permission).toBe('editor')
|
|
|
|
|
|
|
|
|
|
const updatedViaUpdateAgent = await svc.updateAgent({
|
|
|
|
|
agentId: agent!.id,
|
|
|
|
|
name: 'Owner Only Agent Updated',
|
|
|
|
|
type: 'chat',
|
|
|
|
|
enabled: true,
|
|
|
|
|
thinkingLevel: 'off',
|
|
|
|
|
permission: 'reviewer',
|
|
|
|
|
providerId: provider.id,
|
|
|
|
|
modelId: model.id,
|
|
|
|
|
})
|
|
|
|
|
expect(updatedViaUpdateAgent.permission).toBe('reviewer')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('listAgents filters chat agents by requester role when userId is provided', async () => {
|
|
|
|
|
const db = prisma
|
|
|
|
|
const svc = new AgentService()
|
|
|
|
|
const { team, provider, model } = await setupTestData(db)
|
|
|
|
|
|
|
|
|
|
// Create 3 chat agents with different permissions
|
|
|
|
|
const reviewerAgent = await svc.createAgent({
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
name: 'All Users Agent',
|
|
|
|
|
type: 'chat',
|
|
|
|
|
enabled: true,
|
|
|
|
|
thinkingLevel: 'off',
|
|
|
|
|
permission: 'reviewer',
|
|
|
|
|
providerId: provider.id,
|
|
|
|
|
modelId: model.id,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const editorAgent = await svc.createAgent({
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
name: 'Editor Agent',
|
|
|
|
|
type: 'chat',
|
|
|
|
|
enabled: true,
|
|
|
|
|
thinkingLevel: 'off',
|
|
|
|
|
permission: 'editor',
|
|
|
|
|
providerId: provider.id,
|
|
|
|
|
modelId: model.id,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const ownerAgent = await svc.createAgent({
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
name: 'Owner Agent',
|
|
|
|
|
type: 'chat',
|
|
|
|
|
enabled: true,
|
|
|
|
|
thinkingLevel: 'off',
|
|
|
|
|
permission: 'owner',
|
|
|
|
|
providerId: provider.id,
|
|
|
|
|
modelId: model.id,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create 3 test users with different roles in the team
|
|
|
|
|
const reviewerUser = await db.user.create({
|
|
|
|
|
data: { name: 'Reviewer User', email: 'rev@shumai.ai' },
|
|
|
|
|
})
|
|
|
|
|
await db.teamMember.create({
|
|
|
|
|
data: { teamId: team.id, userId: reviewerUser.id, role: 'reviewer' },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const editorUser = await db.user.create({
|
|
|
|
|
data: { name: 'Editor User', email: 'ed@shumai.ai' },
|
|
|
|
|
})
|
|
|
|
|
await db.teamMember.create({
|
|
|
|
|
data: { teamId: team.id, userId: editorUser.id, role: 'editor' },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const ownerUser = await db.user.create({
|
|
|
|
|
data: { name: 'Owner User', email: 'own@shumai.ai' },
|
|
|
|
|
})
|
|
|
|
|
await db.teamMember.create({
|
|
|
|
|
data: { teamId: team.id, userId: ownerUser.id, role: 'owner' },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Reviewer should only see reviewerAgent
|
|
|
|
|
const reviewerList = await svc.listAgents({ teamId: team.id, userId: reviewerUser.id })
|
|
|
|
|
const reviewerAgentIds = reviewerList.map((a) => a.id)
|
|
|
|
|
expect(reviewerAgentIds).toContain(reviewerAgent!.id)
|
|
|
|
|
expect(reviewerAgentIds).not.toContain(editorAgent!.id)
|
|
|
|
|
expect(reviewerAgentIds).not.toContain(ownerAgent!.id)
|
|
|
|
|
|
|
|
|
|
// Editor should see reviewerAgent and editorAgent
|
|
|
|
|
const editorList = await svc.listAgents({ teamId: team.id, userId: editorUser.id })
|
|
|
|
|
const editorAgentIds = editorList.map((a) => a.id)
|
|
|
|
|
expect(editorAgentIds).toContain(reviewerAgent!.id)
|
|
|
|
|
expect(editorAgentIds).toContain(editorAgent!.id)
|
|
|
|
|
expect(editorAgentIds).not.toContain(ownerAgent!.id)
|
|
|
|
|
|
|
|
|
|
// Owner should see all agents
|
|
|
|
|
const ownerList = await svc.listAgents({ teamId: team.id, userId: ownerUser.id })
|
|
|
|
|
const ownerAgentIds = ownerList.map((a) => a.id)
|
|
|
|
|
expect(ownerAgentIds).toContain(reviewerAgent!.id)
|
|
|
|
|
expect(ownerAgentIds).toContain(editorAgent!.id)
|
|
|
|
|
expect(ownerAgentIds).toContain(ownerAgent!.id)
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|