mirror of
https://github.com/langbot-app/LangBot.git
synced 2025-11-25 03:15:06 +08:00
* feat: add comprehensive unit tests for pipeline stages * fix: deps install in ci * ci: use venv * ci: run run_tests.sh * fix: resolve circular import issues in pipeline tests Update all test files to use lazy imports via importlib.import_module() to avoid circular dependency errors. Fix mock_conversation fixture to properly mock list.copy() method. Changes: - Use lazy import pattern in all test files - Fix conftest.py fixture for conversation messages - Add integration test file for full import tests - Update documentation with known issues and workarounds Tests now successfully avoid circular import errors while maintaining full test coverage of pipeline stages. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * docs: add comprehensive testing summary Document implementation details, challenges, solutions, and future improvements for the pipeline unit test suite. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * refactor: rewrite unit tests to test actual pipeline stage code Rewrote unit tests to properly test real stage implementations instead of mock logic: - Test actual BanSessionCheckStage with 7 test cases (100% coverage) - Test actual RateLimit stage with 3 test cases (70% coverage) - Test actual PipelineManager with 5 test cases - Use lazy imports via import_module to avoid circular dependencies - Import pipelinemgr first to ensure proper stage registration - Use Query.model_construct() to bypass Pydantic validation in tests - Remove obsolete pure unit tests that didn't test real code - All 20 tests passing with 48% overall pipeline coverage 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * test: add unit tests for GroupRespondRuleCheckStage Added comprehensive unit tests for resprule stage: - Test person message skips rule check - Test group message with no matching rules (INTERRUPT) - Test group message with matching rule (CONTINUE) - Test AtBotRule removes At component correctly - Test AtBotRule when no At component present Coverage: 100% on resprule.py and atbot.py All 25 tests passing with 51% overall pipeline coverage 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * refactor: restructure tests to tests/unit_tests/pipeline Reorganized test directory structure to support multiple test categories: - Move tests/pipeline → tests/unit_tests/pipeline - Rename .github/workflows/pipeline-tests.yml → run-tests.yml - Update run_tests.sh to run all unit tests (not just pipeline) - Update workflow to trigger on all pkg/** and tests/** changes - Coverage now tracks entire pkg/ module instead of just pipeline This structure allows for easy addition of more unit tests for other modules in the future. All 25 tests passing with 21% overall pkg coverage. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * ci: upload codecov report * ci: codecov file * ci: coverage.xml --------- Co-authored-by: Claude <noreply@anthropic.com>
110 lines
3.7 KiB
Python
110 lines
3.7 KiB
Python
"""
|
|
RateLimit stage unit tests
|
|
|
|
Tests the actual RateLimit implementation from pkg.pipeline.ratelimit
|
|
"""
|
|
|
|
import pytest
|
|
from unittest.mock import AsyncMock, Mock, patch
|
|
from importlib import import_module
|
|
import langbot_plugin.api.entities.builtin.provider.session as provider_session
|
|
|
|
|
|
def get_modules():
|
|
"""Lazy import to ensure proper initialization order"""
|
|
# Import pipelinemgr first to trigger proper stage registration
|
|
pipelinemgr = import_module('pkg.pipeline.pipelinemgr')
|
|
ratelimit = import_module('pkg.pipeline.ratelimit.ratelimit')
|
|
entities = import_module('pkg.pipeline.entities')
|
|
algo_module = import_module('pkg.pipeline.ratelimit.algo')
|
|
return ratelimit, entities, algo_module
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_require_access_allowed(mock_app, sample_query):
|
|
"""Test RequireRateLimitOccupancy allows access when rate limit is not exceeded"""
|
|
ratelimit, entities, algo_module = get_modules()
|
|
|
|
sample_query.launcher_type = provider_session.LauncherTypes.PERSON
|
|
sample_query.launcher_id = '12345'
|
|
sample_query.pipeline_config = {}
|
|
|
|
# Create mock algorithm that allows access
|
|
mock_algo = Mock(spec=algo_module.ReteLimitAlgo)
|
|
mock_algo.require_access = AsyncMock(return_value=True)
|
|
mock_algo.initialize = AsyncMock()
|
|
|
|
stage = ratelimit.RateLimit(mock_app)
|
|
|
|
# Patch the algorithm selection to use our mock
|
|
with patch.object(algo_module, 'preregistered_algos', []):
|
|
stage.algo = mock_algo
|
|
|
|
result = await stage.process(sample_query, 'RequireRateLimitOccupancy')
|
|
|
|
assert result.result_type == entities.ResultType.CONTINUE
|
|
assert result.new_query == sample_query
|
|
mock_algo.require_access.assert_called_once_with(
|
|
sample_query,
|
|
'person',
|
|
'12345'
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_require_access_denied(mock_app, sample_query):
|
|
"""Test RequireRateLimitOccupancy denies access when rate limit is exceeded"""
|
|
ratelimit, entities, algo_module = get_modules()
|
|
|
|
sample_query.launcher_type = provider_session.LauncherTypes.PERSON
|
|
sample_query.launcher_id = '12345'
|
|
sample_query.pipeline_config = {}
|
|
|
|
# Create mock algorithm that denies access
|
|
mock_algo = Mock(spec=algo_module.ReteLimitAlgo)
|
|
mock_algo.require_access = AsyncMock(return_value=False)
|
|
mock_algo.initialize = AsyncMock()
|
|
|
|
stage = ratelimit.RateLimit(mock_app)
|
|
|
|
# Patch the algorithm selection to use our mock
|
|
with patch.object(algo_module, 'preregistered_algos', []):
|
|
stage.algo = mock_algo
|
|
|
|
result = await stage.process(sample_query, 'RequireRateLimitOccupancy')
|
|
|
|
assert result.result_type == entities.ResultType.INTERRUPT
|
|
assert result.user_notice != ''
|
|
mock_algo.require_access.assert_called_once()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_release_access(mock_app, sample_query):
|
|
"""Test ReleaseRateLimitOccupancy releases rate limit occupancy"""
|
|
ratelimit, entities, algo_module = get_modules()
|
|
|
|
sample_query.launcher_type = provider_session.LauncherTypes.PERSON
|
|
sample_query.launcher_id = '12345'
|
|
sample_query.pipeline_config = {}
|
|
|
|
# Create mock algorithm
|
|
mock_algo = Mock(spec=algo_module.ReteLimitAlgo)
|
|
mock_algo.release_access = AsyncMock()
|
|
mock_algo.initialize = AsyncMock()
|
|
|
|
stage = ratelimit.RateLimit(mock_app)
|
|
|
|
# Patch the algorithm selection to use our mock
|
|
with patch.object(algo_module, 'preregistered_algos', []):
|
|
stage.algo = mock_algo
|
|
|
|
result = await stage.process(sample_query, 'ReleaseRateLimitOccupancy')
|
|
|
|
assert result.result_type == entities.ResultType.CONTINUE
|
|
assert result.new_query == sample_query
|
|
mock_algo.release_access.assert_called_once_with(
|
|
sample_query,
|
|
'person',
|
|
'12345'
|
|
)
|