|
| 1 | +import logging |
1 | 2 | from pathlib import Path |
2 | 3 | from typing import Any |
3 | 4 | from unittest.mock import MagicMock, patch |
|
8 | 9 |
|
9 | 10 | from semble import SembleIndex |
10 | 11 | from semble.index.create import create_index_from_path |
11 | | -from semble.index.files import _MAX_FILE_BYTES, FileStatus, get_file_status |
| 12 | +from semble.index.files import _DEFAULT_MAX_FILE_BYTES, FileStatus, get_file_status, get_max_file_bytes |
12 | 13 | from semble.types import ContentType |
13 | 14 | from tests.conftest import make_chunk |
14 | 15 |
|
@@ -69,11 +70,45 @@ def test_index_empty_returns_zero_chunks(mock_model: StaticModel, tmp_path: Path |
69 | 70 | create_index_from_path(tmp_path, mock_model) |
70 | 71 |
|
71 | 72 |
|
72 | | -def test_oversized_file_is_skipped(mock_model: StaticModel, tmp_path: Path) -> None: |
73 | | - """Files exceeding _MAX_FILE_BYTES are silently skipped during indexing.""" |
74 | | - (tmp_path / "big.py").write_bytes(b"x" * (_MAX_FILE_BYTES + 1)) |
75 | | - with pytest.raises(ValueError): # no indexable content remains |
76 | | - create_index_from_path(tmp_path, mock_model) |
| 73 | +def test_max_file_bytes_resolution(monkeypatch: pytest.MonkeyPatch) -> None: |
| 74 | + """The limit resolves from SEMBLE_MAX_FILE_BYTES, falling back to the 1 MB default.""" |
| 75 | + monkeypatch.delenv("SEMBLE_MAX_FILE_BYTES", raising=False) |
| 76 | + assert get_max_file_bytes() == _DEFAULT_MAX_FILE_BYTES |
| 77 | + monkeypatch.setenv("SEMBLE_MAX_FILE_BYTES", str(_DEFAULT_MAX_FILE_BYTES + 5)) |
| 78 | + assert get_max_file_bytes() == _DEFAULT_MAX_FILE_BYTES + 5 |
| 79 | + |
| 80 | + |
| 81 | +def test_max_file_bytes_invalid_values_fall_back(monkeypatch: pytest.MonkeyPatch) -> None: |
| 82 | + """Malformed or nonpositive SEMBLE_MAX_FILE_BYTES values fall back to the default.""" |
| 83 | + monkeypatch.delenv("SEMBLE_MAX_FILE_BYTES", raising=False) |
| 84 | + assert get_max_file_bytes() == _DEFAULT_MAX_FILE_BYTES |
| 85 | + for bad in ("not-a-number", "0", "-5"): |
| 86 | + monkeypatch.setenv("SEMBLE_MAX_FILE_BYTES", bad) |
| 87 | + assert get_max_file_bytes() == _DEFAULT_MAX_FILE_BYTES |
| 88 | + |
| 89 | + |
| 90 | +def test_oversized_file_is_skipped_with_warning( |
| 91 | + mock_model: StaticModel, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture |
| 92 | +) -> None: |
| 93 | + """Files exceeding the limit are skipped during indexing, with a warning naming them.""" |
| 94 | + monkeypatch.delenv("SEMBLE_MAX_FILE_BYTES", raising=False) |
| 95 | + (tmp_path / "big.py").write_bytes(b"x" * (_DEFAULT_MAX_FILE_BYTES + 1)) |
| 96 | + with caplog.at_level(logging.WARNING, logger="semble.index.create"): |
| 97 | + with pytest.raises(ValueError): # no indexable content remains |
| 98 | + create_index_from_path(tmp_path, mock_model) |
| 99 | + assert "big.py" in caplog.text |
| 100 | + assert str(_DEFAULT_MAX_FILE_BYTES) in caplog.text |
| 101 | + assert "SEMBLE_MAX_FILE_BYTES" in caplog.text |
| 102 | + |
| 103 | + |
| 104 | +def test_oversized_file_indexed_when_limit_raised( |
| 105 | + mock_model: StaticModel, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 106 | +) -> None: |
| 107 | + """Raising SEMBLE_MAX_FILE_BYTES lets oversized files into the index.""" |
| 108 | + monkeypatch.setenv("SEMBLE_MAX_FILE_BYTES", str(_DEFAULT_MAX_FILE_BYTES + 1024)) |
| 109 | + (tmp_path / "big.py").write_bytes(b"x = 1\n" + b"#" * _DEFAULT_MAX_FILE_BYTES) |
| 110 | + _, _, chunks, _ = create_index_from_path(tmp_path, mock_model) |
| 111 | + assert any(chunk.file_path.endswith("big.py") for chunk in chunks) |
77 | 112 |
|
78 | 113 |
|
79 | 114 | def test_tiny_invalid_utf8_file_status_does_not_crash(tmp_path: Path) -> None: |
|
0 commit comments