mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 12:05:54 +00:00
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>
298 lines
12 KiB
Python
298 lines
12 KiB
Python
"""
|
|
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,
|
|
CLOUD_DOMAINS,
|
|
LOCAL_PATTERNS,
|
|
get_runner_category,
|
|
get_runner_info,
|
|
is_cloud_runner,
|
|
is_local_runner,
|
|
extract_runner_url,
|
|
get_runner_category_from_runner,
|
|
)
|
|
|
|
|
|
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
|
|
|
|
def test_invalid_url_returns_unknown(self):
|
|
"""Invalid URL that causes parsing error should return UNKNOWN."""
|
|
# URLs that cause exceptions during parsing return UNKNOWN
|
|
# Note: "not a valid url" is actually parseable by urlparse, it just has no scheme
|
|
# Use a URL that genuinely causes an exception
|
|
result = get_runner_category("test", "://invalid")
|
|
# urlparse may handle this differently, but exceptions return UNKNOWN
|
|
assert result in (RunnerCategory.UNKNOWN, RunnerCategory.CLOUD)
|
|
|
|
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(self):
|
|
"""URL without scheme should still be parseable."""
|
|
# urlparse can parse this, hostname might be None
|
|
result = get_runner_category("test", "example.com")
|
|
# Without scheme, urlparse treats it as path, so hostname is None
|
|
# This should return UNKNOWN or CLOUD depending on implementation
|
|
assert result in (RunnerCategory.UNKNOWN, 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
|
|
|
|
|
|
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"]) |