summaryrefslogtreecommitdiff
path: root/test/fs.py
blob: b55ad5ab2abdea18a3332774b62eb7e34f76fcaf (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
#!/usr/bin/env python3
import contextlib
import fcntl
import json
import os
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):
    env = {key: value for key, value in os.environ.items()
           if not key.startswith('PARDES_')}
    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]
        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:
            address = root / f'pardes-9p-{child.pid}.sock'
        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 not address.exists():
            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)
    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 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',
                                                     'look', 'exec', 'log', 'screen', 'listeners', 'pane', '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.endswith(b'/discovery.txt\n'), 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 client.read(f'/pane/{first}/ctl').endswith(b' current ')
            assert client.read(f'/pane/{second}/ctl').endswith(b' notcurrent ')
            try:
                client.write('/focus', b'99999\n')
                raise AssertionError('focus took a serial no pane has')
            except OSError as refused:
                assert 'no such window' 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()
            assert 'Newcol root' in commands and 'Save arg pane' in commands, commands
            assert all(row.split()[-1] in ('root', 'pane') for row in commands), commands
            assert client.stat('/commands')['length'] == len('\n'.join(commands)) + 1
            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}')

    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):
            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 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)
                assert run(client, term, b'true\n') == b'busy\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 test(binary, quic=False):
    started = time.monotonic()
    run_file(binary)
    exit_answers(binary)
    for options, message in [
        (['--fs'], b'no such option'),
        (['--fs9'], b'no such option'),
        (['--9p'], b'--9p needs a socket name'),
        (['--9p='], b'invalid 9P socket name'),
        (['--9p=a/b'], b'invalid 9P socket name'),
        (['--attach=work', '--9p=work'], b'--attach has none'),
        (['--9p-tcp'], b'--9p-tcp needs'),
        (['--9p-tcp='], b'--9p-tcp needs'),
        (['--9p-tcp=tcp!localhost!5640'], b'--9p-tcp needs'),
        (['--9p-tcp=quic!127.0.0.1!5640'], b'--9p-tcp needs'),
        (['--9p-tcp=tcp!127.0.0.1!5640', '--9p-tcp=tcp!127.0.0.1!5641'], b'specified twice'),
        (['--attach=work', '--9p-tcp=tcp!127.0.0.1!5640'], b'--attach has none'),
        (['--9p-quic'], b'--9p-quic needs'),
        (['--9p-quic='], b'--9p-quic needs'),
        (['--9p-quic=tcp!127.0.0.1!5640'], b'--9p-quic needs'),
        (['--mount=os=work'], b'invalid mount name'),
        (['--mount=peer='], b'invalid mount name or dial'),
        (['--mount=peer=one', '--mount=peer=two'], b'duplicate mount'),
        (['--mount=peer=tcp!127.0.0.1!0'], b'invalid mount dial'),
        (['--attach=work', '--mount=peer=one'], b'--attach has none'),
    ]:
        result = subprocess.run([binary, *options], capture_output=True, timeout=5)
        assert result.returncode == 1 and message in result.stderr, (options, result)
    with tempfile.TemporaryDirectory(prefix='pardes-fs-') as directory:
        root = Path(directory)
        with session(binary, root, 'renamed', '--9p=explicit', socket_name='explicit') as (named, named_address):
            assert named_address.name == 'pardes-9p-explicit.sock'
            assert not (root / 'pardes-9p-renamed.sock').exists()
            assert named.read('/pane/1/body') == b'initial\n'
        with session(binary, root, 'first') as (client, address):
            assert {'pane', 'index', 'status', 'look', 'exec', 'log', 'os'} <= set(client.list('/'))
            assert 'self' not in client.list('/')
            assert client.read('/pane/1/body') == b'initial\n'
            assert client.read('/os' + str(root / 'first.txt')) == b'initial\n'
            assert 'first.txt' in client.list('/os' + str(root))
            assert client.read('/pane/1/../1/body') == b'initial\n'
            assert client.read('/os' + str(root) + '/../' + root.name + '/first.txt') == b'initial\n'

            listing = root / 'listing'
            listing.mkdir()
            (listing / 'broken').symlink_to(listing / 'missing')
            for number in range(24):
                (listing / f'entry-{number:02}').touch()
            with Client(address, msize=256) as small:
                assert set(small.list('/os' + str(listing))) == {
                    f'entry-{number:02}' for number in range(24)}

            frozen = client.open('/screen')
            before = bytearray(client.read_fid(frozen, count=31))
            client.write('/pane/1/body', b'screen changed\n')
            while chunk := client.read_fid(frozen, len(before)):
                before.extend(chunk)
            client.close(frozen)
            old_screen = json.loads(before)
            old_text = ''.join(cell[0] for cell in old_screen['cells'])
            assert 'initial' in old_text and 'screen changed' not in old_text
            assert 'screen changed' in ''.join(cell[0] for cell in client.screen()['cells'])

            payload = ('const value = "λ";\n' * 3000).encode()
            serial = new_pane(client, payload)
            body = f'/pane/{serial}/body'
            assert client.read(body) == payload
            client.write(body, b'replaced\n', truncate=True)
            assert client.read(body) == b'replaced\n'

            event = client.open(f'/pane/{serial}/event')
            client.write(body, b'event\n')
            record = client.read_fid(event)
            assert record.startswith(b'EI') and b'event\n' in record, record
            client.close(event)

            os_path = '/os' + str(root / 'first.txt')
            client.write(os_path, b'written through 9P\n', truncate=True)
            assert (root / 'first.txt').read_bytes() == b'written through 9P\n'
            assert client.read(os_path) == b'written through 9P\n'

            for index in range(12):
                with Client(address) as other:
                    assert other.read(body) == b'replaced\nevent\n', index

            missing = False
            try:
                client.read('/pane/4294967295/body')
            except OSError:
                missing = True
            assert missing
            assert client.read(body) == b'replaced\nevent\n'

            if 'src' in client.list('/'):
                source = look(client, '/virtual/src/pardes.zig')
                assert b'pub const Pardes' in client.read(f'/pane/{source}/body')

            (root / 'index').write_bytes(b'OS takes precedence\n')
            opened = look(client, 'index')
            assert client.read(f'/pane/{opened}/body') == b'OS takes precedence\n'
            opened = look(client, '/n/self/index')
            index = client.read(f'/pane/{opened}/body')
            assert index.split()[0] == b'1' and b'first.txt' in index, index
            (root / 'explicit.txt').write_bytes(b'explicit OS mount\n')
            opened = look(client, '/n/os' + str(root / 'explicit.txt'))
            assert client.read(f'/pane/{opened}/body') == b'explicit OS mount\n'

            own = root / 'pardes-9p-second.sock'
            with session(binary, root, 'second', '--mount=peer=' + str(address),
                         '--mount=own=' + str(own)) as (remote, _):
                opened = look(remote, '/n/own/pane/1/body')
                assert remote.read(f'/pane/{opened}/body') == b'initial\n'
                opened = look(remote, '/n/peer/pane/' + str(serial) + '/body')
                assert remote.read(f'/pane/{opened}/body') == b'replaced\nevent\n'
                remote.write(f'/pane/{opened}/body', b'saved to peer\n', truncate=True)
                remote.write(f'/pane/{opened}/exec', b'Save\n')
                assert client.read(body) == b'saved to peer\n'

                large = b'mounted file contents\n' * 60000
                large_path = root / 'large.txt'
                large_path.write_bytes(large)
                opened = look(remote, '/n/peer/os' + str(large_path))
                assert remote.read(f'/pane/{opened}/body') == large
                edited = large[:-3] + b'edited\n'
                remote.write(f'/pane/{opened}/body', edited, truncate=True)
                remote.write(f'/pane/{opened}/exec', b'Save\n')
                assert large_path.read_bytes() == edited
                assert client.read('/os' + str(large_path)) == edited

            with session(binary, root, 'runtime') as (remote, own_address):
                control = new_pane(remote, b'')
                execute(remote, control, 'Mount peer ' + str(address))
                opened = look(remote, '/n/peer/pane/1/body', source=control)
                assert remote.read(f'/pane/{opened}/body') == client.read('/pane/1/body')
                execute(remote, control, 'Mount peer ' + str(own_address))
                execute(remote, control, 'Unmount peer')
                remote.write(f'/pane/{opened}/body', b'runtime mounted Save\n', truncate=True)
                remote.write(f'/pane/{opened}/exec', b'Save\n')
                assert client.read('/pane/1/body') == b'runtime mounted Save\n'
                assert remote.read('/pane/1/body') == b'initial\n'
                roots = look(remote, '/n', source=control)
                assert b'/n/peer/\n' in remote.read(f'/pane/{roots}/body')
                remote.write(f'/pane/{opened}/exec', b'Del\n')
                execute(remote, control, 'Unmount peer')
                remote.write(f'/pane/{roots}/ctl', b'get\n')
                assert remote.read(f'/pane/{roots}/body') == b'/n/os/\n/n/self/\n'
                execute(remote, control, 'Mount peer ' + str(own_address))
                opened = look(remote, '/n/peer/pane/1/body', source=control)
                assert remote.read(f'/pane/{opened}/body') == b'initial\n'

            with session(binary, root, 'mounted-startup', '--mount=peer=' + str(address),
                         file='/n/peer/pane/1/body:1') as (remote, _):
                assert remote.read('/pane/1/body') == b'runtime mounted Save\n'

            client.write(f'/pane/{serial}/exec', b'Del\n')
            assert str(serial) not in client.list('/pane')

        for tty in [False, True]:
            name = 'restore tty space' if tty else 'restore detached space'
            with session(binary, root, name, tty=tty) as (old, address):
                old.write('/pane/1/body', b'dumped state\n', truncate=True)
                control = new_pane(old, b'')
                execute(old, control, 'Restore ' + str(root / 'missing dump.zon'))
                assert old.read('/pane/1/body') == b'dumped state\n'
                execute(old, control, 'Mount own ' + str(address))
                execute(old, control, 'Dump')
                saved = root / (name + '.dump.zon')
                deadline = time.monotonic() + 5
                while not saved.exists():
                    assert time.monotonic() < deadline, 'Dump did not write its state'
                    time.sleep(.005)
                held_body = old.open('/pane/1/body')
                held_screen = old.open('/screen')
                old_ids = {int(row.split()[0]) for row in old.read('/index').splitlines()}
                assert old.read_fid(held_body) == b'dumped state\n'
                assert old.read_fid(held_screen, count=1) == b'{'
                old.write('/pane/1/body', b'changed after dump\n', truncate=True)
                # Unsaved text is asked about once, as Exit asks; the second
                # Restore goes ahead, and is answered before the hang-up.
                try:
                    execute(old, control, 'Restore ' + str(saved))
                except OSError as refused:
                    assert 'Modified' in str(refused), refused
                assert old.read('/pane/1/body') == b'changed after dump\n'
                execute(old, control, 'Restore ' + str(saved))
                # Refused while the connections are cut, or cut already.
                try:
                    old.read_fid(held_body)
                except (EOFError, OSError):
                    pass
                else:
                    raise AssertionError('Restore left an old body fid usable')
                with Client(address) as restored:
                    # The new log names its panes, then where they came from.
                    assert restored.read('/log').endswith(b'\nrestore ' + str(saved).encode() + b'\n'), restored.read('/log')
                    restored_ids = [int(row.split()[0]) for row in restored.read('/index').splitlines()]
                    assert len(restored_ids) == 2 and old_ids.isdisjoint(restored_ids), restored_ids
                    file_id, control_id = restored_ids
                    contents = restored.read(f'/pane/{file_id}/body')
                    assert contents == b'dumped state\n', (contents, saved.read_text(), restored.read('/index'))
                    assert restored.read(f'/pane/{control_id}/body') == b'', 'commands ran through exec, not the body'
                    assert restored.screen()['cols'] == 80
                    opened = look(restored, '/n/own/os' + str(root / (name + '.txt')), source=control_id)
                    assert restored.read(f'/pane/{opened}/body') == b'initial\n'

        with session(binary, root, 'forwarding') as (client, address):
            child_dir = root / 'child-working-directory'
            child_dir.mkdir()
            spaced = child_dir / 'space name.txt'
            spaced.write_bytes(b'first line\nsecond line\n')
            env = os.environ.copy()
            # PARDES_PID is what says "you are inside a pardes"; the socket
            # and the pane only say how to reach it. Without the pid the child
            # starts its own session, so this test used to pass only when the
            # runner itself happened to be running inside one.
            status = dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())
            env.update(PARDES_9P=str(address), PARDES_PANE='1', PARDES_PID=status['pid'])
            env.pop('PARDES_FORWARD_LOOK', None)
            for word, expected, selected, reuse in [('space name.txt:2:4', spaced.read_bytes(), [14, 14], False),
                                                    ('/n/self/pane/1/body', b'initial\n', None, False),
                                                    ('/virtual/pane/1/body:1:2-4', b'initial\n', [1, 4], True)]:
                before = {int(row.split()[0]) for row in client.read('/index').splitlines()}
                result = subprocess.run([binary, '--tty', word], cwd=child_dir, env=env,
                                        capture_output=True, timeout=3)
                assert result.returncode == 0, (word, result.stderr)
                after = {int(row.split()[0]) for row in client.read('/index').splitlines()}
                if reuse:
                    assert after == before
                else:
                    opened, = after - before
                assert client.read(f'/pane/{opened}/body') == expected
                assert client.read('/pane/1/body') == b'initial\n'
                if selected is not None:
                    # The selection is dot; reading it takes no command at all.
                    actual = list(map(int, client.read(f'/pane/{opened}/dot').split()))
                    assert actual == selected, (word, actual, selected)
            addr = client.open('/pane/1/addr', 2)
            limit = client.open('/pane/1/limit', 2)
            for fid, text in [(addr, b'#0,#3'), (limit, b'#0,#3'), (addr, b'#0')]:
                client.rpc(118, struct.pack('<IQI', fid, 0, len(text)) + text)
            try:
                for _ in range(8):
                    result = subprocess.run([binary], cwd=child_dir, env=env,
                                            capture_output=True, timeout=3)
                    assert result.returncode == 1 and b'already inside pardes' in result.stderr
                    text = b'/tial/'
                    try:
                        client.rpc(118, struct.pack('<IQI', addr, 0, len(text)) + text)
                    except OSError:
                        pass
                    else:
                        raise AssertionError('no-argument child cleared the parent address limit')
            finally:
                client.close(addr)
                client.close(limit)
            client.read('/pane/1/ctl')

            control = new_pane(client, b'')
            execute(client, control, 'Dump')
            saved = root / 'forwarding.dump.zon'
            deadline = time.monotonic() + 3
            while not saved.exists():
                assert time.monotonic() < deadline, 'forwarding fixture Dump did not finish'
                time.sleep(.005)
            before = client.read('/index')
            inherited = {key: env[key] for key in ['PARDES_9P', 'PARDES_PANE', 'PARDES_PID']}
            cases = [
                ('mount', ['--mount=peer=' + str(address), '/n/peer/pane/1/body'], None),
                ('name', ['--9p=forwarded-name'], 'forwarded-name'),
                ('tcp', ['--9p-tcp=tcp!127.0.0.1!0'], None),
                ('load', ['-l', str(saved)], None),
                ('shells', ['-n', '3'], None),
                ('toggle', ['--tty-toggle=q'], None),
                ('toggle-arg', ['--tty-toggle', 'q'], None),
            ]
            if quic:
                cases.append(('quic', ['--9p-quic=quic!127.0.0.1!0'], None))
            for name, options, socket_name in cases:
                with session(binary, root, 'forward-' + name, tty=True, socket_name=socket_name,
                             launch=[*([] if name == 'shells' else ['--tty']), *options],
                             inherited=inherited) as (local, local_address):
                    assert local_address != address
                    index = local.read('/index').splitlines()
                    if name == 'mount':
                        assert len(index) == 1 and b'/n/peer/' in index[0], index
                        assert local.read('/pane/1/body') == b'initial\n'
                    elif name == 'load':
                        assert local.read('/index') == before
                        assert local.read('/pane/1/body') == b'initial\n'
                    else:
                        # A bare tty launch is a shell plus the empty text pane
                        # the boot puts under it; --shells=3 is the classic
                        # three-shell layout and has no scratch.
                        assert len(index) == (3 if name == 'shells' else 2), index
                    if name in ['tcp', 'quic']:
                        assert (name + '!127.0.0.1!').encode() in local.read('/listeners')
                    assert client.read('/index') == before
                    assert client.read('/pane/1/body') == b'initial\n'

            for name, identity, word in [
                ('stale-file', dict(inherited, PARDES_9P=str(root / 'absent.sock')), str(spaced)),
                ('stale-missing', dict(inherited, PARDES_9P=str(root / 'absent.sock')), 'missing-child-file.txt'),
                ('stale-noarg', dict(inherited, PARDES_9P=str(root / 'absent.sock')), None),
                ('stale-pane', dict(inherited, PARDES_PANE='4294967295'), str(spaced)),
                # No pid means "not inside a pardes at all", which is what
                # --nested withholds and what a plain shell has.
                ('not-nested', {k: v for k, v in inherited.items() if k != 'PARDES_PID'}, str(spaced)),
            ]:
                with session(binary, root, name, tty=True, inherited=identity,
                             launch=['--tty', *([word] if word else [])]) as (local, local_address):
                    assert local_address != address
                    if word == str(spaced):
                        assert local.read('/pane/1/body') == spaced.read_bytes()
                    elif word:
                        assert word.encode() in local.read('/index') or word in ''.join(
                            cell[0] for cell in local.screen()['cells'])
                    assert client.read('/index') == before

        for tty in [False, True]:
            name = 'lsp-tty' if tty else 'lsp-detached'
            with session(binary, root, name, tty=tty) as (client, _):
                control = new_pane(client, b'')
                execute(client, control, 'Lspinfo')
                deadline = time.monotonic() + 3
                while True:
                    matches = [int(row.split()[0]) for row in client.read('/index').splitlines()
                               if b'+Lsp' in row]
                    if matches:
                        break
                    assert time.monotonic() < deadline, (name, 'Lspinfo did not complete')
                    time.sleep(.005)
                assert len(matches) == 1, matches
                result = client.read(f'/pane/{matches[0]}/body')
                assert result.startswith(b'backend: zls-inproc+lsp-client\n'), result
                assert b'protocol servers (lsp-client):' in result, result
                assert client.read('/pane/1/body') == b'initial\n'
                assert '+Lsp' in ''.join(cell[0] for cell in client.screen()['cells'])

        with session(binary, root, 'highlight') as (client, _):
            client.write('/pane/1/name', b'keywords.zig\n')
            source = b'// padding\n' * 9999 + b'fn check() void {\n    if (true) return;\n}\n'
            client.write('/pane/1/body', source, truncate=True)
            screen = client.screen()
            assert len(screen['cells']) == screen['cols'] * screen['rows']
            found = set()
            for row in range(screen['rows']):
                cells = screen['cells'][row * screen['cols']:(row + 1) * screen['cols']]
                text = ''.join(cell[0] for cell in cells)
                for word in ['fn', 'if']:
                    start = text.find(word + ' ')
                    if start < 0:
                        continue
                    styles = [screen['styles'][cell[1]] for cell in cells[start:start + len(word)]]
                    # Weight is opt-in (SyntaxBold, off by default); colour is
                    # not, so the keyword must differ from the text beside it.
                    plain = screen['styles'][cells[start + len(word)][1]]
                    assert all(not style['bold'] and style['fg'] == styles[0]['fg'] for style in styles), (word, styles)
                    assert styles[0]['fg'] != plain['fg'], (word, styles[0], plain)
                    found.add(word)
            assert found == {'fn', 'if'}, screen

        with session(binary, root, 'long-names') as (client, _):
            directory = root / ('directory-' + 'd' * 54)
            directory.mkdir()
            for length in [29, 63, 127, 255]:
                name = 'f' * (length - 4) + '.txt'
                path = directory / name
                path.write_bytes(b'long filename\n')
                wire_path = '/os' + str(path)
                assert name in client.list('/os' + str(directory))
                assert client.read(wire_path) == b'long filename\n'
                client.write(wire_path, b'written through long filename\n', truncate=True)
                assert path.read_bytes() == b'written through long filename\n'
                opened = look(client, '/n/os' + str(path))
                assert client.read(f'/pane/{opened}/body') == path.read_bytes()
            opened = look(client, '/n/os' + str(directory))
            listing = client.read(f'/pane/{opened}/body')
            for path in directory.iterdir():
                assert ('/n/os' + str(path)).encode() in listing
    if quic:
        quic_test(binary)
    print(f'9P integration: passed ({time.monotonic() - started:.3f}s)')


def quic_test(binary):
    for tty in [False, True]:
        for host in ['127.0.0.1', '::1']:
            with tempfile.TemporaryDirectory(prefix='pardes-quic-fs-') as directory:
                root = Path(directory)
                ports = {}
                with session(binary, root, 'server', '--9p-tcp=tcp!' + host + '!0',
                             '--9p-quic=quic!' + host + '!0', tty=tty, network=ports) as (unix, address):
                    assert set(unix.read('/listeners').decode().splitlines()) == {
                        'unix!' + str(address), 'tcp!' + host + '!' + str(ports['TCP']),
                        'quic!' + host + '!' + str(ports['QUIC'])}
                    with Client((host, ports['TCP'])) as tcp:
                        unix_body = unix.open('/pane/1/body')
                        tcp_body = tcp.open('/pane/1/body')
                        with session(binary, root, 'client') as (remote, _):
                            control = new_pane(remote, b'')
                            dial = 'quic!' + host + '!' + str(ports['QUIC'])
                            execute(remote, control, 'Mount peer ' + dial)
                            opened = look(remote, '/n/peer/pane/1/body', source=control)
                            body = f'/pane/{opened}/body'
                            assert remote.read(body) == b'initial\n'
                            payload = 'saved café λ\n'.encode() + b'x' * (3 * 8192 + 27) + b'\n'
                            remote.write(body, payload, truncate=True)
                            remote.write(f'/pane/{opened}/exec', b'Save\n')
                            assert unix.read_fid(unix_body) == payload[:unix.msize - 11]
                            assert tcp.read('/pane/1/body') == payload
                            tcp.write('/pane/1/body', b'TCP to QUIC Get\n', truncate=True)
                            remote.write(f'/pane/{opened}/ctl', b'get\n')
                            assert remote.read(body) == b'TCP to QUIC Get\n'
                            assert unix.read_fid(unix_body) == b'TCP to QUIC Get\n'
                            assert tcp.read_fid(tcp_body) == b'TCP to QUIC Get\n'
                            screen_pane = look(remote, '/n/peer/screen', source=control)
                            screen = json.loads(remote.read(f'/pane/{screen_pane}/body'))
                            assert len(screen['cells']) == screen['cols'] * screen['rows']
                            assert 'TCP to QUIC Get' in ''.join(cell[0] for cell in screen['cells'])
                            large = ('normal λ line\n' * 20000).encode()
                            fixture = root / 'large.txt'
                            fixture.write_bytes(large)
                            large_pane = look(remote, '/n/peer/os' + str(fixture), source=control)
                            assert remote.read(f'/pane/{large_pane}/body') == large
                            edited = b'QUIC file saved\n'
                            remote.write(f'/pane/{large_pane}/body', edited, truncate=True)
                            remote.write(f'/pane/{large_pane}/exec', b'Save\n')
                            assert fixture.read_bytes() == edited
                            for pane in [opened, screen_pane, large_pane]:
                                remote.write(f'/pane/{pane}/exec', b'Del\n')
                            execute(remote, control, 'Unmount peer')
                            roots = look(remote, '/n', source=control)
                            assert remote.read(f'/pane/{roots}/body') == b'/n/os/\n/n/self/\n'
                            assert unix.read_fid(unix_body) == b'TCP to QUIC Get\n'
                            assert tcp.read_fid(tcp_body) == b'TCP to QUIC Get\n'
                        control = new_pane(unix, b'')
                        execute(unix, control, 'Mount own ' + dial)
                        own = look(unix, '/n/own/pane/1/body', source=control)
                        assert unix.read(f'/pane/{own}/body') == b'TCP to QUIC Get\n'
                        unix.write(f'/pane/{own}/body', b'same-session QUIC Save\n', truncate=True)
                        unix.write(f'/pane/{own}/exec', b'Save\n')
                        assert unix.read_fid(unix_body) == b'same-session QUIC Save\n'
                        assert tcp.read_fid(tcp_body) == b'same-session QUIC Save\n'
                        unix.write(f'/pane/{own}/exec', b'Del\n')
                        execute(unix, control, 'Unmount own')
                        unix.close(unix_body)
                        tcp.close(tcp_body)


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)