summaryrefslogtreecommitdiff
path: root/test/process/tty_terminals.zig
blob: d55408188de436ca4b9f8dc0f9b87ba44e129876 (plain) (blame)
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
//! The tty frontend's own cases (test/tty.zig), terminal panes: tty mode, its keys and mouse, scrollback, commands.
const std = @import("std");
const Tty = @import("../tty.zig").Tty;

pub const cases = .{
    .{ "tty terminals: tty", tty },
    .{ "tty terminals: ttyclick", ttyclick },
    .{ "tty terminals: ttyedit", ttyedit },
    .{ "tty terminals: ttyfollow", ttyfollow },
    .{ "tty terminals: ttyhelp", ttyhelp },
    .{ "tty terminals: ttylook", ttylook },
    .{ "tty terminals: ttyonly", ttyonly },
    .{ "tty terminals: ttypaste", ttypaste },
    .{ "tty terminals: ttyprompt", ttyprompt },
    .{ "tty terminals: ttytaken", ttytaken },
    .{ "tty terminals: psearch", psearch },
    .{ "tty terminals: reflow", reflow },
    .{ "tty terminals: respawn", respawn },
    .{ "tty terminals: scroll", scroll },
    .{ "tty terminals: cmdexit", cmdexit },
    .{ "tty terminals: look-fd-deep", look_fd_deep },
    .{ "tty terminals: ghostty-mid", ghostty_mid },
    .{ "tty terminals: exec", exec },
    .{ "tty terminals: exec-bigdrag", exec_bigdrag },
};

// tty mode round-trip: Ctrl-b in, run a command, Ctrl-b out. Ctrl-b is the one
// switch between raw and normal. Shift-Esc (CSI 27;2u — needs a host that
// reports modifiers on Escape) never switches: in every mode it is what Escape
// is in normal mode — Last, the pane you were in before this one — and a raw
// terminal stays raw. The hop itself is covered by a unit test in pardes.zig.
fn tty(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "tty");
    defer t.deinit();
    try t.file("m.txt", "marker");
    try t.start(31, 100, &.{ "-n", "3" });
    try t.wait(8000, "m.txt");
    try t.stable(700, 20000);
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.snap("tty-on");
    try t.send("echo SNAP''OK");
    try t.key("enter");
    try t.wait(8000, "SNAPOK");
    try t.stable(700, 10000);
    try t.snap("tty-echo");
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.snap("tty-off");
    // Shift-Esc from normal: never into raw; it backs out to the previous pane.
    try t.send("\x1b[27;2u");
    try t.stable(600, 8000);
    try t.snap("shift-esc-normal");
    // Ctrl-b is the switch: into raw, and out again.
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.snap("ctrl-b-on");
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.snap("ctrl-b-off");
    try t.done();
}

// clicks in a shell pane never change its mode: a body click in raw tty
// keeps tty (before: the press dropped it to normal), a round trip through the
// tag leaves it alone (the tag has a mode of its own), and an insert-mode body
// click re-pins the cursor but stays insert. File panes keep the old behavior (click -> normal).
// The mode is the character in each pane's layout box: `•` normal, `^` insert,
// `$` tty.
fn ttyclick(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "ttyclick");
    defer t.deinit();
    try t.file("probe.txt", "marker");
    try t.start(31, 100, &.{ "-n", "3" });
    try t.wait(8000, "probe.txt");
    try t.stable(700, 20000);
    try t.key("c-b");
    try t.stable(600, 8000);
    // body click in raw tty: focus/selection only, the box stays `$`
    try t.press("left", "10", 5);
    try t.release("left", "10", 5);
    try t.stable(400, 5000);
    try t.snap("tty-bodyclick");
    // tag edit from tty: the box still reads `$` while the tag is typed into (the
    // tag's mode is its own: a clicked tag is in normal mode), and `:` hands the keyboard back — a bug
    // that lost the body's mode would show `•` in one snap or the other
    try t.press("left", "@Term", 3);
    try t.release("left", "@Term", 3);
    try t.stable(400, 5000);
    try t.snap("tty-tagedit");
    try t.key(":");
    try t.stable(400, 5000);
    try t.snap("tty-tagexit");
    // out of tty, into insert: a body click moves the cursor but keeps INS
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.key("i");
    try t.stable(400, 5000);
    try t.press("left", "12", 5);
    try t.release("left", "12", 5);
    try t.stable(400, 5000);
    try t.snap("ins-bodyclick");
    try t.done();
}

// editing a shell pane exactly like a document: Enter SPLITS the line under
// the cursor and pushes the shell rows below it down, `o` opens a fresh line,
// and backspace at column 0 joins back up. Before the edit buffer landed a
// terminal could only splice single-row insertion runs, so none of this was
// expressible. test/hxcases/parity.jsonl pins the keystroke-level equivalence
// with a file pane case for case; this is the end-to-end pty proof.
fn ttyedit(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "ttyedit");
    defer t.deinit();
    try t.file("words.txt", "alpha beta");
    try t.start(31, 100, &.{ "-n", "3" });
    try t.wait(8000, "words.txt");
    try t.stable(700, 20000);
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.send("printf 'one two three\\nfour five six\\nseven eight nine\\n'");
    try t.key("enter");
    try t.wait(8000, "seven");
    try t.stable(700, 10000);
    try t.key("c-b");
    try t.stable(600, 8000);
    // click into the middle of the first output row
    try t.press("left", "8", 9);
    try t.release("left", "8", 9);
    try t.stable(400, 5000);
    try t.snap("before");
    // Enter splits the row there: "one t" stays, "wo three" becomes a real line
    // of its own and the two output rows below it slide down
    try t.key("i enter");
    try t.stable(400, 5000);
    try t.snap("split");
    try t.key("esc");
    try t.stable(400, 5000);
    // `o` opens a line below the split's tail and pushes the rest down again
    try t.key("o");
    try t.send("OPEN");
    try t.key("esc");
    try t.stable(400, 5000);
    try t.snap("opened");
    // backspace at column 0 joins the opened line back onto its predecessor
    try t.key("i home bs");
    try t.key("esc");
    try t.stable(400, 5000);
    try t.snap("joined");
    // undo walks the whole session back out to untouched shell output
    try t.key("u u u");
    try t.stable(400, 5000);
    try t.snap("undone");
    try t.done();
}

// follow-output gate: a shell pane in normal/insert mode holds its scrolled-up
// view while new pty output streams in (before: only the ACTIVE pane held);
// back in tty mode output follows to the bottom again. Pane A parks a
// background job blocked on file `go`, pane B triggers it and syncs on `done1`.
fn ttyfollow(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "ttyfollow");
    defer t.deinit();
    try t.file("s.txt", "x");
    try t.start(31, 100, &.{ "-n", "3" });
    try t.wait(8000, "s.txt");
    try t.stable(700, 20000);
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.send("(until test -f go; do sleep 0.05; done; seq 201 260; touch done1) &");
    try t.key("enter");
    try t.stable(500, 8000);
    try t.send("seq 1 200");
    try t.key("enter");
    try t.wait(8000, "199");
    try t.stable(700, 15000);
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.wheel("up", "10", 8);
    try t.wheel("up", "10", 8);
    try t.wheel("up", "10", 8);
    try t.stable(500, 8000);
    try t.snap("held-pre");
    // focus pane B (right column), trigger A's job, sync on completion: A is now a
    // NON-active normal-mode pane receiving output — its view must not move
    try t.press("left", "60", 6);
    try t.release("left", "60", 6);
    try t.stable(400, 5000);
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.send("touch go; until test -f done1; do sleep 0.05; done; echo SYN''CED");
    try t.key("enter");
    try t.wait(8000, "SYNCED");
    try t.stable(700, 15000);
    try t.snap("held-post");
    // back in A, re-enter tty: the echoed keystrokes pull the view to the bottom
    try t.press("left", "10", 9);
    try t.release("left", "10", 9);
    try t.stable(400, 5000);
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.send("seq 261 300");
    try t.key("enter");
    try t.wait(8000, "300");
    try t.stable(700, 15000);
    try t.snap("tty-follows");
    try t.done();
}

// bare boot = tty mode: SPC belongs to the shell, so the topbar Help word is
// the only route to the command list. Row 0 is not a pane, so a middle-click
// there is dispatched before any pane's mode is consulted.
fn ttyhelp(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "ttyhelp");
    defer t.deinit();
    try t.start(31, 100, &.{});
    try t.wait(8000, "Newcol Joincol");
    try t.stable(700, 20000);
    try t.snap("boot");
    try t.press("middle", "@Help", 1);
    try t.release("middle", "@Help", 1);
    try t.wait(10000, "pardes builtins");
    try t.stable(700, 15000);
    try t.snap("helped");
    // and the shell is untouched: still TTY, still at its prompt. The click went to
    // row 0, which is not a pane, so the terminal never saw it.
    try t.key("c-b");
    try t.stable(600, 10000);
    try t.key("space");
    try t.key("?");
    // The bare-tty boot now opens an empty pane under the shell, so the index is
    // half as tall: wait on a row that is on screen. What the leader route
    // produced is the snapshot below, not this.
    try t.wait(10000, "SPC Y     ClipYankMain");
    try t.stable(700, 15000);
    try t.snap("leaderworks");
    try t.done();
}

// tty n/N token motion (normal mode only): n/N SELECT the next/prev
// whitespace-separated token that look resolves — several per line, so the
// greet ls row hops big.txt -> plain.txt -> sub — moving the cursor to the
// token start and opening NOTHING. Return looks the selection (dir -> the
// pane listing it, file -> pane) and focus follows what opened. The first
// n wraps from the prompt row to the first token; insert-mode n still
// types; and a pane with nothing look-able in it never traps the walk,
// because the walk is a RING ACROSS PANES rather than a scan of the one
// you are in.
fn ttylook(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "ttylook");
    defer t.deinit();
    try t.lines("big.txt", 40, "");
    try t.file("plain.txt", "just text");
    try t.dir("sub");
    try t.start(31, 120, &.{ "-n", "3" });
    try t.wait(8000, "big.txt");
    try t.stable(700, 20000);
    // first n: cursor sits at the shell prompt below the output — wrap lands on
    // the ls row's first token, big.txt; nothing opens
    try t.key("space j n");
    try t.stable(400, 5000);
    try t.snap("tok1");
    try t.style("tok1");
    // same row, next token: plain.txt
    try t.key("space j n");
    try t.stable(400, 5000);
    try t.snap("tok2");
    // N steps back to big.txt
    try t.key("space j p");
    try t.stable(400, 5000);
    try t.snap("tokback");
    // insert-mode n still types (a run splice), no stepping; undo it
    try t.key("i");
    try t.send("nn");
    try t.key("esc");
    try t.settle(100);
    try t.stable(400, 5000);
    try t.snap("insert");
    try t.key("u");
    try t.stable(400, 5000);
    // resume the walk from big.txt's start: plain.txt, then sub
    try t.key("space j n");
    try t.stable(400, 5000);
    try t.key("space j n");
    try t.stable(400, 5000);
    try t.snap("tok3");
    // Return looks the selected dir: sub's directory pane opens, empty as sub
    // is, and takes the keyboard
    try t.key("enter");
    try t.wait(10000, "sub/");
    try t.stable(700, 15000);
    try t.snap("diropen");
    // the fresh pane lists nothing, sub being empty, and that is not where n
    // stops: the ring is spun on the panes that have LOOKED, so the step leaves
    // this pane for the one it was looked out of. That pane's cursor is on its
    // last token (`sub`), so the walk runs off the end and comes round to its
    // first — big.txt.
    try t.key("space j n");
    try t.stable(400, 5000);
    try t.snap("noop");
    // back in the first pane, Enter with the cursor ON a token looks it (no
    // selection: the cursor word expands): the click puts it on the ls command
    // line's `ls`, a word no file names, which a terminal lists in a +Search,
    // its row the place in that terminal (@p1:1:1-2)
    try t.press("left", "4", 5);
    try t.release("left", "4", 5);
    try t.stable(400, 5000);
    try t.key("enter");
    try t.wait(10000, "@p1:1:1-2");
    try t.stable(700, 15000);
    try t.snap("cursorword");
    try t.done();
}

// --tty: a single fullscreen shell already in raw tty mode, prompts visible
fn ttyonly(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "ttyonly");
    defer t.deinit();
    try t.file("t.txt", "x");
    try t.start(31, 100, &.{"--tty"});
    try t.wait(8000, "$");
    try t.stable(700, 20000);
    try t.send("echo TTY''ONLY");
    try t.key("enter");
    try t.wait(8000, "TTYONLY");
    try t.stable(700, 10000);
    try t.snap("ttyonly");
    try t.style("ttyonly");
    try t.done();
}

// Pasting INTO the program a tty pane is running.
//
// `SPC p` and the acme 1-3 chord are both unreachable in raw tty: the pty owns
// every keystroke and every button. So tty mode has its own chord, and a paste
// arriving from outside has to reach the shell rather than an edit buffer that
// the pty does not know exists.
//
// Two paths, both ending at the shell's command line:
// Ctrl-V           types the DEFAULT register (config.tty_paste)
// bracketed paste  the outer terminal's own paste, or a desktop one relayed
// by a GUI host, which used to land in an overlay instead
fn ttypaste(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "ttypaste");
    defer t.deinit();
    try t.file("src.txt", "PASTED-PAYLOAD");
    try t.start(31, 100, &.{ "-n", "3" });
    try t.wait(8000, "src.txt");
    try t.stable(700, 20000);
    // fill the default register from a file pane: sweep the word and yank it
    try t.key("c-b");
    try t.stable(600, 8000);
    // in tty now; type a prefix so the pasted text is visibly appended to it
    try t.send("echo");
    try t.stable(600, 8000);
    // the outer terminal's bracketed paste: not a keystroke, no request behind it,
    // and it must arrive at the shell's line
    try t.send("\x1b[200~ BRACKETED\x1b[201~");
    try t.stable(600, 8000);
    try t.snap("bracketed-paste-reached-the-shell");
    // Ctrl-V types the register. Nothing was yanked in this session, so the
    // register is empty and the chord is INERT rather than sending the shell the
    // 0x16 it used to (quoted-insert): the line below is unchanged.
    try t.key("c-v");
    try t.stable(600, 8000);
    try t.snap("ctrl-v-with-empty-register-is-inert");
    try t.done();
}

// Leaving raw tty hides the shell's PROMPT and keeps the command typed at it.
//
// The two share one grid row, so blanking the row used to take the command with
// the prompt and a shell out of tty read as output only. OSC 133 marks the row
// CELL by cell, so config.tty_blank = .prompt cuts the prompt's own columns and
// left-hugs what is left: the command lands at column 0, in line with the
// output under it, rather than in a bay of blanks the prompt used to fill.
//
// `.prompt_and_input` is the old behaviour and blanks the whole row; it is a
// comptime choice, so what THIS script pins is the default.
fn ttyprompt(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "ttyprompt");
    defer t.deinit();
    try t.file("probe.txt", "marker");
    try t.start(31, 100, &.{ "-n", "3" });
    try t.wait(8000, "probe.txt");
    try t.stable(700, 20000);
    // the greeting `ls` was typed at a prompt and already shows the rule: the
    // command is there, the prompt in front of it is not
    try t.snap("greeted");
    // into tty and type a command WITHOUT submitting it, so the shell is sitting at
    // a prompt with input on it — the case the old behaviour erased
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.send("echo ABCDEFGH");
    try t.stable(600, 8000);
    try t.snap("in-tty");
    // ...and out: the prompt goes, `echo ABCDEFGH` stays, hugging the left edge
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.snap("out-of-tty");
    // The columns still have to reach the shell, and they do: the modal cursor's
    // column on a hidden-prompt row is short by exactly the prompt's width, so
    // enterTty adds it back before asking ghostty to walk the shell's own cursor
    // there. That arithmetic is pinned by the "entering tty walks the shell cursor"
    // unit test, which can put a pinned cursor on a known column; a click here
    // cannot, because leaving tty unpins and a body click on a terminal does not
    // pin it again.
    try t.done();
}

// Process monitoring, end to end. A terminal whose tty is held by a
// full-screen program is not somewhere a command line may be typed: the SAME
// Exec from that terminal goes into its shell while it sits at its prompt,
// and into a command pane of its own once `less` owns the terminal -- where
// it runs, instead of becoming keystrokes the pager would eat. Nothing about
// the pager's screen changes. (From any pane but a terminal, an Exec always
// runs as a command pane: exec.snap.)
fn ttytaken(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "ttytaken");
    defer t.deinit();
    try t.lines("big.txt", 40, "");
    try t.start(31, 210, &.{ "-n", "1" });
    try t.wait(8000, "big.txt");
    try t.stable(700, 20000);
    try t.snap("opened");
    // at the prompt: a command word in the terminal's own tag, executed, is
    // typed into its shell. `:` opens the tag, `A` appends, and the execute key
    // runs the word under the cursor.
    try t.key(":");
    try t.key("A");
    try t.key("space");
    try t.send("@`echo TAKEN''ONE`");
    try t.key("esc");
    try t.settle(100);
    try t.key("h");
    try t.key("tab");
    try t.wait(10000, "TAKENONE");
    try t.stable(700, 20000);
    try t.snap("at-prompt");
    // ...and now a pager owns that terminal. Ctrl-b is raw tty
    // (config.tty_toggle), which is how a script types AT the shell instead of
    // into the pane's own edit buffer.
    try t.key("c-b");
    try t.stable(400, 5000);
    try t.send("env LESS= less big.txt");
    try t.key("enter");
    try t.wait(10000, "line 1");
    try t.stable(700, 20000);
    try t.key("c-b");
    try t.stable(400, 5000);
    try t.snap("paging");
    // the same gesture from that terminal: a command pane, not `less` keystrokes
    try t.key(":");
    try t.key("A");
    try t.key("space");
    try t.send("@`echo TAKEN''TWO`");
    try t.key("esc");
    try t.settle(100);
    try t.key("h");
    try t.key("tab");
    try t.wait(10000, "TAKENTWO");
    try t.stable(700, 20000);
    try t.snap("spawned");
    try t.done();
}

// `/` search on a TERMINAL pane: the same tag input as a file pane, searching
// the shell's own scrollback (the plain text view Look reads, prompt rows
// hidden). Results land in a +Search OUTPUT BUFFER — a file pane with no file
// behind it, so its tag has no Save — and point at `@p1:LINE:COL-ENDCOL` (the terminal's serial), the
// look target that names a live pane, since a terminal has no path. n/N SELECT
// the next/prev row in the buffer and open nothing; Enter — the look chord —
// is what scrolls the terminal to that line of its scrollback and selects the
// span the row names.
// PLACEMENT, and this script is the proof: an output buffer is NOT a document
// — it is the result list of the pane that asked for it — so even here, with no
// doc pane in the session for it to join, it does NOT take a column of its own.
// It splits BELOW the shell it came from: every snap below stays ONE column,
// the shell's tag and the +Search tag both starting at x=0, thirty rows split
// between them, and nothing shifts sideways.
fn psearch(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "psearch");
    defer t.deinit();
    try t.start(31, 140, &.{});
    try t.wait(8000, "Newcol Joincol");
    try t.stable(700, 20000);
    // print something to search (a bare boot starts in tty mode; the marker is
    // split so the echoed command line, which normal mode hides anyway, can never
    // be the thing that matched)
    try t.send("printf 'x MA''RK a\\nzz\\nx MA''RK b\\nzz\\nx MA''RK c\\n'");
    try t.key("enter");
    try t.wait(10000, "MARK c");
    try t.stable(700, 10000);
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.snap("output");
    // / types the pattern into the terminal's tag exactly as on a file pane
    try t.key("/");
    try t.stable(400, 5000);
    try t.send("MARK");
    try t.stable(400, 5000);
    try t.snap("typing");
    // Enter opens the results buffer — three @p1:LINE:COL rows — and goes to the
    // first of them: the terminal's cursor lands on that scrollback line, on the
    // matched SPAN (cols 3..6, past the leading "x ") — not the start of the line,
    // and selected rather than merely parked on. No `n` was pressed to get here.
    try t.key("enter");
    try t.wait(10000, "@p1:2:3");
    try t.stable(700, 15000);
    try t.snap("results");
    try t.style("results");
    // n walks on to the next row, Enter looks it
    try t.key("space j n");
    try t.key("enter");
    try t.stable(700, 15000);
    try t.snap("second");
    // N walks back one row, Enter looks it
    try t.key("space j p");
    try t.key("enter");
    try t.stable(700, 15000);
    try t.snap("back");
    // a no-match search opens an empty buffer of its own — and n does not stop
    // there. The walk is a RING across panes, not one buffer's rows, so with
    // nothing look-able in the empty list it carries on into the one that does
    // have rows and resumes it a row past where the N above left off.
    try t.key("/");
    try t.send("zzqq");
    try t.key("enter");
    try t.stable(700, 10000);
    try t.key("space j n");
    try t.stable(400, 5000);
    try t.snap("nomatch");
    // right-clicking a word that names no file is the same search (acme button 3),
    // but NOT in tty mode: there the click belongs to the program on the other end,
    // so the buffer stays empty. Col 5 row 4 is the `M` of the first `x MARK a`,
    // in the shell's body in both modes (normal hides the prompt row, leaving it
    // blank, so the output rows sit at the same place either way).
    // The left click first is the price of the step above: it left the cursor in
    // the results buffer, and c-b is read by the ACTIVE pane — there is no tty
    // behind a +Search to toggle.
    try t.press("left", "5", 5);
    try t.release("left", "5", 5);
    try t.stable(400, 5000);
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.press("right", "5", 5);
    try t.release("right", "5", 5);
    try t.stable(700, 10000);
    try t.snap("ttyinert");
    // back in normal mode the same click searches for MARK
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.press("right", "5", 5);
    try t.release("right", "5", 5);
    try t.wait(10000, "@p1:2:3");
    try t.stable(700, 15000);
    try t.snap("rightsearch");
    try t.done();
}

// Widening a window whose scrollback holds WRAPPED lines, with the viewport
// scrolled into history: ghostty's resizeCols unwraps, then grows rows while
// the history viewport pin is still short, and its Debug-only PageList
// integrity check panicked the whole app ("ViewportPinInsufficientRows").
// build.zig now pins the ghostty dependency at ReleaseSafe so those
// development-only checks are off — the app's own safety checks are unchanged.
fn reflow(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "reflow");
    defer t.deinit();
    try t.start(25, 40, &.{});
    try t.wait(8000, "$");
    try t.send("printf 'y%.0s' $(seq 1 3000); echo do''ne");
    try t.key("enter");
    try t.stable(600, 15000);
    try t.stable(700, 15000);
    try t.wheel("up", "10", 10);
    try t.wheel("up", "10", 10);
    try t.wheel("up", "10", 10);
    try t.wheel("up", "10", 10);
    try t.stable(500, 8000);
    try t.resize(25, 200);
    try t.stable(700, 15000);
    try t.snap("widened");
    try t.done();
}

// a deleted shell pane's pty lingers in its slot (there is no close effect);
// a later spawn reusing the pane id must reap it instead of tripping the
// drainEffects spawn assert — this exact sequence crashed pre-fix
fn respawn(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "respawn");
    defer t.deinit();
    try t.start(31, 140, &.{ "-n", "3" });
    try t.wait(8000, "Newcol Joincol");
    try t.stable(700, 20000);
    // middle-click Del on the lower-left pane's tag: the pane id is freed while
    // its shell process stays alive in the slot
    try t.press("middle", "@Collapse+9", 18);
    try t.release("middle", "@Collapse+9", 18);
    try t.stable(700, 10000);
    // Alt-n reuses the freed id -> spawn effect lands on the occupied slot
    try t.key("a-n");
    try t.stable(700, 15000);
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.send("echo RESP''AWNOK");
    try t.key("enter");
    try t.wait(10000, "RESPAWNOK");
    try t.stable(700, 10000);
    try t.snap("respawn");
    try t.done();
}

// scrolling: wheel + scrollbar gutter clicks over real scrollback
fn scroll(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "scroll");
    defer t.deinit();
    try t.file("s.txt", "x");
    try t.start(31, 100, &.{ "-n", "3" });
    try t.wait(8000, "s.txt");
    try t.stable(700, 20000);
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.send("seq 1 200");
    try t.key("enter");
    try t.wait(8000, "199");
    try t.stable(700, 15000);
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.snap("bottom");
    try t.wheel("up", "10", 8);
    try t.wheel("up", "10", 8);
    try t.wheel("up", "10", 8);
    try t.stable(500, 8000);
    try t.snap("wheel3");
    try t.press("left", "1", 12);
    try t.release("left", "1", 12);
    try t.stable(500, 8000);
    try t.snap("gutter-up");
    try t.press("right", "1", 7);
    try t.release("right", "1", 7);
    try t.stable(500, 8000);
    try t.snap("gutter-down");
    try t.wheel("down", "10", 8);
    try t.stable(500, 8000);
    try t.snap("wheel-down");
    try t.style("wheel-down");
    try t.done();
}

// A command pane's command is over when its process exits, not when its pty
// closes. A job it leaves in the background holds the pty open, and the pane
// still says `exit 0` as soon as the command itself is done; a command that
// lets go of its terminal and runs on is not hung up at its pty's end, and
// says how it ended when it does.
fn cmdexit(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "cmdexit");
    defer t.deinit();
    try t.file("cmds.txt", "sleep 30 & echo BG''STARTED\nexec </dev/null >/dev/null 2>&1; sleep 1; false");
    try t.config("Verbose off");
    try t.start(31, 120, &.{"cmds.txt"});
    try t.wait(8000, "cmds.txt");
    try t.stable(700, 20000);
    try t.snap("opened");
    // line 1: `sleep 30` keeps the pty, and the pane says exit 0 regardless
    try t.press("middle", "7", 4);
    try t.drag("middle", "34", 4);
    try t.release("middle", "34", 4);
    try t.wait(10000, "exit 0");
    try t.stable(700, 15000);
    try t.snap("background");
    // line 2, in a second pane: the first's command is done but its background
    // `sleep 30` lives on (job control gives it its own group) and still holds
    // that pane's pty; here the end of file comes at once, and the exit a second
    // later, from `false`
    try t.press("middle", "7", 5);
    try t.drag("middle", "54", 5);
    try t.release("middle", "54", 5);
    try t.wait(10000, "exit 1");
    try t.stable(700, 15000);
    try t.snap("letgo");
    try t.done();
}

// A shell run under a wrapper exec'd over the one pardes started, that
// stays where it started (fish's own `exec 9ns --mntgen -- fish`; timeout
// here): after a cd the pane is where the foreground shell is, not where
// the wrapper is. So a path fd prints two
// directories down (b/c.txt) and a name ls prints (top.txt) look from there.
// The fd path goes first: the file it opens sits in a/b, a directory that
// could not rescue the ls name after it.
fn look_fd_deep(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "look-fd-deep");
    defer t.deinit();
    try t.dir("a");
    try t.dir("a/b");
    try t.file("a/top.txt", "top-marker");
    try t.file("a/b/c.txt", "deep-marker");
    try t.start(31, 100, &.{});
    try t.wait(8000, "$");
    try t.stable(700, 20000);
    try t.send("exec timeout --foreground 600 bash -i");
    try t.key("enter");
    try t.stable(700, 20000);
    try t.send("cd a && ls && fd");
    try t.key("enter");
    try t.wait(8000, "c.txt");
    try t.wait(8000, "cwd/a Term");
    try t.stable(700, 20000);
    try t.snap("listed");
    try t.press("right", "6", 8);
    try t.release("right", "6", 8);
    try t.wait(10000, "deep-marker");
    try t.stable(700, 15000);
    try t.snap("fd-path-looked");
    // the ls name, from the shell's directory
    try t.press("right", "8", 6);
    try t.release("right", "8", 6);
    try t.wait(10000, "top-marker");
    try t.stable(700, 15000);
    try t.snap("ls-name-looked");
    try t.done();
}

// ghostty's wire flavor of a middle click, byte-exact (mode 1002;1003;1004;1006
// as pardes requests): focus-in, per-cell hover motions (ghostty dedups per
// cell, so none repeat), press keeping SGR button identity, release 'm',
// trailing hover — split across writes like real input. Proves the tty input
// path (vaxis parse -> core execute) handles ghostty's encoding: the live bug
// ("middle click dead only in ghostty") is ghostty's GTK apprt swallowing
// middle press+release BEFORE mouse reporting whenever the desktop has
// gtk-enable-primary-paste=false (apprt/gtk/class/surface.zig, gcMouseDown/
// gcMouseUp) — the bytes below simply never get sent.
fn ghostty_mid(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "ghostty-mid");
    defer t.deinit();
    try t.file("g.txt", "x");
    try t.start(31, 100, &.{ "-n", "3" });
    try t.wait(8000, "g.txt");
    try t.stable(700, 20000);
    try t.press("left", "5", 9);
    try t.release("left", "5", 9);
    try t.stable(400, 5000);
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.send("printf '#!/bin/sh\\necho GHOST''OK\\n' > g.sh; chmod +x g.sh; clear; printf 'ZZPAD\\n./g.sh\\n'");
    try t.key("enter");
    try t.wait(10000, "./g.sh");
    try t.stable(700, 10000);
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.send("\x1b[I");
    try t.send("\x1b[<35;10;10M\x1b[<35;7;8M\x1b[<35;4;6M");
    try t.send("\x1b[<1;4;6M");
    try t.send("\x1b[<1;4;6m");
    try t.send("\x1b[<35;6;6M");
    try t.wait(10000, "GHOSTOK");
    try t.stable(700, 10000);
    try t.snap("ghostty-mid");
    try t.done();
}

// middle-drag executes the selection in the pane's shell; a left press during
// the middle drag captures the kept left selection as a trailing argument;
// middle-click Del on the pane tag closes the pane
fn exec(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "exec");
    defer t.deinit();
    try t.file("e.txt", "x");
    try t.start(31, 140, &.{ "-n", "3" });
    try t.wait(8000, "e.txt");
    try t.stable(700, 20000);
    try t.press("left", "5", 9);
    try t.release("left", "5", 9);
    try t.stable(400, 5000);
    try t.key("i");
    try t.send("echo MID''OK");
    try t.key("esc");
    try t.settle(100);
    try t.stable(400, 5000);
    try t.press("middle", "5", 9);
    try t.drag("middle", "16", 9);
    try t.release("middle", "16", 9);
    try t.wait(10000, "MIDOK");
    try t.stable(700, 10000);
    try t.snap("exec");
    // chord (canonical sequence, as in the prototype e2e): print an arg word and a
    // script path, left-drag-keep the arg, then middle-press on the path + left
    // press (captures) + middle release (runs with the arg appended)
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.send("printf '#!/bin/sh\\necho CHORDARG:\"$1\"\\n' > c.sh; chmod +x c.sh; clear; printf 'ZZARG\\n./c.sh\\n'");
    try t.key("enter");
    try t.wait(10000, "./c.sh");
    try t.stable(700, 10000);
    try t.key("c-b");
    try t.stable(600, 8000);
    try t.press("left", "3", 4);
    try t.drag("left", "7", 4);
    try t.release("left", "7", 4);
    try t.stable(400, 5000);
    try t.snap("kept");
    try t.send("\x1b[<1;4;5M\x1b[<0;4;5M\x1b[<1;4;5m");
    try t.wait(10000, "CHORDARG:ZZARG");
    try t.stable(700, 10000);
    try t.snap("chord");
    // middle-click Del in THIS pane's tag: row 2 carries both panes' tags, so the
    // occurrence is spelled out rather than left to the leftmost-match default
    try t.press("middle", "@Del#1", 3);
    try t.release("middle", "@Del#1", 3);
    try t.stable(700, 10000);
    try t.snap("deleted");
    try t.done();
}

// a giant accidental middle-drag: the whole selection is executed, chunked
// into 64-byte write effects in ONE update — this used to burst the fixed
// 64-slot effect ring and panic the core (assert in emit). Needs a big grid:
// the selection must exceed 64*64 bytes, more than any 30x100 pane can hold.
fn exec_bigdrag(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var t = try Tty.init(gpa, exe, "exec-bigdrag");
    defer t.deinit();
    try t.lines("big.txt", 3000, "");
    // Builtins announce themselves by default, and an announcement is a band at
    // the top of the body -- which moves every row this script clicks. What the
    // verbs do is what is under test, not the announcement.
    try t.config("Verbose off");
    try t.start(61, 200, &.{ "-n", "3" });
    try t.wait(8000, "big.txt");
    try t.stable(500, 20000);
    try t.press("left", "105", 9);
    try t.release("left", "105", 9);
    try t.stable(300, 5000);
    try t.key("i");
    try t.send("tr -d '\\n' < big.txt");
    try t.key("esc");
    try t.settle(100);
    try t.press("middle", "105", 9);
    try t.drag("middle", "126", 9);
    try t.release("middle", "126", 9);
    try t.stable(800, 30000);
    // the accidental sweep over the full-height right pane, then release
    try t.press("middle", "105", 5);
    try t.drag("middle", "150", 31);
    try t.drag("middle", "198", 59);
    try t.release("middle", "198", 59);
    try t.stable(500, 8000);
    try t.stable(800, 30000);
    // still alive: type and execute a marker command
    try t.press("left", "105", 41);
    try t.release("left", "105", 41);
    try t.stable(300, 5000);
    try t.key("i");
    try t.send("echo alive-o''k");
    try t.key("esc");
    try t.settle(100);
    try t.press("middle", "105", 41);
    try t.drag("middle", "119", 41);
    try t.release("middle", "119", 41);
    try t.wait(10000, "alive-ok");
    try t.stable(800, 20000);
    try t.snap("alive");
    try t.done();
}