Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

6.7 KB
Raw Blame
"""Unit tests for Axum/Actix framework edges (F6)."""

from __future__ import annotations

from datetime import datetime
from pathlib import Path

import networkx as nx

from repowise.core.ingestion.framework_edges import add_framework_edges
from repowise.core.ingestion.models import FileInfo, ParsedFile
from repowise.core.ingestion.parser import ASTParser
from repowise.core.ingestion.resolvers.context import ResolverContext


def _file_info(rel: str, abs_path: str) -> FileInfo:
    return FileInfo(
        path=rel,
        abs_path=abs_path,
        language="rust",
        size_bytes=100,
        git_hash="",
        last_modified=datetime.now(),
        is_test=False,
        is_config=False,
        is_api_contract=False,
        is_entry_point=False,
    )


def _build_parsed(repo: Path) -> dict[str, ParsedFile]:
    parser = ASTParser()
    out: dict[str, ParsedFile] = {}
    for src in repo.rglob("*.rs"):
        rel = src.resolve().relative_to(repo.resolve()).as_posix()
        fi = _file_info(rel, str(src.resolve()))
        out[rel] = parser.parse_file(fi, src.read_bytes())
    return out


def _ctx(repo: Path, parsed: dict[str, ParsedFile]) -> ResolverContext:
    path_set = set(parsed.keys())
    stem_map: dict[str, list[str]] = {}
    for p in path_set:
        stem = Path(p).stem.lower()
        stem_map.setdefault(stem, []).append(p)
    return ResolverContext(
        path_set=path_set, stem_map=stem_map, graph=nx.DiGraph(), repo_path=repo
    )


class TestAxum:
    def test_route_to_handler(self, tmp_path: Path) -> None:
        (tmp_path / "handlers.rs").write_text(
            "pub async fn list_users() -> &'static str { \"ok\" }\n"
        )
        (tmp_path / "main.rs").write_text(
            "use axum::{Router, routing::get};\n"
            "mod handlers;\n"
            "fn app() -> Router {\n"
            "  Router::new().route(\"/users\", get(list_users))\n"
            "}\n"
        )
        parsed = _build_parsed(tmp_path)
        graph = nx.DiGraph()
        for p in parsed:
            graph.add_node(p)
        ctx = _ctx(tmp_path, parsed)
        add_framework_edges(graph, parsed, ctx, tech_stack=["axum"])
        assert graph.has_edge("main.rs", "handlers.rs")


class TestActix:
    def test_web_get_to_handler(self, tmp_path: Path) -> None:
        (tmp_path / "handlers.rs").write_text(
            "pub async fn index() -> &'static str { \"ok\" }\n"
        )
        (tmp_path / "main.rs").write_text(
            "use actix_web::{web, App};\n"
            "fn config(cfg: &mut web::ServiceConfig) {\n"
            "  cfg.route(\"/\", web::get().to(index));\n"
            "}\n"
        )
        parsed = _build_parsed(tmp_path)
        graph = nx.DiGraph()
        for p in parsed:
            graph.add_node(p)
        ctx = _ctx(tmp_path, parsed)
        add_framework_edges(graph, parsed, ctx, tech_stack=["actix"])
        assert graph.has_edge("main.rs", "handlers.rs")


class TestAxumNest:
    def test_nest_to_handler(self, tmp_path: Path) -> None:
        (tmp_path / "api.rs").write_text(
            "pub fn api_routes() -> Router { Router::new() }\n"
        )
        (tmp_path / "main.rs").write_text(
            "use axum::Router;\n"
            "fn app() -> Router {\n"
            '  Router::new().nest("/api", api_routes)\n'
            "}\n"
        )
        parsed = _build_parsed(tmp_path)
        graph = nx.DiGraph()
        for p in parsed:
            graph.add_node(p)
        ctx = _ctx(tmp_path, parsed)
        add_framework_edges(graph, parsed, ctx, tech_stack=["axum"])
        assert graph.has_edge("main.rs", "api.rs")


class TestAxumLayerAndFallback:
    def test_layer_to_middleware(self, tmp_path: Path) -> None:
        (tmp_path / "middleware.rs").write_text(
            "pub async fn auth_layer() {}\n"
        )
        (tmp_path / "main.rs").write_text(
            "use axum::Router;\n"
            "fn app() -> Router {\n"
            "  Router::new().layer(auth_layer)\n"
            "}\n"
        )
        parsed = _build_parsed(tmp_path)
        graph = nx.DiGraph()
        for p in parsed:
            graph.add_node(p)
        ctx = _ctx(tmp_path, parsed)
        add_framework_edges(graph, parsed, ctx, tech_stack=["axum"])
        assert graph.has_edge("main.rs", "middleware.rs")

    def test_fallback_to_handler(self, tmp_path: Path) -> None:
        (tmp_path / "fallback.rs").write_text(
            "pub async fn not_found() -> &'static str { \"404\" }\n"
        )
        (tmp_path / "main.rs").write_text(
            "use axum::Router;\n"
            "fn app() -> Router {\n"
            "  Router::new().fallback(not_found)\n"
            "}\n"
        )
        parsed = _build_parsed(tmp_path)
        graph = nx.DiGraph()
        for p in parsed:
            graph.add_node(p)
        ctx = _ctx(tmp_path, parsed)
        add_framework_edges(graph, parsed, ctx, tech_stack=["axum"])
        assert graph.has_edge("main.rs", "fallback.rs")


class TestRocketMount:
    def test_rocket_mount_routes(self, tmp_path: Path) -> None:
        (tmp_path / "routes.rs").write_text(
            '#[rocket::get("/health")]\n'
            "pub fn health() -> &'static str { \"ok\" }\n"
        )
        (tmp_path / "main.rs").write_text(
            "use rocket;\n"
            "fn rocket() -> rocket::Rocket {\n"
            '  rocket::build().mount("/", routes![health])\n'
            "}\n"
        )
        parsed = _build_parsed(tmp_path)
        graph = nx.DiGraph()
        for p in parsed:
            graph.add_node(p)
        ctx = _ctx(tmp_path, parsed)
        add_framework_edges(graph, parsed, ctx, tech_stack=["rocket"])
        assert graph.has_edge("main.rs", "routes.rs")

    def test_rocket_mount_multiple_handlers(self, tmp_path: Path) -> None:
        (tmp_path / "routes.rs").write_text(
            "pub fn index() {}\npub fn about() {}\n"
        )
        (tmp_path / "main.rs").write_text(
            "use rocket;\n"
            "fn rocket() -> rocket::Rocket {\n"
            '  rocket::build().mount("/", routes![index, about])\n'
            "}\n"
        )
        parsed = _build_parsed(tmp_path)
        graph = nx.DiGraph()
        for p in parsed:
            graph.add_node(p)
        ctx = _ctx(tmp_path, parsed)
        add_framework_edges(graph, parsed, ctx, tech_stack=["rocket"])
        assert graph.has_edge("main.rs", "routes.rs")


class TestRustGate:
    def test_non_router_unaffected(self, tmp_path: Path) -> None:
        (tmp_path / "main.rs").write_text("fn main() {}\n")
        parsed = _build_parsed(tmp_path)
        graph = nx.DiGraph()
        for p in parsed:
            graph.add_node(p)
        ctx = _ctx(tmp_path, parsed)
        count = add_framework_edges(graph, parsed, ctx, tech_stack=[])
        assert count == 0