Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
"""The count vocabulary, the judgements the index stores, and held-back steps."""
from __future__ import annotations
import copy
from repowise.core.analysis.health.fix_first import build_fix_first
from repowise.core.analysis.health.fix_first.build import judge_findings
from repowise.core.analysis.health.queue.counts import (
COVERED_BY_PLAN,
NOT_JUDGED,
counts_of,
perf_judgement,
queue_counts,
)
from repowise.core.analysis.health.queue.eligibility import perf_queue_verdict
from tests.unit.health.fix_first_rows import (
FINDINGS,
METRICS,
PERFORMANCE,
PLANS,
REFACTORING,
_step,
)
def test_the_levels_add_up_and_scope_reasons_leave_in_scope() -> None:
counts = queue_counts(
[(None, "now", 2), (None, "later", 3), ("test", None, 4), ("below_min_worth", None, 1),
(NOT_JUDGED, None, 1)],
shown=2,
)
assert counts.as_dict() == {
"inventory": 11,
"in_scope": 7,
"eligible": 5,
"due": 2,
"shown": 2,
"excluded": {"test": 4, "below_min_worth": 1, "not_judged": 1},
"gated": 0,
}
excluded = {"test": 4, "below_min_worth": 1, "not_judged": 1, "tooling": 0}
assert counts_of(5, 2, excluded, 2) == counts
def test_every_open_finding_is_judged_as_fix_first_judges_its_file() -> None:
findings = [{**f, "id": f["public_id"]} for f in FINDINGS]
judged = judge_findings(metrics=METRICS, findings=findings, plans=PLANS)
assert judged["finding_t1"].reason == "test"
assert judged["finding_h1"].reason == "history_only"
assert judged["finding_h2"].reason == "history_only"
# Resolved findings are no unit of the queue.
assert "finding_r1" not in judged
# An eligible finding carries the tier its item would.
queue = build_fix_first(metrics=METRICS, findings=FINDINGS, limit=None)
item = next(i for i in queue.items if i.target.file_path == "src/plain.py")
assert judged["finding_n1"].reason is None and judged["finding_n1"].tier == item.tier
# A file whose plan is an item: the plan speaks for its findings.
covered = judge_findings(
metrics=METRICS, findings=findings, plans=PLANS, planned_files={"src/core.py"}
)
assert covered["finding_c1"].reason == COVERED_BY_PLAN == covered["finding_h1"].reason
def test_only_a_files_lead_finding_is_eligible() -> None:
second = {**FINDINGS[2], "public_id": "finding_n2", "function_name": "walk2",
"health_impact": 0.2, "line_start": 50, "line_end": 80}
findings = [{**f, "id": f["public_id"]} for f in (*FINDINGS, second)]
judged = judge_findings(metrics=METRICS, findings=findings, plans=PLANS)
assert judged["finding_n1"].reason is None
assert judged["finding_n2"].reason == "not_file_lead"
def test_a_cause_is_judged_by_the_default_queue_rule() -> None:
for row in PERFORMANCE:
facets = row["details"]["facets"]
judgement = perf_judgement(row, facets, row["details"]["plan"])
assert judgement.reason == perf_queue_verdict(row).reason
assert (judgement.tier is None) == (judgement.reason is not None)
def test_a_causes_verdict_follows_its_execution_role() -> None:
"""Finalize judges each cause from its row as written, role included, so
a restamped role is judged again on the same write."""
from repowise.core.analysis.health.queue.eligibility import QUEUE_ROLES
row = PERFORMANCE[0]
hot = sorted(QUEUE_ROLES - {"scheduled_job"})[0]
for role, reason in ((hot, None), ("cli", "cold_role")):
facets = {**row["details"]["facets"], "execution_role": role}
judged = {**row, "execution_role": role, "details": {**row["details"], "facets": facets}}
assert perf_judgement(judged, facets, row["details"]["plan"]).reason == reason
def test_unaudited_steps_stay_in_the_plan_not_the_item() -> None:
"""A plan led by an audited kind keeps its opportunity in Fix first, but a
Move Method or Extract Class step behind the lead is held back."""
refactoring = copy.deepcopy(REFACTORING[:1])
steps = refactoring[0]["details"]["steps"]
steps.append(_step("refac2_move", "move_method", "run", 0.4, mechanical=False))
steps.append(_step("refac2_class", "extract_class", "Core", 0.4, mechanical=False))
judged: dict = {}
queue = build_fix_first(
metrics=METRICS, findings=FINDINGS, refactoring=refactoring, plans=PLANS,
limit=None, judged=judged,
)
item = next(i for i in queue.items if i.kind == "refactor")
plain: dict = {}
before = build_fix_first(
metrics=METRICS, findings=FINDINGS, refactoring=REFACTORING[:1], plans=PLANS,
limit=None, judged=plain,
)
# The opportunity, its id and its verdict are unchanged by the held-back steps.
assert item.id == next(i for i in before.items if i.kind == "refactor").id
assert judged["refop2_core"] == plain["refop2_core"]
assert judged["refop2_core"].reason is None
assert item.action.steps_total == 2
assert {s.order for s in item.action.steps} == {1, 2}
assert ("steps held back", "2 (move or extract-class, not yet audited; in the full plan)") in {
(f.label, f.value) for f in item.facts
}
def test_a_gated_unit_counts_once_in_inventory_and_gated() -> None:
groups = [(None, "now", 2), ("test", None, 4)]
plain = queue_counts(groups, shown=2)
held = queue_counts(groups, shown=2, gated=3)
assert plain.gated == 0
assert held.inventory == plain.inventory + 3 and held.gated == 3
# No level below the inventory sees a gated unit.
assert (held.in_scope, held.eligible, held.due, held.excluded) == (
plain.in_scope,
plain.eligible,
plain.due,
plain.excluded,
)