agnes-the-ai-analyst/tests/test_cli_query_render.py
ZdenekSrotyr 1563b05f2e refactor(cli): hard-cutover env vars + config dir to AGNES_*
Task 0.5 of clean-analyst-bootstrap. Greenfield rewrite — no fallback,
no aliases. Existing dev environments lose their cached PAT and must
re-authenticate.

Env var renames (hard cutover):
- DA_CONFIG_DIR    -> AGNES_CONFIG_DIR
- DA_SERVER        -> AGNES_SERVER
- DA_SERVER_URL    -> AGNES_SERVER_URL  (test-only stale ref, not in spec)
- DA_NO_UPDATE_CHECK -> AGNES_NO_UPDATE_CHECK
- DA_LOCAL_DIR     -> AGNES_LOCAL_DIR
- DA_TOKEN         -> AGNES_TOKEN
- DA_STREAM_RETRIES -> AGNES_STREAM_RETRIES

Config dir rename: ~/.config/da/ -> ~/.config/agnes/ (across code,
comments, docstrings, error messages, install templates, dev scripts).

Stale `da X` references in CLI source (and adjacent app/, tests/):
swept docstrings, comments, help text, and error messages where the
verb survives the rewrite (init, pull, push, catalog, status, diagnose,
auth, admin, skills, query, schema, describe, explore, disk-info,
snapshot, login, logout, whoami, server, setup) and replaced `da X`
with `agnes X`. Intentionally kept `da sync`, `da fetch`, `da analyst`,
`da metrics` — those verbs are removed in later tasks; the legacy
strings will be detected by `_LEGACY_STRINGS` (added in Task 2).

Test fixes:
- TestCLIVersion now asserts output starts with `agnes ` (was `da `).

Test results: 2675 passed, 25 skipped (full pytest run, excluding 9
pre-existing test_db.py / test_user_management.py / test_e2e_extract.py
/ test_cli_binary_rename.py failures unrelated to this rename).
2026-05-04 16:35:44 +02:00

66 lines
2.6 KiB
Python

"""CLI commands route BQ-typed errors through the shared renderer.
Three CLI paths surface BQ errors today:
- `agnes query --remote` (cli/commands/query.py:_query_remote → /api/query)
- `agnes query --register-bq` (cli/commands/query.py:_query_hybrid via
RemoteQueryError, which wraps server-side BqAccessError)
- `da fetch` / `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.
"""
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` already has a `details` field. Verify the type's
surface so cli/commands/query.py:_query_hybrid 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"