116 lines
3.7 KiB
Python
116 lines
3.7 KiB
Python
from __future__ import annotations
|
|
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
from app.api import knowledge_graph
|
|
from app.core.database import get_db
|
|
from app.dependencies.auth import get_current_user
|
|
from app.dependencies.tenant import get_tenant_id
|
|
|
|
|
|
class _FakeDB:
|
|
async def commit(self) -> None:
|
|
return None
|
|
|
|
async def rollback(self) -> None:
|
|
return None
|
|
|
|
|
|
def _build_app(*, db_session) -> FastAPI:
|
|
app = FastAPI()
|
|
app.include_router(knowledge_graph.router)
|
|
|
|
async def _fake_get_db():
|
|
yield db_session
|
|
|
|
app.dependency_overrides[get_db] = _fake_get_db
|
|
app.dependency_overrides[get_tenant_id] = lambda: 1
|
|
app.dependency_overrides[get_current_user] = lambda: SimpleNamespace(
|
|
id=9001,
|
|
tenant_id=1,
|
|
name="管理员",
|
|
username="admin",
|
|
role="ADMIN",
|
|
)
|
|
return app
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_sync_work_items_route_uses_work_item_delta_mainline(monkeypatch) -> None:
|
|
called: dict[str, int] = {}
|
|
|
|
async def _fake_enqueue_work_item_delta_events(self, *, limit: int = 500): # noqa: ANN001
|
|
called["limit"] = int(limit)
|
|
return {"enqueued": int(limit), "channel": "work_item_delta"}
|
|
|
|
async def _should_not_use_legacy_alias(self, *, limit: int = 500): # noqa: ANN001
|
|
raise AssertionError(f"legacy alias should not be used: {limit}")
|
|
|
|
monkeypatch.setattr(
|
|
knowledge_graph.ProjectKnowledgeGraphService,
|
|
"enqueue_work_item_delta_events",
|
|
_fake_enqueue_work_item_delta_events,
|
|
)
|
|
monkeypatch.setattr(
|
|
knowledge_graph.ProjectKnowledgeGraphService,
|
|
"enqueue_feishu_delta_events",
|
|
_should_not_use_legacy_alias,
|
|
)
|
|
|
|
app = _build_app(db_session=_FakeDB())
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
response = await client.post(
|
|
"/ai-steward/knowledge-graph/sync/work-items",
|
|
json={"limit": 321},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
payload = response.json()
|
|
assert payload["code"] == 0
|
|
assert payload["message"] == "success"
|
|
assert payload["data"] == {"enqueued": 321, "channel": "work_item_delta"}
|
|
assert called["limit"] == 321
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_sync_feishu_route_is_compat_alias_for_work_item_mainline(monkeypatch) -> None:
|
|
called: dict[str, int] = {}
|
|
|
|
async def _fake_enqueue_work_item_delta_events(self, *, limit: int = 500): # noqa: ANN001
|
|
called["limit"] = int(limit)
|
|
return {"enqueued": int(limit), "channel": "work_item_delta"}
|
|
|
|
async def _should_not_use_legacy_alias(self, *, limit: int = 500): # noqa: ANN001
|
|
raise AssertionError(f"legacy alias should not be used: {limit}")
|
|
|
|
monkeypatch.setattr(
|
|
knowledge_graph.ProjectKnowledgeGraphService,
|
|
"enqueue_work_item_delta_events",
|
|
_fake_enqueue_work_item_delta_events,
|
|
)
|
|
monkeypatch.setattr(
|
|
knowledge_graph.ProjectKnowledgeGraphService,
|
|
"enqueue_feishu_delta_events",
|
|
_should_not_use_legacy_alias,
|
|
)
|
|
|
|
app = _build_app(db_session=_FakeDB())
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
response = await client.post(
|
|
"/ai-steward/knowledge-graph/sync/feishu",
|
|
json={"limit": 123},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
payload = response.json()
|
|
assert payload["code"] == 0
|
|
assert payload["message"] == "success"
|
|
assert payload["data"] == {"enqueued": 123, "channel": "work_item_delta"}
|
|
assert called["limit"] == 123
|