Files
openttd-client/tests/test_gamescript.py
T
kovagoadiandClaude f7ca395a4f
Continuous Integration / lint-and-security (pull_request) Failing after 19s
Continuous Integration / tests-and-coverage (pull_request) Successful in 24s
Put the AdminBridge GameScript under version control
The last commit noted in passing that the server-side half of the admin
GameScript channel "is not in this repo -- docker/config is gitignored --
so it has to be updated separately for any of this to work." That was true
of all nine features the README documents: list_vehicles, list_stations,
list_cargo, get_timetable, get_station, get_station_cargo, get_dispatch and
the event stream all answer from 705 lines of Squirrel that no clone could
reproduce, no reviewer could see, and CI never touched.

The bridge now lives in gamescript/AdminBridge/ with its own README, the
same arrangement docker/patches/ uses for the local JGRPP patches, and
docker-compose.yml bind-mounts it read-only over the container's
game/AdminBridge. docker/config stays ignored -- it also holds savegames,
downloaded content and generated config -- so the copy under it is now
shadowed and can be deleted. main.nut is byte-identical to what was running,
apart from the version work below.

Adds a version handshake, because the channel gives no way to tell a stale
bridge from a hung one: a bridge that does not recognise a command drops it
silently, so a client ahead of the server sees nothing but timeouts. The
bridge now answers get_version with its protocol version plus its command
and event catalogues, and get_bridge_version() raises when that is below
GS_BRIDGE_VERSION. It is opt-in rather than checked on connect: GameScripts
do not tick while the game is paused, so an automatic check would refuse to
connect to a paused server. A bridge older than 4 predates get_version
itself and can only fail by timing out, so the E2E test catches that and
reports it by name instead.

tests/test_gamescript.py gives CI a foothold on the GameScript without a
Squirrel toolchain: it parses the .nut files and pins the protocol version
across info.nut, main.nut and protocol.py, the event catalogue against
GameEventType, and the command table against the commands client.py sends.
The version has to be declared three times because a GameScript cannot read
its own info.nut at runtime -- GSController.GetVersion() returns the OpenTTD
version, not the script's.

info.nut also gains MinVersionToLoad() { return 1; }. The engine defaults it
to GetVersion(), so without it this bump would orphan every savegame pinned
to version 3: the scanner finds no compatible script and falls back with a
warning. The bridge keeps no savegame state, so any version can take over.

HandleCommand now dispatches through the same table get_version reports,
rather than an if/else chain, so the catalogue a client feature-detects
against cannot drift from what is implemented.

Co-Authored-By: Claude <[email protected]>
2026-08-31 19:06:33 +02:00

90 lines
4.1 KiB
Python

"""Checks the checked-in AdminBridge GameScript against the client that talks to it.
The bridge is Squirrel, and CI has no Squirrel toolchain, so these are not a substitute for
running it (see gamescript/AdminBridge/README.md). They cover the one class of breakage that
is invisible until a server is in front of you: the two halves of the protocol drifting apart
-- a version bumped on one side only, an event kind or a command the client uses that the
bridge does not implement, or the docker setup no longer serving the tracked copy.
"""
import re
from pathlib import Path
import pytest
from openttd.protocol import GS_BRIDGE_VERSION, GameEventType
ROOT = Path(__file__).resolve().parents[1]
GS_DIR = ROOT / "gamescript" / "AdminBridge"
INFO_NUT = (GS_DIR / "info.nut").read_text()
MAIN_NUT = (GS_DIR / "main.nut").read_text()
def _block(source, opening, closing):
"""Return the text between `opening` and the next `closing`, e.g. an array or table body."""
start = source.index(opening) + len(opening)
return source[start:source.index(closing, start)]
@pytest.mark.unit
def test_gamescript_version_matches_client():
"""info.nut, main.nut and the client must all name the same protocol version.
A GameScript cannot read its own info.nut at runtime, so main.nut duplicates the version;
this is what keeps the copy honest, and what makes a bump that misses a file fail here.
"""
info_version = int(re.search(r"function GetVersion\(\)\s*{\s*return (\d+);", INFO_NUT).group(1))
main_version = int(re.search(r"BRIDGE_VERSION = (\d+);", MAIN_NUT).group(1))
assert info_version == main_version == GS_BRIDGE_VERSION
@pytest.mark.unit
def test_gamescript_can_load_older_savegames():
"""The bridge must stay loadable by savegames that pinned an older version of it.
OpenTTD defaults MinVersionToLoad() to GetVersion(), so without an explicit override every
version bump orphans existing savegames: the engine finds no compatible script and falls
back with a warning. The bridge keeps no savegame state, so any version can take over.
"""
min_version = int(re.search(r"function MinVersionToLoad\(\)\s*{\s*return (\d+);", INFO_NUT).group(1))
assert min_version <= GS_BRIDGE_VERSION
@pytest.mark.unit
def test_gamescript_event_catalogue_matches_client_enum():
"""Every kind the bridge can push has a GameEventType, and vice versa.
GameEventType is what subscribe_events() validates against, so a kind in one list and not
the other is either an event nobody can subscribe to or a subscription the bridge rejects.
"""
catalogue = set(re.findall(r'"(\w+)"', _block(MAIN_NUT, "this.event_order = [", "];")))
# EventsDropped is emitted by the bridge itself and never subscribed to, so it is
# deliberately absent from the subscribable catalogue.
assert catalogue == {e.value for e in GameEventType} - {GameEventType.EventsDropped}
@pytest.mark.unit
def test_gamescript_implements_every_command_the_client_sends():
"""Each command in a client payload must have a handler in the bridge's dispatch table.
A command the bridge does not know is silently dropped, so the caller only sees a timeout.
The bridge may implement more than the client wraps (list_cargo currently has no method).
"""
commands = set(re.findall(r"^\s*(\w+)\s*=\s*{ handler",
_block(MAIN_NUT, "COMMANDS = {", "\n\t};"), re.MULTILINE))
client_source = (ROOT / "lib" / "openttd" / "client.py").read_text()
sent = set(re.findall(r'{"command": "(\w+)"', client_source))
assert sent, "no GameScript commands found in the client -- has the payload spelling changed?"
assert sent <= commands
@pytest.mark.unit
def test_docker_serves_the_tracked_gamescript():
"""The container must run the copy in git, not an untracked one under docker/config/.
That mount is the whole reason the tracked copy stays honest: without it the server reads a
file nobody reviews, which is how the bridge went unversioned in the first place.
"""
compose = (ROOT / "docker" / "docker-compose.yml").read_text()
assert "../gamescript/AdminBridge:" in compose