|
| 1 | +"""Opt-in live checkout-boundary smoke test; never collected by pytest. |
| 2 | +
|
| 3 | +Run: .venv/bin/python tests/smoke_test_nested_checkouts.py --provider codex --model gpt-6-luna |
| 4 | +Requires an authenticated Codex CLI. Logs and generated artifacts are retained. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import argparse |
| 10 | +from contextlib import ExitStack |
| 11 | +import json |
| 12 | +import os |
| 13 | +from pathlib import Path |
| 14 | +import shutil |
| 15 | +import subprocess |
| 16 | +import sys |
| 17 | +import tempfile |
| 18 | +from unittest.mock import patch |
| 19 | + |
| 20 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
| 21 | + |
| 22 | +from click.testing import CliRunner |
| 23 | + |
| 24 | +import codewiki.cli.config_manager as config_module |
| 25 | +from codewiki.cli.main import cli |
| 26 | +from codewiki.src.be.dependency_analyzer.models.core import Node |
| 27 | +from codewiki.src.be.module_scope import validate_module_file_coverage |
| 28 | + |
| 29 | + |
| 30 | +def git(repo: Path, *args: str) -> None: |
| 31 | + subprocess.run(["git", "-C", str(repo), *args], check=True, capture_output=True, text=True) |
| 32 | + |
| 33 | + |
| 34 | +def commit(repo: Path) -> None: |
| 35 | + git(repo, "add", ".") |
| 36 | + git( |
| 37 | + repo, |
| 38 | + "-c", |
| 39 | + "user.name=Checkout Smoke", |
| 40 | + "-c", |
| 41 | + "user.email=smoke@example.invalid", |
| 42 | + "commit", |
| 43 | + "-qm", |
| 44 | + "fixture", |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +def make_fixture(root: Path) -> Path: |
| 49 | + repo, dependency = root / "repo", root / "dependency" |
| 50 | + repo.mkdir() |
| 51 | + dependency.mkdir() |
| 52 | + git(dependency, "init", "-q") |
| 53 | + (dependency / "support.py").write_text( |
| 54 | + '"""Clamp a numeric value to a nonnegative integer."""\n' |
| 55 | + "def clamp(value: int) -> int:\n return max(0, value)\n" |
| 56 | + ) |
| 57 | + commit(dependency) |
| 58 | + git(repo, "init", "-q") |
| 59 | + (repo / "helper.py").write_text( |
| 60 | + '"""Arithmetic helpers for a small calculator."""\n' |
| 61 | + "def helper(value: int) -> int:\n return value + 1\n" |
| 62 | + ) |
| 63 | + (repo / "app.py").write_text( |
| 64 | + '"""Calculator entrypoint."""\nfrom helper import helper\n\n' |
| 65 | + "def run(value: int) -> int:\n return helper(value)\n" |
| 66 | + ) |
| 67 | + commit(repo) |
| 68 | + git(repo, "worktree", "add", "-qb", "smoke-worktree", ".claude/worktrees/feature") |
| 69 | + git(repo, "clone", "-q", ".", "copies/other") |
| 70 | + for directory in (".claude/worktrees/feature", "copies/other"): |
| 71 | + (repo / directory / "sentinel.py").write_text( |
| 72 | + "def nested_checkout_sentinel():\n return 'must not be documented'\n" |
| 73 | + ) |
| 74 | + git( |
| 75 | + repo, |
| 76 | + "-c", |
| 77 | + "protocol.file.allow=always", |
| 78 | + "submodule", |
| 79 | + "add", |
| 80 | + "-q", |
| 81 | + str(dependency), |
| 82 | + "deps/math_support", |
| 83 | + ) |
| 84 | + # Commit only the intended parent files and submodule registration. |
| 85 | + git( |
| 86 | + repo, |
| 87 | + "-c", |
| 88 | + "user.name=Checkout Smoke", |
| 89 | + "-c", |
| 90 | + "user.email=smoke@example.invalid", |
| 91 | + "commit", |
| 92 | + "-qm", |
| 93 | + "register dependency", |
| 94 | + ) |
| 95 | + (repo / "untracked.py").write_text( |
| 96 | + '"""Format a calculator result."""\n' |
| 97 | + "def format_result(value: int) -> str:\n return str(value)\n" |
| 98 | + ) |
| 99 | + return repo |
| 100 | + |
| 101 | + |
| 102 | +def verify(output: Path, model: str) -> dict: |
| 103 | + metadata = json.loads((output / "metadata.json").read_text()) |
| 104 | + usage = metadata["usage"] |
| 105 | + assert not usage.get("incomplete"), "Generation was marked incomplete" |
| 106 | + assert usage["n_calls"] > 0, "No real model activity recorded" |
| 107 | + calls = usage["calls"] |
| 108 | + assert all(call["provider"] == "codex" for call in calls), calls |
| 109 | + assert all(call["model"] == model for call in calls), calls |
| 110 | + assert any(call["usage_reported"] for call in calls), "No provider usage recorded" |
| 111 | + assert usage["totals"].get("output_tokens", 0) > 0, "No model output recorded" |
| 112 | + |
| 113 | + tree = json.loads((output / "module_tree.json").read_text()) |
| 114 | + # A small repository uses an empty tree and one whole-repository overview. |
| 115 | + assert isinstance(tree, dict), "Invalid module tree" |
| 116 | + overview = output / "overview.md" |
| 117 | + assert overview.is_file() and overview.read_text().strip(), "Missing overview" |
| 118 | + pages = [p for p in output.rglob("*.md") if "temp" not in p.relative_to(output).parts] |
| 119 | + assert pages and all(p.read_text().strip() for p in pages), "Empty documentation page" |
| 120 | + for page in pages: |
| 121 | + text = page.read_text() |
| 122 | + assert "nested_checkout_sentinel" not in text, page |
| 123 | + assert ".claude/worktrees/feature" not in text and "copies/other" not in text, page |
| 124 | + |
| 125 | + graphs = list((output / "temp/dependency_graphs").glob("*_dependency_graph.json")) |
| 126 | + assert len(graphs) == 1, graphs |
| 127 | + graph = json.loads(graphs[0].read_text()) |
| 128 | + paths = {node["relative_path"] for node in graph.values()} |
| 129 | + assert paths == {"app.py", "helper.py", "untracked.py", "deps/math_support/support.py"}, paths |
| 130 | + assert set(graph["app.py::run"]["depends_on"]) == {"helper.py::helper"} |
| 131 | + if tree: |
| 132 | + validate_module_file_coverage( |
| 133 | + tree, {ident: Node.model_validate(node) for ident, node in graph.items()} |
| 134 | + ) |
| 135 | + return { |
| 136 | + "provider": "codex", |
| 137 | + "requested_model": model, |
| 138 | + "recorded_models": sorted({call["model"] for call in calls}), |
| 139 | + "model_calls": usage["n_calls"], |
| 140 | + "source_paths": sorted(paths), |
| 141 | + "documentation_pages": [str(p.relative_to(output)) for p in pages], |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | +def main() -> None: |
| 146 | + parser = argparse.ArgumentParser(description=__doc__) |
| 147 | + parser.add_argument("--provider", choices=["codex"], default="codex") |
| 148 | + parser.add_argument("--model", default="gpt-6-luna") |
| 149 | + args = parser.parse_args() |
| 150 | + if not shutil.which("codex") or not shutil.which("git"): |
| 151 | + parser.error("Git and an authenticated Codex CLI must be on PATH") |
| 152 | + root = Path(tempfile.mkdtemp(prefix="codewiki-checkout-smoke-")) |
| 153 | + print(f"Smoke artifacts: {root}", flush=True) |
| 154 | + repo = make_fixture(root) |
| 155 | + output, config_dir = root / "docs", root / "config" |
| 156 | + previous_cwd = Path.cwd() |
| 157 | + try: |
| 158 | + with ExitStack() as stack: |
| 159 | + stack.enter_context(patch.dict(os.environ, {"CODEWIKI_NO_KEYRING": "1"})) |
| 160 | + for name, value in { |
| 161 | + "CONFIG_DIR": config_dir, |
| 162 | + "CONFIG_FILE": config_dir / "config.json", |
| 163 | + "CREDENTIALS_FILE": config_dir / "credentials.json", |
| 164 | + }.items(): |
| 165 | + stack.enter_context(patch.object(config_module, name, value)) |
| 166 | + config_module.ConfigManager().save( |
| 167 | + provider=args.provider, |
| 168 | + main_model=args.model, |
| 169 | + cluster_model=args.model, |
| 170 | + fallback_model="", |
| 171 | + ) |
| 172 | + os.chdir(repo) |
| 173 | + print(f"Generating with provider={args.provider}, model={args.model}...", flush=True) |
| 174 | + result = CliRunner().invoke( |
| 175 | + cli, |
| 176 | + [ |
| 177 | + "generate", |
| 178 | + "--output", |
| 179 | + str(output), |
| 180 | + "--no-artifacts", |
| 181 | + "--no-cache", |
| 182 | + "--min-token-per-module", |
| 183 | + "1", |
| 184 | + "--max-tokens", |
| 185 | + "8192", |
| 186 | + "--request-limit", |
| 187 | + "24", |
| 188 | + "--verbose", |
| 189 | + "--instructions", |
| 190 | + "Keep documentation concise, under 300 words per page. " |
| 191 | + "Do not include Mermaid diagrams.", |
| 192 | + ], |
| 193 | + ) |
| 194 | + (root / "generate.log").write_text(result.output) |
| 195 | + if result.exit_code: |
| 196 | + raise RuntimeError( |
| 197 | + f"Generation failed with exit {result.exit_code}; see {root / 'generate.log'}" |
| 198 | + ) from result.exception |
| 199 | + report = verify(output, args.model) |
| 200 | + (root / "report.json").write_text(json.dumps(report, indent=2)) |
| 201 | + print(json.dumps(report, indent=2), flush=True) |
| 202 | + print("PASS: live generation preserved repository boundaries", flush=True) |
| 203 | + finally: |
| 204 | + os.chdir(previous_cwd) |
| 205 | + shutil.rmtree(config_dir, ignore_errors=True) |
| 206 | + |
| 207 | + |
| 208 | +if __name__ == "__main__": |
| 209 | + main() |
0 commit comments