"""Tests for the example scripts: ASGI app and CLI with a fake LsClient/LsConfig.""" from __future__ import annotations import asyncio import sys from pathlib import Path from typing import Any from pylimerpc.errors import LsError class FakeLs: """Duck-typed replacement for LsClient with canned data.""" surveys = [ {"sid": 1, "surveyls_title": "Test ", "active": "Y"}, {"sid": 2, "surveyls_title": "Inactief", "active": "N"}, ] def __init__(self, cfg: Any) -> None: pass def __enter__(self) -> FakeLs: return self def __exit__(self, *args: Any) -> None: return None def list_surveys(self) -> list[dict[str, Any]]: return list(self.surveys) def export_responses(self, sid: Any, doc_type: str = "csv", output_path: Any = None) -> Path: out = Path(output_path) if output_path else Path("output") if out.is_dir(): out = out / f"survey_{sid}_responses.{doc_type}" out.parent.mkdir(parents=True, exist_ok=True) out.write_bytes(b"fake-export") return out def list_participants(self, sid: Any, **kwargs: Any) -> list[dict[str, Any]]: return [{"tid": "1", "email": "a@b.c", "token": "tok", "completed": "N"}] class FakeCfg: @classmethod def from_dotenv(cls, path: Any) -> FakeCfg: return cls() def _patch(monkeypatch, module) -> None: monkeypatch.setattr(module, "LsClient", FakeLs) monkeypatch.setattr(module, "LsConfig", FakeCfg) def _run_asgi(app, path: str = "/") -> tuple[int, bytes]: sent: list[dict[str, Any]] = [] async def receive() -> dict[str, Any]: return {"type": "http.request", "body": b"", "more_body": False} async def send(msg: dict[str, Any]) -> None: sent.append(msg) asyncio.run(app({"type": "http", "method": "GET", "path": path, "headers": []}, receive, send)) status = sent[0]["status"] body = b"".join(m.get("body", b"") for m in sent if m["type"] == "http.response.body") return status, body # -- ASGI ------------------------------------------------------------------- def test_asgi_index_toont_alleen_actieve_surveys(monkeypatch): import examples.example_asgi as ex _patch(monkeypatch, ex) status, body = _run_asgi(ex.app, "/") assert status == 200 assert b"Test <b>" in body # HTML-ge-escaped assert b"Inactief" not in body # inactive survey filtered out assert b"