| 1 | """Execute the skill's save-directory setup against isolated configuration.""" |
| 2 | |
| 3 | from pathlib import Path |
| 4 | import re |
| 5 | import shlex |
| 6 | import shutil |
| 7 | import subprocess |
| 8 | import sys |
| 9 | |
| 10 | import pytest |
| 11 | |
| 12 | from tests.skill_contract import reference_text |
| 13 | |
| 14 | |
| 15 | ROOT = Path(__file__).resolve().parents[1] |
| 16 | SKILL = ROOT / "skills" / "last30days" |
| 17 | ENGINE = SKILL / "scripts" / "last30days.py" |
| 18 | BASH = shutil.which("bash") |
| 19 | |
| 20 | |
| 21 | @pytest.fixture |
| 22 | def shell_env(tmp_path): |
| 23 | home = tmp_path / "home" |
| 24 | home.mkdir() |
| 25 | config_dir = tmp_path / "config" |
| 26 | config_dir.mkdir() |
| 27 | bin_dir = tmp_path / "bin" |
| 28 | bin_dir.mkdir() |
| 29 | return { |
| 30 | "PATH": str(bin_dir), |
| 31 | "HOME": str(home), |
| 32 | "LAST30DAYS_PYTHON": sys.executable, |
| 33 | "SKILL_DIR": str(SKILL), |
| 34 | "LAST30DAYS_CONFIG_DIR": str(config_dir), |
| 35 | "LAST30DAYS_CACHE_DIR": str(tmp_path / "cache"), |
| 36 | } |
| 37 | |
| 38 | |
| 39 | def write_config(path, text): |
| 40 | path.parent.mkdir(parents=True, exist_ok=True) |
| 41 | path.write_text(text) |
| 42 | path.chmod(0o600) |
| 43 | |
| 44 | |
| 45 | @pytest.mark.parametrize( |
| 46 | ("global_value", "process_value", "expected"), |
| 47 | [ |
| 48 | ("/tmp/client research", None, "/tmp/client research"), |
| 49 | ("/tmp/global", "/tmp/process research", "/tmp/process research"), |
| 50 | ("/tmp/global", "", ""), |
| 51 | ("", None, ""), |
| 52 | (None, None, None), |
| 53 | ("/tmp/configured research", "${user_config.memory_dir}", "/tmp/configured research"), |
| 54 | ("", "${user_config.memory_dir}", ""), |
| 55 | (None, "${user_config.memory_dir}", None), |
| 56 | ("${user_config.memory_dir}", None, None), |
| 57 | ("${user_config.memory_dir}", "", ""), |
| 58 | ("/tmp/configured research", " ${user_config.memory_dir} ", "/tmp/configured research"), |
| 59 | (None, "/tmp/${user_config.memory_dir}/research", "/tmp/${user_config.memory_dir}/research"), |
| 60 | ], |
| 61 | ) |
| 62 | def test_documented_memory_resolution(global_value, process_value, expected, shell_env, tmp_path): |
| 63 | if expected is None: |
| 64 | expected = str(Path(shell_env["HOME"]) / "Documents" / "Last30Days") |
| 65 | if global_value is not None: |
| 66 | write_config( |
| 67 | Path(shell_env["LAST30DAYS_CONFIG_DIR"]) / ".env", |
| 68 | f'LAST30DAYS_MEMORY_DIR="{global_value}"\n', |
| 69 | ) |
| 70 | if process_value is not None: |
| 71 | shell_env["LAST30DAYS_MEMORY_DIR"] = process_value |
| 72 | assignments = re.findall(r"^LAST30DAYS_MEMORY_DIR=.*$", reference_text("runtime"), re.M) |
| 73 | assert assignments |
| 74 | for assignment in set(assignments): |
| 75 | result = subprocess.run( |
| 76 | [BASH, "-c", assignment + '\nprintf "%s" "$LAST30DAYS_MEMORY_DIR"'], |
| 77 | env=shell_env, cwd=tmp_path, text=True, capture_output=True, check=True, |
| 78 | ) |
| 79 | assert result.stdout == expected |
| 80 | |
| 81 | |
| 82 | @pytest.mark.parametrize("trust", [None, "1", "0", "global"]) |
| 83 | def test_resolver_honors_project_trust(shell_env, tmp_path, trust): |
| 84 | config_text = "LAST30DAYS_MEMORY_DIR=/tmp/global research\n" |
| 85 | if trust == "global": |
| 86 | config_text += "LAST30DAYS_TRUST_PROJECT_CONFIG=1\n" |
| 87 | elif trust is not None: |
| 88 | shell_env["LAST30DAYS_TRUST_PROJECT_CONFIG"] = trust |
| 89 | write_config(Path(shell_env["LAST30DAYS_CONFIG_DIR"]) / ".env", config_text) |
| 90 | write_config( |
| 91 | tmp_path / ".claude" / "last30days.env", |
| 92 | "LAST30DAYS_MEMORY_DIR=project research\nLAST30DAYS_TRUST_PROJECT_CONFIG=1\n", |
| 93 | ) |
| 94 | result = subprocess.run( |
| 95 | [sys.executable, str(ENGINE), "--resolve-save-dir"], |
| 96 | env=shell_env, cwd=tmp_path, text=True, capture_output=True, check=True, |
| 97 | ) |
| 98 | expected = str(tmp_path / "project research") if trust in {"1", "global"} else "/tmp/global research" |
| 99 | assert result.stdout == expected + "\n" |
| 100 | |
| 101 | |
| 102 | @pytest.mark.parametrize("project_value", ["project research", ""]) |
| 103 | def test_process_placeholder_uses_trusted_project_setting(shell_env, tmp_path, project_value): |
| 104 | write_config( |
| 105 | Path(shell_env["LAST30DAYS_CONFIG_DIR"]) / ".env", |
| 106 | "LAST30DAYS_MEMORY_DIR=/tmp/global research\nLAST30DAYS_TRUST_PROJECT_CONFIG=1\n", |
| 107 | ) |
| 108 | write_config(tmp_path / ".claude" / "last30days.env", f"LAST30DAYS_MEMORY_DIR={project_value}\n") |
| 109 | shell_env["LAST30DAYS_MEMORY_DIR"] = "${user_config.memory_dir}" |
| 110 | result = subprocess.run( |
| 111 | [sys.executable, str(ENGINE), "--resolve-save-dir"], |
| 112 | env=shell_env, cwd=tmp_path, text=True, capture_output=True, check=True, |
| 113 | ) |
| 114 | assert result.stdout == (str(tmp_path / project_value) if project_value else "") + "\n" |
| 115 | |
| 116 | |
| 117 | @pytest.mark.parametrize(("process_value", "global_value", "expected"), [ |
| 118 | (None, "/tmp/global research", "/tmp/global research"), |
| 119 | ("${user_config.memory_dir}", "/tmp/global research", "/tmp/global research"), |
| 120 | (None, "", ""), |
| 121 | ("${user_config.memory_dir}", "", ""), |
| 122 | ("", "/tmp/global research", ""), |
| 123 | ("/tmp/process research", "/tmp/global research", "/tmp/process research"), |
| 124 | (None, None, None), |
| 125 | (None, "${user_config.memory_dir}", None), |
| 126 | ("${user_config.memory_dir}", "${user_config.memory_dir}", None), |
| 127 | ]) |
| 128 | def test_project_placeholder_preserves_memory_precedence( |
| 129 | shell_env, tmp_path, process_value, global_value, expected, |
| 130 | ): |
| 131 | shell_env["HOME"] = str(tmp_path / "home") |
| 132 | config_text = "LAST30DAYS_TRUST_PROJECT_CONFIG=1\n" |
| 133 | if global_value is not None: |
| 134 | config_text += f"LAST30DAYS_MEMORY_DIR={global_value}\n" |
| 135 | write_config(Path(shell_env["LAST30DAYS_CONFIG_DIR"]) / ".env", config_text) |
| 136 | write_config( |
| 137 | tmp_path / ".claude" / "last30days.env", |
| 138 | "LAST30DAYS_MEMORY_DIR=${user_config.memory_dir}\n", |
| 139 | ) |
| 140 | if process_value is not None: |
| 141 | shell_env["LAST30DAYS_MEMORY_DIR"] = process_value |
| 142 | |
| 143 | result = subprocess.run( |
| 144 | [sys.executable, str(ENGINE), "--resolve-save-dir"], |
| 145 | env=shell_env, cwd=tmp_path, text=True, capture_output=True, check=True, |
| 146 | ) |
| 147 | |
| 148 | if expected is None: |
| 149 | expected = str(tmp_path / "home" / "Documents" / "Last30Days") |
| 150 | assert result.stdout == expected + "\n" |
| 151 | |
| 152 | |
| 153 | @pytest.mark.parametrize("entrypoint", ["skill", "engine"]) |
| 154 | @pytest.mark.parametrize("disabled", [False, True]) |
| 155 | @pytest.mark.parametrize("process_value", [None, "${user_config.memory_dir}"]) |
| 156 | def test_project_placeholder_preserves_global_research_save( |
| 157 | shell_env, tmp_path, entrypoint, disabled, process_value, |
| 158 | ): |
| 159 | shell_env["HOME"] = str(tmp_path / "home") |
| 160 | if process_value is not None: |
| 161 | shell_env["LAST30DAYS_MEMORY_DIR"] = process_value |
| 162 | target = tmp_path / "global research" |
| 163 | write_config( |
| 164 | Path(shell_env["LAST30DAYS_CONFIG_DIR"]) / ".env", |
| 165 | f"LAST30DAYS_MEMORY_DIR={'' if disabled else target}\nLAST30DAYS_TRUST_PROJECT_CONFIG=1\n", |
| 166 | ) |
| 167 | write_config( |
| 168 | tmp_path / ".claude" / "last30days.env", |
| 169 | "LAST30DAYS_MEMORY_DIR=${user_config.memory_dir}\n", |
| 170 | ) |
| 171 | command = '"$LAST30DAYS_PYTHON" "$SKILL_DIR/scripts/last30days.py" OpenAI --mock --quick --no-browser-cookies --emit=compact' |
| 172 | if entrypoint == "skill": |
| 173 | assignment = re.findall(r"^LAST30DAYS_MEMORY_DIR=.*$", reference_text("runtime"), re.M)[0] |
| 174 | command = assignment + "\n" + command + ' --save-dir="$LAST30DAYS_MEMORY_DIR"' |
| 175 | result = subprocess.run( |
| 176 | [BASH, "-c", command], env=shell_env, cwd=tmp_path, |
| 177 | text=True, capture_output=True, |
| 178 | ) |
| 179 | |
| 180 | assert result.returncode == 0, result.stderr |
| 181 | if disabled: |
| 182 | assert "Raw results saved to" not in result.stdout |
| 183 | assert not target.exists() |
| 184 | else: |
| 185 | artifacts = list(target.glob("*.md")) |
| 186 | assert len(artifacts) == 1 |
| 187 | assert f"Raw results saved to {artifacts[0]}" in result.stdout |
| 188 | assert "OpenAI" in artifacts[0].read_text() |
| 189 | assert not (tmp_path / "home" / "Documents" / "Last30Days").exists() |
| 190 | assert not (tmp_path / "${user_config.memory_dir}").exists() |
| 191 | |
| 192 | |
| 193 | def test_bare_engine_with_only_memory_placeholders_does_not_save(shell_env, tmp_path): |
| 194 | shell_env["HOME"] = str(tmp_path / "home") |
| 195 | shell_env["LAST30DAYS_MEMORY_DIR"] = "${user_config.memory_dir}" |
| 196 | write_config( |
| 197 | Path(shell_env["LAST30DAYS_CONFIG_DIR"]) / ".env", |
| 198 | "LAST30DAYS_MEMORY_DIR=${user_config.memory_dir}\nLAST30DAYS_TRUST_PROJECT_CONFIG=1\n", |
| 199 | ) |
| 200 | write_config( |
| 201 | tmp_path / ".claude" / "last30days.env", |
| 202 | "LAST30DAYS_MEMORY_DIR=${user_config.memory_dir}\n", |
| 203 | ) |
| 204 | |
| 205 | result = subprocess.run( |
| 206 | [sys.executable, str(ENGINE), "OpenAI", "--mock", "--quick", "--no-browser-cookies", "--emit=compact"], |
| 207 | env=shell_env, cwd=tmp_path, text=True, capture_output=True, |
| 208 | ) |
| 209 | |
| 210 | assert result.returncode == 0, result.stderr |
| 211 | assert "✅ All agents reported back!" in result.stdout |
| 212 | assert "Raw results saved to" not in result.stdout |
| 213 | assert not list(tmp_path.rglob("*-raw.md")) |
| 214 | |
| 215 | |
| 216 | @pytest.mark.parametrize("flag", ["flag research", "", "${user_config.memory_dir}", "prefix-${user_config.memory_dir}"]) |
| 217 | def test_resolver_explicit_flag_wins(shell_env, tmp_path, flag): |
| 218 | shell_env["LAST30DAYS_MEMORY_DIR"] = "/tmp/process research" |
| 219 | result = subprocess.run( |
| 220 | [sys.executable, str(ENGINE), "--resolve-save-dir", "--save-dir", flag], |
| 221 | env=shell_env, cwd=tmp_path, text=True, capture_output=True, check=True, |
| 222 | ) |
| 223 | assert result.stdout == (str(tmp_path / flag) if flag else "") + "\n" |
| 224 | |
| 225 | |
| 226 | def test_dotenv_path_is_data_not_shell_code(shell_env, tmp_path): |
| 227 | marker = tmp_path / "must-not-execute" |
| 228 | value = f"$(printf unsafe > {marker})" |
| 229 | write_config(Path(shell_env["LAST30DAYS_CONFIG_DIR"]) / ".env", f"LAST30DAYS_MEMORY_DIR={value}\n") |
| 230 | assignment = re.findall(r"^LAST30DAYS_MEMORY_DIR=.*$", reference_text("runtime"), re.M)[0] |
| 231 | result = subprocess.run( |
| 232 | [BASH, "-c", assignment + '\nprintf "%s" "$LAST30DAYS_MEMORY_DIR"'], |
| 233 | env=shell_env, cwd=tmp_path, text=True, capture_output=True, check=True, |
| 234 | ) |
| 235 | assert result.stdout == str(tmp_path / value) |
| 236 | assert not marker.exists() |
| 237 | |
| 238 | |
| 239 | def test_resolver_exits_before_auth_or_research(monkeypatch, capsys): |
| 240 | import last30days |
| 241 | from lib import env |
| 242 | |
| 243 | assert Path(last30days.__file__).resolve() == ENGINE |
| 244 | assert Path(env.__file__).resolve() == SKILL / "scripts" / "lib" / "env.py" |
| 245 | |
| 246 | def forbidden(*args, **kwargs): |
| 247 | pytest.fail("path query entered credential/research configuration") |
| 248 | |
| 249 | monkeypatch.setattr(env, "_load_keychain", forbidden) |
| 250 | monkeypatch.setattr(env, "_load_pass", forbidden) |
| 251 | monkeypatch.setattr(env, "_discover_and_apply_x_credentials", forbidden) |
| 252 | monkeypatch.setattr(sys, "argv", [str(ENGINE), "--resolve-save-dir", "--save-dir", ""]) |
| 253 | assert last30days.main() == 0 |
| 254 | assert capsys.readouterr().out == "\n" |
| 255 | |
| 256 | |
| 257 | def test_documented_discovery_legs_reuse_captured_directory(shell_env, tmp_path): |
| 258 | text = reference_text("discovery") |
| 259 | starts = re.findall(r"^LAST30DAYS_MEMORY_DIR=.*$", reference_text("runtime"), re.M) |
| 260 | guards = re.findall(r'^: "\$\{LAST30DAYS_MEMORY_DIR\?.*$', text, re.M) |
| 261 | assert len(guards) == 2 |
| 262 | config_path = Path(shell_env["LAST30DAYS_CONFIG_DIR"]) / ".env" |
| 263 | captured = tmp_path / "original research" |
| 264 | write_config(config_path, f"LAST30DAYS_MEMORY_DIR={captured}\n") |
| 265 | changed = tmp_path / "changed research" |
| 266 | command = "\n".join([ |
| 267 | starts[0], |
| 268 | f"printf '%s\\n' {shlex.quote(f'LAST30DAYS_MEMORY_DIR={changed}')} > {shlex.quote(str(config_path))}", |
| 269 | *guards, |
| 270 | 'printf "%s" "$LAST30DAYS_MEMORY_DIR"', |
| 271 | ]) |
| 272 | result = subprocess.run( |
| 273 | [BASH, "-c", command], env=shell_env, cwd=tmp_path, |
| 274 | text=True, capture_output=True, check=True, |
| 275 | ) |
| 276 | assert result.stdout == str(captured) |
| 277 | |
| 278 | |
| 279 | def test_mock_research_saves_to_documented_dotenv_directory(shell_env, tmp_path): |
| 280 | target = tmp_path / "client research" |
| 281 | write_config(Path(shell_env["LAST30DAYS_CONFIG_DIR"]) / ".env", f"LAST30DAYS_MEMORY_DIR={target}\n") |
| 282 | text = reference_text("runtime") |
| 283 | assignment = re.findall(r"^LAST30DAYS_MEMORY_DIR=.*$", text, re.M)[0] |
| 284 | command = assignment + '\n"$LAST30DAYS_PYTHON" "$SKILL_DIR/scripts/last30days.py" OpenAI --mock --quick --no-browser-cookies --emit=md --save-dir="$LAST30DAYS_MEMORY_DIR"' |
| 285 | result = subprocess.run( |
| 286 | [BASH, "-c", command], env=shell_env, cwd=tmp_path, |
| 287 | text=True, capture_output=True, |
| 288 | ) |
| 289 | assert result.returncode == 0, result.stderr |
| 290 | artifacts = list(target.glob("*.md")) |
| 291 | assert artifacts |
| 292 | assert "OpenAI" in artifacts[0].read_text() |
| 293 | |
| 294 | |
| 295 | def test_empty_project_setting_disables_bare_engine_save(shell_env, tmp_path): |
| 296 | target = tmp_path / "global research" |
| 297 | write_config( |
| 298 | Path(shell_env["LAST30DAYS_CONFIG_DIR"]) / ".env", |
| 299 | f"LAST30DAYS_MEMORY_DIR={target}\nLAST30DAYS_TRUST_PROJECT_CONFIG=1\n", |
| 300 | ) |
| 301 | write_config(tmp_path / ".claude" / "last30days.env", "LAST30DAYS_MEMORY_DIR=\n") |
| 302 | result = subprocess.run( |
| 303 | [sys.executable, str(ENGINE), "OpenAI", "--mock", "--quick", "--no-browser-cookies", "--emit=md"], |
| 304 | env=shell_env, cwd=tmp_path, text=True, capture_output=True, |
| 305 | ) |
| 306 | assert result.returncode == 0, result.stderr |
| 307 | assert "OpenAI" in result.stdout |
| 308 | assert not target.exists() |
| 309 | assert "Saved output to" not in result.stderr |
| 310 | |
| 311 | |
| 312 | @pytest.mark.parametrize("disabled", [False, True]) |
| 313 | def test_documented_mock_research_placeholder_uses_configured_save_state(shell_env, tmp_path, disabled): |
| 314 | target = tmp_path / "configured research" |
| 315 | write_config( |
| 316 | Path(shell_env["LAST30DAYS_CONFIG_DIR"]) / ".env", |
| 317 | f"LAST30DAYS_MEMORY_DIR={'' if disabled else target}\n", |
| 318 | ) |
| 319 | shell_env["LAST30DAYS_MEMORY_DIR"] = "${user_config.memory_dir}" |
| 320 | assignment = re.findall(r"^LAST30DAYS_MEMORY_DIR=.*$", reference_text("runtime"), re.M)[0] |
| 321 | command = assignment + '\n"$LAST30DAYS_PYTHON" "$SKILL_DIR/scripts/last30days.py" OpenAI --mock --quick --no-browser-cookies --emit=compact --save-dir="$LAST30DAYS_MEMORY_DIR"' |
| 322 | result = subprocess.run( |
| 323 | [BASH, "-c", command], env=shell_env, cwd=tmp_path, |
| 324 | text=True, capture_output=True, |
| 325 | ) |
| 326 | assert result.returncode == 0, result.stderr |
| 327 | assert "✅ All agents reported back!" in result.stdout |
| 328 | if disabled: |
| 329 | assert "Raw results saved to" not in result.stdout |
| 330 | assert not target.exists() |
| 331 | else: |
| 332 | artifacts = list(target.glob("*.md")) |
| 333 | assert len(artifacts) == 1 |
| 334 | assert f"Raw results saved to {artifacts[0]}" in result.stdout |
| 335 | assert "OpenAI" in artifacts[0].read_text() |
| 336 | |
| 337 | |
| 338 | def test_skill_footer_claims_only_emitted_saved_paths(): |
| 339 | text = (SKILL / "SKILL.md").read_text() |
| 340 | law = text.split("**LAW 5 -", 1)[1].split("**LAW 6 -", 1)[0] |
| 341 | assert "Relay a saved-file pointer only if emitted" in law |
| 342 | assert "Never invent a footer, saved path" in law |
| 343 | assert "subject to governing instructions" in law |
| 344 | synthesis = reference_text("synthesis") |
| 345 | footer = synthesis.split("**THEN - Engine footer pass-through", 1)[1].split("**LAST - Invitation", 1)[0] |
| 346 | assert "only when the engine emitted a saved path" in footer |
| 347 | assert "Never invent a path" in footer |
| 348 | assert "higher-priority instructions" in footer |
| 349 | assert "and ending with `📎 Raw results saved to" not in footer |
| 350 | assert "└─ 📎 Raw results saved to ..." not in text |
| 351 | assert "The research script already saved raw data" not in text |
| 352 | assert "└─ 📎 Raw results saved to ..." not in synthesis |
| 353 | assert "The research script already saved raw data" not in synthesis |
| 354 |