"""Acceptance transport retries cannot resend annotation writes."""
from urllib.error import HTTPError,URLError
from urllib.request import Request
import pytest

from scripts import review_http_retry as retry


def test_retry_only_transport_failures(monkeypatch):
    calls=[];response=object()
    def transport(url,timeout):
        calls.append(url)
        if len(calls)==1:raise URLError('TLS handshake ended')
        return response
    monkeypatch.setattr(retry,'urlopen',transport);monkeypatch.setattr(retry.time,'sleep',lambda _:None)
    assert retry.open_checked('https://example.invalid/healthz') is response
    assert len(calls)==2


def test_annotation_write_never_sent_or_retried(monkeypatch):
    calls=[];monkeypatch.setattr(retry,'urlopen',lambda *a,**k:calls.append(a))
    with pytest.raises(ValueError,match='annotation writes'):
        retry.open_checked(Request('https://example.invalid/api/review/annotations/target',data=b'{}'))
    assert not calls


def test_http_authorization_failure_is_not_retried(monkeypatch):
    calls=[]
    def transport(url,timeout):
        calls.append(url);raise HTTPError(url,401,'Unauthorized',{},None)
    monkeypatch.setattr(retry,'urlopen',transport)
    with pytest.raises(HTTPError):retry.open_checked('https://example.invalid/api/review/summary')
    assert len(calls)==1
