diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 16:33:08 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-04 16:48:55 -0300 |
| commit | ef0d017705146a74bf7cc2706b9796f227ea6977 (patch) | |
| tree | b4f14a8724055be7918416b8893c32760d0ee7f8 /test/process/terminals.zig | |
| parent | be3985092df1c3388cfa09ca5e989d2fa66f55ec (diff) | |
| download | pardes-ef0d017705146a74bf7cc2706b9796f227ea6977.tar.gz pardes-ef0d017705146a74bf7cc2706b9796f227ea6977.zip | |
test: test/fs.py's discovery, run_file, exit_answers, unattended_messages_expire, directory_panes, putall_answers_after_its_saves, new_terminals_named_once, recent_survives_restart, store_shared_between_windows, odd_paths, delcol_last_keeps_window, cut_writes and edit_commands in Zig, the Python deleted (and its now unused newest, walk_tree, del_twice and the --discovery switch): at T2 in new test/process/{tree,terminals,root_ctl,edit}.zig and other_files.zig (the tree's listing, stats, log follow, ctl lock, focus, commands, sel, Save making directories, the 16 slots; a write cut at an 8 KiB msize; pty/run's answers, busy and shell gone; terminals named once; Exit; MessageLinger unattended; Putall; odd paths and ThemeFile; Edit's < | > off the loop, a held write answered after a later read and flushed, X and w; /recent across a restart and a store two sessions share), at T1 a directory pane in test/behaviour/look_and_exec.zig and 9P's new after Delcol of the last column in columns_and_tags.zig; discovery brought up to the tree as documented (sessions, kbdin, mousein at the root, /index stats 0, a sel write stays selected, the unknown-message words, a look's line in dot, a Save under / refused as permission denied), as the Python had gone stale; process.Conn gains replace, writeCut, create, newPane, newest, look, dialRefusal, send and answer, and process.sleep; a failing process test prints its error return trace; each checked to fail against a break (a detached session 161 wide, 17 connection slots, a pty taking one row, Edit's failure said as exit code, ThemeFile's .zon words changed, Exit not quitting, unattended messages never expiring, a directory pane's sub/ without its slash, Putall's modified-on-disk words changed, /recent saying shut for closed, a missing interpreter said as missing)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'test/process/terminals.zig')
| -rw-r--r-- | test/process/terminals.zig | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/test/process/terminals.zig b/test/process/terminals.zig new file mode 100644 index 00000000..e84b48e9 --- /dev/null +++ b/test/process/terminals.zig @@ -0,0 +1,241 @@ +//! docs/typ/guide.typ, Terminals <terminals>, through a real session's +//! socket: its shells run. +const std = @import("std"); +const libc = std.c; +const process = @import("../process.zig"); +const eh = @import("../e2e_harness.zig"); +const expect = std.testing.expect; +const expectEqual = std.testing.expectEqual; +const expectEqualStrings = std.testing.expectEqualStrings; + +extern "c" fn system(cmd: [*:0]const u8) c_int; + +pub const cases = .{ + .{ "terminals: pty/run takes a line and answers how it ended, then what it printed, on the same open; busy while a job holds the tty, gone with its shell", runFile }, + .{ "terminals: a terminal started outside the session's directory is named by it from its new on, and a shell that cannot start leaves no pane", namedOnce }, +}; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +fn refusedSaying(c: *process.Conn, path: []const u8, data: []const u8, why: []const u8) !void { + try std.testing.expectError(error.Refused, c.write(path, data)); + if (!has(c.said, why)) { + std.debug.print("{s} refused saying {s}, not {s}\n", .{ path, c.said, why }); + return error.WrongWords; + } +} + +/// A line written to a terminal's pty/run and the answer read on the same open. +fn run(a: std.mem.Allocator, c: *process.Conn, serial: u32, line: []const u8) ![]const u8 { + var buf: [64]u8 = undefined; + const fid = try c.hold(try std.fmt.bufPrint(&buf, "/pane/{d}/pty/run", .{serial}), 2); + defer c.clunk(fid); + _ = try c.writeFid(fid, 0, line); + return a.dupe(u8, try c.readFid(fid, 0, 1 << 16)); +} + +/// The serials of /index's rows whose fourth word, the path, is `path`. +fn panesAt(a: std.mem.Allocator, c: *process.Conn, path: []const u8) ![]const []const u8 { + var out: std.ArrayList([]const u8) = .empty; + var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n'); + while (rows.next()) |row| { + var w = std.mem.tokenizeScalar(u8, row, ' '); + const serial = w.next().?; + _ = w.next(); + _ = w.next(); + if (std.mem.eql(u8, w.next() orelse "", path)) try out.append(a, serial); + } + return out.items; +} + +/// The serials /index lists. +fn serials(a: std.mem.Allocator, c: *process.Conn) ![]const u32 { + var out: std.ArrayList(u32) = .empty; + var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n'); + while (rows.next()) |row| try out.append(a, try std.fmt.parseInt(u32, row[0..std.mem.indexOfScalar(u8, row, ' ').?], 10)); + return out.items; +} + +fn shell(a: std.mem.Allocator, comptime fmt: []const u8, args: anytype) c_int { + return system(std.fmt.allocPrintSentinel(a, fmt, args, 0) catch @panic("out of memory")); +} + +/// A run on another connection, as another client's, in a thread. +const Other = struct { + a: std.mem.Allocator, + conn: *process.Conn, + serial: u32, + line: []const u8, + got: []const u8 = "", + + fn go(o: *Other) void { + o.got = run(o.a, o.conn, o.serial, o.line) catch ""; + } +}; + +fn runFile(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var d = try process.Detached.start(gpa, exe, "run", .{}); + defer d.deinit(); + const f = &d.files; + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + const c = try process.Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + const root = d.root(); + // From a pane on a peer's path no command or terminal starts: refused + // before any pane is made, no `exit 127` pane left. + const peer = try c.newPane(); + try c.write(f.at("/pane/{d}/name", .{peer}), "/n/nosuchpeer/d/f.txt\n"); + const count = std.mem.count(u8, try c.read(a, "/index"), "\n"); + try refusedSaying(c, f.at("/pane/{d}/exec", .{peer}), "ls\n", "no such directory"); + try refusedSaying(c, f.at("/pane/{d}/exec", .{peer}), "Tty\n", "no such directory"); + try expectEqual(count, std.mem.count(u8, try c.read(a, "/index"), "\n")); + try expect(!has(try c.read(a, "/log"), "exit 127")); + try c.remove(f.at("/pane/{d}", .{peer})); + try c.write("/exec", "Shell bash\n"); + try c.write("/exec", "Tty\n"); + var term: u32 = 0; + var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n'); + while (rows.next()) |r| { + var w = std.mem.tokenizeScalar(u8, r, ' '); + const serial = try std.fmt.parseInt(u32, w.next().?, 10); + if (std.mem.eql(u8, w.next().?, "term")) term = @max(term, serial); + } + const deadline = eh.nowMs() + 10_000; + while (std.mem.eql(u8, try run(a, c, term, "true\n"), "busy\n")) { // until its first prompt + if (eh.nowMs() > deadline) return error.NoPrompt; + process.sleep(50); + } + try expectEqualStrings("exit 1\n", try run(a, c, term, "false\n")); + try expectEqualStrings("exit 0\none\ntwo\n", try run(a, c, term, "printf \"one\\ntwo\\n\"\n")); // then what it printed + try expectEqualStrings("exit 7\n", try run(a, c, term, "sh -c \"exit 7\"\n")); + try expectEqualStrings("error not run\n", try run(a, c, term, "fi\n")); // a syntax error runs nothing + try expectEqualStrings("error not run\n", try run(a, c, term, "echo 'open\n")); // nor does an open quote + try expectEqualStrings("exit 0\n", try run(a, c, term, "true\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. + const said = try run(a, c, term, "python3 -c 'import fcntl,termios,struct;print(*struct.unpack(\"4H\",fcntl.ioctl(0,termios.TIOCGWINSZ,bytes(8))))'\n"); + var lines = std.mem.splitScalar(u8, said, '\n'); + _ = lines.next(); + var size = std.mem.tokenizeScalar(u8, lines.next() orelse "", ' '); + var n: [4]u32 = undefined; + for (&n) |*v| v.* = try std.fmt.parseInt(u32, size.next() orelse return error.NoSize, 10); + try expect(n[2] == n[1] * 8 and n[3] == n[0] * 16 and n[2] > 0); + // 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 (shell(a, "command -v git >/dev/null", .{}) == 0) { + const git = f.at("HOME='{s}' git -C '{s}/pagedrepo' -c user.name=p -c user.email=p@x", .{ root, root }); + try expectEqual(@as(c_int, 0), shell(a, "mkdir '{s}/pagedrepo' && {s} init -q && echo x > '{s}/pagedrepo/f' && {s} add f && {s} commit -qm paged-commit-subject", .{ root, git, root, git, git })); + try expect(std.mem.startsWith(u8, try run(a, c, term, "git -C pagedrepo log\n"), "exit 0\n")); + const pager = try panesAt(a, c, f.at("{s}/pagedrepo/+Pager", .{root})); // git -C runs it in the repo + try expectEqual(@as(usize, 1), pager.len); + const body = try c.read(a, f.at("/pane/{s}/body", .{pager[0]})); + try expect(has(body, "paged-commit-subject") and !has(body, "\x1b")); + try c.remove(f.at("/pane/{s}", .{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. + const systemd = try run(a, c, term, "printf \"%s\\n\" \"$SYSTEMD_PAGER\" \"$SYSTEMD_PAGERSECURE\"\n"); + try expect(std.mem.startsWith(u8, systemd, "exit 0\n") and std.mem.endsWith(u8, std.mem.trimEnd(u8, systemd, " \n"), " -\n0")); + if (shell(a, "command -v journalctl >/dev/null", .{}) == 0) { + try expect(std.mem.startsWith(u8, try run(a, c, term, "journalctl -n 3 --no-hostname\n"), "exit ")); + for (try panesAt(a, c, f.at("{s}/+Pager", .{root}))) |p| try c.remove(f.at("/pane/{s}", .{p})); + } + // 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. + const ptyctl = f.at("/pane/{d}/pty/ctl", .{term}); + try refusedSaying(c, ptyctl, "winsize 20 1\n", "at least 2 rows"); + try c.write(ptyctl, "winsize 20 2\n"); + process.sleep(500); + try expectEqualStrings("exit 0\n", try run(a, c, term, "true\n")); + var words = std.mem.tokenizeAny(u8, try c.read(a, ptyctl), " \n"); + _ = words.next(); + _ = words.next(); + try expectEqualStrings("2", words.next().?); + try c.write(ptyctl, "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. + const locked = try std.fmt.allocPrintSentinel(a, "{s}/locked", .{root}, 0); + try expectEqual(@as(c_int, 0), libc.mkdir(locked, 0o755)); + try expectEqualStrings("exit 0\n", try run(a, c, term, f.at("cd {s}\n", .{locked}))); + const until = eh.nowMs() + 5_000; + while (!has(try c.read(a, "/index"), locked)) { + if (eh.nowMs() > until) return error.NeverMoved; + process.sleep(50); + } + _ = libc.chmod(locked, 0o600); + const refused = if (libc.geteuid() != 0) refusedSaying(c, ptyctl, "exec\n", "shell") else {}; // root enters any directory + _ = libc.chmod(locked, 0o755); + try refused; + try expectEqualStrings("exit 0\n", try run(a, c, term, "true\n")); + try expectEqualStrings("exit 0\n", try run(a, c, term, f.at("cd {s}\n", .{root}))); + const other = try process.Conn.dial(gpa, d.socket()); + defer other.deinit(gpa); + const tv: libc.timeval = .{ .sec = 10, .usec = 0 }; + _ = libc.setsockopt(other.fd, libc.SOL.SOCKET, libc.SO.RCVTIMEO, &tv, @sizeOf(libc.timeval)); + var slow: Other = .{ .a = a, .conn = other, .serial = term, .line = "sleep 1\n" }; + var waiter = try std.Thread.spawn(.{}, Other.go, .{&slow}); + process.sleep(300); + // The job the other client ran holds the tty, and says so. + const busy = run(a, c, term, "true\n"); + waiter.join(); + try expectEqualStrings("busy: sleep is running\n", try busy); + try expectEqualStrings("exit 0\n", slow.got); + var closing: Other = .{ .a = a, .conn = other, .serial = term, .line = "sleep 30\n" }; + waiter = try std.Thread.spawn(.{}, Other.go, .{&closing}); + process.sleep(300); + const del = c.write(f.at("/pane/{d}/exec", .{term}), "Del\n"); + waiter.join(); + try del; + try expectEqualStrings("error shell gone\n", closing.got); +} + +fn namedOnce(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var d = try process.Detached.start(gpa, exe, "names", .{}); + defer d.deinit(); + const f = &d.files; + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + const c = try process.Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + const root = d.root(); + try expectEqual(@as(c_int, 0), libc.mkdir(try std.fmt.allocPrintSentinel(a, "{s}/sub", .{root}, 0), 0o755)); + // A look at a directory starts a terminal there (DirLook terminal); + // DirLook pane, the default, lists it in a pane. + try c.write("/ctl", "DirLook terminal\n"); + var made: std.ArrayList(u32) = .empty; + try made.append(a, try c.look(a, f.at("{s}/sub", .{root}))); + const before = try serials(a, c); + try c.write("/pane/1/ctl", "Tty\n"); + if (shell(a, "command -v fish >/dev/null", .{}) == 0) try c.write("/pane/1/ctl", "Tty fish\n"); + for (try serials(a, c)) |s| if (std.mem.indexOfScalar(u32, before, s) == null) try made.append(a, s); + try expect(made.items.len >= 2); + process.sleep(1500); // their shells start and say where they are + // A shell started again by pty/ctl exec: named as it was. + try c.write(f.at("/pane/{d}/pty/ctl", .{made.items[0]}), "exec\n"); + process.sleep(1500); + const log = f.at("\n{s}", .{try c.read(a, "/log")}); + for (made.items) |s| { + try expect(!has(log, f.at("\nrename {d} ", .{s}))); + try expect(has(log, f.at("\nnew {d} {s}", .{ s, root }))); + } + // 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. + try d.put("badsh", "#!/nonexistent/interp\n"); + const bad = f.at("{s}/badsh", .{root}); + _ = libc.chmod(try std.fmt.allocPrintSentinel(a, "{s}", .{bad}, 0), 0o755); + const panes = std.mem.count(u8, try c.read(a, "/index"), "\n"); + try refusedSaying(c, "/pane/1/ctl", f.at("Tty {s}\n", .{bad}), "interpreter /nonexistent/interp not found"); + try refusedSaying(c, "/ctl", f.at("Shell {s}\n", .{bad}), "interpreter /nonexistent/interp not found"); + process.sleep(300); + try expectEqual(panes, std.mem.count(u8, try c.read(a, "/index"), "\n")); + try expect(!has(try c.read(a, "/log"), "shell: shell not found")); + try expect(std.mem.indexOfScalar(u32, try serials(a, c), made.items[0]) != null); +} |
