steam-backlog-enforcer/steam_installer/tests/test_map_installed.py
Krzysztof kuhy Rudnicki a76e726e36 Merge steam-game-installer into the repo
The installer ships as the steam_installer package beside
steam_backlog_enforcer, with its tests, docs and Proton lock. The
enforcer's fast install now starts the installer from this checkout.

Both appinfo.vdf readers share one key-value decoder
(steam_installer.appinfo_kv), and the duplicated test fixtures and
helpers are folded into conftest and _cli_helpers so the whole-tree
duplicate-code check is clean. check_requirements_sync treats sibling
packages as first-party.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWZCxnNWLtpVwFLVk9w
2026-11-22 20:25:39 +01:00

190 lines
6.3 KiB
Python

"""Mapping games to the native Proton: after an install, and for the whole library."""
from __future__ import annotations
from typing import TYPE_CHECKING, Never
import pytest
from steam_installer import (
anticheat,
cli,
compat_apply,
compat_map,
recognize,
steamctl,
vdf,
)
from steam_installer.tests._acf_helpers import stamp_native_tool
from steam_installer.tests._appinfo_blob import encode
from steam_installer.tests._map_data import (
CONFIG,
CS,
LOCKED,
NATIVE,
PLAIN,
SHARED,
TOOL,
_mapped,
)
if TYPE_CHECKING:
from pathlib import Path
pytestmark = pytest.mark.usefixtures("cli_logs")
@pytest.fixture(name="home")
def _home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
"""A Steam root: config.vdf, the stamped tool, an app cache, anti-cheat list."""
root = tmp_path / "Steam"
(root / "config").mkdir(parents=True)
(root / "config" / "config.vdf").write_text(CONFIG)
stamp_native_tool(root, LOCKED)
cache = tmp_path / "appinfo.vdf"
cache.write_bytes(
encode({10: PLAIN, 20: NATIVE, 30: SHARED, 730: CS, 1493710: TOOL})
)
monkeypatch.setattr(
compat_apply.appinfo.appinfo_cache, "default_path", lambda: cache
)
monkeypatch.setattr(
anticheat, "_download", lambda: b'[{"storeIds": {"steam": "730"}}]'
)
steamapps = root / "steamapps"
steamapps.mkdir()
for appid in (10, 20, 1493710):
(steamapps / f"appmanifest_{appid}.acf").write_text(
vdf.dumps("AppState", {"appid": str(appid), "InstalledDepots": {}})
)
monkeypatch.setattr(cli, "find_steam_root", lambda: root)
return root
def test_dry_run_lists_decisions_and_writes_nothing(
home: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""Dry run lists decisions and writes nothing."""
before = (home / "config" / "config.vdf").read_bytes()
assert cli.main(["--map-installed"]) == 0
out = capsys.readouterr().out
assert "10 Plain: map" in out
assert "20 Native: Valve default: native" in out
assert "1493710" not in out # a tool, not a game
assert "Dry run" in out
assert (home / "config" / "config.vdf").read_bytes() == before
@pytest.mark.usefixtures("home")
def test_appids_decides_any_cached_app(capsys: pytest.CaptureFixture[str]) -> None:
"""Appids decides any cached app."""
assert cli.main(["--map-installed", "--appids", "730,20,30,10,999"]) == 0
out = capsys.readouterr().out
assert "730 CS: Valve default: anti-cheat, native" in out
assert "30 Shared: Valve default: shared depots" in out
assert "10 Plain: map" in out
assert "999: not in Steam's app cache" in out
def test_apply_maps_the_safe_ones_and_a_rerun_changes_nothing(
home: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""Apply maps the safe ones and a rerun changes nothing."""
assert cli.main(["--map-installed", "--apply", "--yes"]) == 0
assert _mapped(home, 10) == ("proton-cachyos-native", "250")
assert _mapped(home, 20) is None
once = (home / "config" / "config.vdf").read_bytes()
capsys.readouterr()
assert cli.main(["--map-installed", "--apply", "--yes"]) == 0
assert "10 Plain: map -- already mapped" in capsys.readouterr().out
assert (home / "config" / "config.vdf").read_bytes() == once
def test_apply_closes_steam_first_and_restarts_it(
home: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Apply closes steam first and restarts it."""
events = []
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [] if events else [5])
def stop(_logger: object) -> bool:
events.append("stop")
return True
monkeypatch.setattr(steamctl, "stop", stop)
monkeypatch.setattr(steamctl, "start", lambda logger: events.append("start"))
assert cli.main(["--map-installed", "--apply", "--yes"]) == 0
assert events == ["stop", "start"]
assert _mapped(home, 10) is not None
def test_apply_declined_writes_nothing(
home: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Apply declined writes nothing."""
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [5])
monkeypatch.setattr("builtins.input", lambda *a: "n")
assert cli.main(["--map-installed", "--apply"]) == 1
assert _mapped(home, 10) is None
def test_a_kept_choice_and_a_write_error_are_reported(
home: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A kept choice and a write error are reported."""
config = home / "config" / "config.vdf"
compat_map.map_app(config, 10)
config.write_text(
config.read_text().replace("proton-cachyos-native", "GE-Proton10")
)
assert cli.main(["--map-installed", "--apply", "--yes"]) == 0
assert "kept: already mapped to GE-Proton10" in capsys.readouterr().out
config.write_text("garbage {")
assert cli.main(["--map-installed", "--apply", "--yes"]) == 1
def test_owned_dlc_in_the_manifest_keeps_valves_default(
home: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""Owned dlc in the manifest keeps valves default."""
manifest = home / "steamapps" / "appmanifest_10.acf"
manifest.write_text(
vdf.dumps(
"AppState",
{
"appid": "10",
"InstalledDepots": {
"11": {"manifest": "1"},
"12": {"manifest": "2", "dlcappid": "13"},
},
},
)
)
assert cli.main(["--map-installed"]) == 0
assert "10 Plain: Valve default: owned DLC" in capsys.readouterr().out
@pytest.mark.usefixtures("home")
def test_valve_proton_only_goes_with_an_install() -> None:
"""Valve proton only goes with an install."""
with pytest.raises(SystemExit):
cli.main(["--map-installed", "--valve-proton"])
def test_a_failed_write_stops_the_run(
home: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
"""A failed write stops the run."""
def boom(_config: Path, _appid: int) -> Never:
msg = "config.vdf line 3: unexpected layout"
raise compat_map.CompatMapError(msg)
monkeypatch.setattr(compat_map, "map_app", boom)
(home / "steamapps" / "appmanifest_notanid.acf").write_text("x")
assert cli.main(["--map-installed", "--apply", "--yes"]) == 1
out = capsys.readouterr().out
assert "Mapping stopped" in out
assert "config.vdf line 3" in out