Files
romm/backend/tests/handler/database/test_states_handler.py
Georges-Antoine Assi 20bff0830a fix(streaming): merge the webstation exit delta over the restored save archive (#5673)
* fix(streaming): merge the webstation exit delta over the restored save archive

A webstation exit export holds only the files changed since launch, but
RomM filed it as a standalone .saves.zip and the next launch restored only
the newest zip. A session that exited without an in-game save therefore
filed an archive with no save data, and the next Play booted without it.

The launch now records which native archive it restored on the session
record, and the exit pull lays the delta over that archive before filing
it, so every stored archive is a full snapshot.

Fixes #5544

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(streaming): type the stored exit archive as bytes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(streaming): skip the exit merge when the restored archive was replaced

PUT /saves/{id} rewrites a save in place under the same id, so the exit
merge could combine the session's delta with save data it never loaded.
Record the restored archive's content hash at launch next to its id, and
merge only while the base still has it; otherwise file the delta alone.

Addresses Greptile's review on #5673, including folding the
_over_restored_archive docstring into one sentence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(streaming): drop ensure_zipfile_writable from the archive overlay

#5678 applies the zipfile_inflate64 fix where it is imported, and removed the helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: code-review

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: simplify

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: review-polish

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose (code-review additions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(streaming): keep imported states, folders and the entry cap in the exit merge

A launch that imported a state left the base archive's states off the
container, so the exit merge now leaves them out too. The merge keeps
both archives' folders and refuses past the archive entry limit, so a
merged archive always gets a content hash. The webstation hydrate now
uploads the save its caller resolved instead of looking it up again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(db): query a platform id no sequence can reach

Postgres keeps id sequences across tests, so a worker that has created
about a thousand platforms hands id 999 to the fixture's own platform.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 08:40:47 -05:00

219 lines
7.5 KiB
Python

"""
Unit tests for DBStatesHandler platform filtering functionality.
This module tests the platform filtering fixes for DBStatesHandler to ensure
it properly filters by platform_id through the Rom relationship.
"""
from tests.factories import make_state
from handler.auth.rom_visibility import UNRESTRICTED
from handler.database import db_state_handler
from models.assets import State
from models.platform import Platform
from models.rom import Rom
from models.user import User
class TestDBStatesHandlerPlatformFiltering:
"""Test suite for platform filtering in DBStatesHandler."""
def test_get_states_without_platform_filter(self, admin_user: User, state: State):
"""Test that get_states returns all states when no platform filter is applied."""
states = db_state_handler.get_states(
user_id=admin_user.id, visibility=UNRESTRICTED
)
assert len(states) >= 1
state_ids = [state.id for state in states]
assert state.id in state_ids
def test_get_states_with_platform_filter(
self, admin_user: User, platform: Platform, state: State
):
"""Test that get_states filters correctly by platform_id."""
states = db_state_handler.get_states(
user_id=admin_user.id, platform_id=platform.id, visibility=UNRESTRICTED
)
assert len(states) == 1
assert states[0].id == state.id
assert states[0].file_name == "test_state.state"
def test_get_states_with_rom_ids_and_platform_filter(
self, admin_user: User, platform: Platform, rom: Rom, state: State
):
"""Test that get_states works with both rom_ids and platform_id filters."""
states = db_state_handler.get_states(
user_id=admin_user.id,
rom_ids=[rom.id],
platform_id=platform.id,
visibility=UNRESTRICTED,
)
assert len(states) == 1
assert states[0].id == state.id
def test_get_states_with_nonexistent_platform(self, admin_user: User, state: State):
"""Test that get_states returns empty list for nonexistent platform."""
states = db_state_handler.get_states(
user_id=admin_user.id, platform_id=-1, visibility=UNRESTRICTED
)
assert len(states) == 0
def test_platform_filtering_relationship_integrity(
self, admin_user: User, platform: Platform, rom: Rom, state: State
):
"""Test that platform filtering correctly uses the Rom relationship."""
assert rom.platform_id == platform.id
states_platform = db_state_handler.get_states(
user_id=admin_user.id, platform_id=platform.id, visibility=UNRESTRICTED
)
assert len(states_platform) == 1
def test_multiple_states_same_platform(
self, admin_user: User, platform: Platform, rom: Rom
):
"""Test filtering with multiple states on the same platform."""
make_state(
rom,
admin_user,
"state1.state",
emulator="emulator1",
file_path=f"{platform.slug}/states/emulator1",
file_size_bytes=100,
)
make_state(
rom,
admin_user,
"state2.state",
emulator="emulator2",
file_path=f"{platform.slug}/states/emulator2",
file_size_bytes=200,
)
# Filter by platform should return both states
states = db_state_handler.get_states(
user_id=admin_user.id, platform_id=platform.id, visibility=UNRESTRICTED
)
assert len(states) == 2
state_names = [state.file_name for state in states]
assert "state1.state" in state_names
assert "state2.state" in state_names
def test_get_state_by_filename_with_platform_filter(
self, admin_user: User, rom: Rom, state: State
):
"""Test that get_state_by_filename works correctly with platform filtering."""
retrieved_state = db_state_handler.get_state_by_filename(
user_id=admin_user.id, rom_id=rom.id, file_name=state.file_name
)
assert retrieved_state is not None
assert retrieved_state.id == state.id
assert retrieved_state.file_name == state.file_name
def test_platform_filtering_with_different_emulators(
self, admin_user: User, platform: Platform, rom: Rom
):
"""Test platform filtering with states from different emulators."""
make_state(
rom,
admin_user,
"state_emu1.state",
emulator="emulator1",
file_path=f"{platform.slug}/states/emulator1",
file_size_bytes=100,
)
make_state(
rom,
admin_user,
"state_emu2.state",
emulator="emulator2",
file_path=f"{platform.slug}/states/emulator2",
file_size_bytes=200,
)
# Filter by platform should return both states regardless of emulator
states = db_state_handler.get_states(
user_id=admin_user.id, platform_id=platform.id, visibility=UNRESTRICTED
)
assert len(states) == 2
emulators = [state.emulator for state in states]
assert "emulator1" in emulators
assert "emulator2" in emulators
def test_get_state_by_id_with_platform_context(
self, admin_user: User, platform: Platform, state: State
):
"""Test that get_state works correctly and maintains platform context."""
retrieved_state = db_state_handler.get_state_by_id(state.id)
assert retrieved_state is not None
assert retrieved_state.id == state.id
assert retrieved_state.file_name == "test_state.state"
# Verify the state is associated with the correct platform through ROM
assert retrieved_state.attached_rom.platform_id == platform.id
class TestGetStatesRomIdsScope:
"""Test suite for the `rom_ids` scope on DBStatesHandler.get_states."""
def test_scopes_to_listed_roms(
self, admin_user: User, rom: Rom, state: State, second_state: State
):
states = db_state_handler.get_states(
user_id=admin_user.id, rom_ids=[rom.id], visibility=UNRESTRICTED
)
assert [s.id for s in states] == [state.id]
def test_scopes_to_multiple_roms(
self,
admin_user: User,
rom: Rom,
second_rom: Rom,
state: State,
second_state: State,
):
states = db_state_handler.get_states(
user_id=admin_user.id,
rom_ids=[rom.id, second_rom.id],
visibility=UNRESTRICTED,
)
assert {s.id for s in states} == {state.id, second_state.id}
def test_empty_scope_returns_nothing(self, admin_user: User, state: State):
assert (
db_state_handler.get_states(
user_id=admin_user.id, rom_ids=[], visibility=UNRESTRICTED
)
== []
)
def test_omitted_scope_returns_everything(self, admin_user: User, state: State):
states = db_state_handler.get_states(
user_id=admin_user.id, rom_ids=None, visibility=UNRESTRICTED
)
assert state.id in [s.id for s in states]
def test_combines_with_platform_filter(
self, admin_user: User, platform: Platform, rom: Rom, state: State
):
states = db_state_handler.get_states(
user_id=admin_user.id,
rom_ids=[rom.id],
platform_id=platform.id,
visibility=UNRESTRICTED,
)
assert [s.id for s in states] == [state.id]