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.

9.2 KB
Raw Blame
"""Tests for the pure entry-point candidacy and ranking rules."""

from __future__ import annotations

from repowise.core.entry_candidacy import (
    GLUE_STEMS,
    boots_application,
    conventional_entry_stems,
    entry_point_depth,
    is_glue_leaf,
    not_an_execution_start,
)
from repowise.core.generation.entry_points import (
    entry_point_rank_key,
    rank_entry_point_paths,
    rank_entry_points,
)

# A representative conventional-entry stem set (registry-derived in production).
_CONV = frozenset({"main", "app", "server", "cli", "run", "manage", "wsgi", "asgi"})


def test_entry_point_depth_counts_directories():
    assert entry_point_depth("main.py") == 0
    assert entry_point_depth("src/main.py") == 1
    assert entry_point_depth("a/b/c/d/main.py") == 4


def test_glue_stems_are_index_and_mod():
    assert set(GLUE_STEMS) == {"index", "mod"}


def test_is_glue_leaf_only_for_deep_generic_stems():
    # A deeply-nested resolver index.py is a dispatch leaf.
    assert is_glue_leaf("packages/core/ingestion/resolvers/dotnet/index.py")
    assert is_glue_leaf("a/b/mod.rs")
    # Shallow generic stems may still be a real package entry.
    assert not is_glue_leaf("index.ts")
    assert not is_glue_leaf("src/index.ts")
    # Non-generic stems are never glue leaves, however deep.
    assert not is_glue_leaf("a/b/c/d/main.py")


def test_not_an_execution_start_is_language_or_glue_leaf():
    # Config/data and infra languages describe or wire the system.
    assert not_an_execution_start("api/server.json", "json")
    assert not_an_execution_start("deploy/Dockerfile", "dockerfile")
    # Deep generic-glue leaves dispatch within it.
    assert not_an_execution_start("core/ingestion/resolvers/dotnet/index.py", "python")
    # A real code entry is neither, at any depth.
    assert not not_an_execution_start("src/main.py", "python")
    assert not not_an_execution_start("a/b/c/d/main.py", "python")
    # Only a *shallow* glue stem survives candidacy. A monorepo package barrel
    # is dropped here even though ``orientation_entry_points`` keeps it by
    # ranking it last — candidacy and ordering answer different questions.
    assert not not_an_execution_start("src/index.ts", "typescript")
    assert not_an_execution_start("packages/cli/src/index.ts", "typescript")


def test_every_consumer_calls_the_shared_rule_not_a_copy():
    # Four surfaces answer "may this file be an entry point": ingestion's flag,
    # the KG curator's list, the tour's scorer, and the wiki's ranking. Each is
    # unchanged *by construction* only while it holds this exact object; a
    # re-inlined copy reopens the divergence this closed. Scope, honestly: this
    # pins the module binding, not the call sites. A copy that left the import
    # in place still passes here — ruff's unused-import rule catches that half.
    from repowise.core.analysis import kg_curation
    from repowise.core.generation import tour
    from repowise.core.ingestion import traverser

    assert kg_curation.not_an_execution_start is not_an_execution_start
    assert tour.not_an_execution_start is not_an_execution_start
    assert traverser.not_an_execution_start is not_an_execution_start


# The flag/ranking stem union (B23) is pinned by
# ``test_stem_union_widens_the_flag_without_dropping_a_stem`` in the traverser
# tests, which builds real files and fails without the change. Nothing is
# asserted about the two sets here: ``conventional_entry_stems()`` is *defined*
# as the registry stems minus ``GLUE_STEMS``, so every relation between them —
# disjointness included — is set algebra that holds for any content.


def test_glue_leaf_never_outranks_a_real_entry():
    # The .NET resolver index.py is highly central (high pagerank+betweenness)
    # but must rank below a shallow, conventionally-named main.py.
    candidates = [
        ("packages/core/ingestion/resolvers/dotnet/index.py", 0.9, 0.9),
        ("packages/cli/src/main.py", 0.1, 0.0),
    ]
    ranked = rank_entry_points(candidates, _CONV)
    assert ranked[0] == "packages/cli/src/main.py"
    assert ranked[-1].endswith("dotnet/index.py")


def test_conventional_name_outranks_neutral_at_same_depth():
    candidates = [
        ("pkg/sub/helper.py", 0.9, 0.9),  # neutral name, very central
        ("pkg/sub/app.py", 0.0, 0.0),  # conventional entry name
    ]
    ranked = rank_entry_points(candidates, _CONV)
    assert ranked[0] == "pkg/sub/app.py"


def test_shallower_entry_wins_within_a_bucket():
    candidates = [
        ("a/b/c/d/main.py", 0.5, 0.5),
        ("a/main.py", 0.0, 0.0),
    ]
    ranked = rank_entry_points(candidates, _CONV)
    assert ranked[0] == "a/main.py"


def test_centrality_only_breaks_ties():
    # Same name bucket and depth: the more central file wins.
    candidates = [
        ("pkg/main.py", 0.2, 0.1),
        ("lib/main.py", 0.9, 0.5),
    ]
    ranked = rank_entry_points(candidates, _CONV)
    assert ranked[0] == "lib/main.py"


def test_rank_is_deterministic_on_full_ties():
    candidates = [
        ("z/main.py", 0.0, 0.0),
        ("a/main.py", 0.0, 0.0),
    ]
    ranked = rank_entry_points(candidates, _CONV)
    assert ranked == ["a/main.py", "z/main.py"]  # path tiebreak


def test_rank_key_orders_bucket_then_depth_then_centrality():
    conv = entry_point_rank_key("a/app.py", pagerank=0.0, conventional_stems=_CONV)
    glue = entry_point_rank_key("a/index.py", pagerank=1.0, conventional_stems=_CONV)
    assert conv < glue  # conventional name beats central glue regardless


def test_manifest_tier_outranks_stem():
    # A package.json ``bin`` target named ``run-it.ts`` five levels deep beats
    # a shallow, highly central ``main.py`` and a shallow ``cli.ts`` guess.
    declared = "packages/tool/src/commands/bin/run-it.ts"
    candidates = [
        ("main.py", 0.9, 0.9),
        ("cli.ts", 0.5, 0.5),
        (declared, 0.0, 0.0),
    ]
    ranked = rank_entry_points(candidates, _CONV, frozenset({declared}))
    assert ranked[0] == declared
    assert ranked[1:] == ["main.py", "cli.ts"]
    # Within the tier the usual rules apply: a declared glue ``index`` sorts
    # after a declared conventional name.
    both = frozenset({"pkg/src/index.ts", "pkg/src/cli.ts"})
    ranked = rank_entry_points(
        [("pkg/src/index.ts", 0.9, 0.9), ("pkg/src/cli.ts", 0.0, 0.0), ("main.py", 0.0, 0.0)],
        _CONV,
        both,
    )
    assert ranked == ["pkg/src/cli.ts", "pkg/src/index.ts", "main.py"]


def test_entry_stem_not_conventional():
    # ``entry`` names a keyring's ``class Entry`` as often as a front door: it
    # ranks neutral. ``run`` is a flag stem (dead-code exempt), never a
    # ranking stem. Both keep the traverser flag.
    from repowise.core.ingestion.traverser import _ENTRY_POINT_STEMS

    assert "entry" not in conventional_entry_stems()
    assert "run" not in conventional_entry_stems()
    assert {"entry", "run"} <= _ENTRY_POINT_STEMS
    stems = conventional_entry_stems()
    ranked = rank_entry_points([("src/entry.ts", 0.9, 0.9), ("src/main.ts", 0.0, 0.0)], stems)
    assert ranked == ["src/main.ts", "src/entry.ts"]
    key = entry_point_rank_key("src/entry.ts", conventional_stems=stems)
    assert key[1] == 1  # neutral name bucket


def test_boots_application_reads_every_spring_boot_start():
    assert boots_application("java", "SpringApplication.run(PetClinicApplication.class, args);")
    # Fineract's ServerApplication starts through the builder.
    assert boots_application(
        "java", "configure(new SpringApplicationBuilder(ServerApplication.class)).run(args);"
    )
    assert boots_application("kotlin", "fun main(args: Array<String>) { runApplication<App>(*args) }")
    assert boots_application("java", "@SpringBootApplication\npublic class App {}")


def test_boots_application_ignores_mentions_and_other_languages():
    # A comment naming the method, a Javadoc link, and a servlet initializer's
    # ``configure(SpringApplicationBuilder builder)`` start nothing.
    assert not boots_application("java", "// SpringApplication.run(String...) does a System.exit")
    assert not boots_application("java", "/** See {@link SpringBootApplication}. */")
    assert not boots_application("java", "SpringApplicationBuilder configure(SpringApplicationBuilder b)")
    assert not boots_application("python", "SpringApplication.run(App.class, args)")


def test_declared_entry_outranks_a_conventional_stem():
    paths = ["lib/misc/Main.java", "provider/src/main/java/org/acme/ServerApplication.java"]
    declared = frozenset({"provider/src/main/java/org/acme/ServerApplication.java"})
    assert rank_entry_point_paths(paths, declared) == [
        "provider/src/main/java/org/acme/ServerApplication.java",
        "lib/misc/Main.java",
    ]


def test_example_entries_rank_after_the_systems_own():
    # ``main`` is a conventional stem and ``ServerApplication`` is not, but a
    # sample demonstrates the system rather than starting it.
    paths = ["generator/examples/Main.java", "src/org/acme/ServerApplication.java"]
    assert rank_entry_point_paths(paths) == [
        "src/org/acme/ServerApplication.java",
        "generator/examples/Main.java",
    ]
    declared = frozenset({"samples/app/App.java"})
    assert rank_entry_point_paths(["samples/app/App.java", "src/cli.py"], declared) == [
        "src/cli.py",
        "samples/app/App.java",
    ]