from pathlib import Path import pytest from dx_productivity.trial_task_center.quick_run_protocol import _encode_vbs_field as enc def case(tmp_path): row = dict(task_id='task', job_id='job', project_path=str(tmp_path / 'board.prj'), schematic_name='MAIN', sheet_number='1', old_part='OLD', new_part='NEW', value_mode='custom', custom_value='10nF', sync_nc='1', target_cell='CELL', target_scope='targets', target_selectors='u1\x1fR1\x1f10nF') snapshot = 'C\tu1\tR1\t0\t0\t0\t1\tNO_PINS\r\nA\tu1\tValue\t10nF\t10nF\t0\t0\t0\t0\t10\t0\t1\r\n' (tmp_path / 'snapshot-job.tsv').write_bytes(snapshot.encode()) header = ['V', '2', 'task', 'job', 'saved_ok', row['project_path'], 'MAIN', '1', 'OLD', 'NEW', 'custom', '10nF', '1', 'CELL', row['target_selectors'], snapshot] records = [header, ['C', 'u1', 'R1', 'NEW', '10nF', '1', 'CELL'], ['E', 'u1', '0', 'Value', '10nF', '10nF', 'unknown']] raw = tmp_path / 'value-readback-job.tsv' raw.write_text('\n'.join('\t'.join(map(enc, record)) for record in records) + '\n', encoding='utf-8') (tmp_path / 'status.tsv').write_text('job_id\tstate\tcode\tmessage\njob\tsaved_ok\tbatch_closed\tverified\n') return row, raw def test_persists_actual_values_and_rejects_later_terminal_job(tmp_path): from dx_productivity.trial_task_center.value_readback import capture_value_readback, load_value_readback row, _ = case(tmp_path) outcome = ('saved_ok', 'batch_closed', 'verified') details = capture_value_readback(tmp_path, row, outcome) assert details['value_readback'][0]['actual_value'] == '10nF' assert details['value_readback'][0]['uid'] == 'u1' assert details['attribute_observations'][0]['layer'] == 'unknown' assert load_value_readback(tmp_path, row, outcome) == details with (tmp_path / 'status.tsv').open('a') as stream: stream.write('other\tsaved_ok\tbatch_closed\tverified\n') assert load_value_readback(tmp_path, row, outcome)['value_readback_status'] == 'rejected' @pytest.mark.parametrize('actual_value,expected_state', [ ('10nF/NC', 'verified'), ('10nF', 'rejected'), ('10nF/NC/NC', 'rejected')]) def test_readback_does_not_accept_lost_leading_nc_marker(tmp_path, actual_value, expected_state): from dx_productivity.trial_task_center.value_readback import capture_value_readback row, raw = case(tmp_path) row['target_selectors'] = 'u1\x1fR1\x1fNC/402K/1%' snapshot = 'C\tu1\tR1\t0\t0\t0\t1\tNO_PINS\r\nA\tu1\tValue\tNC/402K/1%\t\t0\t0\t0\t0\t10\t0\t1\r\n' (tmp_path / 'snapshot-job.tsv').write_bytes(snapshot.encode()) header = ['V', '2', 'task', 'job', 'saved_ok', row['project_path'], 'MAIN', '1', 'OLD', 'NEW', 'custom', '10nF', '1', 'CELL', row['target_selectors'], snapshot] records = [header, ['C', 'u1', 'R1', 'NEW', actual_value, '1', 'CELL']] raw.write_text('\n'.join('\t'.join(map(enc, record)) for record in records) + '\n', encoding='utf-8') result = capture_value_readback(tmp_path, row, ('saved_ok', 'batch_closed', 'verified')) assert result['value_readback_status'] == expected_state @pytest.mark.parametrize('fault', ['uid', 'policy', 'snapshot', 'foreign_job', 'version', 'cell', 'pn', 'value']) def test_readback_rejects_foreign_or_stale_report(tmp_path, fault): from dx_productivity.trial_task_center.value_readback import capture_value_readback row, raw = case(tmp_path) text = raw.read_text(encoding='utf-8') if fault == 'uid': text = text.replace('C\tu1\t', 'C\tu2\t') elif fault == 'policy': row['custom_value'] = '100nF' elif fault == 'snapshot': (tmp_path / 'snapshot-job.tsv').write_text('changed') elif fault == 'foreign_job': text = text.replace('V\t2\ttask\tjob\t', 'V\t2\ttask\tforeign\t') elif fault == 'version': text = text.replace('V\t2\t', 'V\t1\t') elif fault == 'cell': text = text.replace('C\tu1\tR1\tNEW\t10nF\t1\tCELL', 'C\tu1\tR1\tNEW\t10nF\t1\tWRONG') elif fault == 'value': text = text.replace('C\tu1\tR1\tNEW\t10nF\t', 'C\tu1\tR1\tNEW\twrong\t') else: text = text.replace('C\tu1\tR1\tNEW\t', 'C\tu1\tR1\tOLD\t') raw.write_text(text, encoding='utf-8') assert capture_value_readback(tmp_path, row, ('saved_ok', 'batch_closed', 'verified'))['value_readback_status'] == 'rejected' def test_equal_raw_attribute_text_keeps_every_occurrence(tmp_path): from dx_productivity.trial_task_center.value_readback import capture_value_readback row, raw = case(tmp_path) with raw.open('a', encoding='utf-8') as stream: stream.write('E\tu1\t1\tValue\t10nF\t10nF\tunknown\n') details = capture_value_readback(tmp_path, row, ('saved_ok', 'batch_closed', 'verified')) assert len(details['attribute_observations']) == 2 assert details['attribute_ambiguities'] == [{'uid': 'u1', 'name': 'value', 'ordinals': [0, 1]}] def test_visible_report_uses_actual_values_and_explicitly_marks_missing_value(): import json from dx_productivity.trial_task_center.ui.messages import replacement_result_rows event = dict(operation='quick_run_executed', message='Replacements: R1 OLD -> NEW', details={ 'state': 'saved_ok', 'value_readback': {'value_readback_status': 'verified', 'schematic_name': 'MAIN', 'sheet_number': '1', 'value_readback_job_id': 'job', 'value_readback': [ dict(uid='u1', refdes='R1', old_part_number='OLD', actual_part_number='NEW', old_value='planned old', actual_value='actual readback', actual_value_present=True)]}}) rows = replacement_result_rows((json.dumps(event),)) assert rows[0][4:6] == ('planned old', 'actual readback') assert len(rows[0]) == 7 event['details']['value_readback'] = {'value_readback_status': 'rejected'} rows = replacement_result_rows((json.dumps(event),)) assert rows[0][5] == '回读证据被拒绝' assert 'actual readback' not in str(rows)