mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 10:01:00 +01:00
* feat(saves): add save channels and snapshots * feat(saves): close spec gaps in save channels and hash with sigil * feat(saves): play snapshots in the v2 player, which pushes snapshots * fix(saves): code review: authorization, retention losses, held-save sync, batched responses * refactor(saves): simplify pass * refactor(saves): review-polish pass * docs(saves): humanizer pass * feat(saves): run every EmulatorJS page on the v2 player * feat(saves): per-user snapshot pins * feat(sigil): store the identity a save restore needs and pin sigil 0f05f25 * fix(saves): meet type-aware lint in the v2 player * feat(sigil): store N64 header and playlist title ids, pin sigil 0847fda * feat(saves): convert saves for a core on download and list save layouts * feat(saves): prune unreachable archival snapshots and empty detached channels on delete * feat(saves): device sync from the token, held snapshots and shared-card pushes * feat(saves): arc restores past abandoned snapshots and color channels by age * fix(saves): code-review and security-audit fixes * refactor(saves): simplify pass * chore(saves): review-polish pass * docs(saves): humanizer pass * fix(sync): file RetroArch autosave versions as their channel's next snapshot * fix(saves): address review findings and trim docstrings * fix(saves): share-lock planned rows by id so PostgreSQL accepts the lock * test(saves): stub the detached-channels fetch in SaveDataTab stories * feat(saves): carry the full current snapshot in a 409, pin sigil 2071dff * chore(sigil): pin 5d4ddb4, which reads .cue and .gdi sheets * docs(saves): drop v2 wording now that one UI remains * chore(sigil): pin 0190443, which reads PS2 raw BIN images and cue sheets * fix(sigil): treat a file too short for its format as routine during scans * chore(migrations): renumber save channel migrations after upstream's 0154 * fix(snapshots): a hardcore switch on unchanged content makes a new current * fix(sync): decide a channel's slot from its history and the device's snapshot * chore(docker): pin sigil at 794d99f for PSP Store release save prefixes * refactor(db): squash the save channel migrations into 0161 Fold the slot linking (0162) and the rom_files sigil identity columns (0163) into 0161_save_channels, so the feature ships as one revision. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(saves): close visibility and input gaps found in review (pr-ready step 2) code-review --fix: - negotiate_sync drops channel slots on ROMs hidden from the caller - channel PATCH/attach/DELETE answer 404 when the channel's ROM is hidden - a push copying a save from a hidden ROM answers 404 - _own_device applies require_token_device to bound client tokens - adopting a legacy save syncs its sharing with the channel it joins security-audit follow-ups: - another user's detached public save or state is no longer downloadable - profile and option values must be a single path segment - companions are de-duplicated and capped at 8 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(saves): share channel and asset lookups, drop duplicates (pr-ready step 3) simplify: - readable_channel_or_404 and channels_on_files replace four copies of the channel 404 check and two copies of the file-fed channel listing - readable_rom_asset_or_404 serves both public save and state downloads - _own_device resolves through require_device; _clone_origin takes the source - reuse _unprocessable, DESCRIPTOR_EXTENSIONS, SAVE_SLOT_MAX_LENGTH, uniqueBy and uuidv7; neutral forms default their required pattern to the allowed one - RetroArch delete checks held rows in one query, a guarded upload lists the slot only without a channel head, and uploads are read off the event loop - snapshotSession builds push requests once; player comment cleanups Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(saves): trim two docstrings to two lines (pr-ready step 4) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(saves): answer held RetroArch paths with 423, mirror slot labels - RetroArch Cloud Sync answers 423 Locked for a PUT over a held unslotted save and for a DELETE where only held rows sit at the path, instead of 409 and 404 - channelLabelForSlot returns null for blank and state_ slots, as the backend's channel_label does, so the player files them under no channel either - document that sharing a channel lets every user read and push into it Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(docker): pin sigil at 85f88a8 for Switch .nup containers * fix(snapshots): a save pushed in another shape or format makes a new current * refactor(saves): drop the always-true SAVE_SYNC.SNAPSHOTS heartbeat flag Nothing reads it, and the SAVE_SYNC block's presence already tells a client the server speaks the snapshot API. Also trim the rom detail channel field comments to one line each. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(frontend): use uuidv7 for every client-generated id Drop the randomToken wrapper and the notification inbox's Math.random tab id, so uuidv7 is the one helper that works outside a secure context. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(saves): trim redundant snapshot API surface, dead code and duplicate logic API: - Drop PUT /snapshots/{id}/devices/{device_id}; GET /snapshots/{id}?hold=true holds. - GET /snapshots is history only (channel_id required); GET /channels serves currents. - A snapshot carries channel_id instead of an embedded channel. - POST /saves/{id}/content takes the same query parameters as the GET, plus container_path. Backend: - Remove the archival write path, clone.py, DBStatesHandler.get_state, the sigil identity-hash path, unused relationships and composite snapshot indexes. - One held/frozen lookup, one bulk-delete guard, SQLAlchemy dialect upserts, and the shared reserve_version_name in the writer. - The writer takes a visibility filter and checks the channel and parent itself. - The restore handler picks per-game or merge; fewer wrapper types and tables. Frontend: - Channel delete uses the shared typed confirm; DeleteChannelDialog is gone. - Held offline pushes go through a per-channel SnapshotSession. - Remove the slot-based pending upload path. - The timeline shares the fan's badge logic. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * refactor(saves): release archival backups through module functions The saves and states handlers each built a private DBSnapshotsHandler to call release_backup and refresh_backup_digests, since they can't import the shared instance. Both only ever ran inside the caller's session, so they are now module functions taking it, like channel_for_slot. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * refactor(saves): share one deleted-assets handler instance The saves handler built its own DBDeletedAssetsHandler because it can't import the package's instance. The instance is now built in deleted_assets_handler.py and re-exported by the package, so both use it. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> --------- Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
705 lines
21 KiB
Python
705 lines
21 KiB
Python
"""The held, latest known and pinned contract, over HTTP with a device-bound token."""
|
|
|
|
import json
|
|
import uuid
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from fastapi import status
|
|
from fastapi.testclient import TestClient
|
|
from httpx2 import Response
|
|
from tests.factories import make_device_token
|
|
from tests.handler.snapshots.pushes import md5
|
|
|
|
from handler.database import db_device_handler, db_rom_handler, db_snapshot_handler
|
|
from handler.snapshots import retention
|
|
from models.device import Device
|
|
from models.device_channel_sync import DeviceChannelSync
|
|
from models.rom import Rom, RomFile
|
|
from models.user import User
|
|
|
|
pytestmark = pytest.mark.usefixtures("_isolated_assets_dir")
|
|
|
|
DEVICE_SCOPES = "roms.read assets.read assets.write"
|
|
|
|
|
|
@pytest.fixture
|
|
def game_file(rom: Rom) -> RomFile:
|
|
return db_rom_handler.add_rom_file(
|
|
RomFile(
|
|
rom_id=rom.id,
|
|
file_name="game.sfc",
|
|
file_path=rom.fs_path,
|
|
file_size_bytes=1024,
|
|
sha1_hash="b" * 40,
|
|
)
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def device(admin_user: User) -> Device:
|
|
return db_device_handler.add_device(
|
|
Device(id="odin", user_id=admin_user.id, name="Odin", client="argosy")
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def bound(admin_user: User, device: Device) -> dict[str, str]:
|
|
_, raw = make_device_token(admin_user, device.id, scopes=DEVICE_SCOPES)
|
|
return {"Authorization": f"Bearer {raw}"}
|
|
|
|
|
|
def push(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
game_file: RomFile,
|
|
data: bytes,
|
|
**fields: Any,
|
|
) -> Response:
|
|
"""A save push that leaves `expected_current_id` out unless `fields` names it."""
|
|
body = {
|
|
"rom_file_id": game_file.id,
|
|
"save": {"hash": md5(data), "shape": "SINGLE", "format": "neutral"},
|
|
**fields,
|
|
}
|
|
return client.post(
|
|
"/api/snapshots",
|
|
data={"manifest": json.dumps(body)},
|
|
files={"save": ("game.srm", data)},
|
|
headers=headers,
|
|
)
|
|
|
|
|
|
Snapshot = dict[str, Any]
|
|
|
|
|
|
def start(client: TestClient, headers: dict[str, str], game_file: RomFile) -> Snapshot:
|
|
response = push(
|
|
client,
|
|
headers,
|
|
game_file,
|
|
b"start",
|
|
label="default",
|
|
expected_current_id=None,
|
|
)
|
|
assert response.status_code == status.HTTP_201_CREATED, response.text
|
|
body: Snapshot = response.json()
|
|
return body
|
|
|
|
|
|
def follow(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
game_file: RomFile,
|
|
after: Snapshot,
|
|
data: bytes,
|
|
) -> Snapshot:
|
|
"""A push from the web, with no device, on top of `after`."""
|
|
response = push(
|
|
client,
|
|
headers,
|
|
game_file,
|
|
data,
|
|
channel_id=after["channel_id"],
|
|
expected_current_id=after["id"],
|
|
)
|
|
assert response.status_code == status.HTTP_201_CREATED, response.text
|
|
body: Snapshot = response.json()
|
|
return body
|
|
|
|
|
|
def chain(
|
|
client: TestClient, headers: dict[str, str], game_file: RomFile, length: int
|
|
) -> list[Snapshot]:
|
|
pushed = [start(client, headers, game_file)]
|
|
for step in range(1, length):
|
|
pushed.append(
|
|
follow(client, headers, game_file, pushed[-1], f"step-{step}".encode())
|
|
)
|
|
return pushed
|
|
|
|
|
|
def hold(client: TestClient, headers: dict[str, str], id: int) -> Response:
|
|
return client.get(f"/api/snapshots/{id}", params={"hold": True}, headers=headers)
|
|
|
|
|
|
def held_by(client: TestClient, headers: dict[str, str], id: int) -> list[str]:
|
|
response = client.get(f"/api/snapshots/{id}", headers=headers)
|
|
assert response.status_code == status.HTTP_200_OK, response.text
|
|
return [held["device"]["id"] for held in response.json()["held_by"]]
|
|
|
|
|
|
def sync(device_id: str, channel_id: str) -> DeviceChannelSync | None:
|
|
return db_snapshot_handler.get_device_sync(device_id, uuid.UUID(channel_id))
|
|
|
|
|
|
def assert_conflict(response: Response, current_id: int, reason: str) -> Snapshot:
|
|
assert response.status_code == status.HTTP_409_CONFLICT, response.text
|
|
body: Snapshot = response.json()
|
|
assert set(body) >= {"current", "branch", "reason"}
|
|
assert body["current"]["id"] == current_id
|
|
assert body["current"]["digest"]
|
|
assert body["branch"]["kind"] == "branch"
|
|
assert body["reason"] == reason
|
|
return body
|
|
|
|
|
|
# Held
|
|
|
|
|
|
def test_a_push_holds_the_current_it_created(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
first = start(client, bound, game_file)
|
|
second = push(
|
|
client, bound, game_file, b"next", channel_id=first["channel_id"]
|
|
).json()
|
|
|
|
assert second["kind"] == "channel"
|
|
assert held_by(client, headers, first["id"]) == []
|
|
assert held_by(client, headers, second["id"]) == [device.id]
|
|
|
|
|
|
def test_a_push_that_branches_holds_the_branch(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
first = start(client, bound, game_file)
|
|
follow(client, headers, game_file, first, b"elsewhere")
|
|
|
|
response = push(client, bound, game_file, b"mine", channel_id=first["channel_id"])
|
|
branch = response.json()["branch"]
|
|
|
|
assert held_by(client, headers, branch["id"]) == [device.id]
|
|
assert held_by(client, headers, first["id"]) == []
|
|
|
|
|
|
def test_a_plain_fetch_holds_nothing(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
first, second = chain(client, headers, game_file, 2)
|
|
|
|
for snapshot in (first, second):
|
|
assert client.get(f"/api/snapshots/{snapshot['id']}", headers=bound).is_success
|
|
|
|
assert sync(device.id, first["channel_id"]) is None
|
|
assert held_by(client, headers, first["id"]) == []
|
|
assert held_by(client, headers, second["id"]) == []
|
|
|
|
|
|
def test_a_download_with_hold_holds_it(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
first, _ = chain(client, headers, game_file, 2)
|
|
|
|
response = hold(client, bound, first["id"])
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert [h["device"]["id"] for h in response.json()["held_by"]] == [device.id]
|
|
|
|
|
|
def test_holding_another_snapshot_replaces_it(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
first, second = chain(client, headers, game_file, 2)
|
|
hold(client, bound, first["id"])
|
|
|
|
hold(client, bound, second["id"])
|
|
|
|
assert held_by(client, headers, first["id"]) == []
|
|
assert held_by(client, headers, second["id"]) == [device.id]
|
|
|
|
|
|
def test_clearing_held_leaves_the_device_holding_nothing(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
first = start(client, bound, game_file)
|
|
|
|
response = client.delete(f"/api/channels/{first['channel_id']}/held", headers=bound)
|
|
|
|
assert response.status_code == status.HTTP_204_NO_CONTENT
|
|
assert held_by(client, headers, first["id"]) == []
|
|
assert sync(device.id, first["channel_id"]) is None
|
|
|
|
|
|
def test_a_read_only_token_can_hold_and_clear(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
admin_user: User,
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
first = start(client, headers, game_file)
|
|
_, raw = make_device_token(admin_user, device.id, scopes="roms.read assets.read")
|
|
read_only = {"Authorization": f"Bearer {raw}"}
|
|
|
|
held = hold(client, read_only, first["id"])
|
|
cleared = client.delete(
|
|
f"/api/channels/{first['channel_id']}/held", headers=read_only
|
|
)
|
|
|
|
assert held.status_code == status.HTTP_200_OK
|
|
assert cleared.status_code == status.HTTP_204_NO_CONTENT
|
|
assert sync(device.id, first["channel_id"]) is None
|
|
|
|
|
|
def test_a_bound_token_cannot_hold_for_another_device(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
admin_user: User,
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
other = db_device_handler.add_device(Device(id="loki", user_id=admin_user.id))
|
|
first = start(client, headers, game_file)
|
|
_, raw = make_device_token(
|
|
admin_user, device.id, scopes=f"{DEVICE_SCOPES} devices.write"
|
|
)
|
|
|
|
response = client.get(
|
|
f"/api/snapshots/{first['id']}",
|
|
params={"hold": True, "device_id": other.id},
|
|
headers={"Authorization": f"Bearer {raw}"},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
assert sync(other.id, first["channel_id"]) is None
|
|
|
|
|
|
def test_held_by_lists_only_the_viewers_devices(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
editor_headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
editor_user: User,
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
first = start(client, bound, game_file)
|
|
client.patch(
|
|
f"/api/channels/{first['channel_id']}",
|
|
json={"is_public": True},
|
|
headers=headers,
|
|
)
|
|
editor_device = db_device_handler.add_device(
|
|
Device(id="editor-handheld", user_id=editor_user.id, name="Handheld")
|
|
)
|
|
_, raw = make_device_token(editor_user, editor_device.id, scopes=DEVICE_SCOPES)
|
|
hold(client, {"Authorization": f"Bearer {raw}"}, first["id"])
|
|
|
|
assert held_by(client, headers, first["id"]) == [device.id]
|
|
assert held_by(client, editor_headers, first["id"]) == [editor_device.id]
|
|
|
|
|
|
# Latest known
|
|
|
|
|
|
@pytest.mark.parametrize("listing", ["channels", "history"])
|
|
def test_listing_records_the_latest_known_and_keeps_what_is_held(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
device: Device,
|
|
listing: str,
|
|
):
|
|
first = start(client, bound, game_file)
|
|
channel_id = first["channel_id"]
|
|
second = follow(client, headers, game_file, first, b"elsewhere")
|
|
path, params = {
|
|
"channels": ("/api/channels", {"rom_file_id": game_file.id}),
|
|
"history": ("/api/snapshots", {"channel_id": channel_id}),
|
|
}[listing]
|
|
|
|
response = client.get(path, params=params, headers=bound)
|
|
recorded = sync(device.id, channel_id)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert recorded is not None
|
|
assert recorded.latest_known_id == second["id"]
|
|
assert recorded.base_snapshot_id == first["id"]
|
|
assert held_by(client, headers, first["id"]) == [device.id]
|
|
|
|
|
|
def test_a_history_page_past_the_newest_records_nothing(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
_, second, third = chain(client, headers, game_file, 3)
|
|
|
|
client.get(
|
|
"/api/snapshots",
|
|
params={"channel_id": third["channel_id"], "cursor": str(second["id"])},
|
|
headers=bound,
|
|
)
|
|
|
|
assert sync(device.id, third["channel_id"]) is None
|
|
|
|
|
|
def test_a_branching_push_records_the_current_it_missed(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
first = start(client, bound, game_file)
|
|
second = follow(client, headers, game_file, first, b"elsewhere")
|
|
|
|
push(client, bound, game_file, b"mine", channel_id=first["channel_id"])
|
|
recorded = sync(device.id, first["channel_id"])
|
|
|
|
assert recorded is not None
|
|
assert recorded.latest_known_id == second["id"]
|
|
|
|
|
|
def test_holding_an_older_snapshot_records_the_current_as_latest_known(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
first, second = chain(client, headers, game_file, 2)
|
|
|
|
hold(client, bound, first["id"])
|
|
recorded = sync(device.id, first["channel_id"])
|
|
|
|
assert recorded is not None
|
|
assert recorded.latest_known_id == second["id"]
|
|
|
|
|
|
def test_a_push_after_holding_an_older_snapshot_becomes_the_current(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
):
|
|
first, second = chain(client, headers, game_file, 2)
|
|
hold(client, bound, first["id"])
|
|
|
|
response = push(client, bound, game_file, b"older", channel_id=first["channel_id"])
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED, response.text
|
|
assert response.json()["parent_snapshot_id"] == first["id"]
|
|
|
|
|
|
def test_listing_never_changes_what_is_held(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
first, second = chain(client, headers, game_file, 2)
|
|
hold(client, bound, first["id"])
|
|
before = sync(device.id, first["channel_id"])
|
|
|
|
client.get("/api/channels", params={"rom_file_id": game_file.id}, headers=bound)
|
|
after = sync(device.id, first["channel_id"])
|
|
|
|
assert before is not None and after is not None
|
|
assert after.base_snapshot_id == first["id"]
|
|
assert after.synced_at == before.synced_at
|
|
assert held_by(client, headers, second["id"]) == []
|
|
|
|
|
|
# The push table
|
|
|
|
|
|
def test_known_same_held_same_becomes_the_current(
|
|
client: TestClient,
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
):
|
|
first = start(client, bound, game_file)
|
|
|
|
response = push(client, bound, game_file, b"next", channel_id=first["channel_id"])
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED, response.text
|
|
body = response.json()
|
|
assert body["kind"] == "channel"
|
|
assert body["parent_snapshot_id"] == first["id"]
|
|
assert body["kind"] == "channel"
|
|
|
|
|
|
def test_known_same_held_older_becomes_the_current_keeping_the_replaced(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
):
|
|
first, second = chain(client, headers, game_file, 2)
|
|
channel_id = first["channel_id"]
|
|
client.get("/api/snapshots", params={"channel_id": channel_id}, headers=bound)
|
|
hold(client, bound, first["id"])
|
|
|
|
response = push(client, bound, game_file, b"older", channel_id=channel_id)
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED, response.text
|
|
body = response.json()
|
|
assert body["parent_snapshot_id"] == first["id"]
|
|
assert body["kind"] == "channel"
|
|
history = client.get(
|
|
"/api/snapshots", params={"channel_id": channel_id}, headers=headers
|
|
).json()
|
|
assert second["id"] in [snapshot["id"] for snapshot in history]
|
|
|
|
|
|
def test_known_different_held_same_is_a_moved_branch(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
):
|
|
first = start(client, bound, game_file)
|
|
second = follow(client, headers, game_file, first, b"elsewhere")
|
|
|
|
response = push(client, bound, game_file, b"mine", channel_id=first["channel_id"])
|
|
|
|
body = assert_conflict(response, second["id"], "moved")
|
|
assert body["branch"]["parent_snapshot_id"] == first["id"]
|
|
|
|
|
|
def test_known_different_held_older_is_a_moved_from_older_branch(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
):
|
|
first, second = chain(client, headers, game_file, 2)
|
|
channel_id = first["channel_id"]
|
|
client.get("/api/snapshots", params={"channel_id": channel_id}, headers=bound)
|
|
hold(client, bound, first["id"])
|
|
third = follow(client, headers, game_file, second, b"elsewhere")
|
|
|
|
response = push(client, bound, game_file, b"older", channel_id=channel_id)
|
|
|
|
body = assert_conflict(response, third["id"], "moved_from_older")
|
|
assert body["branch"]["parent_snapshot_id"] == first["id"]
|
|
|
|
|
|
def test_with_no_sync_record_a_parent_on_the_current_becomes_the_current(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
_, second = chain(client, headers, game_file, 2)
|
|
assert sync(device.id, second["channel_id"]) is None
|
|
|
|
response = push(
|
|
client,
|
|
bound,
|
|
game_file,
|
|
b"next",
|
|
channel_id=second["channel_id"],
|
|
parent_snapshot_id=second["id"],
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED, response.text
|
|
assert response.json()["kind"] == "channel"
|
|
|
|
|
|
@pytest.mark.parametrize("parent", ["older", "none"])
|
|
def test_with_no_sync_record_any_other_parent_lands_as_a_branch(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
parent: str,
|
|
):
|
|
first, second = chain(client, headers, game_file, 2)
|
|
fields = {"parent_snapshot_id": first["id"]} if parent == "older" else {}
|
|
|
|
response = push(
|
|
client, bound, game_file, b"next", channel_id=first["channel_id"], **fields
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_409_CONFLICT, response.text
|
|
assert response.json()["current"]["id"] == second["id"]
|
|
assert response.json()["branch"]["parent_snapshot_id"] == (
|
|
first["id"] if parent == "older" else None
|
|
)
|
|
|
|
|
|
def test_an_explicit_stale_expected_overrides_a_device_on_the_current(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
):
|
|
first, second = chain(client, headers, game_file, 2)
|
|
hold(client, bound, second["id"])
|
|
|
|
response = push(
|
|
client,
|
|
bound,
|
|
game_file,
|
|
b"mine",
|
|
channel_id=first["channel_id"],
|
|
expected_current_id=first["id"],
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_409_CONFLICT, response.text
|
|
assert response.json()["current"]["id"] == second["id"]
|
|
|
|
|
|
def test_an_explicit_current_expected_overrides_a_device_that_missed_a_push(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
):
|
|
first = start(client, bound, game_file)
|
|
second = follow(client, headers, game_file, first, b"elsewhere")
|
|
|
|
response = push(
|
|
client,
|
|
bound,
|
|
game_file,
|
|
b"mine",
|
|
channel_id=first["channel_id"],
|
|
expected_current_id=second["id"],
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED, response.text
|
|
assert response.json()["parent_snapshot_id"] == second["id"]
|
|
|
|
|
|
# Retention and pins
|
|
|
|
|
|
@pytest.fixture
|
|
def small_retention(monkeypatch):
|
|
monkeypatch.setattr(retention, "SNAPSHOT_RETENTION", 2)
|
|
|
|
|
|
def exists(id: int) -> bool:
|
|
return db_snapshot_handler.get_snapshot(id) is not None
|
|
|
|
|
|
@pytest.mark.usefixtures("small_retention")
|
|
def test_retention_prunes_a_held_unpinned_snapshot_and_the_device_holds_nothing(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
first = start(client, bound, game_file)
|
|
channel_id = first["channel_id"]
|
|
second = follow(client, headers, game_file, first, b"two")
|
|
third = follow(client, headers, game_file, second, b"three")
|
|
|
|
recorded = sync(device.id, channel_id)
|
|
for kept in (second, third):
|
|
assert held_by(client, headers, kept["id"]) == []
|
|
branched = push(client, bound, game_file, b"mine", channel_id=channel_id)
|
|
|
|
assert not exists(first["id"])
|
|
assert recorded is not None
|
|
assert recorded.base_snapshot_id is None
|
|
assert branched.status_code == status.HTTP_409_CONFLICT, branched.text
|
|
assert branched.json()["branch"]["parent_snapshot_id"] is None
|
|
|
|
|
|
@pytest.mark.usefixtures("small_retention")
|
|
def test_after_a_prune_a_parent_on_the_current_becomes_the_current(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
):
|
|
first = start(client, bound, game_file)
|
|
second = follow(client, headers, game_file, first, b"two")
|
|
third = follow(client, headers, game_file, second, b"three")
|
|
|
|
response = push(
|
|
client,
|
|
bound,
|
|
game_file,
|
|
b"mine",
|
|
channel_id=first["channel_id"],
|
|
parent_snapshot_id=third["id"],
|
|
)
|
|
|
|
assert not exists(first["id"])
|
|
assert response.status_code == status.HTTP_201_CREATED, response.text
|
|
|
|
|
|
@pytest.mark.usefixtures("small_retention")
|
|
def test_a_held_snapshot_the_owner_pinned_survives_retention(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
bound: dict[str, str],
|
|
game_file: RomFile,
|
|
device: Device,
|
|
):
|
|
first = start(client, bound, game_file)
|
|
client.patch(
|
|
f"/api/snapshots/{first['id']}", json={"is_pinned": True}, headers=headers
|
|
)
|
|
latest = first
|
|
for step in range(4):
|
|
latest = follow(client, headers, game_file, latest, f"s{step}".encode())
|
|
|
|
assert exists(first["id"])
|
|
assert held_by(client, headers, first["id"]) == [device.id]
|
|
|
|
|
|
@pytest.mark.usefixtures("small_retention")
|
|
def test_another_readers_pin_keeps_a_snapshot_until_unsharing_drops_it(
|
|
client: TestClient,
|
|
headers: dict[str, str],
|
|
editor_headers: dict[str, str],
|
|
game_file: RomFile,
|
|
):
|
|
first = start(client, headers, game_file)
|
|
channel_path = f"/api/channels/{first['channel_id']}"
|
|
client.patch(channel_path, json={"is_public": True}, headers=headers)
|
|
pinned = client.patch(
|
|
f"/api/snapshots/{first['id']}",
|
|
json={"is_pinned": True},
|
|
headers=editor_headers,
|
|
).json()
|
|
owners_view = client.get(f"/api/snapshots/{first['id']}", headers=headers).json()
|
|
latest = first
|
|
for step in range(3):
|
|
latest = follow(client, headers, game_file, latest, f"s{step}".encode())
|
|
survived = exists(first["id"])
|
|
|
|
client.patch(channel_path, json={"is_public": False}, headers=headers)
|
|
follow(client, headers, game_file, latest, b"after-unshare")
|
|
|
|
assert (pinned["is_pinned"], owners_view["is_pinned"]) == (True, False)
|
|
assert survived
|
|
assert not exists(first["id"])
|