"""Tests for FloodfillManager — tracking floodfill routers for NetDB lookups.""" import os import pytest from i2p_netdb.floodfill import FloodfillManager class TestFloodfillAdd: def test_add_floodfill_and_retrieve(self): mgr = FloodfillManager() peer_hash = os.urandom(32) info = {"caps": "f", "host": "127.0.0.1"} mgr.add_floodfill(peer_hash, info) assert mgr.is_floodfill(peer_hash) assert peer_hash in mgr.get_all() def test_add_multiple_floodfills(self): mgr = FloodfillManager() hashes = [os.urandom(32) for _ in range(5)] for h in hashes: mgr.add_floodfill(h, {"caps": "f"}) assert mgr.count == 5 for h in hashes: assert mgr.is_floodfill(h) def test_add_duplicate_overwrites(self): mgr = FloodfillManager() peer_hash = os.urandom(32) mgr.add_floodfill(peer_hash, {"version": "1"}) mgr.add_floodfill(peer_hash, {"version": "2"}) assert mgr.count == 1 class TestFloodfillRemove: def test_remove_floodfill(self): mgr = FloodfillManager() peer_hash = os.urandom(32) mgr.add_floodfill(peer_hash, {"caps": "f"}) mgr.remove_floodfill(peer_hash) assert not mgr.is_floodfill(peer_hash) assert mgr.count == 0 def test_remove_nonexistent_is_noop(self): mgr = FloodfillManager() mgr.remove_floodfill(os.urandom(32)) # should not raise assert mgr.count == 0 class TestFloodfillSelect: def test_select_returns_one_from_available(self): mgr = FloodfillManager() hashes = [os.urandom(32) for _ in range(3)] for h in hashes: mgr.add_floodfill(h, {"caps": "f"}) selected = mgr.select_floodfill() assert selected in hashes def test_select_with_exclude_set(self): mgr = FloodfillManager() h1 = os.urandom(32) h2 = os.urandom(32) h3 = os.urandom(32) mgr.add_floodfill(h1, {}) mgr.add_floodfill(h2, {}) mgr.add_floodfill(h3, {}) selected = mgr.select_floodfill(exclude={h1, h2}) assert selected == h3 def test_select_all_excluded_returns_none(self): mgr = FloodfillManager() h1 = os.urandom(32) mgr.add_floodfill(h1, {}) selected = mgr.select_floodfill(exclude={h1}) assert selected is None def test_select_from_empty_returns_none(self): mgr = FloodfillManager() assert mgr.select_floodfill() is None class TestFloodfillNonFloodfill: def test_non_floodfill_not_returned(self): mgr = FloodfillManager() ff_hash = os.urandom(32) non_ff_hash = os.urandom(32) mgr.add_floodfill(ff_hash, {"caps": "f"}) # non_ff_hash was never added assert not mgr.is_floodfill(non_ff_hash) assert non_ff_hash not in mgr.get_all() class TestFloodfillCount: def test_count_empty(self): mgr = FloodfillManager() assert mgr.count == 0 def test_count_after_adds_and_removes(self): mgr = FloodfillManager() hashes = [os.urandom(32) for _ in range(4)] for h in hashes: mgr.add_floodfill(h, {}) assert mgr.count == 4 mgr.remove_floodfill(hashes[0]) mgr.remove_floodfill(hashes[1]) assert mgr.count == 2 def test_get_all_returns_list(self): mgr = FloodfillManager() hashes = [os.urandom(32) for _ in range(3)] for h in hashes: mgr.add_floodfill(h, {}) all_ffs = mgr.get_all() assert isinstance(all_ffs, list) assert len(all_ffs) == 3 for h in hashes: assert h in all_ffs