Files
romm/backend/tests/handler/easyrpg/test_easyrpg.py
Georges-Antoine Assi 6f2a361481 Play RPG Maker 2000/2003 games in the browser with EasyRPG (#5077)
* feat(player): play RPG Maker 2000/2003 games in the browser with EasyRPG

Embed the EasyRPG web player for extracted game folders on the rpg-maker
platform. The backend generates the player's index.json from the scanned
files and merges in the free EasyRPG RTP under every name the official
and fan RTPs give its assets, so games that lean on the RTP find them.
In-game saves sync with the user's RomM saves through the player's
IndexedDB storage.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor(easyrpg): read the free RTP from a fixed path instead of an env var

The full image always keeps the RTP at /var/www/html/assets/easyrpg/rtp,
so the path is a constant beside the web path nginx serves it under. The
dev container links its copy there at startup.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(easyrpg): code-review fixes for the save sync and asset endpoint

- Wait for an in-flight push before the quit push, so the newest save uploads
- Keep Play disabled until the fresh rom (and its saves) is fetched
- Stop onPlay and the rom fetch from acting after the view unmounts
- Leave through usePlayerExit like the other players
- Handle a rejected IDB commit when the run itself fails
- Track uploads by the browser's file name
- Load only visibility and files per asset request, not the full rom
- Serve DEV_MODE files as octet-stream, matching nginx
- Make the traversal test actually reach the endpoint

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor(easyrpg): simplify pass over the player and asset endpoint

- Load only file paths per asset request, and look up RTP folders in O(1)
- Reuse RomFile.file_name_for_download and usePlayerFullscreen
- Share one dev/prod serve helper for game and RTP files
- Derive the save sync's game key from the rom
- Have the RTP table tool reuse normalize_name
- Make save-module internals private and tidy the prepare/read loops

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat(easyrpg): flag RPG Maker game folders on the rom schemas

Gallery lists carry no files, so the frontend guessed that every multi-file
rom on the rpg-maker platform was playable, including RPG Maker XP/VX/MV
folders. Rom now has a deferred `is_easyrpg_game` subquery (an RPG_RT.ldb at
the folder root), loaded with the other gallery file stats and derived from
loaded files on the with_files path, and the frontend reads it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor(easyrpg): drop server save sync, keep saves per user in the browser

Server sync moves to a follow-up that puts the web players on device sync.
Saves now stay in the player's IndexedDB, as js-dos does, with the same note
on the start page. The player is launched as `<rom id>-<user id>` so each
RomM account sharing a browser profile keeps its own saves; nginx and Vite
drop the user id when routing to the backend.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(easyrpg): address Greptile review

- Start timing the play session once the player page loads
- Use object-form props in the view test's button mock
- Keep the RTP table tool's docstring to one sentence

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor(easyrpg): keep the RTP table in a fixtures folder

Matches handler/metadata/fixtures, where the other bundled data files live.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-03 17:51:38 -04:00

142 lines
4.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from pathlib import Path
from handler.easyrpg import (
EasyRpgHandler,
build_index,
easyrpg_handler,
normalize_name,
)
RTP_TABLE = {
"music": [["戦闘1", "battle 1", "battle1"]],
"system": [["システム", "system"]],
}
def test_root_files_keep_their_extension():
index = build_index(["RPG_RT.ldb", "RPG_RT.lmt", "Map0001.lmu"], {}, {})
assert index["metadata"]["version"] == 2
assert index["cache"] == {
"rpg_rt.ldb": "RPG_RT.ldb",
"rpg_rt.lmt": "RPG_RT.lmt",
"map0001.lmu": "Map0001.lmu",
}
def test_folder_files_drop_their_extension_except_ini_and_po():
cache = build_index(
["ChipSet/World.png", "Language/Meta.ini", "Language/de.po"], {}, {}
)["cache"]
assert cache["chipset"] == {"_dirname": "ChipSet", "world": "World.png"}
assert cache["language"] == {
"_dirname": "Language",
"meta.ini": "Meta.ini",
"de.po": "de.po",
}
def test_exfont_is_keyed_without_its_extension():
cache = build_index(["ExFont.png"], {}, {})["cache"]
assert cache == {"exfont": "ExFont.png"}
def test_keys_are_lowercased_and_width_normalized():
cache = build_index(["Music/Town.mid"], {}, {})["cache"]
assert cache["music"] == {"_dirname": "Music", "town": "Town.mid"}
def test_nested_folders_nest_in_the_index():
cache = build_index(["Language/de/Picture/Title.png"], {}, {})["cache"]
assert cache["language"]["de"]["picture"] == {
"_dirname": "Picture",
"title": "Title.png",
}
def test_rtp_assets_answer_to_every_translated_name():
cache = build_index(["RPG_RT.ldb"], {"Music": ["Battle 1.mid"]}, RTP_TABLE)["cache"]
assert cache["music"] == {
"_dirname": "Music",
"戦闘1": "Battle 1.mid",
"battle 1": "Battle 1.mid",
"battle1": "Battle 1.mid",
}
def test_rtp_assets_join_the_games_folder_under_its_spelling():
cache = build_index(["music/Town.mid"], {"Music": ["Battle 1.mid"]}, RTP_TABLE)[
"cache"
]
assert cache["music"]["_dirname"] == "music"
assert cache["music"]["town"] == "Town.mid"
assert cache["music"]["戦闘1"] == "Battle 1.mid"
def test_game_files_win_over_the_rtp():
cache = build_index(["Music/戦闘1.mid"], {"Music": ["Battle 1.mid"]}, RTP_TABLE)[
"cache"
]
assert cache["music"]["戦闘1"] == "戦闘1.mid"
assert cache["music"]["battle1"] == "Battle 1.mid"
def test_rtp_files_outside_the_table_keep_their_own_name():
cache = build_index([], {"ChipSet": ["retro_World.png"]}, RTP_TABLE)["cache"]
assert cache["chipset"] == {"_dirname": "ChipSet", "retro_world": "retro_World.png"}
def test_reserved_dirname_file_is_skipped():
cache = build_index(["Music/_dirname"], {}, {})["cache"]
assert cache["music"] == {"_dirname": "Music"}
def _rtp_dir(tmp_path: Path) -> Path:
for path in ("Music/Battle 1.mid", "System/System.png", "Music/.gitignore"):
(tmp_path / path).parent.mkdir(parents=True, exist_ok=True)
(tmp_path / path).write_bytes(b"")
return tmp_path
def test_rtp_files_are_listed_by_folder(tmp_path: Path):
handler = EasyRpgHandler(rtp_path=str(_rtp_dir(tmp_path)))
assert handler.rtp_files == {
"Music": ["Battle 1.mid"],
"System": ["System.png"],
}
def test_missing_rtp_folder_lists_nothing(tmp_path: Path):
handler = EasyRpgHandler(rtp_path=str(tmp_path / "missing"))
assert handler.rtp_files == {}
def test_find_rtp_file_ignores_the_folder_case(tmp_path: Path):
handler = EasyRpgHandler(rtp_path=str(_rtp_dir(tmp_path)))
assert handler.find_rtp_file("music/Battle 1.mid") == "Music/Battle 1.mid"
assert handler.find_rtp_file("Music/battle 1.mid") is None
assert handler.find_rtp_file("Music/../System/System.png") is None
def test_is_game_needs_the_database_at_the_root():
assert EasyRpgHandler.is_game(["RPG_RT.LDB", "Music/Town.mid"])
assert not EasyRpgHandler.is_game(["Game/RPG_RT.ldb"])
def test_rtp_table_ships_with_the_handler():
music = easyrpg_handler.rtp_table["music"]
assert any(normalize_name("戦闘1") in row and "battle 1" in row for row in music)