mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-11-30 02:27:09 +08:00
208 lines
7.5 KiB
Python
208 lines
7.5 KiB
Python
from __future__ import annotations
|
|
|
|
import uuid
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pytest
|
|
|
|
from langbot.pkg.api.http.authz import WorkspaceRequiredError
|
|
from langbot.pkg.api.context import ExecutionContext, PrincipalContext, PrincipalType, RequestContext, WorkspaceContext
|
|
from langbot.pkg.entity.persistence.rag import KnowledgeBase
|
|
from langbot.pkg.rag.knowledge.kbmgr import RAGManager, RuntimeKnowledgeBase
|
|
from langbot.pkg.rag.service.runtime import RAGRuntimeService
|
|
from langbot.pkg.workspace.errors import WorkspaceNotFoundError
|
|
|
|
|
|
CONTEXT = ExecutionContext('instance-a', 'workspace-a', 2)
|
|
|
|
|
|
class _ScalarResult:
|
|
def __init__(self, value):
|
|
self.value = value
|
|
|
|
def scalar_one_or_none(self):
|
|
return self.value
|
|
|
|
|
|
def _request(workspace='workspace-a'):
|
|
return RequestContext(
|
|
instance_uuid='instance-a',
|
|
placement_generation=2,
|
|
request_id='request-a',
|
|
auth_type='session',
|
|
principal=PrincipalContext(PrincipalType.ACCOUNT, account_uuid='account-a'),
|
|
workspace=WorkspaceContext(workspace, 'membership-a', 'owner', frozenset()),
|
|
)
|
|
|
|
|
|
def _runtime_app(*, binding='instance-a', kb_result=None):
|
|
return SimpleNamespace(
|
|
workspace_service=SimpleNamespace(
|
|
get_execution_binding=AsyncMock(return_value=SimpleNamespace(instance_uuid=binding))
|
|
),
|
|
persistence_mgr=SimpleNamespace(execute_async=AsyncMock(return_value=_ScalarResult(kb_result))),
|
|
vector_db_mgr=SimpleNamespace(),
|
|
storage_mgr=SimpleNamespace(),
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
'context',
|
|
[
|
|
None,
|
|
ExecutionContext('', 'workspace-a', 2),
|
|
ExecutionContext('instance-a', '', 2),
|
|
ExecutionContext('instance-a', 'workspace-a', 0),
|
|
],
|
|
)
|
|
async def test_rag_runtime_requires_complete_execution_context(context):
|
|
service = RAGRuntimeService(_runtime_app())
|
|
|
|
with pytest.raises(WorkspaceRequiredError):
|
|
await service._validate_execution_context(context)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_rag_runtime_rejects_missing_workspace_service():
|
|
service = RAGRuntimeService(SimpleNamespace())
|
|
|
|
with pytest.raises(WorkspaceRequiredError, match='execution service is unavailable'):
|
|
await service._validate_execution_context(CONTEXT)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_rag_runtime_rejects_wrong_instance_before_database_lookup():
|
|
app = _runtime_app(binding='instance-b', kb_result='kb-a')
|
|
service = RAGRuntimeService(app)
|
|
|
|
with pytest.raises(WorkspaceRequiredError, match='another LangBot instance'):
|
|
await service._resolve_knowledge_base_uuid(CONTEXT, 'kb-a')
|
|
|
|
app.persistence_mgr.execute_async.assert_not_awaited()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_rag_runtime_maps_unknown_collection_to_workspace_not_found():
|
|
app = _runtime_app(kb_result=None)
|
|
service = RAGRuntimeService(app)
|
|
|
|
with pytest.raises(WorkspaceNotFoundError, match='Knowledge base not found'):
|
|
await service._resolve_knowledge_base_uuid(CONTEXT, 'missing')
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize('collection_id', ['', ' ', None])
|
|
async def test_rag_runtime_rejects_empty_collection_id(collection_id):
|
|
service = RAGRuntimeService(_runtime_app())
|
|
|
|
with pytest.raises(WorkspaceNotFoundError, match='Knowledge base not found'):
|
|
await service._resolve_knowledge_base_uuid(CONTEXT, collection_id)
|
|
|
|
service.ap.persistence_mgr.execute_async.assert_not_awaited()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_runtime_knowledge_base_store_artifact_rejects_cross_workspace_context():
|
|
app = _runtime_app()
|
|
app.artifact_service = SimpleNamespace(get=AsyncMock())
|
|
entity = KnowledgeBase(uuid='kb-a', workspace_uuid='workspace-a', name='KB', collection_id='kb-a')
|
|
runtime = RuntimeKnowledgeBase(app, entity, CONTEXT)
|
|
|
|
with pytest.raises(WorkspaceNotFoundError, match='Knowledge base not found'):
|
|
await runtime.store_artifact(_request('workspace-b'), 'art-a')
|
|
|
|
app.artifact_service.get.assert_not_awaited()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_runtime_knowledge_base_store_artifact_rejects_invalid_runtime_context():
|
|
app = _runtime_app()
|
|
app.artifact_service = SimpleNamespace(get=AsyncMock())
|
|
entity = KnowledgeBase(uuid='kb-a', workspace_uuid='workspace-a', name='KB', collection_id='kb-a')
|
|
runtime = RuntimeKnowledgeBase(app, entity, CONTEXT)
|
|
|
|
with pytest.raises(WorkspaceRequiredError, match='ExecutionContext is required'):
|
|
await runtime._assert_execution_context(None)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_runtime_knowledge_base_store_artifact_schedules_file_with_source_context():
|
|
app = _runtime_app()
|
|
app.artifact_service = SimpleNamespace(
|
|
get=AsyncMock(return_value={'name': 'guide.md', 'size': 123}),
|
|
)
|
|
entity = KnowledgeBase(uuid='kb-a', workspace_uuid='workspace-a', name='KB', collection_id='kb-a')
|
|
runtime = RuntimeKnowledgeBase(app, entity, CONTEXT)
|
|
runtime._schedule_file = AsyncMock(return_value=17)
|
|
request = _request()
|
|
|
|
result = await runtime.store_artifact(request, 'artifact-a', parser_plugin_id='parser-a')
|
|
|
|
assert result == 17
|
|
app.artifact_service.get.assert_awaited_once_with(request, 'artifact-a')
|
|
scheduled_context, file_data, parser_plugin_id, file_size = runtime._schedule_file.await_args.args
|
|
assert scheduled_context == ExecutionContext.from_request(request)
|
|
uuid.UUID(file_data['uuid'])
|
|
assert {key: file_data[key] for key in file_data if key != 'uuid'} == {
|
|
'workspace_uuid': 'workspace-a',
|
|
'kb_id': 'kb-a',
|
|
'file_name': 'guide.md',
|
|
'extension': 'md',
|
|
'status': 'pending',
|
|
'artifact_id': 'artifact-a',
|
|
}
|
|
assert parser_plugin_id == 'parser-a'
|
|
assert file_size == 123
|
|
assert runtime._schedule_file.await_args.kwargs == {'source_context': request}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_runtime_knowledge_base_store_artifact_rejects_zip():
|
|
app = _runtime_app()
|
|
app.artifact_service = SimpleNamespace(
|
|
get=AsyncMock(return_value={'name': 'archive.zip', 'size': 123}),
|
|
)
|
|
entity = KnowledgeBase(uuid='kb-a', workspace_uuid='workspace-a', name='KB', collection_id='kb-a')
|
|
runtime = RuntimeKnowledgeBase(app, entity, CONTEXT)
|
|
runtime._schedule_file = AsyncMock()
|
|
request = _request()
|
|
|
|
with pytest.raises(ValueError, match='ZIP ingestion is not supported'):
|
|
await runtime.store_artifact(request, 'artifact-a')
|
|
|
|
runtime._schedule_file.assert_not_awaited()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_rag_manager_to_execution_context_rejects_invalid_context():
|
|
app = _runtime_app()
|
|
manager = RAGManager(app)
|
|
|
|
with pytest.raises(WorkspaceRequiredError, match='RequestContext or ExecutionContext is required'):
|
|
await manager._to_execution_context(None)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_rag_manager_to_execution_context_converts_request_context():
|
|
manager = RAGManager(_runtime_app())
|
|
request = _request()
|
|
|
|
result = await manager._to_execution_context(request)
|
|
|
|
assert (result.instance_uuid, result.workspace_uuid, result.placement_generation) == (
|
|
CONTEXT.instance_uuid,
|
|
CONTEXT.workspace_uuid,
|
|
CONTEXT.placement_generation,
|
|
)
|
|
assert result.trigger_principal == request.principal
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_rag_manager_to_execution_context_rejects_cross_instance_binding():
|
|
manager = RAGManager(_runtime_app(binding='instance-b'))
|
|
|
|
with pytest.raises(WorkspaceNotFoundError, match='Workspace not found'):
|
|
await manager._to_execution_context(CONTEXT)
|