mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 04:29:29 +00:00
* feat(agent): add permission controls for chat agents * refactor(agent): extract shared agent permission helpers Consolidate the duplicated role-hierarchy logic into packages/core/src/agent/permissions.ts: - getRoleLevel(): numeric level for a team role (fail closed at 0) - getAgentRequiredLevel(): required level for an agent permission (defaults to reviewer/1) - getAllowedAgentRoles(): inclusive role set for filtering The typed Record over TeamMemberRole makes the mapping exhaustive, so adding a new role fails compilation until all sites are updated. Used by ChatService.startOrContinueChat, AssetService comment mentions, getAgentChatContextActivity, AgentService.listAgents, and TeamService.getTeamMembers, replacing four inline copies of the hierarchy and the allowed-roles conditionals. Also fix the API permission-denial test to mock HTTPException(403) and assert a 403 response instead of a generic Error/500, matching real authzService behavior. * test(agent): fix flaky comment DAG tests by spacing ULID id generation ULID ids have 1ms timestamp granularity; back-to-back comment creates frequently land in the same millisecond, in which case Prisma ULIDs sort by their random suffix instead of creation order. The DAG spine in initializeAgentSessionActivity chains top-level comments by id, so a shuffled order produces paths that omit preceding comments, making the path-to-root assertions flaky (~80% of full-file runs locally). Add a 2ms sleep between consecutive comment creates in the affected tests, guaranteeing strictly increasing ULID timestamps. A 2ms delay is sufficient because the next create always fires at least 2ms after the previous ULID was generated. Production is unaffected: real comments arrive seconds apart via separate requests.
44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { getRoleLevel, getAgentRequiredLevel, getAllowedAgentRoles } from './permissions'
|
|
|
|
describe('agent permission helpers', () => {
|
|
describe('getRoleLevel', () => {
|
|
it('maps each role to its level', () => {
|
|
expect(getRoleLevel('reviewer')).toBe(1)
|
|
expect(getRoleLevel('editor')).toBe(2)
|
|
expect(getRoleLevel('owner')).toBe(3)
|
|
})
|
|
|
|
it('resolves missing roles to 0 (fail closed)', () => {
|
|
expect(getRoleLevel(null)).toBe(0)
|
|
expect(getRoleLevel(undefined)).toBe(0)
|
|
})
|
|
})
|
|
|
|
describe('getAgentRequiredLevel', () => {
|
|
it('maps each permission to its required level', () => {
|
|
expect(getAgentRequiredLevel('reviewer')).toBe(1)
|
|
expect(getAgentRequiredLevel('editor')).toBe(2)
|
|
expect(getAgentRequiredLevel('owner')).toBe(3)
|
|
})
|
|
|
|
it('defaults missing permissions to reviewer level (1)', () => {
|
|
expect(getAgentRequiredLevel(null)).toBe(1)
|
|
expect(getAgentRequiredLevel(undefined)).toBe(1)
|
|
})
|
|
})
|
|
|
|
describe('getAllowedAgentRoles', () => {
|
|
it('returns inclusive role sets per hierarchy', () => {
|
|
expect(getAllowedAgentRoles('owner')).toEqual(['reviewer', 'editor', 'owner'])
|
|
expect(getAllowedAgentRoles('editor')).toEqual(['reviewer', 'editor'])
|
|
expect(getAllowedAgentRoles('reviewer')).toEqual(['reviewer'])
|
|
})
|
|
|
|
it('defaults missing roles to reviewer only', () => {
|
|
expect(getAllowedAgentRoles(null)).toEqual(['reviewer'])
|
|
expect(getAllowedAgentRoles(undefined)).toEqual(['reviewer'])
|
|
})
|
|
})
|
|
})
|