100 lines
2.7 KiB
GDScript
100 lines
2.7 KiB
GDScript
extends GdUnitTestSuite
|
|
|
|
## NEO-97: `PlayerCombatHealthClient` GET parse.
|
|
|
|
const PlayerCombatHealthClient := preload("res://scripts/player_combat_health_client.gd")
|
|
|
|
|
|
class MockHttpTransport:
|
|
extends Node
|
|
signal request_completed(
|
|
result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray
|
|
)
|
|
var last_url: String = ""
|
|
var response_code: int = 200
|
|
var body_json: String = ""
|
|
|
|
func request(
|
|
url: String,
|
|
_custom_headers: PackedStringArray = PackedStringArray(),
|
|
_method: HTTPClient.Method = HTTPClient.METHOD_GET,
|
|
_request_data: String = ""
|
|
) -> Error:
|
|
last_url = url
|
|
request_completed.emit(
|
|
HTTPRequest.RESULT_SUCCESS,
|
|
response_code,
|
|
PackedStringArray(),
|
|
body_json.to_utf8_buffer()
|
|
)
|
|
return OK
|
|
|
|
|
|
static func _health_json() -> String:
|
|
return (
|
|
'{"schemaVersion":1,"playerId":"dev-local-1","maxHp":100,'
|
|
+ '"currentHp":75,"defeated":false}'
|
|
)
|
|
|
|
|
|
func _make_client(transport: Node) -> Node:
|
|
var c: Node = PlayerCombatHealthClient.new()
|
|
c.set("injected_http", transport)
|
|
c.set("dev_player_id", "dev-local-1")
|
|
auto_free(transport)
|
|
auto_free(c)
|
|
add_child(c)
|
|
return c
|
|
|
|
|
|
func test_request_sync_gets_combat_health_url() -> void:
|
|
# Arrange
|
|
var transport := MockHttpTransport.new()
|
|
transport.body_json = _health_json()
|
|
var c := _make_client(transport)
|
|
# Act
|
|
c.call("request_sync_from_server")
|
|
# Assert
|
|
assert_that(transport.last_url).contains("/game/players/dev-local-1/combat-health")
|
|
|
|
|
|
func test_health_received_emits_parsed_snapshot() -> void:
|
|
# Arrange
|
|
var transport := MockHttpTransport.new()
|
|
transport.body_json = _health_json()
|
|
var c := _make_client(transport)
|
|
var got: Array = []
|
|
c.connect("health_received", func(snapshot: Dictionary) -> void: got.append(snapshot))
|
|
# Act
|
|
c.call("request_sync_from_server")
|
|
# Assert
|
|
assert_that(got.size()).is_equal(1)
|
|
var snapshot: Dictionary = got[0]
|
|
assert_that(int(snapshot.get("currentHp", 0))).is_equal(75)
|
|
assert_that(int(snapshot.get("maxHp", 0))).is_equal(100)
|
|
assert_that(bool(snapshot.get("defeated", false))).is_false()
|
|
|
|
|
|
func test_parse_combat_health_json_rejects_missing_hp_fields() -> void:
|
|
# Arrange
|
|
var text := '{"schemaVersion":1,"playerId":"dev-local-1","defeated":false}'
|
|
# Act
|
|
var parsed: Variant = PlayerCombatHealthClient.parse_combat_health_json(text)
|
|
# Assert
|
|
assert_that(parsed).is_null()
|
|
|
|
|
|
func test_http_404_emits_sync_failed() -> void:
|
|
# Arrange
|
|
var transport := MockHttpTransport.new()
|
|
transport.response_code = 404
|
|
transport.body_json = ""
|
|
var c := _make_client(transport)
|
|
var failed: Array = []
|
|
c.connect("health_sync_failed", func(reason: String) -> void: failed.append(reason))
|
|
# Act
|
|
c.call("request_sync_from_server")
|
|
# Assert
|
|
assert_that(failed.size()).is_equal(1)
|
|
assert_that(str(failed[0])).contains("404")
|