刘光辉
12 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { describe, expect, it } from 'vitest';
 
import { createFieldDefinition } from '../../src/domain/validation';
import type { FieldDefinitionInput } from '../../src/domain/types';
 
function createInput(overrides: Partial<FieldDefinitionInput> = {}): FieldDefinitionInput {
  return {
    mode: 'single',
    fieldCode: 'sample_id',
    alias: '样品编号',
    groupCode: '',
    rowId: '',
    typeConfig: { controlType: 'text' },
    ...overrides,
  };
}
 
describe('field definition validation', () => {
  it('rejects an empty normalized field code first', () => {
    expect(createFieldDefinition(createInput({ fieldCode: ' 中文 ' }))).toEqual({
      ok: false,
      field: 'field-code',
      message: '请输入有效的字段编码',
    });
  });
 
  it('rejects an empty trimmed alias', () => {
    expect(createFieldDefinition(createInput({ alias: '  ' }))).toEqual({
      ok: false,
      field: 'field-alias',
      message: '请输入字段名称',
    });
  });
 
  it('rejects a repeat field without a normalized group code before checking its row ID', () => {
    expect(
      createFieldDefinition(
        createInput({
          mode: 'repeat',
          groupCode: '中文',
          rowId: '',
        }),
      ),
    ).toEqual({
      ok: false,
      field: 'group-code',
      message: '请输入有效的重复组编码',
    });
  });
 
  it('rejects a repeat field without a normalized row ID', () => {
    expect(
      createFieldDefinition(
        createInput({
          mode: 'repeat',
          groupCode: 'results',
          rowId: '中文',
        }),
      ),
    ).toEqual({
      ok: false,
      field: 'row-id',
      message: '请生成或输入行 ID',
    });
  });
 
  it('passes type validation errors through unchanged', () => {
    expect(
      createFieldDefinition(
        createInput({
          typeConfig: { controlType: 'text', defaultValue: 'line 1\nline 2' },
        }),
      ),
    ).toEqual({
      ok: false,
      field: 'text-default',
      message: '单行文本默认值不能换行',
    });
  });
 
  it('normalizes a single field and falls back to its alias for an empty placeholder', () => {
    expect(
      createFieldDefinition(
        createInput({
          fieldCode: ' Sample 编号 ',
          alias: '  样品编号  ',
          groupCode: ' Ignored Group ',
          rowId: ' Ignored Row ',
          typeConfig: {
            controlType: 'text',
            placeholder: '  ',
            defaultValue: 'ABC',
          },
        }),
      ),
    ).toEqual({
      ok: true,
      value: {
        mode: 'single',
        controlType: 'text',
        fieldCode: 'sample',
        alias: '样品编号',
        groupCode: 'ignored_group',
        rowId: 'ignored_row',
        tag: 'eln.field.sample',
        color: { R: 23, G: 109, B: 92 },
        placeholder: '样品编号',
        defaultValue: 'ABC',
        defaultChecked: false,
      },
    });
  });
 
  it('creates a normalized repeat field with the repeat color', () => {
    expect(
      createFieldDefinition(
        createInput({
          mode: 'repeat',
          fieldCode: ' Result ',
          alias: ' 结果 ',
          groupCode: ' Test Results ',
          rowId: ' R_01 ',
          typeConfig: { controlType: 'checkbox', defaultChecked: true },
        }),
      ),
    ).toEqual({
      ok: true,
      value: {
        mode: 'repeat',
        controlType: 'checkbox',
        fieldCode: 'result',
        alias: '结果',
        groupCode: 'test_results',
        rowId: 'r_01',
        tag: 'eln.repeat.test_results.r_01.result',
        color: { R: 171, G: 126, B: 24 },
        placeholder: '结果',
        defaultValue: '',
        defaultChecked: true,
      },
    });
  });
 
  it('preserves a valid dropdown configuration as a discriminated field definition', () => {
    const result = createFieldDefinition(
      createInput({
        alias: '状态',
        typeConfig: {
          controlType: 'dropdown',
          placeholder: ' 请选择 ',
          options: [
            { display: ' 合格 ', value: ' pass ' },
            { display: '不合格', value: 'fail' },
          ],
          defaultOptionValue: ' pass ',
        },
      }),
    );
 
    expect(result).toEqual({
      ok: true,
      value: {
        mode: 'single',
        controlType: 'dropdown',
        fieldCode: 'sample_id',
        alias: '状态',
        groupCode: '',
        rowId: '',
        tag: 'eln.field.sample_id',
        color: { R: 23, G: 109, B: 92 },
        placeholder: '请选择',
        options: [
          { display: '合格', value: 'pass' },
          { display: '不合格', value: 'fail' },
        ],
        selectedIndex: 0,
      },
    });
 
    if (result.ok && result.value.controlType === 'dropdown') {
      expect(result.value.selectedIndex).toBe(0);
    }
  });
});