steam-backlog-enforcer/_installer_import/tests/test_resolve.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

100 lines
2.8 KiB
Python

import io
import json
import urllib.error
import pytest
from steam_installer import resolve
class _FakeResponse(io.BytesIO):
def __enter__(self):
return self
def __exit__(self, *exc):
return False
def test_search_parses_items(monkeypatch):
payload = {"items": [{"id": 1771300, "name": "Kingdom Come: Deliverance II"}]}
monkeypatch.setattr(
resolve.urllib.request,
"urlopen",
lambda url, timeout: _FakeResponse(json.dumps(payload).encode()),
)
results = resolve.search("kingdom come")
assert results == [
resolve.SearchResult(appid=1771300, name="Kingdom Come: Deliverance II")
]
def test_search_quotes_the_term(monkeypatch):
seen = {}
def fake_urlopen(url, timeout):
seen["url"] = url
return _FakeResponse(b'{"items": []}')
monkeypatch.setattr(resolve.urllib.request, "urlopen", fake_urlopen)
resolve.search("kingdom come/2")
assert "kingdom%20come/2" in seen["url"] or "kingdom%20come%2F2" in seen["url"]
def test_search_missing_items_key_returns_empty(monkeypatch):
monkeypatch.setattr(
resolve.urllib.request, "urlopen", lambda url, timeout: _FakeResponse(b"{}")
)
assert resolve.search("nothing") == []
@pytest.mark.parametrize(
"exc", [urllib.error.URLError("tls blocked"), TimeoutError("timed out")]
)
def test_search_network_failure_becomes_search_unavailable(monkeypatch, exc):
"""store.steampowered.com is known to reject scripted clients on some
networks -- that is not a code bug, so it gets its own exception type."""
def boom(url, timeout):
raise exc
monkeypatch.setattr(resolve.urllib.request, "urlopen", boom)
with pytest.raises(resolve.SearchUnavailable):
resolve.search("kingdom come")
def _results():
return [
resolve.SearchResult(appid=1, name="One"),
resolve.SearchResult(appid=2, name="Two"),
]
def test_prompt_confirm_returns_the_chosen_result(monkeypatch):
monkeypatch.setattr("builtins.input", lambda *a: "2")
assert resolve.prompt_confirm(_results()).appid == 2
def test_prompt_confirm_reprompts_until_valid(monkeypatch, capsys):
answers = iter(["", "banana", "9", "1"])
monkeypatch.setattr("builtins.input", lambda *a: next(answers))
assert resolve.prompt_confirm(_results()).appid == 1
assert capsys.readouterr().out.count("Invalid choice") == 3
def test_prompt_confirm_zero_cancels(monkeypatch):
monkeypatch.setattr("builtins.input", lambda *a: "0")
with pytest.raises(resolve.ResolveError, match="Cancelled"):
resolve.prompt_confirm(_results())
def test_prompt_confirm_rejects_empty_result_list():
"""Never guess an app: no matches must be an error, not a default pick."""
with pytest.raises(resolve.ResolveError, match="No matches"):
resolve.prompt_confirm([])