clone

a self-hosted multitenant nostr relay with sites, file storage, git hosting, and inbox and outbox delivery

feat/ui-updates
tinyrelay / tinyagent / hermes / tests / test_ingress.py
test_ingress.py 2.5K
1import json2import hashlib3import sys4from pathlib import Path56sys.path.insert(0, str(Path(__file__).parents[1]))78from ingress import EventTracker, event_thread, is_reply_to_own, reply_parent91011def event(event_id, created=100, tags=None):12 return {"id": hashlib.sha256(event_id.encode()).hexdigest(), "created_at": created, "tags": tags or []}131415def test_first_tracker_does_not_replay_history(tmp_path):16 tracker = EventTracker("http://relay", "a" * 64, ["lab"], state_dir=tmp_path, now=100)17 assert not tracker.should_accept(event("old", 99))18 assert tracker.should_accept(event("same", 100))192021def test_persisted_cursor_overlaps_timestamp_and_deduplicates(tmp_path):22 first = EventTracker("r", "a", ["lab"], state_dir=tmp_path, now=100)23 first.mark(event("one", 101))24 second = EventTracker("r", "a", ["lab"], state_dir=tmp_path, now=100)25 assert second.filter_since()["since"] == 10026 assert second.should_accept(event("two", 101))27 assert not second.should_accept(event("one", 101))282930def test_expiration_and_bounded_seen(tmp_path):31 tracker = EventTracker("r", "a", ["lab"], state_dir=tmp_path, now=100, max_seen=2)32 assert not tracker.should_accept(event("expired", 100, [["expiration", "100"]]))33 for index in range(3):34 tracker.mark(event(str(index), 101 + index))35 assert len(json.loads(tracker.path.read_text())["seen"]) == 2363738def test_nip10_root_reply_and_own_parent():39 root = "r" * 6440 parent = "p" * 6441 child = event("c", tags=[["e", root, "", "root"], ["e", parent, "", "reply"]])42 assert event_thread(child) == root43 assert reply_parent(child) == parent44 assert is_reply_to_own(child, {parent})454647def test_overlap_accepts_out_of_order_events_and_survives_restart(tmp_path):48 tracker = EventTracker('r', 'a', ['lab'], state_dir=tmp_path, now=100)49 tracker.mark(event('newer', 400))50 assert tracker.should_accept(event('earlier', 200))51 tracker.mark(event('earlier', 200))52 restored = EventTracker('r', 'a', ['lab'], state_dir=tmp_path, now=500)53 assert restored.filter_since()['since'] == 10054 assert not restored.should_accept(event('earlier', 200))55 assert restored.should_accept(event('same-time', 400))565758def test_evicted_ids_cannot_become_commands_again(tmp_path):59 tracker = EventTracker('r', 'a', ['lab'], state_dir=tmp_path, now=100, max_seen=2)60 for i in range(3): tracker.mark(event(str(i), 101 + i))61 assert not tracker.should_accept(event('0', 101))62 assert tracker.should_accept(event('next', 104))