steam-backlog-enforcer/_installer_import/tests/test_appinfo.py
Krzysztof kuhy Rudnicki aaf9c18a8a fix(recognize): stop Steam re-downloading after a hand-finished install
The KCD2 run reported success but Steam re-downloaded 90 GB anyway. Two
independent causes, plus a defect that hid both.

force_install_dir sets a Steam *library root*, not an install directory.
Pointing it at the game folder made steamcmd search <game>/steamapps/ for
manifests ("Loaded 0 apps from install folder"), create that stray dir, and
fail the app with "Invalid platform" for lack of depot context. Dropped it;
the app already lives in a registered library. Separately, a Linux steamcmd
needs +@sSteamCmdForcePlatformType windows for a Windows-only title.

Steam then discarded the manifest because it was self-contradictory:
StateFlags=4 while BytesDownloaded (22 GB) != BytesToDownload (92 GB), with
tens of GB still staged under steamapps/downloading/. Across every genuinely
installed manifest the byte counters are equal in pairs, StagingSize is 0 and
staging is empty; DownloadType is kept, not dropped. Settle all of it, and
refuse to call an install real until it holds.

The defect that hid this: steamcmd exited 8 having done nothing and the tool
still wrote StateFlags=4, printed a success line and returned 0. Now gated on
depotdownloader succeeding, every required depot being cached, real game
content on disk, a matching buildid and no running Steam client -- and it
re-reads the manifest after writing.

Also fixes data corruption found in review: vdf.dumps never re-escaped
backslashes, so each parse/dump cycle halved them and silently mangled the
Windows paths in appmanifest_228980.acf's InstallScripts. Manifest writes are
now atomic with a preserved pristine .acf.bak.

Adds per-run logging to ~/.local/state/steam-game-installer (argv, exit codes,
full subprocess output, manifest before/after), teed through a pty so
steamcmd's password prompt still works.

Verified end-to-end on KCD2: Steam logged "Fully Installed, App Running" and
the game launched with no download queued. Coverage 100% statements+branches,
enforced by fail_under.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EK4urCmbTqN4Vrc3nGBcXF
2026-08-05 10:11:34 +02:00

237 lines
7.8 KiB
Python

import pytest
from steam_installer import appinfo
def _app_block(monkeypatch, block):
monkeypatch.setattr(appinfo, "_fetch_app_block", lambda appid: block)
# Shape taken from a real `app_info_print 1771300`: 12 depots, of which an
# english/windows install only ever fetches three.
KCD2_DEPOTS = {
"228989": {"depotfromapp": "228980", "config": {"oslist": "windows"}},
"1771302": {"config": {"oslist": "windows"}},
"1771303": {"config": {"oslist": "windows"}},
"1771304": {"dlcappid": "3406020", "config": {"oslist": "windows"}},
"1771305": {"config": {"oslist": "windows", "language": "czech"}},
"1771306": {"config": {"oslist": "windows", "language": "english"}},
"1771307": {"config": {"oslist": "windows", "language": "french"}},
"4075290": {"dlcappid": "4075290"},
"branches": {"public": {"buildid": "23914554"}},
}
def test_get_depot_plan_selects_only_this_language_and_os(monkeypatch):
_app_block(monkeypatch, {"depots": KCD2_DEPOTS})
plan = appinfo.get_depot_plan(1771300, language="english", os_name="windows")
assert plan.required == {"1771302", "1771303", "1771306"}
assert plan.shared == {"228989"}
assert plan.all_expected == {"1771302", "1771303", "1771306", "228989"}
def test_get_depot_plan_switches_language(monkeypatch):
_app_block(monkeypatch, {"depots": KCD2_DEPOTS})
plan = appinfo.get_depot_plan(1771300, language="czech")
assert "1771305" in plan.required
assert "1771306" not in plan.required
def test_get_depot_plan_excludes_other_os(monkeypatch):
_app_block(
monkeypatch,
{"depots": {"10": {"config": {"oslist": "linux"}}, "11": {"config": {}}}},
)
plan = appinfo.get_depot_plan(1771300, os_name="windows")
assert plan.required == {"11"}
def test_get_depot_plan_skips_optional_depots(monkeypatch):
_app_block(monkeypatch, {"depots": {"10": {"optional": "1"}, "11": {}}})
assert appinfo.get_depot_plan(1771300).required == {"11"}
def test_get_depot_plan_rejects_malformed_depots_section(monkeypatch):
_app_block(monkeypatch, {"depots": "not-a-dict"})
with pytest.raises(appinfo.AppInfoError, match="depots"):
appinfo.get_depot_plan(1771300)
def test_get_depot_plan_with_no_depots_is_empty(monkeypatch):
"""An app with no depot list yields nothing required -- callers must not
read that as "everything downloaded fine"."""
_app_block(monkeypatch, {})
plan = appinfo.get_depot_plan(1771300)
assert plan.required == set()
assert plan.shared == set()
def test_get_build_id_reads_public_branch(monkeypatch):
_app_block(monkeypatch, {"depots": KCD2_DEPOTS})
assert appinfo.get_build_id(1771300) == "23914554"
def test_get_build_id_missing_branch_returns_none(monkeypatch):
_app_block(monkeypatch, {"depots": {"branches": {}}})
assert appinfo.get_build_id(1771300) is None
def test_get_build_id_without_depots_returns_none(monkeypatch):
_app_block(monkeypatch, {})
assert appinfo.get_build_id(1771300) is None
def test_get_metadata_reads_name_and_installdir(monkeypatch):
_app_block(
monkeypatch,
{
"common": {
"name": "Kingdom Come: Deliverance II",
"installdir": "KingdomComeDeliverance2",
}
},
)
metadata = appinfo.get_metadata(1771300)
assert metadata.name == "Kingdom Come: Deliverance II"
assert metadata.installdir == "KingdomComeDeliverance2"
def test_get_metadata_falls_back_to_config_installdir(monkeypatch):
_app_block(
monkeypatch,
{"common": {"name": "Game"}, "config": {"installdir": "GameDir"}},
)
assert appinfo.get_metadata(1).installdir == "GameDir"
def test_get_metadata_requires_name(monkeypatch):
_app_block(monkeypatch, {"common": {"installdir": "GameDir"}})
with pytest.raises(appinfo.AppInfoError, match="no name field"):
appinfo.get_metadata(1)
def test_get_metadata_requires_installdir(monkeypatch):
_app_block(monkeypatch, {"common": {"name": "Game"}})
with pytest.raises(appinfo.AppInfoError, match="no installdir"):
appinfo.get_metadata(1)
def test_get_metadata_rejects_malformed_common_section(monkeypatch):
_app_block(monkeypatch, {"common": "not-a-dict"})
with pytest.raises(appinfo.AppInfoError, match="no 'common' section"):
appinfo.get_metadata(1)
class _FakeCompleted:
def __init__(self, stdout):
self.stdout = stdout
self.returncode = 0
# What steamcmd actually emits: its own log lines, then the VDF block.
STEAMCMD_OUTPUT = """Redirecting stderr to '/home/u/.steam/logs/stderr.txt'
Logging directory: '/home/u/.steam/logs'
"@sSteamCmdForcePlatformType" = "windows"
Logging in user 'anonymous' to Steam Public...OK
"1771300"
{
\t"common"
\t{
\t\t"name"\t\t"Kingdom Come: Deliverance II"
\t\t"installdir"\t\t"KingdomComeDeliverance2"
\t}
\t"depots"
\t{
\t\t"1771303"
\t\t{
\t\t\t"config"
\t\t\t{
\t\t\t\t"oslist"\t\t"windows"
\t\t\t}
\t\t}
\t\t"branches"
\t\t{
\t\t\t"public"
\t\t\t{
\t\t\t\t"buildid"\t\t"23914554"
\t\t\t}
\t\t}
\t}
}
"""
def test_fetch_app_block_parses_real_steamcmd_output(monkeypatch):
seen = {}
def fake_run(cmd, **kwargs):
seen["cmd"] = cmd
return _FakeCompleted(STEAMCMD_OUTPUT)
monkeypatch.setattr(appinfo.subprocess, "run", fake_run)
metadata = appinfo.get_metadata(1771300)
assert metadata.name == "Kingdom Come: Deliverance II"
assert metadata.installdir == "KingdomComeDeliverance2"
# The platform flag must precede +login or a Windows-only app comes back empty.
cmd = seen["cmd"]
assert cmd.index("+@sSteamCmdForcePlatformType") < cmd.index("+login")
assert cmd[cmd.index("+@sSteamCmdForcePlatformType") + 1] == "windows"
def test_get_build_id_through_the_real_parser(monkeypatch):
monkeypatch.setattr(
appinfo.subprocess, "run", lambda cmd, **kw: _FakeCompleted(STEAMCMD_OUTPUT)
)
assert appinfo.get_build_id(1771300) == "23914554"
def test_fetch_app_block_raises_when_appid_header_absent(monkeypatch):
"""steamcmd prints its usual log noise even for an unknown app; without the
`"<appid>"` header there is no app info to parse."""
monkeypatch.setattr(
appinfo.subprocess,
"run",
lambda cmd, **kw: _FakeCompleted("Logging in user 'anonymous'...OK\n"),
)
with pytest.raises(appinfo.AppInfoError, match="did not return app info"):
appinfo.get_metadata(999999999)
def test_fetch_app_block_raises_when_block_is_not_a_mapping(monkeypatch):
"""Defensive guard against a shape vdf.parse cannot currently produce (it
always yields a dict for a `{ ... }` block). Exercised by injecting a parser
that returns a scalar, so the guard stays correct if vdf ever changes."""
monkeypatch.setattr(
appinfo.subprocess,
"run",
lambda cmd, **kw: _FakeCompleted('"1771300"\n{\n}\n'),
)
monkeypatch.setattr(appinfo.vdf, "parse", lambda text: {"1771300": "scalar"})
with pytest.raises(appinfo.AppInfoError, match="unexpected app_info_print"):
appinfo.get_metadata(1771300)
def test_get_build_id_handles_non_dict_sections(monkeypatch):
for text in (
'"1"\n{\n\t"depots"\t\t"scalar"\n}\n',
'"1"\n{\n\t"depots"\n\t{\n\t\t"branches"\t\t"scalar"\n\t}\n}\n',
'"1"\n{\n\t"depots"\n\t{\n\t\t"branches"\n\t\t{\n\t\t\t"public"\t\t"x"\n\t\t}\n\t}\n}\n',
):
monkeypatch.setattr(
appinfo.subprocess, "run", lambda cmd, _t=text, **kw: _FakeCompleted(_t)
)
assert appinfo.get_build_id(1) is None
def test_get_build_id_returns_none_when_buildid_not_a_string(monkeypatch):
text = '"1"\n{\n\t"depots"\n\t{\n\t\t"branches"\n\t\t{\n\t\t\t"public"\n\t\t\t{\n\t\t\t\t"buildid"\n\t\t\t\t{\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n'
monkeypatch.setattr(
appinfo.subprocess, "run", lambda cmd, **kw: _FakeCompleted(text)
)
assert appinfo.get_build_id(1) is None