crabshell-services 0.6.0

Crabshell's services: workspaces, tray, audio, clock, power, Bluetooth, network, media players, notifications, apps, programs, files, PAM, greetd and polkit
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
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
use std::cell::RefCell;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::time::{Duration, Instant};

use super::{DesktopEntries, Service, Sources, object_path};
use crate::testing::{FakeService, Harness, PrivateBus, TempDir, capture_logs, offline, reply};
use crate::{Config, ServicesExt};

/// A user's and a system's `applications` directory, and a `bin` for
/// `PATH`, in a directory of the test's own.
struct Dirs(TempDir);

impl Dirs {
  fn new() -> Dirs {
    let temp = TempDir::new();
    for dir in ["user", "system", "bin"] {
      std::fs::create_dir(temp.path().join(dir)).expect("a directory");
    }
    Dirs(temp)
  }

  fn user(&self) -> PathBuf {
    self.0.path().join("user")
  }

  fn system(&self) -> PathBuf {
    self.0.path().join("system")
  }

  fn bin(&self) -> PathBuf {
    self.0.path().join("bin")
  }

  fn sources(&self) -> Sources {
    Sources {
      dirs: vec![self.user(), self.system()],
      path: vec![self.bin()],
      ..Sources::default()
    }
  }

  /// A program in `bin` that writes its pid, its directory and its
  /// arguments, a line each, to `<name>.out` beside it.
  fn recorder(&self, name: &str) -> (PathBuf, PathBuf) {
    let program = self.bin().join(name);
    std::fs::write(
      &program,
      "#!/bin/sh\n{ echo $$; pwd; printf '%s\\n' \"$@\"; } > \"$0.tmp\" && mv \"$0.tmp\" \"$0.out\"\n",
    )
    .expect("a program");
    let mode = std::fs::Permissions::from_mode(0o755);
    std::fs::set_permissions(&program, mode).expect("its mode");
    let out = self.bin().join(format!("{name}.out"));
    (program, out)
  }
}

/// Writes `text` at `relative` under `dir`, with the directories it needs.
fn write(dir: &Path, relative: &str, text: &str) {
  let path = dir.join(relative);
  let parent = path.parent().expect("a file in a directory");
  std::fs::create_dir_all(parent).expect("its directory");
  std::fs::write(&path, text).expect("a desktop file");
}

/// An application named `name` that runs `exec`, and `extra` keys.
fn app(name: &str, exec: &str, extra: &str) -> String {
  format!("[Desktop Entry]\nType=Application\nName={name}\nExec={exec}\n{extra}\n")
}

fn shell(sources: Sources) -> (Harness, DesktopEntries) {
  let shell = Harness::new(Config {
    desktop_entries: sources,
    ..offline()
  });
  let entries = shell.cx.desktop_entries();
  (shell, entries)
}

fn run(shell: &mut Harness, ms: u64) -> usize {
  shell.run_until(Instant::now() + Duration::from_millis(ms))
}

fn ids(entries: &DesktopEntries) -> Vec<String> {
  let apps = entries.applications().get_untracked();
  apps.into_iter().map(|app| app.id).collect()
}

#[test]
fn nothing_is_read_before_the_loop_turns_after_the_first_ask() {
  let dirs = Dirs::new();
  write(&dirs.user(), "a.desktop", &app("A", "a", ""));
  let mut shell = Harness::new(Config {
    desktop_entries: dirs.sources(),
    ..offline()
  });
  let service = shell.cx.service::<Service>().expect("provided");
  run(&mut shell, 50);
  assert!(service.state.borrow().is_none());

  let entries = shell.cx.desktop_entries();
  assert_eq!(ids(&entries), Vec::<String>::new());
  assert!(!service.shared.watcher.is_open());
  run(&mut shell, 50);
  assert_eq!(ids(&entries), ["a"]);
}

#[test]
fn ids_come_from_the_path_and_the_list_is_sorted_by_name() {
  let dirs = Dirs::new();
  write(
    &dirs.user(),
    "kde4/konsole.desktop",
    &app("Konsole", "konsole", ""),
  );
  write(&dirs.user(), "a/b/c.desktop", &app("beta", "c", ""));
  write(&dirs.user(), "notes.txt", &app("Notes", "notes", ""));
  write(&dirs.system(), "x.desktop", &app("Alpha", "x", ""));
  write(&dirs.system(), "w.desktop", &app("alpha", "w", ""));
  // A linked directory is followed, and a link loop read once.
  let elsewhere = dirs.0.path().join("elsewhere");
  write(&elsewhere, "e.desktop", &app("Linked", "e", ""));
  std::os::unix::fs::symlink(&elsewhere, dirs.user().join("linked")).expect("a link");
  std::os::unix::fs::symlink(".", dirs.user().join("loop")).expect("a link");
  let (mut shell, entries) = shell(dirs.sources());
  run(&mut shell, 50);
  assert_eq!(
    ids(&entries),
    ["w", "x", "a-b-c", "kde4-konsole", "linked-e"]
  );
}

#[test]
fn the_first_directory_wins_and_hidden_removes_the_id() {
  let dirs = Dirs::new();
  write(&dirs.user(), "both.desktop", &app("User's", "u", ""));
  write(&dirs.system(), "both.desktop", &app("System's", "s", ""));
  write(
    &dirs.user(),
    "gone.desktop",
    "[Desktop Entry]\nHidden=true\n",
  );
  write(&dirs.system(), "gone.desktop", &app("Gone", "g", ""));
  write(&dirs.system(), "system.desktop", &app("System", "s", ""));
  // Listed twice, it is read once.
  let mut sources = dirs.sources();
  sources.dirs.push(dirs.system());
  let (mut shell, entries) = shell(sources);
  run(&mut shell, 50);
  let apps = entries.applications().get_untracked();
  let names: Vec<_> = apps.iter().map(|app| (&*app.id, &*app.name)).collect();
  assert_eq!(names, [("system", "System"), ("both", "User's")]);
}

#[test]
fn lookup_finds_a_windows_application() {
  let dirs = Dirs::new();
  for (id, extra) in [
    ("foot", ""),
    ("Code", ""),
    ("code", ""),
    ("alpha", ""),
    ("beta", "StartupWMClass=Alpha"),
    ("org.x.Gamma", ""),
    ("delta", "StartupWMClass=gamma"),
    ("org.gnome.Nautilus", ""),
    ("wm", "StartupWMClass=WM-Class-X"),
  ] {
    write(&dirs.user(), &format!("{id}.desktop"), &app(id, id, extra));
  }
  let (mut shell, entries) = shell(dirs.sources());
  run(&mut shell, 50);
  let found = |app_id| entries.lookup(app_id).map(|app| app.id);
  for (app_id, id) in [
    ("foot", "foot"),
    ("FOOT", "foot"),
    ("Code", "Code"),
    ("code", "code"),
    ("ALPHA", "alpha"),
    ("gamma", "delta"),
    ("nautilus", "org.gnome.Nautilus"),
    ("wm-class-x", "wm"),
  ] {
    assert_eq!(found(app_id).as_deref(), Some(id), "{app_id}");
  }
  assert_eq!(found("missing"), None);
  assert_eq!(found(""), None);
}

#[test]
fn a_change_shows_once_the_files_are_still() {
  let dirs = Dirs::new();
  write(&dirs.user(), "a.desktop", &app("A", "a", ""));
  let (mut shell, entries) = shell(dirs.sources());
  run(&mut shell, 50);
  let seen: Rc<RefCell<Vec<Option<String>>>> = Rc::default();
  let (sink, looked) = (Rc::clone(&seen), entries.clone());
  shell.cx.effect(move || {
    let found = looked.lookup("c").map(|app| app.name);
    sink.borrow_mut().push(found);
  });
  crabshell_reactive::Runtime::current().flush();

  write(&dirs.user(), "b.desktop", &app("B", "b", ""));
  run(&mut shell, 60);
  write(&dirs.system(), "c.desktop", &app("C", "c", ""));
  // B's 100 ms have passed, but not C's.
  run(&mut shell, 70);
  assert_eq!(ids(&entries), ["a"]);
  run(&mut shell, 300);
  assert_eq!(ids(&entries), ["a", "b", "c"]);
  assert_eq!(*seen.borrow(), [None, Some("C".to_owned())]);

  // One wakeup for the change, one for the read, one for the end of
  // the run.
  std::fs::remove_file(dirs.user().join("a.desktop")).expect("removed");
  assert!(run(&mut shell, 300) <= 3);
  assert_eq!(ids(&entries), ["b", "c"]);
}

#[test]
fn new_directories_are_watched() {
  let dirs = Dirs::new();
  let later = dirs.0.path().join("later");
  std::fs::create_dir(&later).expect("a directory");
  let sources = Sources {
    dirs: vec![dirs.user(), later.join("applications")],
    ..Sources::default()
  };
  let (mut shell, entries) = shell(sources);
  run(&mut shell, 50);

  std::fs::create_dir(dirs.user().join("sub")).expect("a directory");
  run(&mut shell, 300);
  write(&dirs.user(), "sub/x.desktop", &app("X", "x", ""));
  run(&mut shell, 300);
  assert_eq!(ids(&entries), ["sub-x"]);

  // An `applications` directory made after the first read.
  write(&later, "applications/y.desktop", &app("Y", "y", ""));
  run(&mut shell, 300);
  assert_eq!(ids(&entries), ["sub-x", "y"]);
  write(&later, "applications/z.desktop", &app("Z", "z", ""));
  run(&mut shell, 300);
  assert_eq!(ids(&entries), ["sub-x", "y", "z"]);
}

#[test]
fn an_idle_list_wakes_nothing() {
  let dirs = Dirs::new();
  write(&dirs.user(), "a.desktop", &app("A", "a", ""));
  write(&dirs.user(), "sub/b.desktop", &app("B", "b", ""));
  let (mut shell, entries) = shell(dirs.sources());
  run(&mut shell, 50);
  assert_eq!(ids(&entries), ["a", "sub-b"]);
  // The one wakeup is the end of the run.
  assert!(run(&mut shell, 2000) <= 1);
}

#[test]
fn a_file_written_in_place_beside_the_entries_reads_nothing() {
  use std::io::Write;
  let dirs = Dirs::new();
  write(&dirs.user(), "a.desktop", &app("A", "a", ""));
  write(&dirs.user(), "mimeapps.list", "");
  let (mut shell, entries) = shell(dirs.sources());
  run(&mut shell, 50);
  assert_eq!(ids(&entries), ["a"]);
  let service = shell.cx.service::<Service>().expect("provided");
  let waiting = || {
    service
      .state
      .borrow()
      .as_ref()
      .is_some_and(|state| state.waiting)
  };
  let path = dirs.user().join("mimeapps.list");
  let mut file = std::fs::OpenOptions::new().append(true).open(path).unwrap();
  file.write_all(b"[Default Applications]\n").unwrap();
  run(&mut shell, 20);
  assert!(!waiting(), "a write that isn't done counts for nothing");
}

#[test]
fn caps_leave_out_what_is_too_big_and_log_once() {
  let dirs = Dirs::new();
  let big = format!("{}# {}\n", app("Big", "big", ""), "x".repeat(70 * 1024));
  write(&dirs.user(), "big.desktop", &big);
  let long = app("Long", "long", &format!("Comment={}", "x".repeat(5000)));
  write(&dirs.user(), "long.desktop", &long);
  for n in 0..super::MAX_ENTRIES {
    write(
      &dirs.user(),
      &format!("many/{n:05}.desktop"),
      &app("Many", "many", ""),
    );
  }
  let logs = capture_logs(|| {
    let (mut shell, entries) = shell(dirs.sources());
    run(&mut shell, 50);
    let apps = entries.applications().get_untracked();
    // Big and long count toward the cap, so the last two are left out.
    assert_eq!(apps.len(), super::MAX_ENTRIES - 1);
    let long = apps
      .iter()
      .find(|app| app.id == "long")
      .expect("long, without its comment");
    assert_eq!(long.comment, "");
    assert!(
      !apps
        .iter()
        .any(|app| app.id == "big" || app.id == "many-many-09999")
    );
    // Read again, logging nothing more.
    write(&dirs.user(), "again.desktop", &app("Again", "again", ""));
    run(&mut shell, 300);
  });
  for line in ["over 64 KiB", "over 4 KiB", "over 10000 desktop entries"] {
    assert_eq!(logs.matches(line).count(), 1, "{line}: {logs}");
  }
}

#[test]
fn launch_runs_the_program_apart_and_reaps_it() {
  let dirs = Dirs::new();
  let (program, out) = dirs.recorder("record");
  let program = program.display();
  let workdir = dirs.0.path().display();
  let extra = format!(
    "Path={workdir}\nActions=second;\n[Desktop Action second]\nName=Second\n\
     Exec=\"{program}\" second %u"
  );
  let exec = format!("\"{program}\" %U --name %c");
  write(
    &dirs.user(),
    "record.desktop",
    &app("Record", &exec, &extra),
  );
  let (mut shell, entries) = shell(dirs.sources());
  run(&mut shell, 50);

  let ran = |shell: &mut Harness| {
    shell.retry_until("the program's output", || out.exists(), |_| {});
    let lines = std::fs::read_to_string(&out).expect("its output");
    std::fs::remove_file(&out).expect("removed");
    let lines: Vec<String> = lines.lines().map(str::to_owned).collect();
    let proc = PathBuf::from(format!("/proc/{}", lines[0]));
    // A zombie would keep its /proc entry.
    shell.retry_until("the reap", || !proc.exists(), |_| {});
    lines[1..].to_vec()
  };
  entries.launch("record");
  assert_eq!(ran(&mut shell), [&*workdir.to_string(), "--name", "Record"]);
  entries.launch_action("record", "second");
  assert_eq!(ran(&mut shell), [&*workdir.to_string(), "second"]);
}

#[test]
fn a_terminal_application_runs_in_xdg_terminal_exec() {
  let dirs = Dirs::new();
  let (_, out) = dirs.recorder("xdg-terminal-exec");
  write(
    &dirs.user(),
    "top.desktop",
    &app("Top", "htop -d 10", "Terminal=true"),
  );
  let (mut shell, entries) = shell(dirs.sources());
  run(&mut shell, 50);
  entries.launch("top");
  shell.retry_until("the terminal's output", || out.exists(), |_| {});
  let lines = std::fs::read_to_string(&out).expect("its output");
  assert_eq!(
    lines.lines().skip(2).collect::<Vec<_>>(),
    ["htop", "-d", "10"]
  );
}

#[test]
fn what_cant_start_is_logged_and_runs_nothing() {
  let dirs = Dirs::new();
  let (program, out) = dirs.recorder("record");
  let program = program.display();
  write(
    &dirs.user(),
    "bad.desktop",
    &app("Bad", &format!("\"{program}\" \"open"), ""),
  );
  let bus = "[Desktop Entry]\nType=Application\nName=Bus\nDBusActivatable=true\n";
  write(&dirs.user(), "bus.desktop", bus);
  let terminal = app("Term", &format!("\"{program}\""), "Terminal=true");
  write(&dirs.user(), "term.desktop", &terminal);
  let (mut shell, entries) = shell(dirs.sources());
  run(&mut shell, 50);
  let logs = capture_logs(|| {
    entries.launch("bad");
    entries.launch("bus");
    entries.launch("term");
    entries.launch("missing");
    entries.launch_action("bus", "missing");
    run(&mut shell, 300);
  });
  assert!(!out.exists());
  for line in [
    "a malformed Exec",
    "no Exec to fall back on",
    "xdg-terminal-exec isn't on PATH",
    "no application has this ID",
    "no such action",
  ] {
    assert!(logs.contains(line), "{line}: {logs}");
  }
}

#[test]
fn an_exec_the_spec_refuses_runs_as_a_shell_splits_it_logged_once() {
  let dirs = Dirs::new();
  let (program, out) = dirs.recorder("record");
  let exec = format!("{} --opt=\"a b\" 'c d' e\\ f", program.display());
  write(&dirs.user(), "loose.desktop", &app("Loose", &exec, ""));
  let (mut shell, entries) = shell(dirs.sources());
  run(&mut shell, 50);
  let logs = capture_logs(|| {
    for _ in 0..2 {
      entries.launch("loose");
      shell.retry_until("the program's output", || out.exists(), |_| {});
      let lines = std::fs::read_to_string(&out).expect("its output");
      std::fs::remove_file(&out).expect("removed");
      let args: Vec<&str> = lines.lines().skip(2).collect();
      assert_eq!(args, ["--opt=a b", "c d", "e f"]);
    }
  });
  assert_eq!(logs.matches("split as a shell would").count(), 1, "{logs}");
}

#[test]
fn a_d_bus_application_is_activated_and_falls_back_to_its_exec() {
  let dirs = Dirs::new();
  let (program, out) = dirs.recorder("record");
  let extra = "DBusActivatable=true\nActions=new-window;\n\
               [Desktop Action new-window]\nName=New Window\nExec=record --new";
  let notes = app("Notes", &format!("\"{}\" plain", program.display()), extra);
  write(&dirs.user(), "org.example.Notes.desktop", &notes);
  let bus = PrivateBus::start();
  let mut shell = Harness::new(Config {
    desktop_entries: dirs.sources(),
    session_bus: Some(bus.address.parse().expect("dbus-daemon's address")),
    ..offline()
  });
  let entries = shell.cx.desktop_entries();
  run(&mut shell, 50);
  let found = entries.lookup("org.example.Notes").expect("the entry");
  assert!(found.dbus_activatable);

  let fake = FakeService::start(&mut shell, &bus, "org.example.Notes");
  let actions: Rc<RefCell<Vec<String>>> = Rc::default();
  fake.on("Activate", |call| reply(call, &()));
  let named = Rc::clone(&actions);
  fake.on("ActivateAction", move |call| {
    type Body = (
      String,
      Vec<zbus::OwnedVariant>,
      std::collections::HashMap<String, zbus::OwnedVariant>,
    );
    let (action, parameters, _): Body = call.body().deserialize().expect("(sava{sv})");
    assert!(parameters.is_empty());
    named.borrow_mut().push(action);
    reply(call, &())
  });
  fake.own(&mut shell);
  let called = |member: &str| {
    let (calls, call) = (
      Rc::clone(&fake.calls),
      format!("{member} /org/example/Notes"),
    );
    move || calls.borrow().contains(&call)
  };
  entries.launch("org.example.Notes");
  shell.retry_until("Activate", called("Activate"), |_| {});
  entries.launch_action("org.example.Notes", "new-window");
  shell.retry_until("ActivateAction", called("ActivateAction"), |_| {});
  assert_eq!(*actions.borrow(), ["new-window"]);
  run(&mut shell, 100);
  assert!(!out.exists(), "Exec didn't run");

  // An app that got the call and failed may have started anyway, so
  // Exec doesn't run, as after a timeout.
  fake.on("Activate", |call| {
    use zbus::DBusError;
    Some(zbus::fdo::Error::Failed("busy".into()).create_reply(&call.header()))
  });
  let logs = capture_logs(|| {
    entries.launch("org.example.Notes");
    shell.retry_until("the failure", || fake.calls.borrow().len() == 3, |_| {});
    run(&mut shell, 200);
  });
  assert!(logs.contains("not running its Exec"), "{logs}");
  assert!(!out.exists(), "Exec didn't run");

  // Nobody owns the name now, and the bus starts nothing.
  fake.leave(&mut shell);
  let logs = capture_logs(|| {
    entries.launch("org.example.Notes");
    shell.retry_until("Exec instead", || out.exists(), |_| {});
  });
  assert!(
    logs.contains("couldn't start the application over D-Bus"),
    "{logs}"
  );
  let lines = std::fs::read_to_string(&out).expect("its output");
  assert_eq!(lines.lines().nth(2), Some("plain"));
}

#[test]
fn an_id_that_isnt_a_d_bus_name_runs_its_exec() {
  assert_eq!(
    object_path("org.gnome.Nautilus")
      .as_ref()
      .map(|path| path.as_str()),
    Some("/org/gnome/Nautilus")
  );
  assert_eq!(
    object_path("org.example.my-app")
      .as_ref()
      .map(|path| path.as_str()),
    Some("/org/example/my_app")
  );
  assert_eq!(object_path("firefox"), None, "one element");
  assert_eq!(object_path("org..x"), None);
  assert_eq!(object_path("1org.example"), None);

  let dirs = Dirs::new();
  let (program, out) = dirs.recorder("record");
  let exec = format!("\"{}\"", program.display());
  write(
    &dirs.user(),
    "firefox.desktop",
    &app("Firefox", &exec, "DBusActivatable=true"),
  );
  let (mut shell, entries) = shell(dirs.sources());
  run(&mut shell, 50);
  let logs = capture_logs(|| entries.launch("firefox"));
  shell.retry_until("Exec instead", || out.exists(), |_| {});
  assert!(logs.contains("isn't a D-Bus name"), "{logs}");
}

#[test]
fn a_linked_directory_added_or_removed_shows() {
  let dirs = Dirs::new();
  let elsewhere = dirs.0.path().join("elsewhere");
  write(&elsewhere, "linked.desktop", &app("Linked", "linked", ""));
  let (mut shell, entries) = shell(dirs.sources());
  run(&mut shell, 50);
  std::os::unix::fs::symlink(&elsewhere, dirs.user().join("foo")).expect("a link");
  run(&mut shell, 300);
  assert_eq!(ids(&entries), ["foo-linked"]);
  std::fs::remove_file(dirs.user().join("foo")).expect("the link removed");
  run(&mut shell, 300);
  assert!(ids(&entries).is_empty());
}

#[test]
fn a_link_on_the_way_to_a_root_swapped_for_another_reads_again() {
  // As NixOS links a user's profile: a link to a link to the store, with
  // the middle one swapped for a new generation.
  let dirs = Dirs::new();
  let base = dirs.0.path();
  write(
    &base.join("store/one"),
    "applications/x.desktop",
    &app("X", "x", ""),
  );
  write(
    &base.join("store/two"),
    "applications/y.desktop",
    &app("Y", "y", ""),
  );
  std::fs::create_dir(base.join("profiles")).expect("a directory");
  let profile = base.join("profiles/profile");
  std::os::unix::fs::symlink(base.join("store/one"), &profile).expect("a link");
  std::os::unix::fs::symlink("profiles/profile", base.join("home-profile")).expect("a link");
  // A link to itself ends the walk rather than the shell.
  std::os::unix::fs::symlink("loop", base.join("loop")).expect("a link");
  let sources = Sources {
    dirs: vec![
      base.join("loop/applications"),
      base.join("home-profile/applications"),
    ],
    ..Sources::default()
  };
  let (mut shell, entries) = shell(sources);
  run(&mut shell, 50);
  assert_eq!(ids(&entries), ["x"]);
  // Files saved beside the links, as dotfiles beside a NixOS profile in
  // `$HOME`, wake nothing.
  std::fs::write(base.join("profiles/notes"), "x").expect("a file");
  std::fs::write(base.join(".bashrc"), "x").expect("a file");
  std::fs::rename(base.join(".bashrc"), base.join(".profile")).expect("renamed");
  assert_eq!(run(&mut shell, 300), 1, "only the run's own deadline");

  let swap = |target: &str| {
    let new = base.join("profiles/profile.new");
    std::os::unix::fs::symlink(base.join(target), &new).expect("a link");
    std::fs::rename(&new, &profile).expect("swapped");
  };
  swap("store/two");
  run(&mut shell, 300);
  assert_eq!(ids(&entries), ["y"]);
  swap("store/one");
  run(&mut shell, 300);
  assert_eq!(ids(&entries), ["x"]);
  // Asleep again once read.
  assert!(run(&mut shell, 500) <= 1);
}

#[test]
fn a_fifo_named_like_an_entry_is_skipped() {
  let dirs = Dirs::new();
  write(&dirs.user(), "a.desktop", &app("A", "a", ""));
  let fifo = dirs.user().join("trap.desktop");
  let made = std::process::Command::new("mkfifo")
    .arg(&fifo)
    .status()
    .expect("mkfifo runs");
  assert!(made.success());
  let (mut shell, entries) = shell(dirs.sources());
  // Opening the FIFO would block here for good.
  run(&mut shell, 50);
  assert_eq!(ids(&entries), ["a"]);
}

/// What the shell was asked tokens for, and each request's callback.
type Asked = Rc<RefCell<Vec<(Option<String>, Box<dyn FnOnce(String)>)>>>;

/// Lets `shell`'s services ask for activation tokens, keeping each request
/// for the test to answer.
fn ask_for_tokens(shell: &Harness) -> Asked {
  let asked = Asked::default();
  let into = Rc::clone(&asked);
  crate::provide_activation_tokens(&shell.cx, move |app_id, done| {
    into.borrow_mut().push((app_id.map(str::to_owned), done));
  });
  asked
}

/// Answers the one request waiting with `token`, checking it named `id`.
fn answer(asked: &Asked, id: &str, token: &str) {
  let (app_id, done) = asked.borrow_mut().pop().expect("a token's request");
  assert!(asked.borrow().is_empty());
  assert_eq!(app_id.as_deref(), Some(id));
  done(token.to_owned());
}

#[test]
fn a_launched_program_gets_the_activation_token_and_nothing_stays_in_the_loop() {
  let dirs = Dirs::new();
  let tokens = dirs.bin().join("tokens");
  let script = format!(
    "#!/bin/sh\necho \"$XDG_ACTIVATION_TOKEN $DESKTOP_STARTUP_ID\" > \"{0}.tmp\" && mv \"{0}.tmp\" \"{0}\"\n",
    tokens.display()
  );
  let program = dirs.bin().join("tokened");
  std::fs::write(&program, script).expect("a program");
  std::fs::set_permissions(&program, std::fs::Permissions::from_mode(0o755)).expect("its mode");
  let exec = format!("\"{}\"", program.display());
  write(&dirs.user(), "tokened.desktop", &app("Tokened", &exec, ""));
  let (mut shell, entries) = shell(dirs.sources());
  let asked = ask_for_tokens(&shell);
  run(&mut shell, 50);

  entries.launch("tokened");
  run(&mut shell, 50);
  assert!(!tokens.exists(), "it waits for the token");
  answer(&asked, "tokened", "token-7");
  shell.retry_until("the program's output", || tokens.exists(), |_| {});
  let read = std::fs::read_to_string(&tokens).expect("its output");
  assert_eq!(read, "token-7 token-7\n");
  // Its exit and the end of the run wake the loop; no timer is left.
  let wakeups = run(&mut shell, 300);
  assert!(wakeups <= 2, "{wakeups} wakeups");
  assert_eq!(run(&mut shell, 300), 1);

  // A token that never comes: it starts after the wait, with none.
  std::fs::remove_file(&tokens).expect("removed");
  let launched = Instant::now();
  entries.launch("tokened");
  shell.retry_until("the program's output", || tokens.exists(), |_| {});
  assert!(launched.elapsed() >= super::activation::WAIT);
  let read = std::fs::read_to_string(&tokens).expect("its output");
  assert_eq!(read, " \n");
}

#[test]
fn a_d_bus_application_gets_the_activation_token_in_its_platform_data() {
  let dirs = Dirs::new();
  let extra = "DBusActivatable=true\nActions=new-window;\n\
               [Desktop Action new-window]\nName=New Window\nExec=true";
  write(
    &dirs.user(),
    "org.example.Notes.desktop",
    &app("Notes", "true", extra),
  );
  let bus = PrivateBus::start();
  let mut shell = Harness::new(Config {
    desktop_entries: dirs.sources(),
    session_bus: Some(bus.address.parse().expect("dbus-daemon's address")),
    ..offline()
  });
  let entries = shell.cx.desktop_entries();
  let asked = ask_for_tokens(&shell);
  run(&mut shell, 50);

  type Data = std::collections::HashMap<String, zbus::OwnedVariant>;
  let data: Rc<RefCell<Vec<Data>>> = Rc::default();
  let fake = FakeService::start(&mut shell, &bus, "org.example.Notes");
  let into = Rc::clone(&data);
  fake.on("Activate", move |call| {
    let (platform_data,): (Data,) = call.body().deserialize().expect("(a{sv})");
    into.borrow_mut().push(platform_data);
    reply(call, &())
  });
  let into = Rc::clone(&data);
  fake.on("ActivateAction", move |call| {
    type Body = (String, Vec<zbus::OwnedVariant>, Data);
    let (_, _, platform_data): Body = call.body().deserialize().expect("(sava{sv})");
    into.borrow_mut().push(platform_data);
    reply(call, &())
  });
  fake.own(&mut shell);

  entries.launch("org.example.Notes");
  answer(&asked, "org.example.Notes", "token-1");
  entries.launch_action("org.example.Notes", "new-window");
  answer(&asked, "org.example.Notes", "token-2");
  shell.retry_until("both calls", || data.borrow().len() == 2, |_| {});
  for (platform_data, token) in data.borrow().iter().zip(["token-1", "token-2"]) {
    for key in ["activation-token", "desktop-startup-id"] {
      let value = platform_data.get(key).expect("the token");
      assert_eq!(<&str>::try_from(&**value).ok(), Some(token), "{key}");
    }
  }
}