Add event_id tracking for Nostr chunks

This commit is contained in:
thePR0M3TH3AN
2025-07-17 14:04:32 -04:00
parent 28f27de8e8
commit 49a5329bf6
6 changed files with 39 additions and 9 deletions

View File

@@ -108,6 +108,7 @@ class DummyFilter:
self.ids: list[str] = []
self.limit_val: int | None = None
self.since_val: int | None = None
self.id_called: bool = False
def author(self, _pk):
return self
@@ -125,6 +126,11 @@ class DummyFilter:
self.ids.append(ident)
return self
def id(self, ident: str):
self.id_called = True
self.ids.append(ident)
return self
def limit(self, val: int):
self.limit_val = val
return self
@@ -167,6 +173,7 @@ class DummyRelayClient:
self.manifests: list[DummyEvent] = []
self.chunks: dict[str, DummyEvent] = {}
self.deltas: list[DummyEvent] = []
self.filters: list[DummyFilter] = []
async def add_relays(self, _relays):
pass
@@ -195,6 +202,7 @@ class DummyRelayClient:
elif event.kind == KIND_SNAPSHOT_CHUNK:
ident = event.tags[0] if event.tags else str(self.counter)
self.chunks[ident] = event
self.chunks[eid] = event
elif event.kind == KIND_DELTA:
if not hasattr(event, "created_at"):
self.ts_counter += 1
@@ -203,6 +211,7 @@ class DummyRelayClient:
return DummySendResult(eid)
async def fetch_events(self, f, _timeout):
self.filters.append(f)
kind = getattr(f, "kind_val", None)
limit = getattr(f, "limit_val", None)
identifier = f.ids[0] if getattr(f, "ids", None) else None

View File

@@ -39,7 +39,7 @@ class MockClient:
class FakeId:
def to_hex(self_inner):
return "abcd"
return "a" * 64
class FakeOutput:
def __init__(self):

View File

@@ -7,6 +7,7 @@ from password_manager.entry_management import EntryManager
from password_manager.backup import BackupManager
from password_manager.config_manager import ConfigManager
from nostr.client import prepare_snapshot
from nostr.backup_models import KIND_SNAPSHOT_CHUNK
def test_manifest_generation(tmp_path):
@@ -35,10 +36,18 @@ def test_retrieve_multi_chunk_snapshot(dummy_nostr_client):
data = os.urandom(120000)
manifest, _ = asyncio.run(client.publish_snapshot(data, limit=50000))
assert len(manifest.chunks) > 1
for meta in manifest.chunks:
assert meta.event_id
fetched_manifest, chunk_bytes = asyncio.run(client.fetch_latest_snapshot())
assert len(chunk_bytes) == len(manifest.chunks)
assert [c.event_id for c in fetched_manifest.chunks] == [
c.event_id for c in manifest.chunks
]
joined = b"".join(chunk_bytes)
assert gzip.decompress(joined) == data
for f in relay.filters:
if getattr(f, "kind_val", None) == KIND_SNAPSHOT_CHUNK:
assert f.id_called
def test_publish_and_fetch_deltas(dummy_nostr_client):

View File

@@ -68,6 +68,8 @@ class DummyClient:
def test_fetch_latest_snapshot():
data = b"seedpass" * 1000
manifest, chunks = prepare_snapshot(data, 50000)
for i, m in enumerate(manifest.chunks):
m.event_id = f"{i:064x}"
manifest_json = json.dumps(
{
"ver": manifest.ver,
@@ -98,3 +100,6 @@ def test_fetch_latest_snapshot():
assert manifest == result_manifest
assert result_chunks == chunks
assert [c.event_id for c in manifest.chunks] == [
c.event_id for c in result_manifest.chunks
]