summaryrefslogtreecommitdiff
path: root/test/fs.py
blob: fa3388fefb51c96bf78c3debb80ed91d48f170ec (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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
#!/usr/bin/env python3
import contextlib
import fcntl
import json
import os
import shutil
from pathlib import Path
import subprocess
import struct
import sys
import tempfile
import termios
import threading
import time

from ninep import Client


@contextlib.contextmanager
def session(binary, root, name, *options, socket_name=None, file=None, tty=False, network=None,
            launch=None, inherited=None, terminal=None):
    # No pager of the caller's (a session's own PAGER, SYSTEMD_PAGER...):
    # its shells get the session's defaults, the ones under test.
    env = {key: value for key, value in os.environ.items()
           if not key.startswith('PARDES_') and 'PAGER' not in key}
    env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root),
               XDG_CONFIG_HOME=str(root / 'config'), PARDES_NOTIME='1',
               PARDES_DUMP=str(root / (name + '.dump.zon')))
    env.update(inherited or {})
    for language in ['RS', 'C', 'GO', 'TS', 'PY']:
        env['PARDES_LSP_' + language] = ''
    fixture = root / (name + '.txt')
    fixture.write_bytes(b'initial\n')
    address = root / ('pardes-9p-' + (socket_name or name) + '.sock')
    child = None
    master = slave = None
    reader = None
    try:
        args = [binary, '--detach=' + name, *options, str(file or fixture)]
        preexec = None
        if tty:
            master, slave = os.openpty()
            fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack('HHHH', 24, 80, 0, 0))
            env['TERM'] = 'xterm-256color'
            args = [binary, '--tty', '--9p=' + (socket_name or name), *options, str(file or fixture)]
            preexec = lambda: fcntl.ioctl(0, termios.TIOCSCTTY, 0)
        if launch is not None:
            args = [binary, *launch]
        existing = set(root.glob('pardes-9p-*.sock'))
        child = subprocess.Popen(args, cwd=root, env=env, stdin=slave,
                                 stdout=slave if tty else subprocess.DEVNULL,
                                 stderr=subprocess.PIPE, start_new_session=True,
                                 preexec_fn=preexec)
        if terminal is not None:
            # Borrowed handles for input-driving probes, valid inside this context.
            terminal.update(pid=child.pid, input_fd=master)
        if launch is not None and socket_name is None:
            # Named by the session's pid: a terminal's session is forked
            # off the process launched, which stays as its stub.
            address = None
        if slave is not None:
            os.close(slave)
            slave = None
            def drain():
                try:
                    while os.read(master, 65536):
                        pass
                except OSError:
                    pass
            reader = threading.Thread(target=drain, daemon=True)
            reader.start()
        deadline = time.monotonic() + 10
        while address is None or not address.exists():
            if address is None:
                address = next(iter(set(root.glob('pardes-9p-*.sock')) - existing), None)
                if address is not None:
                    continue
            if child.poll() is not None:
                raise AssertionError((args, child.returncode, child.stderr.read().decode()))
            if time.monotonic() > deadline:
                raise TimeoutError('default 9P socket did not appear')
            time.sleep(.005)
        with Client(address) as client:
            if network is not None:
                for dial in client.read('/listeners').decode().splitlines():
                    parts = dial.split('!')
                    if parts[0] in ['tcp', 'quic']:
                        assert len(parts) == 3 and 0 < int(parts[2]) < 65536, dial
                        network[parts[0].upper()] = int(parts[2])
                assert set(network) == {'TCP', 'QUIC'}, network
            yield client, address
    finally:
        if terminal is not None:
            terminal.clear()
        if child is not None:
            if child.poll() is None:
                child.terminate()
            try:
                child.wait(timeout=5)
            except subprocess.TimeoutExpired:
                child.kill()
                child.wait()
            child.stderr.close()
        if slave is not None:
            os.close(slave)
        if master is not None:
            os.close(master)
        if reader is not None:
            reader.join(timeout=1)
            assert not reader.is_alive(), 'terminal reader did not stop'


def newest(client):
    """The newest pane's serial: /index is ordered by serial."""
    return int(client.read('/index').splitlines()[-1].split()[0])


def new_pane(client, contents):
    """An open of /pane/new makes a pane; the read names it."""
    serial = int(client.read('/pane/new'))
    if contents:
        client.write(f'/pane/{serial}/body', contents)
    return serial


def look(client, path, source=None):
    """A right click on `path` from a pane (a fresh scratch by default)."""
    serial = source if source is not None else new_pane(client, b'')
    client.write(f'/pane/{serial}/look', f'{path}\n'.encode())
    results = [int(row) for row in client.read('/look').split()]
    assert len(results) == 1 and results[0] != serial, (path, results)
    # The scratch it looked from goes: a column holds only so many panes,
    # each its tag and two rows.
    if source is None:
        client.remove(f'/pane/{serial}')
    return results[0]


def execute(client, serial, command):
    """A middle click on `command` from a pane."""
    client.write(f'/pane/{serial}/exec', f'{command}\n'.encode())


def execute_refused(client, serial, command, words):
    """A middle click on a builtin that fails: the write fails, saying why."""
    try:
        execute(client, serial, command)
    except OSError as why:
        assert words in str(why), (command, why)
        return
    raise AssertionError(f'{command!r} was taken')


def walk_tree(client, path='/', skip=('/log', '/os', '/screen')):
    """Stat every entry below `path`, as `find` does, without opening anything."""
    seen = []
    for name in client.list(path):
        child = (path.rstrip('/') + '/' + name)
        info = client.stat(child)
        seen.append((child, info))
        if info['dir'] and child not in skip:
            seen.extend(walk_tree(client, child, skip))
    return seen


def discovery(binary, embedded=False):
    with tempfile.TemporaryDirectory(prefix='pardes-discovery-') as directory:
        root = Path(directory)
        with session(binary, root, 'discovery') as (client, address):
            top = client.list('/')
            # A set: which files the root holds, not the order a listing gives.
            assert set(top) - {'src', 'shaders'} == {'README', 'index', 'status', 'focus', 'ctl', 'commands', 'recent',
                                                     'look', 'exec', 'pager', 'log', 'screen', 'listeners', 'layout', 'tag', 'tagexec',
                                                     'pane', 'col', 'os'}, top
            assert 'self' not in top and 'cons' not in top and 'new' not in top, top
            assert ('src' in top) == embedded, (top, embedded)
            before = client.read('/index')
            guide = client.read('/README')
            assert guide.count(b'\n') <= 45 and b'Msg hello' in guide and b'pane/new' in guide, guide
            assert client.stat('/README')['length'] == len(guide)
            # ls/stat/find over the whole tree, without opening, creates nothing.
            seen = walk_tree(client)
            names = {path for path, _ in seen}
            assert {'/index', '/status', '/look', '/exec', '/log', '/pane/1', '/pane/1/body',
                    '/pane/1/name', '/pane/1/sel', '/pane/1/ctl', '/pane/1/tag', '/pane/1/event',
                    '/pane/1/dot', '/pane/1/limit', '/pane/1/dirty', '/pane/1/mark',
                    '/pane/1/scroll', '/pane/1/look', '/pane/1/exec'} <= names, sorted(names)
            assert '/pane/1/pty' not in names, 'the fixture file pane grew a pty/'
            assert not any(name.startswith('/self') or name.endswith('/rdsel') or name.endswith('/wrsel')
                           for name in names), sorted(names)
            for path, info in seen:
                assert info['mode'] in (0o755, 0o644, 0o666, 0o444, 0o222, 0o555), (path, info)
                assert info['mtime'] > 0, (path, info)
            assert client.read('/index') == before, 'browsing created a pane'
            assert client.stat('/index')['length'] == len(before) > 0
            index = client.stat('/index')
            assert index['mode'] == 0o444 and client.stat('/status')['mode'] == 0o444
            assert client.stat('/exec')['mode'] == 0o666
            assert client.stat('/pane/1/body')['mode'] == 0o666 and client.stat('/pane')['mode'] == 0o755
            # A range file reads the pair it takes; a flag file reads 0 or 1.
            assert client.stat('/pane/1/addr')['length'] == 24
            assert client.stat('/pane/1/limit')['length'] == 0
            assert client.read('/pane/1/dirty') == b'0\n'
            assert client.read('/pane/1/scroll') == b'1\n'
            fixture = int(before.split()[0])
            assert before.split()[1:3] == [b'text', b'0'] and before.rsplit(b' ', 1)[0].endswith(b'/discovery.txt'), before
            # /status reports the editor; /exec has nothing to report yet.
            status = dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())
            assert int(status['pid']) > 0 and status['version'] and int(status['panes']) >= 1, status
            assert client.read('/exec') == b''
            # An open of /pane/new makes a pane and the read names it; each
            # open makes another. A stat makes none, which is why new can be
            # listed at all: ls -l stats every name a listing gave it.
            # /log answers the recent past and ends; `follow` written to the
            # same open makes its reads past that wait for new records.
            log = client.open('/log', 2)
            frozen = len(client.read_fid(log))
            client.rpc(118, struct.pack('<IQI', log, 0, 7) + b'follow\n')
            first = int(client.read('/pane/new'))
            assert first == newest(client)
            second = int(client.read('/pane/new'))
            assert first != second and first != fixture, (first, second)
            assert 'new' in client.list('/pane'), 'new should be visible to ls'
            client.stat('/pane/new')
            assert newest(client) == second, 'a stat of new made a pane'
            # What the editor said is in the stream too.
            def pane_event():
                while (record := client.read_fid(log, frozen)).startswith((b'msg ', b'err ')):
                    pass
                return record
            assert pane_event() == f'new {first} {root}/+New\n'.encode()
            assert pane_event() == f'new {second} {root}/+New\n'.encode()
            # A follower that is waiting wakes when the editor logs, not when
            # some unrelated request next happens to wait.
            with Client(address) as follower:
                waiting = follower.open('/log', 2)
                start = len(follower.read_fid(waiting))
                follower.rpc(118, struct.pack('<IQI', waiting, 0, 7) + b'follow\n')
                woke = []
                reader = threading.Thread(target=lambda: woke.append(follower.read_fid(waiting, start)), daemon=True)
                reader.start()
                time.sleep(.2)
                client.write('/exec', b'Msg woken\n')
                reader.join(5)
                assert woke and woke[0].endswith(b' woken\n'), woke
            # A pane's ctl takes acme's lock: a second open's lock fails at
            # once with "file in use" until the holder unlocks, and nothing
            # else waits on it meanwhile.
            with Client(address) as other:
                mine = client.open(f'/pane/{first}/ctl', 2)
                client.rpc(118, struct.pack('<IQI', mine, 0, 5) + b'lock\n')
                theirs = other.open(f'/pane/{first}/ctl', 1)
                try:
                    other.rpc(118, struct.pack('<IQI', theirs, 0, 5) + b'lock\n')
                    raise AssertionError('a second lock was taken')
                except OSError as refused:
                    assert 'file in use' in str(refused), refused
                client.write(f'/pane/{first}/addr', b'#0')
                client.rpc(118, struct.pack('<IQI', mine, 0, 7) + b'unlock\n')
                other.rpc(118, struct.pack('<IQI', theirs, 0, 5) + b'lock\n')
                client.close(mine)
                other.close(theirs)
            # /focus names the pane with the keyboard and moves it, as rio's
            # wctl `current` does; a pane's ctl line ends saying which it is.
            client.write('/focus', f'{first}\n'.encode())
            assert client.read('/focus') == f'{first}\n'.encode()
            assert b' current ' in client.read(f'/pane/{first}/ctl')
            assert b' notcurrent ' in client.read(f'/pane/{second}/ctl')
            try:
                client.write('/focus', b'99999\n')
                raise AssertionError('focus took a serial no pane has')
            except OSError as refused:
                assert 'no such pane' in str(refused), refused
            # /ctl reads the settings as a write takes them and takes the
            # session's builtins; a pane's ctl takes the pane's.
            settings = client.read('/ctl')
            assert b'Verbose ' in settings and b'Platform' not in settings, settings
            client.write('/ctl', settings)
            assert client.read('/ctl') == settings
            client.write('/ctl', b'Verbose off\n')
            assert b'Verbose off\n' in client.read('/ctl')
            for path, line, why in (('/ctl', b'Del\n', 'not a session control message "Del"'),
                                    ('/ctl', b'Nope 3\n', 'unknown control message "Nope 3"'),
                                    (f'/pane/{first}/ctl', b'Newcol\n', 'not a window control message "Newcol"')):
                try:
                    client.write(path, line)
                    raise AssertionError(f'{path} took {line!r}')
                except OSError as refused:
                    assert why in str(refused), refused
            commands = client.read('/commands').decode().splitlines()
            def row(start):
                return any(r == start or r.startswith(start + ' ') for r in commands)
            assert row('Newcol root') and row('Save arg pane') and row('Edit arg both'), commands
            # word, `arg` if it takes one, its ctl, a setting's values, then `-- ` what it does
            assert all(r.split()[2 if r.split()[1] == 'arg' else 1] in ('root', 'pane', 'both') for r in commands), commands
            assert row('Placement arg root acme,pardes'), commands
            assert client.stat('/commands')['length'] == len('\n'.join(commands).encode()) + 1  # bytes: a description may end in …
            client.write(f'/pane/{first}/ctl', b'Msg said through ctl\n')
            assert 'said through ctl' in ''.join(cell[0] for cell in client.screen()['cells'])
            assert set(client.list('/pane')) == {'new', str(fixture), str(first), str(second)}
            client.write(f'/pane/{first}/body', b'first pane', truncate=True)
            assert client.read(f'/pane/{first}/body') == b'first pane'
            assert client.read(f'/pane/{second}/body') == b''
            assert client.stat(f'/pane/{first}/body')['length'] == len(b'first pane')
            rows = {int(row.split()[0]): row.split() for row in client.read('/index').splitlines()}
            assert rows[first][1:3] == [b'text', b'1'] and rows[second][1:3] == [b'text', b'0'], rows
            # name: read the file name, write to rename; sel: the selection.
            assert client.read(f'/pane/{first}/name') == f'{root}/+New\n'.encode()
            client.write(f'/pane/{first}/name', b'notes.txt\n')
            assert client.read(f'/pane/{first}/name') == f'{root}/notes.txt\n'.encode()
            assert pane_event() == f'rename {first} {root}/notes.txt\n'.encode()
            assert client.stat(f'/pane/{first}/name')['length'] == len(f'{root}/notes.txt\n')
            # Copying addr onto dot selects that range; sel reads and replaces it.
            client.write(f'/pane/{first}/addr', b'#0,#5')
            client.write(f'/pane/{first}/dot', client.read(f'/pane/{first}/addr'))
            assert client.read(f'/pane/{first}/dot') == client.read(f'/pane/{first}/addr')
            assert client.read(f'/pane/{first}/sel') == b'first'
            assert client.stat(f'/pane/{first}/sel')['length'] == 5
            client.write(f'/pane/{first}/sel', b'FIRST')
            assert client.read(f'/pane/{first}/body') == b'FIRST pane'
            assert client.read(f'/pane/{first}/sel') == b''
            # exec Msg shows text; exec Save writes the named file; rmdir closes.
            client.write(f'/pane/{first}/exec', b'Msg hello from 9P\n')
            assert 'hello from 9P' in ''.join(cell[0] for cell in client.screen()['cells'])
            assert client.read(f'/pane/{first}/dirty') == b'1\n'
            client.write(f'/pane/{first}/exec', b'Save\n')
            assert (root / 'notes.txt').read_bytes() == b'FIRST pane'
            assert pane_event() == f'save {first} {root}/notes.txt\n'.encode()
            assert client.read('/exec') == f'{first}\n'.encode()
            assert client.read(f'/pane/{first}/dirty') == b'0\n'
            client.remove(f'/pane/{second}')
            assert pane_event() == f'del {second} {root}/+New\n'.encode()
            assert str(second) not in client.list('/pane')
            # look opens a file; the serial reads back.
            (root / 'opened.txt').write_bytes(b'one\ntwo\nthree\n')
            client.write('/look', f'{root}/opened.txt:2\n'.encode())
            opened = int(client.read('/look'))
            assert opened not in (fixture, first, second), opened
            assert client.read(f'/pane/{opened}/body') == b'one\ntwo\nthree\n'
            assert client.read(f'/pane/{opened}/name') == f'{root}/opened.txt\n'.encode()
            assert pane_event() == f'new {opened} {root}/opened.txt\n'.encode()
            client.write(f'/pane/{first}/look', f'{root}/opened.txt:3\n'.encode())
            assert client.read('/look') == f'{opened}\n'.encode(), 'a repeated look targets the open pane'
            assert {str(fixture), str(first), str(opened)} <= set(client.list('/pane'))
            # Writing an event record back still performs the action it names.
            client.write(f'/pane/{first}/body', b'Msg via event', truncate=True)
            client.write(f'/pane/{first}/event', b'FX0 13\n')
            assert 'via event' in ''.join(cell[0] for cell in client.screen()['cells'])
            # A command line may not carry control characters, and nothing else
            # in the tree may be created or removed.
            refused = [lambda: client.write('/exec', b'Msg a\x00b'),
                       lambda: client.write('/status', b'anything\n'),
                       lambda: client.mkdir('/', 'x'),
                       lambda: client.mkdir('/pane', 'one'),
                       lambda: client.create('/pane', 'plain-file'),
                       lambda: client.remove('/pane/1/body'),
                       lambda: client.remove('/index')]
            for index, attempt in enumerate(refused):
                try:
                    attempt()
                except OSError:
                    pass
                else:
                    raise AssertionError(f'refusal {index} was accepted')
            client.close(log)
            if embedded:
                assert client.read('/src/pardes.zig').startswith(b'const std')
                assert client.stat('/src/pardes.zig')['mode'] == 0o444
                assert b'pub const Pardes' in client.read(f'/pane/{look(client, "/virtual/src/pardes.zig")}/body')
            # An open of data with OTRUNC (a shell's >) deletes the addressed
            # range only, so the write after it replaces that range.
            scratch = new_pane(client, b'hello world\nsecond line\n')
            client.write(f'/pane/{scratch}/addr', b'#0,#5')
            client.write(f'/pane/{scratch}/data', b'HOWDY', truncate=True)
            assert client.read(f'/pane/{scratch}/body') == b'HOWDY world\nsecond line\n'
            # sam's regexps: ^ at any line; a miss says so and leaves no
            # address, so the data write after it refuses.
            client.write(f'/pane/{scratch}/addr', b'/^second/')
            assert client.read(f'/pane/{scratch}/addr') == b'         12          18 '
            for path, data, why in ((f'/pane/{scratch}/addr', b'/nowhere/', 'no match for regexp'),
                                    (f'/pane/{scratch}/data', b'LOST', 'no address')):
                try:
                    client.write(path, data)
                    raise AssertionError(f'{path} took {data!r}')
                except OSError as refused:
                    assert why in str(refused), refused
            assert client.read(f'/pane/{scratch}/body') == b'HOWDY world\nsecond line\n'
            # Through a mount both read as EINVAL; the log says which and why.
            assert f'err {scratch} addr: no match for regexp\n'.encode() in client.read('/log')
            client.remove(f'/pane/{scratch}')
            # Real `diff -u` in a command pane: the terminal turns the tab
            # before each timestamp into blanks, and a look at a changed line
            # still opens the new file at it.
            (root / 'diff-old.txt').write_text('one\ntwo\nthree\n')
            (root / 'diff-new.txt').write_text('one\nTWO\nthree\n')
            seen = client.read('/log').decode().count('\nexit ')
            client.write('/exec', b'diff -u diff-old.txt diff-new.txt\n')
            deadline = time.monotonic() + 10
            while client.read('/log').decode().count('\nexit ') <= seen:
                assert time.monotonic() < deadline, 'diff -u never ended'
                time.sleep(.05)
            # A command's pane is kind cmd, not term: no shell to type into.
            ran = [row.split()[0] for row in client.read('/index').decode().splitlines() if row.split()[1] == 'cmd']
            shown = client.read(f'/pane/{ran[-1]}/body').decode()
            assert '+++ diff-new.txt' in shown and '\t' not in shown, shown
            client.write(f'/pane/{ran[-1]}/look', b'+TWO\n')
            opened = [row for row in client.read('/index').decode().splitlines() if row.split()[3] == f'{root}/diff-new.txt']
            assert len(opened) == 1, client.read('/index')
            client.remove(f'/pane/{opened[0].split()[0]}')
            client.remove(f'/pane/{ran[-1]}')
            # A Save makes the directories its file goes in, whatever names
            # it: the pane's own name or Save's path.
            # `~` is home (this session's HOME, the test's root) wherever a
            # path is typed: Save, name, a look, with an address or not.
            tilde = new_pane(client, b'one\ntwo\nthree\n')
            client.write(f'/pane/{tilde}/ctl', b'Save ~/tilde-made/sub/x.txt\n')
            assert (root / 'tilde-made/sub/x.txt').read_bytes() == b'one\ntwo\nthree\n'
            client.write(f'/pane/{tilde}/name', b'~/tilde-named.txt\n')
            assert client.read(f'/pane/{tilde}/name') == f'{root}/tilde-named.txt\n'.encode()
            client.remove(f'/pane/{tilde}')
            client.write('/look', b'~/tilde-made/sub/x.txt:3\n')
            looked = [row for row in client.read('/index').decode().splitlines() if row.split()[3] == f'{root}/tilde-made/sub/x.txt']
            assert len(looked) == 1, client.read('/index')
            assert client.read(f'/pane/{looked[0].split()[0]}/addr').startswith(b'8 '), client.read(f'/pane/{looked[0].split()[0]}/addr')
            client.remove(f'/pane/{looked[0].split()[0]}')
            made_dirs = new_pane(client, b'deep\n')
            client.write(f'/pane/{made_dirs}/ctl', f'Save {root}/save-made/a/b/x.txt\n'.encode())
            assert (root / 'save-made/a/b/x.txt').read_bytes() == b'deep\n'
            client.write(f'/pane/{made_dirs}/name', f'{root}/save-named/c/y.txt\n'.encode())
            client.write(f'/pane/{made_dirs}/ctl', b'Save\n')
            assert (root / 'save-named/c/y.txt').read_bytes() == b'deep\n'
            client.remove(f'/pane/{made_dirs}')
            # A save the host cannot do (a directory it may not make) fails
            # the write that asked for it, and the log's err record names
            # the path and why.
            unsaved = new_pane(client, b'unsaved\n')
            try:
                client.write(f'/pane/{unsaved}/ctl', b'Save /nonexistent-pardes-dir/x.txt\n')
                raise AssertionError('a Save into a missing directory succeeded')
            except OSError as refused:
                assert 'Save /nonexistent-pardes-dir/x.txt: no such directory' in str(refused), refused
            assert f'err {unsaved} ctl: Save /nonexistent-pardes-dir/x.txt: '.encode() in client.read('/log'), client.read('/log')
            client.remove(f'/pane/{unsaved}')

            # Sixteen connections fill the slots; the next is told why.
            extra = [Client(address) for _ in range(15)]
            try:
                try:
                    Client(address).__exit__()
                    raise AssertionError('a seventeenth connection was served')
                except OSError as refused:
                    assert 'too many connections' in str(refused), refused
            finally:
                for other in extra:
                    other.__exit__()
            deadline = time.monotonic() + 5
            while b'err - 9p: too many connections' not in client.read('/log'):
                assert time.monotonic() < deadline, client.read('/log')
                time.sleep(.01)

    print('9P discovery: listing/stat/find are inert; new, remove, look, exec, name, sel, log, ctl lock, focus, the ctl split and commands behave')


def run_file(binary):
    """pty/run: a line in, how it ended out, on the same open."""
    def run(c, serial, line):
        fid = c.open(f'/pane/{serial}/pty/run', 2)
        try:
            c.rpc(118, struct.pack('<IQI', fid, 0, len(line)) + line)
            return c.read_fid(fid)
        finally:
            c.close(fid)
    with tempfile.TemporaryDirectory() as directory:
        root = Path(directory)
        with session(binary, root, 'run') as (client, address):
            # From a pane on a peer's path no command or terminal starts:
            # refused before any pane is made, no `exit 127` pane left.
            peer = new_pane(client, b'')
            client.write(f'/pane/{peer}/name', b'/n/nosuchpeer/d/f.txt\n')
            count = len(client.read('/index').splitlines())
            execute_refused(client, peer, 'ls', 'no such directory')
            execute_refused(client, peer, 'Tty', 'no such directory')
            assert len(client.read('/index').splitlines()) == count, client.read('/index')
            assert b'exit 127' not in client.read('/log')
            client.remove(f'/pane/{peer}')
            client.write('/exec', b'Shell bash\n')
            client.write('/exec', b'Tty\n')
            term = int(max(r.split()[0] for r in client.read('/index').splitlines() if r.split()[1] == b'term'))
            deadline = time.monotonic() + 10
            while run(client, term, b'true\n') == b'busy\n':  # until its first prompt
                assert time.monotonic() < deadline, 'the shell never reached a prompt'
                time.sleep(.05)
            assert run(client, term, b'false\n') == b'exit 1\n'
            assert run(client, term, b'printf "one\\ntwo\\n"\n') == b'exit 0\none\ntwo\n'  # then what it printed
            assert run(client, term, b'sh -c "exit 7"\n') == b'exit 7\n'
            assert run(client, term, b'fi\n') == b'error not run\n'  # a syntax error runs nothing
            assert run(client, term, b"echo 'open\n") == b'error not run\n'  # nor does an open quote
            assert run(client, term, b'true\n') == b'exit 0\n'  # ...and the shell is clear after
            # With no frontend attached the pty's size is in pixels too, at a
            # nominal 8x16 cell: a program sizes its kitty graphics by it.
            said = run(client, term, b'python3 -c \'import fcntl,termios,struct;print(*struct.unpack("4H",fcntl.ioctl(0,termios.TIOCGWINSZ,bytes(8))))\'\n')
            rows, cols, xpixel, ypixel = map(int, said.split(b'\n')[1].split())
            assert (xpixel, ypixel) == (cols * 8, rows * 16) and xpixel > 0, said
            # A paging command run through pty/run does not wait in less: the
            # terminal's PAGER is `pardes -`, and its text is a +Pager pane.
            if shutil.which('git'):
                repo = root / 'pagedrepo'
                repo.mkdir()
                git = ['git', '-C', str(repo), '-c', 'user.name=p', '-c', 'user.email=p@x']
                subprocess.run(git + ['init', '-q'], check=True, env=dict(os.environ, HOME=str(root)))
                (repo / 'f').write_text('x\n')
                subprocess.run(git + ['add', 'f'], check=True, env=dict(os.environ, HOME=str(root)))
                subprocess.run(git + ['commit', '-qm', 'paged-commit-subject'], check=True, env=dict(os.environ, HOME=str(root)))
                assert run(client, term, b'git -C pagedrepo log\n').startswith(b'exit 0\n')
                pager = [r.split()[0].decode() for r in client.read('/index').splitlines() if r.split()[3:4] == [f'{repo}/+Pager'.encode()]]  # git -C runs it in the repo
                assert len(pager) == 1, client.read('/index')
                assert b'paged-commit-subject' in client.read(f'/pane/{pager[0]}/body')
                assert b'\x1b' not in client.read(f'/pane/{pager[0]}/body')
                client.remove(f'/pane/{pager[0]}')
            # systemd's tools page through their own variable: set too, its
            # secure mode off, the path unquoted where it needs none. Read
            # by the shell: coreutils 9.12's printenv quotes a value with a
            # blank on a terminal.
            systemd_pager = run(client, term, b'printf "%s\\n" "$SYSTEMD_PAGER" "$SYSTEMD_PAGERSECURE"\n')
            assert systemd_pager.startswith(b'exit 0\n') and systemd_pager.rstrip().endswith(b' -\n0'), systemd_pager
            if shutil.which('journalctl'):
                assert run(client, term, b'journalctl -n 3 --no-hostname\n').startswith(b'exit ')
                for row in client.read('/index').splitlines():
                    if row.split()[3:4] == [f'{root}/+Pager'.encode()]:
                        client.remove(f'/pane/{row.split()[0].decode()}')
            # A pty is not given one row (it loses the prompt's input mark,
            # and the shell would read busy for ever): refused, and said.
            try:
                client.write(f'/pane/{term}/pty/ctl', b'winsize 20 1\n')
                raise AssertionError('winsize 20 1 was taken')
            except OSError as refused:
                assert 'at least 2 rows' in str(refused), refused
            client.write(f'/pane/{term}/pty/ctl', b'winsize 20 2\n')
            time.sleep(.5)
            assert run(client, term, b'true\n') == b'exit 0\n'
            assert client.read(f'/pane/{term}/pty/ctl').split()[2] == b'2', client.read(f'/pane/{term}/pty/ctl')
            client.write(f'/pane/{term}/pty/ctl', b'winsize 80 24\n')
            # A shell started again that cannot start (its directory is
            # there but cannot be entered) fails the write and leaves the
            # running shell be: runs still work.
            locked = root / 'locked'
            locked.mkdir()
            assert run(client, term, f'cd {locked}\n'.encode()) == b'exit 0\n'
            deadline = time.monotonic() + 5
            while str(locked).encode() not in client.read('/index'):
                assert time.monotonic() < deadline, client.read('/index')
                time.sleep(.05)
            locked.chmod(0o600)
            try:
                if os.geteuid() != 0:  # root enters any directory
                    client.write(f'/pane/{term}/pty/ctl', b'exec\n')
                    raise AssertionError('pty/ctl exec of a shell that cannot start was taken')
            except OSError as refused:
                assert 'shell' in str(refused), refused
            finally:
                locked.chmod(0o755)
            kept = run(client, term, b'true\n')
            assert kept == b'exit 0\n', (kept, client.read('/log')[-600:])
            assert run(client, term, f'cd {root}\n'.encode()) == b'exit 0\n'
            with Client(address) as other:
                slow = []
                waiter = threading.Thread(target=lambda: slow.append(run(other, term, b'sleep 1\n')), daemon=True)
                waiter.start()
                time.sleep(.3)
                # The job the other client ran holds the tty, and says so.
                assert run(client, term, b'true\n') == b'busy: sleep is running\n'
                waiter.join(5)
                assert slow == [b'exit 0\n'], slow
                closing = []
                waiter = threading.Thread(target=lambda: closing.append(run(other, term, b'sleep 30\n')), daemon=True)
                waiter.start()
                time.sleep(.3)
                client.write(f'/pane/{term}/exec', b'Del\n')
                waiter.join(5)
                assert closing == [b'error shell gone\n'], closing


def exit_answers(binary):
    """Exit written to /ctl is answered before the editor quits."""
    with tempfile.TemporaryDirectory(prefix='pardes-exit-') as directory:
        seen = {}
        with session(binary, Path(directory), 'exit', terminal=seen) as (client, _):
            client.write('/ctl', b'Exit\n')
            deadline = time.monotonic() + 5
            while time.monotonic() < deadline:
                try:
                    # a zombie has quit; the session reaps it
                    if Path(f'/proc/{seen["pid"]}/stat').read_text().split(') ')[1][0] == 'Z':
                        break
                except FileNotFoundError:
                    break
                time.sleep(.01)
            else:
                raise AssertionError("Exit did not quit the editor")


def unattended_messages_expire(binary):
    """With nothing attached, a message leaves after MessageLinger on its own
    clock: no key, no click, no other request comes to move it along."""
    with tempfile.TemporaryDirectory(prefix='pardes-msg-') as directory:
        with session(binary, Path(directory), 'msg') as (client, _):
            client.write('/pane/1/ctl', b'Msg lingering-words\n')
            def shown():
                screen = json.loads(client.read('/screen'))
                return 'lingering-words' in ''.join(cell[0] for cell in screen['cells'])
            assert shown()
            # Reading /screen is itself a request, so wait without asking.
            time.sleep(2.5)
            assert not shown(), 'an unattended message outlived MessageLinger'


def directory_panes(binary):
    """A look at a directory opens a pane listing it, as acme's directory
    window: a text pane named with a trailing /, its body the entries (a
    directory's marked /), ctl's isdir 1, and get reads it again."""
    with tempfile.TemporaryDirectory(prefix='pardes-dirpane-') as directory:
        root = Path(directory).resolve()
        listed = root / 'listed'
        (listed / 'sub').mkdir(parents=True)
        (listed / '.dot').write_bytes(b'')
        with session(binary, root, 'dirpane') as (client, _):
            serial = look(client, str(listed))
            rows = [row.split() for row in client.read('/index').decode().splitlines()]
            row = next(r for r in rows if int(r[0]) == serial)
            assert row[1:4] == ['text', '0', f'{listed}/'], row
            body = client.read(f'/pane/{serial}/body').decode()
            assert body.split() == ['.dot', 'sub/'], body
            assert client.read(f'/pane/{serial}/ctl').split()[3] == b'1'
            (listed / 'later').write_bytes(b'')
            client.write(f'/pane/{serial}/ctl', b'get\n')
            assert client.read(f'/pane/{serial}/body').decode().split() == ['.dot', 'later', 'sub/']


def putall_answers_after_its_saves(binary):
    """Putall with one pane refused (its file changed on disk under unsaved
    edits) fails, and is answered only once the other panes' saves have
    landed."""
    with tempfile.TemporaryDirectory(prefix='pardes-putall-') as directory:
        root = Path(directory).resolve()
        (root / 'a.txt').write_bytes(b'a\n')
        (root / 'b.txt').write_bytes(b'b\n')
        with session(binary, root, 'putall') as (client, _):
            a = look(client, str(root / 'a.txt'))
            b = look(client, str(root / 'b.txt'))
            for serial in (a, b):
                client.write(f'/pane/{serial}/body', b'edited\n')
            (root / 'b.txt').write_bytes(b'changed on disk\n')
            deadline = time.monotonic() + 10
            while f'changed {b}' not in client.read('/log').decode():
                assert time.monotonic() < deadline, 'the watch never saw b.txt change'
                time.sleep(.05)
            try:
                client.write('/ctl', b'Putall\n')
            except OSError as why:
                assert 'modified on disk' in str(why), why
            else:
                raise AssertionError('Putall over a file changed on disk was taken')
            assert (root / 'a.txt').read_bytes() == b'a\nedited\n', (root / 'a.txt').read_bytes()
            assert (root / 'b.txt').read_bytes() == b'changed on disk\n'


def new_terminals_named_once(binary):
    """A terminal started outside the session's directory is named by that
    directory from its `new` on: no rename to the session's and back."""
    with tempfile.TemporaryDirectory(prefix='pardes-names-') as directory:
        root = Path(directory)
        (root / 'sub').mkdir()
        with session(binary, root, 'names') as (client, _):
            # A look at a directory starts a terminal there (DirLook
            # terminal); DirLook pane, the default, lists it in a pane.
            client.write('/ctl', b'DirLook terminal\n')
            made = [look(client, str(root / 'sub'))]
            before = {int(row.split()[0]) for row in client.read('/index').splitlines()}
            client.write('/pane/1/ctl', b'Tty\n')
            if shutil.which('fish'):
                client.write('/pane/1/ctl', b'Tty fish\n')
            made += [s for s in (int(row.split()[0]) for row in client.read('/index').splitlines()) if s not in before]
            time.sleep(1.5)  # their shells start and say where they are
            # A shell started again by pty/ctl exec: named as it was.
            client.write(f'/pane/{made[0]}/pty/ctl', b'exec\n')
            time.sleep(1.5)
            log = client.read('/log').decode()
            for serial in made:
                assert f'\nrename {serial} ' not in '\n' + log, (serial, log)
                assert f'\nnew {serial} {root}' in '\n' + log, (serial, log)
            # A shell that cannot start (its interpreter is not there): Tty
            # fails the write with why and leaves no pane; pty/ctl exec
            # fails with why and keeps its terminal.
            bad = root / 'badsh'
            bad.write_bytes(b'#!/nonexistent/interp\n')
            bad.chmod(0o755)
            panes = client.read('/index')
            for where, line in (('/pane/1/ctl', f'Tty {bad}\n'), ('/ctl', f'Shell {bad}\n')):
                try:
                    client.write(where, line.encode())
                    raise AssertionError(f'{line} of a shell that cannot start was taken')
                except OSError as refused:
                    assert 'interpreter /nonexistent/interp not found' in str(refused), refused
            time.sleep(.3)
            assert client.read('/index').count(b'\n') == panes.count(b'\n'), client.read('/index')
            assert b'shell: shell not found' not in client.read('/log'), client.read('/log')
            assert str(made[0]).encode() in b' '.join(r.split()[0] for r in client.read('/index').splitlines())


def recent_survives_restart(binary):
    """A file opened and closed is in /recent closed, and still is in the
    next session: the list is kept under $XDG_STATE_HOME (here HOME's
    .local/state) across restarts."""
    with tempfile.TemporaryDirectory(prefix='pardes-recent-') as directory:
        root = Path(directory)
        kept = root / 'kept.txt'
        kept.write_bytes(b'one\ntwo\n')
        with session(binary, root, 'recent1') as (client, _):
            serial = look(client, f'{kept}:2')
            assert f'open {kept}\n' in client.read('/recent').decode()
            client.write(f'/pane/{serial}/ctl', b'Del\n')
            assert f'closed {kept}\n' in client.read('/recent').decode(), client.read('/recent')
        assert (root / '.local' / 'state' / 'pardes' / 'store').exists()
        with session(binary, root, 'recent2') as (client, _):
            listed = client.read('/recent').decode()
            assert f'closed {kept}\n' in listed, listed
            # Recent fills a query pane with it, its row relative to the
            # pane's directory, and a look at the row opens it again.
            client.write('/pane/1/exec', b'Recent\n')
            rows = int(client.read('/pane/1/exec').split()[-1])
            body = client.read(f'/pane/{rows}/body').decode()
            assert body.startswith('Recent '), body
            row = next(r for r in body.splitlines() if r.startswith('kept.txt:'))
            client.write(f'/pane/{rows}/look', (row.split(' ')[0] + '\n').encode())
            assert f'open {kept}\n' in client.read('/recent').decode()


def del_twice(client, serial):
    """Del, and again when the first refused over unsaved text."""
    try:
        client.write(f'/pane/{serial}/ctl', b'Del\n')
    except OSError:
        client.write(f'/pane/{serial}/ctl', b'Del\n')


def store_shared_between_windows(binary):
    """Two windows share one store (Store.zig): a file opened in one is in
    the other's /recent at its next read, with no restart; a pane Del'd in
    one comes back where it was from a look at its @p serial there, its text
    with it, and only there."""
    with tempfile.TemporaryDirectory(prefix='pardes-store-') as directory:
        root = Path(directory)
        shared = root / 'shared.txt'
        shared.write_bytes(b'one\ntwo\nthree\n')
        with session(binary, root, 'store1') as (one, _), session(binary, root, 'store2') as (two, _):
            serial = look(one, f'{shared}:3')
            assert f'open {shared}\n' in one.read('/recent').decode()
            assert f'closed {shared}\n' in two.read('/recent').decode(), two.read('/recent')
            one.write(f'/pane/{serial}/ctl', b'Del\n')
            assert f'closed {shared}\n' in one.read('/recent').decode()
            scratch = new_pane(one, b'kept across a Del\n')
            del_twice(one, scratch)
            assert str(scratch) not in [row.split()[0] for row in one.read('/index').decode().splitlines()]
            back = look(one, f'@p{scratch}')
            assert one.read(f'/pane/{back}/body') == b'kept across a Del\n'
            # The other window's session closed no such pane.
            source = new_pane(two, b'')
            try:
                two.write(f'/pane/{source}/look', f'@p{scratch}\n'.encode())
            except OSError:
                pass
            assert b'kept across a Del' not in b''.join(two.read(f'/pane/{row.split()[0]}/body')
                                                        for row in two.read('/index').decode().splitlines())
        assert (root / '.local' / 'state' / 'pardes' / 'store').exists()


def odd_paths(binary):
    """Long and odd paths written where a path goes -- name, look, DumpDir,
    Dump, Save -- are refused or said, never a crash: a part over 255 bytes
    (ENAMETOOLONG), a loop of links (ELOOP), a file taken for a directory
    (ENOTDIR), one not ours (EACCES)."""
    with tempfile.TemporaryDirectory(prefix='pardes-odd-') as directory:
        root = Path(directory)
        (root / 'loop').symlink_to(root / 'loop')
        (root / 'plain').write_bytes(b'x\n')
        odd = ['/tmp/' + 'x' * 300 + '/f.txt', str(root / ('y' * 300)), str(root / 'loop' / 'f'),
               str(root / 'plain' / 'f'), '/root/nope/f', str(root / 'a' / '..' / 'b.txt')]
        with session(binary, root, 'odd') as (client, _):
            serial = new_pane(client, b'text\n')
            for path in odd:
                for where, line in ((f'/pane/{serial}/name', path), ('/look', path),
                                    ('/ctl', f'DumpDir {path}'), ('/ctl', 'Dump'), (f'/pane/{serial}/ctl', 'Save')):
                    try:
                        client.write(where, (line + '\n').encode())
                    except OSError:
                        pass
                    assert client.read('/index'), (where, line)
            client.write('/ctl', b'DumpDir\n')
            # A theme file that is not there, or no theme, fails the write.
            (root / 'bad.zon').write_bytes(b'.{ .nonsense = 1 }')
            for line, why in ((f'ThemeFile {root}/missing.zon', 'theme file'), (f'ThemeFile {root}/bad.zon', 'theme file'),
                              ('ThemeFile notes.txt', 'a theme file is a .zon')):
                try:
                    client.write('/ctl', (line + '\n').encode())
                    raise AssertionError(f'{line} was taken')
                except OSError as refused:
                    assert why in str(refused), (line, refused)


def delcol_last_keeps_window(binary):
    """Delcol on the last column leaves the window, empty but for the
    workspace tag, as acme does; Newcol and New start again from there."""
    with tempfile.TemporaryDirectory(prefix='pardes-delcol-') as directory:
        with session(binary, Path(directory), 'delcol') as (client, _):
            def columns():
                return [l for l in client.read('/layout').decode().splitlines() if l and not l.startswith('active')]
            while columns():
                client.write(f'/col/{columns()[0].split()[0]}/ctl', b'Delcol\n')
            # Still here, empty.
            assert client.read('/index') == b'', client.read('/index')
            client.write('/ctl', b'Newcol\n')
            assert len(columns()) == 1, columns()
            serial = new_pane(client, b'back\n')
            assert str(serial).encode() in client.read('/index'), client.read('/index')


def cut_writes(binary):
    """A write bigger than a 9P message comes as several, cut anywhere: each
    command line runs once, whole, an Edit block once it ends, and a last
    line with no newline when the open closes. Never a fragment."""
    with tempfile.TemporaryDirectory(prefix='pardes-cut-') as directory:
        with session(binary, Path(directory), 'cut') as (client, _):
            burst = b''.join(b'Msg burst-%04d\n' % i for i in range(1000))
            assert len(burst) > client.msize
            client.write('/exec', burst)
            log = client.read('/log').decode()
            said = [line.split(' ', 2)[2] for line in log.splitlines() if line.startswith('msg ') and 'burst-' in line]
            assert said == ['burst-%04d' % i for i in range(1000)], said[:3] + said[-3:]
            assert '\nrun ' not in log and 'exit 127' not in log, log[-400:]
            client.write('/exec', b'Msg no-newline-at-end')
            assert 'msg ' in client.read('/log').decode().split('no-newline-at-end')[0][-40:]
            serial = new_pane(client, b'')
            block = b'Edit ,a\n' + b''.join(b'line %05d of the block\n' % i for i in range(2500)) + b'.\n'
            assert len(block) > 50000
            client.write(f'/pane/{serial}/ctl', block)
            body = client.read(f'/pane/{serial}/body').decode().splitlines()
            assert body == ['line %05d of the block' % i for i in range(2500)], (len(body), body[:2])
            # A detached session with no frontend is 160x50 until told.
            screen = client.screen()
            assert (screen['cols'], screen['rows']) == (160, 50), (screen['cols'], screen['rows'])
            client.write('/ctl', b'size 132 40\n')
            screen = client.screen()
            assert (screen['cols'], screen['rows']) == (132, 40), (screen['cols'], screen['rows'])


def edit_commands(binary):
    """Edit's `< | >` run off the loop and answer the write that ran them
    once applied; a failing one changes nothing and fails the write in its
    words; X edits every open file it matches; w writes. The editor answers
    other requests while a slow command runs."""
    with tempfile.TemporaryDirectory(prefix='pardes-edit-') as directory:
        root = Path(directory)
        (root / 'a.txt').write_bytes(b'pear\napple\nfig\n')
        (root / 'b.txt').write_bytes(b'apple pie\n')
        with session(binary, root, 'edit') as (client, address):
            a = look(client, str(root / 'a.txt'))
            b = look(client, str(root / 'b.txt'))
            # The write returns with the text sorted, through the session's
            # shell in the file's directory.
            client.write(f'/pane/{a}/ctl', b'Edit , | sort\n')
            assert client.read(f'/pane/{a}/body') == b'apple\nfig\npear\n'
            client.write(f'/pane/{a}/ctl', b'Edit $ < pwd\n')
            assert client.read(f'/pane/{a}/body') == f'apple\nfig\npear\n{root}\n'.encode()
            client.write(f'/pane/{a}/ctl', b'Undo\n')
            assert client.read(f'/pane/{a}/body') == b'apple\nfig\npear\n'
            # A per-match command, one undo step.
            client.write(f'/pane/{a}/ctl', b'Edit ,x/[a-z]+/ | tr a-z A-Z\n')
            assert client.read(f'/pane/{a}/body') == b'APPLE\nFIG\nPEAR\n'
            # A failing command: nothing changed, the write fails in words,
            # its stderr in +Errors.
            try:
                client.write(f'/pane/{a}/ctl', b'Edit , | echo nope >&2; exit 4\n')
                raise AssertionError('a failing Edit command was taken')
            except OSError as why:
                assert 'exit status 4' in str(why), why
            assert client.read(f'/pane/{a}/body') == b'APPLE\nFIG\nPEAR\n'
            index = client.read('/index').decode()
            errors = [line.split()[0] for line in index.splitlines() if line.endswith(('+Errors', f'{root}/+Errors')) or '+Errors' in line]
            assert errors and any(b'nope' in client.read(f'/pane/{e}/body') for e in errors), index
            # While a slow command runs, the editor answers.
            done = threading.Event()

            def slow():
                with Client(address) as other:
                    other.write(f'/pane/{b}/ctl', b'Edit , | sleep 1; cat\n')
                done.set()
            worker = threading.Thread(target=slow)
            worker.start()
            time.sleep(.2)
            began = time.monotonic()
            client.read('/index')
            assert time.monotonic() - began < .5 and not done.is_set(), 'the editor waited on the command'
            worker.join(timeout=15)
            assert done.is_set()
            # On the same connection: the Edit's write is held, and a read sent
            # after it is answered first; a Tflush of it stops it, changes
            # nothing, and frees the way for the next Edit.
            def send(kind, tag, payload):
                client.socket.sendall(struct.pack('<IBH', 7 + len(payload), kind, tag) + payload)

            def answer():
                size, kind, tag = struct.unpack('<IBH', client.receive(7))
                return kind, tag, client.receive(size - 7)
            ctl = client.open(f'/pane/{a}/ctl', 1)
            status = client.open('/status', 0)
            line = b'Edit , | sleep 1; cat\n'
            began = time.monotonic()
            send(118, 900, struct.pack('<IQI', ctl, 0, len(line)) + line)
            send(116, 901, struct.pack('<IQI', status, 0, 4096))
            first = answer()
            assert first[1] == 901 and first[0] == 117 and time.monotonic() - began < .5, (first[:2], time.monotonic() - began)
            second = answer()
            assert second[1] == 900 and second[0] == 119, second
            line = b'Edit , | sleep 5; tr a-z A-Z\n'
            send(118, 902, struct.pack('<IQI', ctl, 0, len(line)) + line)
            time.sleep(.3)
            send(108, 903, struct.pack('<H', 902))
            tags = sorted(answer()[1] for _ in range(2))
            assert tags == [902, 903], tags
            client.close(ctl)
            client.close(status)
            time.sleep(1)
            client.write(f'/pane/{a}/ctl', b'Edit , | cat\n')
            time.sleep(4.5)
            assert client.read(f'/pane/{a}/body') == b'APPLE\nFIG\nPEAR\n', client.read(f'/pane/{a}/body')
            # X: each open file whose line matches; w writes it.
            client.write(f'/pane/{a}/ctl', b'Edit X/b\\.txt$/ ,s/apple/APPLE/\n')
            assert client.read(f'/pane/{b}/body') == b'APPLE pie\n'
            client.write(f'/pane/{b}/ctl', b'Edit w\n')
            assert (root / 'b.txt').read_bytes() == b'APPLE pie\n'
            client.write(f'/pane/{a}/ctl', b'Edit 1 w first.txt\n')
            assert (root / 'first.txt').read_bytes() == b'APPLE\n'
            # A file changed on disk under unsaved edits: Edit's w refuses
            # once, as Save does, and the next w overwrites it.
            w1 = root / 'w1.txt'
            w1.write_bytes(b'one\n')
            w = look(client, str(w1))
            client.write(f'/pane/{w}/ctl', b'Edit $a/two/\n')
            seen = client.read('/log').decode().count(f'\nchanged {w}')
            with open(w1, 'ab') as f:
                f.write(b'external\n')
            deadline = time.monotonic() + 10
            while client.read('/log').decode().count(f'\nchanged {w}') == seen:
                assert time.monotonic() < deadline, 'the change on disk was not heard'
                time.sleep(.05)
            try:
                client.write(f'/pane/{w}/ctl', b'Edit w\n')
                raise AssertionError('w overwrote a file changed on disk')
            except OSError as why:
                assert 'modified on disk since read (w again to overwrite)' in str(why), why
            assert w1.read_bytes() == b'one\nexternal\n'
            client.write(f'/pane/{w}/ctl', b'Edit w\n')
            assert w1.read_bytes() == b'one\ntwo', w1.read_bytes()


def test(binary, quic=False):
    started = time.monotonic()
    cut_writes(binary)
    edit_commands(binary)
    odd_paths(binary)
    delcol_last_keeps_window(binary)
    run_file(binary)
    exit_answers(binary)
    unattended_messages_expire(binary)
    new_terminals_named_once(binary)
    directory_panes(binary)
    putall_answers_after_its_saves(binary)
    recent_survives_restart(binary)
    store_shared_between_windows(binary)
    print(f'9P integration: passed ({time.monotonic() - started:.3f}s)')



if __name__ == '__main__':
    if len(sys.argv) in [3, 4] and sys.argv[2] == '--discovery':
        discovery(str(Path(sys.argv[1]).resolve()), embedded=sys.argv[3:] == ['--embedded'])
        raise SystemExit(0)
    if len(sys.argv) not in [2, 3] or (len(sys.argv) == 3 and sys.argv[2] != '--quic'):
        raise SystemExit('usage: fs.py <pardes> [--quic | --discovery]')
    test(str(Path(sys.argv[1]).resolve()), quic=len(sys.argv) == 3)