aster-completion 0.1.1

Natural shell completion
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
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::process::{Command, Stdio};
use std::thread;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use tempfile::tempdir;

#[test]
fn popup_preserves_highlights_and_shell_bindings() {
    if !command_exists("tmux") || !command_exists("zsh") {
        eprintln!("skipping ZLE integration test because tmux or zsh is unavailable");
        return;
    }

    let aster = Path::new(env!("CARGO_BIN_EXE_aster"));
    let binary_directory = aster.parent().unwrap();
    let temporary = tempdir().unwrap();
    let state = temporary.path().join("state");
    let zdotdir = temporary.path().join("zsh");
    fs::create_dir(&state).unwrap();
    fs::create_dir(&zdotdir).unwrap();
    fs::set_permissions(&state, fs::Permissions::from_mode(0o700)).unwrap();
    fs::set_permissions(&zdotdir, fs::Permissions::from_mode(0o700)).unwrap();

    let config = temporary.path().join("aster.toml");
    let socket = state.join("aster.sock");
    let state_dump = temporary.path().join("zle-state");
    let preview_file = temporary.path().join("preview-target.txt");
    fs::write(&preview_file, "preview-first-line\npreview-second-line\n").unwrap();
    let path = format!(
        "{}:{}",
        binary_directory.display(),
        std::env::var("PATH").unwrap_or_default()
    );
    let zshrc = format!(
        r#"autoload -Uz add-zle-hook-widget compinit
compinit
_aster_test_highlight() {{
  region_highlight=( "${{(@)region_highlight:#*memo=foreign-test*}}" )
  [[ -n "$BUFFER" ]] && region_highlight+=("0 ${{#BUFFER}} fg=green memo=foreign-test")
}}
add-zle-hook-widget line-pre-redraw _aster_test_highlight
_aster_test_native() {{
  sleep 0.12
  if (( CURRENT > 2 )); then
    compadd next-value
  else
    compadd native/path/file native/second/file native/third/file
  fi
}}
compdef _aster_test_native aster-native-fixture
_aster_test_file() {{
  compadd {preview_file}
}}
compdef _aster_test_file aster-preview-fixture
eval "$({aster} init zsh)"
_aster_test_dump_state() {{
  print -r -- "$_ASTER_MENU_ACTIVE|${{#_ASTER_MENU_ACCEPTS}}|$_ASTER_MENU_BUFFER|$BUFFER|${{_ASTER_MENU_ACCEPTS[1]}}|$_ASTER_MENU_INDEX|${{_ASTER_MENU_DISPLAYS[$_ASTER_MENU_INDEX]}}|$_ASTER_FUZZY_ACTIVE|$_ASTER_FUZZY_BASE|$_ASTER_FUZZY_QUERY|$_ASTER_PREVIEW_FD|$_ASTER_PREVIEW_TICKS|$_ASTER_PREVIEW_PATH|${{(j:;:)_ASTER_PREVIEW_LINES}}" > {state_dump}
}}
zle -N _aster_test_dump_state
bindkey '^X^D' _aster_test_dump_state
bindkey -M aster-fuzzy '^X^D' _aster_test_dump_state
PROMPT='%# '
"#,
        aster = shell_quote(aster.to_str().unwrap()),
        preview_file = shell_quote(preview_file.to_str().unwrap()),
        state_dump = shell_quote(state_dump.to_str().unwrap())
    );
    fs::write(zdotdir.join(".zshrc"), zshrc).unwrap();
    let shell_environment = format!(
        "PATH={} ZDOTDIR={} ASTER_CONFIG={} ASTER_STATE_DIR={} ASTER_SOCKET={}",
        shell_quote(&path),
        shell_quote(zdotdir.to_str().unwrap()),
        shell_quote(config.to_str().unwrap()),
        shell_quote(state.to_str().unwrap()),
        shell_quote(socket.to_str().unwrap()),
    );

    let unique = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_nanos();
    let server = format!("aster-ui-{}-{unique}", std::process::id());
    let mut guard = ServerGuard {
        server: server.clone(),
        aster: aster.to_owned(),
        config: config.clone(),
        state: state.clone(),
        socket: socket.clone(),
        started: false,
    };

    let status = Command::new("tmux")
        .args([
            "-L",
            &server,
            "-f",
            "/dev/null",
            "new-session",
            "-d",
            "-s",
            "test",
        ])
        .arg(format!("env {shell_environment} zsh -l"))
        .status()
        .unwrap();
    assert!(status.success(), "failed to start isolated tmux server");
    guard.started = true;

    thread::sleep(Duration::from_millis(300));
    let status = Command::new("tmux")
        .args(["-L", &server, "send-keys", "-l", "-t", "test:0.0", "aste"])
        .status()
        .unwrap();
    assert!(status.success(), "failed to type into isolated Zsh");

    let deadline = Instant::now() + Duration::from_secs(4);
    let mut capture = String::new();
    while Instant::now() < deadline {
        let output = Command::new("tmux")
            .args(["-L", &server, "capture-pane", "-p", "-e", "-t", "test:0.0"])
            .output()
            .unwrap();
        capture = String::from_utf8(output.stdout).unwrap();
        if capture.contains('') {
            break;
        }
        thread::sleep(Duration::from_millis(50));
    }

    assert!(
        capture.contains(''),
        "Aster popup did not appear:\n{capture}"
    );
    assert!(
        capture.contains("\u{1b}[32maste"),
        "existing syntax highlight was lost after popup rendering:\n{capture:?}"
    );

    let status = Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "C-Space"])
        .status()
        .unwrap();
    assert!(status.success(), "failed to accept the selected candidate");

    let deadline = Instant::now() + Duration::from_secs(2);
    let mut accepted_capture = String::new();
    while Instant::now() < deadline {
        accepted_capture = capture_pane(&server, true);
        if accepted_capture.contains("\u{1b}[32master") {
            break;
        }
        thread::sleep(Duration::from_millis(50));
    }
    assert!(
        accepted_capture.contains("\u{1b}[32master"),
        "accepted command was not fully highlighted:\n{accepted_capture:?}"
    );

    let binding_file = temporary.path().join("enter-binding");
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "C-c"])
        .status()
        .unwrap();
    thread::sleep(Duration::from_millis(50));
    let binding_command = format!(
        "{{ bindkey '^M'; bindkey '^I'; bindkey '^[[Z'; }} > {}",
        shell_quote(binding_file.to_str().unwrap())
    );
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-l", "-t", "test:0.0"])
        .arg(binding_command)
        .status()
        .unwrap();
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "Enter"])
        .status()
        .unwrap();

    let deadline = Instant::now() + Duration::from_secs(2);
    while Instant::now() < deadline && !binding_file.exists() {
        thread::sleep(Duration::from_millis(50));
    }
    let binding = fs::read_to_string(&binding_file).unwrap_or_else(|error| {
        panic!(
            "Enter did not execute bindkey: {error}\n{}",
            capture_pane(&server, false)
        )
    });
    assert!(binding.contains(r#""^M" accept-line"#));
    assert!(binding.contains(r#""^I" aster-tab"#));
    assert!(binding.contains("aster-shift-tab"));

    Command::new("tmux")
        .args([
            "-L",
            &server,
            "send-keys",
            "-l",
            "-t",
            "test:0.0",
            "echo before",
        ])
        .status()
        .unwrap();
    thread::sleep(Duration::from_millis(100));
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-l", "-t", "test:0.0"])
        .arg("\u{1b}[200~ pasted\u{1b}[201~")
        .status()
        .unwrap();
    dump_zle_state(&server);
    let pasted_state = fs::read_to_string(&state_dump).unwrap();
    let pasted_fields: Vec<_> = pasted_state.trim_end().split('|').collect();
    assert_eq!(pasted_fields[3], "echo before pasted");
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "C-c"])
        .status()
        .unwrap();
    thread::sleep(Duration::from_millis(50));

    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-l", "-t", "test:0.0"])
        .arg("aster-native-fixture na")
        .status()
        .unwrap();
    let immediate_capture = capture_pane(&server, false);
    assert!(
        !immediate_capture.contains("native/path/file"),
        "native completion ran synchronously instead of waiting for the debounce"
    );
    let deadline = Instant::now() + Duration::from_secs(4);
    let mut native_capture = String::new();
    while Instant::now() < deadline {
        native_capture = capture_pane(&server, false);
        if native_capture.contains("aster-native-fixture native/path/file") {
            break;
        }
        thread::sleep(Duration::from_millis(50));
    }
    assert!(
        native_capture.contains("aster-native-fixture native/path/file")
            && native_capture.contains("Zsh completion"),
        "debounced native Zsh candidate was absent:\n{native_capture}"
    );
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-l", "-t", "test:0.0", "t"])
        .status()
        .unwrap();
    let retained_capture = capture_pane(&server, false);
    assert!(
        retained_capture.contains('')
            && retained_capture.contains("aster-native-fixture native/path/file"),
        "matching candidates flashed off while the next request was pending:\n{retained_capture}"
    );
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "C-n", "C-n"])
        .status()
        .unwrap();
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "BTab"])
        .status()
        .unwrap();
    dump_zle_state(&server);
    let shifted_state = fs::read_to_string(&state_dump).unwrap();
    assert!(
        shifted_state.contains("|2|aster-native-fixture native/second/file"),
        "Shift-Tab did not select the next candidate: {shifted_state:?}"
    );

    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-l", "-t", "test:0.0", "i"])
        .status()
        .unwrap();
    dump_zle_state(&server);
    let typed_state = fs::read_to_string(&state_dump).unwrap();
    assert!(
        typed_state.contains("|aster-native-fixture nati|aster-native-fixture nati|"),
        "ordinary typing did not update the buffer after selection: {typed_state:?}"
    );
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "BSpace"])
        .status()
        .unwrap();
    let deadline = Instant::now() + Duration::from_secs(4);
    while Instant::now() < deadline {
        native_capture = capture_pane(&server, false);
        if native_capture.contains("aster-native-fixture native/path/file") {
            break;
        }
        thread::sleep(Duration::from_millis(50));
    }
    assert!(
        native_capture.contains("aster-native-fixture native/path/file"),
        "Backspace did not resume completion from the edited buffer:\n{native_capture}"
    );
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "C-n"])
        .status()
        .unwrap();
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "Tab"])
        .status()
        .unwrap();
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "C-x", "C-d"])
        .status()
        .unwrap();
    thread::sleep(Duration::from_millis(20));
    let segment_capture = capture_pane(&server, false);
    let state_after_tab = fs::read_to_string(&state_dump).unwrap();
    assert!(
        state_after_tab
            .contains("|aster-native-fixture native/|aster-native-fixture native/|path/file")
            && state_after_tab.contains("|2|aster-native-fixture native/second/file"),
        "Tab did not accept only the next path segment; state was {state_after_tab:?}:\n{segment_capture}"
    );
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "Tab"])
        .status()
        .unwrap();
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "C-x", "C-d"])
        .status()
        .unwrap();
    thread::sleep(Duration::from_millis(20));
    let second_segment_capture = capture_pane(&server, false);
    let state_after_second_tab = fs::read_to_string(&state_dump).unwrap();
    assert!(
        state_after_second_tab.contains(
            "|aster-native-fixture native/second/|aster-native-fixture native/second/|file"
        ),
        "a repeated Tab did not accept the next path segment; state was {state_after_second_tab:?}:\n{second_segment_capture}"
    );
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "Tab"])
        .status()
        .unwrap();
    let deadline = Instant::now() + Duration::from_secs(4);
    let mut follow_up_capture = String::new();
    while Instant::now() < deadline {
        follow_up_capture = capture_pane(&server, false);
        if follow_up_capture.contains("aster-native-fixture native/second/file next-value") {
            break;
        }
        thread::sleep(Duration::from_millis(50));
    }
    assert!(
        follow_up_capture.contains("aster-native-fixture native/second/file next-value"),
        "native completion did not continue after accepting a complete match:\n{follow_up_capture}"
    );

    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "C-c"])
        .status()
        .unwrap();
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-l", "-t", "test:0.0"])
        .arg("aster-native-fixture na")
        .status()
        .unwrap();
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "Tab"])
        .status()
        .unwrap();
    thread::sleep(Duration::from_millis(350));
    let tab_capture = capture_pane(&server, false);
    assert!(
        !tab_capture.contains("completion cannot be used recursively"),
        "ticker entered completion recursively during native Tab:\n{tab_capture}"
    );

    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "C-c"])
        .status()
        .unwrap();
    let source_command = format!(
        "zle -D _aster-native-space; source {}",
        shell_quote(zdotdir.join(".zshrc").to_str().unwrap())
    );
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-l", "-t", "test:0.0"])
        .arg(source_command)
        .status()
        .unwrap();
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "Enter"])
        .status()
        .unwrap();
    thread::sleep(Duration::from_millis(300));
    let rebound_file = temporary.path().join("reloaded-bindings");
    let rebound_command = format!(
        "{{ bindkey '^I'; bindkey '^[[Z'; bindkey '^@'; }} > {}",
        shell_quote(rebound_file.to_str().unwrap())
    );
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-l", "-t", "test:0.0"])
        .arg(rebound_command)
        .status()
        .unwrap();
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "Enter"])
        .status()
        .unwrap();
    let deadline = Instant::now() + Duration::from_secs(2);
    while Instant::now() < deadline && !rebound_file.exists() {
        thread::sleep(Duration::from_millis(20));
    }
    let rebound = fs::read_to_string(&rebound_file).unwrap();
    assert!(rebound.contains(r#""^I" aster-tab"#));
    assert!(rebound.contains("aster-shift-tab"));
    assert!(rebound.contains(r#""^@" aster-complete"#));

    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-l", "-t", "test:0.0", "aste"])
        .status()
        .unwrap();
    let deadline = Instant::now() + Duration::from_secs(4);
    let mut reloaded_capture = String::new();
    while Instant::now() < deadline {
        reloaded_capture = capture_pane(&server, false);
        if reloaded_capture.contains('') {
            break;
        }
        thread::sleep(Duration::from_millis(50));
    }
    assert!(
        reloaded_capture.contains(''),
        "Aster did not restart its ticker after .zshrc was sourced:\n{reloaded_capture}"
    );

    let status = Command::new(aster)
        .args([
            "record",
            "--command",
            "echo fuzzy-history-target",
            "--cwd",
            temporary.path().to_str().unwrap(),
            "--exit-code",
            "0",
            "--session",
            "fuzzy-test",
        ])
        .env("ASTER_CONFIG", &config)
        .env("ASTER_STATE_DIR", &state)
        .env("ASTER_SOCKET", &socket)
        .status()
        .unwrap();
    assert!(status.success(), "failed to seed fuzzy history");
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "C-c"])
        .status()
        .unwrap();
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-l", "-t", "test:0.0", "  fzht"])
        .status()
        .unwrap();
    let deadline = Instant::now() + Duration::from_secs(4);
    let mut fuzzy_capture = String::new();
    while Instant::now() < deadline {
        fuzzy_capture = capture_pane(&server, false);
        if fuzzy_capture.contains("echo fuzzy-history-target") {
            break;
        }
        thread::sleep(Duration::from_millis(50));
    }
    assert!(
        fuzzy_capture.contains("echo fuzzy-history-target"),
        "inline fuzzy history result was absent:\n{fuzzy_capture}"
    );
    dump_zle_state(&server);
    let fuzzy_state = fs::read_to_string(&state_dump).unwrap();
    let fuzzy_fields: Vec<_> = fuzzy_state.trim_end().split('|').collect();
    assert_eq!(fuzzy_fields[3], " ");
    assert_eq!(fuzzy_fields[7], "1");
    assert_eq!(fuzzy_fields[8], " ");
    assert_eq!(fuzzy_fields[9], "fzht");

    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "Escape"])
        .status()
        .unwrap();
    dump_zle_state(&server);
    let escaped_state = fs::read_to_string(&state_dump).unwrap();
    let escaped_fields: Vec<_> = escaped_state.trim_end().split('|').collect();
    assert_eq!(escaped_fields[3], " ");
    assert_eq!(escaped_fields[7], "0");
    assert_eq!(escaped_fields[9], "");

    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-l", "-t", "test:0.0", " fzht"])
        .status()
        .unwrap();
    let deadline = Instant::now() + Duration::from_secs(4);
    while Instant::now() < deadline {
        fuzzy_capture = capture_pane(&server, false);
        if fuzzy_capture.contains("echo fuzzy-history-target") {
            break;
        }
        thread::sleep(Duration::from_millis(50));
    }
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "Tab"])
        .status()
        .unwrap();
    dump_zle_state(&server);
    let accepted_fuzzy_state = fs::read_to_string(&state_dump).unwrap();
    let accepted_fields: Vec<_> = accepted_fuzzy_state.trim_end().split('|').collect();
    assert_eq!(accepted_fields[3], "echo fuzzy-history-target");
    assert_eq!(accepted_fields[7], "0");

    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "C-c"])
        .status()
        .unwrap();
    Command::new("tmux")
        .args([
            "-L",
            &server,
            "resize-window",
            "-t",
            "test:0",
            "-x",
            "140",
            "-y",
            "40",
        ])
        .status()
        .unwrap();
    thread::sleep(Duration::from_millis(50));
    Command::new("tmux")
        .args([
            "-L",
            &server,
            "send-keys",
            "-l",
            "-t",
            "test:0.0",
            "echo fuzzy",
        ])
        .status()
        .unwrap();
    let deadline = Instant::now() + Duration::from_secs(4);
    let mut history_capture = String::new();
    while Instant::now() < deadline {
        history_capture = capture_pane(&server, false);
        if history_capture.contains("echo fuzzy-history-target") {
            break;
        }
        thread::sleep(Duration::from_millis(50));
    }
    assert!(history_capture.contains("echo fuzzy-history-target"));
    assert!(
        !history_capture.contains("Preview:"),
        "history-only suggestions opened an unnecessary preview:\n{history_capture}"
    );
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-t", "test:0.0", "C-c"])
        .status()
        .unwrap();
    thread::sleep(Duration::from_millis(50));
    let preview_prefix = preview_file.to_str().unwrap().strip_suffix("txt").unwrap();
    Command::new("tmux")
        .args(["-L", &server, "send-keys", "-l", "-t", "test:0.0"])
        .arg(format!("aster-preview-fixture {preview_prefix}"))
        .status()
        .unwrap();
    let deadline = Instant::now() + Duration::from_secs(5);
    let mut preview_capture = String::new();
    while Instant::now() < deadline {
        preview_capture = capture_pane(&server, false);
        if preview_capture.contains("preview-first-line") {
            break;
        }
        thread::sleep(Duration::from_millis(50));
    }
    dump_zle_state(&server);
    let preview_state = fs::read_to_string(&state_dump).unwrap();
    assert!(
        preview_capture.contains("preview-first-line") && preview_capture.contains("Preview:"),
        "lazy wide-terminal file preview was absent; state was {preview_state:?}:\n{preview_capture}"
    );
    Command::new("tmux")
        .args([
            "-L",
            &server,
            "resize-window",
            "-t",
            "test:0",
            "-x",
            "80",
            "-y",
            "24",
        ])
        .status()
        .unwrap();

    let ascii_shell = format!("env LC_ALL=C LANG=C {shell_environment} zsh -l");
    let status = Command::new("tmux")
        .args([
            "-L",
            &server,
            "new-window",
            "-d",
            "-t",
            "test",
            "-n",
            "ascii",
        ])
        .arg(ascii_shell)
        .status()
        .unwrap();
    assert!(
        status.success(),
        "failed to start the non-UTF-8 Zsh fixture"
    );
    thread::sleep(Duration::from_millis(300));
    Command::new("tmux")
        .args([
            "-L",
            &server,
            "send-keys",
            "-l",
            "-t",
            "test:ascii.0",
            "aste",
        ])
        .status()
        .unwrap();
    let deadline = Instant::now() + Duration::from_secs(4);
    let mut ascii_capture = String::new();
    while Instant::now() < deadline {
        ascii_capture = capture_target(&server, "test:ascii.0", false);
        if ascii_capture.contains("+-") && ascii_capture.contains("aster") {
            break;
        }
        thread::sleep(Duration::from_millis(50));
    }
    assert!(
        ascii_capture.contains("+-") && ascii_capture.contains("| > "),
        "ASCII fallback menu was absent:\n{ascii_capture}"
    );
    assert!(
        !ascii_capture.contains("<e2>") && !ascii_capture.contains(''),
        "non-UTF-8 menu still contained Unicode rendering:\n{ascii_capture}"
    );
}

fn dump_zle_state(server: &str) {
    Command::new("tmux")
        .args(["-L", server, "send-keys", "-t", "test:0.0", "C-x", "C-d"])
        .status()
        .unwrap();
    thread::sleep(Duration::from_millis(20));
}

fn capture_pane(server: &str, include_escape_sequences: bool) -> String {
    capture_target(server, "test:0.0", include_escape_sequences)
}

fn capture_target(server: &str, target: &str, include_escape_sequences: bool) -> String {
    let mut command = Command::new("tmux");
    command.args(["-L", server, "capture-pane", "-p"]);
    if include_escape_sequences {
        command.arg("-e");
    }
    let output = command.args(["-t", target]).output().unwrap();
    String::from_utf8(output.stdout).unwrap()
}

fn command_exists(name: &str) -> bool {
    Command::new(name)
        .arg("-V")
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .is_ok()
}

fn shell_quote(value: &str) -> String {
    format!("'{}'", value.replace('\'', "'\\''"))
}

struct ServerGuard {
    server: String,
    aster: std::path::PathBuf,
    config: std::path::PathBuf,
    state: std::path::PathBuf,
    socket: std::path::PathBuf,
    started: bool,
}

impl Drop for ServerGuard {
    fn drop(&mut self) {
        if self.started {
            let _ = Command::new("tmux")
                .args(["-L", &self.server, "kill-server"])
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .status();
        }
        let _ = Command::new(&self.aster)
            .arg("stop")
            .env("ASTER_CONFIG", &self.config)
            .env("ASTER_STATE_DIR", &self.state)
            .env("ASTER_SOCKET", &self.socket)
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status();
    }
}