"""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

' self.assertEqual(migrate.html_to_md(html), "Hello **world** and [a link](/x.html) and *this* and `code`.\n") def test_link_around_code(self): html = '

\nSend to bc1q\n

' self.assertEqual(migrate.html_to_md(html), "Send to [`bc1q`](bitcoin:bc1q)\n") def test_heading_with_id(self): self.assertEqual(migrate.html_to_md('

Request an account

'), "## Request an account {#account}\n") self.assertEqual(migrate.html_to_md("

Services

"), "# Services\n") def test_list(self): html = '' self.assertEqual(migrate.html_to_md(html), "- one\n- [two](/a)\n") def test_blocks_are_separated_by_a_blank_line(self): html = "

T

\n\n

\nBody.\n

\n\n" self.assertEqual(migrate.html_to_md(html), "# T\n\nBody.\n\n- x\n") def test_table_stays_raw(self): table = '\n\n\n\n\n\n\n
C
14 days
' self.assertEqual(migrate.html_to_md(table), table + "\n") def test_comment_stays_raw(self): self.assertEqual(migrate.html_to_md(""), "\n") def test_raw_when_there_are_attributes_or_other_tags(self): cases = [ '

\n\n

', '

Technology.
Freedom.
Dissent.

', '

Version 1.2,

', '

\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
\n\n
\n\n

T

\n\n

\nB\n

\n\n\n" self.assertEqual(migrate.extract_content(page), "

T

\n\n

\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

News

\n\n

\nIntro. All.\n

\n\n\n' self.assertEqual(migrate.mark_latest_news(home), '

\nA\n

\n\n

{{latest_news}}

\n') news = '

News

\n\n

\nIntro.\n

\n\n\n\n

\nRSS feed\n

\n' self.assertEqual(migrate.mark_news_list(news), '

News

\n\n

\nIntro.\n

\n\n

{{news_list}}

\n\n

\nRSS feed\n

\n') if __name__ == "__main__": unittest.main()