Initial import of DxDesigner part replacement tool (r21)
This commit is contained in:
commit
ffe066cb1e
151 files changed
+58745
No files matched your search
@@ -0,0 +1,317 @@
|
||||
from pathlib import Path
|
||||
import json
|
||||
import subprocess
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.errors import TaskCenterError
|
||||
from dx_productivity.trial_task_center.quick_run import QuickRunPreparer
|
||||
from dx_productivity.trial_task_center.service import TaskCenterService
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
from dx_productivity.trial_task_center.ui import main_window
|
||||
from dx_productivity.trial_task_center.ui.main_window import (
|
||||
TrialTaskCenterWindow,
|
||||
_aligned_log_line,
|
||||
_format_task_log_lines,
|
||||
_nc_sync_log_lines,
|
||||
)
|
||||
|
||||
|
||||
def test_nc_sync_evidence_is_formatted_for_operator_log():
|
||||
message = (
|
||||
'Native replacement passed. NC synced: '
|
||||
'R1 R0603/NC -> R0805/NC (detected /NC and synchronized); '
|
||||
'C2 C0603/NC -> C0805/NC (detected /NC and synchronized)'
|
||||
)
|
||||
assert _nc_sync_log_lines(message) == (
|
||||
'位号 R1 R0603/NC -> R0805/NC,识别到 NC,已同步',
|
||||
'位号 C2 C0603/NC -> C0805/NC,识别到 NC,已同步',
|
||||
)
|
||||
|
||||
|
||||
def test_task_event_json_is_rendered_as_readable_operator_text():
|
||||
line = json.dumps({
|
||||
'timestamp_utc': '2026-09-06T10:55:34.796685Z',
|
||||
'level': 'INFO',
|
||||
'operation': 'value_policy_changed',
|
||||
'message': '',
|
||||
'details': {'copy_part_number_to_value': True},
|
||||
})
|
||||
|
||||
assert _format_task_log_lines((line, 'legacy line')) == (
|
||||
'2026-09-06 10:55:34 | INFO | value_policy_changed | Value 选项已更新:替换后写入目标 Part Label。',
|
||||
' | INFO | legacy | legacy line',
|
||||
)
|
||||
|
||||
|
||||
def test_log_operation_column_stays_aligned_for_long_names():
|
||||
line = _aligned_log_line(
|
||||
'2026-09-10 12:34:56',
|
||||
'WARNING',
|
||||
'operation_name_that_is_longer_than_the_column',
|
||||
'内容',
|
||||
)
|
||||
|
||||
assert line.index(' | 内容') == 19 + 3 + 7 + 3 + 28
|
||||
assert 'operation_name_that_is_lo...' in line
|
||||
|
||||
|
||||
def test_running_batch_rebuilds_project_table_only_at_project_boundaries(qtbot, tmp_path, monkeypatch):
|
||||
store = TaskStore(tmp_path / 'tasks.db')
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
|
||||
task = service.create_task('Refresh boundary')
|
||||
window = TrialTaskCenterWindow(service)
|
||||
qtbot.addWidget(window)
|
||||
window.open_task(task.task_id)
|
||||
window._active_quick_run_task_id = task.task_id
|
||||
window._quick_run_elapsed.start()
|
||||
window._batch_rendered = (task.task_id, 0)
|
||||
renders = []
|
||||
monkeypatch.setattr(window, '_populate_projects', lambda: renders.append('render'))
|
||||
monkeypatch.setattr(window, '_update_gates', lambda: None)
|
||||
|
||||
progress = SimpleNamespace(
|
||||
completed_items=1,
|
||||
total_items=3,
|
||||
no_match_items=1,
|
||||
completed_project_paths=(),
|
||||
)
|
||||
service._batch_feedback = (task.task_id, progress, '正在处理')
|
||||
window._update_quick_run_elapsed()
|
||||
assert renders == []
|
||||
assert '已处理 1/3 项' in window.run_panel.queue_summary.text()
|
||||
|
||||
progress.completed_items = 2
|
||||
progress.completed_project_paths = (tmp_path / 'project.prj',)
|
||||
service._batch_feedback = (task.task_id, progress, '工程已完成')
|
||||
window._update_quick_run_elapsed()
|
||||
assert renders == ['render']
|
||||
window.close()
|
||||
store.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('code', ['quick_run_session', 'quick_run_preflight'])
|
||||
def test_pre_mutation_failure_does_not_keep_running_label_or_claim_possible_writes(qtbot, tmp_path, code):
|
||||
store = TaskStore(tmp_path / 'tasks.db')
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
|
||||
task = service.create_task('Diagnostic')
|
||||
window = TrialTaskCenterWindow(service)
|
||||
qtbot.addWidget(window)
|
||||
window.open_task(task.task_id)
|
||||
window.run_panel.execution_status.setText('正在执行')
|
||||
window._show_error(TaskCenterError(code, 'test preflight'))
|
||||
assert '执行已停止' in window.run_panel.execution_status.text()
|
||||
assert '尚未开始替换' in window.run_panel.execution_status.text()
|
||||
assert '图页可能已被修改' not in window.run_panel.operator_band.text()
|
||||
assert '不能假定图页未修改' not in window.run_panel.operator_band.text()
|
||||
window.close()
|
||||
store.close()
|
||||
|
||||
|
||||
def test_backup_failure_exposes_one_shot_continue_button(qtbot, tmp_path, monkeypatch):
|
||||
root = tmp_path / 'projects'
|
||||
root.mkdir()
|
||||
(root / 'one.prj').touch()
|
||||
store = TaskStore(tmp_path / 'tasks.db')
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
|
||||
task = service.create_task('Backup override')
|
||||
project = service.scan_projects(task.task_id, root)[0]
|
||||
service.set_project_selected(project.project_id, True)
|
||||
service.add_rule(task.task_id, 'OLD', 'NEW')
|
||||
window = TrialTaskCenterWindow(service)
|
||||
qtbot.addWidget(window)
|
||||
window.open_task(task.task_id)
|
||||
|
||||
assert window.run_panel.continue_without_backup_button.isHidden()
|
||||
window._show_error(TaskCenterError('quick_run_backup', 'simulated backup failure'))
|
||||
assert not window.run_panel.continue_without_backup_button.isHidden()
|
||||
assert window.run_panel.continue_without_backup_button.isEnabled()
|
||||
|
||||
dispatched = []
|
||||
monkeypatch.setattr(
|
||||
QMessageBox,
|
||||
'warning',
|
||||
lambda *_args, **_kwargs: QMessageBox.StandardButton.Yes,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
window,
|
||||
'_start_quick_run_queue',
|
||||
lambda task_id, expected_item, **options: dispatched.append((task_id, expected_item, options)),
|
||||
)
|
||||
window._continue_without_backup()
|
||||
|
||||
assert dispatched[0][0] == task.task_id
|
||||
assert dispatched[0][1][1:] == ('OLD', 'NEW', 'preserve')
|
||||
assert dispatched[0][2] == {'continue_after_backup_failure': True}
|
||||
window.close()
|
||||
store.close()
|
||||
|
||||
|
||||
def test_non_backup_failure_does_not_expose_backup_override(qtbot, tmp_path):
|
||||
store = TaskStore(tmp_path / 'tasks.db')
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
|
||||
task = service.create_task('No backup override')
|
||||
window = TrialTaskCenterWindow(service)
|
||||
qtbot.addWidget(window)
|
||||
window.open_task(task.task_id)
|
||||
|
||||
window._show_error(TaskCenterError('quick_run_preflight', 'simulated preflight failure'))
|
||||
|
||||
assert window.run_panel.continue_without_backup_button.isHidden()
|
||||
window.close()
|
||||
store.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('displayed', [False, True])
|
||||
@pytest.mark.parametrize('state', ['no_match', 'restored_ok'])
|
||||
def test_confirmation_matches_execution_and_zero_match_is_a_completed_skip(qtbot, tmp_path, monkeypatch, displayed, state):
|
||||
data = tmp_path / 'data'
|
||||
executed = []
|
||||
messages = []
|
||||
|
||||
def worker(command, timeout):
|
||||
row = (data / 'quick-run/command.tsv').read_text(encoding='utf-8').splitlines()[1].split('\t')
|
||||
executed.append(row[-1])
|
||||
with (data / 'quick-run/status.tsv').open('a', encoding='utf-8') as handle:
|
||||
if state == 'restored_ok':
|
||||
handle.write(f'{row[1]}\tawaiting_native_replace\t\tcaptured\n')
|
||||
terminal = 'saved_ok' if state == 'restored_ok' else state
|
||||
handle.write(f'{row[1]}\t{terminal}\tbatch_closed\tcurrent sheet checked\n')
|
||||
return subprocess.CompletedProcess(command, 0, '', '')
|
||||
|
||||
class ConfirmBox:
|
||||
Icon = QMessageBox.Icon
|
||||
ButtonRole = QMessageBox.ButtonRole
|
||||
|
||||
def __init__(self, icon, title, text, parent=None):
|
||||
messages.append(text)
|
||||
self.buttons = []
|
||||
|
||||
def addButton(self, text, role):
|
||||
button = object()
|
||||
self.buttons.append(button)
|
||||
return button
|
||||
|
||||
def exec(self):
|
||||
return 0
|
||||
|
||||
def clickedButton(self):
|
||||
return self.buttons[0]
|
||||
|
||||
source = Path(__file__).resolve().parents[2] / 'integration/dxdesigner795/QuickPartReplace.vbs'
|
||||
preparer = QuickRunPreparer(data, script_source=source, process_runner=worker,
|
||||
cscript_resolver=lambda: Path('C:/Windows/SysWOW64/cscript.exe'),
|
||||
sheet_discoverer=lambda project: (('Main', '1'),))
|
||||
store = TaskStore(tmp_path / 'tasks.db')
|
||||
service = TaskCenterService(store, configure_logging(data), quick_run_preparer=preparer)
|
||||
task = service.create_task('Policy consistency')
|
||||
project_root = tmp_path / 'projects'
|
||||
project_root.mkdir()
|
||||
(project_root / 'one.prj').touch()
|
||||
project = service.scan_projects(task.task_id, project_root)[0]
|
||||
service.set_project_selected(project.project_id, True)
|
||||
service.add_rule(task.task_id, 'OLD', 'NEW')
|
||||
service.set_copy_part_number_to_value(task.task_id, displayed)
|
||||
window = TrialTaskCenterWindow(service)
|
||||
qtbot.addWidget(window)
|
||||
window.show()
|
||||
window.open_task(task.task_id)
|
||||
qtbot.waitUntil(lambda: not window._workers)
|
||||
other_store = TaskStore(tmp_path / 'tasks.db')
|
||||
other = TaskCenterService(other_store, configure_logging(data))
|
||||
try:
|
||||
other.set_copy_part_number_to_value(task.task_id, not displayed)
|
||||
assert window.run_panel.copy_value_check.isChecked() is displayed
|
||||
monkeypatch.setattr(main_window, 'QMessageBox', ConfirmBox)
|
||||
window._prepare_quick_run()
|
||||
qtbot.waitUntil(lambda: not window._workers, timeout=10000)
|
||||
expected = 'preserve' if displayed else 'part_label'
|
||||
shown = '保留原 Value,不额外改写' if displayed else '复制目标 Part Label'
|
||||
assert shown in messages[0]
|
||||
assert '枚举工程内全部原理图页' in messages[0]
|
||||
assert '图页清单在替换前固定' in messages[0]
|
||||
assert executed == [expected]
|
||||
assert service.quick_run_progress(task.task_id).queue_complete
|
||||
if state == 'no_match':
|
||||
assert '成功 0,失败 0,未命中 1' in window.run_panel.operator_band.text()
|
||||
assert '全工程图页队列完成' in window.run_panel.operator_band.text()
|
||||
assert window.run_panel.operator_band.property('state') == 'success'
|
||||
assert window.run_panel.execution_status.text() == '队列已完成'
|
||||
assert '未命中 1' in window.run_panel.queue_summary.text()
|
||||
assert '全工程图页队列完成' in window.run_panel.operator_band.text()
|
||||
assert window.project_table.item(0, 3).text() == '未命中跳过'
|
||||
assert '未作替换' in window.project_table.item(0, 4).text()
|
||||
else:
|
||||
assert '成功 1,失败 0,未命中 0' in window.run_panel.operator_band.text()
|
||||
window.open_task(task.task_id)
|
||||
qtbot.waitUntil(lambda: not window._workers)
|
||||
assert '尚未' not in window.run_panel.operator_band.text()
|
||||
assert '成功项已保存' in window.run_panel.operator_band.text()
|
||||
assert '已保存' in window.project_table.item(0, 4).text()
|
||||
finally:
|
||||
window.close()
|
||||
other_store.close()
|
||||
store.close()
|
||||
|
||||
|
||||
def test_closing_during_execution_requests_boundary_pause(qtbot, tmp_path):
|
||||
from threading import Event
|
||||
from PySide6.QtGui import QCloseEvent
|
||||
|
||||
store = TaskStore(tmp_path / 'tasks.db')
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
|
||||
window = TrialTaskCenterWindow(service)
|
||||
qtbot.addWidget(window)
|
||||
window.show()
|
||||
qtbot.waitUntil(lambda: not window._workers)
|
||||
stop = Event()
|
||||
window._batch_stop = stop
|
||||
window._button_generations[window.run_panel.quick_run_button] = 1
|
||||
close_event = QCloseEvent()
|
||||
try:
|
||||
window.closeEvent(close_event)
|
||||
assert not close_event.isAccepted()
|
||||
assert stop.is_set()
|
||||
assert not window.run_panel.stop_queue_button.isEnabled()
|
||||
assert '正在完成当前项' in window.run_panel.stop_queue_button.text()
|
||||
finally:
|
||||
window._button_generations.clear()
|
||||
window._batch_stop = None
|
||||
window.close()
|
||||
store.close()
|
||||
|
||||
|
||||
def test_manual_skip_is_disabled_while_automatic_worker_runs(qtbot, tmp_path):
|
||||
from threading import Event
|
||||
|
||||
root = tmp_path / 'projects'
|
||||
root.mkdir()
|
||||
(root / 'one.prj').touch()
|
||||
store = TaskStore(tmp_path / 'tasks.db')
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
|
||||
task = service.create_task('Automatic queue')
|
||||
project = service.scan_projects(task.task_id, root)[0]
|
||||
service.set_project_selected(project.project_id, True)
|
||||
service.add_rule(task.task_id, 'OLD', 'NEW')
|
||||
window = TrialTaskCenterWindow(service)
|
||||
qtbot.addWidget(window)
|
||||
window.open_task(task.task_id)
|
||||
window.project_table.selectRow(0)
|
||||
window._batch_stop = Event()
|
||||
window._button_generations[window.run_panel.quick_run_button] = 1
|
||||
try:
|
||||
window._update_gates()
|
||||
assert not window.skip_button.isEnabled()
|
||||
assert not window.retry_button.isEnabled()
|
||||
assert window.project_table.isEnabled()
|
||||
assert window.mapping_table.isEnabled()
|
||||
assert not window.project_table.cellWidget(0, 0).isEnabled()
|
||||
assert not window.mapping_table.cellWidget(0, 4).findChild(main_window.QCheckBox).isEnabled()
|
||||
finally:
|
||||
window._button_generations.clear()
|
||||
window._batch_stop = None
|
||||
window.close()
|
||||
store.close()
|
||||
Reference in new issue
Block a user