AI coding agents can edit files, run shell commands, and touch infrastructure. One bad rm -rf, force-push, or docker compose down -v can wipe hours of work or production data.
| 1 | # SPDX-License-Identifier: MIT |
| 2 | from __future__ import annotations |
| 3 | |
| 4 | from pathlib import Path |
| 5 | |
| 6 | from agent_guard.config import ( |
| 7 | CONFIG_FILENAME, |
| 8 | build_init_config, |
| 9 | detect_project_signals, |
| 10 | dump_config, |
| 11 | load_config, |
| 12 | write_config, |
| 13 | ) |
| 14 | from agent_guard.models import PolicyMode |
| 15 | |
| 16 | |
| 17 | def test_detect_signals(tmp_path: Path) -> None: |
| 18 | (tmp_path / ".git").mkdir() |
| 19 | (tmp_path / "package.json").write_text("{}", encoding="utf-8") |
| 20 | (tmp_path / "docker-compose.yml").write_text( |
| 21 | "services:\n db:\n image: mongo\nvolumes:\n mongo_data:\n", |
| 22 | encoding="utf-8", |
| 23 | ) |
| 24 | signals = detect_project_signals(tmp_path) |
| 25 | assert signals["git"] |
| 26 | assert signals["node"] |
| 27 | assert signals["docker"] |
| 28 | assert signals["mongodb"] |
| 29 | |
| 30 | |
| 31 | def test_write_and_load(tmp_path: Path) -> None: |
| 32 | cfg = build_init_config(tmp_path) |
| 33 | path = tmp_path / CONFIG_FILENAME |
| 34 | write_config(path, cfg) |
| 35 | loaded = load_config(path) |
| 36 | assert loaded.version == 1 |
| 37 | assert "project_root" in dump_config(cfg) |
| 38 | assert str(tmp_path) not in dump_config(cfg) or dump_config(cfg).count(str(tmp_path)) == 0 |
| 39 | # Relative project_root |
| 40 | assert "project_root: ." in dump_config(cfg) |
| 41 | |
| 42 | |
| 43 | def test_default_config_without_file(tmp_path: Path, monkeypatch: object) -> None: |
| 44 | monkeypatch.chdir(tmp_path) # type: ignore[attr-defined] |
| 45 | cfg = load_config(project_root=tmp_path) |
| 46 | assert cfg.mode == PolicyMode.DEFAULT |