Files
GTI_GERENCIADOR_FISCAL/agent/tests/test_client.py
T
gtiandClaude Sonnet 5 d8f1bd3415 fix: Agent tolerates ~3 minutes of network outage mid-job (10 attempts with backoff); 1.2.4
Sandbox test: a 26 s outage exhausted the previous 15 s retry budget and failed a REPROCESS_ALL.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-25 12:46:20 -03:00

64 lines
2.0 KiB
Python

import httpx
from gti_fiscal_agent.client import HubClient
def test_identity_requests_authenticated_identity_only(monkeypatch):
calls = {}
payload = {
"agent_id": 2,
"installation_id": "00000000-0000-4000-8000-000000000222",
"hostname": "DESKTOP-B0HL02I",
"version": "1.1.1",
"status": "OFFLINE",
}
class Response:
status_code = 200
def raise_for_status(self):
return None
def json(self):
return payload
def fake_get(url, *, headers, timeout):
calls.update(url=url, headers=headers, timeout=timeout)
return Response()
monkeypatch.setattr(httpx, "get", fake_get)
client = HubClient("https://hub.example", "test-agent-token")
assert client.identity() == payload
assert calls["url"] == "https://hub.example/v1/agents/me/identity"
assert calls["headers"] == {"Authorization": "Bearer test-agent-token"}
def test_transport_errors_and_proxy_5xx_are_retried_but_client_errors_are_not(monkeypatch):
import httpx
from gti_fiscal_agent import client as c
monkeypatch.setattr(c.time, "sleep", lambda s: None)
calls = {"n": 0}
def flaky():
calls["n"] += 1
if calls["n"] == 1:
raise httpx.ConnectError("ssl handshake failure")
if calls["n"] == 2:
return httpx.Response(502)
return httpx.Response(200, json={"ok": True})
assert c._resilient(flaky).status_code == 200 and calls["n"] == 3
calls["n"] = 0
assert c._resilient(lambda: (calls.__setitem__("n", calls["n"] + 1), httpx.Response(401))[1]).status_code == 401 and calls["n"] == 1 # 4xx: no retry
calls["n"] = 0
def always_down():
calls["n"] += 1
raise httpx.ConnectError("down")
import pytest
with pytest.raises(httpx.ConnectError):
c._resilient(always_down)
assert calls["n"] == 10 # bounded: 10 attempts (~3 min), then the job fails cleanly