Merge pull request #609 from PR0M3TH3AN/codex/add-tests-for-sync-and-vault-handling

Add Nostr sync edge case tests
This commit is contained in:
thePR0M3TH3AN
2025-07-17 15:44:17 -04:00
committed by GitHub
3 changed files with 69 additions and 0 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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()