From 576437223a03e7131c71b147d21c4fb94e69101f Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Thu, 17 Jul 2025 15:39:05 -0400 Subject: [PATCH] Add tests for sync and snapshot functionality --- src/tests/test_nostr_dummy_client.py | 27 +++++++++++++++++++++++++++ src/tests/test_post_sync_messages.py | 21 +++++++++++++++++++++ src/tests/test_profiles.py | 21 +++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/src/tests/test_nostr_dummy_client.py b/src/tests/test_nostr_dummy_client.py index ed6ccd5..5284a1e 100644 --- a/src/tests/test_nostr_dummy_client.py +++ b/src/tests/test_nostr_dummy_client.py @@ -105,3 +105,30 @@ def test_fetch_snapshot_fallback_on_missing_chunk(dummy_nostr_client, monkeypatc ) ) assert attempts == 3 + + +def test_fetch_snapshot_uses_event_ids(dummy_nostr_client): + import os + import gzip + + client, relay = dummy_nostr_client + + data = os.urandom(60000) + manifest, _ = asyncio.run(client.publish_snapshot(data)) + + # Remove identifier keys so chunks can only be fetched via event_id + for meta in manifest.chunks: + relay.chunks.pop(meta.id, None) + + relay.filters.clear() + + fetched_manifest, chunk_bytes = asyncio.run(client.fetch_latest_snapshot()) + + assert gzip.decompress(b"".join(chunk_bytes)) == data + + id_filters = [ + f.id_called + for f in relay.filters + if getattr(f, "kind_val", None) == KIND_SNAPSHOT_CHUNK + ] + assert id_filters and all(id_filters) diff --git a/src/tests/test_post_sync_messages.py b/src/tests/test_post_sync_messages.py index c0273d5..d45b74b 100644 --- a/src/tests/test_post_sync_messages.py +++ b/src/tests/test_post_sync_messages.py @@ -29,3 +29,24 @@ def test_handle_post_failure(capsys): main.handle_post_to_nostr(pm) out = capsys.readouterr().out assert "❌ Sync failed…" in out + + +def test_handle_post_prints_all_ids(capsys): + pm = SimpleNamespace( + sync_vault=lambda alt_summary=None: { + "manifest_id": "m1", + "chunk_ids": ["c1", "c2"], + "delta_ids": ["d1", "d2"], + } + ) + main.handle_post_to_nostr(pm) + out_lines = capsys.readouterr().out.splitlines() + expected = [ + " manifest: m1", + " chunk: c1", + " chunk: c2", + " delta: d1", + " delta: d2", + ] + for line in expected: + assert any(line in ol for ol in out_lines) diff --git a/src/tests/test_profiles.py b/src/tests/test_profiles.py index d44e9b7..aec32c8 100644 --- a/src/tests/test_profiles.py +++ b/src/tests/test_profiles.py @@ -85,3 +85,24 @@ def test_sync_index_missing_bad_data(monkeypatch, dummy_nostr_client): assert result is False index_path = dir_path / "seedpass_entries_db.json.enc" assert not index_path.exists() + + +def test_attempt_initial_sync_incomplete_data(monkeypatch, dummy_nostr_client): + client, _relay = dummy_nostr_client + with TemporaryDirectory() as tmpdir: + dir_path = Path(tmpdir) + vault, _enc = create_vault(dir_path) + + pm = PasswordManager.__new__(PasswordManager) + pm.fingerprint_dir = dir_path + pm.vault = vault + pm.nostr_client = client + pm.sync_vault = lambda *a, **k: None + + # Simulate relay snapshot retrieval failure due to missing chunks + monkeypatch.setattr(client, "fetch_latest_snapshot", lambda: None) + + result = pm.attempt_initial_sync() + assert result is False + index_path = dir_path / "seedpass_entries_db.json.enc" + assert not index_path.exists()