//! Regression test: 64-char node_ids (Iroh `EndpointId` hex) must work
//! through the `advertise()` / `enable_addr_auto` path that
//! peat-mesh-node and the consolidated quickstart use.
//!
//! Before the DNS-label fix in this PR, building `instance_name` as
//! `format!("peat-{node_id}")` produced a 69-octet label for a 64-char
//! hex id, violating RFC 1035 §2.3.4 (63-octet DNS label limit), and
//! `mdns_sd::ServiceDaemon` silently rejected the registration. The
//! receive side never saw a `ServiceResolved` for the offending peer.
//!
//! `#[ignore]` for the same multicast-reliability reasons as the
//! companion `mdns_advertise_with_addr` test.

use peat_mesh::discovery::{DiscoveryStrategy, MdnsDiscovery};
use std::time::{Duration, Instant};

#[tokio::test]
#[ignore = "requires working multicast; run with --ignored on bare metal"]
async fn long_node_id_round_trips_through_advertise() {
    let mut a = MdnsDiscovery::new().unwrap();
    let mut b = MdnsDiscovery::new().unwrap();
    a.start().await.unwrap();
    b.start().await.unwrap();
    let long_a = "a".repeat(64);
    let long_b = "b".repeat(64);
    a.advertise(&long_a, 39001, None).unwrap();
    b.advertise(&long_b, 39002, None).unwrap();

    let deadline = Instant::now() + Duration::from_secs(8);
    loop {
        let ap = a.discovered_peers().await;
        let bp = b.discovered_peers().await;
        if ap.iter().any(|p| p.node_id == long_b) && bp.iter().any(|p| p.node_id == long_a) {
            return;
        }
        if Instant::now() > deadline {
            panic!("64-char node_id discovery timed out: A peers {ap:?} B peers {bp:?}");
        }
        tokio::time::sleep(Duration::from_millis(200)).await;
    }
}
