From 71f6cdf5d40b51d7b99491c1fe9ccfde73dcda87 Mon Sep 17 00:00:00 2001 From: MMagTech <64668236+MMagTech@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:44:19 -0500 Subject: [PATCH] fix(states): send each state's content hash in the state list (#5668) * fix(states): send each state's content hash in the state list RomM stores a content hash for every state, and the save list already sends each save's. StateSchema never had the field, so GET /api/states, GET /api/states/{id} and a ROM's user_states left it out. A client that keeps its own copies of states can now tell it already has one without downloading it. Co-Authored-By: Claude Opus 5.5 * chore(pr-ready): code-review fixes Document what a state's content_hash covers in the schema and cover the ROM detail user_states/all_user_states in the endpoint test. Co-Authored-By: Claude Opus 5.5 * chore(pr-ready): simplify Declare content_hash once on ManagedAsset, the schema side of the ChannelContent model that owns the column, so saves and states share one documented field. Build the hash test on the state fixture with a single hash value. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Georges-Antoine Assi --- backend/endpoints/responses/assets.py | 3 +- backend/tests/endpoints/test_states.py | 41 +++++++++++++++++++ .../src/__generated__/models/SaveSchema.ts | 5 ++- .../src/__generated__/models/StateSchema.ts | 4 ++ .../__generated__/models/UserSaveSchema.ts | 5 ++- .../__generated__/models/UserStateSchema.ts | 4 ++ 6 files changed, 59 insertions(+), 3 deletions(-) diff --git a/backend/endpoints/responses/assets.py b/backend/endpoints/responses/assets.py index e2bfec804..bc7734a50 100644 --- a/backend/endpoints/responses/assets.py +++ b/backend/endpoints/responses/assets.py @@ -72,12 +72,13 @@ class UserScreenshotSchema(ScreenshotSchema): class ManagedAsset(BaseModel): channel_id: uuid.UUID | None = None """The ID of the sync channel this was pushed to. Null for a backup that the user manages themselves.""" + content_hash: str | None = None + """MD5 of the file's bytes, or for a zip, of its sorted `name:md5` entry lines. Null for a file never hashed.""" class SaveSchema(BaseAsset, AssetAnnotations, ManagedAsset): emulator: str | None slot: str | None = None - content_hash: str | None = None is_public: bool = False screenshot: ScreenshotSchema | None origin_device_id: str | None = None diff --git a/backend/tests/endpoints/test_states.py b/backend/tests/endpoints/test_states.py index d73319882..5b19d7c2c 100644 --- a/backend/tests/endpoints/test_states.py +++ b/backend/tests/endpoints/test_states.py @@ -1,3 +1,4 @@ +import hashlib import os from contextlib import contextmanager from datetime import datetime, timezone @@ -362,6 +363,46 @@ def test_kiosk_mode_anonymous_visitor_cannot_upload_state(client, rom: Rom): assert response.status_code == status.HTTP_403_FORBIDDEN +def test_state_list_detail_and_rom_detail_send_the_content_hash( + client, access_token: str, rom: Rom, state: State +): + content_hash = "a" * 32 + db_state_handler.update_state(state.id, {"content_hash": content_hash}) + + listed = client.get(f"/api/states?rom_id={rom.id}", headers=bearer(access_token)) + single = client.get(f"/api/states/{state.id}", headers=bearer(access_token)) + rom_detail = client.get(f"/api/roms/{rom.id}", headers=bearer(access_token)) + + assert listed.status_code == status.HTTP_200_OK + assert [item["content_hash"] for item in listed.json()] == [content_hash] + assert single.status_code == status.HTTP_200_OK + assert single.json()["content_hash"] == content_hash + assert rom_detail.status_code == status.HTTP_200_OK + for key in ("user_states", "all_user_states"): + assert [item["content_hash"] for item in rom_detail.json()[key]] == [ + content_hash + ] + + +def test_uploaded_state_reports_its_content_hash( + client, access_token: str, rom: Rom, _isolated_assets_dir +): + data = b"STATE BYTES" + + uploaded = client.post( + f"/api/states?rom_id={rom.id}&emulator=test_emulator", + files={"stateFile": ("game.state", data, "application/octet-stream")}, + headers=bearer(access_token), + ) + assert uploaded.status_code == status.HTTP_200_OK + + listed = client.get(f"/api/states?rom_id={rom.id}", headers=bearer(access_token)) + + expected = hashlib.md5(data, usedforsecurity=False).hexdigest() + assert uploaded.json()["content_hash"] == expected + assert [item["content_hash"] for item in listed.json()] == [expected] + + class TestRomIdsScope: def test_scopes_results_to_listed_roms( self, client, access_token: str, rom: Rom, state: State, second_state: State diff --git a/frontend/src/__generated__/models/SaveSchema.ts b/frontend/src/__generated__/models/SaveSchema.ts index 27386aa59..22baa11e5 100644 --- a/frontend/src/__generated__/models/SaveSchema.ts +++ b/frontend/src/__generated__/models/SaveSchema.ts @@ -9,6 +9,10 @@ export type SaveSchema = { * The ID of the sync channel this was pushed to. Null for a backup that the user manages themselves. */ channel_id?: (string | null); + /** + * MD5 of the file's bytes, or for a zip, of its sorted `name:md5` entry lines. Null for a file never hashed. + */ + content_hash?: (string | null); is_favorite?: boolean; /** * The owner's free-text labels. @@ -39,7 +43,6 @@ export type SaveSchema = { updated_at: string; emulator: (string | null); slot?: (string | null); - content_hash?: (string | null); is_public?: boolean; screenshot: (ScreenshotSchema | null); origin_device_id?: (string | null); diff --git a/frontend/src/__generated__/models/StateSchema.ts b/frontend/src/__generated__/models/StateSchema.ts index 503b5b8fb..c96584d95 100644 --- a/frontend/src/__generated__/models/StateSchema.ts +++ b/frontend/src/__generated__/models/StateSchema.ts @@ -8,6 +8,10 @@ export type StateSchema = { * The ID of the sync channel this was pushed to. Null for a backup that the user manages themselves. */ channel_id?: (string | null); + /** + * MD5 of the file's bytes, or for a zip, of its sorted `name:md5` entry lines. Null for a file never hashed. + */ + content_hash?: (string | null); is_favorite?: boolean; /** * The owner's free-text labels. diff --git a/frontend/src/__generated__/models/UserSaveSchema.ts b/frontend/src/__generated__/models/UserSaveSchema.ts index 82e5aec7c..7052d602a 100644 --- a/frontend/src/__generated__/models/UserSaveSchema.ts +++ b/frontend/src/__generated__/models/UserSaveSchema.ts @@ -13,6 +13,10 @@ export type UserSaveSchema = { * The ID of the sync channel this was pushed to. Null for a backup that the user manages themselves. */ channel_id?: (string | null); + /** + * MD5 of the file's bytes, or for a zip, of its sorted `name:md5` entry lines. Null for a file never hashed. + */ + content_hash?: (string | null); is_favorite?: boolean; /** * The owner's free-text labels. @@ -43,7 +47,6 @@ export type UserSaveSchema = { updated_at: string; emulator: (string | null); slot?: (string | null); - content_hash?: (string | null); is_public?: boolean; screenshot: (ScreenshotSchema | null); origin_device_id?: (string | null); diff --git a/frontend/src/__generated__/models/UserStateSchema.ts b/frontend/src/__generated__/models/UserStateSchema.ts index 24d338773..baec60796 100644 --- a/frontend/src/__generated__/models/UserStateSchema.ts +++ b/frontend/src/__generated__/models/UserStateSchema.ts @@ -12,6 +12,10 @@ export type UserStateSchema = { * The ID of the sync channel this was pushed to. Null for a backup that the user manages themselves. */ channel_id?: (string | null); + /** + * MD5 of the file's bytes, or for a zip, of its sorted `name:md5` entry lines. Null for a file never hashed. + */ + content_hash?: (string | null); is_favorite?: boolean; /** * The owner's free-text labels.