"""Hamilton review MAJOR #3: find_devices_using_css must surface partial failures. The function is per-category resilient by design — if one schema query fails, the others still produce results. But the top-level summary previously hid that some categories errored out: `total_returned` and `any_truncated` only reflected the SUCCESSFUL categories. An LLM consuming "47 references, low impact" wouldn't know that 5 categories errored and the real number is likely much higher. After the fix: the response includes `complete: bool`, `categories_with_errors`, and `error_categories`, so an LLM (or human auditor) can see the partial-failure state and act on it. """ import pytest from mcp_cucm_axl.route_plan import find_devices_using_css class FakeAxlClient: """Minimal stand-in for AxlClient that lets us simulate per-query failures. Returns a fake CSS pkid for the lookup query, then either a single fake row or an exception based on substring matching. """ def __init__(self, error_on_columns: list[str] | None = None): self.error_on_columns = error_on_columns or [] self.queries: list[str] = [] def execute_sql_query(self, sql: str) -> dict: self.queries.append(sql) # The CSS lookup query — return a fake pkid if "callingsearchspace WHERE name" in sql: return {"row_count": 1, "rows": [{"pkid": "fake-css-pkid"}]} # Any query referencing an "error trigger" column → simulate failure for trigger in self.error_on_columns: if trigger in sql: raise RuntimeError(f"simulated cluster failure on {trigger}") # Otherwise return one fake reference row so the category isn't empty return { "row_count": 1, "rows": [{"name": "FakeRef", "context": "FakePart", "description": "fake"}], } def test_no_errors_reports_complete(): """Baseline: when every category succeeds, complete=True and no error fields populated.""" client = FakeAxlClient() result = find_devices_using_css(client, "Some-CSS") assert result["complete"] is True assert result["categories_with_errors"] == 0 assert result["error_categories"] == [] # And total_returned reflects the successful categories assert result["total_returned"] >= 1 def test_one_errored_category_marks_incomplete(): """The audit-trust failure mode: one category errors out and the summary lies. Fix: complete=False, categories_with_errors >= 1. """ client = FakeAxlClient(error_on_columns=["fkcallingsearchspace_cgpnunknown"]) result = find_devices_using_css(client, "Some-CSS") assert result["complete"] is False, ( "complete must be False when any category errored" ) assert result["categories_with_errors"] >= 1 assert "device_cgpn_unknown_css" in result["error_categories"] def test_multiple_errors_all_listed(): """All errored categories must be enumerated in error_categories.""" client = FakeAxlClient( error_on_columns=[ "fkcallingsearchspace_cgpnunknown", "fkcallingsearchspace_reroute", "fkcallingsearchspace_pilotqueuefull", ] ) result = find_devices_using_css(client, "Some-CSS") assert result["complete"] is False assert result["categories_with_errors"] == 3 assert set(result["error_categories"]) == { "device_cgpn_unknown_css", "device_reroute_css", "huntpilot_queue_full_css", } def test_total_returned_does_not_include_error_categories(): """An errored category contributes 0 to total_returned (correct behavior). What's NEW: the response also flags that the count is partial. """ client = FakeAxlClient(error_on_columns=["fkcallingsearchspace_cgpnunknown"]) result = find_devices_using_css(client, "Some-CSS") # The count itself is unchanged from before — what's new is the warning assert result["complete"] is False # The error category has no rows in references_by_category err_cat = result["references_by_category"].get("device_cgpn_unknown_css", {}) assert "error" in err_cat def test_css_not_found_returns_error_not_partial(): """If the CSS lookup itself fails (CSS doesn't exist), we return the 'not found' error early, NOT a partial-failure response. Distinct failure modes deserve distinct shapes. """ class CssNotFoundClient: def execute_sql_query(self, sql): if "callingsearchspace WHERE name" in sql: return {"row_count": 0, "rows": []} return {"row_count": 1, "rows": [{}]} result = find_devices_using_css(CssNotFoundClient(), "Nonexistent-CSS") assert "error" in result assert "complete" not in result, ( "CSS-not-found is a hard error; we shouldn't dress it up as partial" )