Files
insistence 2a055ba648 feat: 新增插件系统 (#112)
* feat: 初始化插件系统

* refactor: 收口插件系统运行时重构

* perf: 优化插件系统类型提示

* fix&perf: 修复和优化插件系统

* fix: 修复gitignore规则误忽略插件文件的问题

* fix: 修复运行时插件根路径算错的问题

* fix: 加强插件发现和路由注册的防护措施

* revert: 回滚定时任务白名单

* fix: 移除未使用的应用路由注册探测

* revert: 恢复部分代码

* perf: 优化插件系统

* docs: 新增插件开发文档

* perf: 优化插件管理模块

* perf: 提升插件系统核心能力

* refactor: 重构生命周期 step runner

* fix: 修复lint错误

* test: 清理测试用例

* test: 调整测试目录名称

* fix: 修复前后端目录硬编码的问题

* fix: 修复插件系统安全性缺口

* refactor: 重新设计插件生命周期 Migration 事务与回滚

* perf: 优化插件系统边界问题

* refactor: 重构当前插件系统的依赖体系设计

* perf: 优化代码

* perf: 优化代码

* fix: 修复代码合并问题

* fix: 修复bug

* perf: 优化代码

* perf&fix: 优化代码和修复bug

* docs: 优化文档格式

* feat: 适配Vue2版本

* docs: 更新README文档

* fix: 修复ruff lint错误

* chore: 更新后端依赖文件
2026-07-28 20:35:18 +08:00

107 lines
3.8 KiB
Python

from pathlib import Path
from cli.runtime.plugin.service import CliPluginRuntimeService
from plugins.core.validation.dependencies import PluginDependencyChecker
from tests.cli.runtime.plugin.conftest import (
FakePluginRuntimeGateway,
FakeRuntimeEnvironment,
build_runtime,
build_runtime_with_gateway,
)
SHARED_CORE_RUNTIME_METHODS = (
'list_plugins',
'get_plugin_info_with_state',
'check_plugin',
'check_plugin_dependencies',
'precheck_plugin_operation',
'health_plugin',
'diagnose_plugin',
'generate_plugin_docs',
'plan_plugins',
'batch_plugins',
'install_plugin_dependencies',
'install_plugin',
'upgrade_plugin',
'set_plugin_enabled',
'uninstall_plugin',
'purge_plugin',
'list_plugin_migrations',
'mark_plugin_migration_success',
'mark_plugin_migration_failed',
'get_plugin_config',
'export_plugin_config',
'import_plugin_config',
'set_plugin_config',
)
def test_plugin_runtime_core_runtime_initializes_without_getattr_recursion(tmp_path: Path) -> None:
"""校验 CLI 插件运行时延迟初始化 core runtime 时不会触发 __getattr__ 递归。"""
backend_root = tmp_path / 'ruoyi-fastapi-backend'
backend_root.mkdir()
runtime = build_runtime(backend_root)
core_runtime = runtime.core_runtime
assert core_runtime is runtime.core_runtime
assert core_runtime is not runtime
def test_plugin_runtime_exposes_only_cli_workflow_surface(tmp_path: Path) -> None:
"""校验 CLI 运行时只暴露命令工作流所需接口。"""
backend_root = tmp_path / 'ruoyi-fastapi-backend'
backend_root.mkdir()
runtime = build_runtime(backend_root)
runtime.core_runtime.dynamic_core_only = 'hidden'
assert not hasattr(runtime, '_missing_internal')
assert not hasattr(runtime, 'dynamic_core_only')
assert [method for method in SHARED_CORE_RUNTIME_METHODS if hasattr(runtime, method)] == []
assert all(
hasattr(runtime, method)
for method in (
'create_plugin',
'test_plugin',
'lock_plugin_dependencies',
'generate_plugin_dependency_allowlist_example',
)
)
def test_plugin_runtime_uses_core_environment_by_default() -> None:
"""校验 CLI 插件运行时默认使用 core 插件运行时环境。"""
runtime = CliPluginRuntimeService()
core_runtime = runtime.core_runtime
assert core_runtime.dependencies.runtime_environment is runtime.dependencies.runtime_environment
assert hasattr(core_runtime.dependencies.runtime_environment, 'get_frontend_mode')
def test_plugin_runtime_dependencies_are_exposed_through_container(tmp_path: Path) -> None:
"""校验 CLI 插件运行时通过集中依赖容器暴露运行时依赖。"""
backend_root = tmp_path / 'ruoyi-fastapi-backend'
backend_root.mkdir()
runtime_gateway = FakePluginRuntimeGateway()
runtime = build_runtime_with_gateway(backend_root, runtime_gateway)
assert runtime.dependencies.runtime_environment.get_backend_dir() == str(backend_root)
assert runtime.dependencies.management_gateway is runtime_gateway
assert runtime.dependencies.model_gateway is runtime_gateway
assert runtime.dependencies.command_gateway is runtime_gateway
def test_plugin_runtime_passes_lifecycle_lock_to_core_runtime(tmp_path: Path) -> None:
"""校验 CLI 创建核心插件运行时时会显式注入生命周期锁。"""
backend_root = tmp_path / 'ruoyi-fastapi-backend'
backend_root.mkdir()
lifecycle_lock = object()
runtime = CliPluginRuntimeService(
runtime_environment=FakeRuntimeEnvironment(backend_root),
dependency_checker=PluginDependencyChecker(),
lifecycle_lock=lifecycle_lock,
)
assert runtime.core_runtime.lifecycle_lock is lifecycle_lock