agnes-the-ai-analyst/tests/test_cli_query_render.py
ZdenekSrotyr 79b55b6ff3 remove agnes query --register-bq from client CLI
The flag ran RemoteQueryEngine in-process on the caller's machine and
required local BigQuery credentials (BIGQUERY_PROJECT + ADC). Analysts
don't have those, so calling --register-bq from an analyst workspace
surfaced as a confusing not_configured error chain ("Could not load
static instance.yaml" + "BigQuery project not configured"). An agent
following CLAUDE.md's hybrid-queries guidance would land in exactly
that trap.

The underlying engine was originally designed server-side (commit
d180b201, "Step 28: Remote query architecture"); the CLI port (commit
d605e7d9) silently assumed parity with the server. Server-side hybrid
already exists as an admin-only POST /api/query/hybrid endpoint
(app/api/query_hybrid.py) and is untouched here.

Analysts combining local + remote data now have two documented paths:
agnes snapshot create a filtered slice and join locally, or run the
join server-side via agnes query --remote. CLAUDE.md, the agent skill,
docs/DATA_SOURCES.md, and connectors.md updated accordingly.
2026-05-12 18:18:13 +02:00

69 lines
2.7 KiB
Python

"""CLI commands route BQ-typed errors through the shared renderer.
Two CLI paths surface BQ errors today:
- `agnes query --remote` (cli/commands/query.py:_query_remote → /api/query)
- `agnes snapshot create` / `agnes schema` / etc. (cli/v2_client.V2ClientError → v2 endpoints)
After the refactor they all call cli.error_render.render_error so analyst
output is consistent and structured. Closes part of #160 §4.7.3.
The third assertion below covers `RemoteQueryError.details`, which is now
exercised only via the server-side `/api/query/hybrid` endpoint
(`app/api/query_hybrid.py`) — the client CLI no longer wraps it.
"""
from __future__ import annotations
import pytest
def test_v2_client_error_uses_renderer():
"""`V2ClientError.__str__` calls render_error so any caller of the v2
HTTP helpers (api_get_json/api_post_json/api_post_arrow) automatically
surfaces typed BQ errors as structured output."""
from cli.v2_client import V2ClientError
body = {"detail": {
"kind": "cross_project_forbidden",
"message": "USER_PROJECT_DENIED",
"hint": "Set data_source.bigquery.billing_project",
}}
err = V2ClientError(status_code=502, body=body)
out = str(err)
# Old form: `HTTP 502: {'detail': {'kind': ...}}` (single line).
# New form: multi-line structured.
assert "\n" in out, f"V2ClientError must use multi-line renderer; got {out!r}"
assert "cross_project_forbidden" in out
assert "billing_project" in out
def test_v2_client_error_drops_truncation_for_dicts():
"""The OLD `message=str(body)[:200]` truncation hid the structured
`hint` field for any reasonably-sized error dict. The new renderer
must NOT pre-truncate dict bodies."""
from cli.v2_client import V2ClientError
body = {"detail": {
"kind": "bq_forbidden",
"billing_project": "x" * 200, # padding to push past old 200-char limit
"data_project": "y" * 200,
"hint": "MUST_REACH_THIS_HINT_IN_OUTPUT",
}}
err = V2ClientError(status_code=502, body=body)
out = str(err)
assert "MUST_REACH_THIS_HINT_IN_OUTPUT" in out, \
"renderer must not pre-truncate dict bodies past the hint field"
def test_remote_query_error_carries_details():
"""`RemoteQueryError` carries a structured `details` field. Verify the
type's surface so `app/api/query_hybrid.py` (the only remaining caller)
can rely on it."""
from src.remote_query import RemoteQueryError
err = RemoteQueryError(
error_type="cross_project_forbidden",
message="USER_PROJECT_DENIED",
details={"billing_project": "", "data_project": "prj"},
)
assert err.details == {"billing_project": "", "data_project": "prj"}
assert err.error_type == "cross_project_forbidden"