|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | +# Copyright (c) 2026 BitConcepts, LLC. All rights reserved. |
| 3 | +"""Sandbox lifecycle test: import existing project and walk lifecycle. |
| 4 | +
|
| 5 | +Creates a realistic project, imports it, then verifies governance files |
| 6 | +and phase tracking work correctly for imported projects. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from pathlib import Path |
| 12 | +from textwrap import dedent |
| 13 | + |
| 14 | +import yaml |
| 15 | +from click.testing import CliRunner |
| 16 | + |
| 17 | +from specsmith.cli import main |
| 18 | + |
| 19 | + |
| 20 | +def _create_test_project(root: Path) -> None: |
| 21 | + """Build a minimal Python project with no governance.""" |
| 22 | + (root / "pyproject.toml").write_text( |
| 23 | + dedent("""\ |
| 24 | + [build-system] |
| 25 | + requires = ["setuptools>=68.0"] |
| 26 | + build-backend = "setuptools.build_meta" |
| 27 | + [project] |
| 28 | + name = "importme" |
| 29 | + version = "1.0.0" |
| 30 | + requires-python = ">=3.10" |
| 31 | + dependencies = ["click>=8.1"] |
| 32 | + [project.scripts] |
| 33 | + importme = "importme.cli:main" |
| 34 | + [tool.setuptools.packages.find] |
| 35 | + where = ["src"] |
| 36 | + """), |
| 37 | + encoding="utf-8", |
| 38 | + ) |
| 39 | + src = root / "src" / "importme" |
| 40 | + src.mkdir(parents=True) |
| 41 | + (src / "__init__.py").write_text('__version__ = "1.0.0"\n', encoding="utf-8") |
| 42 | + (src / "cli.py").write_text( |
| 43 | + dedent("""\ |
| 44 | + import click |
| 45 | +
|
| 46 | + @click.command() |
| 47 | + def main(): |
| 48 | + click.echo("hello") |
| 49 | + """), |
| 50 | + encoding="utf-8", |
| 51 | + ) |
| 52 | + tests = root / "tests" |
| 53 | + tests.mkdir() |
| 54 | + (tests / "test_cli.py").write_text( |
| 55 | + dedent("""\ |
| 56 | + from importme.cli import main |
| 57 | + from click.testing import CliRunner |
| 58 | +
|
| 59 | + def test_main(): |
| 60 | + r = CliRunner().invoke(main) |
| 61 | + assert r.exit_code == 0 |
| 62 | + """), |
| 63 | + encoding="utf-8", |
| 64 | + ) |
| 65 | + (root / "README.md").write_text("# importme\nA test project.\n", encoding="utf-8") |
| 66 | + (root / ".gitignore").write_text("__pycache__/\n", encoding="utf-8") |
| 67 | + |
| 68 | + |
| 69 | +class TestLifecycleImport: |
| 70 | + """Import an existing project and verify lifecycle setup.""" |
| 71 | + |
| 72 | + def test_import_sets_inception_phase(self, tmp_path: Path) -> None: |
| 73 | + root = tmp_path / "importme" |
| 74 | + root.mkdir() |
| 75 | + _create_test_project(root) |
| 76 | + |
| 77 | + runner = CliRunner() |
| 78 | + r = runner.invoke(main, ["import", "--project-dir", str(root), "--yes"]) |
| 79 | + assert r.exit_code == 0, f"Import failed: {r.output}" |
| 80 | + |
| 81 | + # Phase should be inception |
| 82 | + assert (root / "scaffold.yml").exists() |
| 83 | + with open(root / "scaffold.yml") as f: |
| 84 | + cfg = yaml.safe_load(f) |
| 85 | + assert cfg.get("aee_phase") == "inception" |
| 86 | + |
| 87 | + def test_import_creates_governance_files(self, tmp_path: Path) -> None: |
| 88 | + root = tmp_path / "importme" |
| 89 | + root.mkdir() |
| 90 | + _create_test_project(root) |
| 91 | + |
| 92 | + runner = CliRunner() |
| 93 | + runner.invoke(main, ["import", "--project-dir", str(root), "--yes"]) |
| 94 | + |
| 95 | + # New governance files |
| 96 | + gov = root / "docs" / "governance" |
| 97 | + assert (gov / "SESSION-PROTOCOL.md").exists() |
| 98 | + assert (gov / "LIFECYCLE.md").exists() |
| 99 | + assert (gov / "RULES.md").exists() |
| 100 | + |
| 101 | + # Old names should NOT exist |
| 102 | + assert not (gov / "WORKFLOW.md").exists() |
| 103 | + assert not (root / "docs" / "WORKFLOW.md").exists() |
| 104 | + |
| 105 | + def test_import_then_phase_operations(self, tmp_path: Path) -> None: |
| 106 | + root = tmp_path / "importme" |
| 107 | + root.mkdir() |
| 108 | + _create_test_project(root) |
| 109 | + |
| 110 | + runner = CliRunner() |
| 111 | + runner.invoke(main, ["import", "--project-dir", str(root), "--yes"]) |
| 112 | + |
| 113 | + # Phase show |
| 114 | + r = runner.invoke(main, ["phase", "show", "--project-dir", str(root)]) |
| 115 | + assert r.exit_code == 0, f"phase show failed: {r.output}\n{r.exception}" |
| 116 | + assert "Inception" in r.output |
| 117 | + |
| 118 | + # Phase list |
| 119 | + r = runner.invoke(main, ["phase", "list", "--project-dir", str(root)]) |
| 120 | + assert r.exit_code == 0, f"phase list failed: {r.output}\n{r.exception}" |
| 121 | + assert "inception" in r.output |
| 122 | + |
| 123 | + # Advance with --force |
| 124 | + r = runner.invoke(main, ["phase", "next", "--force", "--project-dir", str(root)]) |
| 125 | + assert r.exit_code == 0 |
| 126 | + assert "Architecture" in r.output |
| 127 | + |
| 128 | + with open(root / "scaffold.yml") as f: |
| 129 | + cfg = yaml.safe_load(f) |
| 130 | + assert cfg["aee_phase"] == "architecture" |
| 131 | + |
| 132 | + def test_import_audit_includes_phase_readiness(self, tmp_path: Path) -> None: |
| 133 | + root = tmp_path / "importme" |
| 134 | + root.mkdir() |
| 135 | + _create_test_project(root) |
| 136 | + |
| 137 | + runner = CliRunner() |
| 138 | + runner.invoke(main, ["import", "--project-dir", str(root), "--yes"]) |
| 139 | + |
| 140 | + r = runner.invoke(main, ["audit", "--project-dir", str(root)]) |
| 141 | + # Audit may exit 1 (issues found) but must not crash |
| 142 | + assert r.exit_code in (0, 1), f"audit crashed: {r.exception}" |
| 143 | + # Audit should include phase readiness info |
| 144 | + assert "Phase" in r.output or "phase" in r.output |
0 commit comments