"""Inspection discovery must handle old announcements without loading huge logs.""" from io import BytesIO from pathlib import Path import sys import unittest from unittest.mock import patch sys.path[:0] = [ str(Path(__file__).resolve().parents[2] / "e2e"), str(Path(__file__).resolve().parents[1]), ] import hypothesis_profiles # noqa: E402 from hypothesis import given, settings, strategies as st # noqa: E402 from inspect_support import inspection_node # noqa: E402 class BoundedLog(BytesIO): def read(self, size=-1): if not 0 <= size <= 65536: raise AssertionError(f"unbounded log read: {size}") return super().read(size) class InspectionNodeTests(unittest.TestCase): @settings(max_examples=hypothesis_profiles.cases(100)) @given(st.integers(0, 150000), st.integers(0, 150000)) def test_newest_matching_node_across_block_boundaries(self, prefix, suffix): data = ( b"inspect: node albedo_42@older\n" + b"x" * prefix + b"\ninspect: node albedo_42@newest\n" + b"inspect: node albedo_7@other\n" + b"x" * suffix ) with patch.object(Path, "open", return_value=BoundedLog(data)): self.assertEqual(inspection_node(Path("home"), 42), "albedo_42@newest") def test_absent_node_is_refused(self): with patch.object(Path, "open", return_value=BoundedLog(b"unrelated\n")): with self.assertRaises(ValueError): inspection_node(Path("home"), 42)