Files
settled-reach/client/tests/test_snapshot_parsing.gd
T
jpmschweitzerandClaude Opus 4.6 bc9a691bc1 fix(client): address PR #4 review feedback
Hoshe critical fixes:
- _action_enum_to_wire uses InputMapper.Action constants instead of
  fragile integer literals; OPEN_MENU explicitly handled as client-only
- Remove int() coercion on tick/entity_id — use direct assignment since
  GDScript int is signed 64-bit (safe for realistic tick values)
- Check encode result before buffering in send_input() — reject empty
  bytes instead of corrupting the outbound stream
- Test snapshot now uses Protocol format {tick, entities} instead of
  legacy schema; GameState updated to derive player position from
  entity data; main.gd and world_renderer.gd updated accordingly

Hoshe warnings:
- 5 negative tests added (truncated bytes, wrong type, missing fields,
  empty bytes, encode validation) — 20/20 tests pass
- receive_bytes signal is emitted at consume time in poll_snapshot by
  design (documented in code)

Tyre suggestions:
- Remove duplicated root-level fixtures — single source of truth in
  client/tests/fixtures/msgpack/
- gen_fixtures.rs writes directly to client/ directory
- Add `make fixtures` target for regeneration

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 19:32:49 +01:00

67 lines
2.4 KiB
GDScript

## D-030 Layer 1: Fixture-based tests for snapshot parsing
## Validates that GameState correctly parses Protocol-format ObserverSnapshot data
class_name TestSnapshotParsing
extends GdUnitTestSuite
# Valid snapshot in Protocol format (matches Protocol.decode_snapshot output)
var _valid_snapshot: Dictionary = {
"tick": 1,
"entities": [
{"entity_id": 1, "x": 10.0, "y": 15.0, "z": 0, "kind": {"variant": "Npc", "data": null}},
{"entity_id": 2, "x": 5.0, "y": 20.0, "z": 0, "kind": {"variant": "Npc", "data": null}},
],
}
func test_apply_valid_snapshot() -> void:
GameState.player_entity_id = 1
GameState.apply_snapshot(_valid_snapshot)
assert_that(GameState.current_tick).is_equal(1)
assert_that(GameState.player_position).is_equal(Vector2(10, 15))
assert_that(GameState.visible_entities.size()).is_equal(2)
func test_empty_snapshot_no_crash() -> void:
# Reset state
GameState.player_position = Vector2.ZERO
GameState.visible_entities = []
GameState.apply_snapshot({})
# State should remain at defaults
assert_that(GameState.player_position).is_equal(Vector2.ZERO)
assert_that(GameState.visible_entities.size()).is_equal(0)
func test_no_player_entity_position_unchanged() -> void:
GameState.player_entity_id = 999 # No entity with this ID
GameState.player_position = Vector2(5, 5)
GameState.apply_snapshot(_valid_snapshot)
# Position stays at previous value since no matching entity
assert_that(GameState.player_position).is_equal(Vector2(5, 5))
func test_missing_fields_partial_update() -> void:
# First apply valid snapshot
GameState.player_entity_id = 1
GameState.apply_snapshot(_valid_snapshot)
assert_that(GameState.player_position).is_equal(Vector2(10, 15))
# Apply snapshot with no entities — player position unchanged (no matching entity)
GameState.apply_snapshot({"tick": 2})
assert_that(GameState.player_position).is_equal(Vector2(10, 15))
assert_that(GameState.current_tick).is_equal(2)
func test_sim_bridge_test_snapshot_deterministic() -> void:
SimBridge._test_tick = 0
var snap1 = SimBridge._test_snapshot()
var snap2 = SimBridge._test_snapshot()
assert_that(snap1.tick).is_equal(1)
assert_that(snap2.tick).is_equal(2)
# Snapshot matches Protocol format
assert_that(snap1.has("tick")).is_true()
assert_that(snap1.has("entities")).is_true()
assert_that(snap1.entities.size()).is_greater(0)
assert_that(snap1.entities[0].has("entity_id")).is_true()
assert_that(snap1.entities[0].has("kind")).is_true()