from __future__ import annotations import pytest from stewardship.bootstrap import BootstrapAnswers, apply_bootstrap_plan, build_bootstrap_plan from stewardship.validate import validate_repository def test_bootstrap_plan_writes_nothing(tmp_path): answers = BootstrapAnswers(name="new-proj", title="New project", owners=["team"], repository_type="project") plan = build_bootstrap_plan(answers) assert list(tmp_path.iterdir()) == [] assert plan["planHash"].startswith("sha256:") def test_bootstrap_apply_requires_matching_approval_hash(tmp_path): answers = BootstrapAnswers(name="new-proj", title="New project", owners=["team"], repository_type="project") plan = build_bootstrap_plan(answers) with pytest.raises(ValueError): apply_bootstrap_plan(tmp_path, plan, "sha256:not-the-real-hash") assert list(tmp_path.iterdir()) == [] def test_bootstrap_apply_writes_and_validates(tmp_path): import subprocess subprocess.run(["git", "init", "-q"], cwd=tmp_path, check=True) answers = BootstrapAnswers( name="new-proj", title="New project", owners=["team"], repository_type="project", lifecycle="active" ) plan = build_bootstrap_plan(answers) written = apply_bootstrap_plan(tmp_path, plan, plan["planHash"]) assert "ops/contract.yaml" in written assert (tmp_path / "AGENTS.md").exists() assert (tmp_path / ".claude/skills/repository-bootstrap/SKILL.md").exists() result = validate_repository(tmp_path) errors = [f for f in result.findings if f.severity == "error"] assert errors == [] def test_bootstrap_apply_refuses_to_overwrite(tmp_path): import subprocess subprocess.run(["git", "init", "-q"], cwd=tmp_path, check=True) (tmp_path / "AGENTS.md").write_text("existing", encoding="utf-8") answers = BootstrapAnswers(name="new-proj", title="New project", owners=["team"], repository_type="project") plan = build_bootstrap_plan(answers) with pytest.raises(FileExistsError): apply_bootstrap_plan(tmp_path, plan, plan["planHash"]) assert (tmp_path / "AGENTS.md").read_text(encoding="utf-8") == "existing"