"""Tests of the one-off migration helpers (run with: python3 -m unittest discover -s tools).""" import unittest import migrate class HtmlToMarkdown(unittest.TestCase): def test_paragraph_with_inline_markup(self): html = '
\nHello world and a link and this and code.\n
\nSend to bc1q\n
\nBody.\n
\n| 14 days | \n
\n\n
\nA note.\n
', '\nshop.\n
', ] for html in cases: with self.subTest(html=html): self.assertEqual(migrate.html_to_md(html), html + "\n") def test_unknown_text_between_blocks_fails(self): with self.assertRaises(ValueError): migrate.html_to_md("\nfine\n
\nstray text\n\nmore\n
") class Placeholders(unittest.TestCase): def test_restore_replaces_literal_values(self): values = {"btc": "bc1qabc", "contact_shown": "squid (at) kmalloc.org", "gpg_key": "/squid.asc"} md = "Write to **squid (at) kmalloc.org**. [squid.asc](/squid.asc) and bc1qabc." self.assertEqual(migrate.restore(md, values), "Write to **{{contact_shown}}**. [squid.asc]({{gpg_key}}) and {{btc}}.") def test_restore_prefers_the_longest_value(self): values = {"a": "https://x.org/", "b": "https://x.org/search?q=1"} self.assertEqual(migrate.restore("see https://x.org/search?q=1", values), "see {{b}}") class Content(unittest.TestCase): def test_extract_content_between_header_and_footer(self): page = "\n\nB\n
\n\n\n" self.assertEqual(migrate.extract_content(page), "\nB\n
\n") def test_page_file_name(self): cases = [("/", "en", "en", "index.md"), ("/manifesto.html", "en", "en", "manifesto.md"), ("/news/", "en", "en", "news/index.md"), ("/it/", "en", "it", "index.md"), ("/it/servizi.html", "en", "it", "servizi.md")] for path, default, lang, want in cases: with self.subTest(path=path): self.assertEqual(migrate.page_file(path, default, lang), want) def test_page_header(self): header = migrate.page_header("kmalloc.org: News", "news", "Updates.", "bare", note="", switch={"en": "/?lang=en"}) self.assertEqual(header, "Title: kmalloc.org: News\nID: news\nDescription: Updates.\nLayout: bare\nSwitch: en=/?lang=en\n-\n") def test_page_header_with_manifesto_version(self): header = migrate.page_header("M", "manifesto", "d", "manifesto", note="N", manifesto_version="1.0", manifesto_date="2026-11-12") self.assertEqual(header, "Title: M\nID: manifesto\nDescription: d\nLayout: manifesto\nNote: N\n" "ManifestoVersion: 1.0\nManifestoDate: 2026-11-12\n-\n") def test_post_conversion(self): old = "---\ntitle: A shop\nsummary: About the shop.\n---\n\nThere is a shop.\n" name, text = migrate.convert_post("2026-11-14-shop.md", old) self.assertEqual(name, "shop.md") self.assertEqual(text, "Title: A shop\nAuthor: squid\nDate: 2026-11-14T12:00:00+0100\nCategories: News\n" "Description: About the shop.\n-\n\nThere is a shop.\n") def test_post_without_summary_has_no_description(self): _, text = migrate.convert_post("2026-11-12-x.md", "---\ntitle: X\n---\nBody.\n") self.assertNotIn("Description:", text) def test_block_markers(self): home = '\nA\n
\n\n\nIntro. All.\n
\n\n\nA\n
\n\n{{latest_news}}
\n') news = '\nIntro.\n
\n\n\nRSS feed\n
\n' self.assertEqual(migrate.mark_news_list(news), '\nIntro.\n
\n\n{{news_list}}
\n\n\nRSS feed\n
\n') if __name__ == "__main__": unittest.main()