Files
DungeonCrawlerWorld/tests/unit/test_inventory.gd
T

86 lines
3.7 KiB
GDScript

extends GutTest
const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd")
const ItemDefinitionType = preload("res://scripts/domain/items/item_definition.gd")
const ItemInstanceType = preload("res://scripts/domain/items/item_instance.gd")
const InventoryStateType = preload("res://scripts/domain/inventory/inventory_state.gd")
var catalog: ContentCatalog
func before_each() -> void:
catalog = ContentCatalogType.load_from_directory("res://data/ascii")
ItemInstanceType.reset_id_sequence_for_tests()
func _definition(item_id: StringName) -> ItemDefinition:
return ItemDefinitionType.from_catalog(catalog, item_id)
func test_ascii_stackable_definition_merges_quantity_without_consuming_another_slot() -> void:
var inventory := InventoryStateType.new(1)
var bandage := _definition(&"consumable_bandage")
var first := ItemInstanceType.new(bandage, 2)
var second := ItemInstanceType.new(bandage, 3)
var first_result := inventory.add_item(first)
var second_result := inventory.add_item(second)
assert_true(bandage.stackable)
assert_true(first_result.accepted)
assert_true(second_result.accepted)
assert_eq(inventory.items.size(), 1)
assert_eq(inventory.items[0].quantity, 5)
assert_eq(inventory.items[0].instance_id, first.instance_id)
assert_eq(second_result.stack_merged_into, first.instance_id)
func test_non_stackable_ascii_definition_needs_a_free_slot_and_refuses_when_full() -> void:
var inventory := InventoryStateType.new(1)
var sword := _definition(&"weapon_rusty_sword")
assert_false(sword.stackable)
assert_true(inventory.add_item(ItemInstanceType.new(sword)).accepted)
var refused := inventory.add_item(ItemInstanceType.new(sword))
assert_false(refused.accepted)
assert_eq(refused.reason, &"capacity")
assert_eq(inventory.items.size(), 1)
func test_instances_have_stable_unique_ids_and_round_trip_with_their_definition_and_quantity() -> void:
var bandage := _definition(&"consumable_bandage")
var generated := ItemInstanceType.new(bandage, 4)
var explicit := ItemInstanceType.new(bandage, 1, &"loot-floor0-7")
var restored := ItemInstanceType.from_dict(generated.to_dict(), catalog)
assert_ne(generated.instance_id, explicit.instance_id)
assert_eq(explicit.instance_id, &"loot-floor0-7")
assert_eq(restored.instance_id, generated.instance_id)
assert_eq(restored.definition.id, &"consumable_bandage")
assert_eq(restored.quantity, 4)
func test_restored_generated_id_reserves_its_sequence_for_the_next_new_instance() -> void:
var bandage := _definition(&"consumable_bandage")
var restored := ItemInstanceType.from_dict({"instance_id": "item-00000001", "item_id": "consumable_bandage", "quantity": 1}, catalog)
var later := ItemInstanceType.new(bandage)
assert_eq(restored.instance_id, &"item-00000001")
assert_ne(later.instance_id, restored.instance_id)
func test_ground_pickup_transfers_item_then_leaves_it_when_inventory_is_at_capacity() -> void:
var inventory := InventoryStateType.new(1)
var sword := _definition(&"weapon_rusty_sword")
var floor := FloorState.new(GridMapState.new(Vector2i(3, 3)))
var cell := Vector2i(1, 1)
var first := ItemInstanceType.new(sword, 1, &"ground-first")
var second := ItemInstanceType.new(sword, 1, &"ground-second")
floor.ground_items[cell] = [first, second]
var picked := inventory.pick_up_ground_item(floor, cell)
var refused := inventory.pick_up_ground_item(floor, cell)
assert_true(picked.accepted)
assert_eq(picked.item.instance_id, &"ground-first")
assert_eq(inventory.items[0].instance_id, &"ground-first")
assert_eq(floor.ground_items[cell].size(), 1)
assert_eq(floor.ground_items[cell][0].instance_id, &"ground-second")
assert_false(refused.accepted)
assert_eq(refused.reason, &"capacity")
assert_eq(floor.ground_items[cell][0].instance_id, &"ground-second")