|
| 1 | +"""Copy-examples honesty. |
| 2 | +
|
| 3 | +User reported running `thesis setup` → Step 5 → answered "Yes" → wizard |
| 4 | +replied "copied 0 example file(s)". The call was correct (targets already |
| 5 | +existed) but the report was misleading. These tests lock in the fix: |
| 6 | +report copied / overwritten / already-present separately, and expose |
| 7 | +`--overwrite-examples` for when users actually want to replace them. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import pytest |
| 15 | +from typer.testing import CliRunner |
| 16 | + |
| 17 | +from thesis_agent import cli |
| 18 | +from thesis_agent.config import paths |
| 19 | + |
| 20 | +runner = CliRunner() |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def ws(tmp_path: Path, monkeypatch): |
| 25 | + monkeypatch.chdir(tmp_path) |
| 26 | + for v in ( |
| 27 | + "THESIS_PROVIDER", "ANTHROPIC_API_KEY", |
| 28 | + "OPENROUTER_API_KEY", "OPENROUTER_BASE_URL", |
| 29 | + ): |
| 30 | + monkeypatch.delenv(v, raising=False) |
| 31 | + return tmp_path |
| 32 | + |
| 33 | + |
| 34 | +# --------------------------------------------------------------------------- |
| 35 | +# _copy_examples helper direct tests |
| 36 | +# --------------------------------------------------------------------------- |
| 37 | + |
| 38 | +class TestCopyExamplesHelper: |
| 39 | + def test_fresh_workspace_copies_files(self, ws: Path): |
| 40 | + p = paths() |
| 41 | + for d in (p.raw, p.style_samples, p.thesis_dir): |
| 42 | + d.mkdir(parents=True, exist_ok=True) |
| 43 | + res = cli._copy_examples() |
| 44 | + assert res["copied"] >= 1 |
| 45 | + assert res["already_present"] == 0 |
| 46 | + assert res["overwritten"] == 0 |
| 47 | + assert res["source_missing"] == 0 |
| 48 | + |
| 49 | + def test_second_call_reports_already_present(self, ws: Path): |
| 50 | + p = paths() |
| 51 | + for d in (p.raw, p.style_samples, p.thesis_dir): |
| 52 | + d.mkdir(parents=True, exist_ok=True) |
| 53 | + first = cli._copy_examples() |
| 54 | + second = cli._copy_examples() |
| 55 | + assert second["copied"] == 0 |
| 56 | + assert second["already_present"] == first["copied"] |
| 57 | + |
| 58 | + def test_overwrite_flag_rewrites_files(self, ws: Path): |
| 59 | + p = paths() |
| 60 | + for d in (p.raw, p.style_samples, p.thesis_dir): |
| 61 | + d.mkdir(parents=True, exist_ok=True) |
| 62 | + cli._copy_examples() |
| 63 | + # User edits an example |
| 64 | + edited = p.raw / "example-source.md" |
| 65 | + edited.write_text("MY EDITS", encoding="utf-8") |
| 66 | + |
| 67 | + # Without --overwrite: left alone |
| 68 | + res_no = cli._copy_examples() |
| 69 | + assert edited.read_text(encoding="utf-8") == "MY EDITS" |
| 70 | + assert res_no["overwritten"] == 0 |
| 71 | + assert res_no["already_present"] >= 1 |
| 72 | + |
| 73 | + # With overwrite=True: replaced |
| 74 | + res_yes = cli._copy_examples(overwrite=True) |
| 75 | + assert edited.read_text(encoding="utf-8") != "MY EDITS" |
| 76 | + assert res_yes["overwritten"] >= 1 |
| 77 | + assert res_yes["copied"] == 0 |
| 78 | + |
| 79 | + |
| 80 | +# --------------------------------------------------------------------------- |
| 81 | +# Wizard output: honest report when Yes but nothing to do |
| 82 | +# --------------------------------------------------------------------------- |
| 83 | + |
| 84 | +class TestWizardExampleReport: |
| 85 | + def _run_setup(self, args_extra: list[str] | None = None): |
| 86 | + args = [ |
| 87 | + "setup", "--non-interactive", |
| 88 | + "--provider", "anthropic", "--api-key", "sk-ant-x", |
| 89 | + "--skip-validation", |
| 90 | + ] + (args_extra or []) |
| 91 | + return runner.invoke(cli.app, args) |
| 92 | + |
| 93 | + def test_first_run_says_copied_N(self, ws: Path): |
| 94 | + result = self._run_setup() |
| 95 | + assert result.exit_code == 0, result.stdout |
| 96 | + assert "copied" in result.stdout |
| 97 | + # Should name at least one new file |
| 98 | + assert "new" in result.stdout |
| 99 | + |
| 100 | + def test_second_run_says_already_present(self, ws: Path): |
| 101 | + self._run_setup() |
| 102 | + result = self._run_setup() |
| 103 | + assert result.exit_code == 0 |
| 104 | + # Must NOT mislead with "copied 0" |
| 105 | + assert "copied 0" not in result.stdout |
| 106 | + # Must explain the real state |
| 107 | + assert "already in your workspace" in result.stdout |
| 108 | + # Must point to the escape hatch |
| 109 | + assert "--overwrite-examples" in result.stdout |
| 110 | + |
| 111 | + def test_overwrite_flag_replaces_and_reports(self, ws: Path): |
| 112 | + self._run_setup() |
| 113 | + # Mutate one example |
| 114 | + p = paths() |
| 115 | + (p.raw / "example-source.md").write_text("MINE\n", encoding="utf-8") |
| 116 | + result = self._run_setup(["--overwrite-examples"]) |
| 117 | + assert result.exit_code == 0, result.stdout |
| 118 | + assert "overwrote" in result.stdout |
| 119 | + # And the content was actually replaced |
| 120 | + content = (p.raw / "example-source.md").read_text(encoding="utf-8") |
| 121 | + assert content != "MINE\n" |
| 122 | + |
| 123 | + def test_skip_examples_still_works(self, ws: Path): |
| 124 | + result = self._run_setup(["--skip-examples"]) |
| 125 | + assert result.exit_code == 0 |
| 126 | + assert "skipped examples" in result.stdout.lower() |
| 127 | + # Did not copy |
| 128 | + assert not (ws / "research" / "raw" / "example-source.md").exists() |
| 129 | + |
| 130 | + def test_setup_help_documents_overwrite_flag(self, ws: Path): |
| 131 | + result = runner.invoke(cli.app, ["setup", "--help"]) |
| 132 | + assert result.exit_code == 0 |
| 133 | + assert "--overwrite-examples" in result.stdout |
| 134 | + |
| 135 | + |
| 136 | +# --------------------------------------------------------------------------- |
| 137 | +# Regression: wizard must never claim "copied 0" when Yes was answered |
| 138 | +# --------------------------------------------------------------------------- |
| 139 | + |
| 140 | +def test_yes_with_existing_examples_never_says_copied_zero(ws: Path): |
| 141 | + # First run populates the examples |
| 142 | + runner.invoke(cli.app, [ |
| 143 | + "setup", "--non-interactive", |
| 144 | + "--provider", "anthropic", "--api-key", "sk-ant-x", |
| 145 | + "--skip-validation", |
| 146 | + ]) |
| 147 | + # Second run: user answers "Yes" to copy (via non-interactive default) |
| 148 | + result = runner.invoke(cli.app, [ |
| 149 | + "setup", "--non-interactive", |
| 150 | + "--provider", "anthropic", "--api-key", "sk-ant-x", |
| 151 | + "--skip-validation", |
| 152 | + ]) |
| 153 | + assert result.exit_code == 0 |
| 154 | + assert "copied 0 example" not in result.stdout # the bug that triggered this fix |
0 commit comments