Files
uconsole_asw_game/tests/test_game_integration.py
T
db9979andClaude Opus 5.5 42bc571c61 Audible sonar echoes, exact HQ threat intel, Remote Crew behind a proxy
Sonar echo:
- Every active return is audible when it arrives, after the two-way travel
  time the simulation already models (ship sonar, dipping sonar, wrecks).
  CW returns are a smeared carrier tone, LFM returns a short 100 Hz sweep;
  the pulse is the one the ping was sent with (audio-only memory, never
  saved), loudness follows the measured echo SNR and faint returns sit in
  reverberation hiss. The ping bus queues an echo behind a busy sound.
- Browser cues sonar_echo_cw/_lfm (+ _faint) with matching synthesis.

HQ intelligence:
- New start-report setting: coarse (bearing/range, as before) or exact
  (every hostile unit type with count and expected air-raid waves, analyser
  names). Patrol is exact, Double hunt and Nuclear intercept coarse, the
  free hunt chooses on its difficulty screen. Kept out of the saved
  difficulty set, so saves stay exact v14.

Remote Crew behind an HTTPS proxy:
- The local game accepts --public-origin https://... (crew and solo): the
  F9 listener then answers on its LAN URL and on exactly that proxy host
  (passed through, rewritten to the upstream, or with :443) and origin;
  cookies are Secure only on the HTTPS path. Without the option nothing
  changes; a proxy with its own host name still gets 403. F9 and the solo
  screen show both addresses. Header values may contain HTAB.
- Checked end to end with Chromium through a TLS proxy: crew and solo,
  direct and proxied, pair and reach the live station view.
- docs/web-host.de.md: local proxy section; no pause and saves v14 (the
  page still claimed a host-absence pause and v10).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 20:15:18 +02:00

458 lines
19 KiB
Python

import random
import json
from unittest.mock import Mock
import numpy as np
import pygame
import pytest
import src.core.game as game_module
from src.core import config
from src.core.game import Game
from src.core.station import Station
from src.core.i18n import RawText, Translator, localize, message
from src.core.mission_definition import default_mission
from src.core.preferences import Preferences
from src.enemies.surface import SurfaceShip
from src.enemies.sub import Sub
from src.sonar.sonar import Contact
def test_mixer_preinit_precedes_pygame_init(monkeypatch):
calls = []
pre_init = pygame.mixer.pre_init
init = pygame.init
monkeypatch.setattr(pygame.mixer, "pre_init",
lambda *args, **kwargs: (calls.append(("pre", kwargs)),
pre_init(*args, **kwargs))[1])
monkeypatch.setattr(pygame, "init",
lambda: (calls.append(("init", {})), init())[1])
game = Game(seed=80, audio_enabled=False)
assert [name for name, _ in calls[:2]] == ["pre", "init"]
assert calls[0][1] == {
"frequency": game_module.config.AUDIO_SAMPLE_RATE,
"size": -16,
"channels": game_module.config.AUDIO_CHANNELS,
"buffer": game_module.config.AUDIO_MIXER_BUFFER_SAMPLES,
}
game.audio.shutdown()
def test_update_passes_unclamped_wall_dt_to_audio(monkeypatch):
"""Der Main-Loop klemmt Sim-dt auf 0.1 s; die Audio-Cadence darf nicht."""
game = Game(seed=84, audio_enabled=False)
seen = []
debug = Mock()
monkeypatch.setattr(game.audio, "debug_log", debug)
monkeypatch.setattr(game, "_update_audio", seen.append)
game.update(0.1, audio_dt=0.35)
assert seen == [0.35]
game.update(0.1)
assert seen == [0.35, 0.1]
assert [call.args[0] for call in debug.call_args_list] == [0.35, 0.1]
game.audio.shutdown()
def test_frame_dt_catches_up_slow_frames_within_bounds():
game = Game(seed=86, audio_enabled=False)
game._frame_clock_reset = False
walls = [1 / 30] * 5 + [.35] + [1 / 30] * 10
dts = [game._frame_dt(wall) for wall in walls]
assert max(dts) <= config.SIM_FRAME_DT_MAX + 1e-12
# The spike is fully caught up within a few frames; nothing is lost.
assert sum(dts) == pytest.approx(sum(walls))
assert game._sim_debt_s == pytest.approx(0.0)
assert game._sim_dropped_s == 0.0
# A hang beyond the catch-up bound drops only the excess.
assert game._frame_dt(3.0) == pytest.approx(config.SIM_FRAME_DT_MAX)
assert game._sim_dropped_s == pytest.approx(3.0 - config.SIM_CATCHUP_MAX_S)
assert game._sim_debt_s == pytest.approx(
config.SIM_CATCHUP_MAX_S - config.SIM_FRAME_DT_MAX)
for bad in (float("nan"), float("inf"), -1.0, None):
assert game._frame_dt(bad) >= 0.0
game.audio.shutdown()
def test_frame_dt_never_catches_up_across_menu_or_load(monkeypatch):
game = Game(seed=87, audio_enabled=False)
game._frame_clock_reset = False
game._frame_dt(.5)
assert game._sim_debt_s > 0
game.in_menu = True
game.update(game._frame_dt(1 / 30))
game.in_menu = False
# Menu wall time is not simulated afterwards.
assert game._frame_dt(2.0) == pytest.approx(config.SIM_FRAME_DT_MAX)
assert game._sim_debt_s == 0.0
game._frame_dt(.5)
game.reset(88)
assert game._frame_dt(.5) == pytest.approx(config.SIM_FRAME_DT_MAX)
assert game._sim_debt_s == 0.0
game.audio.shutdown()
def test_frame_rate_option_cycles_and_persists(tmp_path):
game = Game(seed=89, audio_enabled=False)
assert game.frame_rate() == config.FPS_DEFAULT == 30
game._open_administration("options")
game.options_sel = game._OPTION_ROWS.index("frame_rate")
game.handle_event(pygame.event.Event(pygame.KEYDOWN, key=pygame.K_RIGHT))
assert game.preferences.frame_rate == 60 and game.frame_rate() == 60
game.handle_event(pygame.event.Event(pygame.KEYDOWN, key=pygame.K_LEFT))
assert game.frame_rate() == 30
game.draw_options_overlay()
game.audio.shutdown()
def test_listen_bearing_change_keeps_sonar_audio_stream(monkeypatch):
game = Game(seed=90, audio_enabled=False)
game.station = Station.SONAR
game.sonar_audio_enabled = True
stops = Mock()
monkeypatch.setattr(game.audio, "stop_sonar", stops)
game._sonar_audio_sequence = 7
assert game.set_sonar_listen_bearing(123.0) is True
game._joy_horizontal_step(1)
assert game.clear_sonar_focus() is True
stops.assert_not_called()
assert game._sonar_audio_sequence == 7
game.audio.shutdown()
def test_sonar_audio_continues_at_fixed_realtime_speed(monkeypatch):
game = Game(seed=85, audio_enabled=False)
game.station = Station.SONAR
game.sonar_audio_enabled = True
receiver = game.sonar.receiver
receiver.update([], 0, 12, .2, 2, 6)
spy = Mock(return_value=True)
monkeypatch.setattr(game.audio, "play_sonar", spy)
stop = Mock()
monkeypatch.setattr(game.audio, "stop_sonar", stop)
game._update_audio(0.25)
assert spy.call_count == 1
assert game._sonar_audio_sequence == receiver.sequence
stop.assert_called_once_with(immediate=True)
receiver.update([], 0, 12, .2, 2, 6)
game._update_audio(0.25)
assert spy.call_count == 2
assert game._sonar_audio_sequence == receiver.sequence
stop.assert_called_once()
game.audio.shutdown()
def test_legacy_time_keys_do_not_change_simulation_rate():
game = Game(seed=85, start_menu=False, audio_enabled=False)
for key in (pygame.K_z, pygame.K_x, pygame.K_LEFTBRACKET,
pygame.K_RIGHTBRACKET):
game.handle_event(pygame.event.Event(pygame.KEYDOWN, key=key, mod=0))
before = game.sim_t
game.update(.1)
assert not hasattr(game, "time_scale") and not hasattr(game, "time_scale_idx")
assert game.sim_t == pytest.approx(before + .1)
def test_perf_debug_log_is_opt_in_throttled_and_does_not_affect_sim(
monkeypatch, tmp_path):
monkeypatch.delenv("U_JAGD_PERF_DEBUG", raising=False)
disabled_root = tmp_path / "disabled"
monkeypatch.setattr(config, "SAVE_DIR", str(disabled_root))
game = Game(seed=87, start_menu=False, audio_enabled=False)
assert not game._perf_debug_enabled
game.update(.1, audio_dt=.1)
game._perf_debug_log(2.0)
assert not disabled_root.exists()
game.audio.shutdown()
monkeypatch.setenv("U_JAGD_PERF_DEBUG", "1")
debug_root = tmp_path / "debug"
monkeypatch.setattr(config, "SAVE_DIR", str(debug_root))
debug = Game(seed=88, start_menu=False, audio_enabled=False)
assert debug._perf_debug_enabled
debug.update(.1, audio_dt=.1)
debug._perf_debug_log(0.6)
assert not debug_root.exists()
debug.update(.1, audio_dt=.1)
debug._perf_debug_log(0.5)
assert debug.sim_t == pytest.approx(.2)
lines = (debug_root / "perf_debug.log").read_text().splitlines()
assert len(lines) == 1
assert "fps=2" in lines[0]
assert "sim_ms=" in lines[0] and "audio_ms=" in lines[0]
assert "commander_ms=" in lines[0] and "draw_ms=" in lines[0]
debug._perf_debug_log(0.8)
assert len((debug_root / "perf_debug.log").read_text().splitlines()) == 1
debug._perf_debug_log(0.3)
assert len((debug_root / "perf_debug.log").read_text().splitlines()) == 2
debug.audio.shutdown()
def test_audio_replacement_and_run_shutdown_old_engines(monkeypatch):
game = Game(seed=81, audio_enabled=False)
initial = game.audio
monkeypatch.setattr(initial, "shutdown", Mock(wraps=initial.shutdown))
replacement = Mock(available=False)
monkeypatch.setattr(game_module, "AudioEngine", Mock(return_value=replacement))
monkeypatch.setattr(game_module, "save_preferences", Mock())
game._set_preference("audio", True)
old = game_module.AudioEngine.return_value
assert game.audio is old
initial.shutdown.assert_called_once()
# The engine present before replacement was shut down by the transition.
# Use a second transition to make that ownership directly observable.
newer = Mock(available=False)
game_module.AudioEngine.return_value = newer
game._set_preference("audio", False)
old.shutdown.assert_called_once()
assert game._sonar_audio_sequence == -1
game.running = False
monkeypatch.setattr(pygame, "quit", Mock())
game.run()
newer.shutdown.assert_called_once()
def test_options_apply_global_tooltip_preference_and_language_state(monkeypatch):
game = Game(seed=82, audio_enabled=False,
preferences=Preferences(language="en", tooltips=True))
save = Mock()
monkeypatch.setattr(game_module, "save_preferences", save)
game.pinned_tooltip = {"title": "old", "lines": []}
game._set_preference("tooltips", False)
assert not game.tooltips_enabled and not game.preferences.tooltips
assert game.pinned_tooltip is None
save.assert_called_with(game.preferences)
game.pinned_tooltip = {"title": "old", "lines": []}
game._set_preference("language", "de")
assert game.pinned_tooltip is None
assert "Deutsch" in localize(game.msg, game.tr)
assert pygame.display.get_caption()[0] == game.tr("app.title")
def test_runtime_notices_are_structured_for_bt_listening_and_launches(monkeypatch):
game = Game(seed=86, audio_enabled=False)
game.station = game_module.Station.SONAR
game.sonar.bt_profile = {"thermocline_m": 75.0}
monkeypatch.setattr(game.sonar, "measure_environment", lambda *_: True)
game.handle_event(pygame.event.Event(pygame.KEYDOWN, key=pygame.K_e, mod=0))
assert game.msg["__u_jagd_i18n__"] == "runtime.bt.measured"
assert game.feed.entries[-1].text["__u_jagd_i18n__"] == "runtime.bt.feed"
game.handle_event(pygame.event.Event(pygame.KEYDOWN, key=pygame.K_d, mod=0))
assert game.msg["__u_jagd_i18n__"] == "runtime.listen.filtered"
game.helo.state = "HANGAR"
game.toggle_helo()
assert game.msg["__u_jagd_i18n__"] == "runtime.helo.launch"
target = game.subs[0]
contact = Contact(1, target.id, "ping", "sub")
contact.player_class = "U_BOOT"
contact.range_est = 4.0
contact.range_source = "ping"
contact.range_seen = contact.last_seen = game.sim_t
contact.bearing = 0.0
game.target = contact
game.sonar.contacts[target.id] = contact
game.launch_torpedo()
assert game.msg["__u_jagd_i18n__"] == "runtime.torpedo.launched"
assert game.feed.entries[-1].text["__u_jagd_i18n__"] == "runtime.torpedo.feed"
def test_runtime_language_switch_retranslates_feeds_and_keeps_legacy_strings(monkeypatch):
game = Game(seed=87, audio_enabled=False,
preferences=Preferences(language="en"))
monkeypatch.setattr(game_module, "save_preferences", Mock())
structured = message("runtime.helo.return")
game.feed.add("12:00", "mission", structured)
game.feed.add("12:01", "mission", "Saved")
game.messages.extend((("12:00", structured), ("12:01", "Saved")))
game._set_preference("language", "de")
assert len(game.feed.entries) >= 2
assert localize(game.feed.entries[-2].text, game.tr) == "HSP-5: Rückkehr befohlen"
assert localize(game.feed.entries[-1].text, game.tr) == "Gespeichert"
assert game.messages[-1] == ("12:01", "Saved")
def test_initial_threat_intel_is_coarse_static_and_visible_to_radio():
game = Game(seed=87, audio_enabled=False,
preferences=Preferences(language="en"))
# Patrol adds the exact force report after the coarse position cue.
report = next(text for _, text in game.messages
if text.get("__u_jagd_i18n__", "").startswith("runtime.hq.threat_"))
assert game.messages[-1][1]["__u_jagd_i18n__"] == "runtime.hq.intel_exact"
params = report["params"]
target = min(game.subs, key=lambda item: np.hypot(
item.x - game.ship.x, item.y - game.ship.y))
exact_range = np.hypot(target.x - game.ship.x, target.y - game.ship.y)
assert report["__u_jagd_i18n__"] == "runtime.hq.threat_underwater"
assert int(params["bearing"]) % 45 == 0
assert params["range"] % 5 == 0
assert abs(params["range"] - exact_range) <= 2.5
assert set(params) == {"bearing", "range"}
entry = next(item for item in game.feed.entries if item.text is report)
assert entry.category == "funk"
initial = json.dumps(report, sort_keys=True)
target.x += 100
game.update(.1)
assert json.dumps(game.messages[2][1], sort_keys=True) == initial
def test_custom_runtime_mission_text_is_opaque():
game = Game(seed=88, audio_enabled=False,
preferences=Preferences(language="de"))
game.custom_mission_definition = {"description": "Saved {verbatim}"}
game.mission.name = "Save"
assert isinstance(game.mission_name_display(), RawText)
assert localize(game.mission_name_display(), Translator("de").t) == "Save"
assert localize(game.mission_description_display(), Translator("de").t) == \
"Saved {verbatim}"
@pytest.mark.parametrize(
"objective_type,retained_type,english,german",
(("sink", "konvoi", "Sink all submarines", "Alle U-Boote versenken"),
("survive", "nuklearer_abfang", "Convoy: survive the time limit",
"Konvoi: Zeitlimit durchhalten")),
)
def test_custom_objective_and_start_feed_ignore_random_mission_type(
objective_type, retained_type, english, german):
game = Game(seed=89, audio_enabled=False,
preferences=Preferences(language="en"))
definition = default_mission("user.objective")
definition["name"] = "Saved"
definition["objective"]["type"] = objective_type
assert game.start_custom_mission(definition)
game.mission.type_key = retained_type
objective = game.mission_objective_display()
started = next(entry.text for entry in reversed(game.feed.entries)
if entry.category == "mission")
json.dumps(started)
assert localize(objective, Translator("en").t) == english
assert localize(objective, Translator("de").t) == german
assert localize(started, Translator("en").t) == \
f"Mission: Saved (Custom) - {english}"
assert localize(started, Translator("de").t) == \
f"Mission: Saved (Individuell) - {german}"
assert game.messages[-1][1]["__u_jagd_i18n__"] == "runtime.hq.threat_unknown"
def test_sensor_picture_uses_generic_evidence_not_platform_truth(monkeypatch):
game = Game(seed=83, audio_enabled=False)
monkeypatch.setattr(game.world, "land_blocks_line", lambda *args: False)
warship = SurfaceShip(game.ship.x + 4.0, game.ship.y,
random.Random(830), hostile=True)
warship.emitter = True
game.warships = [warship]
game.civilians = []
game.flights.flights = []
game.asms = []
game._update_air_picture(full_scan=True)
track = game.air_picture._tracks[f"S-{warship.id}"]
assert track.label == f"S-{warship.id}"
assert track.course is None and track.hostile is False
assert warship.name not in track.label
contact = Contact(7, warship.id, "ping", "surface")
contact.last_seen = game.sim_t
contact.confidence = contact.quality = 1.0
contact.player_class = None
contact.passive_bearing = 270.0
contact.range_est = 99.0
contact.range_source = "ping"
contact.range_seen = game.sim_t
contact.observed_x = game.ship.x + 3.0
contact.observed_y = game.ship.y - 4.0
monkeypatch.setattr(game.sonar, "update", Mock())
monkeypatch.setattr(game.sonar, "active_contacts", lambda: [contact])
game._update_sensors(.25)
sonar_track = game.air_picture._tracks[f"U-{warship.id}"]
assert (sonar_track.raw_x, sonar_track.raw_y) == pytest.approx(
(contact.observed_x, contact.observed_y))
assert sonar_track.label == "K7" and sonar_track.hostile is False
assert sonar_track.course is None
def test_surface_radar_detection_is_capped_by_geometric_horizon(monkeypatch):
"""A surface contact stays below the radar horizon until it closes, even
though it is well inside the nominal power-limited surface radar range."""
game = Game(seed=85, audio_enabled=False)
monkeypatch.setattr(game.world, "land_blocks_line", lambda *args: False)
game.civilians = []
game.flights.flights = []
game.asms = []
horizon = config.radar_horizon_nm(
config.RADAR_ANTENNA_HEIGHT_M, config.RADAR_SURFACE_TARGET_HEIGHT_M)
assert horizon < config.RADAR_SURFACE_RANGE_NM # the case this test guards
beyond_horizon = SurfaceShip(game.ship.x + horizon + 5.0, game.ship.y,
random.Random(850), hostile=True)
beyond_horizon.emitter = True
game.warships = [beyond_horizon]
game._update_air_picture(full_scan=True)
assert f"S-{beyond_horizon.id}" not in game.air_picture._tracks
within_horizon = SurfaceShip(game.ship.x + horizon - 5.0, game.ship.y,
random.Random(851), hostile=True)
within_horizon.emitter = True
game.warships = [within_horizon]
game._update_air_picture(full_scan=True)
assert f"S-{within_horizon.id}" in game.air_picture._tracks
def test_weapon_datum_uses_canonical_observed_position(monkeypatch):
game = Game(seed=84, audio_enabled=False)
target = game.subs[0]
contact = Contact(1, target.id, "ping", "sub")
contact.player_class = "U_BOOT"
contact.range_est = 12.0
contact.range_source = "ping"
contact.range_seen = contact.last_seen = game.sim_t
contact.bearing = 180.0
contact.ping_pos = (game.ship.x + 8.0, game.ship.y)
contact.tma_pos = (game.ship.x - 8.0, game.ship.y)
contact.observed_x, contact.observed_y = game.ship.x, game.ship.y - 6.0
game.target = contact
game.sonar.contacts[target.id] = contact
game.launch_torpedo()
assert (game.torpedoes[-1].guidance_x, game.torpedoes[-1].guidance_y) == (
contact.observed_x, contact.observed_y)
update = Mock(return_value=True)
monkeypatch.setattr(game.torpedoes[-1], "wire_update", update)
game._update_player_torpedoes(0.1)
update.assert_called_with(contact.observed_x, contact.observed_y)
def test_hfdf_noise_uses_sim_clock_and_is_smooth(monkeypatch):
game = Game(seed=85, audio_enabled=False)
sub = Sub(game.ship.x + 4.0, game.ship.y, 8.0, 0.0, "diesel_alt",
random.Random(85), runtime_catalog=game.runtime_catalog)
sub.endurance.phase = "RADIO"
sub.endurance.radio_left_s = 10.0
sub.x, sub.y = game.ship.x + 4.0, game.ship.y
game.subs = [sub]
monkeypatch.setattr(game.world, "land_blocks_line", lambda *args: False)
game.sim_t, game._t = 9.99, 1.0
game._update_radio_picture()
first = game.radio_picture._tracks[f"H-{sub.id}"].raw_bearing
game.radio_picture._tracks.clear()
game._t = 10000.0
game._update_radio_picture()
assert game.radio_picture._tracks[f"H-{sub.id}"].raw_bearing == first
game.radio_picture._tracks.clear()
game.sim_t = 10.01
game._update_radio_picture()
second = game.radio_picture._tracks[f"H-{sub.id}"].raw_bearing
assert abs(game_module.config.angle_diff_deg(second, first)) < .1