netrunner 2.0.4

netrunner — desktop internet speed-test & network diagnostics app (Zed GPUI) with live download/upload charts
Documentation
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
//! Application state and update logic for the GPUI desktop app.

use futures::StreamExt;
use gpui::{Context, SharedString};
use netrunner_core::{HistoryStorage, Settings, SpeedTestResult, TestConfig, TestEvent};

use crate::engine::spawn_speed_test;

/// Which transfer the live chart is currently tracking.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Phase {
    Idle,
    Locating,
    Servers,
    Latency,
    Download,
    Upload,
    Done,
}

impl Phase {
    pub fn label(self) -> &'static str {
        match self {
            Phase::Idle => "Ready",
            Phase::Locating => "Detecting location…",
            Phase::Servers => "Selecting servers…",
            Phase::Latency => "Measuring latency…",
            Phase::Download => "Testing download…",
            Phase::Upload => "Testing upload…",
            Phase::Done => "Complete",
        }
    }
}

/// Maximum number of samples kept for each live chart.
pub const MAX_SAMPLES: usize = 120;

/// Test servers cycled through by the settings panel.
pub const SERVER_PRESETS: &[&str] = &[
    "https://speed.cloudflare.com",
    "https://httpbin.org",
    "https://www.google.com",
    "https://fast.com",
];

/// The root application entity.
pub struct SpeedApp {
    pub config: TestConfig,
    pub settings: Settings,
    pub status: SharedString,
    pub phase: Phase,
    pub running: bool,

    pub download_samples: Vec<f32>,
    pub upload_samples: Vec<f32>,

    pub download_mbps: f32,
    pub upload_mbps: f32,
    pub peak_download: f32,
    pub peak_upload: f32,
    pub ping_ms: f32,

    pub location: Option<SharedString>,
    pub isp: Option<SharedString>,
    pub server: Option<SharedString>,
    pub result: Option<SpeedTestResult>,

    /// Recent past runs, newest first (read from the shared redb history).
    pub history: Vec<SpeedTestResult>,
}

impl SpeedApp {
    /// Build a fresh, side-effect-free state (no disk access — see [`Self::boot`]).
    pub fn new() -> Self {
        let settings = Settings::default();
        Self {
            config: settings.to_config(),
            settings,
            status: "Ready to jack in.".into(),
            phase: Phase::Idle,
            running: false,
            download_samples: Vec::new(),
            upload_samples: Vec::new(),
            download_mbps: 0.0,
            upload_mbps: 0.0,
            peak_download: 0.0,
            peak_upload: 0.0,
            ping_ms: 0.0,
            location: None,
            isp: None,
            server: None,
            result: None,
            history: Vec::new(),
        }
    }

    /// Load persisted settings (JSON) and history (redb) from disk.
    ///
    /// Kept separate from [`Self::new`] so unit tests stay pure and don't touch
    /// the user's real config/history files.
    pub fn boot(&mut self) {
        self.settings = Settings::load();
        self.config = self.settings.to_config();
        self.reload_history();
    }

    /// Re-read recent runs from the shared history database.
    pub fn reload_history(&mut self) {
        if let Ok(store) = HistoryStorage::new() {
            if let Ok(results) = store.get_recent_results(self.settings.max_history) {
                self.history = results;
            }
        }
    }

    /// Persist the most recent result to the shared history and refresh the list.
    ///
    /// Uses a single store handle for the write **and** the follow-up read, and
    /// surfaces failures on the status line instead of swallowing them. Saving
    /// the same result twice is harmless (identical timestamp key overwrites).
    fn persist_last_result(&mut self) {
        let Some(result) = self.result.clone() else {
            return;
        };
        match HistoryStorage::new() {
            Ok(store) => {
                if let Err(e) = store.save_result(&result) {
                    self.status = format!("Failed to save history: {e}").into();
                }
                if let Ok(results) = store.get_recent_results(self.settings.max_history) {
                    self.history = results;
                }
            }
            Err(e) => {
                self.status = format!("Failed to open history: {e}").into();
            }
        }
    }

    /// Toggle the "run a test on launch" preference and persist settings.
    pub fn toggle_auto_run(&mut self) {
        self.settings.auto_run = !self.settings.auto_run;
        let _ = self.settings.save();
    }

    /// Clear all saved history (and the on-screen list).
    pub fn clear_history(&mut self) {
        if let Ok(store) = HistoryStorage::new() {
            let _ = store.clear_history();
        }
        self.history.clear();
    }

    /// Rebuild the active [`TestConfig`] from settings and persist to JSON.
    fn apply_and_save_settings(&mut self) {
        self.config = self.settings.to_config();
        let _ = self.settings.save();
    }

    /// Cycle the test server through the built-in presets.
    pub fn cycle_server(&mut self) {
        let idx = SERVER_PRESETS
            .iter()
            .position(|s| *s == self.settings.server_url)
            .map(|i| (i + 1) % SERVER_PRESETS.len())
            .unwrap_or(0);
        self.settings.server_url = SERVER_PRESETS[idx].to_string();
        self.apply_and_save_settings();
    }

    /// Adjust the test payload size (MB), clamped to a sane range.
    pub fn adjust_size(&mut self, delta: i64) {
        let v = (self.settings.test_size_mb as i64 + delta).clamp(1, 1000);
        self.settings.test_size_mb = v as u64;
        self.apply_and_save_settings();
    }

    /// Adjust the per-test timeout (seconds), clamped to a sane range.
    pub fn adjust_timeout(&mut self, delta: i64) {
        let v = (self.settings.timeout_seconds as i64 + delta).clamp(5, 120);
        self.settings.timeout_seconds = v as u64;
        self.apply_and_save_settings();
    }

    /// Cycle the output detail level.
    pub fn cycle_detail(&mut self) {
        use netrunner_core::DetailLevel::*;
        self.settings.detail_level = match self.settings.detail_level {
            Basic => Standard,
            Standard => Detailed,
            Detailed => Debug,
            Debug => Basic,
        };
        self.apply_and_save_settings();
    }

    /// Kick off a full speed test and stream its progress into this entity.
    pub fn start(&mut self, cx: &mut Context<Self>) {
        if self.running {
            return;
        }

        // Reset state for a fresh run.
        self.running = true;
        self.phase = Phase::Locating;
        self.status = "Initialising neural interface…".into();
        self.download_samples.clear();
        self.upload_samples.clear();
        self.download_mbps = 0.0;
        self.upload_mbps = 0.0;
        self.peak_download = 0.0;
        self.peak_upload = 0.0;
        self.ping_ms = 0.0;
        self.location = None;
        self.isp = None;
        self.server = None;
        self.result = None;

        let rx = spawn_speed_test(self.config.clone());

        cx.spawn(async move |this, cx| {
            let mut rx = rx;
            while let Some(event) = rx.next().await {
                let keep_going = this
                    .update(cx, |app, cx| {
                        let completed = matches!(&event, TestEvent::Completed(_));
                        app.apply(event);
                        if completed {
                            // Save this run to the shared history (redb) and
                            // refresh the on-screen list.
                            app.persist_last_result();
                        }
                        cx.notify();
                    })
                    .is_ok();
                if !keep_going {
                    break;
                }
            }
            let _ = this.update(cx, |app, cx| {
                app.running = false;
                if app.phase != Phase::Done {
                    app.phase = Phase::Done;
                }
                // Safety net: guarantee the finished run is saved even if the
                // Completed event didn't persist it (idempotent — same key).
                if app.result.is_some() {
                    app.persist_last_result();
                }
                cx.notify();
            });
        })
        .detach();

        cx.notify();
    }

    /// Apply a single progress event to the state.
    pub fn apply(&mut self, event: TestEvent) {
        match event {
            TestEvent::Status(msg) => self.status = msg.into(),
            TestEvent::LocationDetected {
                city, country, isp, ..
            } => {
                self.location = Some(format!("{city}, {country}").into());
                self.isp = isp.map(Into::into);
                self.phase = Phase::Locating;
            }
            TestEvent::ServerPoolBuilt { .. } | TestEvent::NearbyServersFound { .. } => {
                self.phase = Phase::Servers;
            }
            TestEvent::ServersSelected { .. } => self.phase = Phase::Servers,
            TestEvent::PrimarySelected { name, location, .. } => {
                self.server = Some(format!("{name} · {location}").into());
                self.phase = Phase::Servers;
            }
            TestEvent::PhaseStarted(phase) => {
                use netrunner_core::Phase as CorePhase;
                self.phase = match phase {
                    CorePhase::Locating => Phase::Locating,
                    CorePhase::BuildingServers | CorePhase::SelectingServers => Phase::Servers,
                    CorePhase::Latency => Phase::Latency,
                    CorePhase::Download => Phase::Download,
                    CorePhase::Upload => Phase::Upload,
                    CorePhase::Jitter => self.phase,
                };
                self.status = phase.title().to_string().into();
            }
            TestEvent::DownloadSample {
                mbps, peak_mbps, ..
            } => {
                self.download_mbps = mbps as f32;
                self.peak_download = peak_mbps as f32;
                push_sample(&mut self.download_samples, mbps as f32);
            }
            TestEvent::UploadSample {
                mbps, peak_mbps, ..
            } => {
                self.upload_mbps = mbps as f32;
                self.peak_upload = peak_mbps as f32;
                push_sample(&mut self.upload_samples, mbps as f32);
            }
            TestEvent::DownloadComplete { mbps } => self.download_mbps = mbps as f32,
            TestEvent::UploadComplete { mbps } => self.upload_mbps = mbps as f32,
            TestEvent::LatencyProgress { avg_ms } | TestEvent::LatencyComplete { avg_ms } => {
                self.ping_ms = avg_ms as f32;
            }
            TestEvent::Completed(result) => {
                self.download_mbps = result.download_mbps as f32;
                self.upload_mbps = result.upload_mbps as f32;
                self.ping_ms = result.ping_ms as f32;
                self.phase = Phase::Done;
                self.status = "Test complete.".into();
                self.result = Some(*result);
            }
            TestEvent::JitterComplete { .. } | TestEvent::DiagnosticsComplete(_) => {}
        }
    }
}

impl Default for SpeedApp {
    fn default() -> Self {
        Self::new()
    }
}

fn push_sample(buf: &mut Vec<f32>, value: f32) {
    buf.push(value);
    if buf.len() > MAX_SAMPLES {
        let overflow = buf.len() - MAX_SAMPLES;
        buf.drain(0..overflow);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use netrunner_core::Phase as CorePhase;

    #[test]
    fn apply_download_sample_tracks_current_and_peak() {
        let mut app = SpeedApp::new();
        app.apply(TestEvent::PhaseStarted(CorePhase::Download));
        assert_eq!(app.phase, Phase::Download);

        app.apply(TestEvent::DownloadSample {
            mbps: 120.0,
            peak_mbps: 120.0,
            elapsed_secs: 1.0,
        });
        app.apply(TestEvent::DownloadSample {
            mbps: 80.0,
            peak_mbps: 120.0,
            elapsed_secs: 2.0,
        });

        assert_eq!(app.download_samples.len(), 2);
        assert_eq!(app.download_mbps, 80.0);
        assert_eq!(app.peak_download, 120.0);
    }

    #[test]
    fn sample_buffer_is_capped() {
        let mut app = SpeedApp::new();
        for i in 0..(MAX_SAMPLES + 50) {
            app.apply(TestEvent::UploadSample {
                mbps: i as f64,
                peak_mbps: i as f64,
                elapsed_secs: 0.0,
            });
        }
        assert_eq!(app.upload_samples.len(), MAX_SAMPLES);
        // Oldest samples were dropped, newest retained.
        assert_eq!(
            *app.upload_samples.last().unwrap(),
            (MAX_SAMPLES + 49) as f32
        );
    }

    #[test]
    fn completed_populates_result_and_metrics() {
        let mut app = SpeedApp::new();
        let result = netrunner_core::SpeedTestResult {
            download_mbps: 250.0,
            upload_mbps: 40.0,
            ping_ms: 12.0,
            ..Default::default()
        };

        app.apply(TestEvent::Completed(Box::new(result)));

        assert_eq!(app.phase, Phase::Done);
        assert!(app.result.is_some());
        assert_eq!(app.download_mbps, 250.0);
        assert_eq!(app.upload_mbps, 40.0);
        assert_eq!(app.ping_ms, 12.0);
    }

    #[test]
    fn location_event_is_formatted() {
        let mut app = SpeedApp::new();
        app.apply(TestEvent::LocationDetected {
            city: "Berlin".into(),
            country: "Germany".into(),
            isp: Some("Example ISP".into()),
            source: "ipapi.co".into(),
        });
        assert_eq!(
            app.location.as_ref().map(|s| s.to_string()),
            Some("Berlin, Germany".to_string())
        );
        assert_eq!(
            app.isp.as_ref().map(|s| s.to_string()),
            Some("Example ISP".to_string())
        );
    }

    #[test]
    fn completed_run_is_persisted_and_reloaded() {
        // Point the config dir at a throwaway HOME so we don't touch real data.
        let tmp = std::env::temp_dir().join(format!("nr_gui_persist_{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();
        std::env::set_var("HOME", &tmp);
        std::env::set_var("XDG_CONFIG_HOME", tmp.join(".config"));

        let mut app = SpeedApp::new();
        app.reload_history();
        assert_eq!(app.history.len(), 0, "fresh HOME should have empty history");

        app.result = Some(netrunner_core::SpeedTestResult {
            download_mbps: 100.0,
            upload_mbps: 20.0,
            ping_ms: 5.0,
            ..Default::default()
        });
        app.persist_last_result();

        assert_eq!(app.history.len(), 1, "run should be saved and reloaded");
        assert_eq!(app.history[0].download_mbps, 100.0);

        let _ = std::fs::remove_dir_all(&tmp);
    }
}