刘光辉
9 小时以前 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { createPinia, setActivePinia } from 'pinia';
import { beforeEach, describe, expect, it } from 'vitest';
 
import { useFillerStore } from '../../src/stores/record-filler';
 
describe('filler store', () => {
  beforeEach(() => setActivePinia(createPinia()));
 
  it('keeps failed drafts and commits successful fields', () => {
    const store = useFillerStore();
    store.fields = [
      { alias: 'A', tag: 'eln.field.a', internalId: 'a', type: 'text', options: [], currentValue: '', documentValue: '', draftValue: 'new' },
      { alias: 'B', tag: 'eln.field.b', internalId: 'b', type: 'text', options: [], currentValue: '', documentValue: '', draftValue: 'bad' },
    ];
    store.applyResults([
      { tag: 'eln.field.a', ok: true },
      { tag: 'eln.field.b', ok: false, error: '失败' },
    ]);
    expect(store.fields[0]?.documentValue).toBe('new');
    expect(store.fields[1]?.documentValue).toBe('');
    expect(store.fieldErrors).toEqual({ 'eln.field.b': '失败' });
  });
 
  it('synchronizes primary changes to matching reference controls but rejects direct reference edits', () => {
    const store = useFillerStore();
    store.fields = [
      { alias: '主字段', tag: 'eln.field.name', internalId: 'a', type: 'text', options: [], currentValue: '', documentValue: '', draftValue: '' },
      { alias: '引用字段', tag: 'eln.ref.name.instance_1', internalId: 'b', type: 'text', options: [], currentValue: '', documentValue: '', draftValue: '' },
    ];
 
    store.setDraft('eln.field.name', '新值');
    store.setDraft('eln.ref.name.instance_1', '越权值');
 
    expect(store.fields.map((field) => field.draftValue)).toEqual(['新值', '新值']);
  });
 
  it('rejects ordinary drafts for readonly cross-template primary fields', () => {
    const store = useFillerStore();
    store.fields = [{
      alias: '源检验结果', currentValue: '合格', documentValue: '合格', draftValue: '合格',
      internalId: 'cross-1', options: [], readOnly: true, tag: 'eln.field.source_result',
      type: 'text', valueSource: 'CROSS_TEMPLATE',
    }];
 
    store.setDraft('eln.field.source_result', '人工覆盖');
 
    expect(store.fields[0]?.draftValue).toBe('合格');
    expect(store.formulaResultsStale).toBe(false);
  });
 
  it('synchronizes dotted primary field codes to matching references', () => {
    const store = useFillerStore();
    store.fields = [
      { alias: '检验结果', tag: 'eln.field.inspection.result', internalId: 'a', type: 'text', options: [], currentValue: '', documentValue: '', draftValue: '' },
      { alias: '检验结果引用', tag: 'eln.ref.inspection.result.instance_1', internalId: 'b', type: 'text', options: [], currentValue: '', documentValue: '', draftValue: '' },
    ];
 
    store.setDraft('eln.field.inspection.result', '合格');
 
    expect(store.fields.map((field) => field.draftValue)).toEqual(['合格', '合格']);
  });
 
  it('tracks three-way document conflicts and resolves the primary field with its references', () => {
    const store = useFillerStore();
    store.fields = [
      { alias: '主字段', tag: 'eln.field.name', internalId: 'a', type: 'text', options: [], currentValue: '原值', documentValue: '原值', draftValue: '侧栏值' },
      { alias: '引用字段', tag: 'eln.ref.name.instance_1', internalId: 'b', type: 'text', options: [], currentValue: '原值', documentValue: '原值', draftValue: '侧栏值' },
    ];
 
    store.syncDocumentField('eln.field.name', '文档值');
 
    expect(store.conflicts['eln.field.name']).toEqual({
      baseValue: '原值', documentValue: '文档值', draftValue: '侧栏值',
    });
    store.resolveConflict('eln.field.name', 'document');
    expect(store.conflicts).toEqual({});
    expect(store.fields.map((field) => field.draftValue)).toEqual(['文档值', '文档值']);
    expect(store.fields.map((field) => field.documentValue)).toEqual(['文档值', '原值']);
  });
 
  it('automatically absorbs uncontested document edits and can keep a conflicting draft', () => {
    const store = useFillerStore();
    store.fields = [
      { alias: '结果', tag: 'eln.field.result', internalId: 'a', type: 'text', options: [], currentValue: '原值', documentValue: '原值', draftValue: '原值' },
    ];
 
    store.syncDocumentField('eln.field.result', '文档第一次修改');
    expect(store.fields[0]).toMatchObject({ documentValue: '文档第一次修改', draftValue: '文档第一次修改' });
    store.setDraft('eln.field.result', '侧栏草稿');
    store.syncDocumentField('eln.field.result', '文档第二次修改');
    store.resolveConflict('eln.field.result', 'draft');
 
    expect(store.conflicts).toEqual({});
    expect(store.fields[0]).toMatchObject({ documentValue: '文档第二次修改', draftValue: '侧栏草稿' });
  });
 
  it('enforces checkbox groups within one step and allows the whole group to be empty', () => {
    const store = useFillerStore();
    store.setStepStructure({
      configuration: {
        singleStep: false,
        steps: [
          { id: 'step-1', name: '检验', order: 1 },
          { id: 'step-2', name: '复核', order: 2 },
        ],
      },
      controlSteps: {
        'eln.field.failed': 'step-1',
        'eln.field.other': 'step-1',
        'eln.field.passed': 'step-1',
        'eln.field.review': 'step-2',
        'eln.field.standalone': 'step-1',
      },
      repeatGroupSteps: {},
    });
    store.fields = [
      checkbox('eln.field.passed', false, 'inspection_result'),
      checkbox('eln.field.failed', true, 'inspection_result'),
      checkbox('eln.field.other', true, 'other_result'),
      checkbox('eln.field.review', true, 'inspection_result'),
      checkbox('eln.field.standalone', true),
    ];
 
    store.setDraft('eln.field.passed', true);
    expect(store.fields.map((field) => field.draftValue)).toEqual([true, false, true, true, true]);
    expect(store.formulaResultsStale).toBe(true);
 
    store.selectStep('step-2');
    store.selectStep('step-1');
    store.setDraft('eln.field.passed', false);
    expect(store.fields.slice(0, 2).map((field) => field.draftValue)).toEqual([false, false]);
  });
 
  it('isolates repeated checkbox groups by runtime row', () => {
    const store = useFillerStore();
    store.fields = [
      checkbox('eln.repeat.results.row-1.passed', false, 'row_result'),
      checkbox('eln.repeat.results.row-1.failed', true, 'row_result'),
      checkbox('eln.repeat.results.row-2.passed', false, 'row_result'),
      checkbox('eln.repeat.results.row-2.failed', true, 'row_result'),
    ];
 
    store.setDraft('eln.repeat.results.row-1.passed', true);
 
    expect(store.fields.map((field) => field.draftValue)).toEqual([true, false, false, true]);
  });
 
  it('rejects the N+1 selection without changing existing values', () => {
    const store = useFillerStore();
    store.fields = [
      checkbox('eln.field.passed', true, 'inspection_result', 0, 2),
      checkbox('eln.field.failed', true, 'inspection_result', 0, 2),
      checkbox('eln.field.reviewed', false, 'inspection_result', 0, 2),
    ];
 
    store.setDraft('eln.field.reviewed', true);
 
    expect(store.fields.map((field) => field.draftValue)).toEqual([true, true, false]);
    expect(store.checkboxGroupNotice).toBe('选择组“inspection_result”最多选择 2 项');
    expect(store.checkboxGroupError).toBe('');
  });
 
  it('allows deselection below minimum but blocks apply with a group error', () => {
    const store = useFillerStore();
    store.fields = [
      checkbox('eln.field.passed', true, 'inspection_result', 1, null),
      checkbox('eln.field.failed', false, 'inspection_result', 1, null),
    ];
 
    store.setDraft('eln.field.passed', false);
 
    expect(store.fields.map((field) => field.draftValue)).toEqual([false, false]);
    expect(store.checkboxGroupError).toBe('选择组“inspection_result”至少选择 1 项');
  });
 
  it('refreshes checkbox group errors after accepting a conflicting document value', () => {
    const store = useFillerStore();
    store.fields = [
      checkbox('eln.field.passed', true, 'inspection_result'),
      checkbox('eln.field.failed', false, 'inspection_result'),
    ];
    store.conflicts['eln.field.failed'] = {
      baseValue: false,
      documentValue: true,
      draftValue: false,
    };
 
    store.resolveConflict('eln.field.failed', 'document');
 
    expect(store.checkboxGroupError).toBe('选择组“inspection_result”最多选择 1 项');
  });
});
 
function checkbox(
  tag: string,
  value: boolean,
  checkboxGroupCode?: string,
  checkboxGroupMinSelected = 0,
  checkboxGroupMaxSelected: null | number = 1,
) {
  return {
    alias: tag,
    ...(checkboxGroupCode
      ? { checkboxGroupCode, checkboxGroupMaxSelected, checkboxGroupMinSelected }
      : {}),
    currentValue: value,
    documentValue: value,
    draftValue: value,
    internalId: tag,
    options: [],
    tag,
    type: 'checkbox' as const,
  };
}