| 1 | """Execute the raw reference recipe without root-only host substitutions.""" |
| 2 | |
| 3 | import json |
| 4 | import os |
| 5 | import re |
| 6 | import shlex |
| 7 | import shutil |
| 8 | import subprocess |
| 9 | import sys |
| 10 | |
| 11 | import pytest |
| 12 | |
| 13 | import last30days as cli |
| 14 | from tests.skill_contract import reference_text |
| 15 | |
| 16 | |
| 17 | @pytest.mark.parametrize("shell", ["bash", "zsh"]) |
| 18 | @pytest.mark.parametrize("ambient_arguments", [None, "WRONG TOPIC --emit=json --save-dir=/wrong"]) |
| 19 | @pytest.mark.parametrize("saving_enabled", [False, True]) |
| 20 | @pytest.mark.parametrize("with_plan", [False, True]) |
| 21 | def test_raw_research_recipe_preserves_the_topic_without_host_argument_expansion( |
| 22 | tmp_path, ambient_arguments, saving_enabled, with_plan, shell, |
| 23 | ): |
| 24 | if not shutil.which(shell, path=os.defpath): |
| 25 | pytest.skip(f"{shell} is not installed") |
| 26 | reference = reference_text("research-runbook") |
| 27 | commands = re.findall(r"```bash\n(.*?)\n```", reference, re.S) |
| 28 | marker = tmp_path / "topic-must-not-execute" |
| 29 | shell_marker = shlex.quote(str(marker)) |
| 30 | topic = f'McDonald\'s new tools $(touch {shell_marker}) `touch {shell_marker}` "$HOME"' |
| 31 | command = next( |
| 32 | block for block in commands if "{TOPIC}" in block and "--save-suffix=v3" in block |
| 33 | ).replace("{TOPIC}", topic) |
| 34 | plan = { |
| 35 | "subqueries": [{ |
| 36 | "search_query": topic, |
| 37 | "ranking_query": topic, |
| 38 | "sources": ["reddit"], |
| 39 | }], |
| 40 | } |
| 41 | if with_plan: |
| 42 | plan_command = next( |
| 43 | block for block in commands if "{QUERY_PLAN_JSON_FROM_STEP_0.75}" in block |
| 44 | ).replace("{QUERY_PLAN_JSON_FROM_STEP_0.75}", json.dumps(plan)) |
| 45 | command = command.rsplit("\n", 1)[0] + "\n" + plan_command |
| 46 | |
| 47 | installed = tmp_path / "installed skill" |
| 48 | (installed / "scripts").mkdir(parents=True) |
| 49 | (installed / "scripts" / "last30days.py").write_text("") |
| 50 | capture = tmp_path / "engine arguments.jsonl" |
| 51 | interpreter = tmp_path / "argument capture" |
| 52 | interpreter.write_text( |
| 53 | f"#!{sys.executable}\n" |
| 54 | "import json, sys\n" |
| 55 | "args = sys.argv[1:]\n" |
| 56 | "record = {'args': args, 'plan': json.load(sys.stdin) if '--plan' in args else None}\n" |
| 57 | f"with open({str(capture)!r}, 'a') as output:\n" |
| 58 | " output.write(json.dumps(record) + '\\n')\n" |
| 59 | ) |
| 60 | interpreter.chmod(0o755) |
| 61 | save_dir = str(tmp_path / "saved research") if saving_enabled else "" |
| 62 | env = { |
| 63 | "PATH": os.defpath, |
| 64 | "HOME": str(tmp_path / "home"), |
| 65 | "SKILL_DIR": str(installed), |
| 66 | "LAST30DAYS_PYTHON": str(interpreter), |
| 67 | "LAST30DAYS_MEMORY_DIR": save_dir, |
| 68 | } |
| 69 | if ambient_arguments is not None: |
| 70 | env["ARGUMENTS"] = ambient_arguments |
| 71 | result = subprocess.run( |
| 72 | [shell, "-euC", "-c", command], cwd=tmp_path, env=env, |
| 73 | text=True, capture_output=True, timeout=15, |
| 74 | ) |
| 75 | |
| 76 | assert result.returncode == 0, result.stderr |
| 77 | assert not marker.exists(), "topic text executed as shell code" |
| 78 | calls = [json.loads(line) for line in capture.read_text().splitlines()] |
| 79 | assert len(calls) == 1 |
| 80 | call = calls[0] |
| 81 | assert call["args"][0] == str(installed / "scripts" / "last30days.py") |
| 82 | parsed = cli.build_parser().parse_args(call["args"][1:]) |
| 83 | assert parsed.topic == [topic], "the full parsed topic must be one literal argument" |
| 84 | assert parsed.emit == "compact" |
| 85 | assert parsed.save_dir == save_dir |
| 86 | assert parsed.save_suffix == "v3" |
| 87 | assert parsed.plan == ("-" if with_plan else None) |
| 88 | assert call["plan"] == (plan if with_plan else None) |
| 89 |