Files
worldquant-alpha-system/backend/tests/test_mcp_submission.py
T
yuxuanhui 7e990b9a69
Deploy production / deploy (push) Successful in 57s
feat: 增加仅检查 MCP 工具并完善已提交 Alpha 指标展示
2026-09-11 13:14:27 +08:00

64 lines
3.4 KiB
Python

"""Exercise MCP through the durable runner; the fake rejects any submit request."""
import pytest
from fastapi import HTTPException
from app.models import Alpha, SelfCorrelation
from tests.conftest import alpha
from tests.test_mcp import credentials, invoke
from tests.test_mcp import mcp_app as mcp_app
from tests.test_submission import FIELDS, Description, setup
@pytest.mark.parametrize("kind", ["REGULAR", "SUPER"])
@pytest.mark.parametrize("result", ["PASS", "FAIL"])
async def test_mcp_check_never_submits(mcp_app, kind, result, monkeypatch):
from tests import test_submission
checks = [{"name": "PROD_CORRELATION", "result": result}]
monkeypatch.setattr(test_submission, "CHECKS", checks)
sections = ["regular"] if kind == "REGULAR" else ["selection", "combo"]
platform = await setup(mcp_app, alpha(type=kind, **{s: {"code": "rank(close)"} for s in sections}))
platform.pending = 1
principal, _ = await credentials(mcp_app)
caps = await invoke(mcp_app, principal, "get_research_capabilities")
assert caps["submission_check"]["production_submission"] is False
state = await invoke(mcp_app, principal, "get_submission_check", {"alpha_id": "alpha1"})
assert platform.calls == []
args = {"alpha_id": "alpha1", "snapshot": state["snapshot"],
"descriptions": dict.fromkeys(sections, Description(**FIELDS).text())}
started = await invoke(mcp_app, principal, "check_submission", args)
replay = await invoke(mcp_app, principal, "check_submission", args)
assert replay["job_id"] == started["job_id"]
await mcp_app.state.runner.execute(started["job_id"])
job = await invoke(mcp_app, principal, "get_refresh_job", {"job_id": started["job_id"]})
assert job["status"] == "completed", job
data = await invoke(mcp_app, principal, "get_submission_check", {"alpha_id": "alpha1"})
assert data["checks"] == checks and data["checked_at"]
assert data["production_submission"] is False
assert platform.patches == [{s: {"description": args["descriptions"][s]} for s in sections}]
assert platform.calls.count(("GET", "/alphas/alpha1/check")) == 2
assert set(platform.calls) <= {("POST", "/authentication"), ("GET", "/alphas/alpha1"),
("PATCH", "/alphas/alpha1"), ("GET", "/alphas/alpha1/check")}
async with mcp_app.state.sessions() as db:
assert (await db.get(Alpha, "alpha1")).status == "UNSUBMITTED"
async def test_mcp_check_scope_and_guards(mcp_app):
platform = await setup(mcp_app)
reader, _ = await credentials(mcp_app, {"research:read"})
state = await invoke(mcp_app, reader, "get_submission_check", {"alpha_id": "alpha1"})
args = {"alpha_id": "alpha1", "snapshot": state["snapshot"], "descriptions": {"regular": "reviewed"}}
with pytest.raises(HTTPException) as exc:
await mcp_app.state.mcp.invoke(reader, "check_submission", args)
assert exc.value.status_code == 403
principal, _ = await credentials(mcp_app)
for extra in [{"snapshot": "0" * 64}, {"descriptions": {"combo": "wrong"}}, {"submit": True}]:
response = await mcp_app.state.mcp.invoke(principal, "check_submission", args | extra)
assert response.is_error
async with mcp_app.state.sessions.begin() as db:
(await db.get(SelfCorrelation, "alpha1")).result = {"status": "high"}
response = await mcp_app.state.mcp.invoke(principal, "check_submission", args)
assert response.is_error
assert platform.calls == []