"""Spinner middleware must answer the callback when the handler fails fast.""" import ast import asyncio from pathlib import Path import pytest SRC = Path(__file__).resolve().parents[1] / "bot" / "grok_bot.py" def _load_mw(): tree = ast.parse(SRC.read_text(encoding="utf-8")) fn = next(n for n in tree.body if isinstance(n, ast.AsyncFunctionDef) and n.name == "_cb_spinner_mw") fn.decorator_list = [] ns = {"asyncio": asyncio} exec(compile(ast.Module(body=[fn], type_ignores=[]), str(SRC), "exec"), ns) return ns["_cb_spinner_mw"] class _Ev: def __init__(self): self.answered = 0 async def answer(self): self.answered += 1 def test_fast_failure_answers_and_reraises(): mw, ev = _load_mw(), _Ev() async def handler(e, d): raise ValueError("boom") with pytest.raises(ValueError): asyncio.run(mw(handler, ev, {})) assert ev.answered == 1 def test_success_does_not_double_answer(): mw, ev = _load_mw(), _Ev() async def handler(e, d): return "ok" assert asyncio.run(mw(handler, ev, {})) == "ok" assert ev.answered == 0