nsh-cli 0.6.1

The nsh command line tool: build, package and run apps.
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
use std::{
    cell::Cell,
    collections::HashSet,
    net::{Ipv4Addr, SocketAddr},
    path::{Path, PathBuf},
    sync::{
        Arc, Mutex,
        atomic::{AtomicBool, Ordering},
        mpsc,
    },
    time::{Duration, Instant},
};

use anyhow::Context as _;
use notify::{RecursiveMode, Watcher};
use nsh_dev::{
    ClientEvent, DevServer,
    protocol::{HostPlatform, WS_PATH},
};

use crate::{build, logger, ui};

/// File events arriving within this window are coalesced into one rebuild,
/// or into a restart when one of them changes the app's Rust code.
const DEBOUNCE: Duration = Duration::from_millis(30);

/// A running dev server plus the source watcher feeding it.
pub(crate) struct DevSession {
    entry: PathBuf,
    src_dir: PathBuf,
    release: bool,
    server: DevServer,
    /// Whether other machines may connect.
    network: bool,
    devices: Arc<Devices>,
    /// The modules of the last good development build: updates are
    /// computed against it.
    hot: std::cell::RefCell<Option<nsh_bundler::HotBundle>>,
    /// Files read by the last good build (imports may be outside the app).
    watch_files: std::cell::RefCell<Vec<PathBuf>>,
    /// Good builds so far: the first one is "built", the next "rebuilt".
    builds: Cell<u32>,
    /// A Rust app (`nsh.json` `"root"`): nothing to bundle, the hosts run
    /// its root; the session serves its assets, its web page and its logs.
    rust_root: bool,
}

/// Where a dev server listens.
#[derive(Clone, Copy)]
pub(crate) struct Listen {
    /// Its port, zero for one the OS allocates.
    pub(crate) port: u16,
    /// Whether other machines may connect, or only this one.
    pub(crate) network: bool,
}

impl DevSession {
    /// Starts the server and publishes the first build (or its error).
    /// Hosts run the app with `limits` as an embedding app that sets them
    /// would (`--limits`), or with none, as an app on its own.
    pub(crate) fn start(
        entry: &Path,
        listen: Listen,
        release: bool,
        limits: Option<nsh_core::js::HostLimits>,
    ) -> anyhow::Result<DevSession> {
        let entry = dunce::canonicalize(entry)
            .with_context(|| format!("entry not found: {}", entry.display()))?;
        let src_dir = entry
            .parent()
            .context("entry has no directory")?
            .to_path_buf();
        let devices = Arc::<Devices>::default();
        let server = start_server(listen, &src_dir, devices.clone())?;
        server.serve_files(crate::build::crate_assets().map(|a| (a.path, a.source)));
        server.install_commands(install_commands(&src_dir));
        server.set_limits(limits.map(|l| l.to_string()));
        // Browsers too, when there is a web runtime to serve (`nsh run
        // --target web` builds it in a checkout).
        let rust_root = crate::rust_app::is_root(&entry);
        if let Ok(Some(runtime)) = crate::runtime::web(crate::runtime::WebKind::Dev, false) {
            let (page, files) =
                crate::web::with_page_host(crate::web::dev_host(&runtime, rust_root)?, &entry)?;
            server.serve_web(page, files);
        }
        let session = DevSession {
            entry,
            src_dir,
            release,
            server,
            network: listen.network,
            devices,
            hot: Default::default(),
            watch_files: Default::default(),
            builds: Cell::new(0),
            rust_root,
        };
        session.rebuild();
        // A Rust app's hosts that connect (the terminal's) run the root
        // they were built with: the server tells them so.
        if session.rust_root {
            session.server.publish_root();
        }
        Ok(session)
    }

    /// The port the dev server listens on.
    pub(crate) fn port(&self) -> u16 {
        self.server.local_addr().port()
    }

    /// WebSocket URL for hosts on this machine.
    pub(crate) fn local_url(&self) -> String {
        format!(
            "ws://127.0.0.1:{}{WS_PATH}",
            self.server.local_addr().port()
        )
    }

    /// The web host's page for browsers on this machine.
    pub(crate) fn web_url(&self) -> String {
        format!("http://127.0.0.1:{}/", self.server.local_addr().port())
    }

    /// This machine's addresses other machines reach the server on.
    fn network_ips(&self) -> Vec<Ipv4Addr> {
        if self.network { lan_ips() } else { Vec::new() }
    }

    /// Prints where hosts can connect.
    pub(crate) fn print_urls(&self) {
        let port = self.server.local_addr().port();
        ui::note(format!("dev server {}", self.local_url()));
        let web = self.server.serves_web();
        if web {
            ui::detail(format!("browsers: {}", self.web_url()));
        }
        for ip in self.network_ips() {
            ui::detail(format!(
                "devices on this network: ws://{ip}:{port}{WS_PATH}"
            ));
            if web {
                ui::detail(format!("browsers on this network: http://{ip}:{port}/"));
            }
        }
    }

    /// Prints the page browsers open (`nsh run --target web`): the one
    /// address to open here, and dimmed, the same on this network (phones).
    pub(crate) fn print_web_url(&self) {
        let port = self.server.local_addr().port();
        ui::note(format!("open in a browser: {}", ui::link(&self.web_url())));
        if let Some(ip) = self.network_ips().first() {
            ui::detail(ui::dim(format!("on this network: http://{ip}:{port}/")));
        }
    }

    /// Rebuilds on source changes until `stop` returns true.
    pub(crate) fn watch(&self, stop: impl FnMut() -> anyhow::Result<bool>) -> anyhow::Result<()> {
        let result = self.watch_until(stop);
        // The session is over: its hosts going away (stopped by the
        // command, or with the server) is no news.
        self.devices.closing.store(true, Ordering::Relaxed);
        result
    }

    fn watch_until(&self, mut stop: impl FnMut() -> anyhow::Result<bool>) -> anyhow::Result<()> {
        let (tx, rx) = mpsc::channel();
        let mut watcher =
            notify::recommended_watcher(move |event: notify::Result<notify::Event>| {
                if let Ok(event) = event {
                    let _ = tx.send(event);
                }
            })?;
        watcher.watch(&self.src_dir, RecursiveMode::Recursive)?;
        ui::debug(format!("watching {}", ui::path(&self.src_dir)));
        // The app's Rust code: a change builds the app's hosts again.
        let rust = crate::rust_app::current();
        for dir in rust.iter().flat_map(|app| app.watched()) {
            if !dir.starts_with(&self.src_dir) {
                watcher.watch(dir, RecursiveMode::Recursive)?;
            }
        }
        let mut imported_dirs = HashSet::new();
        self.watch_imports(&mut watcher, &mut imported_dirs)?;
        loop {
            if ui::stop_requested() || stop()? {
                return Ok(());
            }
            let Ok(event) = rx.recv_timeout(Duration::from_millis(200)) else {
                continue;
            };
            // inotify reports reads too (FSEvents does not): the build's
            // own would start the next one, forever.
            if matches!(event.kind, notify::EventKind::Access(_)) {
                continue;
            }
            if let Some(app) = &rust
                && event.paths.iter().any(|p| app.is_rust(p))
            {
                return Err(crate::rust_app::Restart.into());
            }
            if !is_source(&event) {
                continue;
            }
            // A Rust change saved with this one (a checkout, a formatter)
            // starts the session again, which builds the TSX too: built
            // now, it would check its imports against the old UI sets.
            let deadline = Instant::now() + DEBOUNCE;
            let is_rust = |p: &Path| rust.as_ref().is_some_and(|app| app.is_rust(p));
            if rust_changed_until(&rx, deadline, is_rust) {
                return Err(crate::rust_app::Restart.into());
            }
            self.rebuild();
            self.watch_imports(&mut watcher, &mut imported_dirs)?;
        }
    }

    /// Watch the directories of shared imports, so atomic saves and new
    /// modules work as they do inside the app. Its own tree is watched above.
    fn watch_imports(
        &self,
        watcher: &mut impl Watcher,
        watched: &mut HashSet<PathBuf>,
    ) -> anyhow::Result<()> {
        for dir in self
            .watch_files
            .borrow()
            .iter()
            .filter_map(|path| path.parent())
        {
            if !dir.starts_with(&self.src_dir) && watched.insert(dir.to_path_buf()) {
                watcher.watch(dir, RecursiveMode::NonRecursive)?;
                ui::debug(format!("watching shared source {}", ui::path(dir)));
            }
        }
        Ok(())
    }

    /// Serves the assets `built` imports by name, whatever their type: the
    /// app's directory itself serves only its media files. It is the one
    /// the build named them from (an entry below its app names them from
    /// the app's directory), so a literal `asset:/` URL finds what a
    /// package of the app holds.
    fn serve_assets(&self, built: &build::Built) {
        if let Some(root) = &built.asset_root {
            self.server.set_assets_root(Some(root.clone()));
        }
        self.server.serve_files(
            built
                .assets
                .iter()
                .map(|asset| (asset.path.clone(), asset.source.clone())),
        );
    }

    fn rebuild(&self) {
        // A Rust app has no JS: its Rust code's changes restart the session.
        if self.rust_root {
            return;
        }
        self.server.building();
        if self.release {
            match build::compile(&self.entry, true) {
                Ok(built) => {
                    self.watch_files.replace(built.watch_files.clone());
                    self.serve_assets(&built);
                    let capabilities = built.capabilities_json();
                    let summary = self.summary(&built);
                    self.server.publish(
                        built.bytecode,
                        built.code,
                        built.source_map,
                        nsh_dev::AppInfo {
                            capabilities,
                            id: crate::package::app_id(&self.entry).ok(),
                            window: crate::package::window_json(&self.entry),
                        },
                        None,
                        built.ui_sets,
                    );
                    self.published(summary);
                }
                Err(err) => self.failed(err),
            }
            return;
        }
        match build::compile_hot(&self.entry) {
            Ok((built, hot)) => {
                self.watch_files.replace(built.watch_files.clone());
                self.serve_assets(&built);
                let summary = self.summary(&built);
                // Other capabilities: the app starts again under them.
                let patch = self
                    .hot
                    .borrow()
                    .as_ref()
                    .filter(|previous| previous.capabilities == hot.capabilities)
                    .map(|previous| hot.patch(previous));
                let patch = match patch {
                    Some(nsh_bundler::HotPatch::Update { code, source_map }) => {
                        Some((code, source_map))
                    }
                    // Nothing to replace; hosts still clear a build error.
                    Some(nsh_bundler::HotPatch::Unchanged) => Some(("true".to_owned(), None)),
                    Some(nsh_bundler::HotPatch::Reload) | None => None,
                };
                let capabilities = built.capabilities_json();
                self.server.publish(
                    built.bytecode,
                    built.code,
                    built.source_map,
                    nsh_dev::AppInfo {
                        capabilities,
                        id: crate::package::app_id(&self.entry).ok(),
                        window: crate::package::window_json(&self.entry),
                    },
                    patch,
                    built.ui_sets,
                );
                *self.hot.borrow_mut() = Some(hot);
                self.published(summary);
            }
            Err(err) => self.failed(err),
        }
    }

    /// What the line of a good build says after its path.
    fn summary(&self, built: &build::Built) -> String {
        if self.builds.get() == 0 {
            built.summary("")
        } else {
            ui::dim(format!("({})", ui::duration(built.elapsed)))
        }
    }

    /// `✓ built App.tsx (...)`, then `✓ rebuilt App.tsx (18ms), 2 devices`.
    fn published(&self, summary: String) {
        let entry = ui::path(&self.entry);
        let builds = self.builds.get();
        self.builds.set(builds + 1);
        if builds == 0 {
            return ui::done(format!("built {entry} {summary}"));
        }
        let devices = match self.server.clients() {
            0 => String::new(),
            1 => ", 1 device".to_owned(),
            n => format!(", {n} devices"),
        };
        ui::done(format!("rebuilt {entry} {summary}{}", ui::dim(devices)));
    }

    fn failed(&self, err: build::BuildError) {
        let plain = err.plain.clone();
        ui::report(&err.into_error(&self.entry));
        self.server.publish_error(plain);
    }
}

impl Drop for DevSession {
    fn drop(&mut self) {
        // The server stops next.
        self.devices.closing.store(true, Ordering::Relaxed);
    }
}

/// The hosts connected to the dev server: what they do is shown as `•`
/// lines, their log lines with their names.
#[derive(Default)]
struct Devices {
    connected: Mutex<Vec<String>>,
    /// The session ends.
    closing: AtomicBool,
}

impl Devices {
    fn connected(&self) -> std::sync::MutexGuard<'_, Vec<String>> {
        self.connected.lock().unwrap_or_else(|e| e.into_inner())
    }

    fn on_event(&self, event: ClientEvent) {
        match event {
            ClientEvent::Connected { device, build } => {
                let build = build.map_or(String::new(), |b| format!(", build {b}"));
                ui::event(format!("{device} connected{build}"));
                self.connected().push(device);
            }
            ClientEvent::CannotRun {
                device,
                missing,
                install,
            } => {
                let label = short_name(&self.connected(), &device);
                let install =
                    install.map_or(String::new(), |c| format!(": {c} installs this app's host"));
                ui::warn(format!(
                    "{label}: its host has no {} (it was built for another app){install}",
                    missing.join(", ")
                ));
            }
            ClientEvent::Disconnected { device } => {
                {
                    let mut connected = self.connected();
                    if let Some(i) = connected.iter().position(|d| *d == device) {
                        connected.remove(i);
                    }
                }
                if !self.closing.load(Ordering::Relaxed) {
                    ui::event(ui::dim(format!("{device} disconnected")));
                }
            }
            ClientEvent::Log(log) => {
                let (label, several) = {
                    let connected = self.connected();
                    (short_name(&connected, &log.device), connected.len() > 1)
                };
                let device = logger::Device {
                    name: &log.device,
                    label: &label,
                    several,
                };
                logger::device(&device, log.level, &log.target, &log.message);
            }
            // Only a test session's traced app sends its batches.
            ClientEvent::Trace { .. } => {}
        }
    }
}

/// `device` without its model ("iOS Simulator (iPhone 16 Plus)": "iOS
/// Simulator"), unless another connected device has that name too.
fn short_name(connected: &[String], device: &str) -> String {
    let short = |name: &str| name.split_once(" (").map_or(name, |(s, _)| s).to_owned();
    let name = short(device);
    let shared = connected
        .iter()
        .any(|other| other != device && short(other) == name);
    if shared { device.to_owned() } else { name }
}

/// The commands that install the app in `dir`'s host, for the platforms
/// `nsh run` has a target for.
fn install_commands(dir: &Path) -> Vec<(HostPlatform, String)> {
    let app = match ui::path(dir) {
        dot if dot == "." => String::new(),
        path if path.contains(' ') => format!(" '{path}'"),
        path => format!(" {path}"),
    };
    use crate::run::Target;
    // The target this machine's `nsh run` defaults to goes without a flag.
    let run = |target: Target, name: &str| {
        if target == Target::native() {
            format!("nsh run{app}")
        } else {
            format!("nsh run{app} --target {name}")
        }
    };
    vec![
        (HostPlatform::MacCatalyst, run(Target::Catalyst, "catalyst")),
        (HostPlatform::MacOs, run(Target::Macos, "macos")),
        (HostPlatform::Linux, run(Target::Linux, "linux")),
        (HostPlatform::Windows, run(Target::Windows, "windows")),
        (HostPlatform::Gpui, run(Target::Gpui, "gpui")),
        (HostPlatform::Tui, run(Target::Tui, "tui")),
        (HostPlatform::IosSimulator, run(Target::Ios, "ios")),
        (HostPlatform::Android, run(Target::Android, "android")),
        (HostPlatform::Web, run(Target::Web, "web")),
    ]
}

fn start_server(
    listen: Listen,
    assets_root: &Path,
    devices: Arc<Devices>,
) -> anyhow::Result<DevServer> {
    let Listen { port, network } = listen;
    if ui::stop_requested() {
        anyhow::bail!("development session is stopping");
    }
    let addr = SocketAddr::from((
        if network {
            [0, 0, 0, 0]
        } else {
            [127, 0, 0, 1]
        },
        port,
    ));
    DevServer::start(
        addr,
        nsh_js::BYTECODE_VERSION,
        Some(assets_root.to_path_buf()),
        move |event| devices.on_event(event),
    )
    .map_err(|error| {
        let message = if error.kind() == std::io::ErrorKind::AddrInUse {
            format!(
                "start dev server: port {port} is taken; stop what uses it or choose another --port"
            )
        } else {
            format!("start dev server on port {port}")
        };
        anyhow::Error::new(error).context(message)
    })
}

/// Waits out the debounce of a source change: whether one of the events
/// arriving until `deadline` changes the app's Rust code (`is_rust`; the
/// first that does ends the wait). Reads change nothing, as in the watch
/// loop.
fn rust_changed_until(
    rx: &mpsc::Receiver<notify::Event>,
    deadline: Instant,
    is_rust: impl Fn(&Path) -> bool,
) -> bool {
    while let Ok(event) = rx.recv_timeout(deadline.saturating_duration_since(Instant::now())) {
        if !matches!(event.kind, notify::EventKind::Access(_))
            && event.paths.iter().any(|p| is_rust(p))
        {
            return true;
        }
    }
    false
}

fn is_source(event: &notify::Event) -> bool {
    event.paths.iter().any(|p| {
        !p.components()
            .any(|c| c.as_os_str() == ".nsh" || c.as_os_str() == "node_modules")
            && matches!(
                p.extension().and_then(|e| e.to_str()),
                Some("ts" | "tsx" | "js" | "jsx" | "json")
            )
    })
}

/// Private IPv4 addresses of physical network interfaces (`en*`; on Linux
/// also `eth*` and Wi-Fi's `wl*`), where devices on the same Wi-Fi or
/// Ethernet can reach this machine. VPN and proxy tunnels are skipped.
#[cfg(unix)]
fn lan_ips() -> Vec<Ipv4Addr> {
    let mut ips = Vec::new();
    let mut list: *mut libc::ifaddrs = std::ptr::null_mut();
    // SAFETY: getifaddrs fills a linked list that we walk read-only and free
    // with freeifaddrs.
    unsafe {
        if libc::getifaddrs(&mut list) != 0 {
            return ips;
        }
        let mut cur = list;
        while let Some(ifa) = cur.as_ref() {
            cur = ifa.ifa_next;
            if ifa.ifa_addr.is_null() || i32::from((*ifa.ifa_addr).sa_family) != libc::AF_INET {
                continue;
            }
            let name = std::ffi::CStr::from_ptr(ifa.ifa_name).to_string_lossy();
            if !["en", "eth", "wl"].iter().any(|p| name.starts_with(p)) {
                continue;
            }
            let addr = &*(ifa.ifa_addr as *const libc::sockaddr_in);
            let ip = Ipv4Addr::from(u32::from_be(addr.sin_addr.s_addr));
            if ip.is_private() && !ips.contains(&ip) {
                ips.push(ip);
            }
        }
        libc::freeifaddrs(list);
    }
    ips
}

/// Private IPv4 addresses of the Ethernet and Wi-Fi adapters that are up
/// (by Windows' interface types: VPN, Hyper-V and WSL adapters are of
/// other types).
#[cfg(windows)]
fn lan_ips() -> Vec<Ipv4Addr> {
    use windows_sys::Win32::{
        Foundation::ERROR_BUFFER_OVERFLOW,
        NetworkManagement::{
            IpHelper::{
                GAA_FLAG_SKIP_ANYCAST, GAA_FLAG_SKIP_DNS_SERVER, GAA_FLAG_SKIP_MULTICAST,
                GetAdaptersAddresses, IF_TYPE_ETHERNET_CSMACD, IF_TYPE_IEEE80211,
                IP_ADAPTER_ADDRESSES_LH,
            },
            Ndis::IfOperStatusUp,
        },
        Networking::WinSock::{AF_INET, SOCKADDR_IN},
    };

    let mut ips = Vec::new();
    let flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER;
    // u64s keep the structures written into the buffer aligned.
    let mut buffer: Vec<u64> = vec![0; 2048];
    loop {
        let mut size = (buffer.len() * 8) as u32;
        // SAFETY: the buffer holds `size` writable bytes.
        let result = unsafe {
            GetAdaptersAddresses(
                u32::from(AF_INET),
                flags,
                std::ptr::null(),
                buffer.as_mut_ptr().cast(),
                &mut size,
            )
        };
        if result == ERROR_BUFFER_OVERFLOW {
            buffer = vec![0; (size as usize).div_ceil(8)];
            continue;
        }
        if result != 0 {
            return ips;
        }
        break;
    }
    let mut adapter = buffer.as_ptr().cast::<IP_ADAPTER_ADDRESSES_LH>();
    // SAFETY: the call filled the buffer with a linked list of adapters and
    // their addresses, all inside it, walked read-only while it lives.
    unsafe {
        while let Some(a) = adapter.as_ref() {
            adapter = a.Next;
            if a.OperStatus != IfOperStatusUp
                || !matches!(a.IfType, IF_TYPE_ETHERNET_CSMACD | IF_TYPE_IEEE80211)
            {
                continue;
            }
            let mut unicast = a.FirstUnicastAddress;
            while let Some(u) = unicast.as_ref() {
                unicast = u.Next;
                let addr = u.Address.lpSockaddr;
                if addr.is_null() || (*addr).sa_family != AF_INET {
                    continue;
                }
                let addr = &*(addr as *const SOCKADDR_IN);
                let ip = Ipv4Addr::from(u32::from_be(addr.sin_addr.S_un.S_addr));
                if ip.is_private() && !ips.contains(&ip) {
                    ips.push(ip);
                }
            }
        }
    }
    ips
}

#[cfg(test)]
mod tests {
    use super::Listen;

    fn on_network(port: u16) -> Listen {
        Listen {
            port,
            network: true,
        }
    }

    #[test]
    fn only_a_network_listen_is_open_to_other_machines() {
        let dir = std::env::temp_dir();
        let local = Listen {
            port: 0,
            network: false,
        };
        let server = super::start_server(local, &dir, Default::default()).unwrap();
        assert!(server.local_addr().ip().is_loopback());
        let server = super::start_server(on_network(0), &dir, Default::default()).unwrap();
        assert!(server.local_addr().ip().is_unspecified());
    }

    #[test]
    fn occupied_port_fails_without_moving_or_stopping_the_listener() {
        let taken = std::net::TcpListener::bind(("0.0.0.0", 0)).unwrap();
        let port = taken.local_addr().unwrap().port();
        let error =
            super::start_server(on_network(port), &std::env::temp_dir(), Default::default())
                .err()
                .expect("must not move to another port");
        assert!(error.to_string().contains(&format!("port {port}")));
        assert_eq!(
            error
                .root_cause()
                .downcast_ref::<std::io::Error>()
                .unwrap()
                .kind(),
            std::io::ErrorKind::AddrInUse
        );
        assert!(std::net::TcpStream::connect(("127.0.0.1", port)).is_ok());
    }

    #[test]
    fn zero_allocates_a_port_and_an_explicit_port_is_kept() {
        let dir = std::env::temp_dir();
        let server = super::start_server(on_network(0), &dir, Default::default()).unwrap();
        let port = server.local_addr().port();
        assert_ne!(port, 0);
        drop(server);
        let server = super::start_server(on_network(port), &dir, Default::default()).unwrap();
        assert_eq!(server.local_addr().port(), port);
    }

    #[test]
    fn devices_go_by_short_names_unless_two_share_one() {
        let connected = [
            "iOS Simulator (iPhone 16 Plus)".to_owned(),
            "Mac Catalyst".to_owned(),
        ];
        assert_eq!(
            super::short_name(&connected, &connected[0]),
            "iOS Simulator"
        );
        assert_eq!(
            super::short_name(&connected, "Mac Catalyst"),
            "Mac Catalyst"
        );
        let two = [
            "iOS Simulator (iPhone 16 Plus)".to_owned(),
            "iOS Simulator (iPad Air)".to_owned(),
        ];
        assert_eq!(super::short_name(&two, &two[1]), "iOS Simulator (iPad Air)");
    }

    #[test]
    fn a_rust_change_right_after_a_tsx_change_is_not_lost() {
        use std::{path::Path, time::Instant};

        use notify::{
            Event, EventKind,
            event::{AccessKind, DataChange, ModifyKind},
        };

        let saved = |path: &str| {
            Event::new(EventKind::Modify(ModifyKind::Data(DataChange::Content)))
                .add_path(path.into())
        };
        let is_rust = |path: &Path| path.extension().is_some_and(|e| e == "rs");
        let (tx, rx) = std::sync::mpsc::channel();
        // `App.tsx`, then `src/lib.rs` within the debounce (a checkout, a
        // formatter): the session starts again.
        tx.send(saved("/app/App.tsx")).unwrap();
        tx.send(saved("/app/src/lib.rs")).unwrap();
        let deadline = Instant::now() + super::DEBOUNCE;
        assert!(super::rust_changed_until(&rx, deadline, is_rust));
        // A read of the Rust code (inotify reports a build's own) is no
        // change: only the TSX is built again.
        tx.send(saved("/app/App.tsx")).unwrap();
        tx.send(Event::new(EventKind::Access(AccessKind::Read)).add_path("/app/src/lib.rs".into()))
            .unwrap();
        let deadline = Instant::now() + super::DEBOUNCE;
        assert!(!super::rust_changed_until(&rx, deadline, is_rust));
    }
}