monitor-probe.py

import ast,asyncio,json,socket
from pathlib import Path
from types import SimpleNamespace
from datetime import datetime,timezone
from typing import Optional,Dict,Any
from unittest.mock import AsyncMock,Mock
from pydantic import BaseModel,Field
from tenacity import retry,stop_after_attempt,wait_exponential
attempts=[]
def deny(*a,**k):attempts.append('network');raise AssertionError('offline')
socket.socket.connect=deny;socket.getaddrinfo=deny
tree=ast.parse(Path('/tmp/fr21-current-review/Cell1.py').read_text());model=next(n for n in tree.body if isinstance(n,ast.ClassDef) and n.name=='HealthStatus');cls=next(n for n in tree.body if isinstance(n,ast.ClassDef) and n.name=='HealthMonitor');method=next(n for n in cls.body if getattr(n,'name',None)=='check_service_health')
ns=dict(globals(),AgentManifest=object,CONFIG=SimpleNamespace(retry_attempts=1,retry_min_delay=1,retry_max_delay=1),service_health_checks=Mock())
exec(compile(ast.Module(body=[model,method],type_ignores=[]),'Cell1-extracted-monitor','exec'),ns)
out=[]
for reported in ['degraded','not_configured','unhealthy']:
 response=SimpleNamespace(status_code=200,json=lambda:{'status':reported,'checks':{'firestore':'unhealthy'}})
 monitor=SimpleNamespace(http_client=SimpleNamespace(get=AsyncMock(return_value=response)))
 result=asyncio.run(ns['check_service_health'](monitor,SimpleNamespace(id='fixture',health_endpoint='/health',endpoints={'task_handler':'http://fixture.invalid/tasks/send'})))
 assert result.status=='healthy';out.append({'reported_body_status':reported,'monitor_classification':result.status})
assert not attempts
Path('/tmp/fr21-current-review/monitor-results.json').write_text(json.dumps({'scope':'exact source HealthStatus and decorated check_service_health method, fixtureHTTP200 bodies; no network','results':out},indent=2)+'\n')
print(out)
← All docsView source on GitHub →