Files
pgames-manager/backend/tests/test_converter.py
T
mrleo1nidandClaude Opus 5.5 a97b46079e Converter: cover real Ren'Py games better, add folder import and dry run
Checked against six real games (Ren'Py 7.5 and 8.x):
- nested labels and named menus become jump anchors inside label bodies
- call with arguments and labels with parameters (dynamically scoped)
- frame-by-frame ATL images, Movie frames, layeredimage, camera, Shake,
  custom audio channels via register_channel, show text with expressions
- generator expressions in Python, no SyntaxWarning noise from decompiled code

Also:
- app.import_dir converts an unpacked game folder without changing it
- app.converter.analyze dry-runs the script part of the converter
- imports use their own "importing" status so a worker restart can't hijack them

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 03:23:20 +03:00

123 lines
5.1 KiB
Python

import json
import shutil
import pytest
from app.blobs import read_bytes
from app.config import get_settings
from app.converter import pyexpr
from app.converter.pipeline import convert_archive
from app.converter.rpy_parser import RpyParser, ScriptData
from tests.make_sample import build
@pytest.fixture
def data_dir(tmp_path, monkeypatch):
monkeypatch.setenv("DATA_DIR", str(tmp_path / "data"))
get_settings.cache_clear()
yield tmp_path / "data"
get_settings.cache_clear()
def parse(src: str) -> ScriptData:
d = ScriptData()
RpyParser(d, "t.rpy").parse(src)
return d
def test_expr_translation():
e = pyexpr.parse_expr("a >= 1 and 'x' in inv or not flag")
assert e["k"] == "bool" and e["op"] == "or"
with pytest.raises(pyexpr.Unsupported):
pyexpr.parse_expr("lambda: 1")
def test_say_variants():
d = parse('label start:\n "narration"\n e happy "hi" with dissolve\n'
' "Bob" "named"\n centered "c"\n')
body = d.modules["t.rpy"][0]["body"]
assert body[0] == {"op": "say", "what": "narration"}
assert body[1]["who"] == "e" and body[1]["attrs"] == ["happy"] and body[1]["with"]["t"] == "dissolve"
assert body[2]["whoStr"] == "Bob"
assert body[3]["who"] == "centered"
def test_string_whitespace_and_escapes():
d = parse('label start:\n "multi\n line \\"q\\" {b}x{/b}"\n')
assert d.modules["t.rpy"][0]["body"][0]["what"] == 'multi line "q" {b}x{/b}'
def test_if_chain_and_menu():
d = parse("label start:\n if a:\n jump x\n elif b:\n pass\n else:\n"
" return\n menu:\n \"A\" if c:\n jump y\n \"B\":\n"
" pass\n")
body = d.modules["t.rpy"][0]["body"]
assert [b["c"] is None for b in body[0]["branches"]] == [False, False, True]
assert [i["text"] for i in body[1]["items"]] == ["A", "B"]
def test_unsupported_python_is_reported_not_fatal():
d = parse("init python:\n def f():\n pass\n x = 1\nlabel start:\n $ y = x\n")
assert d.warnings and d.init_code[0]["op"] == "set"
def test_full_pipeline(data_dir, tmp_path):
archive = build(tmp_path / "sample.zip")
steps = []
result = convert_archive(archive, "sample.zip", tmp_path / "work", steps.append)
files = result.manifest["files"]
assert "game.json" in files and "script/script.json" in files
assert "images/bg room.webp" in files
game = json.loads(read_bytes(files["game.json"]["hash"]))
assert game["title"] == "Тестовая новелла"
assert game["resolution"] == [1280, 720]
assert game["images"]["bg room"]["file"] == "images/bg room.webp"
assert game["images"]["eileen sad"]["props"]["alpha"] == 0.7
assert "e" in game["characters"]
assert result.cover_source is not None
script = json.loads(read_bytes(files["script/script.json"]["hash"]))
assert [lbl["name"] for lbl in script["labels"]] == ["start", "walk", "stay_home", "ending"]
if shutil.which("ffmpeg"):
assert "audio/theme.ogg" in files
def test_nested_labels_and_named_menus_become_anchors():
from app.converter.pipeline import anchors
d = parse("label start:\n if x:\n label inner:\n \"a\"\n"
" menu pick:\n \"A\":\n jump .loc\n"
"label .loc:\n return\n")
labels = d.modules["t.rpy"]
assert [lbl["name"] for lbl in labels] == ["start", "inner.loc"] # local labels follow the last global one
found = anchors(labels)
assert found["inner"] == {"label": "start", "path": [0, 0, 0], "params": None}
assert found["pick"]["path"] == [1]
assert labels[0]["body"][2]["items"][0]["body"][0]["target"] == "inner.loc"
def test_call_arguments_and_label_params():
d = parse('label start:\n call greet("Bob", loud=True) from _c1\n'
'label greet(name, loud=False):\n "Hi [name]"\n return\n')
call = d.modules["t.rpy"][0]["body"][0]
assert call["target"] == "greet" and call["args"]["a"][0]["v"] == "Bob"
assert "loud" in call["args"]["kw"]
params = d.modules["t.rpy"][1]["params"]
assert params[0] == ["name", None] and params[1][1]["v"] is False
def test_atl_frames_layeredimage_and_channels():
d = parse('image blink:\n "a.png" with dissolve\n pause 0.5\n "b.png"\n 0.25\n'
' repeat\n'
'image vid:\n Movie(play="v.webm")\n'
'layeredimage eileen:\n always:\n "eileen_base"\n'
' group face:\n attribute happy default\n attribute sad "sad.png"\n'
'init python:\n renpy.music.register_channel("amb", mixer="sfx", loop=True)\n')
blink = d.images["blink"]
assert [f.get("d") for f in blink["frames"]] == [0.5, 0.25] and blink["repeat"]
assert d.images["vid"] == {"movie": "v.webm"}
layers = d.images["eileen"]["layered"]
assert layers[0] == {"always": {"ref": "eileen_base"}}
assert layers[1] == {"attr": "happy", "group": "face", "default": True,
"img": {"ref": "eileen_face_happy"}}
assert layers[2]["img"] == {"file": "sad.png"}
assert d.channels["amb"] == {"mixer": "sfx", "loop": True}