from pathlib import Path import pytest from openpyxl import load_workbook from dx_productivity.trial_task_center.errors import TaskCenterError from dx_productivity.trial_task_center.spreadsheet_plan import export_target_plan, import_target_plan, read_target_plan from dx_productivity.trial_task_center.store import TaskStore from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow def prepared(tmp_path): store = TaskStore(tmp_path / 'tasks.db') task = store.create_task('board') path = tmp_path / 'board.prj' store.sync_projects(task.task_id, tmp_path, (path,)) base = ComponentTarget(path, 'MAIN', '1', 'u1', 'R1', 'OLD', 'V0') store.record_target_inventory(task.task_id, (base,)) return store, task def test_excel_roundtrip_edits_value_and_nc(tmp_path): store, task = prepared(tmp_path) xlsx = tmp_path / 'plan.xlsx' export_target_plan(store, task.task_id, xlsx) workbook = load_workbook(xlsx) sheet = workbook.active sheet['I2'] = True sheet['J2'] = 'NEW' sheet['K2'] = True sheet['L2'] = 'VALUE' sheet['M2'] = False workbook.save(xlsx) imported = import_target_plan(store, task.task_id, xlsx) assert imported == store.list_target_plan(task.task_id) assert (imported[0].enabled, imported[0].new_part_number, imported[0].custom_value, imported[0].sync_nc) == (True, 'NEW', 'VALUE', False) def test_excel_preview_rejects_equal_rule_without_mutating_saved_plan(tmp_path): store, task = prepared(tmp_path) xlsx = tmp_path / 'plan.xlsx' export_target_plan(store, task.task_id, xlsx) workbook = load_workbook(xlsx) sheet = workbook.active sheet['I2'] = True sheet['J2'] = 'OLD' workbook.save(xlsx) with pytest.raises(TaskCenterError): read_target_plan(store, task.task_id, xlsx) assert store.list_target_plan(task.task_id)[0].enabled is False @pytest.mark.parametrize('change', ['forged', 'duplicate']) def test_excel_rejects_forged_or_duplicate_identity_atomically(tmp_path, change): store, task = prepared(tmp_path) xlsx = tmp_path / 'plan.xlsx' export_target_plan(store, task.task_id, xlsx) workbook = load_workbook(xlsx) sheet = workbook.active if change == 'forged': sheet['E2'] = 'forged-uid' else: sheet.append([cell.value for cell in sheet[2]]) workbook.save(xlsx) with pytest.raises(TaskCenterError): import_target_plan(store, task.task_id, xlsx) assert store.list_target_plan(task.task_id)[0].enabled is False