Files
romm/backend/tests/handler/sync/test_ssh_handler.py
Sam Dornan 5b3d0bf544 feat(sync): record a per-device save baseline to prove what did not change (#4768)
compare_save_state decides a side changed from its timestamp, and
emulators rewrite a save on exit, so an untouched save looks new and a
server change elsewhere turns into a false conflict.

device_save_sync now records what each side held at the last sync
(last_sync_hash from the device, last_sync_server_hash from the server,
migration 0140). A baseline match proves that side unchanged; a mismatch
or NULL falls back to the timestamp logic, so this only removes
conflicts. It runs after the removed_at check in compare_save_state.

- Baselines are recorded only for versions the device provably holds:
  confirm_download pins to the served version, update_save records the
  client hash only when the file was uploaded, deduplicated uploads and
  pushed saves record against the surviving save, and push-pull no-ops
  record hashes only for identical content.
- Identical-content no-ops in negotiate and the watcher record a
  boundary without re-tracking untracked saves.
- The SSH push-pull and file-transfer watcher paths hash device saves
  through hash_save_file, matching compute_content_hash for zipped
  saves, and treat unreadable saves as unknown.

Squashes rommapp/romm#4768 onto master.



Claude-Session: https://claude.ai/code/session_01WAKUdfLb6fyduEqaFbf5A8

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 15:05:57 -05:00

56 lines
1.7 KiB
Python

import hashlib
import shutil
import zipfile
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from anyio import Path as AnyioPath
from tests._zipfile_shim import reload_zipfile
from handler.filesystem.assets_handler import hash_save_file
from handler.sync.ssh_handler import SSHSyncHandler
class _FakeSFTP:
def __init__(self, source: Path):
self._source = source
async def __aenter__(self):
return self
async def __aexit__(self, *exc):
return False
async def get(self, remote_path: str, local_path: str) -> None:
shutil.copyfile(self._source, local_path)
class TestDownloadSave:
@pytest.fixture
def remote_zip(self, tmp_path: Path) -> Path:
path = tmp_path / "remote.zip"
reload_zipfile()
with zipfile.ZipFile(path, "w", compression=zipfile.ZIP_STORED) as zf:
zf.writestr("card/save.bin", b"identical save contents" * 64)
return path
@pytest.mark.asyncio
async def test_a_zipped_save_is_hashed_like_the_server_save(
self, tmp_path: Path, remote_zip: Path
):
remote_bytes = await AnyioPath(remote_zip).read_bytes()
conn = MagicMock()
conn.start_sftp_client.return_value = _FakeSFTP(remote_zip)
handler = SSHSyncHandler.__new__(SSHSyncHandler)
local_path, content_hash = await handler.download_save(
conn, "/saves/remote.zip", str(tmp_path / "local.zip")
)
assert await AnyioPath(local_path).read_bytes() == remote_bytes
assert content_hash == hash_save_file(remote_zip)
assert (
content_hash != hashlib.md5(remote_bytes, usedforsecurity=False).hexdigest()
)