"""Tests for DOM graph visualization.""" import pytest from src.parser.html import parse_html, Element, Text from src.debug.dom_graph import generate_dot_graph, print_dom_tree class TestDOMGraph: def test_generate_dot_graph_empty(self): """Test generating graph for None document.""" dot = generate_dot_graph(None) assert "digraph DOM" in dot assert "Empty Document" in dot def test_generate_dot_graph_simple(self): """Test generating graph for simple HTML.""" html = "

Hello World

" doc = parse_html(html) dot = generate_dot_graph(doc) assert "digraph DOM" in dot assert "node_" in dot # Should have node IDs assert "" in dot assert "" in dot assert "

" in dot assert "Hello World" in dot def test_generate_dot_graph_with_attributes(self): """Test graph generation with element attributes.""" html = '

Content
' doc = parse_html(html) dot = generate_dot_graph(doc) assert "digraph DOM" in dot assert "
" in dot # Attributes should be included (at least some of them) assert "class" in dot or "id" in dot def test_generate_dot_graph_nested(self): """Test graph generation with nested elements.""" html = """

First

Second

""" doc = parse_html(html) dot = generate_dot_graph(doc) assert "digraph DOM" in dot assert "->" in dot # Should have edges assert "First" in dot assert "Second" in dot def test_generate_dot_graph_colors(self): """Test that different element types get different colors.""" html = "

Title

Text

" doc = parse_html(html) dot = generate_dot_graph(doc) # Check for color attributes assert "fillcolor=" in dot assert "lightgreen" in dot or "lightyellow" in dot or "lightgray" in dot def test_print_dom_tree_simple(self): """Test text tree representation.""" html = "

Hello

" doc = parse_html(html) tree = print_dom_tree(doc) assert "" in tree assert "" in tree assert "

" in tree assert "Hello" in tree def test_print_dom_tree_indentation(self): """Test that tree has proper indentation.""" html = "

Nested

" doc = parse_html(html) tree = print_dom_tree(doc) # Should have increasing indentation lines = tree.split('\n') # Find the nested

line - should be more indented than

p_line = [l for l in lines if '

' in l][0] div_line = [l for l in lines if '

' in l][0] # Count leading spaces p_indent = len(p_line) - len(p_line.lstrip()) div_indent = len(div_line) - len(div_line.lstrip()) assert p_indent > div_indent def test_print_dom_tree_max_depth(self): """Test that max_depth limits tree traversal.""" html = "

Deep

" doc = parse_html(html) tree_shallow = print_dom_tree(doc, max_depth=2) tree_deep = print_dom_tree(doc, max_depth=10) # Shallow should be shorter assert len(tree_shallow) < len(tree_deep) assert "..." in tree_shallow def test_generate_dot_graph_text_escaping(self): """Test that special characters in text are escaped.""" html = '

Text with "quotes" and newlines\n

' doc = parse_html(html) dot = generate_dot_graph(doc) # Should have escaped quotes assert '\\"' in dot or 'quotes' in dot # Should not have raw newlines breaking the DOT format lines = dot.split('\n') # All lines should be valid (no line starts with unexpected characters) for line in lines: if line.strip(): assert not line.strip().startswith('"') or line.strip().endswith(';') or line.strip().endswith(']') def test_generate_dot_graph_long_text_truncation(self): """Test that very long text nodes are truncated.""" long_text = "A" * 100 html = f"

{long_text}

" doc = parse_html(html) dot = generate_dot_graph(doc) # Should contain truncation marker assert "..." in dot