"""The installer demos of the README and the site (scripts/make_demo.py). The home page and Get your files score 98 to 100 on a phone: a video must cost nothing before it is on screen, keep still under reduced motion, and say in words what it shows. The files are committed, so their sizes are held here, and the words come from what each recording measured. Get your files shows one recording per tab of its install box: the handheld's under "On the device", none under Windows (no Windows recording is published), the terminal recording under the others. """ from __future__ import annotations import tests # noqa: F401 offline, scratch under tmp/, the suite's own cache import json import re import struct import sys import unittest from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT / "scripts")) import generate_readme # noqa: E402 import make_demo # noqa: E402 import sitestarlight # noqa: E402 DEMO = ROOT / "docs_assets" / "demo" COMPONENT = ROOT / "website" / "src" / "components" / "InstallDemo.astro" DIST = ROOT / "website" / "dist" def _all_facts() -> dict: return json.loads((DEMO / "demo.json").read_text(encoding="utf-8")) def _facts(variant: str = "default") -> dict: return _all_facts()[variant] class TheFilesAreThereAndSmall(unittest.TestCase): def test_each_output_is_under_its_target(self): for variant in make_demo.VARIANTS.values(): for name, limit in make_demo.limits(variant).items(): with self.subTest(name=name): self.assertLessEqual((DEMO / name).stat().st_size, limit) def test_only_the_readme_variant_has_a_gif(self): gifs = sorted(p.name for p in DEMO.glob("*.gif")) self.assertEqual(gifs, [Path(generate_readme.DEMO_GIF).name]) self.assertEqual([v.name for v in make_demo.VARIANTS.values() if v.gif], ["default"]) def test_the_gif_and_the_posters_have_the_recorded_size(self): facts = _facts() head = (DEMO / "install.gif").read_bytes()[:10] self.assertEqual(head[:6], b"GIF89a") self.assertEqual(struct.unpack("II", png[16:24]), (facts["width"], facts["height"])) def test_the_videos_are_webm_and_mp4(self): for variant in make_demo.VARIANTS.values(): with self.subTest(variant=variant.name): self.assertEqual((DEMO / f"{variant.stem}.webm").read_bytes()[:4], b"\x1a\x45\xdf\xa3") self.assertEqual((DEMO / f"{variant.stem}.mp4").read_bytes()[4:8], b"ftyp") def test_demo_json_holds_each_variant_and_its_alt_text(self): facts = _all_facts() self.assertEqual(list(facts), list(make_demo.VARIANTS)) for name, entry in facts.items(): with self.subTest(variant=name): self.assertEqual(entry["alt"], make_demo.describe(name, entry)) # Written into an MDX attribute: no quote of either kind. self.assertNotRegex(entry["alt"], "[\"']") class TheTapeAndTheDriverAgree(unittest.TestCase): def setUp(self): self.tape = make_demo.TAPE.read_text(encoding="utf-8") def test_each_tape_writes_what_the_driver_encodes(self): for variant in make_demo.VARIANTS.values(): tape = variant.tape.read_text(encoding="utf-8") with self.subTest(variant=variant.name): self.assertIn(f"Output {variant.recorded.relative_to(ROOT).as_posix()}\n", tape) self.assertIn( f"Screenshot {variant.poster_shot.relative_to(ROOT).as_posix()}\n", tape) def test_the_size_in_demo_json_is_each_tapes(self): for variant in make_demo.VARIANTS.values(): facts, tape = _facts(variant.name), variant.tape.read_text(encoding="utf-8") with self.subTest(variant=variant.name): self.assertIn(f"Set Width {facts['width']}\n", tape) self.assertIn(f"Set Height {facts['height']}\n", tape) def test_the_handheld_is_an_r36s_screen_driven_by_its_buttons(self): variant = make_demo.VARIANTS["handheld"] tape = variant.tape.read_text(encoding="utf-8") self.assertIn("Set Width 640\n", tape) self.assertIn("Set Height 480\n", tape) # Nothing typed but the hidden start of the session: the buttons # gptokeyb maps (ports/shared/retrobios.gptk: a = enter) do the rest. self.assertEqual(re.findall(r"^Type (.*)$", tape, re.M), ["'exec \"$RETROBIOS_DEMO_SESSION\"'"]) gptk = (ROOT / "ports" / "shared" / "retrobios.gptk").read_text(encoding="utf-8") self.assertRegex(gptk, r"(?m)^a = enter$") env = make_demo.session_env("http://127.0.0.1:1", ROOT / "tmp", ROOT / "tmp", variant) self.assertEqual(env["RETROBIOS_PAD"], "gptokeyb") self.assertEqual(env["RETROBIOS_OS"], "linux") self.assertEqual(variant.mount, "/roms") def test_the_command_typed_is_the_one_the_pages_give(self): from siteguides import install_command # noqa: PLC0415 self.assertIn(f'Type "{install_command("sh", "--for-games")}"\n', self.tape) def test_the_shell_answers_only_the_site_address(self): typed = re.search(r'Type "curl -fsSL (\S+) \|', self.tape) self.assertIsNotNone(typed) driver = Path(make_demo.__file__).read_text(encoding="utf-8") self.assertIn(f'"$*" = "-fsSL {typed.group(1)}"', driver) class TheReadmeShowsTheGif(unittest.TestCase): def test_the_gif_sits_under_the_commands_with_its_description(self): readme = (ROOT / "README.md").read_text(encoding="utf-8") line = f"![{generate_readme.demo_description()}]({generate_readme.DEMO_GIF})" self.assertIn(line, readme) self.assertLess(readme.index("## Install in one line"), readme.index(line)) self.assertLess(readme.index(line), readme.index("## Or download a ZIP")) def test_the_description_names_what_the_recording_measured(self): facts = _facts() words = generate_readme.demo_description(facts) for value in (facts["setup"], f"{facts['games']} games", f"{facts['files']} BIOS files", facts["size"]): self.assertIn(str(value), words) class TheVideoLoadsOnlyOnScreen(unittest.TestCase): def setUp(self): self.source = COMPONENT.read_text(encoding="utf-8") self.markup = self.source.split("