|
| 1 | +"""Tests that update-agent-context.sh/.ps1 prefer feature.json over mtime.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import os |
| 7 | +import shutil |
| 8 | +import subprocess |
| 9 | +import time |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from tests.conftest import requires_bash |
| 15 | + |
| 16 | +PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent |
| 17 | +UPDATE_AGENT_CTX_SH = ( |
| 18 | + PROJECT_ROOT / "extensions" / "agent-context" / "scripts" / "bash" / "update-agent-context.sh" |
| 19 | +) |
| 20 | +UPDATE_AGENT_CTX_PS = ( |
| 21 | + PROJECT_ROOT / "extensions" / "agent-context" / "scripts" / "powershell" / "update-agent-context.ps1" |
| 22 | +) |
| 23 | + |
| 24 | +HAS_PWSH = shutil.which("pwsh") is not None |
| 25 | +_WINDOWS_POWERSHELL = (shutil.which("powershell.exe") or shutil.which("powershell")) if os.name == "nt" else None |
| 26 | + |
| 27 | + |
| 28 | +def _setup_project(root: Path, context_file: str = "CLAUDE.md") -> None: |
| 29 | + """Write the minimal agent-context extension config.""" |
| 30 | + cfg_dir = root / ".specify" / "extensions" / "agent-context" |
| 31 | + cfg_dir.mkdir(parents=True, exist_ok=True) |
| 32 | + (cfg_dir / "agent-context-config.yml").write_text( |
| 33 | + f"context_file: {context_file}\n" |
| 34 | + "context_markers:\n" |
| 35 | + " start: '<!-- SPECKIT START -->'\n" |
| 36 | + " end: '<!-- SPECKIT END -->'\n", |
| 37 | + encoding="utf-8", |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def _write_feature_json(root: Path, feature_directory: str) -> None: |
| 42 | + specify_dir = root / ".specify" |
| 43 | + specify_dir.mkdir(parents=True, exist_ok=True) |
| 44 | + (specify_dir / "feature.json").write_text( |
| 45 | + json.dumps({"feature_directory": feature_directory}), |
| 46 | + encoding="utf-8", |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def _make_plan(root: Path, feature_dir: str, content: str = "# plan\n") -> Path: |
| 51 | + p = root / feature_dir / "plan.md" |
| 52 | + p.parent.mkdir(parents=True, exist_ok=True) |
| 53 | + p.write_text(content, encoding="utf-8") |
| 54 | + return p |
| 55 | + |
| 56 | + |
| 57 | +@requires_bash |
| 58 | +def test_bash_uses_feature_json_when_plan_exists(tmp_path: Path) -> None: |
| 59 | + """feature.json points to the active feature; that plan.md is injected.""" |
| 60 | + _setup_project(tmp_path) |
| 61 | + _make_plan(tmp_path, "specs/001-active") |
| 62 | + _write_feature_json(tmp_path, "specs/001-active") |
| 63 | + |
| 64 | + result = subprocess.run( |
| 65 | + ["bash", str(UPDATE_AGENT_CTX_SH)], |
| 66 | + cwd=tmp_path, |
| 67 | + capture_output=True, |
| 68 | + text=True, |
| 69 | + check=False, |
| 70 | + ) |
| 71 | + assert result.returncode == 0, result.stderr + result.stdout |
| 72 | + ctx = (tmp_path / "CLAUDE.md").read_text(encoding="utf-8") |
| 73 | + assert "specs/001-active/plan.md" in ctx |
| 74 | + |
| 75 | + |
| 76 | +@requires_bash |
| 77 | +def test_bash_ignores_newer_stale_plan_when_feature_json_present(tmp_path: Path) -> None: |
| 78 | + """An older spec's plan.md modified more recently must NOT win over feature.json.""" |
| 79 | + _setup_project(tmp_path) |
| 80 | + |
| 81 | + # Create active feature plan first, then touch the stale one to make it newer. |
| 82 | + _make_plan(tmp_path, "specs/001-active") |
| 83 | + time.sleep(0.05) |
| 84 | + _make_plan(tmp_path, "specs/000-stale") |
| 85 | + |
| 86 | + _write_feature_json(tmp_path, "specs/001-active") |
| 87 | + |
| 88 | + result = subprocess.run( |
| 89 | + ["bash", str(UPDATE_AGENT_CTX_SH)], |
| 90 | + cwd=tmp_path, |
| 91 | + capture_output=True, |
| 92 | + text=True, |
| 93 | + check=False, |
| 94 | + ) |
| 95 | + assert result.returncode == 0, result.stderr + result.stdout |
| 96 | + ctx = (tmp_path / "CLAUDE.md").read_text(encoding="utf-8") |
| 97 | + assert "specs/001-active/plan.md" in ctx |
| 98 | + assert "specs/000-stale/plan.md" not in ctx |
| 99 | + |
| 100 | + |
| 101 | +@requires_bash |
| 102 | +def test_bash_falls_back_to_mtime_when_feature_json_absent(tmp_path: Path) -> None: |
| 103 | + """No feature.json → mtime fallback selects the most recently modified plan.""" |
| 104 | + _setup_project(tmp_path) |
| 105 | + _make_plan(tmp_path, "specs/000-old") |
| 106 | + time.sleep(0.05) |
| 107 | + _make_plan(tmp_path, "specs/001-newer") |
| 108 | + |
| 109 | + result = subprocess.run( |
| 110 | + ["bash", str(UPDATE_AGENT_CTX_SH)], |
| 111 | + cwd=tmp_path, |
| 112 | + capture_output=True, |
| 113 | + text=True, |
| 114 | + check=False, |
| 115 | + ) |
| 116 | + assert result.returncode == 0, result.stderr + result.stdout |
| 117 | + ctx = (tmp_path / "CLAUDE.md").read_text(encoding="utf-8") |
| 118 | + assert "specs/001-newer/plan.md" in ctx |
| 119 | + |
| 120 | + |
| 121 | +@requires_bash |
| 122 | +def test_bash_falls_back_to_mtime_when_plan_not_yet_created(tmp_path: Path) -> None: |
| 123 | + """feature.json exists but plan.md not yet written → fall back to mtime.""" |
| 124 | + _setup_project(tmp_path) |
| 125 | + _make_plan(tmp_path, "specs/000-old") |
| 126 | + # feature.json points to 001-new but its plan.md doesn't exist yet |
| 127 | + _write_feature_json(tmp_path, "specs/001-new") |
| 128 | + |
| 129 | + result = subprocess.run( |
| 130 | + ["bash", str(UPDATE_AGENT_CTX_SH)], |
| 131 | + cwd=tmp_path, |
| 132 | + capture_output=True, |
| 133 | + text=True, |
| 134 | + check=False, |
| 135 | + ) |
| 136 | + assert result.returncode == 0, result.stderr + result.stdout |
| 137 | + ctx = (tmp_path / "CLAUDE.md").read_text(encoding="utf-8") |
| 138 | + assert "specs/000-old/plan.md" in ctx |
| 139 | + |
| 140 | + |
| 141 | +@pytest.mark.skipif(not (HAS_PWSH or _WINDOWS_POWERSHELL), reason="no PowerShell available") |
| 142 | +def test_ps_uses_feature_json_when_plan_exists(tmp_path: Path) -> None: |
| 143 | + """PowerShell: feature.json points to the active feature; that plan is injected.""" |
| 144 | + _setup_project(tmp_path) |
| 145 | + _make_plan(tmp_path, "specs/001-active") |
| 146 | + _write_feature_json(tmp_path, "specs/001-active") |
| 147 | + |
| 148 | + exe = "pwsh" if HAS_PWSH else _WINDOWS_POWERSHELL |
| 149 | + result = subprocess.run( |
| 150 | + [exe, "-NoProfile", "-File", str(UPDATE_AGENT_CTX_PS)], |
| 151 | + cwd=tmp_path, |
| 152 | + capture_output=True, |
| 153 | + text=True, |
| 154 | + check=False, |
| 155 | + ) |
| 156 | + assert result.returncode == 0, result.stderr + result.stdout |
| 157 | + ctx = (tmp_path / "CLAUDE.md").read_text(encoding="utf-8") |
| 158 | + assert "specs/001-active/plan.md" in ctx |
| 159 | + |
| 160 | + |
| 161 | +@pytest.mark.skipif(not (HAS_PWSH or _WINDOWS_POWERSHELL), reason="no PowerShell available") |
| 162 | +def test_ps_ignores_newer_stale_plan_when_feature_json_present(tmp_path: Path) -> None: |
| 163 | + """PowerShell: stale plan touched more recently must not win over feature.json.""" |
| 164 | + _setup_project(tmp_path) |
| 165 | + _make_plan(tmp_path, "specs/001-active") |
| 166 | + time.sleep(0.05) |
| 167 | + _make_plan(tmp_path, "specs/000-stale") |
| 168 | + _write_feature_json(tmp_path, "specs/001-active") |
| 169 | + |
| 170 | + exe = "pwsh" if HAS_PWSH else _WINDOWS_POWERSHELL |
| 171 | + result = subprocess.run( |
| 172 | + [exe, "-NoProfile", "-File", str(UPDATE_AGENT_CTX_PS)], |
| 173 | + cwd=tmp_path, |
| 174 | + capture_output=True, |
| 175 | + text=True, |
| 176 | + check=False, |
| 177 | + ) |
| 178 | + assert result.returncode == 0, result.stderr + result.stdout |
| 179 | + ctx = (tmp_path / "CLAUDE.md").read_text(encoding="utf-8") |
| 180 | + assert "specs/001-active/plan.md" in ctx |
| 181 | + assert "specs/000-stale/plan.md" not in ctx |
0 commit comments