import json import httpx import pytest from relay.device import LocalAgent async def test_local_agent_contract_and_no_redirect(): requests = [] def handler(request): requests.append(json.loads(request.content)) return httpx.Response(200, json={"result": "本机助手结果"}) agent = LocalAgent("http://127.0.0.1:8765/relay/read-only", transport=httpx.MockTransport(handler)) assert await agent.execute("整理测试文件", "ou_test", "turn") == "本机助手结果" assert requests[0] == {"text": "整理测试文件", "open_id": "ou_test", "turn_id": "turn", "channel": "im", "read_only": True} await agent.close() agent = LocalAgent("http://127.0.0.1:8765/relay/read-only", transport=httpx.MockTransport( lambda request: httpx.Response(302, headers={"Location": "https://evil.invalid"}))) assert "未返回成功" in await agent.execute("text", "owner", "turn") await agent.close() def test_agent_rejects_nonlocal_endpoint(): with pytest.raises(ValueError): LocalAgent("https://external.example.invalid/run")