VirtForemanBackend / tests /test_stub_fallback.py
Dirbol's picture
deploy: backend 2026-06-22T11:21:06Z
47fbc33
Raw
History Blame Contribute Delete
2.07 kB
"""Test that the inference service falls back to a stub when OpenRouter fails."""
from __future__ import annotations
import asyncio
import json
import sys
from datetime import date
from app.models.tech_stack import DailyWorkInput
from app.services.tech_stack_service import infer_tech_stack
async def _run() -> int:
print("=" * 60)
print(" Stub fallback test (forces OpenRouter failure)")
print("=" * 60)
# Force-fail OpenRouter by pointing at a black hole URL.
from app.core.config import get_settings
settings = get_settings()
real_base = settings.openrouter_base_url
settings.openrouter_base_url = "http://127.0.0.1:1" # always fails
print(f"Temporarily set OPENROUTER_BASE_URL = {settings.openrouter_base_url}")
sample = DailyWorkInput(
raw_text="Залита плита перекрытия в осях А-В / 1-3",
date_from=date(2026, 5, 1),
date_to=date(2026, 5, 15),
axes="А-В / 1-3",
)
try:
response = await infer_tech_stack(sample)
finally:
settings.openrouter_base_url = real_base
print(f"\n[ok] stub response received")
print(f" request_id : {response.request_id}")
print(f" model_used : {response.tech_stack.model_used}")
print(f" inferred_summary: {response.tech_stack.inferred_summary}")
print(f" steps : {len(response.tech_stack.steps)}")
for s in response.tech_stack.steps:
print(f" - {s.title}")
out_path = "tests/.last_stub.json"
with open(out_path, "w", encoding="utf-8") as f:
json.dump(response.model_dump(mode="json"), f, ensure_ascii=False, indent=2)
print(f"\nFull JSON → {out_path}")
assert response.tech_stack.model_used, "expected a model_used marker"
assert len(response.tech_stack.steps) >= 1, "expected fallback steps"
print(
f"\n✅ PASS — model_used={response.tech_stack.model_used!r}, "
f"steps={len(response.tech_stack.steps)}"
)
return 0
if __name__ == "__main__":
sys.exit(asyncio.run(_run()))