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
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:
1 parent
8b5509d64f
commit
94a873f929
19 files changed
+619
-67
No files matched your search
@@ -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):
|
||||
from types import SimpleNamespace
|
||||
from PySide6.QtWidgets import QFileDialog
|
||||
from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow
|
||||
service, store, tasks, targets, calls = batch
|
||||
# Exercise the real async import callback without any blocking message box.
|
||||
monkeypatch.setattr(service, 'inspect_bom_batch', lambda *args, **kwargs: inspect_batch(
|
||||
service, *args, **kwargs), raising=False)
|
||||
preview = service.preview_bom_sources
|
||||
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[1]['new_part_number'] = ''
|
||||
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)
|
||||
window.open_task(tasks[0].task_id)
|
||||
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)'))
|
||||
window.plan_panel.import_bom_button.click()
|
||||
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):
|
||||
from types import SimpleNamespace
|
||||
from PySide6.QtWidgets import QFileDialog
|
||||
from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow
|
||||
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
|
||||
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
|
||||
original_reader = preparer.collect_inventory
|
||||
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)
|
||||
window.open_task(tasks[0].task_id)
|
||||
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)'))
|
||||
panel = window.plan_panel
|
||||
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 not panel._bom_batches and not calls
|
||||
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
|
||||
qtbot.waitUntil(panel.import_bom_button.isEnabled)
|
||||
preparer.collect_inventory = original_reader
|
||||
panel.import_bom_button.click()
|
||||
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)
|
||||
panel.discard_all()
|
||||
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
|
||||
@@ -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'
|
||||
@@ -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())
|
||||
workbook = load_workbook(output)
|
||||
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.
|
||||
assert workbook.active.max_row == 2
|
||||
assert all(cell.value is None for cell in workbook.active[2])
|
||||
|
||||
Reference in new issue
Block a user