extends GdUnitTestSuite

## NEO-86: `GigProgressionClient` GET parse + failure signals.

const GigProgressionClient := preload("res://scripts/gig_progression_client.gd")

var _progression_capture: Dictionary = {}


func _capture_progression(snapshot: Dictionary) -> void:
	_progression_capture = snapshot


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 _breach_progression_json() -> String:
	return (
		'{"schemaVersion":1,"playerId":"dev-local-1","mainGigId":"breach",'
		+ '"gigs":[{"id":"breach","xp":25,"level":1}]}'
	)


func _make_client(transport: Node) -> Node:
	var c: Node = GigProgressionClient.new()
	c.set("injected_http", transport)
	auto_free(transport)
	auto_free(c)
	add_child(c)
	return c


func test_parse_progression_json_reads_breach_row() -> void:
	# Arrange
	var json := _breach_progression_json()
	# Act
	var snapshot: Variant = GigProgressionClient.parse_progression_json(json)
	# Assert
	assert_that(snapshot is Dictionary).is_true()
	var c: Node = GigProgressionClient.new()
	auto_free(c)
	var row: Dictionary = c.call("gig_row", "breach", snapshot as Dictionary)
	assert_that(int(row.get("xp", -1))).is_equal(25)
	assert_that(int(row.get("level", -1))).is_equal(1)


func test_request_sync_emits_progression_received() -> void:
	# Arrange
	_progression_capture = {}
	var transport := MockHttpTransport.new()
	transport.body_json = _breach_progression_json()
	var c := _make_client(transport)
	c.connect("progression_received", Callable(self, "_capture_progression"))
	monitor_signals(c)
	# Act
	c.call("request_sync_from_server")
	# Assert
	await assert_signal(c).is_emitted("progression_received", any())
	assert_that(_progression_capture.get("mainGigId", "")).is_equal("breach")
	assert_that(transport.last_url).contains("/gig-progression")


func test_http_404_emits_progression_sync_failed() -> void:
	# Arrange
	var transport := MockHttpTransport.new()
	transport.response_code = 404
	var c := _make_client(transport)
	monitor_signals(c)
	# Act
	c.call("request_sync_from_server")
	# Assert
	await assert_signal(c).is_emitted("progression_sync_failed", "HTTP 404 (player unknown)")


func test_invalid_schema_emits_progression_sync_failed() -> void:
	# Arrange
	var transport := MockHttpTransport.new()
	transport.body_json = '{"schemaVersion":2,"mainGigId":"breach","gigs":[]}'
	var c := _make_client(transport)
	monitor_signals(c)
	# Act
	c.call("request_sync_from_server")
	# Assert
	await assert_signal(c).is_emitted(
		"progression_sync_failed", "non-JSON body or schemaVersion mismatch"
	)
