# deck-it - tests for games # Copyright (C) 2026 Danilo M. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) import json import tempfile import unittest import deckit.modules.games as games def lp(s: str) -> bytes: """A gamelist.cache string: little-endian u32 length, then the bytes.""" b = s.encode() return len(b).to_bytes(4, "little") + b def cache(*entries) -> bytes: """A gamelist.cache with (path, serial, title) entries and junk between.""" return b'GLCE"\0\0\0' + b"".join(lp(p) + lp(s) + lp(t) + b"\x01\x02\0\0\x9f" for p, s, t in entries) class Parsers(unittest.TestCase): def test_vdf_nested(self): d = games.vdf('"A"\n{\n\t"name"\t\t"Game \\"X\\""\n\t"apps"\n\t{\n\t\t"10"\t"1"\n\t}\n}\n') self.assertEqual(d, {"A": {"name": 'Game \\"X\\"', "apps": {"10": "1"}}}) def test_get_ignores_case(self): self.assertEqual(games._get({"Software": {"valve": {"x": "1"}}}, "software", "Valve", "x"), "1") self.assertIsNone(games._get({"a": "1"}, "a", "b")) def test_cache_serials(self): data = cache(("/g/A (Europe).cue", "SCES-01615", "A (Europe)"), ("/g/B.iso", "SLES-53559", "B - Most Wanted")) self.assertEqual(games.cache_serials(data), {"A (Europe)": "SCES-01615", "B - Most Wanted": "SLES-53559"}) def test_cache_serials_garbage(self): self.assertEqual(games.cache_serials(b"\xff" * 64), {}) class Lookup(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.root = Path(self.tmp.name) def tearDown(self): self.tmp.cleanup() def put(self, rel, data): p = self.root / rel p.parent.mkdir(parents=True, exist_ok=True) (p.write_bytes if isinstance(data, bytes) else p.write_text)(data) return p def steam(self): steam = self.root / "Steam" other = self.root / "lib2" self.put("Steam/steamapps/libraryfolders.vdf", f'"libraryfolders"\n{{\n"0"\n{{\n"path" "{steam}"\n}}\n"1"\n{{\n"path" "{other}"\n}}\n}}\n') self.put("lib2/steamapps/appmanifest_217200.acf", '"AppState"\n{\n"appid" "217200"\n"name" "Worms"\n}\n') self.put("Steam/userdata/1/config/localconfig.vdf", '"UserLocalConfigStore"\n{\n"Software"\n{\n"Valve"\n{\n"Steam"\n{\n"apps"\n{\n' '"217200"\n{\n"Playtime" "125"\n}\n}\n}\n}\n}\n}\n') self.put("Steam/appcache/librarycache/217200/library_600x900.jpg", b"JPG") return steam def test_steam_game(self): g = games.steam_game(self.steam(), "217200") self.assertEqual((g["platform"], g["name"], g["minutes"]), ("steam", "Worms", 125)) self.assertEqual(g["cover"].name, "library_600x900.jpg") def test_steam_game_missing_files(self): g = games.steam_game(self.root / "nope", "1") self.assertEqual((g["name"], g["minutes"], g["cover"]), (None, None, None)) def emu(self): emu = self.root / "duck" self.put("duck/cache/gamelist.cache", cache(("/g/a.cue", "SCES-01615", "Cool Boarders 3 (Europe)"))) self.put("duck/playtime.dat", "SCES-01615 1006 1789987473\n") self.put("duck/covers/SCES-01615.jpg", b"JPG") return emu def test_emu_game(self): g = games.emu_game("ps1", self.emu(), "playtime.dat", "Cool Boarders 3 (Europe)") self.assertEqual((g["platform"], g["name"], g["minutes"]), ("ps1", "Cool Boarders 3 (Europe)", 16)) self.assertEqual(g["cover"].name, "SCES-01615.jpg") def test_emu_game_title_contains_cache_title(self): g = games.emu_game("ps1", self.emu(), "playtime.dat", "Cool Boarders 3 (Europe) [SCES-01615]") self.assertEqual(g["minutes"], 16) def test_emu_game_unknown(self): g = games.emu_game("ps1", self.emu(), "playtime.dat", "Other") self.assertEqual((g["name"], g["minutes"], g["cover"]), ("Other", None, None)) def test_detect(self): emu = {"duckstation": ("ps1", self.root, "p")} d = lambda *cs: games.detect([{"class": c, "title": t} for c, t in cs], emu) self.assertEqual(d(("kitty", "x"), ("steam_app_42", "Game")), ("steam", "42", "Game")) self.assertEqual(d(("duckstation-qt", "Cool Boarders")), ("duckstation", "Cool Boarders", "Cool Boarders")) self.assertIsNone(d(("duckstation-qt", "DuckStation 0.1-1234"))) self.assertIsNone(d(("steam", "Steam"))) def make(self, clients): emu = {"duckstation": ("ps1", self.emu(), "playtime.dat")} run = lambda cmd, timeout=2: json.dumps(clients[0]) return games.make({"run": run, "steam_root": self.steam(), "emulators": emu}) def test_state_and_cover(self): clients = [[{"class": "steam_app_217200", "title": "Worms W.M.D"}]] m = self.make(clients) st = m.state()["playing"] self.assertEqual((st["platform"], st["name"], st["minutes"]), ("steam", "Worms", 125)) self.assertIsInstance(st["session"], int) key = st["cover"].rsplit("/", 1)[1] self.assertEqual(m.image(key), (b"JPG", "image/jpeg")) self.assertIsNone(m.image("other")) self.assertEqual(m.state()["playing"]["session"], st["session"]) # same game, same session clients[0] = [] self.assertEqual(m.state(), {"playing": None}) self.assertIsNone(m.image(key)) def test_state_hyprctl_fails(self): m = self.make([""]) self.assertEqual(m.state(), {"playing": None}) if __name__ == "__main__": unittest.main()