about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-07-03 20:45:50 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-07-03 20:45:50 +0300
commit8e6b522d09ad85663e319e88cedbe896e328e49a (patch)
treed3a2311101ba14c3528dc91ef334e32b48c68124
parente2ec4402a4d53dc08bd6773dc5b680e0eb283fb9 (diff)
downloadfungus-launcher-8e6b522d09ad85663e319e88cedbe896e328e49a.tar.gz
fungus-launcher-8e6b522d09ad85663e319e88cedbe896e328e49a.tar.xz
update tests
-rw-r--r--fungus/game_model.py5
-rw-r--r--tests/conftest.py15
-rw-r--r--tests/test_game_model.py254
3 files changed, 272 insertions, 2 deletions
diff --git a/fungus/game_model.py b/fungus/game_model.py
index b15c05c..a21ad99 100644
--- a/fungus/game_model.py
+++ b/fungus/game_model.py
@@ -439,7 +439,10 @@ def migrate_to_per_game(
     # Step 2: load games from JSON
     games = load_games_json(games_json_path)
     if not games:
-        # Empty or missing file — nothing to migrate
+        # Empty or missing file — nothing to migrate, but still rename if source exists
+        if not dry_run and os.path.isfile(games_json_path):
+            migrated_path = games_json_path + ".migrated"
+            os.replace(games_json_path, migrated_path)
         return result
 
     # Step 3: deduplicate by gameid (keep first occurrence)
diff --git a/tests/conftest.py b/tests/conftest.py
index 7100715..64039b7 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -71,3 +71,18 @@ def populated_games_file(games_file, sample_games):
 
     save_games_json(str(games_file), sample_games)
     return games_file
+
+
+@pytest.fixture()
+def sample_games_dir(tmp_path, sample_games):
+    """Provide a temporary games directory with sample .ini files.
+
+    Returns the path to the games directory.
+    """
+    from fungus.game_model import GameLibrary
+
+    games_dir = tmp_path / "games"
+    library = GameLibrary(str(games_dir))
+    for game in sample_games:
+        library.save(game)
+    return games_dir
diff --git a/tests/test_game_model.py b/tests/test_game_model.py
index 321c879..7558648 100644
--- a/tests/test_game_model.py
+++ b/tests/test_game_model.py
@@ -1,12 +1,16 @@
-"""Tests for fungus.game_model — Game dataclass and JSON I/O helpers."""
+"""Tests for fungus.game_model — Game dataclass, GameLibrary, and migration."""
 
 import json
 import os
 
 from fungus.game_model import (
     Game,
+    GameLibrary,
+    MigrationResult,
     backup_games_file,
     load_games_json,
+    migrate_to_per_game,
+    needs_migration,
     save_games_json,
     update_game_field,
 )
@@ -223,3 +227,251 @@ class TestBackupGamesFile:
         backup_dest = tmp_config_dir / "backups"
         backup_games_file("/nonexistent/path.json", backup_dir=str(backup_dest))
         assert not backup_dest.exists()
+
+
+# ── GameLibrary ─────────────────────────────────────────────────────────
+
+
+class TestGameLibrary:
+    def test_exists_true(self, sample_games_dir):
+        library = GameLibrary(str(sample_games_dir))
+        assert library.exists("umu-1234") is True
+
+    def test_exists_false(self, sample_games_dir):
+        library = GameLibrary(str(sample_games_dir))
+        assert library.exists("nonexistent") is False
+
+    def test_load_existing(self, sample_games_dir):
+        library = GameLibrary(str(sample_games_dir))
+        game = library.load("umu-1234")
+        assert game is not None
+        assert game.gameid == "umu-1234"
+        assert game.title == "Test Game Alpha"
+        assert game.playtime == 3600
+        assert game.hidden is False
+        assert game.favorite is True
+
+    def test_load_nonexistent(self, sample_games_dir):
+        library = GameLibrary(str(sample_games_dir))
+        assert library.load("nonexistent") is None
+
+    def test_load_all(self, sample_games_dir):
+        library = GameLibrary(str(sample_games_dir))
+        games = library.load_all()
+        assert len(games) == 2
+        ids = {g.gameid for g in games}
+        assert ids == {"umu-1234", "umu-5678"}
+
+    def test_load_all_empty_dir(self, tmp_path):
+        library = GameLibrary(str(tmp_path))
+        assert library.load_all() == []
+
+    def test_save_roundtrip(self, tmp_path):
+        library = GameLibrary(str(tmp_path))
+        game = Game(
+            gameid="umu-99",
+            title="Saved Game",
+            path="/some/path",
+            prefix="/some/prefix",
+            game_arguments="--verbose",
+            runner="GE-Proton10",
+            playtime=500,
+            hidden=True,
+            favorite=False,
+        )
+        library.save(game)
+        loaded = library.load("umu-99")
+        assert loaded is not None
+        assert loaded.gameid == game.gameid
+        assert loaded.title == game.title
+        assert loaded.playtime == game.playtime
+        assert loaded.hidden == game.hidden
+        assert loaded.favorite == game.favorite
+
+    def test_delete_existing(self, sample_games_dir):
+        library = GameLibrary(str(sample_games_dir))
+        assert library.delete("umu-1234") is True
+        assert library.exists("umu-1234") is False
+        # Other game still exists
+        assert library.exists("umu-5678") is True
+
+    def test_delete_nonexistent(self, sample_games_dir):
+        library = GameLibrary(str(sample_games_dir))
+        assert library.delete("nonexistent") is False
+
+    def test_update_field_existing(self, sample_games_dir):
+        library = GameLibrary(str(sample_games_dir))
+        assert library.update_field("umu-1234", "favorite", False) is True
+        game = library.load("umu-1234")
+        assert game.favorite is False
+
+    def test_update_field_nonexistent(self, sample_games_dir):
+        library = GameLibrary(str(sample_games_dir))
+        assert library.update_field("nonexistent", "title", "New") is False
+
+    def test_add_new(self, tmp_path):
+        library = GameLibrary(str(tmp_path))
+        game = Game(gameid="umu-new", title="New Game", path="/new")
+        assert library.add(game) is True
+        assert library.exists("umu-new") is True
+
+    def test_add_duplicate(self, sample_games_dir):
+        library = GameLibrary(str(sample_games_dir))
+        game = Game(gameid="umu-1234", title="Duplicate", path="/dup")
+        assert library.add(game) is False
+        # Original still intact
+        original = library.load("umu-1234")
+        assert original.title == "Test Game Alpha"
+
+    def test_save_all(self, tmp_path):
+        library = GameLibrary(str(tmp_path))
+        games = [
+            Game(gameid="a", title="A", path="/a"),
+            Game(gameid="b", title="B", path="/b"),
+        ]
+        library.save_all(games)
+        assert library.exists("a")
+        assert library.exists("b")
+        assert library.load("a").title == "A"
+        assert library.load("b").title == "B"
+
+    def test_atomic_write_crash_safety(self, tmp_path):
+        """Verify writes use temp files (no partial .ini left on crash)."""
+        library = GameLibrary(str(tmp_path))
+        game = Game(gameid="umu-atomic", title="Atomic", path="/atomic")
+        library.save(game)
+        # No .tmp files left behind
+        tmp_files = [f for f in os.listdir(tmp_path) if f.endswith(".ini.tmp")]
+        assert tmp_files == []
+
+
+class TestDuplicateDetection:
+    def test_load_all_skips_duplicates(self, tmp_path):
+        """If two files have the same gameid, first wins and a warning is logged."""
+        library = GameLibrary(str(tmp_path))
+        # Write first file normally
+        game1 = Game(gameid="umu-dup", title="First", path="/first")
+        library.save(game1)
+        # Manually write a second file with a different name but same internal gameid
+        (tmp_path / "other-name.ini").write_text(
+            "[game]\n"
+            "gameid = umu-dup\n"
+            "title = Second\n"
+            "path = /second\n"
+            "hidden = false\n"
+            "favorite = false\n"
+        )
+        # load_all returns only one (first alphabetically by filename)
+        games = library.load_all()
+        assert len(games) == 1
+        # other-name.ini sorts before umu-dup.ini
+        assert games[0].title == "Second"
+
+
+# ── Migration ───────────────────────────────────────────────────────────
+
+
+class TestMigrationResult:
+    def test_defaults(self):
+        result = MigrationResult()
+        assert result.migrated == 0
+        assert result.skipped_duplicate == 0
+        assert result.skipped_error == 0
+        assert result.errors == []
+
+
+class TestNeedsMigration:
+    def test_needs_migration_true(self, tmp_path):
+        games_json = tmp_path / "games.json"
+        games_json.write_text("[]")
+        games_dir = tmp_path / "games"
+        assert needs_migration(str(games_json), str(games_dir)) is True
+
+    def test_needs_migration_false_dir_exists(self, tmp_path):
+        games_json = tmp_path / "games.json"
+        games_json.write_text("[]")
+        games_dir = tmp_path / "games"
+        games_dir.mkdir()
+        assert needs_migration(str(games_json), str(games_dir)) is False
+
+    def test_needs_migration_false_no_json(self, tmp_path):
+        games_json = tmp_path / "games.json"
+        games_dir = tmp_path / "games"
+        assert needs_migration(str(games_json), str(games_dir)) is False
+
+
+class TestMigrateToPerGame:
+    def test_migrate_basic(self, tmp_path, sample_games):
+        games_json = tmp_path / "games.json"
+        games_dir = tmp_path / "games"
+        # Write monolithic JSON
+        save_games_json(str(games_json), sample_games, backup=False)
+        assert games_json.exists()
+
+        result = migrate_to_per_game(str(games_json), str(games_dir))
+
+        assert result.migrated == 2
+        assert result.skipped_duplicate == 0
+        assert result.skipped_error == 0
+        # Old file renamed
+        assert (tmp_path / "games.json.migrated").exists()
+        # New files created
+        library = GameLibrary(str(games_dir))
+        assert library.exists("umu-1234")
+        assert library.exists("umu-5678")
+        assert library.load("umu-1234").title == "Test Game Alpha"
+
+    def test_migrate_empty_json(self, tmp_path):
+        games_json = tmp_path / "games.json"
+        games_json.write_text("[]")
+        games_dir = tmp_path / "games"
+
+        result = migrate_to_per_game(str(games_json), str(games_dir))
+
+        assert result.migrated == 0
+        # Empty JSON — nothing to migrate, file is still renamed
+        assert (tmp_path / "games.json.migrated").exists()
+
+    def test_migrate_no_json(self, tmp_path):
+        games_dir = tmp_path / "games"
+        result = migrate_to_per_game(str(tmp_path / "nope.json"), str(games_dir))
+        # load_games_json returns [] for missing files — no error, nothing to migrate
+        assert result.migrated == 0
+        assert result.errors == []
+        # No source file to rename
+        assert not (tmp_path / "nope.json.migrated").exists()
+
+    def test_migrate_duplicates(self, tmp_path):
+        games_json = tmp_path / "games.json"
+        games_dir = tmp_path / "games"
+        # Two games with same gameid
+        save_games_json(
+            str(games_json),
+            [
+                Game(gameid="dup", title="First", path="/first"),
+                Game(gameid="dup", title="Second", path="/second"),
+            ],
+            backup=False,
+        )
+
+        result = migrate_to_per_game(str(games_json), str(games_dir))
+
+        assert result.migrated == 1
+        assert result.skipped_duplicate == 1
+        library = GameLibrary(str(games_dir))
+        game = library.load("dup")
+        assert game.title == "First"  # first occurrence wins
+
+    def test_migrate_dry_run(self, tmp_path, sample_games):
+        games_json = tmp_path / "games.json"
+        games_dir = tmp_path / "games"
+        save_games_json(str(games_json), sample_games, backup=False)
+
+        result = migrate_to_per_game(
+            str(games_json), str(games_dir), dry_run=True
+        )
+
+        assert result.migrated == 2
+        # Old file NOT renamed in dry run
+        assert games_json.exists()
+        assert not (tmp_path / "games.json.migrated").exists()