"""Tests for DOM graph visualization.""" from src.parser.html import parse_html 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 = '
First
Second
Text
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
line - should be more indented than
' in line][0] div_line = [line for line in lines if '
Deep
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