ravenous_roll20/effects_web/tests/test_site.py

54 lines
3.0 KiB
Python

import json
import subprocess
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
class EffectsSiteTests(unittest.TestCase):
def test_static_build_is_complete_and_deterministic(self):
with tempfile.TemporaryDirectory() as first, tempfile.TemporaryDirectory() as second:
for target in (first, second):
subprocess.run(["python", "effects_web/tools/build.py", "--output", target], cwd=ROOT, check=True)
one, two = Path(first), Path(second)
expected = {"index.html", "styles.css", "app.js", "catalog.js", "build-info.json", "ravenous-effects-v1.schema.json"}
self.assertEqual({p.name for p in one.iterdir()}, expected)
for path in one.iterdir():
self.assertEqual(path.read_bytes(), (two / path.name).read_bytes())
info = json.loads((one / "build-info.json").read_text(encoding="utf-8"))
self.assertEqual(info["effects"], 81)
self.assertGreaterEqual(len(info["themes"]), 10)
for path in one.iterdir():
if path.suffix in {".html", ".css", ".js"}:
text = path.read_text(encoding="utf-8")
self.assertNotIn("{{", text)
self.assertNotIn("http://", text)
self.assertNotIn("https://", text)
def test_catalog_translation_and_export_contract(self):
with tempfile.TemporaryDirectory() as target:
subprocess.run(["python", "effects_web/tools/build.py", "--output", target], cwd=ROOT, check=True)
raw = (Path(target) / "catalog.js").read_text(encoding="utf-8")
catalog = json.loads(raw.removeprefix("window.RAVENOUS_EFFECTS_CATALOG=").removesuffix(";\n"))
effects = {item["id"]: item for item in catalog["effects"]}
self.assertEqual(effects["al_saignement"]["effect_id"], "al_saignement")
self.assertIn("Chaque marque dure 2 tours", effects["al_saignement"]["description"])
self.assertIn("premiers secours", effects["al_saignement"]["description"])
self.assertIn("réinitialisez la durée", effects["al_poison"]["description"])
self.assertIn("15 marques Poison maximum", effects["al_poison"]["description"])
self.assertIn("Peur", effects["al_peur"]["description"])
for item in effects.values():
self.assertNotRegex(item["description"], r"(?:#(?:alteration-mark|reference)|@[A-Za-z_])")
schema = json.loads((Path(target) / "ravenous-effects-v1.schema.json").read_text(encoding="utf-8"))
self.assertEqual(schema["properties"]["format"]["const"], "ravenous-roll20-effects")
self.assertEqual(schema["$defs"]["effect"]["required"], ["effect_id", "type", "name"])
app = (Path(target) / "app.js").read_text(encoding="utf-8")
self.assertIn("ravenous-roll20-effects", app)
self.assertIn("ravenous-effects-sets", app)
if __name__ == "__main__":
unittest.main()