"""NASClient — monitor-set/report + X-WebAI-Key (respx)."""
import json as _json

import httpx
import respx

from nas_client import NASClient

BASE = "http://nas.test"


@respx.mock
async def test_get_monitor_set_sends_key():
    route = respx.get(f"{BASE}/api/webai/trade-alert/monitor-set").mock(
        return_value=httpx.Response(200, json={"session": "regular", "buy_targets": []}))
    c = NASClient(BASE, "KEY")
    ms = await c.get_monitor_set()
    assert ms["session"] == "regular"
    assert route.calls.last.request.headers["X-WebAI-Key"] == "KEY"
    await c.close()


@respx.mock
async def test_post_report_payload():
    captured = {}

    def _resp(request):
        captured.update(_json.loads(request.content))
        return httpx.Response(200, json={"new_alerts": 1, "cleared": 0})

    respx.post(f"{BASE}/api/webai/trade-alert/report").mock(side_effect=_resp)
    c = NASClient(BASE, "KEY")
    firing = [{"ticker": "005930", "kind": "buy", "condition": "buy_breakout",
               "price": 71500, "detail": {}}]
    out = await c.post_report("2026-07-02T09:01:00+09:00", firing)
    assert out["new_alerts"] == 1
    assert captured["as_of"] == "2026-07-02T09:01:00+09:00"
    assert captured["firing"] == firing
    await c.close()
