import json import pytest from pathlib import Path from mentor_pdf_export.cli import main def test_self_check_writes_machine_readable_report(tmp_path): report=tmp_path / "check.json" assert main(["--self-check","--report",str(report)]) == 0 data=json.loads(report.read_text(encoding="utf-8")) from mentor_pdf_export import __version__ assert data["version"] == __version__ assert "engine_available" in data def test_invalid_cli_export_returns_nonzero(tmp_path): assert main(["--export",str(tmp_path / "missing.prj"),"--output",str(tmp_path / "pdf")]) != 0 def test_report_never_overwrites_project_even_for_self_check(project_factory): project=project_factory() before=project.read_bytes() assert main(["--self-check","--report",str(project)]) == 2 assert project.read_bytes()==before def test_report_inside_source_is_rejected_before_export(project_factory,tmp_path,monkeypatch): from mentor_pdf_export import cli from mentor_pdf_export.exporter import Exporter from pypdf import PdfWriter project=project_factory() protected=project.parent/"settings.json";protected.write_text("original") engine=tmp_path/"sch2pdf.exe";engine.write_bytes(b"fake") def runner(args,*rest,env=None): 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 monkeypatch.setattr(cli,"Exporter",lambda engine,runs:Exporter(engine,runs,runner=runner)) rc=main(["--export",str(project),"--engine",str(engine),"--output",str(tmp_path/"pdfs"),"--runs-root",str(tmp_path/"runs"),"--report",str(protected)]) assert rc==2 assert protected.read_text()=="original" assert not (tmp_path/"pdfs").exists() def test_queue_destinations_protect_every_source_before_first_export(project_factory,tmp_path,monkeypatch): from mentor_pdf_export import cli from mentor_pdf_export.exporter import Exporter from mentor_pdf_export.workspace import fingerprint from pypdf import PdfWriter a=project_factory("a");b=project_factory("b") engine=tmp_path/"sch2pdf.exe";engine.write_bytes(b"fake") before=fingerprint(b.parent) def runner(args,*rest,env=None): 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 monkeypatch.setattr(cli,"Exporter",lambda engine,runs:Exporter(engine,runs,runner=runner)) rc=main(["--export",str(a),str(b),"--engine",str(engine),"--output",str(b.parent/"database"),"--runs-root",str(tmp_path/"runs")]) assert rc==2 assert fingerprint(b.parent)==before assert not (tmp_path/"runs").exists() def test_self_check_cannot_overwrite_unowned_json(tmp_path): report=tmp_path/"critical.json";report.write_text('{"user_setting":123}') before=report.read_bytes() assert main(["--self-check","--report",str(report)]) == 2 assert report.read_bytes()==before def test_self_check_can_refresh_its_own_report(tmp_path): report=tmp_path/"self-check.json" assert main(["--self-check","--report",str(report)]) == 0 assert main(["--self-check","--report",str(report)]) == 0 def test_cli_accepts_explicit_print_and_font_profiles(tmp_path): report=tmp_path/"style-check.json" assert main(["--self-check","--font-mapping","arial","--print-theme","high_contrast","--report",str(report)])==0 def test_cli_rejects_library_replacement_in_direct_mode(tmp_path): assert main(["--self-check","--library",str(tmp_path/"matching.lmc")])==2 @pytest.mark.parametrize('strict',[False,True]) def test_cli_native_update_warning_exit_and_strict_fallback(project_factory,tmp_path,monkeypatch,strict): from mentor_pdf_export import cli from mentor_pdf_export.exporter import Exporter from pypdf import PdfWriter project=project_factory();engine=tmp_path/'sch2pdf.exe';engine.write_bytes(b'fake') def runner(args,*rest,env=None): writer=PdfWriter();writer.add_blank_page(width=500,height=400) with Path(args[args.index('-a')+1]).open('wb') as out:writer.write(out) (project.parent/'database/icdb.dat').write_bytes(b'native update');return 0 monkeypatch.setattr(cli,'Exporter',lambda engine,runs:Exporter(engine,runs,runner=runner)) report=tmp_path/'report.json' args=['--export',str(project),'--engine',str(engine),'--output',str(tmp_path/'pdfs'),'--runs-root',str(tmp_path/'runs'),'--report',str(report)] if strict:args.append('--strict-source') assert main(args)==(1 if strict else 0) result=json.loads(report.read_text(encoding='utf-8'))['results'][0] assert result['status']==('uncertain' if strict else 'success') if not strict:assert result['warnings'] and Path(result['output']).is_file() def test_cli_strict_source_flag_reaches_ui(qtbot,tmp_path,monkeypatch): from mentor_pdf_export import ui original=ui.MainWindow;windows=[] def capture(*args,**kwargs): window=original(*args,**kwargs);qtbot.addWidget(window);windows.append(window);return window monkeypatch.setattr(ui,'MainWindow',capture) assert main(['--strict-source','--ui-smoke',str(tmp_path/'ui.png')])==0 assert windows[0].strict_source_check.isChecked() @pytest.mark.parametrize('flags,exit_code',[ (['--font-mapping','preserve'],0), (['--font-mapping','preserve','--no-legacy-gbk'],0), (['--font-mapping','preserve','--legacy-gbk'],2), ]) def test_cli_preserve_normalizes_implicit_not_explicit_gbk(project_factory,tmp_path,monkeypatch,flags,exit_code): from mentor_pdf_export import cli from mentor_pdf_export.exporter import Exporter from pypdf import PdfWriter project=project_factory();engine=tmp_path/'sch2pdf.exe';engine.write_bytes(b'fake') def runner(args,*rest,env=None): 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 monkeypatch.setattr(cli,'Exporter',lambda e,r:Exporter(e,r,runner=runner)) rc=main(['--export',str(project),'--engine',str(engine),'--output',str(tmp_path/'out'),'--runs-root',str(tmp_path/'runs'),*flags]) assert rc==exit_code