2026-05-25 17:12:00 +08:00
|
|
|
from pathlib import Path
|
|
|
|
|
from tempfile import TemporaryDirectory
|
|
|
|
|
|
2026-09-17 14:56:48 +08:00
|
|
|
from PySide6.QtGui import QPageLayout
|
|
|
|
|
|
2026-05-25 17:12:00 +08:00
|
|
|
from mdhelper.app import parse_arguments
|
2026-06-14 09:51:16 +08:00
|
|
|
from mdhelper import search as search_module
|
2026-05-25 17:12:00 +08:00
|
|
|
from mdhelper.doc_check import check_documents
|
2026-06-14 09:51:16 +08:00
|
|
|
from mdhelper.code_structure import extract_code_headings
|
|
|
|
|
from mdhelper.document_tree import DocumentEntry, MAX_DISCOVERABLE_CODE_BYTES, discover_documents
|
2026-05-25 22:00:37 +08:00
|
|
|
from mdhelper.history import HistoryItem, NavigationHistory
|
2026-05-25 17:12:00 +08:00
|
|
|
from mdhelper.main_window import re_find_markdown_links
|
|
|
|
|
from mdhelper.markdown_utils import extract_headings
|
|
|
|
|
from mdhelper.markdown_renderer import MarkdownRenderer
|
2026-09-17 14:56:48 +08:00
|
|
|
from mdhelper.pdf_export import PdfMargins, build_pdf_export_html, build_pdf_page_layout
|
2026-05-25 17:12:00 +08:00
|
|
|
from mdhelper.search import SearchIndex, search_entries
|
2026-06-14 09:51:16 +08:00
|
|
|
from mdhelper.style_dialog import build_document_style_css
|
2026-05-25 17:12:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sample_media_and_code_rendering() -> None:
|
|
|
|
|
project_root = Path(__file__).resolve().parent.parent
|
|
|
|
|
renderer = MarkdownRenderer(project_root)
|
|
|
|
|
html = renderer.render_file(project_root / "sample.md")
|
|
|
|
|
|
|
|
|
|
assert "<table>" in html
|
|
|
|
|
assert "<audio controls" in html
|
2026-05-27 17:29:46 +08:00
|
|
|
assert "media-player-link" in html
|
|
|
|
|
assert "用内置播放器打开" in html
|
|
|
|
|
assert "用外部播放器打开" in html
|
|
|
|
|
assert "mdhelper-open=player-new-tab" in html
|
|
|
|
|
assert "mdhelper-open=external" in html
|
2026-05-25 17:12:00 +08:00
|
|
|
assert "file:///" in html
|
|
|
|
|
assert 'href="file:///' in html
|
|
|
|
|
assert "docs/user_manual.md" in html
|
|
|
|
|
assert "media/animation.gif" in html
|
|
|
|
|
assert "language-pseudocode" in html
|
|
|
|
|
assert 'id="2-表格示例"' in html
|
2026-05-25 22:00:37 +08:00
|
|
|
assert 'href="#2-%E8%A1%A8%E6%A0%BC%E7%A4%BA%E4%BE%8B"' in html
|
|
|
|
|
assert f'{project_root.as_uri()}#2-%E8%A1%A8%E6%A0%BC%E7%A4%BA%E4%BE%8B' not in html
|
2026-05-25 17:12:00 +08:00
|
|
|
assert "copy-code-button" in html
|
|
|
|
|
assert "code-line" in html
|
|
|
|
|
assert "toggle-code-button" in html
|
2026-06-18 12:30:37 +08:00
|
|
|
assert "CODE_LINE_NUMBER_LIMIT = 240" in html
|
|
|
|
|
assert "CODE_AUTO_COLLAPSE_LIMIT = 420" in html
|
|
|
|
|
assert 'toggle.textContent = block.classList.contains("is-collapsed") ? "展开" : "收起";' in html
|
2026-06-14 09:51:16 +08:00
|
|
|
assert 'block.classList.add("is-collapsible");' in html
|
|
|
|
|
assert 'block.classList.add("is-collapsible", "is-collapsed")' not in html
|
|
|
|
|
assert '${line || " "}' in html
|
|
|
|
|
assert '.join("");' in html
|
|
|
|
|
assert "#569cd6" in html
|
|
|
|
|
assert "#ce9178" in html
|
|
|
|
|
assert "#1e1e1e" in html
|
2026-06-18 12:30:37 +08:00
|
|
|
assert 'decoding="async"' in html
|
|
|
|
|
assert "content-visibility: auto;" in html
|
2026-05-25 17:12:00 +08:00
|
|
|
assert "image-lightbox" in html
|
|
|
|
|
assert "image-lightbox__toolbar" in html
|
2026-05-25 22:00:37 +08:00
|
|
|
assert 'data-action="fit"' in html
|
|
|
|
|
assert 'data-action="actual-size"' in html
|
|
|
|
|
assert 'data-action="save"' in html
|
|
|
|
|
assert 'data-action="reveal"' in html
|
2026-05-25 17:12:00 +08:00
|
|
|
assert "copy-table-button" in html
|
|
|
|
|
assert "table-wrapper" in html
|
|
|
|
|
assert "task-list-item-checkbox" in html
|
|
|
|
|
assert "admonition admonition-tip" in html
|
2026-05-25 22:00:37 +08:00
|
|
|
assert "admonition admonition-note" in html
|
|
|
|
|
assert "admonition admonition-important" in html
|
2026-05-25 17:12:00 +08:00
|
|
|
assert "admonition admonition-warning" in html
|
2026-05-25 22:00:37 +08:00
|
|
|
assert "admonition admonition-caution" in html
|
|
|
|
|
assert '<pre class="mermaid">' in html
|
|
|
|
|
assert "flowchart LR" in html
|
|
|
|
|
assert "math-block" in html
|
|
|
|
|
assert "data-math-engine=\"katex\"" in html
|
2026-06-14 09:51:16 +08:00
|
|
|
assert "data-wheel-scroll-speed=\"1.0\"" in html
|
|
|
|
|
assert "configuredWheelSpeed" in html
|
2026-09-17 14:56:48 +08:00
|
|
|
assert "VISUAL_SCROLL_INTERVAL" not in html
|
|
|
|
|
assert "scheduleScrollEndUpdate" not in html
|
|
|
|
|
assert "headingElements" not in html
|
|
|
|
|
assert "backdrop-filter" not in html
|
2026-05-25 22:00:37 +08:00
|
|
|
assert "--reader-font-family:" in html
|
|
|
|
|
assert "--reader-font-size:" in html
|
2026-06-18 12:30:37 +08:00
|
|
|
assert "--reader-paper-width: 794px;" in html
|
2026-05-25 22:00:37 +08:00
|
|
|
assert "katex.min.css" in html
|
|
|
|
|
assert "auto-render.min.js" in html
|
2026-09-17 14:56:48 +08:00
|
|
|
assert ".math-block .katex" in html
|
|
|
|
|
assert ".math-display mjx-container" in html
|
|
|
|
|
assert ".math-inline .katex" in html
|
|
|
|
|
assert ".math-inline mjx-container" in html
|
|
|
|
|
assert "--reader-math-font-size: var(--reader-font-size);" in html
|
|
|
|
|
assert "syncMathFontSize" in html
|
2026-05-25 22:00:37 +08:00
|
|
|
assert "window.mermaid" in html
|
|
|
|
|
assert "mermaid.min.js" in html
|
2026-05-25 17:12:00 +08:00
|
|
|
assert "qrc:///qtwebchannel/qwebchannel.js" in html
|
|
|
|
|
|
2026-05-25 22:00:37 +08:00
|
|
|
mathjax_html = renderer.render_file(project_root / "sample.md", math_engine="mathjax")
|
|
|
|
|
assert "data-math-engine=\"mathjax\"" in mathjax_html
|
|
|
|
|
assert "window.MathJax" in mathjax_html
|
|
|
|
|
|
|
|
|
|
custom_font_html = renderer.render_file(
|
|
|
|
|
project_root / "sample.md",
|
|
|
|
|
document_font_family="Microsoft YaHei UI",
|
|
|
|
|
document_font_size=18,
|
2026-06-14 09:51:16 +08:00
|
|
|
wheel_scroll_speed=2.4,
|
2026-05-25 22:00:37 +08:00
|
|
|
)
|
2026-06-14 09:51:16 +08:00
|
|
|
assert "data-wheel-scroll-speed=\"2.4\"" in custom_font_html
|
2026-05-25 22:00:37 +08:00
|
|
|
assert '"Microsoft YaHei UI"' in custom_font_html
|
|
|
|
|
assert "--reader-font-size: 18px;" in custom_font_html
|
|
|
|
|
|
2026-06-18 12:30:37 +08:00
|
|
|
custom_width_html = renderer.render_file(project_root / "sample.md", document_width_scale=1.5)
|
|
|
|
|
assert "--reader-paper-width: 1191px;" in custom_width_html
|
|
|
|
|
|
2026-06-14 09:51:16 +08:00
|
|
|
custom_style_html = renderer.render_file(
|
|
|
|
|
project_root / "sample.md",
|
|
|
|
|
document_style_css=".reader { max-width: 720px; }",
|
|
|
|
|
)
|
|
|
|
|
assert ".reader { max-width: 720px; }" in custom_style_html
|
|
|
|
|
|
2026-05-25 17:12:00 +08:00
|
|
|
|
2026-09-17 14:56:48 +08:00
|
|
|
def test_plain_document_omits_optional_runtime_assets(tmp_path: Path) -> None:
|
|
|
|
|
renderer = MarkdownRenderer(tmp_path)
|
|
|
|
|
rendered = renderer.render_source("# Plain\n\nNo formula or diagram.", tmp_path / "plain.md")
|
|
|
|
|
assert "mermaid.min.js" not in rendered
|
|
|
|
|
assert "katex.min.js" not in rendered
|
|
|
|
|
assert "tex-svg-full.js" not in rendered
|
2026-06-18 12:30:37 +08:00
|
|
|
|
2026-09-17 14:56:48 +08:00
|
|
|
|
|
|
|
|
def test_formula_and_diagram_load_independently(tmp_path: Path) -> None:
|
|
|
|
|
renderer = MarkdownRenderer(tmp_path)
|
|
|
|
|
formula = renderer.render_source("Formula $x^2$.", tmp_path / "formula.md")
|
|
|
|
|
assert "katex.min.js" in formula
|
|
|
|
|
assert "mermaid.min.js" not in formula
|
|
|
|
|
diagram = renderer.render_source("```mermaid\ngraph LR\nA-->B\n```", tmp_path / "diagram.md")
|
|
|
|
|
assert "mermaid.min.js" in diagram
|
|
|
|
|
assert "katex.min.js" not in diagram
|
|
|
|
|
mathjax = renderer.render_source(r"Formula \(x^2\).", tmp_path / "math.md", math_engine="mathjax")
|
|
|
|
|
assert "tex-svg-full.js" in mathjax
|
|
|
|
|
assert "katex.min.js" not in mathjax
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_latex_math_is_protected_from_markdown_emphasis(tmp_path: Path) -> None:
|
|
|
|
|
renderer = MarkdownRenderer(tmp_path)
|
|
|
|
|
source = r"""普通强调 _仍然生效_,行内公式 $\widehat{\nabla_0W}^{(i)}_{0ij}$ 不应变成斜体。
|
|
|
|
|
|
|
|
|
|
$$
|
|
|
|
|
\mathbf g^{\mathrm{raw}}_{ij}
|
|
|
|
|
=\nabla_{\boldsymbol\xi_i}W_{0ij}
|
|
|
|
|
=-\nabla_{\boldsymbol\xi_{ij}}W(\boldsymbol\xi_{ij},h_s)
|
|
|
|
|
\in\mathbb R^2.
|
|
|
|
|
$$
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
rendered = renderer.markdown.render(source)
|
|
|
|
|
|
|
|
|
|
assert "<em>仍然生效</em>" in rendered
|
|
|
|
|
assert '<span class="math-inline">$\\widehat{\\nabla_0W}^{(i)}_{0ij}$</span>' in rendered
|
|
|
|
|
assert '<div class="math-block">$$' in rendered
|
|
|
|
|
assert "\\mathbf g^{\\mathrm{raw}}_{ij}" in rendered
|
|
|
|
|
assert "\\nabla_{\\boldsymbol\\xi_i}W_{0ij}" in rendered
|
|
|
|
|
assert "<em>{ij}" not in rendered
|
|
|
|
|
assert "<em>{0ij}" not in rendered
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_parenthesized_latex_math_is_protected_from_markdown_escapes(tmp_path: Path) -> None:
|
|
|
|
|
renderer = MarkdownRenderer(tmp_path)
|
|
|
|
|
|
|
|
|
|
rendered = renderer.markdown.render(
|
|
|
|
|
r"""公式 \(\mathbf g^{c}_{0ij}\) 正常。
|
|
|
|
|
|
|
|
|
|
\[
|
|
|
|
|
\mathbf H_i=\sum_j \mathbf g^{\mathrm{raw}}_{ij}
|
|
|
|
|
\]
|
|
|
|
|
|
|
|
|
|
同一行展示公式 $$\mathbf L_i=\mathbf H_i^{-1}$$ 正常。
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert '<span class="math-inline">\\(\\mathbf g^{c}_{0ij}\\)</span>' in rendered
|
|
|
|
|
assert '<div class="math-block">\\[' in rendered
|
|
|
|
|
assert "\\mathbf H_i=\\sum_j \\mathbf g^{\\mathrm{raw}}_{ij}" in rendered
|
|
|
|
|
assert '<span class="math-display">$$\\mathbf L_i=\\mathbf H_i^{-1}$$</span>' in rendered
|
|
|
|
|
assert "<em>" not in rendered
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_reader_html_excludes_outline_and_progress_chrome() -> None:
|
2026-06-18 12:30:37 +08:00
|
|
|
project_root = Path(__file__).resolve().parent.parent
|
|
|
|
|
renderer = MarkdownRenderer(project_root)
|
|
|
|
|
|
2026-09-17 14:56:48 +08:00
|
|
|
html = renderer.render_file(project_root / "sample.md")
|
|
|
|
|
|
|
|
|
|
assert 'class="reader-shell"' in html
|
|
|
|
|
assert 'id="mdhelper-reader"' in html
|
|
|
|
|
assert "reader-toc" not in html
|
|
|
|
|
assert "reader-progress" not in html
|
|
|
|
|
assert "data-reader-toc" not in html
|
|
|
|
|
assert "data-reader-progress" not in html
|
|
|
|
|
assert "reportScroll" not in html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_pdf_page_layout_uses_configured_margins() -> None:
|
|
|
|
|
configured = PdfMargins(top=12.0, bottom=18.0, left=20.0, right=16.0)
|
|
|
|
|
layout = build_pdf_page_layout(configured)
|
|
|
|
|
margins = layout.margins(QPageLayout.Unit.Millimeter)
|
|
|
|
|
|
|
|
|
|
assert margins.left() == configured.left
|
|
|
|
|
assert margins.right() == configured.right
|
|
|
|
|
assert margins.top() == configured.top
|
|
|
|
|
assert margins.bottom() == configured.bottom
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_pdf_export_html_has_numbered_toc_white_background_and_page_break() -> None:
|
|
|
|
|
project_root = Path(__file__).resolve().parent.parent
|
|
|
|
|
renderer = MarkdownRenderer(project_root)
|
|
|
|
|
headings = extract_headings(project_root / "sample.md")
|
|
|
|
|
html = renderer.render_file(project_root / "sample.md")
|
|
|
|
|
|
|
|
|
|
exported = build_pdf_export_html(html, headings, {index: index + 2 for index in range(len(headings))})
|
|
|
|
|
|
|
|
|
|
assert 'class="pdf-toc' in exported
|
|
|
|
|
assert 'class="reader-toc"' not in exported
|
|
|
|
|
assert 'class="reader-progress"' not in exported
|
|
|
|
|
assert "break-after: page" in exported
|
|
|
|
|
assert "background: #ffffff !important" in exported
|
|
|
|
|
assert "MDHELPER_PDF_BODY_START_7A61" in exported
|
|
|
|
|
assert "MDHELPER_PDF_HEADING_0000" in exported
|
|
|
|
|
assert '<span class="pdf-toc__page">2</span>' in exported
|
|
|
|
|
|
|
|
|
|
final_export = build_pdf_export_html(html, headings, {0: 2}, include_page_markers=False)
|
|
|
|
|
assert "MDHELPER_PDF_BODY_START_7A61" not in final_export
|
|
|
|
|
assert "MDHELPER_PDF_HEADING_0000" not in final_export
|
|
|
|
|
|
2026-06-18 12:30:37 +08:00
|
|
|
|
2026-05-25 17:12:00 +08:00
|
|
|
def test_heading_extraction_matches_rendered_anchor() -> None:
|
|
|
|
|
project_root = Path(__file__).resolve().parent.parent
|
|
|
|
|
headings = extract_headings(project_root / "sample.md")
|
|
|
|
|
|
|
|
|
|
assert any(heading.slug == "2-表格示例" for heading in headings)
|
|
|
|
|
assert any(heading.title == "7. 视频/动画示例" for heading in headings)
|
2026-05-25 22:00:37 +08:00
|
|
|
assert any(heading.title == "10. Mermaid 图表示例" for heading in headings)
|
2026-05-25 17:12:00 +08:00
|
|
|
|
|
|
|
|
|
2026-06-14 09:51:16 +08:00
|
|
|
def test_document_style_builder_supports_word_like_paragraph_options() -> None:
|
|
|
|
|
css = build_document_style_css(
|
|
|
|
|
{
|
|
|
|
|
"paragraph": {
|
|
|
|
|
"chinese_font_family": "Microsoft YaHei UI",
|
|
|
|
|
"english_font_family": "Times New Roman",
|
|
|
|
|
"font_size": 18,
|
|
|
|
|
"line_height_value": 1.7,
|
|
|
|
|
"line_height_unit": "multiple",
|
|
|
|
|
"first_line_indent_chars": 2,
|
|
|
|
|
"space_before_value": 6,
|
|
|
|
|
"space_before_unit": "pt",
|
|
|
|
|
"space_after_value": 0.6,
|
|
|
|
|
"space_after_unit": "multiple",
|
|
|
|
|
},
|
|
|
|
|
"h2": {
|
|
|
|
|
"bold": True,
|
|
|
|
|
"hanging_indent_chars": 1.5,
|
|
|
|
|
},
|
|
|
|
|
"blockquote": {
|
|
|
|
|
"chinese_font_family": "KaiTi",
|
|
|
|
|
"english_font_family": "Georgia",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
".reader strong { color: #0057b8; }",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert ".reader p" in css
|
2026-09-17 14:56:48 +08:00
|
|
|
assert "--reader-math-font-size: 18px;" in css
|
2026-06-14 09:51:16 +08:00
|
|
|
assert 'font-family: "Times New Roman", "Microsoft YaHei UI"' in css
|
|
|
|
|
assert "font-size: 18px;" in css
|
|
|
|
|
assert "line-height: 1.7;" in css
|
|
|
|
|
assert "text-indent: 2em;" in css
|
|
|
|
|
assert "margin-top: 6pt;" in css
|
|
|
|
|
assert "margin-bottom: 0.6em;" in css
|
|
|
|
|
assert ".reader h2" in css
|
|
|
|
|
assert "font-weight: 700;" in css
|
2026-10-29 14:50:16 +08:00
|
|
|
assert "margin-left: 1.5em;" in css
|
2026-06-14 09:51:16 +08:00
|
|
|
assert "text-indent: -1.5em;" in css
|
|
|
|
|
assert ".reader blockquote p" in css
|
|
|
|
|
assert ".reader .admonition p" in css
|
|
|
|
|
assert 'font-family: "Georgia", "KaiTi"' in css
|
|
|
|
|
assert ".reader strong { color: #0057b8; }" in css
|
|
|
|
|
|
|
|
|
|
|
2026-05-25 17:12:00 +08:00
|
|
|
def test_full_text_search_finds_document_body() -> None:
|
|
|
|
|
project_root = Path(__file__).resolve().parent.parent
|
|
|
|
|
entries = discover_documents(project_root)
|
|
|
|
|
matches = search_entries(entries, "斐波那契")
|
|
|
|
|
|
|
|
|
|
assert matches
|
|
|
|
|
assert matches[0].entry.path.name == "sample.md"
|
2026-05-25 22:00:37 +08:00
|
|
|
assert matches[0].match_count >= 1
|
|
|
|
|
assert matches[0].match_kind in {"标题", "路径", "正文"}
|
2026-05-25 17:12:00 +08:00
|
|
|
|
|
|
|
|
index = SearchIndex(entries)
|
|
|
|
|
indexed_matches = index.search("斐波那契")
|
|
|
|
|
assert indexed_matches
|
|
|
|
|
assert indexed_matches[0].entry.path.name == "sample.md"
|
2026-05-25 22:00:37 +08:00
|
|
|
assert indexed_matches[0].match_count >= 1
|
2026-05-25 17:12:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_markdown_link_extraction_for_watchers() -> None:
|
|
|
|
|
links = re_find_markdown_links(
|
|
|
|
|
'\n'
|
|
|
|
|
'[用户手册](docs/user_manual.md)\n'
|
|
|
|
|
'<video src="media/demo.mp4"></video>'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert "media/screenshot.png" in links
|
|
|
|
|
assert "docs/user_manual.md" in links
|
|
|
|
|
assert "media/demo.mp4" in links
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_local_links_are_absolute_file_urls() -> None:
|
|
|
|
|
project_root = Path(__file__).resolve().parent.parent
|
|
|
|
|
renderer = MarkdownRenderer(project_root)
|
|
|
|
|
html = renderer.render_file(project_root / "sample.md")
|
|
|
|
|
|
|
|
|
|
assert (project_root / "docs" / "user_manual.md").resolve().as_uri() in html
|
|
|
|
|
assert (project_root / "media" / "screenshot.png").resolve().as_uri() in html
|
|
|
|
|
assert (project_root / "media" / "demo.mp4").resolve().as_uri() in html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_document_checker_reports_missing_local_target() -> None:
|
|
|
|
|
with TemporaryDirectory() as temp_dir:
|
|
|
|
|
root = Path(temp_dir)
|
|
|
|
|
docs = root / "docs"
|
|
|
|
|
docs.mkdir()
|
|
|
|
|
(docs / "ok.md").write_text("# OK\n", encoding="utf-8")
|
|
|
|
|
(root / "sample.md").write_text(
|
|
|
|
|
"[存在](docs/ok.md)\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"[网页](https://example.com)\n",
|
|
|
|
|
encoding="utf-8",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
issues = check_documents(root)
|
|
|
|
|
|
|
|
|
|
assert len(issues) == 1
|
|
|
|
|
assert issues[0].target == "media/missing.png"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_app_arguments_support_initial_file_and_check_mode() -> None:
|
|
|
|
|
args = parse_arguments(["sample.md"])
|
2026-06-14 09:51:16 +08:00
|
|
|
assert args.path == "sample.md"
|
2026-05-25 17:12:00 +08:00
|
|
|
assert not args.check
|
|
|
|
|
|
|
|
|
|
check_args = parse_arguments(["--check", "--project-root", "."])
|
|
|
|
|
assert check_args.check
|
|
|
|
|
assert check_args.project_root == "."
|
2026-05-25 22:00:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_navigation_history_preserves_scroll_position() -> None:
|
|
|
|
|
history = NavigationHistory()
|
|
|
|
|
first = HistoryItem(path=Path("sample.md"), scroll_y=320)
|
|
|
|
|
second = HistoryItem(path=Path("docs/user_manual.md"), fragment="intro", scroll_y=0)
|
|
|
|
|
|
|
|
|
|
history.push(first)
|
|
|
|
|
history.push(second)
|
|
|
|
|
history.replace_current(HistoryItem(path=second.path, fragment=second.fragment, scroll_y=120))
|
|
|
|
|
|
|
|
|
|
assert history.current is not None
|
|
|
|
|
assert history.current.scroll_y == 120
|
|
|
|
|
assert history.back() == first
|
2026-06-14 09:51:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_document_discovery_filters_optional_code_groups(tmp_path: Path) -> None:
|
|
|
|
|
(tmp_path / "guide.md").write_text("# Guide\n", encoding="utf-8")
|
|
|
|
|
(tmp_path / "tool.py").write_text("def run():\n return 1\n", encoding="utf-8")
|
|
|
|
|
(tmp_path / "engine.cpp").write_text("int solve() { return 1; }\n", encoding="utf-8")
|
2026-06-22 18:07:34 +08:00
|
|
|
(tmp_path / "solver.f90").write_text("program demo\nend program demo\n", encoding="utf-8")
|
2026-06-14 09:51:16 +08:00
|
|
|
|
|
|
|
|
default_entries = discover_documents(tmp_path)
|
2026-06-22 18:07:34 +08:00
|
|
|
all_entries = discover_documents(tmp_path, ("markdown", "python", "cpp", "fortran"))
|
2026-06-14 09:51:16 +08:00
|
|
|
|
|
|
|
|
assert [entry.path.name for entry in default_entries] == ["guide.md"]
|
2026-06-22 18:07:34 +08:00
|
|
|
assert {entry.path.name for entry in all_entries} == {"guide.md", "tool.py", "engine.cpp", "solver.f90"}
|
2026-06-14 09:51:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_document_discovery_prunes_excluded_directories(tmp_path: Path) -> None:
|
|
|
|
|
(tmp_path / "guide.md").write_text("# Guide\n", encoding="utf-8")
|
|
|
|
|
hidden_git = tmp_path / ".git"
|
|
|
|
|
hidden_git.mkdir()
|
|
|
|
|
(hidden_git / "hidden.md").write_text("# Hidden\n", encoding="utf-8")
|
|
|
|
|
build_dir = tmp_path / "build"
|
|
|
|
|
build_dir.mkdir()
|
|
|
|
|
(build_dir / "generated.py").write_text("def generated():\n return 1\n", encoding="utf-8")
|
|
|
|
|
|
|
|
|
|
entries = discover_documents(tmp_path, ("markdown", "python"))
|
|
|
|
|
|
|
|
|
|
assert {entry.relative_path for entry in entries} == {"guide.md"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_search_index_reads_body_lazily(tmp_path: Path, monkeypatch) -> None:
|
|
|
|
|
path = tmp_path / "guide.md"
|
|
|
|
|
path.write_text("placeholder", encoding="utf-8")
|
|
|
|
|
entry = DocumentEntry(path=path, title="Guide", relative_path="guide.md")
|
|
|
|
|
calls: list[Path] = []
|
|
|
|
|
|
|
|
|
|
def fake_read_text(read_entry: DocumentEntry) -> str:
|
|
|
|
|
calls.append(read_entry.path)
|
|
|
|
|
return "body needle"
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(search_module, "_read_text", fake_read_text)
|
|
|
|
|
|
|
|
|
|
index = search_module.SearchIndex([entry])
|
|
|
|
|
|
|
|
|
|
assert calls == []
|
|
|
|
|
assert index.search("Guide")[0].match_kind == "标题"
|
|
|
|
|
assert calls == []
|
|
|
|
|
assert index.search("needle")[0].match_kind == "正文"
|
|
|
|
|
assert calls == [entry.path]
|
|
|
|
|
|
|
|
|
|
def test_code_file_rendering_uses_highlighting_and_line_anchors(tmp_path: Path) -> None:
|
|
|
|
|
code_path = tmp_path / "tool.py"
|
|
|
|
|
source = "class Runner:\n def run(self):\n return 1\n"
|
|
|
|
|
renderer = MarkdownRenderer(tmp_path)
|
|
|
|
|
|
|
|
|
|
html = renderer.render_code_source(source, code_path, language="python")
|
2026-06-22 18:07:34 +08:00
|
|
|
fortran_html = renderer.render_code_source("program demo\nend program demo\n", tmp_path / "solver.f90", language="fortran")
|
2026-06-14 09:51:16 +08:00
|
|
|
|
|
|
|
|
assert "code-file-view" in html
|
|
|
|
|
assert "language-python" in html
|
|
|
|
|
assert "line-1" in html
|
|
|
|
|
assert "Runner" in html
|
2026-06-22 18:07:34 +08:00
|
|
|
assert "language-fortran" in fortran_html
|
|
|
|
|
assert "line-1" in fortran_html
|
|
|
|
|
assert "program" in fortran_html.lower()
|
2026-06-14 09:51:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_code_headings_extract_python_and_cpp_symbols(tmp_path: Path) -> None:
|
|
|
|
|
python_path = tmp_path / "tool.py"
|
|
|
|
|
cpp_path = tmp_path / "engine.cpp"
|
2026-06-22 18:07:34 +08:00
|
|
|
fortran_path = tmp_path / "solver.f90"
|
2026-06-14 09:51:16 +08:00
|
|
|
python_source = "class Runner:\n def run(self):\n return 1\n\nasync def main():\n return None\n"
|
|
|
|
|
cpp_source = "struct Node { int value; };\n\nint solve(int value) { return value; }\n"
|
2026-06-22 18:07:34 +08:00
|
|
|
fortran_source = (
|
|
|
|
|
"module solver_mod\n"
|
|
|
|
|
"type state\nend type state\n"
|
|
|
|
|
"contains\n"
|
|
|
|
|
"subroutine advance(dt)\nend subroutine advance\n"
|
|
|
|
|
"real function energy(x)\nend function energy\n"
|
|
|
|
|
"end module solver_mod\n"
|
|
|
|
|
)
|
2026-06-14 09:51:16 +08:00
|
|
|
|
|
|
|
|
python_headings = extract_code_headings(python_path, python_source)
|
|
|
|
|
cpp_headings = extract_code_headings(cpp_path, cpp_source)
|
2026-06-22 18:07:34 +08:00
|
|
|
fortran_headings = extract_code_headings(fortran_path, fortran_source)
|
2026-06-14 09:51:16 +08:00
|
|
|
|
|
|
|
|
assert [heading.title for heading in python_headings] == ["class Runner", "def run", "async def main"]
|
|
|
|
|
assert [heading.slug for heading in python_headings] == ["line-1", "line-2", "line-5"]
|
|
|
|
|
assert any(heading.title == "struct Node" for heading in cpp_headings)
|
|
|
|
|
assert any(heading.title == "function solve" for heading in cpp_headings)
|
2026-06-22 18:07:34 +08:00
|
|
|
assert [heading.title for heading in fortran_headings] == [
|
|
|
|
|
"module solver_mod",
|
|
|
|
|
"type state",
|
|
|
|
|
"subroutine advance",
|
|
|
|
|
"function energy",
|
|
|
|
|
]
|
2026-06-14 09:51:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_search_index_reads_optional_code_documents(tmp_path: Path) -> None:
|
|
|
|
|
(tmp_path / "tool.py").write_text("def calculate_magic_value():\n return 42\n", encoding="utf-8")
|
|
|
|
|
entries = discover_documents(tmp_path, ("python",))
|
|
|
|
|
|
|
|
|
|
matches = SearchIndex(entries).search("magic_value")
|
|
|
|
|
|
|
|
|
|
assert matches
|
|
|
|
|
assert matches[0].entry.path.name == "tool.py"
|
|
|
|
|
assert matches[0].match_kind == "正文"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_large_code_files_are_not_auto_discovered(tmp_path: Path) -> None:
|
|
|
|
|
(tmp_path / "guide.md").write_text("# Guide\n", encoding="utf-8")
|
|
|
|
|
large_code = tmp_path / "large_module.py"
|
|
|
|
|
large_code.write_text("# large\n" + ("x = 1\n" * ((MAX_DISCOVERABLE_CODE_BYTES // 6) + 2)), encoding="utf-8")
|
|
|
|
|
|
|
|
|
|
entries = discover_documents(tmp_path, ("markdown", "python"))
|
|
|
|
|
|
|
|
|
|
assert {entry.path.name for entry in entries} == {"guide.md"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_code_search_reads_gbk_source(tmp_path: Path) -> None:
|
|
|
|
|
source_path = tmp_path / "legacy.py"
|
|
|
|
|
source_path.write_text("# 中文注释\ndef legacy_name():\n return '命中'\n", encoding="gbk")
|
|
|
|
|
entries = discover_documents(tmp_path, ("python",))
|
|
|
|
|
|
|
|
|
|
matches = SearchIndex(entries).search("命中")
|
|
|
|
|
|
|
|
|
|
assert matches
|
|
|
|
|
assert matches[0].entry.path.name == "legacy.py"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_code_headings_read_gbk_source_when_source_not_supplied(tmp_path: Path) -> None:
|
|
|
|
|
source_path = tmp_path / "legacy.py"
|
|
|
|
|
source_path.write_text("class 中文类:\n def run(self):\n return 1\n", encoding="gbk")
|
|
|
|
|
|
|
|
|
|
headings = extract_code_headings(source_path)
|
|
|
|
|
|
|
|
|
|
assert headings[0].title == "class 中文类"
|
|
|
|
|
assert headings[1].title == "def run"
|
2026-06-18 12:30:37 +08:00
|
|
|
|