from pathlib import Path from openpyxl import Workbook, load_workbook from dx_productivity.trial_task_center.bom_plan import BOM_HEADERS, read_bom_records from tools.convert_cbb_bom import convert def test_cbb_conversion_preserves_completed_rows_and_isolates_conflicts(tmp_path): source = tmp_path / 'source.xlsx' workbook = Workbook() sheet = workbook.active sheet.title = '结果' headers = ['文件名', 'QTY', 'REFDES', 'VALUE'] + [f'column{i}' for i in range(5, 27)] for index, name in {10: '旧料号', 13: '更换为新料号', 22: '更换物料料号', 24: '是否更换完毕'}.items(): headers[index - 1] = name sheet.append(headers) for name, proposal, history, completed in ( ('Board', '4300-NEW-A100', '4300-NEW-A100', 'Y'), ('Board', '431R-NEW-A100', '4300-OTHER-A100', ''), ('Missing', '4300-NEW-A100', '', ''), ('Board', '不可更换', '4300-NEW-A100', ''), ): row = [''] * 26 for index, value in {0: name, 1: 1, 2: f'R{sheet.max_row}', 3: '100nF/NC', 9: '4300-OLD-A100', 12: proposal, 21: history, 23: completed}.items(): row[index] = value sheet.append(row) workbook.save(source) project = tmp_path / 'Board.prj' report = convert(source, (project,), tmp_path / 'converted') full = read_bom_records(Path(report['full_file'])) ready = read_bom_records(Path(report['mapped_file'])) assert len(full) == 3 and len(ready) == 2 assert ready[0].enabled # Completion markers are preserved, not used to remove rules. assert not ready[1].enabled assert ready[1].new == '431R-NEW-A100' assert all(not r.custom and r.sync_nc for r in full) assert report['source_rows'] == 4 and report['conflict_rows'] == 1 workbook = load_workbook(report['mapped_file']) assert tuple(cell.value for cell in workbook.active[1]) == BOM_HEADERS assert 'Y' in workbook.active['D2'].comment.text workbook.close() def test_cbb_conversion_does_not_guess_a_path_for_duplicate_project_names(tmp_path): from tools.convert_cbb_bom import project_index index = project_index((tmp_path / 'left' / 'Board.prj', tmp_path / 'right' / 'Board.prj')) assert len(index['board']) == 2