"""convert module: the Markdown clean-up is pure and always tested; the style patching and real conversions run only where pandoc is installed.""" import contextlib import importlib.util import io import re import shutil import tempfile import unittest import xml.dom.minidom import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[2] SPEC = importlib.util.spec_from_file_location( "convert", str(ROOT / ".agents/modules/convert/skills/ckb-convert/scripts/convert.py")) convert = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(convert) DOC = """--- type: document created: 2026-11-11 author: Ada Lovelace --- # Status: Atlas See [[Atlas Retry Policy]] ([atlas](/wiki/entities/atlas.md)), [Maria](/wiki/entities/maria.md), [[x/y/page|the page]], [web](https://example.com) and ![chart](img/chart.png). ```md [[keep]] [k](/wiki/k.md) ``` """ class Prepare(unittest.TestCase): def test_metadata_from_frontmatter_and_heading(self): meta, body = convert.prepare(DOC, keep_links=False) self.assertEqual(meta, {"title": "Status: Atlas", "author": "Ada Lovelace", "date": "2026-11-11"}) self.assertNotIn("# Status", body) self.assertNotIn("type: document", body) def test_kb_links_become_text_web_links_and_images_stay(self): _, body = convert.prepare(DOC, keep_links=False) self.assertIn("See Atlas Retry Policy, Maria,", body) self.assertIn("the page,", body) self.assertIn("[web](https://example.com)", body) self.assertIn("![chart](img/chart.png)", body) self.assertNotIn("Ask the user", body) def test_code_blocks_untouched(self): _, body = convert.prepare(DOC, keep_links=False) self.assertIn("[[keep]] [k](/wiki/k.md)", body) def test_keep_links(self): _, body = convert.prepare(DOC, keep_links=True) self.assertIn("[Atlas Retry Policy](/wiki/entities/atlas.md)", body) self.assertIn("[Maria](/wiki/entities/maria.md)", body) def test_frontmatter_title_wins_and_heading_stays(self): meta, body = convert.prepare("---\ntitle: Report\n---\n\n# Intro\n\nText\n", False) self.assertEqual(meta["title"], "Report") self.assertIn("# Intro", body) def test_heading_after_text_is_not_the_title(self): meta, body = convert.prepare("Some text.\n\n# Later\n", False) self.assertNotIn("title", meta) self.assertIn("# Later", body) class SchemaOrder(unittest.TestCase): def test_insert_respects_order(self): inner = '' out = convert.set_child(inner, "color", '', convert.RPR_ORDER) self.assertLess(out.index("w:color"), out.index("w:sz")) self.assertGreater(out.index("w:color"), out.index("w:rFonts")) out = convert.set_child(out, "sz", '', convert.RPR_ORDER) self.assertEqual(out.count(".*?", styles, re.S).group(0) self.assertIn('w:ascii="Georgia"', default) self.assertIn('', default) h1 = re.search(r']*w:styleId="Heading1".*?', styles, re.S).group(0) self.assertIn('', h1) self.assertEqual(h1.count("', doc) self.assertIn('w:top="1134"', doc) def test_convert_docx_and_refuse_overwrite(self): self.cli("new-style", "house") self.assertEqual(self.cli("convert", str(self.root / "outputs/documents/r.md")), 0) out = self.root / "outputs/documents/r.docx" self.assertTrue(out.exists()) doc = zipfile.ZipFile(out).read("word/document.xml").decode() self.assertIn("Status: Atlas", doc) self.assertNotIn("Ask the user", doc) self.assertEqual(self.cli("convert", str(self.root / "outputs/documents/r.md")), 1) self.assertEqual(self.cli("convert", str(self.root / "outputs/documents/r.md"), "--force"), 0) def test_dotx_becomes_docx(self): self.cli("new-style", "house") dotx = self.root / "Company.dotx" shutil.copyfile(self.root / "templates/styles/house.docx", dotx) convert.rewrite_zip(dotx, {"[Content_Types].xml": lambda x: x.replace(convert.DOCX_CT, convert.DOTX_CT)}) self.assertEqual(self.cli("new-style", "company", "--from", str(dotx)), 0) ct = zipfile.ZipFile(self.root / "templates/styles/company.docx").read("[Content_Types].xml").decode() self.assertIn(convert.DOCX_CT, ct) self.assertNotIn(convert.DOTX_CT, ct) def test_refusals(self): self.assertEqual(self.cli("new-style", "Bad Name"), 1) self.assertEqual(self.cli("edit-style", "nope", "--size", "10"), 1) self.assertEqual(self.cli("convert", str(self.root / "outputs/documents/r.md"), "--style", "nope", "--style-required"), 1) if __name__ == "__main__": unittest.main()