Documentation
use super::*;

struct IsolatedBitcoind {
    bitcoind: Bitcoind,
    tempdir: Arc<TempDir>,
    rpc_port: u16,
    zmq_port: u16,
}

impl IsolatedBitcoind {
    fn spawn() -> Self {
        for attempt in 0..3 {
            let tempdir = Arc::new(TempDir::new().unwrap());
            let rpc_port = allocate_port();
            let zmq_port = allocate_port();

            match Bitcoind::spawn_no_listen(
                tempdir.clone(),
                rpc_port,
                zmq_port,
                false,
                Network::Signet,
            ) {
                Ok(bitcoind) => {
                    return Self {
                        bitcoind,
                        tempdir,
                        rpc_port,
                        zmq_port,
                    };
                }
                Err(e) if attempt < 2 => {
                    eprintln!(
                        "bitcoind spawn attempt {attempt} failed: {e}, retrying with new ports"
                    );
                }
                Err(e) => panic!("bitcoind failed to spawn after 3 attempts: {e}"),
            }
        }
        unreachable!()
    }

    fn shutdown(&mut self) {
        self.bitcoind.shutdown();
    }

    fn restart(&mut self) {
        self.bitcoind = Bitcoind::spawn_no_listen(
            self.tempdir.clone(),
            self.rpc_port,
            self.zmq_port,
            false,
            Network::Signet,
        )
        .unwrap();
    }
}

async fn wait_for_exit(pool: &mut TestPool) {
    async_timeout(Duration::from_secs(20), async {
        loop {
            match pool.try_wait() {
                Ok(Some(_)) => break,
                Ok(None) => sleep(Duration::from_millis(50)).await,
                Err(e) => panic!("Failed to wait for pool: {e}"),
            }
        }
    })
    .await
    .expect("Pool did not exit");
}

#[tokio::test]
#[timeout(30000)]
async fn exits_on_prolonged_bitcoind_failure() {
    let mut bitcoind = IsolatedBitcoind::spawn();
    let mut pool = TestPool::spawn_with_args(
        &bitcoind.bitcoind,
        "--bitcoind-timeout 5 --update-interval 1 --start-diff 0.00001",
    );

    bitcoind.shutdown();

    wait_for_exit(&mut pool).await;
}

#[tokio::test]
#[timeout(60000)]
async fn zmq_reconnects_after_bitcoind_restart() {
    let mut bitcoind = IsolatedBitcoind::spawn();
    let mut pool = TestPool::spawn_with_args(
        &bitcoind.bitcoind,
        "--bitcoind-timeout 30 --update-interval 1 --start-diff 0.00001",
    );

    bitcoind.shutdown();

    sleep(Duration::from_secs(3)).await;

    bitcoind.restart();

    sleep(Duration::from_secs(5)).await;

    assert!(
        pool.try_wait().unwrap().is_none(),
        "Pool should still be running after bitcoind restart"
    );
}