import sqlite3 from pathlib import Path import pytest from engine.character import Character from engine.character_library import CharacterLibrary from engine.defs import DefRegistry from engine.entity import EntityPosition from engine.map import Map from engine.start_screen import StartScreen, join_world_with_character from engine.tile import Tile, TileLayerInstance from engine.world import World DEFS_DIR = Path(__file__).resolve().parent.parent / "resources" / "defs" @pytest.fixture() def registry(): return DefRegistry.load(DEFS_DIR) @pytest.fixture() def library(): return CharacterLibrary(sqlite3.connect(":memory:")) def make_world(registry): field = Map("field", 10, 10, 1) for x in range(10): for y in range(10): field.set_tile(x, y, 0, Tile(flooring=TileLayerInstance("grass"))) world = World(registry=registry) world.add_map(field) return world def save_sample_character(library, registry, account_id, character_id, name, species_id="human"): character = Character( name=name, species_id=species_id, default_body_parts=registry.new_default_body_parts(species_id), ) library.save_character(account_id, character_id, character) # --- StartScreen ------------------------------------------------------------------------------- def test_load_for_account_lists_that_accounts_characters(registry, library): save_sample_character(library, registry, "acct-1", "char-1", "Vex") save_sample_character(library, registry, "acct-2", "char-2", "Other") # different account screen = StartScreen.load_for_account("acct-1", library) assert [row[1] for row in screen.characters] == ["Vex"] def test_select_sets_selected_index(registry, library): save_sample_character(library, registry, "acct-1", "char-1", "Vex") save_sample_character(library, registry, "acct-1", "char-2", "Kess") screen = StartScreen.load_for_account("acct-1", library) screen.select(1) assert screen.selected_index == 1 def test_select_out_of_range_is_ignored(registry, library): save_sample_character(library, registry, "acct-1", "char-1", "Vex") screen = StartScreen.load_for_account("acct-1", library) screen.select(5) assert screen.selected_index is None def test_selected_character_id_reads_back_the_right_character(registry, library): save_sample_character(library, registry, "acct-1", "char-1", "Vex") save_sample_character(library, registry, "acct-1", "char-2", "Kess") screen = StartScreen.load_for_account("acct-1", library) # list_characters orders by name, so "Kess" (char-2) sorts before "Vex" (char-1) screen.select(1) assert screen.selected_character_id() == "char-1" def test_selected_character_id_none_before_any_selection(registry, library): save_sample_character(library, registry, "acct-1", "char-1", "Vex") screen = StartScreen.load_for_account("acct-1", library) assert screen.selected_character_id() is None def test_refresh_reloads_the_list_and_clears_selection(registry, library): save_sample_character(library, registry, "acct-1", "char-1", "Vex") screen = StartScreen.load_for_account("acct-1", library) screen.select(0) save_sample_character(library, registry, "acct-1", "char-2", "Kess") screen.refresh(library) assert len(screen.characters) == 2 assert screen.selected_index is None # --- join_world_with_character ------------------------------------------------------------------ def test_join_world_spawns_the_loaded_character_at_the_given_position(registry, library): save_sample_character(library, registry, "acct-1", "char-1", "Vex") world = make_world(registry) character = join_world_with_character( world, library, "acct-1", "char-1", registry, EntityPosition("field", 3, 4, 0) ) assert character is not None assert character.name == "Vex" assert (character.position.map_id, character.position.x, character.position.y) == ("field", 3, 4) assert world.entities[character.entity_id] is character def test_join_world_returns_none_for_an_unknown_character(registry, library): world = make_world(registry) result = join_world_with_character( world, library, "acct-1", "no-such-char", registry, EntityPosition("field", 0, 0, 0) ) assert result is None assert world.entities == {}