"""Tests for the local snapshot exporter.""" import hashlib import json import os import sys import tempfile import unittest import zipfile ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, "tools")) import db import export_snapshot as exporter class ExportSnapshotTests(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.addCleanup(self.tmp.cleanup) self.db_path = os.path.join(self.tmp.name, "snapshot.db") self.con = db.connect(self.db_path) self.addCleanup(self.con.close) db.init(self.con) self._seed() def _seed(self): con = self.con con.executemany( "INSERT INTO games (appid, name, type, release_date, metacritic, review_pct, parent_appid) " "VALUES (?,?,?,?,?,?,?)", [ (1, "Alpha", "game", "2020-01-01", 90, 95, None), (2, "Beta", "game", "2021-01-01", 80, 85, None), (3, "Gamma", "game", "2022-01-01", None, 75, None), (4, "Alpha DLC", "dlc", None, None, None, 1), ]) con.executemany( "INSERT INTO game_playtime (appid, minutes, last_played) VALUES (?,?,?)", [(1, 120, "2024-01-01"), (2, 0, None), (3, 30, None), (4, 0, None)]) con.execute("INSERT INTO game_install (appid, installed, size_bytes) VALUES (1, 1, 1024)") con.executemany( "INSERT INTO personal (appid, status, rating, favorite, notes) VALUES (?,?,?,?,?)", [(1, "playing", 9, 1, "личная заметка"), (2, "backlog", None, 0, None)]) con.executemany( "INSERT INTO game_ownership (appid, owned, checked_at) VALUES (?,?,?)", [(1, 1, "2026-01-01"), (2, 0, "2026-01-01")]) con.executemany( "INSERT INTO game_genres (appid, genre) VALUES (?,?)", [(1, "RPG"), (1, "Adventure"), (2, "RPG")]) con.executemany( "INSERT INTO tag_names (tag_id, name) VALUES (?,?)", [(1, "RPG"), (2, "Story Rich"), (3, "Cozy")]) con.executemany( "INSERT INTO game_tags (appid, tag_id) VALUES (?,?)", [(1, 1), (1, 2), (2, 1), (3, 3)]) con.execute( "INSERT INTO game_details (appid, short_description, about, header_image, " "developers, publishers, release_date, lang, status) " "VALUES (1, 'short', 'about', 'img', 'Dev', 'Pub', '2020', 'en', 'ok')") con.execute( "INSERT INTO game_translations (appid, lang, short_description, about, provider) " "VALUES (1, 'ru', 'кратко', 'полно', 'google')") con.execute( "UPDATE games SET hltb_main = 10.0, hltb_extra = 12.0, hltb_complete = 15.0, " "hltb_id = 42, hltb_checked_at = '2026-01-01', hltb_match = 1, " "hltb_updated_at = '2026-01-01' WHERE appid = 1") con.execute("INSERT INTO attributes (appid, key, value) VALUES (1, 'mood', 'calm')") con.execute( "INSERT INTO reco_feedback (appid, mood_key, verdict, created_at) " "VALUES (1, 'preset:dark-fantasy', 'like', '2026-01-01')") con.execute( "INSERT INTO moods (name, description, genres, tags, updated_at) " "VALUES ('custom-cozy', 'Уют', '[\"Simulation\"]', '{\"Cozy\": 1.5}', '2026-01-01')") con.execute( "INSERT INTO moods (name, description, genres, tags, updated_at) " "VALUES ('dark-fantasy', 'встроенный', '[]', '{}', '2026-01-01')") con.execute("INSERT OR REPLACE INTO schema_meta (key, value) VALUES ('steamid', '76561198000000000')") con.commit() def test_collect_basic(self): manifest, files = exporter.collect(self.con, handle="tester") self.assertEqual(manifest["format"], "steambase-snapshot") self.assertEqual(manifest["format_version"], 1) self.assertEqual(manifest["user"]["handle"], "tester") self.assertEqual(manifest["user"]["steamid"], "76561198000000000") self.assertEqual(manifest["counts"], { "catalog": 3, "library": 3, "attributes": 1, "feedback": 1, "moods": 1}) self.assertEqual(manifest["checksum_sha256"], exporter._checksum(files)) catalog = [json.loads(line) for line in files["catalog.jsonl"].decode().splitlines()] by_id = {item["appid"]: item for item in catalog} self.assertEqual(by_id[1]["tags"], ["RPG", "Story Rich"]) self.assertEqual(by_id[1]["genres"], ["Adventure", "RPG"]) self.assertEqual(by_id[1]["details"]["developers"], "Dev") self.assertEqual(by_id[1]["translation"]["lang"], "ru") self.assertEqual(by_id[1]["hltb"]["main"], 10.0) self.assertTrue(by_id[1]["hltb"]["match"]) library = [json.loads(line) for line in files["library.jsonl"].decode().splitlines()] alpha = next(item for item in library if item["appid"] == 1) self.assertEqual(alpha["minutes"], 120) self.assertEqual(alpha["status"], "playing") self.assertEqual(alpha["rating"], 9) self.assertTrue(alpha["favorite"]) self.assertEqual(alpha["notes"], "личная заметка") self.assertTrue(alpha["installed"]) self.assertTrue(alpha["owned"]) moods = json.loads(files["moods.json"]) self.assertEqual([mood["name"] for mood in moods], ["custom-cozy"]) def test_write_without_notes_and_with_dlc(self): path = os.path.join(self.tmp.name, "snapshot.zip") manifest = exporter.write_snapshot( self.con, path, handle="tester", notes=False, include_dlc=True) self.assertEqual(manifest["counts"]["catalog"], 4) with zipfile.ZipFile(path) as archive: self.assertEqual(export_snapshot_names(archive), { "manifest.json", "catalog.jsonl", "library.jsonl", "attributes.jsonl", "feedback.jsonl", "moods.json"}) library = [json.loads(line) for line in archive.read("library.jsonl").decode().splitlines()] self.assertTrue(all("notes" not in item for item in library)) stored = json.loads(archive.read("manifest.json")) self.assertEqual(stored["checksum_sha256"], exporter.snapshot_checksum(path)) def test_no_translations(self): manifest, files = exporter.collect(self.con, handle="tester", translations=False) catalog = [json.loads(line) for line in files["catalog.jsonl"].decode().splitlines()] self.assertTrue(all("translation" not in item for item in catalog)) def test_mode_validation(self): with self.assertRaises(ValueError): exporter.collect(self.con, mode="bad") def test_read_manifest(self): path = os.path.join(self.tmp.name, "snapshot.zip") exporter.write_snapshot(self.con, path, handle="tester") self.assertEqual(exporter.read_manifest(path)["user"]["handle"], "tester") def test_checksum_matches_contract_framing(self): manifest, files = exporter.collect(self.con, handle="tester") digest = hashlib.sha256() for name in ("catalog.jsonl", "library.jsonl", "attributes.jsonl", "feedback.jsonl", "moods.json"): data = files.get(name) if data is None: continue digest.update(name.encode("utf-8")) digest.update(b"\0") digest.update(str(len(data)).encode("ascii")) digest.update(b"\0") digest.update(data) self.assertEqual(manifest["checksum_sha256"], digest.hexdigest()) def export_snapshot_names(archive): return set(archive.namelist()) if __name__ == "__main__": unittest.main()