import threading from pathlib import Path from pypdf import PdfWriter from PySide6.QtCore import Qt from mentor_pdf_export.ui import MainWindow from mentor_pdf_export.exporter import Exporter def runner(args, cwd, log_dir, cancel, timeout, on_event=None): writer = PdfWriter(); writer.add_blank_page(width=500,height=400) with Path(args[args.index("-a")+1]).open("wb") as f: writer.write(f) return 0 def make_window(qtbot, tmp_path, factory=None): window = MainWindow(root=str(tmp_path), output=str(tmp_path / "pdfs"), runs_root=tmp_path.parent / (tmp_path.name + "-runs"), exporter_factory=factory) qtbot.addWidget(window); window.show() return window def test_scan_and_toggle_does_not_reset_table(qtbot, project_factory, tmp_path): project_factory() window = make_window(qtbot,tmp_path) qtbot.mouseClick(window.scan_button, Qt.LeftButton) qtbot.waitUntil(lambda: window.table.rowCount() == 1, timeout=10000) qtbot.waitUntil(lambda: not window.busy) item = window.table.item(0,0) assert item.checkState() == Qt.Checked item.setCheckState(Qt.Unchecked) assert window.table.item(0,0) is item assert window.selected_count() == 0 window.select_all() assert window.selected_count() == 1 def test_export_worker_result_and_buttons(qtbot, project_factory, tmp_path): project_factory() def factory(engine, runs): return Exporter(engine,runs,runner=runner) window = make_window(qtbot,tmp_path,factory) engine=tmp_path/"sch2pdf.exe";engine.write_bytes(b"fake") window.engine_edit.setText(str(engine)) window.scan_projects() qtbot.waitUntil(lambda: window.table.rowCount()==1 and not window.busy,timeout=10000) window.start_export() assert window.busy assert not window.start_button.isEnabled() assert not window.options_group.isEnabled() qtbot.waitUntil(lambda: not window.busy,timeout=10000) assert window.table.item(0,3).text()=="成功" assert window.start_button.isEnabled() assert not window.stop_button.isEnabled() assert window.progress.value()==1 assert "1 页" in window.table.item(0,4).text() def test_cancel_signal_is_requested(qtbot, project_factory, tmp_path): project_factory() started=threading.Event() def waiting(args,cwd,log_dir,cancel,timeout,on_event=None): started.set() assert cancel.wait(5) raise InterruptedError("任务已停止") window = make_window(qtbot,tmp_path,lambda engine,runs:Exporter(engine,runs,runner=waiting)) engine=tmp_path/"sch2pdf.exe";engine.write_bytes(b"fake") window.engine_edit.setText(str(engine)) window.scan_projects() qtbot.waitUntil(lambda: window.table.rowCount()==1 and not window.busy,timeout=10000) window.start_export() qtbot.waitUntil(started.is_set,timeout=10000) window.stop_export() qtbot.waitUntil(lambda:not window.busy,timeout=10000) assert window.table.item(0,3).text()=="已停止" def test_failed_only_selection(qtbot, project_factory, tmp_path): project_factory("one");project_factory("two") window = make_window(qtbot,tmp_path) window.scan_projects() qtbot.waitUntil(lambda:window.table.rowCount()==2 and not window.busy,timeout=10000) window.table.item(0,3).setText("失败") window.table.item(1,3).setText("成功") window.select_failed() assert window.table.item(0,0).checkState()==Qt.Checked assert window.table.item(1,0).checkState()==Qt.Unchecked def test_ui_preflights_all_source_paths_before_queue(qtbot,project_factory,tmp_path): project_factory("a");b=project_factory("b") window=make_window(qtbot,tmp_path,lambda engine,runs:Exporter(engine,runs,runner=runner)) engine=tmp_path/"sch2pdf.exe";engine.write_bytes(b"fake") window.engine_edit.setText(str(engine));window.scan_projects() qtbot.waitUntil(lambda:window.table.rowCount()==2 and not window.busy,timeout=10000) window.output_edit.setText(str(b.parent/"database")) window.start_export() # A must not start and write into B while the second source is unprotected. unexpectedly_started=window.busy if window.busy: window.stop_export() qtbot.waitUntil(lambda:not window.busy,timeout=10000) assert not unexpectedly_started assert window.worker is None