Skip to content

Commit fb9a6d0

Browse files
committed
test(converter): guard that importing converter stays markitdown-free
Adds a regression test that spawns a fresh interpreter and asserts `import openkb.converter` loads none of markitdown / magika / onnxruntime. This locks in the lazy-import optimization: if a future change reintroduces a module-level markitdown import, startup cost and the slim-packaging benefit regress silently while every functional test still passes. The check runs in a subprocess so it is deterministic regardless of what earlier tests imported into this process's sys.modules. Claude-Session: https://claude.ai/code/session_01KZyUSGAzVL9yxpsWWPv6Y2
1 parent 53740cb commit fb9a6d0

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

tests/test_converter.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
import subprocess
6+
import sys
57
from unittest.mock import MagicMock, patch
68

79
from openkb.converter import convert_document, get_pdf_page_count
@@ -164,6 +166,36 @@ def test_docx_conversion_enables_keep_data_uris(self, kb_dir, tmp_path):
164166
assert result.source_path.read_text(encoding="utf-8") == "converted markdown"
165167

166168

169+
# ---------------------------------------------------------------------------
170+
# Lazy markitdown import (guards the deferred-import optimization)
171+
# ---------------------------------------------------------------------------
172+
173+
174+
class TestLazyMarkItDownImport:
175+
def test_importing_converter_does_not_load_markitdown(self):
176+
"""`import openkb.converter` must not pull in markitdown/magika/onnxruntime.
177+
178+
markitdown is imported lazily inside convert_document's Office-format
179+
branch so `import openkb` stays cheap and a packaged build can drop
180+
magika/onnxruntime. This runs in a fresh interpreter (a clean
181+
sys.modules) so the assertion is deterministic regardless of what other
182+
tests in this process already imported.
183+
"""
184+
heavy = ("markitdown", "magika", "onnxruntime")
185+
code = (
186+
"import sys, openkb.converter; "
187+
f"print(','.join(m for m in {heavy!r} if m in sys.modules))"
188+
)
189+
proc = subprocess.run(
190+
[sys.executable, "-c", code],
191+
capture_output=True,
192+
text=True,
193+
)
194+
assert proc.returncode == 0, proc.stderr
195+
loaded = proc.stdout.strip()
196+
assert loaded == "", f"import openkb.converter eagerly loaded: {loaded}"
197+
198+
167199
# ---------------------------------------------------------------------------
168200
# _registry_path
169201
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)