mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-15 00:46:07 +00:00
Merge remote-tracking branch 'origin/master' into feat/card_dify_human_input
This commit is contained in:
+59
-3
@@ -1,6 +1,7 @@
|
||||
# LangBot Test Suite
|
||||
|
||||
This directory contains the test suite for LangBot, with a focus on comprehensive unit testing of pipeline stages.
|
||||
This directory contains the LangBot backend test suite, including unit tests,
|
||||
integration tests, startup E2E tests, and container-backed Box runtime tests.
|
||||
|
||||
## Quality Gate Layers
|
||||
|
||||
@@ -10,10 +11,15 @@ LangBot uses a layered quality gate system for developers and CI:
|
||||
|-------|---------|--------------|-------------|
|
||||
| **Quick** | `make test-quick` or `bash scripts/test-quick.sh` | Ruff lint + Unit tests + Smoke tests | Before every commit |
|
||||
| **Fast Integration** | `make test-integration-fast` or `bash scripts/test-integration-fast.sh` | SQLite/API/Pipeline integration (no external services) | Before PR, weekly |
|
||||
| **Backend E2E** | `uv run --python 3.12 pytest tests/e2e -q --tb=short` | Starts a real LangBot process with minimal config | Before release, CI |
|
||||
| **Box Integration** | `uv run --python 3.12 pytest tests/integration_tests -q --tb=short` | Real Box sandbox/runtime integration | Before Box/runtime changes, CI |
|
||||
| **Frontend E2E** | `cd web && pnpm test:e2e` | Playwright smoke tests with mocked backend and Space APIs | Before web changes, CI |
|
||||
| **Coverage Gate** | `make test-coverage` or `bash scripts/test-coverage.sh` | All tests with coverage, threshold: 18% | Before merge, CI |
|
||||
| **Full Local** | `make test-all-local` | Quick + Integration + Coverage | Before major changes |
|
||||
|
||||
**Note**: PostgreSQL migration tests and slow tests are NOT in local default gates. They run in separate CI workflows.
|
||||
**Note**: PostgreSQL migration tests and slow tests are NOT in local default
|
||||
gates. They run in separate CI workflows. Frontend Playwright tests live under
|
||||
`web/tests/e2e` and are documented in `web/README.md`.
|
||||
|
||||
### Developer Workflow
|
||||
|
||||
@@ -28,6 +34,9 @@ make test-all-local
|
||||
bash scripts/test-quick.sh # ~2 min
|
||||
bash scripts/test-integration-fast.sh # ~3 min
|
||||
bash scripts/test-coverage.sh # ~8 min
|
||||
uv run --python 3.12 pytest tests/e2e -q --tb=short
|
||||
uv run --python 3.12 pytest tests/integration_tests -q --tb=short
|
||||
cd web && pnpm test:e2e
|
||||
```
|
||||
|
||||
### Coverage Baseline
|
||||
@@ -70,6 +79,12 @@ tests/
|
||||
│ └── persistence/ # Database/persistence tests
|
||||
│ ├── __init__.py
|
||||
│ └── test_migrations.py # Alembic migration tests
|
||||
├── e2e/ # Real LangBot startup E2E tests
|
||||
│ ├── conftest.py
|
||||
│ ├── test_startup.py
|
||||
│ └── utils/
|
||||
├── integration_tests/ # Container-backed integration tests
|
||||
│ └── box/ # Box runtime and MCP process tests
|
||||
├── smoke/ # Smoke tests (quick validation)
|
||||
│ └── test_fake_message_flow.py
|
||||
├── unit_tests/ # Unit tests
|
||||
@@ -303,6 +318,44 @@ These tests:
|
||||
- Test prevent_default, exception handling, and full message flow
|
||||
- Do not require real LLM provider keys
|
||||
|
||||
### Running backend E2E startup tests
|
||||
|
||||
Backend E2E tests start a real LangBot process with a generated minimal
|
||||
`data/config.yaml`, SQLite database, local storage, and embedded Chroma path.
|
||||
They do not require provider keys or external services.
|
||||
|
||||
```bash
|
||||
uv run --python 3.12 pytest tests/e2e -q --tb=short
|
||||
```
|
||||
|
||||
These tests verify startup orchestration, migrations, API route registration,
|
||||
and the minimal no-LLM startup path. The E2E process manager disables ambient
|
||||
proxy variables for subprocess startup and uses direct localhost HTTP clients,
|
||||
so local proxy settings should not affect the health checks.
|
||||
|
||||
### Running Box integration tests
|
||||
|
||||
Box integration tests exercise the real sandbox runtime path, including command
|
||||
execution, session persistence, managed process WebSocket attachment, and
|
||||
cleanup behavior.
|
||||
|
||||
```bash
|
||||
uv run --python 3.12 pytest tests/integration_tests -q --tb=short
|
||||
```
|
||||
|
||||
These tests require a working Docker or Podman runtime. In CI, the dedicated
|
||||
Box integration job checks Docker availability before running the tests.
|
||||
|
||||
### Running frontend E2E tests
|
||||
|
||||
Frontend E2E tests live in `web/tests/e2e` and use Playwright. They start Vite
|
||||
and mock the LangBot backend and Space APIs, so no backend process is required.
|
||||
|
||||
```bash
|
||||
cd web
|
||||
pnpm test:e2e
|
||||
```
|
||||
|
||||
### Known Issues
|
||||
|
||||
Some tests may encounter circular import errors. This is a known issue with the current module structure. The test infrastructure is designed to work around this using lazy imports, but if you encounter issues:
|
||||
@@ -320,6 +373,9 @@ Tests are automatically run on:
|
||||
- Push to master/develop branches
|
||||
|
||||
The workflow runs tests on Python 3.11, 3.12, and 3.13 to ensure compatibility.
|
||||
Startup E2E and Box integration tests run as separate Python 3.12 jobs because
|
||||
they exercise process/container behavior instead of pure Python compatibility.
|
||||
Frontend Playwright smoke tests run in `.github/workflows/frontend-tests.yml`.
|
||||
|
||||
## Adding New Tests
|
||||
|
||||
@@ -406,4 +462,4 @@ Check that you're mocking at the right level and using `AsyncMock` for async fun
|
||||
- [ ] Add E2E tests
|
||||
- [ ] Add performance benchmarks
|
||||
- [ ] Add mutation testing for better coverage quality
|
||||
- [ ] Add property-based testing with Hypothesis
|
||||
- [ ] Add property-based testing with Hypothesis
|
||||
|
||||
@@ -92,11 +92,11 @@ def e2e_client(e2e_port, langbot_process):
|
||||
|
||||
base_url = f'http://127.0.0.1:{e2e_port}'
|
||||
|
||||
with httpx.Client(base_url=base_url, timeout=10.0) as client:
|
||||
with httpx.Client(base_url=base_url, timeout=10.0, trust_env=False) as client:
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def e2e_db_path(e2e_tmpdir):
|
||||
"""Path to SQLite database file."""
|
||||
return e2e_tmpdir / 'data' / 'langbot.db'
|
||||
return e2e_tmpdir / 'data' / 'langbot.db'
|
||||
|
||||
@@ -38,12 +38,13 @@ class TestStartupFlow:
|
||||
# System info should contain version info
|
||||
assert 'version' in data['data'] or 'edition' in data['data']
|
||||
|
||||
def test_database_initialized(self, e2e_db_path):
|
||||
def test_database_initialized(self, langbot_process, e2e_db_path):
|
||||
"""Verify SQLite database was created and initialized."""
|
||||
assert e2e_db_path.exists()
|
||||
|
||||
# Database should have some tables after migration
|
||||
import sqlite3
|
||||
|
||||
conn = sqlite3.connect(str(e2e_db_path))
|
||||
cursor = conn.cursor()
|
||||
|
||||
@@ -74,10 +75,13 @@ class TestStartupFlow:
|
||||
def test_auth_endpoint(self, e2e_client, e2e_tmpdir):
|
||||
"""Test auth endpoint."""
|
||||
# First startup may allow initial setup
|
||||
response = e2e_client.post('/api/v1/user/auth', json={
|
||||
'username': 'admin',
|
||||
'password': 'admin',
|
||||
})
|
||||
response = e2e_client.post(
|
||||
'/api/v1/user/auth',
|
||||
json={
|
||||
'user': 'admin',
|
||||
'password': 'admin',
|
||||
},
|
||||
)
|
||||
|
||||
# Response could be:
|
||||
# - 200 if auth succeeds
|
||||
@@ -94,9 +98,10 @@ class TestStartupStages:
|
||||
# If API responds on e2e_port, config was loaded
|
||||
assert e2e_client.get('/api/v1/system/info').status_code == 200
|
||||
|
||||
def test_migrations_applied(self, e2e_db_path):
|
||||
def test_migrations_applied(self, langbot_process, e2e_db_path):
|
||||
"""Verify database migrations were applied."""
|
||||
import sqlite3
|
||||
|
||||
conn = sqlite3.connect(str(e2e_db_path))
|
||||
cursor = conn.cursor()
|
||||
|
||||
|
||||
@@ -104,6 +104,17 @@ def create_minimal_config(tmpdir: Path, port: int = 15300) -> Path:
|
||||
'user': 'postgres',
|
||||
'password': 'postgres',
|
||||
},
|
||||
'valkey_search': {
|
||||
'host': 'localhost',
|
||||
'port': 6379,
|
||||
'db': 0,
|
||||
'password': '',
|
||||
'username': '',
|
||||
'tls': False,
|
||||
'index_algorithm': 'HNSW',
|
||||
'distance_metric': 'COSINE',
|
||||
'request_timeout': 5000,
|
||||
},
|
||||
},
|
||||
'storage': {
|
||||
'use': 'local',
|
||||
@@ -176,4 +187,4 @@ def create_test_directories(tmpdir: Path) -> dict[str, Path]:
|
||||
for path in directories.values():
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
return directories
|
||||
return directories
|
||||
|
||||
@@ -44,6 +44,17 @@ class LangBotProcess:
|
||||
# Prepare environment
|
||||
env = os.environ.copy()
|
||||
env['PYTHONPATH'] = str(self.project_root / 'src')
|
||||
for proxy_key in (
|
||||
'HTTP_PROXY',
|
||||
'HTTPS_PROXY',
|
||||
'ALL_PROXY',
|
||||
'http_proxy',
|
||||
'https_proxy',
|
||||
'all_proxy',
|
||||
):
|
||||
env.pop(proxy_key, None)
|
||||
env['NO_PROXY'] = '127.0.0.1,localhost'
|
||||
env['no_proxy'] = '127.0.0.1,localhost'
|
||||
|
||||
# Set API port via environment variable
|
||||
env['API__PORT'] = str(self.port)
|
||||
@@ -79,9 +90,11 @@ precision = 2
|
||||
f.write(coveragerc_content)
|
||||
|
||||
cmd = [
|
||||
'coverage', 'run',
|
||||
'coverage',
|
||||
'run',
|
||||
'--rcfile=' + str(coveragerc_path),
|
||||
'-m', 'langbot',
|
||||
'-m',
|
||||
'langbot',
|
||||
]
|
||||
else:
|
||||
cmd = ['uv', 'run', 'python', '-m', 'langbot']
|
||||
@@ -113,6 +126,8 @@ precision = 2
|
||||
r = httpx.get(
|
||||
f'http://127.0.0.1:{self.port}/api/v1/system/info',
|
||||
timeout=2.0,
|
||||
follow_redirects=False,
|
||||
trust_env=False,
|
||||
)
|
||||
if r.status_code == 200:
|
||||
logger.info(f'LangBot started successfully on port {self.port}')
|
||||
@@ -185,6 +200,8 @@ precision = 2
|
||||
r = httpx.get(
|
||||
f'http://127.0.0.1:{self.port}/api/v1/system/info',
|
||||
timeout=5.0,
|
||||
follow_redirects=False,
|
||||
trust_env=False,
|
||||
)
|
||||
return r.status_code == 200
|
||||
except Exception:
|
||||
@@ -201,4 +218,4 @@ def find_project_root() -> Path:
|
||||
return parent
|
||||
|
||||
# Fallback to LangBot-test-build directory
|
||||
return Path('/home/glwuy/langbot-app/LangBot-test-build')
|
||||
return Path('/home/glwuy/langbot-app/LangBot-test-build')
|
||||
|
||||
+36
-36
@@ -58,45 +58,45 @@ from tests.factories.platform import (
|
||||
|
||||
__all__ = [
|
||||
# App
|
||||
"FakeApp",
|
||||
"fake_app",
|
||||
'FakeApp',
|
||||
'fake_app',
|
||||
# Message chains
|
||||
"text_chain",
|
||||
"group_text_chain",
|
||||
"mention_chain",
|
||||
"image_chain",
|
||||
'text_chain',
|
||||
'group_text_chain',
|
||||
'mention_chain',
|
||||
'image_chain',
|
||||
# Message events
|
||||
"friend_message_event",
|
||||
"group_message_event",
|
||||
'friend_message_event',
|
||||
'group_message_event',
|
||||
# Mock adapters
|
||||
"mock_adapter",
|
||||
'mock_adapter',
|
||||
# Queries
|
||||
"text_query",
|
||||
"group_text_query",
|
||||
"private_text_query",
|
||||
"command_query",
|
||||
"mention_query",
|
||||
"empty_query",
|
||||
"image_query",
|
||||
"file_query",
|
||||
"unsupported_query",
|
||||
"voice_query",
|
||||
"at_all_query",
|
||||
"query_with_session",
|
||||
"query_with_config",
|
||||
'text_query',
|
||||
'group_text_query',
|
||||
'private_text_query',
|
||||
'command_query',
|
||||
'mention_query',
|
||||
'empty_query',
|
||||
'image_query',
|
||||
'file_query',
|
||||
'unsupported_query',
|
||||
'voice_query',
|
||||
'at_all_query',
|
||||
'query_with_session',
|
||||
'query_with_config',
|
||||
# Provider
|
||||
"FakeProvider",
|
||||
"fake_provider",
|
||||
"fake_provider_pong",
|
||||
"fake_provider_timeout",
|
||||
"fake_provider_auth_error",
|
||||
"fake_provider_rate_limit",
|
||||
"fake_provider_malformed",
|
||||
"fake_model",
|
||||
'FakeProvider',
|
||||
'fake_provider',
|
||||
'fake_provider_pong',
|
||||
'fake_provider_timeout',
|
||||
'fake_provider_auth_error',
|
||||
'fake_provider_rate_limit',
|
||||
'fake_provider_malformed',
|
||||
'fake_model',
|
||||
# Platform
|
||||
"FakePlatform",
|
||||
"fake_platform",
|
||||
"fake_platform_with_streaming",
|
||||
"fake_platform_with_failure",
|
||||
"mock_platform_adapter",
|
||||
]
|
||||
'FakePlatform',
|
||||
'fake_platform',
|
||||
'fake_platform_with_streaming',
|
||||
'fake_platform_with_failure',
|
||||
'mock_platform_adapter',
|
||||
]
|
||||
|
||||
+21
-5
@@ -15,7 +15,7 @@ class FakeApp:
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
command_prefix: list[str] = ["/", "!"],
|
||||
command_prefix: list[str] = ['/', '!'],
|
||||
command_enable: bool = True,
|
||||
pipeline_concurrency: int = 10,
|
||||
admins: list[str] | None = None,
|
||||
@@ -40,6 +40,8 @@ class FakeApp:
|
||||
self.telemetry = self._create_mock_telemetry()
|
||||
self.survey = None
|
||||
self.cmd_mgr = self._create_mock_cmd_mgr()
|
||||
self.skill_mgr = self._create_mock_skill_mgr()
|
||||
self.pipeline_service = self._create_mock_pipeline_service()
|
||||
|
||||
# Apply any extra attributes for specific test scenarios
|
||||
for name, value in extra_attrs.items():
|
||||
@@ -98,9 +100,9 @@ class FakeApp:
|
||||
):
|
||||
instance_config = Mock()
|
||||
instance_config.data = {
|
||||
"command": {"prefix": command_prefix, "enable": command_enable},
|
||||
"concurrency": {"pipeline": pipeline_concurrency},
|
||||
"admins": admins,
|
||||
'command': {'prefix': command_prefix, 'enable': command_enable},
|
||||
'concurrency': {'pipeline': pipeline_concurrency},
|
||||
'admins': admins,
|
||||
}
|
||||
return instance_config
|
||||
|
||||
@@ -119,6 +121,20 @@ class FakeApp:
|
||||
cmd_mgr.execute = AsyncMock()
|
||||
return cmd_mgr
|
||||
|
||||
def _create_mock_skill_mgr(self):
|
||||
"""Mock SkillManager that returns no skill index addition by default."""
|
||||
skill_mgr = Mock()
|
||||
skill_mgr.skills = {}
|
||||
skill_mgr.build_skill_aware_prompt_addition = Mock(return_value='')
|
||||
skill_mgr.get_skill_index = Mock(return_value=[])
|
||||
return skill_mgr
|
||||
|
||||
def _create_mock_pipeline_service(self):
|
||||
"""Mock PipelineService.get_pipeline returning empty extensions prefs."""
|
||||
pipeline_service = AsyncMock()
|
||||
pipeline_service.get_pipeline = AsyncMock(return_value={'extensions_preferences': {}})
|
||||
return pipeline_service
|
||||
|
||||
def capture_message(self, message):
|
||||
"""Capture an outbound message for test assertions."""
|
||||
self._outbound_messages.append(message)
|
||||
@@ -134,4 +150,4 @@ class FakeApp:
|
||||
|
||||
def fake_app(**kwargs) -> FakeApp:
|
||||
"""Create a FakeApp instance with optional overrides."""
|
||||
return FakeApp(**kwargs)
|
||||
return FakeApp(**kwargs)
|
||||
|
||||
+83
-77
@@ -30,32 +30,36 @@ def _next_query_id() -> int:
|
||||
# ============== Message Chain Factories ==============
|
||||
|
||||
|
||||
def text_chain(text: str = "hello") -> platform_message.MessageChain:
|
||||
def text_chain(text: str = 'hello') -> platform_message.MessageChain:
|
||||
"""Create a simple text message chain."""
|
||||
return platform_message.MessageChain([
|
||||
platform_message.Plain(text=text),
|
||||
])
|
||||
return platform_message.MessageChain(
|
||||
[
|
||||
platform_message.Plain(text=text),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def group_text_chain(text: str = "hello") -> platform_message.MessageChain:
|
||||
def group_text_chain(text: str = 'hello') -> platform_message.MessageChain:
|
||||
"""Create a group text message chain (same as text_chain, context provided by event)."""
|
||||
return text_chain(text)
|
||||
|
||||
|
||||
def mention_chain(
|
||||
text: str = "hello",
|
||||
text: str = 'hello',
|
||||
target: typing.Union[int, str] = 12345,
|
||||
) -> platform_message.MessageChain:
|
||||
"""Create a message chain with @mention."""
|
||||
return platform_message.MessageChain([
|
||||
platform_message.At(target=target),
|
||||
platform_message.Plain(text=f" {text}"),
|
||||
])
|
||||
return platform_message.MessageChain(
|
||||
[
|
||||
platform_message.At(target=target),
|
||||
platform_message.Plain(text=f' {text}'),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def image_chain(
|
||||
text: str = "",
|
||||
url: str = "https://example.com/image.png",
|
||||
text: str = '',
|
||||
url: str = 'https://example.com/image.png',
|
||||
) -> platform_message.MessageChain:
|
||||
"""Create a message chain with an image."""
|
||||
components = []
|
||||
@@ -66,13 +70,15 @@ def image_chain(
|
||||
|
||||
|
||||
def command_chain(
|
||||
command: str = "help",
|
||||
prefix: str = "/",
|
||||
command: str = 'help',
|
||||
prefix: str = '/',
|
||||
) -> platform_message.MessageChain:
|
||||
"""Create a command message chain."""
|
||||
return platform_message.MessageChain([
|
||||
platform_message.Plain(text=f"{prefix}{command}"),
|
||||
])
|
||||
return platform_message.MessageChain(
|
||||
[
|
||||
platform_message.Plain(text=f'{prefix}{command}'),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
# ============== Message Event Factories ==============
|
||||
@@ -81,7 +87,7 @@ def command_chain(
|
||||
def friend_message_event(
|
||||
message_chain: platform_message.MessageChain,
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
nickname: str = "TestUser",
|
||||
nickname: str = 'TestUser',
|
||||
) -> platform_events.FriendMessage:
|
||||
"""Create a friend (private) message event."""
|
||||
sender = platform_entities.Friend(
|
||||
@@ -90,7 +96,7 @@ def friend_message_event(
|
||||
remark=None,
|
||||
)
|
||||
return platform_events.FriendMessage(
|
||||
type="FriendMessage",
|
||||
type='FriendMessage',
|
||||
sender=sender,
|
||||
message_chain=message_chain,
|
||||
time=1609459200,
|
||||
@@ -100,9 +106,9 @@ def friend_message_event(
|
||||
def group_message_event(
|
||||
message_chain: platform_message.MessageChain,
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
sender_name: str = "TestUser",
|
||||
sender_name: str = 'TestUser',
|
||||
group_id: typing.Union[int, str] = 99999,
|
||||
group_name: str = "TestGroup",
|
||||
group_name: str = 'TestGroup',
|
||||
) -> platform_events.GroupMessage:
|
||||
"""Create a group message event."""
|
||||
group = platform_entities.Group(
|
||||
@@ -117,7 +123,7 @@ def group_message_event(
|
||||
group=group,
|
||||
)
|
||||
return platform_events.GroupMessage(
|
||||
type="GroupMessage",
|
||||
type='GroupMessage',
|
||||
sender=sender,
|
||||
message_chain=message_chain,
|
||||
time=1609459200,
|
||||
@@ -152,36 +158,36 @@ def _base_query(
|
||||
query_id = _next_query_id()
|
||||
|
||||
base_data = {
|
||||
"query_id": query_id,
|
||||
"launcher_type": launcher_type,
|
||||
"launcher_id": launcher_id,
|
||||
"sender_id": sender_id,
|
||||
"message_chain": message_chain,
|
||||
"message_event": message_event,
|
||||
"adapter": adapter,
|
||||
"pipeline_uuid": "test-pipeline-uuid",
|
||||
"bot_uuid": "test-bot-uuid",
|
||||
"pipeline_config": {
|
||||
"ai": {
|
||||
"runner": {"runner": "local-agent"},
|
||||
"local-agent": {
|
||||
"model": {"primary": "test-model-uuid", "fallbacks": []},
|
||||
"prompt": "test-prompt",
|
||||
'query_id': query_id,
|
||||
'launcher_type': launcher_type,
|
||||
'launcher_id': launcher_id,
|
||||
'sender_id': sender_id,
|
||||
'message_chain': message_chain,
|
||||
'message_event': message_event,
|
||||
'adapter': adapter,
|
||||
'pipeline_uuid': 'test-pipeline-uuid',
|
||||
'bot_uuid': 'test-bot-uuid',
|
||||
'pipeline_config': {
|
||||
'ai': {
|
||||
'runner': {'runner': 'local-agent'},
|
||||
'local-agent': {
|
||||
'model': {'primary': 'test-model-uuid', 'fallbacks': []},
|
||||
'prompt': 'test-prompt',
|
||||
},
|
||||
},
|
||||
"output": {"misc": {"at-sender": False, "quote-origin": False}},
|
||||
"trigger": {"misc": {"combine-quote-message": False}},
|
||||
'output': {'misc': {'at-sender': False, 'quote-origin': False}},
|
||||
'trigger': {'misc': {'combine-quote-message': False}},
|
||||
},
|
||||
"session": None,
|
||||
"prompt": None,
|
||||
"messages": [],
|
||||
"user_message": None,
|
||||
"use_funcs": [],
|
||||
"use_llm_model_uuid": None,
|
||||
"variables": {},
|
||||
"resp_messages": [],
|
||||
"resp_message_chain": None,
|
||||
"current_stage_name": None,
|
||||
'session': None,
|
||||
'prompt': None,
|
||||
'messages': [],
|
||||
'user_message': None,
|
||||
'use_funcs': [],
|
||||
'use_llm_model_uuid': None,
|
||||
'variables': {},
|
||||
'resp_messages': [],
|
||||
'resp_message_chain': None,
|
||||
'current_stage_name': None,
|
||||
}
|
||||
|
||||
# Apply overrides
|
||||
@@ -192,7 +198,7 @@ def _base_query(
|
||||
|
||||
|
||||
def text_query(
|
||||
text: str = "hello",
|
||||
text: str = 'hello',
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
**overrides,
|
||||
) -> pipeline_query.Query:
|
||||
@@ -212,7 +218,7 @@ def text_query(
|
||||
|
||||
|
||||
def private_text_query(
|
||||
text: str = "hello",
|
||||
text: str = 'hello',
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
**overrides,
|
||||
) -> pipeline_query.Query:
|
||||
@@ -221,7 +227,7 @@ def private_text_query(
|
||||
|
||||
|
||||
def group_text_query(
|
||||
text: str = "hello",
|
||||
text: str = 'hello',
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
group_id: typing.Union[int, str] = 99999,
|
||||
**overrides,
|
||||
@@ -242,8 +248,8 @@ def group_text_query(
|
||||
|
||||
|
||||
def command_query(
|
||||
command: str = "help",
|
||||
prefix: str = "/",
|
||||
command: str = 'help',
|
||||
prefix: str = '/',
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
**overrides,
|
||||
) -> pipeline_query.Query:
|
||||
@@ -263,7 +269,7 @@ def command_query(
|
||||
|
||||
|
||||
def mention_query(
|
||||
text: str = "hello",
|
||||
text: str = 'hello',
|
||||
target: typing.Union[int, str] = 12345,
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
group_id: typing.Union[int, str] = 99999,
|
||||
@@ -301,8 +307,8 @@ def empty_query(**overrides) -> pipeline_query.Query:
|
||||
|
||||
|
||||
def image_query(
|
||||
text: str = "",
|
||||
url: str = "https://example.com/image.png",
|
||||
text: str = '',
|
||||
url: str = 'https://example.com/image.png',
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
**overrides,
|
||||
) -> pipeline_query.Query:
|
||||
@@ -322,9 +328,9 @@ def image_query(
|
||||
|
||||
|
||||
def file_query(
|
||||
url: str = "https://example.com/document.pdf",
|
||||
name: str = "document.pdf",
|
||||
text: str = "",
|
||||
url: str = 'https://example.com/document.pdf',
|
||||
name: str = 'document.pdf',
|
||||
text: str = '',
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
**overrides,
|
||||
) -> pipeline_query.Query:
|
||||
@@ -348,8 +354,8 @@ def file_query(
|
||||
|
||||
|
||||
def unsupported_query(
|
||||
unsupported_type: str = "CustomComponent",
|
||||
text: str = "",
|
||||
unsupported_type: str = 'CustomComponent',
|
||||
text: str = '',
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
**overrides,
|
||||
) -> pipeline_query.Query:
|
||||
@@ -358,7 +364,7 @@ def unsupported_query(
|
||||
if text:
|
||||
components.append(platform_message.Plain(text=text))
|
||||
# Use Unknown component for unsupported types
|
||||
components.append(platform_message.Unknown(text=f"Unsupported: {unsupported_type}"))
|
||||
components.append(platform_message.Unknown(text=f'Unsupported: {unsupported_type}'))
|
||||
chain = platform_message.MessageChain(components)
|
||||
event = friend_message_event(chain, sender_id)
|
||||
adapter = mock_adapter()
|
||||
@@ -374,7 +380,7 @@ def unsupported_query(
|
||||
|
||||
|
||||
def query_with_session(
|
||||
text: str = "hello",
|
||||
text: str = 'hello',
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
session: provider_session.Session = None,
|
||||
**overrides,
|
||||
@@ -389,7 +395,7 @@ def query_with_session(
|
||||
launcher_type=provider_session.LauncherTypes.PERSON,
|
||||
launcher_id=sender_id,
|
||||
sender_id=sender_id,
|
||||
use_prompt_name="default",
|
||||
use_prompt_name='default',
|
||||
using_conversation=None,
|
||||
conversations=[],
|
||||
)
|
||||
@@ -398,7 +404,7 @@ def query_with_session(
|
||||
|
||||
|
||||
def query_with_config(
|
||||
text: str = "hello",
|
||||
text: str = 'hello',
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
pipeline_config: dict = None,
|
||||
**overrides,
|
||||
@@ -410,22 +416,22 @@ def query_with_config(
|
||||
"""
|
||||
if pipeline_config is None:
|
||||
pipeline_config = {
|
||||
"ai": {
|
||||
"runner": {"runner": "local-agent"},
|
||||
"local-agent": {
|
||||
"model": {"primary": "test-model-uuid", "fallbacks": []},
|
||||
"prompt": "test-prompt",
|
||||
'ai': {
|
||||
'runner': {'runner': 'local-agent'},
|
||||
'local-agent': {
|
||||
'model': {'primary': 'test-model-uuid', 'fallbacks': []},
|
||||
'prompt': 'test-prompt',
|
||||
},
|
||||
},
|
||||
"output": {"misc": {"at-sender": False, "quote-origin": False}},
|
||||
"trigger": {"misc": {"combine-quote-message": False}},
|
||||
'output': {'misc': {'at-sender': False, 'quote-origin': False}},
|
||||
'trigger': {'misc': {'combine-quote-message': False}},
|
||||
}
|
||||
|
||||
return text_query(text, sender_id, pipeline_config=pipeline_config, **overrides)
|
||||
|
||||
|
||||
def voice_query(
|
||||
url: str = "https://example.com/audio.mp3",
|
||||
url: str = 'https://example.com/audio.mp3',
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
**overrides,
|
||||
) -> pipeline_query.Query:
|
||||
@@ -448,7 +454,7 @@ def voice_query(
|
||||
|
||||
|
||||
def at_all_query(
|
||||
text: str = "hello",
|
||||
text: str = 'hello',
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
group_id: typing.Union[int, str] = 99999,
|
||||
**overrides,
|
||||
@@ -456,7 +462,7 @@ def at_all_query(
|
||||
"""Create a group query with @All mention."""
|
||||
components = [
|
||||
platform_message.AtAll(),
|
||||
platform_message.Plain(text=f" {text}"),
|
||||
platform_message.Plain(text=f' {text}'),
|
||||
]
|
||||
chain = platform_message.MessageChain(components)
|
||||
event = group_message_event(chain, sender_id, group_id=group_id)
|
||||
@@ -469,4 +475,4 @@ def at_all_query(
|
||||
sender_id=sender_id,
|
||||
adapter=adapter,
|
||||
**overrides,
|
||||
)
|
||||
)
|
||||
|
||||
+52
-46
@@ -33,7 +33,7 @@ class FakePlatform:
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
bot_account_id: str = "test-bot",
|
||||
bot_account_id: str = 'test-bot',
|
||||
stream_output_supported: bool = False,
|
||||
raise_error: Exception = None,
|
||||
):
|
||||
@@ -48,16 +48,16 @@ class FakePlatform:
|
||||
# Registered listeners
|
||||
self._listeners: dict = {}
|
||||
|
||||
def raises(self, error: Exception) -> "FakePlatform":
|
||||
def raises(self, error: Exception) -> 'FakePlatform':
|
||||
"""Configure platform to raise an error on send."""
|
||||
self._raise_error = error
|
||||
return self
|
||||
|
||||
def send_failure(self) -> "FakePlatform":
|
||||
def send_failure(self) -> 'FakePlatform':
|
||||
"""Configure platform to simulate send failure."""
|
||||
return self.raises(Exception("Platform send failure"))
|
||||
return self.raises(Exception('Platform send failure'))
|
||||
|
||||
def supports_streaming(self, supported: bool = True) -> "FakePlatform":
|
||||
def supports_streaming(self, supported: bool = True) -> 'FakePlatform':
|
||||
"""Configure whether streaming output is supported."""
|
||||
self._stream_output_supported = supported
|
||||
return self
|
||||
@@ -89,7 +89,7 @@ class FakePlatform:
|
||||
self,
|
||||
text: str,
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
nickname: str = "TestUser",
|
||||
nickname: str = 'TestUser',
|
||||
) -> platform_events.FriendMessage:
|
||||
"""Create an inbound friend (private) message event."""
|
||||
sender = platform_entities.Friend(
|
||||
@@ -97,11 +97,13 @@ class FakePlatform:
|
||||
nickname=nickname,
|
||||
remark=None,
|
||||
)
|
||||
chain = platform_message.MessageChain([
|
||||
platform_message.Plain(text=text),
|
||||
])
|
||||
chain = platform_message.MessageChain(
|
||||
[
|
||||
platform_message.Plain(text=text),
|
||||
]
|
||||
)
|
||||
return platform_events.FriendMessage(
|
||||
type="FriendMessage",
|
||||
type='FriendMessage',
|
||||
sender=sender,
|
||||
message_chain=chain,
|
||||
time=1609459200,
|
||||
@@ -111,9 +113,9 @@ class FakePlatform:
|
||||
self,
|
||||
text: str,
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
sender_name: str = "TestUser",
|
||||
sender_name: str = 'TestUser',
|
||||
group_id: typing.Union[int, str] = 99999,
|
||||
group_name: str = "TestGroup",
|
||||
group_name: str = 'TestGroup',
|
||||
mention_bot: bool = False,
|
||||
) -> platform_events.GroupMessage:
|
||||
"""Create an inbound group message event.
|
||||
@@ -142,12 +144,12 @@ class FakePlatform:
|
||||
components = []
|
||||
if mention_bot:
|
||||
components.append(platform_message.At(target=self.bot_account_id))
|
||||
components.append(platform_message.Plain(text=" "))
|
||||
components.append(platform_message.Plain(text=' '))
|
||||
components.append(platform_message.Plain(text=text))
|
||||
|
||||
chain = platform_message.MessageChain(components)
|
||||
return platform_events.GroupMessage(
|
||||
type="GroupMessage",
|
||||
type='GroupMessage',
|
||||
sender=sender,
|
||||
message_chain=chain,
|
||||
time=1609459200,
|
||||
@@ -155,8 +157,8 @@ class FakePlatform:
|
||||
|
||||
def create_image_message(
|
||||
self,
|
||||
url: str = "https://example.com/image.png",
|
||||
text: str = "",
|
||||
url: str = 'https://example.com/image.png',
|
||||
text: str = '',
|
||||
sender_id: typing.Union[int, str] = 12345,
|
||||
is_group: bool = False,
|
||||
group_id: typing.Union[int, str] = 99999,
|
||||
@@ -169,12 +171,12 @@ class FakePlatform:
|
||||
chain = platform_message.MessageChain(components)
|
||||
|
||||
if is_group:
|
||||
return self.create_group_message("", sender_id, group_id=group_id)
|
||||
return self.create_group_message('', sender_id, group_id=group_id)
|
||||
# Replace chain
|
||||
else:
|
||||
sender = platform_entities.Friend(id=sender_id, nickname="TestUser", remark=None)
|
||||
sender = platform_entities.Friend(id=sender_id, nickname='TestUser', remark=None)
|
||||
return platform_events.FriendMessage(
|
||||
type="FriendMessage",
|
||||
type='FriendMessage',
|
||||
sender=sender,
|
||||
message_chain=chain,
|
||||
time=1609459200,
|
||||
@@ -192,12 +194,14 @@ class FakePlatform:
|
||||
if self._raise_error:
|
||||
raise self._raise_error
|
||||
|
||||
self._outbound_messages.append({
|
||||
"type": "send",
|
||||
"target_type": target_type,
|
||||
"target_id": target_id,
|
||||
"message": message,
|
||||
})
|
||||
self._outbound_messages.append(
|
||||
{
|
||||
'type': 'send',
|
||||
'target_type': target_type,
|
||||
'target_id': target_id,
|
||||
'message': message,
|
||||
}
|
||||
)
|
||||
|
||||
async def reply_message(
|
||||
self,
|
||||
@@ -209,13 +213,15 @@ class FakePlatform:
|
||||
if self._raise_error:
|
||||
raise self._raise_error
|
||||
|
||||
self._outbound_messages.append({
|
||||
"type": "reply",
|
||||
"source_type": message_source.type,
|
||||
"source": message_source,
|
||||
"message": message,
|
||||
"quote_origin": quote_origin,
|
||||
})
|
||||
self._outbound_messages.append(
|
||||
{
|
||||
'type': 'reply',
|
||||
'source_type': message_source.type,
|
||||
'source': message_source,
|
||||
'message': message,
|
||||
'quote_origin': quote_origin,
|
||||
}
|
||||
)
|
||||
|
||||
async def reply_message_chunk(
|
||||
self,
|
||||
@@ -229,15 +235,17 @@ class FakePlatform:
|
||||
if self._raise_error:
|
||||
raise self._raise_error
|
||||
|
||||
self._outbound_chunks.append({
|
||||
"type": "reply_chunk",
|
||||
"source_type": message_source.type,
|
||||
"source": message_source,
|
||||
"bot_message": bot_message,
|
||||
"message": message,
|
||||
"quote_origin": quote_origin,
|
||||
"is_final": is_final,
|
||||
})
|
||||
self._outbound_chunks.append(
|
||||
{
|
||||
'type': 'reply_chunk',
|
||||
'source_type': message_source.type,
|
||||
'source': message_source,
|
||||
'bot_message': bot_message,
|
||||
'message': message,
|
||||
'quote_origin': quote_origin,
|
||||
'is_final': is_final,
|
||||
}
|
||||
)
|
||||
|
||||
async def is_stream_output_supported(self) -> bool:
|
||||
"""Return whether streaming output is supported."""
|
||||
@@ -295,7 +303,7 @@ class FakePlatform:
|
||||
|
||||
|
||||
def fake_platform(
|
||||
bot_account_id: str = "test-bot",
|
||||
bot_account_id: str = 'test-bot',
|
||||
stream_output_supported: bool = False,
|
||||
) -> FakePlatform:
|
||||
"""Create a FakePlatform instance."""
|
||||
@@ -328,9 +336,7 @@ def mock_platform_adapter(platform: FakePlatform = None) -> Mock:
|
||||
adapter.reply_message = AsyncMock(side_effect=platform.reply_message)
|
||||
adapter.reply_message_chunk = AsyncMock(side_effect=platform.reply_message_chunk)
|
||||
adapter.send_message = AsyncMock(side_effect=platform.send_message)
|
||||
adapter.is_stream_output_supported = AsyncMock(
|
||||
return_value=platform._stream_output_supported
|
||||
)
|
||||
adapter.is_stream_output_supported = AsyncMock(return_value=platform._stream_output_supported)
|
||||
adapter._fake_platform = platform # Store for assertions
|
||||
|
||||
return adapter
|
||||
return adapter
|
||||
|
||||
+42
-38
@@ -27,51 +27,51 @@ class FakeProvider:
|
||||
Does not require API keys.
|
||||
"""
|
||||
|
||||
PONG_RESPONSE = "LANGBOT_FAKE_PONG"
|
||||
PONG_RESPONSE = 'LANGBOT_FAKE_PONG'
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
default_response: str = "fake response",
|
||||
default_response: str = 'fake response',
|
||||
streaming_chunks: list[str] = None,
|
||||
raise_error: Exception = None,
|
||||
captured_requests: list = None,
|
||||
):
|
||||
self._default_response = default_response
|
||||
self._streaming_chunks = streaming_chunks or ["fake ", "response"]
|
||||
self._streaming_chunks = streaming_chunks or ['fake ', 'response']
|
||||
self._raise_error = raise_error
|
||||
self._captured_requests = captured_requests if captured_requests is not None else []
|
||||
|
||||
def returns(self, text: str) -> "FakeProvider":
|
||||
def returns(self, text: str) -> 'FakeProvider':
|
||||
"""Configure provider to return a specific text response."""
|
||||
self._default_response = text
|
||||
self._streaming_chunks = [text]
|
||||
return self
|
||||
|
||||
def returns_streaming(self, chunks: list[str]) -> "FakeProvider":
|
||||
def returns_streaming(self, chunks: list[str]) -> 'FakeProvider':
|
||||
"""Configure provider to return streaming chunks."""
|
||||
self._streaming_chunks = chunks
|
||||
self._default_response = "".join(chunks)
|
||||
self._default_response = ''.join(chunks)
|
||||
return self
|
||||
|
||||
def raises(self, error: Exception) -> "FakeProvider":
|
||||
def raises(self, error: Exception) -> 'FakeProvider':
|
||||
"""Configure provider to raise an error."""
|
||||
self._raise_error = error
|
||||
return self
|
||||
|
||||
def timeout(self) -> "FakeProvider":
|
||||
def timeout(self) -> 'FakeProvider':
|
||||
"""Configure provider to simulate timeout."""
|
||||
return self.raises(TimeoutError("Provider timeout"))
|
||||
return self.raises(TimeoutError('Provider timeout'))
|
||||
|
||||
def auth_error(self) -> "FakeProvider":
|
||||
def auth_error(self) -> 'FakeProvider':
|
||||
"""Configure provider to simulate auth error."""
|
||||
return self.raises(Exception("Invalid API key"))
|
||||
return self.raises(Exception('Invalid API key'))
|
||||
|
||||
def rate_limit(self) -> "FakeProvider":
|
||||
def rate_limit(self) -> 'FakeProvider':
|
||||
"""Configure provider to simulate rate limit."""
|
||||
return self.raises(Exception("Rate limit exceeded"))
|
||||
return self.raises(Exception('Rate limit exceeded'))
|
||||
|
||||
def malformed(self) -> "FakeProvider":
|
||||
def malformed(self) -> 'FakeProvider':
|
||||
"""Configure provider to simulate malformed response."""
|
||||
self._default_response = None
|
||||
return self
|
||||
@@ -87,7 +87,7 @@ class FakeProvider:
|
||||
def _create_message(self, content: str) -> provider_message.Message:
|
||||
"""Create a provider message from text content."""
|
||||
return provider_message.Message(
|
||||
role="assistant",
|
||||
role='assistant',
|
||||
content=content,
|
||||
)
|
||||
|
||||
@@ -99,7 +99,7 @@ class FakeProvider:
|
||||
) -> provider_message.MessageChunk:
|
||||
"""Create a provider message chunk."""
|
||||
return provider_message.MessageChunk(
|
||||
role="assistant",
|
||||
role='assistant',
|
||||
content=content,
|
||||
is_final=is_final,
|
||||
msg_sequence=msg_sequence,
|
||||
@@ -116,13 +116,15 @@ class FakeProvider:
|
||||
) -> provider_message.Message:
|
||||
"""Simulate non-streaming LLM invocation."""
|
||||
# Capture request for assertions
|
||||
self._captured_requests.append({
|
||||
"query_id": query.query_id if query else None,
|
||||
"model": model.model_entity.name if model and hasattr(model, 'model_entity') else None,
|
||||
"messages": messages,
|
||||
"funcs": funcs,
|
||||
"extra_args": extra_args,
|
||||
})
|
||||
self._captured_requests.append(
|
||||
{
|
||||
'query_id': query.query_id if query else None,
|
||||
'model': model.model_entity.name if model and hasattr(model, 'model_entity') else None,
|
||||
'messages': messages,
|
||||
'funcs': funcs,
|
||||
'extra_args': extra_args,
|
||||
}
|
||||
)
|
||||
|
||||
# Simulate error if configured
|
||||
if self._raise_error:
|
||||
@@ -131,7 +133,7 @@ class FakeProvider:
|
||||
# Return response
|
||||
if self._default_response is None:
|
||||
# Malformed response
|
||||
return provider_message.Message(role="assistant", content=None)
|
||||
return provider_message.Message(role='assistant', content=None)
|
||||
|
||||
return self._create_message(self._default_response)
|
||||
|
||||
@@ -146,14 +148,16 @@ class FakeProvider:
|
||||
) -> typing.AsyncGenerator[provider_message.MessageChunk, None]:
|
||||
"""Simulate streaming LLM invocation."""
|
||||
# Capture request for assertions
|
||||
self._captured_requests.append({
|
||||
"query_id": query.query_id if query else None,
|
||||
"model": model.model_entity.name if model and hasattr(model, 'model_entity') else None,
|
||||
"messages": messages,
|
||||
"funcs": funcs,
|
||||
"extra_args": extra_args,
|
||||
"streaming": True,
|
||||
})
|
||||
self._captured_requests.append(
|
||||
{
|
||||
'query_id': query.query_id if query else None,
|
||||
'model': model.model_entity.name if model and hasattr(model, 'model_entity') else None,
|
||||
'messages': messages,
|
||||
'funcs': funcs,
|
||||
'extra_args': extra_args,
|
||||
'streaming': True,
|
||||
}
|
||||
)
|
||||
|
||||
# Simulate error if configured
|
||||
if self._raise_error:
|
||||
@@ -161,12 +165,12 @@ class FakeProvider:
|
||||
|
||||
# Yield chunks
|
||||
for i, chunk in enumerate(self._streaming_chunks):
|
||||
is_final = (i == len(self._streaming_chunks) - 1)
|
||||
is_final = i == len(self._streaming_chunks) - 1
|
||||
yield self._create_chunk(chunk, is_final=is_final, msg_sequence=i)
|
||||
|
||||
|
||||
def fake_provider(
|
||||
default_response: str = "fake response",
|
||||
default_response: str = 'fake response',
|
||||
) -> FakeProvider:
|
||||
"""Create a FakeProvider with optional default response."""
|
||||
return FakeProvider(default_response=default_response)
|
||||
@@ -202,8 +206,8 @@ def fake_provider_malformed() -> FakeProvider:
|
||||
|
||||
def fake_model(
|
||||
*,
|
||||
uuid: str = "test-model-uuid",
|
||||
name: str = "test-model",
|
||||
uuid: str = 'test-model-uuid',
|
||||
name: str = 'test-model',
|
||||
abilities: list[str] = None,
|
||||
provider: FakeProvider = None,
|
||||
) -> Mock:
|
||||
@@ -212,7 +216,7 @@ def fake_model(
|
||||
model.model_entity = Mock()
|
||||
model.model_entity.uuid = uuid
|
||||
model.model_entity.name = name
|
||||
model.model_entity.abilities = abilities or ["func_call", "vision"]
|
||||
model.model_entity.abilities = abilities or ['func_call', 'vision']
|
||||
model.model_entity.extra_args = {}
|
||||
|
||||
# Attach fake provider
|
||||
@@ -221,4 +225,4 @@ def fake_model(
|
||||
|
||||
model.provider = provider
|
||||
|
||||
return model
|
||||
return model
|
||||
|
||||
@@ -3,4 +3,4 @@ Integration tests package.
|
||||
|
||||
These tests validate real system behavior with actual database/network resources.
|
||||
Run with: uv run pytest tests/integration/ -m "not slow" -q
|
||||
"""
|
||||
"""
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
API integration tests package.
|
||||
|
||||
Tests for HTTP API endpoints using Quart test client.
|
||||
"""
|
||||
"""
|
||||
|
||||
@@ -48,6 +48,7 @@ def mock_circular_import_chain():
|
||||
clear=clear,
|
||||
):
|
||||
import langbot.pkg.api.http.controller.groups.platform.bots as _bots # noqa: E402, F401
|
||||
|
||||
yield
|
||||
|
||||
|
||||
@@ -56,10 +57,12 @@ def fake_bot_app():
|
||||
"""Create FakeApp with bot services (module scope for reuse)."""
|
||||
app = FakeApp()
|
||||
|
||||
app.instance_config.data.update({
|
||||
'api': {'port': 5300},
|
||||
'system': {'allow_modify_login_info': True, 'limitation': {}},
|
||||
})
|
||||
app.instance_config.data.update(
|
||||
{
|
||||
'api': {'port': 5300},
|
||||
'system': {'allow_modify_login_info': True, 'limitation': {}},
|
||||
}
|
||||
)
|
||||
|
||||
# Auth services
|
||||
app.user_service = Mock()
|
||||
@@ -71,28 +74,29 @@ def fake_bot_app():
|
||||
|
||||
# Bot service
|
||||
app.bot_service = Mock()
|
||||
app.bot_service.get_bots = AsyncMock(return_value=[
|
||||
{
|
||||
app.bot_service.get_bots = AsyncMock(
|
||||
return_value=[
|
||||
{
|
||||
'uuid': 'test-bot-uuid',
|
||||
'name': 'Test Bot',
|
||||
'platform': 'telegram',
|
||||
'pipeline_uuid': 'test-pipeline-uuid',
|
||||
}
|
||||
]
|
||||
)
|
||||
app.bot_service.get_runtime_bot_info = AsyncMock(
|
||||
return_value={
|
||||
'uuid': 'test-bot-uuid',
|
||||
'name': 'Test Bot',
|
||||
'platform': 'telegram',
|
||||
'pipeline_uuid': 'test-pipeline-uuid',
|
||||
'webhook_url': 'https://example.com/webhook/test-bot-uuid',
|
||||
}
|
||||
])
|
||||
app.bot_service.get_runtime_bot_info = AsyncMock(return_value={
|
||||
'uuid': 'test-bot-uuid',
|
||||
'name': 'Test Bot',
|
||||
'platform': 'telegram',
|
||||
'pipeline_uuid': 'test-pipeline-uuid',
|
||||
'webhook_url': 'https://example.com/webhook/test-bot-uuid',
|
||||
})
|
||||
)
|
||||
app.bot_service.create_bot = AsyncMock(return_value={'uuid': 'new-bot-uuid'})
|
||||
app.bot_service.update_bot = AsyncMock(return_value={})
|
||||
app.bot_service.delete_bot = AsyncMock()
|
||||
app.bot_service.list_event_logs = AsyncMock(return_value=(
|
||||
[{'uuid': 'log-1', 'message': 'test log'}],
|
||||
1
|
||||
))
|
||||
app.bot_service.list_event_logs = AsyncMock(return_value=([{'uuid': 'log-1', 'message': 'test log'}], 1))
|
||||
app.bot_service.send_message = AsyncMock()
|
||||
|
||||
# Platform manager
|
||||
@@ -118,10 +122,7 @@ class TestBotEndpoints:
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_bots_success(self, quart_test_client):
|
||||
"""GET /api/v1/platform/bots returns bot list."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/platform/bots',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
response = await quart_test_client.get('/api/v1/platform/bots', headers={'Authorization': 'Bearer test_token'})
|
||||
|
||||
assert response.status_code == 200
|
||||
data = await response.get_json()
|
||||
@@ -135,7 +136,7 @@ class TestBotEndpoints:
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/platform/bots',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={'name': 'New Bot', 'platform': 'telegram', 'pipeline_uuid': 'test-pipeline'}
|
||||
json={'name': 'New Bot', 'platform': 'telegram', 'pipeline_uuid': 'test-pipeline'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -147,8 +148,7 @@ class TestBotEndpoints:
|
||||
async def test_get_single_bot_success(self, quart_test_client):
|
||||
"""GET /api/v1/platform/bots/{uuid} returns bot with runtime info."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/platform/bots/test-bot-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/platform/bots/test-bot-uuid', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -162,7 +162,7 @@ class TestBotEndpoints:
|
||||
response = await quart_test_client.put(
|
||||
'/api/v1/platform/bots/test-bot-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={'name': 'Updated Bot'}
|
||||
json={'name': 'Updated Bot'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -173,8 +173,7 @@ class TestBotEndpoints:
|
||||
async def test_delete_bot_success(self, quart_test_client):
|
||||
"""DELETE /api/v1/platform/bots/{uuid} deletes bot."""
|
||||
response = await quart_test_client.delete(
|
||||
'/api/v1/platform/bots/test-bot-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/platform/bots/test-bot-uuid', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -190,7 +189,7 @@ class TestBotLogsEndpoint:
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/platform/bots/test-bot-uuid/logs',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={'from_index': -1, 'max_count': 10}
|
||||
json={'from_index': -1, 'max_count': 10},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -213,8 +212,8 @@ class TestBotSendMessageEndpoint:
|
||||
json={
|
||||
'target_type': 'person',
|
||||
'target_id': 'user123',
|
||||
'message_chain': [{'type': 'text', 'text': 'Hello'}]
|
||||
}
|
||||
'message_chain': [{'type': 'text', 'text': 'Hello'}],
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -228,7 +227,7 @@ class TestBotSendMessageEndpoint:
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/platform/bots/test-bot-uuid/send_message',
|
||||
headers={'Authorization': 'Bearer test_api_key'},
|
||||
json={'target_id': 'user123', 'message_chain': [{'type': 'text', 'text': 'Hello'}]}
|
||||
json={'target_id': 'user123', 'message_chain': [{'type': 'text', 'text': 'Hello'}]},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
@@ -244,8 +243,8 @@ class TestBotSendMessageEndpoint:
|
||||
json={
|
||||
'target_type': 'invalid',
|
||||
'target_id': 'user123',
|
||||
'message_chain': [{'type': 'text', 'text': 'Hello'}]
|
||||
}
|
||||
'message_chain': [{'type': 'text', 'text': 'Hello'}],
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
@@ -47,6 +47,7 @@ def mock_circular_import_chain():
|
||||
clear=clear,
|
||||
):
|
||||
import langbot.pkg.api.http.controller.groups.pipelines.embed as _embed # noqa: E402, F401
|
||||
|
||||
yield
|
||||
|
||||
|
||||
@@ -55,10 +56,12 @@ def fake_embed_app():
|
||||
"""Create FakeApp with embed widget services (module scope)."""
|
||||
app = FakeApp()
|
||||
|
||||
app.instance_config.data.update({
|
||||
'api': {'port': 5300},
|
||||
'system': {'allow_modify_login_info': True, 'limitation': {}},
|
||||
})
|
||||
app.instance_config.data.update(
|
||||
{
|
||||
'api': {'port': 5300},
|
||||
'system': {'allow_modify_login_info': True, 'limitation': {}},
|
||||
}
|
||||
)
|
||||
|
||||
# Create mock web_page_bot with valid UUID format
|
||||
mock_bot_entity = Mock()
|
||||
@@ -83,9 +86,7 @@ def fake_embed_app():
|
||||
|
||||
# WebSocket proxy bot with adapter
|
||||
mock_websocket_adapter = Mock()
|
||||
mock_websocket_adapter.get_websocket_messages = Mock(return_value=[
|
||||
{'id': 'msg-1', 'content': 'test message'}
|
||||
])
|
||||
mock_websocket_adapter.get_websocket_messages = Mock(return_value=[{'id': 'msg-1', 'content': 'test message'}])
|
||||
mock_websocket_adapter.reset_session = Mock()
|
||||
mock_websocket_adapter.handle_websocket_message = AsyncMock()
|
||||
|
||||
@@ -117,9 +118,7 @@ class TestEmbedWidgetEndpoint:
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_widget_js_success(self, quart_test_client):
|
||||
"""GET /api/v1/embed/{bot_uuid}/widget.js returns JS."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/embed/a1b2c3d4-5678-90ab-cdef-123456789abc/widget.js'
|
||||
)
|
||||
response = await quart_test_client.get('/api/v1/embed/a1b2c3d4-5678-90ab-cdef-123456789abc/widget.js')
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'javascript' in response.content_type
|
||||
@@ -127,18 +126,14 @@ class TestEmbedWidgetEndpoint:
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_widget_js_invalid_uuid(self, quart_test_client):
|
||||
"""GET widget.js with invalid UUID returns 400."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/embed/invalid-uuid/widget.js'
|
||||
)
|
||||
response = await quart_test_client.get('/api/v1/embed/invalid-uuid/widget.js')
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_widget_js_bot_not_found(self, quart_test_client):
|
||||
"""GET widget.js for non-existent bot returns 404."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/embed/00000000-0000-0000-0000-000000000000/widget.js'
|
||||
)
|
||||
response = await quart_test_client.get('/api/v1/embed/00000000-0000-0000-0000-000000000000/widget.js')
|
||||
|
||||
assert response.status_code == 404
|
||||
|
||||
@@ -164,8 +159,7 @@ class TestEmbedTurnstileVerifyEndpoint:
|
||||
async def test_turnstile_verify_no_secret(self, quart_test_client):
|
||||
"""POST turnstile verify without secret returns dummy token."""
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/embed/a1b2c3d4-5678-90ab-cdef-123456789abc/turnstile/verify',
|
||||
json={'token': 'test-token'}
|
||||
'/api/v1/embed/a1b2c3d4-5678-90ab-cdef-123456789abc/turnstile/verify', json={'token': 'test-token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -177,8 +171,7 @@ class TestEmbedTurnstileVerifyEndpoint:
|
||||
async def test_turnstile_verify_invalid_uuid(self, quart_test_client):
|
||||
"""POST turnstile verify with invalid UUID returns 400."""
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/embed/invalid-uuid/turnstile/verify',
|
||||
json={'token': 'test-token'}
|
||||
'/api/v1/embed/invalid-uuid/turnstile/verify', json={'token': 'test-token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
@@ -187,8 +180,7 @@ class TestEmbedTurnstileVerifyEndpoint:
|
||||
async def test_turnstile_verify_missing_token(self, quart_test_client):
|
||||
"""POST turnstile verify without token returns 400."""
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/embed/a1b2c3d4-5678-90ab-cdef-123456789abc/turnstile/verify',
|
||||
json={}
|
||||
'/api/v1/embed/a1b2c3d4-5678-90ab-cdef-123456789abc/turnstile/verify', json={}
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
@@ -203,7 +195,7 @@ class TestEmbedMessagesEndpoint:
|
||||
"""GET messages/person returns messages."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/embed/a1b2c3d4-5678-90ab-cdef-123456789abc/messages/person',
|
||||
headers={'Authorization': 'Bearer 1234567890.dummy'}
|
||||
headers={'Authorization': 'Bearer 1234567890.dummy'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -216,7 +208,7 @@ class TestEmbedMessagesEndpoint:
|
||||
"""GET messages/group returns messages."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/embed/a1b2c3d4-5678-90ab-cdef-123456789abc/messages/group',
|
||||
headers={'Authorization': 'Bearer 1234567890.dummy'}
|
||||
headers={'Authorization': 'Bearer 1234567890.dummy'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -226,7 +218,7 @@ class TestEmbedMessagesEndpoint:
|
||||
"""GET messages with invalid session_type returns 400."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/embed/a1b2c3d4-5678-90ab-cdef-123456789abc/messages/invalid',
|
||||
headers={'Authorization': 'Bearer 1234567890.dummy'}
|
||||
headers={'Authorization': 'Bearer 1234567890.dummy'},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
@@ -241,7 +233,7 @@ class TestEmbedResetEndpoint:
|
||||
"""POST reset/person resets session."""
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/embed/a1b2c3d4-5678-90ab-cdef-123456789abc/reset/person',
|
||||
headers={'Authorization': 'Bearer 1234567890.dummy'}
|
||||
headers={'Authorization': 'Bearer 1234567890.dummy'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -252,8 +244,7 @@ class TestEmbedResetEndpoint:
|
||||
async def test_reset_session_invalid_uuid(self, quart_test_client):
|
||||
"""POST reset with invalid UUID returns 400."""
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/embed/invalid-uuid/reset/person',
|
||||
headers={'Authorization': 'Bearer 1234567890.dummy'}
|
||||
'/api/v1/embed/invalid-uuid/reset/person', headers={'Authorization': 'Bearer 1234567890.dummy'}
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
@@ -269,7 +260,7 @@ class TestEmbedFeedbackEndpoint:
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/embed/a1b2c3d4-5678-90ab-cdef-123456789abc/feedback',
|
||||
headers={'Authorization': 'Bearer 1234567890.dummy'},
|
||||
json={'message_id': 'msg-123', 'feedback_type': 1}
|
||||
json={'message_id': 'msg-123', 'feedback_type': 1},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -283,7 +274,7 @@ class TestEmbedFeedbackEndpoint:
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/embed/a1b2c3d4-5678-90ab-cdef-123456789abc/feedback',
|
||||
headers={'Authorization': 'Bearer 1234567890.dummy'},
|
||||
json={'message_id': 'msg-123', 'feedback_type': 2}
|
||||
json={'message_id': 'msg-123', 'feedback_type': 2},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -294,7 +285,7 @@ class TestEmbedFeedbackEndpoint:
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/embed/a1b2c3d4-5678-90ab-cdef-123456789abc/feedback',
|
||||
headers={'Authorization': 'Bearer 1234567890.dummy'},
|
||||
json={'message_id': 'msg-123', 'feedback_type': 99}
|
||||
json={'message_id': 'msg-123', 'feedback_type': 99},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
@@ -49,6 +49,7 @@ def mock_circular_import_chain():
|
||||
clear=clear,
|
||||
):
|
||||
import langbot.pkg.api.http.controller.groups.knowledge.base as _knowledge # noqa: E402, F401
|
||||
|
||||
yield
|
||||
|
||||
|
||||
@@ -57,10 +58,12 @@ def fake_knowledge_app():
|
||||
"""Create FakeApp with knowledge services (module scope for reuse)."""
|
||||
app = FakeApp()
|
||||
|
||||
app.instance_config.data.update({
|
||||
'api': {'port': 5300},
|
||||
'system': {'allow_modify_login_info': True, 'limitation': {}},
|
||||
})
|
||||
app.instance_config.data.update(
|
||||
{
|
||||
'api': {'port': 5300},
|
||||
'system': {'allow_modify_login_info': True, 'limitation': {}},
|
||||
}
|
||||
)
|
||||
|
||||
# Auth services
|
||||
app.user_service = Mock()
|
||||
@@ -72,33 +75,35 @@ def fake_knowledge_app():
|
||||
|
||||
# Knowledge service
|
||||
app.knowledge_service = Mock()
|
||||
app.knowledge_service.get_knowledge_bases = AsyncMock(return_value=[
|
||||
{
|
||||
app.knowledge_service.get_knowledge_bases = AsyncMock(
|
||||
return_value=[
|
||||
{
|
||||
'uuid': 'test-kb-uuid',
|
||||
'name': 'Test Knowledge Base',
|
||||
'description': 'Test KB description',
|
||||
'engine_plugin_id': 'test/engine',
|
||||
'created_at': '2024-01-01T00:00:00',
|
||||
'updated_at': '2024-01-01T00:00:00',
|
||||
}
|
||||
]
|
||||
)
|
||||
app.knowledge_service.get_knowledge_base = AsyncMock(
|
||||
return_value={
|
||||
'uuid': 'test-kb-uuid',
|
||||
'name': 'Test Knowledge Base',
|
||||
'description': 'Test KB description',
|
||||
'engine_plugin_id': 'test/engine',
|
||||
'created_at': '2024-01-01T00:00:00',
|
||||
'updated_at': '2024-01-01T00:00:00',
|
||||
}
|
||||
])
|
||||
app.knowledge_service.get_knowledge_base = AsyncMock(return_value={
|
||||
'uuid': 'test-kb-uuid',
|
||||
'name': 'Test Knowledge Base',
|
||||
'description': 'Test KB description',
|
||||
'engine_plugin_id': 'test/engine',
|
||||
})
|
||||
)
|
||||
app.knowledge_service.create_knowledge_base = AsyncMock(return_value={'uuid': 'new-kb-uuid'})
|
||||
app.knowledge_service.update_knowledge_base = AsyncMock(return_value={})
|
||||
app.knowledge_service.delete_knowledge_base = AsyncMock()
|
||||
app.knowledge_service.get_files_by_knowledge_base = AsyncMock(return_value=[
|
||||
{'uuid': 'test-file-uuid', 'filename': 'test.pdf'}
|
||||
])
|
||||
app.knowledge_service.get_files_by_knowledge_base = AsyncMock(
|
||||
return_value=[{'uuid': 'test-file-uuid', 'filename': 'test.pdf'}]
|
||||
)
|
||||
app.knowledge_service.store_file = AsyncMock(return_value={'task_id': 'test-task-id'})
|
||||
app.knowledge_service.delete_file = AsyncMock()
|
||||
app.knowledge_service.retrieve_knowledge_base = AsyncMock(return_value=[
|
||||
{'content': 'test result', 'score': 0.95}
|
||||
])
|
||||
app.knowledge_service.retrieve_knowledge_base = AsyncMock(return_value=[{'content': 'test result', 'score': 0.95}])
|
||||
|
||||
# RAG manager
|
||||
app.rag_mgr = Mock()
|
||||
@@ -124,8 +129,7 @@ class TestKnowledgeBaseEndpoints:
|
||||
async def test_get_knowledge_bases_success(self, quart_test_client):
|
||||
"""GET /api/v1/knowledge/bases returns knowledge base list."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/knowledge/bases',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/knowledge/bases', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -140,7 +144,7 @@ class TestKnowledgeBaseEndpoints:
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/knowledge/bases',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={'name': 'New KB', 'engine_plugin_id': 'test/engine'}
|
||||
json={'name': 'New KB', 'engine_plugin_id': 'test/engine'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -152,8 +156,7 @@ class TestKnowledgeBaseEndpoints:
|
||||
async def test_get_single_knowledge_base_success(self, quart_test_client):
|
||||
"""GET /api/v1/knowledge/bases/{uuid} returns knowledge base."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/knowledge/bases/test-kb-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/knowledge/bases/test-kb-uuid', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -167,7 +170,7 @@ class TestKnowledgeBaseEndpoints:
|
||||
response = await quart_test_client.put(
|
||||
'/api/v1/knowledge/bases/test-kb-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={'name': 'Updated KB'}
|
||||
json={'name': 'Updated KB'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -178,8 +181,7 @@ class TestKnowledgeBaseEndpoints:
|
||||
async def test_delete_knowledge_base_success(self, quart_test_client):
|
||||
"""DELETE /api/v1/knowledge/bases/{uuid} deletes knowledge base."""
|
||||
response = await quart_test_client.delete(
|
||||
'/api/v1/knowledge/bases/test-kb-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/knowledge/bases/test-kb-uuid', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -193,8 +195,7 @@ class TestKnowledgeBaseFilesEndpoints:
|
||||
async def test_get_files_success(self, quart_test_client):
|
||||
"""GET /api/v1/knowledge/bases/{uuid}/files returns files."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/knowledge/bases/test-kb-uuid/files',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/knowledge/bases/test-kb-uuid/files', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -208,7 +209,7 @@ class TestKnowledgeBaseFilesEndpoints:
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/knowledge/bases/test-kb-uuid/files',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={'file_id': 'test-file-id', 'parser_plugin_id': 'test/parser'}
|
||||
json={'file_id': 'test-file-id', 'parser_plugin_id': 'test/parser'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -220,8 +221,7 @@ class TestKnowledgeBaseFilesEndpoints:
|
||||
async def test_delete_file_from_knowledge_base(self, quart_test_client):
|
||||
"""DELETE /api/v1/knowledge/bases/{uuid}/files/{file_id}."""
|
||||
response = await quart_test_client.delete(
|
||||
'/api/v1/knowledge/bases/test-kb-uuid/files/test-file-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/knowledge/bases/test-kb-uuid/files/test-file-uuid', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -237,7 +237,7 @@ class TestKnowledgeBaseRetrieveEndpoint:
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/knowledge/bases/test-kb-uuid/retrieve',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={'query': 'test query', 'retrieval_settings': {'top_k': 5}}
|
||||
json={'query': 'test query', 'retrieval_settings': {'top_k': 5}},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -249,9 +249,7 @@ class TestKnowledgeBaseRetrieveEndpoint:
|
||||
async def test_retrieve_without_query_returns_error(self, quart_test_client):
|
||||
"""POST retrieve without query returns 400."""
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/knowledge/bases/test-kb-uuid/retrieve',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={}
|
||||
'/api/v1/knowledge/bases/test-kb-uuid/retrieve', headers={'Authorization': 'Bearer test_token'}, json={}
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
@@ -46,6 +46,7 @@ def mock_circular_import_chain():
|
||||
clear=clear,
|
||||
):
|
||||
import langbot.pkg.api.http.controller.groups.monitoring as _monitoring # noqa: E402, F401
|
||||
|
||||
yield
|
||||
|
||||
|
||||
@@ -54,10 +55,12 @@ def fake_monitoring_app():
|
||||
"""Create FakeApp with monitoring services (module scope)."""
|
||||
app = FakeApp()
|
||||
|
||||
app.instance_config.data.update({
|
||||
'api': {'port': 5300},
|
||||
'system': {'allow_modify_login_info': True, 'limitation': {}},
|
||||
})
|
||||
app.instance_config.data.update(
|
||||
{
|
||||
'api': {'port': 5300},
|
||||
'system': {'allow_modify_login_info': True, 'limitation': {}},
|
||||
}
|
||||
)
|
||||
|
||||
# Auth services - USER_TOKEN auth requires jwt verification AND get_user_by_email
|
||||
app.user_service = Mock()
|
||||
@@ -67,40 +70,35 @@ def fake_monitoring_app():
|
||||
|
||||
# Monitoring service
|
||||
app.monitoring_service = Mock()
|
||||
app.monitoring_service.get_overview_metrics = AsyncMock(return_value={
|
||||
'total_messages': 100,
|
||||
'total_llm_calls': 50,
|
||||
'total_sessions': 20,
|
||||
'active_sessions': 5,
|
||||
'total_errors': 2,
|
||||
})
|
||||
app.monitoring_service.get_messages = AsyncMock(return_value=(
|
||||
[{'id': 'msg-1', 'content': 'test'}], 100
|
||||
))
|
||||
app.monitoring_service.get_llm_calls = AsyncMock(return_value=(
|
||||
[{'id': 'llm-1'}], 50
|
||||
))
|
||||
app.monitoring_service.get_embedding_calls = AsyncMock(return_value=(
|
||||
[{'id': 'emb-1'}], 10
|
||||
))
|
||||
app.monitoring_service.get_sessions = AsyncMock(return_value=(
|
||||
[{'session_id': 'sess-1'}], 20
|
||||
))
|
||||
app.monitoring_service.get_errors = AsyncMock(return_value=(
|
||||
[{'id': 'err-1'}], 2
|
||||
))
|
||||
app.monitoring_service.get_session_analysis = AsyncMock(return_value={
|
||||
'found': True,
|
||||
'session_id': 'sess-1',
|
||||
})
|
||||
app.monitoring_service.get_message_details = AsyncMock(return_value={
|
||||
'found': True,
|
||||
'message_id': 'msg-1',
|
||||
})
|
||||
app.monitoring_service.get_overview_metrics = AsyncMock(
|
||||
return_value={
|
||||
'total_messages': 100,
|
||||
'total_llm_calls': 50,
|
||||
'total_sessions': 20,
|
||||
'active_sessions': 5,
|
||||
'total_errors': 2,
|
||||
}
|
||||
)
|
||||
app.monitoring_service.get_messages = AsyncMock(return_value=([{'id': 'msg-1', 'content': 'test'}], 100))
|
||||
app.monitoring_service.get_llm_calls = AsyncMock(return_value=([{'id': 'llm-1'}], 50))
|
||||
app.monitoring_service.get_tool_calls = AsyncMock(return_value=([{'id': 'tool-1'}], 5))
|
||||
app.monitoring_service.get_embedding_calls = AsyncMock(return_value=([{'id': 'emb-1'}], 10))
|
||||
app.monitoring_service.get_sessions = AsyncMock(return_value=([{'session_id': 'sess-1'}], 20))
|
||||
app.monitoring_service.get_errors = AsyncMock(return_value=([{'id': 'err-1'}], 2))
|
||||
app.monitoring_service.get_session_analysis = AsyncMock(
|
||||
return_value={
|
||||
'found': True,
|
||||
'session_id': 'sess-1',
|
||||
}
|
||||
)
|
||||
app.monitoring_service.get_message_details = AsyncMock(
|
||||
return_value={
|
||||
'found': True,
|
||||
'message_id': 'msg-1',
|
||||
}
|
||||
)
|
||||
app.monitoring_service.get_feedback_stats = AsyncMock(return_value={'like_count': 10})
|
||||
app.monitoring_service.get_feedback_list = AsyncMock(return_value=(
|
||||
[{'feedback_id': 'fb-1'}], 12
|
||||
))
|
||||
app.monitoring_service.get_feedback_list = AsyncMock(return_value=([{'feedback_id': 'fb-1'}], 12))
|
||||
app.monitoring_service.export_messages = AsyncMock(return_value=[{'id': 'msg-1'}])
|
||||
app.monitoring_service.export_llm_calls = AsyncMock(return_value=[{'id': 'llm-1'}])
|
||||
app.monitoring_service.export_errors = AsyncMock(return_value=[{'id': 'err-1'}])
|
||||
@@ -130,8 +128,7 @@ class TestMonitoringOverviewEndpoint:
|
||||
async def test_get_overview_success(self, quart_test_client):
|
||||
"""GET /api/v1/monitoring/overview returns metrics."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/overview',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/overview', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -147,8 +144,7 @@ class TestMonitoringMessagesEndpoint:
|
||||
async def test_get_messages_success(self, quart_test_client):
|
||||
"""GET /api/v1/monitoring/messages returns message list."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/messages',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/messages', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -165,8 +161,7 @@ class TestMonitoringLLMCallsEndpoint:
|
||||
async def test_get_llm_calls_success(self, quart_test_client):
|
||||
"""GET /api/v1/monitoring/llm-calls."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/llm-calls',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/llm-calls', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -180,8 +175,7 @@ class TestMonitoringEmbeddingCallsEndpoint:
|
||||
async def test_get_embedding_calls_success(self, quart_test_client):
|
||||
"""GET /api/v1/monitoring/embedding-calls."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/embedding-calls',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/embedding-calls', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -195,8 +189,7 @@ class TestMonitoringSessionsEndpoint:
|
||||
async def test_get_sessions_success(self, quart_test_client):
|
||||
"""GET /api/v1/monitoring/sessions."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/sessions',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/sessions', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -210,8 +203,7 @@ class TestMonitoringErrorsEndpoint:
|
||||
async def test_get_errors_success(self, quart_test_client):
|
||||
"""GET /api/v1/monitoring/errors."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/errors',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/errors', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -225,8 +217,7 @@ class TestMonitoringAllDataEndpoint:
|
||||
async def test_get_all_data_success(self, quart_test_client):
|
||||
"""GET /api/v1/monitoring/data returns all data."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/data',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/data', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -242,8 +233,7 @@ class TestMonitoringDetailsEndpoints:
|
||||
async def test_get_session_analysis(self, quart_test_client):
|
||||
"""GET /api/v1/monitoring/sessions/{id}/analysis."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/sessions/sess-1/analysis',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/sessions/sess-1/analysis', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -252,8 +242,7 @@ class TestMonitoringDetailsEndpoints:
|
||||
async def test_get_message_details(self, quart_test_client):
|
||||
"""GET /api/v1/monitoring/messages/{id}/details."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/messages/msg-1/details',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/messages/msg-1/details', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -267,8 +256,7 @@ class TestMonitoringFeedbackEndpoints:
|
||||
async def test_get_feedback_stats(self, quart_test_client):
|
||||
"""GET /api/v1/monitoring/feedback/stats."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/feedback/stats',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/feedback/stats', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -277,8 +265,7 @@ class TestMonitoringFeedbackEndpoints:
|
||||
async def test_get_feedback_list(self, quart_test_client):
|
||||
"""GET /api/v1/monitoring/feedback."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/feedback',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/feedback', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -292,8 +279,7 @@ class TestMonitoringExportEndpoint:
|
||||
async def test_export_messages(self, quart_test_client):
|
||||
"""GET export?type=messages returns CSV."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/export?type=messages',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/export?type=messages', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -303,8 +289,7 @@ class TestMonitoringExportEndpoint:
|
||||
async def test_export_llm_calls(self, quart_test_client):
|
||||
"""GET export?type=llm-calls returns CSV."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/export?type=llm-calls',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/export?type=llm-calls', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -313,8 +298,7 @@ class TestMonitoringExportEndpoint:
|
||||
async def test_export_sessions(self, quart_test_client):
|
||||
"""GET export?type=sessions returns CSV."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/export?type=sessions',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/export?type=sessions', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -323,8 +307,7 @@ class TestMonitoringExportEndpoint:
|
||||
async def test_export_feedback(self, quart_test_client):
|
||||
"""GET export?type=feedback returns CSV."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/monitoring/export?type=feedback',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/monitoring/export?type=feedback', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
@@ -20,6 +20,7 @@ pytestmark = pytest.mark.integration
|
||||
|
||||
# ============== FIXTURE FOR SYS.MODULES ISOLATION ==============
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def mock_circular_import_chain():
|
||||
"""Break circular import chain for API controller."""
|
||||
@@ -53,21 +54,25 @@ def mock_circular_import_chain():
|
||||
):
|
||||
# Import groups after mocking to populate preregistered_groups
|
||||
import langbot.pkg.api.http.controller.groups.pipelines.pipelines as _pipelines # noqa: E402, F401
|
||||
|
||||
yield
|
||||
|
||||
|
||||
# ============== FAKE APPLICATION WITH PIPELINE SERVICES ==============
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def fake_pipeline_app():
|
||||
"""Create FakeApp with pipeline-specific services (module scope for reuse)."""
|
||||
app = FakeApp()
|
||||
|
||||
# Pipeline config
|
||||
app.instance_config.data.update({
|
||||
'api': {'port': 5300},
|
||||
'system': {'allow_modify_login_info': True, 'limitation': {}},
|
||||
})
|
||||
app.instance_config.data.update(
|
||||
{
|
||||
'api': {'port': 5300},
|
||||
'system': {'allow_modify_login_info': True, 'limitation': {}},
|
||||
}
|
||||
)
|
||||
|
||||
# Auth services
|
||||
app.user_service = Mock()
|
||||
@@ -79,25 +84,31 @@ def fake_pipeline_app():
|
||||
|
||||
# Pipeline service
|
||||
app.pipeline_service = Mock()
|
||||
app.pipeline_service.get_pipeline_metadata = AsyncMock(return_value=[
|
||||
{'name': 'trigger', 'stages': []},
|
||||
{'name': 'ai', 'stages': []},
|
||||
])
|
||||
app.pipeline_service.get_pipelines = AsyncMock(return_value=[
|
||||
{
|
||||
app.pipeline_service.get_pipeline_metadata = AsyncMock(
|
||||
return_value=[
|
||||
{'name': 'trigger', 'stages': []},
|
||||
{'name': 'ai', 'stages': []},
|
||||
]
|
||||
)
|
||||
app.pipeline_service.get_pipelines = AsyncMock(
|
||||
return_value=[
|
||||
{
|
||||
'uuid': 'test-pipeline-uuid',
|
||||
'name': 'Test Pipeline',
|
||||
'description': 'Test description',
|
||||
'created_at': '2024-01-01T00:00:00',
|
||||
'updated_at': '2024-01-01T00:00:00',
|
||||
'is_default': False,
|
||||
}
|
||||
]
|
||||
)
|
||||
app.pipeline_service.get_pipeline = AsyncMock(
|
||||
return_value={
|
||||
'uuid': 'test-pipeline-uuid',
|
||||
'name': 'Test Pipeline',
|
||||
'description': 'Test description',
|
||||
'created_at': '2024-01-01T00:00:00',
|
||||
'updated_at': '2024-01-01T00:00:00',
|
||||
'is_default': False,
|
||||
'config': {},
|
||||
}
|
||||
])
|
||||
app.pipeline_service.get_pipeline = AsyncMock(return_value={
|
||||
'uuid': 'test-pipeline-uuid',
|
||||
'name': 'Test Pipeline',
|
||||
'config': {},
|
||||
})
|
||||
)
|
||||
app.pipeline_service.create_pipeline = AsyncMock(return_value={'uuid': 'new-pipeline-uuid'})
|
||||
app.pipeline_service.update_pipeline = AsyncMock(return_value={})
|
||||
app.pipeline_service.delete_pipeline = AsyncMock()
|
||||
@@ -112,6 +123,10 @@ def fake_pipeline_app():
|
||||
app.mcp_service = Mock()
|
||||
app.mcp_service.get_mcp_servers = AsyncMock(return_value=[])
|
||||
|
||||
# Skill service (for extensions endpoint)
|
||||
app.skill_service = Mock()
|
||||
app.skill_service.list_skills = AsyncMock(return_value=[])
|
||||
|
||||
# Plugin connector (for extensions endpoint)
|
||||
app.plugin_connector.list_plugins = AsyncMock(return_value=[])
|
||||
|
||||
@@ -130,6 +145,7 @@ async def quart_test_client(fake_pipeline_app, http_controller_cls):
|
||||
|
||||
# ============== PIPELINE ENDPOINT TESTS ==============
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('mock_circular_import_chain')
|
||||
class TestPipelineMetadataEndpoint:
|
||||
"""Tests for /api/v1/pipelines/_/metadata endpoint."""
|
||||
@@ -138,8 +154,7 @@ class TestPipelineMetadataEndpoint:
|
||||
async def test_get_pipeline_metadata_success(self, quart_test_client):
|
||||
"""GET /api/v1/pipelines/_/metadata returns metadata list."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/pipelines/_/metadata',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/pipelines/_/metadata', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -162,10 +177,7 @@ class TestPipelinesListEndpoint:
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_pipelines_success(self, quart_test_client):
|
||||
"""GET /api/v1/pipelines returns pipeline list."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/pipelines',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
response = await quart_test_client.get('/api/v1/pipelines', headers={'Authorization': 'Bearer test_token'})
|
||||
|
||||
assert response.status_code == 200
|
||||
data = await response.get_json()
|
||||
@@ -176,8 +188,7 @@ class TestPipelinesListEndpoint:
|
||||
async def test_get_pipelines_with_sort_param(self, quart_test_client):
|
||||
"""GET pipelines with sort parameter."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/pipelines?sort_by=created_at&sort_order=DESC',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/pipelines?sort_by=created_at&sort_order=DESC', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -193,8 +204,7 @@ class TestPipelinesCRUDEndpoints:
|
||||
async def test_get_single_pipeline_success(self, quart_test_client):
|
||||
"""GET /api/v1/pipelines/{uuid} returns pipeline."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/pipelines/test-pipeline-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/pipelines/test-pipeline-uuid', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -208,7 +218,7 @@ class TestPipelinesCRUDEndpoints:
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/pipelines',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={'name': 'New Pipeline', 'config': {}}
|
||||
json={'name': 'New Pipeline', 'config': {}},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -222,7 +232,7 @@ class TestPipelinesCRUDEndpoints:
|
||||
response = await quart_test_client.put(
|
||||
'/api/v1/pipelines/test-pipeline-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={'name': 'Updated Pipeline'}
|
||||
json={'name': 'Updated Pipeline'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -233,8 +243,7 @@ class TestPipelinesCRUDEndpoints:
|
||||
async def test_delete_pipeline_success(self, quart_test_client):
|
||||
"""DELETE /api/v1/pipelines/{uuid} deletes pipeline."""
|
||||
response = await quart_test_client.delete(
|
||||
'/api/v1/pipelines/test-pipeline-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/pipelines/test-pipeline-uuid', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -245,8 +254,7 @@ class TestPipelinesCRUDEndpoints:
|
||||
async def test_copy_pipeline_success(self, quart_test_client):
|
||||
"""POST /api/v1/pipelines/{uuid}/copy copies pipeline."""
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/pipelines/test-pipeline-uuid/copy',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/pipelines/test-pipeline-uuid/copy', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -263,8 +271,7 @@ class TestPipelineExtensionsEndpoint:
|
||||
async def test_get_extensions(self, quart_test_client):
|
||||
"""GET /api/v1/pipelines/{uuid}/extensions."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/pipelines/test-pipeline-uuid/extensions',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/pipelines/test-pipeline-uuid/extensions', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
# Should return 200 if pipeline found
|
||||
|
||||
@@ -49,6 +49,7 @@ def mock_circular_import_chain():
|
||||
):
|
||||
import langbot.pkg.api.http.controller.groups.provider.providers as _providers # noqa: E402, F401
|
||||
import langbot.pkg.api.http.controller.groups.provider.models as _models # noqa: E402, F401
|
||||
|
||||
yield
|
||||
|
||||
|
||||
@@ -57,10 +58,12 @@ def fake_provider_app():
|
||||
"""Create FakeApp with provider/model services (module scope for reuse)."""
|
||||
app = FakeApp()
|
||||
|
||||
app.instance_config.data.update({
|
||||
'api': {'port': 5300},
|
||||
'system': {'allow_modify_login_info': True, 'limitation': {}},
|
||||
})
|
||||
app.instance_config.data.update(
|
||||
{
|
||||
'api': {'port': 5300},
|
||||
'system': {'allow_modify_login_info': True, 'limitation': {}},
|
||||
}
|
||||
)
|
||||
|
||||
# Auth services
|
||||
app.user_service = Mock()
|
||||
@@ -72,27 +75,23 @@ def fake_provider_app():
|
||||
|
||||
# Provider service
|
||||
app.provider_service = Mock()
|
||||
app.provider_service.get_providers = AsyncMock(return_value=[
|
||||
{'uuid': 'test-provider-uuid', 'name': 'OpenAI', 'requester': 'chatcmpl'}
|
||||
])
|
||||
app.provider_service.get_provider = AsyncMock(return_value={
|
||||
'uuid': 'test-provider-uuid', 'name': 'OpenAI', 'requester': 'chatcmpl'
|
||||
})
|
||||
app.provider_service.get_providers = AsyncMock(
|
||||
return_value=[{'uuid': 'test-provider-uuid', 'name': 'OpenAI', 'requester': 'chatcmpl'}]
|
||||
)
|
||||
app.provider_service.get_provider = AsyncMock(
|
||||
return_value={'uuid': 'test-provider-uuid', 'name': 'OpenAI', 'requester': 'chatcmpl'}
|
||||
)
|
||||
app.provider_service.create_provider = AsyncMock(return_value='new-provider-uuid')
|
||||
app.provider_service.update_provider = AsyncMock(return_value={})
|
||||
app.provider_service.delete_provider = AsyncMock()
|
||||
app.provider_service.get_provider_model_counts = AsyncMock(return_value={
|
||||
'llm_count': 2, 'embedding_count': 1, 'rerank_count': 0
|
||||
})
|
||||
app.provider_service.get_provider_model_counts = AsyncMock(
|
||||
return_value={'llm_count': 2, 'embedding_count': 1, 'rerank_count': 0}
|
||||
)
|
||||
|
||||
# LLM model service
|
||||
app.llm_model_service = Mock()
|
||||
app.llm_model_service.get_llm_models = AsyncMock(return_value=[
|
||||
{'uuid': 'test-model-uuid', 'name': 'gpt-4'}
|
||||
])
|
||||
app.llm_model_service.get_llm_model = AsyncMock(return_value={
|
||||
'uuid': 'test-model-uuid', 'name': 'gpt-4'
|
||||
})
|
||||
app.llm_model_service.get_llm_models = AsyncMock(return_value=[{'uuid': 'test-model-uuid', 'name': 'gpt-4'}])
|
||||
app.llm_model_service.get_llm_model = AsyncMock(return_value={'uuid': 'test-model-uuid', 'name': 'gpt-4'})
|
||||
app.llm_model_service.create_llm_model = AsyncMock(return_value={'uuid': 'new-model-uuid'})
|
||||
app.llm_model_service.update_llm_model = AsyncMock(return_value={})
|
||||
app.llm_model_service.delete_llm_model = AsyncMock()
|
||||
@@ -133,8 +132,7 @@ class TestProviderEndpoints:
|
||||
async def test_get_providers_success(self, quart_test_client):
|
||||
"""GET /api/v1/provider/providers returns provider list with complete structure."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/provider/providers',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/provider/providers', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -157,8 +155,7 @@ class TestProviderEndpoints:
|
||||
async def test_get_single_provider_success(self, quart_test_client):
|
||||
"""GET /api/v1/provider/providers/{uuid} returns complete provider structure."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/provider/providers/test-provider-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/provider/providers/test-provider-uuid', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -177,7 +174,7 @@ class TestProviderEndpoints:
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/provider/providers',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={'name': 'New Provider', 'requester': 'chatcmpl'}
|
||||
json={'name': 'New Provider', 'requester': 'chatcmpl'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -194,7 +191,7 @@ class TestProviderEndpoints:
|
||||
response = await quart_test_client.put(
|
||||
'/api/v1/provider/providers/test-provider-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={'name': 'Updated Provider'}
|
||||
json={'name': 'Updated Provider'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -205,8 +202,7 @@ class TestProviderEndpoints:
|
||||
async def test_delete_provider_success(self, quart_test_client):
|
||||
"""DELETE /api/v1/provider/providers/{uuid} deletes provider."""
|
||||
response = await quart_test_client.delete(
|
||||
'/api/v1/provider/providers/test-provider-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/provider/providers/test-provider-uuid', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -215,8 +211,7 @@ class TestProviderEndpoints:
|
||||
async def test_get_provider_includes_model_counts(self, quart_test_client):
|
||||
"""GET provider response includes model counts."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/provider/providers/test-provider-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/provider/providers/test-provider-uuid', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -237,8 +232,7 @@ class TestModelEndpoints:
|
||||
async def test_get_llm_models_success(self, quart_test_client):
|
||||
"""GET /api/v1/provider/models/llm returns model list."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/provider/models/llm',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/provider/models/llm', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -250,8 +244,7 @@ class TestModelEndpoints:
|
||||
async def test_get_single_llm_model_success(self, quart_test_client):
|
||||
"""GET /api/v1/provider/models/llm/{uuid} returns model."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/provider/models/llm/test-model-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/provider/models/llm/test-model-uuid', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -264,7 +257,7 @@ class TestModelEndpoints:
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/provider/models/llm',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={'name': 'New Model', 'provider_uuid': 'test-provider-uuid'}
|
||||
json={'name': 'New Model', 'provider_uuid': 'test-provider-uuid'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -276,8 +269,7 @@ class TestModelEndpoints:
|
||||
async def test_delete_llm_model_success(self, quart_test_client):
|
||||
"""DELETE /api/v1/provider/models/llm/{uuid} deletes model."""
|
||||
response = await quart_test_client.delete(
|
||||
'/api/v1/provider/models/llm/test-model-uuid',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/provider/models/llm/test-model-uuid', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -291,8 +283,7 @@ class TestEmbeddingModelEndpoints:
|
||||
async def test_get_embedding_models_success(self, quart_test_client):
|
||||
"""GET /api/v1/provider/models/embedding returns model list."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/provider/models/embedding',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/provider/models/embedding', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -306,7 +297,7 @@ class TestEmbeddingModelEndpoints:
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/provider/models/embedding',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={'name': 'New Embedding Model', 'provider_uuid': 'test-provider-uuid'}
|
||||
json={'name': 'New Embedding Model', 'provider_uuid': 'test-provider-uuid'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -323,8 +314,7 @@ class TestRerankModelEndpoints:
|
||||
async def test_get_rerank_models_success(self, quart_test_client):
|
||||
"""GET /api/v1/provider/models/rerank returns model list."""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/provider/models/rerank',
|
||||
headers={'Authorization': 'Bearer test_token'}
|
||||
'/api/v1/provider/models/rerank', headers={'Authorization': 'Bearer test_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -338,7 +328,7 @@ class TestRerankModelEndpoints:
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/provider/models/rerank',
|
||||
headers={'Authorization': 'Bearer test_token'},
|
||||
json={'name': 'New Rerank Model', 'provider_uuid': 'test-provider-uuid'}
|
||||
json={'name': 'New Rerank Model', 'provider_uuid': 'test-provider-uuid'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
@@ -20,6 +20,7 @@ pytestmark = pytest.mark.integration
|
||||
|
||||
# ============== FIXTURE FOR SYS.MODULES ISOLATION ==============
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def mock_circular_import_chain():
|
||||
"""
|
||||
@@ -69,6 +70,7 @@ def mock_circular_import_chain():
|
||||
|
||||
# ============== FAKE APPLICATION FOR API TESTS ==============
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake_api_app():
|
||||
"""
|
||||
@@ -79,12 +81,14 @@ def fake_api_app():
|
||||
app = FakeApp()
|
||||
|
||||
# API-specific config
|
||||
app.instance_config.data.update({
|
||||
'api': {'port': 5300},
|
||||
'plugin': {'enable_marketplace': True},
|
||||
'space': {'url': 'https://space.langbot.app'},
|
||||
'system': {'allow_modify_login_info': True, 'limitation': {}},
|
||||
})
|
||||
app.instance_config.data.update(
|
||||
{
|
||||
'api': {'port': 5300},
|
||||
'plugin': {'enable_marketplace': True},
|
||||
'space': {'url': 'https://space.langbot.app'},
|
||||
'system': {'allow_modify_login_info': True, 'limitation': {}},
|
||||
}
|
||||
)
|
||||
|
||||
# API-specific services
|
||||
app.user_service = Mock()
|
||||
@@ -118,6 +122,7 @@ def fake_api_app():
|
||||
|
||||
# ============== QUART TEST CLIENT FIXTURE ==============
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def quart_test_client(fake_api_app, http_controller_cls):
|
||||
"""
|
||||
@@ -135,6 +140,7 @@ async def quart_test_client(fake_api_app, http_controller_cls):
|
||||
|
||||
# ============== API SMOKE TESTS ==============
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('mock_circular_import_chain')
|
||||
class TestHealthEndpoint:
|
||||
"""Tests for /healthz endpoint - simplest smoke test."""
|
||||
@@ -222,8 +228,7 @@ class TestProtectedEndpoints:
|
||||
Protected endpoint returns 401 with invalid token.
|
||||
"""
|
||||
response = await quart_test_client.get(
|
||||
'/api/v1/user/check-token',
|
||||
headers={'Authorization': 'Bearer invalid_token'}
|
||||
'/api/v1/user/check-token', headers={'Authorization': 'Bearer invalid_token'}
|
||||
)
|
||||
|
||||
assert response.status_code == 401
|
||||
@@ -254,10 +259,7 @@ class TestInvalidPayload:
|
||||
"""
|
||||
POST with wrong JSON structure returns stable error.
|
||||
"""
|
||||
response = await quart_test_client.post(
|
||||
'/api/v1/user/auth',
|
||||
json={'wrong_field': 'value'}
|
||||
)
|
||||
response = await quart_test_client.post('/api/v1/user/auth', json={'wrong_field': 'value'})
|
||||
|
||||
# Should return error with stable JSON structure
|
||||
assert response.status_code in (400, 500, 401)
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
Persistence integration tests package.
|
||||
|
||||
Tests for database migrations and storage behavior.
|
||||
"""
|
||||
"""
|
||||
|
||||
@@ -17,7 +17,21 @@ from langbot.pkg.persistence.alembic_runner import (
|
||||
run_alembic_upgrade,
|
||||
run_alembic_stamp,
|
||||
get_alembic_current,
|
||||
_ALEMBIC_DIR,
|
||||
)
|
||||
from alembic.config import Config
|
||||
from alembic.script import ScriptDirectory
|
||||
|
||||
|
||||
def _get_script_head() -> str:
|
||||
"""Resolve the current Alembic head revision from the script directory.
|
||||
|
||||
Avoids hardcoding a revision number in assertions so adding a new
|
||||
migration doesn't require editing the migration tests.
|
||||
"""
|
||||
cfg = Config()
|
||||
cfg.set_main_option('script_location', _ALEMBIC_DIR)
|
||||
return ScriptDirectory.from_config(cfg).get_current_head()
|
||||
|
||||
|
||||
pytestmark = pytest.mark.integration
|
||||
@@ -26,8 +40,8 @@ pytestmark = pytest.mark.integration
|
||||
@pytest.fixture
|
||||
def sqlite_db_url(tmp_path):
|
||||
"""Create SQLite URL with temporary database file."""
|
||||
db_file = tmp_path / "test_migrations.db"
|
||||
return f"sqlite+aiosqlite:///{db_file}"
|
||||
db_file = tmp_path / 'test_migrations.db'
|
||||
return f'sqlite+aiosqlite:///{db_file}'
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -102,9 +116,11 @@ class TestSQLiteMigrationUpgrade:
|
||||
|
||||
# Verify revision
|
||||
rev = await get_alembic_current(sqlite_engine)
|
||||
assert rev is not None, "Expected a revision after upgrade"
|
||||
# Head should be the latest migration
|
||||
assert rev.startswith('0003'), f"Expected head to be 0003_*, got {rev}"
|
||||
assert rev is not None, 'Expected a revision after upgrade'
|
||||
# Head should be the latest migration. Resolve the actual head from the
|
||||
# Alembic script directory instead of hardcoding a revision number, so
|
||||
# adding a new migration doesn't require editing this assertion.
|
||||
assert rev == _get_script_head(), f'Expected head {_get_script_head()}, got {rev}'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_upgrade_idempotent(self, sqlite_engine):
|
||||
@@ -131,7 +147,7 @@ class TestSQLiteMigrationUpgrade:
|
||||
await run_alembic_upgrade(sqlite_engine, 'head')
|
||||
|
||||
rev2 = await get_alembic_current(sqlite_engine)
|
||||
assert rev2 == rev1, f"Expected {rev1}, got {rev2}"
|
||||
assert rev2 == rev1, f'Expected {rev1}, got {rev2}'
|
||||
|
||||
|
||||
class TestSQLiteMigrationFreshDatabase:
|
||||
@@ -149,8 +165,8 @@ class TestSQLiteMigrationFreshDatabase:
|
||||
4. Verify revision
|
||||
"""
|
||||
# Use different DB file for fresh test
|
||||
fresh_db_file = tmp_path / "test_migrations_fresh.db"
|
||||
fresh_url = f"sqlite+aiosqlite:///{fresh_db_file}"
|
||||
fresh_db_file = tmp_path / 'test_migrations_fresh.db'
|
||||
fresh_url = f'sqlite+aiosqlite:///{fresh_db_file}'
|
||||
fresh_engine = create_async_engine(fresh_url)
|
||||
|
||||
# Create tables on fresh DB
|
||||
@@ -162,7 +178,7 @@ class TestSQLiteMigrationFreshDatabase:
|
||||
|
||||
# Verify revision
|
||||
rev = await get_alembic_current(fresh_engine)
|
||||
assert rev is not None, "Expected a revision on fresh DB"
|
||||
assert rev is not None, 'Expected a revision on fresh DB'
|
||||
|
||||
await fresh_engine.dispose()
|
||||
|
||||
@@ -181,8 +197,8 @@ class TestSQLiteMigrationFreshDatabase:
|
||||
IMPORTANT: This test verifies the ACTUAL behavior, not accepting
|
||||
any arbitrary failure with try-except pass.
|
||||
"""
|
||||
fresh_db_file = tmp_path / "test_empty_migrations.db"
|
||||
fresh_url = f"sqlite+aiosqlite:///{fresh_db_file}"
|
||||
fresh_db_file = tmp_path / 'test_empty_migrations.db'
|
||||
fresh_url = f'sqlite+aiosqlite:///{fresh_db_file}'
|
||||
fresh_engine = create_async_engine(fresh_url)
|
||||
|
||||
# Capture the actual behavior
|
||||
@@ -201,23 +217,23 @@ class TestSQLiteMigrationFreshDatabase:
|
||||
# Verify specific behavior - one of two outcomes is expected
|
||||
if actual_result is not None:
|
||||
# Migration succeeded - verify revision exists
|
||||
assert actual_result is not None, "Revision should exist after successful migration"
|
||||
assert actual_result is not None, 'Revision should exist after successful migration'
|
||||
else:
|
||||
# Migration failed - verify the error type is known
|
||||
# Alembic typically raises specific errors for missing tables
|
||||
assert actual_error is not None, "Error should be captured if migration failed"
|
||||
assert actual_error is not None, 'Error should be captured if migration failed'
|
||||
# Log the error type for documentation (don't silently pass)
|
||||
error_type = type(actual_error).__name__
|
||||
# Acceptable error types for empty DB scenarios
|
||||
acceptable_errors = [
|
||||
'OperationalError', # SQLite table not found
|
||||
'ProgrammingError', # SQLAlchemy errors
|
||||
'CommandError', # Alembic command errors
|
||||
'CommandError', # Alembic command errors
|
||||
]
|
||||
assert error_type in acceptable_errors, (
|
||||
f"Unexpected error type: {error_type}. "
|
||||
f"This may indicate a regression in migration behavior. "
|
||||
f"Error: {actual_error}"
|
||||
f'Unexpected error type: {error_type}. '
|
||||
f'This may indicate a regression in migration behavior. '
|
||||
f'Error: {actual_error}'
|
||||
)
|
||||
|
||||
|
||||
@@ -235,7 +251,7 @@ class TestSQLiteMigrationGetCurrent:
|
||||
|
||||
# No stamp - should return None
|
||||
rev = await get_alembic_current(sqlite_engine)
|
||||
assert rev is None, f"Expected None for unstamped DB, got {rev}"
|
||||
assert rev is None, f'Expected None for unstamped DB, got {rev}'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_current_after_stamp_returns_revision(self, sqlite_engine):
|
||||
@@ -248,4 +264,4 @@ class TestSQLiteMigrationGetCurrent:
|
||||
await run_alembic_stamp(sqlite_engine, '0001_baseline')
|
||||
|
||||
rev = await get_alembic_current(sqlite_engine)
|
||||
assert rev == '0001_baseline'
|
||||
assert rev == '0001_baseline'
|
||||
|
||||
@@ -23,7 +23,21 @@ from langbot.pkg.persistence.alembic_runner import (
|
||||
run_alembic_upgrade,
|
||||
run_alembic_stamp,
|
||||
get_alembic_current,
|
||||
_ALEMBIC_DIR,
|
||||
)
|
||||
from alembic.config import Config
|
||||
from alembic.script import ScriptDirectory
|
||||
|
||||
|
||||
def _get_script_head() -> str:
|
||||
"""Resolve the current Alembic head revision from the script directory.
|
||||
|
||||
Avoids hardcoding a revision number in assertions so adding a new
|
||||
migration doesn't require editing the migration tests.
|
||||
"""
|
||||
cfg = Config()
|
||||
cfg.set_main_option('script_location', _ALEMBIC_DIR)
|
||||
return ScriptDirectory.from_config(cfg).get_current_head()
|
||||
|
||||
|
||||
pytestmark = [pytest.mark.integration, pytest.mark.slow]
|
||||
@@ -34,14 +48,14 @@ def postgres_url():
|
||||
"""Get PostgreSQL URL from environment."""
|
||||
url = os.environ.get('TEST_POSTGRES_URL')
|
||||
if not url:
|
||||
pytest.skip("TEST_POSTGRES_URL not set")
|
||||
pytest.skip('TEST_POSTGRES_URL not set')
|
||||
return url
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def postgres_engine(postgres_url):
|
||||
"""Create async PostgreSQL engine."""
|
||||
engine = create_async_engine(postgres_url, isolation_level="AUTOCOMMIT")
|
||||
engine = create_async_engine(postgres_url, isolation_level='AUTOCOMMIT')
|
||||
yield engine
|
||||
await engine.dispose()
|
||||
|
||||
@@ -66,7 +80,7 @@ async def clean_alembic_version(postgres_engine):
|
||||
async with postgres_engine.begin() as conn:
|
||||
# Drop alembic_version table if exists
|
||||
try:
|
||||
await conn.execute(text("DROP TABLE IF EXISTS alembic_version"))
|
||||
await conn.execute(text('DROP TABLE IF EXISTS alembic_version'))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -74,7 +88,7 @@ async def clean_alembic_version(postgres_engine):
|
||||
|
||||
async with postgres_engine.begin() as conn:
|
||||
try:
|
||||
await conn.execute(text("DROP TABLE IF EXISTS alembic_version"))
|
||||
await conn.execute(text('DROP TABLE IF EXISTS alembic_version'))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -83,9 +97,7 @@ class TestPostgreSQLMigrationBaseline:
|
||||
"""Tests for baseline stamp workflow on PostgreSQL."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_postgres_baseline_stamp_sets_revision(
|
||||
self, postgres_engine, clean_tables, clean_alembic_version
|
||||
):
|
||||
async def test_postgres_baseline_stamp_sets_revision(self, postgres_engine, clean_tables, clean_alembic_version):
|
||||
"""
|
||||
Stamp baseline on existing tables sets correct revision.
|
||||
|
||||
@@ -106,9 +118,7 @@ class TestPostgreSQLMigrationBaseline:
|
||||
assert rev == '0001_baseline', f"Expected '0001_baseline', got {rev}"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_postgres_baseline_stamp_on_empty_db(
|
||||
self, postgres_engine, clean_tables, clean_alembic_version
|
||||
):
|
||||
async def test_postgres_baseline_stamp_on_empty_db(self, postgres_engine, clean_tables, clean_alembic_version):
|
||||
"""
|
||||
Stamp on empty database (no tables) still sets revision.
|
||||
|
||||
@@ -125,9 +135,7 @@ class TestPostgreSQLMigrationUpgrade:
|
||||
"""Tests for upgrade to head workflow on PostgreSQL."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_postgres_upgrade_from_baseline_to_head(
|
||||
self, postgres_engine, clean_tables, clean_alembic_version
|
||||
):
|
||||
async def test_postgres_upgrade_from_baseline_to_head(self, postgres_engine, clean_tables, clean_alembic_version):
|
||||
"""
|
||||
Upgrade from baseline to head applies all migrations.
|
||||
|
||||
@@ -149,14 +157,14 @@ class TestPostgreSQLMigrationUpgrade:
|
||||
|
||||
# Verify revision
|
||||
rev = await get_alembic_current(postgres_engine)
|
||||
assert rev is not None, "Expected a revision after upgrade"
|
||||
# Head should be the latest migration (0003 for current state)
|
||||
assert rev.startswith('0003'), f"Expected head to be 0003_*, got {rev}"
|
||||
assert rev is not None, 'Expected a revision after upgrade'
|
||||
# Head should be the latest migration. Resolve the actual head from the
|
||||
# Alembic script directory instead of hardcoding a revision number, so
|
||||
# adding a new migration doesn't require editing this assertion.
|
||||
assert rev == _get_script_head(), f'Expected head {_get_script_head()}, got {rev}'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_postgres_upgrade_idempotent(
|
||||
self, postgres_engine, clean_tables, clean_alembic_version
|
||||
):
|
||||
async def test_postgres_upgrade_idempotent(self, postgres_engine, clean_tables, clean_alembic_version):
|
||||
"""
|
||||
Running upgrade to head multiple times is idempotent.
|
||||
|
||||
@@ -180,7 +188,7 @@ class TestPostgreSQLMigrationUpgrade:
|
||||
await run_alembic_upgrade(postgres_engine, 'head')
|
||||
|
||||
rev2 = await get_alembic_current(postgres_engine)
|
||||
assert rev2 == rev1, f"Expected {rev1}, got {rev2}"
|
||||
assert rev2 == rev1, f'Expected {rev1}, got {rev2}'
|
||||
|
||||
|
||||
class TestPostgreSQLMigrationGetCurrent:
|
||||
@@ -199,7 +207,7 @@ class TestPostgreSQLMigrationGetCurrent:
|
||||
|
||||
# No stamp - should return None
|
||||
rev = await get_alembic_current(postgres_engine)
|
||||
assert rev is None, f"Expected None for unstamped DB, got {rev}"
|
||||
assert rev is None, f'Expected None for unstamped DB, got {rev}'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_postgres_get_current_after_stamp_returns_revision(
|
||||
@@ -214,4 +222,4 @@ class TestPostgreSQLMigrationGetCurrent:
|
||||
await run_alembic_stamp(postgres_engine, '0001_baseline')
|
||||
|
||||
rev = await get_alembic_current(postgres_engine)
|
||||
assert rev == '0001_baseline'
|
||||
assert rev == '0001_baseline'
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
Pipeline integration tests package.
|
||||
|
||||
Tests for full pipeline flow using fake provider/runner.
|
||||
"""
|
||||
"""
|
||||
|
||||
@@ -26,6 +26,7 @@ pytestmark = pytest.mark.integration
|
||||
|
||||
# ============== FIXTURE FOR SYS.MODULES ISOLATION ==============
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def mock_circular_import_chain():
|
||||
"""
|
||||
@@ -103,6 +104,7 @@ def mock_circular_import_chain():
|
||||
|
||||
# ============== FAKE RUNNER ==============
|
||||
|
||||
|
||||
class FakeRunner:
|
||||
"""Minimal fake runner class for pipeline integration tests.
|
||||
|
||||
@@ -117,12 +119,13 @@ class FakeRunner:
|
||||
self.config = config or {}
|
||||
self._provider = FakeProvider()
|
||||
# Instance-level configuration set via class attribute
|
||||
self._response_text = "fake response"
|
||||
self._response_text = 'fake response'
|
||||
self._raise_error = None
|
||||
|
||||
@classmethod
|
||||
def returns(cls, text: str):
|
||||
"""Create a runner class configured to return specific text."""
|
||||
|
||||
# We create a subclass with configured response
|
||||
class ConfiguredRunner(cls):
|
||||
name = cls.name
|
||||
@@ -132,11 +135,13 @@ class FakeRunner:
|
||||
def __init__(self, app=None, config=None):
|
||||
super().__init__(app, config)
|
||||
self._response_text = text
|
||||
|
||||
return ConfiguredRunner
|
||||
|
||||
@classmethod
|
||||
def raises(cls, error: Exception):
|
||||
"""Create a runner class configured to raise an error."""
|
||||
|
||||
class ConfiguredRunner(cls):
|
||||
name = cls.name
|
||||
_response_text = None
|
||||
@@ -145,6 +150,7 @@ class FakeRunner:
|
||||
def __init__(self, app=None, config=None):
|
||||
super().__init__(app, config)
|
||||
self._raise_error = error
|
||||
|
||||
return ConfiguredRunner
|
||||
|
||||
async def run(self, query):
|
||||
@@ -161,6 +167,7 @@ class FakeRunner:
|
||||
|
||||
# ============== PIPELINE APP FIXTURE ==============
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def pipeline_app():
|
||||
"""
|
||||
@@ -187,6 +194,7 @@ def pipeline_app():
|
||||
def __init__(self, name, messages):
|
||||
self.name = name
|
||||
self.messages = messages
|
||||
|
||||
def copy(self):
|
||||
return MockPrompt(self.name, list(self.messages))
|
||||
|
||||
@@ -237,14 +245,17 @@ def fake_platform_adapter():
|
||||
@pytest.fixture
|
||||
def set_fake_runner():
|
||||
"""Factory fixture to set a fake runner CLASS in preregistered_runners."""
|
||||
|
||||
def _set_runner(runner_cls):
|
||||
# preregistered_runners expects a list of runner classes
|
||||
sys.modules['langbot.pkg.provider.runner'].preregistered_runners = [runner_cls]
|
||||
|
||||
return _set_runner
|
||||
|
||||
|
||||
# ============== PIPELINE CONFIGURATION ==============
|
||||
|
||||
|
||||
def create_minimal_pipeline_config():
|
||||
"""Create minimal pipeline configuration for tests."""
|
||||
return {
|
||||
@@ -273,6 +284,7 @@ def create_minimal_pipeline_config():
|
||||
|
||||
# ============== HELPER TO PROCESS COROUTINE/GENERATOR ==============
|
||||
|
||||
|
||||
async def collect_processor_results(processor, query, stage_name):
|
||||
"""
|
||||
Helper to handle the coroutine -> async_generator pattern.
|
||||
@@ -296,6 +308,7 @@ async def collect_processor_results(processor, query, stage_name):
|
||||
|
||||
# ============== TESTS ==============
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('mock_circular_import_chain')
|
||||
class TestPipelineStageChainReal:
|
||||
"""Tests for real pipeline stage chain."""
|
||||
@@ -337,7 +350,7 @@ class TestPreProcessorStage:
|
||||
adapter, platform = fake_platform_adapter
|
||||
|
||||
# Create query with adapter
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.adapter = adapter
|
||||
query.pipeline_config = create_minimal_pipeline_config()
|
||||
|
||||
@@ -365,7 +378,7 @@ class TestPreProcessorStage:
|
||||
|
||||
adapter, platform = fake_platform_adapter
|
||||
|
||||
query = text_query("test message content")
|
||||
query = text_query('test message content')
|
||||
query.adapter = adapter
|
||||
query.pipeline_config = create_minimal_pipeline_config()
|
||||
|
||||
@@ -396,11 +409,11 @@ class TestProcessorStage:
|
||||
adapter, platform = fake_platform_adapter
|
||||
|
||||
# Set fake runner that returns pong
|
||||
fake_runner = FakeRunner().returns("LANGBOT_FAKE_PONG")
|
||||
fake_runner = FakeRunner().returns('LANGBOT_FAKE_PONG')
|
||||
set_fake_runner(fake_runner)
|
||||
|
||||
# Create query
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.adapter = adapter
|
||||
query.pipeline_config = create_minimal_pipeline_config()
|
||||
query.resp_messages = []
|
||||
@@ -414,6 +427,7 @@ class TestProcessorStage:
|
||||
|
||||
# Create Processor stage
|
||||
from langbot.pkg.pipeline.process import process
|
||||
|
||||
processor_stage = process.Processor(pipeline_app)
|
||||
await processor_stage.initialize(query.pipeline_config)
|
||||
|
||||
@@ -432,7 +446,7 @@ class TestProcessorStage:
|
||||
adapter, platform = fake_platform_adapter
|
||||
|
||||
# Create query
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.adapter = adapter
|
||||
query.pipeline_config = create_minimal_pipeline_config()
|
||||
|
||||
@@ -445,6 +459,7 @@ class TestProcessorStage:
|
||||
|
||||
# Create Processor stage
|
||||
from langbot.pkg.pipeline.process import process
|
||||
|
||||
processor_stage = process.Processor(pipeline_app)
|
||||
await processor_stage.initialize(query.pipeline_config)
|
||||
|
||||
@@ -462,13 +477,13 @@ class TestProcessorStage:
|
||||
adapter, platform = fake_platform_adapter
|
||||
|
||||
# Create query
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.adapter = adapter
|
||||
query.pipeline_config = create_minimal_pipeline_config()
|
||||
query.resp_messages = []
|
||||
|
||||
# Create reply chain
|
||||
reply_chain = text_chain("plugin response")
|
||||
reply_chain = text_chain('plugin response')
|
||||
|
||||
# Mock plugin_connector to prevent default with reply
|
||||
mock_event_ctx = Mock()
|
||||
@@ -479,6 +494,7 @@ class TestProcessorStage:
|
||||
|
||||
# Create Processor stage
|
||||
from langbot.pkg.pipeline.process import process
|
||||
|
||||
processor_stage = process.Processor(pipeline_app)
|
||||
await processor_stage.initialize(query.pipeline_config)
|
||||
|
||||
@@ -502,7 +518,7 @@ class TestRunnerExceptionFlow:
|
||||
adapter, platform = fake_platform_adapter
|
||||
|
||||
# Set fake runner that raises exception
|
||||
fake_runner = FakeRunner().raises(ValueError("API Error: rate limit exceeded"))
|
||||
fake_runner = FakeRunner().raises(ValueError('API Error: rate limit exceeded'))
|
||||
set_fake_runner(fake_runner)
|
||||
|
||||
# Create query with exception handling config
|
||||
@@ -510,7 +526,7 @@ class TestRunnerExceptionFlow:
|
||||
config['output']['misc']['exception-handling'] = 'show-hint'
|
||||
config['output']['misc']['failure-hint'] = 'Request failed.'
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.adapter = adapter
|
||||
query.pipeline_config = config
|
||||
|
||||
@@ -523,6 +539,7 @@ class TestRunnerExceptionFlow:
|
||||
|
||||
# Create Processor stage
|
||||
from langbot.pkg.pipeline.process import process
|
||||
|
||||
processor_stage = process.Processor(pipeline_app)
|
||||
await processor_stage.initialize(query.pipeline_config)
|
||||
|
||||
@@ -541,14 +558,14 @@ class TestRunnerExceptionFlow:
|
||||
adapter, platform = fake_platform_adapter
|
||||
|
||||
# Set fake runner that raises specific exception
|
||||
fake_runner = FakeRunner().raises(RuntimeError("Custom runtime error"))
|
||||
fake_runner = FakeRunner().raises(RuntimeError('Custom runtime error'))
|
||||
set_fake_runner(fake_runner)
|
||||
|
||||
# Create query with show-error mode
|
||||
config = create_minimal_pipeline_config()
|
||||
config['output']['misc']['exception-handling'] = 'show-error'
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.adapter = adapter
|
||||
query.pipeline_config = config
|
||||
|
||||
@@ -561,6 +578,7 @@ class TestRunnerExceptionFlow:
|
||||
|
||||
# Create Processor stage
|
||||
from langbot.pkg.pipeline.process import process
|
||||
|
||||
processor_stage = process.Processor(pipeline_app)
|
||||
await processor_stage.initialize(query.pipeline_config)
|
||||
|
||||
@@ -578,14 +596,14 @@ class TestRunnerExceptionFlow:
|
||||
adapter, platform = fake_platform_adapter
|
||||
|
||||
# Set fake runner that raises exception
|
||||
fake_runner = FakeRunner().raises(Exception("Hidden error"))
|
||||
fake_runner = FakeRunner().raises(Exception('Hidden error'))
|
||||
set_fake_runner(fake_runner)
|
||||
|
||||
# Create query with hide mode
|
||||
config = create_minimal_pipeline_config()
|
||||
config['output']['misc']['exception-handling'] = 'hide'
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.adapter = adapter
|
||||
query.pipeline_config = config
|
||||
|
||||
@@ -598,6 +616,7 @@ class TestRunnerExceptionFlow:
|
||||
|
||||
# Create Processor stage
|
||||
from langbot.pkg.pipeline.process import process
|
||||
|
||||
processor_stage = process.Processor(pipeline_app)
|
||||
await processor_stage.initialize(query.pipeline_config)
|
||||
|
||||
@@ -623,7 +642,7 @@ class TestSendResponseBackStage:
|
||||
adapter, platform = fake_platform_adapter
|
||||
|
||||
# Create query with response message
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.adapter = adapter
|
||||
query.pipeline_config = create_minimal_pipeline_config()
|
||||
|
||||
@@ -643,6 +662,100 @@ class TestSendResponseBackStage:
|
||||
assert len(outbound) == 1
|
||||
assert outbound[0]['type'] == 'reply'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_response_failure_notifies_plugin_diagnostic(self, pipeline_app):
|
||||
"""Plugin-provided deferred replies should report delivery failures."""
|
||||
from langbot.pkg.pipeline import plugin_diagnostics
|
||||
from langbot.pkg.pipeline.respback import respback
|
||||
from tests.factories.message import text_chain
|
||||
from langbot_plugin.api.entities.builtin.provider.message import Message
|
||||
|
||||
query = text_query('hello')
|
||||
query.adapter.reply_message.side_effect = RuntimeError('send failed')
|
||||
query.pipeline_config = create_minimal_pipeline_config()
|
||||
query.current_stage_name = 'SendResponseBackStage'
|
||||
query.resp_messages = [Message(role='assistant', content='test response')]
|
||||
query.resp_message_chain = [text_chain('test response')]
|
||||
plugin_diagnostics.record_plugin_response_source(
|
||||
query,
|
||||
0,
|
||||
[
|
||||
{
|
||||
'kind': 'reply_message_chain',
|
||||
'plugin': {'author': 'tester', 'name': 'demo'},
|
||||
}
|
||||
],
|
||||
[{'manifest': {'metadata': {'author': 'observer', 'name': 'not-reply-source'}}}],
|
||||
'NormalMessageResponded',
|
||||
)
|
||||
pipeline_app.plugin_connector.notify_plugin_diagnostic = AsyncMock()
|
||||
|
||||
respback_stage = respback.SendResponseBackStage(pipeline_app)
|
||||
|
||||
with pytest.raises(RuntimeError, match='send failed'):
|
||||
await respback_stage.process(query, 'SendResponseBackStage')
|
||||
|
||||
pipeline_app.plugin_connector.notify_plugin_diagnostic.assert_awaited_once()
|
||||
payload = pipeline_app.plugin_connector.notify_plugin_diagnostic.await_args.args[0]
|
||||
assert payload['code'] == 'response_delivery_failed'
|
||||
assert payload['plugin'] == {'author': 'tester', 'name': 'demo'}
|
||||
assert payload['query']['event_name'] == 'NormalMessageResponded'
|
||||
assert payload['delivery']['error_type'] == 'RuntimeError'
|
||||
assert 'attribution_warning' not in payload['details']
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_response_failure_warns_for_old_runtime_attribution(self, pipeline_app):
|
||||
"""Older plugin runtimes without response_sources should get approximate diagnostics."""
|
||||
from langbot.pkg.pipeline import plugin_diagnostics
|
||||
from langbot.pkg.pipeline.respback import respback
|
||||
from tests.factories.message import text_chain
|
||||
from langbot_plugin.api.entities.builtin.provider.message import Message
|
||||
|
||||
query = text_query('hello')
|
||||
query.adapter.reply_message.side_effect = RuntimeError('send failed')
|
||||
query.pipeline_config = create_minimal_pipeline_config()
|
||||
query.resp_messages = [Message(role='assistant', content='test response')]
|
||||
query.resp_message_chain = [text_chain('test response')]
|
||||
plugin_diagnostics.record_plugin_response_source(
|
||||
query,
|
||||
0,
|
||||
None,
|
||||
[{'manifest': {'metadata': {'author': 'tester', 'name': 'demo'}}}],
|
||||
'NormalMessageResponded',
|
||||
)
|
||||
pipeline_app.plugin_connector.notify_plugin_diagnostic = AsyncMock()
|
||||
|
||||
respback_stage = respback.SendResponseBackStage(pipeline_app)
|
||||
|
||||
with pytest.raises(RuntimeError, match='send failed'):
|
||||
await respback_stage.process(query, 'SendResponseBackStage')
|
||||
|
||||
payload = pipeline_app.plugin_connector.notify_plugin_diagnostic.await_args.args[0]
|
||||
assert payload['plugin'] == {'author': 'tester', 'name': 'demo'}
|
||||
assert 'attribution_warning' in payload['details']
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_response_failure_ignores_query_variable_spoofing(self, pipeline_app):
|
||||
"""Plugin-controlled query variables must not mask delivery failures."""
|
||||
from langbot.pkg.pipeline.respback import respback
|
||||
from tests.factories.message import text_chain
|
||||
from langbot_plugin.api.entities.builtin.provider.message import Message
|
||||
|
||||
query = text_query('hello')
|
||||
query.adapter.reply_message.side_effect = RuntimeError('send failed')
|
||||
query.pipeline_config = create_minimal_pipeline_config()
|
||||
query.resp_messages = [Message(role='assistant', content='test response')]
|
||||
query.resp_message_chain = [text_chain('test response')]
|
||||
query.variables['_plugin_response_sources'] = {0: ['malformed']}
|
||||
pipeline_app.plugin_connector.notify_plugin_diagnostic = AsyncMock()
|
||||
|
||||
respback_stage = respback.SendResponseBackStage(pipeline_app)
|
||||
|
||||
with pytest.raises(RuntimeError, match='send failed'):
|
||||
await respback_stage.process(query, 'SendResponseBackStage')
|
||||
|
||||
pipeline_app.plugin_connector.notify_plugin_diagnostic.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('mock_circular_import_chain')
|
||||
class TestStageChainIntegration:
|
||||
@@ -666,12 +779,12 @@ class TestStageChainIntegration:
|
||||
adapter, platform = fake_platform_adapter
|
||||
|
||||
# Set fake runner
|
||||
fake_runner = FakeRunner().returns("LANGBOT_FAKE_PONG")
|
||||
fake_runner = FakeRunner().returns('LANGBOT_FAKE_PONG')
|
||||
set_fake_runner(fake_runner)
|
||||
|
||||
# Create query
|
||||
config = create_minimal_pipeline_config()
|
||||
query = text_query("ping")
|
||||
query = text_query('ping')
|
||||
query.adapter = adapter
|
||||
query.pipeline_config = config
|
||||
query.resp_messages = []
|
||||
@@ -690,7 +803,7 @@ class TestStageChainIntegration:
|
||||
|
||||
pipeline_app.plugin_connector.emit_event = AsyncMock()
|
||||
pipeline_app.plugin_connector.emit_event.side_effect = [
|
||||
mock_event_ctx_preproc, # PreProcessor PromptPreProcessing
|
||||
mock_event_ctx_preproc, # PreProcessor PromptPreProcessing
|
||||
mock_event_ctx_processor, # Processor NormalMessageReceived
|
||||
]
|
||||
|
||||
@@ -711,6 +824,7 @@ class TestStageChainIntegration:
|
||||
|
||||
# Build resp_message_chain from resp_messages
|
||||
from tests.factories.message import text_chain
|
||||
|
||||
for resp_msg in query.resp_messages:
|
||||
if resp_msg.content:
|
||||
query.resp_message_chain.append(text_chain(resp_msg.content))
|
||||
@@ -737,7 +851,7 @@ class TestStageChainIntegration:
|
||||
adapter, platform = fake_platform_adapter
|
||||
|
||||
# Create query
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.adapter = adapter
|
||||
query.pipeline_config = create_minimal_pipeline_config()
|
||||
|
||||
@@ -754,7 +868,7 @@ class TestStageChainIntegration:
|
||||
|
||||
pipeline_app.plugin_connector.emit_event = AsyncMock()
|
||||
pipeline_app.plugin_connector.emit_event.side_effect = [
|
||||
mock_event_ctx_preproc, # PreProcessor PromptPreProcessing
|
||||
mock_event_ctx_preproc, # PreProcessor PromptPreProcessing
|
||||
mock_event_ctx_processor, # Processor NormalMessageReceived
|
||||
]
|
||||
|
||||
@@ -775,4 +889,4 @@ class TestStageChainIntegration:
|
||||
assert results[0].result_type == entities.ResultType.INTERRUPT
|
||||
|
||||
# Chain stops here - no resp_messages
|
||||
assert len(query.resp_messages) == 0
|
||||
assert len(query.resp_messages) == 0
|
||||
|
||||
@@ -0,0 +1,343 @@
|
||||
"""Integration tests for the Valkey Search VDB backend.
|
||||
|
||||
These are SLOW, real-server tests. They are gated on ``TEST_VALKEY_URL`` and
|
||||
skipped when it is unset (same precedent as the PostgreSQL migration tests).
|
||||
|
||||
Run locally against valkey/valkey-bundle:9.1.0::
|
||||
|
||||
podman run -d --name valkey-test-langbot -p 6380:6379 valkey/valkey-bundle:9.1.0
|
||||
TEST_VALKEY_URL=valkey://localhost:6380 \\
|
||||
uv run pytest tests/integration/vector/test_valkey_search.py -m slow -q
|
||||
|
||||
The default upstream fast CI lane (``-m "not slow"``) skips these; the local
|
||||
supervisor validator MUST run them.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import uuid
|
||||
from types import SimpleNamespace
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import pytest
|
||||
|
||||
pytestmark = [pytest.mark.integration, pytest.mark.slow]
|
||||
|
||||
|
||||
def _parse_valkey_url(url: str) -> tuple[str, int, int]:
|
||||
"""Parse ``valkey://host:port/db`` into ``(host, port, db)``."""
|
||||
parsed = urlparse(url)
|
||||
host = parsed.hostname or 'localhost'
|
||||
port = parsed.port or 6379
|
||||
db = 0
|
||||
if parsed.path and parsed.path.strip('/'):
|
||||
try:
|
||||
db = int(parsed.path.strip('/'))
|
||||
except ValueError:
|
||||
db = 0
|
||||
return host, port, db
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def valkey_config():
|
||||
url = os.environ.get('TEST_VALKEY_URL')
|
||||
if not url:
|
||||
pytest.skip('TEST_VALKEY_URL not set')
|
||||
host, port, db = _parse_valkey_url(url)
|
||||
return {
|
||||
'host': host,
|
||||
'port': port,
|
||||
'db': db,
|
||||
'password': '',
|
||||
'username': '',
|
||||
'tls': False,
|
||||
'index_algorithm': 'HNSW',
|
||||
'distance_metric': 'COSINE',
|
||||
}
|
||||
|
||||
|
||||
def _make_ap(valkey_config):
|
||||
"""Build a minimal fake ``ap`` with the config + a no-op logger."""
|
||||
logger = SimpleNamespace(
|
||||
info=lambda *a, **k: None,
|
||||
warning=lambda *a, **k: None,
|
||||
error=lambda *a, **k: None,
|
||||
debug=lambda *a, **k: None,
|
||||
)
|
||||
instance_config = SimpleNamespace(data={'vdb': {'valkey_search': valkey_config}})
|
||||
return SimpleNamespace(instance_config=instance_config, logger=logger)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def backend(valkey_config):
|
||||
"""Create a Valkey Search backend, skip if module/server unavailable."""
|
||||
from langbot.pkg.vector.vdbs.valkey_search import (
|
||||
ValkeySearchVectorDatabase,
|
||||
VALKEY_SEARCH_AVAILABLE,
|
||||
)
|
||||
from glide import ft
|
||||
|
||||
if not VALKEY_SEARCH_AVAILABLE:
|
||||
pytest.skip('valkey-glide not installed')
|
||||
|
||||
ap = _make_ap(valkey_config)
|
||||
db = ValkeySearchVectorDatabase(ap)
|
||||
client = await db._ensure_client()
|
||||
|
||||
# Module-presence gate: FT.LIST must be available (Search module loaded).
|
||||
try:
|
||||
await ft.list(client)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
await client.close()
|
||||
pytest.skip(f'Valkey Search module not available: {exc}')
|
||||
|
||||
collection = f'test_{uuid.uuid4().hex[:12]}'
|
||||
yield db, collection
|
||||
|
||||
# Cleanup
|
||||
try:
|
||||
await db.delete_collection(collection)
|
||||
except Exception:
|
||||
pass
|
||||
if db._client is not None:
|
||||
await db._client.close()
|
||||
|
||||
|
||||
async def _poll_until(coro_factory, predicate, timeout=5.0, interval=0.2):
|
||||
"""Poll an async result until predicate is true (indexer is async)."""
|
||||
deadline = asyncio.get_event_loop().time() + timeout
|
||||
result = await coro_factory()
|
||||
while not predicate(result) and asyncio.get_event_loop().time() < deadline:
|
||||
await asyncio.sleep(interval)
|
||||
result = await coro_factory()
|
||||
return result
|
||||
|
||||
|
||||
def _sample_docs():
|
||||
ids = ['d1', 'd2', 'd3']
|
||||
embeddings = [
|
||||
[1.0, 0.0, 0.0, 0.0],
|
||||
[0.0, 1.0, 0.0, 0.0],
|
||||
[0.9, 0.1, 0.0, 0.0],
|
||||
]
|
||||
metadatas = [
|
||||
{'file_id': 'fileA', 'topic': 'cats'},
|
||||
{'file_id': 'fileB', 'topic': 'dogs'},
|
||||
{'file_id': 'fileA', 'topic': 'cats'},
|
||||
]
|
||||
documents = [
|
||||
'the quick brown fox',
|
||||
'lazy dogs sleeping',
|
||||
'foxes and cats playing',
|
||||
]
|
||||
return ids, embeddings, metadatas, documents
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_and_vector_search(backend):
|
||||
db, collection = backend
|
||||
ids, embeddings, metadatas, documents = _sample_docs()
|
||||
await db.add_embeddings(collection, ids, embeddings, metadatas, documents)
|
||||
|
||||
result = await _poll_until(
|
||||
lambda: db.search(collection, [1.0, 0.0, 0.0, 0.0], k=3, search_type='vector'),
|
||||
lambda r: len(r['ids'][0]) >= 1,
|
||||
)
|
||||
assert len(result['ids'][0]) >= 1
|
||||
# Closest to [1,0,0,0] should be d1.
|
||||
assert result['ids'][0][0] == 'd1'
|
||||
assert all(isinstance(d, float) for d in result['distances'][0])
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_full_text_search(backend):
|
||||
db, collection = backend
|
||||
ids, embeddings, metadatas, documents = _sample_docs()
|
||||
await db.add_embeddings(collection, ids, embeddings, metadatas, documents)
|
||||
|
||||
result = await _poll_until(
|
||||
lambda: db.search(collection, [0.0, 0.0, 0.0, 0.0], k=5, search_type='full_text', query_text='dogs'),
|
||||
lambda r: len(r['ids'][0]) >= 1,
|
||||
)
|
||||
assert 'd2' in result['ids'][0]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_hybrid_filter_then_knn(backend):
|
||||
db, collection = backend
|
||||
ids, embeddings, metadatas, documents = _sample_docs()
|
||||
await db.add_embeddings(collection, ids, embeddings, metadatas, documents)
|
||||
|
||||
result = await _poll_until(
|
||||
lambda: db.search(
|
||||
collection,
|
||||
[1.0, 0.0, 0.0, 0.0],
|
||||
k=5,
|
||||
search_type='hybrid',
|
||||
query_text='cats',
|
||||
filter={'file_id': 'fileA'},
|
||||
),
|
||||
lambda r: len(r['ids'][0]) >= 1,
|
||||
)
|
||||
# Only fileA docs (d1, d3) should be candidates.
|
||||
assert set(result['ids'][0]).issubset({'d1', 'd3'})
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vector_weight_not_honored(backend):
|
||||
"""Passing different vector_weight values must NOT change ranking."""
|
||||
db, collection = backend
|
||||
ids, embeddings, metadatas, documents = _sample_docs()
|
||||
await db.add_embeddings(collection, ids, embeddings, metadatas, documents)
|
||||
|
||||
common = dict(
|
||||
collection=collection, query_embedding=[1.0, 0.0, 0.0, 0.0], k=3, search_type='hybrid', query_text='cats'
|
||||
)
|
||||
await _poll_until(lambda: db.search(**common), lambda r: len(r['ids'][0]) >= 1)
|
||||
|
||||
r_low = await db.search(**common, vector_weight=0.1)
|
||||
r_high = await db.search(**common, vector_weight=0.9)
|
||||
assert r_low['ids'][0] == r_high['ids'][0]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_filter_operators(backend):
|
||||
db, collection = backend
|
||||
ids, embeddings, metadatas, documents = _sample_docs()
|
||||
await db.add_embeddings(collection, ids, embeddings, metadatas, documents)
|
||||
|
||||
# Wait for indexing.
|
||||
await _poll_until(
|
||||
lambda: db.list_by_filter(collection, limit=10),
|
||||
lambda r: r[1] >= 3,
|
||||
)
|
||||
|
||||
# $eq
|
||||
items, total = await db.list_by_filter(collection, filter={'file_id': 'fileA'})
|
||||
assert total == 2
|
||||
assert {it['id'] for it in items} == {'d1', 'd3'}
|
||||
|
||||
# $ne
|
||||
items, total = await db.list_by_filter(collection, filter={'file_id': {'$ne': 'fileA'}})
|
||||
assert {it['id'] for it in items} == {'d2'}
|
||||
|
||||
# $in
|
||||
items, total = await db.list_by_filter(collection, filter={'file_id': {'$in': ['fileA', 'fileB']}})
|
||||
assert total == 3
|
||||
|
||||
# $nin
|
||||
items, total = await db.list_by_filter(collection, filter={'file_id': {'$nin': ['fileB']}})
|
||||
assert {it['id'] for it in items} == {'d1', 'd3'}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_by_file_id(backend):
|
||||
db, collection = backend
|
||||
ids, embeddings, metadatas, documents = _sample_docs()
|
||||
await db.add_embeddings(collection, ids, embeddings, metadatas, documents)
|
||||
await _poll_until(lambda: db.list_by_filter(collection, limit=10), lambda r: r[1] >= 3)
|
||||
|
||||
await db.delete_by_file_id(collection, 'fileA')
|
||||
items, total = await _poll_until(
|
||||
lambda: db.list_by_filter(collection, limit=10),
|
||||
lambda r: r[1] <= 1,
|
||||
)
|
||||
assert {it['id'] for it in items} == {'d2'}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_by_filter_returns_count(backend):
|
||||
db, collection = backend
|
||||
ids, embeddings, metadatas, documents = _sample_docs()
|
||||
await db.add_embeddings(collection, ids, embeddings, metadatas, documents)
|
||||
await _poll_until(lambda: db.list_by_filter(collection, limit=10), lambda r: r[1] >= 3)
|
||||
|
||||
deleted = await db.delete_by_filter(collection, filter={'file_id': 'fileA'})
|
||||
assert deleted == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_by_filter_pagination(backend):
|
||||
db, collection = backend
|
||||
ids, embeddings, metadatas, documents = _sample_docs()
|
||||
await db.add_embeddings(collection, ids, embeddings, metadatas, documents)
|
||||
await _poll_until(lambda: db.list_by_filter(collection, limit=10), lambda r: r[1] >= 3)
|
||||
|
||||
page1, total = await db.list_by_filter(collection, limit=2, offset=0)
|
||||
assert total == 3
|
||||
assert len(page1) == 2
|
||||
|
||||
page2, total = await db.list_by_filter(collection, limit=2, offset=2)
|
||||
assert total == 3
|
||||
assert len(page2) == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_collection(backend):
|
||||
db, collection = backend
|
||||
ids, embeddings, metadatas, documents = _sample_docs()
|
||||
await db.add_embeddings(collection, ids, embeddings, metadatas, documents)
|
||||
await _poll_until(lambda: db.list_by_filter(collection, limit=10), lambda r: r[1] >= 3)
|
||||
|
||||
await db.delete_collection(collection)
|
||||
|
||||
# After dropping, search on a missing index returns empty.
|
||||
result = await db.search(collection, [1.0, 0.0, 0.0, 0.0], k=3, search_type='vector')
|
||||
assert result['ids'][0] == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_adversarial_filter_and_query_input(backend):
|
||||
"""Crafted FT special chars in file_id / query_text must not break out.
|
||||
|
||||
Guarantees locked in here:
|
||||
* A file_id full of injection-style chars (quotes, parens, ``|``, ``@``,
|
||||
``:``, spaces, dashes) only ever matches its own row — the payload is
|
||||
escaped to literal TAG content, never interpreted as extra clauses.
|
||||
* A query_text full of FT operators does not raise and does not widen the
|
||||
result set.
|
||||
* A file_id containing FT-unsafe chars (``{`` / ``}`` / ``*``) is
|
||||
percent-encoded, so it round-trips correctly: an exact match returns ONLY
|
||||
its own row and never widens to an unrelated row, and the query does not
|
||||
raise.
|
||||
"""
|
||||
db, collection = backend
|
||||
|
||||
# Injection-style file_id WITHOUT FT-unsafe chars (the realistic surface).
|
||||
injection_fid = 'evil") @file_id (".id|x-y:z'
|
||||
# file_id WITH FT-unsafe chars that previously could not be queried.
|
||||
brace_fid = 'x} @file_id:{*'
|
||||
ids = ['adv1', 'benign2', 'brace3']
|
||||
embeddings = [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]]
|
||||
metadatas = [{'file_id': injection_fid}, {'file_id': 'plainB'}, {'file_id': brace_fid}]
|
||||
documents = ['payload row content', 'unrelated benign content', 'brace row content']
|
||||
await db.add_embeddings(collection, ids, embeddings, metadatas, documents)
|
||||
await _poll_until(lambda: db.list_by_filter(collection, limit=10), lambda r: r[1] >= 3)
|
||||
|
||||
# Exact-match on the crafted file_id returns ONLY its own row.
|
||||
items, total = await db.list_by_filter(collection, filter={'file_id': injection_fid})
|
||||
assert total == 1
|
||||
assert {it['id'] for it in items} == {'adv1'}
|
||||
|
||||
# A query_text packed with FT operators must not raise and must not match
|
||||
# the benign row (escaped to literal terms, none of which it contains).
|
||||
result = await db.search(
|
||||
collection,
|
||||
[0.0, 0.0, 0.0, 0.0],
|
||||
k=5,
|
||||
search_type='full_text',
|
||||
query_text='@document:{*} | -()~ "evil"',
|
||||
)
|
||||
assert 'benign2' not in result['ids'][0]
|
||||
|
||||
# The brace/star-bearing file_id is encoded, so it round-trips: exact match
|
||||
# returns ONLY its own row and never widens. No RequestError is raised.
|
||||
b_items, b_total = await db.list_by_filter(collection, filter={'file_id': brace_fid})
|
||||
assert b_total == 1
|
||||
assert {it['id'] for it in b_items} == {'brace3'}
|
||||
|
||||
# And deletion by that file_id removes exactly its own row.
|
||||
deleted = await db.delete_by_filter(collection, filter={'file_id': brace_fid})
|
||||
assert deleted == 1
|
||||
@@ -0,0 +1,329 @@
|
||||
"""Integration tests for LangBot Box.
|
||||
|
||||
These tests verify the end-to-end behavior of the Box sandbox execution
|
||||
system. Tests decorated with ``requires_container`` need a real container
|
||||
runtime (Podman or Docker) and are skipped otherwise.
|
||||
|
||||
CI only runs ``tests/unit_tests/``, so these tests never execute in the
|
||||
CI pipeline. Run them locally with::
|
||||
|
||||
pytest tests/integration_tests/ -v
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import shutil
|
||||
import socket
|
||||
import subprocess
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from langbot.pkg.box.service import BoxService
|
||||
from langbot_plugin.box.backend import BaseSandboxBackend
|
||||
from langbot_plugin.box.client import ActionRPCBoxClient
|
||||
from langbot_plugin.box.errors import BoxBackendUnavailableError
|
||||
from langbot_plugin.box.models import BoxExecutionStatus, BoxNetworkMode, BoxSpec
|
||||
from langbot_plugin.box.runtime import BoxRuntime
|
||||
from langbot_plugin.box.server import BoxServerHandler
|
||||
|
||||
import langbot_plugin.api.entities.builtin.pipeline.query as pipeline_query
|
||||
|
||||
_logger = logging.getLogger('test.box.integration')
|
||||
|
||||
# Default image for integration tests — small and fast to pull.
|
||||
_TEST_IMAGE = 'alpine:latest'
|
||||
|
||||
|
||||
# ── Skip helpers ──────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def _has_container_runtime() -> bool:
|
||||
for cmd in ('podman', 'docker'):
|
||||
if shutil.which(cmd) is None:
|
||||
continue
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[cmd, 'info'],
|
||||
capture_output=True,
|
||||
timeout=10,
|
||||
)
|
||||
if result.returncode == 0:
|
||||
return True
|
||||
except Exception:
|
||||
continue
|
||||
return False
|
||||
|
||||
|
||||
def _can_open_test_socket() -> bool:
|
||||
try:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
except OSError:
|
||||
return False
|
||||
sock.close()
|
||||
return True
|
||||
|
||||
|
||||
requires_container = pytest.mark.skipif(
|
||||
not _has_container_runtime(),
|
||||
reason='no container runtime (podman/docker) available',
|
||||
)
|
||||
|
||||
requires_socket = pytest.mark.skipif(
|
||||
not _can_open_test_socket(),
|
||||
reason='local test environment does not permit opening TCP sockets',
|
||||
)
|
||||
|
||||
|
||||
# ── Helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class _QueueConnection:
|
||||
"""In-process Connection backed by asyncio Queues — no real IO."""
|
||||
|
||||
def __init__(self, rx: asyncio.Queue[str], tx: asyncio.Queue[str]):
|
||||
self._rx = rx
|
||||
self._tx = tx
|
||||
|
||||
async def send(self, message: str) -> None:
|
||||
await self._tx.put(message)
|
||||
|
||||
async def receive(self) -> str:
|
||||
return await self._rx.get()
|
||||
|
||||
async def close(self) -> None:
|
||||
pass
|
||||
|
||||
|
||||
async def _make_rpc_pair(runtime: BoxRuntime):
|
||||
"""Create an in-process (ActionRPCBoxClient, server_task, client_task) connected via queues."""
|
||||
from langbot_plugin.runtime.io.handler import Handler
|
||||
|
||||
c2s: asyncio.Queue[str] = asyncio.Queue()
|
||||
s2c: asyncio.Queue[str] = asyncio.Queue()
|
||||
client_conn = _QueueConnection(rx=s2c, tx=c2s)
|
||||
server_conn = _QueueConnection(rx=c2s, tx=s2c)
|
||||
|
||||
server_handler = BoxServerHandler(server_conn, runtime)
|
||||
server_task = asyncio.create_task(server_handler.run())
|
||||
|
||||
client_handler = Handler.__new__(Handler)
|
||||
Handler.__init__(client_handler, client_conn)
|
||||
client_task = asyncio.create_task(client_handler.run())
|
||||
|
||||
client = ActionRPCBoxClient(logger=_logger)
|
||||
client.set_handler(client_handler)
|
||||
|
||||
return client, server_task, client_task
|
||||
|
||||
|
||||
# ── Fixtures ──────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def box_client():
|
||||
"""Yield an ActionRPCBoxClient backed by a real BoxRuntime via in-process RPC."""
|
||||
runtime = BoxRuntime(logger=_logger)
|
||||
await runtime.initialize()
|
||||
client, server_task, client_task = await _make_rpc_pair(runtime)
|
||||
yield client
|
||||
server_task.cancel()
|
||||
client_task.cancel()
|
||||
await runtime.shutdown()
|
||||
|
||||
|
||||
# ── 1. Simple command execution ───────────────────────────────────────
|
||||
|
||||
|
||||
@requires_container
|
||||
@requires_socket
|
||||
@pytest.mark.asyncio
|
||||
async def test_exec_simple_command(box_client: ActionRPCBoxClient):
|
||||
"""Box starts a simple command and returns stdout."""
|
||||
spec = BoxSpec(
|
||||
cmd='echo hello-box',
|
||||
session_id='int-simple',
|
||||
workdir='/tmp',
|
||||
image=_TEST_IMAGE,
|
||||
)
|
||||
result = await box_client.execute(spec)
|
||||
|
||||
assert result.status == BoxExecutionStatus.COMPLETED
|
||||
assert result.exit_code == 0
|
||||
assert 'hello-box' in result.stdout
|
||||
|
||||
|
||||
# ── 2. Session file persistence ───────────────────────────────────────
|
||||
|
||||
|
||||
@requires_container
|
||||
@requires_socket
|
||||
@pytest.mark.asyncio
|
||||
async def test_session_persists_files(box_client: ActionRPCBoxClient):
|
||||
"""Write a file in one exec, read it back in a second exec on the same session."""
|
||||
sid = 'int-persist'
|
||||
|
||||
write_result = await box_client.execute(
|
||||
BoxSpec(
|
||||
cmd='echo "hello from file" > /tmp/testfile.txt',
|
||||
session_id=sid,
|
||||
workdir='/tmp',
|
||||
image=_TEST_IMAGE,
|
||||
)
|
||||
)
|
||||
assert write_result.exit_code == 0
|
||||
|
||||
read_result = await box_client.execute(
|
||||
BoxSpec(
|
||||
cmd='cat /tmp/testfile.txt',
|
||||
session_id=sid,
|
||||
workdir='/tmp',
|
||||
image=_TEST_IMAGE,
|
||||
)
|
||||
)
|
||||
assert read_result.exit_code == 0
|
||||
assert 'hello from file' in read_result.stdout
|
||||
|
||||
|
||||
# ── 3. Timeout handling ───────────────────────────────────────────────
|
||||
|
||||
|
||||
@requires_container
|
||||
@requires_socket
|
||||
@pytest.mark.asyncio
|
||||
async def test_timeout_kills_command(box_client: ActionRPCBoxClient):
|
||||
"""A long-running command is killed after timeout_sec."""
|
||||
session_id = 'int-timeout'
|
||||
spec = BoxSpec(
|
||||
cmd='sleep 120',
|
||||
session_id=session_id,
|
||||
workdir='/tmp',
|
||||
timeout_sec=3,
|
||||
image=_TEST_IMAGE,
|
||||
)
|
||||
result = await box_client.execute(spec)
|
||||
|
||||
assert result.status == BoxExecutionStatus.TIMED_OUT
|
||||
assert result.exit_code is None
|
||||
|
||||
sessions = await box_client.get_sessions()
|
||||
assert all(session['session_id'] != session_id for session in sessions)
|
||||
|
||||
|
||||
# ── 4. Network isolation ─────────────────────────────────────────────
|
||||
|
||||
|
||||
@requires_container
|
||||
@requires_socket
|
||||
@pytest.mark.asyncio
|
||||
async def test_offline_cannot_reach_network(box_client: ActionRPCBoxClient):
|
||||
"""With network=OFF the sandbox cannot reach the internet."""
|
||||
spec = BoxSpec(
|
||||
cmd='wget -q -O /dev/null --timeout=3 http://1.1.1.1 2>&1; exit $?',
|
||||
session_id='int-offline',
|
||||
workdir='/tmp',
|
||||
network=BoxNetworkMode.OFF,
|
||||
image=_TEST_IMAGE,
|
||||
)
|
||||
result = await box_client.execute(spec)
|
||||
|
||||
assert result.exit_code != 0
|
||||
|
||||
|
||||
# ── 5. Backend unavailable ───────────────────────────────────────────
|
||||
|
||||
|
||||
class _UnavailableBackend(BaseSandboxBackend):
|
||||
"""A backend that always reports itself as unavailable."""
|
||||
|
||||
name = 'unavailable'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(logging.getLogger('test'))
|
||||
|
||||
async def is_available(self) -> bool:
|
||||
return False
|
||||
|
||||
async def start_session(self, spec):
|
||||
raise NotImplementedError
|
||||
|
||||
async def exec(self, session, spec):
|
||||
raise NotImplementedError
|
||||
|
||||
async def stop_session(self, session):
|
||||
pass
|
||||
|
||||
|
||||
@requires_socket
|
||||
@pytest.mark.asyncio
|
||||
async def test_backend_unavailable_returns_error():
|
||||
"""When no backend is available the full RPC path returns BoxBackendUnavailableError."""
|
||||
runtime = BoxRuntime(logger=_logger, backends=[_UnavailableBackend()])
|
||||
await runtime.initialize()
|
||||
client, server_task, client_task = await _make_rpc_pair(runtime)
|
||||
try:
|
||||
spec = BoxSpec(
|
||||
cmd='echo hello',
|
||||
session_id='int-no-backend',
|
||||
workdir='/tmp',
|
||||
)
|
||||
with pytest.raises(BoxBackendUnavailableError):
|
||||
await client.execute(spec)
|
||||
finally:
|
||||
server_task.cancel()
|
||||
client_task.cancel()
|
||||
await runtime.shutdown()
|
||||
|
||||
|
||||
# ── 6. Full service-to-runtime path ──────────────────────────────────
|
||||
|
||||
|
||||
@requires_container
|
||||
@requires_socket
|
||||
@pytest.mark.asyncio
|
||||
async def test_full_service_to_remote_runtime(tmp_path):
|
||||
"""BoxService -> ActionRPCBoxClient -> RPC -> BoxRuntime -> real backend."""
|
||||
runtime = BoxRuntime(logger=_logger)
|
||||
await runtime.initialize()
|
||||
client, server_task, client_task = await _make_rpc_pair(runtime)
|
||||
try:
|
||||
host_dir = tmp_path / 'workspace'
|
||||
host_dir.mkdir()
|
||||
|
||||
mock_ap = SimpleNamespace(
|
||||
logger=_logger,
|
||||
instance_config=SimpleNamespace(
|
||||
data={
|
||||
'box': {
|
||||
'backend': 'local',
|
||||
'runtime': {'endpoint': ''},
|
||||
'local': {
|
||||
'profile': 'default',
|
||||
'allowed_mount_roots': [str(tmp_path)],
|
||||
'default_workspace': str(host_dir),
|
||||
},
|
||||
'e2b': {'api_key': '', 'api_url': '', 'template': ''},
|
||||
}
|
||||
}
|
||||
),
|
||||
)
|
||||
|
||||
service = BoxService(mock_ap, client=client)
|
||||
await service.initialize()
|
||||
|
||||
query = pipeline_query.Query.model_construct(query_id=42)
|
||||
result = await service.execute_tool(
|
||||
{'command': 'echo service-path'},
|
||||
query,
|
||||
)
|
||||
|
||||
assert result['ok'] is True
|
||||
assert result['status'] == 'completed'
|
||||
assert 'service-path' in result['stdout']
|
||||
assert result['session_id'] == 'query_42'
|
||||
finally:
|
||||
server_task.cancel()
|
||||
client_task.cancel()
|
||||
await runtime.shutdown()
|
||||
@@ -0,0 +1,368 @@
|
||||
"""Integration tests for Box MCP-related features.
|
||||
|
||||
These tests verify managed process lifecycle, WebSocket stdio attach,
|
||||
session cleanup, and the single-session query API using a real container
|
||||
runtime.
|
||||
|
||||
CI only runs ``tests/unit_tests/``, so these tests never execute in the
|
||||
CI pipeline. Run them locally with::
|
||||
|
||||
pytest tests/integration_tests/box/test_box_mcp_integration.py -v
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import shutil
|
||||
import socket
|
||||
import subprocess
|
||||
|
||||
import aiohttp
|
||||
import pytest
|
||||
from aiohttp.test_utils import TestServer
|
||||
|
||||
from langbot_plugin.box.client import ActionRPCBoxClient
|
||||
from langbot_plugin.box.errors import BoxManagedProcessNotFoundError, BoxSessionNotFoundError
|
||||
from langbot_plugin.box.models import BoxManagedProcessSpec, BoxManagedProcessStatus, BoxSpec
|
||||
from langbot_plugin.box.runtime import BoxRuntime
|
||||
from langbot_plugin.box.server import BoxServerHandler, create_ws_relay_app
|
||||
|
||||
_logger = logging.getLogger('test.box.mcp_integration')
|
||||
|
||||
_TEST_IMAGE = 'alpine:latest'
|
||||
|
||||
|
||||
# ── Skip helpers ──────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def _has_container_runtime() -> bool:
|
||||
for cmd in ('podman', 'docker'):
|
||||
if shutil.which(cmd) is None:
|
||||
continue
|
||||
try:
|
||||
result = subprocess.run([cmd, 'info'], capture_output=True, timeout=10)
|
||||
if result.returncode == 0:
|
||||
return True
|
||||
except Exception:
|
||||
continue
|
||||
return False
|
||||
|
||||
|
||||
def _can_open_test_socket() -> bool:
|
||||
try:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
except OSError:
|
||||
return False
|
||||
sock.close()
|
||||
return True
|
||||
|
||||
|
||||
requires_container = pytest.mark.skipif(
|
||||
not _has_container_runtime(),
|
||||
reason='no container runtime (podman/docker) available',
|
||||
)
|
||||
|
||||
requires_socket = pytest.mark.skipif(
|
||||
not _can_open_test_socket(),
|
||||
reason='local test environment does not permit opening TCP sockets',
|
||||
)
|
||||
|
||||
|
||||
# ── Helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class _QueueConnection:
|
||||
"""In-process Connection backed by asyncio Queues — no real IO."""
|
||||
|
||||
def __init__(self, rx: asyncio.Queue[str], tx: asyncio.Queue[str]):
|
||||
self._rx = rx
|
||||
self._tx = tx
|
||||
|
||||
async def send(self, message: str) -> None:
|
||||
await self._tx.put(message)
|
||||
|
||||
async def receive(self) -> str:
|
||||
return await self._rx.get()
|
||||
|
||||
async def close(self) -> None:
|
||||
pass
|
||||
|
||||
|
||||
async def _make_rpc_pair(runtime: BoxRuntime):
|
||||
"""Create an in-process RPC pair connected via queues."""
|
||||
from langbot_plugin.runtime.io.handler import Handler
|
||||
|
||||
c2s: asyncio.Queue[str] = asyncio.Queue()
|
||||
s2c: asyncio.Queue[str] = asyncio.Queue()
|
||||
client_conn = _QueueConnection(rx=s2c, tx=c2s)
|
||||
server_conn = _QueueConnection(rx=c2s, tx=s2c)
|
||||
|
||||
server_handler = BoxServerHandler(server_conn, runtime)
|
||||
server_task = asyncio.create_task(server_handler.run())
|
||||
|
||||
client_handler = Handler.__new__(Handler)
|
||||
Handler.__init__(client_handler, client_conn)
|
||||
client_task = asyncio.create_task(client_handler.run())
|
||||
|
||||
client = ActionRPCBoxClient(logger=_logger)
|
||||
client.set_handler(client_handler)
|
||||
|
||||
return client, server_task, client_task
|
||||
|
||||
|
||||
# ── Fixtures ──────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def box_server():
|
||||
"""Yield a (ws_relay_url, ActionRPCBoxClient) backed by a real BoxRuntime."""
|
||||
runtime = BoxRuntime(logger=_logger)
|
||||
await runtime.initialize()
|
||||
|
||||
# Start ws relay for managed process attach
|
||||
ws_app = create_ws_relay_app(runtime)
|
||||
ws_server = TestServer(ws_app)
|
||||
await ws_server.start_server()
|
||||
|
||||
client, server_task, client_task = await _make_rpc_pair(runtime)
|
||||
|
||||
ws_relay_url = str(ws_server.make_url(''))
|
||||
yield ws_relay_url, client
|
||||
|
||||
server_task.cancel()
|
||||
client_task.cancel()
|
||||
await runtime.shutdown()
|
||||
await ws_server.close()
|
||||
|
||||
|
||||
# ── 1. Managed process lifecycle ─────────────────────────────────────
|
||||
|
||||
|
||||
@requires_container
|
||||
@requires_socket
|
||||
@pytest.mark.asyncio
|
||||
async def test_managed_process_start_and_query(box_server):
|
||||
"""Start a managed process and query its status."""
|
||||
ws_relay_url, client = box_server
|
||||
|
||||
# Create session
|
||||
spec = BoxSpec(
|
||||
cmd='',
|
||||
session_id='mcp-int-lifecycle',
|
||||
workdir='/tmp',
|
||||
image=_TEST_IMAGE,
|
||||
)
|
||||
await client.create_session(spec)
|
||||
|
||||
# Start a managed process that stays alive
|
||||
proc_spec = BoxManagedProcessSpec(
|
||||
command='sh',
|
||||
args=['-c', 'while true; do sleep 1; done'],
|
||||
cwd='/tmp',
|
||||
)
|
||||
info = await client.start_managed_process('mcp-int-lifecycle', proc_spec)
|
||||
assert info.status == BoxManagedProcessStatus.RUNNING
|
||||
|
||||
# Query it
|
||||
info2 = await client.get_managed_process('mcp-int-lifecycle')
|
||||
assert info2.status == BoxManagedProcessStatus.RUNNING
|
||||
assert info2.command == 'sh'
|
||||
|
||||
# Stop only the managed process while keeping the session available
|
||||
await client.stop_managed_process('mcp-int-lifecycle')
|
||||
with pytest.raises(BoxManagedProcessNotFoundError):
|
||||
await client.get_managed_process('mcp-int-lifecycle')
|
||||
session_info = await client.get_session('mcp-int-lifecycle')
|
||||
assert session_info['session_id'] == 'mcp-int-lifecycle'
|
||||
|
||||
# Cleanup
|
||||
await client.delete_session('mcp-int-lifecycle')
|
||||
|
||||
|
||||
# ── 2. WebSocket stdio attach ────────────────────────────────────────
|
||||
|
||||
|
||||
@requires_container
|
||||
@requires_socket
|
||||
@pytest.mark.asyncio
|
||||
async def test_ws_stdio_attach_echo(box_server):
|
||||
"""Attach to a managed process via WebSocket and verify bidirectional IO."""
|
||||
ws_relay_url, client = box_server
|
||||
|
||||
spec = BoxSpec(
|
||||
cmd='',
|
||||
session_id='mcp-int-ws',
|
||||
workdir='/tmp',
|
||||
image=_TEST_IMAGE,
|
||||
)
|
||||
await client.create_session(spec)
|
||||
|
||||
# Start a cat process (echoes stdin to stdout)
|
||||
proc_spec = BoxManagedProcessSpec(
|
||||
command='cat',
|
||||
args=[],
|
||||
cwd='/tmp',
|
||||
)
|
||||
await client.start_managed_process('mcp-int-ws', proc_spec)
|
||||
|
||||
# Connect via WebSocket (ws relay)
|
||||
ws_url = client.get_managed_process_websocket_url('mcp-int-ws', ws_relay_url)
|
||||
session = aiohttp.ClientSession()
|
||||
try:
|
||||
async with session.ws_connect(ws_url) as ws:
|
||||
# Send a line
|
||||
await ws.send_str('hello from test')
|
||||
|
||||
# Expect to receive it back (cat echoes)
|
||||
msg = await asyncio.wait_for(ws.receive(), timeout=5)
|
||||
assert msg.type == aiohttp.WSMsgType.TEXT
|
||||
assert 'hello from test' in msg.data
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
await client.delete_session('mcp-int-ws')
|
||||
|
||||
|
||||
# ── 3. Session cleanup removes container ─────────────────────────────
|
||||
|
||||
|
||||
@requires_container
|
||||
@requires_socket
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_session_cleans_up(box_server):
|
||||
"""After deleting a session, it should no longer exist."""
|
||||
ws_relay_url, client = box_server
|
||||
|
||||
spec = BoxSpec(
|
||||
cmd='',
|
||||
session_id='mcp-int-cleanup',
|
||||
workdir='/tmp',
|
||||
image=_TEST_IMAGE,
|
||||
)
|
||||
await client.create_session(spec)
|
||||
|
||||
# Start a process
|
||||
proc_spec = BoxManagedProcessSpec(
|
||||
command='sleep',
|
||||
args=['3600'],
|
||||
cwd='/tmp',
|
||||
)
|
||||
await client.start_managed_process('mcp-int-cleanup', proc_spec)
|
||||
|
||||
# Delete
|
||||
await client.delete_session('mcp-int-cleanup')
|
||||
|
||||
# Session should be gone
|
||||
with pytest.raises(BoxSessionNotFoundError):
|
||||
await client.get_session('mcp-int-cleanup')
|
||||
|
||||
|
||||
# ── 4. GET session details ────────────────────────────────────────
|
||||
|
||||
|
||||
@requires_container
|
||||
@requires_socket
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_session_returns_details(box_server):
|
||||
"""Get single session returns session details and managed process info."""
|
||||
ws_relay_url, client = box_server
|
||||
|
||||
spec = BoxSpec(
|
||||
cmd='',
|
||||
session_id='mcp-int-get',
|
||||
workdir='/tmp',
|
||||
image=_TEST_IMAGE,
|
||||
)
|
||||
await client.create_session(spec)
|
||||
|
||||
# Query without managed process
|
||||
info = await client.get_session('mcp-int-get')
|
||||
assert info['session_id'] == 'mcp-int-get'
|
||||
assert info['image'] == _TEST_IMAGE
|
||||
assert 'managed_process' not in info
|
||||
|
||||
# Start a process and query again
|
||||
proc_spec = BoxManagedProcessSpec(
|
||||
command='sleep',
|
||||
args=['3600'],
|
||||
cwd='/tmp',
|
||||
)
|
||||
await client.start_managed_process('mcp-int-get', proc_spec)
|
||||
|
||||
info2 = await client.get_session('mcp-int-get')
|
||||
assert info2['session_id'] == 'mcp-int-get'
|
||||
assert 'managed_process' in info2
|
||||
assert info2['managed_process']['status'] == BoxManagedProcessStatus.RUNNING.value
|
||||
|
||||
await client.delete_session('mcp-int-get')
|
||||
|
||||
|
||||
# ── 5. Process exit detected ────────────────────────────────────────
|
||||
|
||||
|
||||
@requires_container
|
||||
@requires_socket
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_exit_detected(box_server):
|
||||
"""When a managed process exits, its status should reflect EXITED."""
|
||||
ws_relay_url, client = box_server
|
||||
|
||||
spec = BoxSpec(
|
||||
cmd='',
|
||||
session_id='mcp-int-exit',
|
||||
workdir='/tmp',
|
||||
image=_TEST_IMAGE,
|
||||
)
|
||||
await client.create_session(spec)
|
||||
|
||||
# Start a process that exits immediately
|
||||
proc_spec = BoxManagedProcessSpec(
|
||||
command='sh',
|
||||
args=['-c', 'echo done && exit 0'],
|
||||
cwd='/tmp',
|
||||
)
|
||||
await client.start_managed_process('mcp-int-exit', proc_spec)
|
||||
|
||||
# Wait a bit for process to exit
|
||||
await asyncio.sleep(2)
|
||||
|
||||
info = await client.get_managed_process('mcp-int-exit')
|
||||
assert info.status == BoxManagedProcessStatus.EXITED
|
||||
assert info.exit_code == 0
|
||||
|
||||
await client.delete_session('mcp-int-exit')
|
||||
|
||||
|
||||
# ── 6. Instance ID orphan cleanup ───────────────────────────────────
|
||||
|
||||
|
||||
@requires_container
|
||||
@requires_socket
|
||||
@pytest.mark.asyncio
|
||||
async def test_orphan_cleanup_preserves_own_containers(box_server):
|
||||
"""Orphan cleanup should not remove containers belonging to the current instance."""
|
||||
ws_relay_url, client = box_server
|
||||
|
||||
# Create a session (container gets current instance ID label)
|
||||
spec = BoxSpec(
|
||||
cmd='',
|
||||
session_id='mcp-int-orphan',
|
||||
workdir='/tmp',
|
||||
image=_TEST_IMAGE,
|
||||
)
|
||||
await client.create_session(spec)
|
||||
|
||||
# Verify session exists
|
||||
sessions = await client.get_sessions()
|
||||
assert any(s['session_id'] == 'mcp-int-orphan' for s in sessions)
|
||||
|
||||
# Trigger status check (which doesn't clean up own containers)
|
||||
status = await client.get_status()
|
||||
assert status['active_sessions'] >= 1
|
||||
|
||||
# Our session should still exist
|
||||
sessions = await client.get_sessions()
|
||||
assert any(s['session_id'] == 'mcp-int-orphan' for s in sessions)
|
||||
|
||||
await client.delete_session('mcp-int-orphan')
|
||||
@@ -0,0 +1,132 @@
|
||||
"""End-to-end test: boot the real MCPMount on a port and drive it with an MCP client.
|
||||
|
||||
Exercises the ASGI dispatcher (auth + /mcp routing), the FastMCP streamable-HTTP
|
||||
transport, and a real tool call against the (mocked) service layer.
|
||||
|
||||
Run: uv run --no-sync python tests/manual/mcp_smoke.py
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import contextlib
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from hypercorn.asyncio import serve
|
||||
from hypercorn.config import Config
|
||||
from quart import Quart
|
||||
|
||||
from langbot.pkg.api.mcp.mount import MCPMount
|
||||
|
||||
PORT = 5399
|
||||
GLOBAL_KEY = 'test-global-key-123'
|
||||
|
||||
|
||||
def build_ap() -> SimpleNamespace:
|
||||
ap = SimpleNamespace()
|
||||
ap.instance_config = SimpleNamespace(
|
||||
data={'api': {'global_api_key': GLOBAL_KEY}, 'system': {'edition': 'community', 'instance_id': 'inst-1'}}
|
||||
)
|
||||
ap.ver_mgr = SimpleNamespace(get_current_version=lambda: '4.5.0-test')
|
||||
ap.logger = SimpleNamespace(info=print, error=print, warning=print)
|
||||
|
||||
# API key verification: reuse real logic shape (global key match)
|
||||
async def verify_api_key(key: str) -> bool:
|
||||
return bool(key) and key == GLOBAL_KEY
|
||||
|
||||
ap.apikey_service = SimpleNamespace(verify_api_key=verify_api_key)
|
||||
ap.bot_service = SimpleNamespace(
|
||||
get_bots=AsyncMock(return_value=[{'uuid': 'bot-1', 'name': 'Demo Bot', 'adapter': 'telegram'}])
|
||||
)
|
||||
ap.pipeline_service = SimpleNamespace(get_pipelines=AsyncMock(return_value=[{'uuid': 'pl-1', 'name': 'default'}]))
|
||||
ap.llm_model_service = SimpleNamespace(get_llm_models=AsyncMock(return_value=[]))
|
||||
ap.embedding_models_service = SimpleNamespace(get_embedding_models=AsyncMock(return_value=[]))
|
||||
ap.provider_service = SimpleNamespace(get_providers=AsyncMock(return_value=[]))
|
||||
ap.knowledge_service = SimpleNamespace(get_knowledge_bases=AsyncMock(return_value=[]))
|
||||
ap.mcp_service = SimpleNamespace(get_mcp_servers=AsyncMock(return_value=[]))
|
||||
ap.skill_service = SimpleNamespace(list_skills=AsyncMock(return_value=[{'name': 'demo-skill'}]))
|
||||
return ap
|
||||
|
||||
|
||||
async def run_server(mount: MCPMount, shutdown: asyncio.Event) -> None:
|
||||
quart_app = Quart(__name__)
|
||||
|
||||
@quart_app.route('/healthz')
|
||||
async def healthz():
|
||||
return {'code': 0, 'msg': 'ok'}
|
||||
|
||||
config = Config()
|
||||
config.bind = [f'127.0.0.1:{PORT}']
|
||||
config.accesslog = None
|
||||
asgi = mount.wrap(quart_app)
|
||||
await serve(asgi, config, shutdown_trigger=shutdown.wait)
|
||||
|
||||
|
||||
async def main() -> int:
|
||||
from mcp.client.session import ClientSession
|
||||
from mcp.client.streamable_http import streamablehttp_client
|
||||
|
||||
ap = build_ap()
|
||||
mount = MCPMount(ap)
|
||||
await mount.start_session_manager()
|
||||
|
||||
shutdown = asyncio.Event()
|
||||
server_task = asyncio.create_task(run_server(mount, shutdown))
|
||||
await asyncio.sleep(1.0) # let the server bind
|
||||
|
||||
url = f'http://127.0.0.1:{PORT}/mcp'
|
||||
failures = []
|
||||
|
||||
# 1. Unauthorized request is rejected.
|
||||
import httpx
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
r = await client.post(url, json={'jsonrpc': '2.0', 'id': 1, 'method': 'ping'})
|
||||
if r.status_code != 401:
|
||||
failures.append(f'expected 401 without key, got {r.status_code}')
|
||||
else:
|
||||
print('PASS: unauthorized request rejected (401)')
|
||||
|
||||
# 2. Authorized MCP session: list tools + call two.
|
||||
headers = {'X-API-Key': GLOBAL_KEY}
|
||||
async with streamablehttp_client(url, headers=headers) as (read, write, _):
|
||||
async with ClientSession(read, write) as session:
|
||||
await session.initialize()
|
||||
tools = await session.list_tools()
|
||||
names = [t.name for t in tools.tools]
|
||||
print(f'PASS: listed {len(names)} tools')
|
||||
for required in ('list_bots', 'get_system_info', 'list_skills'):
|
||||
if required not in names:
|
||||
failures.append(f'missing tool {required}')
|
||||
|
||||
res = await session.call_tool('list_bots', {})
|
||||
text = res.content[0].text if res.content else ''
|
||||
if 'Demo Bot' not in text:
|
||||
failures.append(f'list_bots did not return expected data: {text!r}')
|
||||
else:
|
||||
print('PASS: list_bots returned bot data')
|
||||
|
||||
res2 = await session.call_tool('get_system_info', {})
|
||||
text2 = res2.content[0].text if res2.content else ''
|
||||
if '4.5.0-test' not in text2:
|
||||
failures.append(f'get_system_info wrong: {text2!r}')
|
||||
else:
|
||||
print('PASS: get_system_info returned version')
|
||||
|
||||
shutdown.set()
|
||||
with contextlib.suppress(Exception):
|
||||
await asyncio.wait_for(server_task, timeout=5)
|
||||
await mount.stop_session_manager()
|
||||
|
||||
if failures:
|
||||
print('\nFAILURES:')
|
||||
for f in failures:
|
||||
print(' -', f)
|
||||
return 1
|
||||
print('\nALL MCP SMOKE CHECKS PASSED')
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(asyncio.run(main()))
|
||||
@@ -3,4 +3,4 @@ Smoke tests package.
|
||||
|
||||
Smoke tests verify basic functionality works without testing edge cases.
|
||||
Run with: uv run pytest tests/smoke/ -q
|
||||
"""
|
||||
"""
|
||||
|
||||
@@ -39,19 +39,19 @@ class TestFakeMessageFlow:
|
||||
assert app.instance_config is not None
|
||||
|
||||
# Verify default config
|
||||
assert app.instance_config.data["command"]["prefix"] == ["/", "!"]
|
||||
assert app.instance_config.data["command"]["enable"] is True
|
||||
assert app.instance_config.data['command']['prefix'] == ['/', '!']
|
||||
assert app.instance_config.data['command']['enable'] is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fake_provider_returns_text(self):
|
||||
"""Test FakeProvider returns configured response."""
|
||||
provider = FakeProvider(default_response="test response")
|
||||
provider = FakeProvider(default_response='test response')
|
||||
|
||||
# Create mock model with provider
|
||||
model = fake_model(provider=provider)
|
||||
|
||||
# Create a simple query
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
|
||||
# Simulate invoke
|
||||
result = await provider.invoke_llm(
|
||||
@@ -63,15 +63,15 @@ class TestFakeMessageFlow:
|
||||
)
|
||||
|
||||
assert result is not None
|
||||
assert result.role == "assistant"
|
||||
assert result.content == "test response"
|
||||
assert result.role == 'assistant'
|
||||
assert result.content == 'test response'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fake_provider_pong(self):
|
||||
"""Test FakeProvider returns LANGBOT_FAKE_PONG marker."""
|
||||
provider = fake_provider_pong()
|
||||
model = fake_model(provider=provider)
|
||||
query = text_query("ping")
|
||||
query = text_query('ping')
|
||||
|
||||
result = await provider.invoke_llm(
|
||||
query=query,
|
||||
@@ -86,9 +86,9 @@ class TestFakeMessageFlow:
|
||||
@pytest.mark.asyncio
|
||||
async def test_fake_provider_streaming(self):
|
||||
"""Test FakeProvider streaming response."""
|
||||
provider = FakeProvider().returns_streaming(["Hello", " World"])
|
||||
provider = FakeProvider().returns_streaming(['Hello', ' World'])
|
||||
model = fake_model(provider=provider)
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
|
||||
chunks = []
|
||||
# invoke_llm_stream returns an async generator, don't await it
|
||||
@@ -102,8 +102,8 @@ class TestFakeMessageFlow:
|
||||
chunks.append(chunk)
|
||||
|
||||
assert len(chunks) == 2
|
||||
assert chunks[0].content == "Hello"
|
||||
assert chunks[1].content == " World"
|
||||
assert chunks[0].content == 'Hello'
|
||||
assert chunks[1].content == ' World'
|
||||
assert chunks[1].is_final is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -111,9 +111,9 @@ class TestFakeMessageFlow:
|
||||
"""Test FakeProvider simulates timeout error."""
|
||||
provider = FakeProvider().timeout()
|
||||
model = fake_model(provider=provider)
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
|
||||
with pytest.raises(TimeoutError, match="Provider timeout"):
|
||||
with pytest.raises(TimeoutError, match='Provider timeout'):
|
||||
await provider.invoke_llm(
|
||||
query=query,
|
||||
model=model,
|
||||
@@ -127,9 +127,9 @@ class TestFakeMessageFlow:
|
||||
"""Test FakeProvider simulates rate limit error."""
|
||||
provider = FakeProvider().rate_limit()
|
||||
model = fake_model(provider=provider)
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
|
||||
with pytest.raises(Exception, match="Rate limit exceeded"):
|
||||
with pytest.raises(Exception, match='Rate limit exceeded'):
|
||||
await provider.invoke_llm(
|
||||
query=query,
|
||||
model=model,
|
||||
@@ -142,34 +142,34 @@ class TestFakeMessageFlow:
|
||||
async def test_fake_provider_captures_requests(self):
|
||||
"""Test FakeProvider captures request arguments."""
|
||||
provider = FakeProvider()
|
||||
model = fake_model(name="gpt-4", provider=provider)
|
||||
query = text_query("hello")
|
||||
model = fake_model(name='gpt-4', provider=provider)
|
||||
query = text_query('hello')
|
||||
|
||||
await provider.invoke_llm(
|
||||
query=query,
|
||||
model=model,
|
||||
messages=[{"role": "user", "content": "hello"}],
|
||||
funcs=[{"name": "test_func"}],
|
||||
extra_args={"temperature": 0.7},
|
||||
messages=[{'role': 'user', 'content': 'hello'}],
|
||||
funcs=[{'name': 'test_func'}],
|
||||
extra_args={'temperature': 0.7},
|
||||
)
|
||||
|
||||
captured = provider.get_captured_requests()
|
||||
assert len(captured) == 1
|
||||
assert captured[0]["model"] == "gpt-4"
|
||||
assert captured[0]["messages"] == [{"role": "user", "content": "hello"}]
|
||||
assert captured[0]["funcs"] == [{"name": "test_func"}]
|
||||
assert captured[0]["extra_args"] == {"temperature": 0.7}
|
||||
assert captured[0]['model'] == 'gpt-4'
|
||||
assert captured[0]['messages'] == [{'role': 'user', 'content': 'hello'}]
|
||||
assert captured[0]['funcs'] == [{'name': 'test_func'}]
|
||||
assert captured[0]['extra_args'] == {'temperature': 0.7}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fake_platform_capture_outbound(self):
|
||||
"""Test FakePlatform captures outbound messages."""
|
||||
platform = FakePlatform(bot_account_id="test-bot")
|
||||
query = text_query("hello")
|
||||
platform = FakePlatform(bot_account_id='test-bot')
|
||||
query = text_query('hello')
|
||||
|
||||
# Simulate sending reply
|
||||
from tests.factories.message import text_chain
|
||||
|
||||
reply_chain = text_chain("response text")
|
||||
reply_chain = text_chain('response text')
|
||||
event = query.message_event
|
||||
|
||||
await platform.reply_message(event, reply_chain, quote_origin=False)
|
||||
@@ -177,38 +177,38 @@ class TestFakeMessageFlow:
|
||||
# Verify captured
|
||||
outbound = platform.get_outbound_messages()
|
||||
assert len(outbound) == 1
|
||||
assert outbound[0]["type"] == "reply"
|
||||
assert outbound[0]["message"] == reply_chain
|
||||
assert outbound[0]['type'] == 'reply'
|
||||
assert outbound[0]['message'] == reply_chain
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fake_platform_friend_message(self):
|
||||
"""Test FakePlatform creates friend message events."""
|
||||
platform = FakePlatform(bot_account_id="test-bot")
|
||||
platform = FakePlatform(bot_account_id='test-bot')
|
||||
|
||||
event = platform.create_friend_message(
|
||||
text="hello bot",
|
||||
text='hello bot',
|
||||
sender_id=12345,
|
||||
nickname="TestUser",
|
||||
nickname='TestUser',
|
||||
)
|
||||
|
||||
assert event.type == "FriendMessage"
|
||||
assert event.type == 'FriendMessage'
|
||||
assert event.sender.id == 12345
|
||||
assert event.sender.nickname == "TestUser"
|
||||
assert str(event.message_chain) == "hello bot"
|
||||
assert event.sender.nickname == 'TestUser'
|
||||
assert str(event.message_chain) == 'hello bot'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fake_platform_group_message_with_mention(self):
|
||||
"""Test FakePlatform creates group message with @mention."""
|
||||
platform = FakePlatform(bot_account_id="test-bot")
|
||||
platform = FakePlatform(bot_account_id='test-bot')
|
||||
|
||||
event = platform.create_group_message(
|
||||
text="hello everyone",
|
||||
text='hello everyone',
|
||||
sender_id=12345,
|
||||
group_id=99999,
|
||||
mention_bot=True,
|
||||
)
|
||||
|
||||
assert event.type == "GroupMessage"
|
||||
assert event.type == 'GroupMessage'
|
||||
assert event.sender.id == 12345
|
||||
assert event.group.id == 99999
|
||||
|
||||
@@ -220,54 +220,57 @@ class TestFakeMessageFlow:
|
||||
async def test_query_factories_basic(self):
|
||||
"""Test basic query factory functions."""
|
||||
# Text query
|
||||
q1 = text_query("hello world")
|
||||
assert q1.launcher_type.value == "person"
|
||||
assert str(q1.message_chain) == "hello world"
|
||||
q1 = text_query('hello world')
|
||||
assert q1.launcher_type.value == 'person'
|
||||
assert str(q1.message_chain) == 'hello world'
|
||||
|
||||
# Group query
|
||||
from tests.factories import group_text_query
|
||||
q2 = group_text_query("hello group", group_id=88888)
|
||||
assert q2.launcher_type.value == "group"
|
||||
|
||||
q2 = group_text_query('hello group', group_id=88888)
|
||||
assert q2.launcher_type.value == 'group'
|
||||
assert q2.launcher_id == 88888
|
||||
|
||||
# Command query
|
||||
from tests.factories import command_query
|
||||
q3 = command_query("help", prefix="/")
|
||||
assert str(q3.message_chain) == "/help"
|
||||
|
||||
q3 = command_query('help', prefix='/')
|
||||
assert str(q3.message_chain) == '/help'
|
||||
|
||||
# Mention query
|
||||
from tests.factories import mention_query
|
||||
q4 = mention_query("hi", target="test-bot", group_id=77777)
|
||||
assert q4.launcher_type.value == "group"
|
||||
|
||||
q4 = mention_query('hi', target='test-bot', group_id=77777)
|
||||
assert q4.launcher_type.value == 'group'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fake_platform_send_failure(self):
|
||||
"""Test FakePlatform simulates send failure."""
|
||||
platform = FakePlatform().send_failure()
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
|
||||
from tests.factories.message import text_chain
|
||||
|
||||
with pytest.raises(Exception, match="Platform send failure"):
|
||||
with pytest.raises(Exception, match='Platform send failure'):
|
||||
await platform.reply_message(
|
||||
query.message_event,
|
||||
text_chain("response"),
|
||||
text_chain('response'),
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mock_platform_adapter(self):
|
||||
"""Test mock_platform_adapter helper."""
|
||||
platform = FakePlatform(bot_account_id="bot-123")
|
||||
platform = FakePlatform(bot_account_id='bot-123')
|
||||
adapter = mock_platform_adapter(platform)
|
||||
|
||||
assert adapter.bot_account_id == "bot-123"
|
||||
assert adapter.bot_account_id == 'bot-123'
|
||||
assert adapter._fake_platform is platform
|
||||
|
||||
# Test reply_message is wired
|
||||
from tests.factories.message import text_chain
|
||||
|
||||
query = text_query("test")
|
||||
await adapter.reply_message(query.message_event, text_chain("response"))
|
||||
query = text_query('test')
|
||||
await adapter.reply_message(query.message_event, text_chain('response'))
|
||||
|
||||
# Verify platform captured it
|
||||
assert len(platform.get_outbound_messages()) == 1
|
||||
@@ -293,18 +296,18 @@ class TestMessageFlowIntegration:
|
||||
Note: This does NOT run actual LangBot pipeline stages.
|
||||
"""
|
||||
# Setup
|
||||
platform = FakePlatform(bot_account_id="test-bot")
|
||||
platform = FakePlatform(bot_account_id='test-bot')
|
||||
provider = fake_provider_pong()
|
||||
model = fake_model(provider=provider)
|
||||
|
||||
# Create inbound message
|
||||
query = text_query("ping")
|
||||
query = text_query('ping')
|
||||
|
||||
# Simulate provider processing
|
||||
response = await provider.invoke_llm(
|
||||
query=query,
|
||||
model=model,
|
||||
messages=[{"role": "user", "content": "ping"}],
|
||||
messages=[{'role': 'user', 'content': 'ping'}],
|
||||
funcs=[],
|
||||
extra_args={},
|
||||
)
|
||||
@@ -321,16 +324,16 @@ class TestMessageFlowIntegration:
|
||||
# Verify platform captured outbound
|
||||
outbound = platform.get_outbound_messages()
|
||||
assert len(outbound) == 1
|
||||
assert outbound[0]["type"] == "reply"
|
||||
assert str(outbound[0]["message"]) == FakeProvider.PONG_RESPONSE
|
||||
assert outbound[0]['type'] == 'reply'
|
||||
assert str(outbound[0]['message']) == FakeProvider.PONG_RESPONSE
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_streaming_message_flow(self):
|
||||
"""Smoke test: streaming message flow."""
|
||||
platform = FakePlatform().supports_streaming()
|
||||
provider = FakeProvider().returns_streaming(["Hello", " there"])
|
||||
provider = FakeProvider().returns_streaming(['Hello', ' there'])
|
||||
model = fake_model(provider=provider)
|
||||
query = text_query("hi")
|
||||
query = text_query('hi')
|
||||
|
||||
chunks = []
|
||||
async for chunk in provider.invoke_llm_stream(
|
||||
@@ -344,8 +347,8 @@ class TestMessageFlowIntegration:
|
||||
|
||||
# Verify streaming worked
|
||||
assert len(chunks) == 2
|
||||
full_content = "".join(c.content for c in chunks)
|
||||
assert full_content == "Hello there"
|
||||
full_content = ''.join(c.content for c in chunks)
|
||||
assert full_content == 'Hello there'
|
||||
|
||||
# Verify platform supports streaming
|
||||
assert await platform.is_stream_output_supported() is True
|
||||
assert await platform.is_stream_output_supported() is True
|
||||
|
||||
@@ -15,22 +15,12 @@ import pathlib
|
||||
# Resolve project root (one level up from tests/)
|
||||
_PROJECT_ROOT = pathlib.Path(__file__).resolve().parent.parent
|
||||
|
||||
VULN_FILE = (
|
||||
_PROJECT_ROOT
|
||||
/ "src"
|
||||
/ "langbot"
|
||||
/ "pkg"
|
||||
/ "api"
|
||||
/ "http"
|
||||
/ "controller"
|
||||
/ "groups"
|
||||
/ "system.py"
|
||||
)
|
||||
VULN_FILE = _PROJECT_ROOT / 'src' / 'langbot' / 'pkg' / 'api' / 'http' / 'controller' / 'groups' / 'system.py'
|
||||
|
||||
|
||||
def test_no_exec_call_in_system_controller():
|
||||
"""Verify there is no exec() call in system.py that takes user input."""
|
||||
with open(VULN_FILE, "r") as f:
|
||||
with open(VULN_FILE, 'r') as f:
|
||||
source = f.read()
|
||||
|
||||
tree = ast.parse(source)
|
||||
@@ -40,27 +30,26 @@ def test_no_exec_call_in_system_controller():
|
||||
if isinstance(node, ast.Call):
|
||||
func = node.func
|
||||
# Match bare exec() call
|
||||
if isinstance(func, ast.Name) and func.id == "exec":
|
||||
if isinstance(func, ast.Name) and func.id == 'exec':
|
||||
exec_calls.append(node.lineno)
|
||||
|
||||
assert len(exec_calls) == 0, (
|
||||
f"Found exec() call(s) at line(s) {exec_calls} in system.py. "
|
||||
"User-supplied code must never be passed to exec()."
|
||||
f'Found exec() call(s) at line(s) {exec_calls} in system.py. User-supplied code must never be passed to exec().'
|
||||
)
|
||||
|
||||
|
||||
def test_no_debug_exec_route():
|
||||
"""Verify the /debug/exec route is not registered."""
|
||||
with open(VULN_FILE, "r") as f:
|
||||
with open(VULN_FILE, 'r') as f:
|
||||
source = f.read()
|
||||
|
||||
assert "debug/exec" not in source, (
|
||||
"The /debug/exec route still exists in system.py. "
|
||||
"This endpoint allows arbitrary code execution and must be removed."
|
||||
assert 'debug/exec' not in source, (
|
||||
'The /debug/exec route still exists in system.py. '
|
||||
'This endpoint allows arbitrary code execution and must be removed.'
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if __name__ == '__main__':
|
||||
test_no_exec_call_in_system_controller()
|
||||
test_no_debug_exec_route()
|
||||
print("All tests passed!")
|
||||
print('All tests passed!')
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
### 4. 向量数据库 (`vector/vdbs/`)
|
||||
- **路径**: `src/langbot/pkg/vector/vdbs/`
|
||||
- **模块**: chroma, milvus, pgvector, qdrant, seekdb
|
||||
- **模块**: chroma, milvus, pgvector, qdrant, seekdb, valkey_search
|
||||
- **排除原因**: 需要真实向量数据库实例运行
|
||||
- **测试方式**: 需要 Docker 启动测试数据库或 mock
|
||||
- **状态**: 后续可补充 mock 测试
|
||||
|
||||
@@ -1 +1 @@
|
||||
"""Unit tests for LangBot API HTTP service layer."""
|
||||
"""Unit tests for LangBot API HTTP service layer."""
|
||||
|
||||
@@ -13,4 +13,4 @@ Does NOT:
|
||||
- Call real provider/platform/network
|
||||
|
||||
Uses tests.factories.FakeApp as base mock application.
|
||||
"""
|
||||
"""
|
||||
|
||||
@@ -132,9 +132,7 @@ class TestApiKeyServiceCreateApiKey:
|
||||
with patch('langbot.pkg.api.http.service.apikey.secrets.token_urlsafe', return_value='fixed-token'):
|
||||
result = await service.create_api_key('New Key', 'Test description')
|
||||
|
||||
assert insert_params == [
|
||||
{'name': 'New Key', 'key': 'lbk_fixed-token', 'description': 'Test description'}
|
||||
]
|
||||
assert insert_params == [{'name': 'New Key', 'key': 'lbk_fixed-token', 'description': 'Test description'}]
|
||||
assert result['key'].startswith('lbk_')
|
||||
assert result['key'] == 'lbk_fixed-token'
|
||||
assert result['name'] == 'New Key'
|
||||
@@ -257,16 +255,22 @@ class TestApiKeyServiceGetApiKey:
|
||||
class TestApiKeyServiceVerifyApiKey:
|
||||
"""Tests for verify_api_key method."""
|
||||
|
||||
@staticmethod
|
||||
def _make_ap(db_key=None, global_api_key=''):
|
||||
"""Build a mock Application with persistence + instance_config."""
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
mock_result = Mock()
|
||||
mock_result.first = Mock(return_value=db_key)
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
||||
ap.instance_config = SimpleNamespace(data={'api': {'global_api_key': global_api_key}})
|
||||
return ap
|
||||
|
||||
async def test_verify_api_key_valid(self):
|
||||
"""Returns True for valid API key."""
|
||||
# Setup
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
|
||||
key = Mock(spec=ApiKey)
|
||||
mock_result = Mock()
|
||||
mock_result.first = Mock(return_value=key)
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
||||
ap = self._make_ap(db_key=key)
|
||||
|
||||
service = ApiKeyService(ap)
|
||||
|
||||
@@ -279,12 +283,7 @@ class TestApiKeyServiceVerifyApiKey:
|
||||
async def test_verify_api_key_invalid(self):
|
||||
"""Returns False for invalid API key."""
|
||||
# Setup
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
|
||||
mock_result = Mock()
|
||||
mock_result.first = Mock(return_value=None)
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
||||
ap = self._make_ap(db_key=None)
|
||||
|
||||
service = ApiKeyService(ap)
|
||||
|
||||
@@ -297,12 +296,7 @@ class TestApiKeyServiceVerifyApiKey:
|
||||
async def test_verify_api_key_empty_string(self):
|
||||
"""Returns False for empty key string."""
|
||||
# Setup
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
|
||||
mock_result = Mock()
|
||||
mock_result.first = Mock(return_value=None)
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
||||
ap = self._make_ap(db_key=None)
|
||||
|
||||
service = ApiKeyService(ap)
|
||||
|
||||
@@ -315,12 +309,7 @@ class TestApiKeyServiceVerifyApiKey:
|
||||
async def test_verify_api_key_unknown_key(self):
|
||||
"""Returns False when the key is not present in persistence."""
|
||||
# Setup
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
|
||||
mock_result = Mock()
|
||||
mock_result.first = Mock(return_value=None)
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
||||
ap = self._make_ap(db_key=None)
|
||||
|
||||
service = ApiKeyService(ap)
|
||||
|
||||
@@ -330,6 +319,70 @@ class TestApiKeyServiceVerifyApiKey:
|
||||
# Verify
|
||||
assert result is False
|
||||
|
||||
async def test_verify_global_api_key_match(self):
|
||||
"""Returns True when key matches the config.yaml global API key (no DB lookup)."""
|
||||
# Setup: no DB record, but a global key is configured
|
||||
ap = self._make_ap(db_key=None, global_api_key='my-global-secret')
|
||||
|
||||
service = ApiKeyService(ap)
|
||||
|
||||
# Execute
|
||||
result = await service.verify_api_key('my-global-secret')
|
||||
|
||||
# Verify: accepted purely on config match
|
||||
assert result is True
|
||||
# DB should not have been consulted for the global-key path
|
||||
ap.persistence_mgr.execute_async.assert_not_called()
|
||||
|
||||
async def test_verify_global_api_key_no_prefix_required(self):
|
||||
"""Global API key is accepted even without the lbk_ prefix."""
|
||||
ap = self._make_ap(db_key=None, global_api_key='plainsecret123')
|
||||
|
||||
service = ApiKeyService(ap)
|
||||
|
||||
result = await service.verify_api_key('plainsecret123')
|
||||
|
||||
assert result is True
|
||||
|
||||
async def test_verify_global_api_key_mismatch_falls_back_to_db(self):
|
||||
"""A non-matching key still falls through to the DB lookup."""
|
||||
# Global key set, but request uses a different lbk_ key that IS in DB
|
||||
key = Mock(spec=ApiKey)
|
||||
ap = self._make_ap(db_key=key, global_api_key='my-global-secret')
|
||||
|
||||
service = ApiKeyService(ap)
|
||||
|
||||
result = await service.verify_api_key('lbk_db_key')
|
||||
|
||||
assert result is True
|
||||
ap.persistence_mgr.execute_async.assert_called_once()
|
||||
|
||||
async def test_verify_empty_global_api_key_disabled(self):
|
||||
"""An empty global_api_key must never authenticate an empty/blank request."""
|
||||
ap = self._make_ap(db_key=None, global_api_key='')
|
||||
|
||||
service = ApiKeyService(ap)
|
||||
|
||||
# Empty request key is rejected, and a blank global key never matches
|
||||
assert await service.verify_api_key('') is False
|
||||
assert await service.verify_api_key(' ') is False
|
||||
|
||||
async def test_verify_api_key_missing_global_config_key(self):
|
||||
"""Works even when api.global_api_key is absent (existing installs)."""
|
||||
# instance_config without the global_api_key field at all
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
mock_result = Mock()
|
||||
mock_result.first = Mock(return_value=None)
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
||||
ap.instance_config = SimpleNamespace(data={'api': {}})
|
||||
|
||||
service = ApiKeyService(ap)
|
||||
|
||||
result = await service.verify_api_key('lbk_some_key')
|
||||
|
||||
assert result is False
|
||||
|
||||
|
||||
class TestApiKeyServiceDeleteApiKey:
|
||||
"""Tests for delete_api_key method."""
|
||||
|
||||
@@ -303,13 +303,7 @@ class TestBotServiceCreateBot:
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.instance_config = SimpleNamespace()
|
||||
ap.instance_config.data = {
|
||||
'system': {
|
||||
'limitation': {
|
||||
'max_bots': 2
|
||||
}
|
||||
}
|
||||
}
|
||||
ap.instance_config.data = {'system': {'limitation': {'max_bots': 2}}}
|
||||
ap.platform_mgr = SimpleNamespace()
|
||||
ap.platform_mgr.load_bot = AsyncMock()
|
||||
|
||||
@@ -318,9 +312,7 @@ class TestBotServiceCreateBot:
|
||||
bot2 = _create_mock_bot(bot_uuid='uuid-2')
|
||||
mock_result = _create_mock_result([bot1, bot2])
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
||||
ap.persistence_mgr.serialize_model = Mock(
|
||||
return_value={'uuid': 'uuid-1', 'name': 'Bot 1'}
|
||||
)
|
||||
ap.persistence_mgr.serialize_model = Mock(return_value={'uuid': 'uuid-1', 'name': 'Bot 1'})
|
||||
|
||||
service = BotService(ap)
|
||||
|
||||
@@ -352,6 +344,7 @@ class TestBotServiceCreateBot:
|
||||
bot_result.first = Mock(return_value=_create_mock_bot())
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -362,9 +355,7 @@ class TestBotServiceCreateBot:
|
||||
return bot_result # Get bot
|
||||
|
||||
ap.persistence_mgr.execute_async = AsyncMock(side_effect=mock_execute)
|
||||
ap.persistence_mgr.serialize_model = Mock(
|
||||
return_value={'uuid': 'new-uuid', 'name': 'New Bot'}
|
||||
)
|
||||
ap.persistence_mgr.serialize_model = Mock(return_value={'uuid': 'new-uuid', 'name': 'New Bot'})
|
||||
|
||||
service = BotService(ap)
|
||||
|
||||
@@ -397,6 +388,7 @@ class TestBotServiceCreateBot:
|
||||
bot_result.first = Mock(return_value=_create_mock_bot())
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -492,6 +484,7 @@ class TestBotServiceUpdateBot:
|
||||
pipeline_result.first = Mock(return_value=mock_pipeline)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -582,10 +575,9 @@ class TestBotServiceListEventLogs:
|
||||
# Mock runtime bot with logger
|
||||
runtime_bot = SimpleNamespace()
|
||||
runtime_bot.logger = SimpleNamespace()
|
||||
runtime_bot.logger.get_logs = AsyncMock(return_value=(
|
||||
[SimpleNamespace(to_json=Mock(return_value={'msg': 'log1'}))],
|
||||
5
|
||||
))
|
||||
runtime_bot.logger.get_logs = AsyncMock(
|
||||
return_value=([SimpleNamespace(to_json=Mock(return_value={'msg': 'log1'}))], 5)
|
||||
)
|
||||
ap.platform_mgr.get_bot_by_uuid = AsyncMock(return_value=runtime_bot)
|
||||
|
||||
service = BotService(ap)
|
||||
@@ -646,11 +638,7 @@ class TestBotServiceSendMessage:
|
||||
service = BotService(ap)
|
||||
|
||||
# Execute with valid message chain format
|
||||
message_chain_data = {
|
||||
'messages': [
|
||||
{'type': 'text', 'data': {'text': 'Hello'}}
|
||||
]
|
||||
}
|
||||
message_chain_data = {'messages': [{'type': 'text', 'data': {'text': 'Hello'}}]}
|
||||
|
||||
# Patch the import location - the module imports inside the function
|
||||
with patch('langbot_plugin.api.entities.builtin.platform.message.MessageChain') as MockMessageChain:
|
||||
|
||||
@@ -6,6 +6,7 @@ Tests cover:
|
||||
- Knowledge engine discovery
|
||||
- File operations
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
@@ -52,9 +53,7 @@ class TestGetKnowledgeBases:
|
||||
"""Test that it returns all knowledge base details."""
|
||||
knowledge_module = get_knowledge_service_module()
|
||||
mock_app = create_mock_app()
|
||||
mock_app.rag_mgr.get_all_knowledge_base_details = AsyncMock(
|
||||
return_value=[{'uuid': 'kb1', 'name': 'KB1'}]
|
||||
)
|
||||
mock_app.rag_mgr.get_all_knowledge_base_details = AsyncMock(return_value=[{'uuid': 'kb1', 'name': 'KB1'}])
|
||||
|
||||
service = knowledge_module.KnowledgeService(mock_app)
|
||||
result = await service.get_knowledge_bases()
|
||||
@@ -83,9 +82,7 @@ class TestGetKnowledgeBase:
|
||||
"""Test that it returns specific KB details."""
|
||||
knowledge_module = get_knowledge_service_module()
|
||||
mock_app = create_mock_app()
|
||||
mock_app.rag_mgr.get_knowledge_base_details = AsyncMock(
|
||||
return_value={'uuid': 'kb1', 'name': 'KB1'}
|
||||
)
|
||||
mock_app.rag_mgr.get_knowledge_base_details = AsyncMock(return_value={'uuid': 'kb1', 'name': 'KB1'})
|
||||
|
||||
service = knowledge_module.KnowledgeService(mock_app)
|
||||
result = await service.get_knowledge_base('kb1')
|
||||
@@ -153,9 +150,7 @@ class TestCreateKnowledgeBase:
|
||||
|
||||
service = knowledge_module.KnowledgeService(mock_app)
|
||||
|
||||
await service.create_knowledge_base({
|
||||
'knowledge_engine_plugin_id': 'author/engine'
|
||||
})
|
||||
await service.create_knowledge_base({'knowledge_engine_plugin_id': 'author/engine'})
|
||||
|
||||
# Check that default name 'Untitled' was used
|
||||
call_args = mock_app.rag_mgr.create_knowledge_base.call_args
|
||||
@@ -170,20 +165,21 @@ class TestUpdateKnowledgeBase:
|
||||
"""Test that only mutable fields are updated."""
|
||||
knowledge_module = get_knowledge_service_module()
|
||||
mock_app = create_mock_app()
|
||||
mock_app.rag_mgr.get_knowledge_base_details = AsyncMock(
|
||||
return_value={'uuid': 'kb1', 'name': 'Updated'}
|
||||
)
|
||||
mock_app.rag_mgr.get_knowledge_base_details = AsyncMock(return_value={'uuid': 'kb1', 'name': 'Updated'})
|
||||
mock_app.rag_mgr.remove_knowledge_base_from_runtime = AsyncMock()
|
||||
mock_app.rag_mgr.load_knowledge_base = AsyncMock()
|
||||
|
||||
service = knowledge_module.KnowledgeService(mock_app)
|
||||
|
||||
# Pass both mutable and immutable fields
|
||||
await service.update_knowledge_base('kb1', {
|
||||
'name': 'New Name',
|
||||
'description': 'New desc',
|
||||
'uuid': 'should_be_filtered', # immutable
|
||||
})
|
||||
await service.update_knowledge_base(
|
||||
'kb1',
|
||||
{
|
||||
'name': 'New Name',
|
||||
'description': 'New desc',
|
||||
'uuid': 'should_be_filtered', # immutable
|
||||
},
|
||||
)
|
||||
|
||||
# Check that only mutable fields were passed to update
|
||||
call_args = mock_app.persistence_mgr.execute_async.call_args
|
||||
@@ -288,9 +284,7 @@ class TestListKnowledgeEngines:
|
||||
"""Test that it returns empty list and logs warning on exception."""
|
||||
knowledge_module = get_knowledge_service_module()
|
||||
mock_app = create_mock_app()
|
||||
mock_app.plugin_connector.list_knowledge_engines = AsyncMock(
|
||||
side_effect=Exception('Connection error')
|
||||
)
|
||||
mock_app.plugin_connector.list_knowledge_engines = AsyncMock(side_effect=Exception('Connection error'))
|
||||
|
||||
service = knowledge_module.KnowledgeService(mock_app)
|
||||
result = await service.list_knowledge_engines()
|
||||
@@ -386,12 +380,10 @@ class TestGetEngineSchemas:
|
||||
"""Test that it returns empty dict and logs warning on exception."""
|
||||
knowledge_module = get_knowledge_service_module()
|
||||
mock_app = create_mock_app()
|
||||
mock_app.plugin_connector.get_rag_creation_schema = AsyncMock(
|
||||
side_effect=Exception('Plugin error')
|
||||
)
|
||||
mock_app.plugin_connector.get_rag_creation_schema = AsyncMock(side_effect=Exception('Plugin error'))
|
||||
|
||||
service = knowledge_module.KnowledgeService(mock_app)
|
||||
result = await service.get_engine_creation_schema('author/engine')
|
||||
|
||||
assert result == {}
|
||||
mock_app.logger.warning.assert_called_once()
|
||||
mock_app.logger.warning.assert_called_once()
|
||||
|
||||
@@ -174,9 +174,7 @@ class TestMaintenanceServiceGetStorageAnalysis:
|
||||
# Setup
|
||||
ap = SimpleNamespace()
|
||||
ap.instance_config = SimpleNamespace()
|
||||
ap.instance_config.data = {
|
||||
'database': {'use': 'sqlite', 'sqlite': {'path': 'data/langbot.db'}}
|
||||
}
|
||||
ap.instance_config.data = {'database': {'use': 'sqlite', 'sqlite': {'path': 'data/langbot.db'}}}
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.logger = SimpleNamespace()
|
||||
ap.logger.warning = Mock()
|
||||
@@ -292,12 +290,8 @@ class TestMaintenanceServiceGetStorageAnalysis:
|
||||
service._file_count = Mock(return_value=0)
|
||||
service._monitoring_counts = AsyncMock(return_value={})
|
||||
service._binary_storage_stats = AsyncMock(return_value={'count': 0, 'size_bytes': 0})
|
||||
service._expired_uploaded_candidates = AsyncMock(return_value=[
|
||||
{'key': 'old_file', 'size_bytes': 100}
|
||||
])
|
||||
service._expired_log_candidates = Mock(return_value=[
|
||||
{'name': 'old_log', 'size_bytes': 50}
|
||||
])
|
||||
service._expired_uploaded_candidates = AsyncMock(return_value=[{'key': 'old_file', 'size_bytes': 100}])
|
||||
service._expired_log_candidates = Mock(return_value=[{'name': 'old_log', 'size_bytes': 50}])
|
||||
|
||||
# Execute
|
||||
result = await service.get_storage_analysis()
|
||||
@@ -367,6 +361,7 @@ class TestMaintenanceServiceBinaryStorageStats:
|
||||
size_result = _create_mock_result(scalar_value=5000)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -396,6 +391,7 @@ class TestMaintenanceServiceBinaryStorageStats:
|
||||
count_result = _create_mock_result(scalar_value=5)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -821,4 +817,4 @@ class TestMaintenanceServiceExpiredLocalUploadCandidates:
|
||||
result = service._expired_local_upload_candidates(7, include_paths=True)
|
||||
|
||||
# Verify - path included
|
||||
assert 'path' in result[0]
|
||||
assert 'path' in result[0]
|
||||
|
||||
@@ -90,6 +90,56 @@ class TestMCPServiceGetRuntimeInfo:
|
||||
assert result is None
|
||||
|
||||
|
||||
class TestMCPServiceResources:
|
||||
"""Tests for MCP resource helpers."""
|
||||
|
||||
async def test_get_resource_templates_delegates_to_loader(self):
|
||||
ap = SimpleNamespace()
|
||||
ap.tool_mgr = SimpleNamespace()
|
||||
ap.tool_mgr.mcp_tool_loader = SimpleNamespace()
|
||||
ap.tool_mgr.mcp_tool_loader.get_resource_templates = AsyncMock(
|
||||
return_value=[{'uri_template': 'file:///{path}', 'name': 'files'}]
|
||||
)
|
||||
|
||||
service = MCPService(ap)
|
||||
|
||||
result = await service.get_mcp_server_resource_templates('docs')
|
||||
|
||||
assert result == [{'uri_template': 'file:///{path}', 'name': 'files'}]
|
||||
ap.tool_mgr.mcp_tool_loader.get_resource_templates.assert_awaited_once_with('docs')
|
||||
|
||||
async def test_read_resource_envelope_uses_ui_preview_source(self):
|
||||
ap = SimpleNamespace()
|
||||
ap.tool_mgr = SimpleNamespace()
|
||||
ap.tool_mgr.mcp_tool_loader = SimpleNamespace()
|
||||
ap.tool_mgr.mcp_tool_loader.read_resource_envelope = AsyncMock(
|
||||
return_value={
|
||||
'server_name': 'docs',
|
||||
'uri': 'file:///README.md',
|
||||
'contents': [],
|
||||
'source': 'ui_preview',
|
||||
}
|
||||
)
|
||||
|
||||
service = MCPService(ap)
|
||||
|
||||
result = await service.read_mcp_server_resource_envelope(
|
||||
'docs',
|
||||
'file:///README.md',
|
||||
max_bytes=4096,
|
||||
include_blob=True,
|
||||
)
|
||||
|
||||
assert result['source'] == 'ui_preview'
|
||||
ap.tool_mgr.mcp_tool_loader.read_resource_envelope.assert_awaited_once_with(
|
||||
'docs',
|
||||
'file:///README.md',
|
||||
include_blob=True,
|
||||
source='ui_preview',
|
||||
max_bytes=4096,
|
||||
)
|
||||
|
||||
|
||||
class TestMCPServiceGetMCPServers:
|
||||
"""Tests for get_mcp_servers method."""
|
||||
|
||||
@@ -186,13 +236,7 @@ class TestMCPServiceCreateMCPServer:
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.instance_config = SimpleNamespace()
|
||||
ap.instance_config.data = {
|
||||
'system': {
|
||||
'limitation': {
|
||||
'max_extensions': 2
|
||||
}
|
||||
}
|
||||
}
|
||||
ap.instance_config.data = {'system': {'limitation': {'max_extensions': 2}}}
|
||||
ap.plugin_connector = SimpleNamespace()
|
||||
ap.plugin_connector.list_plugins = AsyncMock(return_value=[Mock(), Mock()]) # 2 plugins
|
||||
|
||||
@@ -236,6 +280,25 @@ class TestMCPServiceCreateMCPServer:
|
||||
assert server_uuid is not None
|
||||
assert len(server_uuid) == 36 # UUID format
|
||||
|
||||
async def test_create_mcp_server_duplicate_name_raises(self):
|
||||
"""Rejects duplicate MCP server names."""
|
||||
# Setup
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.instance_config = SimpleNamespace()
|
||||
ap.instance_config.data = {'system': {'limitation': {'max_extensions': -1}}}
|
||||
ap.tool_mgr = None
|
||||
|
||||
existing_server = _create_mock_mcp_server(name='Existing Server')
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_mock_result(first_item=existing_server))
|
||||
ap.persistence_mgr.serialize_model = Mock(return_value={})
|
||||
|
||||
service = MCPService(ap)
|
||||
|
||||
# Execute & Verify
|
||||
with pytest.raises(ValueError, match='MCP server already exists: Existing Server'):
|
||||
await service.create_mcp_server({'name': 'Existing Server'})
|
||||
|
||||
async def test_create_mcp_server_loads_server(self):
|
||||
"""Loads server into tool_mgr when enabled."""
|
||||
# Setup
|
||||
@@ -252,11 +315,12 @@ class TestMCPServiceCreateMCPServer:
|
||||
server_entity = _create_mock_mcp_server(server_uuid='new-uuid', enable=True)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
if call_count == 1:
|
||||
return _create_mock_result([]) # Empty list for limit check
|
||||
return _create_mock_result([]) # Empty result for duplicate-name check
|
||||
elif call_count == 2:
|
||||
return Mock() # Insert
|
||||
return _create_mock_result(first_item=server_entity) # Select created
|
||||
@@ -361,6 +425,7 @@ class TestMCPServiceUpdateMCPServer:
|
||||
old_server = _create_mock_mcp_server(name='Old Server', enable=True)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -394,6 +459,7 @@ class TestMCPServiceUpdateMCPServer:
|
||||
updated_server = _create_mock_mcp_server(name='Old Server', enable=True)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -432,6 +498,7 @@ class TestMCPServiceUpdateMCPServer:
|
||||
|
||||
# Mock for: first select -> update -> second select (for updated server)
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -465,6 +532,7 @@ class TestMCPServiceUpdateMCPServer:
|
||||
|
||||
# Mock execute for select and update
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -499,6 +567,7 @@ class TestMCPServiceDeleteMCPServer:
|
||||
server = _create_mock_mcp_server(name='Server to Delete')
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -530,6 +599,7 @@ class TestMCPServiceDeleteMCPServer:
|
||||
server = _create_mock_mcp_server(name='Not in Sessions')
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -559,6 +629,7 @@ class TestMCPServiceDeleteMCPServer:
|
||||
|
||||
# No server found
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -596,9 +667,7 @@ class TestMCPServiceTestMCPServer:
|
||||
ap.tool_mgr.mcp_tool_loader.get_session = Mock(return_value=mock_session)
|
||||
|
||||
ap.task_mgr = SimpleNamespace()
|
||||
ap.task_mgr.create_user_task = Mock(
|
||||
return_value=SimpleNamespace(id=123)
|
||||
)
|
||||
ap.task_mgr.create_user_task = Mock(return_value=SimpleNamespace(id=123))
|
||||
|
||||
service = MCPService(ap)
|
||||
|
||||
@@ -634,9 +703,7 @@ class TestMCPServiceTestMCPServer:
|
||||
ap.tool_mgr.mcp_tool_loader.load_mcp_server = AsyncMock(return_value=mock_session)
|
||||
|
||||
ap.task_mgr = SimpleNamespace()
|
||||
ap.task_mgr.create_user_task = Mock(
|
||||
return_value=SimpleNamespace(id=456)
|
||||
)
|
||||
ap.task_mgr.create_user_task = Mock(return_value=SimpleNamespace(id=456))
|
||||
|
||||
service = MCPService(ap)
|
||||
|
||||
@@ -645,4 +712,4 @@ class TestMCPServiceTestMCPServer:
|
||||
|
||||
# Verify - load_mcp_server called
|
||||
ap.tool_mgr.mcp_tool_loader.load_mcp_server.assert_called_once()
|
||||
assert task_id == 456
|
||||
assert task_id == 456
|
||||
|
||||
@@ -23,6 +23,7 @@ from langbot.pkg.api.http.service.model import (
|
||||
RerankModelsService,
|
||||
_parse_provider_api_keys,
|
||||
_runtime_model_data,
|
||||
_validate_provider_supports,
|
||||
)
|
||||
from langbot.pkg.entity.persistence.model import LLMModel, EmbeddingModel, RerankModel, ModelProvider
|
||||
|
||||
@@ -35,6 +36,7 @@ def _create_mock_llm_model(
|
||||
name: str = 'Test LLM',
|
||||
provider_uuid: str = 'provider-uuid',
|
||||
abilities: list = None,
|
||||
context_length: int | None = None,
|
||||
extra_args: dict = None,
|
||||
) -> Mock:
|
||||
"""Helper to create mock LLMModel entity."""
|
||||
@@ -43,6 +45,7 @@ def _create_mock_llm_model(
|
||||
model.name = name
|
||||
model.provider_uuid = provider_uuid
|
||||
model.abilities = abilities or []
|
||||
model.context_length = context_length
|
||||
model.extra_args = extra_args or {}
|
||||
return model
|
||||
|
||||
@@ -142,10 +145,12 @@ class TestRuntimeModelData:
|
||||
'name': 'Model',
|
||||
'provider_uuid': 'provider',
|
||||
'abilities': ['vision'],
|
||||
'context_length': 128000,
|
||||
'extra_args': {'temp': 0.7},
|
||||
}
|
||||
result = _runtime_model_data('uuid', update_payload)
|
||||
assert result['abilities'] == ['vision']
|
||||
assert result['context_length'] == 128000
|
||||
assert result['extra_args'] == {'temp': 0.7}
|
||||
|
||||
|
||||
@@ -162,6 +167,7 @@ class TestLLMModelsServiceGetLLMModels:
|
||||
mock_provider_result = _create_mock_result([])
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
return mock_result if call_count == 0 else mock_provider_result
|
||||
|
||||
@@ -188,13 +194,14 @@ class TestLLMModelsServiceGetLLMModels:
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
|
||||
model = _create_mock_llm_model()
|
||||
model = _create_mock_llm_model(context_length=128000)
|
||||
provider = _create_mock_provider()
|
||||
|
||||
mock_model_result = _create_mock_result([model])
|
||||
mock_provider_result = _create_mock_result([provider])
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -206,6 +213,7 @@ class TestLLMModelsServiceGetLLMModels:
|
||||
'uuid': entity.uuid,
|
||||
'name': entity.name,
|
||||
'provider_uuid': entity.provider_uuid if hasattr(entity, 'provider_uuid') else None,
|
||||
'context_length': getattr(entity, 'context_length', None),
|
||||
'api_keys': entity.api_keys if hasattr(entity, 'api_keys') else None,
|
||||
}
|
||||
)
|
||||
@@ -218,6 +226,7 @@ class TestLLMModelsServiceGetLLMModels:
|
||||
# Verify
|
||||
assert len(result) == 1
|
||||
assert result[0]['name'] == 'Test LLM'
|
||||
assert result[0]['context_length'] == 128000
|
||||
|
||||
async def test_get_llm_models_hide_secret_keys(self):
|
||||
"""Hides secret API keys when include_secret=False."""
|
||||
@@ -232,6 +241,7 @@ class TestLLMModelsServiceGetLLMModels:
|
||||
mock_provider_result = _create_mock_result([provider])
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -265,13 +275,14 @@ class TestLLMModelsServiceGetLLMModel:
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
|
||||
model = _create_mock_llm_model(model_uuid='found-uuid')
|
||||
model = _create_mock_llm_model(model_uuid='found-uuid', context_length=128000)
|
||||
provider = _create_mock_provider()
|
||||
|
||||
mock_model_result = _create_mock_result([], first_item=model)
|
||||
mock_provider_result = _create_mock_result([], first_item=provider)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -279,11 +290,12 @@ class TestLLMModelsServiceGetLLMModel:
|
||||
|
||||
ap.persistence_mgr.execute_async = AsyncMock(side_effect=mock_execute)
|
||||
ap.persistence_mgr.serialize_model = Mock(
|
||||
return_value={
|
||||
'uuid': 'found-uuid',
|
||||
'name': 'Test LLM',
|
||||
'provider_uuid': 'provider-uuid',
|
||||
'provider': {'uuid': 'provider-uuid', 'api_keys': ['key']},
|
||||
side_effect=lambda model_cls, entity: {
|
||||
'uuid': entity.uuid,
|
||||
'name': entity.name,
|
||||
'provider_uuid': getattr(entity, 'provider_uuid', None),
|
||||
'context_length': getattr(entity, 'context_length', None),
|
||||
'api_keys': getattr(entity, 'api_keys', None),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -295,6 +307,7 @@ class TestLLMModelsServiceGetLLMModel:
|
||||
# Verify
|
||||
assert result is not None
|
||||
assert result['uuid'] == 'found-uuid'
|
||||
assert result['context_length'] == 128000
|
||||
|
||||
async def test_get_llm_model_not_found(self):
|
||||
"""Returns None when model not found."""
|
||||
@@ -328,9 +341,7 @@ class TestLLMModelsServiceGetLLMModelsByProvider:
|
||||
|
||||
mock_result = _create_mock_result([model1, model2])
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
||||
ap.persistence_mgr.serialize_model = Mock(
|
||||
return_value={'uuid': 'model-1', 'name': 'Model 1'}
|
||||
)
|
||||
ap.persistence_mgr.serialize_model = Mock(return_value={'uuid': 'model-1', 'name': 'Model 1'})
|
||||
|
||||
service = LLMModelsService(ap)
|
||||
|
||||
@@ -362,12 +373,14 @@ class TestLLMModelsServiceCreateLLMModel:
|
||||
service = LLMModelsService(ap)
|
||||
|
||||
# Execute
|
||||
model_uuid = await service.create_llm_model({
|
||||
'name': 'New LLM',
|
||||
'provider_uuid': 'provider-uuid',
|
||||
'abilities': [],
|
||||
'extra_args': {},
|
||||
})
|
||||
model_uuid = await service.create_llm_model(
|
||||
{
|
||||
'name': 'New LLM',
|
||||
'provider_uuid': 'provider-uuid',
|
||||
'abilities': [],
|
||||
'extra_args': {},
|
||||
}
|
||||
)
|
||||
|
||||
# Verify
|
||||
assert model_uuid is not None
|
||||
@@ -391,17 +404,53 @@ class TestLLMModelsServiceCreateLLMModel:
|
||||
service = LLMModelsService(ap)
|
||||
|
||||
# Execute
|
||||
model_uuid = await service.create_llm_model({
|
||||
'uuid': 'preserved-uuid',
|
||||
'name': 'Preserved UUID Model',
|
||||
'provider_uuid': 'provider-uuid',
|
||||
'abilities': [],
|
||||
'extra_args': {},
|
||||
}, preserve_uuid=True)
|
||||
model_uuid = await service.create_llm_model(
|
||||
{
|
||||
'uuid': 'preserved-uuid',
|
||||
'name': 'Preserved UUID Model',
|
||||
'provider_uuid': 'provider-uuid',
|
||||
'abilities': [],
|
||||
'extra_args': {},
|
||||
},
|
||||
preserve_uuid=True,
|
||||
)
|
||||
|
||||
# Verify
|
||||
assert model_uuid == 'preserved-uuid'
|
||||
|
||||
async def test_create_llm_model_persists_context_length_as_column(self):
|
||||
"""Creates LLM model with context_length outside extra_args."""
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.model_mgr = SimpleNamespace()
|
||||
ap.model_mgr.provider_dict = {'provider-uuid': Mock()}
|
||||
ap.model_mgr.llm_models = []
|
||||
ap.model_mgr.load_llm_model_with_provider = AsyncMock(return_value=Mock())
|
||||
ap.pipeline_service = SimpleNamespace(update_pipeline=AsyncMock())
|
||||
|
||||
mock_result = _create_mock_result([])
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
||||
|
||||
service = LLMModelsService(ap)
|
||||
|
||||
await service.create_llm_model(
|
||||
{
|
||||
'uuid': 'model-with-context',
|
||||
'name': 'Context Model',
|
||||
'provider_uuid': 'provider-uuid',
|
||||
'abilities': ['func_call'],
|
||||
'context_length': 128000,
|
||||
'extra_args': {'temperature': 0.2},
|
||||
},
|
||||
preserve_uuid=True,
|
||||
auto_set_to_default_pipeline=False,
|
||||
)
|
||||
|
||||
runtime_entity = ap.model_mgr.load_llm_model_with_provider.await_args.args[0]
|
||||
assert runtime_entity.context_length == 128000
|
||||
assert runtime_entity.extra_args == {'temperature': 0.2}
|
||||
assert 'context_length' not in runtime_entity.extra_args
|
||||
|
||||
async def test_create_llm_model_provider_not_found_raises_error(self):
|
||||
"""Raises Exception when provider not found in runtime."""
|
||||
# Setup
|
||||
@@ -417,12 +466,14 @@ class TestLLMModelsServiceCreateLLMModel:
|
||||
|
||||
# Execute & Verify
|
||||
with pytest.raises(Exception, match='provider not found'):
|
||||
await service.create_llm_model({
|
||||
'name': 'No Provider Model',
|
||||
'provider_uuid': 'nonexistent-provider',
|
||||
'abilities': [],
|
||||
'extra_args': {},
|
||||
})
|
||||
await service.create_llm_model(
|
||||
{
|
||||
'name': 'No Provider Model',
|
||||
'provider_uuid': 'nonexistent-provider',
|
||||
'abilities': [],
|
||||
'extra_args': {},
|
||||
}
|
||||
)
|
||||
|
||||
async def test_create_llm_model_with_provider_data(self):
|
||||
"""Creates provider when provider data provided."""
|
||||
@@ -448,16 +499,18 @@ class TestLLMModelsServiceCreateLLMModel:
|
||||
service = LLMModelsService(ap)
|
||||
|
||||
# Execute - with provider data (no UUID)
|
||||
result_uuid = await service.create_llm_model({
|
||||
'name': 'Model with New Provider',
|
||||
'provider': {
|
||||
'requester': 'openai',
|
||||
'base_url': 'https://api.openai.com',
|
||||
'api_keys': ['key'],
|
||||
},
|
||||
'abilities': [],
|
||||
'extra_args': {},
|
||||
})
|
||||
result_uuid = await service.create_llm_model(
|
||||
{
|
||||
'name': 'Model with New Provider',
|
||||
'provider': {
|
||||
'requester': 'openai',
|
||||
'base_url': 'https://api.openai.com',
|
||||
'api_keys': ['key'],
|
||||
},
|
||||
'abilities': [],
|
||||
'extra_args': {},
|
||||
}
|
||||
)
|
||||
|
||||
# Verify - provider_service was called and UUID generated
|
||||
ap.provider_service.find_or_create_provider.assert_called_once()
|
||||
@@ -483,11 +536,14 @@ class TestLLMModelsServiceUpdateLLMModel:
|
||||
service = LLMModelsService(ap)
|
||||
|
||||
# Execute
|
||||
await service.update_llm_model('existing-uuid', {
|
||||
'uuid': 'should-be-removed',
|
||||
'name': 'Updated Name',
|
||||
'provider_uuid': 'provider-uuid',
|
||||
})
|
||||
await service.update_llm_model(
|
||||
'existing-uuid',
|
||||
{
|
||||
'uuid': 'should-be-removed',
|
||||
'name': 'Updated Name',
|
||||
'provider_uuid': 'provider-uuid',
|
||||
},
|
||||
)
|
||||
|
||||
# Verify - remove and load called
|
||||
ap.model_mgr.remove_llm_model.assert_called_once_with('existing-uuid')
|
||||
@@ -507,10 +563,42 @@ class TestLLMModelsServiceUpdateLLMModel:
|
||||
|
||||
# Execute & Verify
|
||||
with pytest.raises(Exception, match='provider not found'):
|
||||
await service.update_llm_model('model-uuid', {
|
||||
'name': 'Update',
|
||||
'provider_uuid': 'nonexistent-provider',
|
||||
})
|
||||
await service.update_llm_model(
|
||||
'model-uuid',
|
||||
{
|
||||
'name': 'Update',
|
||||
'provider_uuid': 'nonexistent-provider',
|
||||
},
|
||||
)
|
||||
|
||||
async def test_update_llm_model_reloads_context_length_as_column(self):
|
||||
"""Updates runtime model with context_length outside extra_args."""
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace(execute_async=AsyncMock())
|
||||
ap.model_mgr = SimpleNamespace()
|
||||
ap.model_mgr.provider_dict = {'provider-uuid': Mock()}
|
||||
ap.model_mgr.llm_models = []
|
||||
ap.model_mgr.remove_llm_model = AsyncMock()
|
||||
ap.model_mgr.load_llm_model_with_provider = AsyncMock(return_value=Mock())
|
||||
|
||||
service = LLMModelsService(ap)
|
||||
|
||||
await service.update_llm_model(
|
||||
'existing-uuid',
|
||||
{
|
||||
'name': 'Updated Name',
|
||||
'provider_uuid': 'provider-uuid',
|
||||
'abilities': ['vision'],
|
||||
'context_length': 64000,
|
||||
'extra_args': {'temperature': 0.4},
|
||||
},
|
||||
)
|
||||
|
||||
runtime_entity = ap.model_mgr.load_llm_model_with_provider.await_args.args[0]
|
||||
assert runtime_entity.uuid == 'existing-uuid'
|
||||
assert runtime_entity.context_length == 64000
|
||||
assert runtime_entity.extra_args == {'temperature': 0.4}
|
||||
assert 'context_length' not in runtime_entity.extra_args
|
||||
|
||||
|
||||
class TestLLMModelsServiceDeleteLLMModel:
|
||||
@@ -547,9 +635,7 @@ class TestEmbeddingModelsServiceGetEmbeddingModels:
|
||||
|
||||
mock_result = _create_mock_result([])
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
||||
ap.persistence_mgr.serialize_model = Mock(
|
||||
return_value={'uuid': 'embedding-uuid', 'name': 'Test'}
|
||||
)
|
||||
ap.persistence_mgr.serialize_model = Mock(return_value={'uuid': 'embedding-uuid', 'name': 'Test'})
|
||||
|
||||
service = EmbeddingModelsService(ap)
|
||||
|
||||
@@ -572,6 +658,7 @@ class TestEmbeddingModelsServiceGetEmbeddingModels:
|
||||
mock_provider_result = _create_mock_result([provider])
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -612,6 +699,7 @@ class TestEmbeddingModelsServiceGetEmbeddingModel:
|
||||
mock_provider_result = _create_mock_result([], first_item=provider)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -671,11 +759,13 @@ class TestEmbeddingModelsServiceCreateEmbeddingModel:
|
||||
service = EmbeddingModelsService(ap)
|
||||
|
||||
# Execute
|
||||
model_uuid = await service.create_embedding_model({
|
||||
'name': 'New Embedding',
|
||||
'provider_uuid': 'provider-uuid',
|
||||
'extra_args': {},
|
||||
})
|
||||
model_uuid = await service.create_embedding_model(
|
||||
{
|
||||
'name': 'New Embedding',
|
||||
'provider_uuid': 'provider-uuid',
|
||||
'extra_args': {},
|
||||
}
|
||||
)
|
||||
|
||||
# Verify
|
||||
assert model_uuid is not None
|
||||
@@ -696,11 +786,13 @@ class TestEmbeddingModelsServiceCreateEmbeddingModel:
|
||||
|
||||
# Execute & Verify
|
||||
with pytest.raises(Exception, match='provider not found'):
|
||||
await service.create_embedding_model({
|
||||
'name': 'No Provider Embedding',
|
||||
'provider_uuid': 'nonexistent',
|
||||
'extra_args': {},
|
||||
})
|
||||
await service.create_embedding_model(
|
||||
{
|
||||
'name': 'No Provider Embedding',
|
||||
'provider_uuid': 'nonexistent',
|
||||
'extra_args': {},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class TestEmbeddingModelsServiceDeleteEmbeddingModel:
|
||||
@@ -758,6 +850,7 @@ class TestRerankModelsServiceGetRerankModels:
|
||||
mock_provider_result = _create_mock_result([provider])
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -798,6 +891,7 @@ class TestRerankModelsServiceGetRerankModel:
|
||||
mock_provider_result = _create_mock_result([], first_item=provider)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -857,11 +951,13 @@ class TestRerankModelsServiceCreateRerankModel:
|
||||
service = RerankModelsService(ap)
|
||||
|
||||
# Execute
|
||||
model_uuid = await service.create_rerank_model({
|
||||
'name': 'New Rerank',
|
||||
'provider_uuid': 'provider-uuid',
|
||||
'extra_args': {},
|
||||
})
|
||||
model_uuid = await service.create_rerank_model(
|
||||
{
|
||||
'name': 'New Rerank',
|
||||
'provider_uuid': 'provider-uuid',
|
||||
'extra_args': {},
|
||||
}
|
||||
)
|
||||
|
||||
# Verify
|
||||
assert model_uuid is not None
|
||||
@@ -881,11 +977,13 @@ class TestRerankModelsServiceCreateRerankModel:
|
||||
|
||||
# Execute & Verify
|
||||
with pytest.raises(Exception, match='provider not found'):
|
||||
await service.create_rerank_model({
|
||||
'name': 'No Provider Rerank',
|
||||
'provider_uuid': 'nonexistent',
|
||||
'extra_args': {},
|
||||
})
|
||||
await service.create_rerank_model(
|
||||
{
|
||||
'name': 'No Provider Rerank',
|
||||
'provider_uuid': 'nonexistent',
|
||||
'extra_args': {},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class TestRerankModelsServiceDeleteRerankModel:
|
||||
@@ -924,9 +1022,7 @@ class TestEmbeddingModelsServiceGetEmbeddingModelsByProvider:
|
||||
|
||||
mock_result = _create_mock_result([model1, model2])
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
||||
ap.persistence_mgr.serialize_model = Mock(
|
||||
return_value={'uuid': 'emb-1', 'name': 'Embedding 1'}
|
||||
)
|
||||
ap.persistence_mgr.serialize_model = Mock(return_value={'uuid': 'emb-1', 'name': 'Embedding 1'})
|
||||
|
||||
service = EmbeddingModelsService(ap)
|
||||
|
||||
@@ -951,9 +1047,7 @@ class TestRerankModelsServiceGetRerankModelsByProvider:
|
||||
|
||||
mock_result = _create_mock_result([model1, model2])
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
||||
ap.persistence_mgr.serialize_model = Mock(
|
||||
return_value={'uuid': 'rerank-1', 'name': 'Rerank 1'}
|
||||
)
|
||||
ap.persistence_mgr.serialize_model = Mock(return_value={'uuid': 'rerank-1', 'name': 'Rerank 1'})
|
||||
|
||||
service = RerankModelsService(ap)
|
||||
|
||||
@@ -961,4 +1055,50 @@ class TestRerankModelsServiceGetRerankModelsByProvider:
|
||||
result = await service.get_rerank_models_by_provider('provider-uuid')
|
||||
|
||||
# Verify
|
||||
assert len(result) == 2
|
||||
assert len(result) == 2
|
||||
|
||||
|
||||
class TestValidateProviderSupports:
|
||||
"""Tests for _validate_provider_supports guard."""
|
||||
|
||||
@staticmethod
|
||||
def _make_ap(requester_name: str, support_type):
|
||||
"""Build a fake ap whose model_mgr resolves a manifest with support_type."""
|
||||
manifest = SimpleNamespace(spec={'support_type': support_type})
|
||||
runtime_provider = SimpleNamespace(provider_entity=SimpleNamespace(requester=requester_name))
|
||||
model_mgr = SimpleNamespace(
|
||||
provider_dict={'p1': runtime_provider},
|
||||
get_available_requester_manifest_by_name=lambda name: manifest if name == requester_name else None,
|
||||
)
|
||||
return SimpleNamespace(model_mgr=model_mgr)
|
||||
|
||||
async def test_allows_supported_type(self):
|
||||
ap = self._make_ap('cohere-rerank', ['rerank'])
|
||||
# Should not raise
|
||||
await _validate_provider_supports(ap, 'p1', 'rerank')
|
||||
|
||||
async def test_rejects_unsupported_type(self):
|
||||
ap = self._make_ap('cohere-rerank', ['rerank'])
|
||||
with pytest.raises(ValueError, match='does not support llm'):
|
||||
await _validate_provider_supports(ap, 'p1', 'llm')
|
||||
|
||||
async def test_allows_when_support_type_missing(self):
|
||||
# Manifest without support_type must not block (backward compatible)
|
||||
manifest = SimpleNamespace(spec={})
|
||||
runtime_provider = SimpleNamespace(provider_entity=SimpleNamespace(requester='legacy'))
|
||||
model_mgr = SimpleNamespace(
|
||||
provider_dict={'p1': runtime_provider},
|
||||
get_available_requester_manifest_by_name=lambda name: manifest,
|
||||
)
|
||||
ap = SimpleNamespace(model_mgr=model_mgr)
|
||||
await _validate_provider_supports(ap, 'p1', 'rerank')
|
||||
|
||||
async def test_allows_when_provider_unknown(self):
|
||||
ap = self._make_ap('cohere-rerank', ['rerank'])
|
||||
# Unknown provider uuid -> no entry -> no block
|
||||
await _validate_provider_supports(ap, 'missing', 'llm')
|
||||
|
||||
async def test_degrades_when_model_mgr_incomplete(self):
|
||||
# A bare ap without a usable model_mgr must not raise (defensive)
|
||||
ap = SimpleNamespace(model_mgr=SimpleNamespace())
|
||||
await _validate_provider_supports(ap, 'p1', 'llm')
|
||||
|
||||
@@ -215,13 +215,7 @@ class TestPipelineServiceCreatePipeline:
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.instance_config = SimpleNamespace()
|
||||
ap.instance_config.data = {
|
||||
'system': {
|
||||
'limitation': {
|
||||
'max_pipelines': 2
|
||||
}
|
||||
}
|
||||
}
|
||||
ap.instance_config.data = {'system': {'limitation': {'max_pipelines': 2}}}
|
||||
ap.pipeline_mgr = SimpleNamespace()
|
||||
ap.pipeline_mgr.load_pipeline = AsyncMock()
|
||||
ap.ver_mgr = SimpleNamespace()
|
||||
@@ -229,9 +223,7 @@ class TestPipelineServiceCreatePipeline:
|
||||
|
||||
mock_result = _create_mock_result([_create_mock_pipeline(), _create_mock_pipeline()])
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
||||
ap.persistence_mgr.serialize_model = Mock(
|
||||
return_value={'uuid': 'uuid-1', 'name': 'Pipeline 1'}
|
||||
)
|
||||
ap.persistence_mgr.serialize_model = Mock(return_value={'uuid': 'uuid-1', 'name': 'Pipeline 1'})
|
||||
|
||||
service = PipelineService(ap)
|
||||
|
||||
@@ -258,14 +250,14 @@ class TestPipelineServiceCreatePipeline:
|
||||
|
||||
# Mock persistence for insert
|
||||
ap.persistence_mgr.execute_async = AsyncMock()
|
||||
ap.persistence_mgr.serialize_model = Mock(
|
||||
return_value={'uuid': 'new-uuid', 'name': 'New Pipeline'}
|
||||
)
|
||||
ap.persistence_mgr.serialize_model = Mock(return_value={'uuid': 'new-uuid', 'name': 'New Pipeline'})
|
||||
|
||||
# Mock the file read for default config - patch at the utils module level
|
||||
default_config = {'trigger': {}, 'safety': {}, 'ai': {}, 'output': {}}
|
||||
with patch('builtins.open', mock_open(read_data=json.dumps(default_config))):
|
||||
with patch('langbot.pkg.utils.paths.get_resource_path', return_value='templates/default-pipeline-config.json'):
|
||||
with patch(
|
||||
'langbot.pkg.utils.paths.get_resource_path', return_value='templates/default-pipeline-config.json'
|
||||
):
|
||||
bot_uuid = await service.create_pipeline({'name': 'New Pipeline'})
|
||||
|
||||
# Verify
|
||||
@@ -286,7 +278,9 @@ class TestPipelineServiceCreatePipeline:
|
||||
|
||||
service = PipelineService(ap)
|
||||
service.get_pipelines = AsyncMock(return_value=[])
|
||||
service.get_pipeline = AsyncMock(return_value={'uuid': 'new-uuid', 'name': 'Default Pipeline', 'is_default': True})
|
||||
service.get_pipeline = AsyncMock(
|
||||
return_value={'uuid': 'new-uuid', 'name': 'Default Pipeline', 'is_default': True}
|
||||
)
|
||||
|
||||
ap.persistence_mgr.execute_async = AsyncMock()
|
||||
ap.persistence_mgr.serialize_model = Mock(
|
||||
@@ -296,7 +290,9 @@ class TestPipelineServiceCreatePipeline:
|
||||
# Mock the file read
|
||||
default_config = {}
|
||||
with patch('builtins.open', mock_open(read_data=json.dumps(default_config))):
|
||||
with patch('langbot.pkg.utils.paths.get_resource_path', return_value='templates/default-pipeline-config.json'):
|
||||
with patch(
|
||||
'langbot.pkg.utils.paths.get_resource_path', return_value='templates/default-pipeline-config.json'
|
||||
):
|
||||
await service.create_pipeline({'name': 'Default Pipeline'}, default=True)
|
||||
|
||||
# Verify - execute was called
|
||||
@@ -316,10 +312,12 @@ class TestPipelineServiceCreatePipeline:
|
||||
|
||||
service = PipelineService(ap)
|
||||
service.get_pipelines = AsyncMock(return_value=[])
|
||||
service.get_pipeline = AsyncMock(return_value={
|
||||
'uuid': 'new-uuid',
|
||||
'extensions_preferences': {},
|
||||
})
|
||||
service.get_pipeline = AsyncMock(
|
||||
return_value={
|
||||
'uuid': 'new-uuid',
|
||||
'extensions_preferences': {},
|
||||
}
|
||||
)
|
||||
|
||||
insert_params = []
|
||||
|
||||
@@ -339,7 +337,9 @@ class TestPipelineServiceCreatePipeline:
|
||||
|
||||
default_config = {}
|
||||
with patch('builtins.open', mock_open(read_data=json.dumps(default_config))):
|
||||
with patch('langbot.pkg.utils.paths.get_resource_path', return_value='templates/default-pipeline-config.json'):
|
||||
with patch(
|
||||
'langbot.pkg.utils.paths.get_resource_path', return_value='templates/default-pipeline-config.json'
|
||||
):
|
||||
await service.create_pipeline({'name': 'New Pipeline'})
|
||||
|
||||
assert len(insert_params) == 1
|
||||
@@ -348,11 +348,14 @@ class TestPipelineServiceCreatePipeline:
|
||||
'enable_all_mcp_servers': True,
|
||||
'plugins': [],
|
||||
'mcp_servers': [],
|
||||
'mcp_resources': [],
|
||||
'mcp_resource_agent_read_enabled': True,
|
||||
}
|
||||
|
||||
|
||||
class _MockResultWithBots:
|
||||
"""Helper class to mock SQLAlchemy result with iterable .all() method."""
|
||||
|
||||
def __init__(self, bots_list):
|
||||
self._bots_list = bots_list
|
||||
|
||||
@@ -428,6 +431,7 @@ class TestPipelineServiceUpdatePipeline:
|
||||
# 1. UPDATE (line 125) - returns Mock (no result needed)
|
||||
# 2. SELECT bots (line 136) - returns bot_result with .all()
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -528,13 +532,7 @@ class TestPipelineServiceCopyPipeline:
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.instance_config = SimpleNamespace()
|
||||
ap.instance_config.data = {
|
||||
'system': {
|
||||
'limitation': {
|
||||
'max_pipelines': 2
|
||||
}
|
||||
}
|
||||
}
|
||||
ap.instance_config.data = {'system': {'limitation': {'max_pipelines': 2}}}
|
||||
ap.pipeline_mgr = SimpleNamespace()
|
||||
ap.pipeline_mgr.load_pipeline = AsyncMock()
|
||||
ap.ver_mgr = SimpleNamespace()
|
||||
@@ -542,10 +540,12 @@ class TestPipelineServiceCopyPipeline:
|
||||
|
||||
service = PipelineService(ap)
|
||||
# Mock get_pipelines to return 2 pipelines
|
||||
service.get_pipelines = AsyncMock(return_value=[
|
||||
{'uuid': 'uuid-1', 'name': 'Pipeline 1'},
|
||||
{'uuid': 'uuid-2', 'name': 'Pipeline 2'},
|
||||
])
|
||||
service.get_pipelines = AsyncMock(
|
||||
return_value=[
|
||||
{'uuid': 'uuid-1', 'name': 'Pipeline 1'},
|
||||
{'uuid': 'uuid-2', 'name': 'Pipeline 2'},
|
||||
]
|
||||
)
|
||||
|
||||
# Execute & Verify
|
||||
with pytest.raises(ValueError, match='Maximum number of pipelines'):
|
||||
@@ -642,9 +642,7 @@ class TestPipelineServiceCopyPipeline:
|
||||
service = PipelineService(ap)
|
||||
service.get_pipelines = AsyncMock(return_value=[])
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_mock_result(first_item=original))
|
||||
ap.persistence_mgr.serialize_model = Mock(
|
||||
return_value={'uuid': 'copy-uuid', 'is_default': False}
|
||||
)
|
||||
ap.persistence_mgr.serialize_model = Mock(return_value={'uuid': 'copy-uuid', 'is_default': False})
|
||||
|
||||
service.get_pipeline = AsyncMock(return_value={'uuid': 'copy-uuid', 'is_default': False})
|
||||
|
||||
@@ -681,11 +679,10 @@ class TestPipelineServiceUpdatePipelineExtensions:
|
||||
ap.pipeline_mgr.remove_pipeline = AsyncMock()
|
||||
ap.pipeline_mgr.load_pipeline = AsyncMock()
|
||||
|
||||
original_pipeline = _create_mock_pipeline(
|
||||
extensions_preferences={'enable_all_plugins': True, 'plugins': []}
|
||||
)
|
||||
original_pipeline = _create_mock_pipeline(extensions_preferences={'enable_all_plugins': True, 'plugins': []})
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -700,7 +697,7 @@ class TestPipelineServiceUpdatePipelineExtensions:
|
||||
'extensions_preferences': {
|
||||
'enable_all_plugins': False,
|
||||
'plugins': [{'plugin_uuid': 'plugin-1'}],
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -711,7 +708,7 @@ class TestPipelineServiceUpdatePipelineExtensions:
|
||||
'extensions_preferences': {
|
||||
'enable_all_plugins': False,
|
||||
'plugins': [{'plugin_uuid': 'plugin-1'}],
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -738,6 +735,7 @@ class TestPipelineServiceUpdatePipelineExtensions:
|
||||
original_pipeline = _create_mock_pipeline()
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -752,7 +750,7 @@ class TestPipelineServiceUpdatePipelineExtensions:
|
||||
'extensions_preferences': {
|
||||
'enable_all_mcp_servers': False,
|
||||
'mcp_servers': ['mcp-server-1'],
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -794,6 +792,7 @@ class TestPipelineServiceUpdatePipelineExtensions:
|
||||
)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -817,6 +816,47 @@ class TestPipelineServiceUpdatePipelineExtensions:
|
||||
# Verify - persistence was called
|
||||
ap.persistence_mgr.execute_async.assert_called()
|
||||
|
||||
async def test_update_extensions_preserves_mcp_resource_agent_read_when_omitted(self):
|
||||
"""Does not reset mcp_resource_agent_read_enabled when omitted by older clients."""
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.pipeline_mgr = SimpleNamespace()
|
||||
ap.pipeline_mgr.remove_pipeline = AsyncMock()
|
||||
ap.pipeline_mgr.load_pipeline = AsyncMock()
|
||||
|
||||
original_pipeline = _create_mock_pipeline(
|
||||
extensions_preferences={
|
||||
'enable_all_plugins': True,
|
||||
'enable_all_mcp_servers': True,
|
||||
'plugins': [],
|
||||
'mcp_servers': [],
|
||||
'mcp_resources': [{'server_uuid': 'srv-1', 'uri': 'file:///README.md'}],
|
||||
'mcp_resource_agent_read_enabled': False,
|
||||
}
|
||||
)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
if call_count == 1:
|
||||
return _create_mock_result(first_item=original_pipeline)
|
||||
return Mock()
|
||||
|
||||
ap.persistence_mgr.execute_async = AsyncMock(side_effect=mock_execute)
|
||||
ap.persistence_mgr.serialize_model = Mock(return_value={'uuid': 'test-uuid'})
|
||||
|
||||
service = PipelineService(ap)
|
||||
service.get_pipeline = AsyncMock(return_value={'uuid': 'test-uuid'})
|
||||
|
||||
await service.update_pipeline_extensions('test-uuid', bound_plugins=[])
|
||||
|
||||
assert original_pipeline.extensions_preferences['mcp_resource_agent_read_enabled'] is False
|
||||
assert original_pipeline.extensions_preferences['mcp_resources'] == [
|
||||
{'server_uuid': 'srv-1', 'uri': 'file:///README.md'}
|
||||
]
|
||||
|
||||
|
||||
class TestDefaultStageOrder:
|
||||
"""Tests for default_stage_order constant."""
|
||||
|
||||
@@ -245,12 +245,14 @@ class TestModelProviderServiceCreateProvider:
|
||||
service = ModelProviderService(ap)
|
||||
|
||||
# Execute
|
||||
provider_uuid = await service.create_provider({
|
||||
'name': 'New Provider',
|
||||
'requester': 'openai',
|
||||
'base_url': 'https://api.openai.com',
|
||||
'api_keys': ['key'],
|
||||
})
|
||||
provider_uuid = await service.create_provider(
|
||||
{
|
||||
'name': 'New Provider',
|
||||
'requester': 'openai',
|
||||
'base_url': 'https://api.openai.com',
|
||||
'api_keys': ['key'],
|
||||
}
|
||||
)
|
||||
|
||||
# Verify - UUID is generated
|
||||
assert provider_uuid is not None
|
||||
@@ -274,12 +276,14 @@ class TestModelProviderServiceCreateProvider:
|
||||
service = ModelProviderService(ap)
|
||||
|
||||
# Execute
|
||||
result_uuid = await service.create_provider({
|
||||
'name': 'Runtime Provider',
|
||||
'requester': 'openai',
|
||||
'base_url': 'https://api.openai.com',
|
||||
'api_keys': ['key'],
|
||||
})
|
||||
result_uuid = await service.create_provider(
|
||||
{
|
||||
'name': 'Runtime Provider',
|
||||
'requester': 'openai',
|
||||
'base_url': 'https://api.openai.com',
|
||||
'api_keys': ['key'],
|
||||
}
|
||||
)
|
||||
|
||||
# Verify - provider added to runtime dict and UUID generated
|
||||
ap.model_mgr.load_provider.assert_called_once()
|
||||
@@ -302,10 +306,13 @@ class TestModelProviderServiceUpdateProvider:
|
||||
service = ModelProviderService(ap)
|
||||
|
||||
# Execute
|
||||
await service.update_provider('existing-uuid', {
|
||||
'uuid': 'should-be-removed', # Will be removed
|
||||
'name': 'Updated Name',
|
||||
})
|
||||
await service.update_provider(
|
||||
'existing-uuid',
|
||||
{
|
||||
'uuid': 'should-be-removed', # Will be removed
|
||||
'name': 'Updated Name',
|
||||
},
|
||||
)
|
||||
|
||||
# Verify - reload called
|
||||
ap.model_mgr.reload_provider.assert_called_once_with('existing-uuid')
|
||||
@@ -364,6 +371,7 @@ class TestModelProviderServiceDeleteProvider:
|
||||
rerank_result.first = Mock(return_value=None)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -396,6 +404,7 @@ class TestModelProviderServiceDeleteProvider:
|
||||
rerank_result.first = Mock(return_value=Mock(spec=RerankModel)) # Has rerank model
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -454,6 +463,7 @@ class TestModelProviderServiceGetProviderModelCounts:
|
||||
rerank_result.scalar = Mock(return_value=1)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -637,9 +647,7 @@ class TestModelProviderServiceUpdateSpaceModelProviderApiKeys:
|
||||
await service.update_space_model_provider_api_keys('space-api-key')
|
||||
|
||||
# Verify - update and reload called for Space provider UUID
|
||||
ap.model_mgr.reload_provider.assert_called_once_with(
|
||||
'00000000-0000-0000-0000-000000000000'
|
||||
)
|
||||
ap.model_mgr.reload_provider.assert_called_once_with('00000000-0000-0000-0000-000000000000')
|
||||
|
||||
|
||||
class TestModelProviderServiceScanProviderModels:
|
||||
@@ -795,9 +803,7 @@ class TestModelProviderServiceScanProviderModels:
|
||||
runtime_provider.token_mgr = Mock()
|
||||
runtime_provider.token_mgr.get_token = Mock(return_value='token')
|
||||
runtime_provider.token_mgr.tokens = ['token']
|
||||
runtime_provider.requester.scan_models = AsyncMock(
|
||||
side_effect=NotImplementedError('scan not supported')
|
||||
)
|
||||
runtime_provider.requester.scan_models = AsyncMock(side_effect=NotImplementedError('scan not supported'))
|
||||
ap.model_mgr.load_provider = AsyncMock(return_value=runtime_provider)
|
||||
|
||||
service = ModelProviderService(ap)
|
||||
@@ -848,9 +854,7 @@ class TestModelProviderServiceScanProviderModels:
|
||||
ap.model_mgr.load_provider = AsyncMock(return_value=runtime_provider)
|
||||
|
||||
# Mock existing LLM model
|
||||
ap.llm_model_service.get_llm_models_by_provider = AsyncMock(
|
||||
return_value=[{'name': 'Existing Model'}]
|
||||
)
|
||||
ap.llm_model_service.get_llm_models_by_provider = AsyncMock(return_value=[{'name': 'Existing Model'}])
|
||||
ap.embedding_models_service.get_embedding_models_by_provider = AsyncMock(return_value=[])
|
||||
|
||||
service = ModelProviderService(ap)
|
||||
@@ -863,4 +867,4 @@ class TestModelProviderServiceScanProviderModels:
|
||||
assert existing_model['already_added'] is True
|
||||
|
||||
new_model = next(m for m in result['models'] if m['name'] == 'New Model')
|
||||
assert new_model['already_added'] is False
|
||||
assert new_model['already_added'] is False
|
||||
|
||||
@@ -393,14 +393,16 @@ class TestSpaceServiceRefreshToken:
|
||||
# Mock HTTP response
|
||||
mock_response = MagicMock()
|
||||
mock_response.status = 200
|
||||
mock_response.json = AsyncMock(return_value={
|
||||
'code': 0,
|
||||
'data': {
|
||||
'access_token': 'new_access_token',
|
||||
'refresh_token': 'new_refresh_token',
|
||||
'expires_in': 3600,
|
||||
mock_response.json = AsyncMock(
|
||||
return_value={
|
||||
'code': 0,
|
||||
'data': {
|
||||
'access_token': 'new_access_token',
|
||||
'refresh_token': 'new_refresh_token',
|
||||
'expires_in': 3600,
|
||||
},
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
with patch('langbot.pkg.api.http.service.space.httpclient.get_session') as mock_session:
|
||||
mock_session_obj = MagicMock()
|
||||
@@ -429,10 +431,12 @@ class TestSpaceServiceRefreshToken:
|
||||
# Mock HTTP response with error
|
||||
mock_response = MagicMock()
|
||||
mock_response.status = 200
|
||||
mock_response.json = AsyncMock(return_value={
|
||||
'code': 1,
|
||||
'msg': 'Invalid refresh token',
|
||||
})
|
||||
mock_response.json = AsyncMock(
|
||||
return_value={
|
||||
'code': 1,
|
||||
'msg': 'Invalid refresh token',
|
||||
}
|
||||
)
|
||||
mock_response.text = AsyncMock(return_value='{"code":1,"msg":"Invalid refresh token"}')
|
||||
|
||||
with patch('langbot.pkg.api.http.service.space.httpclient.get_session') as mock_session:
|
||||
@@ -489,14 +493,16 @@ class TestSpaceServiceExchangeOAuthCode:
|
||||
# Mock HTTP response
|
||||
mock_response = MagicMock()
|
||||
mock_response.status = 200
|
||||
mock_response.json = AsyncMock(return_value={
|
||||
'code': 0,
|
||||
'data': {
|
||||
'access_token': 'new_access_token',
|
||||
'refresh_token': 'new_refresh_token',
|
||||
'expires_in': 3600,
|
||||
mock_response.json = AsyncMock(
|
||||
return_value={
|
||||
'code': 0,
|
||||
'data': {
|
||||
'access_token': 'new_access_token',
|
||||
'refresh_token': 'new_refresh_token',
|
||||
'expires_in': 3600,
|
||||
},
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
with patch('langbot.pkg.api.http.service.space.httpclient.get_session') as mock_session:
|
||||
mock_session_obj = MagicMock()
|
||||
@@ -555,13 +561,15 @@ class TestSpaceServiceGetUserInfoRaw:
|
||||
# Mock HTTP response
|
||||
mock_response = MagicMock()
|
||||
mock_response.status = 200
|
||||
mock_response.json = AsyncMock(return_value={
|
||||
'code': 0,
|
||||
'data': {
|
||||
'email': 'test@example.com',
|
||||
'credits': 100,
|
||||
mock_response.json = AsyncMock(
|
||||
return_value={
|
||||
'code': 0,
|
||||
'data': {
|
||||
'email': 'test@example.com',
|
||||
'credits': 100,
|
||||
},
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
with patch('langbot.pkg.api.http.service.space.httpclient.get_session') as mock_session:
|
||||
mock_session_obj = MagicMock()
|
||||
@@ -669,27 +677,29 @@ class TestSpaceServiceGetModels:
|
||||
# Mock HTTP response with proper model data matching SpaceModel schema
|
||||
mock_response = MagicMock()
|
||||
mock_response.status = 200
|
||||
mock_response.json = AsyncMock(return_value={
|
||||
'code': 0,
|
||||
'data': {
|
||||
'models': [
|
||||
{
|
||||
'uuid': 'uuid-1',
|
||||
'model_id': 'model-1',
|
||||
'provider': 'provider-1',
|
||||
'category': 'chat',
|
||||
'status': 'active',
|
||||
},
|
||||
{
|
||||
'uuid': 'uuid-2',
|
||||
'model_id': 'model-2',
|
||||
'provider': 'provider-2',
|
||||
'category': 'chat',
|
||||
'status': 'active',
|
||||
},
|
||||
]
|
||||
mock_response.json = AsyncMock(
|
||||
return_value={
|
||||
'code': 0,
|
||||
'data': {
|
||||
'models': [
|
||||
{
|
||||
'uuid': 'uuid-1',
|
||||
'model_id': 'model-1',
|
||||
'provider': 'provider-1',
|
||||
'category': 'chat',
|
||||
'status': 'active',
|
||||
},
|
||||
{
|
||||
'uuid': 'uuid-2',
|
||||
'model_id': 'model-2',
|
||||
'provider': 'provider-2',
|
||||
'category': 'chat',
|
||||
'status': 'active',
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
with patch('langbot.pkg.api.http.service.space.httpclient.get_session') as mock_session:
|
||||
mock_session_obj = MagicMock()
|
||||
@@ -775,4 +785,4 @@ class TestSpaceServiceCreditsCache:
|
||||
# Verify - cache updated
|
||||
assert result == 500
|
||||
assert 'test@example.com' in service._credits_cache
|
||||
assert service._credits_cache['test@example.com'][0] == 500
|
||||
assert service._credits_cache['test@example.com'][0] == 500
|
||||
|
||||
@@ -495,6 +495,7 @@ class TestUserServiceCreateOrUpdateSpaceUser:
|
||||
|
||||
# First call (line 138) returns None, second call (line 194) returns new_user
|
||||
call_count = 0
|
||||
|
||||
async def mock_get_by_space_uuid(uuid):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -565,6 +566,7 @@ class TestUserServiceCreateOrUpdateSpaceUser:
|
||||
|
||||
# First call (line 138) returns None, second call (line 194) returns new_user
|
||||
call_count = 0
|
||||
|
||||
async def mock_get_by_space_uuid(uuid):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -605,4 +607,4 @@ class TestUserServiceCreateUserLock:
|
||||
|
||||
# Verify lock exists
|
||||
assert hasattr(service, '_create_user_lock')
|
||||
assert service._create_user_lock is not None
|
||||
assert service._create_user_lock is not None
|
||||
|
||||
@@ -132,6 +132,7 @@ class TestWebhookServiceCreateWebhook:
|
||||
|
||||
# execute_async returns different results
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -181,6 +182,7 @@ class TestWebhookServiceCreateWebhook:
|
||||
)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -217,6 +219,7 @@ class TestWebhookServiceCreateWebhook:
|
||||
created_webhook = _create_mock_webhook(webhook_id=1, enabled=False)
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_execute(query):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
@@ -225,9 +228,7 @@ class TestWebhookServiceCreateWebhook:
|
||||
return _create_mock_result(first_item=created_webhook)
|
||||
|
||||
ap.persistence_mgr.execute_async = AsyncMock(side_effect=mock_execute)
|
||||
ap.persistence_mgr.serialize_model = Mock(
|
||||
return_value={'id': 1, 'enabled': False}
|
||||
)
|
||||
ap.persistence_mgr.serialize_model = Mock(return_value={'id': 1, 'enabled': False})
|
||||
|
||||
service = WebhookService(ap)
|
||||
|
||||
@@ -503,4 +504,4 @@ class TestWebhookServiceGetEnabledWebhooks:
|
||||
result = await service.get_enabled_webhooks()
|
||||
|
||||
# Verify - should be empty (SQL would filter disabled)
|
||||
assert result == []
|
||||
assert result == []
|
||||
|
||||
@@ -12,7 +12,8 @@ from langbot.pkg.api.http.service.apikey import ApiKeyService
|
||||
@pytest.mark.parametrize('api_key', [None, 123, b'lbk_bytes', '', 'plain_key', ' LBK_bad', 'sk-lbk_fake'])
|
||||
async def test_verify_api_key_rejects_non_lbk_keys_without_db_query(api_key):
|
||||
persistence_mgr = SimpleNamespace(execute_async=AsyncMock())
|
||||
service = ApiKeyService(SimpleNamespace(persistence_mgr=persistence_mgr))
|
||||
instance_config = SimpleNamespace(data={'api': {'global_api_key': ''}})
|
||||
service = ApiKeyService(SimpleNamespace(persistence_mgr=persistence_mgr, instance_config=instance_config))
|
||||
|
||||
result = await service.verify_api_key(api_key)
|
||||
|
||||
@@ -32,7 +33,8 @@ async def test_verify_api_key_keeps_db_validation_for_lbk_keys(db_row, expected)
|
||||
query_result = Mock()
|
||||
query_result.first.return_value = db_row
|
||||
persistence_mgr = SimpleNamespace(execute_async=AsyncMock(return_value=query_result))
|
||||
service = ApiKeyService(SimpleNamespace(persistence_mgr=persistence_mgr))
|
||||
instance_config = SimpleNamespace(data={'api': {'global_api_key': ''}})
|
||||
service = ApiKeyService(SimpleNamespace(persistence_mgr=persistence_mgr, instance_config=instance_config))
|
||||
|
||||
result = await service.verify_api_key('lbk_valid_format')
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from langbot.pkg.api.http.controller.groups.box_visibility import should_hide_box_runtime_status
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('edition', 'box_enabled', 'expected'),
|
||||
[
|
||||
('cloud', False, True),
|
||||
('cloud', True, False),
|
||||
('cloud', None, False),
|
||||
('community', False, False),
|
||||
],
|
||||
)
|
||||
def test_should_hide_box_runtime_status(edition, box_enabled, expected):
|
||||
assert should_hide_box_runtime_status(edition, box_enabled) is expected
|
||||
@@ -0,0 +1,76 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import types
|
||||
from importlib import import_module
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
import quart
|
||||
|
||||
core_app_module = types.ModuleType('langbot.pkg.core.app')
|
||||
core_app_module.Application = object
|
||||
sys.modules.setdefault('langbot.pkg.core.app', core_app_module)
|
||||
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
async def _create_test_client(mcp_service: SimpleNamespace):
|
||||
app = quart.Quart(__name__)
|
||||
user_service = SimpleNamespace(
|
||||
verify_jwt_token=AsyncMock(return_value='test@example.com'),
|
||||
get_user_by_email=AsyncMock(return_value=SimpleNamespace(user='test@example.com')),
|
||||
)
|
||||
ap = SimpleNamespace(mcp_service=mcp_service, user_service=user_service)
|
||||
MCPRouterGroup = import_module('langbot.pkg.api.http.controller.groups.resources.mcp').MCPRouterGroup
|
||||
group = MCPRouterGroup(ap, app)
|
||||
await group.initialize()
|
||||
return app.test_client()
|
||||
|
||||
|
||||
async def test_mcp_server_route_accepts_encoded_slash_name():
|
||||
mcp_service = SimpleNamespace(
|
||||
get_mcp_server_by_name=AsyncMock(
|
||||
return_value={
|
||||
'uuid': 'test-uuid',
|
||||
'name': 'pab1it0/prometheus',
|
||||
'enable': True,
|
||||
'mode': 'stdio',
|
||||
'extra_args': {},
|
||||
}
|
||||
)
|
||||
)
|
||||
client = await _create_test_client(mcp_service)
|
||||
|
||||
response = await client.get(
|
||||
'/api/v1/mcp/servers/pab1it0%2Fprometheus',
|
||||
headers={'Authorization': 'Bearer test-token'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
mcp_service.get_mcp_server_by_name.assert_awaited_once_with('pab1it0/prometheus')
|
||||
payload = await response.get_json()
|
||||
assert payload['data']['server']['name'] == 'pab1it0/prometheus'
|
||||
|
||||
|
||||
async def test_mcp_resource_route_accepts_encoded_slash_name():
|
||||
mcp_service = SimpleNamespace(
|
||||
get_mcp_server_by_name=AsyncMock(),
|
||||
get_mcp_server_resources=AsyncMock(return_value=[]),
|
||||
get_mcp_server_resource_templates=AsyncMock(return_value=[]),
|
||||
get_runtime_info=AsyncMock(return_value={'resource_capabilities': {'subscribe': False}}),
|
||||
)
|
||||
client = await _create_test_client(mcp_service)
|
||||
|
||||
response = await client.get(
|
||||
'/api/v1/mcp/servers/pab1it0%2Fprometheus/resources',
|
||||
headers={'Authorization': 'Bearer test-token'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
mcp_service.get_mcp_server_by_name.assert_not_awaited()
|
||||
mcp_service.get_mcp_server_resources.assert_awaited_once_with('pab1it0/prometheus')
|
||||
payload = await response.get_json()
|
||||
assert payload['data']['resource_capabilities'] == {'subscribe': False}
|
||||
@@ -0,0 +1,106 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from langbot_plugin.box.client import ActionRPCBoxClient
|
||||
from langbot.pkg.box.connector import BoxRuntimeConnector
|
||||
|
||||
|
||||
def make_app(logger: Mock, runtime_endpoint: str = ''):
|
||||
return SimpleNamespace(
|
||||
logger=logger,
|
||||
instance_config=SimpleNamespace(
|
||||
data={
|
||||
'box': {
|
||||
'backend': 'local',
|
||||
'runtime': {'endpoint': runtime_endpoint},
|
||||
'local': {
|
||||
'profile': 'default',
|
||||
'allowed_mount_roots': [],
|
||||
'default_workspace': '',
|
||||
},
|
||||
'e2b': {'api_key': '', 'api_url': '', 'template': ''},
|
||||
}
|
||||
}
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def test_box_runtime_connector_stdio_when_no_url(monkeypatch: pytest.MonkeyPatch):
|
||||
"""Without runtime.endpoint, on a non-Docker Unix platform, use stdio."""
|
||||
monkeypatch.setattr('langbot.pkg.utils.platform.get_platform', lambda: 'linux')
|
||||
monkeypatch.setattr('langbot.pkg.utils.platform.standalone_box', False)
|
||||
connector = BoxRuntimeConnector(make_app(Mock()))
|
||||
|
||||
assert connector._uses_websocket() is False
|
||||
assert isinstance(connector.client, ActionRPCBoxClient)
|
||||
|
||||
|
||||
def test_box_runtime_connector_ws_when_url_configured(monkeypatch: pytest.MonkeyPatch):
|
||||
"""With an explicit runtime.endpoint, always use WebSocket."""
|
||||
monkeypatch.setattr('langbot.pkg.utils.platform.get_platform', lambda: 'linux')
|
||||
monkeypatch.setattr('langbot.pkg.utils.platform.standalone_box', False)
|
||||
logger = Mock()
|
||||
connector = BoxRuntimeConnector(make_app(logger, runtime_endpoint='http://box-runtime:5410'))
|
||||
|
||||
assert connector._uses_websocket() is True
|
||||
assert isinstance(connector.client, ActionRPCBoxClient)
|
||||
|
||||
|
||||
def test_box_runtime_connector_ws_in_docker(monkeypatch: pytest.MonkeyPatch):
|
||||
"""Inside Docker (no explicit URL), use WebSocket to reach a sibling container."""
|
||||
monkeypatch.setattr('langbot.pkg.utils.platform.get_platform', lambda: 'docker')
|
||||
monkeypatch.setattr('langbot.pkg.utils.platform.standalone_box', False)
|
||||
connector = BoxRuntimeConnector(make_app(Mock()))
|
||||
|
||||
assert connector._uses_websocket() is True
|
||||
assert connector.ws_relay_base_url == 'http://langbot_box:5410'
|
||||
|
||||
|
||||
def test_box_runtime_connector_ws_with_standalone_flag(monkeypatch: pytest.MonkeyPatch):
|
||||
"""With --standalone-box flag, use WebSocket even on a local Unix platform."""
|
||||
monkeypatch.setattr('langbot.pkg.utils.platform.get_platform', lambda: 'linux')
|
||||
monkeypatch.setattr('langbot.pkg.utils.platform.standalone_box', True)
|
||||
connector = BoxRuntimeConnector(make_app(Mock()))
|
||||
|
||||
assert connector._uses_websocket() is True
|
||||
|
||||
|
||||
def test_box_runtime_connector_ws_relay_url_default(monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr('langbot.pkg.utils.platform.get_platform', lambda: 'linux')
|
||||
monkeypatch.setattr('langbot.pkg.utils.platform.standalone_box', False)
|
||||
connector = BoxRuntimeConnector(make_app(Mock()))
|
||||
|
||||
assert connector.ws_relay_base_url == 'http://127.0.0.1:5410'
|
||||
|
||||
|
||||
def test_box_runtime_connector_ws_relay_url_explicit(monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr('langbot.pkg.utils.platform.get_platform', lambda: 'linux')
|
||||
monkeypatch.setattr('langbot.pkg.utils.platform.standalone_box', False)
|
||||
connector = BoxRuntimeConnector(make_app(Mock(), runtime_endpoint='http://box-runtime:5410'))
|
||||
assert connector.ws_relay_base_url == 'http://box-runtime:5410'
|
||||
|
||||
|
||||
def test_box_runtime_connector_dispose_terminates_subprocess(monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr('langbot.pkg.utils.platform.get_platform', lambda: 'linux')
|
||||
monkeypatch.setattr('langbot.pkg.utils.platform.standalone_box', False)
|
||||
logger = Mock()
|
||||
connector = BoxRuntimeConnector(make_app(logger))
|
||||
subprocess = Mock()
|
||||
subprocess.returncode = None
|
||||
handler_task = Mock()
|
||||
ctrl_task = Mock()
|
||||
connector._subprocess = subprocess
|
||||
connector._handler_task = handler_task
|
||||
connector._ctrl_task = ctrl_task
|
||||
|
||||
connector.dispose()
|
||||
|
||||
subprocess.terminate.assert_called_once()
|
||||
handler_task.cancel.assert_called_once()
|
||||
ctrl_task.cancel.assert_called_once()
|
||||
assert connector._handler_task is None
|
||||
assert connector._ctrl_task is None
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,149 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from langbot.pkg.box.workspace import (
|
||||
BoxWorkspaceSession,
|
||||
classify_python_workspace,
|
||||
infer_workspace_host_path,
|
||||
rewrite_mounted_path,
|
||||
wrap_python_command_with_env,
|
||||
)
|
||||
|
||||
|
||||
def test_rewrite_mounted_path_translates_host_prefix():
|
||||
result = rewrite_mounted_path('/tmp/demo/project/app.py', '/tmp/demo/project')
|
||||
assert result == '/workspace/app.py'
|
||||
|
||||
|
||||
def test_infer_workspace_host_path_unwraps_virtualenv_bin_dir():
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
project_root = os.path.join(tmpdir, 'project')
|
||||
os.makedirs(os.path.join(project_root, '.venv', 'bin'))
|
||||
python_bin = os.path.join(project_root, '.venv', 'bin', 'python')
|
||||
script = os.path.join(project_root, 'server.py')
|
||||
|
||||
with open(python_bin, 'w', encoding='utf-8') as handle:
|
||||
handle.write('')
|
||||
with open(script, 'w', encoding='utf-8') as handle:
|
||||
handle.write('print("ok")\n')
|
||||
|
||||
result = infer_workspace_host_path(python_bin, [script])
|
||||
|
||||
assert result == os.path.realpath(project_root)
|
||||
|
||||
|
||||
def test_classify_python_workspace_detects_package_and_requirements():
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
assert classify_python_workspace(tmpdir) is None
|
||||
|
||||
with open(os.path.join(tmpdir, 'requirements.txt'), 'w', encoding='utf-8') as handle:
|
||||
handle.write('requests\n')
|
||||
assert classify_python_workspace(tmpdir) == 'requirements'
|
||||
|
||||
with open(os.path.join(tmpdir, 'pyproject.toml'), 'w', encoding='utf-8') as handle:
|
||||
handle.write('[project]\nname = "demo"\n')
|
||||
assert classify_python_workspace(tmpdir) == 'package'
|
||||
|
||||
|
||||
def test_wrap_python_command_with_env_contains_bootstrap_and_command():
|
||||
command = wrap_python_command_with_env('python script.py')
|
||||
|
||||
assert '_LB_SYSTEM_PYTHON="$(command -v python3 || command -v python || true)"' in command
|
||||
assert '"$_LB_SYSTEM_PYTHON" -m venv "$_LB_VENV_DIR"' in command
|
||||
assert 'kill -0 "$_LB_LOCK_OWNER"' in command
|
||||
assert 'export VIRTUAL_ENV="$_LB_VENV_DIR"' in command
|
||||
assert command.rstrip().endswith('python script.py')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_workspace_session_execute_for_query_uses_session_payload():
|
||||
box_service = SimpleNamespace(execute_spec_payload=AsyncMock(return_value={'ok': True}))
|
||||
workspace = BoxWorkspaceSession(
|
||||
box_service,
|
||||
'skill-person_123-demo',
|
||||
host_path='/tmp/project',
|
||||
host_path_mode='rw',
|
||||
env={'FOO': 'bar'},
|
||||
)
|
||||
|
||||
query = SimpleNamespace(query_id='q1')
|
||||
result = await workspace.execute_for_query(query, 'python run.py', workdir='/workspace', timeout_sec=30)
|
||||
|
||||
assert result == {'ok': True}
|
||||
payload = box_service.execute_spec_payload.await_args.args[0]
|
||||
assert payload == {
|
||||
'session_id': 'skill-person_123-demo',
|
||||
'workdir': '/workspace',
|
||||
'env': {'FOO': 'bar'},
|
||||
'persistent': False,
|
||||
'host_path': '/tmp/project',
|
||||
'host_path_mode': 'rw',
|
||||
'cmd': 'python run.py',
|
||||
'timeout_sec': 30,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_workspace_session_start_managed_process_rewrites_command_and_args():
|
||||
box_service = SimpleNamespace(start_managed_process=AsyncMock(return_value={'status': 'running'}))
|
||||
workspace = BoxWorkspaceSession(
|
||||
box_service,
|
||||
'mcp-u1',
|
||||
host_path='/tmp/project',
|
||||
host_path_mode='ro',
|
||||
)
|
||||
|
||||
result = await workspace.start_managed_process(
|
||||
'/tmp/project/.venv/bin/python',
|
||||
['/tmp/project/server.py', '--config', '/tmp/project/config.json'],
|
||||
env={'TOKEN': '1'},
|
||||
)
|
||||
|
||||
assert result == {'status': 'running'}
|
||||
session_id = box_service.start_managed_process.await_args.args[0]
|
||||
payload = box_service.start_managed_process.await_args.args[1]
|
||||
assert session_id == 'mcp-u1'
|
||||
assert payload == {
|
||||
'command': 'python',
|
||||
'args': ['/workspace/server.py', '--config', '/workspace/config.json'],
|
||||
'env': {'TOKEN': '1'},
|
||||
'cwd': '/workspace',
|
||||
'process_id': 'default',
|
||||
}
|
||||
|
||||
|
||||
def test_workspace_session_build_session_payload_keeps_generic_workspace_shape():
|
||||
workspace = BoxWorkspaceSession(
|
||||
Mock(),
|
||||
'workspace-1',
|
||||
host_path='/tmp/project',
|
||||
host_path_mode='rw',
|
||||
env={'FOO': 'bar'},
|
||||
network='on',
|
||||
read_only_rootfs=False,
|
||||
image='python:3.11',
|
||||
cpus=1.0,
|
||||
memory_mb=512,
|
||||
pids_limit=128,
|
||||
)
|
||||
|
||||
assert workspace.build_session_payload() == {
|
||||
'session_id': 'workspace-1',
|
||||
'workdir': '/workspace',
|
||||
'env': {'FOO': 'bar'},
|
||||
'persistent': False,
|
||||
'network': 'on',
|
||||
'read_only_rootfs': False,
|
||||
'host_path': '/tmp/project',
|
||||
'host_path_mode': 'rw',
|
||||
'image': 'python:3.11',
|
||||
'cpus': 1.0,
|
||||
'memory_mb': 512,
|
||||
'pids_limit': 128,
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
# Unit tests for command module
|
||||
# Unit tests for command module
|
||||
|
||||
@@ -529,4 +529,4 @@ class TestEmptyAndEdgeInputs:
|
||||
|
||||
# Should yield CommandNotFoundError (no such command registered)
|
||||
assert len(results) == 1
|
||||
assert results[0].error is not None
|
||||
assert results[0].error is not None
|
||||
|
||||
@@ -197,6 +197,7 @@ class TestCommandOperatorBase:
|
||||
op = TestOperator(None)
|
||||
# Should not raise
|
||||
import asyncio
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(op.initialize())
|
||||
|
||||
def test_execute_is_abstract(self):
|
||||
@@ -299,4 +300,4 @@ class TestMultipleOperators:
|
||||
yield None
|
||||
|
||||
assert AdminOperator.lowest_privilege == 2
|
||||
assert SubOperator.lowest_privilege == 1
|
||||
assert SubOperator.lowest_privilege == 1
|
||||
|
||||
@@ -25,7 +25,7 @@ class TestYAMLConfigFile:
|
||||
@pytest.mark.asyncio
|
||||
async def test_valid_yaml_loads(self, tmp_path):
|
||||
"""Valid YAML config should load correctly."""
|
||||
config_file = tmp_path / "test_config.yaml"
|
||||
config_file = tmp_path / 'test_config.yaml'
|
||||
|
||||
# Write valid YAML
|
||||
config_file.write_text("""
|
||||
@@ -51,7 +51,7 @@ settings:
|
||||
@pytest.mark.asyncio
|
||||
async def test_invalid_yaml_raises_error(self, tmp_path):
|
||||
"""Invalid YAML should raise clear error."""
|
||||
config_file = tmp_path / "invalid.yaml"
|
||||
config_file = tmp_path / 'invalid.yaml'
|
||||
|
||||
# Write invalid YAML (unclosed bracket)
|
||||
config_file.write_text("""
|
||||
@@ -67,13 +67,13 @@ settings:
|
||||
template_data={'name': 'default'},
|
||||
)
|
||||
|
||||
with pytest.raises(Exception, match="Syntax error"):
|
||||
with pytest.raises(Exception, match='Syntax error'):
|
||||
await yaml_file.load(completion=False)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_config_creates_from_template(self, tmp_path):
|
||||
"""Missing config file should be created from template."""
|
||||
config_file = tmp_path / "new_config.yaml"
|
||||
config_file = tmp_path / 'new_config.yaml'
|
||||
|
||||
# File doesn't exist yet
|
||||
assert not config_file.exists()
|
||||
@@ -92,7 +92,7 @@ settings:
|
||||
@pytest.mark.asyncio
|
||||
async def test_template_completion(self, tmp_path):
|
||||
"""Config should be completed with template defaults."""
|
||||
config_file = tmp_path / "partial.yaml"
|
||||
config_file = tmp_path / 'partial.yaml'
|
||||
|
||||
# Write partial config missing some template keys
|
||||
config_file.write_text("""
|
||||
@@ -115,7 +115,7 @@ name: custom_name
|
||||
@pytest.mark.asyncio
|
||||
async def test_yaml_save(self, tmp_path):
|
||||
"""YAML config can be saved."""
|
||||
config_file = tmp_path / "save_test.yaml"
|
||||
config_file = tmp_path / 'save_test.yaml'
|
||||
|
||||
yaml_file = YAMLConfigFile(
|
||||
str(config_file),
|
||||
@@ -131,7 +131,7 @@ name: custom_name
|
||||
|
||||
def test_yaml_save_sync(self, tmp_path):
|
||||
"""YAML config can be saved synchronously."""
|
||||
config_file = tmp_path / "sync_save.yaml"
|
||||
config_file = tmp_path / 'sync_save.yaml'
|
||||
|
||||
yaml_file = YAMLConfigFile(
|
||||
str(config_file),
|
||||
@@ -151,14 +151,18 @@ class TestJSONConfigFile:
|
||||
@pytest.mark.asyncio
|
||||
async def test_valid_json_loads(self, tmp_path):
|
||||
"""Valid JSON config should load correctly."""
|
||||
config_file = tmp_path / "test_config.json"
|
||||
config_file = tmp_path / 'test_config.json'
|
||||
|
||||
# Write valid JSON
|
||||
config_file.write_text(json.dumps({
|
||||
'name': 'json_app',
|
||||
'version': '1.0',
|
||||
'settings': {'debug': True, 'port': 8080},
|
||||
}))
|
||||
config_file.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
'name': 'json_app',
|
||||
'version': '1.0',
|
||||
'settings': {'debug': True, 'port': 8080},
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
json_file = JSONConfigFile(
|
||||
str(config_file),
|
||||
@@ -174,7 +178,7 @@ class TestJSONConfigFile:
|
||||
@pytest.mark.asyncio
|
||||
async def test_invalid_json_raises_error(self, tmp_path):
|
||||
"""Invalid JSON should raise clear error."""
|
||||
config_file = tmp_path / "invalid.json"
|
||||
config_file = tmp_path / 'invalid.json'
|
||||
|
||||
# Write invalid JSON (missing closing brace)
|
||||
config_file.write_text('{"name": "test", "unclosed": ')
|
||||
@@ -184,13 +188,13 @@ class TestJSONConfigFile:
|
||||
template_data={'name': 'default'},
|
||||
)
|
||||
|
||||
with pytest.raises(Exception, match="Syntax error"):
|
||||
with pytest.raises(Exception, match='Syntax error'):
|
||||
await json_file.load(completion=False)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_json_creates_from_template(self, tmp_path):
|
||||
"""Missing JSON file should be created from template."""
|
||||
config_file = tmp_path / "new_config.json"
|
||||
config_file = tmp_path / 'new_config.json'
|
||||
|
||||
json_file = JSONConfigFile(
|
||||
str(config_file),
|
||||
@@ -205,7 +209,7 @@ class TestJSONConfigFile:
|
||||
@pytest.mark.asyncio
|
||||
async def test_json_save(self, tmp_path):
|
||||
"""JSON config can be saved."""
|
||||
config_file = tmp_path / "save_test.json"
|
||||
config_file = tmp_path / 'save_test.json'
|
||||
|
||||
json_file = JSONConfigFile(
|
||||
str(config_file),
|
||||
@@ -226,7 +230,7 @@ class TestConfigManager:
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_manager_load(self, tmp_path):
|
||||
"""ConfigManager loads config correctly."""
|
||||
config_file = tmp_path / "manager_test.yaml"
|
||||
config_file = tmp_path / 'manager_test.yaml'
|
||||
config_file.write_text('name: managed_app\nversion: "1.0"\n')
|
||||
|
||||
yaml_file = YAMLConfigFile(
|
||||
@@ -243,7 +247,7 @@ class TestConfigManager:
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_manager_dump(self, tmp_path):
|
||||
"""ConfigManager can dump config."""
|
||||
config_file = tmp_path / "dump_test.yaml"
|
||||
config_file = tmp_path / 'dump_test.yaml'
|
||||
|
||||
yaml_file = YAMLConfigFile(
|
||||
str(config_file),
|
||||
@@ -260,7 +264,7 @@ class TestConfigManager:
|
||||
|
||||
def test_config_manager_dump_sync(self, tmp_path):
|
||||
"""ConfigManager can dump config synchronously."""
|
||||
config_file = tmp_path / "sync_dump.yaml"
|
||||
config_file = tmp_path / 'sync_dump.yaml'
|
||||
|
||||
yaml_file = YAMLConfigFile(
|
||||
str(config_file),
|
||||
@@ -280,7 +284,7 @@ class TestConfigExists:
|
||||
|
||||
def test_yaml_exists_true(self, tmp_path):
|
||||
"""exists() returns True for existing file."""
|
||||
config_file = tmp_path / "exists.yaml"
|
||||
config_file = tmp_path / 'exists.yaml'
|
||||
config_file.write_text('name: test')
|
||||
|
||||
yaml_file = YAMLConfigFile(str(config_file), template_data={})
|
||||
@@ -288,14 +292,14 @@ class TestConfigExists:
|
||||
|
||||
def test_yaml_exists_false(self, tmp_path):
|
||||
"""exists() returns False for missing file."""
|
||||
config_file = tmp_path / "missing.yaml"
|
||||
config_file = tmp_path / 'missing.yaml'
|
||||
|
||||
yaml_file = YAMLConfigFile(str(config_file), template_data={})
|
||||
assert yaml_file.exists() is False
|
||||
|
||||
def test_json_exists_true(self, tmp_path):
|
||||
"""exists() returns True for existing JSON file."""
|
||||
config_file = tmp_path / "exists.json"
|
||||
config_file = tmp_path / 'exists.json'
|
||||
config_file.write_text('{}')
|
||||
|
||||
json_file = JSONConfigFile(str(config_file), template_data={})
|
||||
@@ -303,7 +307,7 @@ class TestConfigExists:
|
||||
|
||||
def test_json_exists_false(self, tmp_path):
|
||||
"""exists() returns False for missing JSON file."""
|
||||
config_file = tmp_path / "missing.json"
|
||||
config_file = tmp_path / 'missing.json'
|
||||
|
||||
json_file = JSONConfigFile(str(config_file), template_data={})
|
||||
assert json_file.exists() is False
|
||||
assert json_file.exists() is False
|
||||
|
||||
@@ -1 +1 @@
|
||||
"""Core module unit tests."""
|
||||
"""Core module unit tests."""
|
||||
|
||||
@@ -4,6 +4,7 @@ Tests cover:
|
||||
- _get_positive_int_config() validation
|
||||
- _get_positive_float_config() validation
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import Mock
|
||||
@@ -188,4 +189,4 @@ class TestGetPositiveFloatConfig:
|
||||
result = app._get_positive_float_config('not-a-number', default=1.5, name='test.config')
|
||||
|
||||
assert result == 1.5
|
||||
mock_logger.warning.assert_called_once()
|
||||
mock_logger.warning.assert_called_once()
|
||||
|
||||
@@ -27,6 +27,7 @@ class TestCheckDeps:
|
||||
from langbot.pkg.core.bootutils.deps import check_deps
|
||||
|
||||
import asyncio
|
||||
|
||||
result = asyncio.get_event_loop().run_until_complete(check_deps())
|
||||
|
||||
assert result == []
|
||||
@@ -46,6 +47,7 @@ class TestCheckDeps:
|
||||
from langbot.pkg.core.bootutils.deps import check_deps
|
||||
|
||||
import asyncio
|
||||
|
||||
result = asyncio.get_event_loop().run_until_complete(check_deps())
|
||||
|
||||
assert 'requests' in result
|
||||
@@ -61,6 +63,7 @@ class TestCheckDeps:
|
||||
from langbot.pkg.core.bootutils.deps import check_deps, required_deps
|
||||
|
||||
import asyncio
|
||||
|
||||
result = asyncio.get_event_loop().run_until_complete(check_deps())
|
||||
|
||||
# Should include all required_deps keys
|
||||
@@ -107,6 +110,7 @@ class TestPrecheckPluginDeps:
|
||||
with patch('os.path.exists', return_value=False):
|
||||
with patch('langbot.pkg.core.bootutils.deps.pkgmgr.install_requirements') as mock_install:
|
||||
import asyncio
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(precheck_plugin_deps())
|
||||
|
||||
mock_install.assert_not_called()
|
||||
@@ -129,6 +133,7 @@ class TestPrecheckPluginDeps:
|
||||
with patch('os.listdir', side_effect=mock_listdir):
|
||||
with patch('langbot.pkg.core.bootutils.deps.pkgmgr.install_requirements') as mock_install:
|
||||
import asyncio
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(precheck_plugin_deps())
|
||||
|
||||
mock_install.assert_called_once_with('plugins/plugin1/requirements.txt', extra_params=[])
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
"""Tests for the daily-grouped rotating log file handler.
|
||||
|
||||
Regression coverage for the bug where a long-running process names its log
|
||||
file after the *start* day and keeps appending to it across midnight, so no
|
||||
file ever appears for the current day. See
|
||||
``langbot.pkg.core.bootutils.log.DailyGroupedRotatingFileHandler``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
|
||||
import langbot.pkg.core.bootutils.log as logmod
|
||||
from langbot.pkg.core.bootutils.log import DailyGroupedRotatingFileHandler
|
||||
|
||||
# Mirror of the cleanup pattern in api/http/service/maintenance.py.
|
||||
MAINTENANCE_LOG_FILE_PATTERN = re.compile(r'^langbot-(\d{4}-\d{2}-\d{2})\.log(?:\.\d+)?$')
|
||||
|
||||
|
||||
def _listing(directory):
|
||||
return sorted(os.listdir(directory))
|
||||
|
||||
|
||||
def _make_logger(handler, name):
|
||||
logger = logging.getLogger(name)
|
||||
logger.setLevel(logging.INFO)
|
||||
logger.handlers.clear()
|
||||
logger.addHandler(handler)
|
||||
logger.propagate = False
|
||||
return logger
|
||||
|
||||
|
||||
class TestDailyGroupedRotatingFileHandler:
|
||||
def _patch_date(self, monkeypatch, box):
|
||||
"""Make the handler read its current date from ``box['date']``."""
|
||||
|
||||
def fake_strftime(fmt, t=None):
|
||||
if fmt == '%Y-%m-%d':
|
||||
return box['date']
|
||||
return '00:00:00'
|
||||
|
||||
monkeypatch.setattr(logmod.time, 'strftime', fake_strftime)
|
||||
|
||||
def test_initial_file_named_for_current_day(self, tmp_path, monkeypatch):
|
||||
box = {'date': '2026-06-08'}
|
||||
self._patch_date(monkeypatch, box)
|
||||
|
||||
handler = DailyGroupedRotatingFileHandler(str(tmp_path), max_bytes=10_000, backup_count=3)
|
||||
logger = _make_logger(handler, 'lb_logtest_initial')
|
||||
logger.info('hello')
|
||||
handler.close()
|
||||
|
||||
assert _listing(tmp_path) == ['langbot-2026-06-08.log']
|
||||
|
||||
def test_same_day_size_rotation_creates_numbered_backups(self, tmp_path, monkeypatch):
|
||||
box = {'date': '2026-06-08'}
|
||||
self._patch_date(monkeypatch, box)
|
||||
|
||||
handler = DailyGroupedRotatingFileHandler(str(tmp_path), max_bytes=200, backup_count=3)
|
||||
logger = _make_logger(handler, 'lb_logtest_size')
|
||||
for i in range(40):
|
||||
logger.info('padding line to exceed maxBytes %d', i)
|
||||
handler.close()
|
||||
|
||||
files = _listing(tmp_path)
|
||||
assert 'langbot-2026-06-08.log' in files
|
||||
assert any(f.startswith('langbot-2026-06-08.log.') for f in files)
|
||||
|
||||
def test_rolls_to_new_file_when_day_changes(self, tmp_path, monkeypatch):
|
||||
box = {'date': '2026-06-08'}
|
||||
self._patch_date(monkeypatch, box)
|
||||
|
||||
handler = DailyGroupedRotatingFileHandler(str(tmp_path), max_bytes=10_000, backup_count=3)
|
||||
logger = _make_logger(handler, 'lb_logtest_midnight')
|
||||
logger.info('day1 line')
|
||||
|
||||
# Simulate crossing midnight within the same running process.
|
||||
box['date'] = '2026-06-09'
|
||||
logger.info('day2 line after midnight')
|
||||
handler.close()
|
||||
|
||||
files = _listing(tmp_path)
|
||||
assert 'langbot-2026-06-08.log' in files
|
||||
assert 'langbot-2026-06-09.log' in files
|
||||
|
||||
day2 = (tmp_path / 'langbot-2026-06-09.log').read_text(encoding='utf-8')
|
||||
assert 'day2 line after midnight' in day2
|
||||
assert 'day1 line' not in day2
|
||||
|
||||
def test_rollover_repeats_across_multiple_days(self, tmp_path, monkeypatch):
|
||||
box = {'date': '2026-06-08'}
|
||||
self._patch_date(monkeypatch, box)
|
||||
|
||||
handler = DailyGroupedRotatingFileHandler(str(tmp_path), max_bytes=10_000, backup_count=3)
|
||||
logger = _make_logger(handler, 'lb_logtest_multiday')
|
||||
for day in ('2026-06-08', '2026-06-09', '2026-06-10'):
|
||||
box['date'] = day
|
||||
logger.info('line for %s', day)
|
||||
handler.close()
|
||||
|
||||
files = _listing(tmp_path)
|
||||
for day in ('2026-06-08', '2026-06-09', '2026-06-10'):
|
||||
assert f'langbot-{day}.log' in files
|
||||
|
||||
def test_all_filenames_match_maintenance_cleanup_pattern(self, tmp_path, monkeypatch):
|
||||
box = {'date': '2026-06-08'}
|
||||
self._patch_date(monkeypatch, box)
|
||||
|
||||
handler = DailyGroupedRotatingFileHandler(str(tmp_path), max_bytes=200, backup_count=3)
|
||||
logger = _make_logger(handler, 'lb_logtest_pattern')
|
||||
for i in range(40):
|
||||
logger.info('padding line %d', i)
|
||||
box['date'] = '2026-06-09'
|
||||
logger.info('next day line')
|
||||
handler.close()
|
||||
|
||||
for name in _listing(tmp_path):
|
||||
assert MAINTENANCE_LOG_FILE_PATTERN.match(name), name
|
||||
@@ -7,6 +7,7 @@ Tests cover:
|
||||
- Dict type skipping
|
||||
- Missing key creation
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
@@ -248,15 +249,8 @@ class TestApplyEnvOverridesToConfig:
|
||||
"""Test multiple env vars applied in order."""
|
||||
load_config = get_load_config_module()
|
||||
|
||||
cfg = {
|
||||
'system': {'name': 'default', 'enable': True},
|
||||
'concurrency': {'pipeline': 5}
|
||||
}
|
||||
env = {
|
||||
'SYSTEM__NAME': 'custom',
|
||||
'SYSTEM__ENABLE': 'false',
|
||||
'CONCURRENCY__PIPELINE': '10'
|
||||
}
|
||||
cfg = {'system': {'name': 'default', 'enable': True}, 'concurrency': {'pipeline': 5}}
|
||||
env = {'SYSTEM__NAME': 'custom', 'SYSTEM__ENABLE': 'false', 'CONCURRENCY__PIPELINE': '10'}
|
||||
|
||||
with patch.dict(os.environ, env, clear=True):
|
||||
result = load_config._apply_env_overrides_to_config(cfg)
|
||||
@@ -287,4 +281,4 @@ class TestApplyEnvOverridesToConfig:
|
||||
with patch.dict(os.environ, env, clear=True):
|
||||
result = load_config._apply_env_overrides_to_config(cfg)
|
||||
|
||||
assert result['api']['extra_webhook_prefix'] == 'https://extra.example.com'
|
||||
assert result['api']['extra_webhook_prefix'] == 'https://extra.example.com'
|
||||
|
||||
@@ -1,238 +0,0 @@
|
||||
"""Tests for core migration registration and abstract classes."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
import pytest
|
||||
|
||||
from tests.utils.import_isolation import isolated_sys_modules
|
||||
|
||||
|
||||
class TestMigrationClassDecorator:
|
||||
"""Tests for @migration_class decorator."""
|
||||
|
||||
def _make_migration_import_mocks(self):
|
||||
"""Create mocks for migration import."""
|
||||
return {
|
||||
'langbot.pkg.core.app': MagicMock(),
|
||||
}
|
||||
|
||||
def test_migration_class_registers_migration(self):
|
||||
"""@migration_class registers migration in preregistered_migrations."""
|
||||
mocks = self._make_migration_import_mocks()
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.core.migration import migration_class, preregistered_migrations
|
||||
|
||||
# Clear for clean test
|
||||
preregistered_migrations.clear()
|
||||
|
||||
@migration_class('test-migration', 1)
|
||||
class TestMigration:
|
||||
pass
|
||||
|
||||
assert len(preregistered_migrations) == 1
|
||||
assert preregistered_migrations[0] == TestMigration
|
||||
|
||||
def test_migration_class_sets_name_attribute(self):
|
||||
"""@migration_class sets name attribute on class."""
|
||||
mocks = self._make_migration_import_mocks()
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.core.migration import migration_class
|
||||
|
||||
@migration_class('test-migration', 1)
|
||||
class TestMigration:
|
||||
pass
|
||||
|
||||
assert TestMigration.name == 'test-migration'
|
||||
|
||||
def test_migration_class_sets_number_attribute(self):
|
||||
"""@migration_class sets number attribute on class."""
|
||||
mocks = self._make_migration_import_mocks()
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.core.migration import migration_class
|
||||
|
||||
@migration_class('test-migration', 42)
|
||||
class TestMigration:
|
||||
pass
|
||||
|
||||
assert TestMigration.number == 42
|
||||
|
||||
def test_migration_class_returns_original_class(self):
|
||||
"""@migration_class returns the original class."""
|
||||
mocks = self._make_migration_import_mocks()
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.core.migration import migration_class
|
||||
|
||||
@migration_class('test', 1)
|
||||
class TestMigration:
|
||||
custom_attr = 'value'
|
||||
|
||||
assert TestMigration.custom_attr == 'value'
|
||||
|
||||
def test_migration_class_multiple_migrations(self):
|
||||
"""Multiple migrations can be registered."""
|
||||
mocks = self._make_migration_import_mocks()
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.core.migration import migration_class, preregistered_migrations
|
||||
|
||||
preregistered_migrations.clear()
|
||||
|
||||
@migration_class('migration1', 1)
|
||||
class Migration1:
|
||||
pass
|
||||
|
||||
@migration_class('migration2', 2)
|
||||
class Migration2:
|
||||
pass
|
||||
|
||||
assert len(preregistered_migrations) == 2
|
||||
assert preregistered_migrations[0] == Migration1
|
||||
assert preregistered_migrations[1] == Migration2
|
||||
|
||||
|
||||
class TestMigrationAbstractClass:
|
||||
"""Tests for Migration abstract class."""
|
||||
|
||||
def _make_migration_import_mocks(self):
|
||||
return {'langbot.pkg.core.app': MagicMock()}
|
||||
|
||||
def test_migration_is_abstract(self):
|
||||
"""Migration is abstract and cannot be instantiated directly."""
|
||||
mocks = self._make_migration_import_mocks()
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.core.migration import Migration
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
Migration(MagicMock())
|
||||
|
||||
def test_migration_requires_need_migrate_method(self):
|
||||
"""Subclass must implement need_migrate method."""
|
||||
mocks = self._make_migration_import_mocks()
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.core.migration import Migration
|
||||
|
||||
class IncompleteMigration(Migration):
|
||||
async def run(self):
|
||||
pass
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
IncompleteMigration(MagicMock())
|
||||
|
||||
def test_migration_requires_run_method(self):
|
||||
"""Subclass must implement run method."""
|
||||
mocks = self._make_migration_import_mocks()
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.core.migration import Migration
|
||||
|
||||
class IncompleteMigration(Migration):
|
||||
async def need_migrate(self) -> bool:
|
||||
return False
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
IncompleteMigration(MagicMock())
|
||||
|
||||
def test_migration_subclass_works(self):
|
||||
"""Complete subclass can be instantiated."""
|
||||
mocks = self._make_migration_import_mocks()
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.core.migration import Migration
|
||||
|
||||
class CompleteMigration(Migration):
|
||||
async def need_migrate(self) -> bool:
|
||||
return True
|
||||
|
||||
async def run(self):
|
||||
pass
|
||||
|
||||
mock_ap = MagicMock()
|
||||
migration = CompleteMigration(mock_ap)
|
||||
assert migration.ap == mock_ap
|
||||
|
||||
def test_migration_stores_app_reference(self):
|
||||
"""Migration stores ap reference in __init__."""
|
||||
mocks = self._make_migration_import_mocks()
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.core.migration import Migration
|
||||
|
||||
class TestMigration(Migration):
|
||||
async def need_migrate(self) -> bool:
|
||||
return False
|
||||
|
||||
async def run(self):
|
||||
pass
|
||||
|
||||
mock_ap = MagicMock()
|
||||
migration = TestMigration(mock_ap)
|
||||
assert migration.ap is mock_ap
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_migration_need_migrate_returns_bool(self):
|
||||
"""need_migrate must return bool."""
|
||||
mocks = self._make_migration_import_mocks()
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.core.migration import Migration
|
||||
|
||||
class TestMigration(Migration):
|
||||
async def need_migrate(self) -> bool:
|
||||
return True
|
||||
|
||||
async def run(self):
|
||||
pass
|
||||
|
||||
migration = TestMigration(MagicMock())
|
||||
result = await migration.need_migrate()
|
||||
assert isinstance(result, bool)
|
||||
assert result == True
|
||||
|
||||
|
||||
class TestPreregisteredMigrations:
|
||||
"""Tests for preregistered_migrations global registry."""
|
||||
|
||||
def _make_migration_import_mocks(self):
|
||||
return {'langbot.pkg.core.app': MagicMock()}
|
||||
|
||||
def test_preregistered_migrations_is_list(self):
|
||||
"""preregistered_migrations is a list."""
|
||||
mocks = self._make_migration_import_mocks()
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.core.migration import preregistered_migrations
|
||||
|
||||
assert isinstance(preregistered_migrations, list)
|
||||
|
||||
def test_preregistered_migrations_order(self):
|
||||
"""Migrations are registered in order of decoration."""
|
||||
mocks = self._make_migration_import_mocks()
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.core.migration import migration_class, preregistered_migrations
|
||||
|
||||
preregistered_migrations.clear()
|
||||
|
||||
@migration_class('first', 1)
|
||||
class First:
|
||||
pass
|
||||
|
||||
@migration_class('second', 2)
|
||||
class Second:
|
||||
pass
|
||||
|
||||
@migration_class('third', 3)
|
||||
class Third:
|
||||
pass
|
||||
|
||||
# Order should match decoration order
|
||||
assert preregistered_migrations[0].number == 1
|
||||
assert preregistered_migrations[1].number == 2
|
||||
assert preregistered_migrations[2].number == 3
|
||||
@@ -175,4 +175,4 @@ class TestPreregisteredStages:
|
||||
pass
|
||||
|
||||
for key in preregistered_stages:
|
||||
assert isinstance(key, str)
|
||||
assert isinstance(key, str)
|
||||
|
||||
@@ -7,6 +7,7 @@ Tests cover:
|
||||
|
||||
Note: Uses import_isolation to break circular import chains.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
@@ -19,15 +20,17 @@ from typing import Generator
|
||||
|
||||
class MockLifecycleControlScopeEnum:
|
||||
"""Mock enum value for LifecycleControlScope with .value attribute."""
|
||||
|
||||
def __init__(self, value: str):
|
||||
self.value = value
|
||||
|
||||
def __repr__(self):
|
||||
return f"LifecycleControlScope.{self.value.upper()}"
|
||||
return f'LifecycleControlScope.{self.value.upper()}'
|
||||
|
||||
|
||||
class MockLifecycleControlScope:
|
||||
"""Mock enum for LifecycleControlScope."""
|
||||
|
||||
APPLICATION = MockLifecycleControlScopeEnum('application')
|
||||
PLATFORM = MockLifecycleControlScopeEnum('platform')
|
||||
PIPELINE = MockLifecycleControlScopeEnum('pipeline')
|
||||
@@ -40,17 +43,17 @@ def isolated_taskmgr_import() -> Generator[None, None, None]:
|
||||
# Mock modules that cause circular imports
|
||||
mock_entities = MagicMock()
|
||||
mock_entities.LifecycleControlScope = MockLifecycleControlScope
|
||||
|
||||
|
||||
mock_app = MagicMock()
|
||||
|
||||
|
||||
mock_importutil = MagicMock()
|
||||
mock_importutil.import_modules_in_pkg = lambda pkg: None
|
||||
mock_importutil.import_modules_in_pkgs = lambda pkgs: None
|
||||
|
||||
|
||||
mock_http_controller = MagicMock()
|
||||
|
||||
|
||||
mock_rag_mgr = MagicMock()
|
||||
|
||||
|
||||
mocks = {
|
||||
'langbot.pkg.core.entities': mock_entities,
|
||||
'langbot.pkg.core.app': mock_app,
|
||||
@@ -58,26 +61,26 @@ def isolated_taskmgr_import() -> Generator[None, None, None]:
|
||||
'langbot.pkg.rag.knowledge.kbmgr': mock_rag_mgr,
|
||||
'langbot.pkg.utils.importutil': mock_importutil,
|
||||
}
|
||||
|
||||
|
||||
# Save original state
|
||||
saved = {}
|
||||
for name in mocks:
|
||||
if name in sys.modules:
|
||||
saved[name] = sys.modules[name]
|
||||
|
||||
|
||||
# Clear taskmgr to force re-import
|
||||
taskmgr_name = 'langbot.pkg.core.taskmgr'
|
||||
if taskmgr_name in sys.modules:
|
||||
saved[taskmgr_name] = sys.modules[taskmgr_name]
|
||||
|
||||
|
||||
try:
|
||||
# Apply mocks
|
||||
for name, module in mocks.items():
|
||||
sys.modules[name] = module
|
||||
|
||||
|
||||
# Clear taskmgr
|
||||
sys.modules.pop(taskmgr_name, None)
|
||||
|
||||
|
||||
yield
|
||||
finally:
|
||||
# Restore
|
||||
@@ -86,7 +89,7 @@ def isolated_taskmgr_import() -> Generator[None, None, None]:
|
||||
sys.modules[name] = saved[name]
|
||||
else:
|
||||
sys.modules.pop(name, None)
|
||||
|
||||
|
||||
if taskmgr_name in saved:
|
||||
sys.modules[taskmgr_name] = saved[taskmgr_name]
|
||||
else:
|
||||
@@ -97,6 +100,7 @@ def get_taskmgr_classes():
|
||||
"""Get TaskContext, TaskWrapper, AsyncTaskManager classes."""
|
||||
with isolated_taskmgr_import():
|
||||
from langbot.pkg.core.taskmgr import TaskContext, TaskWrapper, AsyncTaskManager
|
||||
|
||||
return TaskContext, TaskWrapper, AsyncTaskManager
|
||||
|
||||
|
||||
@@ -194,9 +198,10 @@ class TestTaskContext:
|
||||
"""Test TaskContext.placeholder() returns singleton."""
|
||||
with isolated_taskmgr_import():
|
||||
from langbot.pkg.core.taskmgr import TaskContext
|
||||
|
||||
|
||||
# Reset global placeholder
|
||||
import langbot.pkg.core.taskmgr as taskmgr_module
|
||||
|
||||
taskmgr_module.placeholder_context = None
|
||||
|
||||
ctx1 = TaskContext.placeholder()
|
||||
@@ -269,7 +274,8 @@ class TestTaskWrapper:
|
||||
return 'result'
|
||||
|
||||
wrapper = TaskWrapper(
|
||||
mock_app, immediate_coro(),
|
||||
mock_app,
|
||||
immediate_coro(),
|
||||
name='test_task',
|
||||
label='Test Task',
|
||||
)
|
||||
@@ -414,7 +420,7 @@ class TestAsyncTaskManager:
|
||||
async def test_cancel_by_scope(self):
|
||||
"""Test cancel_by_scope cancels matching tasks."""
|
||||
_, _, AsyncTaskManager = get_taskmgr_classes()
|
||||
|
||||
|
||||
mock_app = create_mock_app()
|
||||
manager = AsyncTaskManager(mock_app)
|
||||
|
||||
@@ -422,16 +428,10 @@ class TestAsyncTaskManager:
|
||||
await asyncio.sleep(10)
|
||||
|
||||
# Create task with APPLICATION scope
|
||||
w1 = manager.create_task(
|
||||
long_coro(),
|
||||
scopes=[MockLifecycleControlScope.APPLICATION]
|
||||
)
|
||||
w1 = manager.create_task(long_coro(), scopes=[MockLifecycleControlScope.APPLICATION])
|
||||
|
||||
# Create task with different scope
|
||||
w2 = manager.create_task(
|
||||
long_coro(),
|
||||
scopes=[MockLifecycleControlScope.PIPELINE]
|
||||
)
|
||||
w2 = manager.create_task(long_coro(), scopes=[MockLifecycleControlScope.PIPELINE])
|
||||
|
||||
manager.cancel_by_scope(MockLifecycleControlScope.APPLICATION)
|
||||
|
||||
|
||||
@@ -15,68 +15,68 @@ class TestI18nString:
|
||||
|
||||
def test_create_with_english_only(self):
|
||||
"""Create I18nString with only English."""
|
||||
i18n = I18nString(en_US="Hello")
|
||||
i18n = I18nString(en_US='Hello')
|
||||
|
||||
assert i18n.en_US == "Hello"
|
||||
assert i18n.en_US == 'Hello'
|
||||
assert i18n.zh_Hans is None
|
||||
|
||||
def test_create_with_multiple_languages(self):
|
||||
"""Create I18nString with multiple languages."""
|
||||
i18n = I18nString(
|
||||
en_US="Hello",
|
||||
zh_Hans="你好",
|
||||
zh_Hant="你好",
|
||||
ja_JP="こんにちは",
|
||||
en_US='Hello',
|
||||
zh_Hans='你好',
|
||||
zh_Hant='你好',
|
||||
ja_JP='こんにちは',
|
||||
)
|
||||
|
||||
assert i18n.en_US == "Hello"
|
||||
assert i18n.zh_Hans == "你好"
|
||||
assert i18n.zh_Hant == "你好"
|
||||
assert i18n.ja_JP == "こんにちは"
|
||||
assert i18n.en_US == 'Hello'
|
||||
assert i18n.zh_Hans == '你好'
|
||||
assert i18n.zh_Hant == '你好'
|
||||
assert i18n.ja_JP == 'こんにちは'
|
||||
|
||||
def test_to_dict_with_english_only(self):
|
||||
"""to_dict returns only non-None fields."""
|
||||
i18n = I18nString(en_US="Hello")
|
||||
i18n = I18nString(en_US='Hello')
|
||||
|
||||
result = i18n.to_dict()
|
||||
|
||||
assert result == {"en_US": "Hello"}
|
||||
assert result == {'en_US': 'Hello'}
|
||||
|
||||
def test_to_dict_with_multiple_languages(self):
|
||||
"""to_dict returns all non-None fields."""
|
||||
i18n = I18nString(
|
||||
en_US="Hello",
|
||||
zh_Hans="你好",
|
||||
en_US='Hello',
|
||||
zh_Hans='你好',
|
||||
)
|
||||
|
||||
result = i18n.to_dict()
|
||||
|
||||
assert result == {"en_US": "Hello", "zh_Hans": "你好"}
|
||||
assert result == {'en_US': 'Hello', 'zh_Hans': '你好'}
|
||||
|
||||
def test_to_dict_excludes_none(self):
|
||||
"""to_dict excludes None values."""
|
||||
i18n = I18nString(
|
||||
en_US="Hello",
|
||||
en_US='Hello',
|
||||
zh_Hans=None,
|
||||
ja_JP="こんにちは",
|
||||
ja_JP='こんにちは',
|
||||
)
|
||||
|
||||
result = i18n.to_dict()
|
||||
|
||||
assert "zh_Hans" not in result
|
||||
assert "en_US" in result
|
||||
assert "ja_JP" in result
|
||||
assert 'zh_Hans' not in result
|
||||
assert 'en_US' in result
|
||||
assert 'ja_JP' in result
|
||||
|
||||
def test_to_dict_all_languages(self):
|
||||
"""to_dict with all supported languages."""
|
||||
i18n = I18nString(
|
||||
en_US="Hello",
|
||||
zh_Hans="你好",
|
||||
zh_Hant="你好",
|
||||
ja_JP="こんにちは",
|
||||
th_TH="สวัสดี",
|
||||
vi_VN="Xin chào",
|
||||
es_ES="Hola",
|
||||
en_US='Hello',
|
||||
zh_Hans='你好',
|
||||
zh_Hant='你好',
|
||||
ja_JP='こんにちは',
|
||||
th_TH='สวัสดี',
|
||||
vi_VN='Xin chào',
|
||||
es_ES='Hola',
|
||||
)
|
||||
|
||||
result = i18n.to_dict()
|
||||
@@ -92,30 +92,30 @@ class TestMetadata:
|
||||
from langbot.pkg.discover.engine import I18nString
|
||||
|
||||
metadata = Metadata(
|
||||
name="test-component",
|
||||
label=I18nString(en_US="Test Component"),
|
||||
name='test-component',
|
||||
label=I18nString(en_US='Test Component'),
|
||||
)
|
||||
|
||||
assert metadata.name == "test-component"
|
||||
assert metadata.label.en_US == "Test Component"
|
||||
assert metadata.name == 'test-component'
|
||||
assert metadata.label.en_US == 'Test Component'
|
||||
|
||||
def test_create_with_all_fields(self):
|
||||
"""Create Metadata with all optional fields."""
|
||||
from langbot.pkg.discover.engine import I18nString
|
||||
|
||||
metadata = Metadata(
|
||||
name="test-component",
|
||||
label=I18nString(en_US="Test"),
|
||||
description=I18nString(en_US="A test component"),
|
||||
version="1.0.0",
|
||||
icon="test-icon",
|
||||
author="Test Author",
|
||||
repository="https://github.com/test/repo",
|
||||
name='test-component',
|
||||
label=I18nString(en_US='Test'),
|
||||
description=I18nString(en_US='A test component'),
|
||||
version='1.0.0',
|
||||
icon='test-icon',
|
||||
author='Test Author',
|
||||
repository='https://github.com/test/repo',
|
||||
)
|
||||
|
||||
assert metadata.version == "1.0.0"
|
||||
assert metadata.icon == "test-icon"
|
||||
assert metadata.author == "Test Author"
|
||||
assert metadata.version == '1.0.0'
|
||||
assert metadata.icon == 'test-icon'
|
||||
assert metadata.author == 'Test Author'
|
||||
|
||||
|
||||
class TestComponentManifest:
|
||||
|
||||
@@ -7,6 +7,7 @@ Tests cover:
|
||||
|
||||
Note: Uses import isolation to break circular import chains.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
@@ -86,6 +87,7 @@ def get_database_module():
|
||||
"""Get database module with import isolation."""
|
||||
with isolated_database_import():
|
||||
from langbot.pkg.persistence import database
|
||||
|
||||
return database
|
||||
|
||||
|
||||
@@ -198,4 +200,4 @@ class TestManagerClassDecorator:
|
||||
# Create instance to test method (with mock app)
|
||||
mock_app = Mock()
|
||||
instance = ManagerWithMethods(mock_app)
|
||||
assert instance.custom_method() == 'test_value'
|
||||
assert instance.custom_method() == 'test_value'
|
||||
|
||||
@@ -4,6 +4,7 @@ Tests cover:
|
||||
- execute_async() with mock database
|
||||
- get_db_engine() with mock database manager
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
@@ -85,7 +86,7 @@ class TestExecuteAsync:
|
||||
mock_db.get_engine = Mock(return_value=mock_engine)
|
||||
mgr.db = mock_db
|
||||
|
||||
result = await mgr.execute_async(sqlalchemy.text("SELECT 1"))
|
||||
result = await mgr.execute_async(sqlalchemy.text('SELECT 1'))
|
||||
|
||||
# Verify result is the same object returned by execute
|
||||
assert result is mock_result
|
||||
@@ -152,4 +153,4 @@ class TestSerializeModelEdgeCases:
|
||||
result = mgr.serialize_model(SimpleModel, instance, masked_columns=['id', 'name'])
|
||||
|
||||
# Result should be empty dict when all columns masked
|
||||
assert result == {}
|
||||
assert result == {}
|
||||
|
||||
@@ -5,6 +5,7 @@ Tests cover:
|
||||
- datetime conversion to isoformat
|
||||
- masked_columns exclusion
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
|
||||
@@ -12,6 +12,12 @@ from __future__ import annotations
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
|
||||
# Preload pipelinemgr so the pipeline.stage module is fully initialised before
|
||||
# any individual stage test (e.g. preproc, longtext) tries to import it. Without
|
||||
# this, running a stage test in isolation triggers a circular-import error:
|
||||
# stage.py → core.app → pipelinemgr → stage.stage_class (not yet bound).
|
||||
import langbot.pkg.pipeline.pipelinemgr # noqa: F401
|
||||
|
||||
import langbot_plugin.api.entities.builtin.pipeline.query as pipeline_query
|
||||
import langbot_plugin.api.entities.builtin.platform.message as platform_message
|
||||
import langbot_plugin.api.entities.builtin.platform.events as platform_events
|
||||
@@ -34,6 +40,9 @@ class MockApplication:
|
||||
self.query_pool = self._create_mock_query_pool()
|
||||
self.instance_config = self._create_mock_instance_config()
|
||||
self.task_mgr = self._create_mock_task_manager()
|
||||
# Skill manager is optional; PreProcessor only touches it for the
|
||||
# local-agent runner. None keeps the skill-binding branch inert.
|
||||
self.skill_mgr = None
|
||||
|
||||
def _create_mock_logger(self):
|
||||
logger = Mock()
|
||||
|
||||
@@ -49,7 +49,7 @@ class TestPendingMessage:
|
||||
"""PendingMessage should be created with correct fields."""
|
||||
aggregator = get_aggregator_module()
|
||||
|
||||
chain = text_chain("hello")
|
||||
chain = text_chain('hello')
|
||||
event = friend_message_event(chain)
|
||||
adapter = mock_adapter()
|
||||
|
||||
@@ -88,7 +88,7 @@ class TestSessionBuffer:
|
||||
"""SessionBuffer should accept initial messages."""
|
||||
aggregator = get_aggregator_module()
|
||||
|
||||
chain = text_chain("hello")
|
||||
chain = text_chain('hello')
|
||||
event = friend_message_event(chain)
|
||||
adapter = mock_adapter()
|
||||
|
||||
@@ -309,7 +309,7 @@ class TestMessageAggregatorAddMessage:
|
||||
app = make_aggregator_app()
|
||||
agg = aggregator.MessageAggregator(app)
|
||||
|
||||
chain = text_chain("hello")
|
||||
chain = text_chain('hello')
|
||||
event = friend_message_event(chain)
|
||||
adapter = mock_adapter()
|
||||
|
||||
@@ -348,7 +348,7 @@ class TestMessageAggregatorAddMessage:
|
||||
|
||||
agg = aggregator.MessageAggregator(app)
|
||||
|
||||
chain = text_chain("hello")
|
||||
chain = text_chain('hello')
|
||||
event = friend_message_event(chain)
|
||||
adapter = mock_adapter()
|
||||
|
||||
@@ -387,7 +387,7 @@ class TestMessageAggregatorAddMessage:
|
||||
|
||||
agg = aggregator.MessageAggregator(app)
|
||||
|
||||
chain = text_chain("hello")
|
||||
chain = text_chain('hello')
|
||||
event = friend_message_event(chain)
|
||||
adapter = mock_adapter()
|
||||
|
||||
@@ -419,7 +419,7 @@ class TestMessageAggregatorMerge:
|
||||
app = make_aggregator_app()
|
||||
agg = aggregator.MessageAggregator(app)
|
||||
|
||||
chain = text_chain("hello")
|
||||
chain = text_chain('hello')
|
||||
event = friend_message_event(chain)
|
||||
adapter = mock_adapter()
|
||||
|
||||
@@ -445,8 +445,8 @@ class TestMessageAggregatorMerge:
|
||||
app = make_aggregator_app()
|
||||
agg = aggregator.MessageAggregator(app)
|
||||
|
||||
chain1 = text_chain("hello")
|
||||
chain2 = text_chain("world")
|
||||
chain1 = text_chain('hello')
|
||||
chain2 = text_chain('world')
|
||||
event = friend_message_event(chain1)
|
||||
adapter = mock_adapter()
|
||||
|
||||
@@ -476,8 +476,8 @@ class TestMessageAggregatorMerge:
|
||||
|
||||
# Should contain both messages with separator
|
||||
merged_str = str(merged.message_chain)
|
||||
assert "hello" in merged_str
|
||||
assert "world" in merged_str
|
||||
assert 'hello' in merged_str
|
||||
assert 'world' in merged_str
|
||||
|
||||
def test_merge_messages_preserves_routed_by_rule_if_any_input_matches(self):
|
||||
"""Merged PendingMessage should keep routed_by_rule when any input was rule-routed."""
|
||||
@@ -486,8 +486,8 @@ class TestMessageAggregatorMerge:
|
||||
app = make_aggregator_app()
|
||||
agg = aggregator.MessageAggregator(app)
|
||||
|
||||
chain1 = text_chain("first")
|
||||
chain2 = text_chain("second")
|
||||
chain1 = text_chain('first')
|
||||
chain2 = text_chain('second')
|
||||
event = friend_message_event(chain1)
|
||||
adapter = mock_adapter()
|
||||
|
||||
@@ -545,7 +545,7 @@ class TestMessageAggregatorFlush:
|
||||
app = make_aggregator_app()
|
||||
agg = aggregator.MessageAggregator(app)
|
||||
|
||||
chain = text_chain("hello")
|
||||
chain = text_chain('hello')
|
||||
event = friend_message_event(chain)
|
||||
adapter = mock_adapter()
|
||||
|
||||
@@ -597,7 +597,7 @@ class TestMessageAggregatorFlushAll:
|
||||
app = make_aggregator_app()
|
||||
agg = aggregator.MessageAggregator(app)
|
||||
|
||||
chain = text_chain("hello")
|
||||
chain = text_chain('hello')
|
||||
event = friend_message_event(chain)
|
||||
adapter = mock_adapter()
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ from tests.factories import FakeApp
|
||||
|
||||
# ============== FIXTURE USING IMPORT ISOLATION UTILITY ==============
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def mock_circular_import_chain():
|
||||
"""
|
||||
@@ -36,9 +37,11 @@ def mock_circular_import_chain():
|
||||
# Create a default runner that yields a simple response
|
||||
class DefaultRunner:
|
||||
name = 'local-agent'
|
||||
|
||||
def __init__(self, app, config):
|
||||
self.app = app
|
||||
self.config = config
|
||||
|
||||
async def run(self, query):
|
||||
yield Message(role='assistant', content='fake response')
|
||||
|
||||
@@ -70,9 +73,12 @@ def mock_event_ctx():
|
||||
@pytest.fixture
|
||||
def set_runner():
|
||||
"""Factory fixture to set a custom runner for tests."""
|
||||
|
||||
def _set_runner(runner_class):
|
||||
import sys
|
||||
|
||||
sys.modules['langbot.pkg.provider.runner'].preregistered_runners = [runner_class]
|
||||
|
||||
return _set_runner
|
||||
|
||||
|
||||
@@ -87,6 +93,7 @@ def get_chat_handler():
|
||||
global _chat_handler_module
|
||||
if _chat_handler_module is None:
|
||||
from importlib import import_module
|
||||
|
||||
_chat_handler_module = import_module('langbot.pkg.pipeline.process.handlers.chat')
|
||||
return _chat_handler_module
|
||||
|
||||
@@ -96,12 +103,14 @@ def get_entities():
|
||||
global _entities_module
|
||||
if _entities_module is None:
|
||||
from importlib import import_module
|
||||
|
||||
_entities_module = import_module('langbot.pkg.pipeline.entities')
|
||||
return _entities_module
|
||||
|
||||
|
||||
# ============== REAL ChatMessageHandler Tests ==============
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('mock_circular_import_chain')
|
||||
class TestChatMessageHandlerReal:
|
||||
"""Tests for real ChatMessageHandler class."""
|
||||
@@ -188,9 +197,11 @@ class TestChatMessageHandlerReal:
|
||||
|
||||
class QuickRunner:
|
||||
name = 'local-agent'
|
||||
|
||||
def __init__(self, app, config):
|
||||
self.app = app
|
||||
self.config = config
|
||||
|
||||
async def run(self, query):
|
||||
yield Message(role='assistant', content='ok')
|
||||
|
||||
@@ -222,9 +233,11 @@ class TestChatMessageHandlerReal:
|
||||
|
||||
class SingleRunner:
|
||||
name = 'local-agent'
|
||||
|
||||
def __init__(self, app, config):
|
||||
self.app = app
|
||||
self.config = config
|
||||
|
||||
async def run(self, query):
|
||||
yield Message(role='assistant', content='response')
|
||||
|
||||
@@ -262,9 +275,11 @@ class TestChatHandlerStreaming:
|
||||
|
||||
class StreamRunner:
|
||||
name = 'local-agent'
|
||||
|
||||
def __init__(self, app, config):
|
||||
self.app = app
|
||||
self.config = config
|
||||
|
||||
async def run(self, query):
|
||||
yield MessageChunk(role='assistant', content='Hello', is_final=False)
|
||||
yield MessageChunk(role='assistant', content=' World', is_final=True)
|
||||
@@ -303,14 +318,19 @@ class TestChatHandlerExceptions:
|
||||
|
||||
query.pipeline_config = {
|
||||
'output': {'misc': {'exception-handling': 'show-hint', 'failure-hint': 'Request failed.'}},
|
||||
'ai': {'runner': {'runner': 'local-agent'}, 'local-agent': {'prompt': 'default', 'model': {'primary': 'test'}}},
|
||||
'ai': {
|
||||
'runner': {'runner': 'local-agent'},
|
||||
'local-agent': {'prompt': 'default', 'model': {'primary': 'test'}},
|
||||
},
|
||||
}
|
||||
|
||||
class FailingRunner:
|
||||
name = 'local-agent'
|
||||
|
||||
def __init__(self, app, config):
|
||||
self.app = app
|
||||
self.config = config
|
||||
|
||||
async def run(self, query):
|
||||
raise ValueError('API error')
|
||||
yield
|
||||
@@ -346,14 +366,19 @@ class TestChatHandlerExceptions:
|
||||
|
||||
query.pipeline_config = {
|
||||
'output': {'misc': {'exception-handling': 'show-error'}},
|
||||
'ai': {'runner': {'runner': 'local-agent'}, 'local-agent': {'prompt': 'default', 'model': {'primary': 'test'}}},
|
||||
'ai': {
|
||||
'runner': {'runner': 'local-agent'},
|
||||
'local-agent': {'prompt': 'default', 'model': {'primary': 'test'}},
|
||||
},
|
||||
}
|
||||
|
||||
class ErrorRunner:
|
||||
name = 'local-agent'
|
||||
|
||||
def __init__(self, app, config):
|
||||
self.app = app
|
||||
self.config = config
|
||||
|
||||
async def run(self, query):
|
||||
raise ValueError('Custom error')
|
||||
yield
|
||||
@@ -386,14 +411,19 @@ class TestChatHandlerExceptions:
|
||||
|
||||
query.pipeline_config = {
|
||||
'output': {'misc': {'exception-handling': 'hide'}},
|
||||
'ai': {'runner': {'runner': 'local-agent'}, 'local-agent': {'prompt': 'default', 'model': {'primary': 'test'}}},
|
||||
'ai': {
|
||||
'runner': {'runner': 'local-agent'},
|
||||
'local-agent': {'prompt': 'default', 'model': {'primary': 'test'}},
|
||||
},
|
||||
}
|
||||
|
||||
class HideErrorRunner:
|
||||
name = 'local-agent'
|
||||
|
||||
def __init__(self, app, config):
|
||||
self.app = app
|
||||
self.config = config
|
||||
|
||||
async def run(self, query):
|
||||
raise RuntimeError('hidden')
|
||||
yield
|
||||
@@ -433,4 +463,4 @@ class TestChatHandlerHelper:
|
||||
chat = get_chat_handler()
|
||||
handler = chat.ChatMessageHandler(fake_app)
|
||||
result = handler.cut_str('first line\nsecond line')
|
||||
assert '...' in result
|
||||
assert '...' in result
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
import langbot_plugin.api.entities.builtin.provider.message as provider_message
|
||||
|
||||
# TODO: unskip once the handler ↔ app circular import is resolved
|
||||
pytest.skip(
|
||||
'circular import in handler ↔ app; will be unblocked once resolved',
|
||||
allow_module_level=True,
|
||||
)
|
||||
|
||||
from langbot.pkg.pipeline.process.handler import MessageHandler # noqa: E402
|
||||
|
||||
|
||||
class _StubHandler(MessageHandler):
|
||||
async def handle(self, query):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
handler = _StubHandler(ap=Mock())
|
||||
|
||||
|
||||
def test_chat_handler_formats_tool_call_request_log():
|
||||
result = provider_message.Message(
|
||||
role='assistant',
|
||||
content='',
|
||||
tool_calls=[
|
||||
provider_message.ToolCall(
|
||||
id='call-1',
|
||||
type='function',
|
||||
function=provider_message.FunctionCall(name='exec', arguments='{}'),
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
summary = handler.format_result_log(result)
|
||||
|
||||
assert summary == 'assistant: requested tools: exec'
|
||||
|
||||
|
||||
def test_chat_handler_formats_tool_result_log():
|
||||
result = provider_message.Message(
|
||||
role='tool',
|
||||
content='{"status":"completed","exit_code":0,"backend":"podman","stdout":"42\\n"}',
|
||||
tool_call_id='call-1',
|
||||
)
|
||||
|
||||
summary = handler.format_result_log(result)
|
||||
|
||||
# Tool results use generic cut_str truncation
|
||||
assert summary is not None
|
||||
assert summary.startswith('tool: {"status":"com')
|
||||
assert summary.endswith('...')
|
||||
|
||||
|
||||
def test_chat_handler_formats_tool_error_log():
|
||||
result = provider_message.MessageChunk(
|
||||
role='tool',
|
||||
content='err: host_path must point to an existing directory on the host',
|
||||
tool_call_id='call-1',
|
||||
is_final=True,
|
||||
)
|
||||
|
||||
summary = handler.format_result_log(result)
|
||||
|
||||
assert summary is not None
|
||||
assert summary.startswith('tool error: err: host_path must')
|
||||
assert summary.endswith('...')
|
||||
|
||||
|
||||
def test_chat_handler_skips_empty_assistant_log():
|
||||
result = provider_message.Message(role='assistant', content='')
|
||||
|
||||
summary = handler.format_result_log(result)
|
||||
|
||||
assert summary is None
|
||||
@@ -67,7 +67,11 @@ def make_pipeline_config(**overrides):
|
||||
for key, value in overrides.items():
|
||||
if key in base_config and isinstance(base_config[key], dict) and isinstance(value, dict):
|
||||
for sub_key, sub_value in value.items():
|
||||
if sub_key in base_config[key] and isinstance(base_config[key][sub_key], dict) and isinstance(sub_value, dict):
|
||||
if (
|
||||
sub_key in base_config[key]
|
||||
and isinstance(base_config[key][sub_key], dict)
|
||||
and isinstance(sub_value, dict)
|
||||
):
|
||||
base_config[key][sub_key].update(sub_value)
|
||||
else:
|
||||
base_config[key][sub_key] = sub_value
|
||||
@@ -141,7 +145,7 @@ class TestPreContentFilter:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello world")
|
||||
query = text_query('hello world')
|
||||
query.pipeline_config = pipeline_config
|
||||
|
||||
result = await stage.process(query, 'PreContentFilterStage')
|
||||
@@ -163,7 +167,7 @@ class TestPreContentFilter:
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
# Empty message chain
|
||||
query = text_query("")
|
||||
query = text_query('')
|
||||
query.message_chain = platform_message.MessageChain([])
|
||||
query.pipeline_config = pipeline_config
|
||||
|
||||
@@ -185,7 +189,7 @@ class TestPreContentFilter:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query(" ") # Only whitespace
|
||||
query = text_query(' ') # Only whitespace
|
||||
query.pipeline_config = pipeline_config
|
||||
|
||||
result = await stage.process(query, 'PreContentFilterStage')
|
||||
@@ -234,7 +238,7 @@ class TestPreContentFilter:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello world")
|
||||
query = text_query('hello world')
|
||||
query.pipeline_config = pipeline_config
|
||||
|
||||
result = await stage.process(query, 'PreContentFilterStage')
|
||||
@@ -266,7 +270,7 @@ class TestContentIgnoreFilter:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("/help me")
|
||||
query = text_query('/help me')
|
||||
query.pipeline_config = pipeline_config
|
||||
|
||||
result = await stage.process(query, 'PreContentFilterStage')
|
||||
@@ -294,7 +298,7 @@ class TestContentIgnoreFilter:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("http://example.com")
|
||||
query = text_query('http://example.com')
|
||||
query.pipeline_config = pipeline_config
|
||||
|
||||
result = await stage.process(query, 'PreContentFilterStage')
|
||||
@@ -322,7 +326,7 @@ class TestContentIgnoreFilter:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("normal message")
|
||||
query = text_query('normal message')
|
||||
query.pipeline_config = pipeline_config
|
||||
|
||||
result = await stage.process(query, 'PreContentFilterStage')
|
||||
@@ -343,7 +347,7 @@ class TestContentIgnoreFilter:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("/help me")
|
||||
query = text_query('/help me')
|
||||
query.pipeline_config = pipeline_config
|
||||
|
||||
result = await stage.process(query, 'PreContentFilterStage')
|
||||
@@ -368,12 +372,10 @@ class TestPostContentFilter:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
# Add a response message
|
||||
query.resp_messages = [
|
||||
provider_message.Message(role='assistant', content='Hello back!')
|
||||
]
|
||||
query.resp_messages = [provider_message.Message(role='assistant', content='Hello back!')]
|
||||
|
||||
result = await stage.process(query, 'PostContentFilterStage')
|
||||
|
||||
@@ -398,11 +400,9 @@ class TestPostContentFilter:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_messages = [
|
||||
provider_message.Message(role='assistant', content='Response')
|
||||
]
|
||||
query.resp_messages = [provider_message.Message(role='assistant', content='Response')]
|
||||
|
||||
result = await stage.process(query, 'PostContentFilterStage')
|
||||
|
||||
@@ -422,7 +422,7 @@ class TestPostContentFilter:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
# Non-string content - use model_construct to bypass validation
|
||||
# The actual content type could be a list of ContentElement objects
|
||||
@@ -450,11 +450,9 @@ class TestPostContentFilter:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_messages = [
|
||||
provider_message.Message(role='assistant', content='')
|
||||
]
|
||||
query.resp_messages = [provider_message.Message(role='assistant', content='')]
|
||||
|
||||
result = await stage.process(query, 'PostContentFilterStage')
|
||||
|
||||
@@ -476,7 +474,7 @@ class TestContentFilterStageInvalidName:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
|
||||
with pytest.raises(ValueError, match='未知的 stage_inst_name'):
|
||||
@@ -506,7 +504,7 @@ class TestContentIgnoreFilterDirect:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("normal message without prefix")
|
||||
query = text_query('normal message without prefix')
|
||||
query.pipeline_config = pipeline_config
|
||||
|
||||
result = await stage.process(query, 'PreContentFilterStage')
|
||||
|
||||
@@ -15,6 +15,7 @@ from tests.factories import FakeApp, command_query
|
||||
|
||||
# ============== FIXTURE USING IMPORT ISOLATION UTILITY ==============
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def mock_circular_import_chain():
|
||||
"""
|
||||
@@ -56,6 +57,7 @@ def mock_event_ctx():
|
||||
@pytest.fixture
|
||||
def mock_execute_factory():
|
||||
"""Factory fixture to create mock cmd_mgr.execute generators."""
|
||||
|
||||
def _create_execute(
|
||||
text: str | None = 'ok',
|
||||
error: str | None = None,
|
||||
@@ -71,7 +73,9 @@ def mock_execute_factory():
|
||||
ret.image_base64 = image_base64
|
||||
ret.file_url = file_url
|
||||
yield ret
|
||||
|
||||
return mock_execute
|
||||
|
||||
return _create_execute
|
||||
|
||||
|
||||
@@ -86,6 +90,7 @@ def get_command_handler():
|
||||
global _command_handler_module
|
||||
if _command_handler_module is None:
|
||||
from importlib import import_module
|
||||
|
||||
_command_handler_module = import_module('langbot.pkg.pipeline.process.handlers.command')
|
||||
return _command_handler_module
|
||||
|
||||
@@ -95,12 +100,14 @@ def get_entities():
|
||||
global _entities_module
|
||||
if _entities_module is None:
|
||||
from importlib import import_module
|
||||
|
||||
_entities_module = import_module('langbot.pkg.pipeline.entities')
|
||||
return _entities_module
|
||||
|
||||
|
||||
# ============== REAL CommandHandler Tests ==============
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('mock_circular_import_chain')
|
||||
class TestCommandHandlerReal:
|
||||
"""Tests for real CommandHandler class."""
|
||||
@@ -127,6 +134,7 @@ class TestCommandHandlerReal:
|
||||
fake_app.plugin_connector.emit_event = AsyncMock(return_value=mock_event_ctx)
|
||||
|
||||
executed_commands = []
|
||||
|
||||
async def track_execute(command_text, full_command_text, query, session):
|
||||
executed_commands.append(command_text)
|
||||
ret = Mock()
|
||||
@@ -334,8 +342,7 @@ class TestCommandHandlerReal:
|
||||
command = get_command_handler()
|
||||
fake_app.plugin_connector.emit_event = AsyncMock(return_value=mock_event_ctx)
|
||||
fake_app.cmd_mgr.execute = mock_execute_factory(
|
||||
text='Here is the image:',
|
||||
image_url='https://example.com/image.png'
|
||||
text='Here is the image:', image_url='https://example.com/image.png'
|
||||
)
|
||||
|
||||
handler = command.CommandHandler(fake_app)
|
||||
@@ -393,4 +400,4 @@ class TestCommandHandlerHelper:
|
||||
command = get_command_handler()
|
||||
handler = command.CommandHandler(fake_app)
|
||||
result = handler.cut_str('first line\nsecond line')
|
||||
assert '...' in result
|
||||
assert '...' in result
|
||||
|
||||
@@ -126,11 +126,9 @@ class TestLongTextProcessStageProcess:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_message_chain = [
|
||||
platform_message.MessageChain([platform_message.Plain(text="very long response")])
|
||||
]
|
||||
query.resp_message_chain = [platform_message.MessageChain([platform_message.Plain(text='very long response')])]
|
||||
|
||||
result = await stage.process(query, 'LongTextProcessStage')
|
||||
|
||||
@@ -151,11 +149,9 @@ class TestLongTextProcessStageProcess:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_message_chain = [
|
||||
platform_message.MessageChain([platform_message.Plain(text="short response")])
|
||||
]
|
||||
query.resp_message_chain = [platform_message.MessageChain([platform_message.Plain(text='short response')])]
|
||||
|
||||
result = await stage.process(query, 'LongTextProcessStage')
|
||||
|
||||
@@ -179,14 +175,13 @@ class TestLongTextProcessStageProcess:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
# Non-Plain component (Image)
|
||||
query.resp_message_chain = [
|
||||
platform_message.MessageChain([
|
||||
platform_message.Plain(text="short"),
|
||||
platform_message.Image(url="https://example.com/img.png")
|
||||
])
|
||||
platform_message.MessageChain(
|
||||
[platform_message.Plain(text='short'), platform_message.Image(url='https://example.com/img.png')]
|
||||
)
|
||||
]
|
||||
|
||||
result = await stage.process(query, 'LongTextProcessStage')
|
||||
@@ -213,7 +208,7 @@ class TestLongTextProcessStageProcess:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_message_chain = []
|
||||
|
||||
@@ -232,7 +227,7 @@ class TestLongTextProcessStageProcess:
|
||||
stage = longtext.LongTextProcessStage(app)
|
||||
stage.strategy_impl = AsyncMock()
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = make_longtext_config(strategy='forward', threshold=1)
|
||||
query.resp_message_chain = []
|
||||
|
||||
@@ -242,6 +237,7 @@ class TestLongTextProcessStageProcess:
|
||||
assert result.new_query is query
|
||||
stage.strategy_impl.process.assert_not_called()
|
||||
|
||||
|
||||
class TestForwardStrategy:
|
||||
"""Tests for ForwardComponentStrategy."""
|
||||
|
||||
@@ -260,7 +256,7 @@ class TestForwardStrategy:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
# Create a mock adapter with bot_account_id
|
||||
mock_adapter = Mock()
|
||||
@@ -268,10 +264,8 @@ class TestForwardStrategy:
|
||||
query.adapter = mock_adapter
|
||||
|
||||
# Long text exceeding threshold
|
||||
long_text = "This is a very long response that exceeds the threshold"
|
||||
query.resp_message_chain = [
|
||||
platform_message.MessageChain([platform_message.Plain(text=long_text)])
|
||||
]
|
||||
long_text = 'This is a very long response that exceeds the threshold'
|
||||
query.resp_message_chain = [platform_message.MessageChain([platform_message.Plain(text=long_text)])]
|
||||
|
||||
result = await stage.process(query, 'LongTextProcessStage')
|
||||
|
||||
@@ -297,13 +291,13 @@ class TestForwardStrategy:
|
||||
|
||||
await strat.initialize()
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = make_longtext_config()
|
||||
mock_adapter = Mock()
|
||||
mock_adapter.bot_account_id = '12345'
|
||||
query.adapter = mock_adapter
|
||||
|
||||
components = await strat.process("test message", query)
|
||||
components = await strat.process('test message', query)
|
||||
|
||||
assert len(components) == 1
|
||||
assert isinstance(components[0], platform_message.Forward)
|
||||
@@ -326,14 +320,12 @@ class TestLongTextThreshold:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
|
||||
# Text below threshold
|
||||
short_text = "x" * (threshold - 1)
|
||||
query.resp_message_chain = [
|
||||
platform_message.MessageChain([platform_message.Plain(text=short_text)])
|
||||
]
|
||||
short_text = 'x' * (threshold - 1)
|
||||
query.resp_message_chain = [platform_message.MessageChain([platform_message.Plain(text=short_text)])]
|
||||
|
||||
result = await stage.process(query, 'LongTextProcessStage')
|
||||
|
||||
|
||||
@@ -115,7 +115,7 @@ class TestRoundTruncatorProcess:
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
# Create query with 3 messages (within limit)
|
||||
query = text_query("current message")
|
||||
query = text_query('current message')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.messages = [
|
||||
provider_message.Message(role='user', content='message 1'),
|
||||
@@ -154,7 +154,7 @@ class TestRoundTruncatorProcess:
|
||||
|
||||
# Create query with many messages exceeding limit
|
||||
# 7 messages = 3 full rounds + 1 current user
|
||||
query = text_query("current message")
|
||||
query = text_query('current message')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.messages = [
|
||||
provider_message.Message(role='user', content='message 1'),
|
||||
@@ -194,7 +194,7 @@ class TestRoundTruncatorProcess:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.messages = []
|
||||
|
||||
@@ -216,7 +216,7 @@ class TestRoundTruncatorProcess:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.messages = [
|
||||
provider_message.Message(role='user', content='hello'),
|
||||
@@ -240,7 +240,7 @@ class TestRoundTruncatorProcess:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("current")
|
||||
query = text_query('current')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.messages = [
|
||||
provider_message.Message(role='user', content='user1'),
|
||||
@@ -274,7 +274,7 @@ class TestRoundTruncatorProcess:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("current")
|
||||
query = text_query('current')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.messages = [
|
||||
provider_message.Message(role='user', content='old1'),
|
||||
@@ -305,7 +305,7 @@ class TestRoundTruncatorDirect:
|
||||
trun = trun_cls(app)
|
||||
break
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = make_truncate_config(max_round=3)
|
||||
query.messages = [
|
||||
provider_message.Message(role='user', content='m1'),
|
||||
|
||||
@@ -14,33 +14,43 @@ import json
|
||||
import sys
|
||||
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
||||
|
||||
# Break the circular import chain before importing n8nsvapi:
|
||||
import pytest
|
||||
import langbot_plugin.api.entities.builtin.provider.message as provider_message
|
||||
|
||||
# Break the circular import chain while importing n8nsvapi:
|
||||
# n8nsvapi → runner → app → pipelinemgr → all runners → runner (partially init)
|
||||
_mock_runner = MagicMock()
|
||||
_mock_runner.runner_class = lambda name: (lambda cls: cls) # no-op decorator
|
||||
_mock_runner.RequestRunner = object
|
||||
_mocked_imports = {
|
||||
'langbot.pkg.provider.runner': _mock_runner,
|
||||
# The stubs are restored in a ``finally`` block so this module does NOT pollute
|
||||
# sys.modules for other test modules (e.g. ones importing the real
|
||||
# LocalAgentRunner, which would otherwise inherit ``object`` and break).
|
||||
# Mirrors master's intent but uses try/finally so a raised import doesn't
|
||||
# leave the global namespace in a stubbed state, and includes
|
||||
# ``langbot.pkg.utils.httpclient`` which master didn't stub.
|
||||
_runner_stub = MagicMock()
|
||||
_runner_stub.runner_class = lambda name: (lambda cls: cls) # no-op decorator
|
||||
_runner_stub.RequestRunner = object
|
||||
_import_stubs = {
|
||||
'langbot.pkg.provider.runner': _runner_stub,
|
||||
'langbot.pkg.core.app': MagicMock(),
|
||||
'langbot.pkg.utils.httpclient': MagicMock(),
|
||||
}
|
||||
_original_imports = {name: sys.modules.get(name) for name in _mocked_imports}
|
||||
sys.modules.update(_mocked_imports)
|
||||
|
||||
import pytest # noqa: E402
|
||||
import langbot_plugin.api.entities.builtin.provider.message as provider_message # noqa: E402
|
||||
from langbot.pkg.provider.runners.n8nsvapi import N8nServiceAPIRunner # noqa: E402
|
||||
|
||||
for _name, _original in _original_imports.items():
|
||||
if _original is None:
|
||||
sys.modules.pop(_name, None)
|
||||
else:
|
||||
sys.modules[_name] = _original
|
||||
_saved_modules = {name: sys.modules.get(name) for name in _import_stubs}
|
||||
for _name, _stub in _import_stubs.items():
|
||||
sys.modules[_name] = _stub
|
||||
try:
|
||||
from langbot.pkg.provider.runners.n8nsvapi import N8nServiceAPIRunner
|
||||
finally:
|
||||
for _name, _original in _saved_modules.items():
|
||||
if _original is None:
|
||||
sys.modules.pop(_name, None)
|
||||
else:
|
||||
sys.modules[_name] = _original
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def make_runner(output_key: str = 'response') -> N8nServiceAPIRunner:
|
||||
ap = Mock()
|
||||
ap.logger = Mock()
|
||||
@@ -83,6 +93,7 @@ async def collect_chunks(runner: N8nServiceAPIRunner, chunks: list[bytes | str])
|
||||
# _process_response: stream format (type:item/end)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stream_format_single_item():
|
||||
"""Single item + end in one chunk yields final chunk with full content."""
|
||||
@@ -165,6 +176,7 @@ async def test_stream_format_no_spurious_empty_yield():
|
||||
# _process_response: plain JSON fallback
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_plain_json_with_output_key():
|
||||
"""Plain JSON with matching output_key extracts value via output_key."""
|
||||
@@ -235,6 +247,7 @@ async def test_invalid_json_returns_raw_text():
|
||||
# _call_webhook: output type depends on is_stream
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def make_query(is_stream: bool):
|
||||
"""Build a minimal Query mock."""
|
||||
query = Mock()
|
||||
|
||||
@@ -162,3 +162,46 @@ async def test_runtime_pipeline_execute(mock_app, sample_query):
|
||||
|
||||
# Verify stage was called
|
||||
mock_stage.process.assert_called_once()
|
||||
|
||||
|
||||
def test_runtime_pipeline_prefers_local_agent_mcp_resources(mock_app):
|
||||
"""Local Agent resource selection should override legacy extension prefs."""
|
||||
pipelinemgr = get_pipelinemgr_module()
|
||||
persistence_pipeline = get_persistence_pipeline_module()
|
||||
|
||||
pipeline_entity = Mock(spec=persistence_pipeline.LegacyPipeline)
|
||||
pipeline_entity.config = {
|
||||
'ai': {
|
||||
'local-agent': {
|
||||
'mcp-resources': [{'server_uuid': 'srv-new', 'uri': 'file:///new.md'}],
|
||||
'mcp-resource-agent-read-enabled': False,
|
||||
}
|
||||
}
|
||||
}
|
||||
pipeline_entity.extensions_preferences = {
|
||||
'mcp_resources': [{'server_uuid': 'srv-old', 'uri': 'file:///old.md'}],
|
||||
'mcp_resource_agent_read_enabled': True,
|
||||
}
|
||||
|
||||
runtime_pipeline = pipelinemgr.RuntimePipeline(mock_app, pipeline_entity, [])
|
||||
|
||||
assert runtime_pipeline.mcp_resource_attachments == [{'server_uuid': 'srv-new', 'uri': 'file:///new.md'}]
|
||||
assert runtime_pipeline.mcp_resource_agent_read_enabled is False
|
||||
|
||||
|
||||
def test_runtime_pipeline_falls_back_to_extension_mcp_resources(mock_app):
|
||||
"""Existing extension prefs remain compatible until a Local Agent value exists."""
|
||||
pipelinemgr = get_pipelinemgr_module()
|
||||
persistence_pipeline = get_persistence_pipeline_module()
|
||||
|
||||
pipeline_entity = Mock(spec=persistence_pipeline.LegacyPipeline)
|
||||
pipeline_entity.config = {'ai': {'local-agent': {}}}
|
||||
pipeline_entity.extensions_preferences = {
|
||||
'mcp_resources': [{'server_uuid': 'srv-old', 'uri': 'file:///old.md'}],
|
||||
'mcp_resource_agent_read_enabled': False,
|
||||
}
|
||||
|
||||
runtime_pipeline = pipelinemgr.RuntimePipeline(mock_app, pipeline_entity, [])
|
||||
|
||||
assert runtime_pipeline.mcp_resource_attachments == [{'server_uuid': 'srv-old', 'uri': 'file:///old.md'}]
|
||||
assert runtime_pipeline.mcp_resource_agent_read_enabled is False
|
||||
|
||||
@@ -14,6 +14,7 @@ from __future__ import annotations
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
from importlib import import_module
|
||||
from types import SimpleNamespace
|
||||
|
||||
from tests.factories import (
|
||||
FakeApp,
|
||||
@@ -78,7 +79,7 @@ class TestPreProcessorNormalText:
|
||||
app.plugin_connector.emit_event = AsyncMock(return_value=mock_event_ctx)
|
||||
|
||||
stage = preproc.PreProcessor(app)
|
||||
query = text_query("hello world")
|
||||
query = text_query('hello world')
|
||||
|
||||
result = await stage.process(query, 'PreProcessor')
|
||||
|
||||
@@ -113,7 +114,7 @@ class TestPreProcessorNormalText:
|
||||
app.plugin_connector.emit_event = AsyncMock(return_value=mock_event_ctx)
|
||||
|
||||
stage = preproc.PreProcessor(app)
|
||||
query = text_query("test message")
|
||||
query = text_query('test message')
|
||||
|
||||
result = await stage.process(query, 'PreProcessor')
|
||||
|
||||
@@ -194,13 +195,16 @@ class TestPreProcessorImageSegment:
|
||||
|
||||
stage = preproc.PreProcessor(app)
|
||||
# Image query with base64
|
||||
query = image_query(text="look at this", url=None)
|
||||
query = image_query(text='look at this', url=None)
|
||||
# Set base64 on the image component
|
||||
import langbot_plugin.api.entities.builtin.platform.message as platform_message
|
||||
chain = platform_message.MessageChain([
|
||||
platform_message.Plain(text="look at this"),
|
||||
platform_message.Image(base64="data:image/png;base64,abc123"),
|
||||
])
|
||||
|
||||
chain = platform_message.MessageChain(
|
||||
[
|
||||
platform_message.Plain(text='look at this'),
|
||||
platform_message.Image(base64='data:image/png;base64,abc123'),
|
||||
]
|
||||
)
|
||||
query.message_chain = chain
|
||||
|
||||
result = await stage.process(query, 'PreProcessor')
|
||||
@@ -238,7 +242,7 @@ class TestPreProcessorImageSegment:
|
||||
app.plugin_connector.emit_event = AsyncMock(return_value=mock_event_ctx)
|
||||
|
||||
stage = preproc.PreProcessor(app)
|
||||
query = image_query(text="describe this")
|
||||
query = image_query(text='describe this')
|
||||
|
||||
result = await stage.process(query, 'PreProcessor')
|
||||
|
||||
@@ -276,7 +280,7 @@ class TestPreProcessorModelSelection:
|
||||
app.plugin_connector.emit_event = AsyncMock(return_value=mock_event_ctx)
|
||||
|
||||
stage = preproc.PreProcessor(app)
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
|
||||
# Set pipeline config with primary model
|
||||
query.pipeline_config = {
|
||||
@@ -335,7 +339,7 @@ class TestPreProcessorModelSelection:
|
||||
app.plugin_connector.emit_event = AsyncMock(return_value=mock_event_ctx)
|
||||
|
||||
stage = preproc.PreProcessor(app)
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
|
||||
query.pipeline_config = {
|
||||
'ai': {
|
||||
@@ -384,7 +388,7 @@ class TestPreProcessorVariables:
|
||||
app.plugin_connector.emit_event = AsyncMock(return_value=mock_event_ctx)
|
||||
|
||||
stage = preproc.PreProcessor(app)
|
||||
query = text_query("hello", sender_id=67890)
|
||||
query = text_query('hello', sender_id=67890)
|
||||
|
||||
result = await stage.process(query, 'PreProcessor')
|
||||
|
||||
@@ -421,10 +425,67 @@ class TestPreProcessorVariables:
|
||||
app.plugin_connector.emit_event = AsyncMock(return_value=mock_event_ctx)
|
||||
|
||||
stage = preproc.PreProcessor(app)
|
||||
query = group_text_query("hello", group_id=99999)
|
||||
query = group_text_query('hello', group_id=99999)
|
||||
|
||||
result = await stage.process(query, 'PreProcessor')
|
||||
|
||||
variables = result.new_query.variables
|
||||
assert 'group_name' in variables
|
||||
assert 'sender_name' in variables
|
||||
|
||||
|
||||
class TestPreProcessorToolSelection:
|
||||
"""Tests for Local Agent tool selection."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_local_agent_filters_selected_tools(self):
|
||||
"""Only selected tools should be exposed when all-tools mode is off."""
|
||||
preproc = get_preproc_module()
|
||||
|
||||
app = FakeApp()
|
||||
mock_session = Mock()
|
||||
mock_session.launcher_type = Mock(value='person')
|
||||
mock_session.launcher_id = 12345
|
||||
app.sess_mgr.get_session = AsyncMock(return_value=mock_session)
|
||||
|
||||
mock_conversation = Mock()
|
||||
mock_conversation.prompt = Mock(messages=[])
|
||||
mock_conversation.prompt.copy = Mock(return_value=Mock(messages=[]))
|
||||
mock_conversation.messages = []
|
||||
mock_conversation.uuid = None
|
||||
app.sess_mgr.get_conversation = AsyncMock(return_value=mock_conversation)
|
||||
|
||||
mock_model = Mock()
|
||||
mock_model.model_entity = Mock(uuid='primary-model-uuid', abilities=['func_call'])
|
||||
app.model_mgr.get_model_by_uuid = AsyncMock(return_value=mock_model)
|
||||
app.tool_mgr.get_all_tools = AsyncMock(
|
||||
return_value=[
|
||||
SimpleNamespace(name='exec'),
|
||||
SimpleNamespace(name='plugin_tool'),
|
||||
SimpleNamespace(name='mcp_tool'),
|
||||
]
|
||||
)
|
||||
|
||||
mock_event_ctx = Mock()
|
||||
mock_event_ctx.event = Mock(default_prompt=[], prompt=[])
|
||||
app.plugin_connector.emit_event = AsyncMock(return_value=mock_event_ctx)
|
||||
|
||||
stage = preproc.PreProcessor(app)
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = {
|
||||
'ai': {
|
||||
'runner': {'runner': 'local-agent'},
|
||||
'local-agent': {
|
||||
'model': {'primary': 'primary-model-uuid', 'fallbacks': []},
|
||||
'prompt': 'default',
|
||||
'enable-all-tools': False,
|
||||
'tools': ['plugin_tool'],
|
||||
},
|
||||
},
|
||||
'output': {'misc': {'at-sender': False}},
|
||||
'trigger': {'misc': {}},
|
||||
}
|
||||
|
||||
result = await stage.process(query, 'PreProcessor')
|
||||
|
||||
assert [tool.name for tool in result.new_query.use_funcs] == ['plugin_tool']
|
||||
|
||||
@@ -46,7 +46,7 @@ class TestFixedWindowAlgo:
|
||||
'safety': {
|
||||
'rate-limit': {
|
||||
'window-length': 60, # 60 seconds window
|
||||
'limitation': 10, # 10 requests per window
|
||||
'limitation': 10, # 10 requests per window
|
||||
'strategy': 'drop',
|
||||
}
|
||||
}
|
||||
@@ -75,11 +75,9 @@ class TestFixedWindowAlgo:
|
||||
# Make requests within limit
|
||||
for i in range(10):
|
||||
result = await algo.require_access(
|
||||
sample_query_with_rate_limit,
|
||||
provider_session.LauncherTypes.PERSON,
|
||||
'12345'
|
||||
sample_query_with_rate_limit, provider_session.LauncherTypes.PERSON, '12345'
|
||||
)
|
||||
assert result is True, f"Request {i+1} should be allowed"
|
||||
assert result is True, f'Request {i + 1} should be allowed'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fixedwin_exceeds_limit_drop_strategy(self, mock_app_for_algo, sample_query_with_rate_limit):
|
||||
@@ -91,20 +89,12 @@ class TestFixedWindowAlgo:
|
||||
|
||||
# Exhaust the limit
|
||||
for i in range(10):
|
||||
await algo.require_access(
|
||||
sample_query_with_rate_limit,
|
||||
provider_session.LauncherTypes.PERSON,
|
||||
'12345'
|
||||
)
|
||||
await algo.require_access(sample_query_with_rate_limit, provider_session.LauncherTypes.PERSON, '12345')
|
||||
|
||||
# Next request should be denied
|
||||
result = await algo.require_access(
|
||||
sample_query_with_rate_limit,
|
||||
provider_session.LauncherTypes.PERSON,
|
||||
'12345'
|
||||
)
|
||||
result = await algo.require_access(sample_query_with_rate_limit, provider_session.LauncherTypes.PERSON, '12345')
|
||||
|
||||
assert result is False, "Request exceeding limit should be denied"
|
||||
assert result is False, 'Request exceeding limit should be denied'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fixedwin_different_sessions_isolated(self, mock_app_for_algo, sample_query_with_rate_limit):
|
||||
@@ -116,20 +106,14 @@ class TestFixedWindowAlgo:
|
||||
|
||||
# Exhaust limit for session 1
|
||||
for i in range(10):
|
||||
await algo.require_access(
|
||||
sample_query_with_rate_limit,
|
||||
provider_session.LauncherTypes.PERSON,
|
||||
'session1'
|
||||
)
|
||||
await algo.require_access(sample_query_with_rate_limit, provider_session.LauncherTypes.PERSON, 'session1')
|
||||
|
||||
# Session 2 should still have its own limit
|
||||
result = await algo.require_access(
|
||||
sample_query_with_rate_limit,
|
||||
provider_session.LauncherTypes.PERSON,
|
||||
'session2'
|
||||
sample_query_with_rate_limit, provider_session.LauncherTypes.PERSON, 'session2'
|
||||
)
|
||||
|
||||
assert result is True, "Different session should have independent limit"
|
||||
assert result is True, 'Different session should have independent limit'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fixedwin_limit_one_request(self, mock_app_for_algo, sample_query):
|
||||
@@ -150,19 +134,11 @@ class TestFixedWindowAlgo:
|
||||
await algo.initialize()
|
||||
|
||||
# First request allowed
|
||||
result1 = await algo.require_access(
|
||||
sample_query,
|
||||
provider_session.LauncherTypes.PERSON,
|
||||
'12345'
|
||||
)
|
||||
result1 = await algo.require_access(sample_query, provider_session.LauncherTypes.PERSON, '12345')
|
||||
assert result1 is True
|
||||
|
||||
# Second request denied
|
||||
result2 = await algo.require_access(
|
||||
sample_query,
|
||||
provider_session.LauncherTypes.PERSON,
|
||||
'12345'
|
||||
)
|
||||
result2 = await algo.require_access(sample_query, provider_session.LauncherTypes.PERSON, '12345')
|
||||
assert result2 is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -174,11 +150,7 @@ class TestFixedWindowAlgo:
|
||||
await algo.initialize()
|
||||
|
||||
# First request creates container
|
||||
await algo.require_access(
|
||||
sample_query_with_rate_limit,
|
||||
provider_session.LauncherTypes.PERSON,
|
||||
'12345'
|
||||
)
|
||||
await algo.require_access(sample_query_with_rate_limit, provider_session.LauncherTypes.PERSON, '12345')
|
||||
|
||||
# Key format: 'LauncherTypes.PERSON_12345' (enum string representation)
|
||||
expected_key = 'LauncherTypes.PERSON_12345'
|
||||
@@ -230,7 +202,7 @@ class TestFixedWindowAlgo:
|
||||
|
||||
# New request should be allowed (new window)
|
||||
result = await algo.require_access(sample_query, provider_session.LauncherTypes.PERSON, 'test')
|
||||
assert result is True, "New window should allow new requests"
|
||||
assert result is True, 'New window should allow new requests'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fixedwin_wait_strategy_blocks_until_next_window(self, mock_app_for_algo, sample_query):
|
||||
@@ -256,29 +228,21 @@ class TestFixedWindowAlgo:
|
||||
|
||||
# First request allowed
|
||||
start_time = time.time()
|
||||
result1 = await algo.require_access(
|
||||
sample_query,
|
||||
provider_session.LauncherTypes.PERSON,
|
||||
'wait_test'
|
||||
)
|
||||
result1 = await algo.require_access(sample_query, provider_session.LauncherTypes.PERSON, 'wait_test')
|
||||
assert result1 is True
|
||||
|
||||
# Exhaust limit
|
||||
await algo.require_access(sample_query, provider_session.LauncherTypes.PERSON, 'wait_test')
|
||||
|
||||
# Third request should wait and then succeed
|
||||
result3 = await algo.require_access(
|
||||
sample_query,
|
||||
provider_session.LauncherTypes.PERSON,
|
||||
'wait_test'
|
||||
)
|
||||
result3 = await algo.require_access(sample_query, provider_session.LauncherTypes.PERSON, 'wait_test')
|
||||
elapsed = time.time() - start_time
|
||||
|
||||
assert result3 is True, "After wait, request should succeed"
|
||||
assert result3 is True, 'After wait, request should succeed'
|
||||
# Should have waited approximately until next window
|
||||
# With 1-second window, elapsed should be > 0.5 second (allowing for timing variance)
|
||||
# Note: This is a timing-sensitive test, so we use a generous tolerance
|
||||
assert elapsed >= 0.5, f"Should have waited for next window, elapsed={elapsed:.2f}s"
|
||||
assert elapsed >= 0.5, f'Should have waited for next window, elapsed={elapsed:.2f}s'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fixedwin_release_access(self, mock_app_for_algo, sample_query_with_rate_limit):
|
||||
@@ -289,11 +253,7 @@ class TestFixedWindowAlgo:
|
||||
await algo.initialize()
|
||||
|
||||
# release_access is empty in current implementation
|
||||
await algo.release_access(
|
||||
sample_query_with_rate_limit,
|
||||
provider_session.LauncherTypes.PERSON,
|
||||
'12345'
|
||||
)
|
||||
await algo.release_access(sample_query_with_rate_limit, provider_session.LauncherTypes.PERSON, '12345')
|
||||
|
||||
# Should not raise or change state
|
||||
assert 'person_12345' not in algo.containers
|
||||
|
||||
@@ -36,6 +36,11 @@ def get_entities_module():
|
||||
return import_module('langbot.pkg.pipeline.entities')
|
||||
|
||||
|
||||
def get_plugin_diagnostics_module():
|
||||
"""Lazy import for plugin diagnostic attribution helpers."""
|
||||
return import_module('langbot.pkg.pipeline.plugin_diagnostics')
|
||||
|
||||
|
||||
def make_wrapper_config():
|
||||
"""Create a pipeline config for wrapper tests."""
|
||||
return {
|
||||
@@ -55,7 +60,7 @@ def make_session():
|
||||
launcher_type=provider_session.LauncherTypes.PERSON,
|
||||
launcher_id=12345,
|
||||
sender_id=12345,
|
||||
use_prompt_name="default",
|
||||
use_prompt_name='default',
|
||||
using_conversation=None,
|
||||
conversations=[],
|
||||
)
|
||||
@@ -93,11 +98,9 @@ class TestResponseWrapperMessageChain:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_messages = [
|
||||
platform_message.MessageChain([platform_message.Plain(text="response")])
|
||||
]
|
||||
query.resp_messages = [platform_message.MessageChain([platform_message.Plain(text='response')])]
|
||||
query.resp_message_chain = []
|
||||
|
||||
results = []
|
||||
@@ -108,6 +111,45 @@ class TestResponseWrapperMessageChain:
|
||||
assert results[0].result_type == entities.ResultType.CONTINUE
|
||||
assert len(results[0].new_query.resp_message_chain) == 1
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_message_chain_direct_append_consumes_pending_plugin_source(self):
|
||||
"""MessageChain replies from earlier plugin events keep attribution."""
|
||||
wrapper = get_wrapper_module()
|
||||
|
||||
app = FakeApp()
|
||||
stage = wrapper.ResponseWrapper(app)
|
||||
await stage.initialize(make_wrapper_config())
|
||||
|
||||
reply_chain = platform_message.MessageChain([platform_message.Plain(text='response')])
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = make_wrapper_config()
|
||||
query.resp_messages = [reply_chain]
|
||||
query.resp_message_chain = []
|
||||
plugin_diagnostics = get_plugin_diagnostics_module()
|
||||
plugin_diagnostics.record_pending_plugin_response_source(
|
||||
query,
|
||||
reply_chain,
|
||||
[
|
||||
{
|
||||
'kind': 'reply_message_chain',
|
||||
'plugin': {'author': 'tester', 'name': 'demo'},
|
||||
}
|
||||
],
|
||||
[{'manifest': {'metadata': {'author': 'observer', 'name': 'not-reply-source'}}}],
|
||||
'PersonNormalMessageReceived',
|
||||
)
|
||||
|
||||
results = []
|
||||
async for result in stage.process(query, 'ResponseWrapper'):
|
||||
results.append(result)
|
||||
|
||||
sources = plugin_diagnostics._get_response_sources(results[0].new_query, 0)
|
||||
assert sources[0].plugin == {'author': 'tester', 'name': 'demo'}
|
||||
assert sources[0].event_name == 'PersonNormalMessageReceived'
|
||||
assert sources[0].is_approximate is False
|
||||
assert '_plugin_response_sources' not in query.variables
|
||||
assert '_plugin_pending_response_sources' not in query.variables
|
||||
|
||||
|
||||
class TestResponseWrapperCommand:
|
||||
"""Tests for command response wrapping."""
|
||||
@@ -125,7 +167,7 @@ class TestResponseWrapperCommand:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_message_chain = []
|
||||
|
||||
@@ -133,7 +175,7 @@ class TestResponseWrapperCommand:
|
||||
command_resp = Mock()
|
||||
command_resp.role = 'command'
|
||||
command_resp.get_content_platform_message_chain = Mock(
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text="Help info")])
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text='Help info')])
|
||||
)
|
||||
query.resp_messages = [command_resp]
|
||||
|
||||
@@ -163,7 +205,7 @@ class TestResponseWrapperPlugin:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_message_chain = []
|
||||
|
||||
@@ -171,7 +213,7 @@ class TestResponseWrapperPlugin:
|
||||
plugin_resp = Mock()
|
||||
plugin_resp.role = 'plugin'
|
||||
plugin_resp.get_content_platform_message_chain = Mock(
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text="Plugin response")])
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text='Plugin response')])
|
||||
)
|
||||
query.resp_messages = [plugin_resp]
|
||||
|
||||
@@ -211,17 +253,17 @@ class TestResponseWrapperAssistant:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_message_chain = []
|
||||
|
||||
# Create assistant response with content
|
||||
assistant_resp = Mock()
|
||||
assistant_resp.role = 'assistant'
|
||||
assistant_resp.content = "Hello back!"
|
||||
assistant_resp.content = 'Hello back!'
|
||||
assistant_resp.tool_calls = None
|
||||
assistant_resp.get_content_platform_message_chain = Mock(
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text="Hello back!")])
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text='Hello back!')])
|
||||
)
|
||||
query.resp_messages = [assistant_resp]
|
||||
|
||||
@@ -247,7 +289,7 @@ class TestResponseWrapperAssistant:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_message_chain = []
|
||||
|
||||
@@ -292,7 +334,7 @@ class TestResponseWrapperAssistant:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_message_chain = []
|
||||
|
||||
@@ -303,10 +345,10 @@ class TestResponseWrapperAssistant:
|
||||
|
||||
assistant_resp = Mock()
|
||||
assistant_resp.role = 'assistant'
|
||||
assistant_resp.content = "Processing..."
|
||||
assistant_resp.content = 'Processing...'
|
||||
assistant_resp.tool_calls = [mock_tool_call]
|
||||
assistant_resp.get_content_platform_message_chain = Mock(
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text="Processing...")])
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text='Processing...')])
|
||||
)
|
||||
query.resp_messages = [assistant_resp]
|
||||
|
||||
@@ -346,17 +388,17 @@ class TestResponseWrapperInterrupt:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_message_chain = []
|
||||
|
||||
# Create assistant response with content
|
||||
assistant_resp = Mock()
|
||||
assistant_resp.role = 'assistant'
|
||||
assistant_resp.content = "Hello!"
|
||||
assistant_resp.content = 'Hello!'
|
||||
assistant_resp.tool_calls = None
|
||||
assistant_resp.get_content_platform_message_chain = Mock(
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text="Hello!")])
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text='Hello!')])
|
||||
)
|
||||
query.resp_messages = [assistant_resp]
|
||||
|
||||
@@ -384,7 +426,7 @@ class TestResponseWrapperCustomReply:
|
||||
app.sess_mgr.get_session = AsyncMock(return_value=session)
|
||||
|
||||
# Mock plugin connector with custom reply
|
||||
custom_chain = platform_message.MessageChain([platform_message.Plain(text="Custom reply")])
|
||||
custom_chain = platform_message.MessageChain([platform_message.Plain(text='Custom reply')])
|
||||
mock_event_ctx = Mock()
|
||||
mock_event_ctx.is_prevented_default = Mock(return_value=False)
|
||||
mock_event_ctx.event = Mock()
|
||||
@@ -397,17 +439,17 @@ class TestResponseWrapperCustomReply:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_message_chain = []
|
||||
|
||||
# Create assistant response
|
||||
assistant_resp = Mock()
|
||||
assistant_resp.role = 'assistant'
|
||||
assistant_resp.content = "Default reply"
|
||||
assistant_resp.content = 'Default reply'
|
||||
assistant_resp.tool_calls = None
|
||||
assistant_resp.get_content_platform_message_chain = Mock(
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text="Default reply")])
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text='Default reply')])
|
||||
)
|
||||
query.resp_messages = [assistant_resp]
|
||||
|
||||
@@ -421,7 +463,105 @@ class TestResponseWrapperCustomReply:
|
||||
assert len(results[0].new_query.resp_message_chain) == 1
|
||||
# Should be the custom chain
|
||||
chain = results[0].new_query.resp_message_chain[0]
|
||||
assert "Custom reply" in str(chain)
|
||||
assert 'Custom reply' in str(chain)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_custom_reply_records_plugin_source(self):
|
||||
"""Plugin reply_message_chain should keep emitted plugin attribution."""
|
||||
wrapper = get_wrapper_module()
|
||||
|
||||
app = FakeApp()
|
||||
app.sess_mgr.get_session = AsyncMock(return_value=make_session())
|
||||
|
||||
custom_chain = platform_message.MessageChain([platform_message.Plain(text='Custom reply')])
|
||||
mock_event_ctx = Mock()
|
||||
mock_event_ctx.is_prevented_default = Mock(return_value=False)
|
||||
mock_event_ctx.event = Mock()
|
||||
mock_event_ctx.event.reply_message_chain = custom_chain
|
||||
mock_event_ctx._emitted_plugins = [
|
||||
{
|
||||
'manifest': {'metadata': {'author': 'observer', 'name': 'not-reply-source'}},
|
||||
'plugin_config': {'token': 'secret-token'},
|
||||
},
|
||||
]
|
||||
mock_event_ctx._response_sources = [
|
||||
{
|
||||
'kind': 'reply_message_chain',
|
||||
'plugin': {'author': 'tester', 'name': 'demo'},
|
||||
}
|
||||
]
|
||||
app.plugin_connector.emit_event = AsyncMock(return_value=mock_event_ctx)
|
||||
|
||||
stage = wrapper.ResponseWrapper(app)
|
||||
pipeline_config = make_wrapper_config()
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_message_chain = []
|
||||
assistant_resp = Mock()
|
||||
assistant_resp.role = 'assistant'
|
||||
assistant_resp.content = 'Default reply'
|
||||
assistant_resp.tool_calls = None
|
||||
assistant_resp.get_content_platform_message_chain = Mock(
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text='Default reply')])
|
||||
)
|
||||
query.resp_messages = [assistant_resp]
|
||||
|
||||
results = []
|
||||
async for result in stage.process(query, 'ResponseWrapper'):
|
||||
results.append(result)
|
||||
|
||||
plugin_diagnostics = get_plugin_diagnostics_module()
|
||||
sources = plugin_diagnostics._get_response_sources(results[0].new_query, 0)
|
||||
assert sources[0].plugin == {'author': 'tester', 'name': 'demo'}
|
||||
assert sources[0].event_name == 'NormalMessageResponded'
|
||||
assert sources[0].is_approximate is False
|
||||
assert 'secret-token' not in str(sources)
|
||||
assert '_plugin_response_sources' not in query.variables
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_custom_reply_falls_back_to_emitted_plugins_for_old_runtime(self):
|
||||
"""Older plugin runtimes without response_sources keep approximate attribution."""
|
||||
wrapper = get_wrapper_module()
|
||||
|
||||
app = FakeApp()
|
||||
app.sess_mgr.get_session = AsyncMock(return_value=make_session())
|
||||
|
||||
custom_chain = platform_message.MessageChain([platform_message.Plain(text='Custom reply')])
|
||||
mock_event_ctx = Mock()
|
||||
mock_event_ctx.is_prevented_default = Mock(return_value=False)
|
||||
mock_event_ctx.event = Mock()
|
||||
mock_event_ctx.event.reply_message_chain = custom_chain
|
||||
mock_event_ctx._emitted_plugins = [
|
||||
{'manifest': {'metadata': {'author': 'tester', 'name': 'demo'}}},
|
||||
]
|
||||
app.plugin_connector.emit_event = AsyncMock(return_value=mock_event_ctx)
|
||||
|
||||
stage = wrapper.ResponseWrapper(app)
|
||||
pipeline_config = make_wrapper_config()
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_message_chain = []
|
||||
assistant_resp = Mock()
|
||||
assistant_resp.role = 'assistant'
|
||||
assistant_resp.content = 'Default reply'
|
||||
assistant_resp.tool_calls = None
|
||||
assistant_resp.get_content_platform_message_chain = Mock(
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text='Default reply')])
|
||||
)
|
||||
query.resp_messages = [assistant_resp]
|
||||
|
||||
results = []
|
||||
async for result in stage.process(query, 'ResponseWrapper'):
|
||||
results.append(result)
|
||||
|
||||
plugin_diagnostics = get_plugin_diagnostics_module()
|
||||
sources = plugin_diagnostics._get_response_sources(results[0].new_query, 0)
|
||||
assert sources[0].plugin == {'author': 'tester', 'name': 'demo'}
|
||||
assert sources[0].is_approximate is True
|
||||
|
||||
|
||||
class TestResponseWrapperVariables:
|
||||
@@ -452,7 +592,7 @@ class TestResponseWrapperVariables:
|
||||
|
||||
await stage.initialize(pipeline_config)
|
||||
|
||||
query = text_query("hello")
|
||||
query = text_query('hello')
|
||||
query.pipeline_config = pipeline_config
|
||||
query.resp_message_chain = []
|
||||
query.variables['_pipeline_bound_plugins'] = ['plugin1', 'plugin2']
|
||||
@@ -460,10 +600,10 @@ class TestResponseWrapperVariables:
|
||||
# Create assistant response
|
||||
assistant_resp = Mock()
|
||||
assistant_resp.role = 'assistant'
|
||||
assistant_resp.content = "Hello"
|
||||
assistant_resp.content = 'Hello'
|
||||
assistant_resp.tool_calls = None
|
||||
assistant_resp.get_content_platform_message_chain = Mock(
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text="Hello")])
|
||||
return_value=platform_message.MessageChain([platform_message.Plain(text='Hello')])
|
||||
)
|
||||
query.resp_messages = [assistant_resp]
|
||||
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
"""Unit tests for ResponseWrapper outbound-attachment helpers.
|
||||
|
||||
Covers the sandbox -> user attachment path added for the Box attachment
|
||||
round-trip:
|
||||
|
||||
* ``_is_final_assistant_message`` — only the terminal, tool-call-free assistant
|
||||
message (or a final MessageChunk) should trigger collection.
|
||||
* ``_append_outbound_attachments`` — collects sandbox outbox files exactly once
|
||||
per query and maps each descriptor to the right platform component, swallowing
|
||||
collection errors.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
|
||||
import pytest
|
||||
|
||||
import langbot_plugin.api.entities.builtin.platform.message as platform_message
|
||||
import langbot_plugin.api.entities.builtin.provider.message as provider_message
|
||||
|
||||
from langbot.pkg.pipeline.wrapper.wrapper import ResponseWrapper
|
||||
|
||||
|
||||
def _make_wrapper(box_service) -> ResponseWrapper:
|
||||
app = SimpleNamespace(logger=Mock())
|
||||
wrapper = ResponseWrapper.__new__(ResponseWrapper)
|
||||
wrapper.ap = app
|
||||
return wrapper
|
||||
|
||||
|
||||
def _make_query():
|
||||
return SimpleNamespace(variables={})
|
||||
|
||||
|
||||
def test_is_final_assistant_message_plain_assistant():
|
||||
wrapper = _make_wrapper(box_service=None)
|
||||
msg = provider_message.Message(role='assistant', content='done')
|
||||
assert wrapper._is_final_assistant_message(msg) is True
|
||||
|
||||
|
||||
def test_is_final_assistant_message_rejects_non_assistant():
|
||||
wrapper = _make_wrapper(box_service=None)
|
||||
msg = provider_message.Message(role='tool', content='{}')
|
||||
assert wrapper._is_final_assistant_message(msg) is False
|
||||
|
||||
|
||||
def test_is_final_assistant_message_rejects_tool_call_round():
|
||||
wrapper = _make_wrapper(box_service=None)
|
||||
msg = provider_message.Message(
|
||||
role='assistant',
|
||||
content='calling',
|
||||
tool_calls=[
|
||||
provider_message.ToolCall(
|
||||
id='c1',
|
||||
type='function',
|
||||
function=provider_message.FunctionCall(name='exec', arguments='{}'),
|
||||
)
|
||||
],
|
||||
)
|
||||
assert wrapper._is_final_assistant_message(msg) is False
|
||||
|
||||
|
||||
def test_is_final_assistant_message_non_final_chunk():
|
||||
wrapper = _make_wrapper(box_service=None)
|
||||
chunk = provider_message.MessageChunk(role='assistant', content='partial', is_final=False)
|
||||
assert wrapper._is_final_assistant_message(chunk) is False
|
||||
|
||||
final_chunk = provider_message.MessageChunk(role='assistant', content='partial', is_final=True)
|
||||
assert wrapper._is_final_assistant_message(final_chunk) is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_append_outbound_attachments_maps_each_type():
|
||||
box_service = SimpleNamespace(
|
||||
available=True,
|
||||
collect_outbound_attachments=AsyncMock(
|
||||
return_value=[
|
||||
{'type': 'Image', 'base64': 'data:image/png;base64,iVBORw0K'},
|
||||
{'type': 'Voice', 'base64': 'data:audio/wav;base64,UklGRg=='},
|
||||
{'type': 'File', 'name': 'report.xlsx', 'base64': 'data:app;base64,UEsDBA=='},
|
||||
]
|
||||
),
|
||||
)
|
||||
wrapper = _make_wrapper(box_service)
|
||||
wrapper.ap.box_service = box_service
|
||||
query = _make_query()
|
||||
chain = platform_message.MessageChain([])
|
||||
|
||||
await wrapper._append_outbound_attachments(query, chain)
|
||||
|
||||
kinds = [type(c).__name__ for c in chain]
|
||||
assert kinds == ['Image', 'Voice', 'File']
|
||||
assert query.variables['_sandbox_outbound_collected'] is True
|
||||
# File keeps its name
|
||||
file_comp = chain[2]
|
||||
assert getattr(file_comp, 'name', None) == 'report.xlsx'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_append_outbound_attachments_runs_once_per_query():
|
||||
box_service = SimpleNamespace(
|
||||
available=True,
|
||||
collect_outbound_attachments=AsyncMock(return_value=[]),
|
||||
)
|
||||
wrapper = _make_wrapper(box_service)
|
||||
wrapper.ap.box_service = box_service
|
||||
query = _make_query()
|
||||
query.variables['_sandbox_outbound_collected'] = True
|
||||
chain = platform_message.MessageChain([])
|
||||
|
||||
await wrapper._append_outbound_attachments(query, chain)
|
||||
|
||||
box_service.collect_outbound_attachments.assert_not_awaited()
|
||||
assert len(chain) == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_append_outbound_attachments_noop_without_box_service():
|
||||
wrapper = _make_wrapper(box_service=None)
|
||||
wrapper.ap.box_service = None
|
||||
query = _make_query()
|
||||
chain = platform_message.MessageChain([])
|
||||
|
||||
await wrapper._append_outbound_attachments(query, chain)
|
||||
assert len(chain) == 0
|
||||
# not marked collected, since service is unavailable
|
||||
assert '_sandbox_outbound_collected' not in query.variables
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_append_outbound_attachments_swallows_collection_error():
|
||||
box_service = SimpleNamespace(
|
||||
available=True,
|
||||
collect_outbound_attachments=AsyncMock(side_effect=RuntimeError('boom')),
|
||||
)
|
||||
wrapper = _make_wrapper(box_service)
|
||||
wrapper.ap.box_service = box_service
|
||||
query = _make_query()
|
||||
chain = platform_message.MessageChain([])
|
||||
|
||||
# must not raise
|
||||
await wrapper._append_outbound_attachments(query, chain)
|
||||
assert len(chain) == 0
|
||||
wrapper.ap.logger.warning.assert_called_once()
|
||||
@@ -0,0 +1,538 @@
|
||||
import pytest
|
||||
import aiocqhttp
|
||||
|
||||
import langbot_plugin.api.entities.builtin.platform.message as platform_message
|
||||
from langbot.pkg.platform.sources.aiocqhttp import (
|
||||
AiocqhttpAdapter,
|
||||
AiocqhttpEventConverter,
|
||||
AiocqhttpMessageConverter,
|
||||
)
|
||||
|
||||
|
||||
async def _convert_single(component: platform_message.MessageComponent):
|
||||
chain = platform_message.MessageChain([component])
|
||||
message, _, _ = await AiocqhttpMessageConverter.yiri2target(chain)
|
||||
return message[0]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
('payload', 'expected'),
|
||||
[
|
||||
('data:image/jpeg;base64,raw-image', 'base64://raw-image'),
|
||||
('raw-image', 'base64://raw-image'),
|
||||
('base64://raw-image', 'base64://raw-image'),
|
||||
],
|
||||
)
|
||||
async def test_image_base64_payload_is_normalized(payload, expected):
|
||||
segment = await _convert_single(platform_message.Image(base64=payload))
|
||||
|
||||
assert segment.type == 'image'
|
||||
assert segment.data['file'] == expected
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_voice_data_uri_base64_payload_is_normalized():
|
||||
segment = await _convert_single(platform_message.Voice(base64='data:audio/wav;base64,raw-voice'))
|
||||
|
||||
assert segment.type == 'record'
|
||||
assert segment.data['file'] == 'base64://raw-voice'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
('component', 'expected'),
|
||||
[
|
||||
(
|
||||
platform_message.File(name='report.txt', base64='data:text/plain;base64,raw-file'),
|
||||
{'file': 'base64://raw-file', 'name': 'report.txt'},
|
||||
),
|
||||
(
|
||||
platform_message.File(name='report.txt', base64='raw-file'),
|
||||
{'file': 'base64://raw-file', 'name': 'report.txt'},
|
||||
),
|
||||
(
|
||||
platform_message.File(name='a.txt', url='http://example.com/a.txt'),
|
||||
{'file': 'http://example.com/a.txt', 'name': 'a.txt'},
|
||||
),
|
||||
(
|
||||
platform_message.File(name='a.txt', path='/tmp/a.txt'),
|
||||
{'file': '/tmp/a.txt', 'name': 'a.txt'},
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_file_message_uses_available_file_source(component, expected):
|
||||
segment = await _convert_single(component)
|
||||
|
||||
assert segment.type == 'file'
|
||||
assert segment.data == expected
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_forward_image_base64_payload_is_normalized():
|
||||
forward = platform_message.Forward(
|
||||
node_list=[
|
||||
platform_message.ForwardMessageNode(
|
||||
sender_id='10001',
|
||||
sender_name='Tester',
|
||||
message_chain=platform_message.MessageChain(
|
||||
[platform_message.Image(base64='data:image/png;base64,raw-forward-image')]
|
||||
),
|
||||
)
|
||||
]
|
||||
)
|
||||
messages = []
|
||||
|
||||
class Logger:
|
||||
async def info(self, _message):
|
||||
return None
|
||||
|
||||
async def error(self, _message):
|
||||
return None
|
||||
|
||||
class Bot:
|
||||
async def call_action(self, action, **kwargs):
|
||||
assert action == 'send_forward_msg'
|
||||
messages.append(kwargs)
|
||||
|
||||
platform = AiocqhttpAdapter.model_construct(
|
||||
bot_account_id='10000',
|
||||
config={},
|
||||
logger=Logger(),
|
||||
bot=Bot(),
|
||||
)
|
||||
|
||||
await platform._send_forward_message(1000, forward)
|
||||
|
||||
assert messages[0]['messages'][0]['data']['content'][0] == {
|
||||
'type': 'image',
|
||||
'data': {'file': 'base64://raw-forward-image'},
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_message_member_name_prefers_group_card():
|
||||
event = aiocqhttp.Event(
|
||||
{
|
||||
'post_type': 'message',
|
||||
'message_type': 'group',
|
||||
'message_id': 1000,
|
||||
'message': '',
|
||||
'time': 1776491725,
|
||||
'group_id': 2000,
|
||||
'sender': {
|
||||
'user_id': 3000,
|
||||
'nickname': 'QQ Nickname',
|
||||
'card': 'Group Card',
|
||||
'role': 'member',
|
||||
'title': 'Special Title',
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
class Bot:
|
||||
async def get_group_info(self, group_id):
|
||||
assert group_id == 2000
|
||||
return {'group_id': group_id, 'group_name': 'Test Group'}
|
||||
|
||||
converted = await AiocqhttpEventConverter().target2yiri(event, Bot())
|
||||
|
||||
assert converted.sender.member_name == 'Group Card'
|
||||
assert converted.sender.group.id == 2000
|
||||
assert converted.sender.group.name == 'Test Group'
|
||||
assert converted.sender.special_title == 'Special Title'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_message_member_name_falls_back_to_nickname():
|
||||
event = aiocqhttp.Event(
|
||||
{
|
||||
'post_type': 'message',
|
||||
'message_type': 'group',
|
||||
'message_id': 1000,
|
||||
'message': '',
|
||||
'time': 1776491725,
|
||||
'group_id': 2000,
|
||||
'sender': {
|
||||
'user_id': 3000,
|
||||
'nickname': 'QQ Nickname',
|
||||
'card': '',
|
||||
'role': 'member',
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
converted = await AiocqhttpEventConverter().target2yiri(event)
|
||||
|
||||
assert converted.sender.member_name == 'QQ Nickname'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_message_special_title_uses_group_member_info_when_sender_title_is_empty():
|
||||
event = aiocqhttp.Event(
|
||||
{
|
||||
'post_type': 'message',
|
||||
'message_type': 'group',
|
||||
'message_id': 1000,
|
||||
'message': '',
|
||||
'time': 1776491725,
|
||||
'group_id': 2000,
|
||||
'sender': {
|
||||
'user_id': 3000,
|
||||
'nickname': 'QQ Nickname',
|
||||
'card': 'Group Card',
|
||||
'role': 'member',
|
||||
'title': '',
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
class Bot:
|
||||
async def get_group_info(self, group_id):
|
||||
return {'group_id': group_id, 'group_name': 'Test Group'}
|
||||
|
||||
async def get_group_member_info(self, group_id, user_id):
|
||||
assert group_id == 2000
|
||||
assert user_id == 3000
|
||||
return {'group_id': group_id, 'user_id': user_id, 'title': 'Member Title'}
|
||||
|
||||
converted = await AiocqhttpEventConverter().target2yiri(event, Bot())
|
||||
|
||||
assert converted.sender.special_title == 'Member Title'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_message_special_title_does_not_lookup_when_sender_title_exists():
|
||||
event = aiocqhttp.Event(
|
||||
{
|
||||
'post_type': 'message',
|
||||
'message_type': 'group',
|
||||
'message_id': 1000,
|
||||
'message': '',
|
||||
'time': 1776491725,
|
||||
'group_id': 2000,
|
||||
'sender': {
|
||||
'user_id': 3000,
|
||||
'nickname': 'QQ Nickname',
|
||||
'card': 'Group Card',
|
||||
'role': 'member',
|
||||
'title': 'Event Title',
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
class Bot:
|
||||
async def get_group_info(self, group_id):
|
||||
return {'group_id': group_id, 'group_name': 'Test Group'}
|
||||
|
||||
async def get_group_member_info(self, group_id, user_id):
|
||||
raise AssertionError('get_group_member_info should not be called')
|
||||
|
||||
converted = await AiocqhttpEventConverter().target2yiri(event, Bot())
|
||||
|
||||
assert converted.sender.special_title == 'Event Title'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_message_special_title_member_info_failure_is_cached(monkeypatch):
|
||||
event = aiocqhttp.Event(
|
||||
{
|
||||
'post_type': 'message',
|
||||
'message_type': 'group',
|
||||
'message_id': 1000,
|
||||
'message': '',
|
||||
'time': 1776491725,
|
||||
'group_id': 2000,
|
||||
'sender': {
|
||||
'user_id': 3000,
|
||||
'nickname': 'QQ Nickname',
|
||||
'card': 'Group Card',
|
||||
'role': 'member',
|
||||
'title': '',
|
||||
},
|
||||
}
|
||||
)
|
||||
now = 1000.0
|
||||
|
||||
class Bot:
|
||||
member_info_calls = 0
|
||||
|
||||
async def get_group_info(self, group_id):
|
||||
return {'group_id': group_id, 'group_name': 'Test Group'}
|
||||
|
||||
async def get_group_member_info(self, group_id, user_id):
|
||||
self.member_info_calls += 1
|
||||
raise RuntimeError('api unavailable')
|
||||
|
||||
monkeypatch.setattr('langbot.pkg.platform.sources.aiocqhttp.time.monotonic', lambda: now)
|
||||
|
||||
bot = Bot()
|
||||
converter = AiocqhttpEventConverter()
|
||||
|
||||
first = await converter.target2yiri(event, bot)
|
||||
second = await converter.target2yiri(event, bot)
|
||||
|
||||
assert first.sender.special_title == ''
|
||||
assert second.sender.special_title == ''
|
||||
assert bot.member_info_calls == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_message_special_title_member_info_cache_expires(monkeypatch):
|
||||
event = aiocqhttp.Event(
|
||||
{
|
||||
'post_type': 'message',
|
||||
'message_type': 'group',
|
||||
'message_id': 1000,
|
||||
'message': '',
|
||||
'time': 1776491725,
|
||||
'group_id': 2000,
|
||||
'sender': {
|
||||
'user_id': 3000,
|
||||
'nickname': 'QQ Nickname',
|
||||
'card': 'Group Card',
|
||||
'role': 'member',
|
||||
'title': '',
|
||||
},
|
||||
}
|
||||
)
|
||||
now = 1000.0
|
||||
|
||||
class Bot:
|
||||
member_info_calls = 0
|
||||
|
||||
async def get_group_info(self, group_id):
|
||||
return {'group_id': group_id, 'group_name': 'Test Group'}
|
||||
|
||||
async def get_group_member_info(self, group_id, user_id):
|
||||
self.member_info_calls += 1
|
||||
return {
|
||||
'group_id': group_id,
|
||||
'user_id': user_id,
|
||||
'title': f'Member Title {self.member_info_calls}',
|
||||
}
|
||||
|
||||
monkeypatch.setattr('langbot.pkg.platform.sources.aiocqhttp.time.monotonic', lambda: now)
|
||||
|
||||
bot = Bot()
|
||||
converter = AiocqhttpEventConverter()
|
||||
|
||||
first = await converter.target2yiri(event, bot)
|
||||
now = 87401.0
|
||||
second = await converter.target2yiri(event, bot)
|
||||
|
||||
assert first.sender.special_title == 'Member Title 1'
|
||||
assert second.sender.special_title == 'Member Title 2'
|
||||
assert bot.member_info_calls == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_message_special_title_retries_after_negative_cache_expires(monkeypatch):
|
||||
event = aiocqhttp.Event(
|
||||
{
|
||||
'post_type': 'message',
|
||||
'message_type': 'group',
|
||||
'message_id': 1000,
|
||||
'message': '',
|
||||
'time': 1776491725,
|
||||
'group_id': 2000,
|
||||
'sender': {
|
||||
'user_id': 3000,
|
||||
'nickname': 'QQ Nickname',
|
||||
'card': 'Group Card',
|
||||
'role': 'member',
|
||||
'title': '',
|
||||
},
|
||||
}
|
||||
)
|
||||
now = 1000.0
|
||||
|
||||
class Bot:
|
||||
member_info_calls = 0
|
||||
|
||||
async def get_group_info(self, group_id):
|
||||
return {'group_id': group_id, 'group_name': 'Test Group'}
|
||||
|
||||
async def get_group_member_info(self, group_id, user_id):
|
||||
self.member_info_calls += 1
|
||||
if self.member_info_calls == 1:
|
||||
raise RuntimeError('api unavailable')
|
||||
return {'group_id': group_id, 'user_id': user_id, 'title': 'Recovered Title'}
|
||||
|
||||
monkeypatch.setattr('langbot.pkg.platform.sources.aiocqhttp.time.monotonic', lambda: now)
|
||||
|
||||
bot = Bot()
|
||||
converter = AiocqhttpEventConverter()
|
||||
|
||||
failed = await converter.target2yiri(event, bot)
|
||||
now = 1601.0
|
||||
recovered = await converter.target2yiri(event, bot)
|
||||
|
||||
assert failed.sender.special_title == ''
|
||||
assert recovered.sender.special_title == 'Recovered Title'
|
||||
assert bot.member_info_calls == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_message_group_name_is_cached(monkeypatch):
|
||||
event = aiocqhttp.Event(
|
||||
{
|
||||
'post_type': 'message',
|
||||
'message_type': 'group',
|
||||
'message_id': 1000,
|
||||
'message': '',
|
||||
'time': 1776491725,
|
||||
'group_id': 2000,
|
||||
'sender': {
|
||||
'user_id': 3000,
|
||||
'nickname': 'QQ Nickname',
|
||||
'card': 'Group Card',
|
||||
'role': 'member',
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
class Bot:
|
||||
calls = 0
|
||||
|
||||
async def get_group_info(self, group_id):
|
||||
self.calls += 1
|
||||
assert group_id == 2000
|
||||
return {'group_id': group_id, 'group_name': 'Cached Group'}
|
||||
|
||||
monotonic = 1000.0
|
||||
monkeypatch.setattr('langbot.pkg.platform.sources.aiocqhttp.time.monotonic', lambda: monotonic)
|
||||
|
||||
bot = Bot()
|
||||
converter = AiocqhttpEventConverter()
|
||||
|
||||
first = await converter.target2yiri(event, bot)
|
||||
second = await converter.target2yiri(event, bot)
|
||||
|
||||
assert first.sender.group.name == 'Cached Group'
|
||||
assert second.sender.group.name == 'Cached Group'
|
||||
assert bot.calls == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_message_group_name_cache_expires(monkeypatch):
|
||||
event = aiocqhttp.Event(
|
||||
{
|
||||
'post_type': 'message',
|
||||
'message_type': 'group',
|
||||
'message_id': 1000,
|
||||
'message': '',
|
||||
'time': 1776491725,
|
||||
'group_id': 2000,
|
||||
'sender': {
|
||||
'user_id': 3000,
|
||||
'nickname': 'QQ Nickname',
|
||||
'card': 'Group Card',
|
||||
'role': 'member',
|
||||
},
|
||||
}
|
||||
)
|
||||
now = 1000.0
|
||||
|
||||
class Bot:
|
||||
calls = 0
|
||||
|
||||
async def get_group_info(self, group_id):
|
||||
self.calls += 1
|
||||
return {'group_id': group_id, 'group_name': f'Group Name {self.calls}'}
|
||||
|
||||
monkeypatch.setattr('langbot.pkg.platform.sources.aiocqhttp.time.monotonic', lambda: now)
|
||||
|
||||
bot = Bot()
|
||||
converter = AiocqhttpEventConverter()
|
||||
|
||||
first = await converter.target2yiri(event, bot)
|
||||
now = 4601.0
|
||||
second = await converter.target2yiri(event, bot)
|
||||
|
||||
assert first.sender.group.name == 'Group Name 1'
|
||||
assert second.sender.group.name == 'Group Name 2'
|
||||
assert bot.calls == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_message_group_name_uses_placeholder_when_lookup_fails(monkeypatch):
|
||||
event = aiocqhttp.Event(
|
||||
{
|
||||
'post_type': 'message',
|
||||
'message_type': 'group',
|
||||
'message_id': 1000,
|
||||
'message': '',
|
||||
'time': 1776491725,
|
||||
'group_id': 2000,
|
||||
'sender': {
|
||||
'user_id': 3000,
|
||||
'nickname': 'QQ Nickname',
|
||||
'card': 'Group Card',
|
||||
'role': 'member',
|
||||
},
|
||||
}
|
||||
)
|
||||
now = 1000.0
|
||||
|
||||
class Bot:
|
||||
calls = 0
|
||||
|
||||
async def get_group_info(self, group_id):
|
||||
self.calls += 1
|
||||
raise RuntimeError('api unavailable')
|
||||
|
||||
monkeypatch.setattr('langbot.pkg.platform.sources.aiocqhttp.time.monotonic', lambda: now)
|
||||
|
||||
bot = Bot()
|
||||
converter = AiocqhttpEventConverter()
|
||||
|
||||
converted = await converter.target2yiri(event, bot)
|
||||
cached_failure = await converter.target2yiri(event, bot)
|
||||
|
||||
assert converted.sender.group.name == 'Group 2000'
|
||||
assert cached_failure.sender.group.name == 'Group 2000'
|
||||
assert bot.calls == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_message_group_name_retries_after_negative_cache_expires(monkeypatch):
|
||||
event = aiocqhttp.Event(
|
||||
{
|
||||
'post_type': 'message',
|
||||
'message_type': 'group',
|
||||
'message_id': 1000,
|
||||
'message': '',
|
||||
'time': 1776491725,
|
||||
'group_id': 2000,
|
||||
'sender': {
|
||||
'user_id': 3000,
|
||||
'nickname': 'QQ Nickname',
|
||||
'card': 'Group Card',
|
||||
'role': 'member',
|
||||
},
|
||||
}
|
||||
)
|
||||
now = 1000.0
|
||||
|
||||
class Bot:
|
||||
calls = 0
|
||||
|
||||
async def get_group_info(self, group_id):
|
||||
self.calls += 1
|
||||
if self.calls == 1:
|
||||
raise RuntimeError('api unavailable')
|
||||
return {'group_id': group_id, 'group_name': 'Recovered Group'}
|
||||
|
||||
monkeypatch.setattr('langbot.pkg.platform.sources.aiocqhttp.time.monotonic', lambda: now)
|
||||
|
||||
bot = Bot()
|
||||
converter = AiocqhttpEventConverter()
|
||||
|
||||
failed = await converter.target2yiri(event, bot)
|
||||
now = 1061.0
|
||||
recovered = await converter.target2yiri(event, bot)
|
||||
|
||||
assert failed.sender.group.name == 'Group 2000'
|
||||
assert recovered.sender.group.name == 'Recovered Group'
|
||||
assert bot.calls == 2
|
||||
@@ -0,0 +1,92 @@
|
||||
"""Unit tests for WebSocketAdapter._process_image_components.
|
||||
|
||||
The web debug client uploads Image / Voice / File components carrying a storage
|
||||
key in ``path``. This helper resolves each to a base64 data URI (so multimodal
|
||||
LLM input and the Box sandbox inbox have usable bytes), then deletes the
|
||||
consumed storage object and clears ``path``. Covers mimetype selection per
|
||||
type and graceful error handling.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from langbot.pkg.platform.sources.websocket_adapter import WebSocketAdapter
|
||||
|
||||
|
||||
def _make_adapter(load_return=b'hello', load_side_effect=None):
|
||||
provider = Mock()
|
||||
provider.load = AsyncMock(return_value=load_return, side_effect=load_side_effect)
|
||||
provider.delete = AsyncMock()
|
||||
ap = Mock()
|
||||
ap.storage_mgr.storage_provider = provider
|
||||
logger = Mock()
|
||||
logger.error = AsyncMock()
|
||||
# WebSocketAdapter is a pydantic model; bypass full __init__/validation.
|
||||
adapter = WebSocketAdapter.model_construct(ap=ap, logger=logger)
|
||||
return adapter, provider
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_image_jpeg_mimetype_and_cleanup():
|
||||
adapter, provider = _make_adapter(load_return=b'\xff\xd8\xff')
|
||||
chain = [{'type': 'Image', 'path': 'storage://abc/photo.jpg'}]
|
||||
|
||||
await adapter._process_image_components(chain)
|
||||
|
||||
expected_b64 = base64.b64encode(b'\xff\xd8\xff').decode('utf-8')
|
||||
assert chain[0]['base64'] == f'data:image/jpeg;base64,{expected_b64}'
|
||||
assert chain[0]['path'] == '' # consumed
|
||||
provider.delete.assert_awaited_once_with('storage://abc/photo.jpg')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_image_defaults_to_png():
|
||||
adapter, _ = _make_adapter()
|
||||
chain = [{'type': 'Image', 'path': 'storage://abc/blob'}]
|
||||
await adapter._process_image_components(chain)
|
||||
assert chain[0]['base64'].startswith('data:image/png;base64,')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_voice_uses_guessed_or_wav_mimetype():
|
||||
adapter, _ = _make_adapter()
|
||||
chain = [{'type': 'Voice', 'path': 'storage://abc/clip.wav'}]
|
||||
await adapter._process_image_components(chain)
|
||||
assert chain[0]['base64'].startswith('data:audio/')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_file_uses_octet_stream_fallback():
|
||||
adapter, _ = _make_adapter()
|
||||
chain = [{'type': 'File', 'path': 'storage://abc/unknownblob'}]
|
||||
await adapter._process_image_components(chain)
|
||||
assert chain[0]['base64'].startswith('data:application/octet-stream;base64,')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_skips_components_without_path_or_unknown_type():
|
||||
adapter, provider = _make_adapter()
|
||||
chain = [
|
||||
{'type': 'Image', 'path': ''}, # no path
|
||||
{'type': 'Plain', 'path': 'storage://abc/x'}, # not a file component
|
||||
{'type': 'At', 'target': '123'}, # no path key at all
|
||||
]
|
||||
await adapter._process_image_components(chain)
|
||||
provider.load.assert_not_awaited()
|
||||
assert 'base64' not in chain[0]
|
||||
assert 'base64' not in chain[1]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_load_failure_is_logged_not_raised():
|
||||
adapter, _ = _make_adapter(load_side_effect=RuntimeError('storage down'))
|
||||
chain = [{'type': 'File', 'path': 'storage://abc/doc.pdf'}]
|
||||
|
||||
# must not raise
|
||||
await adapter._process_image_components(chain)
|
||||
assert 'base64' not in chain[0]
|
||||
adapter.logger.error.assert_awaited_once()
|
||||
@@ -0,0 +1,91 @@
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
|
||||
import langbot_plugin.api.definition.abstract.platform.event_logger as abstract_platform_logger
|
||||
import langbot_plugin.api.entities.builtin.platform.message as platform_message
|
||||
from langbot.pkg.platform.sources.wecomcs import WecomCSAdapter
|
||||
|
||||
|
||||
class DummyLogger(abstract_platform_logger.AbstractEventLogger):
|
||||
async def info(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
async def debug(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
async def warning(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
async def error(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
||||
def make_adapter():
|
||||
return WecomCSAdapter(
|
||||
config={
|
||||
'corpid': 'corp-id',
|
||||
'secret': 'secret',
|
||||
'token': 'token',
|
||||
'EncodingAESKey': 'encoding-key',
|
||||
},
|
||||
logger=DummyLogger(),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_message_sends_text_to_customer_service_user():
|
||||
adapter = make_adapter()
|
||||
adapter.bot_account_id = 'kf-test'
|
||||
adapter.bot = SimpleNamespace(send_text_msg=AsyncMock())
|
||||
|
||||
message = platform_message.MessageChain([platform_message.Plain(text='hello')])
|
||||
|
||||
await adapter.send_message('person', 'uexternal-user', message)
|
||||
|
||||
adapter.bot.send_text_msg.assert_awaited_once()
|
||||
kwargs = adapter.bot.send_text_msg.await_args.kwargs
|
||||
assert kwargs['open_kfid'] == 'kf-test'
|
||||
assert kwargs['external_userid'] == 'external-user'
|
||||
assert kwargs['content'] == 'hello'
|
||||
assert kwargs['msgid'].startswith('langbot_')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_message_allows_explicit_open_kfid_in_target_id():
|
||||
adapter = make_adapter()
|
||||
adapter.bot = SimpleNamespace(send_text_msg=AsyncMock())
|
||||
|
||||
message = platform_message.MessageChain([platform_message.Plain(text='hello')])
|
||||
|
||||
await adapter.send_message('person', 'kf-explicit|uexternal-user', message)
|
||||
|
||||
kwargs = adapter.bot.send_text_msg.await_args.kwargs
|
||||
assert kwargs['open_kfid'] == 'kf-explicit'
|
||||
assert kwargs['external_userid'] == 'external-user'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_message_requires_open_kfid():
|
||||
adapter = make_adapter()
|
||||
adapter.bot = SimpleNamespace(send_text_msg=AsyncMock())
|
||||
message = platform_message.MessageChain([platform_message.Plain(text='hello')])
|
||||
|
||||
with pytest.raises(ValueError, match='open_kfid is required'):
|
||||
await adapter.send_message('person', 'uexternal-user', message)
|
||||
|
||||
adapter.bot.send_text_msg.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_message_rejects_group_targets():
|
||||
adapter = make_adapter()
|
||||
adapter.bot_account_id = 'kf-test'
|
||||
adapter.bot = SimpleNamespace(send_text_msg=AsyncMock())
|
||||
message = platform_message.MessageChain([platform_message.Plain(text='hello')])
|
||||
|
||||
with pytest.raises(ValueError, match='only supports sending messages to person'):
|
||||
await adapter.send_message('group', 'group-id', message)
|
||||
|
||||
adapter.bot.send_text_msg.assert_not_called()
|
||||
@@ -6,12 +6,15 @@ Tests cover:
|
||||
- RAG methods (ingest, retrieve, schema)
|
||||
- Disabled plugin early returns
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from unittest.mock import Mock, AsyncMock
|
||||
from importlib import import_module
|
||||
|
||||
from tests.factories import text_query
|
||||
|
||||
|
||||
def get_connector_module():
|
||||
"""Lazy import to avoid circular import issues."""
|
||||
@@ -86,16 +89,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 +126,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()
|
||||
|
||||
@@ -137,6 +134,130 @@ class TestListPlugins:
|
||||
assert result[0]['debug'] is True
|
||||
|
||||
|
||||
class TestPluginDiagnostics:
|
||||
@pytest.mark.asyncio
|
||||
async def test_emit_event_preserves_response_sources(self):
|
||||
connector = create_mock_connector()
|
||||
query = text_query('hello')
|
||||
event = query.message_event
|
||||
object.__setattr__(event, 'query', query)
|
||||
connector_module = get_connector_module()
|
||||
original_from_event = connector_module.context.EventContext.from_event
|
||||
original_model_validate = connector_module.context.EventContext.model_validate
|
||||
response_sources = [
|
||||
{
|
||||
'kind': 'reply_message_chain',
|
||||
'plugin': {'author': 'tester', 'name': 'demo'},
|
||||
}
|
||||
]
|
||||
|
||||
async def emit_event_response(event_context, include_plugins=None):
|
||||
return {
|
||||
'event_context': event_context,
|
||||
'emitted_plugins': [],
|
||||
'response_sources': response_sources,
|
||||
}
|
||||
|
||||
connector.handler = AsyncMock()
|
||||
connector.handler.emit_event = AsyncMock(side_effect=emit_event_response)
|
||||
|
||||
fake_event_ctx = Mock()
|
||||
event_dump = event.model_dump()
|
||||
event_dump['event_name'] = 'FriendMessage'
|
||||
fake_event_ctx.model_dump.return_value = {
|
||||
'query_id': query.query_id,
|
||||
'eid': 0,
|
||||
'event_name': 'FriendMessage',
|
||||
'event': event_dump,
|
||||
'is_prevent_default': False,
|
||||
'is_prevent_postorder': False,
|
||||
}
|
||||
connector_module.context.EventContext.from_event = Mock(return_value=fake_event_ctx)
|
||||
parsed_event_ctx = Mock()
|
||||
connector_module.context.EventContext.model_validate = Mock(return_value=parsed_event_ctx)
|
||||
try:
|
||||
event_ctx = await connector.emit_event(event)
|
||||
finally:
|
||||
connector_module.context.EventContext.from_event = original_from_event
|
||||
connector_module.context.EventContext.model_validate = original_model_validate
|
||||
|
||||
assert event_ctx is parsed_event_ctx
|
||||
assert event_ctx._response_sources == response_sources
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_emit_event_leaves_response_sources_absent_for_old_runtime(self):
|
||||
connector = create_mock_connector()
|
||||
query = text_query('hello')
|
||||
event = query.message_event
|
||||
object.__setattr__(event, 'query', query)
|
||||
connector_module = get_connector_module()
|
||||
original_from_event = connector_module.context.EventContext.from_event
|
||||
original_model_validate = connector_module.context.EventContext.model_validate
|
||||
|
||||
async def emit_event_response(event_context, include_plugins=None):
|
||||
return {
|
||||
'event_context': event_context,
|
||||
'emitted_plugins': [
|
||||
{'manifest': {'metadata': {'author': 'tester', 'name': 'demo'}}},
|
||||
],
|
||||
}
|
||||
|
||||
connector.handler = AsyncMock()
|
||||
connector.handler.emit_event = AsyncMock(side_effect=emit_event_response)
|
||||
|
||||
fake_event_ctx = Mock()
|
||||
event_dump = event.model_dump()
|
||||
event_dump['event_name'] = 'FriendMessage'
|
||||
fake_event_ctx.model_dump.return_value = {
|
||||
'query_id': query.query_id,
|
||||
'eid': 0,
|
||||
'event_name': 'FriendMessage',
|
||||
'event': event_dump,
|
||||
'is_prevent_default': False,
|
||||
'is_prevent_postorder': False,
|
||||
}
|
||||
connector_module.context.EventContext.from_event = Mock(return_value=fake_event_ctx)
|
||||
parsed_event_ctx = Mock()
|
||||
connector_module.context.EventContext.model_validate = Mock(return_value=parsed_event_ctx)
|
||||
try:
|
||||
event_ctx = await connector.emit_event(event)
|
||||
finally:
|
||||
connector_module.context.EventContext.from_event = original_from_event
|
||||
connector_module.context.EventContext.model_validate = original_model_validate
|
||||
|
||||
assert '_response_sources' not in vars(event_ctx)
|
||||
assert event_ctx._emitted_plugins == [
|
||||
{'manifest': {'metadata': {'author': 'tester', 'name': 'demo'}}},
|
||||
]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_notify_plugin_diagnostic_skips_when_disabled(self):
|
||||
connector_module = get_connector_module()
|
||||
|
||||
async def mock_disconnect(conn):
|
||||
pass
|
||||
|
||||
mock_app = create_mock_app()
|
||||
mock_app.instance_config.data = {'plugin': {'enable': False}}
|
||||
connector = connector_module.PluginRuntimeConnector(mock_app, mock_disconnect)
|
||||
connector.handler = AsyncMock()
|
||||
|
||||
await connector.notify_plugin_diagnostic({'code': 'response_delivery_failed'})
|
||||
|
||||
connector.handler.notify_plugin_diagnostic.assert_not_called()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_notify_plugin_diagnostic_is_best_effort(self):
|
||||
connector = create_mock_connector()
|
||||
connector.handler = AsyncMock()
|
||||
connector.handler.notify_plugin_diagnostic = AsyncMock(side_effect=RuntimeError('action not found'))
|
||||
|
||||
await connector.notify_plugin_diagnostic({'code': 'response_delivery_failed'})
|
||||
|
||||
connector.handler.notify_plugin_diagnostic.assert_awaited_once()
|
||||
connector.ap.logger.debug.assert_called_once()
|
||||
|
||||
|
||||
class TestListKnowledgeEngines:
|
||||
"""Tests for list_knowledge_engines method."""
|
||||
|
||||
@@ -230,7 +351,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 +373,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 +384,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 +412,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 +446,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 +472,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 +562,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 +584,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
|
||||
@@ -156,6 +159,36 @@ class TestHandlerRagErrorResponse:
|
||||
assert 'KeyError' in response.message
|
||||
|
||||
|
||||
class TestHandlerPluginDiagnostic:
|
||||
@pytest.mark.asyncio
|
||||
async def test_notify_plugin_diagnostic_falls_back_to_raw_protocol_action(self):
|
||||
"""Diagnostic forwarding works before the SDK enum exists."""
|
||||
app = SimpleNamespace()
|
||||
app.logger = SimpleNamespace(debug=MagicMock())
|
||||
runtime_handler = make_handler(app)
|
||||
runtime_handler.call_action = AsyncMock(return_value={})
|
||||
|
||||
payload = {'code': 'response_delivery_failed'}
|
||||
await runtime_handler.notify_plugin_diagnostic(payload)
|
||||
|
||||
action = runtime_handler.call_action.await_args.args[0]
|
||||
assert action.value == 'plugin_diagnostic'
|
||||
assert runtime_handler.call_action.await_args.args[1] is payload
|
||||
assert runtime_handler.call_action.await_args.kwargs['timeout'] == 5
|
||||
|
||||
def test_langbot_to_runtime_action_uses_enum_when_available(self):
|
||||
"""The compatibility helper should prefer SDK enums once available."""
|
||||
from langbot.pkg.plugin import handler as plugin_handler
|
||||
|
||||
sentinel = object()
|
||||
original = plugin_handler.LangBotToRuntimeAction
|
||||
plugin_handler.LangBotToRuntimeAction = SimpleNamespace(PLUGIN_DIAGNOSTIC=sentinel)
|
||||
try:
|
||||
assert plugin_handler._langbot_to_runtime_action('PLUGIN_DIAGNOSTIC', 'plugin_diagnostic') is sentinel
|
||||
finally:
|
||||
plugin_handler.LangBotToRuntimeAction = original
|
||||
|
||||
|
||||
class TestConstantsSemanticVersion:
|
||||
"""Tests for version constant access."""
|
||||
|
||||
|
||||
@@ -27,6 +27,68 @@ def compiled_params(statement):
|
||||
return statement.compile().params
|
||||
|
||||
|
||||
class TestRagRerankAction:
|
||||
"""Tests for RAG rerank action handler."""
|
||||
|
||||
@pytest.fixture
|
||||
def app(self):
|
||||
mock_app = Mock()
|
||||
mock_app.model_mgr = Mock()
|
||||
mock_app.logger = Mock()
|
||||
return mock_app
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_invokes_rerank_model_and_sorts_scores(self, app):
|
||||
"""Rerank action uses the selected model and returns top scores."""
|
||||
provider = Mock()
|
||||
provider.invoke_rerank = AsyncMock(
|
||||
return_value=[
|
||||
{'index': 0, 'relevance_score': 0.2},
|
||||
{'index': 1, 'relevance_score': 0.9},
|
||||
]
|
||||
)
|
||||
rerank_model = SimpleNamespace(provider=provider)
|
||||
app.model_mgr.get_rerank_model_by_uuid = AsyncMock(return_value=rerank_model)
|
||||
runtime_handler = make_handler(app)
|
||||
|
||||
response = await runtime_handler.actions[PluginToRuntimeAction.INVOKE_RERANK.value](
|
||||
{
|
||||
'rerank_model_uuid': 'rerank-1',
|
||||
'query': 'hello',
|
||||
'documents': ['a', 'b'],
|
||||
'top_k': 1,
|
||||
'extra_args': {'return_documents': False},
|
||||
}
|
||||
)
|
||||
|
||||
assert response.code == 0
|
||||
assert response.data['results'] == [{'index': 1, 'relevance_score': 0.9}]
|
||||
app.model_mgr.get_rerank_model_by_uuid.assert_awaited_once_with('rerank-1')
|
||||
provider.invoke_rerank.assert_awaited_once_with(
|
||||
model=rerank_model,
|
||||
query='hello',
|
||||
documents=['a', 'b'],
|
||||
extra_args={'return_documents': False},
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_error_when_rerank_model_missing(self, app):
|
||||
"""Missing rerank model returns an action error."""
|
||||
app.model_mgr.get_rerank_model_by_uuid = AsyncMock(side_effect=ValueError('not found'))
|
||||
runtime_handler = make_handler(app)
|
||||
|
||||
response = await runtime_handler.actions[PluginToRuntimeAction.INVOKE_RERANK.value](
|
||||
{
|
||||
'rerank_model_uuid': 'missing',
|
||||
'query': 'hello',
|
||||
'documents': ['a'],
|
||||
}
|
||||
)
|
||||
|
||||
assert response.code != 0
|
||||
assert 'Rerank model with rerank_model_uuid missing not found' in response.message
|
||||
|
||||
|
||||
class TestInitializePluginSettings:
|
||||
"""Tests for initialize_plugin_settings action handler."""
|
||||
|
||||
@@ -47,12 +109,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 +146,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 +227,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 +267,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 +290,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 +319,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 +352,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 +371,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 +399,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 +415,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
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import pytest
|
||||
|
||||
from src.langbot.pkg.plugin.connector import PluginRuntimeConnector
|
||||
from langbot.pkg.plugin.connector import PluginRuntimeConnector
|
||||
|
||||
|
||||
def test_parse_plugin_id_accepts_author_name():
|
||||
|
||||
@@ -1 +1 @@
|
||||
|
||||
"""Provider requester tests"""
|
||||
|
||||
@@ -88,7 +88,10 @@ class AnotherFakeRequester(requester.ProviderAPIRequester):
|
||||
|
||||
async def invoke_llm(self, query, model, messages, funcs=None, extra_args={}, remove_think=False):
|
||||
import langbot_plugin.api.entities.builtin.provider.message as provider_message
|
||||
return provider_message.Message(role='assistant', content=[provider_message.ContentElement(type='text', text='Another response')])
|
||||
|
||||
return provider_message.Message(
|
||||
role='assistant', content=[provider_message.ContentElement(type='text', text='Another response')]
|
||||
)
|
||||
|
||||
async def invoke_rerank(self, model, query: str, documents: list, extra_args={}):
|
||||
"""Return fake rerank results."""
|
||||
@@ -135,8 +138,10 @@ def mock_app_for_modelmgr():
|
||||
|
||||
# Fake persistence manager - returns empty results by default
|
||||
app.persistence_mgr = SimpleNamespace()
|
||||
|
||||
async def default_execute(query):
|
||||
return _make_mock_result([])
|
||||
|
||||
app.persistence_mgr.execute_async = AsyncMock(side_effect=default_execute)
|
||||
|
||||
# Fake discover engine
|
||||
@@ -165,9 +170,7 @@ def fake_requester_registry(mock_app_for_modelmgr):
|
||||
fake_component = _create_fake_component('fake-requester', FakeProviderAPIRequester)
|
||||
another_component = _create_fake_component('another-fake-requester', AnotherFakeRequester)
|
||||
|
||||
app.discover.get_components_by_kind = Mock(
|
||||
return_value=[fake_component, another_component]
|
||||
)
|
||||
app.discover.get_components_by_kind = Mock(return_value=[fake_component, another_component])
|
||||
|
||||
model_mgr = ModelManager(app)
|
||||
return model_mgr
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
"""Tests for AnthropicMessages requester.
|
||||
|
||||
Tests config and pure utility methods.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
class TestAnthropicMessagesConfig:
|
||||
"""Tests for default config."""
|
||||
|
||||
def test_default_config_values(self):
|
||||
"""Check default_config."""
|
||||
from langbot.pkg.provider.modelmgr.requesters.anthropicmsgs import AnthropicMessages
|
||||
|
||||
assert AnthropicMessages.default_config['base_url'] == 'https://api.anthropic.com'
|
||||
assert AnthropicMessages.default_config['timeout'] == 120
|
||||
|
||||
def test_config_override(self):
|
||||
"""Config can override defaults."""
|
||||
from langbot.pkg.provider.modelmgr.requesters.anthropicmsgs import AnthropicMessages
|
||||
|
||||
mock_app = MagicMock()
|
||||
req = AnthropicMessages(mock_app, {
|
||||
'base_url': 'https://custom.anthropic.com',
|
||||
'timeout': 60,
|
||||
})
|
||||
|
||||
assert req.requester_cfg['base_url'] == 'https://custom.anthropic.com'
|
||||
assert req.requester_cfg['timeout'] == 60
|
||||
@@ -1,247 +0,0 @@
|
||||
"""Tests for requester error handling - direct import version.
|
||||
|
||||
Tests error handling branches by importing real packages and mocking
|
||||
only the necessary dependencies.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
import pytest
|
||||
import openai # Import real openai package
|
||||
|
||||
from langbot.pkg.provider.modelmgr.errors import RequesterError
|
||||
|
||||
|
||||
class TestInvokeLLMErrorHandling:
|
||||
"""Tests for invoke_llm error handling branches."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_app(self):
|
||||
"""Create mock Application."""
|
||||
app = MagicMock()
|
||||
app.tool_mgr = MagicMock()
|
||||
app.tool_mgr.generate_tools_for_openai = AsyncMock(return_value=[])
|
||||
return app
|
||||
|
||||
@pytest.fixture
|
||||
def mock_model(self):
|
||||
"""Create mock RuntimeLLMModel."""
|
||||
model = MagicMock()
|
||||
model.model_entity = MagicMock()
|
||||
model.model_entity.name = 'gpt-4'
|
||||
model.provider = MagicMock()
|
||||
model.provider.token_mgr = MagicMock()
|
||||
model.provider.token_mgr.get_token = MagicMock(return_value='test-key')
|
||||
return model
|
||||
|
||||
@pytest.fixture
|
||||
def mock_message(self):
|
||||
"""Create mock provider message."""
|
||||
msg = MagicMock()
|
||||
msg.dict = MagicMock(return_value={'role': 'user', 'content': 'test'})
|
||||
return msg
|
||||
|
||||
@pytest.fixture
|
||||
def requester_with_mocked_client(self, mock_app):
|
||||
"""Create requester with mocked OpenAI client."""
|
||||
from langbot.pkg.provider.modelmgr.requesters.chatcmpl import OpenAIChatCompletions
|
||||
|
||||
req = OpenAIChatCompletions(mock_app, {
|
||||
'base_url': 'https://api.openai.com/v1',
|
||||
'timeout': 120,
|
||||
})
|
||||
|
||||
# Replace client with mock
|
||||
req.client = MagicMock()
|
||||
req.client.chat = MagicMock()
|
||||
req.client.chat.completions = MagicMock()
|
||||
req.client.chat.completions.create = AsyncMock()
|
||||
|
||||
return req
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_timeout_error(self, requester_with_mocked_client, mock_model, mock_message):
|
||||
"""TimeoutError is wrapped as RequesterError."""
|
||||
requester_with_mocked_client.client.chat.completions.create = AsyncMock(
|
||||
side_effect=asyncio.TimeoutError()
|
||||
)
|
||||
|
||||
with pytest.raises(RequesterError) as exc:
|
||||
await requester_with_mocked_client.invoke_llm(
|
||||
query=None,
|
||||
model=mock_model,
|
||||
messages=[mock_message],
|
||||
)
|
||||
|
||||
assert '超时' in str(exc.value)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bad_request_context_length(self, requester_with_mocked_client, mock_model, mock_message):
|
||||
"""BadRequestError with context_length_exceeded has special message."""
|
||||
error = openai.BadRequestError(
|
||||
message='context_length_exceeded: max 4096',
|
||||
response=MagicMock(status_code=400),
|
||||
body={}
|
||||
)
|
||||
requester_with_mocked_client.client.chat.completions.create = AsyncMock(
|
||||
side_effect=error
|
||||
)
|
||||
|
||||
with pytest.raises(RequesterError) as exc:
|
||||
await requester_with_mocked_client.invoke_llm(
|
||||
query=None,
|
||||
model=mock_model,
|
||||
messages=[mock_message],
|
||||
)
|
||||
|
||||
assert '上文过长' in str(exc.value)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_authentication_error(self, requester_with_mocked_client, mock_model, mock_message):
|
||||
"""AuthenticationError shows invalid api-key message."""
|
||||
error = openai.AuthenticationError(
|
||||
message='Invalid API key',
|
||||
response=MagicMock(status_code=401),
|
||||
body={}
|
||||
)
|
||||
requester_with_mocked_client.client.chat.completions.create = AsyncMock(
|
||||
side_effect=error
|
||||
)
|
||||
|
||||
with pytest.raises(RequesterError) as exc:
|
||||
await requester_with_mocked_client.invoke_llm(
|
||||
query=None,
|
||||
model=mock_model,
|
||||
messages=[mock_message],
|
||||
)
|
||||
|
||||
assert 'api-key' in str(exc.value).lower() or '无效' in str(exc.value)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rate_limit_error(self, requester_with_mocked_client, mock_model, mock_message):
|
||||
"""RateLimitError shows rate limit message."""
|
||||
error = openai.RateLimitError(
|
||||
message='Rate limit exceeded',
|
||||
response=MagicMock(status_code=429),
|
||||
body={}
|
||||
)
|
||||
requester_with_mocked_client.client.chat.completions.create = AsyncMock(
|
||||
side_effect=error
|
||||
)
|
||||
|
||||
with pytest.raises(RequesterError) as exc:
|
||||
await requester_with_mocked_client.invoke_llm(
|
||||
query=None,
|
||||
model=mock_model,
|
||||
messages=[mock_message],
|
||||
)
|
||||
|
||||
assert '频繁' in str(exc.value) or '余额' in str(exc.value)
|
||||
|
||||
|
||||
class TestInvokeEmbeddingErrorHandling:
|
||||
"""Tests for invoke_embedding error handling."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_app(self):
|
||||
return MagicMock()
|
||||
|
||||
@pytest.fixture
|
||||
def mock_embedding_model(self):
|
||||
model = MagicMock()
|
||||
model.model_entity = MagicMock()
|
||||
model.model_entity.name = 'text-embedding-ada-002'
|
||||
model.model_entity.extra_args = {}
|
||||
model.provider = MagicMock()
|
||||
model.provider.token_mgr = MagicMock()
|
||||
model.provider.token_mgr.get_token = MagicMock(return_value='test-key')
|
||||
return model
|
||||
|
||||
@pytest.fixture
|
||||
def requester_with_mocked_client(self, mock_app):
|
||||
from langbot.pkg.provider.modelmgr.requesters.chatcmpl import OpenAIChatCompletions
|
||||
|
||||
req = OpenAIChatCompletions(mock_app, {})
|
||||
req.client = MagicMock()
|
||||
req.client.embeddings = MagicMock()
|
||||
req.client.embeddings.create = AsyncMock()
|
||||
|
||||
return req
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_embedding_timeout_error(self, requester_with_mocked_client, mock_embedding_model):
|
||||
"""TimeoutError in embedding request."""
|
||||
requester_with_mocked_client.client.embeddings.create = AsyncMock(
|
||||
side_effect=asyncio.TimeoutError()
|
||||
)
|
||||
|
||||
with pytest.raises(RequesterError) as exc:
|
||||
await requester_with_mocked_client.invoke_embedding(
|
||||
model=mock_embedding_model,
|
||||
input_text=['test'],
|
||||
)
|
||||
|
||||
assert '超时' in str(exc.value)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_embedding_bad_request_error(self, requester_with_mocked_client, mock_embedding_model):
|
||||
"""BadRequestError in embedding request."""
|
||||
error = openai.BadRequestError(
|
||||
message='Invalid model',
|
||||
response=MagicMock(status_code=400),
|
||||
body={}
|
||||
)
|
||||
requester_with_mocked_client.client.embeddings.create = AsyncMock(
|
||||
side_effect=error
|
||||
)
|
||||
|
||||
with pytest.raises(RequesterError) as exc:
|
||||
await requester_with_mocked_client.invoke_embedding(
|
||||
model=mock_embedding_model,
|
||||
input_text=['test'],
|
||||
)
|
||||
|
||||
assert '参数' in str(exc.value)
|
||||
|
||||
|
||||
class TestRequesterErrorClass:
|
||||
"""Tests for RequesterError."""
|
||||
|
||||
def test_error_message_prefix(self):
|
||||
"""RequesterError has '模型请求失败' prefix."""
|
||||
from langbot.pkg.provider.modelmgr.errors import RequesterError
|
||||
|
||||
error = RequesterError('test error')
|
||||
assert '模型请求失败' in str(error)
|
||||
|
||||
def test_error_is_exception(self):
|
||||
"""RequesterError inherits Exception."""
|
||||
from langbot.pkg.provider.modelmgr.errors import RequesterError
|
||||
|
||||
error = RequesterError('test')
|
||||
assert isinstance(error, Exception)
|
||||
|
||||
|
||||
class TestDefaultConfig:
|
||||
"""Tests for requester default config."""
|
||||
|
||||
def test_default_config(self):
|
||||
"""Check default_config values."""
|
||||
from langbot.pkg.provider.modelmgr.requesters.chatcmpl import OpenAIChatCompletions
|
||||
|
||||
assert OpenAIChatCompletions.default_config['base_url'] == 'https://api.openai.com/v1'
|
||||
assert OpenAIChatCompletions.default_config['timeout'] == 120
|
||||
|
||||
def test_config_override(self):
|
||||
"""Config overrides defaults."""
|
||||
from langbot.pkg.provider.modelmgr.requesters.chatcmpl import OpenAIChatCompletions
|
||||
|
||||
req = OpenAIChatCompletions(MagicMock(), {
|
||||
'base_url': 'https://custom.com/v1',
|
||||
'timeout': 60,
|
||||
})
|
||||
|
||||
assert req.requester_cfg['base_url'] == 'https://custom.com/v1'
|
||||
assert req.requester_cfg['timeout'] == 60
|
||||
@@ -1,340 +0,0 @@
|
||||
"""Tests for requester pure utility functions.
|
||||
|
||||
Tests the helper methods in OpenAIChatCompletions that don't require network calls.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from tests.utils.import_isolation import isolated_sys_modules
|
||||
|
||||
|
||||
class TestMaskApiKey:
|
||||
"""Tests for _mask_api_key method."""
|
||||
|
||||
def _create_requester_with_mocks(self):
|
||||
"""Create requester instance with mocked dependencies."""
|
||||
mocks = {
|
||||
'langbot.pkg.core.app': MagicMock(),
|
||||
'langbot_plugin.api.entities.builtin.resource.tool': MagicMock(),
|
||||
'langbot_plugin.api.entities.builtin.pipeline.query': MagicMock(),
|
||||
'langbot_plugin.api.entities.builtin.provider.message': MagicMock(),
|
||||
'langbot.pkg.provider.modelmgr.errors': MagicMock(),
|
||||
}
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.provider.modelmgr.requesters.chatcmpl import OpenAIChatCompletions
|
||||
|
||||
mock_app = MagicMock()
|
||||
requester = OpenAIChatCompletions(mock_app, {})
|
||||
return requester
|
||||
|
||||
def test_mask_api_key_full(self):
|
||||
"""Mask a full API key."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
result = requester._mask_api_key('sk-1234567890abcdef')
|
||||
assert result == 'sk-1...cdef'
|
||||
|
||||
def test_mask_api_key_short(self):
|
||||
"""Mask a short API key (<=8 chars)."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
result = requester._mask_api_key('short')
|
||||
assert result == '****'
|
||||
|
||||
def test_mask_api_key_empty(self):
|
||||
"""Empty API key returns empty string."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
result = requester._mask_api_key('')
|
||||
assert result == ''
|
||||
|
||||
def test_mask_api_key_none(self):
|
||||
"""None API key returns empty string."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
result = requester._mask_api_key(None)
|
||||
assert result == ''
|
||||
|
||||
def test_mask_api_key_exact_8_chars(self):
|
||||
"""API key with exactly 8 chars is masked as **** (<=8 threshold)."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
result = requester._mask_api_key('12345678')
|
||||
assert result == '****' # <= 8 chars gets masked
|
||||
|
||||
|
||||
class TestInferModelType:
|
||||
"""Tests for _infer_model_type method."""
|
||||
|
||||
def _create_requester_with_mocks(self):
|
||||
mocks = {
|
||||
'langbot.pkg.core.app': MagicMock(),
|
||||
'langbot_plugin.api.entities.builtin.resource.tool': MagicMock(),
|
||||
'langbot_plugin.api.entities.builtin.pipeline.query': MagicMock(),
|
||||
'langbot_plugin.api.entities.builtin.provider.message': MagicMock(),
|
||||
'langbot.pkg.provider.modelmgr.errors': MagicMock(),
|
||||
}
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.provider.modelmgr.requesters.chatcmpl import OpenAIChatCompletions
|
||||
|
||||
mock_app = MagicMock()
|
||||
requester = OpenAIChatCompletions(mock_app, {})
|
||||
return requester
|
||||
|
||||
def test_infer_embedding_from_name(self):
|
||||
"""Infer embedding type from model name."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
assert requester._infer_model_type('text-embedding-ada-002') == 'embedding'
|
||||
assert requester._infer_model_type('bge-large-en') == 'embedding'
|
||||
assert requester._infer_model_type('e5-base') == 'embedding'
|
||||
assert requester._infer_model_type('m3e-base') == 'embedding'
|
||||
|
||||
def test_infer_llm_from_name(self):
|
||||
"""Infer LLM type from model name."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
assert requester._infer_model_type('gpt-4') == 'llm'
|
||||
assert requester._infer_model_type('claude-3-opus') == 'llm'
|
||||
assert requester._infer_model_type('llama-2-70b') == 'llm'
|
||||
|
||||
def test_infer_model_type_none_id(self):
|
||||
"""Handle None model_id."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
result = requester._infer_model_type(None)
|
||||
assert result == 'llm' # Default
|
||||
|
||||
def test_infer_model_type_empty_id(self):
|
||||
"""Handle empty model_id."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
result = requester._infer_model_type('')
|
||||
assert result == 'llm' # Default
|
||||
|
||||
|
||||
class TestNormalizeModalities:
|
||||
"""Tests for _normalize_modalities method."""
|
||||
|
||||
def _create_requester_with_mocks(self):
|
||||
mocks = {
|
||||
'langbot.pkg.core.app': MagicMock(),
|
||||
'langbot_plugin.api.entities.builtin.resource.tool': MagicMock(),
|
||||
'langbot_plugin.api.entities.builtin.pipeline.query': MagicMock(),
|
||||
'langbot_plugin.api.entities.builtin.provider.message': MagicMock(),
|
||||
'langbot.pkg.provider.modelmgr.errors': MagicMock(),
|
||||
}
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.provider.modelmgr.requesters.chatcmpl import OpenAIChatCompletions
|
||||
|
||||
mock_app = MagicMock()
|
||||
requester = OpenAIChatCompletions(mock_app, {})
|
||||
return requester
|
||||
|
||||
def test_normalize_string_modality(self):
|
||||
"""Normalize single string modality."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
result = requester._normalize_modalities('text,image')
|
||||
assert result == ['text', 'image']
|
||||
|
||||
def test_normalize_list_modalities(self):
|
||||
"""Normalize list of modalities."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
result = requester._normalize_modalities(['text', 'image', 'audio'])
|
||||
assert result == ['text', 'image', 'audio']
|
||||
|
||||
def test_normalize_dict_modalities(self):
|
||||
"""Normalize dict with nested modalities."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
result = requester._normalize_modalities({'input': ['text'], 'output': ['text', 'image']})
|
||||
assert result == ['text', 'image']
|
||||
|
||||
def test_normalize_none(self):
|
||||
"""Handle None input."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
result = requester._normalize_modalities(None)
|
||||
assert result == []
|
||||
|
||||
def test_normalize_arrow_separator(self):
|
||||
"""Handle arrow separator in modality string."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
result = requester._normalize_modalities('text->image')
|
||||
assert result == ['text', 'image']
|
||||
|
||||
|
||||
class TestParseRerankResponse:
|
||||
"""Tests for _parse_rerank_response static method."""
|
||||
|
||||
def test_parse_cohere_jina_format(self):
|
||||
"""Parse Cohere/Jina/SiliconFlow format."""
|
||||
from langbot.pkg.provider.modelmgr.requesters.chatcmpl import OpenAIChatCompletions
|
||||
|
||||
data = {
|
||||
'results': [
|
||||
{'index': 0, 'relevance_score': 0.95},
|
||||
{'index': 1, 'relevance_score': 0.80},
|
||||
]
|
||||
}
|
||||
|
||||
result = OpenAIChatCompletions._parse_rerank_response(data)
|
||||
assert result == [
|
||||
{'index': 0, 'relevance_score': 0.95},
|
||||
{'index': 1, 'relevance_score': 0.80},
|
||||
]
|
||||
|
||||
def test_parse_voyage_format(self):
|
||||
"""Parse Voyage AI format."""
|
||||
from langbot.pkg.provider.modelmgr.requesters.chatcmpl import OpenAIChatCompletions
|
||||
|
||||
data = {
|
||||
'data': [
|
||||
{'index': 0, 'relevance_score': 0.90},
|
||||
{'index': 2, 'relevance_score': 0.75},
|
||||
]
|
||||
}
|
||||
|
||||
result = OpenAIChatCompletions._parse_rerank_response(data)
|
||||
assert result == [
|
||||
{'index': 0, 'relevance_score': 0.90},
|
||||
{'index': 2, 'relevance_score': 0.75},
|
||||
]
|
||||
|
||||
def test_parse_dashscope_format(self):
|
||||
"""Parse DashScope format."""
|
||||
from langbot.pkg.provider.modelmgr.requesters.chatcmpl import OpenAIChatCompletions
|
||||
|
||||
data = {
|
||||
'output': {
|
||||
'results': [
|
||||
{'index': 0, 'relevance_score': 0.85},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
result = OpenAIChatCompletions._parse_rerank_response(data)
|
||||
assert result == [{'index': 0, 'relevance_score': 0.85}]
|
||||
|
||||
def test_parse_unknown_format(self):
|
||||
"""Handle unknown format returns empty list."""
|
||||
from langbot.pkg.provider.modelmgr.requesters.chatcmpl import OpenAIChatCompletions
|
||||
|
||||
data = {'unknown_key': 'value'}
|
||||
|
||||
result = OpenAIChatCompletions._parse_rerank_response(data)
|
||||
assert result == []
|
||||
|
||||
def test_parse_empty_results(self):
|
||||
"""Handle empty results."""
|
||||
from langbot.pkg.provider.modelmgr.requesters.chatcmpl import OpenAIChatCompletions
|
||||
|
||||
data = {'results': []}
|
||||
|
||||
result = OpenAIChatCompletions._parse_rerank_response(data)
|
||||
assert result == []
|
||||
|
||||
|
||||
class TestExtractScanMetadata:
|
||||
"""Tests for _extract_scan_metadata method."""
|
||||
|
||||
def _create_requester_with_mocks(self):
|
||||
mocks = {
|
||||
'langbot.pkg.core.app': MagicMock(),
|
||||
'langbot_plugin.api.entities.builtin.resource.tool': MagicMock(),
|
||||
'langbot_plugin.api.entities.builtin.pipeline.query': MagicMock(),
|
||||
'langbot_plugin.api.entities.builtin.provider.message': MagicMock(),
|
||||
'langbot.pkg.provider.modelmgr.errors': MagicMock(),
|
||||
}
|
||||
|
||||
with isolated_sys_modules(mocks):
|
||||
from langbot.pkg.provider.modelmgr.requesters.chatcmpl import OpenAIChatCompletions
|
||||
|
||||
mock_app = MagicMock()
|
||||
requester = OpenAIChatCompletions(mock_app, {})
|
||||
return requester
|
||||
|
||||
def test_extract_basic_metadata(self):
|
||||
"""Extract basic model metadata."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
item = {
|
||||
'id': 'gpt-4',
|
||||
'name': 'GPT-4 Turbo',
|
||||
'description': 'Most capable GPT-4 model',
|
||||
'context_length': 128000,
|
||||
'owned_by': 'openai',
|
||||
}
|
||||
|
||||
result = requester._extract_scan_metadata(item, 'gpt-4')
|
||||
|
||||
assert result['display_name'] == 'GPT-4 Turbo'
|
||||
assert result['description'] == 'Most capable GPT-4 model'
|
||||
assert result['context_length'] == 128000
|
||||
assert result['owned_by'] == 'openai'
|
||||
|
||||
def test_extract_metadata_missing_fields(self):
|
||||
"""Handle missing metadata fields."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
item = {'id': 'unknown-model'}
|
||||
|
||||
result = requester._extract_scan_metadata(item, 'unknown-model')
|
||||
|
||||
assert result['display_name'] is None
|
||||
assert result['description'] is None
|
||||
assert result['context_length'] is None
|
||||
assert result['owned_by'] is None
|
||||
|
||||
def test_extract_metadata_top_provider_context(self):
|
||||
"""Extract context_length from top_provider."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
item = {
|
||||
'id': 'model',
|
||||
'top_provider': {
|
||||
'context_length': 4096,
|
||||
},
|
||||
}
|
||||
|
||||
result = requester._extract_scan_metadata(item, 'model')
|
||||
|
||||
assert result['context_length'] == 4096
|
||||
|
||||
def test_extract_metadata_empty_strings(self):
|
||||
"""Handle empty string values."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
item = {
|
||||
'id': 'model',
|
||||
'name': '', # Empty name
|
||||
'description': ' ', # Whitespace only
|
||||
'owned_by': '',
|
||||
}
|
||||
|
||||
result = requester._extract_scan_metadata(item, 'model')
|
||||
|
||||
assert result['display_name'] is None
|
||||
assert result['description'] is None
|
||||
assert result['owned_by'] is None
|
||||
|
||||
def test_extract_metadata_name_matches_id(self):
|
||||
"""When name equals id, display_name is None."""
|
||||
requester = self._create_requester_with_mocks()
|
||||
|
||||
item = {
|
||||
'id': 'gpt-4',
|
||||
'name': 'gpt-4', # Same as id
|
||||
}
|
||||
|
||||
result = requester._extract_scan_metadata(item, 'gpt-4')
|
||||
|
||||
assert result['display_name'] is None
|
||||
@@ -1,264 +0,0 @@
|
||||
"""Tests for OllamaChatCompletions requester.
|
||||
|
||||
Tests model inference, payload construction, and error handling.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
import pytest
|
||||
|
||||
from langbot.pkg.provider.modelmgr.errors import RequesterError
|
||||
|
||||
|
||||
class TestOllamaRequesterConfig:
|
||||
"""Tests for default config."""
|
||||
|
||||
def test_default_config_values(self):
|
||||
"""Check default_config."""
|
||||
from langbot.pkg.provider.modelmgr.requesters.ollamachat import OllamaChatCompletions
|
||||
|
||||
assert OllamaChatCompletions.default_config['base_url'] == 'http://127.0.0.1:11434'
|
||||
assert OllamaChatCompletions.default_config['timeout'] == 120
|
||||
|
||||
def test_config_override(self):
|
||||
"""Config can override defaults."""
|
||||
from langbot.pkg.provider.modelmgr.requesters.ollamachat import OllamaChatCompletions
|
||||
|
||||
mock_app = MagicMock()
|
||||
req = OllamaChatCompletions(mock_app, {
|
||||
'base_url': 'http://custom.ollama:11434',
|
||||
'timeout': 300,
|
||||
})
|
||||
|
||||
assert req.requester_cfg['base_url'] == 'http://custom.ollama:11434'
|
||||
assert req.requester_cfg['timeout'] == 300
|
||||
|
||||
|
||||
class TestOllamaInferModelType:
|
||||
"""Tests for _infer_model_type pure function."""
|
||||
|
||||
@pytest.fixture
|
||||
def requester(self):
|
||||
from langbot.pkg.provider.modelmgr.requesters.ollamachat import OllamaChatCompletions
|
||||
|
||||
return OllamaChatCompletions(MagicMock(), {})
|
||||
|
||||
def test_infer_embedding_from_name(self, requester):
|
||||
"""Embedding keywords return 'embedding'."""
|
||||
assert requester._infer_model_type('nomic-embed-text') == 'embedding'
|
||||
assert requester._infer_model_type('bge-large') == 'embedding'
|
||||
assert requester._infer_model_type('text-embedding') == 'embedding'
|
||||
|
||||
def test_infer_llm_from_name(self, requester):
|
||||
"""Non-embedding keywords return 'llm'."""
|
||||
assert requester._infer_model_type('llama2') == 'llm'
|
||||
assert requester._infer_model_type('mistral') == 'llm'
|
||||
assert requester._infer_model_type('codellama') == 'llm'
|
||||
|
||||
def test_infer_model_type_none(self, requester):
|
||||
"""None model_id returns 'llm'."""
|
||||
assert requester._infer_model_type(None) == 'llm'
|
||||
|
||||
def test_infer_model_type_empty(self, requester):
|
||||
"""Empty model_id returns 'llm'."""
|
||||
assert requester._infer_model_type('') == 'llm'
|
||||
|
||||
|
||||
class TestOllamaInferModelAbilities:
|
||||
"""Tests for _infer_model_abilities pure function."""
|
||||
|
||||
@pytest.fixture
|
||||
def requester(self):
|
||||
from langbot.pkg.provider.modelmgr.requesters.ollamachat import OllamaChatCompletions
|
||||
|
||||
return OllamaChatCompletions(MagicMock(), {})
|
||||
|
||||
def test_infer_vision_ability(self, requester):
|
||||
"""Vision keywords add 'vision' ability."""
|
||||
item = {
|
||||
'details': {
|
||||
'family': 'llava',
|
||||
}
|
||||
}
|
||||
|
||||
abilities = requester._infer_model_abilities(item, 'llava-v1.5')
|
||||
assert 'vision' in abilities
|
||||
|
||||
def test_infer_vision_from_model_id(self, requester):
|
||||
"""Vision keywords in model_id add 'vision' ability."""
|
||||
item = {}
|
||||
abilities = requester._infer_model_abilities(item, 'llava-7b')
|
||||
assert 'vision' in abilities
|
||||
|
||||
def test_infer_func_call_ability(self, requester):
|
||||
"""Tool/function keywords add 'func_call' ability."""
|
||||
item = {
|
||||
'details': {
|
||||
'families': ['tools'],
|
||||
}
|
||||
}
|
||||
|
||||
abilities = requester._infer_model_abilities(item, 'model')
|
||||
assert 'func_call' in abilities
|
||||
|
||||
def test_infer_no_abilities(self, requester):
|
||||
"""No matching keywords returns empty abilities."""
|
||||
item = {
|
||||
'details': {
|
||||
'family': 'llama',
|
||||
}
|
||||
}
|
||||
|
||||
abilities = requester._infer_model_abilities(item, 'llama-2')
|
||||
assert len(abilities) == 0
|
||||
|
||||
def test_infer_multiple_abilities(self, requester):
|
||||
"""Multiple keywords can add multiple abilities."""
|
||||
item = {
|
||||
'details': {
|
||||
'family': 'vision',
|
||||
'families': ['tools'],
|
||||
}
|
||||
}
|
||||
|
||||
abilities = requester._infer_model_abilities(item, 'vision-tool-model')
|
||||
assert 'vision' in abilities
|
||||
assert 'func_call' in abilities
|
||||
|
||||
|
||||
class TestOllamaMakeMessage:
|
||||
"""Tests for _make_msg response parsing."""
|
||||
|
||||
@pytest.fixture
|
||||
def requester(self):
|
||||
from langbot.pkg.provider.modelmgr.requesters.ollamachat import OllamaChatCompletions
|
||||
|
||||
return OllamaChatCompletions(MagicMock(), {})
|
||||
|
||||
def _create_ollama_response(self, content, tool_calls=None):
|
||||
"""Helper to create mock ollama response."""
|
||||
import ollama
|
||||
|
||||
mock_response = MagicMock(spec=ollama.ChatResponse)
|
||||
mock_message = MagicMock(spec=ollama.Message)
|
||||
mock_message.content = content
|
||||
mock_message.tool_calls = tool_calls
|
||||
mock_response.message = mock_message
|
||||
|
||||
return mock_response
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_make_msg_text_content(self, requester):
|
||||
"""Text content is extracted."""
|
||||
mock_response = self._create_ollama_response('Hello world')
|
||||
|
||||
result = await requester._make_msg(mock_response)
|
||||
|
||||
assert result.content == 'Hello world'
|
||||
assert result.role == 'assistant'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_make_msg_with_tool_calls(self, requester):
|
||||
"""Tool calls are parsed."""
|
||||
mock_tool_call = MagicMock()
|
||||
mock_tool_call.function = MagicMock()
|
||||
mock_tool_call.function.name = 'get_weather'
|
||||
mock_tool_call.function.arguments = {'location': 'Beijing'}
|
||||
|
||||
mock_response = self._create_ollama_response('', tool_calls=[mock_tool_call])
|
||||
|
||||
result = await requester._make_msg(mock_response)
|
||||
|
||||
assert result.tool_calls is not None
|
||||
assert len(result.tool_calls) == 1
|
||||
assert result.tool_calls[0].function.name == 'get_weather'
|
||||
# Arguments should be JSON string
|
||||
assert isinstance(result.tool_calls[0].function.arguments, str)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_make_msg_empty_message_raises(self, requester):
|
||||
"""Empty message raises ValueError."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.message = None
|
||||
|
||||
with pytest.raises(ValueError, match='message'):
|
||||
await requester._make_msg(mock_response)
|
||||
|
||||
|
||||
class TestOllamaErrorHandling:
|
||||
"""Tests for error handling branches."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_app(self):
|
||||
app = MagicMock()
|
||||
app.tool_mgr = MagicMock()
|
||||
app.tool_mgr.generate_tools_for_openai = AsyncMock(return_value=[])
|
||||
return app
|
||||
|
||||
@pytest.fixture
|
||||
def requester_with_mocked_client(self, mock_app):
|
||||
from langbot.pkg.provider.modelmgr.requesters.ollamachat import OllamaChatCompletions
|
||||
|
||||
req = OllamaChatCompletions(mock_app, {})
|
||||
req.client = MagicMock()
|
||||
req.client.chat = AsyncMock()
|
||||
|
||||
return req
|
||||
|
||||
@pytest.fixture
|
||||
def mock_model(self):
|
||||
model = MagicMock()
|
||||
model.model_entity = MagicMock()
|
||||
model.model_entity.name = 'llama2'
|
||||
model.provider = MagicMock()
|
||||
model.provider.token_mgr = MagicMock()
|
||||
model.provider.token_mgr.get_token = MagicMock(return_value='')
|
||||
return model
|
||||
|
||||
@pytest.fixture
|
||||
def mock_message(self):
|
||||
msg = MagicMock()
|
||||
msg.role = 'user'
|
||||
msg.content = 'test'
|
||||
msg.dict = MagicMock(return_value={'role': 'user', 'content': 'test'})
|
||||
return msg
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_timeout_error(self, requester_with_mocked_client, mock_model, mock_message):
|
||||
"""TimeoutError is converted to RequesterError."""
|
||||
requester_with_mocked_client.client.chat = AsyncMock(side_effect=asyncio.TimeoutError())
|
||||
|
||||
with pytest.raises(RequesterError) as exc:
|
||||
await requester_with_mocked_client.invoke_llm(
|
||||
query=None,
|
||||
model=mock_model,
|
||||
messages=[mock_message],
|
||||
)
|
||||
|
||||
assert '超时' in str(exc.value)
|
||||
|
||||
|
||||
class TestOllamaScanModels:
|
||||
"""Tests for scan_models method."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_app(self):
|
||||
return MagicMock()
|
||||
|
||||
@pytest.fixture
|
||||
def requester(self, mock_app):
|
||||
from langbot.pkg.provider.modelmgr.requesters.ollamachat import OllamaChatCompletions
|
||||
|
||||
req = OllamaChatCompletions(mock_app, {
|
||||
'base_url': 'http://127.0.0.1:11434',
|
||||
'timeout': 120,
|
||||
})
|
||||
return req
|
||||
|
||||
def test_requester_name_constant(self):
|
||||
"""REQUESTER_NAME constant exists."""
|
||||
from langbot.pkg.provider.modelmgr.requesters.ollamachat import REQUESTER_NAME
|
||||
|
||||
assert REQUESTER_NAME == 'ollama-chat'
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user