Files
awoooi/apps/api/tests/test_mcp_audit_service.py
Your Name 0e2e856f12
All checks were successful
Code Review / ai-code-review (push) Successful in 11s
CD Pipeline / tests (push) Successful in 58s
CD Pipeline / build-and-deploy (push) Successful in 4m39s
CD Pipeline / post-deploy-checks (push) Successful in 1m17s
fix(mcp): normalize audit session ids
2026-05-06 17:40:42 +08:00

56 lines
1.4 KiB
Python

from __future__ import annotations
from typing import Any
import pytest
from src.services import mcp_audit_service
class _FakeDb:
def __init__(self) -> None:
self.executed_params: list[dict[str, Any]] = []
async def execute(self, _statement: Any, params: dict[str, Any]) -> None:
self.executed_params.append(params)
class _FakeDbContext:
def __init__(self, db: _FakeDb) -> None:
self.db = db
async def __aenter__(self) -> _FakeDb:
return self.db
async def __aexit__(self, *_args: Any) -> None:
return None
@pytest.mark.asyncio
async def test_record_mcp_call_normalizes_long_session_id(monkeypatch) -> None:
db = _FakeDb()
monkeypatch.setattr(
mcp_audit_service,
"get_db_context",
lambda: _FakeDbContext(db),
)
await mcp_audit_service.record_mcp_call(
mcp_server="k8s_provider",
tool_name="get_pods",
input_params={
"_mcp_audit": {
"session_id": "incident:INC-20260505-E8033A:pre_decision",
"agent_role": "pre_decision_investigator",
},
},
output_result={"ok": True},
duration_ms=12,
success=True,
error_message=None,
)
audit_insert_params = db.executed_params[0]
assert audit_insert_params["session_id"] == "inc:INC-20260505-E8033A:pre"
assert len(audit_insert_params["session_id"]) <= 36