刘光辉
7 小时以前 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
import { describe, expect, it } from 'vitest';
import {
  addRepeatPatternField,
  formAnnotationTag,
  removeRepeatPatternField,
  repeatGroups,
  repeatInsertSpec,
  repeatPatternUsesField,
  repeatPatternValidationError,
} from '../../src/domain/annotation';
 
describe('动态字段与子表组合', () => {
  const fields = [
    { key: 'departments.department', label: '分发部门-部门', formId: 'common', table: 'departments', column: 'department' },
    { key: 'departments.copies', label: '分发部门-份数', formId: 'common', table: 'departments', column: 'copies' },
  ];
  it('uses the common tag returned by the server', () => {
    expect(formAnnotationTag({ key: 'newField', label: '新字段', tag: 'dms.qianpiye.form.newField' })).toBe('dms.qianpiye.form.newField');
  });
  it('preserves readable labels while binding columns in the row template', () => {
    const group = repeatGroups(fields)[0]!;
    const spec = repeatInsertSpec(group, '{{部门}}({{份数}})');
    expect(spec.tag).toBe('dms.qianpiye.rows.common.departments');
    expect(spec.placeholder).toBe('{{部门}}({{份数}})');
    expect(JSON.parse(spec.alias).fields).toEqual({ 部门: 'department', 份数: 'copies' });
  });
  it('rejects missing placeholders and unknown columns', () => {
    const group = repeatGroups(fields)[0]!;
    expect(() => repeatInsertSpec(group, '固定文字')).toThrow();
    expect(() => repeatInsertSpec(group, '{{不存在}}')).toThrow();
  });
  it('adds only the fields selected by the user and prevents duplicates', () => {
    const fileType = { key: 'departments.fileType', label: '分发部门-文件类别', formId: 'common', table: 'departments', column: 'fileType' };
    const group = repeatGroups([...fields, fileType])[0]!;
    const [department, copies] = group.fields;
    let pattern = addRepeatPatternField('', department!);
    pattern = addRepeatPatternField(pattern, copies!);
    pattern = addRepeatPatternField(pattern, department!);
    expect(pattern).toBe('{{部门}} {{份数}}');
    expect(repeatPatternUsesField(pattern, department!)).toBe(true);
    expect(pattern).not.toContain('文件类别');
  });
  it('normalizes bilingual field labels to a single-line row format', () => {
    for (const separator of ['\n', '\\n', '\r\n', '\\r\\n']) {
      const bilingualFields = [
        { ...fields[0]!, label: `分发部门 Distribution Department-部门${separator}Department` },
        { ...fields[1]!, label: `分发部门 Distribution Department-份数${separator}Copies` },
      ];
      const group = repeatGroups(bilingualFields)[0]!;
      let pattern = addRepeatPatternField('', group.fields[0]!);
      pattern = addRepeatPatternField(pattern, group.fields[1]!);
      expect(pattern).toBe('{{部门 Department}} {{份数 Copies}}');
      expect(repeatPatternValidationError(group, pattern)).toBe('');
      const spec = repeatInsertSpec(group, pattern);
      expect(spec.placeholder).toBe(pattern);
      expect(JSON.parse(spec.alias).fields).toEqual({ '部门 Department': 'department', '份数 Copies': 'copies' });
    }
  });
  it('removes selected fields without disturbing custom text', () => {
    const [department, copies] = fields;
    expect(removeRepeatPatternField('{{部门}} {{份数}}', department!)).toBe('{{份数}}');
    expect(removeRepeatPatternField('{{部门}} ## {{份数}}', copies!)).toBe('{{部门}} ##');
  });
  it('validates the editable row format', () => {
    const group = repeatGroups(fields)[0]!;
    expect(repeatPatternValidationError(group, '{{部门}}({{份数}})')).toBe('');
    expect(repeatPatternValidationError(group, '{{文件类别}}')).toContain('未找到子表字段');
  });
});