Public Access
Template
806 lines
30 KiB
Python
806 lines
30 KiB
Python
"""Tests for wiki graph_generator module.
|
|
|
|
Test cases for MarkdownLinkParser, WikiGraph, and HTMLGraphRenderer classes.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
|
|
from fiona.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."""
|
|
|
|
def test_parse_standard_markdown_links(self):
|
|
"""Test parsing standard markdown links: [text](path/to/file.md)."""
|
|
parser = MarkdownLinkParser(wiki_root="/path/to/wiki")
|
|
content = "This is a link to [file1](wiki/concepts/file1.md)."
|
|
links = parser._extract_links_from_content(content)
|
|
assert len(links) == 1
|
|
assert links[0] == "wiki/concepts/file1.md"
|
|
|
|
def test_parse_multiple_standard_links(self):
|
|
"""Test parsing multiple standard markdown links."""
|
|
parser = MarkdownLinkParser(wiki_root="/path/to/wiki")
|
|
content = """
|
|
Link to [file1](wiki/concepts/file1.md) and [file2](wiki/concepts/file2.md).
|
|
Also [file3](wiki/other/file3.md).
|
|
"""
|
|
links = parser._extract_links_from_content(content)
|
|
assert len(links) == 3
|
|
assert "wiki/concepts/file1.md" in links
|
|
assert "wiki/concepts/file2.md" in links
|
|
assert "wiki/other/file3.md" in links
|
|
|
|
def test_parse_relative_paths(self):
|
|
"""Test parsing relative paths: ./file.md, ../dir/file.md."""
|
|
parser = MarkdownLinkParser(wiki_root="/path/to/wiki")
|
|
content = """
|
|
Relative: [file1](./file1.md) and [file2](../dir/file2.md).
|
|
"""
|
|
links = parser._extract_links_from_content(content)
|
|
assert len(links) == 2
|
|
assert "./file1.md" in links
|
|
assert "../dir/file2.md" in links
|
|
|
|
def test_parse_absolute_paths(self):
|
|
"""Test parsing absolute paths within wiki root."""
|
|
parser = MarkdownLinkParser(wiki_root="/path/to/wiki")
|
|
content = "Absolute: [file1](/path/to/wiki/file1.md)."
|
|
links = parser._extract_links_from_content(content)
|
|
assert len(links) == 1
|
|
assert "/path/to/wiki/file1.md" in links
|
|
|
|
def test_parse_wikilinks_enabled(self):
|
|
"""Test parsing wikilinks format: [[file]], [[dir/file]]."""
|
|
parser = MarkdownLinkParser(wiki_root="/path/to/wiki", wikilinks_enabled=True)
|
|
content = "Wikilinks: [[file1]] and [[dir/file2]]."
|
|
links = parser._extract_links_from_content(content)
|
|
assert len(links) == 2
|
|
assert "file1" in links
|
|
assert "dir/file2" in links
|
|
|
|
def test_parse_wikilinks_disabled(self):
|
|
"""Test that wikilinks are not parsed when disabled."""
|
|
parser = MarkdownLinkParser(wiki_root="/path/to/wiki", wikilinks_enabled=False)
|
|
content = "Wikilinks: [[file1]] and [[dir/file2]]."
|
|
links = parser._extract_links_from_content(content)
|
|
assert len(links) == 0
|
|
|
|
def test_resolve_link_paths(self):
|
|
"""Test resolving link paths to absolute paths."""
|
|
parser = MarkdownLinkParser(wiki_root="/path/to/wiki")
|
|
|
|
# Test relative path resolution with ./ (source in /path/to/wiki/dir/)
|
|
resolved = parser._resolve_link_path("./file.md", "/path/to/wiki/dir/source.md")
|
|
assert resolved == "/path/to/wiki/dir/file.md"
|
|
|
|
# Test parent path resolution (go up from /path/to/wiki/dir/ to /path/to/wiki/)
|
|
resolved = parser._resolve_link_path("../file.md", "/path/to/wiki/dir/source.md")
|
|
assert resolved == "/path/to/wiki/file.md"
|
|
|
|
# Test absolute path within wiki root
|
|
resolved = parser._resolve_link_path("/path/to/wiki/file.md", "/path/to/wiki/dir/source.md")
|
|
assert resolved == "/path/to/wiki/file.md"
|
|
|
|
# Test path without ./ or ../ (resolved relative to source directory)
|
|
resolved = parser._resolve_link_path("concepts/file.md", "/path/to/wiki/dir/source.md")
|
|
assert resolved == "/path/to/wiki/dir/concepts/file.md"
|
|
|
|
def test_parse_file(self):
|
|
"""Test parsing a complete markdown file."""
|
|
parser = MarkdownLinkParser(wiki_root="/path/to/wiki")
|
|
content = """
|
|
# Title
|
|
|
|
This links to [file1](concepts/file1.md) and [[file2]].
|
|
|
|
Also [relative](./local.md) and [absolute](/path/to/wiki/abs.md).
|
|
"""
|
|
links = parser.parse_content(content, "/path/to/wiki/source.md")
|
|
assert len(links) == 4
|
|
# concepts/file1.md is resolved relative to source directory (/path/to/wiki/)
|
|
assert "/path/to/wiki/concepts/file1.md" in links
|
|
# [[file2]] becomes file2.md (wikilink with extension added)
|
|
assert "/path/to/wiki/file2.md" in links
|
|
# ./local.md is resolved relative to source directory
|
|
assert "/path/to/wiki/local.md" in links
|
|
# Absolute path
|
|
assert "/path/to/wiki/abs.md" in links
|
|
|
|
def test_identify_external_links(self):
|
|
"""Test that external URLs are identified and filtered out."""
|
|
parser = MarkdownLinkParser(wiki_root="/path/to/wiki")
|
|
content = """
|
|
External: [Google](https://www.google.com) and [GitHub](http://github.com).
|
|
Also [FTP](ftp://example.com/file.zip).
|
|
"""
|
|
# Internal links should be empty
|
|
links = parser._extract_links_from_content(content)
|
|
assert len(links) == 0
|
|
|
|
def test_parse_mixed_internal_and_external_links(self):
|
|
"""Test parsing file with both internal and external links."""
|
|
parser = MarkdownLinkParser(wiki_root="/path/to/wiki")
|
|
content = """
|
|
Internal: [file1](wiki/file1.md) and external: [Google](https://www.google.com).
|
|
Also [file2](wiki/file2.md) and [GitHub](http://github.com).
|
|
"""
|
|
links = parser._extract_links_from_content(content)
|
|
# Should only get internal links
|
|
assert len(links) == 2
|
|
assert "wiki/file1.md" in links
|
|
assert "wiki/file2.md" in links
|
|
|
|
def test_track_external_links_per_file(self):
|
|
"""Test tracking external links count per file."""
|
|
parser = MarkdownLinkParser(wiki_root="/path/to/wiki")
|
|
content = """
|
|
Internal: [file1](wiki/file1.md).
|
|
External: [Google](https://www.google.com) and [GitHub](http://github.com).
|
|
More external: [Docs](https://docs.example.com).
|
|
Another internal: [file2](wiki/file2.md).
|
|
"""
|
|
# Parse with tracking enabled
|
|
internal_links, external_links = parser.parse_content_with_external(content, "/path/to/wiki/source.md")
|
|
assert len(internal_links) == 2
|
|
assert len(external_links) == 3
|
|
# Internal links are resolved to absolute paths relative to source file
|
|
assert "/path/to/wiki/wiki/file1.md" in internal_links
|
|
assert "/path/to/wiki/wiki/file2.md" in internal_links
|
|
assert "https://www.google.com" in external_links
|
|
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."""
|
|
|
|
def test_add_node(self):
|
|
"""Test adding nodes to the graph."""
|
|
graph = WikiGraph()
|
|
graph.add_node("wiki/file1.md")
|
|
graph.add_node("wiki/file2.md")
|
|
assert len(graph.nodes) == 2
|
|
assert "wiki/file1.md" in graph.nodes
|
|
assert "wiki/file2.md" in graph.nodes
|
|
|
|
def test_add_edge(self):
|
|
"""Test adding edges between nodes."""
|
|
graph = WikiGraph()
|
|
graph.add_node("wiki/file1.md")
|
|
graph.add_node("wiki/file2.md")
|
|
graph.add_edge("wiki/file1.md", "wiki/file2.md")
|
|
assert len(graph.edges) == 1
|
|
assert ("wiki/file1.md", "wiki/file2.md") in graph.edges
|
|
|
|
def test_add_edge_increases_weight(self):
|
|
"""Test that adding the same edge multiple times increases weight."""
|
|
graph = WikiGraph()
|
|
graph.add_node("wiki/file1.md")
|
|
graph.add_node("wiki/file2.md")
|
|
graph.add_edge("wiki/file1.md", "wiki/file2.md")
|
|
graph.add_edge("wiki/file1.md", "wiki/file2.md")
|
|
assert graph.edges[("wiki/file1.md", "wiki/file2.md")] == 2
|
|
|
|
def test_get_in_degree(self):
|
|
"""Test getting in-degree of a node."""
|
|
graph = WikiGraph()
|
|
graph.add_node("wiki/file1.md")
|
|
graph.add_node("wiki/file2.md")
|
|
graph.add_node("wiki/file3.md")
|
|
graph.add_edge("wiki/file1.md", "wiki/file2.md")
|
|
graph.add_edge("wiki/file3.md", "wiki/file2.md")
|
|
assert graph.get_in_degree("wiki/file2.md") == 2
|
|
assert graph.get_in_degree("wiki/file1.md") == 0
|
|
|
|
def test_get_out_degree(self):
|
|
"""Test getting out-degree of a node."""
|
|
graph = WikiGraph()
|
|
graph.add_node("wiki/file1.md")
|
|
graph.add_node("wiki/file2.md")
|
|
graph.add_node("wiki/file3.md")
|
|
graph.add_edge("wiki/file1.md", "wiki/file2.md")
|
|
graph.add_edge("wiki/file1.md", "wiki/file3.md")
|
|
assert graph.get_out_degree("wiki/file1.md") == 2
|
|
assert graph.get_out_degree("wiki/file2.md") == 0
|
|
|
|
def test_identify_orphans(self):
|
|
"""Test identifying orphan files (no incoming or outgoing links)."""
|
|
graph = WikiGraph()
|
|
graph.add_node("wiki/file1.md")
|
|
graph.add_node("wiki/file2.md")
|
|
graph.add_node("wiki/file3.md")
|
|
graph.add_edge("wiki/file1.md", "wiki/file2.md")
|
|
|
|
orphans = graph.get_orphans()
|
|
assert len(orphans) == 1
|
|
assert "wiki/file3.md" in orphans
|
|
|
|
def test_build_from_files(self):
|
|
"""Test building graph from file links."""
|
|
graph = WikiGraph()
|
|
file_links = {
|
|
"wiki/file1.md": ["wiki/file2.md", "wiki/file3.md"],
|
|
"wiki/file2.md": ["wiki/file3.md"],
|
|
"wiki/file3.md": []
|
|
}
|
|
graph.build_from_files(file_links)
|
|
assert len(graph.nodes) == 3
|
|
assert len(graph.edges) == 3
|
|
assert graph.get_in_degree("wiki/file3.md") == 2
|
|
|
|
def test_add_external_link(self):
|
|
"""Test adding external links to the graph."""
|
|
graph = WikiGraph()
|
|
graph.add_external_link("wiki/file1.md", "https://example.com")
|
|
graph.add_external_link("wiki/file1.md", "https://another.com")
|
|
graph.add_external_link("wiki/file2.md", "https://third.com")
|
|
|
|
assert graph.get_external_links_count() == 3
|
|
assert len(graph.get_all_external_links()) == 3
|
|
|
|
external_per_file = graph.get_external_links_per_file()
|
|
assert external_per_file["wiki/file1.md"] == 2
|
|
assert external_per_file["wiki/file2.md"] == 1
|
|
|
|
def test_build_from_files_with_external(self):
|
|
"""Test building graph with external links."""
|
|
graph = WikiGraph()
|
|
file_links = {
|
|
"wiki/file1.md": ["wiki/file2.md"],
|
|
"wiki/file2.md": []
|
|
}
|
|
external_links = {
|
|
"wiki/file1.md": ["https://example.com", "https://another.com"],
|
|
"wiki/file2.md": ["https://third.com"]
|
|
}
|
|
graph.build_from_files_with_external(file_links, external_links)
|
|
|
|
assert len(graph.nodes) == 2
|
|
assert len(graph.edges) == 1
|
|
assert graph.get_external_links_count() == 3
|
|
assert len(graph.get_all_external_links()) == 3
|
|
|
|
|
|
class TestHTMLGraphRenderer:
|
|
"""Tests for HTMLGraphRenderer class."""
|
|
|
|
def test_generate_html_basic(self):
|
|
"""Test generating basic HTML with graph data."""
|
|
graph = WikiGraph()
|
|
graph.add_node("wiki/file1.md")
|
|
graph.add_node("wiki/file2.md")
|
|
graph.add_edge("wiki/file1.md", "wiki/file2.md")
|
|
|
|
renderer = HTMLGraphRenderer(graph)
|
|
html = renderer.generate_html()
|
|
|
|
assert "<!DOCTYPE html>" in html
|
|
assert "wiki/file1.md" in html
|
|
assert "wiki/file2.md" in html
|
|
|
|
def test_generate_html_includes_d3js(self):
|
|
"""Test that generated HTML includes D3.js library."""
|
|
graph = WikiGraph()
|
|
renderer = HTMLGraphRenderer(graph)
|
|
html = renderer.generate_html()
|
|
|
|
assert "d3.js" in html or "https://d3js.org" in html
|
|
|
|
def test_generate_html_has_valid_javascript(self):
|
|
"""Test that generated HTML contains valid JavaScript method chains.
|
|
|
|
This catches errors like calling .numIterations() on the wrong object,
|
|
which would cause 'is not a function' errors in the browser.
|
|
"""
|
|
graph = WikiGraph()
|
|
graph.add_node("wiki/file1.md")
|
|
graph.add_node("wiki/file2.md")
|
|
graph.add_edge("wiki/file1.md", "wiki/file2.md")
|
|
|
|
renderer = HTMLGraphRenderer(graph)
|
|
html = renderer.generate_html()
|
|
|
|
# Check for valid D3 force simulation method chains
|
|
# The simulation should have .alphaDecay(), .alphaMin(), .velocityDecay()
|
|
assert ".alphaDecay(" in html
|
|
assert ".alphaMin(" in html
|
|
assert ".velocityDecay(" in html
|
|
|
|
# Check for valid force configurations
|
|
assert "forceSimulation" in html
|
|
assert "forceLink" in html
|
|
assert "forceManyBody" in html
|
|
assert "forceCenter" in html
|
|
assert "forceCollide" in html
|
|
|
|
# Check that forces are properly chained with .id(), .distance(), etc.
|
|
assert ".id(" in html
|
|
assert ".distance(" in html
|
|
assert ".strength(" in html
|
|
|
|
def test_generate_html_has_edge_highlight_on_hover(self):
|
|
"""Test that generated HTML includes edge highlighting on node hover."""
|
|
graph = WikiGraph()
|
|
graph.add_node("wiki/file1.md")
|
|
graph.add_node("wiki/file2.md")
|
|
graph.add_node("wiki/file3.md")
|
|
graph.add_edge("wiki/file1.md", "wiki/file2.md")
|
|
graph.add_edge("wiki/file1.md", "wiki/file3.md")
|
|
|
|
renderer = HTMLGraphRenderer(graph)
|
|
html = renderer.generate_html()
|
|
|
|
# Check that node hover handler includes edge highlighting logic
|
|
# Should contain code that sets opacity on hover to dim non-connected edges
|
|
assert "linkD.source.id" in html or "linkD.source" in html
|
|
assert "return 0.2" in html # Dimmed opacity for non-connected edges
|
|
|
|
def test_generate_json(self):
|
|
"""Test generating JSON output."""
|
|
graph = WikiGraph()
|
|
graph.add_node("wiki/file1.md")
|
|
graph.add_node("wiki/file2.md")
|
|
graph.add_edge("wiki/file1.md", "wiki/file2.md")
|
|
|
|
renderer = HTMLGraphRenderer(graph)
|
|
json_output = renderer.generate_json(" /path/to/wiki")
|
|
|
|
data = json.loads(json_output)
|
|
assert "metadata" in data
|
|
assert "nodes" in data
|
|
assert "edges" in data
|
|
assert len(data["nodes"]) == 2
|
|
assert len(data["edges"]) == 1
|
|
|
|
def test_generate_json_with_external_links(self):
|
|
"""Test generating JSON output with external links."""
|
|
graph = WikiGraph()
|
|
graph.add_node("wiki/file1.md")
|
|
graph.add_node("wiki/file2.md")
|
|
graph.add_edge("wiki/file1.md", "wiki/file2.md")
|
|
graph.add_external_link("wiki/file1.md", "https://example.com")
|
|
graph.add_external_link("wiki/file1.md", "https://another.com")
|
|
graph.add_external_link("wiki/file2.md", "https://third.com")
|
|
|
|
renderer = HTMLGraphRenderer(graph)
|
|
json_output = renderer.generate_json(" /path/to/wiki")
|
|
|
|
data = json.loads(json_output)
|
|
assert "metadata" in data
|
|
assert "external_links_count" in data["metadata"]
|
|
assert "external_links" in data["metadata"]
|
|
assert data["metadata"]["external_links_count"] == 3
|
|
assert len(data["metadata"]["external_links"]) == 3
|
|
assert "https://example.com" in data["metadata"]["external_links"]
|
|
|
|
|
|
class TestWikiGraphMain:
|
|
"""Tests for wiki_graph_main CLI entry point."""
|
|
|
|
def test_main_with_html_output(self, tmp_path):
|
|
"""Test main function with HTML output."""
|
|
# Create temporary markdown files
|
|
md_file1 = tmp_path / "file1.md"
|
|
md_file1.write_text("# File 1\n\nLink to [file2](file2.md).")
|
|
md_file2 = tmp_path / "file2.md"
|
|
md_file2.write_text("# File 2\n")
|
|
|
|
output_file = tmp_path / "graph.html"
|
|
|
|
result = wiki_graph_main(
|
|
directory=str(tmp_path),
|
|
output=str(output_file),
|
|
format_type="html"
|
|
)
|
|
assert result == 0
|
|
assert output_file.exists()
|
|
|
|
def test_main_with_json_output(self, tmp_path):
|
|
"""Test main function with JSON output."""
|
|
md_file1 = tmp_path / "file1.md"
|
|
md_file1.write_text("# File 1\n\nLink to [file2](file2.md).")
|
|
md_file2 = tmp_path / "file2.md"
|
|
md_file2.write_text("# File 2\n")
|
|
|
|
output_file = tmp_path / "graph.json"
|
|
|
|
result = wiki_graph_main(
|
|
directory=str(tmp_path),
|
|
output=str(output_file),
|
|
format_type="json"
|
|
)
|
|
assert result == 0
|
|
assert output_file.exists()
|
|
|
|
with open(output_file) as f:
|
|
data = json.load(f)
|
|
assert "nodes" in data
|
|
assert "edges" in data
|
|
|
|
def test_main_default_output(self, tmp_path, monkeypatch):
|
|
"""Test main function with default output file."""
|
|
md_file1 = tmp_path / "file1.md"
|
|
md_file1.write_text("# File 1\n")
|
|
|
|
# Change to tmp_path
|
|
original_cwd = os.getcwd()
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
try:
|
|
result = wiki_graph_main(
|
|
directory=str(tmp_path)
|
|
)
|
|
assert result == 0
|
|
assert (tmp_path / "wiki_graph.html").exists()
|
|
finally:
|
|
os.chdir(original_cwd)
|
|
|
|
def test_main_wikilinks_disabled(self, tmp_path):
|
|
"""Test main function with wikilinks disabled."""
|
|
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"
|
|
|
|
result = wiki_graph_main(
|
|
directory=str(tmp_path),
|
|
output=str(output_file),
|
|
format_type="json",
|
|
wikilinks=False
|
|
)
|
|
assert result == 0
|
|
|
|
with open(output_file) as f:
|
|
data = json.load(f)
|
|
# With wikilinks disabled, the wikilink shouldn't create an edge
|
|
assert len(data["edges"]) == 0
|
|
|
|
def test_main_with_external_links(self, tmp_path):
|
|
"""Test main function with external links tracking enabled."""
|
|
md_file1 = tmp_path / "file1.md"
|
|
md_file1.write_text("# File 1\n\n[Internal](file2.md) and [External](https://example.com).")
|
|
md_file2 = tmp_path / "file2.md"
|
|
md_file2.write_text("# File 2\n\n[Another External](https://another.com).")
|
|
|
|
output_file = tmp_path / "graph.json"
|
|
|
|
result = wiki_graph_main(
|
|
directory=str(tmp_path),
|
|
output=str(output_file),
|
|
format_type="json",
|
|
external_links=True
|
|
)
|
|
assert result == 0
|
|
|
|
with open(output_file) as f:
|
|
data = json.load(f)
|
|
|
|
# Should have internal link
|
|
assert len(data["edges"]) == 1
|
|
# Should have external links in metadata
|
|
assert "external_links_count" in data["metadata"]
|
|
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, monkeypatch):
|
|
"""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")
|
|
|
|
# AI/LLM: chmod does not clear directory write access on Windows.
|
|
target = os.path.normcase(os.path.abspath(tmp_path))
|
|
real_access = os.access
|
|
|
|
def deny_directory_write(path, mode):
|
|
if os.path.normcase(os.path.abspath(path)) == target and mode == os.W_OK:
|
|
return False
|
|
return real_access(path, mode)
|
|
|
|
monkeypatch.setattr("fiona.wiki.graph_generator.os.access", deny_directory_write)
|
|
|
|
output_file = tmp_path / "graph.html"
|
|
result = wiki_graph_main(
|
|
directory=str(tmp_path),
|
|
output=str(output_file)
|
|
)
|
|
assert result == 1
|
|
|
|
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
|