guanghu-ice-heart/server-tools/persona-model-smart-router/test_openlux_router.py

31 lines
2.2 KiB
Python

#!/usr/bin/env python3
import importlib.util,json,unittest
from pathlib import Path
from unittest.mock import patch
S=Path(__file__).with_name('openlux_router.py'); spec=importlib.util.spec_from_file_location('router',S); M=importlib.util.module_from_spec(spec); spec.loader.exec_module(M)
C=json.loads((S.parents[2]/'routing/persona-model-api-smart-router-map.json').read_text())
E={"parent_persona_id":"ICE-P-ZY001","child_agent_id":"SUBAGENT::ICE-P-ZY001::TEST","controller":"ICE-P-ZY001","channel_id":"ICE-CH-HB001","agent_role":"TEST_DEPUTY","authority":"CURRENT_TASK_ONLY","task":"返回收到"}
class Tests(unittest.TestCase):
def test_cost_order_uses_only_available_models(self):
self.assertEqual(M.candidates(C,'LOW',{'glm-4-flash','gpt-5-nano'}),['glm-4-flash','gpt-5-nano'])
def test_quality_first_uses_high_tier_before_lower_tiers(self):
available={'qwen-flash','gpt-5.4-mini','gpt-6-astra-2026-09-03'}
self.assertEqual(M.candidates(C,'HIGH',available,'QUALITY_FIRST'),['gpt-6-astra-2026-09-03','gpt-5.4-mini','qwen-flash'])
def test_identity_envelope_and_key_never_returned(self):
def fake(url,key,payload=None,timeout=45):
if url.endswith('/models'): return {"data":[{"id":"qwen-flash"}]}
self.assertIn('ICE-P-ZY001',payload['messages'][0]['content'])
self.assertIn('FACT',payload['messages'][0]['content'])
self.assertIn('禁止虚构日志',payload['messages'][0]['content'])
self.assertEqual(key,'hidden')
return {"choices":[{"message":{"content":"收到"}}]}
with patch.object(M,'request_json',side_effect=fake):
result=M.route(E,C,secret_reader=lambda _: 'hidden')
self.assertEqual(result['selected_model'],'qwen-flash'); self.assertNotIn('hidden',json.dumps(result))
self.assertEqual(result['response_validation_state'],'CALLER_REVIEW_REQUIRED')
def test_missing_identity_and_unallowlisted_url_fail(self):
with self.assertRaisesRegex(M.RouterError,'IDENTITY_ENVELOPE'): M.route({"task":"x"},C,secret_reader=lambda _:'hidden')
with self.assertRaisesRegex(M.RouterError,'NOT_ALLOWLISTED'): M.request_json('https://evil.example/v1/models','hidden')
if __name__=='__main__': unittest.main()