Files
MoviePilot/tests/test_rust_accel_toggle.py
freeman f3ad81b39e style: apply safe ruff auto-fixes and exempt startup late imports
Run `ruff check app tests scripts --fix` with the configured rule set
(E4/E7/E9/F/I): import sorting (I001), f-strings without placeholders
(F541), lambda/variable/unused-import cleanups that ruff marks safe.
Nine files whose only ruff debt sits next to pre-existing pylint
import-resolution errors (lazy exports, sys.path script imports) or
whose "unused" imports are reached through attribute access by tests are
left untouched so the changed-file pylint gate and the benchmark tests
stay green; their diagnostics remain in the baseline.

app/startup/lifecycle/__init__.py and app/main.py import business
modules after cache composition, import-path and stdio setup on purpose
(the source comments document the contract), so declare E402
per-file-ignores for them instead of adding per-line noqa.

Ruff ratchet watermark drops from 498 to 100; sync the baseline fixture
and the optimization checklist figure.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-09 13:29:09 +08:00

59 lines
2.0 KiB
Python

from app.adapters.system import rust as rust_accel
from app.runtime.config import settings
class _DummyRustExtension:
"""
测试用 Rust 扩展替身,用来验证总开关会阻止扩展调用。
"""
@staticmethod
def is_available() -> bool:
"""
模拟 Rust 扩展基础能力可用。
"""
return True
@staticmethod
def parse_filter_rule_fast(_expression: str) -> list:
"""
如果总开关关闭后仍调用扩展,测试应立即失败。
"""
raise AssertionError("Rust extension should not be called")
def test_rust_accel_runtime_switch_disables_fast_paths(monkeypatch):
"""
RUST_ACCEL 关闭时,即便扩展可用也应回退到 Python 路径。
"""
monkeypatch.setattr(settings, "RUST_ACCEL", False)
monkeypatch.setattr(rust_accel, "is_required", lambda: False)
monkeypatch.setattr(rust_accel, "_moviepilot_rust", _DummyRustExtension())
assert rust_accel.is_available()
assert not rust_accel.is_enabled()
assert rust_accel.parse_filter_rule("HDR") is None
def test_free_threaded_runtime_requires_rust_acceleration(monkeypatch):
"""free-threaded 运行时不能通过配置关闭 Rust 快路径。"""
monkeypatch.setattr(settings, "RUST_ACCEL", False)
monkeypatch.setattr(rust_accel, "is_required", lambda: True)
monkeypatch.setattr(rust_accel, "_moviepilot_rust", _DummyRustExtension())
assert rust_accel.is_config_enabled()
assert rust_accel.is_enabled()
assert rust_accel.status()["required"] is True
def test_rust_accel_status_reports_enabled_state(monkeypatch):
"""
状态接口应同时体现扩展可用性和配置开关后的实际启用状态。
"""
monkeypatch.setattr(settings, "RUST_ACCEL", True)
monkeypatch.setattr(rust_accel, "is_required", lambda: False)
monkeypatch.setattr(rust_accel, "_moviepilot_rust", _DummyRustExtension())
assert rust_accel.status()["available"] is True
assert rust_accel.status()["enabled"] is True