from pathlib import Path from pypdf import PdfWriter from mentor_pdf_export.models import ExportOptions from mentor_pdf_export.scanner import read_project from mentor_pdf_export.exporter import Exporter from mentor_pdf_export.workers import BatchWorker def test_batch_pre_record_failure_stops_before_engine(qapp,project_factory,tmp_path,monkeypatch): from mentor_pdf_export import workers called=[] def runner(*args):called.append(True);return 0 def full(*args):raise OSError("disk full") monkeypatch.setattr(workers,"write_json",full) info=read_project(project_factory()) worker=BatchWorker([(0,info)],tmp_path/"pdfs",ExportOptions(),Exporter(Path("fake.exe"),tmp_path/"runs",runner=runner)) worker.run() assert not called assert not worker.results def test_late_batch_record_failure_keeps_result_and_pre_record(qapp,project_factory,tmp_path,monkeypatch): from mentor_pdf_export import workers original=workers.write_json count=0 def fail_later(path,data): nonlocal count count+=1 if count>1:raise OSError("disk full later") original(path,data) monkeypatch.setattr(workers,"write_json",fail_later) def runner(args,*rest): writer=PdfWriter();writer.add_blank_page(width=500,height=400) with Path(args[args.index("-a")+1]).open("wb") as out:writer.write(out) return 0 info=read_project(project_factory()) worker=BatchWorker([(0,info)],tmp_path/"pdfs",ExportOptions(),Exporter(Path("fake.exe"),tmp_path/"runs",runner=runner)) texts=[];worker.completed.connect(texts.append) worker.run() assert len(worker.results)==1 and worker.results[0].status=="success" assert Path(worker.summary_path).exists() assert "成功 1" in texts[-1] and "记录" in texts[-1]