feat: add tests, CI and P0 reliability features
This commit is contained in:
1 parent
0754065dae
commit
626650e44a
22 files changed
+1576
-49
No files matched your search
@@ -0,0 +1,257 @@
|
||||
"""Tests for the Steam file readers."""
|
||||
|
||||
import os
|
||||
import struct
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, os.path.join(ROOT, "tools"))
|
||||
|
||||
import steam_source
|
||||
|
||||
|
||||
class VdfBuilder:
|
||||
"""Build a binary VDF blob in the appinfo.vdf v29 format."""
|
||||
|
||||
def __init__(self):
|
||||
self._strings = []
|
||||
self._index = {}
|
||||
|
||||
def _idx(self, value):
|
||||
if value not in self._index:
|
||||
self._index[value] = len(self._strings)
|
||||
self._strings.append(value)
|
||||
return self._index[value]
|
||||
|
||||
def _entry(self, kind, key, payload=b""):
|
||||
return struct.pack("<BI", kind, self._idx(key)) + payload
|
||||
|
||||
def obj(self, key, payload):
|
||||
return self._entry(0x00, key, payload + b"\x08")
|
||||
|
||||
def string(self, key, value):
|
||||
return self._entry(0x01, key, value.encode("utf-8") + b"\x00")
|
||||
|
||||
def int32(self, key, value):
|
||||
return self._entry(0x02, key, struct.pack("<i", value))
|
||||
|
||||
def float32(self, key, value):
|
||||
return self._entry(0x03, key, struct.pack("<f", value))
|
||||
|
||||
def uint32(self, key, value, kind=0x04):
|
||||
return self._entry(kind, key, struct.pack("<I", value))
|
||||
|
||||
def uint64(self, key, value):
|
||||
return self._entry(0x07, key, struct.pack("<Q", value))
|
||||
|
||||
def int64(self, key, value):
|
||||
return self._entry(0x0A, key, struct.pack("<q", value))
|
||||
|
||||
|
||||
def _unique(values):
|
||||
seen = set()
|
||||
out = []
|
||||
for value in values:
|
||||
if value not in seen:
|
||||
seen.add(value)
|
||||
out.append(value)
|
||||
return out
|
||||
|
||||
|
||||
def make_app(appid, common_payload):
|
||||
builder = VdfBuilder()
|
||||
body = builder.obj(
|
||||
"appinfo",
|
||||
builder.uint32("appid", appid) + builder.obj("common", common_payload(builder)),
|
||||
) + b"\x08"
|
||||
return appid, body, builder._strings
|
||||
|
||||
|
||||
def build_appinfo_from_apps(apps, magic=0x07564429):
|
||||
"""Return bytes for a synthetic appinfo.vdf built from make_app() records."""
|
||||
records = b""
|
||||
strings = []
|
||||
for appid, payload, app_strings in apps:
|
||||
records += struct.pack("<II", appid, len(payload) + 60) + b"\x00" * 60 + payload
|
||||
strings.extend(app_strings)
|
||||
records += b"\x00" * 8
|
||||
|
||||
strings = _unique(strings)
|
||||
string_table = struct.pack("<I", len(strings))
|
||||
for value in strings:
|
||||
string_table += value.encode("utf-8") + b"\x00"
|
||||
|
||||
table_offset = 16 + len(records)
|
||||
header = struct.pack("<IIQ", magic, 0, table_offset)
|
||||
return header + records + string_table
|
||||
|
||||
|
||||
class AppinfoStringsTests(unittest.TestCase):
|
||||
def test_reads_count_and_utf8_strings(self):
|
||||
data = struct.pack("<I", 3) + b"alpha\x00beta\x00\xd0\xb8\xd0\xb3\xd1\x80\xd0\xb0\x00"
|
||||
self.assertEqual(steam_source._appinfo_strings(data, 0), ["alpha", "beta", "игра"])
|
||||
|
||||
def test_empty_table(self):
|
||||
self.assertEqual(steam_source._appinfo_strings(struct.pack("<I", 0), 0), [])
|
||||
|
||||
|
||||
class AppinfoKvTests(unittest.TestCase):
|
||||
def _parse(self, payload):
|
||||
builder = VdfBuilder()
|
||||
body = builder.obj("root", payload(builder)) + b"\x08"
|
||||
return steam_source._appinfo_kv(body, 0, builder._strings)[0]["root"]
|
||||
|
||||
def test_all_scalar_types(self):
|
||||
result = self._parse(lambda b: (
|
||||
b.string("text", "value")
|
||||
+ b.int32("signed", -7)
|
||||
+ b.float32("ratio", 1.5)
|
||||
+ b.uint32("pointer", 11, 0x04)
|
||||
+ b.uint32("uint", 12, 0x05)
|
||||
+ b.uint32("color", 13, 0x06)
|
||||
+ b.uint64("big", 2**40)
|
||||
+ b.int64("huge", -(2**40))
|
||||
))
|
||||
self.assertEqual(result["text"], "value")
|
||||
self.assertEqual(result["signed"], -7)
|
||||
self.assertAlmostEqual(result["ratio"], 1.5)
|
||||
self.assertEqual(result["pointer"], 11)
|
||||
self.assertEqual(result["uint"], 12)
|
||||
self.assertEqual(result["color"], 13)
|
||||
self.assertEqual(result["big"], 2**40)
|
||||
self.assertEqual(result["huge"], -(2**40))
|
||||
|
||||
def test_nested_objects(self):
|
||||
result = self._parse(lambda b: b.obj("outer", b.string("inner", "x") + b.obj("deep", b.uint32("id", 5))))
|
||||
self.assertEqual(result["outer"]["inner"], "x")
|
||||
self.assertEqual(result["outer"]["deep"]["id"], 5)
|
||||
|
||||
def test_unknown_type_raises(self):
|
||||
data = b"\x63" + struct.pack("<I", 0)
|
||||
with self.assertRaises(ValueError):
|
||||
steam_source._appinfo_kv(data, 0, ["key"])
|
||||
|
||||
|
||||
class ReadAppinfoTests(unittest.TestCase):
|
||||
def test_parses_synthetic_file(self):
|
||||
apps = [
|
||||
make_app(100, lambda b: (
|
||||
b.string("name", "Тестовая игра")
|
||||
+ b.string("type", "Game")
|
||||
+ b.obj("genres", b.uint32("0", 25) + b.uint32("1", 3))
|
||||
+ b.obj("store_tags", b.uint32("0", 122) + b.uint32("1", 4747))
|
||||
+ b.uint32("metacritic_score", 96)
|
||||
+ b.uint32("review_percentage", 91)
|
||||
+ b.uint32("steam_release_date", 1691074800)
|
||||
)),
|
||||
make_app(200, lambda b: b.string("name", "Tool") + b.string("type", "Tool")),
|
||||
]
|
||||
data = build_appinfo_from_apps(apps)
|
||||
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
os.makedirs(os.path.join(root, "appcache"))
|
||||
with open(os.path.join(root, "appcache", "appinfo.vdf"), "wb") as fh:
|
||||
fh.write(data)
|
||||
result = steam_source.read_appinfo(root)
|
||||
|
||||
self.assertEqual(set(result), {100, 200})
|
||||
game = result[100]
|
||||
self.assertEqual(game["name"], "Тестовая игра")
|
||||
self.assertEqual(game["type"], "Game")
|
||||
self.assertEqual(game["genres"], {"0": 25, "1": 3})
|
||||
self.assertEqual(game["tags"], {"0": 122, "1": 4747})
|
||||
self.assertEqual(game["metacritic"], 96)
|
||||
self.assertEqual(game["review_pct"], 91)
|
||||
self.assertEqual(game["release"], 1691074800)
|
||||
self.assertEqual(result[200]["tags"], {})
|
||||
|
||||
def test_skips_malformed_records_and_continues(self):
|
||||
good = make_app(300, lambda b: b.string("name", "Good"))
|
||||
junk = (301, b"\x63" + struct.pack("<I", 0), [])
|
||||
data = build_appinfo_from_apps([junk, good])
|
||||
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
os.makedirs(os.path.join(root, "appcache"))
|
||||
with open(os.path.join(root, "appcache", "appinfo.vdf"), "wb") as fh:
|
||||
fh.write(data)
|
||||
result = steam_source.read_appinfo(root)
|
||||
|
||||
self.assertEqual(set(result), {300})
|
||||
self.assertEqual(result[300]["name"], "Good")
|
||||
|
||||
def test_missing_file_returns_empty(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
self.assertEqual(steam_source.read_appinfo(root), {})
|
||||
|
||||
def test_rejects_unsupported_version(self):
|
||||
data = build_appinfo_from_apps([], magic=0x07564428)
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
os.makedirs(os.path.join(root, "appcache"))
|
||||
with open(os.path.join(root, "appcache", "appinfo.vdf"), "wb") as fh:
|
||||
fh.write(data)
|
||||
with self.assertRaises(SystemExit):
|
||||
steam_source.read_appinfo(root)
|
||||
|
||||
def test_real_steam_file(self):
|
||||
try:
|
||||
root = steam_source.find_steam_root()
|
||||
except SystemExit:
|
||||
self.skipTest("Steam is not installed")
|
||||
path = os.path.join(root, "appcache", "appinfo.vdf")
|
||||
if not os.path.isfile(path):
|
||||
self.skipTest("appinfo.vdf is not present")
|
||||
|
||||
result = steam_source.read_appinfo(root)
|
||||
self.assertIsInstance(result, dict)
|
||||
self.assertTrue(result, "real appinfo.vdf should contain apps")
|
||||
|
||||
expected_keys = {"name", "type", "genres", "tags", "metacritic", "review_pct", "release"}
|
||||
for appid, info in result.items():
|
||||
self.assertIsInstance(appid, int)
|
||||
self.assertEqual(set(info), expected_keys)
|
||||
self.assertIsInstance(info["genres"], dict)
|
||||
self.assertIsInstance(info["tags"], dict)
|
||||
|
||||
self.assertTrue(any(info["name"] for info in result.values()))
|
||||
named_genres = [info for info in result.values() if info["genres"]]
|
||||
self.assertTrue(named_genres, "real appinfo.vdf should contain genres")
|
||||
for info in named_genres:
|
||||
self.assertTrue(all(isinstance(v, int) for v in info["genres"].values()))
|
||||
|
||||
|
||||
class FindCoverTests(unittest.TestCase):
|
||||
def _touch(self, path):
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
with open(path, "wb") as fh:
|
||||
fh.write(b"x")
|
||||
|
||||
def test_flat_layout(self):
|
||||
with tempfile.TemporaryDirectory() as cache:
|
||||
self._touch(os.path.join(cache, "10", "header.jpg"))
|
||||
self.assertEqual(steam_source.find_cover(10, cache), os.path.join(cache, "10", "header.jpg"))
|
||||
|
||||
def test_hashed_layout(self):
|
||||
with tempfile.TemporaryDirectory() as cache:
|
||||
path = os.path.join(cache, "11", "abcdef", "library_capsule.jpg")
|
||||
self._touch(path)
|
||||
self.assertEqual(steam_source.find_cover(11, cache), path)
|
||||
|
||||
def test_prefers_best_cover_across_layouts(self):
|
||||
with tempfile.TemporaryDirectory() as cache:
|
||||
self._touch(os.path.join(cache, "12", "hash", "header.jpg"))
|
||||
best = os.path.join(cache, "12", "library_600x900.jpg")
|
||||
self._touch(os.path.join(cache, "12", "library_capsule.jpg"))
|
||||
self._touch(best)
|
||||
self.assertEqual(steam_source.find_cover(12, cache), best)
|
||||
|
||||
def test_missing_cover_returns_none(self):
|
||||
with tempfile.TemporaryDirectory() as cache:
|
||||
self.assertIsNone(steam_source.find_cover(13, cache))
|
||||
self.assertIsNone(steam_source.find_cover(13, None))
|
||||
self.assertIsNone(steam_source.find_cover(13, os.path.join(cache, "nope")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user