steam-backlog-enforcer/_installer_import/tests/test_ghost.py
Krzysztof kuhy Rudnicki 15f647f639 Install any game from an appid, a URL or a name; adopt Steam's partial downloads
`./run.sh <anything>` is now the entry point. It installs its own dependencies
(steamcmd via yay, DepotDownloader from the SteamRE release, a local .venv) and
works out what the argument is: an appid, a Steam URL, an exact title, or part
of a title. Every route is confirmed through steamcmd's own app_info before
anything acts on it.

Name search moves to steamcommunity.com/actions/SearchApps, which works, with
the storefront API kept as a fallback -- it TLS-rejects scripted clients here,
so search had been effectively dead. DNS on this box fails transiently about one
lookup in three, so each backend is retried before giving up. An exact title now
wins over Steam's own ranking, which puts "Portal 2" above "Portal"; an
ambiguous name shows a picker, or exits 2 rather than guessing.

Two problems found while building this, both now handled:

Anonymous steamcmd has no licence for a paid app and does not say so. It
transfers nothing, prints "Success! App '284160' fully installed.", exits 0, and
writes StateFlags=4 with SizeOnDisk=0 and no depots. A running Steam client then
reads that, decides the app is installed but stale, and downloads the whole game
by itself. `ghost.py` detects that state -- validated read-only against all ten
manifests in a real library, where the nine genuine installs are untouched and
only the fabricated one matches -- and `--repair-ghosts` clears it away. Repair
uses rmdir, never rmtree, so a non-empty directory survives a wrong diagnosis.

And Steam's own in-progress downloads are no longer wasted. `takeover.py`
hardlinks `steamapps/downloading/<appid>/` into the install directory before
depotdownloader runs; depotdownloader hash-checks what is already on disk
(ContentDownloader.cs skips a file when neededChunks is empty) and fetches only
the remainder. Nothing is deleted up front -- the staging tree is cleared only
after the install is verified -- so a failed run still leaves Steam able to
resume. The trees share inodes, so an in-place chunk patch corrects both.

cli.py drops from 311 to 244 lines, back under the repo's 250-line cap, by
moving the install flow to flow.py and the manifest finish to finalize.py.

install.sh had a latent bug: `pip install -e .[dev]` only warns about a missing
extra and exits 0, so the `|| fallback` that installed pytest and coverage never
ran. A fresh venv had no test tools at all; the existing one only worked because
they had been installed by hand earlier. Now named outright and asserted.

246 tests, 100% statement and branch coverage. Verified live: all four input
forms resolve to 284160, an exact "Portal" resolves to 400 and not 620, an
ambiguous name exits 2, ghost detection flags nothing on nine real installs, and
a from-scratch venv bootstraps and runs.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sgkb3wH6MU9fYquX328ovT
2026-09-11 21:38:16 +02:00

160 lines
5.4 KiB
Python

"""The fake-install detector.
The anchor test uses the real manifest `steamcmd +login anonymous +app_update
284160` wrote on 2026-09-11 -- a paid app it had no licence for, reported as
"fully installed" after transferring nothing. The negatives are built from the
field signatures of the nine genuinely installed apps in the same library,
because the only failure mode that matters here is deleting a real install.
Shared fixtures (`library`, `GHOST_ACF`, `_write`, `_real_install`) live in
conftest.py; the repair side lives in test_ghost_repair.py.
"""
import pytest
from steam_installer import ghost, vdf
from steam_installer.library import SteamLibrary
from tests.conftest import GHOST_ACF, _real_install, _write
def test_the_real_anonymous_steamcmd_manifest_is_detected(library):
_write(library, 284160, GHOST_ACF)
(library.common / "BeamNG.drive").mkdir()
report = ghost.inspect(library, 284160)
assert report.is_ghost
assert not report.is_warm
assert "InstalledDepots is empty" in report.reasons
assert "no game content on disk" in report.reasons
def test_a_missing_content_directory_is_still_a_ghost(library):
"""The directory need not exist at all -- only the lie has to."""
_write(library, 284160, GHOST_ACF)
assert ghost.inspect(library, 284160).is_ghost
def test_a_real_install_is_never_a_ghost(library):
_real_install(library)
report = ghost.inspect(library, 475150)
assert not report.is_ghost
assert report.content_bytes == 4096
def test_content_on_disk_alone_disqualifies_a_ghost(library):
"""The interlock that matters: even a manifest that looks exactly like the
fake one is not a ghost if there are real bytes behind it."""
_write(library, 284160, GHOST_ACF)
content = library.common / "BeamNG.drive"
content.mkdir()
(content / "game.pak").write_bytes(b"x" * 1024)
assert not ghost.inspect(library, 284160).is_ghost
def test_a_nonzero_size_on_disk_disqualifies_a_ghost(library):
_write(
library, 284160, GHOST_ACF.replace('"SizeOnDisk"\t\t"0"', '"SizeOnDisk"\t\t"9"')
)
assert not ghost.inspect(library, 284160).is_ghost
def test_listed_depots_disqualify_a_ghost(library):
_write(
library,
284160,
GHOST_ACF.replace(
'\t"InstalledDepots"\n\t{\n\t}',
'\t"InstalledDepots"\n\t{\n\t\t"284161"\n\t\t{\n\t\t\t"manifest"\t\t"1"\n\t\t}\n\t}',
),
)
assert not ghost.inspect(library, 284160).is_ghost
@pytest.mark.parametrize("flags", ["1026", "2", "0", "notanumber", ""])
def test_a_manifest_not_claiming_installed_is_not_a_ghost(library, flags):
"""Without bit 4 the manifest is not claiming anything is installed, so
there is no lie to clean up -- 1026 is a normal mid-download state."""
_write(
library,
284160,
GHOST_ACF.replace('"StateFlags"\t\t"4"', f'"StateFlags"\t\t"{flags}"'),
)
assert not ghost.inspect(library, 284160).is_ghost
def test_state_flags_six_still_counts_as_claiming_installed(library):
"""6 is 4|2 -- installed, update required. Every genuinely installed app in
a real library carries it, so the bit test must not be an equality test."""
_write(
library, 284160, GHOST_ACF.replace('"StateFlags"\t\t"4"', '"StateFlags"\t\t"6"')
)
assert ghost.inspect(library, 284160).is_ghost
def test_a_missing_manifest_is_not_a_ghost(library):
assert not ghost.inspect(library, 284160).is_ghost
def test_a_manifest_for_a_different_appid_is_ignored(library):
_write(
library, 284160, GHOST_ACF.replace('"appid"\t\t"284160"', '"appid"\t\t"999"')
)
assert not ghost.inspect(library, 284160).is_ghost
def test_a_manifest_without_an_installdir_is_not_a_ghost(library):
_write(library, 284160, GHOST_ACF.replace('\t"installdir"\t\t"BeamNG.drive"\n', ""))
report = ghost.inspect(library, 284160)
assert report.install_dir is None
assert report.is_ghost
def test_our_own_downloader_state_disqualifies_a_ghost(library):
"""An empty install dir mid-run is not a ghost; .DepotDownloader proves a
real download is in progress against it."""
_write(library, 284160, GHOST_ACF)
cache = library.common / "BeamNG.drive" / ".DepotDownloader"
cache.mkdir(parents=True)
(cache / "284161_999.manifest").write_bytes(b"x")
assert not ghost.inspect(library, 284160).is_ghost
def test_staged_content_makes_a_ghost_warm(library):
_write(library, 284160, GHOST_ACF)
staged = library.steamapps / "downloading" / "284160"
staged.mkdir(parents=True)
(staged / "part.pak").write_bytes(b"x" * 2048)
report = ghost.inspect(library, 284160)
assert report.is_ghost
assert report.is_warm
assert report.staging_bytes == 2048
def test_scan_finds_only_the_ghost(library):
_write(library, 284160, GHOST_ACF)
_real_install(library)
(library.steamapps / "appmanifest_notanumber.acf").write_text(GHOST_ACF)
assert [r.appid for r in ghost.scan([library])] == [284160]
def test_scan_skips_a_library_with_no_steamapps(tmp_path):
assert ghost.scan([SteamLibrary(tmp_path / "nope")]) == []
def test_an_unparseable_manifest_is_not_a_ghost(library, monkeypatch):
"""A manifest we cannot read is not a manifest we may act on."""
_write(library, 284160, "this is not vdf {{{")
def boom(path):
raise vdf.VdfParseError("unbalanced braces", 0)
monkeypatch.setattr(ghost.recognize, "read_app_state", boom)
assert not ghost.inspect(library, 284160).is_ghost