"""Serve exact DocForge accessibility fixtures to the Playwright gate.""" from __future__ import annotations import json import shutil import sys import tempfile from pathlib import Path from typing import cast from docforge.graph_projection import ( GraphViewRequestV1, build_graph_projection_package, build_graph_view_plan, ) from docforge.index import ProjectIndex from docforge.project import Project from docforge.rendering import RenderService from docforge.visualization import VisualizationRunner from docforge_renderers.graph import PortableGraphHtmlRenderer ROOT = Path(__file__).resolve().parents[1] def _manual_html(project: Project) -> str: result = RenderService(project).render("manual") output = result.get("output") if not isinstance(output, dict): raise RuntimeError("Manual render did not return output identity") relative_path = cast(dict[str, object], output).get("path") if not isinstance(relative_path, str): raise RuntimeError("Manual render did not return an output path") return (project.descriptor.root / relative_path).read_text(encoding="utf-8") def _portable_graph_html(project: Project) -> str: plan = build_graph_view_plan( project.load(), GraphViewRequestV1( view_id="accessibility-gate", title="Portable graph accessibility fixture", root_node_id="guide.workflow", depth=2, max_nodes=20, max_edges=40, max_work=1_000, ), False, ) package = build_graph_projection_package( plan, renderer_id=PortableGraphHtmlRenderer.renderer_id, renderer_version=PortableGraphHtmlRenderer.renderer_version, max_output_bytes=1_000_000, ) artifact = PortableGraphHtmlRenderer().render(package).artifacts[0] return artifact.content.decode("utf-8") def main() -> int: with tempfile.TemporaryDirectory(prefix="docforge-accessibility-") as directory: project_root = Path(directory) / "alpha" shutil.copytree(ROOT / "tests" / "fixtures" / "alpha", project_root) project = Project.open(project_root) index = ProjectIndex(project) index.build() portable_html = _portable_graph_html(project) manual_html = _manual_html(project) runner = VisualizationRunner( index, register_atexit=False, persistent=True, ) try: visualization = runner.start(node_id="guide.workflow", depth=2) live_url = visualization.get("url") if not isinstance(live_url, str): raise RuntimeError("Live viewer did not return a URL") print( json.dumps( { "schema_version": 1, "manual_html": manual_html, "portable_html": portable_html, "live_url": live_url, }, separators=(",", ":"), ), flush=True, ) sys.stdin.buffer.read() finally: runner.stop() return 0 if __name__ == "__main__": sys.exit(main())