#![cfg(feature = "discovery")]

use rs_zero::discovery::{
    Discovery, InstanceEndpoint, InstanceSelector, MemoryRegistry, Registry, RoundRobinSelector,
    ServiceInstance, StaticDiscovery,
};

#[tokio::test]
async fn discovery_adapters_cover_static_memory_and_selection() {
    let endpoint = InstanceEndpoint::new("127.0.0.1", 8080).expect("endpoint");
    let first = ServiceInstance::new("api", "api-1", endpoint.clone());
    let second = ServiceInstance::new("api", "api-2", endpoint);

    let static_discovery = StaticDiscovery::new()
        .with_instance(first.clone())
        .with_instance(second.clone());
    assert_eq!(
        static_discovery
            .discover("api")
            .await
            .expect("static")
            .len(),
        2
    );

    let registry = MemoryRegistry::new();
    registry.register(first).await.expect("register first");
    registry.register(second).await.expect("register second");
    let instances = registry.discover("api").await.expect("memory");

    let selector = RoundRobinSelector::new();
    assert_eq!(
        selector.select("api", &instances).expect("select").service,
        "api"
    );

    registry
        .deregister("api", "api-1")
        .await
        .expect("deregister");
    assert_eq!(registry.discover("api").await.expect("remaining").len(), 1);
}
