mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 09:20:22 +08:00
refactor: 将 adapter 层的计费/模型抓取/行为变体能力下沉到 core.api_format 注册表
- 新增 core/api_format/capabilities.py,统一注册计费模板、模型抓取、 total_input_context 计算和 provider behavior variant - 新增 core/usage_tokens.py,抽取 cache token 解析逻辑到 core 层 - handler adapter 移除各自的 compute_total_input_context / fetch_models / BILLING_TEMPLATE 覆盖,改为委托 core 注册表解析 - provider/behavior.py 改为薄封装,底层委托 core registry - 新增 tests/test_architecture_import_rules.py 架构导入约束测试 - 新增 tests/services/api_format/test_capabilities.py 能力注册表测试 Closes #207 Co-authored-by: AAEE86 <ppk0227@hotmail.com>
This commit is contained in:
78
tests/test_architecture_import_rules.py
Normal file
78
tests/test_architecture_import_rules.py
Normal file
@@ -0,0 +1,78 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class _Violation:
|
||||
file: Path
|
||||
line: int
|
||||
imported: str
|
||||
|
||||
|
||||
def _repo_root() -> Path:
|
||||
return Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def _iter_python_files(root: Path) -> list[Path]:
|
||||
files: list[Path] = []
|
||||
for path in root.rglob("*.py"):
|
||||
if "__pycache__" in path.parts:
|
||||
continue
|
||||
files.append(path)
|
||||
return files
|
||||
|
||||
|
||||
def _scan_imports(py_file: Path) -> list[tuple[int, str]]:
|
||||
"""返回 (lineno, module_name) 列表。"""
|
||||
text = py_file.read_text(encoding="utf-8").lstrip("\ufeff")
|
||||
tree = ast.parse(text, filename=str(py_file))
|
||||
|
||||
imports: list[tuple[int, str]] = []
|
||||
for node in ast.walk(tree):
|
||||
if isinstance(node, ast.Import):
|
||||
for alias in node.names:
|
||||
imports.append((int(getattr(node, "lineno", 0) or 0), str(alias.name)))
|
||||
elif isinstance(node, ast.ImportFrom):
|
||||
if node.level:
|
||||
continue
|
||||
if node.module:
|
||||
imports.append((int(getattr(node, "lineno", 0) or 0), str(node.module)))
|
||||
return imports
|
||||
|
||||
|
||||
def _scan_forbidden_imports(*, scope_dir: Path, forbidden_prefix: str) -> list[_Violation]:
|
||||
violations: list[_Violation] = []
|
||||
for py_file in _iter_python_files(scope_dir):
|
||||
for line, imported in _scan_imports(py_file):
|
||||
if imported == forbidden_prefix or imported.startswith(f"{forbidden_prefix}."):
|
||||
violations.append(_Violation(file=py_file, line=line, imported=imported))
|
||||
return violations
|
||||
|
||||
|
||||
def _format_violations(title: str, violations: list[_Violation]) -> str:
|
||||
lines = [title, ""]
|
||||
for v in sorted(violations, key=lambda x: (str(x.file), x.line, x.imported)):
|
||||
rel = v.file.resolve().relative_to(_repo_root())
|
||||
lines.append(f"- {rel}:{v.line} -> {v.imported}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def test_services_should_not_import_api() -> None:
|
||||
repo = _repo_root()
|
||||
violations = _scan_forbidden_imports(
|
||||
scope_dir=repo / "src" / "services",
|
||||
forbidden_prefix="src.api",
|
||||
)
|
||||
assert not violations, _format_violations("services 层禁止 import api 层:", violations)
|
||||
|
||||
|
||||
def test_core_should_not_import_services() -> None:
|
||||
repo = _repo_root()
|
||||
violations = _scan_forbidden_imports(
|
||||
scope_dir=repo / "src" / "core",
|
||||
forbidden_prefix="src.services",
|
||||
)
|
||||
assert not violations, _format_violations("core 层禁止 import services 层:", violations)
|
||||
Reference in New Issue
Block a user