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.

fix(health): count a test's oracle wherever it sits in a with header (#2436)

Raghav Chamadiya5793133bc40f
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/assertions.py b/packages/core/src/repowise/core/analysis/health/complexity/assertions.py
index 1b4ac7d8c..128c7028c 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/assertions.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/assertions.py
@@ -190,15 +190,41 @@ def _find_assert_call(stmt: Node, kinds: frozenset[str]) -> Node | None:
return None
-def _find_context_manager_call(stmt: Node, lmap: LanguageNodeMap) -> Node | None:
- """An assertion-shaped call in a ``with`` header, body excluded."""
- stack = [c for c in stmt.children if c.is_named and c.type not in lmap.block_kinds]
+def _context_manager_tier(
+ stmt: Node, lmap: LanguageNodeMap, dialect: AssertDialect | None
+) -> int:
+ """The tier a ``with`` header asserts at, body excluded.
+
+ ``assert_call_kinds`` is the language's plain call node, so stopping at the
+ first one met answered on whichever item came first:
+ ``with self.assertRaises(E), atomic():`` was classified on ``atomic()``.
+ Every item is classified instead, and an assertion anywhere beats a
+ verification anywhere, so no verdict turns on the order they were typed in.
+ A header holding only a verification still reports one, as the single-item
+ form always has.
+
+ A declining call's own children are not scanned -- the *argument* half of
+ the bound :func:`_find_assert_call` puts on a statement. The depth half
+ does not carry over: a with-item's call sits under an ``as_pattern`` or a
+ ``with_clause``, so the descent must reach through any non-call node.
+ """
+
+ def _header_children(node: Node) -> list[Node]:
+ return [c for c in node.children if c.is_named and c.type not in lmap.block_kinds]
+
+ verification = _NOT_ASSERTION
+ stack = _header_children(stmt)[::-1]
while stack:
node = stack.pop()
if node.type in lmap.assert_call_kinds:
- return node
- stack.extend(c for c in node.children if c.is_named and c.type not in lmap.block_kinds)
- return None
+ tier = _tier_without_narrow(node, dialect)
+ if tier == _VERIFICATION:
+ verification = tier
+ elif tier != _NOT_ASSERTION:
+ return tier
+ continue
+ stack.extend(reversed(_header_children(node)))
+ return verification
def _assertion_tier(stmt: Node, lmap: LanguageNodeMap, dialect: AssertDialect | None) -> int:
@@ -231,8 +257,7 @@ def _assertion_tier(stmt: Node, lmap: LanguageNodeMap, dialect: AssertDialect |
# ``with pytest.raises(...)`` / ``with self.assertRaises(...)`` is the
# oracle, but the call sits in the header rather than in a statement of
# its own, so every tier above misses it.
- call = _find_context_manager_call(stmt, lmap)
- return _NOT_ASSERTION if call is None else _tier_without_narrow(call, dialect)
+ return _context_manager_tier(stmt, lmap, dialect)
else:
return _NOT_ASSERTION
if call is None:
diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.py
index 1b57aac3e..9eff27ef5 100644
--- a/packages/core/src/repowise/core/analysis/health/engine.py
+++ b/packages/core/src/repowise/core/analysis/health/engine.py
@@ -101,7 +101,34 @@ log = structlog.get_logger(__name__)
# Not a licence to move a calibrated scoring weight — those are frozen
# independently of this stamp.
#
-# Current stamp: every marker now reads ``FileContext.all_functions``, the full
+# Current stamp: a multi-item ``with`` header is classified on its oracle rather
+# than on whichever context manager the author happened to type first.
+# ``assert_call_kinds`` is the language's plain call node, not an assertion
+# shape, so the header scan returned the first call it met and classified only
+# that one: ``with pytest.raises(E), atomic():`` counted nothing while
+# ``with atomic(), pytest.raises(E):`` counted one. Each item is classified now,
+# and a declining call's arguments are not scanned, so an assertion passed as an
+# argument still does not stand in for the header's oracle.
+#
+# v21 moves two stored counts, both of which were order-dependent in the same
+# way. ``assertion_count`` rises on such a header. ``mock_setup_count`` falls on
+# it, because ``mock_walk._count_body_setup`` skips a statement
+# ``_is_assertion_statement`` admits and that predicate runs this same branch --
+# ``with patch(...), pytest.raises(E):`` already counted no mock setup in v20,
+# and the reversed spelling now agrees with it. Both feed
+# ``mock_saturated_test``, one per side of its ratio, and it is advisory.
+#
+# ``assertion_blocks`` does not move at all: a ``with`` header is capped at the
+# broad tier, so it can never join a narrow run. Verified set-for-set against
+# v20 on four corpora, with the average defect score unmoved on all four and no
+# ``mock_saturated_test`` finding gained or lost. The two calibrated block
+# markers read that field alone and are untouched, and every reader of the two
+# counts above is advisory, so no score moves. Python is the only language that
+# maps ``with_kinds`` and opts into assertion detection, so no other language's
+# rows change. A cached v20 walk carries the old counts, and this stamp is what
+# re-walks them.
+#
+# v20: every marker now reads ``FileContext.all_functions``, the full
# walked list, where eleven of them used to read a ``function_metrics`` map keyed
# by function name. That key kept one row per distinct name, so a file's anonymous
# ``it`` callbacks collapsed into one and a method shadowed its same-named
@@ -213,7 +240,7 @@ log = structlog.get_logger(__name__)
# forms. Files that were counted untested and are not become tested, which
# moves untested-hotspot findings and the scores that carry them, on every
# language with a prefix or spec convention rather than Ruby alone.
-HEALTH_ANALYZER_VERSION = 20
+HEALTH_ANALYZER_VERSION = 21
def walked_functions(
diff --git a/packages/ui/__tests__/health/findings-dimension-filter.test.tsx b/packages/ui/__tests__/health/findings-dimension-filter.test.tsx
new file mode 100644
index 000000000..a2fd19b42
--- /dev/null
+++ b/packages/ui/__tests__/health/findings-dimension-filter.test.tsx
@@ -0,0 +1,52 @@
+/**
+ * The dimension filter has to offer every dimension the query accepts.
+ *
+ * Both findings route docstrings promise that asking for a dimension returns
+ * it, and the component's own state is typed `HealthDimension | "all"`, so
+ * `performance` and `advisory` were legal values with no way to select them.
+ */
+import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";
+import { describe, expect, it, vi } from "vitest";
+
+import type { HealthWorkQueueQuery } from "@repowise-dev/types";
+
+import type { CodeHealthAdapter } from "../../src/health/code-health-adapter";
+import { FindingsView } from "../../src/health/findings-view";
+
+const overview = () => ({ files: [], biomarkers: [], summary: {} }) as never;
+
+function adapter(
+ getHealthWorkQueue: CodeHealthAdapter["getHealthWorkQueue"],
+): CodeHealthAdapter {
+ return {
+ cacheKey: `dimension-filter-${Math.random()}`,
+ getOverview: async () => overview(),
+ listFindings: async () => [],
+ getHealthWorkQueue,
+ updateFindingStatus: async () => undefined,
+ renderFileDrawer: () => null,
+ } as unknown as CodeHealthAdapter;
+}
+
+describe("FindingsView dimension filter", () => {
+ it("offers every dimension the findings query accepts", async () => {
+ render(<FindingsView adapter={adapter(async () => ({ targets: [], total: 0 }))} />);
+ const select = await screen.findByLabelText("Dimension");
+ const names = within(select)
+ .getAllByRole("option")
+ .map((o) => (o as HTMLOptionElement).value);
+ expect(names).toEqual(["all", "defect", "maintainability", "performance", "advisory"]);
+ });
+
+ it("asks the server for the advisory dimension once it is selected", async () => {
+ const load = vi.fn(async (_opts?: HealthWorkQueueQuery) => ({ targets: [], total: 0 }));
+ render(<FindingsView adapter={adapter(load)} />);
+ const select = await screen.findByLabelText("Dimension");
+ await waitFor(() => expect(load).toHaveBeenCalled());
+
+ fireEvent.change(select, { target: { value: "advisory" } });
+ await waitFor(() =>
+ expect(load.mock.calls.at(-1)?.[0]).toMatchObject({ dimension: "advisory" }),
+ );
+ });
+});
diff --git a/packages/ui/src/health/findings-view.tsx b/packages/ui/src/health/findings-view.tsx
index 4c0865677..56dbecda0 100644
--- a/packages/ui/src/health/findings-view.tsx
+++ b/packages/ui/src/health/findings-view.tsx
@@ -337,6 +337,8 @@ export function FindingsView({ adapter }: { adapter: CodeHealthAdapter }) {
{ value: "all", label: "All dimensions" },
{ value: "defect", label: "Defect risk" },
{ value: "maintainability", label: "Maintainability" },
+ { value: "performance", label: "Performance" },
+ { value: "advisory", label: "Advisory" },
]}
/>
<FilterSelect
diff --git a/tests/unit/health/test_assertion_free_test.py b/tests/unit/health/test_assertion_free_test.py
index 9b8e1ed7f..dd7574f9d 100644
--- a/tests/unit/health/test_assertion_free_test.py
+++ b/tests/unit/health/test_assertion_free_test.py
@@ -215,6 +215,201 @@ def test_a_with_block_never_joins_an_assertion_run() -> None:
assert fc.functions[0].assertion_blocks == []
+def _walk_one(body: str):
+ """The single ``FunctionComplexity`` a one-function source produces."""
+ source = "import pytest\n\n" + body
+ fc = walk_file("tests/test_sample.py", "python", source.encode("utf-8"))
+ return fc.functions[0]
+
+
+def test_a_multi_item_with_is_classified_on_its_oracle_not_its_first_item() -> None:
+ _require("python")
+ # ``assert_call_kinds`` is Python's plain ``call``, so a scan that stopped
+ # at the first call it met answered on whichever context manager the author
+ # typed first. The same two managers had to count the same either way round.
+ first = _walk_one(
+ "def test_oracle_first():\n"
+ " with pytest.raises(ValueError), atomic():\n"
+ " boom()\n"
+ )
+ second = _walk_one(
+ "def test_oracle_second():\n"
+ " with atomic(), pytest.raises(ValueError):\n"
+ " boom()\n"
+ )
+ middle = _walk_one(
+ "def test_oracle_middle():\n"
+ " with atomic(), pytest.raises(ValueError), other():\n"
+ " boom()\n"
+ )
+ assert (first.assertion_count, second.assertion_count, middle.assertion_count) == (1, 1, 1)
+
+
+def test_a_pytest_oracle_is_found_wherever_it_sits_in_the_header() -> None:
+ _require("python")
+ # The marker-level consequence, and the one a user sees. ``checks_something``
+ # has a ``called_names`` floor under the narrow prefixes, which answers the
+ # ``assertRaises`` spelling by name whatever the count says -- ``pytest``'s
+ # matches no prefix, so this test had nothing else to be found by.
+ source = (
+ "import pytest\n"
+ "\n"
+ "def test_only_pytest_oracle():\n"
+ " with pytest.raises(ValueError), atomic():\n"
+ " boom()\n"
+ )
+ assert _flagged(source) == []
+
+
+def test_the_header_descent_reaches_through_every_wrapper_shape() -> None:
+ _require("python")
+ # Each of these puts the call one node further from the ``with`` than the
+ # plain form: an ``as_pattern``, the ``async`` spelling, the parenthesised
+ # clause, and a suite on the header's own line. They are what the descent
+ # has to reach through, and a grammar bump is what would take one away.
+ as_pattern = _walk_one(
+ "def test_as():\n"
+ " with pytest.raises(ValueError) as exc, atomic() as tx:\n"
+ " boom()\n"
+ )
+ asynchronous = _walk_one(
+ "async def test_async():\n"
+ " async with atomic(), pytest.raises(ValueError):\n"
+ " await boom()\n"
+ )
+ parenthesised = _walk_one(
+ "def test_parens():\n"
+ " with (\n"
+ " atomic() as tx,\n"
+ " pytest.raises(ValueError) as exc,\n"
+ " ):\n"
+ " boom()\n"
+ )
+ same_line = _walk_one(
+ "def test_same_line():\n"
+ " with atomic(), pytest.raises(ValueError): boom()\n"
+ )
+ counts = (
+ as_pattern.assertion_count,
+ asynchronous.assertion_count,
+ parenthesised.assertion_count,
+ same_line.assertion_count,
+ )
+ assert counts == (1, 1, 1, 1)
+
+
+def test_a_header_oracle_leaves_the_mock_setup_count() -> None:
+ _require("python")
+ # Both sides of ``mock_saturated_test``'s ratio move on this one header:
+ # ``mock_walk._count_body_setup`` skips a statement ``_is_assertion_statement``
+ # admits, and that predicate runs the same header classification. Pinned
+ # together because the pair is what the ratio reads.
+ fn = _walk_one(
+ "def test_mocked():\n"
+ " with pytest.raises(ValueError), mock.patch('svc.client'):\n"
+ " run()\n"
+ )
+ assert (fn.assertion_count, fn.mock_setup_count) == (1, 0)
+
+
+def _with_tiers(body: str, dialect) -> list[int]:
+ """Every ``with`` statement's tier in *body*, under *dialect*.
+
+ Driven below ``walk_file`` on purpose: a verification in a ``with`` header
+ is unreachable through it, because no shipped Python dialect carries
+ ``verify_names`` and the configured vocabulary widens assert names only.
+ """
+ from tree_sitter import Parser
+
+ from repowise.core.analysis.health.complexity.assertions import _assertion_tier
+ from repowise.core.analysis.health.complexity.languages import get_language_map
+ from repowise.core.ingestion.parser import _get_language
+
+ lmap = get_language_map("python")
+ tree = Parser(_get_language("python")).parse(body.encode("utf-8"))
+ tiers: list[int] = []
+ stack = [tree.root_node]
+ while stack:
+ node = stack.pop()
+ if node.type in lmap.with_kinds:
+ tiers.append(_assertion_tier(node, lmap, dialect))
+ stack.extend(reversed(node.children))
+ return tiers
+
+
+def test_an_assertion_beats_a_verification_wherever_each_sits() -> None:
+ _require("python")
+ # The same order dependence one tier up, and the one place it could come
+ # back. No shipped Python dialect can produce a verification here, so this
+ # pins the rule against the day a ``verify_names`` row lands -- the failure
+ # it would cause is silent, a unit moving between two stored counts.
+ from repowise.core.analysis.health.asserts.lexicon import AssertDialect
+ from repowise.core.analysis.health.complexity.assertions import _BROAD, _VERIFICATION
+
+ dialect = AssertDialect(
+ receiver_methods={"pytest": frozenset({"raises"})},
+ verify_names=frozenset({"verify"}),
+ )
+ tiers = _with_tiers(
+ "def test_verify_first():\n"
+ " with verify(mock), pytest.raises(ValueError):\n"
+ " boom()\n"
+ "\n"
+ "def test_verify_second():\n"
+ " with pytest.raises(ValueError), verify(mock):\n"
+ " boom()\n"
+ "\n"
+ "def test_verify_alone():\n"
+ " with verify(mock):\n"
+ " boom()\n",
+ dialect,
+ )
+ # The first two agree, which is the whole point; a header with only a
+ # verification still reports one.
+ assert tiers == [_BROAD, _BROAD, _VERIFICATION]
+
+
+def test_a_with_header_of_no_oracles_still_counts_nothing() -> None:
+ _require("python")
+ fn = _walk_one(
+ "def test_no_oracle():\n"
+ " with atomic(), other():\n"
+ " boom()\n"
+ )
+ assert fn.assertion_count == 0
+
+
+def test_an_assertion_in_argument_position_is_not_the_headers_oracle() -> None:
+ _require("python")
+ # The bound a statement gets in ``_find_assert_call``: a call buried in an
+ # argument is not the thing being stated. Scanning past a declining call
+ # must not start reading into one.
+ fn = _walk_one(
+ "def test_argument():\n"
+ " with atomic(pytest.raises(ValueError)):\n"
+ " boom()\n"
+ )
+ assert fn.assertion_count == 0
+
+
+def test_a_multi_item_with_breaks_a_run_exactly_as_a_single_item_one_does() -> None:
+ _require("python")
+ # The block markers are calibrated on narrow runs. A ``with`` header is
+ # capped at broad in either form, so counting its oracle must leave the
+ # runs around it byte-identical to what a single-item header produces.
+ run = " assert a == 1\n{header} boom()\n assert b == 2\n assert c == 3\n"
+ single = _walk_one(
+ "def test_single():\n"
+ + run.format(header=" with pytest.raises(ValueError):\n")
+ )
+ multi = _walk_one(
+ "def test_multi():\n"
+ + run.format(header=" with pytest.raises(ValueError), atomic():\n")
+ )
+ assert multi.assertion_blocks == single.assertion_blocks
+ assert multi.assertion_blocks == [(7, 8, 2)]
+
+
# --------------------------------------------------------------------------
# Verification is an oracle here and is not one next door
# --------------------------------------------------------------------------
diff --git a/tests/unit/health/test_model_version_coupling.py b/tests/unit/health/test_model_version_coupling.py
index 04bbbbcd4..704de1747 100644
--- a/tests/unit/health/test_model_version_coupling.py
+++ b/tests/unit/health/test_model_version_coupling.py
@@ -22,7 +22,7 @@ from repowise.core.analysis.health.refactoring.identity import (
# The three stamps as they stand together. Moving either identity model means
# moving the analyzer with it, and updating this tuple in the same commit.
-_STAMPS = (20, 2, 2)
+_STAMPS = (21, 2, 2)
def test_the_identity_models_and_the_analyzer_stamp_are_pinned_together() -> None: