"""Focused bridge-level v2 projection and observation-boundary tests.""" from concurrent.futures import ThreadPoolExecutor from copy import deepcopy import json import random from types import SimpleNamespace as NS import numpy as np import pytest from src.commander.bridge import CommanderBridge, sonar_pcm_s16le from src.commander.projections import ROLE_NAMES from src.core import config from src.core.game import Game from src.sonar.sonar import Contact class Server: """Compact detached v2 publication target shared by projection tests.""" connected = True def __init__(self): self.v2_states = None self.v2_charts = None self.proposals = None self.events = None self.simlogs = None self.revocations = 0 @property def state(self): # A few projection tests only need the common version field. return self.v2_states["sonar"] def publish_v2(self, states, charts): json.dumps(states, allow_nan=False) json.dumps(charts, allow_nan=False) self.v2_states = deepcopy(states) # The real server serialises each role's chart to its own bytes, so a # chart object shared between roles is still independent once published. self.v2_charts = {role: deepcopy(chart) for role, chart in charts.items()} def publish_proposals_v2(self, **payload): self.proposals = deepcopy(payload) def publish_events_v2(self, **payload): self.events = deepcopy(payload) def publish_simlog_v2(self, **payload): self.simlogs = deepcopy(payload) def drain_commands_v2(self): return [] def authority_current_v2(self, _envelope): return False def invalidate_v2_commands(self, _reason): pass def revoke(self): self.revocations += 1 self.connected = False @pytest.fixture def game(): current = Game(seed=31, start_menu=False, audio_enabled=False, language="en") yield current current.audio.shutdown() def observe(game, key="U-7654321", kind="UNKNOWN", source="SONAR-BRG", now=None): return game.air_picture.observe( track_id=key, kind=kind, target_id=int(key.split("-")[1]), source=source, bearing=90.0, range_nm=None, observer_x=game.ship.x, observer_y=game.ship.y, course=None, quality=.8, now=game.sim_t if now is None else now, label="K7") def contact(game): current = Contact(7, 7654321, "passiv", "sub") current.update_passive(90.0, .8, .8, "hidden producer label", game.sim_t) game.sonar.contacts[current.target_id] = current observe(game) return current def publish(game, now=100.0): bridge, server = CommanderBridge(), Server() assert bridge.allowed bridge.pump(game, server, now=now) return bridge, server def test_sonar_pcm_conversion_is_exact_deterministic_and_does_not_mutate(): samples = np.zeros(1024, dtype=np.float32) samples[:8] = [0.0, 0.5, -0.5, 0.75, 1.0, -1.0, np.nan, np.inf] original = samples.copy() first = sonar_pcm_s16le(samples) assert first == sonar_pcm_s16le(samples) and len(first) == 2048 assert np.array_equal(samples, original, equal_nan=True) assert np.frombuffer(first, dtype="= 2 and server.discontinuities == [] published = len(server.audio) game.set_sonar_listen_bearing(receiver_bearing := 135.0) assert receiver.sequence >= 0 and not receiver.blocks_since(-1) for _ in range(3): game.update(.25) now += .25 bridge.pump(game, server, now=now) assert len(server.audio) > published assert server.discontinuities == ["sonar"] assert game.sonar.listen_bearing == receiver_bearing