import { describe, expect, it } from 'vitest';
|
|
import {
|
actionLabel,
|
actionsForResource,
|
canExcludeRead,
|
normalizePrincipalId,
|
permissionDateToIso,
|
permissionResourceDisplayName,
|
principalDisplayName,
|
principalDisplayTitle,
|
principalSearchValue,
|
scopesForResource,
|
} from '../permissionRules';
|
|
describe('dMS permission form rules', () => {
|
it('hides extensions for logical documents only', () => {
|
const base = { createdAt: '', id: 'resource-1', name: 'fallback.docx', sourceId: '', sourceType: '' };
|
expect(permissionResourceDisplayName({ ...base, documentName: '玻思韬-TMS调研报告.docx', resourceType: 'DOCUMENT' })).toBe('玻思韬-TMS调研报告');
|
expect(permissionResourceDisplayName({ ...base, resourceType: 'DOCUMENT' })).toBe('fallback');
|
expect(permissionResourceDisplayName({ ...base, fileName: '玻思韬-TMS调研报告.docx', resourceType: 'FILE_VERSION' })).toBe('玻思韬-TMS调研报告.docx');
|
expect(permissionResourceDisplayName({ ...base, name: '目录.归档', resourceType: 'FOLDER' })).toBe('目录.归档');
|
});
|
|
it('allows CREATE only on folders', () => {
|
expect(actionsForResource('FOLDER').map((item) => item.value)).toContain('CREATE');
|
expect(actionsForResource('DOCUMENT').map((item) => item.value)).not.toContain('CREATE');
|
expect(actionsForResource('FILE_VERSION').map((item) => item.value)).not.toContain('CREATE');
|
});
|
|
it('offers PRINT and DOWNLOAD permissions for every resource type', () => {
|
for (const resourceType of ['FOLDER', 'DOCUMENT', 'FILE_VERSION'] as const) {
|
expect(actionsForResource(resourceType).map((item) => item.value)).toEqual(expect.arrayContaining(['PRINT', 'DOWNLOAD']));
|
}
|
expect(actionLabel('PRINT')).toBe('打印');
|
expect(actionLabel('DOWNLOAD')).toBe('下载');
|
});
|
|
it('allows SUBTREE only on folders', () => {
|
expect(scopesForResource('FOLDER').map((item) => item.value)).toContain('SUBTREE');
|
expect(scopesForResource('DOCUMENT').map((item) => item.value)).toEqual(['SELF']);
|
});
|
|
it('allows READ exclusions only on documents and file versions', () => {
|
expect(canExcludeRead('FOLDER')).toBe(false);
|
expect(canExcludeRead('DOCUMENT')).toBe(true);
|
expect(canExcludeRead('FILE_VERSION')).toBe(true);
|
});
|
|
it('removes selector suffixes before sending principal ids', () => {
|
expect(normalizePrincipalId('org-1--progenyOrg')).toBe('org-1');
|
expect(normalizePrincipalId('pos-1--pos')).toBe('pos-1');
|
expect(normalizePrincipalId('user-1')).toBe('user-1');
|
});
|
|
it('converts JNPF DateRange timestamps to the same ISO instant', () => {
|
const instant = new Date('2026-09-23T02:30:00.000Z').getTime();
|
expect(permissionDateToIso(instant)).toBe('2026-09-23T02:30:00.000Z');
|
expect(permissionDateToIso()).toBeUndefined();
|
});
|
|
it('prefers a user display name while retaining the id in the tooltip', () => {
|
expect(principalDisplayName('张三(zhangsan)', 'user-1')).toBe('张三(zhangsan)');
|
expect(principalDisplayTitle('张三(zhangsan)', 'user-1')).toContain('user-1');
|
expect(principalDisplayTitle('张三(zhangsan)', 'user-1')).toContain('张三(zhangsan)');
|
});
|
|
it('falls back to the id and keeps both name and id searchable', () => {
|
expect(principalDisplayName(undefined, 'user-1')).toBe('user-1');
|
expect(principalSearchValue('张三(zhangsan)', 'user-1')).toContain('张三(zhangsan)');
|
expect(principalSearchValue('张三(zhangsan)', 'user-1')).toContain('user-1');
|
});
|
});
|