Files
settled-reach/client/tests/test_snapshot_parsing.gd
T
jpmschweitzerandClaude Opus 4.6 e6d6802c8d fix(client): align tests with kind-based player detection
Tests used entity_id matching to find the player, but game_state now
finds the player by kind.variant == "Player". Update test fixture to
include a Player entity and remove stale player_entity_id assignments.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-13 02:07:43 +01:00

88 lines
3.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": "Player", "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.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_position = Vector2(5, 5)
# Snapshot with only NPCs — no Player entity, so position should not change
var npc_only_snapshot := {
"tick": 1,
"entities": [
{"entity_id": 1, "x": 10.0, "y": 15.0, "z": 0, "kind": {"variant": "Npc", "data": null}},
],
}
GameState.apply_snapshot(npc_only_snapshot)
# Position stays at previous value since no Player entity found
assert_that(GameState.player_position).is_equal(Vector2(5, 5))
func test_missing_fields_partial_update() -> void:
# First apply valid snapshot (contains Player entity at 10,15)
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_los_clear_path() -> void:
# No wall between (10,10) and (10,8) — clear LOS
assert_that(SimBridge._test_has_los(Vector2i(10, 10), Vector2i(10, 8))).is_true()
func test_sim_bridge_los_blocked_by_wall() -> void:
# Wall at (12,10) blocks LOS from (10,10) to (12,9) via east path
# Direct line from (10,10) → (12,9) passes through (11,10) then (12,10) — wall
assert_that(SimBridge._test_has_los(Vector2i(10, 10), Vector2i(14, 10))).is_false()
func test_sim_bridge_los_diagonal() -> void:
# Diagonal LOS from (10,10) to (11,9) — no wall in path
assert_that(SimBridge._test_has_los(Vector2i(10, 10), Vector2i(11, 9))).is_true()
func test_sim_bridge_los_same_position() -> void:
# LOS to self is always true
assert_that(SimBridge._test_has_los(Vector2i(10, 10), Vector2i(10, 10))).is_true()
func test_sim_bridge_test_snapshot_deterministic() -> void:
SimBridge.reset_test_state()
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()