Files
UnheardCity/tests/test_workspace.py

177 lines
6.4 KiB
Python

"""Apply / revert safety on a synthetic game: byte-exact reverts, size limits, conflict detection,
mod packages."""
import hashlib
import os
import pytest
from ackit import workspace as W
from ackit.inject import SizeLimitError, inject
from ackit.modpack import export_mod, import_mod, match_mod
import synth
def sha(p):
return hashlib.sha1(open(p, "rb").read()).hexdigest()
def files(root):
out = []
for d, _, fs in os.walk(os.path.join(root, "BmGame")):
out += [os.path.join(d, f) for f in fs]
return {f: sha(f) for f in sorted(out)}
def ws_for(root, **kw):
return W.Workspace(root, archives=["INT", "SFX"], **kw)
def test_apply_in_place_and_revert_byte_identical(synth_game):
root, originals = synth_game
before = files(root)
ws = ws_for(root)
s = ws.sound("Speech_Synth_Radio_02")
x, sr = synth.tone(1.2, 24000, 900)
wem, info = ws.prepare(s, x, sr)
assert len(wem) <= ws.limit(s)
ws.apply([(s, wem, {"source": "test"})])
assert ws.current_bytes(s) == wem and ws.state(s) == W.MODIFIED
assert ws.original_bytes(s) == originals[s.target]
# neighbours untouched
for i in (0, 2):
t = "%08X:%d" % (synth.SPEECH_ENTRY, i)
assert ws.current_bytes(ws.catalog.by_key["INT:" + t]) == originals[t]
assert ws.revert([s]) == [s]
assert files(root) == before
assert ws.state(s) == W.ORIGINAL and not ws.store.entries
def test_pack_media_resize_and_revert_byte_identical(synth_game):
root, originals = synth_game
before = files(root)
ws = ws_for(root)
s = ws.catalog.by_key["SFX:%08X/%08X" % (synth.PACK_ID, synth.MEDIA[1])]
assert ws.limit(s) is None
x, sr = synth.tone(8.0, 48000, 200, 2)
wem, _ = ws.prepare(s, x, sr)
assert len(wem) > len(originals[s.target])
ws.apply([(s, wem, {})])
assert ws.current_bytes(s) == wem
ws.revert_all()
assert files(root) == before
def test_size_limit_enforced(synth_game):
root, _ = synth_game
ws = ws_for(root)
s = ws.sound("CHR_RadioScanner_3")
big = synth.wem(5.0, 440)
with pytest.raises(SizeLimitError):
ws.apply([(s, big, {})])
assert not ws.modified_keys()
def test_reapply_uses_original_limit(synth_game):
root, _ = synth_game
ws = ws_for(root)
s = ws.sound("Speech_Synth_Radio_02")
lim = ws.limit(s)
ws.apply([(s, synth.wem(0.3, 1000), {})]) # much smaller
assert ws.limit(s) == lim # still the original's size
ws.apply([(s, synth.wem(1.0, 800), {})])
ws.revert([s])
assert not ws.store.entries
def test_conflict_when_changed_underneath(synth_game):
root, _ = synth_game
ws = ws_for(root)
s = ws.sound("CHR_RadioScanner_1")
ws.apply([(s, synth.wem(0.5, 1200), {})])
# simulate another tool / Steam writing something else into the same slot
other = synth.wem(0.4, 150)
inject(ws.wad_path(s), {s.target: other}, {s.target: ws.limit(s)})
assert ws.state(s) == W.CHANGED
with pytest.raises(W.ConflictError):
ws.apply([(s, synth.wem(0.5, 1300), {})])
ws.apply([(s, synth.wem(0.5, 1300), {})], force=True)
ws.revert([s])
assert ws.state(s) == W.ORIGINAL
def test_lost_state_after_external_restore(synth_game):
root, originals = synth_game
ws = ws_for(root)
s = ws.sound("CHR_RadioScanner_1")
ws.apply([(s, synth.wem(0.5, 1200), {})])
inject(ws.wad_path(s), {s.target: originals[s.target]}, {s.target: ws.limit(s)}) # like 'verify files'
assert ws.state(s) == W.LOST
def test_full_backup_taken_once(synth_game):
root, _ = synth_game
ws = ws_for(root)
s = ws.sound("CHR_RadioScanner_1")
ws.apply([(s, synth.wem(0.5, 1200), {})])
rel = ws.game.rel(ws.wad_path(s))
assert ws.store.full_copy(rel)
restored = ws.restore_from_full_backup()
assert rel in restored and ws.state(s) == W.ORIGINAL
def test_mod_from_another_language_matched_by_line(synth_game, tmp_path):
"""A voice line from another language is found through the game's line identity (its WAD position there
can differ); a voice line from a package without line identities is never guessed by name."""
import json
import zipfile
root, _ = synth_game
ws = ws_for(root, store_root=str(tmp_path / "store"))
s = ws.catalog.by_key["INT:%08X:1" % synth.SPEECH_ENTRY]
assert s.line and s.name == synth.SPEECH_EVENT # the game's line, named after its event
ws.apply([(s, synth.wem(0.3, 1000), {})])
pkg = str(tmp_path / "other_lang.acmod")
export_mod(ws, pkg, ws.modified_keys(), "t")
ws.revert_all()
def rewrite(change):
with zipfile.ZipFile(pkg) as z:
files = {n: z.read(n) for n in z.namelist()}
man = json.loads(files["manifest.json"])
change(man["items"][0])
files["manifest.json"] = json.dumps(man).encode()
with zipfile.ZipFile(pkg, "w") as z:
for n, b in files.items():
z.writestr(n, b)
def other_language(it): # as if made on the ITA game, at another position
it["archive"], it["target"], it["name"] = "ITA", "%08X:2" % synth.SPEECH_ENTRY, "Speech_Synth_Radio_02"
rewrite(other_language)
_, items = match_mod(ws, pkg)
assert items[0].how == "same line" and items[0].sound is s
rewrite(lambda it: it.pop("line")) # an older package: names alone are no proof
_, items = match_mod(ws, pkg)
assert items[0].how == "missing" and "older version" in items[0].note
def test_mod_export_import_roundtrip(synth_game, synth_template, tmp_path):
root, _ = synth_game
ws = ws_for(root)
a = ws.sound("Speech_Synth_Radio_02")
b = ws.catalog.by_key["SFX:%08X/%08X" % (synth.PACK_ID, synth.MEDIA[0])]
wa, wb = synth.wem(1.0, 820), synth.wem(2.5, 410, 48000, 2)
ws.apply([(a, wa, {}), (b, wb, {})])
pkg = str(tmp_path / "test.acmod")
assert export_mod(ws, pkg, ws.modified_keys(), "t") == 2
ws.revert_all()
# import on a fresh copy of the game ("another machine")
import shutil
other = str(tmp_path / "other")
shutil.copytree(synth_template[0], other)
ws2 = ws_for(other, store_root=str(tmp_path / "store2"))
_, items = match_mod(ws2, pkg)
assert [i.how for i in items] == ["exact", "exact"]
res, _ = import_mod(ws2, pkg)
assert all(i.note == "exact copy" for i in res)
assert ws2.current_bytes(ws2.catalog.by_key[a.key]) == wa
assert ws2.current_bytes(ws2.catalog.by_key[b.key]) == wb