import pytest from laya_router.config import load_settings from laya_router.launchd import agent_plist from laya_router.render import is_slash_command, truncate_prompt from laya_router.result import normalize_answers def test_normalize_answers_reads_choice_and_noul(): result = normalize_answers( { "task_level": { "type": "choice", "choice": "normal", "confidence": 0.42, "probabilities": {"simple": 0.2, "normal": 0.5, "complex": 0.3}, }, "needs_plan": {"type": "noul", "noul": 0.61, "confidence": 0.61}, "needs_repo": {"type": "noul", "noul": 0.7, "confidence": 0.7}, "needs_web": {"type": "noul", "noul": 0.1, "confidence": 0.9}, "needs_deep_reasoning": {"type": "noul", "noul": 0.33, "confidence": 0.67}, } ) assert result["level"] == "normal" assert result["needs_plan"] == pytest.approx(0.61) assert result["level_probabilities"]["normal"] == pytest.approx(0.5) assert result["questions_version"] == "1" def test_truncate_keeps_head_and_tail(): text = "HEAD" + ("中" * 5000) + "TAIL" clipped = truncate_prompt(text, 1000) assert clipped.startswith("HEAD") assert clipped.endswith("TAIL") assert "[...truncated...]" in clipped assert len(clipped) < len(text) def test_slash_command_detection(): assert is_slash_command("/hooks") assert is_slash_command("/model gpt-5") assert not is_slash_command("/Users/richard/proj 看一下") assert not is_slash_command("帮我 /review 一下") def test_config_file_and_env(monkeypatch, tmp_path): monkeypatch.setenv("LAYA_ROUTER_HOME", str(tmp_path)) (tmp_path / "config.toml").write_text('port = 9999\ndevice = "cpu"\n', encoding="utf-8") monkeypatch.setenv("LAYA_ROUTER_PORT", "7777") settings = load_settings() assert settings.port == 7777 assert settings.device == "cpu" assert settings.model == "convaiinnovations/laya" def test_launchd_plist_is_a_single_process_without_reload(tmp_path): settings = load_settings(tmp_path) plist = agent_plist(settings) assert plist["ProgramArguments"] == [str(tmp_path / ".venv" / "bin" / "laya-router"), "serve"] assert "--reload" not in plist["ProgramArguments"] assert plist["EnvironmentVariables"]["USE_TF"] == "0" assert plist["EnvironmentVariables"]["PYTHONUNBUFFERED"] == "1" assert plist["EnvironmentVariables"]["PATH"].endswith("/usr/bin:/bin:/usr/sbin:/sbin") assert plist["KeepAlive"] is True assert plist["ThrottleInterval"] >= 10