From 4365f291aee22b4b4e42de0060c5606039ae60f1 Mon Sep 17 00:00:00 2001 From: nvaldez Date: Mon, 28 Sep 2026 21:32:46 +0200 Subject: [PATCH] test: add comprehensive tests for wiki/graph_generator.py to achieve 100% coverage Added tests for all uncovered code paths in graph_generator.py: - TestLoadGraphSettings: tests for loading settings from fiona-settings.json - MarkdownLinkParser: tests for fragment handling, wikilinks, external URLs, file parsing error handling, and link validation - WikiGraphMain: tests for directory validation, settings override, output directory creation, file writability checks, max_files limit, overwrite behavior, and exception handling This increases coverage from 85% to 100% for wiki/graph_generator.py (315/315 statements). Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe --- src/tests/test_graph_generator.py | 308 ++++++++++++++++++++++++++++++ 1 file changed, 308 insertions(+) diff --git a/src/tests/test_graph_generator.py b/src/tests/test_graph_generator.py index 42c5aa9..fbc948e 100644 --- a/src/tests/test_graph_generator.py +++ b/src/tests/test_graph_generator.py @@ -10,10 +10,70 @@ from wiki.graph_generator import ( HTMLGraphRenderer, MarkdownLinkParser, WikiGraph, + load_graph_settings, wiki_graph_main, ) +class TestLoadGraphSettings: + """Tests for load_graph_settings function.""" + + def test_load_graph_settings_not_found(self): + """Test loading settings when file doesn't exist.""" + settings = load_graph_settings("/nonexistent/path") + assert settings == {} + + def test_load_graph_settings_found(self, tmp_path): + """Test loading settings when file exists.""" + settings_dir = tmp_path / "subdir" + settings_dir.mkdir() + settings_file = settings_dir / "fiona-settings.json" + settings_file.write_text(json.dumps({ + "fiona": { + "wiki": { + "graph": { + "wikilinks_enabled": False, + "external_links": True, + "max_files": 100 + } + } + } + })) + + settings = load_graph_settings(str(settings_dir)) + assert settings == { + "wikilinks_enabled": False, + "external_links": True, + "max_files": 100 + } + + def test_load_graph_settings_invalid_json(self, tmp_path): + """Test loading settings with invalid JSON.""" + settings_file = tmp_path / "fiona-settings.json" + settings_file.write_text("invalid json") + + settings = load_graph_settings(str(tmp_path)) + assert settings == {} + + def test_load_graph_settings_parent_directory(self, tmp_path): + """Test loading settings from parent directory.""" + subdir = tmp_path / "subdir" + subdir.mkdir() + settings_file = tmp_path / "fiona-settings.json" + settings_file.write_text(json.dumps({ + "fiona": { + "wiki": { + "graph": { + "test_key": "test_value" + } + } + } + })) + + settings = load_graph_settings(str(subdir)) + assert settings == {"test_key": "test_value"} + + class TestMarkdownLinkParser: """Tests for MarkdownLinkParser class.""" @@ -158,6 +218,69 @@ Also [relative](./local.md) and [absolute](/path/to/wiki/abs.md). assert "http://github.com" in external_links assert "https://docs.example.com" in external_links + def test_extract_links_with_fragment(self): + """Test that links with fragments are filtered out.""" + parser = MarkdownLinkParser(wiki_root="/path/to/wiki") + content = "Link with [fragment](file.md#section)." + links = parser._extract_links_from_content(content) + assert len(links) == 0 + + def test_extract_wikilinks_with_fragment(self): + """Test that wikilinks with fragments are filtered out (line 112).""" + parser = MarkdownLinkParser(wiki_root="/path/to/wiki", wikilinks_enabled=True) + content = "Wikilink with [[file#section]]." + links = parser._extract_links_from_content(content) + assert len(links) == 0 + + def test_extract_wikilinks_with_external_url(self): + """Test that wikilinks with external URLs are filtered out.""" + parser = MarkdownLinkParser(wiki_root="/path/to/wiki", wikilinks_enabled=True) + content = "External wikilink: [[https://example.com]]." + links = parser._extract_links_from_content(content) + assert len(links) == 0 + + def test_is_external_or_fragment_with_fragment(self): + """Test fragment detection in _is_external_or_fragment (line 128).""" + parser = MarkdownLinkParser(wiki_root="/path/to/wiki") + assert parser._is_external_or_fragment("file.md#section") == True + assert parser._is_external_or_fragment("#section") == True + assert parser._is_external_or_fragment("file.md") == False + + def test_is_valid_markdown_link(self): + """Test _is_valid_markdown_link method (line 169).""" + parser = MarkdownLinkParser(wiki_root="/path/to/wiki") + # Valid markdown links + assert parser._is_valid_markdown_link("file.md") == True + assert parser._is_valid_markdown_link("path/to/file.md") == True + assert parser._is_valid_markdown_link("file") == True + # Invalid links + assert parser._is_valid_markdown_link("https://example.com") == False + assert parser._is_valid_markdown_link("file.md#section") == False + + def test_parse_file_nonexistent(self, tmp_path): + """Test parse_file with non-existent file (lines 286-288).""" + parser = MarkdownLinkParser(wiki_root=str(tmp_path)) + links = parser.parse_file("/nonexistent/file.md") + assert links == [] + + def test_parse_file_with_external_nonexistent(self, tmp_path): + """Test parse_file_with_external with non-existent file (lines 303-305).""" + parser = MarkdownLinkParser(wiki_root=str(tmp_path)) + internal, external = parser.parse_file_with_external("/nonexistent/file.md") + assert internal == [] + assert external == [] + + def test_parse_content_with_external_wikilink_external_url(self): + """Test parse_content_with_external with external URL wikilinks (lines 263-269).""" + parser = MarkdownLinkParser(wiki_root="/path/to/wiki", wikilinks_enabled=True) + content = "Wikilink: [[https://example.com]] and normal wikilink: [[file1]]." + internal, external = parser.parse_content_with_external(content, "/path/to/wiki/source.md") + assert len(external) == 1 + assert "https://example.com" in external + # Internal wikilink should be resolved + assert len(internal) == 1 + assert "/path/to/wiki/file1.md" in internal + class TestWikiGraph: """Tests for WikiGraph class.""" @@ -492,3 +615,188 @@ class TestWikiGraphMain: assert data["metadata"]["external_links_count"] == 2 assert "https://example.com" in data["metadata"]["external_links"] assert "https://another.com" in data["metadata"]["external_links"] + + def test_main_directory_not_found(self): + """Test main function with non-existent directory (lines 651-652).""" + result = wiki_graph_main(directory="/nonexistent/path") + assert result == 1 + + def test_main_with_settings_override(self, tmp_path): + """Test main function with settings override from fiona-settings.json (lines 651-652, 660, 663, 666).""" + # Create settings file + settings_file = tmp_path / "fiona-settings.json" + settings_file.write_text(json.dumps({ + "fiona": { + "wiki": { + "graph": { + "wikilinks_enabled": False, + "external_links": True, + "max_files": 5 + } + } + } + })) + + # Create markdown files with wikilinks + md_file1 = tmp_path / "file1.md" + md_file1.write_text("# File 1\n\nWikilink: [[file2]].") + md_file2 = tmp_path / "file2.md" + md_file2.write_text("# File 2\n") + + output_file = tmp_path / "graph.json" + + # Wikilinks should be disabled via settings + result = wiki_graph_main( + directory=str(tmp_path), + output=str(output_file), + format_type="json" + ) + assert result == 0 + + with open(output_file) as f: + data = json.load(f) + # With wikilinks disabled via settings, no edges should be created + assert len(data["edges"]) == 0 + + def test_main_output_directory_not_exists(self, tmp_path): + """Test main function when output directory doesn't exist (lines 678-683).""" + md_file1 = tmp_path / "file1.md" + md_file1.write_text("# File 1\n") + + # Output to a non-existent subdirectory + output_file = tmp_path / "nonexistent" / "graph.html" + + result = wiki_graph_main( + directory=str(tmp_path), + output=str(output_file) + ) + assert result == 0 + assert output_file.exists() + + def test_main_output_directory_creation_fails(self, tmp_path, monkeypatch): + """Test main function when output directory creation fails (lines 678-683).""" + md_file1 = tmp_path / "file1.md" + md_file1.write_text("# File 1\n") + + # Mock os.makedirs to raise an error + def mock_makedirs(path, exist_ok=False): + raise OSError("Permission denied") + + monkeypatch.setattr("os.makedirs", mock_makedirs) + + output_file = tmp_path / "restricted" / "graph.html" + + result = wiki_graph_main( + directory=str(tmp_path), + output=str(output_file) + ) + assert result == 1 + + def test_main_output_not_writable(self, tmp_path): + """Test main function when output file is not writable (lines 687-690).""" + md_file1 = tmp_path / "file1.md" + md_file1.write_text("# File 1\n") + + # Create a read-only output file + output_file = tmp_path / "graph.html" + output_file.write_text("existing content") + os.chmod(output_file, 0o444) # Read-only + + try: + result = wiki_graph_main( + directory=str(tmp_path), + output=str(output_file) + ) + assert result == 1 + finally: + # Restore permissions for cleanup + os.chmod(output_file, 0o644) + + def test_main_output_directory_not_writable(self, tmp_path): + """Test main function when output directory is not writable (lines 694-696).""" + md_file1 = tmp_path / "file1.md" + md_file1.write_text("# File 1\n") + + # Make the tmp_path directory read-only + os.chmod(tmp_path, 0o555) # Read and execute only + + output_file = tmp_path / "graph.html" + + try: + result = wiki_graph_main( + directory=str(tmp_path), + output=str(output_file) + ) + assert result == 1 + finally: + # Restore permissions for cleanup + os.chmod(tmp_path, 0o755) + + def test_main_max_files_limit(self, tmp_path): + """Test main function respects max_files limit from settings (lines 712-713).""" + # Create settings file with max_files + settings_file = tmp_path / "fiona-settings.json" + settings_file.write_text(json.dumps({ + "fiona": { + "wiki": { + "graph": { + "max_files": 5 + } + } + } + })) + + # Create more files than max_files + for i in range(10): + md_file = tmp_path / f"file{i}.md" + md_file.write_text(f"# File {i}\n") + + output_file = tmp_path / "graph.json" + + result = wiki_graph_main( + directory=str(tmp_path), + output=str(output_file), + format_type="json", + include_pattern="*.md" + ) + assert result == 0 + + with open(output_file) as f: + data = json.load(f) + # Should only process first 5 files + assert len(data["nodes"]) == 5 + + def test_main_overwrite_existing_file(self, tmp_path): + """Test main function overwrites existing file (line 753).""" + md_file1 = tmp_path / "file1.md" + md_file1.write_text("# File 1\n") + + output_file = tmp_path / "graph.html" + output_file.write_text("old content") + + result = wiki_graph_main( + directory=str(tmp_path), + output=str(output_file) + ) + assert result == 0 + + # File should be overwritten + content = output_file.read_text() + assert "old content" not in content + assert "File 1" in content or "file1.md" in content + + def test_main_exception_handling(self, tmp_path, monkeypatch): + """Test main function exception handling (lines 766-769).""" + # Mock glob.glob to raise an exception + def mock_glob(pattern, root_dir=None, recursive=False): + raise OSError("glob error") + + monkeypatch.setattr("glob.glob", mock_glob) + + output_file = tmp_path / "graph.html" + + result = wiki_graph_main( + directory=str(tmp_path), + output=str(output_file) + ) + assert result == 1