Files
DungeonCrawlerWorld/tests/unit/test_save_repository.gd
T

166 lines
7.7 KiB
GDScript

extends GutTest
const SaveRepository = preload("res://scripts/infrastructure/save/save_repository.gd")
const SaveMigrator = preload("res://scripts/infrastructure/save/save_migrator.gd")
const ContentCatalog = preload("res://scripts/domain/content/content_catalog.gd")
const RunState = preload("res://scripts/domain/run/run_state.gd")
const RunSettings = preload("res://scripts/domain/run/run_settings.gd")
const FloorState = preload("res://scripts/domain/world/floor_state.gd")
const GridMapState = preload("res://scripts/domain/grid_map_state.gd")
const GameState = preload("res://scripts/domain/game_state.gd")
const ActorState = preload("res://scripts/domain/actor_state.gd")
const ItemDefinition = preload("res://scripts/domain/items/item_definition.gd")
const ItemInstance = preload("res://scripts/domain/items/item_instance.gd")
const CompanionState = preload("res://scripts/domain/entities/companion_state.gd")
const PartyState = preload("res://scripts/domain/entities/party_state.gd")
const QuestState = preload("res://scripts/domain/narrative/quest_state.gd")
const GameEvent = preload("res://scripts/domain/run/game_event.gd")
var repository: SaveRepository
var catalog: ContentCatalog
func before_each() -> void:
repository = SaveRepository.new("user://g1-save-repository-tests")
repository.delete_all()
catalog = ContentCatalog.load_from_directory("res://data/ascii")
func after_each() -> void:
repository.delete_all()
func test_save_load_round_trip_preserves_full_run_state_across_multiple_floors_items_quests_party_and_meta() -> void:
var fixture := _full_fixture()
var saved := repository.save(2, fixture.run, fixture.game, fixture.dialogue_player)
assert_true(saved.ok, String(saved.get("error", "")))
assert_eq(repository.list_slots().size(), 5)
assert_true(repository.list_slots()[1].exists)
var loaded := repository.load(2, catalog)
assert_true(loaded.ok, String(loaded.get("error", "")))
var run: RunState = loaded.run_state
var game: GameState = loaded.game_state
assert_eq(run.seed, "G1-ROUNDTRIP")
assert_eq(run.settings.difficulty_id, &"veteran")
assert_true(run.settings.permadeath)
assert_eq(run.current_floor, 2)
assert_eq(run.current_turn, 17)
assert_eq(_sorted_keys(run.floors), [0, 2])
assert_eq(run.floors[2].chests[Vector2i(2, 2)].opened, true)
assert_eq(run.floors[2].ground_items[Vector2i(3, 2)][0].instance_id, &"ground-sword")
assert_eq(run.inventory.item_by_id(&"pack-sword").quantity, 2)
assert_eq(run.equipment.item_in_slot(&"main_hand").instance_id, &"equipped-sword")
assert_true(run.identification.is_identified(run.equipment.item_in_slot(&"main_hand")))
assert_eq(run.quest_records[0].quest_id, "quest_find_lighter")
assert_eq(run.achievement_state.achievement_event_counts.actor_died, 3)
assert_eq(run.audience_state.followers, 50)
assert_eq(run.recap_state.recap_episodes[0].turn, 17)
assert_eq(run.faction_war_state.faction_war_scores.borant, 2)
assert_eq(run.moderation_state.moderation_strikes, 1)
assert_eq(run.floor_deadlines[2].turns_remaining, 42)
assert_eq(run.log[0].payload.to, Vector2i(2, 2))
assert_eq(game.player.cell, Vector2i(2, 2))
assert_eq(game.player.focus, 9)
assert_eq(game.companion.choice_id, &"moth")
assert_eq(game.party.members[0].id, &"recruit-one")
assert_eq(loaded.dialogue_player.race, &"race_humain_standard")
func test_only_five_one_based_slots_are_available_and_missing_or_invalid_slots_are_explicit() -> void:
assert_eq(repository.list_slots().map(func(meta: Dictionary) -> int: return int(meta.slot)), [1, 2, 3, 4, 5])
assert_false(repository.load(1, catalog).ok)
assert_false(repository.load(0, catalog).ok)
assert_false(repository.save(6, _full_fixture().run, _full_fixture().game).ok)
func test_ascii_migration_maps_supported_legacy_data_and_reports_every_unsupported_key() -> void:
var legacy := {
"version": 7,
"seed": "ASCII-G1",
"difficulty": "veteran",
"permadeath": true,
"current_floor": 2,
"player": {"id": "player", "x": 4, "y": 5, "hp": 11, "max_hp": 15, "attack": 3},
"levels": {"0": _legacy_floor(), "2": _legacy_floor()},
"flags": {"terminal_seen": true},
"quests": {"quest_find_lighter": {"state": "active"}},
"achievements": {"first_kill": true},
"audience": {"followers": 12},
"floor_deadline": {"2": {"turns_remaining": 40}},
"companion": null,
"recruits": [],
"recap": {},
"faction_war": {},
"options": {"colors": false},
"playtime_seconds": 33.5,
"updated_at": 1234.0,
"forgotten_mod_key": {"must": "be reported"},
}
var migrated := SaveMigrator.migrate(legacy)
assert_true(migrated.ok, String(migrated.get("error", "")))
assert_eq(migrated.data.version, SaveRepository.SCHEMA_VERSION)
assert_eq(migrated.data.run.seed, "ASCII-G1")
assert_eq(migrated.data.runtime.player.cell, [4, 5])
assert_eq(_sorted_keys(migrated.data.run.floors), ["0", "2"])
assert_eq(migrated.data.run.meta.legacy_flags.terminal_seen, true)
assert_eq(migrated.report.unsupported_keys, ["forgotten_mod_key"])
assert_false(migrated.report.is_clean)
func _full_fixture() -> Dictionary:
var run := RunState.new("G1-ROUNDTRIP", RunSettings.new("G1-ROUNDTRIP", &"veteran", true, true))
run.player_name = "Archivist"
run.base_archetype = &"mystic"
run.current_floor = 2
run.current_turn = 17
run.gold = 91
run.slot_policy = &"active"
var floor_zero := _floor(0)
var floor_two := _floor(2)
run.set_floor_state(floor_zero)
run.set_floor_state(floor_two)
run.floor_deadlines[0] = run.ensure_floor_deadline(0)
run.floor_deadlines[2] = run.ensure_floor_deadline(2)
run.floor_deadlines[2].turns_remaining = 42
var sword := ItemDefinition.from_catalog(catalog, &"weapon_rusty_sword")
run.inventory.add_item(ItemInstance.new(sword, 2, &"pack-sword"))
var equipped := ItemInstance.new(sword, 1, &"equipped-sword")
run.equipment.equipped[&"main_hand"] = equipped
run.identification.reveal(equipped)
floor_two.ground_items[Vector2i(3, 2)] = [ItemInstance.new(sword, 1, &"ground-sword")]
var quest := QuestState.new(&"quest_find_lighter")
quest.status = QuestState.Status.ACTIVE
quest.objectives[&"find_lighter"] = 1
run.quest_records.append(quest.to_record())
run.achievement_state = {"achievement_event_counts": {"actor_died": 3}}
run.audience_state = {"followers": 50, "spectacle_score": 10}
run.recap_state = {"recap_episodes": [{"turn": 17, "highlights": ["A win"]}]}
run.faction_war_state = {"faction_war_scores": {"borant": 2}}
run.moderation_state = {"moderation_strikes": 1}
run.log = [GameEvent.new(17, 2, &"moved", {"to": Vector2i(2, 2)}, "Moved.", &"player", &"", Vector2i(2, 2))]
var player := ActorState.new(&"player", Vector2i(2, 2), 15, 3, &"player", {"xp": 120})
player.hit_points = 11
player.focus = 9
var game := GameState.new(floor_two.map, player, floor_two.enemies)
game.set_floor_state(floor_two)
game.companion = CompanionState.from_choice(&"moth", Vector2i(1, 2))
game.party = PartyState.new()
game.party.members.append(ActorState.new(&"recruit-one", Vector2i(2, 1), 8, 2, &"npc_recruit"))
var dialogue_player := {"race": "race_humain_standard", "archetype": "mystic", "level": 2}
return {"run": run, "game": game, "dialogue_player": dialogue_player}
func _floor(number: int) -> FloorState:
var map := GridMapState.new(Vector2i(5, 5), GridMapState.CellKind.FLOOR)
var floor := FloorState.new(map, 100 + number, number)
floor.entrance = Vector2i(0, 0)
floor.exit = Vector2i(4, 4)
floor.chests[Vector2i(2, 2)] = {"opened": true, "contents": ["test"]}
floor.explored_cells[Vector2i(1, 1)] = true
floor.metadata = {"marker": "floor-%d" % number}
return floor
func _legacy_floor() -> Dictionary:
return {"seed": 1, "floor_number": 2, "map": {"size": [1, 1], "tiles": [{"cell": [0, 0], "kind": 0, "room_id": ""}]}, "rooms": []}
func _sorted_keys(values: Dictionary) -> Array:
var keys: Array = values.keys()
keys.sort()
return keys