"""Shared helpers/fixtures for the watchdog (ORCH-100, F1b) test suite.

A tiny urllib-style fake opener so HTTP collectors / Telegram transport never
touch the network (test plan §scope: all collectors/transport are mocked).
"""
from __future__ import annotations

import io
import urllib.error


class FakeResponse:
    """Context-manager response mimicking ``urllib`` ``addinfourl``."""

    def __init__(self, status: int = 200, body: bytes = b"{}"):
        self.status = status
        self._body = body

    def getcode(self):
        return self.status

    def read(self):
        return self._body

    def __enter__(self):
        return self

    def __exit__(self, *a):
        return False


def make_opener(*, status=200, body=b"{}", exc=None):
    """Build a fake ``urlopen`` that returns a body or raises ``exc``."""

    def _opener(req, timeout=None):
        if exc is not None:
            raise exc
        return FakeResponse(status=status, body=body)

    return _opener


def http_error(code: int) -> urllib.error.HTTPError:
    return urllib.error.HTTPError(
        url="http://x", code=code, msg="err", hdrs=None, fp=io.BytesIO(b"")
    )
