from __future__ import annotations

import os
import subprocess
import sys


def main() -> None:
    code = r"""
import asyncio
from services.llm import LLMService

async def run():
    svc = LLMService()
    text, source = await svc.reply_with_meta("测试降级")
    print("source:", source)
    print("reply:", text)

asyncio.run(run())
"""
    env = os.environ.copy()
    env["LLM_BASE_URL"] = "http://127.0.0.1:9"
    env["LLM_TIMEOUT"] = "1"
    proc = subprocess.run([sys.executable, "-c", code], env=env, capture_output=True, text=True)
    print(proc.stdout.strip())
    if proc.returncode != 0:
        print(proc.stderr.strip())
        raise SystemExit(proc.returncode)
    if "source: fallback" not in proc.stdout:
        raise SystemExit("fallback check failed: source is not fallback")
    print("fallback check: ok")


if __name__ == "__main__":
    main()
