#!/usr/bin/env python3 """Tests for the MkDocs -> Discourse markdown converter. Run: python3 docs_sp/tools/test_converter.py """ import sys from pathlib import Path # Ensure the tools directory is importable sys.path.insert(0, str(Path(__file__).parent)) from converter import ( clean_blank_lines, convert, convert_admonitions, convert_emoji_shortcodes, convert_grid_cards, convert_tabs, resolve_internal_links, strip_front_matter, ) # --------------------------------------------------------------------------- # 1. Strip YAML Front Matter # --------------------------------------------------------------------------- def test_strip_front_matter_basic(): input_text = """--- title: My Page description: A test page --- # Hello """ expected = """# Hello """ result = strip_front_matter(input_text) assert result == expected, f"FAIL:\n{result!r}\n!=\n{expected!r}" print(" PASS: strip_front_matter_basic") def test_strip_front_matter_absent(): input_text = "# Hello\n\nContent here.\n" result = strip_front_matter(input_text) assert result == input_text, f"FAIL:\n{result!r}\n!=\n{input_text!r}" print(" PASS: strip_front_matter_absent") # --------------------------------------------------------------------------- # 2. Convert Admonitions # --------------------------------------------------------------------------- def test_basic_warning(): input_text = """!!! warning "Important" sunnypilot is a **driver assistance** system. Always pay attention. """ expected = """> [!WARNING] Important > sunnypilot is a **driver assistance** system. > Always pay attention. """ result = convert_admonitions(input_text) assert result == expected, f"FAIL basic_warning:\n{result!r}\n!=\n{expected!r}" print(" PASS: basic_warning") def test_info_no_title(): input_text = """!!! info Content line 1 Content line 2 """ expected = """> [!INFO] > Content line 1 > Content line 2 """ result = convert_admonitions(input_text) assert result == expected, f"FAIL info_no_title:\n{result!r}\n!=\n{expected!r}" print(" PASS: info_no_title") def test_info_with_title(): input_text = """!!! info "Requirements" - Longitudinal control must be available - ICBM must be enabled """ expected = """> [!INFO] Requirements > - Longitudinal control must be available > - ICBM must be enabled """ result = convert_admonitions(input_text) assert result == expected, f"FAIL info_with_title:\n{result!r}\n!=\n{expected!r}" print(" PASS: info_with_title") def test_danger(): input_text = """!!! danger "Important" sunnypilot is a **driver assistance** system. It is **NOT** a self-driving system. """ expected = """> [!DANGER] Important > sunnypilot is a **driver assistance** system. It is **NOT** a self-driving system. """ result = convert_admonitions(input_text) assert result == expected, f"FAIL danger:\n{result!r}\n!=\n{expected!r}" print(" PASS: danger") def test_tip(): input_text = """!!! tip The more detail you provide, the faster we can diagnose and fix the issue. """ expected = """> [!TIP] > The more detail you provide, the faster we can diagnose and fix the issue. """ result = convert_admonitions(input_text) assert result == expected, f"FAIL tip:\n{result!r}\n!=\n{expected!r}" print(" PASS: tip") def test_multiline_with_blank(): input_text = """!!! warning Line 1 Line 2 after blank """ expected = """> [!WARNING] > Line 1 > > Line 2 after blank """ result = convert_admonitions(input_text) assert result == expected, f"FAIL multiline_with_blank:\n{result!r}\n!=\n{expected!r}" print(" PASS: multiline_with_blank") def test_collapsible(): input_text = """??? warning "Click to see" Hidden content """ expected = """> [!WARNING] Click to see > Hidden content """ result = convert_admonitions(input_text) assert result == expected, f"FAIL collapsible:\n{result!r}\n!=\n{expected!r}" print(" PASS: collapsible") def test_collapsible_open(): input_text = """???+ info "Open by default" Visible content """ expected = """> [!INFO] Open by default > Visible content """ result = convert_admonitions(input_text) assert result == expected, f"FAIL collapsible_open:\n{result!r}\n!=\n{expected!r}" print(" PASS: collapsible_open") def test_surrounded_by_content(): input_text = """Some text before. !!! note "Note Title" Note content here. Some text after. """ expected = """Some text before. > [!NOTE] Note Title > Note content here. Some text after. """ result = convert_admonitions(input_text) assert result == expected, f"FAIL surrounded:\n{result!r}\n!=\n{expected!r}" print(" PASS: surrounded_by_content") def test_multiple_admonitions(): input_text = """!!! info "Requirements" - Req 1 - Req 2 !!! warning "Vehicle Restrictions" - Tesla: disabled on release - Rivian: always disabled """ expected = """> [!INFO] Requirements > - Req 1 > - Req 2 > [!WARNING] Vehicle Restrictions > - Tesla: disabled on release > - Rivian: always disabled """ result = convert_admonitions(input_text) assert result == expected, f"FAIL multiple:\n{result!r}\n!=\n{expected!r}" print(" PASS: multiple_admonitions") def test_real_doc_snippet(): """Test with an actual snippet from docs_sp content.""" input_text = """## Speed Limit Mode | Property | Value | |----------|-------| | **Param** | `SpeedLimitMode` | | **Type** | Multi-button selector | !!! info "Requirements" - Longitudinal control must be available, **or** ICBM must be enabled !!! warning "Vehicle Restrictions" - **Tesla:** Speed Limit Assist mode is disabled on release branches - **Rivian:** Speed Limit Assist mode is always disabled --- """ expected = """## Speed Limit Mode | Property | Value | |----------|-------| | **Param** | `SpeedLimitMode` | | **Type** | Multi-button selector | > [!INFO] Requirements > - Longitudinal control must be available, **or** ICBM must be enabled > [!WARNING] Vehicle Restrictions > - **Tesla:** Speed Limit Assist mode is disabled on release branches > - **Rivian:** Speed Limit Assist mode is always disabled --- """ result = convert_admonitions(input_text) assert result == expected, f"FAIL real_doc:\n{result!r}\n!=\n{expected!r}" print(" PASS: real_doc_snippet") # --------------------------------------------------------------------------- # 3. Convert Tabs # --------------------------------------------------------------------------- def test_tabs_basic(): input_text = """=== "Tab One" Content for tab one. === "Tab Two" Content for tab two. """ expected = """**Tab One** Content for tab one. --- **Tab Two** Content for tab two. --- """ result = convert_tabs(input_text) assert result == expected, f"FAIL tabs_basic:\n{result!r}\n!=\n{expected!r}" print(" PASS: tabs_basic") def test_tabs_multiline(): input_text = """=== "Details" Line 1 Line 2 Line 3 """ expected = """**Details** Line 1 Line 2 Line 3 --- """ result = convert_tabs(input_text) assert result == expected, f"FAIL tabs_multiline:\n{result!r}\n!=\n{expected!r}" print(" PASS: tabs_multiline") # --------------------------------------------------------------------------- # 4. Convert Grid Cards # --------------------------------------------------------------------------- def test_grid_cards_stripped(): input_text = """