From d5b314f6b64753dc8f0b70e2be430137e228a9ee Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Sun, 21 Jun 2026 16:24:51 +0200 Subject: [PATCH] feat: scan.resolve_index with end-of-batch error Co-Authored-By: Claude Opus 4.8 --- gates/scan.py | 8 ++++++++ tests/test_scan.py | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/gates/scan.py b/gates/scan.py index 08f7adb..5832986 100644 --- a/gates/scan.py +++ b/gates/scan.py @@ -28,3 +28,11 @@ def list_images(folder, depth=0): results.append(str(cur / name)) results.sort(key=lambda p: natural_key(os.path.relpath(p, root))) return results + + +def resolve_index(count, index): + if count == 0: + raise FileNotFoundError("No images found in folder") + if index < 0 or index >= count: + raise IndexError(f"index {index} out of range: {count} images") + return index diff --git a/tests/test_scan.py b/tests/test_scan.py index e3e76c8..eaf5897 100644 --- a/tests/test_scan.py +++ b/tests/test_scan.py @@ -37,3 +37,19 @@ def test_list_images_bad_path_raises(tmp_path): import pytest with pytest.raises(NotADirectoryError): scan.list_images(str(tmp_path / "nope")) + +def test_resolve_index_ok(): + assert scan.resolve_index(5, 0) == 0 + assert scan.resolve_index(5, 4) == 4 + +def test_resolve_index_out_of_range_raises(): + import pytest + with pytest.raises(IndexError): + scan.resolve_index(5, 5) + with pytest.raises(IndexError): + scan.resolve_index(5, -1) + +def test_resolve_index_empty_raises(): + import pytest + with pytest.raises(FileNotFoundError): + scan.resolve_index(0, 0)