diff --git a/DOCS-web-install.md b/DOCS-web-install.md index 2aa1adc..357b673 100644 --- a/DOCS-web-install.md +++ b/DOCS-web-install.md @@ -1,4 +1,4 @@ -# `install` and `repair-ghosts` jobs +# `install`, `repair-ghosts` and `map-installed` jobs Part of the web control API ([DOCS-web-control-api.md](DOCS-web-control-api.md)); the TS mirror of the result is `InstallResultData` in `web/src/api/contract.ts`. @@ -49,3 +49,58 @@ At `POST /api/jobs` the phrase is checked first (`400 wrong_phrase`), then the guard (`409 game_running`). Either refusal creates no job. The job re-checks both, because a game can start in between. The UI paints the `result.game-running` mark on a `game_running` refusal. + +## Proton map: `GET /api/compat` and `map-installed` + +`GET /api/compat` (`_compat_view.compat_view`) returns +`{tool, tool_ready, rows}`: one row per installed game, built by +`steam_installer.compat_rows.rows` (the same rows `--map-installed` logs, so +the view cannot disagree with the apply). A row is +`{app_id, name, current, priority, decided, reasons, note, map, changes, +cached}` (`CompatRow` in `web/src/api/contract.ts`); `null` in `current` or +`decided` means Valve's default. Steam's own tools are not rows. An unreadable +Steam, app cache or overrides file is `502 op_failed`. + +Deciding parses the ~10 MB app cache, so the payload is kept in memory and +rebuilt only when a file it was decided from changes (`cache_key`: app cache, +`config.vdf`, `libraryfolders.vdf`, every `appmanifest_*.acf`, the overrides, +the tool's `VERSION`, the anti-cheat cache and its 6 h age window). A GET +never waits on the network unless that anti-cheat cache is older. + +`./run.sh map-installed` and the `map-installed` job (Library page, view +`#view=compat`, icon `cmd.map-installed`) run the installer's +`--map-installed --apply --yes --appids ` (`main/map_installed.py`). The +apply closes Steam (Steam rewrites `config.vdf` on exit), maps each game the +decision proves safe to the native Proton and starts Steam again, so it refuses +while a game runs (`409 game_running`). The job streams the installer's +output as `log` lines and has a 180 s timer. + +Param `appids` (string, optional): comma-separated installed app ids. Absent, +the job maps every game whose row has `changes: true`. Ids that are not +installed, not digits, or an empty list are `400 invalid_params` +(`_compat_view.checked_appids`). The installer gets exactly the ids the phrase +counted, never "everything". The result `data` is `{app_ids: [...]}`. + +| Command | Phrase (exact) | Checked | +|---|---|---| +| `map-installed` | `map {appid_count} games`, e.g. `map 3 games` | at `POST /api/jobs` (`_web_jobs._PRECHECKED`), then again in the job | + +The count is part of the phrase, so a stale count is a wrong phrase. At +`POST /api/jobs` the ids are checked first (`400 invalid_params`, `502 +op_failed` when Steam is unreadable), then the phrase (`400 wrong_phrase`), +then the guard (`409 game_running`). Each refusal creates no job. The UI fills +`{appid_count}` from the same ids (`fillPhrase` in `web/src/commands/catalog.ts`). + +The view (`web/src/library/compatTable.ts`, state in `compatState.ts`) has +sortable columns, per-column filters, fuzzy search, a 30 s poll and row +checkboxes for games that change; with none ticked, Map applies to every +visible changing game. Its state lives in the URL hash, keys prefixed `c.` +because the cover grid owns `q` and `sort` in the same hash: + +| Key | Meaning | +|---|---| +| `view=compat` | show the Proton map | +| `c.sort=`, `c.dir=asc\|desc` | sort column (`name`, `app_id`, `current`, `decided`, `why`, `changes`) and direction; default `changes` ascending | +| `c.q=` | fuzzy search | +| `c.f.=` | filter: substring for `name` and `why`, exact option for `current`, `decided`, `changes` (`changes` or `same`) | +| `c.f.app_id=..` | app id range, either side optional | diff --git a/steam_backlog_enforcer/_command_catalog.py b/steam_backlog_enforcer/_command_catalog.py index 19389f4..4cb0fb7 100644 --- a/steam_backlog_enforcer/_command_catalog.py +++ b/steam_backlog_enforcer/_command_catalog.py @@ -73,6 +73,7 @@ CATALOG: Final[dict[str, _Entry]] = { "unhide": _job("library"), "add-exception": _job("library"), "repair-ghosts": _job("library"), + "map-installed": _job("library"), "gaming-status": _view("gaming", "/api/budget"), "gaming-reset": _job("gaming"), "gaming-unblock": _job("gaming"), diff --git a/steam_backlog_enforcer/_command_help.py b/steam_backlog_enforcer/_command_help.py index c206bc6..5b1c38b 100644 --- a/steam_backlog_enforcer/_command_help.py +++ b/steam_backlog_enforcer/_command_help.py @@ -32,6 +32,7 @@ COMMAND_DESCRIPTIONS: Final[dict[str, str]] = { "gaming-status": "Show today's gaming time and block state", "gaming-reset": "Reset today's gaming counter (root + phrase)", "repair-ghosts": "Delete the fake 'installed' manifests anonymous steamcmd left", + "map-installed": "Map every safe installed game to the native Proton", } # Commands with non-standard arg handling (shown in help but not in COMMANDS). diff --git a/steam_backlog_enforcer/_command_params.py b/steam_backlog_enforcer/_command_params.py index a9b68a1..fdec2cd 100644 --- a/steam_backlog_enforcer/_command_params.py +++ b/steam_backlog_enforcer/_command_params.py @@ -127,6 +127,16 @@ PARAMS: Final[dict[str, tuple[ParamSpec, ...]]] = { default=0, ), ), + # The rows the UI selected; absent = every installed game that would change. + "map-installed": ( + ParamSpec( + "appids", + "App ids", + "string", + required=False, + help="Comma-separated installed app ids.", + ), + ), # Not a CLI command: POST /api/backups/{id}/restore runs it as a job. "restore-backup": (ParamSpec("backup_id", "Backup id", "string"),), } diff --git a/steam_backlog_enforcer/_compat_view.py b/steam_backlog_enforcer/_compat_view.py new file mode 100644 index 0000000..d8eba36 --- /dev/null +++ b/steam_backlog_enforcer/_compat_view.py @@ -0,0 +1,141 @@ +"""``GET /api/compat``: every installed game's compat-tool (Proton) decision. + +The rows come from the merged installer's own decision code +(``steam_installer.compat_rows``, the same rows ``--map-installed`` logs), so +the view can never disagree with what the apply job does. Deciding parses +Steam's ~10 MB app cache and reads the anti-cheat list, so the answer is kept +in memory and rebuilt only when one of the files it was decided from changes +(apply rewrites config.vdf, an install adds a manifest). The anti-cheat list +is reused from its on-disk cache for :data:`ANTICHEAT_MAX_AGE`: a GET never +waits on the network unless that cache is older. + +The apply job (``map-installed``) checks its ids here too: :func:`checked_appids`. +""" + +from __future__ import annotations + +import threading +import time +from typing import TYPE_CHECKING, Final, cast + +from steam_backlog_enforcer._command_params import InvalidParamsError +from steam_backlog_enforcer._web_errors import ApiError +from steam_backlog_enforcer._web_io import Reply, ok +from steam_installer import anticheat, appinfo_cache, compat_decide, compat_map +from steam_installer.compat_apply import TOOL_DIR, Context +from steam_installer.compat_rows import rows +from steam_installer.errors import InstallerError +from steam_installer.library import find_steam_root, list_libraries + +if TYPE_CHECKING: + from collections.abc import Mapping + from pathlib import Path + + from steam_backlog_enforcer._web_io import Request + +ANTICHEAT_MAX_AGE: Final = 6 * 3600.0 + +_lock = threading.Lock() +_memo: dict[str, tuple[tuple[object, ...], dict[str, object]]] = {} + + +def clear_cache() -> None: + """Forget the memoised payload (tests).""" + with _lock: + _memo.clear() + + +def _stat(path: Path) -> tuple[str, int, int] | None: + try: + st = path.stat() + except OSError: + return None + return str(path), st.st_mtime_ns, st.st_size + + +def cache_key(root: Path) -> tuple[object, ...]: + """Stats of every file a decision reads, plus the anti-cheat age window.""" + paths = [ + appinfo_cache.default_path(), + compat_map.config_path(root), + root / "steamapps" / "libraryfolders.vdf", + anticheat.CACHE, + compat_decide.OVERRIDES, + root / TOOL_DIR / "VERSION", + ] + for library in list_libraries(root): + paths += sorted(library.steamapps.glob("appmanifest_*.acf")) + return (int(time.time() // ANTICHEAT_MAX_AGE), *(_stat(p) for p in paths)) + + +def compat_payload() -> dict[str, object]: + """``{tool, tool_ready, rows}``, from memory while no input file changed. + + Raises: + InstallerError: Steam's app cache or config.vdf cannot be read. + OSError: No Steam installation. + ValueError: The overrides file is malformed. + """ + root = find_steam_root() + key = cache_key(root) + with _lock: + hit = _memo.get("payload") + if hit is not None and hit[0] == key: + return hit[1] + context = Context(root, anticheat_max_age=ANTICHEAT_MAX_AGE) + payload: dict[str, object] = { + "tool": compat_map.TOOL, + "tool_ready": context.tool_ready, + "rows": [row.to_json() for row in rows(root, context=context)], + } + _memo["payload"] = (key, payload) + return payload + + +def compat_view(_request: Request) -> Reply: + """``GET /api/compat``. + + Raises: + ApiError: ``op_failed`` when the decision inputs cannot be read. + """ + try: + return ok(compat_payload()) + except (InstallerError, OSError, ValueError) as exc: + raise ApiError(str(exc), code="op_failed") from None + + +def _rows() -> list[dict[str, object]]: + return cast("list[dict[str, object]]", compat_payload()["rows"]) + + +def checked_appids(raw: str | None) -> list[int]: + """The ids an apply maps: *raw* checked, or every game that would change. + + Raises: + InvalidParamsError: *raw* is not comma-separated digits, names a game + that is not installed, or there is nothing to map. + """ + table = _rows() + if raw is None: + ids = [int(str(r["app_id"])) for r in table if r["changes"]] + else: + parts = [part.strip() for part in raw.split(",")] + if not all(part.isdigit() for part in parts): + msg = f"appids must be comma-separated app ids, got {raw!r}" + raise InvalidParamsError(msg) + ids = list(dict.fromkeys(int(part) for part in parts)) + known = {r["app_id"] for r in table} + missing = [str(i) for i in ids if i not in known] + if missing: + msg = f"Not installed: {', '.join(missing)}" + raise InvalidParamsError(msg) + if not ids: + msg = "Nothing to map: no installed game would change." + raise InvalidParamsError(msg) + return ids + + +def phrase_fields(params: Mapping[str, object]) -> dict[str, object]: + """The ``map-installed`` phrase's slots: how many games the job is given.""" + raw = params.get("appids") + return {"appid_count": len(checked_appids(None if raw is None else str(raw)))} diff --git a/steam_backlog_enforcer/_fast_install_cmd.py b/steam_backlog_enforcer/_fast_install_cmd.py index 0e2e1f3..0934060 100644 --- a/steam_backlog_enforcer/_fast_install_cmd.py +++ b/steam_backlog_enforcer/_fast_install_cmd.py @@ -88,18 +88,37 @@ def build_cmd( return desktop_user_cmd(cmd, user) -def build_repair_ghosts_cmd(installer: Path, user: str | None) -> list[str]: - """The installer's ``--repair-ghosts`` run, dropped to the desktop user. - - No AppID: it scans every library for the fake manifests anonymous - steamcmd left and clears only the cold ones (``DOCS-steam-installer.md``). - """ +def _installer_cmd(installer: Path, user: str | None, *flags: str) -> list[str]: + """``python3 -m steam_installer.cli ``, dropped to the desktop user.""" cmd = [ "env", f"PYTHONPATH={installer.parents[1]}", _PYTHON, "-m", "steam_installer.cli", - "--repair-ghosts", + *flags, ] return desktop_user_cmd(cmd, user) + + +def build_repair_ghosts_cmd(installer: Path, user: str | None) -> list[str]: + """The installer's ``--repair-ghosts`` run, dropped to the desktop user. + + No AppID: it scans every library for the fake manifests anonymous + steamcmd left and clears only the cold ones (``DOCS-steam-installer.md``). + """ + return _installer_cmd(installer, user, "--repair-ghosts") + + +def build_map_installed_cmd( + installer: Path, user: str | None, appids: list[int] +) -> list[str]: + """``--map-installed --apply`` for exactly *appids* (the phrase counted them). + + ``--yes`` because a job has no tty: the typed phrase was the confirmation, + and the installer would otherwise cancel at its "close Steam?" prompt. + """ + ids = ",".join(str(appid) for appid in appids) + return _installer_cmd( + installer, user, "--map-installed", "--apply", "--yes", "--appids", ids + ) diff --git a/steam_backlog_enforcer/_friction.py b/steam_backlog_enforcer/_friction.py index a9e0b73..0209ef0 100644 --- a/steam_backlog_enforcer/_friction.py +++ b/steam_backlog_enforcer/_friction.py @@ -54,6 +54,8 @@ FRICTION: Final[dict[str, Friction]] = { "restore-backup": Friction("restore backup {backup_id}"), # Checked at POST /api/jobs too (_web_jobs._PRECHECKED), then again by the job. "repair-ghosts": Friction("delete ghost manifests"), + # appid_count, not count: the UI fills ``count`` with the uninstall count. + "map-installed": Friction("map {appid_count} games"), } diff --git a/steam_backlog_enforcer/_web_jobs.py b/steam_backlog_enforcer/_web_jobs.py index 2221374..059bd28 100644 --- a/steam_backlog_enforcer/_web_jobs.py +++ b/steam_backlog_enforcer/_web_jobs.py @@ -15,6 +15,7 @@ from typing import TYPE_CHECKING, Any, Final from steam_backlog_enforcer._backups import list_backups from steam_backlog_enforcer._command_params import InvalidParamsError, validate_params +from steam_backlog_enforcer._compat_view import phrase_fields from steam_backlog_enforcer._friction import expected_phrase, phrase_matches from steam_backlog_enforcer._steam_restart_guard import ( GAME_RUNNING_JOB_MSG, @@ -29,8 +30,11 @@ from steam_backlog_enforcer.jobs._errors import JobError from steam_backlog_enforcer.jobs._specs import JOB_FLAGS, is_privileged from steam_backlog_enforcer.jobs._store import create_job from steam_backlog_enforcer.jobs._view import list_jobs, read_job +from steam_installer.errors import InstallerError if TYPE_CHECKING: + from collections.abc import Callable, Mapping + from steam_backlog_enforcer._web_io import Request # The UI's enforce modes onto the job registry's ``demo`` flag; ``restart`` @@ -39,7 +43,12 @@ _ENFORCE_MODES = {"demo": 1, "restart": 0} # Jobs whose phrase and game-running guard are checked here, before a job # exists, so a refusal never leaves a job behind; the job re-checks both. -_PRECHECKED: Final = frozenset({"repair-ghosts"}) +_PRECHECKED: Final = frozenset({"repair-ghosts", "map-installed"}) + +# The phrase slots a prechecked job's template names, from its clean params. +_PHRASE_FIELDS: Final[ + dict[str, Callable[[Mapping[str, object]], dict[str, object]]] +] = {"map-installed": phrase_fields} def _invalid(message: str) -> ApiError: @@ -81,18 +90,37 @@ def _job_request(request: Request) -> tuple[str, dict[str, Any], str | None]: return command, params, phrase -def _precheck(command: str, phrase: str | None) -> None: +def _phrase_fields(command: str, params: Mapping[str, object]) -> dict[str, object]: + """The phrase slots for *command*; bad params are a 400, unreadable Steam a 502. + + Raises: + ApiError: ``invalid_params`` or ``op_failed``. + """ + fields = _PHRASE_FIELDS.get(command) + if fields is None: + return {} + try: + return fields(params) + except InvalidParamsError as exc: + raise _invalid(str(exc)) from None + except (InstallerError, OSError, ValueError) as exc: + raise ApiError(str(exc), code="op_failed") from None + + +def _precheck(command: str, params: Mapping[str, object], phrase: str | None) -> None: """Refuse a :data:`_PRECHECKED` job before it is created. The phrase goes first: it is what the user can fix, and it stays - provable while a game runs. + provable while a game runs. Its slots (``map-installed``'s count) come + from the params, so a bad id list is refused before either. Raises: - ApiError: ``wrong_phrase``, or ``game_running`` while a game runs. + ApiError: ``invalid_params``, ``wrong_phrase``, or ``game_running`` + while a game runs. """ if command not in _PRECHECKED: return - expected = expected_phrase(command) or "" + expected = expected_phrase(command, **_phrase_fields(command, params)) or "" if not phrase_matches(expected, phrase or ""): msg = f'Type "{expected}" to run {command}.' raise ApiError(msg, code="wrong_phrase") @@ -125,7 +153,7 @@ def create_job_view(request: Request) -> Reply: raise _invalid(str(exc)) from None if is_privileged(command, clean): return run_privileged(command, clean, phrase) - _precheck(command, phrase) + _precheck(command, clean, phrase) try: job_id = create_job(command, clean, confirm_phrase=phrase) except JobError as exc: diff --git a/steam_backlog_enforcer/_web_routes.py b/steam_backlog_enforcer/_web_routes.py index b6e5e57..aee6f4a 100644 --- a/steam_backlog_enforcer/_web_routes.py +++ b/steam_backlog_enforcer/_web_routes.py @@ -12,6 +12,7 @@ import re from typing import TYPE_CHECKING, Final from steam_backlog_enforcer import ( + _compat_view, _web_art, _web_daemon, _web_icon_words, @@ -57,6 +58,7 @@ ROUTES: Final[tuple[_Entry, ...]] = ( _r("GET", "/api/status", _web_views.status_view), _r("GET", "/api/stats", _web_views.stats_view), _r("GET", "/api/installed", _web_views.installed_view), + _r("GET", "/api/compat", _compat_view.compat_view), _r("GET", "/api/library", _web_library.library_view), _r("GET", "/api/art/{id}", _web_art.art_view), _r("GET", "/api/commands", _web_views.commands_view), diff --git a/steam_backlog_enforcer/jobs/_registry.py b/steam_backlog_enforcer/jobs/_registry.py index aaa6467..71625cb 100644 --- a/steam_backlog_enforcer/jobs/_registry.py +++ b/steam_backlog_enforcer/jobs/_registry.py @@ -39,6 +39,7 @@ from steam_backlog_enforcer.main import ( cmd_unhide, cmd_uninstall, ) +from steam_backlog_enforcer.main.map_installed import run_map_installed from steam_backlog_enforcer.scanning import do_check, do_scan if TYPE_CHECKING: @@ -154,6 +155,14 @@ def _restore_backup( return {"restored": backup_id, "previous_backup": previous and previous.id} +def _map_installed( + params: Mapping[str, ParamValue], _progress: Progress, _prompter: Prompter +) -> JobResult: + """``map-installed``: the selected ids, or every game that would change.""" + raw = params.get("appids") + return {"app_ids": run_map_installed(None if raw is None else str(raw))} + + def _via_daemon(command: str) -> JobHandler: """A placeholder for a privileged command: it always refuses.""" @@ -182,6 +191,7 @@ HANDLERS: dict[str, JobHandler] = { "reset": _cli(cmd_reset), "restore-backup": _restore_backup, "repair-ghosts": _cli(cmd_repair_ghosts), + "map-installed": _map_installed, "stats": _cli(cmd_stats), "list": _cli(cmd_list), "enforce": _enforce_demo, diff --git a/steam_backlog_enforcer/jobs/_specs.py b/steam_backlog_enforcer/jobs/_specs.py index d0f3c2d..5d086bc 100644 --- a/steam_backlog_enforcer/jobs/_specs.py +++ b/steam_backlog_enforcer/jobs/_specs.py @@ -57,6 +57,7 @@ JOB_FLAGS: Final[dict[str, JobFlags]] = { "reset": JobFlags(), "restore-backup": JobFlags(), "repair-ghosts": JobFlags(), + "map-installed": JobFlags(), "stats": JobFlags(mutating=False, cancellable=True), "list": JobFlags(mutating=False, cancellable=True), # Only the demo runs as a job (see is_privileged). diff --git a/steam_backlog_enforcer/main/__init__.py b/steam_backlog_enforcer/main/__init__.py index 2b18da5..6ccf9d9 100644 --- a/steam_backlog_enforcer/main/__init__.py +++ b/steam_backlog_enforcer/main/__init__.py @@ -60,6 +60,7 @@ from steam_backlog_enforcer.main.install import ( cmd_unhide, cmd_uninstall, ) +from steam_backlog_enforcer.main.map_installed import cmd_map_installed from steam_backlog_enforcer.main.misc import ( cmd_block_gaming, cmd_buy_dlc, @@ -112,6 +113,7 @@ __all__ = [ "cmd_install", "cmd_installed", "cmd_list", + "cmd_map_installed", "cmd_pick", "cmd_pick_manual", "cmd_repair_ghosts", @@ -145,6 +147,7 @@ _HANDLERS: dict[str, Callable[[Config, State], object]] = { "gaming-status": cmd_gaming_status, "gaming-reset": cmd_gaming_reset, "repair-ghosts": cmd_repair_ghosts, + "map-installed": cmd_map_installed, } # Descriptions live in _command_help (the web catalog reads them too); the diff --git a/steam_backlog_enforcer/main/_install_target.py b/steam_backlog_enforcer/main/_install_target.py index d503a65..20b311f 100644 --- a/steam_backlog_enforcer/main/_install_target.py +++ b/steam_backlog_enforcer/main/_install_target.py @@ -24,6 +24,8 @@ from steam_backlog_enforcer.jobs._errors import JobError from steam_installer.target import parse_literal if TYPE_CHECKING: + from pathlib import Path + from steam_backlog_enforcer.config import Config, State # The dry run only reads Steam's app cache and library folders; repair-ghosts @@ -85,10 +87,7 @@ def dry_run_install(app_id: int, name: str, *, valve_proton: bool) -> None: Raises: JobError: ``op_failed`` when the installer is missing or fails. """ - installer = installer_path() - if installer is None: - msg = "steam-game-installer is missing from this checkout." - raise JobError(msg, code="op_failed") + installer = require_installer(installer_path()) cmd = build_cmd( installer, app_id, @@ -103,6 +102,18 @@ def dry_run_install(app_id: int, name: str, *, valve_proton: bool) -> None: raise JobError(msg, code="op_failed") +def require_installer(installer: Path | None) -> Path: + """*installer* itself, or the ``op_failed`` refusal when it is missing. + + Raises: + JobError: ``op_failed`` when the checkout has no installer. + """ + if installer is None: + msg = "steam-game-installer is missing from this checkout." + raise JobError(msg, code="op_failed") + return installer + + def relay_installer(cmd: list[str]) -> int: """Run a short, prompt-free installer *cmd*, echo its output, return its exit. diff --git a/steam_backlog_enforcer/main/map_installed.py b/steam_backlog_enforcer/main/map_installed.py new file mode 100644 index 0000000..241d384 --- /dev/null +++ b/steam_backlog_enforcer/main/map_installed.py @@ -0,0 +1,111 @@ +"""``map-installed``: the installer's ``--map-installed --apply``, behind friction. + +The apply closes Steam (``flow.prepare_steam``: Steam rewrites config.vdf on +exit), maps each game the decision proves safe to the native Proton, and +starts Steam again. Closing Steam under a running game would kill it, so this +refuses while one runs. The web server checks the ids, the phrase (it names +how many games) and the guard before a job exists (``_web_jobs``); this +re-checks all three inside the job. The installer gets exactly the ids the +phrase counted, never "everything". +""" + +from __future__ import annotations + +import subprocess +import threading +from typing import TYPE_CHECKING, Final + +from steam_backlog_enforcer._command_params import InvalidParamsError +from steam_backlog_enforcer._compat_view import checked_appids +from steam_backlog_enforcer._desktop_env import resolve_desktop_user +from steam_backlog_enforcer._fast_install_cmd import ( + build_map_installed_cmd, + installer_path, +) +from steam_backlog_enforcer._prompter import PromptAbortedError, confirm_phrase +from steam_backlog_enforcer._steam_restart_guard import ( + GAME_RUNNING_JOB_MSG, + game_is_running, +) +from steam_backlog_enforcer.game_install import _echo +from steam_backlog_enforcer.jobs._errors import JobError +from steam_backlog_enforcer.main._install_target import require_installer + +if TYPE_CHECKING: + from steam_backlog_enforcer.config import Config, State + +# Closing Steam may take steamctl.SHUTDOWN_TIMEOUT (60 s), then the mapping +# and the restart: well past the dry run's 50 s. +APPLY_TIMEOUT_SECONDS: Final = 180 + + +def stream_installer(cmd: list[str], *, timeout: float) -> int: + """Run *cmd*, echo each output line as it comes, return its exit code. + + Streamed, so the job page shows Steam closing and each game mapped live. + A timer kills it at *timeout* even while it still holds stdout open; a + killed run returns non-zero. + """ + killed = threading.Event() + with subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + stdin=subprocess.DEVNULL, + text=True, + ) as proc: + + def kill() -> None: + killed.set() + proc.kill() + + timer = threading.Timer(timeout, kill) + timer.daemon = True + timer.start() + try: + for line in proc.stdout or (): + _echo(f" {line.rstrip()}") + code = proc.wait() + finally: + timer.cancel() + if killed.is_set(): + _echo(f" Killed after {timeout:.0f} s.") + return code or 1 + return code + + +def run_map_installed(raw_appids: str | None) -> list[int]: + """Map *raw_appids* (or every game that would change), after guard and phrase. + + Returns: + The app ids handed to the installer. + + Raises: + JobError: ``game_running`` while a game runs; ``invalid_params`` when + the ids are no longer valid; ``op_failed`` when the installer is + missing or exits non-zero. + PromptAbortedError: The phrase was not typed exactly. + """ + if game_is_running(): + raise JobError(GAME_RUNNING_JOB_MSG, code="game_running") + try: + appids = checked_appids(raw_appids) + except InvalidParamsError as exc: + raise JobError(str(exc), code="invalid_params") from None + count = len(appids) + question = f"Close Steam and map {count} games to the native Proton?" + if not confirm_phrase("map-installed", question, appid_count=count): + msg = "Aborted." + raise PromptAbortedError(msg) + installer = require_installer(installer_path()) + cmd = build_map_installed_cmd(installer, resolve_desktop_user(), appids) + code = stream_installer(cmd, timeout=APPLY_TIMEOUT_SECONDS) + if code: + msg = f"map-installed failed (installer exit {code})." + raise JobError(msg, code="op_failed") + return appids + + +def cmd_map_installed(_config: Config, _state: State) -> None: + """``./run.sh map-installed``: every installed game that would change.""" + run_map_installed(None) diff --git a/steam_backlog_enforcer/main/repair_ghosts.py b/steam_backlog_enforcer/main/repair_ghosts.py index ac416c3..88cb042 100644 --- a/steam_backlog_enforcer/main/repair_ghosts.py +++ b/steam_backlog_enforcer/main/repair_ghosts.py @@ -23,7 +23,10 @@ from steam_backlog_enforcer._steam_restart_guard import ( game_is_running, ) from steam_backlog_enforcer.jobs._errors import JobError -from steam_backlog_enforcer.main._install_target import relay_installer +from steam_backlog_enforcer.main._install_target import ( + relay_installer, + require_installer, +) if TYPE_CHECKING: from steam_backlog_enforcer.config import Config, State @@ -42,10 +45,7 @@ def cmd_repair_ghosts(_config: Config, _state: State) -> None: if not confirm_phrase("repair-ghosts", "Delete the ghost install manifests?"): msg = "Aborted." raise PromptAbortedError(msg) - installer = installer_path() - if installer is None: - msg = "steam-game-installer is missing from this checkout." - raise JobError(msg, code="op_failed") + installer = require_installer(installer_path()) code = relay_installer(build_repair_ghosts_cmd(installer, resolve_desktop_user())) if code: msg = f"repair-ghosts failed (installer exit {code})." diff --git a/steam_backlog_enforcer/tests/_post_job.py b/steam_backlog_enforcer/tests/_post_job.py new file mode 100644 index 0000000..c4dc50e --- /dev/null +++ b/steam_backlog_enforcer/tests/_post_job.py @@ -0,0 +1,60 @@ +"""Shared POST-time friction test helpers: drive ``create_job_view`` once.""" + +from __future__ import annotations + +from contextlib import ExitStack +from typing import TYPE_CHECKING +from unittest.mock import MagicMock, patch + +from steam_backlog_enforcer import _web_jobs +from steam_backlog_enforcer._web_errors import ApiError +from steam_backlog_enforcer.tests._web_request import make_request + +if TYPE_CHECKING: + from collections.abc import Iterable + from contextlib import AbstractContextManager + from http import HTTPStatus + +WEB = "steam_backlog_enforcer._web_jobs" + + +def post_job( + command: str, + params: dict[str, object], + phrase: str | None, + *, + running: bool = False, + extra: Iterable[AbstractContextManager[object]] = (), +) -> tuple[object, MagicMock]: + """POST *command* with the job store and the game guard patched. + + Returns the reply (or the ``ApiError`` raised) and the ``create_job`` mock. + """ + body: dict[str, object] = {"command": command, "params": params} + if phrase is not None: + body["confirm_phrase"] = phrase + with ExitStack() as stack: + create = stack.enter_context(patch(f"{WEB}.create_job", return_value="j1")) + stack.enter_context(patch(f"{WEB}.read_job", return_value={"id": "j1"})) + stack.enter_context(patch(f"{WEB}.game_is_running", return_value=running)) + for patcher in extra: + stack.enter_context(patcher) + try: + reply: object = _web_jobs.create_job_view(make_request(body=body)) + except ApiError as exc: + reply = exc + return reply, create + + +def assert_refused( + reply: object, + create: MagicMock, + code: str, + status: HTTPStatus | None = None, +) -> None: + """The POST was refused with *code* (and *status*) and no job was made.""" + assert isinstance(reply, ApiError) + assert reply.code == code + if status is not None: + assert reply.status == status + create.assert_not_called() diff --git a/steam_backlog_enforcer/tests/test_command_catalog.py b/steam_backlog_enforcer/tests/test_command_catalog.py index 3b3d844..1415001 100644 --- a/steam_backlog_enforcer/tests/test_command_catalog.py +++ b/steam_backlog_enforcer/tests/test_command_catalog.py @@ -155,15 +155,20 @@ class TestPhrasesAreFillable: def test_every_template_field_has_a_source(self) -> None: """Every phrase field can be filled from a param or derived value.""" # Mirrors fillPhrase in web/src/commands/catalog.ts: a field is a - # param, ``game_name`` from an ``app_id`` param, or ``count`` from - # the installed list. Anything else leaves Run disabled forever. + # param, ``game_name`` from an ``app_id`` param, ``appid_count`` from + # an ``appids`` param, or ``count`` from the installed list. Anything + # else leaves Run disabled forever. # The same two tables ``_spec`` builds each CommandSpec from. for name in _command_catalog.CATALOG: friction = FRICTION.get(name) if friction is None: continue params = {p.name for p in PARAMS.get(name, ())} - derived = {"count"} | ({"game_name"} if "app_id" in params else set()) + derived = {"count"} + if "app_id" in params: + derived.add("game_name") + if "appids" in params: + derived.add("appid_count") template = friction.phrase_template fields = {f for _, f, _, _ in Formatter().parse(template) if f} assert fields <= params | derived, name diff --git a/steam_backlog_enforcer/tests/test_compat_view.py b/steam_backlog_enforcer/tests/test_compat_view.py new file mode 100644 index 0000000..aa7aee1 --- /dev/null +++ b/steam_backlog_enforcer/tests/test_compat_view.py @@ -0,0 +1,165 @@ +"""``GET /api/compat``: the map-installed decisions as rows, cached by file stats.""" + +from __future__ import annotations + +import re +from typing import TYPE_CHECKING +from unittest.mock import MagicMock, patch + +import pytest + +from steam_backlog_enforcer import _compat_view, _web_routes +from steam_backlog_enforcer._command_params import InvalidParamsError +from steam_backlog_enforcer._web_errors import ApiError +from steam_backlog_enforcer.tests._web_request import make_request +from steam_installer.appinfo import AppInfoError +from steam_installer.compat_rows import Row + +if TYPE_CHECKING: + from collections.abc import Iterator + from pathlib import Path + +V = "steam_backlog_enforcer._compat_view" + + +def _row(appid: int, *, changes: bool) -> Row: + return Row( + appid=appid, + name=f"G{appid}", + current=None, + priority=None, + decided="proton-cachyos-native" if changes else None, + reasons=() if changes else ("native",), + note=None, + map=changes, + changes=changes, + cached=True, + ) + + +ROWS = [_row(10, changes=True), _row(20, changes=False), _row(30, changes=True)] + + +@pytest.fixture(name="env") +def _env() -> Iterator[dict[str, MagicMock]]: + """A fake Steam root, a fixed stat key and the installer's rows mocked.""" + _compat_view.clear_cache() + with ( + patch(f"{V}.find_steam_root", return_value=MagicMock()) as root, + patch(f"{V}.cache_key", return_value=("k",)) as key, + patch(f"{V}.Context") as context, + patch(f"{V}.rows", return_value=ROWS) as rows, + ): + context.return_value.tool_ready = True + yield {"root": root, "key": key, "context": context, "rows": rows} + _compat_view.clear_cache() + + +def test_payload_shape(env: dict[str, MagicMock]) -> None: + """Rows as the CompatRow contract, plus the tool and whether it is ready.""" + data = _compat_view.compat_payload() + assert data["tool"] == "proton-cachyos-native" + assert data["tool_ready"] is True + rows = data["rows"] + assert isinstance(rows, list) + assert [r["app_id"] for r in rows] == [10, 20, 30] + assert rows[1]["reasons"] == ["native"] + _, kwargs = env["context"].call_args + assert kwargs == {"anticheat_max_age": _compat_view.ANTICHEAT_MAX_AGE} + + +def test_same_key_is_served_from_memory(env: dict[str, MagicMock]) -> None: + """A second GET with unchanged files decides nothing again.""" + _compat_view.compat_payload() + _compat_view.compat_payload() + env["rows"].assert_called_once() + + +def test_a_changed_file_recomputes(env: dict[str, MagicMock]) -> None: + """Any stat change (config.vdf written by apply) is a fresh decision.""" + _compat_view.compat_payload() + env["key"].return_value = ("k2",) + _compat_view.compat_payload() + assert env["rows"].call_count == 2 + + +@pytest.mark.usefixtures("env") +def test_view_replies_ok() -> None: + """The route answers 200 with the payload.""" + reply = _compat_view.compat_view(make_request()) + assert reply.status == 200 + assert isinstance(reply.payload, dict) + + +@pytest.mark.parametrize( + "exc", [AppInfoError("no cache"), FileNotFoundError("no Steam"), ValueError("x")] +) +def test_view_failure_is_op_failed(env: dict[str, MagicMock], exc: Exception) -> None: + """No app cache, no Steam or a bad overrides file is a 502, not a crash.""" + env["rows"].side_effect = exc + with pytest.raises(ApiError) as err: + _compat_view.compat_view(make_request()) + assert err.value.code == "op_failed" + + +def test_route_is_registered() -> None: + """``GET /api/compat`` is in the route table.""" + assert any( + e.method == "GET" and re.fullmatch(e.pattern, "/api/compat") + for e in _web_routes.ROUTES + ) + + +def test_cache_key_tracks_the_files(tmp_path: Path) -> None: + """The key changes when config.vdf or a manifest changes.""" + root = tmp_path / "Steam" + (root / "config").mkdir(parents=True) + (root / "steamapps").mkdir() + config = root / "config" / "config.vdf" + config.write_text("a") + with patch(f"{V}.appinfo_cache.default_path", return_value=root / "none.vdf"): + first = _compat_view.cache_key(root) + config.write_text("ab") + second = _compat_view.cache_key(root) + (root / "steamapps" / "appmanifest_1.acf").write_text("x") + third = _compat_view.cache_key(root) + assert len({first, second, third}) == 3 + + +class TestCheckedAppids: + """The ``appids`` param: digits only, installed only, never empty.""" + + @pytest.mark.usefixtures("env") + def test_none_means_every_changing_game(self) -> None: + """No appids: every row that would change.""" + assert _compat_view.checked_appids(None) == [10, 30] + + @pytest.mark.usefixtures("env") + def test_list_is_parsed_and_deduped(self) -> None: + """Spaces forgiven, duplicates dropped, order kept.""" + assert _compat_view.checked_appids(" 30, 10,30 ") == [30, 10] + + @pytest.mark.usefixtures("env") + @pytest.mark.parametrize("raw", ["10,x", "10,,20", "-1", "10;20"]) + def test_junk_is_refused(self, raw: str) -> None: + """Anything but comma-separated digits is invalid.""" + with pytest.raises(InvalidParamsError): + _compat_view.checked_appids(raw) + + @pytest.mark.usefixtures("env") + def test_a_game_that_is_not_installed_is_refused(self) -> None: + """Apply never maps a game that is not installed.""" + with pytest.raises(InvalidParamsError, match="999"): + _compat_view.checked_appids("10,999") + + def test_nothing_to_map_is_refused(self, env: dict[str, MagicMock]) -> None: + """No changing game and no list: nothing to do is an error, not a no-op.""" + env["rows"].return_value = [_row(20, changes=False)] + with pytest.raises(InvalidParamsError): + _compat_view.checked_appids(None) + + @pytest.mark.usefixtures("env") + def test_phrase_fields(self) -> None: + """The phrase counts exactly the games the job will be given.""" + assert _compat_view.phrase_fields({"appids": "10,20"}) == {"appid_count": 2} + assert _compat_view.phrase_fields({}) == {"appid_count": 2} diff --git a/steam_backlog_enforcer/tests/test_map_installed_job.py b/steam_backlog_enforcer/tests/test_map_installed_job.py new file mode 100644 index 0000000..f88f5bf --- /dev/null +++ b/steam_backlog_enforcer/tests/test_map_installed_job.py @@ -0,0 +1,208 @@ +"""``map-installed``: the apply job, its streaming and the tables.""" + +from __future__ import annotations + +import io +from typing import TYPE_CHECKING +from unittest.mock import MagicMock, patch + +import pytest + +from steam_backlog_enforcer import _command_catalog +from steam_backlog_enforcer._command_params import PARAMS, InvalidParamsError +from steam_backlog_enforcer._fast_install_cmd import build_map_installed_cmd +from steam_backlog_enforcer._friction import expected_phrase +from steam_backlog_enforcer._prompter import PromptAbortedError, use_prompter +from steam_backlog_enforcer.config import Config, State +from steam_backlog_enforcer.jobs import _registry +from steam_backlog_enforcer.jobs._errors import JobError +from steam_backlog_enforcer.jobs._specs import JOB_FLAGS +from steam_backlog_enforcer.main import COMMANDS +from steam_backlog_enforcer.main import map_installed as job +from steam_backlog_enforcer.tests._fake_prompter import FakePrompter + +if TYPE_CHECKING: + from collections.abc import Callable, Iterator + from pathlib import Path + +PKG = "steam_backlog_enforcer.main.map_installed" + + +def _proc(lines: str, code: int = 0) -> MagicMock: + proc = MagicMock(returncode=code) + proc.stdout = io.StringIO(lines) + proc.wait.return_value = code + proc.__enter__.return_value = proc + return proc + + +@pytest.fixture(name="env") +def _env() -> Iterator[dict[str, MagicMock]]: + """No game running, two games to map, an installer, Popen mocked.""" + with ( + patch(f"{PKG}.game_is_running", return_value=False) as running, + patch(f"{PKG}.checked_appids", return_value=[10, 30]) as ids, + patch(f"{PKG}.installer_path", return_value=MagicMock()), + patch(f"{PKG}.build_map_installed_cmd", return_value=["inst"]) as build, + patch(f"{PKG}.subprocess.Popen") as popen, + patch(f"{PKG}._echo") as echo, + ): + popen.return_value = _proc("Compat tool for 10: mapped.\n") + yield { + "running": running, + "ids": ids, + "build": build, + "popen": popen, + "echo": echo, + } + + +def _run( + raw: str | None = "10,30", phrases: tuple[bool, ...] = (True,) +) -> FakePrompter: + prompter = FakePrompter(phrases=phrases) + with use_prompter(prompter): + job.run_map_installed(raw) + return prompter + + +class TestJob: + """``run_map_installed``: guard, phrase, then the streamed installer.""" + + def test_maps_the_checked_ids_and_streams_output( + self, env: dict[str, MagicMock] + ) -> None: + """The phrase counts the ids; the installer gets exactly those ids.""" + prompter = _run() + assert prompter.asked[0][1] == "map 2 games" + env["ids"].assert_called_once_with("10,30") + assert env["build"].call_args.args[2] == [10, 30] + env["echo"].assert_any_call(" Compat tool for 10: mapped.") + + def test_game_running_refuses_first(self, env: dict[str, MagicMock]) -> None: + """Closing Steam would kill the game: refuse before anything else.""" + env["running"].return_value = True + with pytest.raises(JobError) as exc: + _run(phrases=()) + assert exc.value.code == "game_running" + env["popen"].assert_not_called() + + def test_wrong_phrase_aborts(self, env: dict[str, MagicMock]) -> None: + """A wrong phrase never starts the installer.""" + with pytest.raises(PromptAbortedError): + _run(phrases=(False,)) + env["popen"].assert_not_called() + + def test_bad_appids_fail_the_job(self, env: dict[str, MagicMock]) -> None: + """Ids that stopped being valid since the POST are ``invalid_params``.""" + env["ids"].side_effect = InvalidParamsError("999 is not installed") + with pytest.raises(JobError) as exc: + _run() + assert exc.value.code == "invalid_params" + + def test_installer_failure(self, env: dict[str, MagicMock]) -> None: + """A non-zero exit is ``op_failed``.""" + env["popen"].return_value = _proc("ERROR\n", code=1) + with pytest.raises(JobError) as exc: + _run() + assert exc.value.code == "op_failed" + + def test_missing_installer(self, env: dict[str, MagicMock]) -> None: + """No installer in the checkout fails before any subprocess.""" + with patch(f"{PKG}.installer_path", return_value=None), pytest.raises(JobError): + _run() + env["popen"].assert_not_called() + + def test_cli_command_maps_every_changing_game( + self, env: dict[str, MagicMock] + ) -> None: + """``./run.sh map-installed`` is the job with no list.""" + with use_prompter(FakePrompter(phrases=(True,))): + job.cmd_map_installed(Config(), State()) + env["ids"].assert_called_once_with(None) + + def test_job_handler_passes_the_param(self, env: dict[str, MagicMock]) -> None: + """The job handler hands ``appids`` through and reports the ids.""" + with use_prompter(FakePrompter(phrases=(True,))): + data = _registry.HANDLERS["map-installed"]( + {"appids": "10,30"}, MagicMock(), MagicMock() + ) + env["ids"].assert_called_once_with("10,30") + assert data == {"app_ids": [10, 30]} + + +class _FiringTimer: + """A ``threading.Timer`` whose deadline has already passed.""" + + def __init__(self, _seconds: float, fn: Callable[[], None]) -> None: + self.fn = fn + self.daemon = False + + def start(self) -> None: + """Fire at once: the deadline has already passed.""" + self.fn() + + def cancel(self) -> None: + """Nothing left to cancel.""" + + +def test_stream_times_out_and_kills() -> None: + """A run past its deadline is killed and reported as a failure.""" + proc = _proc("") + proc.wait.return_value = -9 + with ( + patch(f"{PKG}.subprocess.Popen", return_value=proc), + patch(f"{PKG}.threading.Timer", _FiringTimer), + patch(f"{PKG}._echo") as echo, + ): + assert job.stream_installer(["x"], timeout=1) == -9 + proc.kill.assert_called_once() + echo.assert_any_call(" Killed after 1 s.") + + +def test_stream_killed_with_a_zero_exit_still_fails() -> None: + """A kill that somehow reports 0 is still a failure.""" + proc = _proc("") + proc.wait.return_value = 0 + with ( + patch(f"{PKG}.subprocess.Popen", return_value=proc), + patch(f"{PKG}.threading.Timer", _FiringTimer), + patch(f"{PKG}._echo"), + ): + assert job.stream_installer(["x"], timeout=1) == 1 + + +def test_stream_returns_the_exit_code() -> None: + """No timeout: the installer's own exit code.""" + proc = _proc("a\n", code=3) + with patch(f"{PKG}.subprocess.Popen", return_value=proc), patch(f"{PKG}._echo"): + assert job.stream_installer(["x"], timeout=60) == 3 + + +def test_argv(tmp_path: Path) -> None: + """``--map-installed --apply --yes --appids`` with the checked ids.""" + installer = tmp_path / "steam_installer" / "cli.py" + with patch( + "steam_backlog_enforcer._fast_install_cmd.desktop_user_cmd", + side_effect=lambda cmd, _user: cmd, + ): + cmd = build_map_installed_cmd(installer, None, [10, 30]) + assert cmd[-6:] == [ + "steam_installer.cli", + "--map-installed", + "--apply", + "--yes", + "--appids", + "10,30", + ] + assert f"PYTHONPATH={tmp_path}" in cmd + + +def test_tables() -> None: + """Phrase, CLI command, job, catalog entry and the optional param.""" + assert expected_phrase("map-installed", appid_count=3) == "map 3 games" + assert COMMANDS["map-installed"][1] is job.cmd_map_installed + assert JOB_FLAGS["map-installed"].mutating + assert _command_catalog.CATALOG["map-installed"].category == "library" + (spec,) = PARAMS["map-installed"] + assert (spec.name, spec.type, spec.required) == ("appids", "string", False) diff --git a/steam_backlog_enforcer/tests/test_map_installed_post.py b/steam_backlog_enforcer/tests/test_map_installed_post.py new file mode 100644 index 0000000..5fb8114 --- /dev/null +++ b/steam_backlog_enforcer/tests/test_map_installed_post.py @@ -0,0 +1,73 @@ +"""``map-installed``: what ``POST /api/jobs`` refuses before a job exists.""" + +from __future__ import annotations + +from http import HTTPStatus +from typing import TYPE_CHECKING +from unittest.mock import patch + +import pytest + +from steam_backlog_enforcer._command_params import InvalidParamsError +from steam_backlog_enforcer._web_errors import ApiError +from steam_backlog_enforcer.tests._post_job import assert_refused, post_job + +if TYPE_CHECKING: + from unittest.mock import MagicMock + + +def _post( + phrase: str | None, appids: str = "10,30", *, running: bool = False +) -> tuple[object, MagicMock]: + checked = patch( + "steam_backlog_enforcer._compat_view.checked_appids", + side_effect=lambda raw: ( + [int(x) for x in str(raw).split(",") if x.isdigit()] or _bad() + ), + ) + return post_job( + "map-installed", {"appids": appids}, phrase, running=running, extra=[checked] + ) + + +def _bad() -> list[int]: + msg = "not installed" + raise InvalidParamsError(msg) + + +class TestPostTimeFriction: + """``POST /api/jobs`` refuses before a job exists.""" + + @pytest.mark.parametrize("phrase", [None, "map 3 games", "Map 2 games"]) + def test_wrong_phrase_creates_no_job(self, phrase: str | None) -> None: + """The count is part of the phrase: a stale count is wrong.""" + assert_refused(*_post(phrase), "wrong_phrase") + + def test_game_running_is_409(self) -> None: + """The right phrase while a game runs: ``game_running``, no job.""" + assert_refused( + *_post("map 2 games", running=True), "game_running", HTTPStatus.CONFLICT + ) + + def test_bad_appids_are_invalid_params(self) -> None: + """Ids that are not installed games: 400 before the phrase.""" + assert_refused(*_post("map 0 games", appids="x"), "invalid_params") + + def test_unreadable_steam_is_op_failed(self) -> None: + """Steam's library cannot be read: ``op_failed`` before a job exists.""" + unreadable = patch( + "steam_backlog_enforcer._compat_view.checked_appids", + side_effect=OSError("no library"), + ) + reply, create = post_job( + "map-installed", {"appids": "10"}, "map 1 games", extra=[unreadable] + ) + assert_refused(reply, create, "op_failed") + + def test_right_phrase_creates_the_job(self) -> None: + """Right phrase, no game: the job, with the phrase passed on.""" + reply, create = _post("map 2 games") + assert not isinstance(reply, ApiError) + create.assert_called_once_with( + "map-installed", {"appids": "10,30"}, confirm_phrase="map 2 games" + ) diff --git a/steam_backlog_enforcer/tests/test_repair_ghosts.py b/steam_backlog_enforcer/tests/test_repair_ghosts.py index 1bc43c2..3414264 100644 --- a/steam_backlog_enforcer/tests/test_repair_ghosts.py +++ b/steam_backlog_enforcer/tests/test_repair_ghosts.py @@ -8,7 +8,7 @@ from unittest.mock import MagicMock, patch import pytest -from steam_backlog_enforcer import _command_catalog, _web_jobs +from steam_backlog_enforcer import _command_catalog from steam_backlog_enforcer._fast_install_cmd import build_repair_ghosts_cmd from steam_backlog_enforcer._friction import expected_phrase from steam_backlog_enforcer._prompter import PromptAbortedError, use_prompter @@ -20,7 +20,7 @@ from steam_backlog_enforcer.jobs._specs import JOB_FLAGS from steam_backlog_enforcer.main import COMMANDS from steam_backlog_enforcer.main.repair_ghosts import cmd_repair_ghosts from steam_backlog_enforcer.tests._fake_prompter import FakePrompter -from steam_backlog_enforcer.tests._web_request import make_request +from steam_backlog_enforcer.tests._post_job import assert_refused, post_job if TYPE_CHECKING: from collections.abc import Iterator @@ -136,23 +136,8 @@ class TestWiring: assert data == {"current_app_id": None, "current_game_name": None} -_WEB = "steam_backlog_enforcer._web_jobs" - - def _post(phrase: str | None, *, running: bool = False) -> tuple[object, MagicMock]: - body: dict[str, object] = {"command": "repair-ghosts", "params": {}} - if phrase is not None: - body["confirm_phrase"] = phrase - with ( - patch(f"{_WEB}.create_job", return_value="j1") as create, - patch(f"{_WEB}.read_job", return_value={"id": "j1"}), - patch(f"{_WEB}.game_is_running", return_value=running), - ): - try: - reply: object = _web_jobs.create_job_view(make_request(body=body)) - except ApiError as exc: - reply = exc - return reply, create + return post_job("repair-ghosts", {}, phrase, running=running) class TestPostTimeFriction: @@ -161,23 +146,17 @@ class TestPostTimeFriction: @pytest.mark.parametrize("phrase", [None, "", "Delete ghost manifests"]) def test_wrong_phrase_creates_no_job(self, phrase: str | None) -> None: """A missing or wrong phrase is ``wrong_phrase`` and no job.""" - reply, create = _post(phrase) - assert isinstance(reply, ApiError) - assert reply.code == "wrong_phrase" - create.assert_not_called() + assert_refused(*_post(phrase), "wrong_phrase") def test_wrong_phrase_wins_over_a_running_game(self) -> None: """The phrase is checked first, so friction is provable mid-game.""" - reply, _ = _post("nope", running=True) - assert isinstance(reply, ApiError) - assert reply.code == "wrong_phrase" + assert_refused(*_post("nope", running=True), "wrong_phrase") def test_game_running_creates_no_job(self) -> None: """The right phrase while a game runs is ``game_running`` (409).""" - reply, create = _post(PHRASE, running=True) - assert isinstance(reply, ApiError) - assert (reply.code, reply.status) == ("game_running", HTTPStatus.CONFLICT) - create.assert_not_called() + assert_refused( + *_post(PHRASE, running=True), "game_running", HTTPStatus.CONFLICT + ) def test_right_phrase_creates_the_job(self) -> None: """The right phrase with no game creates the job, phrase passed on.""" @@ -189,12 +168,5 @@ class TestPostTimeFriction: def test_other_commands_are_not_prechecked(self) -> None: """Commands outside the set keep their job-side check only.""" - with ( - patch(f"{_WEB}.create_job", return_value="j1") as create, - patch(f"{_WEB}.read_job", return_value={"id": "j1"}), - patch(f"{_WEB}.game_is_running", return_value=True), - ): - _web_jobs.create_job_view( - make_request(body={"command": "uninstall", "params": {}}) - ) + _, create = post_job("uninstall", {}, None, running=True) create.assert_called_once() diff --git a/steam_installer/anticheat.py b/steam_installer/anticheat.py index 0efa71c..f0d1f0d 100644 --- a/steam_installer/anticheat.py +++ b/steam_installer/anticheat.py @@ -12,6 +12,7 @@ from __future__ import annotations import json import os from pathlib import Path +import time import urllib.error from steam_installer import _http @@ -43,8 +44,18 @@ def _ids(raw: bytes | str) -> set[int]: return ids -def steam_ids(cache: Path = CACHE) -> set[int] | None: - """Anti-cheat Steam app ids, or None when no list can be had.""" +def steam_ids(cache: Path = CACHE, max_age: float | None = None) -> set[int] | None: + """Anti-cheat Steam app ids, or None when no list can be had. + + With *max_age* (seconds), a cache younger than that is used as it is, so a + caller that must answer fast (the web view) never waits on the network. + """ + if max_age is not None: + try: + if time.time() - cache.stat().st_mtime < max_age: + return _ids(cache.read_bytes()) + except OSError, ValueError, TypeError: + pass try: raw = _download() ids = _ids(raw) diff --git a/steam_installer/compat_apply.py b/steam_installer/compat_apply.py index 4cda5ba..9c3133e 100644 --- a/steam_installer/compat_apply.py +++ b/steam_installer/compat_apply.py @@ -23,12 +23,20 @@ class Context: """What every decision in one run shares: fetched once, not per game.""" def __init__( - self, steam_root: Path, extra_overrides: dict[str, str] | None = None + self, + steam_root: Path, + extra_overrides: dict[str, str] | None = None, + *, + anticheat_max_age: float | None = None, ) -> None: - """Remember what every decision in this run shares.""" + """Remember what every decision in this run shares. + + *anticheat_max_age* lets a fast reader reuse a recent anti-cheat list + instead of downloading it (``anticheat.steam_ids``). + """ self.steam_root = steam_root self.config = compat_map.config_path(steam_root) - self.anticheat = anticheat.steam_ids() + self.anticheat = anticheat.steam_ids(max_age=anticheat_max_age) self.overrides = {**compat_decide.load_overrides(), **(extra_overrides or {})} self.tool_ready = compat_decide.tool_ready( steam_root / TOOL_DIR, proton_pkg.read_lock().version diff --git a/steam_installer/compat_map.py b/steam_installer/compat_map.py index 6e0dd07..45775e5 100644 --- a/steam_installer/compat_map.py +++ b/steam_installer/compat_map.py @@ -75,6 +75,20 @@ def current(config: Path, appid: int) -> tuple[str, str] | None: return entry.values.get("name", ""), entry.values.get("priority", "") +def keep_reason(entry: tuple[str, str] | None) -> Outcome | None: + """Why :func:`map_app` would leave *entry* alone, or None when it would map. + + *entry* is :func:`current`'s ``(name, priority)``. The native tool is + "already"; any other tool off Valve's priority is kuhy's own choice, "kept". + """ + name, priority = entry or ("", "") + if name == TOOL: + return Outcome("already") + if name and priority != VALVE_PRIORITY: + return Outcome("kept", f"already mapped to {name} (priority {priority})") + return None + + def map_app(config: Path, appid: int) -> Outcome: """Point `appid` at the native Proton unless kuhy chose another tool.""" pids = recognize.steam_client_pids() @@ -88,11 +102,9 @@ def map_app(config: Path, appid: int) -> Outcome: entry = layout.entry if entry is not None: name = entry.values.get("name", "") - priority = entry.values.get("priority", "") - if name == TOOL: - return Outcome("already") - if name and priority != VALVE_PRIORITY: - return Outcome("kept", f"already mapped to {name} (priority {priority})") + held = keep_reason((name, entry.values.get("priority", ""))) + if held is not None: + return held key_line = entry.open_line - 1 if ( lines[key_line].strip() != f'"{appid}"' diff --git a/steam_installer/compat_rows.py b/steam_installer/compat_rows.py new file mode 100644 index 0000000..0828b9b --- /dev/null +++ b/steam_installer/compat_rows.py @@ -0,0 +1,149 @@ +"""What ``--map-installed --apply`` would do, one structured row per game. + +``map_installed.run`` logs from these rows and the enforcer's web view serves +them (``GET /api/compat``), so the two can never disagree and nothing ever +parses the log. ``changes`` uses the same :func:`compat_map.keep_reason` that +:func:`compat_map.map_app` uses, so a row says "changes" exactly when apply +would rewrite that game's entry. +""" + +from __future__ import annotations + +from dataclasses import asdict, dataclass +from typing import TYPE_CHECKING, Any + +from steam_installer import appinfo, compat_apply, compat_map, recognize +from steam_installer.library import SteamLibrary, list_libraries + +if TYPE_CHECKING: + from pathlib import Path + +# Steam's own compat tools and runtimes are apps too, but not games. +TOOL_TYPES = {"tool", "config"} +NOT_CACHED = "not in Steam's app cache" + + +@dataclass(frozen=True) +class Row: + """One game's compat decision. + + Attributes: + current: The tool config.vdf maps it to; None = unmapped (Valve's default). + priority: That mapping's priority, None when unmapped. + decided: The tool after apply; None = Valve's default. + reasons: Why the decision keeps Valve's default (empty = map it). + note: Why apply leaves an entry the decision would map ("already + mapped", or kuhy's own tool "kept: ..."). + map: The decision's verdict. + changes: Whether apply would rewrite this game's entry. + cached: Whether Steam's app cache knows the game at all. + """ + + appid: int + name: str + current: str | None + priority: str | None + decided: str | None + reasons: tuple[str, ...] + note: str | None + map: bool + changes: bool + cached: bool + + def to_json(self) -> dict[str, object]: + """The web contract's ``CompatRow``.""" + data = asdict(self) + data["app_id"] = data.pop("appid") + data["reasons"] = list(self.reasons) + return data + + +def installed(libraries: list[SteamLibrary]) -> dict[int, dict[str, Any]]: + """Appid -> AppState for every manifest in every library.""" + found: dict[int, dict[str, Any]] = {} + for library in libraries: + for manifest in sorted(library.steamapps.glob("appmanifest_*.acf")): + appid = manifest.stem.removeprefix("appmanifest_") + if appid.isdigit(): + found[int(appid)] = recognize.read_app_state(manifest) + return found + + +def owned_dlc(app_state: dict[str, Any]) -> bool: + """Whether the manifest lists an owned DLC depot.""" + depots = app_state.get("InstalledDepots") + entries = depots.values() if isinstance(depots, dict) else [] + return any(isinstance(entry, dict) and "dlcappid" in entry for entry in entries) + + +def is_tool(block: dict[str, Any]) -> bool: + """Whether the app is one of Steam's tools or runtimes, not a game.""" + common = block.get("common") + kind = common.get("type", "") if isinstance(common, dict) else "" + return isinstance(kind, str) and kind.lower() in TOOL_TYPES + + +def _row( + context: compat_apply.Context, + appid: int, + block: dict[str, Any] | None, + app_state: dict[str, Any], +) -> Row: + entry = compat_map.current(context.config, appid) + current = (entry[0] or None) if entry else None + priority = (entry[1] or None) if entry else None + if block is None: + name = app_state.get("name", "") + return Row( + appid=appid, + name=name if isinstance(name, str) else "", + current=current, + priority=priority, + decided=current, + reasons=(NOT_CACHED,), + note=None, + map=False, + changes=False, + cached=False, + ) + decision = context.decide(appid, block, owned_dlc=owned_dlc(app_state)) + held = compat_map.keep_reason(entry) if decision.map else None + changes = decision.map and held is None + note = None + if held is not None: + note = "already mapped" if held.action == "already" else f"kept: {held.detail}" + common = block.get("common") + name = common.get("name", "?") if isinstance(common, dict) else "?" + return Row( + appid=appid, + name=str(name), + current=current, + priority=priority, + decided=compat_map.TOOL if changes else current, + reasons=tuple(decision.reasons), + note=note, + map=decision.map, + changes=changes, + cached=True, + ) + + +def rows( + steam_root: Path, + appids: list[int] | None = None, + context: compat_apply.Context | None = None, +) -> list[Row]: + """Rows for every installed game, or for exactly *appids* (tools included). + + Raises: + appinfo.AppInfoError: Steam's app cache cannot be read. + """ + context = context or compat_apply.Context(steam_root) + states = installed(list_libraries(steam_root)) + ids = appids if appids is not None else sorted(states) + blocks = appinfo.cached_blocks(ids) + return [ + _row(context, appid, blocks.get(appid), states.get(appid, {})) + for appid in ids + if appids is not None or appid not in blocks or not is_tool(blocks[appid]) + ] diff --git a/steam_installer/map_installed.py b/steam_installer/map_installed.py index dddd81b..34d5a0f 100644 --- a/steam_installer/map_installed.py +++ b/steam_installer/map_installed.py @@ -11,26 +11,21 @@ first (it rewrites config.vdf on exit) and restarts it afterwards. from __future__ import annotations import logging -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING from steam_installer import ( - appinfo, compat_apply, compat_map, + compat_rows, flow, - recognize, runlog, steamctl, ) -from steam_installer.library import SteamLibrary, list_libraries if TYPE_CHECKING: import argparse from pathlib import Path -# Steam's own compat tools and runtimes are apps too, but not games. -TOOL_TYPES = {"tool", "config"} - def add_arguments(parser: argparse.ArgumentParser) -> None: """Register the ``--map-installed`` options on *parser*.""" @@ -60,61 +55,31 @@ def check_arguments(parser: argparse.ArgumentParser, args: argparse.Namespace) - parser.error("--valve-proton goes with an install, not --map-installed") -def _installed(libraries: list[SteamLibrary]) -> dict[int, dict[str, Any]]: - """Appid -> AppState for every manifest in every library.""" - found: dict[int, dict[str, Any]] = {} - for library in libraries: - for manifest in sorted(library.steamapps.glob("appmanifest_*.acf")): - appid = manifest.stem.removeprefix("appmanifest_") - if appid.isdigit(): - found[int(appid)] = recognize.read_app_state(manifest) - return found - - -def _owned_dlc(app_state: dict[str, Any]) -> bool: - depots = app_state.get("InstalledDepots") - entries = depots.values() if isinstance(depots, dict) else [] - return any(isinstance(entry, dict) and "dlcappid" in entry for entry in entries) - - -def _is_tool(block: dict[str, Any]) -> bool: - common = block.get("common") - kind = common.get("type", "") if isinstance(common, dict) else "" - return isinstance(kind, str) and kind.lower() in TOOL_TYPES +def _line(row: compat_rows.Row) -> str: + """The log line for one decided row (the dry run's report).""" + verdict = "map" if row.map else f"Valve default: {', '.join(row.reasons)}" + line = f"{row.appid} {row.name}: {verdict}" + already = row.map and row.current == compat_map.TOOL + return line + " -- already mapped" if already else line def run(args: argparse.Namespace, log: runlog.RunLog, steam_root: Path) -> int: """Decide, and with ``--apply`` write, mappings for installed games.""" logger = log.logger - installed = _installed(list_libraries(steam_root)) + appids = None if args.appids: appids = [int(x) for x in args.appids.split(",") if x.strip().isdigit()] - else: - appids = sorted(installed) context = compat_apply.Context(steam_root) - blocks = appinfo.cached_blocks(appids) - to_map: list[int] = [] - for appid in appids: - block = blocks.get(appid) - if block is None: - logger.info("%s: not in Steam's app cache -- skipped", appid) - continue - if _is_tool(block) and not args.appids: - continue - name = block.get("common", {}).get("name", "?") - decision = context.decide( - appid, block, owned_dlc=_owned_dlc(installed.get(appid, {})) - ) - line = f"{appid} {name}: {decision.describe()}" - if decision.map: - to_map.append(appid) - current = compat_map.current(context.config, appid) - if current and current[0] == compat_map.TOOL: - line += " -- already mapped" - logger.info("%s", line) + decided = compat_rows.rows(steam_root, appids, context) + for row in decided: + if row.cached: + logger.info("%s", _line(row)) + else: + logger.info("%s: not in Steam's app cache -- skipped", row.appid) if not args.apply: logger.info("Dry run: nothing written. Rerun with --apply to write.") return 0 + to_map = [row.appid for row in decided if row.map] return _apply(args, logger, context.config, to_map) diff --git a/steam_installer/tests/_steam_root.py b/steam_installer/tests/_steam_root.py new file mode 100644 index 0000000..1fbaea6 --- /dev/null +++ b/steam_installer/tests/_steam_root.py @@ -0,0 +1,49 @@ +"""A fake Steam root for the map-installed tests: config, app cache, manifests.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Any + +from steam_installer import anticheat, compat_apply, 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, LOCKED + +if TYPE_CHECKING: + from collections.abc import Iterable + from pathlib import Path + + import pytest + + +def build_root( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + *, + cached: dict[int, dict[str, Any]], + installed: Iterable[int], + anticheat_json: bytes, +) -> Path: + """A Steam root with the stamped tool, an app cache and installed manifests. + + *cached* is the app cache's content; *installed* the appids that get an + ``appmanifest`` (named ``Game``), cached or not. + """ + 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(cached)) + monkeypatch.setattr( + compat_apply.appinfo.appinfo_cache, "default_path", lambda: cache + ) + monkeypatch.setattr(anticheat, "_download", lambda: anticheat_json) + steamapps = root / "steamapps" + steamapps.mkdir() + for appid in installed: + state = {"appid": str(appid), "name": f"Game{appid}", "InstalledDepots": {}} + (steamapps / f"appmanifest_{appid}.acf").write_text( + vdf.dumps("AppState", state) + ) + return root diff --git a/steam_installer/tests/test_compat_rows.py b/steam_installer/tests/test_compat_rows.py new file mode 100644 index 0000000..97b1f9a --- /dev/null +++ b/steam_installer/tests/test_compat_rows.py @@ -0,0 +1,183 @@ +"""Structured compat rows: what ``--map-installed --apply`` would do per game.""" + +from __future__ import annotations + +import os +import time +from typing import TYPE_CHECKING, Never + +import pytest + +from steam_installer import anticheat, compat_apply, compat_map, compat_rows +from steam_installer.tests._map_data import NATIVE, PLAIN, TOOL +from steam_installer.tests._steam_root import build_root + +if TYPE_CHECKING: + from pathlib import Path + +TOOL_NAME = compat_map.TOOL + + +@pytest.fixture(name="root") +def _root(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: + """A Steam root with games 10 (Plain), 20 (Native), 40 (uncached) and a tool.""" + return build_root( + tmp_path, + monkeypatch, + cached={10: PLAIN, 20: NATIVE, 1493710: TOOL}, + installed=(10, 20, 40, 1493710), + anticheat_json=b"[]", + ) + + +def _by_id(root: Path) -> dict[int, compat_rows.Row]: + return {row.appid: row for row in compat_rows.rows(root)} + + +def _set_entry(root: Path, name: str, priority: str) -> None: + config = root / "config" / "config.vdf" + compat_map.map_app(config, 10) + text = config.read_text().replace(TOOL_NAME, name) + config.write_text(text.replace(f'"{compat_map.PRIORITY}"', f'"{priority}"')) + + +def test_installed_games_only_tools_skipped(root: Path) -> None: + """Every installed game is a row; Steam's own tools are not.""" + assert sorted(_by_id(root)) == [10, 20, 40] + + +def test_a_safe_unmapped_game_changes(root: Path) -> None: + """Plain is Windows-only and unmapped: apply maps it.""" + row = _by_id(root)[10] + assert (row.name, row.current, row.decided) == ("Plain", None, TOOL_NAME) + assert (row.map, row.changes, row.reasons, row.note) == (True, True, (), None) + + +def test_a_native_game_keeps_valves_default(root: Path) -> None: + """Native keeps Valve's default, with the decision's reason.""" + row = _by_id(root)[20] + assert (row.decided, row.map, row.changes) == (None, False, False) + assert row.reasons == ("native",) + + +def test_an_uncached_game_is_listed_unchanged(root: Path) -> None: + """An installed game missing from the app cache is a row that never changes.""" + row = _by_id(root)[40] + assert (row.name, row.cached, row.changes, row.decided) == ( + "Game40", + False, + False, + None, + ) + assert row.reasons == ("not in Steam's app cache",) + + +def test_already_mapped_does_not_change(root: Path) -> None: + """A game already on the native tool is noted and unchanged.""" + compat_map.map_app(root / "config" / "config.vdf", 10) + row = _by_id(root)[10] + assert (row.current, row.priority, row.decided) == (TOOL_NAME, "250", TOOL_NAME) + assert (row.changes, row.note) == (False, "already mapped") + + +def test_a_user_choice_is_kept(root: Path) -> None: + """A tool kuhy chose (non-Valve priority) is kept, exactly as map_app keeps it.""" + _set_entry(root, "GE-Proton10", "250") + row = _by_id(root)[10] + assert (row.current, row.decided, row.changes) == ( + "GE-Proton10", + "GE-Proton10", + False, + ) + assert row.note == "kept: already mapped to GE-Proton10 (priority 250)" + + +def test_valves_own_choice_is_replaced(root: Path) -> None: + """An entry at Valve's priority is Valve's choice: apply replaces it.""" + _set_entry(root, "proton_9", compat_map.VALVE_PRIORITY) + row = _by_id(root)[10] + assert (row.current, row.decided, row.changes) == ("proton_9", TOOL_NAME, True) + + +def test_explicit_appids_include_tools_and_uncached(root: Path) -> None: + """``appids`` decides exactly those ids, tools included, in the given order.""" + got = compat_rows.rows(root, appids=[1493710, 999]) + assert [(r.appid, r.cached) for r in got] == [(1493710, True), (999, False)] + assert got[1].name == "" + + +def test_to_json_shape(root: Path) -> None: + """The web contract's CompatRow keys, reasons as a list.""" + assert _by_id(root)[20].to_json() == { + "app_id": 20, + "name": "Native", + "current": None, + "priority": None, + "decided": None, + "reasons": ["native"], + "note": None, + "map": False, + "changes": False, + "cached": True, + } + + +@pytest.mark.parametrize( + ("entry", "action"), + [ + (None, None), + (("", ""), None), + ((TOOL_NAME, "250"), "already"), + (("GE-Proton10", "250"), "kept"), + (("proton_9", compat_map.VALVE_PRIORITY), None), + ], +) +def test_keep_reason(entry: tuple[str, str] | None, action: str | None) -> None: + """The one predicate map_app and the rows share.""" + held = compat_map.keep_reason(entry) + assert (held.action if held else None) == action + + +def _cache(tmp_path: Path, age: float) -> Path: + cache = tmp_path / "ac.json" + cache.write_text('[{"storeIds": {"steam": "7"}}]') + stamp = time.time() - age + os.utime(cache, (stamp, stamp)) + return cache + + +def test_steam_ids_max_age_reads_a_fresh_cache_offline( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """A cache younger than ``max_age`` is used without touching the network.""" + + def boom() -> Never: + pytest.fail("downloaded") + + monkeypatch.setattr(anticheat, "_download", boom) + assert anticheat.steam_ids(_cache(tmp_path, 10), max_age=60) == {7} + + +def test_steam_ids_max_age_downloads_a_stale_cache( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """An older (or missing) cache is downloaded as before.""" + monkeypatch.setattr( + anticheat, "_download", lambda: b'[{"storeIds": {"steam": "8"}}]' + ) + assert anticheat.steam_ids(_cache(tmp_path, 120), max_age=60) == {8} + assert anticheat.steam_ids(tmp_path / "none.json", max_age=60) == {8} + + +def test_context_passes_max_age(root: Path, monkeypatch: pytest.MonkeyPatch) -> None: + """``Context(anticheat_max_age=...)`` reaches ``steam_ids``.""" + seen: list[float | None] = [] + + def ids(max_age: float | None = None) -> set[int]: + seen.append(max_age) + return set() + + monkeypatch.setattr(anticheat, "steam_ids", ids) + compat_apply.Context(root, anticheat_max_age=5.0) + compat_apply.Context(root) + assert seen == [5.0, None] diff --git a/steam_installer/tests/test_map_installed.py b/steam_installer/tests/test_map_installed.py index d8b35b4..8778a32 100644 --- a/steam_installer/tests/test_map_installed.py +++ b/steam_installer/tests/test_map_installed.py @@ -7,26 +7,21 @@ 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, ) +from steam_installer.tests._steam_root import build_root if TYPE_CHECKING: from pathlib import Path @@ -38,26 +33,13 @@ 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}) + root = build_root( + tmp_path, + monkeypatch, + cached={10: PLAIN, 20: NATIVE, 30: SHARED, 730: CS, 1493710: TOOL}, + installed=(10, 20, 1493710), + anticheat_json=b'[{"storeIds": {"steam": "730"}}]', ) - 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 diff --git a/web/src/api/client.ts b/web/src/api/client.ts index e801e7d..87206ed 100644 --- a/web/src/api/client.ts +++ b/web/src/api/client.ts @@ -7,6 +7,7 @@ import { TOKEN_HEADER, TOKEN_META, type CommandSpec, + type CompatPayload, type DaemonStatus, type InstalledPayload, type LibraryPayload, @@ -97,6 +98,7 @@ export const api = { status: () => get(ENDPOINTS.status), stats: () => get(ENDPOINTS.stats), installed: () => get(ENDPOINTS.installed), + compat: () => get(ENDPOINTS.compat), library: () => get(ENDPOINTS.library), commands: () => get(ENDPOINTS.commands), setup: () => get(ENDPOINTS.setup), diff --git a/web/src/api/compatContract.ts b/web/src/api/compatContract.ts new file mode 100644 index 0000000..04da3d9 --- /dev/null +++ b/web/src/api/compatContract.ts @@ -0,0 +1,31 @@ +// The Proton map's shapes (`GET /api/compat`). Split from contract.ts (file-length +// cap); same frozen contract, same prose spec: DOCS-web-install.md. + +/** + * One installed game's compat-tool decision (`GET /api/compat`), from the + * installer's own `--map-installed` rows (steam_installer/compat_rows.py). + */ +export interface CompatRow { + app_id: number + name: string + /** The tool config.vdf maps it to; null = unmapped (Valve's default). */ + current: string | null + priority: string | null + /** The tool after apply; null = Valve's default. */ + decided: string | null + /** Why the decision keeps Valve's default; empty = map it. */ + reasons: string[] + /** Why apply leaves a mappable game alone ("already mapped", "kept: โ€ฆ"). */ + note: string | null + map: boolean + /** Whether `map-installed` would rewrite this game's entry. */ + changes: boolean + /** False when Steam's app cache does not know the game. */ + cached: boolean +} + +export interface CompatPayload { + tool: string + tool_ready: boolean + rows: CompatRow[] +} diff --git a/web/src/api/contract.ts b/web/src/api/contract.ts index e737370..b2d9ac1 100644 --- a/web/src/api/contract.ts +++ b/web/src/api/contract.ts @@ -31,6 +31,7 @@ export type CommandName = | 'gaming-unblock' | 'serve' | 'repair-ghosts' + | 'map-installed' /** Job spawned by `POST /api/backups/{id}/restore`, not a CLI command. */ | 'restore-backup' /** Failed end-of-window store re-block the web server records; never started by hand. */ @@ -202,25 +203,13 @@ export interface DaemonStatus { fast_installs?: FastInstallRun[] } -export interface ServerHealth { - stale: boolean - started_at: string - version: string -} - -export interface StateBackup { - id: string - created_at: string - reason: string - size_bytes: number -} - export const ENDPOINTS = { dataset: '/api/dataset', budget: '/api/budget', status: '/api/status', stats: '/api/stats', installed: '/api/installed', + compat: '/api/compat', library: '/api/library', /** Portrait cover JPEG; 404 when there is none (draw a title tile). */ art: (appId: number) => `/api/art/${appId}`, @@ -248,3 +237,7 @@ export type { BudgetSnapshot, WebDataset } // Icon words (DOCS-icon-words.md): types in iconWordsContract.ts. export * from './iconWordsContract' + +// Compat rows (compatContract.ts) and server health/backups (serverContract.ts). +export * from './compatContract' +export * from './serverContract' diff --git a/web/src/api/queries.ts b/web/src/api/queries.ts index 6efabde..9702a3c 100644 --- a/web/src/api/queries.ts +++ b/web/src/api/queries.ts @@ -12,6 +12,8 @@ export const q = { status: new Query(api.status, { intervalMs: 15_000 }), stats: new Query(api.stats), installed: new Query(api.installed), + // Polled while the Proton map is mounted: Steam rewrites config.vdf on exit. + compat: new Query(api.compat, { intervalMs: 30_000 }), library: new Query(api.library, { staleMs: 60_000 }), commands: new Query(api.commands, { intervalMs: 30_000 }), setup: new Query(api.setup), diff --git a/web/src/api/serverContract.ts b/web/src/api/serverContract.ts new file mode 100644 index 0000000..b490750 --- /dev/null +++ b/web/src/api/serverContract.ts @@ -0,0 +1,15 @@ +// Server health and state-backup shapes. Split from contract.ts (file-length +// cap); same frozen contract, same prose spec: DOCS-web-control-api.md. + +export interface ServerHealth { + stale: boolean + started_at: string + version: string +} + +export interface StateBackup { + id: string + created_at: string + reason: string + size_bytes: number +} diff --git a/web/src/commands/catalog.test.ts b/web/src/commands/catalog.test.ts index 3a03d6a..2d91ae3 100644 --- a/web/src/commands/catalog.test.ts +++ b/web/src/commands/catalog.test.ts @@ -41,6 +41,11 @@ describe('fillPhrase', () => { expect(fillPhrase('drop {game_name}', {}, ctx()).missing).toEqual(['game_name']) }) + it('counts the ids in appids for {appid_count} (map-installed)', () => { + expect(fillPhrase('map {appid_count} games', { appids: '10,30,40' }, ctx())).toEqual({ text: 'map 3 games', missing: [] }) + expect(fillPhrase('map {appid_count} games', {}, ctx()).missing).toEqual(['appid_count']) + }) + it('counts removable installed games for {count}', () => { const installed = [ makeInstalled({ app_id: 1 }), diff --git a/web/src/commands/catalog.ts b/web/src/commands/catalog.ts index 6ee889e..13dd5b0 100644 --- a/web/src/commands/catalog.ts +++ b/web/src/commands/catalog.ts @@ -45,6 +45,7 @@ export const COMMAND_HOME = { 'restore-backup': '/system', 're-block': '/store', 'repair-ghosts': '/library', + 'map-installed': '/library', } as const satisfies Record /** A command's registered icon id (`cmd.`); the compiler proves every command has one. */ @@ -88,6 +89,8 @@ export function fillPhrase( const text = template.replace(/\{(\w+)\}/g, (whole, key: string) => { let value: string | null = null if (key in params && params[key] !== '') value = String(params[key]) + // map-installed: the server counts the same comma-separated ids. + else if (key === 'appid_count' && typeof params.appids === 'string') value = String(params.appids.split(',').length) else if (key === 'game_name' && 'app_id' in params) value = ctx.gameName(Number(params.app_id)) else if (key === 'count' && ctx.installed) { value = String(ctx.installed.filter((g) => !g.assigned && !g.protected).length) diff --git a/web/src/commands/paramsForm.ts b/web/src/commands/paramsForm.ts index 71b9d53..11b21a2 100644 --- a/web/src/commands/paramsForm.ts +++ b/web/src/commands/paramsForm.ts @@ -22,6 +22,7 @@ const PARAM_ICON: Record = { target: 'params.target', dry_run: 'params.dry_run', valve_proton: 'params.valve_proton', + appids: 'params.appids', } function paramGlyph(spec: ParamSpec): HTMLSpanElement { diff --git a/web/src/icons/coverage.test.ts b/web/src/icons/coverage.test.ts index 0a86bb8..9372396 100644 --- a/web/src/icons/coverage.test.ts +++ b/web/src/icons/coverage.test.ts @@ -48,9 +48,9 @@ describe('icon words cover the whole UI', () => { expect(hits, `${hits.length} unregistered icon sites`).toEqual([]) }) - it('the first 47 registry entries are the pass-1 icons (all answered but cmd.re-block and cmd.repair-ghosts), then the rest', () => { + it('the first 48 registry entries are the pass-1 icons (all answered but cmd.re-block, cmd.repair-ghosts and cmd.map-installed), then the rest', () => { const ids = ICONS.map((e) => e.id) - expect(ids.slice(0, 47).filter((id) => /^(nav|palette|cmd|job\.state|notice)\./.test(id))).toHaveLength(47) - expect(ids[46]).toBe('notice.danger') + expect(ids.slice(0, 48).filter((id) => /^(nav|palette|cmd|job\.state|notice)\./.test(id))).toHaveLength(48) + expect(ids[47]).toBe('notice.danger') }) }) diff --git a/web/src/icons/registry.json b/web/src/icons/registry.json index 434324d..cdc1343 100644 --- a/web/src/icons/registry.json +++ b/web/src/icons/registry.json @@ -24,6 +24,7 @@ "cmd.install": { "glyph": "โฌ‡๏ธ", "spoken": "Install", "desc": "Runs install: downloads an allowed game, trying the fast installer first.", "words": ["install", "download", "get"], "alts": ["๐Ÿ“ฅ", "๐Ÿ”ฝ", "โฌ"] }, "cmd.uninstall": { "glyph": "๐Ÿ—‘๏ธ", "spoken": "Uninstall", "desc": "Runs uninstall: removes every installed game that is neither assigned nor protected.", "words": ["uninstall", "remove", "clean"], "alts": ["๐Ÿงน", "โ๏ธ", "๐Ÿชฃ"] }, "cmd.repair-ghosts": { "glyph": "๐Ÿ‘ป", "spoken": "Repair ghosts", "desc": "Runs repair ghosts: deletes the fake installed manifests anonymous steamcmd left, keeping any with staged bytes.", "words": ["ghosts", "phantom", "fake"], "alts": ["๐ŸงŸ", "๐Ÿชฆ", "๐Ÿซฅ"] }, + "cmd.map-installed": { "glyph": "๐Ÿ—บ๏ธ", "spoken": "Map installed", "desc": "Runs map installed: closes Steam, points each installed game proven safe at the native Proton, then starts Steam again.", "words": ["map", "proton", "compat"], "alts": ["๐Ÿงญ", "๐Ÿ”€", "๐Ÿ“Œ"]}, "cmd.hide": { "glyph": "๐Ÿ™ˆ", "spoken": "Hide", "desc": "Runs hide: hides every game except the assigned one in the Steam library.", "words": ["hide", "conceal", "mask"], "alts": ["๐Ÿซฃ", "๐Ÿ•ถ๏ธ", "๐ŸŒซ๏ธ"] }, "cmd.unhide": { "glyph": "๐Ÿ‘๏ธ", "spoken": "Unhide", "desc": "Runs unhide: shows every game in the Steam library again.", "words": ["unhide", "show", "reveal"], "alts": ["๐Ÿ‘€", "๐Ÿ”ฆ", "๐ŸชŸ"] }, "cmd.buy-dlc": { "glyph": "๐Ÿ’ณ", "spoken": "Buy DLC", "desc": "Runs buy DLC: unblocks the Steam store for 15 minutes so you can buy a game or DLC.", "words": ["DLC", "buy", "purchase"], "alts": ["๐Ÿ’ต", "๐Ÿช™", "๐Ÿท๏ธ"] }, @@ -97,6 +98,16 @@ "libpage.role": { "glyph": "๐Ÿท๏ธ", "spoken": "Role", "desc": "Header of the role column in the installed table: whether a game is assigned, protected or removable.", "words": ["role", "status", "tag"], "alts": ["๐Ÿ”–", "๐ŸŽญ", "๐Ÿชช"] }, "libpage.protected": { "glyph": "๐Ÿ›ก๏ธ", "spoken": "Protected", "desc": "Tags an installed game that is protected, so uninstall never removes it.", "words": ["protected", "safe", "shielded"], "alts": ["๐Ÿ”ฐ", "๐Ÿ”", "๐Ÿงฑ"] }, "libpage.candidate": { "glyph": "๐Ÿ—‘๏ธ", "spoken": "Uninstall candidate", "desc": "Marks an installed game that is neither assigned nor protected, so uninstall would remove it.", "words": ["removable", "trash", "surplus"], "alts": ["๐Ÿงน", "โ™ป๏ธ", "๐Ÿชฃ"] }, + "libpage.compat": { "glyph": "๐Ÿท", "spoken": "Proton map", "desc": "Switch on the library page that shows the compat-tool (Proton) decision for every installed game, and maps the safe ones.", "words": ["proton", "wine", "compat"], "alts": ["๐Ÿงช", "๐Ÿ”ง", "๐Ÿงฌ"]}, + "compat.current": { "glyph": "๐Ÿ”ง", "spoken": "Current tool", "desc": "Header of the current-tool column of the Proton map: the compat tool Steam's config maps the game to now.", "words": ["current", "now", "tool"], "alts": ["๐Ÿ› ๏ธ", "๐Ÿ”ฉ", "โš™๏ธ"]}, + "compat.decided": { "glyph": "๐Ÿงญ", "spoken": "Decided tool", "desc": "Header of the decided-tool column of the Proton map: the compat tool the game has after map installed runs.", "words": ["decided", "next", "target"], "alts": ["โžก๏ธ", "๐Ÿ”ฎ", "๐Ÿ“"]}, + "compat.why": { "glyph": "โ”", "spoken": "Reason", "desc": "Header of the reason column of the Proton map: why a game keeps Valve's default, or why map installed leaves it alone.", "words": ["why", "reason", "because"], "alts": ["โ“", "๐Ÿ’ญ", "๐Ÿ“"]}, + "compat.changes": { "glyph": "๐Ÿ”„", "spoken": "Changes", "desc": "Marks a game whose compat tool map installed would change, and heads that column of the Proton map.", "words": ["changes", "switch", "differs"], "alts": ["๐Ÿ”ƒ", "โ™ป๏ธ", "๐Ÿ”‚"]}, + "compat.same": { "glyph": "๐ŸŸฐ", "spoken": "Unchanged", "desc": "Marks a game in the Proton map whose compat tool map installed would leave exactly as it is.", "words": ["unchanged", "same", "kept"], "alts": ["โธ๏ธ", "๐ŸŸฆ", "โž–"]}, + "compat.valve": { "glyph": "โ™จ๏ธ", "spoken": "Valve default", "desc": "Marks a game with no compat-tool mapping of its own, so Steam runs it on Valve's default Proton.", "words": ["valve", "default", "stock"], "alts": ["๐ŸŒซ๏ธ", "๐Ÿ’จ", "๐Ÿš‚"]}, + "compat.safe": { "glyph": "โœ…", "spoken": "Safe to map", "desc": "Marks a game in the Proton map that the decision proved safe for the native Proton.", "words": ["safe", "ok", "proven"], "alts": ["๐ŸŸข", "๐Ÿ‘Œ", "๐Ÿ†—"]}, + "compat.select": { "glyph": "โ˜‘๏ธ", "spoken": "Select", "desc": "Header of the selection column of the Proton map: tick games to map only those.", "words": ["select", "pick", "choose"], "alts": ["โœ”๏ธ", "๐Ÿ”˜", "๐Ÿ“‹"]}, + "params.appids": { "glyph": "๐Ÿ”ข", "spoken": "App ids", "desc": "Field in the map installed dialog: the comma-separated app ids of the games to map.", "words": ["appids", "ids", "games"], "alts": ["๐Ÿ†”", "๐Ÿ“‡", "๐Ÿงพ"]}, "jobs.command": { "glyph": "โŒจ๏ธ", "spoken": "Command", "desc": "Header of the command column in the job history.", "words": ["command", "action", "task"], "alts": ["๐Ÿ’ป", "๐Ÿ”ค", "๐Ÿงฐ"] }, "jobs.state": { "glyph": "๐Ÿšฆ", "spoken": "State", "desc": "Header of the state column in the job history: queued, running, finished or failed.", "words": ["state", "status", "outcome"], "alts": ["๐ŸŸข", "๐Ÿ“ถ", "๐Ÿšฅ"] }, "jobs.started": { "glyph": "๐Ÿ•", "spoken": "Started", "desc": "When a job started: the started column of the job history and the created time on a job page.", "words": ["started", "created", "when"], "alts": ["๐Ÿ•’", "๐Ÿ“…", "๐Ÿ•ฐ๏ธ"] }, diff --git a/web/src/icons/registry.test.ts b/web/src/icons/registry.test.ts index 46c22b2..829ff5c 100644 --- a/web/src/icons/registry.test.ts +++ b/web/src/icons/registry.test.ts @@ -9,13 +9,13 @@ const JOB_STATES: JobState[] = ['queued', 'running', 'waiting_input', 'succeeded const TONES: Tone[] = ['info', 'success', 'warning', 'danger'] describe('icon registry', () => { - it('registers pass 1 first (8 nav, the palette, 28 commands, 6 job states, 4 tones), then the rest of the UI', () => { + it('registers pass 1 first (8 nav, the palette, 29 commands, 6 job states, 4 tones), then the rest of the UI', () => { const ids = ICONS.map((e) => e.id) expect(new Set(ids).size).toBe(ids.length) const pass1 = /^(nav|palette|cmd|job\.state|notice)\./ - expect(ids.slice(0, 8 + 1 + 28 + 6 + 4).every((id) => pass1.test(id))).toBe(true) - expect(ids.slice(8 + 1 + 28 + 6 + 4).some((id) => pass1.test(id))).toBe(false) - expect(ids.length).toBeGreaterThan(47) + expect(ids.slice(0, 8 + 1 + 29 + 6 + 4).every((id) => pass1.test(id))).toBe(true) + expect(ids.slice(8 + 1 + 29 + 6 + 4).some((id) => pass1.test(id))).toBe(false) + expect(ids.length).toBeGreaterThan(48) for (const n of NAV) expect(isIconId(n.icon)).toBe(true) expect(isIconId('palette.open')).toBe(true) for (const name of Object.keys(COMMAND_HOME) as (keyof typeof COMMAND_HOME)[]) { diff --git a/web/src/library/compatState.test.ts b/web/src/library/compatState.test.ts new file mode 100644 index 0000000..1087f82 --- /dev/null +++ b/web/src/library/compatState.test.ts @@ -0,0 +1,134 @@ +import { describe, expect, it } from 'vitest' +import type { CompatRow } from '../api/contract' +import { + COMPAT_COLUMNS, + compatOptions, + defaultCompat, + readCompat, + toggleCompatSort, + toolLabel, + visibleCompat, + whyText, + writeCompat, + type CompatState, +} from './compatState' +import { makeCompatRow } from '../test/fixtures' +import { buildIndex } from './fuzzy' + +const TOOL = 'proton-cachyos-native' + +// Real shape: most rows keep Valve's default (duplicates), one kept tool, +// one already mapped, one missing from the app cache, long reason lists. +const ROWS: CompatRow[] = [ + makeCompatRow({ app_id: 1245620, name: 'Elden Ring', decided: null, map: false, changes: false, reasons: ['anti-cheat'] }), + makeCompatRow({ app_id: 70, name: 'Half-Life', decided: null, map: false, changes: false, reasons: ['native', 'not Windows-only'] }), + makeCompatRow({ app_id: 292030, name: 'The Witcher 3', current: null }), + makeCompatRow({ app_id: 1091500, name: 'Cyberpunk 2077', current: 'GE-Proton10', priority: '250', decided: 'GE-Proton10', changes: false, note: 'kept: already mapped to GE-Proton10 (priority 250)' }), + makeCompatRow({ app_id: 413150, name: 'Stardew Valley', current: TOOL, priority: '250', changes: false, note: 'already mapped' }), + makeCompatRow({ app_id: 4000, name: 'Garry', decided: null, map: false, changes: false, cached: false, reasons: ["not in Steam's app cache"] }), +] + +const names = (s: CompatState) => visibleCompat(ROWS, s, buildIndex(ROWS)).map((r) => r.name) +const state = (over: Partial = {}): CompatState => ({ ...defaultCompat(), ...over }) + +describe('compat columns', () => { + it('has the six data columns in order', () => { + expect(COMPAT_COLUMNS.map((c) => c.key)).toEqual(['name', 'app_id', 'current', 'decided', 'why', 'changes']) + }) + + it('gives every column a text form, app ids as digits', () => { + expect(COMPAT_COLUMNS.map((c) => c.text(ROWS[2]))).toEqual(['The Witcher 3', '292030', 'Valve default', TOOL, '', 'changes']) + }) + + it('labels a missing tool as Valve default and joins reasons, else the note', () => { + expect(toolLabel(null)).toBe('Valve default') + expect(toolLabel(TOOL)).toBe(TOOL) + expect(whyText(ROWS[1])).toBe('native, not Windows-only') + expect(whyText(ROWS[4])).toBe('already mapped') + expect(whyText(ROWS[2])).toBe('') + }) +}) + +describe('visibleCompat', () => { + it('puts the games that change first by default', () => { + expect(names(state())[0]).toBe('The Witcher 3') + }) + + it('sorts names Aโ†’Z and reverses on a second click', () => { + const once = toggleCompatSort(state(), 'name') + expect(names(once)).toEqual(['Cyberpunk 2077', 'Elden Ring', 'Garry', 'Half-Life', 'Stardew Valley', 'The Witcher 3']) + expect(names(toggleCompatSort(once, 'name'))).toEqual([...names(once)].reverse()) + }) + + it('reverses ties too, so a column of equal values still reorders', () => { + const asc = state({ sort: 'current', asc: true }) + const desc = state({ sort: 'current', asc: false }) + expect(names(asc)).not.toEqual(names(desc)) + }) + + it('breaks a name tie by app id, in the sort direction', () => { + const twins = [makeCompatRow({ app_id: 2, name: 'Twin' }), makeCompatRow({ app_id: 1, name: 'Twin' })] + const ids = (asc: boolean) => visibleCompat(twins, state({ sort: 'name', asc }), buildIndex(twins)).map((r) => r.app_id) + expect(ids(true)).toEqual([1, 2]) + expect(ids(false)).toEqual([2, 1]) + }) + + it('sorts app ids numerically, high first on the first click', () => { + const s = toggleCompatSort(state(), 'app_id') + expect(visibleCompat(ROWS, s, buildIndex(ROWS)).map((r) => r.app_id)[0]).toBe(1245620) + }) + + it('filters by a select column value', () => { + const s = state({ filters: new Map([['decided', 'Valve default']]) }) + expect(names(s).sort()).toEqual(['Elden Ring', 'Garry', 'Half-Life']) + const changing = state({ filters: new Map([['changes', 'changes']]) }) + expect(names(changing)).toEqual(['The Witcher 3']) + }) + + it('filters text columns by substring and app ids by range', () => { + expect(names(state({ filters: new Map([['why', 'anti']]) }))).toEqual(['Elden Ring']) + expect(names(state({ filters: new Map([['app_id.min', '1000000']]) })).sort()).toEqual(['Cyberpunk 2077', 'Elden Ring']) + expect(names(state({ filters: new Map([['app_id.max', '100']]) }))).toEqual(['Half-Life']) + expect(names(state({ filters: new Map([['name', 'ring']]) }))).toEqual(['Elden Ring']) + }) + + it('fuzzy-searches names with typos and roman numerals', () => { + expect(names(state({ query: 'wicher iii' }))).toEqual(['The Witcher 3']) + }) +}) + +describe('compat options and hash', () => { + it('lists distinct options, plus a restored value', () => { + const decided = COMPAT_COLUMNS.find((c) => c.key === 'decided') + if (!decided) throw new Error('no decided column') + expect(compatOptions(decided, ROWS)).toEqual(['GE-Proton10', TOOL, 'Valve default']) + expect(compatOptions(decided, ROWS, 'gone')).toContain('gone') + const changes = COMPAT_COLUMNS.find((c) => c.key === 'changes') + if (!changes) throw new Error('no changes column') + expect(compatOptions(changes, [])).toEqual(['changes', 'same']) + }) + + it('round-trips through c.-prefixed keys and leaves other keys alone', () => { + const s = state({ sort: 'name', asc: true, query: 'ring', filters: new Map([['why', 'anti'], ['app_id.min', '5']]) }) + const p = new URLSearchParams('q=hollow&sort=recent&view=compat') + writeCompat(s, p) + expect(p.get('q')).toBe('hollow') + expect(p.get('sort')).toBe('recent') + expect(p.get('c.sort')).toBe('name') + expect(p.get('c.f.app_id')).toBe('5..') + const back = readCompat(p) + expect(back).toEqual(s) + }) + + it('writes nothing for the default state and drops stale keys', () => { + const p = new URLSearchParams('c.sort=name&c.dir=asc&c.q=x&c.f.why=y') + writeCompat(defaultCompat(), p) + expect(p.toString()).toBe('') + }) + + it('ignores an unknown sort column', () => { + expect(readCompat(new URLSearchParams('c.sort=nope')).sort).toBe(defaultCompat().sort) + expect(readCompat(new URLSearchParams('c.sort=name&c.dir=desc')).asc).toBe(false) + expect(readCompat(new URLSearchParams('c.f.app_id=..9')).filters.get('app_id.max')).toBe('9') + }) +}) diff --git a/web/src/library/compatState.ts b/web/src/library/compatState.ts new file mode 100644 index 0000000..73f3dc1 --- /dev/null +++ b/web/src/library/compatState.ts @@ -0,0 +1,150 @@ +// The Proton map's columns, sort, per-column filters, fuzzy search and hash +// state, as pure functions so only wires inputs to them. +// Hash grammar (prefixed `c.`: the cover grid owns `q` and `sort` in the same +// hash): `c.sort=&c.dir=asc|desc`, `c.q=`, `c.f.=text` for +// text/select columns and `c.f.app_id=min..max` (either side optional). + +import type { CompatRow } from '../api/contract' +import { search, type Searchable } from './fuzzy' + +export type CompatKey = 'name' | 'app_id' | 'current' | 'decided' | 'why' | 'changes' +export type CompatFilter = 'text' | 'range' | 'select' + +export interface CompatColumn { + key: CompatKey + label: string + numeric: boolean + filter: CompatFilter + /** Sort key (and the range filter's value). */ + value: (r: CompatRow) => number | string + /** Select columns: the option a row belongs to; text columns: the searched text. */ + text: (r: CompatRow) => string +} + +export const VALVE_DEFAULT = 'Valve default' + +/** A tool as data: its name, or Valve's default when the game has none. */ +export const toolLabel = (tool: string | null): string => tool ?? VALVE_DEFAULT + +/** The reason column: the decision's reasons, else apply's note ('' = maps). */ +export const whyText = (r: CompatRow): string => (r.reasons.length > 0 ? r.reasons.join(', ') : (r.note ?? '')) + +const changeOption = (r: CompatRow) => (r.changes ? 'changes' : 'same') + +export const COMPAT_COLUMNS: readonly CompatColumn[] = [ + { key: 'name', label: 'Game', numeric: false, filter: 'text', value: (r) => r.name.toLowerCase(), text: (r) => r.name }, + { key: 'app_id', label: 'App id', numeric: true, filter: 'range', value: (r) => r.app_id, text: (r) => String(r.app_id) }, + { key: 'current', label: 'Current tool', numeric: false, filter: 'select', value: (r) => toolLabel(r.current), text: (r) => toolLabel(r.current) }, + { key: 'decided', label: 'Decided tool', numeric: false, filter: 'select', value: (r) => toolLabel(r.decided), text: (r) => toolLabel(r.decided) }, + { key: 'why', label: 'Reason', numeric: false, filter: 'text', value: (r) => whyText(r), text: whyText }, + // 0 sorts first: ascending (the first click) lists the games that change on top. + { key: 'changes', label: 'Changes', numeric: false, filter: 'select', value: (r) => (r.changes ? 0 : 1), text: changeOption }, +] + +const BY_KEY = Object.fromEntries(COMPAT_COLUMNS.map((c) => [c.key, c])) as Record + +export interface CompatState { + sort: CompatKey + asc: boolean + /** Raw filter values: `` for text/select, `app_id.min` / `app_id.max`. */ + filters: Map + query: string +} + +/** Games that change first: what an apply would do is the point of the view. */ +export const defaultCompat = (): CompatState => ({ sort: 'changes', asc: true, filters: new Map(), query: '' }) + +/** Header click: flip the direction, or switch column (text Aโ†’Z, numbers highโ†’low). */ +export function toggleCompatSort(s: CompatState, key: CompatKey): CompatState { + if (key === s.sort) return { ...s, asc: !s.asc } + return { ...s, sort: key, asc: !BY_KEY[key].numeric } +} + +const bound = (raw: string | undefined): number | null => { + const t = raw?.trim() ?? '' + const n = Number(t) + return t === '' || !Number.isFinite(n) ? null : n +} + +function passes(r: CompatRow, c: CompatColumn, filters: ReadonlyMap): boolean { + if (c.filter === 'range') { + const min = bound(filters.get(`${c.key}.min`)) + const max = bound(filters.get(`${c.key}.max`)) + const v = c.value(r) as number + return (min === null || v >= min) && (max === null || v <= max) + } + const want = (filters.get(c.key) ?? '').trim().toLowerCase() + if (!want) return true + const have = c.text(r).toLowerCase() + return c.filter === 'select' ? have === want : have.includes(want) +} + +const compare = (a: number | string, b: number | string): number => + typeof a === 'string' ? a.localeCompare(String(b)) : a - (b as number) + +/** + * Rows matching the fuzzy search and every column filter, in the state's + * order. Ties fall back to name then app id IN THE SAME DIRECTION, so a + * column whose values are all equal still reverses on its second click. + */ +export function visibleCompat(rows: readonly CompatRow[], s: CompatState, index: ReadonlyMap): CompatRow[] { + const hits = s.query.trim() ? new Set(search(rows, index, s.query).map((r) => r.app_id)) : null + const col = BY_KEY[s.sort] + const sign = s.asc ? 1 : -1 + return rows + .filter((r) => (hits === null || hits.has(r.app_id)) && COMPAT_COLUMNS.every((c) => passes(r, c, s.filters))) + .sort((a, b) => sign * (compare(col.value(a), col.value(b)) || a.name.localeCompare(b.name) || a.app_id - b.app_id)) +} + +/** Distinct options of a select column across `rows`, plus `keep` (a restored value). */ +export function compatOptions(c: CompatColumn, rows: readonly CompatRow[], keep = ''): string[] { + if (c.key === 'changes') return ['changes', 'same'] + const opts = new Set(rows.map((r) => c.text(r))) + if (keep) opts.add(keep) + return [...opts].sort((a, b) => a.localeCompare(b)) +} + +const P = 'c.' + +/** Write `s` into `p` under `c.` keys, dropping keys at their default. */ +export function writeCompat(s: CompatState, p: URLSearchParams): void { + for (const key of [...p.keys()]) if (key.startsWith(P)) p.delete(key) + const d = defaultCompat() + if (s.sort !== d.sort || s.asc !== d.asc) { + p.set(`${P}sort`, s.sort) + p.set(`${P}dir`, s.asc ? 'asc' : 'desc') + } + if (s.query.trim()) p.set(`${P}q`, s.query) + for (const c of COMPAT_COLUMNS) { + if (c.filter === 'range') { + const min = s.filters.get(`${c.key}.min`)?.trim() ?? '' + const max = s.filters.get(`${c.key}.max`)?.trim() ?? '' + if (min || max) p.set(`${P}f.${c.key}`, `${min}..${max}`) + } else { + const v = s.filters.get(c.key) ?? '' + if (v) p.set(`${P}f.${c.key}`, v) + } + } +} + +export function readCompat(p: URLSearchParams): CompatState { + const s = defaultCompat() + const sort = COMPAT_COLUMNS.find((c) => c.key === p.get(`${P}sort`)) + if (sort) { + s.sort = sort.key + s.asc = p.get(`${P}dir`) !== 'desc' + } + s.query = p.get(`${P}q`) ?? '' + for (const c of COMPAT_COLUMNS) { + const raw = p.get(`${P}f.${c.key}`) + if (raw === null) continue + if (c.filter === 'range') { + const [min = '', max = ''] = raw.split('..') + if (min) s.filters.set(`${c.key}.min`, min) + if (max) s.filters.set(`${c.key}.max`, max) + } else { + s.filters.set(c.key, raw) + } + } + return s +} diff --git a/web/src/library/compatTable.test.ts b/web/src/library/compatTable.test.ts new file mode 100644 index 0000000..36b5f95 --- /dev/null +++ b/web/src/library/compatTable.test.ts @@ -0,0 +1,179 @@ +import { screen, within } from '@testing-library/dom' +import userEvent from '@testing-library/user-event' +import { afterEach, describe, expect, it } from 'vitest' +import type { CompatPayload } from '../api/contract' +import { q } from '../api/queries' +import { runner, type RunRequest } from '../lib/runner' +import { makeCompatRow, makeInstalled, makeLibGame, makeSpec } from '../test/fixtures' +import { apiError, flush, mount, stubApi, type Handler } from '../test/harness' +import '../sections/library' +import './compatTable' + +const TOOL = 'proton-cachyos-native' +const payload = (rows = ROWS): CompatPayload => ({ tool: TOOL, tool_ready: true, rows }) +const ROWS = [ + makeCompatRow({ app_id: 292030, name: 'The Witcher 3' }), + makeCompatRow({ app_id: 70, name: 'Half-Life', decided: null, map: false, changes: false, reasons: ['native'] }), + makeCompatRow({ app_id: 1091500, name: 'Cyberpunk 2077' }), + makeCompatRow({ app_id: 413150, name: 'Stardew Valley', current: TOOL, priority: '250', changes: false, note: 'already mapped' }), +] + +const at = (url: string) => { + history.replaceState(null, '', url) +} +const region = () => screen.getByRole('region', { name: 'Proton map' }) +const bodyNames = () => [...region().querySelectorAll('tbody tr')].map((tr) => tr.querySelector('td.name')?.textContent) +const applyBtn = () => screen.getByRole('button', { name: /^Map \d+ games$/ }) + +async function show(compat: Handler = payload()): Promise { + stubApi({ 'GET /api/compat': compat }) + mount('') + await screen.findByRole('region', { name: 'Proton map' }) + const seen: RunRequest[] = [] + runner.onRun((r) => seen.push(r)) + return seen +} + +describe('', () => { + afterEach(() => { + at('/') + }) + + it('shows every game, the changing ones first, Valve default as an icon', async () => { + await show() + expect(bodyNames()).toEqual(['Cyberpunk 2077', 'The Witcher 3', 'Half-Life', 'Stardew Valley']) + const halfLife = within(region()).getByRole('row', { name: /Half-Life/ }) + expect(within(halfLife).getAllByRole('img', { name: 'Valve default' })).toHaveLength(2) + expect(halfLife).toHaveTextContent('native') + expect(within(halfLife).getByRole('img', { name: 'Unchanged' })).toBeInTheDocument() + expect(region()).toHaveTextContent(TOOL) + expect(region().textContent).not.toMatch(/\[object Object\]|undefined|NaN/) + }) + + it('sorts on every header click, flips on the second, and keeps it in the hash', async () => { + await show() + const heads = [...region().querySelectorAll('thead th.clickable')] + expect(heads).toHaveLength(6) + for (const th of heads) { + const before = bodyNames().join() + await userEvent.click(th) + const once = bodyNames().join() + await userEvent.click(th) + expect([once, bodyNames().join()].some((order) => order !== before)).toBe(true) + expect(th).toHaveAttribute('aria-sort') + } + expect(location.hash).toContain('c.sort=changes') + }) + + it('restores sort, filters and search from the hash', async () => { + at('/library/#view=compat&c.sort=name&c.dir=asc&c.q=wicher') + await show() + expect(bodyNames()).toEqual(['The Witcher 3']) + expect(screen.getByRole('searchbox', { name: 'Search games' })).toHaveValue('wicher') + }) + + it('filters by a column select and by search', async () => { + await show() + await userEvent.selectOptions(screen.getByRole('combobox', { name: 'Filter Changes' }), 'same') + expect(bodyNames()).toEqual(['Half-Life', 'Stardew Valley']) + expect(location.hash).toContain('c.f.changes=same') + await userEvent.type(screen.getByRole('searchbox', { name: 'Filter Reason' }), 'already') + expect(bodyNames()).toEqual(['Stardew Valley']) + }) + + it('maps every visible changing game when none is selected', async () => { + const seen = await show() + expect(applyBtn()).toHaveAccessibleName('Map 2 games') + await userEvent.click(applyBtn()) + expect(seen).toEqual([{ name: 'map-installed', preset: { appids: '1091500,292030' } }]) + }) + + it('maps only the selected games, and only changing rows can be selected', async () => { + const seen = await show() + expect(screen.queryByRole('checkbox', { name: 'Select Half-Life' })).toBeNull() + await userEvent.click(screen.getByRole('checkbox', { name: 'Select The Witcher 3' })) + await userEvent.click(screen.getByRole('button', { name: 'Map 1 games' })) + expect(seen.at(-1)).toEqual({ name: 'map-installed', preset: { appids: '292030' } }) + }) + + it('follows a hand-edited hash, clears a filter and narrows by app id range', async () => { + await show() + at('/library/#c.f.changes=same') + window.dispatchEvent(new HashChangeEvent('hashchange')) + expect(bodyNames()).toEqual(['Half-Life', 'Stardew Valley']) + await userEvent.selectOptions(screen.getByRole('combobox', { name: 'Filter Changes' }), '') + expect(bodyNames()).toHaveLength(4) + expect(location.hash).not.toContain('c.f.changes') + await userEvent.type(screen.getByRole('spinbutton', { name: 'App id min' }), '400000') + expect(bodyNames()).toEqual(['Cyberpunk 2077', 'Stardew Valley']) + }) + + it('unticking the last selected game goes back to every changing game', async () => { + await show() + const box = screen.getByRole('checkbox', { name: 'Select The Witcher 3' }) + await userEvent.click(box) + expect(applyBtn()).toHaveAccessibleName('Map 1 games') + await userEvent.click(box) + expect(applyBtn()).toHaveAccessibleName('Map 2 games') + }) + + it('cannot apply when nothing would change', async () => { + await show(payload([ROWS[1], ROWS[3]])) + expect(applyBtn()).toBeDisabled() + }) + + it('refreshes rows in place without losing the search text', async () => { + const routes: Record = { 'GET /api/compat': payload() } + stubApi(routes) + mount('') + await screen.findByRole('region', { name: 'Proton map' }) + const search = screen.getByRole('searchbox', { name: 'Search games' }) + await userEvent.type(search, 'cyber') + await userEvent.click(screen.getByRole('checkbox', { name: 'Select Cyberpunk 2077' })) + routes['GET /api/compat'] = payload([makeCompatRow({ app_id: 1091500, name: 'Cyberpunk 2077', changes: false, note: 'already mapped', current: TOOL })]) + await q.compat.refetch() + await flush() + expect(screen.getByRole('searchbox', { name: 'Search games' })).toBe(search) + expect(search).toHaveValue('cyber') + expect(applyBtn()).toBeDisabled() + }) + + it('keeps a selected game that still changes after a refresh', async () => { + const routes: Record = { 'GET /api/compat': payload() } + stubApi(routes) + mount('') + await screen.findByRole('region', { name: 'Proton map' }) + await userEvent.click(screen.getByRole('checkbox', { name: 'Select The Witcher 3' })) + await q.compat.refetch() + await flush() + expect(screen.getByRole('checkbox', { name: 'Select The Witcher 3' })).toBeChecked() + expect(applyBtn()).toHaveAccessibleName('Map 1 games') + }) + + it('shows the server error', async () => { + stubApi({ 'GET /api/compat': apiError(502, 'op_failed', 'no app cache') }) + mount('') + await flush(20) + expect(document.body).toHaveTextContent('โ›”') + }) +}) + +describe('library view switch', () => { + afterEach(() => { + at('/') + }) + + it('offers the Proton map as a third view, kept in the hash', async () => { + at('/library/') + stubApi({ + 'GET /api/library': { games: [makeLibGame()] }, + 'GET /api/installed': { games: [makeInstalled()] }, + 'GET /api/commands': [makeSpec('map-installed')], + 'GET /api/compat': payload(), + }) + mount('') + await userEvent.click(screen.getByRole('button', { name: 'Proton map' })) + expect(location.hash).toContain('view=compat') + expect(await screen.findByRole('region', { name: 'Proton map' })).toBeVisible() + }) +}) diff --git a/web/src/library/compatTable.ts b/web/src/library/compatTable.ts new file mode 100644 index 0000000..51ecb74 --- /dev/null +++ b/web/src/library/compatTable.ts @@ -0,0 +1,239 @@ +// : the Proton map, every installed game's compat-tool decision +// (`GET /api/compat`, the installer's own map-installed rows) with the R3 +// table defaults: a sortable header and a filter on every column, fuzzy +// search, state in the URL hash (`c.` keys, compatState.ts) and a 30 s poll. +// Built once; a refresh repaints rows in place, so typing never loses focus. +// The apply button runs `map-installed` on the selected rows, or on every +// visible row that would change; the server checks the phrase and the +// game-running guard. Textgate: headers and marks are icons with spoken +// names and one-word hovers; only data (names, ids, tools, reasons) is text. + +import type { CompatRow } from '../api/contract' +import { q } from '../api/queries' +import type { IconId } from '../icons/registry' +import { iconText } from '../icons/iconText' +import { iconInline } from '../icons/render' +import { define, h } from '../lib/dom' +import { LiveElement } from '../lib/element' +import { runner } from '../lib/runner' +import { errorNotice, loading } from '../ui/build' +import { + COMPAT_COLUMNS, + compatOptions, + readCompat, + toggleCompatSort, + visibleCompat, + whyText, + writeCompat, + type CompatColumn, + type CompatKey, + type CompatState, +} from './compatState' +import { buildIndex, type Searchable } from './fuzzy' +import { readHash, replaceHash } from './hashState' + +const COLUMN_ICON = { + name: 'libpage.game', + app_id: 'libpage.app-id', + current: 'compat.current', + decided: 'compat.decided', + why: 'compat.why', + changes: 'compat.changes', +} as const satisfies Record + +/** A painted icon that is the cell's whole content: spoken as an image. */ +const mark = (id: IconId, spoken: string) => h('span', { role: 'img', 'aria-label': spoken }, iconInline(id)) +const toolCell = (tool: string | null) => tool ?? mark('compat.valve', 'Valve default') +// An