mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-17 19:24:19 +00:00
test: format test suite (#2252)
This commit is contained in:
@@ -6,6 +6,7 @@ Tests cover:
|
||||
- RAG methods (ingest, retrieve, schema)
|
||||
- Disabled plugin early returns
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
@@ -86,16 +87,12 @@ class TestListPlugins:
|
||||
return_value=[
|
||||
{
|
||||
'manifest': {'manifest': {'metadata': {'author': 'a', 'name': 'p1'}}},
|
||||
'components': [
|
||||
{'manifest': {'manifest': {'kind': 'Command'}}}
|
||||
],
|
||||
'components': [{'manifest': {'manifest': {'kind': 'Command'}}}],
|
||||
'debug': False,
|
||||
},
|
||||
{
|
||||
'manifest': {'manifest': {'metadata': {'author': 'b', 'name': 'p2'}}},
|
||||
'components': [
|
||||
{'manifest': {'manifest': {'kind': 'Tool'}}}
|
||||
],
|
||||
'components': [{'manifest': {'manifest': {'kind': 'Tool'}}}],
|
||||
'debug': False,
|
||||
},
|
||||
]
|
||||
@@ -127,9 +124,7 @@ class TestListPlugins:
|
||||
},
|
||||
]
|
||||
)
|
||||
connector.ap.persistence_mgr.execute_async = AsyncMock(
|
||||
return_value=Mock(__iter__=lambda self: iter([]))
|
||||
)
|
||||
connector.ap.persistence_mgr.execute_async = AsyncMock(return_value=Mock(__iter__=lambda self: iter([])))
|
||||
|
||||
result = await connector.list_plugins()
|
||||
|
||||
@@ -230,7 +225,8 @@ class TestCallParser:
|
||||
)
|
||||
|
||||
connector.handler.parse_document.assert_called_once_with(
|
||||
'author', 'parser',
|
||||
'author',
|
||||
'parser',
|
||||
{'mime_type': 'text/plain', 'filename': 'test.txt'},
|
||||
b'file content',
|
||||
)
|
||||
@@ -251,9 +247,7 @@ class TestRAGMethods:
|
||||
|
||||
result = await connector.call_rag_ingest('author/engine', {'file': 'test.pdf'})
|
||||
|
||||
connector.handler.rag_ingest_document.assert_called_once_with(
|
||||
'author', 'engine', {'file': 'test.pdf'}
|
||||
)
|
||||
connector.handler.rag_ingest_document.assert_called_once_with('author', 'engine', {'file': 'test.pdf'})
|
||||
assert result['status'] == 'success'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -264,14 +258,16 @@ class TestRAGMethods:
|
||||
|
||||
connector.handler = AsyncMock()
|
||||
connector.handler.retrieve_knowledge = AsyncMock(
|
||||
return_value={'results': [{'id': 'doc1', 'content': [{'type': 'text', 'text': 'test'}], 'metadata': {}, 'distance': 0.1}]}
|
||||
return_value={
|
||||
'results': [
|
||||
{'id': 'doc1', 'content': [{'type': 'text', 'text': 'test'}], 'metadata': {}, 'distance': 0.1}
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
result = await connector.call_rag_retrieve('author/engine', {'query': 'test'})
|
||||
|
||||
connector.handler.retrieve_knowledge.assert_called_once_with(
|
||||
'author', 'engine', '', {'query': 'test'}
|
||||
)
|
||||
connector.handler.retrieve_knowledge.assert_called_once_with('author', 'engine', '', {'query': 'test'})
|
||||
assert result == {
|
||||
'results': [
|
||||
{
|
||||
@@ -290,9 +286,7 @@ class TestRAGMethods:
|
||||
connector = create_mock_connector()
|
||||
|
||||
connector.handler = AsyncMock()
|
||||
connector.handler.get_rag_creation_schema = AsyncMock(
|
||||
return_value={'properties': {'name': {'type': 'string'}}}
|
||||
)
|
||||
connector.handler.get_rag_creation_schema = AsyncMock(return_value={'properties': {'name': {'type': 'string'}}})
|
||||
|
||||
result = await connector.get_rag_creation_schema('author/engine')
|
||||
|
||||
@@ -326,9 +320,7 @@ class TestRAGMethods:
|
||||
|
||||
await connector.rag_on_kb_create('author/engine', 'kb-uuid', {'model': 'test'})
|
||||
|
||||
connector.handler.rag_on_kb_create.assert_called_once_with(
|
||||
'author', 'engine', 'kb-uuid', {'model': 'test'}
|
||||
)
|
||||
connector.handler.rag_on_kb_create.assert_called_once_with('author', 'engine', 'kb-uuid', {'model': 'test'})
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rag_on_kb_delete(self):
|
||||
@@ -354,9 +346,7 @@ class TestRAGMethods:
|
||||
|
||||
result = await connector.call_rag_delete_document('author/engine', 'doc-uuid', 'kb-uuid')
|
||||
|
||||
connector.handler.rag_delete_document.assert_called_once_with(
|
||||
'author', 'engine', 'doc-uuid', 'kb-uuid'
|
||||
)
|
||||
connector.handler.rag_delete_document.assert_called_once_with('author', 'engine', 'doc-uuid', 'kb-uuid')
|
||||
assert result is True
|
||||
|
||||
|
||||
@@ -446,9 +436,7 @@ class TestGetPluginInfo:
|
||||
connector = create_mock_connector()
|
||||
|
||||
connector.handler = AsyncMock()
|
||||
connector.handler.get_plugin_info = AsyncMock(
|
||||
return_value={'manifest': {'metadata': {'name': 'plugin'}}}
|
||||
)
|
||||
connector.handler.get_plugin_info = AsyncMock(return_value={'manifest': {'metadata': {'name': 'plugin'}}})
|
||||
|
||||
result = await connector.get_plugin_info('author', 'plugin')
|
||||
|
||||
@@ -470,9 +458,7 @@ class TestSetPluginConfig:
|
||||
|
||||
await connector.set_plugin_config('author', 'plugin', {'setting': 'value'})
|
||||
|
||||
connector.handler.set_plugin_config.assert_called_once_with(
|
||||
'author', 'plugin', {'setting': 'value'}
|
||||
)
|
||||
connector.handler.set_plugin_config.assert_called_once_with('author', 'plugin', {'setting': 'value'})
|
||||
|
||||
|
||||
class TestPingPluginRuntime:
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
Tests cover:
|
||||
- _parse_plugin_id() parsing and validation
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -6,6 +6,7 @@ Tests cover:
|
||||
- Handling missing requirements.txt
|
||||
- Handling empty/malformed requirements.txt
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import zipfile
|
||||
@@ -82,13 +83,13 @@ class TestExtractDepsMetadata:
|
||||
"""Test that comments and empty lines are filtered."""
|
||||
connector_instance = create_mock_connector()
|
||||
|
||||
requirements = '''# This is a comment
|
||||
requirements = """# This is a comment
|
||||
requests>=2.0
|
||||
|
||||
# Another comment
|
||||
flask==1.0
|
||||
|
||||
numpy'''
|
||||
numpy"""
|
||||
zip_bytes = create_zip_with_requirements(requirements)
|
||||
|
||||
task_context = Mock()
|
||||
@@ -147,9 +148,9 @@ numpy'''
|
||||
"""Test handling requirements.txt with only comments."""
|
||||
connector_instance = create_mock_connector()
|
||||
|
||||
requirements = '''# Comment 1
|
||||
requirements = """# Comment 1
|
||||
# Comment 2
|
||||
# Comment 3'''
|
||||
# Comment 3"""
|
||||
zip_bytes = create_zip_with_requirements(requirements)
|
||||
|
||||
task_context = Mock()
|
||||
|
||||
@@ -40,11 +40,13 @@ class TestHandlerQueryVariables:
|
||||
"""Test set_query_var returns error when query not found."""
|
||||
runtime_handler = make_handler(mock_app)
|
||||
|
||||
response = await runtime_handler.actions[PluginToRuntimeAction.SET_QUERY_VAR.value]({
|
||||
'query_id': 'nonexistent-query',
|
||||
'key': 'test_var',
|
||||
'value': 'test_value',
|
||||
})
|
||||
response = await runtime_handler.actions[PluginToRuntimeAction.SET_QUERY_VAR.value](
|
||||
{
|
||||
'query_id': 'nonexistent-query',
|
||||
'key': 'test_var',
|
||||
'value': 'test_value',
|
||||
}
|
||||
)
|
||||
|
||||
assert response.code != 0
|
||||
assert 'nonexistent-query' in response.message
|
||||
@@ -58,11 +60,13 @@ class TestHandlerQueryVariables:
|
||||
|
||||
mock_app.query_pool.cached_queries['test-query'] = mock_query
|
||||
|
||||
response = await runtime_handler.actions[PluginToRuntimeAction.SET_QUERY_VAR.value]({
|
||||
'query_id': 'test-query',
|
||||
'key': 'test_var',
|
||||
'value': 'test_value',
|
||||
})
|
||||
response = await runtime_handler.actions[PluginToRuntimeAction.SET_QUERY_VAR.value](
|
||||
{
|
||||
'query_id': 'test-query',
|
||||
'key': 'test_var',
|
||||
'value': 'test_value',
|
||||
}
|
||||
)
|
||||
|
||||
assert response.code == 0
|
||||
assert mock_query.variables['test_var'] == 'test_value'
|
||||
@@ -76,10 +80,12 @@ class TestHandlerQueryVariables:
|
||||
|
||||
mock_app.query_pool.cached_queries['test-query'] = mock_query
|
||||
|
||||
response = await runtime_handler.actions[PluginToRuntimeAction.GET_QUERY_VAR.value]({
|
||||
'query_id': 'test-query',
|
||||
'key': 'existing_var',
|
||||
})
|
||||
response = await runtime_handler.actions[PluginToRuntimeAction.GET_QUERY_VAR.value](
|
||||
{
|
||||
'query_id': 'test-query',
|
||||
'key': 'existing_var',
|
||||
}
|
||||
)
|
||||
|
||||
assert response.code == 0
|
||||
assert response.data == {'value': 'existing_value'}
|
||||
@@ -93,9 +99,11 @@ class TestHandlerQueryVariables:
|
||||
|
||||
mock_app.query_pool.cached_queries['test-query'] = mock_query
|
||||
|
||||
response = await runtime_handler.actions[PluginToRuntimeAction.GET_QUERY_VARS.value]({
|
||||
'query_id': 'test-query',
|
||||
})
|
||||
response = await runtime_handler.actions[PluginToRuntimeAction.GET_QUERY_VARS.value](
|
||||
{
|
||||
'query_id': 'test-query',
|
||||
}
|
||||
)
|
||||
|
||||
assert response.code == 0
|
||||
assert response.data == {'vars': mock_query.variables}
|
||||
@@ -108,7 +116,7 @@ class TestHandlerRagErrorResponse:
|
||||
"""Test basic error response creation."""
|
||||
from langbot.pkg.plugin.handler import _make_rag_error_response
|
||||
|
||||
error = Exception("test error")
|
||||
error = Exception('test error')
|
||||
response = _make_rag_error_response(error, 'TestError')
|
||||
|
||||
# ActionResponse is a pydantic model, check message field
|
||||
@@ -120,13 +128,8 @@ class TestHandlerRagErrorResponse:
|
||||
"""Test error response with extra context."""
|
||||
from langbot.pkg.plugin.handler import _make_rag_error_response
|
||||
|
||||
error = ValueError("invalid input")
|
||||
response = _make_rag_error_response(
|
||||
error,
|
||||
'ValidationError',
|
||||
field='name',
|
||||
value='test'
|
||||
)
|
||||
error = ValueError('invalid input')
|
||||
response = _make_rag_error_response(error, 'ValidationError', field='name', value='test')
|
||||
|
||||
assert 'ValidationError' in response.message
|
||||
assert 'field=name' in response.message
|
||||
@@ -137,7 +140,7 @@ class TestHandlerRagErrorResponse:
|
||||
"""Test error response includes exception type."""
|
||||
from langbot.pkg.plugin.handler import _make_rag_error_response
|
||||
|
||||
error = RuntimeError("connection failed")
|
||||
error = RuntimeError('connection failed')
|
||||
response = _make_rag_error_response(error, 'ConnectionError')
|
||||
|
||||
assert 'RuntimeError' in response.message
|
||||
@@ -148,7 +151,7 @@ class TestHandlerRagErrorResponse:
|
||||
"""Test error response with no extra context."""
|
||||
from langbot.pkg.plugin.handler import _make_rag_error_response
|
||||
|
||||
error = KeyError("missing_key")
|
||||
error = KeyError('missing_key')
|
||||
response = _make_rag_error_response(error, 'LookupError')
|
||||
|
||||
# No context parts means no brackets
|
||||
|
||||
@@ -47,12 +47,14 @@ class TestInitializePluginSettings:
|
||||
Mock(),
|
||||
]
|
||||
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.INITIALIZE_PLUGIN_SETTINGS.value]({
|
||||
'plugin_author': 'test-author',
|
||||
'plugin_name': 'test-plugin',
|
||||
'install_source': 'local',
|
||||
'install_info': {'path': '/test'},
|
||||
})
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.INITIALIZE_PLUGIN_SETTINGS.value](
|
||||
{
|
||||
'plugin_author': 'test-author',
|
||||
'plugin_name': 'test-plugin',
|
||||
'install_source': 'local',
|
||||
'install_info': {'path': '/test'},
|
||||
}
|
||||
)
|
||||
|
||||
assert response.code == 0
|
||||
assert app.persistence_mgr.execute_async.await_count == 2
|
||||
@@ -82,12 +84,14 @@ class TestInitializePluginSettings:
|
||||
Mock(),
|
||||
]
|
||||
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.INITIALIZE_PLUGIN_SETTINGS.value]({
|
||||
'plugin_author': 'test-author',
|
||||
'plugin_name': 'test-plugin',
|
||||
'install_source': 'github',
|
||||
'install_info': {'repo': 'author/name'},
|
||||
})
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.INITIALIZE_PLUGIN_SETTINGS.value](
|
||||
{
|
||||
'plugin_author': 'test-author',
|
||||
'plugin_name': 'test-plugin',
|
||||
'install_source': 'github',
|
||||
'install_info': {'repo': 'author/name'},
|
||||
}
|
||||
)
|
||||
|
||||
assert response.code == 0
|
||||
assert app.persistence_mgr.execute_async.await_count == 3
|
||||
@@ -161,9 +165,7 @@ class TestSetBinaryStorage:
|
||||
runtime_handler = make_handler(app)
|
||||
app.persistence_mgr.execute_async.return_value = make_result(SimpleNamespace(value=b'old'))
|
||||
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.SET_BINARY_STORAGE.value](
|
||||
self.payload(b'new')
|
||||
)
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.SET_BINARY_STORAGE.value](self.payload(b'new'))
|
||||
|
||||
assert response.code == 0
|
||||
assert app.persistence_mgr.execute_async.await_count == 2
|
||||
@@ -203,9 +205,7 @@ class TestSetBinaryStorage:
|
||||
runtime_handler = make_handler(app)
|
||||
app.instance_config.data['plugin']['binary_storage']['max_value_bytes'] = 0
|
||||
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.SET_BINARY_STORAGE.value](
|
||||
self.payload(b'x')
|
||||
)
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.SET_BINARY_STORAGE.value](self.payload(b'x'))
|
||||
|
||||
assert response.code != 0
|
||||
assert '1 > 0 bytes' in response.message
|
||||
@@ -228,10 +228,12 @@ class TestGetPluginSettings:
|
||||
runtime_handler = make_handler(app)
|
||||
app.persistence_mgr.execute_async.return_value = make_result()
|
||||
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.GET_PLUGIN_SETTINGS.value]({
|
||||
'plugin_author': 'test-author',
|
||||
'plugin_name': 'test-plugin',
|
||||
})
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.GET_PLUGIN_SETTINGS.value](
|
||||
{
|
||||
'plugin_author': 'test-author',
|
||||
'plugin_name': 'test-plugin',
|
||||
}
|
||||
)
|
||||
|
||||
assert response.code == 0
|
||||
assert response.data == {
|
||||
@@ -255,10 +257,12 @@ class TestGetPluginSettings:
|
||||
)
|
||||
app.persistence_mgr.execute_async.return_value = make_result(setting)
|
||||
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.GET_PLUGIN_SETTINGS.value]({
|
||||
'plugin_author': 'test-author',
|
||||
'plugin_name': 'test-plugin',
|
||||
})
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.GET_PLUGIN_SETTINGS.value](
|
||||
{
|
||||
'plugin_author': 'test-author',
|
||||
'plugin_name': 'test-plugin',
|
||||
}
|
||||
)
|
||||
|
||||
assert response.code == 0
|
||||
assert response.data == {
|
||||
@@ -286,11 +290,13 @@ class TestGetBinaryStorage:
|
||||
runtime_handler = make_handler(app)
|
||||
app.persistence_mgr.execute_async.return_value = make_result(SimpleNamespace(value=b'test binary content'))
|
||||
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.GET_BINARY_STORAGE.value]({
|
||||
'key': 'test-key',
|
||||
'owner_type': 'plugin',
|
||||
'owner': 'test-owner',
|
||||
})
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.GET_BINARY_STORAGE.value](
|
||||
{
|
||||
'key': 'test-key',
|
||||
'owner_type': 'plugin',
|
||||
'owner': 'test-owner',
|
||||
}
|
||||
)
|
||||
|
||||
assert response.code == 0
|
||||
assert response.data == {
|
||||
@@ -303,11 +309,13 @@ class TestGetBinaryStorage:
|
||||
runtime_handler = make_handler(app)
|
||||
app.persistence_mgr.execute_async.return_value = make_result()
|
||||
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.GET_BINARY_STORAGE.value]({
|
||||
'key': 'test-key',
|
||||
'owner_type': 'plugin',
|
||||
'owner': 'test-owner',
|
||||
})
|
||||
response = await runtime_handler.actions[RuntimeToLangBotAction.GET_BINARY_STORAGE.value](
|
||||
{
|
||||
'key': 'test-key',
|
||||
'owner_type': 'plugin',
|
||||
'owner': 'test-owner',
|
||||
}
|
||||
)
|
||||
|
||||
assert response.code != 0
|
||||
assert 'Storage with key test-key not found' in response.message
|
||||
@@ -329,9 +337,11 @@ class TestHandlerQueryLookup:
|
||||
"""Query-bound actions return error when query_id is not cached."""
|
||||
runtime_handler = make_handler(app)
|
||||
|
||||
response = await runtime_handler.actions[PluginToRuntimeAction.GET_BOT_UUID.value]({
|
||||
'query_id': 'nonexistent-query',
|
||||
})
|
||||
response = await runtime_handler.actions[PluginToRuntimeAction.GET_BOT_UUID.value](
|
||||
{
|
||||
'query_id': 'nonexistent-query',
|
||||
}
|
||||
)
|
||||
|
||||
assert response.code != 0
|
||||
assert 'nonexistent-query' in response.message
|
||||
@@ -343,9 +353,11 @@ class TestHandlerQueryLookup:
|
||||
query = SimpleNamespace(variables={}, bot_uuid='test-bot-uuid')
|
||||
app.query_pool.cached_queries['existing-query'] = query
|
||||
|
||||
response = await runtime_handler.actions[PluginToRuntimeAction.GET_BOT_UUID.value]({
|
||||
'query_id': 'existing-query',
|
||||
})
|
||||
response = await runtime_handler.actions[PluginToRuntimeAction.GET_BOT_UUID.value](
|
||||
{
|
||||
'query_id': 'existing-query',
|
||||
}
|
||||
)
|
||||
|
||||
assert response.code == 0
|
||||
assert response.data == {'bot_uuid': 'test-bot-uuid'}
|
||||
|
||||
@@ -4,6 +4,7 @@ Tests cover:
|
||||
- _make_rag_error_response() helper function
|
||||
- RuntimeConnectionHandler cleanup_plugin_data method
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
@@ -23,7 +24,7 @@ class TestMakeRagErrorResponse:
|
||||
"""Test basic error response creation."""
|
||||
handler = get_handler_module()
|
||||
|
||||
error = ValueError("test error message")
|
||||
error = ValueError('test error message')
|
||||
result = handler._make_rag_error_response(error, 'TestError')
|
||||
|
||||
# ActionResponse.error() returns code=1 (error status)
|
||||
@@ -36,7 +37,7 @@ class TestMakeRagErrorResponse:
|
||||
"""Test that error type is included in message."""
|
||||
handler = get_handler_module()
|
||||
|
||||
error = RuntimeError("something went wrong")
|
||||
error = RuntimeError('something went wrong')
|
||||
result = handler._make_rag_error_response(error, 'VectorStoreError')
|
||||
|
||||
assert '[VectorStoreError/RuntimeError]' in result.message
|
||||
@@ -45,7 +46,7 @@ class TestMakeRagErrorResponse:
|
||||
"""Test that extra context fields are included."""
|
||||
handler = get_handler_module()
|
||||
|
||||
error = Exception("embedding failed")
|
||||
error = Exception('embedding failed')
|
||||
result = handler._make_rag_error_response(
|
||||
error,
|
||||
'EmbeddingError',
|
||||
@@ -71,7 +72,7 @@ class TestMakeRagErrorResponse:
|
||||
"""Test multiple context fields are comma separated."""
|
||||
handler = get_handler_module()
|
||||
|
||||
error = IOError("file not found")
|
||||
error = IOError('file not found')
|
||||
result = handler._make_rag_error_response(
|
||||
error,
|
||||
'FileServiceError',
|
||||
@@ -119,9 +120,7 @@ class TestCleanupPluginData:
|
||||
handler_instance = Mock(spec=handler_module.RuntimeConnectionHandler)
|
||||
handler_instance.ap = mock_app
|
||||
|
||||
await handler_module.RuntimeConnectionHandler.cleanup_plugin_data(
|
||||
handler_instance, 'author', 'plugin-name'
|
||||
)
|
||||
await handler_module.RuntimeConnectionHandler.cleanup_plugin_data(handler_instance, 'author', 'plugin-name')
|
||||
|
||||
# Should have at least 2 calls: one for settings, one for binary storage
|
||||
assert mock_app.persistence_mgr.execute_async.call_count >= 2
|
||||
assert mock_app.persistence_mgr.execute_async.call_count >= 2
|
||||
|
||||
Reference in New Issue
Block a user