| 1 | """Execute the HTML reference commands with the runtime's skill directory.""" |
| 2 | |
| 3 | import os |
| 4 | from pathlib import Path |
| 5 | import re |
| 6 | import subprocess |
| 7 | import sys |
| 8 | |
| 9 | from tests.skill_contract import reference_text |
| 10 | |
| 11 | |
| 12 | ROOT = Path(__file__).resolve().parents[1] |
| 13 | SKILL = ROOT / "skills" / "last30days" |
| 14 | REFERENCE = SKILL / "references" / "save-html-brief.md" |
| 15 | |
| 16 | |
| 17 | def _command_environment(tmp_path): |
| 18 | installed_skill = tmp_path / "installed skills" / "last30days" |
| 19 | installed_skill.parent.mkdir() |
| 20 | installed_skill.symlink_to(SKILL, target_is_directory=True) |
| 21 | synthesis = tmp_path / "research synthesis.md" |
| 22 | synthesis.write_text("What I learned:\n\n**Path regression** works offline.\n") |
| 23 | output = tmp_path / "saved brief.html" |
| 24 | scratch = tmp_path / "synthesis drafts" |
| 25 | scratch.mkdir() |
| 26 | return { |
| 27 | "PATH": os.defpath, |
| 28 | "HOME": str(tmp_path / "home"), |
| 29 | "TMPDIR": str(scratch), |
| 30 | "LAST30DAYS_CONFIG_DIR": str(tmp_path / "config"), |
| 31 | "LAST30DAYS_CACHE_DIR": str(tmp_path / "cache"), |
| 32 | "LAST30DAYS_PYTHON": sys.executable, |
| 33 | "LAST30DAYS_SKIP_PREFLIGHT": "1", |
| 34 | "SKILL_DIR": str(installed_skill), |
| 35 | "TOPIC": "test topic", |
| 36 | "SYNTHESIS_FILE": str(synthesis), |
| 37 | "HTML_PATH": str(output), |
| 38 | "HTML_TMP": str(output), |
| 39 | }, output |
| 40 | |
| 41 | |
| 42 | def _engine_command(block): |
| 43 | start = block.index('"${LAST30DAYS_PYTHON}"') |
| 44 | lines = [] |
| 45 | for line in block[start:].splitlines(): |
| 46 | lines.append(line) |
| 47 | if not line.rstrip().endswith("\\"): |
| 48 | break |
| 49 | return "\n".join(lines) |
| 50 | |
| 51 | |
| 52 | def test_documented_local_export_uses_runtime_skill_directory(tmp_path): |
| 53 | blocks = re.findall(r"```bash\n(.*?)```", reference_text("save-html-brief"), re.DOTALL) |
| 54 | env, output = _command_environment(tmp_path) |
| 55 | initial = subprocess.run( |
| 56 | [sys.executable, str(SKILL / "scripts/last30days.py"), env["TOPIC"], |
| 57 | "--mock", "--quick", "--search=reddit", "--no-browser-cookies"], |
| 58 | cwd=tmp_path, env=env, capture_output=True, text=True, timeout=30, |
| 59 | ) |
| 60 | assert initial.returncode == 0, initial.stderr |
| 61 | command = _engine_command(blocks[0]) |
| 62 | result = subprocess.run( |
| 63 | [ |
| 64 | "bash", "-ec", |
| 65 | "SCOPE_FLAGS=(--mock --quick --search=reddit --no-browser-cookies)\n" |
| 66 | + command, |
| 67 | ], |
| 68 | cwd=tmp_path, |
| 69 | env=env, |
| 70 | capture_output=True, |
| 71 | text=True, |
| 72 | timeout=30, |
| 73 | ) |
| 74 | |
| 75 | assert result.returncode == 0, result.stderr |
| 76 | html = output.read_text() |
| 77 | assert html.lower().startswith("<!doctype html>") |
| 78 | assert "</html>" in html |
| 79 | assert "test topic" in html |
| 80 | assert "<strong>Path regression</strong> works offline." in html |
| 81 | |
| 82 | |
| 83 | def test_documented_publish_command_reaches_engine_parser(tmp_path): |
| 84 | blocks = re.findall(r"```bash\n(.*?)```", reference_text("save-html-brief"), re.DOTALL) |
| 85 | env, _ = _command_environment(tmp_path) |
| 86 | result = subprocess.run( |
| 87 | ["bash", "-ec", blocks[1].replace("--emit=html", "--help --emit=html", 1)], |
| 88 | cwd=tmp_path, |
| 89 | env=env, |
| 90 | capture_output=True, |
| 91 | text=True, |
| 92 | timeout=10, |
| 93 | ) |
| 94 | |
| 95 | assert result.returncode == 0, result.stderr |
| 96 | assert not list(Path(env["TMPDIR"]).iterdir()) |
| 97 |