pulpod 0.0.42

Pulpo daemon — manages agent sessions via tmux/Docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};

use anyhow::Result;
use pulpo_common::node::NodeInfo;
use pulpo_common::peer::PeerStatus;
use pulpo_common::session::Session;
use tokio::sync::RwLock;
use tracing::{debug, warn};

use super::PeerRegistry;

/// Build a base URL from a peer address. Addresses that already include a scheme
/// (e.g., `https://machine.tailnet.ts.net`) are used as-is; bare `host:port`
/// addresses get `http://` prepended.
pub fn base_url(address: &str) -> String {
    if address.starts_with("http://") || address.starts_with("https://") {
        address.to_owned()
    } else {
        format!("http://{address}")
    }
}

/// Result of probing a peer node.
#[derive(Debug, Clone)]
pub struct ProbeResult {
    pub node_info: NodeInfo,
    pub session_count: usize,
}

/// Trait for probing peer health. Enables testing with mocks.
pub trait PeerProber: Send + Sync {
    fn probe(
        &self,
        address: &str,
        token: Option<&str>,
    ) -> impl std::future::Future<Output = Result<ProbeResult>> + Send;
}

/// Production prober that uses HTTP requests to peer `/api/v1/node` and `/api/v1/sessions`.
pub struct HttpPeerProber {
    client: reqwest::Client,
}

impl Default for HttpPeerProber {
    fn default() -> Self {
        Self {
            client: reqwest::Client::builder()
                .timeout(Duration::from_secs(5))
                .build()
                .unwrap_or_default(),
        }
    }
}

impl HttpPeerProber {
    pub fn new() -> Self {
        Self::default()
    }
}

impl HttpPeerProber {
    /// Build a GET request, attaching `Authorization: Bearer` when a token is present.
    fn authed_get(&self, url: String, token: Option<&str>) -> reqwest::RequestBuilder {
        let req = self.client.get(url);
        if let Some(t) = token {
            req.bearer_auth(t)
        } else {
            req
        }
    }
}

impl PeerProber for HttpPeerProber {
    async fn probe(&self, address: &str, token: Option<&str>) -> Result<ProbeResult> {
        let base = base_url(address);
        let node_info: NodeInfo = self
            .authed_get(format!("{base}/api/v1/node"), token)
            .send()
            .await?
            .json()
            .await?;

        let sessions: Vec<Session> = self
            .authed_get(format!("{base}/api/v1/sessions"), token)
            .send()
            .await?
            .json()
            .await?;

        Ok(ProbeResult {
            node_info,
            session_count: sessions.len(),
        })
    }
}

/// Cached result from a single peer probe.
struct CachedProbeResult {
    result: ProbeResult,
    fetched_at: Instant,
}

/// On-demand peer prober with a per-peer TTL cache.
///
/// Instead of running a background polling loop, `CachedProber` probes peers
/// lazily when `probe_all()` or `probe_peer()` is called and caches results
/// for a configurable TTL (default 60 s). Subsequent calls within the TTL
/// window return the cached value without hitting the network.
pub struct CachedProber<P: PeerProber> {
    prober: P,
    cache: Arc<RwLock<HashMap<String, CachedProbeResult>>>,
    ttl: Duration,
}

impl<P: PeerProber> CachedProber<P> {
    pub fn new(prober: P, ttl: Duration) -> Self {
        Self {
            prober,
            cache: Arc::new(RwLock::new(HashMap::new())),
            ttl,
        }
    }

    /// Probe a single peer, returning a cached result if still fresh.
    pub async fn probe_peer(
        &self,
        name: &str,
        address: &str,
        token: Option<&str>,
    ) -> Option<ProbeResult> {
        // Check cache
        if let Some(cached) = self.cache.read().await.get(name)
            && cached.fetched_at.elapsed() < self.ttl
        {
            return Some(cached.result.clone());
        }
        // Probe and cache
        match self.prober.probe(address, token).await {
            Ok(result) => {
                self.cache.write().await.insert(
                    name.to_owned(),
                    CachedProbeResult {
                        result: result.clone(),
                        fetched_at: Instant::now(),
                    },
                );
                Some(result)
            }
            Err(e) => {
                warn!("Peer {name} at {address} is offline: {e}");
                None
            }
        }
    }

    /// Probe all peers in the registry concurrently, updating their statuses.
    pub async fn probe_all(&self, registry: &PeerRegistry) {
        let peers = registry.get_all().await;

        // Collect (name, address, token) tuples first, then probe concurrently.
        let mut tasks = Vec::with_capacity(peers.len());
        for peer in &peers {
            let token = registry.get_token(&peer.name).await;
            tasks.push((peer.name.clone(), peer.address.clone(), token));
        }

        let futures: Vec<_> = tasks
            .iter()
            .map(|(name, address, token)| self.probe_peer(name, address, token.as_deref()))
            .collect();

        let results = futures::future::join_all(futures).await;

        for ((name, address, _), result) in tasks.iter().zip(results) {
            match result {
                Some(probe) => {
                    debug!(
                        "Peer {} at {} is online ({} sessions)",
                        name, address, probe.session_count
                    );
                    registry
                        .update_status(
                            name,
                            PeerStatus::Online,
                            Some(probe.node_info),
                            Some(probe.session_count),
                        )
                        .await;
                }
                None => {
                    registry
                        .update_status(name, PeerStatus::Offline, None, None)
                        .await;
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pulpo_common::peer::PeerEntry;
    struct MockProber {
        results: HashMap<String, Result<ProbeResult, String>>,
    }

    impl PeerProber for MockProber {
        async fn probe(&self, address: &str, _token: Option<&str>) -> Result<ProbeResult> {
            match self.results.get(address) {
                Some(Ok(result)) => Ok(result.clone()),
                Some(Err(msg)) => Err(anyhow::anyhow!("{msg}")),
                None => Err(anyhow::anyhow!("unknown address: {address}")),
            }
        }
    }

    fn make_node_info(name: &str) -> NodeInfo {
        NodeInfo {
            name: name.into(),
            hostname: "host".into(),
            os: "linux".into(),
            arch: "x86_64".into(),
            cpus: 4,
            memory_mb: 8192,
            gpu: None,
        }
    }

    // ---- CachedProber tests ----
    //
    // All CachedProber tests use MockProber exclusively so that there is a
    // single monomorphisation of `CachedProber<MockProber>`. This avoids
    // LLVM coverage instrumentation artifacts where cross-instantiation
    // region merging creates phantom "uncovered" lines.

    #[tokio::test]
    async fn test_cached_prober_caches_result() {
        let mut results = HashMap::new();
        results.insert(
            "10.0.0.1:7433".into(),
            Ok(ProbeResult {
                node_info: make_node_info("node-a"),
                session_count: 1,
            }),
        );
        let prober = MockProber { results };
        let cached = CachedProber::new(prober, Duration::from_secs(60));

        // First probe — hits the prober
        let result = cached.probe_peer("node-a", "10.0.0.1:7433", None).await;
        assert!(result.is_some());
        assert_eq!(result.unwrap().session_count, 1);

        // Second probe within TTL — returns cached
        let result = cached.probe_peer("node-a", "10.0.0.1:7433", None).await;
        assert!(result.is_some());
        assert_eq!(result.unwrap().session_count, 1);
    }

    #[tokio::test]
    async fn test_cached_prober_expires_cache() {
        let mut results = HashMap::new();
        results.insert(
            "10.0.0.1:7433".into(),
            Ok(ProbeResult {
                node_info: make_node_info("node-a"),
                session_count: 1,
            }),
        );
        let prober = MockProber { results };
        // Use a zero-duration TTL so cache expires immediately
        let cached = CachedProber::new(prober, Duration::ZERO);

        let result = cached.probe_peer("node-a", "10.0.0.1:7433", None).await;
        assert!(result.is_some());

        // TTL is zero, so cache is already stale — prober called again
        let result = cached.probe_peer("node-a", "10.0.0.1:7433", None).await;
        assert!(result.is_some());
    }

    #[tokio::test]
    async fn test_cached_prober_probe_all_updates_registry() {
        let mut configured = HashMap::new();
        configured.insert("node-a".into(), PeerEntry::Simple("10.0.0.1:7433".into()));
        let registry = PeerRegistry::new(&configured);

        let mut results = HashMap::new();
        results.insert(
            "10.0.0.1:7433".into(),
            Ok(ProbeResult {
                node_info: make_node_info("node-a"),
                session_count: 3,
            }),
        );
        let prober = MockProber { results };
        let cached = CachedProber::new(prober, Duration::from_secs(60));

        cached.probe_all(&registry).await;

        let peer = registry.get("node-a").await.unwrap();
        assert_eq!(peer.status, PeerStatus::Online);
        assert_eq!(peer.session_count, Some(3));
        assert!(peer.node_info.is_some());
    }

    #[tokio::test]
    async fn test_cached_prober_probe_all_offline() {
        let mut configured = HashMap::new();
        configured.insert("node-b".into(), PeerEntry::Simple("10.0.0.2:7433".into()));
        let registry = PeerRegistry::new(&configured);

        let mut results = HashMap::new();
        results.insert("10.0.0.2:7433".into(), Err("connection refused".into()));
        let prober = MockProber { results };
        let cached = CachedProber::new(prober, Duration::from_secs(60));

        cached.probe_all(&registry).await;

        let peer = registry.get("node-b").await.unwrap();
        assert_eq!(peer.status, PeerStatus::Offline);
        assert!(peer.node_info.is_none());
        assert!(peer.session_count.is_none());
    }

    #[tokio::test]
    async fn test_cached_prober_probe_all_mixed() {
        let mut configured = HashMap::new();
        configured.insert("online".into(), PeerEntry::Simple("10.0.0.1:7433".into()));
        configured.insert("offline".into(), PeerEntry::Simple("10.0.0.2:7433".into()));
        let registry = PeerRegistry::new(&configured);

        let mut results = HashMap::new();
        results.insert(
            "10.0.0.1:7433".into(),
            Ok(ProbeResult {
                node_info: make_node_info("online"),
                session_count: 1,
            }),
        );
        results.insert("10.0.0.2:7433".into(), Err("timeout".into()));
        let prober = MockProber { results };
        let cached = CachedProber::new(prober, Duration::from_secs(60));

        cached.probe_all(&registry).await;

        let online = registry.get("online").await.unwrap();
        assert_eq!(online.status, PeerStatus::Online);
        assert_eq!(online.session_count, Some(1));

        let offline = registry.get("offline").await.unwrap();
        assert_eq!(offline.status, PeerStatus::Offline);
    }

    #[tokio::test]
    async fn test_cached_prober_probe_all_empty() {
        let registry = PeerRegistry::new(&HashMap::new());
        let prober = MockProber {
            results: HashMap::new(),
        };
        let cached = CachedProber::new(prober, Duration::from_secs(60));
        // Should not panic
        cached.probe_all(&registry).await;
    }

    #[tokio::test]
    async fn test_cached_prober_probe_peer_unknown_address() {
        let prober = MockProber {
            results: HashMap::new(), // no results for any address
        };
        let cached = CachedProber::new(prober, Duration::from_secs(60));

        let result = cached.probe_peer("mystery", "10.99.99.99:7433", None).await;
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_cached_prober_different_peers_independent() {
        let mut results = HashMap::new();
        results.insert(
            "10.0.0.1:7433".into(),
            Ok(ProbeResult {
                node_info: make_node_info("node-a"),
                session_count: 1,
            }),
        );
        results.insert(
            "10.0.0.2:7433".into(),
            Ok(ProbeResult {
                node_info: make_node_info("node-b"),
                session_count: 2,
            }),
        );
        let prober = MockProber { results };
        let cached = CachedProber::new(prober, Duration::from_secs(60));

        // Probe two different peers
        let r1 = cached.probe_peer("node-a", "10.0.0.1:7433", None).await;
        let r2 = cached.probe_peer("node-b", "10.0.0.2:7433", None).await;
        assert_eq!(r1.unwrap().session_count, 1);
        assert_eq!(r2.unwrap().session_count, 2);

        // Re-probe both — returns cached results
        let r1 = cached.probe_peer("node-a", "10.0.0.1:7433", None).await;
        let r2 = cached.probe_peer("node-b", "10.0.0.2:7433", None).await;
        assert_eq!(r1.unwrap().session_count, 1);
        assert_eq!(r2.unwrap().session_count, 2);
    }

    // ---- HttpPeerProber tests (kept from original) ----

    #[tokio::test]
    async fn test_http_peer_prober_new() {
        let prober = HttpPeerProber::new();
        // Verify construction doesn't panic
        let debug = format!("{:?}", prober.client);
        assert!(!debug.is_empty());
    }

    #[tokio::test]
    async fn test_http_peer_prober_default() {
        let prober = HttpPeerProber::default();
        let debug = format!("{:?}", prober.client);
        assert!(!debug.is_empty());
    }

    #[tokio::test]
    async fn test_http_peer_prober_probe_connection_refused() {
        let prober = HttpPeerProber::new();
        // Probe a non-existent address — should fail
        let result = prober.probe("127.0.0.1:1", None).await;
        assert!(result.is_err());
    }

    /// Integration test: start a real axum server and probe it.
    #[tokio::test]
    async fn test_http_peer_prober_probe_real_server() {
        use axum::{Router, routing::get};
        let node_json = serde_json::to_string(&make_node_info("test-peer")).unwrap();
        let sessions = vec![
            Session {
                id: uuid::Uuid::new_v4(),
                name: "s1".into(),
                workdir: "/tmp".into(),
                command: "echo test".into(),
                status: pulpo_common::session::SessionStatus::Active,
                ..Default::default()
            },
            Session {
                id: uuid::Uuid::new_v4(),
                name: "s2".into(),
                workdir: "/tmp".into(),
                command: "echo test".into(),
                status: pulpo_common::session::SessionStatus::Active,
                ..Default::default()
            },
        ];
        let sessions_json = serde_json::to_string(&sessions).unwrap();

        let app = Router::new()
            .route(
                "/api/v1/node",
                get(move || {
                    let body = node_json.clone();
                    async move { ([("content-type", "application/json")], body) }
                }),
            )
            .route(
                "/api/v1/sessions",
                get(move || {
                    let body = sessions_json.clone();
                    async move { ([("content-type", "application/json")], body) }
                }),
            );

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(axum::serve(listener, app).into_future());

        let prober = HttpPeerProber::new();
        let result = prober
            .probe(&format!("127.0.0.1:{}", addr.port()), None)
            .await
            .unwrap();
        assert_eq!(result.node_info.name, "test-peer");
        assert_eq!(result.session_count, 2);
    }

    /// Test probe fails when node endpoint returns invalid JSON.
    #[tokio::test]
    async fn test_http_peer_prober_probe_invalid_node_json() {
        use axum::{Router, routing::get};

        let app = Router::new().route(
            "/api/v1/node",
            get(|| async { ([("content-type", "application/json")], "not json") }),
        );

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(axum::serve(listener, app).into_future());

        let prober = HttpPeerProber::new();
        let result = prober
            .probe(&format!("127.0.0.1:{}", addr.port()), None)
            .await;
        assert!(result.is_err());
    }

    /// Test probe fails when sessions endpoint returns invalid JSON.
    #[tokio::test]
    async fn test_http_peer_prober_probe_invalid_sessions_json() {
        use axum::{Router, routing::get};

        let node_json = serde_json::to_string(&make_node_info("test-peer")).unwrap();
        let app = Router::new()
            .route(
                "/api/v1/node",
                get(move || {
                    let body = node_json.clone();
                    async move { ([("content-type", "application/json")], body) }
                }),
            )
            .route(
                "/api/v1/sessions",
                get(|| async { ([("content-type", "application/json")], "not json") }),
            );

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(axum::serve(listener, app).into_future());

        let prober = HttpPeerProber::new();
        let result = prober
            .probe(&format!("127.0.0.1:{}", addr.port()), None)
            .await;
        assert!(result.is_err());
    }

    /// Test probe fails when sessions endpoint is missing (node works but sessions 404s).
    #[tokio::test]
    async fn test_http_peer_prober_probe_missing_sessions_endpoint() {
        use axum::{Router, routing::get};

        let node_json = serde_json::to_string(&make_node_info("test-peer")).unwrap();
        let app = Router::new().route(
            "/api/v1/node",
            get(move || {
                let body = node_json.clone();
                async move { ([("content-type", "application/json")], body) }
            }),
        );

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(axum::serve(listener, app).into_future());

        let prober = HttpPeerProber::new();
        let result = prober
            .probe(&format!("127.0.0.1:{}", addr.port()), None)
            .await;
        // 404 response body won't parse as Vec<Session>
        assert!(result.is_err());
    }

    /// Test that probe sends Authorization header when token is provided.
    #[tokio::test]
    async fn test_http_peer_prober_probe_with_token() {
        use axum::extract::Request;
        use axum::{Router, routing::get};
        use std::sync::Mutex;

        let captured_auth = Arc::new(Mutex::new(None::<String>));
        let auth_clone = captured_auth.clone();

        let node_json = serde_json::to_string(&make_node_info("tok-peer")).unwrap();
        let sessions: Vec<pulpo_common::session::Session> = vec![];
        let sessions_json = serde_json::to_string(&sessions).unwrap();

        let app = Router::new()
            .route(
                "/api/v1/node",
                get(move |req: Request| {
                    let auth = req
                        .headers()
                        .get("authorization")
                        .map(|v| v.to_str().unwrap().to_owned());
                    *auth_clone.lock().unwrap() = auth;
                    let body = node_json.clone();
                    async move { ([("content-type", "application/json")], body) }
                }),
            )
            .route(
                "/api/v1/sessions",
                get(move || {
                    let body = sessions_json.clone();
                    async move { ([("content-type", "application/json")], body) }
                }),
            );

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(axum::serve(listener, app).into_future());

        let prober = HttpPeerProber::new();
        let result = prober
            .probe(&format!("127.0.0.1:{}", addr.port()), Some("my-token"))
            .await
            .unwrap();
        assert_eq!(result.node_info.name, "tok-peer");

        let auth = captured_auth.lock().unwrap().clone();
        assert_eq!(auth, Some("Bearer my-token".into()));
    }

    /// Test that probe does NOT send Authorization header when no token.
    #[tokio::test]
    async fn test_http_peer_prober_probe_without_token_no_header() {
        use axum::extract::Request;
        use axum::{Router, routing::get};
        use std::sync::Mutex;

        let has_auth = Arc::new(Mutex::new(true));
        let flag = has_auth.clone();

        let node_json = serde_json::to_string(&make_node_info("no-tok")).unwrap();
        let sessions: Vec<pulpo_common::session::Session> = vec![];
        let sessions_json = serde_json::to_string(&sessions).unwrap();

        let app = Router::new()
            .route(
                "/api/v1/node",
                get(move |req: Request| {
                    *flag.lock().unwrap() = req.headers().contains_key("authorization");
                    let body = node_json.clone();
                    async move { ([("content-type", "application/json")], body) }
                }),
            )
            .route(
                "/api/v1/sessions",
                get(move || {
                    let body = sessions_json.clone();
                    async move { ([("content-type", "application/json")], body) }
                }),
            );

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(axum::serve(listener, app).into_future());

        let prober = HttpPeerProber::new();
        let _ = prober
            .probe(&format!("127.0.0.1:{}", addr.port()), None)
            .await
            .unwrap();

        assert!(!*has_auth.lock().unwrap());
    }

    /// Test that probe returns an error when the server shuts down between
    /// the node and sessions requests (exercises the `.send().await?` error
    /// path on the sessions request).
    #[tokio::test]
    async fn test_http_peer_prober_probe_sessions_send_error() {
        use axum::{Router, routing::get};
        use tokio::sync::oneshot;

        let node_json = serde_json::to_string(&make_node_info("gone-peer")).unwrap();
        let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();
        let shutdown_tx = Arc::new(std::sync::Mutex::new(Some(shutdown_tx)));

        let app = Router::new().route(
            "/api/v1/node",
            get(move || {
                // After serving the node response, trigger server shutdown
                // so the sessions request will find no server.
                let _ = shutdown_tx.lock().unwrap().take().map(|tx| tx.send(()));
                let body = node_json.clone();
                async move { ([("content-type", "application/json")], body) }
            }),
        );
        // No /api/v1/sessions route — server shuts down before it's needed

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move {
            axum::serve(listener, app)
                .with_graceful_shutdown(async {
                    shutdown_rx.await.ok();
                })
                .await
                .unwrap();
        });

        let prober = HttpPeerProber::new();
        let result = prober
            .probe(&format!("127.0.0.1:{}", addr.port()), None)
            .await;
        assert!(result.is_err());
    }

    #[test]
    fn test_probe_result_debug() {
        let result = ProbeResult {
            node_info: make_node_info("debug-test"),
            session_count: 5,
        };
        let debug = format!("{result:?}");
        assert!(debug.contains("debug-test"));
        assert!(debug.contains('5'));
    }

    #[test]
    fn test_probe_result_clone() {
        let result = ProbeResult {
            node_info: make_node_info("clone-test"),
            session_count: 2,
        };
        let cloned = result.clone();
        assert_eq!(cloned.session_count, result.session_count);
        assert_eq!(cloned.node_info.name, "clone-test");
    }

    // ---- base_url tests ----

    #[test]
    fn test_base_url_bare_host_port() {
        assert_eq!(base_url("10.0.0.1:7433"), "http://10.0.0.1:7433");
    }

    #[test]
    fn test_base_url_https_passthrough() {
        assert_eq!(
            base_url("https://machine.tailnet.ts.net"),
            "https://machine.tailnet.ts.net"
        );
    }

    #[test]
    fn test_base_url_http_passthrough() {
        assert_eq!(base_url("http://localhost:7433"), "http://localhost:7433");
    }

    #[test]
    fn test_base_url_hostname_no_scheme() {
        assert_eq!(base_url("myhost:8080"), "http://myhost:8080");
    }
}