#!/usr/bin/env python3 """ Test suite for Issue #136: Index page for notes in a directory This test suite validates the index page generation functionality for HTML files, including directory scanning, HTML generation, and CLI integration. TDD8 Workflow: ISSUE→TEST→RED→GREEN→REFACTOR→DOCUMENT→REFINE→PUBLISH State: RED (Tests should fail initially) """ import pytest import tempfile import os import shutil from pathlib import Path from unittest.mock import patch, MagicMock import subprocess import re from html.parser import HTMLParser class SimpleHTMLParser(HTMLParser): """Simple HTML parser to extract title and links for testing.""" def __init__(self): super().__init__() self.title = None self.links = [] self.in_title = False def handle_starttag(self, tag, attrs): if tag == 'title': self.in_title = True elif tag == 'a': href = dict(attrs).get('href', '') self.links.append({'href': href, 'text': ''}) def handle_endtag(self, tag): if tag == 'title': self.in_title = False def handle_data(self, data): if self.in_title: self.title = data.strip() elif self.links and not self.links[-1]['text']: self.links[-1]['text'] = data.strip() class TestHTMLFileDiscovery: """Test HTML file discovery and processing.""" def setup_method(self): """Set up test environment with temporary directories and files.""" self.temp_dir = tempfile.mkdtemp() self.test_dir = Path(self.temp_dir) / "test_notes" self.test_dir.mkdir() # Create test HTML files (self.test_dir / "index.html").write_text("""
Main index
""") (self.test_dir / "document1.html").write_text("""Content here
""") (self.test_dir / "notes.html").write_text("""Note content
""") # Create subdirectory with HTML files sub_dir = self.test_dir / "subdir" sub_dir.mkdir() (sub_dir / "subdoc.html").write_text("""Sub content
""") # Create non-HTML files (should be ignored) (self.test_dir / "readme.txt").write_text("Not HTML") (self.test_dir / "image.png").write_bytes(b"fake image data") def teardown_method(self): """Clean up test environment.""" shutil.rmtree(self.temp_dir) def test_find_html_files_in_directory(self): """Test finding all HTML files in a directory.""" from markitect.plugins.builtin.markdown_commands import find_html_files html_files = find_html_files(self.test_dir) expected_files = [ self.test_dir / "index.html", self.test_dir / "document1.html", self.test_dir / "notes.html" ] assert len(html_files) == 3 for expected_file in expected_files: assert expected_file in html_files def test_find_html_files_recursively(self): """Test finding HTML files recursively in subdirectories.""" from markitect.plugins.builtin.markdown_commands import find_html_files html_files = find_html_files(self.test_dir, recursive=True) expected_files = [ self.test_dir / "index.html", self.test_dir / "document1.html", self.test_dir / "notes.html", self.test_dir / "subdir" / "subdoc.html" ] assert len(html_files) == 4 for expected_file in expected_files: assert expected_file in html_files def test_extract_title_from_html_file(self): """Test extracting title from HTML file.""" from markitect.plugins.builtin.markdown_commands import extract_html_title title = extract_html_title(self.test_dir / "document1.html") assert title == "Document One" title = extract_html_title(self.test_dir / "notes.html") assert title == "My Notes" def test_extract_title_from_h1_if_no_title_tag(self): """Test extracting title from H1 tag if no title tag exists.""" from markitect.plugins.builtin.markdown_commands import extract_html_title # Create HTML file without title tag no_title_file = self.test_dir / "no_title.html" no_title_file.write_text("""Content
""") title = extract_html_title(no_title_file) assert title == "Header Title" def test_extract_title_fallback_to_filename(self): """Test falling back to filename if no title or H1 found.""" from markitect.plugins.builtin.markdown_commands import extract_html_title # Create HTML file without title or H1 plain_file = self.test_dir / "plain_file.html" plain_file.write_text("""Just content
""") title = extract_html_title(plain_file) assert title == "plain_file" class TestIndexPageGeneration: """Test index page HTML generation.""" def setup_method(self): """Set up test environment.""" self.temp_dir = tempfile.mkdtemp() self.test_dir = Path(self.temp_dir) / "test_notes" self.test_dir.mkdir() def teardown_method(self): """Clean up test environment.""" shutil.rmtree(self.temp_dir) def test_generate_index_html_structure(self): """Test generating basic index HTML structure.""" from markitect.plugins.builtin.markdown_commands import generate_index_html html_files = [ {"path": self.test_dir / "doc1.html", "title": "Document One", "relative_path": "doc1.html"}, {"path": self.test_dir / "doc2.html", "title": "Document Two", "relative_path": "doc2.html"} ] html_content = generate_index_html(html_files, "Test Directory Index") # Parse HTML to verify structure parser = SimpleHTMLParser() parser.feed(html_content) assert parser.title == "Test Directory Index" # Check for navigation list assert "