summaryrefslogtreecommitdiff
path: root/test/behaviour/other_files.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-04 11:01:39 -0300
committerGabriel Schneider <[email protected]>2026-11-04 11:02:14 -0300
commit3c5cddb963c67df5df0ed0ec023d68e43ba26bd5 (patch)
tree2e0bc9d5a65497d341220c3c3be1a5d9425bf859 /test/behaviour/other_files.zig
parent350e7cc22b85130cbe95a114c668945b1dc12fd7 (diff)
downloadpardes-3c5cddb963c67df5df0ed0ec023d68e43ba26bd5.tar.gz
pardes-3c5cddb963c67df5df0ed0ec023d68e43ba26bd5.zip
test: the Recent, Jumplist and Back/Forward group of src/ninep/ctl.zig, nine tests, through the 9P tree alone: in test/behaviour/other_files.zig Jumplist refilling its one +Jumps, a Jumplist entry (closed) only while no pane has its file, Recent's row of an open file at its dot now, a closed file found again (/recent and Recent list it closed, a look at its row reopens it at its dot, a Back under JumpScope all too), Back and Forward over a closed file's two entries reopening it once, Recent keeping only files on disk, Back over a closed file deleted since skipping it and saying so, and a remembered place clamped to the file as it is now; in settings.zig Back and Forward written to exec keeping to the focused file, and JumpScope all taking every place in order; each checked to fail against a break (refillOrOpen never refilling, a look's reopen not relinking a closed entry, Recent's open row at its closing place, Recent's (closed) word, a jump's reopen relinking no other entry, JumpScope's file test inverted, recentKeeps keeping any local name, Back's deleted words, the clamp's words)
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'test/behaviour/other_files.zig')
-rw-r--r--test/behaviour/other_files.zig229
1 files changed, 229 insertions, 0 deletions
diff --git a/test/behaviour/other_files.zig b/test/behaviour/other_files.zig
index ef1fab7e..2d5cb112 100644
--- a/test/behaviour/other_files.zig
+++ b/test/behaviour/other_files.zig
@@ -80,3 +80,232 @@ test "other-files: /recent keeps a PDF as it keeps a file: open, then closed" {
try s.write(s.at("/pane/{d}/ctl", .{n}), "Del\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read("/recent"), s.at("closed {s}\n", .{name})) != null);
}
+
+fn has(hay: []const u8, needle: []const u8) bool {
+ return std.mem.indexOf(u8, hay, needle) != null;
+}
+
+/// A fresh directory's absolute path, kept until deinit.
+fn tmpPath(s: *Session, tmp: *std.testing.TmpDir) ![]const u8 {
+ var buf: [4096]u8 = undefined;
+ return s.at("{s}", .{buf[0..try tmp.dir.realPath(std.testing.io, &buf)]});
+}
+
+/// The serials /index lists with a name ending `suffix`.
+fn named(s: *Session, suffix: []const u8) ![]u32 {
+ var out: std.ArrayList(u32) = .empty;
+ var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
+ while (rows.next()) |line| {
+ const name = line[0 .. std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue];
+ if (std.mem.endsWith(u8, name, suffix)) try out.append(s.arena.allocator(), try std.fmt.parseInt(u32, line[0..std.mem.indexOfScalar(u8, line, ' ').?], 10));
+ }
+ return out.items;
+}
+
+/// The body of the pane a root exec of `word` answers (Recent, Jumplist).
+fn listing(s: *Session, word: []const u8) ![]const u8 {
+ try s.write("/exec", s.at("{s}\n", .{word}));
+ return s.read(s.at("/pane/{s}/body", .{std.mem.trimEnd(u8, try s.read("/exec"), "\n")}));
+}
+
+test "other-files: Jumplist fills its one +Jumps again, as Recent does, never a second" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ for (0..2) |_| {
+ try s.write(s.at("/pane/{d}/exec", .{try s.focused()}), "Jumplist\n");
+ try std.testing.expectEqual(@as(usize, 1), (try named(&s, "/+Jumps")).len);
+ }
+}
+
+test "other-files: a Jumplist entry is (closed) only while no pane has its file open" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "one\n" });
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = "two\n" });
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const dir = try tmpPath(&s, &tmp);
+ try s.write("/look", s.at("{s}/a.txt\n", .{dir}));
+ const a = try s.focused();
+ try s.write("/look", s.at("{s}/b.txt\n", .{dir}));
+ try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n");
+ try std.testing.expect(has(try listing(&s, "Jumplist"), "(closed)"));
+ // Open again, by a look: no entry is closed any more.
+ try s.write("/look", s.at("{s}/a.txt\n", .{dir}));
+ try std.testing.expect(!has(try listing(&s, "Jumplist"), "(closed)"));
+}
+
+test "other-files: Recent's row for a file still open is where its dot is now, not where it last closed" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" });
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ try s.write("/look", s.at("{s}/a.txt:3\n", .{try tmpPath(&s, &tmp)}));
+ try s.write(s.at("/pane/{d}/dot", .{try s.focused()}), "6 6"); // line 2, column 3
+ // A query pane under the file (line 1 `Recent `), its rows said from
+ // its directory.
+ const rows = try listing(&s, "Recent");
+ try std.testing.expect(std.mem.startsWith(u8, rows, "Recent \n"));
+ try std.testing.expect(has(rows, "\na.txt:2:3 open\n"));
+}
+
+test "other-files: a closed file is found again: Recent and /recent list it closed, a look at its row reopens it at its dot, and a jump back does too" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" });
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = "bee\n" });
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const dir = try tmpPath(&s, &tmp);
+ const a = s.at("{s}/a.txt", .{dir});
+ // Opened, its dot on line 3, then closed.
+ try s.write("/look", s.at("{s}:3\n", .{a}));
+ const opened = try s.focused();
+ try std.testing.expect(has(try s.read("/recent"), s.at("open {s}\n", .{a})));
+ // Another file, so the jumplist has somewhere to come back from.
+ try s.write("/look", s.at("{s}/b.txt\n", .{dir}));
+ try s.write(s.at("/pane/{d}/ctl", .{opened}), "Del\n");
+ try std.testing.expect(has(try s.read("/recent"), s.at("closed {s}\n", .{a})));
+ // Recent lists it, closed, at its dot; a look at the row opens it there.
+ const rows = try listing(&s, "Recent");
+ const recent = std.mem.trimEnd(u8, try s.read("/exec"), "\n");
+ const at = std.mem.indexOf(u8, rows, "\na.txt:3:").? + 1;
+ const row = rows[at..std.mem.indexOfScalarPos(u8, rows, at, '\n').?];
+ try std.testing.expect(std.mem.endsWith(u8, row, " (closed)"));
+ try s.write(s.at("/pane/{s}/look", .{recent}), s.at("{s}\n", .{row[0 .. row.len - " (closed)".len]}));
+ const back = try s.focused();
+ try s.expect(s.at("/pane/{d}/name", .{back}), s.at("{s}\n", .{a}));
+ try std.testing.expectEqual(@as(u32, 8), (try s.range(s.at("/pane/{d}/dot", .{back})))[0]);
+ // Closed again; the jumplist keeps its entry, closed, and a jump back
+ // to it opens it again.
+ try s.write(s.at("/pane/{d}/ctl", .{back}), "Del\n");
+ try std.testing.expect(has(try listing(&s, "Jumplist"), "(closed)"));
+ // Every entry in order (JumpScope all): which file has the focus after
+ // a Del is the layout's to choose, and Back keeps to it first.
+ try s.write("/ctl", "JumpScope all\n");
+ for (0..8) |_| {
+ if ((try named(&s, "/a.txt")).len > 0) break;
+ try s.write("/exec", "Back\n");
+ }
+ try s.expect(s.at("/pane/{d}/name", .{try s.focused()}), s.at("{s}\n", .{a}));
+}
+
+test "other-files: Back and Forward over a closed file's entries: one reopen is that file's pane in every entry, so the next step moves" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))) });
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = "bee\n" });
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "c.txt", .data = "sea\n" });
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const dir = try tmpPath(&s, &tmp);
+ try s.write("/look", s.at("{s}/b.txt\n", .{dir}));
+ const b = try s.focused();
+ // Two entries for a.txt, far apart in it.
+ try s.write("/look", s.at("{s}/a.txt:1\n", .{dir}));
+ const a = try s.focused();
+ try s.write("/look", s.at("{s}/a.txt:90\n", .{dir}));
+ try s.write("/look", s.at("{s}/c.txt\n", .{dir}));
+ const c = try s.focused();
+ try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n");
+ try s.write("/focus", s.at("{d}\n", .{c}));
+ try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, try listing(&s, "Jumplist"), "(closed)"));
+ try s.write("/focus", s.at("{d}\n", .{c}));
+ // Back reopens it at line 90: both its entries are that pane now.
+ try s.write("/exec", "Back\n");
+ const re = try s.focused();
+ try s.expect(s.at("/pane/{d}/name", .{re}), s.at("{s}/a.txt\n", .{dir}));
+ try std.testing.expectEqual(@as(u32, 89), (try s.range(s.at("/pane/{d}/dot", .{re})))[0] / 4);
+ try std.testing.expect(!has(try listing(&s, "Jumplist"), "(closed)"));
+ try s.write("/focus", s.at("{d}\n", .{re}));
+ // Back again goes to its line 1, in the same pane, then to b.
+ try s.write("/exec", "Back\n");
+ try std.testing.expectEqual(re, try s.focused());
+ try std.testing.expectEqual(@as(u32, 0), (try s.range(s.at("/pane/{d}/dot", .{re})))[0] / 4);
+ try s.write("/exec", "Back\n");
+ try std.testing.expectEqual(b, try s.focused());
+ // And Forward walks it all again, to c.
+ try s.write("/exec", "Forward\n");
+ try s.write("/exec", "Forward\n");
+ try std.testing.expectEqual(@as(u32, 89), (try s.range(s.at("/pane/{d}/dot", .{re})))[0] / 4);
+ try s.write("/exec", "Forward\n");
+ try std.testing.expectEqual(c, try s.focused());
+}
+
+test "other-files: Recent keeps only files on disk: not a name never saved, nor the served tree's" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "real.txt", .data = "real\n" });
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const dir = try tmpPath(&s, &tmp);
+ const names = [_][]const u8{ s.at("{s}/c", .{dir}), "/virtual/zz", s.at("{s}/real.txt", .{dir}) };
+ for (names) |name| {
+ const made = try s.newPane();
+ try s.write(s.at("/pane/{d}/name", .{made}), s.at("{s}\n", .{name}));
+ // Asked once if it is a file with text to lose; gone either way.
+ _ = try s.refused(s.at("/pane/{d}/ctl", .{made}), "Del\n");
+ _ = s.refused(s.at("/pane/{d}/ctl", .{made}), "Del\n") catch 0;
+ }
+ const recent = try s.read("/recent");
+ try std.testing.expect(!has(recent, s.at(" {s}\n", .{names[0]})));
+ try std.testing.expect(!has(recent, " /virtual/zz\n"));
+ try std.testing.expect(has(recent, s.at(" {s}\n", .{names[2]})));
+}
+
+test "other-files: Back over a closed file deleted since skips it, says so, and goes on to the place before" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ for ([_][]const u8{ "a.txt", "gone.txt", "c.txt" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "text\n" });
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const dir = try tmpPath(&s, &tmp);
+ try s.write("/look", s.at("{s}/a.txt\n", .{dir}));
+ const a = try s.focused();
+ try s.write("/look", s.at("{s}/gone.txt\n", .{dir}));
+ const gone = try s.focused();
+ try s.write("/look", s.at("{s}/c.txt\n", .{dir}));
+ const c = try s.focused();
+ try s.write(s.at("/pane/{d}/ctl", .{gone}), "Del\n");
+ try tmp.dir.deleteFile(std.testing.io, "gone.txt");
+ try s.write("/focus", s.at("{d}\n", .{c}));
+ try s.write("/exec", "Back\n");
+ try std.testing.expectEqual(a, try s.focused());
+ try std.testing.expect(has(try s.read("/log"), "gone.txt: deleted, skipped"));
+}
+
+test "other-files: a remembered place is clamped to the file as it is now, its selection's two ends kept where they fit" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "c.txt", .data = "one\ntwo\nthree\nfour\nfive\n" });
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const dir = try tmpPath(&s, &tmp);
+ try s.write("/look", s.at("{s}/c.txt:2\n", .{dir}));
+ const c = try s.focused();
+ // A selection over lines 2 to 4, then closed.
+ try s.write(s.at("/pane/{d}/dot", .{c}), "4 19");
+ try s.write(s.at("/pane/{d}/ctl", .{c}), "Del\n");
+ // The file shrinks to two lines while it is closed.
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "c.txt", .data = "one\ntwo\n" });
+ const rows = try listing(&s, "Recent");
+ const recent = std.mem.trimEnd(u8, try s.read("/exec"), "\n");
+ const at = std.mem.indexOf(u8, rows, "c.txt:").?;
+ const row = rows[at..std.mem.indexOfScalarPos(u8, rows, at, '\n').?];
+ // Both ends are in the row, a range.
+ try std.testing.expect(std.mem.indexOfScalar(u8, row, '-') != null);
+ const loc = s.at("{s}/{s}\n", .{ dir, row[0..std.mem.indexOfScalar(u8, row, ' ').?] });
+ // A look at it opens the file, never a miss, clamped to its last line.
+ try s.write(s.at("/pane/{s}/look", .{recent}), loc);
+ const back = try s.focused();
+ try s.expect(s.at("/pane/{d}/name", .{back}), s.at("{s}/c.txt\n", .{dir}));
+ // Its ends clamped to the last character, `two`'s `o`, not its newline.
+ const dot = try s.range(s.at("/pane/{d}/dot", .{back}));
+ try std.testing.expect(dot[0] < dot[1] and dot[1] <= 7);
+ try std.testing.expect(has(try s.read("/log"), "c.txt has 2 lines now: at its last"));
+ // The same row with the file open: the same place, said the same way.
+ try s.write(s.at("/pane/{s}/look", .{recent}), loc);
+ try std.testing.expectEqual(back, try s.focused());
+ try std.testing.expectEqual(dot, try s.range(s.at("/pane/{d}/dot", .{back})));
+}