fix: address PR21 BOM routing and supervision review
Core regression / Python core and release configuration (push) Successful in 19s
Core regression / JavaScript syntax (push) Successful in 4s
Core regression / Python core and release configuration (pull_request) Successful in 17s
Core regression / JavaScript syntax (pull_request) Successful in 3s

This commit is contained in:
阿白叔 committed 2026-11-05 12:42:20 +08:00
1 parent 8b5509d64f
commit 94a873f929
19 files changed
+619 -67

No files matched your search

+3 -1
View File
@@ -24,7 +24,7 @@ jobs:
- name: Install runner Python and test dependencies - name: Install runner Python and test dependencies
run: | run: |
apt-get update apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends python3 python3-pytest DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends python3 python3-pytest python3-openpyxl
python3 --version python3 --version
- name: Compile application and build helpers - name: Compile application and build helpers
run: python3 -m compileall -q src packaging tools run: python3 -m compileall -q src packaging tools
@@ -39,6 +39,8 @@ jobs:
tests/packaging/test_dsh_runtime_staging.py tests/packaging/test_dsh_runtime_staging.py
tests/packaging/test_release_configuration.py tests/packaging/test_release_configuration.py
tests/packaging/test_self_extracting_release.py tests/packaging/test_self_extracting_release.py
tests/trial_task_center/test_bom_template_contract.py
tests/trial_task_center/test_bom_import_review.py
--junitxml=build/ci-results.xml --tb=short --junitxml=build/ci-results.xml --tb=short
- name: Windows release scope - name: Windows release scope
run: | run: |
@@ -0,0 +1,32 @@
# PR #21 审查修复与独立验证
审查来源:[评论 217](https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement/pulls/21#issuecomment-217)。本轮修复基于 `8b5509d`,覆盖 1 项 P2、5 项 P3,以及 CI 覆盖建议。保留 PR,未执行合并。
## 修复与验收对应
| 审查项 | 最终行为 | 独立验证 |
| --- | --- | --- |
| 任务显示名与工程文件名不一致 | 精确路径优先;接受唯一的任务显示名或工程文件名;显示名冲突仍阻断。单工程路径留空时保留旧的唯一工程绑定行为,显式错误路径不能任意兜底 | 改名“音频板”后导出、修改为旧路径、真实 XLSX 重新导入;显示名重复与精确路径优先;单工程任意显示名空路径 |
| 跨文件重复提示行号矛盾 | 报告提供 `source_file`、`source_row`;重复原因同时列出全部原文件与原行;界面显示原始行号 | 第一份第 2 行与第二份第 5 行重复,报告两行均失败且无预览;UI 格式验证显示第二份第 5 行 |
| 来源证据无引用、无导出、无限累积 | 来源映射存于锚点任务的 `quick-run-history/bom-import-<uuid>/source-map.json`;报告和任务事件引用;诊断包只纳入 JSON 元数据并沿用脱敏;复用现有历史数量、大小、时间限制;临时规范化 Excel 在 `finally` 删除 | 真实服务导入后检查报告、事件、诊断 ZIP 与 Excel 删除;两次导入后限制保留 1 份,旧映射删除、新映射保留 |
| 匹配原因失真与非原子写入 | 区分精确路径、任务显示名、工程名称、迁移名称、唯一工程兜底;相对路径在规范化前拒绝;映射使用现有原子 `write_record` 和共享占用处理 | 各匹配原因断言;相对路径失败;模拟替换 JSON 失败后原内容完整、无遗留临时 JSON |
| 用户模板缺少 Value 来源列 | 有任务/无任务模板均导出第 12 列,提供全部五种策略下拉、样式与批注;底层旧写入接口默认保持 11 列 | 真实 openpyxl 检查两个模板、五种策略读入和编译、旧 11 列自定义值推断;空任务 UI 下载 |
| 两个操作之间释放占用,读源无进度 | `preview_bom_sources` 在任何本地源读取前进入监督进程;本地读取、路由、检查全程持有一个快速执行租约;读取前通知 READING,并逐文件报告名称与序号 | 在读取与检查内部尝试竞争租约,均被拒;监督入口禁止本地读源;逐文件进度;真实监督子进程端到端返回来源元数据与预览 |
| CI 未覆盖功能 | Linux 工作流安装 openpyxl,加入模板/策略契约与本轮来源导入回归用例,无 Qt/CAD 依赖 | 本机定向执行;远端 CI 状态另由 PR 检查项记录,不用 Windows 本机结果冒充 Linux 结果 |
同时修正监督清理失败的一个边界:业务失败报告由 `primary_error` 继承时,也必须清除其可执行预览。覆盖业务成功/失败 × 清理待处理/异常退出四种组合;报告保留,预览均为空。
## 本轮验证记录
本轮仅验证新增修复及直接相关功能,不重跑整个仓库,也不重复真实 CAD/PCB。原始真实替换验收仍见 `2026-11-05-item-value-bom-native.md`。
- 修复前先运行新复现文件:`test_bom_import_review.py` 的原始 9 项全部失败;修复后 9 项全部通过。随后补齐连续占用、名称冲突和匹配原因用例。
- `python -m pytest -q tests/trial_task_center/test_bom_import_review.py tests/trial_task_center/test_item_value_and_bom_sources.py tests/v1_1/test_bom_sources_supervision.py --tb=short`:**44 passed**,涵盖 14 项审查回归、24 项既有策略/多来源验证、6 项监督验证。
- 模板、旧 BOM、料号类型、CBB 和两个模板导出入口的定向命令:**34 passed**,其中新增模板契约 8 项。
- 监督协议、结果处理和新监督入口:**22 passed**。与上面的 44 项存在 6 项重叠,不累加。
- 报告、诊断包和历史保留相邻用例首轮:**37 passed、1 failed、1 error**。第一项 UI 用例仍拦截旧的检查接口,首轮异步等待超时,未结束的任务影响了后一项的占用检查;单独定位还发现按钮尚未启用就点击的问题。两项现已通过真实的新入口、使用无 CAD 的运行时替身,并明确等待按钮可用。
- 仅重跑上述两项 UI 及新增原始来源格式用例:**3 passed**(`--basetemp=build/pr21-ui-review-confirm`);已通过的相邻用例没有重复跑。
- 最后新增真实监督进程完整导入测试:`test_real_supervised_sources_roundtrip_origin_metadata_and_preview`:**1 passed**(`--basetemp=build/pr21-supervised-import-confirm`)。父进程调用真实服务,子进程读取两份 XLSX 和预先保存的库存,返回完整预览、原文件/行号和 `Path` 类型的映射路径;未打开 CAD,未保存新计划,临时 Excel 已删除。
- 独立只读代码复核覆盖 service/workspace/report/supervisor/diagnostics/retention/CI,未发现阻塞问题。
各条记录属于不同范围,存在重叠,不能相加作为独立总数。未构建安装包,未重复真实 CAD 替换,未执行 PCB。
@@ -89,7 +89,7 @@ def run(request_path):
from .stop_control import StopControl from .stop_control import StopControl
stop = StopControl() if request.get('interactive_stop', False) else threading.Event() stop = StopControl() if request.get('interactive_stop', False) else threading.Event()
finished, output_lock = threading.Event(), threading.Lock() finished, output_lock = threading.Event(), threading.Lock()
kind = {'inspect_bom_batch': 'bom_import', 'preview_bom_batch': 'bom_import', kind = {'inspect_bom_batch': 'bom_import', 'preview_bom_batch': 'bom_import', 'preview_bom_sources': 'bom_import',
'execute_task_queue': 'task_queue', 'execute_quick_run_queue': 'quick_run_queue'}.get(request['operation'], request['operation']) 'execute_task_queue': 'task_queue', 'execute_quick_run_queue': 'quick_run_queue'}.get(request['operation'], request['operation'])
runtime = BatchCadRuntime(root, kind, stop, session_id=batch_id) runtime = BatchCadRuntime(root, kind, stop, session_id=batch_id)
sequence = 0 sequence = 0
@@ -156,9 +156,9 @@ def run(request_path):
elif operation == 'execute_task_queue': elif operation == 'execute_task_queue':
from .task_queue import execute_task_queue from .task_queue import execute_task_queue
result = execute_task_queue(service, **arguments, stop_event=stop, runtime=runtime) result = execute_task_queue(service, **arguments, stop_event=stop, runtime=runtime)
elif operation in {'inspect_bom_batch', 'preview_bom_batch'}: elif operation in {'inspect_bom_batch', 'preview_bom_batch', 'preview_bom_sources'}:
controls = {'stop_event': stop, 'on_progress': progress, 'runtime': runtime} controls = {'stop_event': stop, 'on_progress': progress, 'runtime': runtime}
if operation == 'inspect_bom_batch': if operation in {'inspect_bom_batch', 'preview_bom_sources'}:
controls['on_phase'] = phase controls['on_phase'] = phase
result = getattr(service, operation)(**arguments, **controls) result = getattr(service, operation)(**arguments, **controls)
elif operation == 'collect_target_inventory': elif operation == 'collect_target_inventory':
@@ -174,7 +174,7 @@ def run(request_path):
response.update(status='ok', business_succeeded=True, result=encode(result)) response.update(status='ok', business_succeeded=True, result=encode(result))
write_record(directory / 'business-result.json', {'version': VERSION, 'batch_id': batch_id, write_record(directory / 'business-result.json', {'version': VERSION, 'batch_id': batch_id,
'attempt_id': attempt_id, 'generation': max(1, runtime.generation), 'payload': response}) 'attempt_id': attempt_id, 'generation': max(1, runtime.generation), 'payload': response})
if operation == 'inspect_bom_batch': if operation in {'inspect_bom_batch', 'preview_bom_sources'}:
from .bom_import_state import BomImportPhase from .bom_import_state import BomImportPhase
phase(BomImportPhase.RELEASING, '本批位号核验结束,正在释放后台资源…') phase(BomImportPhase.RELEASING, '本批位号核验结束,正在释放后台资源…')
except Exception as error: except Exception as error:
@@ -45,7 +45,7 @@ def _identity(scope):
def export_template(store, task_id, path): def export_template(store, task_id, path):
records = () if task_id is None else tuple({'project_name': task.name, 'project_path': str(project.path), records = () if task_id is None else tuple({'project_name': task.name, 'project_path': str(project.path),
'enabled': True, 'sync_nc': True} for task, project in _scope(store, task_id)) 'enabled': True, 'sync_nc': True} for task, project in _scope(store, task_id))
return write_bom_template(path, records) return write_bom_template(path, records, include_value_mode=True)
def _assert_editable(service, task_id): def _assert_editable(service, task_id):
@@ -23,6 +23,9 @@ class BomRowResult:
project_name: str project_name: str
status: str # passed, failed, unchecked status: str # passed, failed, unchecked
reason: str = '' reason: str = ''
source_file: str = ''
source_row: int | None = None
duplicate_rows: tuple[int, ...] = ()
@dataclass(frozen=True) @dataclass(frozen=True)
@@ -39,6 +42,7 @@ class BomImportReport:
projects: tuple[BomProjectResult, ...] projects: tuple[BomProjectResult, ...]
preview: BomBatchPreview | None = None preview: BomBatchPreview | None = None
errors: tuple[str, ...] = () errors: tuple[str, ...] = ()
source_map_path: Path | None = None
@property @property
def total_rows(self): def total_rows(self):
@@ -120,7 +124,8 @@ def _check_record_conflicts(rows, records, indices):
reason = '位号在 BOM 中重复(Excel 行 ' + ', '.join( reason = '位号在 BOM 中重复(Excel 行 ' + ', '.join(
str(rows[item].excel_row) for item in dict.fromkeys(claimed)) + ')' str(rows[item].excel_row) for item in dict.fromkeys(claimed)) + ')'
for index in claimed: for index in claimed:
rows[index] = replace(rows[index], status='failed', reason=reason) rows[index] = replace(rows[index], status='failed', reason=reason,
duplicate_rows=tuple(rows[item].excel_row for item in dict.fromkeys(claimed)))
def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None, on_phase=None, runtime=None): def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None, on_phase=None, runtime=None):
@@ -316,7 +321,8 @@ def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None,
if len(indices_for_target) > 1: if len(indices_for_target) > 1:
for index in indices_for_target: for index in indices_for_target:
rows[index] = replace(rows[index], status='failed', reason='位号在 BOM 中重复(Excel 行 ' + rows[index] = replace(rows[index], status='failed', reason='位号在 BOM 中重复(Excel 行 ' +
', '.join(str(rows[item].excel_row) for item in indices_for_target) + ')') ', '.join(str(rows[item].excel_row) for item in indices_for_target) + ')',
duplicate_rows=tuple(rows[item].excel_row for item in indices_for_target))
combined = tuple(matched_rows.get(key, row) for key, row in matcher.by_key.items()) if matcher else () combined = tuple(matched_rows.get(key, row) for key, row in matcher.by_key.items()) if matcher else ()
try: try:
compile_target_rules(combined, default_value_mode=task_default_value_mode) compile_target_rules(combined, default_value_mode=task_default_value_mode)
@@ -29,6 +29,8 @@ _HEADER_HELP = (
'是否替换:TRUE/FALSE 或 是/否;留空默认替换。未列出的位号保留。', '是否替换:TRUE/FALSE 或 是/否;留空默认替换。未列出的位号保留。',
'自定义 Value,可选;留空沿用工具原有 Value 策略。', '自定义 Value,可选;留空沿用工具原有 Value 策略。',
'同步 NC:TRUE/FALSE 或 是/否;留空默认开启。', '同步 NC:TRUE/FALSE 或 是/否;留空默认开启。',
'Value 来源:inherit 沿用任务策略,preserve 保留原值,part_label 使用 Part Label,'
'part_number 使用新料号,custom 使用自定义 Value 列;留空时有自定义值则使用 custom,否则 inherit。',
) )
_REFDES_SPLIT = re.compile(r'[,,;;、\s]+') _REFDES_SPLIT = re.compile(r'[,,;;、\s]+')
_ALIASES = { _ALIASES = {
@@ -45,9 +47,9 @@ _ALIASES = {
} }
def write_bom_template(path: Path, records=()) -> Path: def write_bom_template(path: Path, records=(), *, include_value_mode=False) -> Path:
records = tuple(records) records = tuple(records)
headers = BOM_HEADERS + (('value_mode',) if any('value_mode' in record for record in records) else ()) headers = BOM_HEADERS + (('value_mode',) if include_value_mode or any('value_mode' in record for record in records) else ())
workbook = Workbook() workbook = Workbook()
sheet = workbook.active sheet = workbook.active
sheet.title = 'BOM Replacement' sheet.title = 'BOM Replacement'
@@ -52,6 +52,9 @@ def _scrub_json(value):
def _allowed(parts): def _allowed(parts):
if (len(parts) == 5 and parts[0] == 'tasks' and parts[2] == 'quick-run-history'
and re.fullmatch(r'bom-import-[A-Za-z0-9-]+', parts[3])):
return parts[-1] == 'source-map.json'
if len(parts) == 2 and parts[0] == 'logs': if len(parts) == 2 and parts[0] == 'logs':
return bool(re.fullmatch(r'[^/]+\.log(?:\.\d+)?', parts[-1])) return bool(re.fullmatch(r'[^/]+\.log(?:\.\d+)?', parts[-1]))
if len(parts) == 3 and parts[0] == 'batch-resources': if len(parts) == 3 and parts[0] == 'batch-resources':
@@ -16,7 +16,7 @@ HISTORY_MAX_AGE_DAYS = 30
HISTORY_MAX_ENTRIES = 100 HISTORY_MAX_ENTRIES = 100
HISTORY_MAX_BYTES = 5 * 1024 * 1024 * 1024 HISTORY_MAX_BYTES = 5 * 1024 * 1024 * 1024
HISTORY_MIN_ENTRIES = 10 HISTORY_MIN_ENTRIES = 10
_HISTORY_NAME = re.compile(r"(?:batch|recovery|sheet-discovery|obsolete)-[A-Za-z0-9-]+\Z") _HISTORY_NAME = re.compile(r"(?:batch|recovery|sheet-discovery|obsolete|bom-import)-[A-Za-z0-9-]+\Z")
@dataclass(frozen=True, slots=True) @dataclass(frozen=True, slots=True)
@@ -198,48 +198,79 @@ class TaskCenterService:
def preview_bom_sources(self, task_id: str, sources, *, stop_event=None, on_progress=None, def preview_bom_sources(self, task_id: str, sources, *, stop_event=None, on_progress=None,
on_phase=None, runtime=None): on_phase=None, runtime=None):
"""Normalize several BOM files and exact-name migrated project paths before CAD reads.""" """Normalize several BOM files and exact-name migrated project paths before CAD reads."""
sources = tuple(Path(source) for source in sources)
if runtime is None and self._supervised:
return self._supervise('preview_bom_sources', {'task_id': task_id, 'sources': sources},
stop_event=stop_event, on_progress=on_progress, on_phase=on_phase)
from .bom_batch import _scope from .bom_batch import _scope
from .bom_import_report import BomImportReport from .bom_import_report import BomImportReport, inspect_batch
from .bom_import_state import BomImportPhase
from .retention import maintain_quick_run_storage
from .workspace_bom import load_sources, route_sources, write_routed_bom from .workspace_bom import load_sources, route_sources, write_routed_bom
from uuid import uuid4 from uuid import uuid4
def normalize(): def preview():
rows = load_sources(tuple(Path(source) for source in sources), stop_event=stop_event) message = '正在读取 BOM 来源表格…'
if on_phase is not None:
on_phase(BomImportPhase.READING, message)
if on_progress is not None:
on_progress(message)
rows = load_sources(sources, stop_event=stop_event, on_progress=on_progress)
scope = _scope(self._store, task_id) scope = _scope(self._store, task_id)
paths = tuple(project.path for _, project in scope) paths = tuple(project.path for _, project in scope)
routes = route_sources(rows, paths, single=len(paths) == 1) names = {project.path: task.name for task, project in scope}
routes = route_sources(rows, paths, single=len(paths) == 1, project_names=names)
unresolved = [(row.source.name, row.excel_row, row.project_name or row.project_path) unresolved = [(row.source.name, row.excel_row, row.project_name or row.project_path)
for row in rows if routes.get(row.project_key) is None] for row in rows if routes.get(row.project_key) is None]
if unresolved: if unresolved:
details = '; '.join(f'{name} 第 {number} 行:{key}' for name, number, key in unresolved[:20]) details = '; '.join(f'{name} 第 {number} 行:{key}' for name, number, key in unresolved[:20])
raise TaskCenterError('project_binding', f'无法唯一匹配 BOM 工程,请确认迁移工程对应关系:{details}') raise TaskCenterError('project_binding', f'无法唯一匹配 BOM 工程,请确认迁移工程对应关系:{details}')
root = self._logging.root / 'imports' / uuid4().hex task_root = self._logging.root / 'tasks' / task_id
normalized = write_routed_bom(root / 'normalized-bom.xlsx', rows, routes) maintain_quick_run_storage(task_root)
return normalized, rows root = task_root / 'quick-run-history' / ('bom-import-' + uuid4().hex)
normalized = root / 'normalized-bom.xlsx'
mapping = root / 'source-map.json'
normalized, sources_by_row = self._idle_quick_run_operation('preview_bom_sources', task_id, normalize) def annotate(report):
if report is None:
return report
annotated = []
for row in report.rows:
if not 2 <= row.excel_row <= len(rows) + 1:
annotated.append(row)
continue
source = rows[row.excel_row - 2]
reason = row.reason
if row.duplicate_rows:
locations = (f'{rows[number - 2].source.name} 第 {rows[number - 2].excel_row} 行'
for number in row.duplicate_rows)
reason = '位号在 BOM 中重复(' + ';'.join(locations) + ')'
annotated.append(replace(row, source_file=str(source.source), source_row=source.excel_row,
reason=reason + f';来源:{source.source} 第 {source.excel_row} 行'))
return replace(report, rows=tuple(annotated), source_map_path=mapping)
def annotate(report): def inspection_phase(phase, message):
if report is None: if on_phase is not None and phase is not BomImportPhase.READING:
return report on_phase(phase, message)
annotated = tuple(
replace(row, reason=(row.reason + ';来源:' +
f"{sources_by_row[row.excel_row - 2].source} 第 {sources_by_row[row.excel_row - 2].excel_row} 行")
if 2 <= row.excel_row <= len(sources_by_row) + 1 else row.reason)
for row in report.rows)
return replace(report, rows=annotated)
# Reuse the existing supervised reader and its cancellation/resource gates. try:
try: write_routed_bom(normalized, rows, routes, project_names=names)
report = self.inspect_bom_batch(task_id, normalized, stop_event=stop_event, append_task_event(self._logging, level='INFO', task_id=task_id,
on_progress=on_progress, on_phase=on_phase, runtime=runtime) operation='preview_bom_sources', message='BOM 导入来源记录已保存',
except TaskCenterError as error: details={'source_map_path': str(mapping), 'source_count': len({row.source for row in rows})})
if getattr(error, 'bom_report', None) is not None: report = inspect_batch(self, task_id, normalized, stop_event=stop_event,
error.bom_report = annotate(error.bom_report) on_progress=on_progress, on_phase=inspection_phase, runtime=runtime)
if isinstance(getattr(error, 'completed_result', None), BomImportReport): return annotate(report)
error.completed_result = annotate(error.completed_result) except TaskCenterError as error:
raise if getattr(error, 'bom_report', None) is not None:
return annotate(report) error.bom_report = annotate(error.bom_report)
if isinstance(getattr(error, 'completed_result', None), BomImportReport):
error.completed_result = annotate(error.completed_result)
raise
finally:
normalized.unlink(missing_ok=True)
return self._idle_quick_run_operation('preview_bom_sources', task_id, preview)
def save_bom_batch(self, preview, rows_by_task): def save_bom_batch(self, preview, rows_by_task):
from .bom_batch import save_batch from .bom_batch import save_batch
@@ -11,7 +11,7 @@ import re
from .errors import TaskCenterError from .errors import TaskCenterError
VERSION = 1 VERSION = 1
OPERATIONS = frozenset({'probe', 'runtime_smoke', 'inspect_bom_batch', 'preview_bom_batch', OPERATIONS = frozenset({'probe', 'runtime_smoke', 'inspect_bom_batch', 'preview_bom_batch', 'preview_bom_sources',
'collect_target_inventory', 'execute_task_queue', 'execute_quick_run_queue', 'execute_quick_run', 'collect_target_inventory', 'execute_task_queue', 'execute_quick_run_queue', 'execute_quick_run',
'prepare_quick_run', 'prepare_failed_quick_run', 'recover_quick_run', 'cleanup_batch_resources', 'prepare_quick_run', 'prepare_failed_quick_run', 'recover_quick_run', 'cleanup_batch_resources',
'target_cell_choices'}) 'target_cell_choices'})
@@ -13,7 +13,8 @@ def validated_outcome(operation, body):
or body.get('status') not in {'ok', 'error'}): or body.get('status') not in {'ok', 'error'}):
raise ValueError('Invalid supervisor business outcome') raise ValueError('Invalid supervisor business outcome')
result = decode(body['result']) if body['business_succeeded'] else None result = decode(body['result']) if body['business_succeeded'] else None
expected = {'inspect_bom_batch': BomImportReport, 'preview_bom_batch': BomBatchPreview, expected = {'inspect_bom_batch': BomImportReport, 'preview_bom_sources': BomImportReport,
'preview_bom_batch': BomBatchPreview,
'execute_task_queue': TaskQueueResult, 'execute_quick_run_queue': BatchRunResult, 'execute_task_queue': TaskQueueResult, 'execute_quick_run_queue': BatchRunResult,
'execute_quick_run': QuickRunStepResult, 'recover_quick_run': QuickRunStepResult, 'execute_quick_run': QuickRunStepResult, 'recover_quick_run': QuickRunStepResult,
'prepare_quick_run': QuickRunResult, 'prepare_failed_quick_run': QuickRunProgress, 'prepare_quick_run': QuickRunResult, 'prepare_failed_quick_run': QuickRunProgress,
@@ -43,10 +44,11 @@ def resource_pending_error(operation, result, error, primary_error=None):
result = replace(result, preview=None) result = replace(result, preview=None)
failure = BatchResourceCleanupError('后台资源仍待清理;报告和已保存核验结果保留。' + (str(error) if error else ''), failure = BatchResourceCleanupError('后台资源仍待清理;报告和已保存核验结果保留。' + (str(error) if error else ''),
completed_result=result, primary_error=primary_error, cleanup_error=error) completed_result=result, primary_error=primary_error, cleanup_error=error)
if isinstance(result, BomImportReport): for candidate in (result, getattr(error, 'bom_report', None),
failure.bom_report = result getattr(primary_error, 'bom_report', None)):
elif getattr(error, 'bom_report', None) is not None: if isinstance(candidate, BomImportReport):
failure.bom_report = replace(error.bom_report, preview=None) failure.bom_report = replace(candidate, preview=None)
break
return failure return failure
@@ -697,7 +697,10 @@ class ProjectPlanPanel(QWidget):
f'共 {report.total_projects} 个工程,通过 {report.passed_projects} 个、' f'共 {report.total_projects} 个工程,通过 {report.passed_projects} 个、'
f'失败 {report.failed_projects} 个、未检查 {report.unchecked_projects} 个。') f'失败 {report.failed_projects} 个、未检查 {report.unchecked_projects} 个。')
details = [summary, *report.errors] details = [summary, *report.errors]
details.extend(f'Excel 第 {row.excel_row} 行 · {row.project_name or "未指定工程"} ' if report.source_map_path is not None:
details.append(f'导入来源记录:{report.source_map_path}')
details.extend(f'{Path(row.source_file).name + " · " if row.source_file else ""}'
f'Excel 第 {row.source_row if row.source_row is not None else row.excel_row} 行 · {row.project_name or "未指定工程"} '
f'({row.project_path or "无工程路径"})· ' f'({row.project_path or "无工程路径"})· '
f'{ {"passed": "通过", "failed": "失败", "unchecked": "未检查"}[row.status]}:{row.reason}' f'{ {"passed": "通过", "failed": "失败", "unchecked": "未检查"}[row.status]}:{row.reason}'
for row in report.rows) for row in report.rows)
@@ -1,8 +1,7 @@
"""Safe multi-file BOM loading and project routing for workspace imports.""" """Safe multi-file BOM loading and project routing for workspace imports."""
from dataclasses import dataclass from dataclasses import dataclass
import json from pathlib import Path, PureWindowsPath
from pathlib import Path
from openpyxl import load_workbook from openpyxl import load_workbook
@@ -54,7 +53,7 @@ def _selected_xlsx(sources):
return tuple(sorted(paths, key=lambda path: str(path).casefold())) return tuple(sorted(paths, key=lambda path: str(path).casefold()))
def load_sources(sources, *, stop_event=None): def load_sources(sources, *, stop_event=None, on_progress=None):
"""Read every selected workbook before any CAD operation starts.""" """Read every selected workbook before any CAD operation starts."""
from .operation_control import check_cancelled from .operation_control import check_cancelled
@@ -62,8 +61,10 @@ def load_sources(sources, *, stop_event=None):
if not paths: if not paths:
raise TaskCenterError('bom_import', '未找到可导入的 .xlsx 替换清单') raise TaskCenterError('bom_import', '未找到可导入的 .xlsx 替换清单')
result = [] result = []
for path in paths: for number, path in enumerate(paths, 1):
check_cancelled(stop_event) check_cancelled(stop_event)
if on_progress is not None:
on_progress(f'正在读取 BOM {number}/{len(paths)}:{path.name}')
workbook = None workbook = None
try: try:
workbook = load_workbook(path, read_only=True, data_only=False) workbook = load_workbook(path, read_only=True, data_only=False)
@@ -91,6 +92,9 @@ def load_sources(sources, *, stop_event=None):
) )
if not any(values[index] not in (None, '') for index in (2, 3, 4, 9)): if not any(values[index] not in (None, '') for index in (2, 3, 4, 9)):
continue continue
project_path = str(values[1] or '').strip()
if project_path and not (Path(project_path).is_absolute() or PureWindowsPath(project_path).is_absolute()):
raise ValueError(f'Excel 第 {excel_row} 行工程路径必须是完整路径')
try: try:
_part_number_text(cells, positions, 'old_part_number', '旧料号') _part_number_text(cells, positions, 'old_part_number', '旧料号')
_part_number_text(cells, positions, 'new_part_number', '新料号') _part_number_text(cells, positions, 'new_part_number', '新料号')
@@ -120,7 +124,7 @@ def _exact_name_matches(project_name, paths):
return tuple(path for path in paths if _project_name(path) == name) return tuple(path for path in paths if _project_name(path) == name)
def resolve_project(project_path, project_name, paths): def resolve_project(project_path, project_name, paths, *, project_names=None):
"""Resolve exact path first, then an exact unique migrated project name.""" """Resolve exact path first, then an exact unique migrated project name."""
paths = tuple(Path(path) for path in paths) paths = tuple(Path(path) for path in paths)
raw = _path_key(project_path) raw = _path_key(project_path)
@@ -128,18 +132,21 @@ def resolve_project(project_path, project_name, paths):
if len(exact_path) == 1: if len(exact_path) == 1:
return exact_path[0] return exact_path[0]
if project_name: if project_name:
aliases = tuple(path for path in paths
if _path_key((project_names or {}).get(path)) == _path_key(project_name))
# A project_name containing directories is stronger than a basename. # A project_name containing directories is stronger than a basename.
name_key = _path_key(project_name) name_key = _path_key(project_name)
suffix = tuple(path for path in paths if _path_key(path).endswith('/' + name_key.lstrip('/'))) suffix = tuple(path for path in paths if _path_key(path).endswith('/' + name_key.lstrip('/')))
if len(suffix) == 1: matches = tuple(dict.fromkeys((*aliases, *suffix)))
return suffix[0] if len(matches) == 1:
if len(suffix) > 1: return matches[0]
if len(matches) > 1:
return None return None
matches = _exact_name_matches(project_name or project_path, paths) matches = _exact_name_matches(project_name or project_path, paths)
return matches[0] if len(matches) == 1 else None return matches[0] if len(matches) == 1 else None
def route_sources(rows, paths, *, manual=None, single=False): def route_sources(rows, paths, *, manual=None, single=False, project_names=None):
"""Route source rows; stale paths are allowed only on exact unique names.""" """Route source rows; stale paths are allowed only on exact unique names."""
paths, manual = tuple(Path(path) for path in paths), manual or {} paths, manual = tuple(Path(path) for path in paths), manual or {}
routes = {} routes = {}
@@ -153,14 +160,14 @@ def route_sources(rows, paths, *, manual=None, single=False):
if chosen not in paths: if chosen not in paths:
raise TaskCenterError('project_binding', '人工选择的工程不在本次工程列表中') raise TaskCenterError('project_binding', '人工选择的工程不在本次工程列表中')
else: else:
chosen = resolve_project(row.project_path, row.project_name, paths) chosen = resolve_project(row.project_path, row.project_name, paths, project_names=project_names)
if chosen is None and single and len(paths) == 1 and not row.project_name and not row.project_path: if chosen is None and single and len(paths) == 1 and not row.project_path:
chosen = paths[0] chosen = paths[0]
routes[key] = chosen routes[key] = chosen
return routes return routes
def write_routed_bom(destination, rows, routes): def write_routed_bom(destination, rows, routes, *, project_names=None):
"""Write canonical project paths while preserving row order and values.""" """Write canonical project paths while preserving row order and values."""
records = [] records = []
source_map = [] source_map = []
@@ -172,14 +179,26 @@ def write_routed_bom(destination, rows, routes):
record['project_path'] = str(Path(target).resolve()) record['project_path'] = str(Path(target).resolve())
record['value_mode'] = row.value_mode record['value_mode'] = row.value_mode
records.append(record) records.append(record)
if _path_key(row.project_path) == _path_key(target.resolve()):
reason = 'exact_path'
elif row.project_name and _path_key(row.project_name) == _path_key((project_names or {}).get(target)):
reason = 'migrated_task_name' if row.project_path else 'task_name'
elif row.project_name and _path_key(target).endswith('/' + _path_key(row.project_name).lstrip('/')):
reason = 'migrated_exact_name' if row.project_path else 'exact_name'
elif row.project_name and _project_name(row.project_name) == _project_name(target):
reason = 'migrated_basename' if row.project_path else 'basename'
elif row.project_path:
reason = 'migrated_basename'
else:
reason = 'single_project'
source_map.append({'source_file': str(row.source), 'source_row': row.excel_row, source_map.append({'source_file': str(row.source), 'source_row': row.excel_row,
'original_project_path': row.project_path, 'original_project_path': row.project_path,
'project_name': row.project_name, 'project_name': row.project_name,
'matched_project_path': str(Path(target).resolve()), 'matched_project_path': str(Path(target).resolve()),
'match_reason': 'migrated_exact_name' if _path_key(row.project_path) != _path_key(target.resolve()) else 'exact_path'}) 'match_reason': reason})
destination = Path(destination) destination = Path(destination)
destination.parent.mkdir(parents=True, exist_ok=True) destination.parent.mkdir(parents=True, exist_ok=True)
result = write_bom_template(destination, records) result = write_bom_template(destination, records)
destination.with_name('source-map.json').write_text( from .batch_resources import write_record
json.dumps(source_map, ensure_ascii=False, indent=2), encoding='utf-8') write_record(destination.with_name('source-map.json'), source_map)
return result return result
@@ -0,0 +1,218 @@
"""PR21 routing, source evidence and operation-boundary regressions; no CAD."""
import json
from pathlib import Path
from types import SimpleNamespace
import zipfile
from openpyxl import Workbook, load_workbook
import pytest
from dx_productivity.trial_task_center.bom_import_report import BomImportReport
from dx_productivity.trial_task_center.bom_plan import BOM_HEADERS
from dx_productivity.trial_task_center.errors import TaskCenterError
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.target_plan import ComponentTarget
from dx_productivity.trial_task_center.workspace_bom import load_sources, route_sources, write_routed_bom
@pytest.fixture
def case(tmp_path):
root = tmp_path / 'projects'
for name in ('BoardA', 'BoardB'):
project = root / name / (name + '.prj')
project.parent.mkdir(parents=True)
project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
store = TaskStore(tmp_path / 'tasks.db')
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
tasks = service.create_project_tasks(root)
paths = tuple(service.projects(t.task_id)[0].path for t in tasks)
for task, path in zip(tasks, paths):
store.record_target_inventory(task.task_id, (
ComponentTarget(path, 'MAIN', '1', 'u1', 'R1', 'OLD', '10nF'),))
yield SimpleNamespace(service=service, store=store, tasks=tasks, paths=paths,
runtime=SimpleNamespace(bind_logs=lambda *_: None), root=tmp_path / 'data')
store.close()
def source(path, *, name='BoardA.prj', project='Z:/old/BoardA.prj', row=2):
book = Workbook()
sheet = book.active
sheet.append(BOM_HEADERS)
for _ in range(2, row):
sheet.append([None] * len(BOM_HEADERS))
sheet.append([name, project, 'OLD', 'NEW', 'R1', '', '', '', True, '', True])
book.save(path)
book.close()
return path
def test_renamed_task_template_routes_stale_path_in_multi_project_batch(case, tmp_path):
task = case.tasks[0]
case.service.update_task(task.task_id, name='音频板', note='')
path = case.service.export_bom_template(task.task_id, tmp_path / 'renamed.xlsx')
book = load_workbook(path)
sheet = book.active
sheet['B2'] = 'Z:/moved/BoardA.prj'
sheet['C2'], sheet['D2'], sheet['E2'] = 'OLD', 'NEW', 'R1'
book.save(path)
book.close()
report = case.service.preview_bom_sources(task.task_id, (path,), runtime=case.runtime)
assert report.passed_rows == 1 and report.preview
assert report.preview.projects[0].project_path == case.paths[0]
assert json.loads(report.source_map_path.read_text(encoding='utf-8'))[0]['match_reason'] == 'migrated_task_name'
def test_single_project_blank_path_keeps_legacy_display_name_binding(case, tmp_path):
task_id = case.tasks[0].task_id
case.store._connection.execute('UPDATE tasks SET group_id = NULL WHERE task_id = ?', (task_id,))
path = source(tmp_path / 'blank-path.xlsx', name='display name from another machine', project='')
report = case.service.preview_bom_sources(task_id, (path,), runtime=case.runtime)
assert report.passed_rows == 1 and report.preview
assert json.loads(report.source_map_path.read_text(encoding='utf-8'))[0]['match_reason'] == 'single_project'
def test_duplicate_messages_use_both_original_workbook_coordinates(case, tmp_path):
first = source(tmp_path / 'first.xlsx')
second = source(tmp_path / 'second.xlsx', row=5)
report = case.service.preview_bom_sources(case.tasks[0].task_id, (first, second), runtime=case.runtime)
assert report.preview is None and report.failed_rows == 2
assert [row.source_row for row in report.rows] == [2, 5]
for row in report.rows:
assert 'first.xlsx 第 2 行' in row.reason
assert 'second.xlsx 第 5 行' in row.reason
assert 'Excel 行 2, 3' not in row.reason
def test_import_evidence_is_referenced_exportable_and_discards_bom_copy(case, tmp_path):
from dx_productivity.trial_task_center.diagnostics_bundle import export_diagnostic_bundle
path = source(tmp_path / 'original.xlsx')
report = case.service.preview_bom_sources(case.tasks[0].task_id, (path,), runtime=case.runtime)
mapping = report.source_map_path
assert mapping.is_file()
assert not mapping.with_name('normalized-bom.xlsx').exists()
task_events = case.root / 'tasks' / case.tasks[0].task_id / 'events.jsonl'
assert any(json.loads(line).get('details', {}).get('source_map_path') == str(mapping)
for line in task_events.read_text(encoding='utf-8').splitlines())
bundle = export_diagnostic_bundle(case.root, tmp_path / 'diagnostics.zip')
with zipfile.ZipFile(bundle) as archive:
assert mapping.relative_to(case.root).as_posix() in archive.namelist()
assert not any(name.endswith('.xlsx') for name in archive.namelist())
def test_source_map_has_bounded_existing_history_retention(case, tmp_path):
from dx_productivity.trial_task_center.retention import maintain_quick_run_storage
path = source(tmp_path / 'source.xlsx')
first = case.service.preview_bom_sources(case.tasks[0].task_id, (path,), runtime=case.runtime)
second = case.service.preview_bom_sources(case.tasks[0].task_id, (path,), runtime=case.runtime)
task_root = case.root / 'tasks' / case.tasks[0].task_id
maintain_quick_run_storage(task_root, max_entries=1, min_entries=0)
assert not first.source_map_path.exists()
assert second.source_map_path.is_file()
def test_relative_project_path_is_rejected_before_normalization(tmp_path):
path = source(tmp_path / 'relative.xlsx', project='sub/BoardA.prj')
with pytest.raises(TaskCenterError, match='完整路径'):
load_sources((path,))
def test_failed_source_map_replace_preserves_previous_valid_json(tmp_path, monkeypatch):
import dx_productivity.trial_task_center.batch_resources as resources
path = source(tmp_path / 'source.xlsx')
rows = load_sources((path,))
routes = route_sources(rows, (tmp_path / 'BoardA.prj',))
mapping = tmp_path / 'source-map.json'
mapping.write_text('[{"previous":true}]', encoding='utf-8')
real_replace = resources.os.replace
def deny(source_path, destination_path):
if Path(destination_path).name == 'source-map.json':
raise OSError('controlled disk write failure')
return real_replace(source_path, destination_path)
monkeypatch.setattr(resources.os, 'replace', deny)
with pytest.raises(OSError, match='controlled disk'):
write_routed_bom(tmp_path / 'normalized.xlsx', rows, routes)
assert json.loads(mapping.read_text(encoding='utf-8')) == [{'previous': True}]
assert not list(tmp_path.glob('source-map.json.*.tmp'))
def test_supervised_import_dispatches_before_reading_any_source(case, tmp_path, monkeypatch):
import dx_productivity.trial_task_center.workspace_bom as workspace
case.service._supervised = True
path = source(tmp_path / 'source.xlsx')
monkeypatch.setattr(workspace, 'load_sources', lambda *_a, **_kw: pytest.fail('source read escaped supervisor'))
def supervise(operation, arguments, **controls):
assert operation == 'preview_bom_sources'
assert arguments['sources'] == (path,)
return BomImportReport((), ())
monkeypatch.setattr(case.service, '_supervise', supervise)
assert case.service.preview_bom_sources(case.tasks[0].task_id, (path,)).total_rows == 0
def test_each_source_reports_read_progress_before_inspection(case, tmp_path, monkeypatch):
import dx_productivity.trial_task_center.bom_import_report as inspection
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
paths = (source(tmp_path / 'first.xlsx'), source(tmp_path / 'second.xlsx', name='BoardB.prj', project='Z:/old/BoardB.prj'))
progress, phases = [], []
original = inspection.inspect_batch
def inspect(*args, **kwargs):
assert any('first.xlsx' in message for message in progress)
assert any('second.xlsx' in message for message in progress)
assert phases[0] is BomImportPhase.READING
return original(*args, **kwargs)
monkeypatch.setattr(inspection, 'inspect_batch', inspect)
report = case.service.preview_bom_sources(case.tasks[0].task_id, paths, runtime=case.runtime,
on_progress=progress.append, on_phase=lambda phase, _: phases.append(phase))
assert report.passed_rows == 2
def test_single_lease_covers_source_read_and_inspection(case, tmp_path, monkeypatch):
import dx_productivity.trial_task_center.workspace_bom as workspace
import dx_productivity.trial_task_center.bom_import_report as inspection
from dx_productivity.trial_task_center.service import _quick_run_lease
checked = []
def assert_busy():
with pytest.raises(TaskCenterError) as failure:
with _quick_run_lease(case.root):
pytest.fail('import operation released its lease')
assert failure.value.code == 'quick_run_busy'
original_load, original_inspect = workspace.load_sources, inspection.inspect_batch
def load(*args, **kwargs):
assert_busy()
checked.append('read')
return original_load(*args, **kwargs)
def inspect(*args, **kwargs):
assert_busy()
checked.append('inspect')
return original_inspect(*args, **kwargs)
monkeypatch.setattr(workspace, 'load_sources', load)
monkeypatch.setattr(inspection, 'inspect_batch', inspect)
report = case.service.preview_bom_sources(case.tasks[0].task_id,
(source(tmp_path / 'source.xlsx'),), runtime=case.runtime)
assert report.passed_rows == 1 and checked == ['read', 'inspect']
with _quick_run_lease(case.root):
pass
def test_duplicate_task_alias_blocks_but_exact_path_wins(case, tmp_path):
from dx_productivity.trial_task_center.workspace_bom import resolve_project
names = dict.fromkeys(case.paths, 'same display name')
assert resolve_project('Z:/old/BoardA.prj', 'same display name', case.paths, project_names=names) is None
assert resolve_project(str(case.paths[0]), 'same display name', case.paths, project_names=names) == case.paths[0]
@pytest.mark.parametrize('name,project,reason', [
('BoardA.prj', '', 'exact_name'),
('', '', 'single_project'),
('BoardA.prj', 'Z:/old/BoardA.prj', 'migrated_exact_name'),
])
def test_source_map_distinguishes_name_migration_from_blank_fallback(case, tmp_path, name, project, reason):
task_id = case.tasks[0].task_id
case.store._connection.execute('UPDATE tasks SET group_id = NULL WHERE task_id = ?', (task_id,))
path = source(tmp_path / 'source.xlsx', name=name, project=project)
report = case.service.preview_bom_sources(task_id, (path,), runtime=case.runtime)
assert report.passed_rows == 1
assert json.loads(report.source_map_path.read_text(encoding='utf-8'))[0]['match_reason'] == reason
@@ -0,0 +1,81 @@
"""Portable XLSX and item Value contracts; no CAD, Qt, or Windows runtime."""
import pytest
from openpyxl import load_workbook
from dx_productivity.trial_task_center.bom_batch import export_template
from dx_productivity.trial_task_center.bom_plan import (
BOM_HEADERS, match_bom_records, read_bom_records, write_bom_template,
)
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow, compile_target_rules
@pytest.mark.parametrize('with_task', [False, True])
def test_user_template_exposes_editable_value_source_without_inventory(tmp_path, with_task):
store = TaskStore(tmp_path / 'tasks.db')
try:
task_id = None
if with_task:
task_id = store.create_task('Board').task_id
store.sync_projects(task_id, tmp_path, (tmp_path / 'Board.prj',))
path = export_template(store, task_id, tmp_path / 'template.xlsx')
book = load_workbook(path)
try:
sheet = book.active
assert tuple(cell.value for cell in sheet[1]) == BOM_HEADERS + ('value_mode',)
header = sheet['L1']
assert header.font.bold and header.font.color.rgb == '00FFFFFF'
assert header.fill.patternType == 'solid' and header.fill.fgColor.rgb == '001D3557'
assert header.comment and all(mode in header.comment.text for mode in (
'inherit', 'preserve', 'part_label', 'part_number', 'custom'))
assert any(validation.formula1 == '"inherit,preserve,part_label,part_number,custom"'
and 'L2' in validation for validation in sheet.data_validations.dataValidation)
assert sheet['C2'].number_format == sheet['D2'].number_format == '@'
if with_task:
assert sheet['B2'].value == str(tmp_path / 'Board.prj')
assert not store.list_target_plan(task_id)
finally:
book.close()
assert read_bom_records(path) == ()
finally:
store.close()
def test_legacy_writer_keeps_eleven_columns_and_custom_value_inference(tmp_path):
path = write_bom_template(tmp_path / 'legacy.xlsx', ({
'old_part_number': '001', 'new_part_number': '002', 'refdes': 'R1', 'custom_value': '47k',
},))
book = load_workbook(path)
try:
assert len(BOM_HEADERS) == 11
assert tuple(cell.value for cell in book.active[1]) == BOM_HEADERS
finally:
book.close()
record, = read_bom_records(path)
assert (record.old, record.new, record.value_mode, record.custom) == ('001', '002', 'custom', '47k')
@pytest.mark.parametrize('mode,custom,expected', [
('inherit', '', 'part_label'),
('preserve', '', 'preserve'),
('part_label', '', 'part_label'),
('part_number', '', 'part_number'),
('custom', '47k', 'custom'),
])
def test_record_value_source_roundtrips_and_compiles_with_task_default(tmp_path, mode, custom, expected):
path = write_bom_template(tmp_path / 'values.xlsx', ({
'old_part_number': '001', 'new_part_number': '002', 'refdes': 'R1',
'value_mode': mode, 'custom_value': custom,
},))
book = load_workbook(path)
try:
assert book.active['L1'].comment is not None
assert book.active['L1'].font.bold
finally:
book.close()
target = ComponentTarget(tmp_path / 'Board.prj', 'MAIN', '1', 'u1', 'R1', '001', '10nF')
records = read_bom_records(path)
rows = match_bom_records((TargetPlanRow(target),), records)
rule, = compile_target_rules(rows, default_value_mode='part_label')
assert (rows[0].value_mode, rule.value_mode, rule.custom_value) == (mode, expected, custom)
@@ -282,6 +282,7 @@ def test_service_multi_file_preview_normalizes_migrated_routes_before_cad(tmp_pa
def fake_inspect(_service, _task_id, path, **_options): def fake_inspect(_service, _task_id, path, **_options):
seen['path'] = Path(path) seen['path'] = Path(path)
assert seen['path'].is_file()
return report_module.BomImportReport( return report_module.BomImportReport(
rows=(report_module.BomRowResult(2, str(current), 'Board', 'passed', 'validated'),), rows=(report_module.BomRowResult(2, str(current), 'Board', 'passed', 'validated'),),
projects=(report_module.BomProjectResult(str(current), 'Board', 'passed', (2,)),), projects=(report_module.BomProjectResult(str(current), 'Board', 'passed', (2,)),),
@@ -292,7 +293,8 @@ def test_service_multi_file_preview_normalizes_migrated_routes_before_cad(tmp_pa
report = service.preview_bom_sources(task.task_id, (source,)) report = service.preview_bom_sources(task.task_id, (source,))
assert report.rows[0].reason.startswith('validated;来源:') assert report.rows[0].reason.startswith('validated;来源:')
assert 'migrated.xlsx' in report.rows[0].reason and '第 2 行' in report.rows[0].reason assert 'migrated.xlsx' in report.rows[0].reason and '第 2 行' in report.rows[0].reason
assert seen['path'].is_file() assert not seen['path'].exists()
assert report.source_map_path == seen['path'].parent / 'source-map.json'
assert (seen['path'].parent / 'source-map.json').is_file() assert (seen['path'].parent / 'source-map.json').is_file()
finally: finally:
store.close() store.close()
@@ -398,8 +400,8 @@ def test_multi_source_import_reuses_supervised_inspection(tmp_path, monkeypatch)
_write_bom(source, 'Board.prj', r'Z:\old\Board.prj', 'R1') _write_bom(source, 'Board.prj', r'Z:\old\Board.prj', 'R1')
phases = [] phases = []
def supervised(operation, arguments, **controls): def supervised(operation, arguments, **controls):
assert operation == 'inspect_bom_batch' assert operation == 'preview_bom_sources'
assert Path(arguments['path']).is_file() assert arguments['sources'] == (source,)
controls['on_phase']('validating', 'normalized') controls['on_phase']('validating', 'normalized')
return BomImportReport((), ()) return BomImportReport((), ())
monkeypatch.setattr(service, '_supervise', supervised) monkeypatch.setattr(service, '_supervise', supervised)
+27 -2
View File
@@ -236,12 +236,14 @@ def test_cancellation_during_excel_parsing_keeps_operation_cancelled(batch, tmp_
def test_ui_report_is_persistent_and_failure_does_not_stage(batch, tmp_path, qtbot, monkeypatch): def test_ui_report_is_persistent_and_failure_does_not_stage(batch, tmp_path, qtbot, monkeypatch):
from types import SimpleNamespace
from PySide6.QtWidgets import QFileDialog from PySide6.QtWidgets import QFileDialog
from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow
service, store, tasks, targets, calls = batch service, store, tasks, targets, calls = batch
# Exercise the real async import callback without any blocking message box. # Exercise the real async import callback without any blocking message box.
monkeypatch.setattr(service, 'inspect_bom_batch', lambda *args, **kwargs: inspect_batch( preview = service.preview_bom_sources
service, *args, **kwargs), raising=False) monkeypatch.setattr(service, 'preview_bom_sources', lambda *args, **kwargs: preview(
*args, runtime=SimpleNamespace(bind_logs=lambda *_: None), **kwargs))
records = list(records_for(tasks, targets)) records = list(records_for(tasks, targets))
records[1]['new_part_number'] = '' records[1]['new_part_number'] = ''
path = write_bom_template(tmp_path / 'bad-ui.xlsx', records) path = write_bom_template(tmp_path / 'bad-ui.xlsx', records)
@@ -249,6 +251,7 @@ def test_ui_report_is_persistent_and_failure_does_not_stage(batch, tmp_path, qtb
qtbot.addWidget(window) qtbot.addWidget(window)
window.open_task(tasks[0].task_id) window.open_task(tasks[0].task_id)
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy) qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
qtbot.waitUntil(window.plan_panel.import_bom_button.isEnabled)
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: ([str(path)], 'Excel (*.xlsx)')) monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: ([str(path)], 'Excel (*.xlsx)'))
window.plan_panel.import_bom_button.click() window.plan_panel.import_bom_button.click()
qtbot.waitUntil(lambda: not window._workers) qtbot.waitUntil(lambda: not window._workers)
@@ -264,10 +267,14 @@ def test_ui_report_is_persistent_and_failure_does_not_stage(batch, tmp_path, qtb
def test_ui_cleanup_failure_keeps_details_and_retry_clears_previous_result(batch, tmp_path, qtbot, monkeypatch): def test_ui_cleanup_failure_keeps_details_and_retry_clears_previous_result(batch, tmp_path, qtbot, monkeypatch):
from types import SimpleNamespace
from PySide6.QtWidgets import QFileDialog from PySide6.QtWidgets import QFileDialog
from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
service, store, tasks, targets, calls = batch service, store, tasks, targets, calls = batch
preview = service.preview_bom_sources
monkeypatch.setattr(service, 'preview_bom_sources', lambda *args, **kwargs: preview(
*args, runtime=SimpleNamespace(bind_logs=lambda *_: None), **kwargs))
preparer = service.for_project_task(tasks[0].task_id)._quick_run_preparer preparer = service.for_project_task(tasks[0].task_id)._quick_run_preparer
original_reader = preparer.collect_inventory original_reader = preparer.collect_inventory
def failed_reader(*args, **kwargs): def failed_reader(*args, **kwargs):
@@ -278,6 +285,7 @@ def test_ui_cleanup_failure_keeps_details_and_retry_clears_previous_result(batch
qtbot.addWidget(window) qtbot.addWidget(window)
window.open_task(tasks[0].task_id) window.open_task(tasks[0].task_id)
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy) qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
qtbot.waitUntil(window.plan_panel.import_bom_button.isEnabled)
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: ([str(path)], 'Excel (*.xlsx)')) monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: ([str(path)], 'Excel (*.xlsx)'))
panel = window.plan_panel panel = window.plan_panel
panel.import_bom_button.click() panel.import_bom_button.click()
@@ -287,6 +295,7 @@ def test_ui_cleanup_failure_keeps_details_and_retry_clears_previous_result(batch
assert 'UI-CLEANUP-1' in panel.report_text and panel.report_toggle.isVisibleTo(panel) assert 'UI-CLEANUP-1' in panel.report_text and panel.report_toggle.isVisibleTo(panel)
assert not panel._bom_batches and not calls assert not panel._bom_batches and not calls
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy) qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
qtbot.waitUntil(panel.import_bom_button.isEnabled)
preparer.collect_inventory = original_reader preparer.collect_inventory = original_reader
panel.import_bom_button.click() panel.import_bom_button.click()
assert panel.last_bom_report is None and not panel.report_toggle.isVisibleTo(panel) assert panel.last_bom_report is None and not panel.report_toggle.isVisibleTo(panel)
@@ -296,3 +305,19 @@ def test_ui_cleanup_failure_keeps_details_and_retry_clears_previous_result(batch
assert all(not store.list_target_plan(task.task_id) for task in tasks) assert all(not store.list_target_plan(task.task_id) for task in tasks)
panel.discard_all() panel.discard_all()
window.close() window.close()
def test_ui_report_uses_original_workbook_row_and_links_source_evidence(tmp_path):
from types import SimpleNamespace
from dx_productivity.trial_task_center.bom_import_report import BomImportReport, BomRowResult
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
from dx_productivity.trial_task_center.ui.project_plan import ProjectPlanPanel
report = BomImportReport((BomRowResult(3, 'board.prj', 'Board', 'failed', 'duplicate',
source_file=str(tmp_path / 'second.xlsx'), source_row=5),), (),
source_map_path=tmp_path / 'source-map.json')
state = SimpleNamespace(report=report, phase=BomImportPhase.FAILED, notice='')
_, text, _ = ProjectPlanPanel._format_bom_report(state)
assert 'second.xlsx · Excel 第 5 行' in text
assert 'Excel 第 3 行' not in text
assert str(report.source_map_path) in text
+126
View File
@@ -0,0 +1,126 @@
import json
import os
from contextlib import closing
import sys
import textwrap
import pytest
from test_bom_batch import batch
def test_real_supervised_sources_roundtrip_origin_metadata_and_preview(batch, tmp_path):
from dx_productivity.trial_task_center.bom_plan import write_bom_template
service, store, tasks, targets, calls = batch
for task in tasks:
store.record_target_inventory(task.task_id, targets[task.task_id])
sources = tuple(write_bom_template(tmp_path / f'source-{index}.xlsx', ({
'project_name': task.name, 'project_path': str(targets[task.task_id][0].project_path),
'old_part_number': 'OLD', 'new_part_number': 'NEW', 'refdes': 'R1', 'value_mode': 'part_number',
},)) for index, task in enumerate(tasks))
service._supervised = True
report = service.preview_bom_sources(tasks[0].task_id, sources)
assert report.passed_rows == 2 and report.preview is not None
assert tuple(row.source_file for row in report.rows) == tuple(str(path.resolve()) for path in sources)
assert all(row.source_row == 2 for row in report.rows)
assert report.source_map_path.is_file()
assert not report.source_map_path.with_name('normalized-bom.xlsx').exists()
assert all(project.rows[0].value_mode == 'part_number' for project in report.preview.projects)
assert not calls
assert all(not row.enabled for task in tasks for row in store.list_target_plan(task.task_id))
def test_bom_sources_run_in_one_worker_with_import_controls(tmp_path, monkeypatch):
"""Exercise real request admission and resource release, without opening CAD."""
from dx_productivity.trial_task_center import batch_supervisor
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
bootstrap = textwrap.dedent('''
import os, sys
from pathlib import Path
from dx_productivity.trial_task_center.batch_worker import run
from dx_productivity.trial_task_center.bom_import_report import BomImportReport, BomRowResult
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
from dx_productivity.trial_task_center.service import TaskCenterService
def business(self, task_id, sources, *, runtime, stop_event, on_progress, on_phase):
assert task_id == 'task'
assert type(sources) is tuple and len(sources) == 2
assert all(isinstance(source, Path) for source in sources)
assert tuple(source.name for source in sources) == ('first.xlsx', 'second.xlsx')
assert runtime.kind == 'bom_import'
assert runtime.stop_event is stop_event
on_progress('read both sources')
on_phase(BomImportPhase.READING, 'reading sources')
return BomImportReport((BomRowResult(2, str(os.getpid()), 'Board', 'passed'),), ())
TaskCenterService.preview_bom_sources = business
run(Path(sys.argv[1]))
''')
monkeypatch.setattr(batch_supervisor, '_command',
lambda request: (sys.executable, '-c', bootstrap, str(request)))
progress, phases = [], []
with closing(TaskStore(tmp_path / 'task-center.db')) as store:
service = TaskCenterService(store, configure_logging(tmp_path))
report = batch_supervisor.run_supervised(service, 'preview_bom_sources',
{'task_id': 'task', 'sources': (tmp_path / 'first.xlsx', tmp_path / 'second.xlsx')},
on_progress=progress.append, on_phase=lambda phase, message: phases.append(phase))
assert report.rows[0].project_name == 'Board'
assert report.rows[0].project_path != str(os.getpid())
assert progress == ['read both sources']
assert phases == [BomImportPhase.READING, BomImportPhase.RELEASING]
directory = next((tmp_path / 'batch-resources').iterdir())
journal = json.loads((directory / 'supervisor.json').read_text(encoding='utf-8'))
assert journal['state'] == 'finished'
assert journal['operation'] == 'preview_bom_sources'
assert journal['task_ids'] == ['task']
batch_supervisor.assert_supervisor_idle(tmp_path)
def test_bom_sources_supervisor_rejects_wrong_result_type():
from dx_productivity.trial_task_center.supervisor_protocol import encode
from dx_productivity.trial_task_center.supervisor_result import validated_outcome
with pytest.raises(ValueError, match='result type'):
validated_outcome('preview_bom_sources',
{'business_succeeded': True, 'status': 'ok', 'result': encode(())})
@pytest.mark.parametrize('shutdown_failed', [False, True])
@pytest.mark.parametrize('business_succeeded', [False, True])
def test_bom_sources_cleanup_preserves_rows_without_preview(
tmp_path, monkeypatch, shutdown_failed, business_succeeded):
from dx_productivity.trial_task_center import batch_resources
from dx_productivity.trial_task_center.batch_supervisor import _finish_response
from dx_productivity.trial_task_center.bom_batch import BomBatchPreview
from dx_productivity.trial_task_center.bom_import_report import BomImportReport, BomRowResult
from dx_productivity.trial_task_center.errors import TaskCenterError
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.supervisor_protocol import encode, encode_error
report = BomImportReport((BomRowResult(4, 'board.prj', 'Board', 'passed'),), (),
BomBatchPreview('task', (), ()))
journal = {'operation': 'preview_bom_sources', 'batch_id': 'a' * 32, 'state': 'running'}
directory = tmp_path / 'batch-resources' / journal['batch_id']
if shutdown_failed:
batch_resources.write_record(directory / 'session.json',
{'cad_shutdown': {'orderly_shutdown': False}})
pending = () if shutdown_failed else ({'session_id': journal['batch_id']},)
monkeypatch.setattr(batch_resources, 'cleanup_pending_resources', lambda *args, **kwargs: pending)
response = {'business_succeeded': business_succeeded, 'status': 'error',
'error': encode_error(TaskCenterError('batch_resources_pending', 'Quit failed'))}
if business_succeeded:
response['result'] = encode(report)
else:
primary = TaskCenterError('inventory_cleanup', 'Reader failed')
primary.bom_report = report
response['operation_error'] = encode_error(primary)
with pytest.raises(TaskCenterError) as caught:
_finish_response(tmp_path, journal, response, configure_logging(tmp_path))
assert caught.value.code == ('application_shutdown_failed' if shutdown_failed else 'batch_resources_pending')
assert caught.value.bom_report.rows == report.rows
assert caught.value.bom_report.preview is None
if business_succeeded:
assert caught.value.completed_result.preview is None
else:
assert caught.value.primary_error.code == 'inventory_cleanup'
+1 -1
View File
@@ -152,7 +152,7 @@ def test_blank_bom_can_be_downloaded_without_task_or_inventory(qtbot, tmp_path,
qtbot.waitUntil(lambda: not window._workers and output.exists()) qtbot.waitUntil(lambda: not window._workers and output.exists())
workbook = load_workbook(output) workbook = load_workbook(output)
try: try:
assert tuple(cell.value for cell in workbook.active[1]) == BOM_HEADERS assert tuple(cell.value for cell in workbook.active[1]) == BOM_HEADERS + ('value_mode',)
# The first editing row is explicitly text-formatted to preserve zeroes. # The first editing row is explicitly text-formatted to preserve zeroes.
assert workbook.active.max_row == 2 assert workbook.active.max_row == 2
assert all(cell.value is None for cell in workbook.active[2]) assert all(cell.value is None for cell in workbook.active[2])