Feat/test build (#2174)

* fix(ci): update unit-test workflow paths to match current source layout

Replace stale pkg/** filter with src/langbot/** and add uv.lock.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* docs(tests): update README to reflect current test layout

- Fix stale paths: tests/pipeline → tests/unit_tests/pipeline
- Update CI Python versions: 3.11, 3.12, 3.13
- Add test directory structure for box, config, platform, plugin, provider, storage
- Document pytest markers and uv commands
- Mention planned E2E tests

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* feat(test): add shared test factories package

Create tests/factories/ with reusable test factories:
- FakeApp: mock application with all dependencies
- Message chains: text_chain, mention_chain, image_chain
- Query factories: text_query, group_text_query, command_query, etc.

No test changes - maintains backward compatibility.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* feat(test): add fake provider factory

Add tests/factories/provider.py with:
- FakeProvider: deterministic fake LLM provider
- Error simulation: timeout, auth, rate-limit, malformed
- Request capture for assertions
- fake_model: mock model with attached provider

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* feat(test): add fake platform factory

Add tests/factories/platform.py with:
- FakePlatform: simulated platform adapter
- Inbound message construction: friend/group/image
- Mention-bot flag simulation
- Outbound message capture for assertions
- Streaming output support simulation
- Send failure simulation

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* feat(test): add comprehensive message/query factories

Extend tests/factories/message.py with:
- file_query: file attachment query
- unsupported_query: unknown message segment
- voice_query: audio/voice query
- at_all_query: group @All mention
- query_with_session: query with session object
- query_with_config: query with custom pipeline config

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* feat(test): add fake message flow smoke test

Create tests/smoke/test_fake_message_flow.py:
- TestFakeMessageFlow: factory verification tests
- TestMessageFlowIntegration: minimal flow smoke test
- Tests FakeApp, FakeProvider, FakePlatform, query factories
- Verifies LANGBOT_FAKE_PONG marker response
- Captures outbound messages for assertions

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* feat(test): add developer test-quick command

Add scripts/test-quick.sh and Makefile with:
- test-quick: runs ruff check + unit tests + smoke tests
- No real provider keys or platform accounts required
- Suitable for local branch self-test

Update tests/README.md:
- Document test-quick command
- Document test factories package
- Add smoke tests and factories directory structure

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(test): make test-quick reliable as developer gate

Fixes for D-001验收问题:
1. test-quick.sh: use set -euo pipefail, uv run ruff, no tail pipe
2. Remove unused imports in factories (app.py, platform.py, provider.py)
3. Fix unused variable in smoke test
4. Add noqa: E402 to test_n8nsvapi.py lazy imports
5. Update smoke test docs: "minimal fake flow" not full pipeline

Now test-quick is a reliable gate: lint failures exit 1, test failures propagate.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(unit): add preproc and taskmgr unit tests

U-001: Pipeline Preprocessor tests
- Normal text message processing
- Empty message handling
- Image segment with/without vision model
- Model selection and fallback
- Variable extraction

U-004: Core Task Manager tests (pattern-based)
- Task creation and tracking patterns
- Task cancellation patterns
- Scope-based cancellation
- Task type filtering
- Pruning completed tasks
- Wait all tasks

Taskmgr tests use pattern-based approach to avoid circular import
in source code (taskmgr → app → http_controller → migration → taskmgr).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(unit): add config loader unit tests

U-005: Config Loader tests
- Valid YAML config loading
- Valid JSON config loading
- Invalid YAML/JSON error behavior
- Missing config file creation from template
- Template completion for missing keys
- ConfigManager load/dump operations
- Exists check for both YAML and JSON

All tests use tmp_path fixture, no real project config.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(unit): add chat and command handler pattern tests

U-002: Chat Handler tests (pattern-based)
- Normal message event emission pattern
- prevent_default handling
- User message alteration pattern
- Runner selection pattern
- Streaming/non-streaming response patterns
- Exception handling modes (show-error, show-hint, hide)
- Message history update pattern
- Telemetry payload pattern

U-003: Command Handler tests (pattern-based)
- Command parsing and text extraction
- Event creation pattern
- Privilege/admin check pattern
- Command result handling (text, error, image)
- prevent_default handling
- String truncation helper

Uses pattern-based testing to avoid circular import issues in source code.
Direct imports of handler modules trigger circular import chain.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* style: fix unused imports after ruff auto-fix

Remove unused imports in test files:
- test_config_loader.py: remove unused os
- test_taskmgr.py: remove unused Mock
- test_preproc.py: remove unused unsupported_query, image_chain

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(unit): improve taskmgr tests to test real classes

U-004 improved: Tests now import and test actual classes:
- TaskContext: new(), trace(), to_dict(), placeholder()
- TaskWrapper: task creation, context, exception/result capture, cancel, to_dict
- AsyncTaskManager: create_task, create_user_task, cancel_task, cancel_by_scope
- Task pruning behavior

Uses pre-mocking technique:
- Mock langbot.pkg.core.app before import (breaks circular chain)
- Mock langbot.pkg.core.entities with proper Enum

All 24 tests now test real class behavior, not patterns.
taskmgr.py coverage should improve significantly.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor(test): consolidate FakeApp and add sys.modules isolation utility

- Extract tests/utils/import_isolation.py with isolated_sys_modules context manager
- Extend tests/factories/app.py FakeApp with handler-specific attributes
- Refactor test_chat_handler.py to use centralized FakeApp and cached imports
- Refactor test_command_handler.py with mock_execute_factory fixture
- Refactor test_smoke.py to move import-time sys.modules manipulation into fixture
- Add SQLite migration integration tests (G-002)
- Add HTTP API smoke integration tests (G-005)
- Update CI workflow to call pytest for SQLite migrations (G-004)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* feat(test): add developer quality gate consolidation (G-007)

- Add scripts/test-integration-fast.sh for fast integration tests
- Add scripts/test-coverage.sh with 12% baseline threshold
- Update Makefile with test-integration-fast, test-coverage, test-all-local
- Update CI workflow with integration and coverage jobs
- Add smoke marker to pytest.ini
- Update tests/README.md with quality gate layers documentation
- Add tests/integration/pipeline/ for pipeline stage-chain tests

Quality gate layers:
- Quick: ruff + unit + smoke (~2 min)
- Fast Integration: SQLite/API/Pipeline (~3 min)
- Coverage: 12% threshold gate (~8 min)
- Full Local: all three combined

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* feat(test): add PostgreSQL migration slow integration tests (G-003)

- Add tests/integration/persistence/test_migrations_postgres.py
- All tests marked with @pytest.mark.slow
- Tests skip when TEST_POSTGRES_URL is not set (no local PostgreSQL)
- Database isolation via clean_tables and clean_alembic_version fixtures
- Update CI workflow to use pytest instead of inline Python script
- Remove TODO(G-003) comment
- Update tests/README.md with PostgreSQL test documentation

Covered scenarios:
- Baseline stamp sets revision
- Upgrade from baseline to head
- Upgrade idempotent
- Get current on unstamped DB returns None

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* feat(test): Phase 1.5 coverage expansion - COV-001 to COV-013

Coverage baseline raised from 13.65% to 26% (+12.35%)
Gate raised from 12% to 18%

Tasks completed:
- COV-001: Command system unit tests (100% coverage)
- COV-002: API service unit tests batch 1 (user/apikey/model/provider)
- COV-003: Provider model manager unit tests
- COV-004: Pipeline remaining stage tests (aggregator/cntfilter/longtext/msgtrun)
- COV-005: Storage and utils coverage pass
- COV-006: Gate ratchet 12%→15%
- COV-007: Gate ratchet 15%→18%
- COV-008: API service batch 2 (bot/pipeline/webhook/space/maintenance/mcp)
- COV-009: Blocked - API controller circular import issue documented
- COV-010: Plugin runtime unit tests (+0.08%)
- COV-011: RAG and vector unit tests (+0.68%)
- COV-012: Core boot and migration unit tests
- COV-013: Provider requester logic unit tests (+0.62%)

Key additions:
- tests/utils/import_isolation.py: sys.modules isolation for circular imports
- Provider requester mock tests: proved HTTP-dependent code can be tested locally
- Vector filter utilities: 100% coverage on pure functions
- API services: fake persistence pattern for unit testing

Blocked issue COV-009 documented in langbot-test-plan/1.5/issues/

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(phase1): add unit tests for telemetry, plugin, rag, persistence

Add initial unit tests for Phase 1 of test coverage improvement:
- telemetry: test initialization, payload sanitization, early returns (14.3% → 62.9%)
- plugin: test _parse_plugin_id static method
- rag: test _to_i18n_name static method
- persistence: test serialize_model with datetime handling

Overall core coverage: 41.9% → 42.2%

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(phase2): add unit tests for core, persistence, plugin, utils

- Add test_handler_helpers.py for plugin handler helpers (7 tests)
- Add test_mgr_methods.py for persistence manager (5 tests)
- Add test_app_config_validation.py for core app config (12 tests)
- Add test_knowledge_service.py for API knowledge service (22 tests)
- Add test_kbmgr.py for RAG knowledge base manager (39 tests)
- Add test_survey_manager.py for survey manager (22 tests)
- Add test_connector_methods.py for plugin connector (24 tests)
- Add test_funcschema.py for utils function schema (9 tests)
- Add test_platform.py for utils platform detection (7 tests)
- Add test_extract_deps.py for plugin deps extraction (7 tests)
- Add test_database_decorator.py for persistence decorator (7 tests)
- Add test_load_config.py for core config loading (19 tests)
- Add COVERAGE_EXCLUSIONS.md documenting external adapter exclusions
- Fix test_chat_session_limit.py path for portability

Coverage: core 28% → 30%, persistence 24% → 24.4%, plugin 27% → 28%
Total: 1082 tests passed, core module coverage 45.5%

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(integration): add API controller integration tests

- Add test_pipelines.py (10 tests) covering pipelines CRUD operations
  - GET/POST/PUT/DELETE on /api/v1/pipelines
  - Extensions endpoint
  - Metadata endpoint
  - Coverage: pipelines controller 27% → 80%

- Add test_providers.py (10 tests) covering provider/model management
  - Provider CRUD with model counts
  - LLM model CRUD
  - Coverage: providers controller 23% → 81%, models 29% → 45%

Tests use Quart TestClient with mocked services for real HTTP behavior
without external dependencies.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(integration): add knowledge, bots, and model endpoints tests

- Add test_knowledge.py (10 tests) covering knowledge base management
  - CRUD operations on /api/v1/knowledge/bases
  - Files management endpoints
  - Retrieve endpoint with validation
  - Coverage: knowledge/base.py 26% → 91%

- Add test_bots.py (9 tests) covering bot management
  - CRUD operations on /api/v1/platform/bots
  - Logs endpoint
  - Send message endpoint with validation
  - Coverage: platform/bots.py 24% → 87%

- Extend test_providers.py (+4 tests) for embedding/rerank models
  - Embedding models CRUD
  - Rerank models CRUD
  - Coverage: provider/models.py 29% → 60%

Total integration tests: 53 (smoke 12 + pipelines 10 + providers 14 + knowledge 10 + bots 9)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(integration): add embed and monitoring endpoint tests

Add integration tests for embed widget and monitoring API endpoints:
- test_embed.py: 15 tests for widget.js, logo, turnstile, messages, reset, feedback
- test_monitoring.py: 15 tests for overview, messages, llm-calls, sessions, errors, export

Coverage improvements:
- embed.py: 17% → 56%
- monitoring.py: 17% → 93%

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(e2e): add minimal startup E2E tests

Add E2E tests for LangBot startup flow:
- tests/e2e/utils/config_factory.py: minimal config generation
- tests/e2e/utils/process_manager.py: LangBot subprocess management
- tests/e2e/conftest.py: E2E fixtures (session-scoped process)
- tests/e2e/test_startup.py: 12 tests for startup verification

Tests verify:
- boot.py + stages execution
- database initialization (SQLite)
- API availability
- migrations applied

Uses embedded databases (SQLite, Chroma) - no external dependencies.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(quality): fix fake tests and add missing coverage

P0 fixes:
- telemetry: rewrite fake tests with real behavior verification (25 tests)
- config: delete copied-source tests, use proper imports (2 deleted)
- persistence: fix try-except pass to verify specific errors

P1 fixes:
- pipeline: add real FixedWindowAlgo tests instead of mocks (12 tests)
- provider: add SessionManager and ToolManager tests (25 tests)
- storage: add S3StorageProvider tests with moto mock (16 tests)
- plugin: add handler action tests for setting inheritance (15 tests)
- rag: add file storage and ZIP processing tests (21 tests)
- vector: add VDB filter conversion tests (30 tests)

P2 fixes:
- pipeline/msgtrun: strengthen assertions for exact message count
- api: add response structure validation in integration tests

New test files:
- provider/test_session_manager.py
- provider/test_tool_manager.py
- storage/test_s3storage.py
- plugin/test_handler_actions.py
- rag/test_file_storage.py
- vector/test_vdb_filter_conversion.py

Source code bugs documented:
- provider: TokenManager.next_token() ZeroDivisionError
- telemetry: send_tasks class variable shared state
- command: empty command IndexError, unused parameters
- utils: funcschema KeyError
- entity: vector.py independent declarative_base

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* docs(test): update coverage stats and test structure

- Update coverage from 22% to 30%
- Add new test files to structure:
  - provider: session_manager, tool_manager
  - storage: s3storage
  - plugin: handler_actions
  - rag: file_storage
  - vector: vdb_filter_conversion
  - telemetry: rewritten tests
- Update module coverage percentages

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test: add 105 new unit tests for untested core functionality

Add comprehensive tests for B-class issues (core functionality untested):

Pipeline:
- test_pool.py: QueryPool ID generation, caching, async context (12 tests)
- test_ratelimit.py: Fixed timing-sensitive test tolerance
- test_pipelinemgr.py: Use real Pydantic StageProcessResult instead of Mock

Utils:
- test_version.py: Version comparison functions (20 tests)
- test_logcache.py: Log page management and retrieval (18 tests)
- test_httpclient.py: HTTP session pool management (10 tests)
- test_proxy.py: Proxy configuration from env and config (10 tests)
- test_image.py: URL parsing and base64 extraction (12 tests)
- test_pkgmgr.py: Pip command generation (8 tests)

Discover:
- test_engine.py: I18nString, Metadata, Component manifest (15 tests)

Test count: 1193 → 1298 (+105 tests)

Note: Some B-class issues cannot be tested due to circular import bugs
filed as GitHub issues #2175 (pipeline) and #2176 (persistence).

* test: tighten phase 1 coverage contracts

* test: align ci integration isolation

---------

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
huanghuoguoguo
2026-05-16 12:05:54 +08:00
committed by GitHub
parent 4a4c0921a4
commit 17bbc8bf10
130 changed files with 32711 additions and 889 deletions

View File

@@ -1,15 +1,208 @@
from langbot.pkg.utils.funcschema import get_func_schema
"""Unit tests for utils funcschema.
Tests cover:
- get_func_schema() function
- Docstring parsing
- Parameter type extraction
- Required parameter detection
Note: Do NOT use 'from __future__ import annotations' because
funcschema.py expects actual type objects, not string annotations.
"""
import pytest
from importlib import import_module
def test_get_func_schema_uses_empty_description_for_undocumented_parameter():
def sample_function(documented: str, undocumented: int):
"""Sample function.
def get_funcschema_module():
"""Lazy import to avoid circular import issues."""
return import_module('langbot.pkg.utils.funcschema')
Args:
documented(str): documented parameter description
"""
schema = get_func_schema(sample_function)
class TestGetFuncSchema:
"""Tests for get_func_schema function."""
assert schema['parameters']['properties']['documented']['description'] == 'documented parameter description'
assert schema['parameters']['properties']['undocumented']['description'] == ''
def test_simple_function_schema(self):
"""Test schema generation for simple function."""
funcschema = get_funcschema_module()
def simple_func(name: str, count: int):
"""Simple function description.
Args:
name: The name parameter.
count: The count parameter.
"""
pass
result = funcschema.get_func_schema(simple_func)
assert result['description'] == 'Simple function description.'
assert result['parameters']['type'] == 'object'
assert 'name' in result['parameters']['properties']
assert 'count' in result['parameters']['properties']
assert result['parameters']['properties']['name']['type'] == 'string'
assert result['parameters']['properties']['count']['type'] == 'integer'
def test_parameter_type_mapping(self):
"""Test that Python types are mapped to JSON schema types."""
funcschema = get_funcschema_module()
def typed_func(a: str, b: int, c: float, d: bool, e: list, f: dict):
"""Typed function.
Args:
a: String param.
b: Int param.
c: Float param.
d: Bool param.
e: List param.
f: Dict param.
"""
pass
result = funcschema.get_func_schema(typed_func)
props = result['parameters']['properties']
assert props['a']['type'] == 'string'
assert props['b']['type'] == 'integer'
assert props['c']['type'] == 'number'
assert props['d']['type'] == 'boolean'
assert props['e']['type'] == 'array'
assert props['f']['type'] == 'object'
def test_required_parameters_detection(self):
"""Test that required parameters are detected correctly."""
funcschema = get_funcschema_module()
def func_with_defaults(name: str, optional: str = 'default'):
"""Function with default.
Args:
name: Required param.
optional: Optional param.
"""
pass
result = funcschema.get_func_schema(func_with_defaults)
assert 'name' in result['parameters']['required']
assert 'optional' not in result['parameters']['required']
def test_self_and_query_excluded(self):
"""Test that self and query parameters are excluded."""
funcschema = get_funcschema_module()
def method_func(self, query, other: str):
"""Method function.
Args:
self: Self parameter.
query: Query parameter.
other: Other parameter.
"""
pass
result = funcschema.get_func_schema(method_func)
props = result['parameters']['properties']
assert 'self' not in props
assert 'query' not in props
assert 'other' in props
def test_array_type_extraction(self):
"""Test that list[T] types extract element type."""
funcschema = get_funcschema_module()
def list_func(items: list[str], numbers: list[int]):
"""List function.
Args:
items: List of strings.
numbers: List of integers.
"""
pass
result = funcschema.get_func_schema(list_func)
props = result['parameters']['properties']
assert props['items']['type'] == 'array'
assert props['items']['items']['type'] == 'string'
assert props['numbers']['type'] == 'array'
assert props['numbers']['items']['type'] == 'integer'
def test_function_without_docstring_raises(self):
"""Test that function without docstring raises exception."""
funcschema = get_funcschema_module()
def no_doc_func(a: str):
pass
with pytest.raises(Exception) as exc_info:
funcschema.get_func_schema(no_doc_func)
assert 'has no docstring' in str(exc_info.value)
def test_description_extraction(self):
"""Test that description is extracted from first paragraph."""
funcschema = get_funcschema_module()
def desc_func(a: str):
"""This is the description.
Args:
a: Param a.
"""
pass
result = funcschema.get_func_schema(desc_func)
assert result['description'] == 'This is the description.'
def test_function_reference_stored(self):
"""Test that function reference is stored in schema."""
funcschema = get_funcschema_module()
def stored_func(a: str):
"""Stored function.
Args:
a: Param a.
"""
pass
result = funcschema.get_func_schema(stored_func)
assert result['function'] is stored_func
def test_description_from_args_doc(self):
"""Test that arg description is extracted from docstring."""
funcschema = get_funcschema_module()
def doc_func(param_name: str):
"""Function with documented param.
Args:
param_name: This is the param description.
"""
pass
result = funcschema.get_func_schema(doc_func)
assert result['parameters']['properties']['param_name']['description'] == 'This is the param description.'
def test_missing_parameter_doc_uses_empty_description(self):
"""Undocumented parameters should not break schema generation."""
funcschema = get_funcschema_module()
def sample_function(documented: str, undocumented: int):
"""Sample function.
Args:
documented(str): documented parameter description
"""
pass
result = funcschema.get_func_schema(sample_function)
assert result['parameters']['properties']['documented']['description'] == 'documented parameter description'
assert result['parameters']['properties']['undocumented']['description'] == ''

View File

@@ -0,0 +1,146 @@
"""
Unit tests for HTTP client session pool.
Tests session management, reuse, and cleanup.
"""
from __future__ import annotations
import pytest
import aiohttp
from aiohttp import web
from langbot.pkg.utils import httpclient
pytestmark = pytest.mark.asyncio
class TestGetSession:
"""Tests for get_session function."""
async def test_get_session_returns_client_session(self):
"""get_session returns an aiohttp.ClientSession."""
session = httpclient.get_session()
assert isinstance(session, aiohttp.ClientSession)
assert not session.closed
# Cleanup
await session.close()
async def test_get_session_returns_same_instance(self):
"""get_session returns the same session for same trust_env."""
session1 = httpclient.get_session(trust_env=False)
session2 = httpclient.get_session(trust_env=False)
assert session1 is session2
# Cleanup
await session1.close()
async def test_get_session_different_trust_env_creates_different(self):
"""Different trust_env values create different sessions."""
session1 = httpclient.get_session(trust_env=False)
session2 = httpclient.get_session(trust_env=True)
assert session1 is not session2
# Cleanup
await session1.close()
await session2.close()
async def test_get_session_recreates_if_closed(self):
"""get_session creates new session if previous is closed."""
session1 = httpclient.get_session()
await session1.close()
session2 = httpclient.get_session()
assert session2 is not session1
assert not session2.closed
# Cleanup
await session2.close()
class TestCloseAll:
"""Tests for close_all function."""
async def test_close_all_closes_all_sessions(self):
"""close_all closes all sessions."""
# Create multiple sessions
session1 = httpclient.get_session(trust_env=False)
session2 = httpclient.get_session(trust_env=True)
await httpclient.close_all()
assert session1.closed
assert session2.closed
async def test_close_all_clears_pool(self):
"""close_all clears the session pool."""
httpclient.get_session()
httpclient.get_session(trust_env=True)
await httpclient.close_all()
assert len(httpclient._sessions) == 0
async def test_close_all_handles_already_closed(self):
"""close_all handles already closed sessions gracefully."""
session = httpclient.get_session()
await session.close()
# Should not raise
await httpclient.close_all()
async def test_close_all_idempotent(self):
"""close_all can be called multiple times."""
httpclient.get_session()
await httpclient.close_all()
await httpclient.close_all() # Should not raise
assert len(httpclient._sessions) == 0
class TestSessionPoolIntegration:
"""Integration tests for session pool behavior."""
async def test_session_can_make_request(self):
"""Session can be used for HTTP requests without relying on external network."""
app = web.Application()
async def handle_get(request):
return web.json_response({'ok': True})
app.router.add_get('/get', handle_get)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, '127.0.0.1', 0)
await site.start()
port = site._server.sockets[0].getsockname()[1]
session = httpclient.get_session()
try:
async with session.get(
f'http://127.0.0.1:{port}/get',
timeout=aiohttp.ClientTimeout(total=5),
) as resp:
assert resp.status == 200
assert await resp.json() == {'ok': True}
finally:
await httpclient.close_all()
await runner.cleanup()
async def test_multiple_requests_same_session(self):
"""Multiple requests can use the same session."""
session = httpclient.get_session()
# Both calls return the same session
session2 = httpclient.get_session()
assert session is session2
await httpclient.close_all()

View File

@@ -1,22 +1,158 @@
from langbot.pkg.utils.image import get_qq_image_downloadable_url
"""
Unit tests for image utility functions.
Tests URL parsing and base64 extraction without network calls.
"""
from __future__ import annotations
import pytest
import base64
from langbot.pkg.utils.image import (
get_qq_image_downloadable_url,
extract_b64_and_format,
)
def test_get_qq_image_downloadable_url_preserves_https_scheme():
url, query = get_qq_image_downloadable_url('https://gchat.qpic.cn/gchatpic_new/abc/0?term=2&is_origin=1')
class TestGetQQImageDownloadableUrl:
"""Tests for get_qq_image_downloadable_url function."""
assert url == 'https://gchat.qpic.cn/gchatpic_new/abc/0'
assert query == {'term': ['2'], 'is_origin': ['1']}
def test_basic_url(self):
"""Parse basic image URL."""
url = "http://example.com/image.jpg"
result_url, query = get_qq_image_downloadable_url(url)
assert result_url == "http://example.com/image.jpg"
assert query == {}
def test_url_with_query_params(self):
"""Parse URL with query parameters."""
url = "http://example.com/image.jpg?param1=value1&param2=value2"
result_url, query = get_qq_image_downloadable_url(url)
assert result_url == "http://example.com/image.jpg"
assert query == {"param1": ["value1"], "param2": ["value2"]}
def test_url_with_port(self):
"""Parse URL with port number."""
url = "http://example.com:8080/image.jpg"
result_url, query = get_qq_image_downloadable_url(url)
assert result_url == "http://example.com:8080/image.jpg"
def test_url_with_path(self):
"""Parse URL with complex path."""
url = "http://example.com/path/to/image.jpg"
result_url, query = get_qq_image_downloadable_url(url)
assert result_url == "http://example.com/path/to/image.jpg"
def test_url_with_fragment(self):
"""Parse URL with fragment (fragment is not part of query)."""
url = "http://example.com/image.jpg#fragment"
result_url, query = get_qq_image_downloadable_url(url)
# Fragment is not included in query string parsing
assert "http://example.com/image.jpg" in result_url
def test_https_url(self):
"""Parse HTTPS URL and preserve its scheme."""
url = "https://example.com/image.jpg"
result_url, query = get_qq_image_downloadable_url(url)
assert result_url == "https://example.com/image.jpg"
assert query == {}
def test_preserves_qq_https_scheme_and_query(self):
"""QQ image URLs keep HTTPS and query parameters."""
result_url, query = get_qq_image_downloadable_url(
'https://gchat.qpic.cn/gchatpic_new/abc/0?term=2&is_origin=1'
)
assert result_url == 'https://gchat.qpic.cn/gchatpic_new/abc/0'
assert query == {'term': ['2'], 'is_origin': ['1']}
def test_defaults_missing_scheme_to_http(self):
"""Scheme-less image URLs default to HTTP."""
result_url, query = get_qq_image_downloadable_url('gchat.qpic.cn/gchatpic_new/abc/0?term=2')
assert result_url == 'http://gchat.qpic.cn/gchatpic_new/abc/0'
assert query == {'term': ['2']}
def test_get_qq_image_downloadable_url_preserves_http_scheme():
url, query = get_qq_image_downloadable_url('http://gchat.qpic.cn/gchatpic_new/abc/0?term=2')
class TestExtractB64AndFormat:
"""Tests for extract_b64_and_format function."""
assert url == 'http://gchat.qpic.cn/gchatpic_new/abc/0'
assert query == {'term': ['2']}
@pytest.mark.asyncio
async def test_jpeg_data_uri(self):
"""Extract base64 and format from JPEG data URI."""
# Create a simple base64 string
original_data = b"test image data"
b64_data = base64.b64encode(original_data).decode()
data_uri = f"data:image/jpeg;base64,{b64_data}"
result_b64, result_format = await extract_b64_and_format(data_uri)
def test_get_qq_image_downloadable_url_defaults_missing_scheme_to_http():
url, query = get_qq_image_downloadable_url('gchat.qpic.cn/gchatpic_new/abc/0?term=2')
assert result_b64 == b64_data
assert result_format == "jpeg"
assert url == 'http://gchat.qpic.cn/gchatpic_new/abc/0'
assert query == {'term': ['2']}
@pytest.mark.asyncio
async def test_png_data_uri(self):
"""Extract base64 and format from PNG data URI."""
original_data = b"test png data"
b64_data = base64.b64encode(original_data).decode()
data_uri = f"data:image/png;base64,{b64_data}"
result_b64, result_format = await extract_b64_and_format(data_uri)
assert result_b64 == b64_data
assert result_format == "png"
@pytest.mark.asyncio
async def test_gif_data_uri(self):
"""Extract base64 and format from GIF data URI."""
original_data = b"test gif data"
b64_data = base64.b64encode(original_data).decode()
data_uri = f"data:image/gif;base64,{b64_data}"
result_b64, result_format = await extract_b64_and_format(data_uri)
assert result_b64 == b64_data
assert result_format == "gif"
@pytest.mark.asyncio
async def test_webp_data_uri(self):
"""Extract base64 and format from WebP data URI."""
original_data = b"test webp data"
b64_data = base64.b64encode(original_data).decode()
data_uri = f"data:image/webp;base64,{b64_data}"
result_b64, result_format = await extract_b64_and_format(data_uri)
assert result_b64 == b64_data
assert result_format == "webp"
@pytest.mark.asyncio
async def test_complex_base64(self):
"""Handle base64 with special characters."""
# Base64 can include + and / characters
original_data = bytes(range(256)) # All byte values
b64_data = base64.b64encode(original_data).decode()
data_uri = f"data:image/png;base64,{b64_data}"
result_b64, result_format = await extract_b64_and_format(data_uri)
assert result_b64 == b64_data
# Verify we can decode back to original
assert base64.b64decode(result_b64) == original_data
@pytest.mark.asyncio
async def test_empty_base64(self):
"""Handle empty base64 string."""
data_uri = "data:image/png;base64,"
result_b64, result_format = await extract_b64_and_format(data_uri)
assert result_b64 == ""
assert result_format == "png"

View File

@@ -0,0 +1,192 @@
"""
Tests for langbot.pkg.utils.importutil module.
Tests import utility functions:
- import_dir: imports modules from a directory
- import_modules_in_pkg: imports all modules in a package
- import_modules_in_pkgs: imports all modules in multiple packages
- import_dot_style_dir: imports modules using dot notation path
- read_resource_file: reads a text resource file
- read_resource_file_bytes: reads a binary resource file
- list_resource_files: lists files in a resource directory
Uses mocking for import operations to avoid actual module imports.
"""
import pytest
import importlib
from unittest.mock import patch, MagicMock
class TestImportDir:
"""Test import_dir function."""
def test_calls_importlib_for_each_python_file(self, tmp_path):
"""Should call importlib.import_module for each .py file."""
module_dir = tmp_path / "test_modules"
module_dir.mkdir()
(module_dir / "__init__.py").write_text("")
(module_dir / "module_a.py").write_text("VALUE_A = 'a'\n")
(module_dir / "module_b.py").write_text("VALUE_B = 'b'\n")
(module_dir / "readme.txt").write_text("not a module")
from langbot.pkg.utils import importutil
with patch.object(importlib, "import_module") as mock_import:
importutil.import_dir(str(module_dir), path_prefix="test_prefix.")
# Should call import_module for each .py file (excluding __init__.py)
assert mock_import.call_count == 2
def test_skips_init_py(self, tmp_path):
"""Should skip __init__.py when importing."""
module_dir = tmp_path / "test_modules"
module_dir.mkdir()
(module_dir / "__init__.py").write_text("")
(module_dir / "regular.py").write_text("VALUE = 1\n")
from langbot.pkg.utils import importutil
with patch.object(importlib, "import_module") as mock_import:
importutil.import_dir(str(module_dir), path_prefix="test_prefix.")
# __init__.py should be skipped
mock_import.assert_called_once()
# The call should not include __init__
call_args = mock_import.call_args[0][0]
assert "__init__" not in call_args
def test_ignores_non_py_files(self, tmp_path):
"""Should ignore non-.py files."""
module_dir = tmp_path / "test_modules"
module_dir.mkdir()
(module_dir / "module.py").write_text("VALUE = 1\n")
(module_dir / "readme.txt").write_text("text")
(module_dir / "data.json").write_text("{}")
from langbot.pkg.utils import importutil
with patch.object(importlib, "import_module") as mock_import:
importutil.import_dir(str(module_dir), path_prefix="test_prefix.")
# Only .py files should be imported
assert mock_import.call_count == 1
class TestImportModulesInPkg:
"""Test import_modules_in_pkg function."""
def test_imports_modules_from_package(self, tmp_path):
"""Should import all modules from a package object."""
mock_pkg = MagicMock()
mock_pkg.__file__ = str(tmp_path / "__init__.py")
(tmp_path / "__init__.py").write_text("")
(tmp_path / "mod1.py").write_text("MOD1 = 1\n")
from langbot.pkg.utils import importutil
with patch.object(importutil, "import_dir") as mock_import_dir:
importutil.import_modules_in_pkg(mock_pkg)
mock_import_dir.assert_called_once()
call_path = mock_import_dir.call_args[0][0]
assert call_path == str(tmp_path)
class TestImportModulesInPkgs:
"""Test import_modules_in_pkgs function."""
def test_imports_from_multiple_packages(self):
"""Should call import_modules_in_pkg for each package."""
from langbot.pkg.utils import importutil
mock_pkg1 = MagicMock()
mock_pkg1.__file__ = "/path/to/pkg1/__init__.py"
mock_pkg2 = MagicMock()
mock_pkg2.__file__ = "/path/to/pkg2/__init__.py"
with patch.object(importutil, "import_modules_in_pkg") as mock_import:
importutil.import_modules_in_pkgs([mock_pkg1, mock_pkg2])
assert mock_import.call_count == 2
class TestImportDotStyleDir:
"""Test import_dot_style_dir function."""
def test_converts_dot_notation_to_path(self, tmp_path):
"""Should convert dot notation to path and import."""
# Create structure matching the dot notation
(tmp_path / "my").mkdir()
(tmp_path / "my" / "pkg").mkdir()
(tmp_path / "my" / "pkg" / "test").mkdir()
from langbot.pkg.utils import importutil
with patch.object(importutil, "import_dir") as mock_import_dir:
importutil.import_dot_style_dir("my.pkg.test")
# The path should be converted using os.path.join
call_path = mock_import_dir.call_args[0][0]
# Should contain the path components joined
assert "my" in call_path
class TestReadResourceFile:
"""Test read_resource_file function."""
def test_reads_resource_file_content(self):
"""Should read content from a resource file."""
from langbot.pkg.utils import importutil
content = importutil.read_resource_file("templates/config.yaml")
assert "admins:" in content
assert "edition: community" in content
def test_raises_for_nonexistent_file(self):
"""Should raise exception for non-existent resource file."""
from langbot.pkg.utils import importutil
with pytest.raises((FileNotFoundError, Exception)):
importutil.read_resource_file("nonexistent/path/file.txt")
class TestReadResourceFileBytes:
"""Test read_resource_file_bytes function."""
def test_reads_resource_file_as_bytes(self):
"""Should read content as bytes from a resource file."""
from langbot.pkg.utils import importutil
content = importutil.read_resource_file_bytes("templates/config.yaml")
assert b"admins:" in content
assert b"edition: community" in content
def test_raises_for_nonexistent_file_bytes(self):
"""Should raise exception for non-existent resource file."""
from langbot.pkg.utils import importutil
with pytest.raises((FileNotFoundError, Exception)):
importutil.read_resource_file_bytes("nonexistent/path/file.txt")
class TestListResourceFiles:
"""Test list_resource_files function."""
def test_lists_files_in_resource_directory(self):
"""Should list files in a resource directory."""
from langbot.pkg.utils import importutil
files = importutil.list_resource_files("templates")
assert "config.yaml" in files
assert "default-pipeline-config.json" in files
assert all(isinstance(file, str) for file in files)
def test_raises_for_nonexistent_directory(self):
"""Should raise exception for non-existent directory."""
from langbot.pkg.utils import importutil
with pytest.raises((FileNotFoundError, Exception)):
importutil.list_resource_files("nonexistent_directory_xyz")
if __name__ == "__main__":
pytest.main([__file__, "-v"])

View File

@@ -0,0 +1,210 @@
"""
Unit tests for log cache utilities.
Tests log page management and pointer-based retrieval.
"""
from __future__ import annotations
from langbot.pkg.utils.logcache import LogPage, LogCache, LOG_PAGE_SIZE, MAX_CACHED_PAGES
class TestLogPage:
"""Tests for LogPage class."""
def test_init_creates_empty_page(self):
"""LogPage initializes with empty logs list."""
page = LogPage(number=0)
assert page.number == 0
assert page.logs == []
def test_add_log_appends_to_list(self):
"""add_log appends log to the list."""
page = LogPage(number=0)
page.add_log('log entry 1')
page.add_log('log entry 2')
assert len(page.logs) == 2
assert page.logs[0] == 'log entry 1'
assert page.logs[1] == 'log entry 2'
def test_add_log_returns_false_when_not_full(self):
"""add_log returns False when page is not full."""
page = LogPage(number=0)
for i in range(LOG_PAGE_SIZE - 1):
result = page.add_log(f'log {i}')
assert result is False
def test_add_log_returns_true_when_full(self):
"""add_log returns True when page reaches LOG_PAGE_SIZE."""
page = LogPage(number=0)
for i in range(LOG_PAGE_SIZE - 1):
page.add_log(f'log {i}')
result = page.add_log('last log')
assert result is True
def test_add_log_exactly_page_size(self):
"""Page contains exactly LOG_PAGE_SIZE logs when full."""
page = LogPage(number=0)
for i in range(LOG_PAGE_SIZE):
page.add_log(f'log {i}')
assert len(page.logs) == LOG_PAGE_SIZE
class TestLogCache:
"""Tests for LogCache class."""
def test_init_creates_first_page(self):
"""LogCache initializes with first empty page."""
cache = LogCache()
assert len(cache.log_pages) == 1
assert cache.log_pages[0].number == 0
assert cache.log_pages[0].logs == []
def test_add_log_to_first_page(self):
"""add_log adds to the first page initially."""
cache = LogCache()
cache.add_log('test log')
assert len(cache.log_pages) == 1
assert cache.log_pages[0].logs[0] == 'test log'
def test_add_log_creates_new_page_when_full(self):
"""add_log creates new page when current page is full."""
cache = LogCache()
# Fill first page
for i in range(LOG_PAGE_SIZE):
cache.add_log(f'log {i}')
# Add one more to trigger new page
cache.add_log('overflow log')
assert len(cache.log_pages) == 2
assert cache.log_pages[1].number == 1
assert cache.log_pages[1].logs[0] == 'overflow log'
def test_add_log_removes_oldest_page_when_exceeds_max(self):
"""Cache removes oldest page when exceeding MAX_CACHED_PAGES."""
cache = LogCache()
# Fill enough pages to exceed MAX_CACHED_PAGES
total_logs = (MAX_CACHED_PAGES + 1) * LOG_PAGE_SIZE
for i in range(total_logs):
cache.add_log(f'log {i}')
# Should have exactly MAX_CACHED_PAGES pages
assert len(cache.log_pages) == MAX_CACHED_PAGES
# First page should not be page 0
assert cache.log_pages[0].number > 0
def test_get_log_by_pointer_single_page(self):
"""get_log_by_pointer retrieves logs from single page."""
cache = LogCache()
cache.add_log('log 1')
cache.add_log('log 2')
cache.add_log('log 3')
result, page_num, offset = cache.get_log_by_pointer(0, 0)
assert 'log 1' in result
assert 'log 2' in result
assert 'log 3' in result
def test_get_log_by_pointer_with_offset(self):
"""get_log_by_pointer respects start offset."""
cache = LogCache()
cache.add_log('log 1')
cache.add_log('log 2')
cache.add_log('log 3')
result, page_num, offset = cache.get_log_by_pointer(0, 1)
assert 'log 1' not in result
assert 'log 2' in result
assert 'log 3' in result
def test_get_log_by_pointer_across_pages(self):
"""get_log_by_pointer retrieves logs across pages."""
cache = LogCache()
# Fill first page and add to second
for i in range(LOG_PAGE_SIZE):
cache.add_log(f'page0 log {i}')
cache.add_log('page1 log 0')
# Get from first page offset 0
result, page_num, offset = cache.get_log_by_pointer(0, 0)
# Should contain all logs from page 0 and page 1
assert 'page0 log 0' in result
assert 'page1 log 0' in result
def test_get_log_by_pointer_from_second_page(self):
"""get_log_by_pointer can start from second page."""
cache = LogCache()
# Fill first page and add to second
for i in range(LOG_PAGE_SIZE):
cache.add_log(f'page0 log {i}')
cache.add_log('page1 log 0')
# Get from second page
result, page_num, offset = cache.get_log_by_pointer(1, 0)
assert 'page0' not in result
assert 'page1 log 0' in result
def test_page_numbers_sequential(self):
"""Page numbers are sequential."""
cache = LogCache()
# Create multiple pages
for i in range(LOG_PAGE_SIZE * 3):
cache.add_log(f'log {i}')
for i, page in enumerate(cache.log_pages):
assert page.number == i
def test_empty_cache_get_log(self):
"""get_log_by_pointer works with empty cache."""
cache = LogCache()
result, page_num, offset = cache.get_log_by_pointer(0, 0)
assert result == ''
def test_get_log_by_pointer_nonexistent_page(self):
"""get_log_by_pointer handles nonexistent page number."""
cache = LogCache()
cache.add_log('log 1')
# Request page that doesn't exist
result, page_num, offset = cache.get_log_by_pointer(99, 0)
# Returns empty or last available
# Behavior depends on implementation
def test_max_cached_pages_constant(self):
"""MAX_CACHED_PAGES is defined and reasonable."""
assert MAX_CACHED_PAGES > 0
assert MAX_CACHED_PAGES <= 100 # Reasonable upper bound
def test_log_page_size_constant(self):
"""LOG_PAGE_SIZE is defined and reasonable."""
assert LOG_PAGE_SIZE > 0
assert LOG_PAGE_SIZE <= 1000 # Reasonable upper bound

View File

@@ -0,0 +1,223 @@
"""
Tests for langbot.pkg.utils.paths module.
Tests path utility functions:
- get_frontend_path: locates frontend build files
- get_resource_path: locates resource files
- _check_if_source_install: detects source install mode
Uses tmp_path for file system isolation where applicable.
"""
import os
import pytest
from unittest.mock import patch
class TestCheckIfSourceInstall:
"""Test _check_if_source_install function."""
def test_returns_true_for_source_install(self, tmp_path, monkeypatch):
"""Should return True when main.py with LangBot marker exists."""
main_py = tmp_path / "main.py"
main_py.write_text('# LangBot/main.py\n# This is the entry point')
monkeypatch.chdir(tmp_path)
from langbot.pkg.utils import paths
paths._is_source_install = None
result = paths._check_if_source_install()
assert result is True
paths._is_source_install = None
def test_returns_false_when_no_main_py(self, tmp_path, monkeypatch):
"""Should return False when main.py doesn't exist."""
monkeypatch.chdir(tmp_path)
from langbot.pkg.utils import paths
paths._is_source_install = None
result = paths._check_if_source_install()
assert result is False
paths._is_source_install = None
def test_returns_false_when_main_py_without_marker(self, tmp_path, monkeypatch):
"""Should return False when main.py exists but lacks LangBot marker."""
main_py = tmp_path / "main.py"
main_py.write_text('# Some other project\nprint("hello")')
monkeypatch.chdir(tmp_path)
from langbot.pkg.utils import paths
paths._is_source_install = None
result = paths._check_if_source_install()
assert result is False
paths._is_source_install = None
def test_handles_io_error_gracefully(self, tmp_path, monkeypatch):
"""Should return False when main.py cannot be read."""
main_py = tmp_path / "main.py"
main_py.write_text('# LangBot/main.py\n')
monkeypatch.chdir(tmp_path)
from langbot.pkg.utils import paths
paths._is_source_install = None
# Patch open to raise IOError
with patch("builtins.open", side_effect=IOError("Cannot read")):
result = paths._check_if_source_install()
assert result is False
paths._is_source_install = None
class TestGetFrontendPath:
"""Test get_frontend_path function."""
def test_returns_web_dist_by_default(self):
"""Should return a path containing web/dist as default."""
from langbot.pkg.utils import paths
paths._is_source_install = None
result = paths.get_frontend_path()
# The result should contain web/dist or be an absolute path to it
assert "web/dist" in result or result.endswith("dist")
paths._is_source_install = None
def test_finds_dist_directory_in_source_mode(self, tmp_path, monkeypatch):
"""Should find web/dist when running from source mode."""
main_py = tmp_path / "main.py"
main_py.write_text('# LangBot/main.py\n')
web_dist = tmp_path / "web" / "dist"
web_dist.mkdir(parents=True)
monkeypatch.chdir(tmp_path)
from langbot.pkg.utils import paths
paths._is_source_install = None
result = paths.get_frontend_path()
assert result == "web/dist"
paths._is_source_install = None
def test_prefers_dist_over_out_in_source_mode(self, tmp_path, monkeypatch):
"""Should prefer web/dist over web/out when both exist in source mode."""
main_py = tmp_path / "main.py"
main_py.write_text('# LangBot/main.py\n')
web_dist = tmp_path / "web" / "dist"
web_dist.mkdir(parents=True)
web_out = tmp_path / "web" / "out"
web_out.mkdir(parents=True)
monkeypatch.chdir(tmp_path)
from langbot.pkg.utils import paths
paths._is_source_install = None
result = paths.get_frontend_path()
assert result == "web/dist"
paths._is_source_install = None
class TestGetResourcePath:
"""Test get_resource_path function."""
def test_returns_original_path_when_not_found(self, tmp_path, monkeypatch):
"""Should return original path when resource not found."""
monkeypatch.chdir(tmp_path)
from langbot.pkg.utils import paths
paths._is_source_install = None
result = paths.get_resource_path("nonexistent/file.txt")
assert result == "nonexistent/file.txt"
paths._is_source_install = None
def test_finds_resource_in_current_directory_source_mode(self, tmp_path, monkeypatch):
"""Should find resource in current directory when in source mode."""
main_py = tmp_path / "main.py"
main_py.write_text('# LangBot/main.py\n')
resource_file = tmp_path / "templates" / "config.yaml"
resource_file.parent.mkdir(parents=True, exist_ok=True)
resource_file.write_text("test: value")
monkeypatch.chdir(tmp_path)
from langbot.pkg.utils import paths
paths._is_source_install = None
result = paths.get_resource_path("templates/config.yaml")
assert os.path.exists(result)
paths._is_source_install = None
def test_returns_relative_path_in_source_mode(self, tmp_path, monkeypatch):
"""Should return relative path if resource exists in source mode."""
main_py = tmp_path / "main.py"
main_py.write_text('# LangBot/main.py\n')
resource_file = tmp_path / "test_resource.txt"
resource_file.write_text("test content")
monkeypatch.chdir(tmp_path)
from langbot.pkg.utils import paths
paths._is_source_install = None
result = paths.get_resource_path("test_resource.txt")
assert result == "test_resource.txt"
paths._is_source_install = None
class TestPathFunctionsCaching:
"""Test that path functions use caching correctly."""
def test_source_install_cache_is_used(self, tmp_path, monkeypatch):
"""_check_if_source_install should use cached result."""
main_py = tmp_path / "main.py"
main_py.write_text('# LangBot/main.py\n')
monkeypatch.chdir(tmp_path)
from langbot.pkg.utils import paths
paths._is_source_install = None
# First call sets cache
result1 = paths._check_if_source_install()
assert result1 is True
assert paths._is_source_install is True
# Second call uses cache (no file read needed)
result2 = paths._check_if_source_install()
assert result2 is True
paths._is_source_install = None
if __name__ == "__main__":
pytest.main([__file__, "-v"])

View File

@@ -1,58 +1,157 @@
"""
Unit tests for package manager utilities.
Tests pip command generation without actual installation.
"""
from __future__ import annotations
import inspect
from unittest.mock import patch
from langbot.pkg.utils import pkgmgr
def test_install_requirements_defaults_extra_params_to_none():
signature = inspect.signature(pkgmgr.install_requirements)
class TestPkgMgr:
"""Tests for package manager functions."""
assert signature.parameters['extra_params'].default is None
def test_install_calls_pipmain(self):
"""install calls pipmain with correct arguments."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.install('requests')
mock_pipmain.assert_called_once_with(['install', 'requests'])
def test_install_requirements_omitted_extra_params_uses_base_command(monkeypatch):
calls = []
monkeypatch.setattr(pkgmgr, 'pipmain', calls.append)
def test_install_with_version(self):
"""install handles package with version specifier."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.install('requests>=2.0.0')
pkgmgr.install_requirements('requirements.txt')
pkgmgr.install_requirements('requirements-dev.txt')
mock_pipmain.assert_called_once_with(['install', 'requests>=2.0.0'])
assert calls == [
[
'install',
'-r',
'requirements.txt',
'-i',
'https://pypi.tuna.tsinghua.edu.cn/simple',
'--trusted-host',
'pypi.tuna.tsinghua.edu.cn',
],
[
'install',
'-r',
'requirements-dev.txt',
'-i',
'https://pypi.tuna.tsinghua.edu.cn/simple',
'--trusted-host',
'pypi.tuna.tsinghua.edu.cn',
],
]
def test_install_upgrade_calls_pipmain(self):
"""install_upgrade calls pipmain with upgrade and mirror."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.install_upgrade('requests')
expected_args = [
'install',
'--upgrade',
'requests',
'-i',
'https://pypi.tuna.tsinghua.edu.cn/simple',
'--trusted-host',
'pypi.tuna.tsinghua.edu.cn',
]
mock_pipmain.assert_called_once_with(expected_args)
def test_install_requirements_preserves_explicit_extra_params(monkeypatch):
calls = []
monkeypatch.setattr(pkgmgr, 'pipmain', calls.append)
def test_run_pip_with_params(self):
"""run_pip passes params to pipmain."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.run_pip(['list', '--outdated'])
pkgmgr.install_requirements('requirements.txt', extra_params=['--no-deps'])
mock_pipmain.assert_called_once_with(['list', '--outdated'])
assert calls == [
[
'install',
'-r',
'requirements.txt',
'-i',
'https://pypi.tuna.tsinghua.edu.cn/simple',
'--trusted-host',
'pypi.tuna.tsinghua.edu.cn',
'--no-deps',
def test_run_pip_empty_params(self):
"""run_pip handles empty params."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.run_pip([])
mock_pipmain.assert_called_once_with([])
def test_install_requirements_calls_pipmain(self):
"""install_requirements calls pipmain with requirements file."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.install_requirements('requirements.txt')
expected_args = [
'install',
'-r',
'requirements.txt',
'-i',
'https://pypi.tuna.tsinghua.edu.cn/simple',
'--trusted-host',
'pypi.tuna.tsinghua.edu.cn',
]
mock_pipmain.assert_called_once_with(expected_args)
def test_install_requirements_defaults_extra_params_to_none(self):
"""install_requirements should not use a mutable default for extra_params."""
signature = inspect.signature(pkgmgr.install_requirements)
assert signature.parameters['extra_params'].default is None
def test_install_requirements_omitted_extra_params_uses_independent_base_commands(self, monkeypatch):
"""Omitted extra_params should not share mutable state across calls."""
calls = []
monkeypatch.setattr(pkgmgr, 'pipmain', calls.append)
pkgmgr.install_requirements('requirements.txt')
pkgmgr.install_requirements('requirements-dev.txt')
assert calls == [
[
'install',
'-r',
'requirements.txt',
'-i',
'https://pypi.tuna.tsinghua.edu.cn/simple',
'--trusted-host',
'pypi.tuna.tsinghua.edu.cn',
],
[
'install',
'-r',
'requirements-dev.txt',
'-i',
'https://pypi.tuna.tsinghua.edu.cn/simple',
'--trusted-host',
'pypi.tuna.tsinghua.edu.cn',
],
]
]
def test_install_requirements_preserves_explicit_extra_params(self, monkeypatch):
"""Explicit extra_params should be appended to the generated pip command."""
calls = []
monkeypatch.setattr(pkgmgr, 'pipmain', calls.append)
pkgmgr.install_requirements('requirements.txt', extra_params=['--no-deps'])
assert calls == [
[
'install',
'-r',
'requirements.txt',
'-i',
'https://pypi.tuna.tsinghua.edu.cn/simple',
'--trusted-host',
'pypi.tuna.tsinghua.edu.cn',
'--no-deps',
]
]
def test_install_requirements_with_extra_params(self):
"""install_requirements handles extra params."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.install_requirements('requirements.txt', ['--no-cache-dir'])
expected_args = [
'install',
'-r',
'requirements.txt',
'-i',
'https://pypi.tuna.tsinghua.edu.cn/simple',
'--trusted-host',
'pypi.tuna.tsinghua.edu.cn',
'--no-cache-dir',
]
mock_pipmain.assert_called_once_with(expected_args)
def test_install_requirements_multiple_extra_params(self):
"""install_requirements handles multiple extra params."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.install_requirements('requirements.txt', ['--no-cache-dir', '--verbose'])
call_args = mock_pipmain.call_args[0][0]
assert '--no-cache-dir' in call_args
assert '--verbose' in call_args

View File

@@ -0,0 +1,89 @@
"""Unit tests for utils platform detection.
Tests cover:
- get_platform() function
- Docker environment detection
- WebSocket plugin runtime mode
"""
from __future__ import annotations
import os
import sys
from unittest.mock import patch
from importlib import import_module
def get_platform_module():
"""Lazy import to avoid circular import issues."""
return import_module('langbot.pkg.utils.platform')
class TestGetPlatform:
"""Tests for get_platform function."""
def test_returns_docker_when_dockerenv_exists(self):
"""Test returns 'docker' when /.dockerenv file exists."""
platform_module = get_platform_module()
with patch('os.path.exists', return_value=True):
with patch.dict(os.environ, {}, clear=True):
result = platform_module.get_platform()
assert result == 'docker'
def test_returns_docker_when_env_var_true(self):
"""Test returns 'docker' when DOCKER_ENV=true."""
platform_module = get_platform_module()
with patch('os.path.exists', return_value=False):
with patch.dict(os.environ, {'DOCKER_ENV': 'true'}, clear=True):
result = platform_module.get_platform()
assert result == 'docker'
def test_returns_sys_platform_when_not_docker(self):
"""Test returns sys.platform when not in Docker."""
platform_module = get_platform_module()
with patch('os.path.exists', return_value=False):
with patch.dict(os.environ, {'DOCKER_ENV': 'false'}, clear=True):
result = platform_module.get_platform()
assert result == sys.platform
def test_returns_sys_platform_when_no_env_var(self):
"""Test returns sys.platform when DOCKER_ENV not set."""
platform_module = get_platform_module()
with patch('os.path.exists', return_value=False):
# Make sure DOCKER_ENV is not set
env_copy = os.environ.copy()
if 'DOCKER_ENV' in env_copy:
del env_copy['DOCKER_ENV']
with patch.dict(os.environ, env_copy, clear=True):
result = platform_module.get_platform()
assert result == sys.platform
def test_standalone_runtime_default_false(self):
"""Test standalone_runtime defaults to False."""
platform_module = get_platform_module()
# Check the module attribute
assert platform_module.standalone_runtime is False
def test_use_websocket_returns_standalone_runtime(self):
"""Test use_websocket_to_connect_plugin_runtime returns standalone_runtime."""
platform_module = get_platform_module()
result = platform_module.use_websocket_to_connect_plugin_runtime()
assert result == platform_module.standalone_runtime
def test_standalone_runtime_can_be_modified(self):
"""Test standalone_runtime can be modified."""
platform_module = get_platform_module()
original = platform_module.standalone_runtime
# Modify
platform_module.standalone_runtime = True
assert platform_module.use_websocket_to_connect_plugin_runtime() is True
# Restore
platform_module.standalone_runtime = original

View File

@@ -0,0 +1,167 @@
"""
Unit tests for ProxyManager.
Tests proxy configuration from environment and config.
"""
from __future__ import annotations
import pytest
import os
from unittest.mock import Mock, patch
from langbot.pkg.utils.proxy import ProxyManager
pytestmark = pytest.mark.asyncio
class TestProxyManager:
"""Tests for ProxyManager class."""
def _create_mock_app(self, proxy_config: dict = None):
"""Create mock app with proxy config."""
mock_app = Mock()
mock_app.instance_config = Mock()
mock_app.instance_config.data = {'proxy': proxy_config or {}}
return mock_app
def test_init_creates_empty_proxies(self):
"""ProxyManager initializes with empty forward_proxies."""
mock_app = self._create_mock_app()
pm = ProxyManager(mock_app)
assert pm.forward_proxies == {}
async def test_initialize_reads_env_variables(self):
"""initialize reads HTTP_PROXY from environment."""
mock_app = self._create_mock_app()
with patch.dict(os.environ, {'HTTP_PROXY': 'http://env-proxy:8080', 'HTTPS_PROXY': 'https://env-proxy:8443'}):
pm = ProxyManager(mock_app)
await pm.initialize()
assert pm.forward_proxies['http://'] == 'http://env-proxy:8080'
assert pm.forward_proxies['https://'] == 'https://env-proxy:8443'
async def test_initialize_reads_lower_case_env(self):
"""initialize reads lower case http_proxy from environment."""
mock_app = self._create_mock_app()
with patch.dict(os.environ, {'http_proxy': 'http://lower-proxy:8080'}, clear=True):
# Clear HTTP_PROXY to test fallback
if 'HTTP_PROXY' in os.environ:
del os.environ['HTTP_PROXY']
pm = ProxyManager(mock_app)
await pm.initialize()
assert pm.forward_proxies['http://'] == 'http://lower-proxy:8080'
async def test_initialize_config_overrides_env(self):
"""Config proxy overrides environment variables."""
mock_app = self._create_mock_app(proxy_config={
'http': 'http://config-proxy:8080',
'https': 'https://config-proxy:8443',
})
with patch.dict(os.environ, {'HTTP_PROXY': 'http://env-proxy:8080'}):
pm = ProxyManager(mock_app)
await pm.initialize()
assert pm.forward_proxies['http://'] == 'http://config-proxy:8080'
assert pm.forward_proxies['https://'] == 'https://config-proxy:8443'
async def test_initialize_sets_env_variables(self):
"""initialize sets proxy to environment variables."""
mock_app = self._create_mock_app(proxy_config={
'http': 'http://test-proxy:8080',
'https': 'https://test-proxy:8443',
})
pm = ProxyManager(mock_app)
await pm.initialize()
assert os.environ.get('HTTP_PROXY') == 'http://test-proxy:8080'
assert os.environ.get('HTTPS_PROXY') == 'https://test-proxy:8443'
async def test_initialize_handles_empty_config(self):
"""initialize handles empty proxy config."""
mock_app = self._create_mock_app(proxy_config={})
with patch.dict(os.environ, clear=True):
pm = ProxyManager(mock_app)
await pm.initialize()
assert pm.forward_proxies['http://'] is None
assert pm.forward_proxies['https://'] is None
async def test_initialize_handles_no_env_no_config(self):
"""initialize handles no env and no config."""
mock_app = self._create_mock_app(proxy_config={})
# Clear proxy env vars
env_backup = {}
for key in ['HTTP_PROXY', 'http_proxy', 'HTTPS_PROXY', 'https_proxy']:
env_backup[key] = os.environ.get(key)
if key in os.environ:
del os.environ[key]
try:
pm = ProxyManager(mock_app)
await pm.initialize()
assert pm.forward_proxies['http://'] is None
assert pm.forward_proxies['https://'] is None
finally:
# Restore env
for key, value in env_backup.items():
if value is not None:
os.environ[key] = value
def test_get_forward_proxies_returns_copy(self):
"""get_forward_proxies returns a copy of the dict."""
mock_app = self._create_mock_app()
pm = ProxyManager(mock_app)
pm.forward_proxies = {'http://': 'http://test:8080'}
result = pm.get_forward_proxies()
assert result == pm.forward_proxies
assert result is not pm.forward_proxies # Different object
def test_get_forward_proxies_modification_safe(self):
"""Modifying returned dict doesn't affect internal state."""
mock_app = self._create_mock_app()
pm = ProxyManager(mock_app)
pm.forward_proxies = {'http://': 'http://test:8080'}
result = pm.get_forward_proxies()
result['http://'] = 'http://modified:9999'
assert pm.forward_proxies['http://'] == 'http://test:8080'
async def test_initialize_http_only_config(self):
"""initialize handles http-only config."""
mock_app = self._create_mock_app(proxy_config={
'http': 'http://http-only:8080',
})
# Clear any existing proxy env vars
env_backup = {}
for key in ['HTTP_PROXY', 'http_proxy', 'HTTPS_PROXY', 'https_proxy']:
env_backup[key] = os.environ.get(key)
if key in os.environ:
del os.environ[key]
try:
pm = ProxyManager(mock_app)
await pm.initialize()
assert pm.forward_proxies['http://'] == 'http://http-only:8080'
assert pm.forward_proxies['https://'] is None
finally:
# Restore env
for key, value in env_backup.items():
if value is not None:
os.environ[key] = value

View File

@@ -1,46 +1,327 @@
"""
Tests for langbot.pkg.utils.runner module.
Tests runner category detection functions:
- get_runner_category: categorizes runner URLs as local, cloud, or unknown
- is_cloud_runner / is_local_runner: helper functions
- extract_runner_url: extracts URL from runner config
- get_runner_info: returns runner info dict
"""
import pytest
from unittest.mock import Mock, patch
from langbot.pkg.utils.runner import RunnerCategory, get_runner_category
@pytest.mark.parametrize(
'runner_url',
[
'api.dify.ai/v1',
'localhost:7860',
'https:///v1',
'https://',
'https://exa mple.com',
'http://[::1',
'http://localhost:bad',
],
from langbot.pkg.utils.runner import (
RunnerCategory,
CLOUD_DOMAINS,
LOCAL_PATTERNS,
get_runner_category,
get_runner_info,
is_cloud_runner,
is_local_runner,
extract_runner_url,
get_runner_category_from_runner,
)
def test_get_runner_category_returns_unknown_for_invalid_urls(runner_url):
assert get_runner_category('dify-service-api', runner_url) == RunnerCategory.UNKNOWN
@pytest.mark.parametrize(
'runner_url',
[
'http://localhost:7860',
'http://127.0.0.1:7860',
'http://10.0.0.1:7860',
'http://172.16.0.1:7860',
'http://172.31.255.255:7860',
'http://192.168.1.20:7860',
'http://[::1]:7860',
],
)
def test_get_runner_category_detects_local_hosts_with_ipaddress(runner_url):
assert get_runner_category('langflow-api', runner_url) == RunnerCategory.LOCAL
class TestGetRunnerCategory:
"""Test runner category detection from URL."""
def test_empty_url_returns_unknown(self):
"""Empty or None URL should return UNKNOWN."""
assert get_runner_category("test", "") == RunnerCategory.UNKNOWN
assert get_runner_category("test", None) == RunnerCategory.UNKNOWN
def test_localhost_returns_local(self):
"""localhost URL should be categorized as LOCAL."""
assert get_runner_category("test", "http://localhost:3000") == RunnerCategory.LOCAL
assert get_runner_category("test", "https://localhost") == RunnerCategory.LOCAL
def test_127_0_0_1_returns_local(self):
"""127.0.0.1 URL should be categorized as LOCAL."""
assert get_runner_category("test", "http://127.0.0.1:8080") == RunnerCategory.LOCAL
assert get_runner_category("test", "https://127.0.0.1") == RunnerCategory.LOCAL
def test_0_0_0_0_returns_local(self):
"""0.0.0.0 URL should be categorized as LOCAL."""
assert get_runner_category("test", "http://0.0.0.0:8080") == RunnerCategory.LOCAL
def test_private_ip_192_168_returns_local(self):
"""192.168.x.x private IP should be categorized as LOCAL."""
assert get_runner_category("test", "http://192.168.1.1:3000") == RunnerCategory.LOCAL
assert get_runner_category("test", "http://192.168.0.100") == RunnerCategory.LOCAL
def test_private_ip_10_returns_local(self):
"""10.x.x.x private IP should be categorized as LOCAL."""
assert get_runner_category("test", "http://10.0.0.1:8080") == RunnerCategory.LOCAL
assert get_runner_category("test", "http://10.255.255.255") == RunnerCategory.LOCAL
def test_private_ip_172_16_31_returns_local(self):
"""172.16.x.x - 172.31.x.x private IP range should be categorized as LOCAL."""
assert get_runner_category("test", "http://172.16.0.1:8080") == RunnerCategory.LOCAL
assert get_runner_category("test", "http://172.20.0.1") == RunnerCategory.LOCAL
assert get_runner_category("test", "http://172.31.255.255") == RunnerCategory.LOCAL
def test_n8n_cloud_returns_cloud(self):
"""n8n.cloud domain should be categorized as CLOUD."""
assert get_runner_category("test", "https://myinstance.n8n.cloud") == RunnerCategory.CLOUD
assert get_runner_category("test", "https://test.n8n.io") == RunnerCategory.CLOUD
def test_dify_cloud_returns_cloud(self):
"""Dify cloud domains should be categorized as CLOUD."""
assert get_runner_category("test", "https://api.dify.ai/v1") == RunnerCategory.CLOUD
assert get_runner_category("test", "https://cloud.dify.ai") == RunnerCategory.CLOUD
def test_coze_cloud_returns_cloud(self):
"""Coze domains should be categorized as CLOUD."""
assert get_runner_category("test", "https://api.coze.com") == RunnerCategory.CLOUD
assert get_runner_category("test", "https://api.coze.cn") == RunnerCategory.CLOUD
def test_langflow_cloud_returns_cloud(self):
"""Langflow domains should be categorized as CLOUD."""
assert get_runner_category("test", "https://cloud.langflow.ai") == RunnerCategory.CLOUD
assert get_runner_category("test", "https://test.langflow.org") == RunnerCategory.CLOUD
def test_other_url_returns_cloud(self):
"""Other URLs should default to CLOUD category."""
assert get_runner_category("test", "https://example.com") == RunnerCategory.CLOUD
assert get_runner_category("test", "https://myserver.example.org") == RunnerCategory.CLOUD
@pytest.mark.parametrize(
'runner_url',
[
'api.dify.ai/v1',
'localhost:7860',
'https:///v1',
'https://',
'https://exa mple.com',
'http://[::1',
'http://localhost:bad',
],
)
def test_invalid_urls_return_unknown(self, runner_url):
"""Invalid or incomplete URLs should return UNKNOWN."""
assert get_runner_category("test", runner_url) == RunnerCategory.UNKNOWN
def test_urlparse_exception_returns_unknown(self):
"""Exception during URL parsing should return UNKNOWN."""
# Test by mocking urlparse to raise an exception
from langbot.pkg.utils import runner
def mock_urlparse(url):
raise Exception("URL parsing failed")
with patch("langbot.pkg.utils.runner.urlparse", side_effect=mock_urlparse):
result = runner.get_runner_category("test", "http://example.com")
assert result == RunnerCategory.UNKNOWN
def test_url_without_scheme_returns_unknown(self):
"""URL without scheme should return UNKNOWN."""
assert get_runner_category("test", "example.com") == RunnerCategory.UNKNOWN
@pytest.mark.parametrize(
'runner_url',
[
'http://localhost:7860',
'http://127.0.0.1:7860',
'http://10.0.0.1:7860',
'http://172.16.0.1:7860',
'http://172.31.255.255:7860',
'http://192.168.1.20:7860',
'http://[::1]:7860',
],
)
def test_detects_local_hosts_with_ipaddress(self, runner_url):
"""Local hostnames and private IPs should be categorized as LOCAL."""
assert get_runner_category('langflow-api', runner_url) == RunnerCategory.LOCAL
@pytest.mark.parametrize(
'runner_url',
[
'http://10.evil.com',
'http://192.168.example.com',
],
)
def test_private_ip_prefix_domains_are_not_local(self, runner_url):
"""Domain names that only look like private IP prefixes should not be LOCAL."""
assert get_runner_category('langflow-api', runner_url) == RunnerCategory.CLOUD
class TestIsCloudRunner:
"""Test is_cloud_runner helper function."""
def test_cloud_runner_returns_true(self):
"""Cloud URL should return True."""
assert is_cloud_runner("test", "https://api.dify.ai") is True
def test_local_runner_returns_false(self):
"""Local URL should return False."""
assert is_cloud_runner("test", "http://localhost:3000") is False
def test_unknown_returns_false(self):
"""Unknown category should return False."""
assert is_cloud_runner("test", None) is False
@pytest.mark.parametrize(
'runner_url',
[
'http://10.evil.com',
'http://192.168.example.com',
],
)
def test_get_runner_category_does_not_treat_private_ip_prefix_domains_as_local(runner_url):
assert get_runner_category('langflow-api', runner_url) == RunnerCategory.CLOUD
class TestIsLocalRunner:
"""Test is_local_runner helper function."""
def test_local_runner_returns_true(self):
"""Local URL should return True."""
assert is_local_runner("test", "http://localhost:3000") is True
def test_cloud_runner_returns_false(self):
"""Cloud URL should return False."""
assert is_local_runner("test", "https://api.dify.ai") is False
def test_unknown_returns_false(self):
"""Unknown category should return False."""
assert is_local_runner("test", None) is False
class TestGetRunnerInfo:
"""Test get_runner_info function."""
def test_returns_dict_with_expected_keys(self):
"""Should return dict with name, url, and category keys."""
info = get_runner_info("my-runner", "http://localhost:3000")
assert "name" in info
assert "url" in info
assert "category" in info
def test_includes_correct_values(self):
"""Should include correct values in dict."""
info = get_runner_info("my-runner", "http://localhost:3000")
assert info["name"] == "my-runner"
assert info["url"] == "http://localhost:3000"
assert info["category"] == RunnerCategory.LOCAL
class TestExtractRunnerUrl:
"""Test extract_runner_url function."""
def test_dify_service_api_extracts_url(self):
"""Should extract base-url from dify-service-api config."""
runner = Mock()
runner.pipeline_config = {}
pipeline_config = {
"ai": {
"dify-service-api": {"base-url": "https://api.dify.ai"}
}
}
url = extract_runner_url("dify-service-api", runner, pipeline_config)
assert url == "https://api.dify.ai"
def test_n8n_service_api_extracts_url(self):
"""Should extract webhook-url from n8n-service-api config."""
runner = Mock()
runner.pipeline_config = {}
pipeline_config = {
"ai": {
"n8n-service-api": {"webhook-url": "https://my.n8n.cloud/webhook"}
}
}
url = extract_runner_url("n8n-service-api", runner, pipeline_config)
assert url == "https://my.n8n.cloud/webhook"
def test_coze_api_extracts_url(self):
"""Should extract api-base from coze-api config."""
runner = Mock()
runner.pipeline_config = {}
pipeline_config = {
"ai": {
"coze-api": {"api-base": "https://api.coze.com"}
}
}
url = extract_runner_url("coze-api", runner, pipeline_config)
assert url == "https://api.coze.com"
def test_langflow_api_extracts_url(self):
"""Should extract base-url from langflow-api config."""
runner = Mock()
runner.pipeline_config = {}
pipeline_config = {
"ai": {
"langflow-api": {"base-url": "https://cloud.langflow.ai"}
}
}
url = extract_runner_url("langflow-api", runner, pipeline_config)
assert url == "https://cloud.langflow.ai"
def test_unknown_runner_returns_none(self):
"""Unknown runner name should return None."""
runner = Mock()
runner.pipeline_config = {}
pipeline_config = {}
url = extract_runner_url("unknown-runner", runner, pipeline_config)
assert url is None
def test_none_runner_returns_none(self):
"""None runner should return None."""
url = extract_runner_url("test", None, {})
assert url is None
def test_runner_without_pipeline_config_returns_none(self):
"""Runner without pipeline_config attribute should return None."""
runner = Mock(spec=[]) # Empty spec means no attributes
url = extract_runner_url("test", runner, {})
assert url is None
def test_none_pipeline_config_returns_none(self):
"""None pipeline_config should return None."""
runner = Mock()
runner.pipeline_config = {}
url = extract_runner_url("dify-service-api", runner, None)
assert url is None
def test_missing_ai_config_returns_none(self):
"""Missing ai config should return None."""
runner = Mock()
runner.pipeline_config = {}
pipeline_config = {}
url = extract_runner_url("dify-service-api", runner, pipeline_config)
assert url is None
class TestGetRunnerCategoryFromRunner:
"""Test get_runner_category_from_runner function."""
def test_extracts_and_categorizes(self):
"""Should extract URL and return correct category."""
runner = Mock()
runner.pipeline_config = {}
pipeline_config = {
"ai": {
"dify-service-api": {"base-url": "https://api.dify.ai"}
}
}
category = get_runner_category_from_runner("dify-service-api", runner, pipeline_config)
assert category == RunnerCategory.CLOUD
def test_returns_unknown_for_missing_url(self):
"""Should return UNKNOWN when URL cannot be extracted."""
runner = Mock()
runner.pipeline_config = {}
category = get_runner_category_from_runner("unknown", runner, {})
assert category == RunnerCategory.UNKNOWN
class TestConstants:
"""Test that constants are properly defined."""
def test_runner_category_constants(self):
"""RunnerCategory should have LOCAL, CLOUD, UNKNOWN."""
assert RunnerCategory.LOCAL == "local"
assert RunnerCategory.CLOUD == "cloud"
assert RunnerCategory.UNKNOWN == "unknown"
def test_cloud_domains_not_empty(self):
"""CLOUD_DOMAINS should not be empty."""
assert len(CLOUD_DOMAINS) > 0
def test_local_patterns_not_empty(self):
"""LOCAL_PATTERNS should not be empty."""
assert len(LOCAL_PATTERNS) > 0
if __name__ == "__main__":
pytest.main([__file__, "-v"])

View File

@@ -0,0 +1,136 @@
"""
Unit tests for version utility functions.
Tests version comparison logic without network calls.
"""
from __future__ import annotations
from unittest.mock import Mock
from langbot.pkg.utils.version import VersionManager
class TestVersionComparison:
"""Tests for version comparison functions."""
def _create_version_manager(self):
"""Create a VersionManager with mock app."""
mock_app = Mock()
mock_app.proxy_mgr = Mock()
mock_app.proxy_mgr.get_forward_providers = Mock(return_value={})
mock_app.logger = Mock()
return VersionManager(mock_app)
def test_is_newer_same_version(self):
"""is_newer returns False for same version."""
vm = self._create_version_manager()
result = vm.is_newer('v1.0.0', 'v1.0.0')
assert result is False
def test_is_newer_different_major_version(self):
"""is_newer returns False for different major version."""
# Note: is_newer ignores major version changes
vm = self._create_version_manager()
result = vm.is_newer('v2.0.0', 'v1.0.0')
assert result is False
def test_is_newer_minor_update(self):
"""is_newer returns True for minor update within same major."""
vm = self._create_version_manager()
result = vm.is_newer('v1.1.0', 'v1.0.0')
assert result is True
def test_is_newer_patch_update(self):
"""is_newer returns True for patch update within same major."""
vm = self._create_version_manager()
result = vm.is_newer('v1.0.1', 'v1.0.0')
assert result is True
def test_is_newer_with_fourth_segment(self):
"""is_newer ignores fourth version segment."""
# Both have same first 3 segments
vm = self._create_version_manager()
result = vm.is_newer('v1.0.0.1', 'v1.0.0.0')
assert result is False
def test_is_newer_short_version(self):
"""is_newer handles short version numbers."""
vm = self._create_version_manager()
result = vm.is_newer('v1.0', 'v1.0')
assert result is False
def test_is_newer_older_version(self):
"""is_newer returns True when new > old."""
vm = self._create_version_manager()
result = vm.is_newer('v1.2.0', 'v1.1.0')
assert result is True
class TestCompareVersionStr:
"""Tests for compare_version_str static method."""
def test_compare_equal_versions(self):
"""Equal versions return 0."""
result = VersionManager.compare_version_str('v1.0.0', 'v1.0.0')
assert result == 0
def test_compare_without_v_prefix(self):
"""Versions without v prefix work the same."""
result = VersionManager.compare_version_str('1.0.0', '1.0.0')
assert result == 0
def test_compare_mixed_prefix(self):
"""Mixed v prefix works correctly."""
result = VersionManager.compare_version_str('v1.0.0', '1.0.0')
assert result == 0
def test_compare_first_greater(self):
"""First version greater returns 1."""
result = VersionManager.compare_version_str('v1.1.0', 'v1.0.0')
assert result == 1
def test_compare_first_smaller(self):
"""First version smaller returns -1."""
result = VersionManager.compare_version_str('v1.0.0', 'v1.1.0')
assert result == -1
def test_compare_different_lengths(self):
"""Different length versions are padded with zeros."""
result = VersionManager.compare_version_str('v1.0', 'v1.0.0')
assert result == 0
def test_compare_shorter_greater(self):
"""Shorter version padded, first still greater."""
result = VersionManager.compare_version_str('v1.1', 'v1.0.0')
assert result == 1
def test_compare_longer_greater(self):
"""Longer version, first smaller."""
result = VersionManager.compare_version_str('v1.0', 'v1.0.1')
assert result == -1
def test_compare_major_version(self):
"""Major version comparison."""
result = VersionManager.compare_version_str('v2.0.0', 'v1.9.9')
assert result == 1
def test_compare_minor_version(self):
"""Minor version comparison."""
result = VersionManager.compare_version_str('v1.5.0', 'v1.4.9')
assert result == 1
def test_compare_patch_version(self):
"""Patch version comparison."""
result = VersionManager.compare_version_str('v1.0.1', 'v1.0.0')
assert result == 1
def test_compare_four_segments(self):
"""Four segment version comparison."""
result = VersionManager.compare_version_str('v1.0.0.1', 'v1.0.0.0')
assert result == 1
def test_compare_long_versions(self):
"""Long version strings work correctly."""
result = VersionManager.compare_version_str('v1.2.3.4.5', 'v1.2.3.4.4')
assert result == 1