Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/nnnotes/replaydata.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ def _package(files: dict[str, bytes], source: Path, model: dict | None, name: st
built = json.loads((source / "build.json").read_text(encoding="utf8"))
except (OSError, ValueError):
raise deckdata.DeckDataError(f"{name}: missing or malformed build.json") from None
if built.get("format") != build_format or not model or built.get("commit") != model.get("commit"):
raise deckdata.DeckDataError(f"{name}: build.json commit differs from the pinned deck model")
if not model or not model.get("commit"):
raise deckdata.DeckDataError(f"{name}: the pinned deck model identity is required")
if built.get("format") != build_format:
raise deckdata.DeckDataError(f"{name}: build.json format is {built.get('format')!r}, not {build_format}")
if built.get("commit") != model["commit"]:
raise deckdata.DeckDataError(f"{name}: build.json commit {built.get('commit')!r} differs from the pinned "
f"deck model {model['commit']}")
js_path, wasm_path, build_path = f"{prefix}/{js}", f"{prefix}/{wasm}", f"{prefix}/build.json"
for file, path in ((js, js_path), (wasm, wasm_path)):
if not (source / file).is_file():
raise deckdata.DeckDataError(f"{name}: missing {file} in {source}")
files[path] = (source / file).read_bytes()
if not files[wasm_path].startswith(b"\x00asm\x01\x00\x00\x00"):
raise deckdata.DeckDataError(f"{name}: not a WASM v1 module")
if not model.get("commit"):
raise deckdata.DeckDataError(f"{name}: the pinned deck model identity is required")
if built.get("jsSha256") != hashlib.sha256(files[js_path]).hexdigest() or \
built.get("wasmSha256") != hashlib.sha256(files[wasm_path]).hexdigest():
raise deckdata.DeckDataError(f"{name}: JS/WASM SHA differs from build.json")
Expand Down
7 changes: 4 additions & 3 deletions tests/test_replaydata.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def test_engine_manifest_rejects_stale_pin_and_changed_wasm(tmp_path):
(engine / "build.json").write_text(json.dumps(built))
stale = tmp_path / "stale"
stale.mkdir()
with pytest.raises(musicdata.MusicDataError, match="commit differs"):
stale_pin = f"build.json commit '0{{40}}' differs from the pinned deck model {FakeDeck.COMMIT}"
with pytest.raises(musicdata.MusicDataError, match=stale_pin):
export(stale, deck=FakeDeck(), replay_dir=stale / "replay", replay_engine=engine)
assert not (stale / "music.json").exists()
built["commit"] = FakeDeck.COMMIT
Expand Down Expand Up @@ -128,8 +129,8 @@ def test_recommend_engine_shares_the_model_of_the_deck_data(tmp_path):


@pytest.mark.parametrize("change,message", [
("commit", "recommend engine: build.json commit differs"),
("format", "recommend engine: build.json commit differs"),
("commit", "recommend engine: build.json commit '0{40}' differs from the pinned deck model"),
("format", "recommend engine: build.json format is 'ournotes.replay-engine/1', not ournotes.recommend-engine/1"),
("wasm", "recommend engine: JS/WASM SHA differs"),
("missing", "recommend engine: missing ournotes_recommend.js"),
("no-replay-dir", "--recommend-engine needs --replay-dir"),
Expand Down
Loading