diff options
| author | user <user@clank> | 2026-09-02 14:57:46 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-09-02 14:57:46 +0200 |
| commit | bac4b796f3f729d8d76b9c7c973126eac49872f2 (patch) | |
| tree | 0d040eb64e433122f982ea45912eba37ab54822a /tests | |
| parent | webdemo: version from contrib/version, published by ship (diff) | |
| parent | multi-attach: the config knobs (diff) | |
| download | slosh-bac4b796f3f729d8d76b9c7c973126eac49872f2.tar.gz slosh-bac4b796f3f729d8d76b9c7c973126eac49872f2.tar.xz slosh-bac4b796f3f729d8d76b9c7c973126eac49872f2.zip | |
Merge branch 'multiplayer': shared multi-client attach
Several terminals can attach to one session at once: the canonical
screen becomes a model projected into every client's own size, with
per-client diff history, input parsing and kitty graphics state. The
active client (newest attach, then last input) sizes the canvas by
default; multi_attach and size_follows in the config control both
halves of the policy.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/live_graphics.py | 29 | ||||
| -rw-r--r-- | tests/screen_test.c | 79 | ||||
| -rw-r--r-- | tests/test_nested.py | 6 | ||||
| -rw-r--r-- | tests/test_session.py | 144 |
4 files changed, 240 insertions, 18 deletions
diff --git a/tests/live_graphics.py b/tests/live_graphics.py index 97dbdd4..74e4b2c 100644 --- a/tests/live_graphics.py +++ b/tests/live_graphics.py @@ -156,12 +156,29 @@ def main(): str(back[:6]), ) - os.write(fd, b"\x01q") - drain(fd, idle=0.2, limit=2.0) - try: - os.kill(pid, 9) - except ProcessLookupError: - pass + # A second terminal has its own kitty image namespace. The first client's + # `sent` bit must not make a new viewer receive only a placement for pixels + # its terminal has never seen. + pid2, fd2 = spawn() + second = drain(fd2, idle=0.6, limit=30.0) + second_cmds = gfx_commands(second) + ok &= check( + "a joining client receives its own image transmission", + any(c.startswith(b"a=t") for c, _ in second_cmds), + str(second_cmds[:8]), + ) + ok &= check( + "and joining it leaves the first client attached", + os.waitpid(pid, os.WNOHANG) == (0, 0), + ) + + os.write(fd2, b"\x01q") + drain(fd2, idle=0.2, limit=2.0) + for child in (pid, pid2): + try: + os.kill(child, 9) + except ProcessLookupError: + pass return 0 if ok else 1 diff --git a/tests/screen_test.c b/tests/screen_test.c new file mode 100644 index 0000000..248479e --- /dev/null +++ b/tests/screen_test.c @@ -0,0 +1,79 @@ +/* Canonical-screen projection into independently sized client views. */ +#include "slosh.h" + +#include <stdio.h> +#include <string.h> + +static int fails; + +static void ok(const char *name, bool cond) { + if (!cond) fails++; + printf("%s %s\n", cond ? "ok " : "FAIL", name); +} + +static char at(screen_t *s, uint16_t x, uint16_t y) { + cell_t *c = screen_at(s, x, y); + return c && c->len ? c->text[0] : ' '; +} + +int main(void) { + screen_t src, view; + screen_init(&src, 6, 4); + for (uint16_t y = 0; y < src.rows; y++) + for (uint16_t x = 0; x < src.cols; x++) { + char ch = (char)('A' + y * src.cols + x); + screen_put_utf8(&src, x, y, &ch, 1, (color_t){0}, (color_t){0}, 0); + } + src.cursor_visible = true; + src.cursor_x = 4; + src.cursor_y = 2; + uint16_t link = screen_link_id(&src, "https://example.test", 20); + screen_set_link(&src, 3, 2, link); + screen_at(&src, 3, 2)->attrs = ATTR_BOLD; + + screen_init(&view, 8, 5); + screen_project(&view, &src, 0, 0); + ok("a larger client gets the canonical top-left", at(&view, 5, 3) == 'X'); + ok("columns beyond the canonical screen are filler", at(&view, 6, 0) == ' '); + ok("rows beyond the canonical screen are filler", at(&view, 0, 4) == ' '); + ok("the cursor is translated into the client", + view.cursor_visible && view.cursor_x == 4 && view.cursor_y == 2); + + screen_resize(&view, 3, 2); + screen_project(&view, &src, 2, 1); + ok("a smaller client receives the selected crop", + at(&view, 0, 0) == 'I' && at(&view, 2, 1) == 'Q'); + ok("styles survive projection", (screen_at(&view, 1, 1)->attrs & ATTR_BOLD)); + ok("hyperlinks survive projection", + view.cur_link[4] != 0 && + strcmp(view.links[view.cur_link[4] - 1], "https://example.test") == 0); + ok("a cursor in the crop uses local coordinates", + view.cursor_visible && view.cursor_x == 2 && view.cursor_y == 1); + + uint16_t vx = 0, vy = 0; + screen_follow_cursor(&src, 3, 2, &vx, &vy); + ok("a small viewport follows the cursor minimally", vx == 2 && vy == 1); + src.cursor_x = 1; + src.cursor_y = 0; + screen_follow_cursor(&src, 3, 2, &vx, &vy); + ok("the viewport follows the cursor back toward the origin", + vx == 1 && vy == 0); + src.cursor_visible = false; + vx = 99; + vy = 99; + screen_follow_cursor(&src, 3, 2, &vx, &vy); + ok("a hidden cursor retains and clamps the viewport", vx == 3 && vy == 2); + + screen_put_utf8(&src, 4, 0, "日", strlen("日"), (color_t){0}, (color_t){0}, + 0); + screen_resize(&view, 5, 1); + screen_project(&view, &src, 0, 0); + ok("a wide glyph cut at the right edge is blank", at(&view, 4, 0) == ' '); + screen_project(&view, &src, 5, 0); + ok("a wide-glyph tail cut at the left edge is blank", at(&view, 0, 0) == ' '); + + screen_free(&view); + screen_free(&src); + printf("\n%s (%d failures)\n", fails ? "FAILED" : "all green", fails); + return fails ? 1 : 0; +} diff --git a/tests/test_nested.py b/tests/test_nested.py index 16c10ed..78c2df7 100644 --- a/tests/test_nested.py +++ b/tests/test_nested.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 """Running slosh from inside slosh. -A new client displaces the current display, so `slosh` typed into a pane of -the very session it would attach used to displace the client showing that -pane — you watched your own session detach, from inside it. The pane's +`slosh` typed into a pane of the very session it would attach would feed the +session's display back through one of its own panes: a recursive view with no +useful stopping point. The pane's environment (SLOSH, SLOSH_SESSION) says which session this is, so the attach path refuses that one name and only that one name: any other is deliberate nesting, and the control verbs (`cmd`, `ls`) are how a program in a pane is diff --git a/tests/test_session.py b/tests/test_session.py index 7c3f22b..5878945 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -6,6 +6,7 @@ the assertions are about what survives: content, layout, and the process. """ import fcntl +import json import os import pty import select @@ -86,6 +87,23 @@ def control(name, line): return out.stdout.strip() +def screen_size(name): + snap = json.loads(control(name, "snapshot json")) + return snap["cols"], snap["rows"] + + +def resize(pid, fd, cols, rows): + fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack("HHHH", rows, cols, 0, 0)) + os.kill(pid, signal.SIGWINCH) + + +def running(pid): + try: + return os.waitpid(pid, os.WNOHANG)[0] == 0 + except ChildProcessError: + return False + + def wait_gone(pid, timeout=3.0): end = time.time() + timeout while time.time() < end: @@ -188,22 +206,75 @@ def main(): out = drain(fd2) check("input works after reattach", b"beta" in out, repr(out[-60:])) - # --- a second client replaces the first ----------------------------- - pid3, fd3 = attach(name) + # --- multiple attached clients share one session -------------------- + pid3, fd3 = attach(name, cols=50, rows=12) drain(fd3) tail = drain(fd2, idle=0.2, limit=2.0) check( - "the displaced client is told it was replaced", - b"replaced" in tail or wait_gone(pid2, 1.0), + "a second client leaves the first attached", + b"replaced" not in tail and running(pid2), repr(tail[-60:]), ) + check("the newest attachment owns the size", screen_size(name) == (50, 12)) - # --- closing the last pane ends the session ------------------------- - os.write(fd3, b"\x01x") # close pane 2 + # The control path asks the canonical screen for a full repaint. Since the + # canonical screen is now only a model, that request has to fan out to both + # clients' independent diff histories. + drain(fd2) + drain(fd3) + control(name, "reload") + repaint2 = drain(fd2) + repaint3 = drain(fd3) + check( + "a canonical repaint reaches every attached client", + b"\x1b[2J" in repaint2 and b"\x1b[2J" in repaint3, + repr((repaint2[-80:], repaint3[-80:])), + ) + + os.write(fd2, b"-from-first-client") + out2 = drain(fd2) + out3 = drain(fd3) + shared = control(name, "snapshot text") + check( + "either client can type into the shared pane", + b"t-client" in out2 + and b"t-client" in out3 + and "from-firs" in shared + and "t-client" in shared, + repr((out2[-80:], out3[-80:])), + ) + check("the last client to type owns the size", screen_size(name) == (80, 24)) + + resize(pid3, fd3, 40, 10) drain(fd3) - os.write(fd3, b"\x01x") # close the last one - drain(fd3, idle=0.3, limit=2.0) - check("the client exits when the session ends", wait_gone(pid3)) + check( + "resizing an inactive client does not steal the size", + screen_size(name) == (80, 24), + ) + resize(pid2, fd2, 70, 20) + drain(fd2) + check("the active client's resize wins", screen_size(name) == (70, 20)) + + os.write(fd3, b"-small-client-active") + drain(fd3) + drain(fd2) + check("input promotes the smaller client", screen_size(name) == (40, 10)) + + os.write(fd3, b"\x01d") + tail = drain(fd3, idle=0.2, limit=2.0) + check("detach exits only its issuing client", wait_gone(pid3), repr(tail[-60:])) + check("the other client stays attached", running(pid2)) + check( + "disconnecting the active client restores the previous active size", + screen_size(name) == (70, 20), + ) + + # --- closing the last pane ends the session ------------------------- + os.write(fd2, b"\x01x") # close pane 2 + drain(fd2) + os.write(fd2, b"\x01x") # close the last one + drain(fd2, idle=0.3, limit=2.0) + check("every client exits when the session ends", wait_gone(pid2)) time.sleep(0.3) listing = subprocess.run([BIN, "ls"], capture_output=True, text=True).stdout @@ -215,6 +286,61 @@ def main(): except ProcessLookupError: pass + # --- the config knobs ----------------------------------------------- + # multi_attach false restores the displacing attach; size_follows + # "smallest" sizes the shared screen to the tightest attached terminal. + # Each knob gets a fresh session, whose config exists before its server + # starts (the server reads it once at startup, plus on reload). + with open(CONFIG, "w") as f: + f.write("splash_ms 0\nmulti_attach false\n") + name_x = "t" + uuid.uuid4().hex[:8] + pid_a, fd_a = attach(name_x) + drain(fd_a) + pid_b, fd_b = attach(name_x) + drain(fd_b) + tail = drain(fd_a, idle=0.2, limit=2.0) + check( + "multi_attach false: the old client is displaced", + b"replaced" in tail or wait_gone(pid_a, 1.0), + repr(tail[-60:]), + ) + check("multi_attach false: the new client is attached", running(pid_b)) + control(name_x, "quit") + drain(fd_b, idle=0.2, limit=2.0) + + with open(CONFIG, "w") as f: + f.write('splash_ms 0\nsize_follows "smallest"\n') + name_y = "t" + uuid.uuid4().hex[:8] + pid_c, fd_c = attach(name_y, cols=80, rows=24) + drain(fd_c) + pid_d, fd_d = attach(name_y, cols=50, rows=12) + drain(fd_d) + drain(fd_c) + check( + "size_follows smallest: the tightest client sizes the screen", + screen_size(name_y) == (50, 12), + ) + os.write(fd_c, b"x") # input on the big client must not steal the size + drain(fd_c) + check( + "size_follows smallest: input does not steal the size", + screen_size(name_y) == (50, 12), + ) + os.write(fd_d, b"\x01d") # the small client leaves; the floor rises + drain(fd_d, idle=0.2, limit=2.0) + check( + "size_follows smallest: a departing client raises the floor", + screen_size(name_y) == (80, 24), + ) + control(name_y, "quit") + drain(fd_c, idle=0.2, limit=2.0) + + for p in (pid_a, pid_b, pid_c, pid_d): + try: + os.kill(p, signal.SIGKILL) + except ProcessLookupError: + pass + print(f"\n{'FAILED' if fails else 'all green'} ({fails} failures)") return 1 if fails else 0 |
