180 lines
6.1 KiB
Python
180 lines
6.1 KiB
Python
"""Gauss adaptation agent answers with a concrete change and preserves local edits."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
from pathlib import Path
|
|
|
|
|
|
def _load_tool():
|
|
path = (
|
|
Path(__file__).resolve().parents[1]
|
|
/ "app"
|
|
/ "gateway"
|
|
/ "routers"
|
|
/ "_gauss_data_adaptation_seed_assets"
|
|
/ "skills"
|
|
/ "gauss-data-adaptation"
|
|
/ "scripts"
|
|
/ "print_gauss_adaptation.py"
|
|
)
|
|
spec = importlib.util.spec_from_file_location("gauss_data_adaptation", path)
|
|
assert spec and spec.loader
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
def _drivers(**overrides: bool) -> dict[str, bool]:
|
|
found = {
|
|
"asyncpg": True,
|
|
"opengauss_sqlalchemy": True,
|
|
"asyncmy": True,
|
|
"aiomysql": False,
|
|
}
|
|
found.update(overrides)
|
|
return found
|
|
|
|
|
|
def test_soul_answers_from_the_error_before_a_tutorial() -> None:
|
|
from app.gateway.routers import _gauss_data_adaptation_seed as seed
|
|
|
|
soul = (seed._ASSETS_DIR / seed.GAUSS_DATA_ADAPTATION_AGENT_ID / "SOUL.md").read_text(encoding="utf-8")
|
|
guide = (seed._ASSETS_DIR / "skills" / seed.GAUSS_DATA_ADAPTATION_SKILL_NAME / "references" / "adaptation-map.md").read_text(encoding="utf-8")
|
|
assert soul.index("报错原文已经能对应") < soul.index("先确认模式")
|
|
assert "要改的地方" in soul
|
|
assert "先不要改" in soul
|
|
assert "不要先给安装教程" in soul
|
|
assert "Automatic ORM schema creation failed" in soul
|
|
for phrase in (
|
|
"asyncpg is not installed",
|
|
"322",
|
|
"in-memory fallback",
|
|
"current_schema()",
|
|
"VARCHAR(n)",
|
|
"standard_conforming_strings",
|
|
"UV_EXTRAS=gauss",
|
|
"DEERFLOW_DATABASE_PRODUCT=gauss_mysql",
|
|
seed_prohibition(),
|
|
"通用问答",
|
|
"智能体",
|
|
):
|
|
assert phrase in guide
|
|
|
|
|
|
def seed_prohibition() -> str:
|
|
return _load_tool().PROHIBITION
|
|
|
|
|
|
def test_seed_is_idempotent_and_preserves_local_edits(tmp_path, monkeypatch) -> None:
|
|
from app.gateway.routers import _gauss_data_adaptation_seed as seed
|
|
|
|
class FakePaths:
|
|
def agent_dir(self, agent_id: str) -> Path:
|
|
return tmp_path / "agents" / agent_id
|
|
|
|
monkeypatch.setattr(seed, "get_paths", lambda: FakePaths())
|
|
monkeypatch.setattr(seed, "_ensure_skill", lambda: False)
|
|
|
|
assert seed.ensure_gauss_data_adaptation_agent() is True
|
|
soul = tmp_path / "agents" / seed.GAUSS_DATA_ADAPTATION_AGENT_ID / "SOUL.md"
|
|
soul.write_text("现场调整", encoding="utf-8")
|
|
|
|
assert seed.ensure_gauss_data_adaptation_agent() is False
|
|
assert soul.read_text(encoding="utf-8") == "现场调整"
|
|
|
|
|
|
def test_native_gauss_redacts_secret_and_flags_postgres_checkpointer() -> None:
|
|
tool = _load_tool()
|
|
printed = "\n".join(
|
|
tool.classify(
|
|
{
|
|
"backend": "gauss",
|
|
"gauss_url": "opengauss+asyncpg://user:s3cret@10.0.0.1:26000/deerflow",
|
|
},
|
|
{"type": "postgres", "connection_string": "postgresql://user:s3cret@10.0.0.1:26000/deerflow"},
|
|
"",
|
|
_drivers(),
|
|
)
|
|
)
|
|
assert "判定模式:原生 openGauss" in printed
|
|
assert "10.0.0.1:26000/deerflow" in printed
|
|
assert "s3cret" not in printed
|
|
assert "user:" not in printed
|
|
assert tool.PROHIBITION in printed
|
|
assert "checkpointer.type 改为 sqlite" in printed
|
|
|
|
|
|
def test_script_does_not_read_the_url_environment_variable(tmp_path, monkeypatch, capsys) -> None:
|
|
tool = _load_tool()
|
|
config = tmp_path / "config.yaml"
|
|
config.write_text(
|
|
"\n".join(
|
|
[
|
|
"database:",
|
|
" backend: gauss",
|
|
" gauss_url: $GAUSS_DATABASE_URL",
|
|
"checkpointer:",
|
|
" type: sqlite",
|
|
" connection_string: .deer-flow/data/checkpoints.db",
|
|
" # gauss_url: opengauss://user:hidden@10.9.9.9/db",
|
|
]
|
|
)
|
|
+ "\n",
|
|
encoding="utf-8",
|
|
)
|
|
monkeypatch.setenv("GAUSS_DATABASE_URL", "opengauss://user:supersecret@10.1.1.1/db")
|
|
monkeypatch.delenv("DEERFLOW_DATABASE_PRODUCT", raising=False)
|
|
monkeypatch.setattr(tool, "config_candidates", lambda: [config])
|
|
monkeypatch.setattr(tool, "driver_status", lambda: _drivers())
|
|
tool.main()
|
|
printed = capsys.readouterr().out
|
|
assert "环境变量 GAUSS_DATABASE_URL(不读取变量值)" in printed
|
|
assert "supersecret" not in printed
|
|
assert "hidden" not in printed
|
|
assert "10.1.1.1" not in printed
|
|
assert ".deer-flow/data/checkpoints.db" in printed
|
|
assert "配置层面没有必须修改的项" in printed
|
|
|
|
|
|
def test_dolphin_and_conflict_modes_name_the_change() -> None:
|
|
tool = _load_tool()
|
|
dolphin = "\n".join(
|
|
tool.classify(
|
|
{"backend": "mysql", "mysql_url": "mysql://user:s3cret@10.0.0.8:3306/deerflow"},
|
|
{"type": "sqlite", "connection_string": ".deer-flow/data/checkpoints.db"},
|
|
"gauss_mysql",
|
|
_drivers(asyncmy=False, aiomysql=False),
|
|
)
|
|
)
|
|
assert "判定模式:Dolphin" in dolphin
|
|
assert "s3cret" not in dolphin
|
|
assert ".deer-flow/python-vendor" in dolphin
|
|
assert "SHOW CREATE TABLE" in dolphin
|
|
|
|
conflict = "\n".join(
|
|
tool.classify(
|
|
{"backend": "gauss", "gauss_url": "$GAUSS_DATABASE_URL"},
|
|
{"type": "sqlite"},
|
|
" GAUSS_MYSQL ",
|
|
_drivers(),
|
|
)
|
|
)
|
|
assert "判定模式:冲突" in conflict
|
|
assert "database.backend 改回 mysql" in conflict
|
|
|
|
|
|
def test_missing_native_driver_points_at_the_offline_image() -> None:
|
|
tool = _load_tool()
|
|
printed = "\n".join(
|
|
tool.classify(
|
|
{"backend": "gauss", "gauss_url": "$GAUSS_DATABASE_URL"},
|
|
{"type": "sqlite", "connection_string": ".deer-flow/data/checkpoints.db"},
|
|
"",
|
|
_drivers(asyncpg=False, opengauss_sqlalchemy=False),
|
|
)
|
|
)
|
|
assert "UV_EXTRAS=gauss" in printed
|
|
assert "不要在内网访问公网安装" in printed
|
|
assert "判定模式:原生 openGauss" in printed
|