feat: standardize Word export and Office conversion
This commit is contained in:
1 parent
4ce2f3a502
commit
485a8fa9d9
12 files changed
+410
-60
No files matched your search
@@ -68,3 +68,29 @@ def test_es_query_routing_is_injected_into_lead_and_allowed_custom_agent_prompts
|
||||
assert "自定义 ES 路由规则" in lead_prompt
|
||||
assert '<skill_routing skill="es_query">' in allowed_custom_prompt
|
||||
assert '<skill_routing skill="es_query">' not in denied_custom_prompt
|
||||
|
||||
|
||||
def test_formal_markdown_rules_are_scoped_to_ordinary_qa(monkeypatch) -> None:
|
||||
config = _app_config(enabled=False)
|
||||
monkeypatch.setattr(prompt_module, "_get_memory_context", lambda *args, **kwargs: "")
|
||||
monkeypatch.setattr(prompt_module, "get_agent_soul", lambda *args, **kwargs: "")
|
||||
monkeypatch.setattr(prompt_module, "get_skills_prompt_section", lambda *args, **kwargs: "")
|
||||
monkeypatch.setattr(prompt_module, "get_deferred_tools_prompt_section", lambda *args, **kwargs: "")
|
||||
monkeypatch.setattr(prompt_module, "_build_acp_section", lambda *args, **kwargs: "")
|
||||
monkeypatch.setattr(prompt_module, "_build_custom_mounts_section", lambda *args, **kwargs: "")
|
||||
monkeypatch.setattr(prompt_module, "_current_time_context", lambda: "")
|
||||
|
||||
ordinary_prompt = prompt_module.apply_prompt_template(app_config=config)
|
||||
writing_prompt = prompt_module.apply_prompt_template(app_config=config, writing_mode=True)
|
||||
notebook_prompt = prompt_module.apply_prompt_template(app_config=config, is_notebook_mode=True)
|
||||
scheduled_prompt = prompt_module.apply_prompt_template(app_config=config, is_scheduled_run=True)
|
||||
|
||||
assert '<ordinary_qa_markdown_format>' in ordinary_prompt
|
||||
assert "## 一、标题" in ordinary_prompt
|
||||
assert "### (一) 标题" in ordinary_prompt
|
||||
assert "#### 1. 标题" in ordinary_prompt
|
||||
assert "2.1.1" in ordinary_prompt
|
||||
assert "无序圆点列表" in ordinary_prompt
|
||||
assert '<ordinary_qa_markdown_format>' not in writing_prompt
|
||||
assert '<ordinary_qa_markdown_format>' not in notebook_prompt
|
||||
assert '<ordinary_qa_markdown_format>' not in scheduled_prompt
|
||||
@@ -0,0 +1,57 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
from deerflow.utils import file_conversion
|
||||
|
||||
|
||||
def test_legacy_word_is_normalized_before_markitdown(monkeypatch, tmp_path: Path) -> None:
|
||||
source = tmp_path / "legacy.doc"
|
||||
source.write_bytes(b"legacy")
|
||||
|
||||
monkeypatch.setattr(file_conversion.shutil, "which", lambda _name: "/usr/bin/soffice")
|
||||
monkeypatch.setattr(
|
||||
file_conversion,
|
||||
"_convert_with_markitdown",
|
||||
lambda path: f"converted:{path.suffix}",
|
||||
)
|
||||
|
||||
def fake_run(command, **_kwargs):
|
||||
output_dir = Path(command[command.index("--outdir") + 1])
|
||||
(output_dir / "legacy.docx").write_bytes(b"normalized")
|
||||
return SimpleNamespace(returncode=0, stderr="")
|
||||
|
||||
monkeypatch.setattr(file_conversion.subprocess, "run", fake_run)
|
||||
|
||||
assert file_conversion._do_convert(source, "auto") == "converted:.docx"
|
||||
|
||||
|
||||
def test_modern_office_file_does_not_start_libreoffice(monkeypatch, tmp_path: Path) -> None:
|
||||
source = tmp_path / "modern.pptx"
|
||||
source.write_bytes(b"modern")
|
||||
monkeypatch.setattr(
|
||||
file_conversion,
|
||||
"_convert_with_markitdown",
|
||||
lambda path: f"direct:{path.suffix}",
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
file_conversion.subprocess,
|
||||
"run",
|
||||
lambda *_args, **_kwargs: (_ for _ in ()).throw(AssertionError("unexpected LibreOffice call")),
|
||||
)
|
||||
|
||||
assert file_conversion._do_convert(source, "auto") == "direct:.pptx"
|
||||
|
||||
|
||||
def test_legacy_office_falls_back_when_libreoffice_is_missing(monkeypatch, tmp_path: Path) -> None:
|
||||
source = tmp_path / "legacy.xls"
|
||||
source.write_bytes(b"legacy")
|
||||
monkeypatch.setattr(file_conversion.shutil, "which", lambda _name: None)
|
||||
monkeypatch.setattr(
|
||||
file_conversion,
|
||||
"_convert_with_markitdown",
|
||||
lambda path: f"fallback:{path.suffix}",
|
||||
)
|
||||
|
||||
assert file_conversion._do_convert(source, "auto") == "fallback:.xls"
|
||||
@@ -41,8 +41,10 @@ def test_formal_docx_contains_required_layout_numbering_and_embedded_font(tmp_pa
|
||||
## 一、一级标题
|
||||
这是正文,用于验证首行缩进、两端对齐和固定行距。
|
||||
|
||||
### (一)二级标题
|
||||
#### 1. 三级标题
|
||||
### 2.1 二级标题
|
||||
#### 2.1.1 三级标题
|
||||
|
||||
---
|
||||
|
||||
表1 测试表格
|
||||
| 项目 | 数值 |
|
||||
@@ -77,8 +79,11 @@ def test_formal_docx_contains_required_layout_numbering_and_embedded_font(tmp_pa
|
||||
assert '<w:pStyle w:val="DocumentTitle"/>' in document
|
||||
assert "方正小标宋简体" in document
|
||||
assert "一、一级标题" not in document
|
||||
assert "(一)二级标题" not in document
|
||||
assert "1. 三级标题" not in document
|
||||
assert "2.1 二级标题" not in document
|
||||
assert "2.1.1 三级标题" not in document
|
||||
assert "────────" not in document
|
||||
assert "二级标题" in document
|
||||
assert "三级标题" in document
|
||||
|
||||
assert 'w:line="579" w:lineRule="exact"' in styles
|
||||
assert '<w:ind w:left="0" w:hanging="0" w:firstLineChars="200"/>' in styles
|
||||
@@ -86,11 +91,13 @@ def test_formal_docx_contains_required_layout_numbering_and_embedded_font(tmp_pa
|
||||
assert 'w:val="44"' in styles
|
||||
assert "仿宋" in styles and 'w:val="32"' in styles
|
||||
assert "宋体" in styles and 'w:val="28"' in styles
|
||||
assert styles.count('w:firstLineChars="200"') >= 6
|
||||
|
||||
assert '<w:numFmt w:val="chineseCounting"/>' in numbering
|
||||
assert '<w:lvlText w:val="%1、"/>' in numbering
|
||||
assert '<w:lvlText w:val="(%2)"/>' in numbering
|
||||
assert '<w:lvlText w:val="(%2)"/>' in numbering
|
||||
assert '<w:lvlText w:val="%3."/>' in numbering
|
||||
assert numbering.count('w:firstLineChars="200"') == 3
|
||||
assert '<w:evenAndOddHeaders/>' in settings
|
||||
assert '<w:updateFields w:val="true"/>' in settings
|
||||
|
||||
@@ -114,3 +121,20 @@ def test_export_rejects_wrong_font(tmp_path: Path) -> None:
|
||||
|
||||
with pytest.raises(word_export.WordExportFontError):
|
||||
word_export.build_markdown_docx("正文", font_path=wrong_font)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("source", "expected"),
|
||||
[
|
||||
("一、一级标题", "一级标题"),
|
||||
("(一) 二级标题", "二级标题"),
|
||||
("(一)二级标题", "二级标题"),
|
||||
("1. 三级标题", "三级标题"),
|
||||
("2.1 二级标题", "二级标题"),
|
||||
("2.1.1 三级标题", "三级标题"),
|
||||
("2.1.1. 三级标题", "三级标题"),
|
||||
("正文标题", "正文标题"),
|
||||
],
|
||||
)
|
||||
def test_manual_heading_number_is_removed_as_one_complete_prefix(source: str, expected: str) -> None:
|
||||
assert word_export._strip_manual_heading_number(source) == expected
|
||||
Reference in new issue
Block a user