M6.1.3-R2 非五类兼容(阻断修复):DTO date 缺失时从 occurred_at 派生, 刻意不做时区换算以与 _infer_window 同一时钟;calendar_event 取 start_at、 creative_progress 取 project.updated_at;未知 kind 不再默认 bot_observed_activity; capture_hard_fact L4→L3;TYPE_CONTRACTS 覆盖全部 12 类,修复非五类 source_kind 兜底成 observed 污染本地记录;proactive_message 的 image_path 改为 has_image+ image_name(原样透传会被自身隐私校验整条静默丢弃);snapshot/daily_plan 透传 version。 M6.1.4 创作/媒体/主观分域:creative_progress 补 chunk 形参(生产调用点一直 TypeError 导致创作推进整步被阻断)并只留里程碑摘要;photo_generation 改为媒体索引 (原 payload 带 prompt 键与绝对路径,100% 静默丢弃);幂等键纳入隐私校验; 禁用键清单收窄,放行媒体标识键;两条 Bridge 路径统一送 body payload。 M6.1.5 Bot 专用检索 Profiles:按 Profile 分槽注入,固定槽位与条数、跨槽去重、 声明当前以本地运行态为准;旧版记忆插件回退通用检索。 M6.1.6 分层只读 API:新增 build_agenda_runtime_view 与 GET /agenda/runtime, 记忆插件停用时仍完整返回并标降级。 M6.1.7 隐私与残余收口:DTO 新增 contexts 禁用键;已删除娱乐功能(qzone/news/ web_exploration)的残余配置入口从展示面隐藏,旧值保留不删。 测试:174 项通过(pytest 与 unittest 双口径)。联合 E2E 135 项全部通过。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
102 行
3.2 KiB
Python
102 行
3.2 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""测试用 AstrBot 宿主打桩。
|
||
|
||
陪伴插件的大模块(atrelay / llm_tool_actions / command_handlers / private_image /
|
||
user_memory)在导入期就需要真实 AstrBot 宿主包。本模块让任意 `astrbot.*` 解析为
|
||
一个宽松的桩模块,从而可以在没有宿主的环境下对纯业务逻辑做定向测试。
|
||
被测业务逻辑本身不依赖这些桩的行为。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import importlib.abc
|
||
import importlib.machinery
|
||
import logging
|
||
import sys
|
||
import types
|
||
from typing import Any
|
||
|
||
|
||
class _StubClass:
|
||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||
pass
|
||
|
||
def __call__(self, *args: Any, **kwargs: Any) -> "_StubClass":
|
||
return self
|
||
|
||
def __getattr__(self, name: str) -> Any:
|
||
return _StubClass()
|
||
|
||
|
||
class _FilterStub:
|
||
def __getattr__(self, name: str) -> Any:
|
||
def decorator(*args: Any, **kwargs: Any) -> Any:
|
||
def wrap(func: Any) -> Any:
|
||
return func
|
||
|
||
if args and callable(args[0]):
|
||
return args[0]
|
||
return wrap
|
||
|
||
return decorator
|
||
|
||
|
||
def _stub_attribute(name: str) -> Any:
|
||
if name == "logger":
|
||
return logging.getLogger("astrbot.stub")
|
||
if name == "filter":
|
||
return _FilterStub()
|
||
if name == "AstrBotConfig":
|
||
return dict
|
||
if name == "register":
|
||
return lambda *args, **kwargs: (lambda cls: cls)
|
||
return _StubClass
|
||
|
||
|
||
class MagicModule(types.ModuleType):
|
||
__path__: list[str] = []
|
||
|
||
def __getattr__(self, name: str) -> Any:
|
||
if name.startswith("__"):
|
||
raise AttributeError(name)
|
||
value = _stub_attribute(name)
|
||
setattr(self, name, value)
|
||
return value
|
||
|
||
|
||
class _AstrbotStubFinder(importlib.abc.MetaPathFinder, importlib.abc.Loader):
|
||
def find_spec(self, fullname, path=None, target=None): # type: ignore[override]
|
||
if fullname == "astrbot" or fullname.startswith("astrbot."):
|
||
if fullname in sys.modules:
|
||
return None
|
||
return importlib.machinery.ModuleSpec(fullname, self, is_package=True)
|
||
return None
|
||
|
||
def create_module(self, spec): # type: ignore[override]
|
||
return MagicModule(spec.name)
|
||
|
||
def exec_module(self, module): # type: ignore[override]
|
||
pass
|
||
|
||
|
||
def _augment(module: types.ModuleType) -> None:
|
||
"""把别的测试模块预先放进 sys.modules 的极简桩升级成宽松桩,不替换对象。"""
|
||
if module is None or isinstance(module, MagicModule):
|
||
return
|
||
if not hasattr(module, "__path__"):
|
||
module.__path__ = [] # type: ignore[attr-defined]
|
||
if "__getattr__" not in module.__dict__:
|
||
def _module_getattr(name: str, _module: types.ModuleType = module) -> Any:
|
||
if name.startswith("__"):
|
||
raise AttributeError(name)
|
||
value = _stub_attribute(name)
|
||
setattr(_module, name, value)
|
||
return value
|
||
|
||
module.__dict__["__getattr__"] = _module_getattr
|
||
|
||
|
||
def install() -> None:
|
||
if not any(isinstance(finder, _AstrbotStubFinder) for finder in sys.meta_path):
|
||
sys.meta_path.insert(0, _AstrbotStubFinder())
|
||
for name in ("astrbot", "astrbot.api", "astrbot.core"):
|
||
_augment(sys.modules.get(name))
|