use pkecs_core::{
    component::Component,
    entity::{Entity, Entities},
};

/// Asserts that [`None`] is returned from queries when there is no matching archetype.
#[test]
fn empty_query() {
    let mut entities = Entities::default();

    let opt_ref = entities.query(Test::query());
    assert!(opt_ref.is_none());

    let opt_mut = entities.query_mut(Test::query());
    assert!(opt_mut.is_none());

    let opt_disjoint_mut = entities.query_disjoint_mut([Test::query()]);
    assert!(opt_disjoint_mut[0].is_none());
}

struct Test;
impl Component for Test {}

/// Asserts that entities can be created.
#[test]
fn can_spawn_entity() {
    let mut entities = Entities::default();

    entities.spawn(CanSpawn);
    let result = entities.query(CanSpawn::query());

    assert!(result.is_some());
}

struct CanSpawn;
impl Component for CanSpawn {}

/// Asserts that entities can be mutated.
#[test]
fn can_mutate_entities() {
    const EXPECTED: i32 = 1;
    let mut entities = Entities::default();
    entities.spawn(CanMutate(0));

    let entity_mut = entities
        .query_mut(CanMutate::query())
        .unwrap()
        .first_mut::<CanMutate>()
        .unwrap();
    entity_mut.0 = EXPECTED;

    let entity_ref = entities
        .query(CanMutate::query())
        .unwrap()
        .first::<CanMutate>()
        .unwrap();

    assert_eq!(entity_ref.0, EXPECTED);
}

struct CanMutate(i32);
impl Component for CanMutate {}
