from __future__ import annotations import zipfile import shutil from pathlib import Path from mdhelper.app import parse_arguments, project_and_initial_path, resolve_launch_path from mdhelper.docx_exporter import export_markdown_to_docx from mdhelper.logging_config import setup_logging def test_parse_arguments_accepts_folder_alias() -> None: args = parse_arguments(["--folder", "docs"]) assert args.project_root == "docs" def test_resolve_launch_path_finds_relative_folder_name(tmp_path: Path, monkeypatch) -> None: launch_root = tmp_path / "launcher" docs_root = launch_root / "docs_project" work_root = tmp_path / "work" docs_root.mkdir(parents=True) work_root.mkdir() monkeypatch.chdir(work_root) resolved = resolve_launch_path("docs_project", (Path.cwd(), launch_root)) assert resolved == docs_root.resolve() def test_project_and_initial_path_uses_file_parent(tmp_path: Path) -> None: markdown_file = tmp_path / "guide.md" markdown_file.write_text("# Guide\n", encoding="utf-8") project_root, initial_path = project_and_initial_path(markdown_file, None) assert project_root == tmp_path assert initial_path == markdown_file def test_project_and_initial_path_uses_folder_directly(tmp_path: Path) -> None: project_root, initial_path = project_and_initial_path(tmp_path, None) assert project_root == tmp_path assert initial_path is None def test_export_markdown_to_docx_writes_word_document(tmp_path: Path) -> None: output_path = tmp_path / "guide.docx" export_markdown_to_docx( "# 标题\n\n正文 **加粗** 和 `代码`。\n\n- 项目一\n- 项目二\n\n| A | B |\n| - | - |\n| 1 | 2 |\n", output_path, engine="builtin", ) assert output_path.exists() with zipfile.ZipFile(output_path) as package: names = set(package.namelist()) document_xml = package.read("word/document.xml").decode("utf-8") assert {"[Content_Types].xml", "_rels/.rels", "word/document.xml", "word/styles.xml"} <= names assert "标题" in document_xml assert "项目一" in document_xml assert "" in document_xml def test_export_markdown_to_docx_python_docx_engine(tmp_path: Path) -> None: output_path = tmp_path / "python_docx.docx" export_markdown_to_docx("# 标题\n\n正文\n", output_path, engine="python-docx") assert output_path.exists() with zipfile.ZipFile(output_path) as package: document_xml = package.read("word/document.xml").decode("utf-8") assert "标题" in document_xml def test_export_markdown_to_docx_pandoc_engine_when_available(tmp_path: Path) -> None: if shutil.which("pandoc") is None: return output_path = tmp_path / "pandoc.docx" source_path = tmp_path / "source.md" source_path.write_text("# 标题\n\n正文\n", encoding="utf-8") export_markdown_to_docx(source_path.read_text(encoding="utf-8"), output_path, engine="pandoc", source_path=source_path) assert output_path.exists() with zipfile.ZipFile(output_path) as package: assert "word/document.xml" in package.namelist() def test_setup_logging_uses_configured_writable_user_directory(tmp_path: Path, monkeypatch) -> None: log_dir = tmp_path / "user_logs" monkeypatch.setenv("MDHELPER_LOG_DIR", str(log_dir)) log_path = setup_logging(tmp_path / "install_root") assert log_path == log_dir / "mdhelper.log" assert log_path.exists()