"""Replay saved execution evidence in a copied database; never start CAD.""" import os, sys, json, sqlite3, shutil, hashlib, time, argparse from pathlib import Path os.environ['QT_QPA_PLATFORM'] = 'offscreen' sys.path.insert(0, str(Path.cwd() / 'src')) from PySide6.QtCore import QThreadPool, QPoint, QRect from PySide6.QtGui import QFont, QFontDatabase from PySide6.QtWidgets import QApplication from dx_productivity.trial_task_center.store import TaskStore from dx_productivity.trial_task_center.service import TaskCenterService from dx_productivity.trial_task_center.logging_setup import configure_logging from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow from dx_productivity.trial_task_center.bom_import_report import BomImportReport, BomRowResult, BomProjectResult parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--source', type=Path, required=True) parser.add_argument('--output', type=Path, required=True) args = parser.parse_args() source = args.source.resolve() output = args.output.resolve() output.mkdir(parents=True, exist_ok=False) before = hashlib.sha256((source / 'tasks.db').read_bytes()).hexdigest() with sqlite3.connect((source / 'tasks.db').as_uri() + '?mode=ro', uri=True) as src, sqlite3.connect(output / 'tasks.db') as dst: src.backup(dst) shutil.copytree(source / 'data', output / 'data') app = QApplication([]) fid = QFontDatabase.addApplicationFont(str(Path(os.environ['WINDIR']) / 'Fonts/msyh.ttc')) fonts = QFontDatabase.applicationFontFamilies(fid) if fonts: app.setFont(QFont(fonts[0], 10)) store = TaskStore(output / 'tasks.db') service = TaskCenterService(store, configure_logging(output / 'data')) window = TrialTaskCenterWindow(service, quick_run_confirmer=lambda *_: True, close_plan_confirmer=lambda: 'discard') def idle(): end = time.monotonic() + 60 while window._workers or window._loading or window._task_query.busy or window._context_query.busy or window._log_presenter.query.busy or window._feedback_query.busy: assert time.monotonic() < end app.processEvents() time.sleep(0.01) app.processEvents() report = {} try: window.show() idle() tasks = service.tasks() task = max(tasks, key=lambda t: len(str(store.list_projects(t.task_id)[0].path))) window.open_task(task.task_id) idle() window.mapping_tabs.setCurrentIndex(1) previous = json.loads((source / 'report.json').read_text(encoding='utf-8')) raw = next((s['report'] for s in previous['steps'] if s['stage'] == '02-import-with-failed-row')) bom = BomImportReport(tuple((BomRowResult(**r) for r in raw['rows'])), tuple((BomProjectResult(**r | {'excel_rows': tuple(r['excel_rows'])}) for r in raw['projects'])), None, tuple(raw['errors'])) window.plan_panel._received_bom_report(bom) app.processEvents() window.resize(1280, 880) app.processEvents() window.grab().save(str(output / '01-default-with-import-report.png')) report['default_size'] = [window.width(), window.height()] report['table_content_width'] = window.plan_panel.table.horizontalHeader().length() report['table_viewport_width'] = window.plan_panel.table.viewport().width() window.plan_panel.report_toggle.setChecked(True) window._show_task_log(force=True) idle() window.resize(980, 700) app.processEvents() window.grab().save(str(output / '02-requested-980x700.png')) report['requested_small_size'] = [980, 700] report['actual_small_size'] = [window.width(), window.height()] report['minimum_size_hint'] = [window.minimumSizeHint().width(), window.minimumSizeHint().height()] report['small_table_horizontal_scroll_max'] = window.plan_panel.table.horizontalScrollBar().maximum() report['small_table_viewport_height'] = window.plan_panel.table.viewport().height() report['small_visible_widgets'] = {} for name, w in [('execute', window.run_panel.quick_run_button), ('new_round', window.run_panel.new_round_button), ('bom_template', window.plan_panel.export_bom_button), ('copy_part_label', window.run_panel.copy_value_check)]: r = QRect(w.mapTo(window, QPoint(0, 0)), w.size()) report['small_visible_widgets'][name] = {'visible': w.isVisible(), 'inside_window': window.rect().contains(r), 'rect': [r.x(), r.y(), r.width(), r.height()]} other = store.create_task('UI 审查:另一个批次') folder = output / 'other' folder.mkdir() path = folder / 'review-only.prj' path.touch() project = store.sync_projects(other.task_id, folder, (path,))[0] store.set_project_selected(project.project_id, True) store.bind_project_task(other.task_id, project.project_id, 'ui-review-separate-batch', 0) window.run_panel.log_dialog.grab().save(str(output / '04-execution-results.png')) window.plan_panel.report_dialog.grab().save(str(output / '05-bom-validation-details.png')) report['result_rows'] = window.run_panel.log_dialog.result_view.model().rowCount() before_text = window.plan_panel.report_text window.open_task(other.task_id) idle() report['bom_report_cross_batch'] = {'current_task': window.plan_panel.task_id, 'expected_other_task': other.task_id, 'visible': bool(window.plan_panel.report_dialog and window.plan_panel.report_dialog.isVisible()), 'retained_previous_text': window.plan_panel.report_text == before_text, 'contains_previous_project': 'Audio_D2128' in window.plan_panel.report_text} window.resize(1280, 880) app.processEvents() window.grab().save(str(output / '03-other-batch-report-hidden.png')) assert report['small_table_viewport_height'] >= 100 assert not report['bom_report_cross_batch']['visible'] assert not report['bom_report_cross_batch']['contains_previous_project'] window.open_task(task.task_id) idle() assert window.plan_panel.last_bom_report is bom report['report_restored_on_return'] = True report['source_database_unchanged'] = hashlib.sha256((source / 'tasks.db').read_bytes()).hexdigest() == before (output / 'report.json').write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding='utf-8') print(json.dumps(report, ensure_ascii=False, indent=2), flush=True) finally: idle() window.close() QThreadPool.globalInstance().waitForDone() store.close()