muse-agent-example/tests/architecture/test_framework_port_purity.py

44 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""框架端口纯度门:framework 不得反向依赖 Muse 业务实现。"""
from __future__ import annotations
import pathlib
import re
import unittest
ROOT = next(
parent
for parent in (pathlib.Path(__file__).resolve().parent, *pathlib.Path(__file__).resolve().parents)
if (parent / "AGENTS.md").is_file() and (parent / ".git").exists()
)
FORBIDDEN_IMPORTS = (
"agent_trace",
"muse_role",
"muse_role_contract",
"muse_db",
"psycopg",
"fixed-opus",
)
class FrameworkPortPurityTest(unittest.TestCase):
def test_framework_does_not_import_muse_runtime_contracts(self) -> None:
offenders: list[str] = []
pattern = re.compile(
r"(?:from|import)\s+(?:" + "|".join(map(re.escape, FORBIDDEN_IMPORTS)) + r")\b"
)
for path in (ROOT / "framework").rglob("*"):
if not path.is_file() or "__pycache__" in path.parts:
continue
if path.suffix not in {".py", ".ts", ".md"}:
continue
text = path.read_text(encoding="utf-8")
if pattern.search(text):
offenders.append(path.relative_to(ROOT).as_posix())
self.assertEqual(offenders, [], "framework 反向依赖 Muse 业务合同或运行时:\n" + "\n".join(offenders))
if __name__ == "__main__":
unittest.main()