## Tests for HoldableItem — hand slot attachment on drag release.
extends GutTest


func test_holdable_item_id_default_empty() -> void:
	var item: HoldableItem = HoldableItem.new()
	add_child_autofree(item)
	assert_eq(item.item_id, "")


func test_holdable_item_is_not_in_hand_initially() -> void:
	var item: HoldableItem = HoldableItem.new()
	add_child_autofree(item)
	assert_false(item.is_in_hand_slot())


func test_holdable_item_attaches_to_nearest_free_hand() -> void:
	var character: Character = preload("res://scenes/characters/Character.tscn").instantiate() as Character
	add_child_autofree(character)
	var item: HoldableItem = HoldableItem.new()
	add_child_autofree(item)
	item.item_id = "test_item"
	item.global_position = character.get_node("HandLeft").global_position
	item._on_drag_released(item.global_position)
	assert_true(item.is_in_hand_slot())


func test_holdable_item_does_not_attach_if_no_character_in_range() -> void:
	var item: HoldableItem = HoldableItem.new()
	add_child_autofree(item)
	item.global_position = Vector2(9999.0, 9999.0)
	item._on_drag_released(item.global_position)
	assert_false(item.is_in_hand_slot())


func test_holdable_item_does_not_attach_to_occupied_hand() -> void:
	var character: Character = preload("res://scenes/characters/Character.tscn").instantiate() as Character
	add_child_autofree(character)
	var item1: Node2D = Node2D.new()
	var item2: HoldableItem = HoldableItem.new()
	add_child_autofree(item1)
	add_child_autofree(item2)
	character.attach_item("left", item1)
	var item_filler: Node2D = Node2D.new()
	add_child_autofree(item_filler)
	character.attach_item("right", item_filler)
	item2.global_position = character.global_position
	item2._on_drag_released(item2.global_position)
	assert_false(item2.is_in_hand_slot())


func test_holdable_item_detaches_on_pickup_when_in_slot() -> void:
	var character: Character = preload("res://scenes/characters/Character.tscn").instantiate() as Character
	add_child_autofree(character)
	var item: HoldableItem = HoldableItem.new()
	add_child_autofree(item)
	character.attach_item("left", item)
	assert_true(item.is_in_hand_slot())
	item._on_drag_picked_up(item.global_position)
	assert_false(item.is_in_hand_slot())


func test_holdable_item_detach_preserves_global_position() -> void:
	var character: Character = preload("res://scenes/characters/Character.tscn").instantiate() as Character
	add_child_autofree(character)
	var item: HoldableItem = HoldableItem.new()
	add_child_autofree(item)
	character.attach_item("left", item)
	var hand_pos: Vector2 = character.get_node("HandLeft").global_position
	item._on_drag_picked_up(hand_pos)
	assert_eq(item.global_position, hand_pos)
