|
| 1 | +"""Regression tests for #241 — around:/grep_around: per-op byte cap. |
| 2 | +
|
| 3 | +A large context (:N) on a file of long (minified) lines could dump hundreds |
| 4 | +of KB in a single op, blowing the caller's context budget. The ops now cap |
| 5 | +their output at a byte budget, truncating at a line boundary and appending a |
| 6 | +footer that points at the narrower tools (smaller :N, between:). |
| 7 | +""" |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import supertool |
| 13 | + |
| 14 | + |
| 15 | +# A long single line so a few of them blow past the 16KB cap fast. |
| 16 | +LONG = "x" * 4000 |
| 17 | + |
| 18 | + |
| 19 | +def _make_long_file(tmp_path: Path, n_lines: int = 40) -> Path: |
| 20 | + f = tmp_path / "big.js" |
| 21 | + body = "\n".join(f"line{i} {LONG} TARGET{i}" for i in range(n_lines)) + "\n" |
| 22 | + f.write_text(body) |
| 23 | + return f |
| 24 | + |
| 25 | + |
| 26 | +def test_around_caps_large_window(tmp_path: Path) -> None: |
| 27 | + f = _make_long_file(tmp_path) |
| 28 | + out = supertool.op_around("TARGET20", str(f), 40) |
| 29 | + assert len(out.encode()) <= 16000 + 200, f"not capped: {len(out)} bytes" |
| 30 | + assert "truncated" in out |
| 31 | + assert "between:" in out |
| 32 | + |
| 33 | + |
| 34 | +def test_around_truncates_at_line_boundary(tmp_path: Path) -> None: |
| 35 | + f = _make_long_file(tmp_path) |
| 36 | + out = supertool.op_around("TARGET20", str(f), 40) |
| 37 | + # Everything before the footer must end on a complete line (newline). |
| 38 | + footer_idx = out.index("… truncated") |
| 39 | + body = out[:footer_idx] |
| 40 | + assert body.endswith("\n"), "truncation cut mid-line" |
| 41 | + |
| 42 | + |
| 43 | +def test_around_small_file_not_capped(tmp_path: Path) -> None: |
| 44 | + f = tmp_path / "small.py" |
| 45 | + f.write_text("a\nb\nTARGET\nd\ne\n") |
| 46 | + out = supertool.op_around("TARGET", str(f), 2) |
| 47 | + assert "truncated" not in out |
| 48 | + assert "TARGET" in out |
| 49 | + |
| 50 | + |
| 51 | +def test_grep_around_caps_large_window(tmp_path: Path) -> None: |
| 52 | + f = _make_long_file(tmp_path) |
| 53 | + # grep_around routes through op_grep with context > 0. |
| 54 | + out = supertool.op_grep("TARGET", str(f), limit=50, context=40) |
| 55 | + assert len(out.encode()) <= 16000 + 200, f"not capped: {len(out)} bytes" |
| 56 | + assert "truncated" in out |
| 57 | + |
| 58 | + |
| 59 | +def test_grep_no_context_not_capped_by_window(tmp_path: Path) -> None: |
| 60 | + """Plain grep (context=0) takes a different branch — no window footer.""" |
| 61 | + f = tmp_path / "small.py" |
| 62 | + f.write_text("alpha\nTARGET here\nbeta\n") |
| 63 | + out = supertool.op_grep("TARGET", str(f), limit=10, context=0) |
| 64 | + assert "truncated (~" not in out |
| 65 | + assert "TARGET" in out |
| 66 | + |
| 67 | + |
| 68 | +def test_around_env_override(tmp_path: Path, monkeypatch) -> None: |
| 69 | + f = _make_long_file(tmp_path) |
| 70 | + monkeypatch.setenv("SUPERTOOL_AROUND_MAX_BYTES", "4000") |
| 71 | + out = supertool.op_around("TARGET20", str(f), 40) |
| 72 | + assert len(out.encode()) <= 4000 + 200, f"env cap ignored: {len(out)} bytes" |
| 73 | + assert "truncated" in out |
| 74 | + |
| 75 | + |
| 76 | +def test_around_dir_aggregate_capped(tmp_path: Path) -> None: |
| 77 | + """Dir fan-out caps the TOTAL op output, not just each file (#241 review).""" |
| 78 | + d = tmp_path / "pkg" |
| 79 | + d.mkdir() |
| 80 | + for i in range(10): |
| 81 | + (d / f"f{i}.js").write_text(f"head{i}\n{LONG} TARGET\n{LONG}\n") |
| 82 | + out = supertool.op_around("TARGET", str(d), 20) |
| 83 | + assert len(out.encode()) <= 16000 + 200, f"dir aggregate not capped: {len(out)}" |
| 84 | + assert "truncated" in out |
| 85 | + # Exactly one truncation footer — the per-file cap was removed, so footers |
| 86 | + # don't appear interleaved between files. |
| 87 | + assert out.count("… truncated") == 1 |
| 88 | + |
| 89 | + |
| 90 | +def test_around_single_giant_line_no_newline(tmp_path: Path) -> None: |
| 91 | + """A single line longer than the cap: pass the partial through + footer, |
| 92 | + never crash (nl == -1 branch).""" |
| 93 | + f = tmp_path / "min.js" |
| 94 | + f.write_text("TARGET " + "y" * 40000) # one line, no trailing context lines |
| 95 | + out = supertool.op_around("TARGET", str(f), 0) |
| 96 | + assert len(out.encode()) <= 16000 + 200, f"giant line not capped: {len(out)}" |
| 97 | + assert "truncated" in out |
0 commit comments