## Tests for GameState — character positions, object states, save/load round-trip.
extends GutTest

const GameStateScript: GDScript = preload("res://scripts/autoload/GameState.gd")

var _state: Node


func before_each() -> void:
	_state = GameStateScript.new()
	add_child_autofree(_state)


func test_get_character_position_default_is_zero() -> void:
	assert_eq(_state.get_character_position("bunny_01"), Vector2.ZERO)


func test_has_character_position_returns_false_for_unknown_id() -> void:
	assert_false(_state.has_character_position("bunny_01"))


func test_set_character_position_stores_value() -> void:
	_state.set_character_position("bunny_01", Vector2(100.0, 200.0))
	assert_eq(_state.get_character_position("bunny_01"), Vector2(100.0, 200.0))


func test_has_character_position_returns_true_after_set() -> void:
	_state.set_character_position("bunny_01", Vector2(50.0, 50.0))
	assert_true(_state.has_character_position("bunny_01"))


func test_set_character_position_emits_character_moved() -> void:
	watch_signals(_state)
	_state.set_character_position("bunny_01", Vector2(100.0, 200.0))
	assert_signal_emitted(_state, "character_moved")


func test_set_character_position_emits_state_changed() -> void:
	watch_signals(_state)
	_state.set_character_position("bunny_01", Vector2(100.0, 200.0))
	assert_signal_emitted(_state, "state_changed")


func test_get_object_state_default_is_idle() -> void:
	assert_eq(_state.get_object_state("gift_box_1"), "idle")


func test_set_object_state_stores_value() -> void:
	_state.set_object_state("gift_box_1", "open")
	assert_eq(_state.get_object_state("gift_box_1"), "open")


func test_set_object_state_emits_state_changed() -> void:
	watch_signals(_state)
	_state.set_object_state("gift_box_1", "open")
	assert_signal_emitted(_state, "state_changed")


func test_save_data_round_trip_character_position() -> void:
	_state.set_character_position("bunny_01", Vector2(100.0, 200.0))
	var data: Dictionary = _state.get_save_data()
	_state.set_character_position("bunny_01", Vector2.ZERO)
	_state.apply_save_data(data)
	assert_eq(_state.get_character_position("bunny_01"), Vector2(100.0, 200.0))


func test_save_data_round_trip_object_state() -> void:
	_state.set_object_state("gift_box_1", "open")
	var data: Dictionary = _state.get_save_data()
	var state2: Node = GameStateScript.new()
	add_child_autofree(state2)
	state2.apply_save_data(data)
	assert_eq(state2.get_object_state("gift_box_1"), "open")


func test_apply_save_data_with_empty_dict_does_not_crash() -> void:
	_state.set_character_position("bunny_01", Vector2(10.0, 20.0))
	_state.apply_save_data({})
	assert_eq(_state.get_character_position("bunny_01"), Vector2(10.0, 20.0))
