neser 1.2.0

NESER - Nintendo Emulation Systems Engine (Rust). Desktop and WebAssembly frontends.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
"""Tests for MetadataDb — runs entirely in-memory, no network."""
import os
import sys
import unittest

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from metadata_db import MetadataDb

class TestMetadataDbSchema(unittest.TestCase):
    def setUp(self):
        self.db = MetadataDb(":memory:")

    def tearDown(self):
        self.db.close()

    def _table_names(self):
        cur = self.db._conn.execute(
            "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"
        )
        return {row[0] for row in cur.fetchall()}

    def test_all_expected_tables_are_created(self):
        tables = self._table_names()
        for expected in (
            "platforms",
            "games",
            "genres",
            "developers",
            "publishers",
            "regions",
            "countries",
            "game_genres",
            "game_developers",
            "game_publishers",
            "images",
            "image_base_urls",
            "sync_log",
        ):
            self.assertIn(expected, tables)


class TestMetadataDbPlatforms(unittest.TestCase):
    def setUp(self):
        self.db = MetadataDb(":memory:")

    def tearDown(self):
        self.db.close()

    def test_upsert_platform_inserts_new_row(self):
        self.db.upsert_platform({"id": 7, "name": "Nintendo Entertainment System (NES)", "alias": "nes"})
        row = self.db.get_platform(7)
        self.assertIsNotNone(row)
        self.assertEqual(row["name"], "Nintendo Entertainment System (NES)")
        self.assertEqual(row["alias"], "nes")

    def test_upsert_platform_updates_existing_row(self):
        self.db.upsert_platform({"id": 7, "name": "Old Name", "alias": "old"})
        self.db.upsert_platform({"id": 7, "name": "New Name", "alias": "new"})
        row = self.db.get_platform(7)
        self.assertEqual(row["name"], "New Name")

    def test_get_platform_returns_none_for_unknown_id(self):
        self.assertIsNone(self.db.get_platform(9999))

    def test_list_platforms_returns_all_platforms(self):
        self.db.upsert_platform({"id": 7, "name": "NES", "alias": "nes"})
        self.db.upsert_platform({"id": 4, "name": "Game Boy", "alias": "gb"})
        platforms = self.db.list_platforms()
        self.assertEqual(len(platforms), 2)


class TestMetadataDbReferenceData(unittest.TestCase):
    def setUp(self):
        self.db = MetadataDb(":memory:")

    def tearDown(self):
        self.db.close()

    def test_upsert_genre_and_retrieve(self):
        self.db.upsert_reference("genres", {"id": 15, "name": "Action"})
        row = self.db.get_reference("genres", 15)
        self.assertIsNotNone(row)
        self.assertEqual(row["name"], "Action")

    def test_upsert_developer_and_retrieve(self):
        self.db.upsert_reference("developers", {"id": 389, "name": "Nintendo"})
        row = self.db.get_reference("developers", 389)
        self.assertEqual(row["name"], "Nintendo")

    def test_upsert_publisher_and_retrieve(self):
        self.db.upsert_reference("publishers", {"id": 252, "name": "Nintendo of America"})
        row = self.db.get_reference("publishers", 252)
        self.assertEqual(row["name"], "Nintendo of America")

    def test_upsert_region_and_retrieve(self):
        self.db.upsert_reference("regions", {"id": 1, "name": "US"})
        row = self.db.get_reference("regions", 1)
        self.assertEqual(row["name"], "US")

    def test_upsert_country_and_retrieve(self):
        self.db.upsert_reference("countries", {"id": 50, "name": "United States"})
        row = self.db.get_reference("countries", 50)
        self.assertEqual(row["name"], "United States")

    def test_upsert_reference_updates_existing(self):
        self.db.upsert_reference("genres", {"id": 15, "name": "Old Name"})
        self.db.upsert_reference("genres", {"id": 15, "name": "Action"})
        row = self.db.get_reference("genres", 15)
        self.assertEqual(row["name"], "Action")

    def test_upsert_reference_rejects_invalid_table(self):
        with self.assertRaises(ValueError):
            self.db.upsert_reference("evil_table; DROP TABLE games", {"id": 1, "name": "x"})

    def test_get_reference_rejects_invalid_table(self):
        with self.assertRaises(ValueError):
            self.db.get_reference("evil_table", 1)


class TestMetadataDbGames(unittest.TestCase):
    def setUp(self):
        self.db = MetadataDb(":memory:")
        self.db.upsert_platform({"id": 7, "name": "NES", "alias": "nes"})
        self._sample_game = {
            "id": 135,
            "game_title": "Castlevania",
            "release_date": "1987-05-01",
            "platform": 7,
            "region_id": 2,
            "country_id": 50,
            "players": 1,
            "overview": "Fight Dracula.",
            "last_updated": "2025-08-10 23:58:25",
            "rating": "E - Everyone",
            "coop": "No",
            "youtube": "ENSDrPpFp-Y",
            "alternates": None,
            "developers": [476],
            "genres": [15],
            "publishers": [23],
        }

    def tearDown(self):
        self.db.close()

    def test_upsert_game_inserts_new_row(self):
        self.db.upsert_game(self._sample_game)
        game = self.db.get_game(135)
        self.assertIsNotNone(game)
        self.assertEqual(game["game_title"], "Castlevania")

    def test_upsert_game_updates_existing_row(self):
        self.db.upsert_game(self._sample_game)
        updated = dict(self._sample_game)
        updated["game_title"] = "Castlevania Updated"
        self.db.upsert_game(updated)
        game = self.db.get_game(135)
        self.assertEqual(game["game_title"], "Castlevania Updated")

    def test_upsert_game_stores_overview(self):
        self.db.upsert_game(self._sample_game)
        game = self.db.get_game(135)
        self.assertEqual(game["overview"], "Fight Dracula.")

    def test_upsert_game_stores_platform_id(self):
        self.db.upsert_game(self._sample_game)
        game = self.db.get_game(135)
        self.assertEqual(game["platform_id"], 7)

    def test_get_game_returns_none_for_unknown_id(self):
        self.assertIsNone(self.db.get_game(9999))

    def test_upsert_game_creates_genre_join_rows(self):
        self.db.upsert_reference("genres", {"id": 15, "name": "Action"})
        self.db.upsert_game(self._sample_game)
        genres = self.db.get_game_genres(135)
        self.assertEqual(genres, [15])

    def test_upsert_game_creates_developer_join_rows(self):
        self.db.upsert_reference("developers", {"id": 476, "name": "Konami"})
        self.db.upsert_game(self._sample_game)
        devs = self.db.get_game_developers(135)
        self.assertEqual(devs, [476])

    def test_upsert_game_creates_publisher_join_rows(self):
        self.db.upsert_reference("publishers", {"id": 23, "name": "Konami Inc."})
        self.db.upsert_game(self._sample_game)
        pubs = self.db.get_game_publishers(135)
        self.assertEqual(pubs, [23])

    def test_list_games_by_platform(self):
        self.db.upsert_game(self._sample_game)
        games = self.db.list_games(platform_id=7)
        self.assertEqual(len(games), 1)
        self.assertEqual(games[0]["id"], 135)

    def test_list_games_by_platform_excludes_other_platforms(self):
        self.db.upsert_platform({"id": 4, "name": "GB", "alias": "gb"})
        other = dict(self._sample_game)
        other["id"] = 999
        other["platform"] = 4
        self.db.upsert_game(other)
        self.db.upsert_game(self._sample_game)
        games = self.db.list_games(platform_id=4)
        self.assertEqual(len(games), 1)
        self.assertEqual(games[0]["id"], 999)

    def test_list_all_games_returns_all_platforms(self):
        self.db.upsert_platform({"id": 4, "name": "GB", "alias": "gb"})
        other = dict(self._sample_game)
        other["id"] = 999
        other["platform"] = 4
        self.db.upsert_game(other)
        self.db.upsert_game(self._sample_game)
        games = self.db.list_games()
        self.assertEqual(len(games), 2)


class TestMetadataDbImages(unittest.TestCase):
    def setUp(self):
        self.db = MetadataDb(":memory:")
        self.db.upsert_platform({"id": 7, "name": "NES", "alias": "nes"})
        game = {
            "id": 135,
            "game_title": "Castlevania",
            "release_date": "1987-05-01",
            "platform": 7,
            "region_id": 2,
            "country_id": 50,
            "players": 1,
            "overview": "",
            "last_updated": "",
            "rating": "",
            "coop": "",
            "youtube": "",
            "alternates": None,
            "developers": [],
            "genres": [],
            "publishers": [],
        }
        self.db.upsert_game(game)

    def tearDown(self):
        self.db.close()

    def test_upsert_image_stores_filename_and_type(self):
        self.db.upsert_image({
            "id": 718,
            "game_id": 135,
            "type": "boxart",
            "side": "back",
            "filename": "boxart/back/135-2.jpg",
            "resolution": "1000x1435",
        })
        images = self.db.get_game_images(135)
        self.assertEqual(len(images), 1)
        self.assertEqual(images[0]["filename"], "boxart/back/135-2.jpg")
        self.assertEqual(images[0]["type"], "boxart")

    def test_upsert_image_updates_existing_by_id(self):
        self.db.upsert_image({"id": 718, "game_id": 135, "type": "boxart", "side": "back",
                               "filename": "old.jpg", "resolution": None})
        self.db.upsert_image({"id": 718, "game_id": 135, "type": "boxart", "side": "back",
                               "filename": "new.jpg", "resolution": None})
        images = self.db.get_game_images(135)
        self.assertEqual(len(images), 1)
        self.assertEqual(images[0]["filename"], "new.jpg")

    def test_get_game_images_returns_empty_list_for_no_images(self):
        images = self.db.get_game_images(135)
        self.assertEqual(images, [])

    def test_get_game_image_counts_by_type_returns_empty_dict_for_no_images(self):
        counts = self.db.get_game_image_counts_by_type(135)
        self.assertEqual(counts, {})

    def test_get_game_image_counts_by_type_groups_by_type(self):
        self.db.upsert_game({"id": 135, "game_title": "Castlevania", "platform": 7})
        for img_id, img_type in [
            (1, "boxart"), (2, "boxart"), (3, "screenshot"),
            (4, "screenshot"), (5, "screenshot"), (6, "fanart"),
        ]:
            self.db.upsert_image({"id": img_id, "game_id": 135, "type": img_type,
                                   "side": None, "filename": f"{img_id}.jpg", "resolution": None})
        counts = self.db.get_game_image_counts_by_type(135)
        self.assertEqual(counts, {"boxart": 2, "screenshot": 3, "fanart": 1})

    def test_get_game_image_counts_by_type_handles_null_type(self):
        self.db.upsert_game({"id": 135, "game_title": "Castlevania", "platform": 7})
        self.db.upsert_image({"id": 1, "game_id": 135, "type": None,
                               "side": None, "filename": "1.jpg", "resolution": None})
        self.db.upsert_image({"id": 2, "game_id": 135, "type": "boxart",
                               "side": None, "filename": "2.jpg", "resolution": None})
        counts = self.db.get_game_image_counts_by_type(135)
        self.assertEqual(counts.get("boxart"), 1)
        self.assertEqual(counts.get(None), 1)

    def test_upsert_image_base_urls_stores_all_sizes(self):
        base_urls = {
            "original": "https://cdn.thegamesdb.net/images/original/",
            "small": "https://cdn.thegamesdb.net/images/small/",
            "thumb": "https://cdn.thegamesdb.net/images/thumb/",
            "cropped_center_thumb": "https://cdn.thegamesdb.net/images/cropped_center_thumb/",
            "medium": "https://cdn.thegamesdb.net/images/medium/",
            "large": "https://cdn.thegamesdb.net/images/large/",
        }
        self.db.upsert_image_base_urls(base_urls)
        stored = self.db.get_image_base_urls()
        for size, url in base_urls.items():
            self.assertEqual(stored[size], url)

    def test_build_image_url_combines_base_and_filename(self):
        self.db.upsert_image_base_urls({
            "original": "https://cdn.thegamesdb.net/images/original/",
        })
        self.db.upsert_image({"id": 718, "game_id": 135, "type": "boxart", "side": "back",
                               "filename": "boxart/back/135-2.jpg", "resolution": None})
        url = self.db.build_image_url(718, "original")
        self.assertEqual(url, "https://cdn.thegamesdb.net/images/original/boxart/back/135-2.jpg")

    def test_build_image_url_raises_for_unknown_size(self):
        self.db.upsert_image({"id": 718, "game_id": 135, "type": "boxart", "side": "back",
                               "filename": "boxart/back/135-2.jpg", "resolution": None})
        with self.assertRaises(KeyError):
            self.db.build_image_url(718, "nonexistent_size")


class TestMetadataDbSyncLog(unittest.TestCase):
    def setUp(self):
        self.db = MetadataDb(":memory:")
        self.db.upsert_platform({"id": 7, "name": "NES", "alias": "nes"})

    def tearDown(self):
        self.db.close()

    def test_get_sync_log_returns_none_for_new_platform(self):
        log = self.db.get_sync_log(7)
        self.assertIsNone(log)

    def test_update_sync_log_full_sync(self):
        self.db.update_sync_log(7, last_full_sync="2026-01-01T00:00:00", last_update_id=1000)
        log = self.db.get_sync_log(7)
        self.assertIsNotNone(log)
        self.assertEqual(log["last_full_sync"], "2026-01-01T00:00:00")
        self.assertEqual(log["last_update_id"], 1000)

    def test_update_sync_log_incremental_sync(self):
        self.db.update_sync_log(7, last_full_sync="2026-01-01T00:00:00", last_update_id=1000)
        self.db.update_sync_log(7, last_incremental_sync="2026-02-01T00:00:00", last_update_id=2000)
        log = self.db.get_sync_log(7)
        self.assertEqual(log["last_incremental_sync"], "2026-02-01T00:00:00")
        self.assertEqual(log["last_update_id"], 2000)
        # full sync timestamp should be preserved
        self.assertEqual(log["last_full_sync"], "2026-01-01T00:00:00")

    def test_get_game_count_per_platform(self):
        self.db.upsert_platform({"id": 4, "name": "GB", "alias": "gb"})
        game_nes = {
            "id": 135, "game_title": "Castlevania", "release_date": "", "platform": 7,
            "region_id": 0, "country_id": 0, "players": 1, "overview": "",
            "last_updated": "", "rating": "", "coop": "", "youtube": "",
            "alternates": None, "developers": [], "genres": [], "publishers": [],
        }
        self.db.upsert_game(game_nes)
        counts = self.db.get_game_counts()
        self.assertEqual(counts[7], 1)
        self.assertNotIn(4, counts)


class TestMetadataDbSearchGames(unittest.TestCase):
    """Tests for MetadataDb.search_games — case-insensitive substring lookup."""

    def setUp(self):
        self.db = MetadataDb(":memory:")
        self.db.upsert_platform({"id": 7, "name": "NES", "alias": "nes"})
        self.db.upsert_platform({"id": 4, "name": "GB", "alias": "gb"})
        self._base = {
            "release_date": "", "region_id": 0, "country_id": 0, "players": 1,
            "overview": "", "last_updated": "", "rating": "", "coop": "",
            "youtube": "", "alternates": None, "developers": [], "genres": [], "publishers": [],
        }

    def tearDown(self):
        self.db.close()

    def _game(self, game_id, title, platform=7):
        return {"id": game_id, "game_title": title, "platform": platform, **self._base}

    def test_returns_exact_match(self):
        self.db.upsert_game(self._game(135, "Castlevania"))
        results = self.db.search_games("Castlevania")
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]["id"], 135)

    def test_partial_name_matches_substring(self):
        self.db.upsert_game(self._game(135, "Castlevania"))
        self.db.upsert_game(self._game(136, "Castlevania II - Simon's Quest"))
        results = self.db.search_games("castlevania")
        self.assertEqual(len(results), 2)

    def test_case_insensitive_match(self):
        self.db.upsert_game(self._game(135, "Castlevania"))
        results = self.db.search_games("CASTLE")
        self.assertEqual(len(results), 1)

    def test_no_match_returns_empty_list(self):
        self.db.upsert_game(self._game(135, "Castlevania"))
        results = self.db.search_games("zeldaXXX")
        self.assertEqual(results, [])

    def test_platform_filter_narrows_results(self):
        self.db.upsert_game(self._game(135, "Castlevania", platform=7))
        self.db.upsert_game(self._game(200, "Castlevania II", platform=4))
        results = self.db.search_games("castlevania", platform_id=7)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]["id"], 135)

    def test_results_include_game_fields(self):
        self.db.upsert_game(self._game(135, "Castlevania"))
        results = self.db.search_games("Castlevania")
        self.assertIn("id", results[0])
        self.assertIn("game_title", results[0])
        self.assertIn("platform_id", results[0])


if __name__ == "__main__":
    unittest.main()