inference-engine 0.1.0-alpha.14

Native Rust RDF inference engine with a shared command-line interface
Documentation
"""Check the actual GraphArch site before it can be deployed to GitHub Pages."""

import json
from html.parser import HTMLParser
from pathlib import Path
import unittest
from urllib.parse import unquote, urlsplit


REPOSITORY = Path(__file__).resolve().parents[1]
ROOT = REPOSITORY / "output" / "grapharch"
SITE = ROOT / "site"
GUIDES = {
    "install/": "install",
    "cli/": "cli",
    "alpha-scope/": "alpha-scope",
    "conformance/": "conformance",
    "rdfs-subclass/": "rdfs-subclass",
    "rdfs-source/": "rdfs-source",
    "rdfs-context/": "rdfs-context",
    "rdfs-targets/": "rdfs-targets",
    "rdfs-ground/": "rdfs-ground",
    "shacl12/": "shacl12",
    "publication/": "publication",
}


class GraphArchPublicationTests(unittest.TestCase):
    def test_only_verified_main_pushes_deploy(self) -> None:
        workflow = (
            REPOSITORY / ".github" / "workflows" / "grapharch.yml"
        ).read_text(encoding="utf-8")
        main_push = "github.event_name == 'push' && github.ref == 'refs/heads/main'"
        build, deployment = workflow.split("  deploy:\n", 1)
        self.assertIn("grapharch build --offline --strict .", build)
        self.assertIn("EKGF/grapharch@7658a95413d0cda7638ff1880f24624586d5876e", build)
        self.assertLess(
            build.index("Verify documentation"), build.index("upload-pages-artifact")
        )
        self.assertIn(f"if: {main_push}", build)
        self.assertIn("path: output/grapharch/site/", build)
        self.assertIn(f"if: {main_push}", deployment)
        self.assertIn("needs: documentation", deployment)
        self.assertIn("name: github-pages", deployment)
        self.assertIn("pages: write", deployment)
        self.assertIn("id-token: write", deployment)
        self.assertIn("uses: actions/deploy-pages@", deployment)

    def test_all_guides_have_searchable_routes(self) -> None:
        index = json.loads((SITE / "search-index.json").read_text(encoding="utf-8"))
        self.assertLessEqual(GUIDES.keys(), {entry["route"] for entry in index})
        for route in GUIDES:
            with self.subTest(route=route):
                self.assertTrue((SITE / route / "index.html").is_file())

    def test_home_links_to_the_guides(self) -> None:
        home = (SITE / "index.html").read_text(encoding="utf-8")
        article = home.split("<article>", 1)[1].split("</article>", 1)[0]
        for route in GUIDES:
            with self.subTest(route=route):
                self.assertTrue(f'href="{route}"' in article, f"Missing home link: {route}")

    def test_home_is_a_summary_with_details_in_guides(self) -> None:
        home = (SITE / "index.html").read_text(encoding="utf-8")
        article = home.split("<article>", 1)[1].split("</article>", 1)[0]
        class Text(HTMLParser):
            def __init__(self):
                super().__init__()
                self.words = []
            def handle_data(self, data):
                self.words.extend(data.split())
        text = Text()
        text.feed(article)
        self.assertLessEqual(len(text.words), 350, "Keep detailed explanations in the guides")
        self.assertNotIn("<pre", article, "Code examples belong in the relevant guide")

    def test_same_repository_layout_as_dotenvage(self) -> None:
        home = (SITE / "index.html").read_text(encoding="utf-8")
        for marker in (
            'class="repository-site"', 'class="repository-header"',
            'class="repository-tabs"', 'class="repository-sidebar-content"',
            'class="repository-footer-grid"', 'data-search-index="search-index.json"',
            'href="repository/releases/"', 'content="GraphArch"',
        ):
            with self.subTest(marker=marker):
                self.assertIn(marker, home)
        self.assertTrue((SITE / "style.css").is_file())
        for topic in ("releases", "packages", "licensing", "contributing"):
            with self.subTest(topic=topic):
                self.assertTrue((SITE / "repository" / topic / "index.html").is_file())

    def test_retained_graph_has_public_guides_and_checkout_provenance(self) -> None:
        graph = (ROOT / "documentation.nq").read_text(encoding="utf-8")
        for identity in (*GUIDES.values(), "contributing"):
            with self.subTest(identity=identity):
                self.assertTrue(f"<urn:inference-engine:docs:{identity}>" in graph, f"Missing guide: {identity}")
        self.assertIn("<https://ekgf.org/ontology/grapharch#SourceCheckout>", graph)
        for private_input in ("AGENTS.md", "AGENT-RDF12", "tests/fixtures/"):
            self.assertNotIn(private_input, graph)

    def test_generated_local_links_resolve(self) -> None:
        class Links(HTMLParser):
            def __init__(self, page):
                super().__init__()
                self.page = page
                self.broken = []

            def handle_starttag(self, tag, attrs):
                for name, value in attrs:
                    if name not in ("href", "src") or not value:
                        continue
                    url = urlsplit(value)
                    if url.scheme or url.netloc or not url.path:
                        continue
                    destination = (self.page.parent / unquote(url.path)).resolve()
                    if not destination.exists():
                        self.broken.append(value)

        pages = list(SITE.rglob("*.html"))
        self.assertTrue(pages, "GraphArch did not produce any HTML pages")
        for page in pages:
            with self.subTest(page=page.relative_to(SITE)):
                parser = Links(page)
                parser.feed(page.read_text(encoding="utf-8"))
                self.assertFalse(parser.broken)


if __name__ == "__main__":
    unittest.main()