From 3c5cddb963c67df5df0ed0ec023d68e43ba26bd5 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 11:01:39 -0300 Subject: 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 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- test/behaviour/other_files.zig | 229 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 229 insertions(+) (limited to 'test/behaviour/other_files.zig') 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}))); +} -- cgit v1.3 From ee36e1d07d455aed8b3895e35329b1de4c21ab94 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 11:14:05 -0300 Subject: test: review of the ctl migration: the second LookWord list look checked refilled with beta's places (its +Search body's words.txt:1:7-10), not only the one +Search; a failing builtin's test again refuses Msg a\x01b on the root exec with its control-character reason, as the old test did; a look opening a spaced path checks the source's ctl undo flag still 0 (the old revision and undo_len checks; the first 60 bytes stopped at dirty); a name's own backslash doubled in /index and in /recent's closed row moved to test/behaviour/other_files.zig through a name write to a file named back\nslash, the old /index and /recent tests trimmed to their setCwd control bytes and newline (no 9P name holds one); each checked to fail against a break (events' backslash doubling skipped, the second look searching alpha again, both control-character checks letting \x01 through); ten of the migrated tests checked against the reviewer's own breaks (fitErr's end left mid-character, Tab taking 0, a deleted file's Back step not going on, Delete asking, recentKeeps keeping a non-local name, a 1 MiB line not discarding its tail, is a dir not EISDIR, an event range backwards taken, DEL let through both checks, a remembered place's column clamped past the line) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/ninep/ctl.zig | 11 +++++------ test/behaviour/look_and_exec.zig | 11 ++++++++++- test/behaviour/other_files.zig | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 7 deletions(-) (limited to 'test/behaviour/other_files.zig') diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index ec7cb3bc..89549933 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -2471,10 +2471,7 @@ test "/index shows a name as the log does: a newline in it is \\n, a control byt // Its tag never carries the raw newline either. const tag = rd(p, Node.of(serialOf(p), .tag), 0, 4096).bytes; try testing.expect(std.mem.indexOf(u8, tag, "/tmp/two\\nlines") != null); - // A backslash of the name's own is doubled: `\n` written in a name - // reads back unlike a newline. - p.setCwd(term, "/tmp/back\\nslash"); - try testing.expect(std.mem.indexOf(u8, rd(p, @backingInt(tree.TopFile.index), 0, 4096).bytes, "/tmp/back\\\\nslash") != null); + // A name's own backslash doubled is test/behaviour/other_files.zig's. } test "a column takes panes until their documented minimums fill it, not until none can be halved" { @@ -2525,9 +2522,11 @@ test "a new file saved shows in /recent, open, while it is" { test "/recent shows a name as /index does, a newline in it escaped" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); - p.recent.opened(p.recentGpa(), "/tmp/a\nb\\c"); + // A backslash doubled is test/behaviour/other_files.zig's; a 9P name + // write cannot hold a newline. + p.recent.opened(p.recentGpa(), "/tmp/a\nb"); const text = rd(p, @backingInt(tree.TopFile.recent), 0, 8192).bytes; - try testing.expect(std.mem.indexOf(u8, text, "closed /tmp/a\\nb\\\\c\n") != null); + try testing.expect(std.mem.indexOf(u8, text, "closed /tmp/a\\nb\n") != null); } test "size is monotonic: growing is never refused, and a size once taken is taken again" { diff --git a/test/behaviour/look_and_exec.zig b/test/behaviour/look_and_exec.zig index 2ce95354..f769d72a 100644 --- a/test/behaviour/look_and_exec.zig +++ b/test/behaviour/look_and_exec.zig @@ -458,7 +458,11 @@ test "look-and-exec: a look opens spaced paths and locations without editing the try expectEqual(@as(u32, 8), (try s.range(s.at("/pane/{d}/dot", .{opened})))[0]); // line 2, column 3 // The source: its text, its undo steps and its cursor as they were. try s.expect(s.at("/pane/{d}/body", .{source}), "source stays intact\n"); - try std.testing.expectEqualStrings(ctl[0..60], (try s.read(s.at("/pane/{d}/ctl", .{source})))[0..60]); + const after = try s.read(s.at("/pane/{d}/ctl", .{source})); + try std.testing.expectEqualStrings(ctl[0..60], after[0..60]); + var fields = std.mem.tokenizeScalar(u8, after, ' '); + for (0..8) |_| _ = fields.next(); + try std.testing.expectEqualStrings("0", fields.next().?); // still no undo step try expectEqual(.{ @as(u32, 3), @as(u32, 3) }, try s.range(s.at("/pane/{d}/dot", .{source}))); try s.expect("/look", s.at("{d}\n", .{opened})); // From the root, with the source focused: the same pane, moved. @@ -623,6 +627,8 @@ test "look-and-exec: a second LookWord list look in a directory refills its +Sea try s.write(s.at("/pane/{d}/look", .{n}), "beta\n"); try expectEqual(@as(usize, 1), std.mem.count(u8, try s.read("/index"), "/+Search ")); try expectEqual(first, try paneNamed(&s, "/+Search")); + // Refilled with beta's places (columns 7-10), not alpha's. + try std.testing.expect(has(try s.read(s.at("/pane/{d}/body", .{first})), "words.txt:1:7-10\n")); } test "look-and-exec: `@pN:` looks at pane N as `a.txt:` looks at a.txt, and a line past the end is said one way, `:N` or `file:N`" { @@ -668,6 +674,9 @@ test "look-and-exec: a builtin that fails through exec, tagexec or a column's ex // the same record, counted. try expectEqual(@as(usize, 3), std.mem.count(u8, log, "Unmount: no such mount zzz")); try std.testing.expect(has(log, "mount zzz (x2)")); + // A control character is refused with its reason. + try expectEqual(@as(u16, 22), try s.refused("/exec", "Msg a\x01b\n")); + try std.testing.expectEqualStrings("invalid command line: it holds a control character (or DEL) other than a tab", s.said); // A look miss: one err, no msg; the same miss again is that record counted. try s.write("/look", "zzqqnotthere\n"); try std.testing.expect(has(try s.read("/log"), "look: no match for \"zzqqnotthere\"")); diff --git a/test/behaviour/other_files.zig b/test/behaviour/other_files.zig index 2d5cb112..701f4f14 100644 --- a/test/behaviour/other_files.zig +++ b/test/behaviour/other_files.zig @@ -309,3 +309,20 @@ test "other-files: a remembered place is clamped to the file as it is now, its s try std.testing.expectEqual(back, try s.focused()); try std.testing.expectEqual(dot, try s.range(s.at("/pane/{d}/dot", .{back}))); } + +test "other-files: /index and /recent show a name's own backslash doubled, so `\\n` written in a name reads back unlike a newline" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "back\\nslash", .data = "x\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/back\\nslash\n", .{dir})); + const index = try s.read("/index"); + try std.testing.expect(has(index, s.at("{s}/back\\\\nslash ", .{dir}))); + try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, index, "\n")); + try s.write(s.at("/pane/{d}/exec", .{n}), "Get\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "Del\n"); + try std.testing.expect(has(try s.read("/recent"), s.at("closed {s}/back\\\\nslash\n", .{dir}))); +} -- cgit v1.3