blubat 0.4.0

Bluetooth battery monitor for macOS: one-shot CLI readings, JSON output and a live TUI
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
//! The one-shot watches `blubat wait` hands over, and what a reading makes of
//! them.
//!
//! A wait registered against a running daemon is a file in the watch directory
//! and nothing else, so there is no socket, no daemon-side queue to query, and
//! nothing left behind by a wait that was interrupted. The daemon takes each
//! file over on the poll after it appears, deleting it as it does: a watch is
//! one notification, and a file still on disk is one a second reader could
//! notify from again.

use std::fs;
use std::path::{Path, PathBuf};

use blubat_core::{Device, Snapshot, Timestamp, Watch};

use crate::notify::{Banner, Notifier};
use crate::wait::completed;

/// The watches this daemon has taken over, held between readings.
///
/// In memory rather than on disk, which is what makes each of them exactly one
/// notification. A daemon that is restarted forgets the watches it had adopted,
/// the same trade the in-memory history makes: launchd restarts it only when it
/// fails, and a wait outliving that is what `--timeout` is for.
#[derive(Debug, Default)]
pub struct Watches {
    pending: Vec<Watch>,
}

/// What one reading makes of one watch.
#[derive(Clone, Debug, PartialEq, Eq)]
enum Verdict {
    /// The device reached the level the watch was registered for.
    Met {
        device: String,
        level: u8,
    },
    Waiting,
    /// Nothing paired matches the substring, so no reading will complete it.
    Unknown,
    /// The deadline passed with the level unmet.
    Expired,
}

impl Watches {
    /// Takes over every watch file in `directory`, deleting each as it goes.
    ///
    /// A file that cannot be read is deleted too and reported: leaving it would
    /// mean reporting it again on every poll for as long as the daemon runs.
    pub fn adopt(&mut self, directory: &Path) -> Vec<String> {
        files(directory)
            .into_iter()
            .map(|path| {
                let adopted = Watch::read(&path);
                let _ = fs::remove_file(&path);

                match adopted {
                    Ok(watch) => {
                        let line = format!("watching `{}` for {}%", watch.device, watch.target);
                        self.pending.push(watch);

                        line
                    }
                    Err(problem) => format!("{}: {problem}", path.display()),
                }
            })
            .collect()
    }

    /// Measures every held watch against one reading, notifying what it met.
    ///
    /// Each line it hands back is one watch that is now finished with, whether
    /// that is because it was met, because nothing matching it is paired, or
    /// because it ran out of time.
    pub fn settle(
        &mut self,
        reading: &Snapshot,
        sound: &str,
        now: Timestamp,
        notifier: &dyn Notifier,
    ) -> Vec<String> {
        let mut lines = Vec::new();
        let mut waiting = Vec::new();

        for watch in std::mem::take(&mut self.pending) {
            match verdict(&watch, reading, now) {
                Verdict::Waiting => waiting.push(watch),
                Verdict::Met { device, level } => {
                    lines.push(announce(&device, level, sound, notifier));
                }
                Verdict::Unknown => lines.push(format!(
                    "nothing paired matches `{}`, dropping the watch for {}%",
                    watch.device, watch.target
                )),
                Verdict::Expired => lines.push(format!(
                    "gave up watching `{}` for {}%",
                    watch.device, watch.target
                )),
            }
        }

        self.pending = waiting;

        lines
    }
}

/// The watch files in `directory`, in the order their names sort.
///
/// A directory that does not exist yet is simply empty: nothing under the state
/// directory exists until a wait first registers one.
fn files(directory: &Path) -> Vec<PathBuf> {
    let mut paths: Vec<PathBuf> = fs::read_dir(directory)
        .into_iter()
        .flatten()
        .filter_map(Result::ok)
        .map(|entry| entry.path())
        .filter(|path| {
            path.extension()
                .is_some_and(|extension| extension == "toml")
        })
        .collect();
    paths.sort();

    paths
}

/// Measures one watch against one reading.
///
/// Being met is decided before the deadline, so a watch whose level arrives in
/// the same reading its time runs out completes rather than expiring. A device
/// that is merely disconnected is waited on rather than dropped, because that
/// is the usual reason to be waiting on one; only a substring nothing paired
/// matches is hopeless, and only when the reading is in a position to say so.
fn verdict(watch: &Watch, reading: &Snapshot, now: Timestamp) -> Verdict {
    let reached = reading
        .matching(&watch.device)
        .find_map(|device| level_at_or_above(device, watch.target));

    match reached {
        Some((device, level)) => Verdict::Met { device, level },
        None if watch.deadline.is_some_and(|deadline| now >= deadline) => Verdict::Expired,
        None if unknown(watch, reading) => Verdict::Unknown,
        None => Verdict::Waiting,
    }
}

/// Whether this reading is entitled to say nothing paired matches the watch.
///
/// A reading that lost a source, or that arrived before the slow one had
/// answered for the first time, is missing devices rather than reporting that
/// they are not there. Dropping a live watch on one of those would discard it
/// for good over a source that is about to come back.
fn unknown(watch: &Watch, reading: &Snapshot) -> bool {
    !reading.degraded
        && !reading.devices.is_empty()
        && reading.matching(&watch.device).next().is_none()
}

/// The device's name and live level, once that level has reached `target`.
fn level_at_or_above(device: &Device, target: u8) -> Option<(String, u8)> {
    device
        .active_level()
        .filter(|level| *level >= target)
        .map(|level| (device.name.clone(), level))
}

/// Posts the banner a met watch owes, and says what became of it.
///
/// The wording is the one a foreground wait ends with, so which path notified
/// is not something the banner makes the reader work out.
fn announce(device: &str, level: u8, sound: &str, notifier: &dyn Notifier) -> String {
    let posted = notifier.post(&Banner::new("blubat", completed(device, level), sound));

    match posted {
        Ok(_) => format!("{device} reached {level}%"),
        Err(problem) => {
            format!("{device} reached {level}%, but the banner was not posted: {problem}")
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use blubat_core::{Address, ChargeState, Levels, Source};

    use crate::notify::fake::Recorder as PostedBanners;
    use crate::scratch::Scratch;

    use super::*;

    const TRACKPAD: &str = "Paul\u{2019}s Magic Trackpad";
    const READ_AT: i64 = 1_785_643_199;

    fn at(second: i64) -> Timestamp {
        Timestamp::from_unix(READ_AT + second)
    }

    fn reading(level: Option<u8>, connected: bool) -> Snapshot {
        Snapshot {
            read_at: at(0),
            devices: vec![Device {
                address: Address::parse("30-82-16-f2-24-90").expect("valid address"),
                name: TRACKPAD.to_string(),
                kind: None,
                transport: None,
                levels: Levels {
                    main: level,
                    ..Levels::default()
                },
                charge: ChargeState::Charging,
                source: Source::IoKit,
                connected,
                read_at: at(0),
            }],
            degraded: false,
            warnings: Vec::new(),
        }
    }

    fn watch(target: u8, deadline: Option<Timestamp>) -> Watch {
        Watch {
            device: "trackpad".to_string(),
            target,
            created: at(0),
            deadline,
        }
    }

    /// A watch directory holding the watches given, as `blubat wait` left them.
    fn registered(scratch: &Scratch, watches: &[Watch]) -> PathBuf {
        let directory = scratch.paths().watch_dir();

        for watch in watches {
            watch.write(&directory).expect("a written watch");
        }

        directory
    }

    fn holding(watches: &[Watch]) -> Watches {
        Watches {
            pending: watches.to_vec(),
        }
    }

    #[test]
    fn a_watch_file_is_taken_over_and_removed_on_the_first_poll() {
        let scratch = Scratch::new();
        let directory = registered(&scratch, &[watch(100, None)]);
        let mut watches = Watches::default();

        let lines = watches.adopt(&directory);

        assert_eq!(lines, ["watching `trackpad` for 100%"]);
        assert_eq!(files(&directory), Vec::<PathBuf>::new(), "nothing is left");
        assert_eq!(watches.pending.len(), 1);
    }

    #[test]
    fn a_directory_no_wait_has_ever_written_to_is_simply_empty() {
        let scratch = Scratch::new();
        let mut watches = Watches::default();

        assert!(watches.adopt(&scratch.paths().watch_dir()).is_empty());
    }

    #[test]
    fn a_watch_file_that_cannot_be_read_is_reported_once_and_deleted() {
        let scratch = Scratch::new();
        let directory = registered(&scratch, &[]);
        fs::create_dir_all(&directory).expect("a scratch directory");
        fs::write(directory.join("broken.toml"), "not a watch at all {{").expect("a written file");
        let mut watches = Watches::default();

        let lines = watches.adopt(&directory);

        assert_eq!(lines.len(), 1);
        assert!(lines[0].contains("unreadable"), "{lines:?}");
        assert!(
            watches.adopt(&directory).is_empty(),
            "and is gone, so it is not reported on every poll after it"
        );
    }

    #[test]
    fn reaching_the_target_notifies_once_and_completes_the_watch() {
        let banners = Arc::new(PostedBanners::new());
        let mut watches = holding(&[watch(100, None)]);

        let lines = watches.settle(&reading(Some(100), true), "Glass", at(0), &banners);

        assert_eq!(lines, [format!("{TRACKPAD} reached 100%")]);
        assert_eq!(banners.posted().len(), 1);
        assert_eq!(
            banners.posted()[0].body,
            format!("{TRACKPAD} is at 100%, safe to unplug.")
        );
        assert_eq!(banners.posted()[0].sound.as_deref(), Some("Glass"));
        assert!(
            watches
                .settle(&reading(Some(100), true), "Glass", at(0), &banners)
                .is_empty(),
            "a one-shot watch is finished with"
        );
        assert_eq!(banners.posted().len(), 1, "and notifies exactly once");
    }

    #[test]
    fn a_level_short_of_the_target_keeps_the_watch_waiting() {
        let banners = Arc::new(PostedBanners::new());
        let mut watches = holding(&[watch(100, None)]);

        assert!(
            watches
                .settle(&reading(Some(85), true), "Glass", at(0), &banners)
                .is_empty()
        );
        assert!(banners.posted().is_empty());

        let lines = watches.settle(&reading(Some(100), true), "Glass", at(0), &banners);

        assert_eq!(lines.len(), 1, "the reading that reached it ends the watch");
    }

    #[test]
    fn a_disconnected_device_is_waited_on_rather_than_dropped() {
        let banners = Arc::new(PostedBanners::new());
        let mut watches = holding(&[watch(100, None)]);

        assert!(
            watches
                .settle(&reading(Some(100), false), "Glass", at(0), &banners)
                .is_empty(),
            "a level nobody is reporting is not a crossing"
        );
        assert_eq!(watches.pending.len(), 1, "and the watch is still held");
    }

    #[test]
    fn a_watch_nothing_paired_matches_is_dropped_with_a_line_saying_so() {
        let banners = Arc::new(PostedBanners::new());
        let mut watches = holding(&[Watch {
            device: "zzz-no-such-device".to_string(),
            ..watch(100, None)
        }]);

        let lines = watches.settle(&reading(Some(100), true), "Glass", at(0), &banners);

        assert_eq!(lines.len(), 1);
        assert!(lines[0].contains("zzz-no-such-device"), "{lines:?}");
        assert!(banners.posted().is_empty());
        assert!(watches.pending.is_empty());
    }

    /// A device only `system_profiler` knows about is missing from a reading
    /// taken before it answered, and from one taken after it timed out. Neither
    /// is the reading saying nothing paired matches.
    #[test]
    fn a_reading_that_is_missing_a_source_does_not_drop_a_watch() {
        let banners = Arc::new(PostedBanners::new());
        let airpods = Watch {
            device: "airpods".to_string(),
            ..watch(100, None)
        };

        for incomplete in [
            Snapshot {
                degraded: true,
                ..reading(Some(85), true)
            },
            Snapshot {
                devices: Vec::new(),
                ..reading(Some(85), true)
            },
        ] {
            let mut watches = holding(std::slice::from_ref(&airpods));

            assert!(
                watches
                    .settle(&incomplete, "Glass", at(0), &banners)
                    .is_empty(),
                "{incomplete:?}"
            );
            assert_eq!(watches.pending.len(), 1, "the watch is still held");
        }
    }

    #[test]
    fn a_watch_past_its_deadline_is_dropped_unmet() {
        let banners = Arc::new(PostedBanners::new());
        let mut watches = holding(&[watch(100, Some(at(600)))]);

        assert!(
            watches
                .settle(&reading(Some(85), true), "Glass", at(599), &banners)
                .is_empty(),
            "there is still time"
        );

        let lines = watches.settle(&reading(Some(85), true), "Glass", at(600), &banners);

        assert_eq!(lines, ["gave up watching `trackpad` for 100%"]);
        assert!(banners.posted().is_empty());
    }

    #[test]
    fn a_level_arriving_as_the_deadline_does_completes_the_watch() {
        let banners = Arc::new(PostedBanners::new());
        let mut watches = holding(&[watch(100, Some(at(600)))]);

        let lines = watches.settle(&reading(Some(100), true), "Glass", at(600), &banners);

        assert_eq!(lines, [format!("{TRACKPAD} reached 100%")]);
        assert_eq!(banners.posted().len(), 1);
    }

    #[test]
    fn a_banner_that_could_not_be_posted_still_finishes_the_watch() {
        let banners = Arc::new(PostedBanners::failing("no notification centre"));
        let mut watches = holding(&[watch(100, None)]);

        let lines = watches.settle(&reading(Some(100), true), "Glass", at(0), &banners);

        assert_eq!(lines.len(), 1);
        assert!(lines[0].contains("no notification centre"), "{lines:?}");
        assert!(watches.pending.is_empty());
    }

    #[test]
    fn two_watches_on_one_device_are_taken_over_and_met_in_turn() {
        let scratch = Scratch::new();
        let banners = Arc::new(PostedBanners::new());
        let directory = registered(&scratch, &[watch(80, None), watch(100, None)]);
        let mut watches = Watches::default();

        assert_eq!(watches.adopt(&directory).len(), 2);
        assert_eq!(
            watches
                .settle(&reading(Some(85), true), "Glass", at(0), &banners)
                .len(),
            1,
            "the 80% one is met and the 100% one waits"
        );
        assert_eq!(
            watches
                .settle(&reading(Some(100), true), "Glass", at(1), &banners)
                .len(),
            1
        );
        assert_eq!(banners.posted().len(), 2);
    }
}