-
Notifications
You must be signed in to change notification settings - Fork 414
Expand file tree
/
Copy pathtest_converter.py
More file actions
136 lines (101 loc) · 5.05 KB
/
Copy pathtest_converter.py
File metadata and controls
136 lines (101 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Tests for openkb.converter."""
from __future__ import annotations
import shutil
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from openkb.converter import ConvertResult, convert_document, get_pdf_page_count
# ---------------------------------------------------------------------------
# get_pdf_page_count
# ---------------------------------------------------------------------------
class TestGetPdfPageCount:
def test_returns_page_count(self, tmp_path):
"""Mock pymupdf to return a doc with 5 pages."""
fake_doc = MagicMock()
fake_doc.page_count = 5
fake_doc.__enter__ = MagicMock(return_value=fake_doc)
fake_doc.__exit__ = MagicMock(return_value=False)
with patch("openkb.converter.pymupdf.open", return_value=fake_doc):
count = get_pdf_page_count(tmp_path / "fake.pdf")
assert count == 5
# ---------------------------------------------------------------------------
# convert_document — .md input
# ---------------------------------------------------------------------------
class TestConvertDocumentMarkdown:
def test_md_file_copied_to_wiki_sources(self, kb_dir):
"""A .md file is read and saved under wiki/sources/."""
src = kb_dir / "raw" / "notes.md"
src.write_text("# Notes\n\nSome content here.", encoding="utf-8")
result = convert_document(src, kb_dir)
assert result.skipped is False
assert result.is_long_doc is False
assert result.source_path is not None
assert result.source_path.exists()
assert result.source_path.read_text(encoding="utf-8").startswith("# Notes")
def test_md_duplicate_skipped(self, kb_dir):
"""Second call with same file returns skipped=True when hash is registered."""
from openkb.state import HashRegistry
src = kb_dir / "raw" / "notes.md"
src.write_text("# Notes\n\nSome content here.", encoding="utf-8")
result1 = convert_document(src, kb_dir) # first call
# Simulate CLI registering the hash after successful compilation
registry = HashRegistry(kb_dir / ".openkb" / "hashes.json")
registry.add(result1.file_hash, {"name": src.name, "type": "md"})
result2 = convert_document(src, kb_dir) # second call
assert result2.skipped is True
assert result2.source_path is None
assert result2.raw_path is None
def test_md_raw_file_copied(self, kb_dir):
"""The original file should also be copied to raw/."""
src = kb_dir / "input" / "notes.md"
src.parent.mkdir(parents=True, exist_ok=True)
src.write_text("# Notes\n", encoding="utf-8")
result = convert_document(src, kb_dir)
assert result.raw_path is not None
assert result.raw_path.exists()
# ---------------------------------------------------------------------------
# convert_document — PDF short doc
# ---------------------------------------------------------------------------
class TestConvertDocumentPdfShort:
def test_short_pdf_converted_via_markitdown(self, kb_dir, tmp_path):
"""PDF under threshold is converted with markitdown."""
src = tmp_path / "short.pdf"
src.write_bytes(b"%PDF-1.4 fake content")
fake_result = MagicMock()
fake_result.text_content = "# Short PDF\n\nConverted content."
with (
patch("openkb.converter.pymupdf.open") as mock_mu,
patch("openkb.converter.MarkItDown") as mock_mid_cls,
):
fake_doc = MagicMock()
fake_doc.page_count = 5 # below default threshold of 20
fake_doc.__enter__ = MagicMock(return_value=fake_doc)
fake_doc.__exit__ = MagicMock(return_value=False)
mock_mu.return_value = fake_doc
mock_mid_cls.return_value.convert.return_value = fake_result
result = convert_document(src, kb_dir)
assert result.skipped is False
assert result.is_long_doc is False
assert result.source_path is not None
assert result.source_path.exists()
# ---------------------------------------------------------------------------
# convert_document — PDF long doc
# ---------------------------------------------------------------------------
class TestConvertDocumentPdfLong:
def test_long_pdf_returns_is_long_doc(self, kb_dir, tmp_path):
"""PDF >= threshold pages returns is_long_doc=True, source_path=None."""
src = tmp_path / "long.pdf"
src.write_bytes(b"%PDF-1.4 fake long content")
with (
patch("openkb.converter.pymupdf.open") as mock_mu,
):
fake_doc = MagicMock()
fake_doc.page_count = 200 # above threshold
fake_doc.__enter__ = MagicMock(return_value=fake_doc)
fake_doc.__exit__ = MagicMock(return_value=False)
mock_mu.return_value = fake_doc
result = convert_document(src, kb_dir)
assert result.is_long_doc is True
assert result.source_path is None
assert result.skipped is False
assert result.raw_path is not None