//! docs/typ/reference.typ, Other files , through a real //! session's socket. const std = @import("std"); const process = @import("../process.zig"); pub const cases = .{ .{ "other-files: kbdin and mousein drive a detached session as the user would, each write done before it is answered", keysAndMouse }, .{ "other-files: a held connection lists a directory, stats a file, writes and reads, is refused in words and removes a pane", heldConnection }, .{ "other-files: a file opened and closed is in /recent closed, and still is in the next session; Recent's row opens it again", recentSurvivesRestart }, .{ "other-files: two sessions share Recent: a file opened in one is in the other's /recent, a Del'd pane comes back from @p only where it was", recentShared }, }; extern "c" fn system(cmd: [*:0]const u8) c_int; fn has(hay: []const u8, needle: []const u8) bool { return std.mem.indexOf(u8, hay, needle) != null; } /// A session's root kept past it, removed at the test's end. fn keepRoot(d: *process.Detached, buf: *[4096]u8) []const u8 { d.owns_root = false; @memcpy(buf[0..d.root().len], d.root()); return buf[0..d.root().len]; } fn removeRoot(root: []const u8) void { var cmd: [4200]u8 = undefined; _ = system(std.fmt.bufPrintSentinel(&cmd, "rm -rf -- '{s}'", .{root}, 0) catch return); } fn stateExists(root: []const u8) bool { var buf: [4200]u8 = undefined; return std.c.access(std.fmt.bufPrintSentinel(&buf, "{s}/.local/share/pardes/pardes.xitdb", .{root}, 0) catch return false, 0) == 0; } /// A script types and clicks as the user does: no sleep or poll between a /// write and the read that checks it; where to click is read off /screen. /// (test/fs.py's keys_and_mouse, the first case written through the files /// alone.) fn keysAndMouse(gpa: std.mem.Allocator, exe: [:0]const u8) !void { var d = try process.Detached.start(gpa, exe, "kbd", .{}); defer d.deinit(); const f = &d.files; var buf: [4096]u8 = undefined; try @import("../e2e_harness.zig").writeFile(try std.fmt.bufPrintSentinel(&buf, "{s}/target.txt", .{d.root()}, 0), "hello-target\n"); const serial = try f.newPane(); try f.write(f.at("/pane/{d}/body", .{serial}), "see \n"); try f.write("/focus", f.at("{d}\n", .{serial})); try f.keys("Atarget.txt\x1b"); try f.expect(f.at("/pane/{d}/body", .{serial}), "see target.txt\n"); try f.click("target.txt", 2, 4); var index = std.mem.splitBackwardsScalar(u8, std.mem.trimEnd(u8, try f.read("/index"), "\n"), '\n'); const last = index.first(); try std.testing.expect(std.mem.endsWith(u8, last, "target.txt 1")); const opened = try std.fmt.parseInt(u32, last[0..std.mem.indexOfScalar(u8, last, ' ').?], 10); try std.testing.expect(opened != serial); try f.expect(f.at("/pane/{d}/body", .{opened}), "hello-target\n"); // Undone from the keyboard, as the user would: u in normal mode. try f.write("/focus", f.at("{d}\n", .{serial})); try f.keys("u"); try f.expect(f.at("/pane/{d}/body", .{serial}), "see \n"); } /// The runner's held connection, against a real session: a directory's /// entries, a stat, an open held while another write lands, a remove. fn heldConnection(gpa: std.mem.Allocator, exe: [:0]const u8) !void { var d = try process.Detached.start(gpa, exe, "conn", .{}); defer d.deinit(); const c = try process.Conn.dial(gpa, d.socket()); defer c.deinit(gpa); const names = try c.list(gpa, "/"); defer { for (names) |n| gpa.free(n); gpa.free(names); } var has_index = false; for (names) |n| has_index = has_index or std.mem.eql(u8, n, "index"); try std.testing.expect(has_index); try std.testing.expect((try c.stat("/kbdin")).mode & 0o222 != 0); const serial = try std.fmt.parseInt(u32, std.mem.trim(u8, try d.files.read("/pane/new"), "\n"), 10); var path: [64]u8 = undefined; const body = try std.fmt.bufPrint(&path, "/pane/{d}/body", .{serial}); try c.write(body, "held\n"); const got = try c.read(gpa, body); defer gpa.free(got); try std.testing.expectEqualStrings("held\n", got); try std.testing.expectError(error.Refused, c.write("/mousein", "nonsense\n")); try std.testing.expect(c.said.len > 0); try c.remove(try std.fmt.bufPrint(&path, "/pane/{d}", .{serial})); try std.testing.expectError(error.NotFound, c.walk(body)); } /// The list is kept in the state file (Db.zig), under $XDG_DATA_HOME (here /// HOME's .local/share), across restarts. fn recentSurvivesRestart(gpa: std.mem.Allocator, exe: [:0]const u8) !void { var arena = std.heap.ArenaAllocator.init(gpa); defer arena.deinit(); const a = arena.allocator(); var root_buf: [4096]u8 = undefined; var one = try process.Detached.start(gpa, exe, "recent1", .{}); const root = keepRoot(&one, &root_buf); defer removeRoot(root); const kept = try std.fmt.allocPrint(a, "{s}/kept.txt", .{root}); { defer one.deinit(); try one.put("kept.txt", "one\ntwo\n"); const c = try process.Conn.dial(gpa, one.socket()); defer c.deinit(gpa); const serial = try c.look(a, try std.fmt.allocPrint(a, "{s}:2", .{kept}), null); try std.testing.expect(has(try c.read(a, "/recent"), try std.fmt.allocPrint(a, "open {s}\n", .{kept}))); try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{serial}), "Del\n"); try std.testing.expect(has(try c.read(a, "/recent"), try std.fmt.allocPrint(a, "closed {s}\n", .{kept}))); } try std.testing.expect(stateExists(root)); var two = try process.Detached.start(gpa, exe, "recent2", .{ .root = root }); defer two.deinit(); const c = try process.Conn.dial(gpa, two.socket()); defer c.deinit(gpa); try std.testing.expect(has(try c.read(a, "/recent"), try std.fmt.allocPrint(a, "closed {s}\n", .{kept}))); // Recent fills a query pane with it, its row relative to the pane's // directory, and a look at the row opens it again. try c.write("/pane/1/exec", "Recent\n"); var answer = std.mem.tokenizeAny(u8, try c.read(a, "/pane/1/exec"), " \n"); var last: []const u8 = ""; while (answer.next()) |w| last = w; const rows = try std.fmt.parseInt(u32, last, 10); const body = try c.read(a, try std.fmt.allocPrint(a, "/pane/{d}/body", .{rows})); try std.testing.expect(std.mem.startsWith(u8, body, "Recent ")); var lines = std.mem.tokenizeScalar(u8, body, '\n'); const row = while (lines.next()) |l| { if (std.mem.startsWith(u8, l, "kept.txt:")) break l; } else return error.NoRow; try c.write(try std.fmt.allocPrint(a, "/pane/{d}/look", .{rows}), try std.fmt.allocPrint(a, "{s}\n", .{row[0 .. std.mem.indexOfScalar(u8, row, ' ') orelse row.len]})); try std.testing.expect(has(try c.read(a, "/recent"), try std.fmt.allocPrint(a, "open {s}\n", .{kept}))); } /// Del, and again when the first refused over unsaved text. fn delTwice(c: *process.Conn, ctl: []const u8) !void { c.write(ctl, "Del\n") catch try c.write(ctl, "Del\n"); } /// Recent's state file (Db.zig) shared 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. fn recentShared(gpa: std.mem.Allocator, exe: [:0]const u8) !void { var arena = std.heap.ArenaAllocator.init(gpa); defer arena.deinit(); const a = arena.allocator(); var root_buf: [4096]u8 = undefined; var one = try process.Detached.start(gpa, exe, "store1", .{}); const root = keepRoot(&one, &root_buf); defer removeRoot(root); { defer one.deinit(); var two = try process.Detached.start(gpa, exe, "store2", .{ .root = root }); defer two.deinit(); const c1 = try process.Conn.dial(gpa, one.socket()); defer c1.deinit(gpa); const c2 = try process.Conn.dial(gpa, two.socket()); defer c2.deinit(gpa); try one.put("shared.txt", "one\ntwo\nthree\n"); const shared = try std.fmt.allocPrint(a, "{s}/shared.txt", .{root}); const serial = try c1.look(a, try std.fmt.allocPrint(a, "{s}:3", .{shared}), null); try std.testing.expect(has(try c1.read(a, "/recent"), try std.fmt.allocPrint(a, "open {s}\n", .{shared}))); try std.testing.expect(has(try c2.read(a, "/recent"), try std.fmt.allocPrint(a, "closed {s}\n", .{shared}))); try c1.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{serial}), "Del\n"); try std.testing.expect(has(try c1.read(a, "/recent"), try std.fmt.allocPrint(a, "closed {s}\n", .{shared}))); const scratch = try c1.newPane(); try c1.write(try std.fmt.allocPrint(a, "/pane/{d}/body", .{scratch}), "kept across a Del\n"); try delTwice(c1, try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{scratch})); var rows = std.mem.tokenizeScalar(u8, try c1.read(a, "/index"), '\n'); while (rows.next()) |r| try std.testing.expect(!std.mem.startsWith(u8, r, try std.fmt.allocPrint(a, "{d} ", .{scratch}))); const back = try c1.look(a, try std.fmt.allocPrint(a, "@p{d}", .{scratch}), null); try std.testing.expectEqualStrings("kept across a Del\n", try c1.read(a, try std.fmt.allocPrint(a, "/pane/{d}/body", .{back}))); // The other window's session closed no such pane. const source = try c2.newPane(); c2.write(try std.fmt.allocPrint(a, "/pane/{d}/look", .{source}), try std.fmt.allocPrint(a, "@p{d}\n", .{scratch})) catch {}; var others = std.mem.tokenizeScalar(u8, try c2.read(a, "/index"), '\n'); while (others.next()) |r| { const body = try c2.read(a, try std.fmt.allocPrint(a, "/pane/{s}/body", .{r[0..std.mem.indexOfScalar(u8, r, ' ').?]})); try std.testing.expect(!has(body, "kept across a Del")); } } try std.testing.expect(stateExists(root)); }