刘光辉
4 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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');
  });
});