import { describe, expect, it } from 'vitest' import { LINKED_ACTION_LABELS, dutyConditionNote, linkedCapabilitySummary } from '../src/permissionExplanations' import { describeSource } from '../src/rdmManagement' const LINKED = ['project.settings', 'project.members.manage', 'project.permissions.manage', 'document.duties.assign'] describe('linked capability summary', () => { it('returns empty when the server omits the field (older backend)', () => { expect(linkedCapabilitySummary(undefined)).toBe('') }) it('explains held project-create eligibility and the self-created-project scope', () => { expect(linkedCapabilitySummary({ projectCreate: true, source: 'grant', actions: LINKED })).toBe( '已具备创建项目资格(来源:管理员授予);创建本人项目后自动具备:管理项目设置、管理项目成员、管理项目权限、分配文档职责;' + '这些资格仅对本人创建的项目生效,其他项目按项目授权判断') }) it('maps source variants and falls back to the raw source', () => { expect(linkedCapabilitySummary({ projectCreate: true, source: 'role', actions: [] })).toBe('已具备创建项目资格(来源:角色)') expect(linkedCapabilitySummary({ projectCreate: true, source: 'admin', actions: [] })).toBe('已具备创建项目资格(来源:系统管理员)') expect(linkedCapabilitySummary({ projectCreate: true, source: 'admin-grant', actions: [] })).toContain('来源:管理员授予') expect(linkedCapabilitySummary({ projectCreate: true, source: 'future', actions: [] })).toContain('来源:future') expect(linkedCapabilitySummary({ projectCreate: true, actions: [] })).toBe('已具备创建项目资格') }) it('appends explicit conflicts with Chinese labels', () => { const text = linkedCapabilitySummary({ projectCreate: true, source: 'role', actions: ['project.settings'], conflicts: ['project.members.manage', 'document.duties.assign'], }) expect(text).toContain('创建本人项目后自动具备:管理项目设置') expect(text.endsWith('存在冲突:管理项目成员、分配文档职责 被明确禁止')).toBe(true) }) it('states missing eligibility without listing linked actions', () => { expect(linkedCapabilitySummary({ projectCreate: false, actions: [] })).toBe('未具备创建项目资格') expect(linkedCapabilitySummary({ projectCreate: false, actions: ['project.settings'], conflicts: ['project.settings'] })).toBe('未具备创建项目资格') }) it('labels every linked action and keeps unknown ids verbatim', () => { expect(LINKED.map(id => LINKED_ACTION_LABELS[id])).toEqual(['管理项目设置', '管理项目成员', '管理项目权限', '分配文档职责']) expect(linkedCapabilitySummary({ projectCreate: true, actions: ['x.y'] })).toContain('自动具备:x.y') }) }) describe('duty condition note', () => { it('explains the duty and state conditions for document author/review/approve', () => { expect(dutyConditionNote('document.author')).toBe('仍需被分配该文档的编制职责,且文档状态符合要求') expect(dutyConditionNote('document.review')).toBe('仍需被分配该文档的审核职责,且文档状态符合要求') expect(dutyConditionNote('document.approve')).toBe('仍需被分配该文档的批准职责,且文档状态符合要求') }) it('is empty for every other action', () => { for (const id of ['document.read', 'project.settings', 'project.create', '']) expect(dutyConditionNote(id)).toBe('') }) }) describe('describeSource future sources', () => { it('maps creator and role sources, keeping the raw fallback', () => { expect(describeSource('creator')).toBe('本人项目创建者自动具备(仅本人创建的项目)') expect(describeSource('role')).toBe('职位角色') expect(describeSource('weird')).toBe('weird') }) })