mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-29 21:08:53 -06:00
chore: merge the wanted files page
This commit is contained in:
commit
c4f746e2d5
18 files changed
+1409
-170
No files matched your search
@@ -43,6 +43,7 @@ WEBSITE = REPO_ROOT / "website"
|
||||
DIST = WEBSITE / "dist"
|
||||
COMPONENTS = WEBSITE / "src" / "components"
|
||||
SITE_TS = WEBSITE / "src" / "scripts" / "site.ts"
|
||||
HASHES_TS = WEBSITE / "src" / "scripts" / "hashes.ts"
|
||||
CONFIG = WEBSITE / "astro.config.mjs"
|
||||
PLUGIN = WEBSITE / "src" / "plugins" / "rehype-stack-tables.mjs"
|
||||
|
||||
@@ -188,6 +189,38 @@ class SectionTabsInTheHeader(unittest.TestCase):
|
||||
self.assertRegex(nav.group(1), r'aria-current="true"[^>]*>\s*Start here')
|
||||
|
||||
|
||||
class TheWantedPageIsInTheBar(unittest.TestCase):
|
||||
"""The wanted page is the site's call to contribute: in the bar on every
|
||||
page, beside the search and menu buttons on a phone, where the section
|
||||
tabs fold into the menu."""
|
||||
|
||||
LINK = re.compile(r'<a class="rb-wanted-link[^"]*" href="/retrobios/wanted/"([^>]*)>Wanted files</a>')
|
||||
|
||||
def test_the_bar_holds_it_outside_the_tabs(self):
|
||||
header = _read(COMPONENTS / "Header.astro")
|
||||
bar = header[header.index('<div class="rb-header-bar">'):header.index("</div>")]
|
||||
self.assertIn('class="rb-wanted-link print:hidden"', bar)
|
||||
self.assertIn("aria-current={onWanted ? 'page' : undefined}", bar)
|
||||
# Theme colours only, the same in both themes' variables.
|
||||
style = header[header.index(".rb-wanted-link {"):header.index("@media")]
|
||||
self.assertNotRegex(style, r"#[0-9a-fA-F]{3,6}\b")
|
||||
# At 360 px the name was cut to "Retr": the logo stays, the name is
|
||||
# kept for screen readers only.
|
||||
narrow = header[header.index("@media (max-width: 26.99rem)"):]
|
||||
self.assertIn(".site-title span {", narrow[:200])
|
||||
self.assertIn("clip-path: inset(50%);", narrow[:400])
|
||||
|
||||
def test_built_on_every_page_current_on_its_own(self):
|
||||
for page in ("which-pack", "files", "systems"):
|
||||
with self.subTest(page=page):
|
||||
found = self.LINK.search(_built(page))
|
||||
self.assertIsNotNone(found)
|
||||
self.assertNotIn("aria-current", found.group(1))
|
||||
own = self.LINK.search(_built("wanted"))
|
||||
self.assertIsNotNone(own)
|
||||
self.assertIn('aria-current="page"', own.group(1))
|
||||
|
||||
|
||||
def _title_block(html: str) -> str:
|
||||
"""What PageTitle draws: from the main column to the h1."""
|
||||
end = html.index('id="_top"')
|
||||
@@ -497,26 +530,76 @@ class HashAFileHere(unittest.TestCase):
|
||||
source = _read(SITE_TS)
|
||||
self.assertIn("input.type = 'file';", source)
|
||||
self.assertIn("It is hashed on this device. Nothing is uploaded.", source)
|
||||
self.assertIn("crypto.subtle.digest(name, buffer)", source)
|
||||
self.assertIn("['SHA-1', 'SHA-256']", source)
|
||||
self.assertIn("import { type FileHashes, hashFile } from './hashes.ts';", source)
|
||||
self.assertIn("lookUp(status, result, [found.sha1, found.md5, found.crc32, found.sha256])", source)
|
||||
hashing = _read(HASHES_TS)
|
||||
self.assertIn("crypto.subtle.digest(name, buffer)", hashing)
|
||||
self.assertIn("['SHA-1', 'SHA-256']", hashing)
|
||||
|
||||
def test_md5_and_crc32_match_python(self):
|
||||
source = _read(SITE_TS)
|
||||
block = source[source.index("// Hashing a file on this device"):source.index("type FileHashes")]
|
||||
sizes = [0, 1, 55, 56, 63, 64, 65, 119, 120, 128, 1000, 70_001]
|
||||
out = _node_module(block + (
|
||||
f"console.log(JSON.stringify({json.dumps(sizes)}.map((n) => {{"
|
||||
"const bytes = new Uint8Array(n);"
|
||||
"for (let i = 0; i < n; i += 1) bytes[i] = (i * 31 + 7) & 0xff;"
|
||||
"return [md5(bytes), crc32(bytes)]; })));"
|
||||
|
||||
class FileHashesMatchPython(unittest.TestCase):
|
||||
"""MD5 and CRC32 are written here, Web Crypto having neither: each is
|
||||
checked against hashlib and zlib, whole and fed in uneven slices, and
|
||||
hashFile's four values against a file read in 4 MiB slices."""
|
||||
|
||||
SIZES = [0, 1, 55, 56, 63, 64, 65, 119, 120, 128, 1000, 70_001]
|
||||
|
||||
@staticmethod
|
||||
def _bytes(size: int) -> bytes:
|
||||
return bytes((i * 31 + 7) & 0xFF for i in range(size))
|
||||
|
||||
def _run(self, body: str) -> list:
|
||||
return json.loads(_node_module(
|
||||
f"import {{ Crc32, Md5, crc32, hashFile, md5 }} from {json.dumps(HASHES_TS.as_uri())};"
|
||||
"const fill = (n) => { const b = new Uint8Array(n);"
|
||||
" for (let i = 0; i < n; i += 1) b[i] = (i * 31 + 7) & 0xff; return b; };" + body
|
||||
))
|
||||
for size, (md5, crc) in zip(sizes, json.loads(out)):
|
||||
data = bytes((i * 31 + 7) & 0xFF for i in range(size))
|
||||
|
||||
def test_md5_and_crc32_whole(self):
|
||||
out = self._run(
|
||||
f"console.log(JSON.stringify({json.dumps(self.SIZES)}.map((n) => "
|
||||
"{ const bytes = fill(n); return [md5(bytes), crc32(bytes)]; })));"
|
||||
)
|
||||
for size, (md5, crc) in zip(self.SIZES, out):
|
||||
data = self._bytes(size)
|
||||
with self.subTest(size=size):
|
||||
self.assertEqual(md5, hashlib.md5(data).hexdigest())
|
||||
self.assertEqual(crc, f"{zlib.crc32(data):08x}")
|
||||
|
||||
def test_md5_and_crc32_in_slices(self):
|
||||
"""A slice ending inside a 64-byte block carries it to the next."""
|
||||
size, steps = 200_003, [1, 63, 64, 65, 3, 1000, 7, 100_000]
|
||||
out = self._run(
|
||||
f"const bytes = fill({size}); const steps = {json.dumps(steps)};"
|
||||
"const m = new Md5(); const c = new Crc32();"
|
||||
"for (let at = 0, k = 0; at < bytes.length; k += 1) {"
|
||||
" const piece = bytes.subarray(at, at + steps[k % steps.length]);"
|
||||
" m.update(piece); c.update(piece); at += piece.length; }"
|
||||
"console.log(JSON.stringify([m.digest(), c.digest()]));"
|
||||
)
|
||||
data = self._bytes(size)
|
||||
self.assertEqual(out, [hashlib.md5(data).hexdigest(), f"{zlib.crc32(data):08x}"])
|
||||
|
||||
def test_hash_file_reads_slices_and_reports_progress(self):
|
||||
size = 9 * 1024 * 1024 + 17
|
||||
out = self._run(
|
||||
f"const shares = []; const found = await hashFile(new Blob([fill({size})]),"
|
||||
" (share) => shares.push(share));"
|
||||
"console.log(JSON.stringify([found, shares.length, shares.at(-1),"
|
||||
" shares.every((v, i) => i === 0 || v >= shares[i - 1])]));"
|
||||
)
|
||||
data = self._bytes(size)
|
||||
found, steps, last, rising = out
|
||||
self.assertEqual(found, {
|
||||
"sha1": hashlib.sha1(data).hexdigest(),
|
||||
"md5": hashlib.md5(data).hexdigest(),
|
||||
"crc32": f"{zlib.crc32(data):08x}",
|
||||
"sha256": hashlib.sha256(data).hexdigest(),
|
||||
})
|
||||
self.assertGreaterEqual(steps, 3)
|
||||
self.assertEqual(last, 1)
|
||||
self.assertTrue(rising)
|
||||
|
||||
|
||||
class SearchFoldsPagesUnderAHash(unittest.TestCase):
|
||||
"""Under "Look up this hash" came 14 unrelated pages: Magnavox for
|
||||
|
||||
@@ -42,6 +42,7 @@ START_PAGES = {
|
||||
START_LEVEL_PAGES = {
|
||||
"wiki/measurements.md": 300,
|
||||
"wiki/archive-recipes.md": 160,
|
||||
"wanted.md": 160,
|
||||
}
|
||||
# Level 1: the guide part of each setup page, above its Reference heading.
|
||||
SETUP_CEILING = 220
|
||||
@@ -230,7 +231,8 @@ def parse(markdown: str) -> Page:
|
||||
flush()
|
||||
tab = 0
|
||||
continue
|
||||
if "rb-button" in stripped or "rb-tile" in stripped or stripped.startswith("<FilePicker"):
|
||||
if "rb-button" in stripped or "rb-tile" in stripped or stripped.startswith(
|
||||
("<FilePicker", "<WantedFiles")):
|
||||
act()
|
||||
continue
|
||||
if re.match(r"</?(?!abbr\b|a\b|code\b|strong\b|em\b)[A-Za-z]", stripped):
|
||||
|
||||
@@ -55,6 +55,7 @@ REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(REPO_ROOT / "scripts"))
|
||||
|
||||
import generate_site # noqa: E402
|
||||
import sitewanted # noqa: E402
|
||||
import siterender # noqa: E402
|
||||
from common import ( # noqa: E402
|
||||
list_registered_platforms,
|
||||
@@ -1256,10 +1257,18 @@ class FilesToSourceOnAPhone(unittest.TestCase):
|
||||
for row in rows:
|
||||
self.assertIn(f"[Have it? Upload]({generate_site.UPLOAD_URL}", row)
|
||||
|
||||
def test_the_upload_lands_in_the_console_folder(self):
|
||||
with mock.patch("siterender._own_section", return_value=("Sony", "PlayStation")):
|
||||
self.assertEqual(generate_site._upload_folder({"systems": ["sony-playstation"]}),
|
||||
"Sony/PlayStation")
|
||||
def test_the_upload_lands_in_the_folder_the_review_asks_for(self):
|
||||
"""The link read the profile's first system: Raze's Duke Nukem 3D
|
||||
files were sent to bios/Monolith/Blood/. It reads validate_pr's
|
||||
placement now (tests/test_site_wanted.py)."""
|
||||
held = "a" * 40
|
||||
db = {"files": {held: {"path": "bios/Sony/PlayStation/scph1001.bin"}},
|
||||
"indexes": {"by_md5": {}, "by_sha256": {}, "by_name": {}}}
|
||||
profile_hashes = {"folders": {"b" * 40: {"bios/Sony/PlayStation/"}}, "named": {}}
|
||||
placer = sitewanted.Placer(db, {}, profile_hashes)
|
||||
self.assertEqual(placer.folder({"name": "scph5501.bin", "sha1": "b" * 40}),
|
||||
"bios/Sony/PlayStation/")
|
||||
self.assertEqual(placer.folder({"name": "x.bin"}), "")
|
||||
|
||||
|
||||
GENERATED = REPO_ROOT / "website" / "src" / "generated"
|
||||
|
||||
@@ -0,0 +1,344 @@
|
||||
"""The wanted page's list: what the collection lacks, by hash, and where
|
||||
each file goes.
|
||||
|
||||
A visitor drops a file on wanted/ and the page says whether the collection
|
||||
wants it. The list is built from the gaps page's own report, so a file the
|
||||
collection holds must never be on it, and its folder must be the one the
|
||||
review of the pull request asks for (validate_pr.placement): a page that
|
||||
sent a file elsewhere would have its upload flagged by the same project.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import tests # noqa: F401 offline, scratch under tmp/, the suite's own cache
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import zlib
|
||||
from pathlib import Path
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(REPO_ROOT / "scripts"))
|
||||
|
||||
import generate_site # noqa: E402
|
||||
import sitewanted # noqa: E402
|
||||
import validate_pr # noqa: E402
|
||||
from common import load_database # noqa: E402
|
||||
|
||||
WEBSITE = REPO_ROOT / "website"
|
||||
WANTED_TS = WEBSITE / "src" / "scripts" / "wanted.ts"
|
||||
PUBLISHED = WEBSITE / "public" / sitewanted.DATA_FILE
|
||||
|
||||
HELD = b"held rom"
|
||||
WANTED = b"wanted rom"
|
||||
VARIANT = b"another revision"
|
||||
MEMBER = b"rom inside an archive"
|
||||
CRC_ONLY = b"crc only rom"
|
||||
CUT = b"cut md5 rom"
|
||||
RECALBOX = [b"revision one", b"revision two"]
|
||||
|
||||
|
||||
def sha1(data: bytes) -> str:
|
||||
return hashlib.sha1(data).hexdigest()
|
||||
|
||||
|
||||
def md5(data: bytes) -> str:
|
||||
return hashlib.md5(data).hexdigest()
|
||||
|
||||
|
||||
def crc(data: bytes) -> str:
|
||||
return f"{zlib.crc32(data):08x}"
|
||||
|
||||
|
||||
class _World(unittest.TestCase):
|
||||
"""One held file of system x in bios/Maker/Console/, a profile and a
|
||||
platform that ask for files around it."""
|
||||
|
||||
def setUp(self) -> None:
|
||||
self._tmp = tempfile.TemporaryDirectory()
|
||||
self.tmp = Path(self._tmp.name)
|
||||
self._cwd = os.getcwd()
|
||||
os.chdir(self.tmp)
|
||||
self.addCleanup(os.chdir, self._cwd)
|
||||
self.addCleanup(self._tmp.cleanup)
|
||||
self.db = {
|
||||
"files": {sha1(HELD): {"path": "bios/Maker/Console/a.bin", "name": "a.bin",
|
||||
"size": len(HELD), "sha1": sha1(HELD), "md5": md5(HELD),
|
||||
"crc32": crc(HELD)},
|
||||
sha1(b"named"): {"path": "bios/Maker/Console/v.bin", "name": "v.bin",
|
||||
"size": 5, "sha1": sha1(b"named"), "md5": md5(b"named"),
|
||||
"crc32": crc(b"named")}},
|
||||
"indexes": {"by_md5": {md5(HELD): sha1(HELD), md5(b"named"): sha1(b"named")},
|
||||
"by_sha256": {}, "by_name": {"v.bin": [sha1(b"named")],
|
||||
"a.bin": [sha1(HELD)]}},
|
||||
}
|
||||
(self.tmp / "bios" / "Maker" / "Console").mkdir(parents=True)
|
||||
(self.tmp / "bios" / "Maker" / "Console" / "a.bin").write_bytes(HELD)
|
||||
(self.tmp / "bios" / "Maker" / "Console" / "v.bin").write_bytes(b"named")
|
||||
emulators = self.tmp / "emulators"
|
||||
emulators.mkdir()
|
||||
(emulators / "engine.yml").write_text(
|
||||
"emulator: Engine\ntype: standalone\nsystems: [x]\nfiles:\n"
|
||||
f" - name: a.bin\n system: x\n sha1: \"{sha1(HELD)}\"\n"
|
||||
f" - name: b.bin\n system: x\n sha1: \"{sha1(WANTED)}\"\n required: true\n"
|
||||
f" - name: m.bin\n system: x\n sha1: \"{sha1(MEMBER)}\"\n"
|
||||
f" - name: c.bin\n system: x\n crc32: \"{crc(CRC_ONLY)}\"\n size: {len(CRC_ONLY)}\n"
|
||||
" - name: nohash.bin\n system: x\n"
|
||||
" - name: later.bin\n system: x\n unsourceable: not dumped yet\n"
|
||||
f" md5: \"{md5(b'later')}\"\n"
|
||||
)
|
||||
self.profiles = {"engine": {
|
||||
"emulator": "Engine", "type": "standalone", "systems": ["x"],
|
||||
"files": [
|
||||
{"name": "a.bin", "system": "x", "sha1": sha1(HELD)},
|
||||
{"name": "b.bin", "system": "x", "sha1": sha1(WANTED), "required": True},
|
||||
{"name": "m.bin", "system": "x", "sha1": sha1(MEMBER)},
|
||||
{"name": "c.bin", "system": "x", "crc32": crc(CRC_ONLY), "size": len(CRC_ONLY)},
|
||||
{"name": "nohash.bin", "system": "x"},
|
||||
{"name": "later.bin", "system": "x", "unsourceable": "not dumped yet",
|
||||
"md5": md5(b"later")},
|
||||
],
|
||||
}}
|
||||
self.gap_report = {"engine": {
|
||||
"emulator": "Engine", "systems": ["x"],
|
||||
"gap_details": [
|
||||
{"name": name, "source": "missing", "identity": "", "required": name == "b.bin"}
|
||||
for name in ("a.bin", "b.bin", "m.bin", "c.bin", "nohash.bin")
|
||||
] + [{"name": "pkg.bin", "source": "missing", "bundled": True, "identity": ""}],
|
||||
"unsourceable": [{"name": "later.bin", "required": False, "reason": "not dumped yet"}],
|
||||
}}
|
||||
platform = {
|
||||
"x": {"files": [
|
||||
{"name": "v.bin", "md5": md5(VARIANT)},
|
||||
{"name": "cut.bin", "md5": md5(CUT)[:29]},
|
||||
{"name": "z.zip", "md5": md5(b"inner rom"), "zipped_file": "inner.rom"},
|
||||
{"name": "r.bin", "md5": ",".join(md5(r) for r in RECALBOX)},
|
||||
]},
|
||||
}
|
||||
self.coverages = {"plat": {
|
||||
"platform": "Plat", "config": {"systems": platform},
|
||||
"details": [
|
||||
{"name": "v.bin", "status": "untested", "system": "x", "required": True},
|
||||
{"name": "cut.bin", "status": "missing", "system": "x", "required": False},
|
||||
{"name": "z.zip", "status": "missing", "system": "x", "required": True},
|
||||
{"name": "r.bin", "status": "missing", "system": "x", "required": True},
|
||||
{"name": "a.bin", "status": "ok", "system": "x"},
|
||||
],
|
||||
}}
|
||||
self.ledger = [{"path": "bios/Maker/Console/set.zip", "members": [
|
||||
{"name": "m.bin", "size": len(MEMBER), "crc32": crc(MEMBER), "sha1": sha1(MEMBER)},
|
||||
]}]
|
||||
self.platform_hashes = {"sha1": set(), "md5": set(), "names": set(), "folders": {},
|
||||
"named": {}}
|
||||
self.profile_hashes = validate_pr.load_profile_hashes(str(emulators), self.db)
|
||||
self.placer = sitewanted.Placer(self.db, self.platform_hashes, self.profile_hashes)
|
||||
self.data = sitewanted.build_wanted(
|
||||
self.profiles, self.coverages, self.gap_report, self.db, self.placer,
|
||||
lambda system: f"System {system.upper()}", self.ledger,
|
||||
)
|
||||
|
||||
def entry(self, name: str) -> dict:
|
||||
found = [e for e in self.data["entries"] if e["n"] == name]
|
||||
self.assertEqual(len(found), 1, name)
|
||||
return found[0]
|
||||
|
||||
|
||||
class TheListHoldsWhatTheGapsReportLacks(_World):
|
||||
def test_names(self):
|
||||
names = sorted(e["n"] for e in self.data["entries"])
|
||||
# a.bin is held, m.bin is held inside set.zip, nohash.bin and z.zip
|
||||
# (a zipped_file md5 names a ROM inside the archive) declare nothing
|
||||
# a dropped file can be compared with, pkg.bin comes with its
|
||||
# emulator: none is wanted. r.bin is one entry per revision.
|
||||
self.assertEqual(names, ["b.bin", "c.bin", "cut.bin", "later.bin", "r.bin", "r.bin", "v.bin"])
|
||||
|
||||
def test_an_entry_carries_its_readers_system_and_need(self):
|
||||
entry = self.entry("b.bin")
|
||||
self.assertEqual(entry["h"], {"sha1": [sha1(WANTED)]})
|
||||
self.assertEqual(entry["s"], "System X")
|
||||
self.assertEqual(entry["r"], 1)
|
||||
self.assertEqual([self.data["readers"][i] for i in entry["w"]], [["e", "engine", "Engine"]])
|
||||
self.assertEqual([self.data["readers"][i] for i in self.entry("v.bin")["w"]],
|
||||
[["p", "plat", "Plat"]])
|
||||
|
||||
def test_hashes_as_the_resolver_reads_them(self):
|
||||
self.assertEqual(self.entry("c.bin")["h"], {"crc32": [crc(CRC_ONLY)]})
|
||||
self.assertEqual(self.entry("c.bin")["z"], [len(CRC_ONLY)])
|
||||
self.assertEqual(self.entry("cut.bin")["h"], {"md5": [md5(CUT)[:29]]})
|
||||
revisions = sorted(e["h"]["md5"][0] for e in self.data["entries"] if e["n"] == "r.bin")
|
||||
self.assertEqual(revisions, sorted(md5(r) for r in RECALBOX))
|
||||
|
||||
def test_a_cut_md5_another_wanted_md5_starts_is_that_md5(self):
|
||||
records = [{"hashes": {"md5": [md5(CUT)[:29]]}}, {"hashes": {"md5": [md5(CUT)]}}]
|
||||
sitewanted._settle_cut(records)
|
||||
self.assertEqual(records[0]["hashes"], {"md5": [md5(CUT)]})
|
||||
both = [{"hashes": {"md5": ["ab" * 14 + "a"]}},
|
||||
{"hashes": {"md5": ["ab" * 14 + "a" + "000"]}},
|
||||
{"hashes": {"md5": ["ab" * 14 + "a" + "111"]}}]
|
||||
sitewanted._settle_cut(both)
|
||||
self.assertEqual(both[0]["hashes"], {})
|
||||
|
||||
def test_a_cut_md5_a_held_md5_starts_is_held(self):
|
||||
held = sitewanted.Held(self.db)
|
||||
self.assertTrue(held.holds({"md5": [md5(HELD)[:29]]}, []))
|
||||
self.assertFalse(held.holds({"md5": [md5(CUT)[:29]]}, []))
|
||||
|
||||
def test_a_crc32_counts_at_its_declared_size(self):
|
||||
held = sitewanted.Held(self.db)
|
||||
self.assertTrue(held.holds({"crc32": [crc(HELD)]}, [len(HELD)]))
|
||||
self.assertTrue(held.holds({"crc32": [crc(HELD)]}, []))
|
||||
self.assertFalse(held.holds({"crc32": [crc(HELD)]}, [len(HELD) + 1]))
|
||||
|
||||
|
||||
class EveryFolderIsTheReviewsFolder(_World):
|
||||
"""The folder and name of each entry, against what validate_pr says for
|
||||
the same bytes dropped outside bios/: the destination --place copies to."""
|
||||
|
||||
def test_the_review_places_each_file_where_the_page_sends_it(self):
|
||||
platform_hashes = {**self.platform_hashes, "md5": {md5(VARIANT)}, "names": {"v.bin"}}
|
||||
for data, name in ((WANTED, "b.bin"), (VARIANT, "v.bin")):
|
||||
with self.subTest(name=name):
|
||||
path = self.tmp / "card" / name
|
||||
path.parent.mkdir(exist_ok=True)
|
||||
path.write_bytes(data)
|
||||
result = validate_pr.validate_file(
|
||||
f"card/{name}", self.db, platform_hashes, self.profile_hashes)
|
||||
entry = self.entry(name)
|
||||
folder = self.data["folders"][entry["f"]]
|
||||
expected = (f"{folder}.variants/{entry['t']}.{sha1(data)[:8]}" if entry.get("v")
|
||||
else folder + entry["t"])
|
||||
self.assertEqual(result.destination, expected)
|
||||
self.assertEqual(self.data["folders"][self.entry("b.bin")["f"]], "bios/Maker/Console/")
|
||||
self.assertEqual(self.entry("v.bin").get("v"), 1)
|
||||
|
||||
def test_each_entry_is_placed_by_validate_pr(self):
|
||||
for entry in self.data["entries"]:
|
||||
values = [v for algo in ("sha1", "md5", "sha256") for v in entry["h"].get(algo, [])
|
||||
if len(v) == sitewanted.FULL_LENGTH[algo]]
|
||||
spot = validate_pr.placement(values, entry["n"], self.db, self.platform_hashes,
|
||||
self.profile_hashes, declared=True,
|
||||
exists=self.placer.paths.__contains__)
|
||||
with self.subTest(entry=entry["n"]):
|
||||
self.assertEqual(self.data["folders"][entry["f"]] if "f" in entry else "",
|
||||
spot.folder)
|
||||
self.assertEqual(entry.get("t", ""), spot.name)
|
||||
self.assertEqual(bool(entry.get("v")), spot.variant)
|
||||
|
||||
def test_the_gaps_page_upload_link_reads_the_same_rule(self):
|
||||
source = (REPO_ROOT / "scripts" / "generate_site.py").read_text(encoding="utf-8")
|
||||
self.assertIn("folder = placer.folder(declared[0]) if placer and declared else \"\"", source)
|
||||
self.assertNotIn("def _upload_folder", source)
|
||||
self.assertEqual(self.placer.folder(self.profiles["engine"]["files"][1]),
|
||||
"bios/Maker/Console/")
|
||||
self.assertEqual(sitewanted.upload_href(sitewanted.UPLOAD_ROOT, "bios/Maker/Sub Dir/"),
|
||||
"https://github.com/Abdess/retrobios/upload/main/bios/Maker/Sub%20Dir")
|
||||
self.assertEqual(sitewanted.upload_href(sitewanted.UPLOAD_ROOT, ""),
|
||||
generate_site.UPLOAD_URL)
|
||||
|
||||
|
||||
def _node(script: str) -> str:
|
||||
node = shutil.which("node")
|
||||
if not node:
|
||||
raise unittest.SkipTest("node is missing")
|
||||
return subprocess.run([node, "--input-type=module-typescript", "-e", script],
|
||||
capture_output=True, text=True, check=True, cwd=WEBSITE).stdout
|
||||
|
||||
|
||||
class ThePageReadsTheListAsTheResolverDoes(_World):
|
||||
"""wanted.ts against the list built above: a dropped file matches by
|
||||
every hash its entry declares, and its upload goes where the review
|
||||
places it."""
|
||||
|
||||
def _page(self, data: bytes, own_name: str) -> dict:
|
||||
hashes = {"sha1": sha1(data), "md5": md5(data), "crc32": crc(data),
|
||||
"sha256": hashlib.sha256(data).hexdigest()}
|
||||
return json.loads(_node(
|
||||
f"import {{ matches, target, uploadUrl, issueUrl }} from {json.dumps(WANTED_TS.as_uri())};"
|
||||
f"const data = {json.dumps(self.data)}; const file = {json.dumps(hashes)};"
|
||||
f"const found = matches(data, file, {len(data)});"
|
||||
f"const place = found.length ? target(data, found[0], file, {json.dumps(own_name)}) : null;"
|
||||
"console.log(JSON.stringify({names: found.map((e) => e.n), place,"
|
||||
" upload: place && uploadUrl(data, place.folder),"
|
||||
f" issue: found.length && issueUrl(data, found[0].n, file, {len(data)})}}));"
|
||||
))
|
||||
|
||||
def test_matches(self):
|
||||
self.assertEqual(self._page(WANTED, "x.bin")["names"], ["b.bin"])
|
||||
self.assertEqual(self._page(CRC_ONLY, "x.bin")["names"], ["c.bin"])
|
||||
self.assertEqual(self._page(CUT, "x.bin")["names"], ["cut.bin"])
|
||||
self.assertEqual(self._page(RECALBOX[1], "x.bin")["names"], ["r.bin"])
|
||||
self.assertEqual(self._page(HELD, "a.bin")["names"], [])
|
||||
self.assertEqual(self._page(b"unknown", "u.bin")["names"], [])
|
||||
|
||||
def test_a_crc32_at_another_size_is_not_the_file(self):
|
||||
entry = {"n": "c.bin", "h": {"crc32": [crc(CRC_ONLY)]}, "w": [], "z": [len(CRC_ONLY) + 1]}
|
||||
out = _node(
|
||||
f"import {{ satisfies }} from {json.dumps(WANTED_TS.as_uri())};"
|
||||
f"console.log(JSON.stringify(satisfies({json.dumps(entry)}, "
|
||||
f"{{sha1: '', md5: '', sha256: '', crc32: {json.dumps(crc(CRC_ONLY))}}}, {len(CRC_ONLY)})));"
|
||||
)
|
||||
self.assertFalse(json.loads(out))
|
||||
|
||||
def test_the_upload_goes_where_the_review_places_it(self):
|
||||
page = self._page(WANTED, "dump.rom")
|
||||
self.assertEqual(page["place"], {"folder": "bios/Maker/Console/", "name": "b.bin"})
|
||||
self.assertEqual(page["upload"],
|
||||
"https://github.com/Abdess/retrobios/upload/main/bios/Maker/Console")
|
||||
variant = self._page(VARIANT, "v.bin")["place"]
|
||||
self.assertEqual(variant, {"folder": "bios/Maker/Console/.variants/",
|
||||
"name": f"v.bin.{sha1(VARIANT)[:8]}"})
|
||||
|
||||
def test_the_issue_form_is_filled_as_the_review_fills_it(self):
|
||||
result = validate_pr.ValidationResult("b.bin")
|
||||
result.sha1, result.md5, result.size = sha1(WANTED), md5(WANTED), len(WANTED)
|
||||
self.assertEqual(self._page(WANTED, "b.bin")["issue"], validate_pr.large_file_issue_url(result))
|
||||
|
||||
|
||||
class TheTwoPagesLinkEachOther(unittest.TestCase):
|
||||
"""files/ checks any file, wanted/ the ones the collection lacks: each
|
||||
sends a reader to the other in one line."""
|
||||
|
||||
def test_links(self):
|
||||
self.assertIn("[See if it is wanted](../wanted.md)", generate_site.generate_files_page())
|
||||
self.assertIn("[Find or check a file](files/index.md)", sitewanted.page(1))
|
||||
|
||||
|
||||
class ThePublishedList(unittest.TestCase):
|
||||
"""The list generate_site.py wrote, against the collection: nothing it
|
||||
holds, every folder by validate_pr, and light enough for a phone."""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls) -> None:
|
||||
if not PUBLISHED.is_file():
|
||||
raise unittest.SkipTest("no generated site data")
|
||||
cls.data = json.loads(PUBLISHED.read_text(encoding="utf-8"))
|
||||
cls.db = load_database(str(REPO_ROOT / "database.json"))
|
||||
|
||||
def test_nothing_held_is_wanted(self):
|
||||
held = sitewanted.Held(self.db, sitewanted.load_ledger(REPO_ROOT / "recipes" / "archives.json"))
|
||||
for entry in self.data["entries"]:
|
||||
with self.subTest(entry=entry["n"]):
|
||||
self.assertFalse(held.holds(entry["h"], entry.get("z", [])))
|
||||
|
||||
def test_every_folder_is_the_reviews(self):
|
||||
placer = sitewanted.Placer(
|
||||
self.db, validate_pr.load_platform_hashes(str(REPO_ROOT / "platforms"), self.db),
|
||||
validate_pr.load_profile_hashes(str(REPO_ROOT / "emulators"), self.db))
|
||||
for entry in self.data["entries"]:
|
||||
spot = placer.place(entry["h"], entry["n"])
|
||||
with self.subTest(entry=entry["n"]):
|
||||
self.assertEqual(self.data["folders"][entry["f"]] if "f" in entry else "", spot.folder)
|
||||
self.assertEqual(entry.get("t", ""), spot.name)
|
||||
|
||||
def test_weight(self):
|
||||
self.assertLess(PUBLISHED.stat().st_size, 200_000)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -186,6 +186,33 @@ class AFileOnlyACoreReadsIsKnown(_Fixture):
|
||||
self.assertTrue(any("Placed under bios/Other/Place/" in m for _s, m in placed.checks))
|
||||
|
||||
|
||||
class ADeclaredFileBesideItsHeldNameIsAVariant(_Fixture):
|
||||
"""A file a profile declares, of a system the collection files nowhere
|
||||
yet, whose name the collection holds: the review named the folder and
|
||||
left the copy to land on the held file of that name."""
|
||||
|
||||
def test_place_copies_it_beside_the_held_name(self):
|
||||
import hashlib # noqa: PLC0415
|
||||
|
||||
data = b"declared revision"
|
||||
emulators = self.tmp / "emulators"
|
||||
emulators.mkdir()
|
||||
(emulators / "engine.yml").write_text(
|
||||
"emulator: Engine\ntype: standalone\nsystems: [x, y]\nfiles:\n"
|
||||
f" - name: \"v.bin\"\n sha1: \"{hashlib.sha1(data).hexdigest()}\"\n"
|
||||
)
|
||||
db = {"files": {"a" * 40: {"path": "bios/Maker/Console/v.bin"}},
|
||||
"indexes": {"by_md5": {}, "by_sha256": {}, "by_name": {"v.bin": ["a" * 40]}}}
|
||||
known = validate_pr.load_profile_hashes(str(emulators), db)
|
||||
result = validate_pr.validate_file(
|
||||
self._bios_file("card/v.bin", data), db, EMPTY_HASHES, known)
|
||||
slot = f"bios/Maker/Console/.variants/v.bin.{result.sha1[:8]}"
|
||||
self.assertEqual(result.destination, slot)
|
||||
self.assertIn(("WARN", "File is not under bios/ yet. The collection keeps this name in "
|
||||
f"`bios/Maker/Console/`. --place copies it to `{slot}`"),
|
||||
result.checks)
|
||||
|
||||
|
||||
class ASizeEveryDeclarationRulesOutIsNotAVariant(_Fixture):
|
||||
"""1024 random bytes named scph5501.bin, a name every declaration sizes
|
||||
at 524288, were told "File size OK" and offered a .variants slot at the
|
||||
|
||||
Reference in new issue
Block a user