From 7bbcd971a761def328fb9eeb881301bf45fb4783 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 10:28:11 -0300 Subject: test: twenty-one of src/ninep/ctl.zig's tests through the 9P tree alone: in test/behaviour/look_and_exec.zig a look's miss quoting up to 256 bytes then an ellipsis, @p:
taking any sam address (a miss and an unknown serial errs), file:3:0 refused as addr 3:0 is, ./x, ../x, x and /abs/x misses, a terminal's file:addr miss logged once, and an exec's builtin refused wrong #args bare or with an argument it takes none of; in flags.zig Undo and Redo stepping the body and saying when there is none (counted), dirty back to 0 at the saved text; in edit.zig an Edit rename one undo step and a failed one changing nothing; in panes.zig Mini bare, the name word bare, and ctl's undo and redo fields; in addresses.zig malformed addresses and a terminal's addr, dot and limit refused; in event.zig a bad record's words; in the new repls.zig Repl with no such language; in settings.zig two bad values logged as two; in root_ctl.zig Kill with a name and nothing running; in mounts.zig an unknown network a bad dial address; the old Unmount test dropped, mounts.zig's first test covering it; each checked to fail against a break (miss_path_cap 200, "no pane at", captured's wrong #args words, Redo's and Kill's messages, the name word's refusal, an event's backwards-range words, e_syntax, Repl's "others)", Mount's bad dial words, a splice back to the saved text left dirty, Edit's no substitution words, a setting's refusal quoting "x", zeroColumn missing a column 0, a look's no such file words, a terminal's text miss unquoted, ctl's undo flag needing two edits) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/ninep/ctl.zig | 301 ------------------------------------------------------ 1 file changed, 301 deletions(-) (limited to 'src') diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index bd460098..bb0fcb96 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -1465,73 +1465,6 @@ test "commands lists every builtin with its argument and the ctl that takes it" try testing.expect(std.mem.indexOf(u8, listed.bytes, row) != null); } -test "Undo and Redo on a pane's ctl step its body through its edits" { - const p = try withFile(testing.allocator, "one\n"); - defer p.deinit(); - const serial = serialOf(p); - const body = Node.of(serial, .body); - try testing.expectEqual(Status.ok, wr(p, body, "two\n").reply.status); - try testing.expectEqualStrings("one\ntwo\n", rd(p, body, 0, 64).bytes); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Undo").reply.status); - try testing.expectEqualStrings("one\n", rd(p, body, 0, 64).bytes); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Redo").reply.status); - try testing.expectEqualStrings("one\ntwo\n", rd(p, body, 0, 64).bytes); - // Nothing left: said, and the write still succeeds, as acme's is silent. - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Redo").reply.status); - try testing.expect(th.logHas(p, "Redo: nothing to redo")); - _ = wr(p, Node.of(serial, .ctl), "Undo"); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Undo").reply.status); - try testing.expect(th.logHas(p, "Undo: nothing to undo")); - // Said again word for word, it is that line counted, as err's are. - for (0..3) |_| _ = wr(p, Node.of(serial, .ctl), "Undo"); - try testing.expect(th.logHas(p, "Undo: nothing to undo (x4)\n")); -} - -test "Edit through a pane's ctl renames every foo in one undo step, and a failed one changes nothing" { - const p = try withFile(testing.allocator, "foo x foo y foo\n"); - defer p.deinit(); - const serial = serialOf(p); - const ctl_node = Node.of(serial, .ctl); - const body = Node.of(serial, .body); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "Edit ,x/foo/c/foobar/\n").reply.status); - try testing.expectEqualStrings("foobar x foobar y foobar\n", rd(p, body, 0, 64).bytes); - const failed = wr(p, ctl_node, "Edit ,s/nothing/x/\n"); - try testing.expectEqual(E.IO, failed.errno()); - try testing.expectStringStartsWith(failed.reply.ename, "Edit: no substitution"); - try testing.expectEqualStrings("foobar x foobar y foobar\n", rd(p, body, 0, 64).bytes); - // The whole rename is one undo step. - try testing.expectEqual(Status.ok, wr(p, ctl_node, "Undo").reply.status); - try testing.expectEqualStrings("foo x foo y foo\n", rd(p, body, 0, 64).bytes); -} - - -test "dirty goes back to 0 when the text is the saved text again" { - const p = try withFile(testing.allocator, "one\n"); - defer p.deinit(); - const serial = serialOf(p); - const dirty = Node.of(serial, .dirty); - try testing.expectEqualStrings("0\n", rd(p, dirty, 0, 8).bytes); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .body), "two\n").reply.status); - try testing.expectEqualStrings("1\n", rd(p, dirty, 0, 8).bytes); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Undo").reply.status); - try testing.expectEqualStrings("0\n", rd(p, dirty, 0, 8).bytes); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Redo").reply.status); - try testing.expectEqualStrings("1\n", rd(p, dirty, 0, 8).bytes); -} - -test "a look's miss repeats the path whole up to 256 bytes, then gives up its middle to an ellipsis" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const root = @backingInt(tree.TopFile.look); - const long = "some/deeply/nested/directory/that/goes/on/and/on/with/a_rather_long_file_name.txt"; - var line: [512]u8 = undefined; - _ = wr(p, root, try std.fmt.bufPrint(&line, "{s}\n", .{long})); - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&line, "look: no match for \"{s}\"\n", .{long}))); - const huge = std.mem.asBytes(&@as([150][2]u8, @splat("é".*))); // 300 bytes: its middle cut to fit 256, between characters - _ = wr(p, root, huge ++ "\n"); - try testing.expect(th.logHas(p, "look: no match for \"" ++ std.mem.asBytes(&@as([63][2]u8, @splat("é".*))) ++ "…" ++ std.mem.asBytes(&@as([63][2]u8, @splat("é".*))) ++ "\"\n")); -} test "status reads the editor, and look and exec validate whole batches" { const gpa = testing.allocator; @@ -2150,17 +2083,6 @@ test "a look at a file it may not read fails the write naming the file" { try testing.expectEqualStrings(expected, refused.reply.ename); } -test "a bad setting value is quoted, so two different bad values log as two records, never one counted twice" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - _ = wr(p, root_ctl, "Verbose maybe\n"); - _ = wr(p, root_ctl, "Verbose nope\n"); - try testing.expect(th.logHas(p, "takes on, off \"maybe\"\n")); - try testing.expect(th.logHas(p, "takes on, off \"nope\"\n")); - try testing.expect(!th.logHas(p, "(x2)")); -} - test "a look line's trailing colon is dropped, and a look whose address fails never leaves its file open" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -2408,19 +2330,6 @@ test "Joincol with no column to the right and Theme with no such theme say so" { } -test "a look at file:addr whose file is not there is one miss, quoting what was written, in a terminal too" { - const p = try th.withTerm(testing.allocator); - defer p.deinit(); - const looked = Node.of(serialOf(p), .look); - for ([_][]const u8{ "zzq:#3", "zzq:2", "zzq:/re/" }) |written| { - var line: [64]u8 = undefined; - _ = wr(p, looked, try std.fmt.bufPrint(&line, "{s}\n", .{written})); - var want: [64]u8 = undefined; - try testing.expectEqual(@as(usize, 1), th.logCount(p, try std.fmt.bufPrint(&want, "look: no match for \"{s}\"\n", .{written}))); - } - try testing.expectEqual(@as(usize, 3), th.logCount(p, "\nerr ")); -} - test "a look whose pane is refused a place fails with that alone, not a missing line too" { const gpa = testing.allocator; // A screen with rows for one pane only. @@ -2603,16 +2512,6 @@ test "Tty in a directory that is not there fails and makes no pane; a shell that try testing.expectEqualStrings("error shell gone\n", call(p, .{ .tag = 3, .op = .read, .node = run_node, .handle = h, .size = 64 }).bytes); } -test "a builtin that needs its argument, written bare to an exec, is EINVAL as through ctl" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - for ([_]u64{ root_exec, Node.of(serialOf(p), .exec), @backingInt(tree.TopFile.tagexec) }) |node| { - const refused = wr(p, node, "Mount\n"); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expectEqualStrings("wrong #args in control message \"Mount\"", refused.reply.ename); - } -} - test "a script's Grep and Find fill the pane's one query pane, a row a line under its file; a miss says not found and opens nothing" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -2677,14 +2576,6 @@ test "a script's Grep and Find fill the pane's one query pane, a row a line unde try testing.expect(panes.Query.ofOwner(p, serialOf(p)) == null); } -test "Unmount of a name not mounted says no such mount, which a mount reads as ENOENT" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const refused = wr(p, @backingInt(tree.TopFile.ctl), "Unmount nothere\n"); - try testing.expectEqual(E.NOENT, refused.errno()); - try testing.expectStringStartsWith(refused.reply.ename, "Unmount: no such mount nothere"); -} - test "a terminal told the directory it already goes by logs no rename" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -2987,14 +2878,6 @@ test "a failure said while a 9P write waits on it posts no msg, only the write's try testing.expect(p.fs.late_failure_len > 0); } -test "Mini with no argument is wrong #args, EINVAL, as every word that needs one" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const r = wr(p, Node.of(serialOf(p), .ctl), "Mini\n"); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expectEqualStrings("wrong #args in control message \"Mini\"", r.reply.ename); -} - test "a look that misses file:/re/ names the file, and is logged as its pane's" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -3049,26 +2932,6 @@ test "with no pane at all, the root look opens its file and the root exec runs i try testing.expectEqualStrings(session, made.cwdSlice()); } -test "a look at file:3:0 is refused as addr 3:0 is, a column counting from 1" { - const p = try withFile(testing.allocator, "one\ntwo\nthree\n"); - defer p.deinit(); - const serial = serialOf(p); - const by_addr = wr(p, Node.of(serial, .addr), "3:0"); - const by_look = wr(p, Node.of(serial, .look), "/test.txt:3:0\n"); - try testing.expectEqual(E.INVAL, by_addr.errno()); - try testing.expectEqual(E.INVAL, by_look.errno()); - try testing.expectEqualStrings(by_addr.reply.ename, by_look.reply.ename); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .look), "/test.txt:3:1\n").reply.status); -} - -test "Kill with a name and nothing running says so and does not fail, as bare Kill does" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const r = wr(p, @backingInt(tree.TopFile.ctl), "Kill zzz\n"); - try testing.expectEqual(Status.ok, r.reply.status); - try testing.expect(th.logHas(p, "Kill: nothing running")); -} - test "a restored pane whose file is gone from disk comes back dirty, and Del asks first" { if (comptime !pardes.hosted) return error.SkipZigTest; const p = try withFile(testing.allocator, "kept text\n"); @@ -3136,17 +2999,6 @@ test "a Restore of a name in the DumpDir asks for, and logs, the whole path it r } -test "a builtin that takes no argument, written with one to an exec, is refused wrong #args, as at a ctl" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - for ([_]u64{ @backingInt(tree.TopFile.exec), Node.of(serial, .exec) }) |node| for ([_][]const u8{ "Config extra\n", "DumpConfig extra\n" }) |line| { - const r = wr(p, node, line); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expectStringStartsWith(r.reply.ename, "wrong #args in control message"); - }; -} - test "acme's words run as pardes's (Put is Save, Delete a Del that does not ask) or are refused, never run as commands" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -3202,32 +3054,6 @@ test "a pane's ctl takes acme's words: done by the files that replace them, or r try testing.expectEqualStrings("0", std.mem.trimEnd(u8, rd(p, Node.of(serial, .dirty), 0, 8).bytes, "\n")); } -test "ctl reads acme's fields in acme's order, undo and redo flags included, then pardes's own" { - const p = try withFile(testing.allocator, "one\n"); - defer p.deinit(); - const serial = serialOf(p); - const ctl_node = Node.of(serial, .ctl); - const fields = struct { - fn of(text: []const u8, buf: *[16][]const u8) [][]const u8 { - var it = std.mem.tokenizeScalar(u8, text, ' '); - var n: usize = 0; - while (it.next()) |t| : (n += 1) buf[n] = t; - return buf[0..n]; - } - }; - var buf: [16][]const u8 = undefined; - var f = fields.of(rd(p, ctl_node, 0, 4096).bytes, &buf); - try testing.expectEqualStrings("0", f[8]); // nothing to undo - try testing.expectEqualStrings("0", f[9]); // nothing to redo - try testing.expect(std.mem.eql(u8, f[10], "current") or std.mem.eql(u8, f[10], "notcurrent")); - _ = wr(p, Node.of(serial, .body), "two\n"); - f = fields.of(rd(p, ctl_node, 0, 4096).bytes, &buf); - try testing.expectEqualStrings("1", f[8]); - _ = wr(p, ctl_node, "Undo\n"); - f = fields.of(rd(p, ctl_node, 0, 4096).bytes, &buf); - try testing.expectEqualStrings("1", f[9]); -} - test "a write to addr, limit or mark asks for no frame; one to body does" { const p = try withFile(testing.allocator, "one\n"); defer p.deinit(); @@ -3284,26 +3110,6 @@ test "/index shows a name as the log does: a newline in it is \\n, a control byt try testing.expect(std.mem.indexOf(u8, rd(p, @backingInt(tree.TopFile.index), 0, 4096).bytes, "/tmp/back\\\\nslash") != null); } -test "a look at a ./ or ../ name that is not there is a miss naming the file; a plain one is looked for as text" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.createDirPath(testing.io, "many"); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - _ = wr(p, Node.of(serialOf(p), .name), try std.fmt.bufPrint(&line, "{s}/many/a.txt\n", .{dir})); - for ([_][]const u8{ "./zz.txt", "../zz.txt" }) |name| { - const r = wr(p, Node.of(serialOf(p), .look), try std.fmt.bufPrint(&line, "{s}\n", .{name})); - try testing.expectEqual(Status.ok, r.reply.status); - var want: [64]u8 = undefined; - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&want, "look: {s}: no such file", .{name}))); - } - try testing.expectEqual(Status.ok, wr(p, Node.of(serialOf(p), .look), "zz.txt\n").reply.status); - try testing.expect(th.logHas(p, "look: no match for \"zz.txt\"")); -} - test "a served-tree pane spoils no Grep, and Look . from a gone directory is ENOENT, not /virtual/" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -3329,43 +3135,6 @@ test "a served-tree pane spoils no Grep, and Look . from a gone directory is ENO try testing.expectStringStartsWith(looked.reply.ename, "look: /tmp/pardes-no-such-dir-zz: no such directory"); } -test "a malformed address is bad address syntax; on a pane with no text any address is refused as nothing to address" { - const p = try th.withTerm(testing.allocator); - defer p.deinit(); - const t = try withFile(testing.allocator, "one\n"); - defer t.deinit(); - for ([_][]const u8{ "1x", "/(/", ",,x" }) |expr| { - const refused = wr(t, Node.of(serialOf(t), .addr), expr); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expect(refused.reply.ename.len > 0); - } - try testing.expectEqualStrings("bad address syntax", wr(t, Node.of(serialOf(t), .addr), "1x").reply.ename); - // A terminal (as an image or a PDF): addr, dot and limit alike. - for ([_]tree.PaneFile{ .addr, .dot, .limit }) |file| for ([_][]const u8{ "1", "1x" }) |expr| { - const refused = wr(p, Node.of(serialOf(p), file), expr); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expectEqualStrings("invalid address: this pane has no text to address", refused.reply.ename); - }; -} - -test "a bad event record says what is wrong with it" { - const p = try withFile(testing.allocator, "one\n"); - defer p.deinit(); - const event = Node.of(serialOf(p), .event); - const h = call(p, .{ .tag = 1, .op = .open, .node = event, .omode = 2 }).reply.handle; - for ([_][2][]const u8{ - .{ "MI0 1 0 1 x\n", "only a look or exec" }, - .{ "MX3 1 0 0 \n", "ends before it starts" }, - .{ "MX9 9 0 0 \n", "range past end" }, - .{ "garbage\n", "a record is" }, - }) |c| { - const refused = call(p, .{ .tag = 2, .op = .write, .node = event, .handle = h, .data = c[0] }); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expect(std.mem.indexOf(u8, refused.reply.ename, c[1]) != null); - } - _ = call(p, .{ .tag = 3, .op = .release, .node = event, .handle = h }); -} - test "a column takes panes until their documented minimums fill it, not until none can be halved" { const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 50 }); defer p.deinit(); @@ -3790,24 +3559,6 @@ test "size is monotonic: growing is never refused, and a size once taken is take try testing.expectStringStartsWith(bad.reply.ename, "invalid size"); } -test "Repl with no such language is invalid, EINVAL, and names a few whole and how many more" { - const p = try th.withTerm(testing.allocator); - defer p.deinit(); - const refused = wr(p, Node.of(serialOf(p), .ctl), "Repl zzlang\n"); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expect(std.mem.indexOf(u8, refused.reply.ename, "Repl: invalid language \"zzlang\"; - or one like zig ada bash c c_sharp clojure (") != null); - try testing.expect(std.mem.endsWith(u8, refused.reply.ename, " more)")); -} - -test "answer quotes the choice it refuses, and a bare name says it names nothing" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const node = Node.of(serialOf(p), .ctl); - const empty = wr(p, node, "name \n"); - try testing.expectEqual(E.INVAL, empty.errno()); - try testing.expectEqualStrings("invalid file name: name takes the name, and there is none", empty.reply.ename); -} - test "a 10k-line Edit text block is taken in linear time and memory" { const gpa = testing.allocator; const p = try withFile(gpa, "x\n"); @@ -4040,40 +3791,6 @@ test "a word written to a raw terminal's look is listed as in normal mode" { try testing.expect(made.file.?.output.?.from == .search); } -test "@p:
takes any sam address; a miss and an unknown serial are errs" { - const p = try withFile(testing.allocator, "foo x foo y\nbar\n"); - defer p.deinit(); - const serial = serialOf(p); - const root = @backingInt(tree.TopFile.look); - const dot = Node.of(serial, .dot); - const Case = struct { addr: []const u8, want: []const u8 }; - for ([_]Case{ - .{ .addr = "0/foo/", .want = " 0 3 " }, - .{ .addr = "/foo/", .want = " 6 9 " }, - .{ .addr = "#12,#15", .want = " 12 15 " }, - .{ .addr = "$", .want = " 16 16 " }, - .{ .addr = "1,2", .want = " 0 16 " }, - // Backward, as in addr: from the dot's start, wrapping to the last. - .{ .addr = "?foo?", .want = " 6 9 " }, - .{ .addr = "$-?y?", .want = " 10 11 " }, - }) |c| { - var line: [64]u8 = undefined; - try testing.expectEqual(Status.ok, wr(p, root, try std.fmt.bufPrint(&line, "@p{d}:{s}\n", .{ serial, c.addr })).reply.status); - try testing.expectEqualStrings(c.want, rd(p, dot, 0, 64).bytes); - } - var line: [64]u8 = undefined; - _ = wr(p, root, try std.fmt.bufPrint(&line, "@p{d}:/zzqq/\n", .{serial})); - try testing.expect(th.logHas(p, ": no match for regexp")); - try testing.expectEqualStrings("", rd(p, root, 0, 64).bytes); - _ = wr(p, root, "@p999:/foo/\n"); - try testing.expect(th.logHas(p, "look: no pane @p999")); - try testing.expectEqualStrings("", rd(p, root, 0, 64).bytes); - // A line address to no pane is a miss too, not the active pane. - _ = wr(p, root, "@p77:1\n"); - try testing.expect(th.logHas(p, "look: no pane @p77")); - try testing.expectEqualStrings("", rd(p, root, 0, 64).bytes); -} - test "a look that goes to another pane leaves the looking pane's dot alone" { const p = try withFile(testing.allocator, "alpha bravo\ncharlie\n"); defer p.deinit(); @@ -4380,16 +4097,6 @@ test "Lspinfo's answer is the +Lsp pane it fills, as a Rename preview's is its + } -test "a look of ./x that is not there is a miss, as x and /abs/x are: answered, the err logged" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - for ([_][]const u8{ "./zzq-nosuch.txt\n", "zzq-nosuch.txt\n", "/zzq-nosuch.txt\n" }) |line| { - const r = wr(p, Node.of(serialOf(p), .look), line); - try testing.expectEqual(Status.ok, r.reply.status); - } - try testing.expect(th.logHas(p, "look: ./zzq-nosuch.txt: no such file")); -} - test "a ctl line over 1 MiB is refused once, EINVAL, and its tail is not run as a line of its own" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -4429,14 +4136,6 @@ test "DumpDir refuses a relative directory and one that may not be written, up f try testing.expect(std.mem.endsWith(u8, p.fs.late_failure[0..p.fs.late_failure_len], ": permission denied")); } -test "a Mount dial with a network it does not know is a bad dial address, EINVAL" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const r = wr(p, @backingInt(tree.TopFile.ctl), "Mount peer nowhere!x\n"); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expectStringStartsWith(r.reply.ename, "Mount: bad dial address: nowhere!x"); -} - test "a command or a terminal refused for room is refused before anything is logged of it, and its serial is never handed out again" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); -- cgit v1.3 From b798f2c1cf5dd4f98c8c1f81c8642b005890f6ee Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 10:40:00 -0300 Subject: test: ten more of src/ninep/ctl.zig's tests through the 9P tree alone, in test/behaviour/panes.zig: get reloading from disk and asking once over unsaved text, Get and Get file (asking once, loading a file by a name from the pane's directory), Undo after Get file or Edit e putting the old name, text and clean state back with Putall saving nothing, a failed Get file leaving no name, text or undo step changed, Zerox's twins (one text, undo and dirty state, a cursor each, Del of one asking nothing, a rename logged for each), the small refusals (one name an open, EISDIR, EACCES and ENOTDIR in words, /dev/zero not a regular file, twins saved once by Putall, a 2-byte scratch loaded over unasked) and Get of a file not there saying so unasked; in root_ctl.zig a bad Tab width and a bad Incl directory refused, changing nothing (bare Incl's list the same); the Zerox test kept for its Save performed, and the init-file half of the Get test, each trimmed; each checked to fail against a break (get's Modified words, Get not asking about unsaved edits, an undo of a load not putting the name back, Del asking about a twin, a twin's rename not logged, Putall saving each twin, Get's not-there check after the ask, Tab taking 17, a failed Get file pushing an undo step) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/ninep/ctl.zig | 285 ++------------------------------------------ test/behaviour/panes.zig | 258 +++++++++++++++++++++++++++++++++++++++ test/behaviour/root_ctl.zig | 31 +++++ 3 files changed, 297 insertions(+), 277 deletions(-) (limited to 'src') diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index bb0fcb96..fc09c730 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -1580,204 +1580,28 @@ test "look validates the whole batch before opening virtual files" { try testing.expectEqualStrings("source\n", source.file.?.content); } -test "get reloads the pane from disk and reports a missing file without losing dirty contents" { - const gpa = testing.allocator; - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" }); - var directory_buf: [4096]u8 = undefined; - const directory = directory_buf[0..try tmp.dir.realPath(testing.io, &directory_buf)]; - var path_buf: [4096]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "{s}/note.txt", .{directory}); - - const p = try withFile(gpa, "in memory\n"); - defer p.deinit(); - const serial = serialOf(p); - const ctl_node = Node.of(serial, .ctl); - const pane = p.panes[0].?; - - var name: [std.fs.max_path_bytes + 8]u8 = undefined; - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&name, "{s}\n", .{path})); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "get\n").reply.status); - try testing.expectEqualStrings("from disk\n", pane.file.?.content); - try testing.expect(!pane_files.dirtyOf(pane)); - try testing.expect(pane.file.?.history.undo_len > 0); - // Renamed but never edited, it was clean: nothing thrown away, nothing - // logged as reloaded over edits. - var reloaded: [32]u8 = undefined; - try testing.expect(!th.logHas(p, try std.fmt.bufPrint(&reloaded, "changed {d} reloaded\n", .{serial}))); - - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&name, "{s}/missing.txt\n", .{directory})); - _ = wr(p, Node.of(serial, .body), "unsaved\n"); - // Unsaved text is asked about once, as acme's get asks winclean. - const asked = wr(p, ctl_node, "get\n"); - try testing.expectEqual(E.IO, asked.errno()); - try testing.expect(std.mem.endsWith(u8, asked.reply.ename, ": Modified (get again to discard)")); - var unsaved: [4200]u8 = undefined; - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&unsaved, "unsaved {d} {s}/missing.txt\n", .{ serial, directory }))); - try testing.expectEqual(E.NOENT, wr(p, ctl_node, "get\n").errno()); - try testing.expect(p.paneBySerial(serial) != null); - try testing.expectEqualStrings("from disk\nunsaved\n", pane.file.?.content); - try testing.expect(pane_files.dirtyOf(pane)); -} -test "Get reads the pane's file again, refusing unsaved edits once, and Get file loads that file into the pane" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "other.txt", .data = "the other\n" }); - var directory_buf: [4096]u8 = undefined; - const directory = directory_buf[0..try tmp.dir.realPath(testing.io, &directory_buf)]; - const p = try withFile(testing.allocator, "in memory\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - var name: [std.fs.max_path_bytes + 8]u8 = undefined; - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&name, "{s}/note.txt\n", .{directory})); - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expectEqualStrings("from disk\n", pane.file.?.content); - try testing.expect(!pane_files.dirtyOf(pane)); - // Edited: asked once, then read again. - _ = wr(p, Node.of(serial, .body), "unsaved\n"); - const failures = p.fs.failures; - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expect(p.fs.failures != failures); - try testing.expectEqualStrings("from disk\nunsaved\n", pane.file.?.content); - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expectEqualStrings("from disk\n", pane.file.?.content); - // Get file: that file, by a name from the pane's directory. - try testing.expect(p.executeBuiltinLine(0, "Get other.txt")); - try testing.expectEqualStrings("the other\n", pane.file.?.content); - try testing.expect(std.mem.endsWith(u8, pane.file.?.path, "/other.txt")); - try testing.expect(!pane_files.dirtyOf(pane)); -} - - -test "Zerox makes a second pane on one buffer: one text, undo and dirty state, its own cursor; Save cleans both, Del of one asks nothing" { +test "Zerox twins: a Save from one, performed, cleans both" { + // The rest of Zerox is test/behaviour/panes.zig's; this needs the save done. var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); var dir_buf: [4096]u8 = undefined; const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - const p = try withFile(testing.allocator, "one\ntwo\nthree\n"); + const p = try withFile(testing.allocator, "one\n"); defer p.deinit(); - const first = serialOf(p); var name: [std.fs.max_path_bytes + 8]u8 = undefined; - _ = wr(p, Node.of(first, .name), try std.fmt.bufPrint(&name, "{s}/z.txt\n", .{dir})); + _ = wr(p, Node.of(serialOf(p), .name), try std.fmt.bufPrint(&name, "{s}/z.txt\n", .{dir})); try testing.expect(p.executeBuiltinLine(0, "Zerox")); const twin_id = for (p.panes, 0..) |slot, id| { if (id != 0) if (slot) |pane| if (pane.file != null) break id; } else return error.NoTwin; const a = p.panes[0].?; const b = p.panes[twin_id].?; - // Both in the index, each its serial, one name. - const index = rd(p, @backingInt(tree.TopFile.index), 0, 4096).bytes; - try testing.expectEqual(@as(usize, 2), std.mem.count(u8, index, "/z.txt")); - // b's cursor on `three`; a line written above it in a moves it down. - b.body.cur_row = 2; - b.body.cur_col = 1; - _ = wr(p, Node.of(first, .addr), "0"); - try testing.expectEqual(Status.ok, wr(p, Node.of(first, .data), "zero\n").reply.status); - try testing.expectEqualStrings("zero\none\ntwo\nthree\n", b.file.?.content); - try testing.expectEqual(a.file.?.content.ptr, b.file.?.content.ptr); - try testing.expectEqual(@as(i32, 3), b.body.cur_row); - try testing.expectEqual(@as(i32, 1), b.body.cur_col); - try testing.expect(pane_files.dirtyOf(a) and pane_files.dirtyOf(b)); - // A write through b is a's too; Undo in a takes it back from both. - try testing.expectEqual(Status.ok, wr(p, Node.of(b.serial, .body), "four\n").reply.status); - try testing.expectEqualStrings("zero\none\ntwo\nthree\nfour\n", a.file.?.content); - try testing.expect(p.executeBuiltinLine(0, "Undo")); - try testing.expectEqualStrings("zero\none\ntwo\nthree\n", b.file.?.content); - // Saved from b: both come clean. + try testing.expectEqual(Status.ok, wr(p, Node.of(b.serial, .body), "two\n").reply.status); try testing.expect(p.executeBuiltinLine(twin_id, "Save")); while (p.nextEffect()) |e| p.perform(e); try testing.expect(!pane_files.dirtyOf(a) and !pane_files.dirtyOf(b)); - try testing.expectEqualStrings("zero\none\ntwo\nthree\n", p.fallback.get(b.file.?.path).?); - // Edited again, Del of a asks nothing: b holds the text. - try testing.expectEqual(Status.ok, wr(p, Node.of(b.serial, .body), "five\n").reply.status); - const failures = p.fs.failures; - try testing.expect(p.executeBuiltinLine(0, "Del")); - p.sync(); - try testing.expectEqual(failures, p.fs.failures); - try testing.expect(p.paneBySerial(first) == null); - try testing.expect(pane_files.dirtyOf(b)); - try testing.expectEqualStrings("zero\none\ntwo\nthree\nfive\n", b.file.?.content); - // Alone now, it is no twin; Del asks about it as about any pane. - try testing.expectEqual(@as(u32, 0), b.file.?.twin); - try testing.expect(p.executeBuiltinLine(p.paneBySerial(b.serial).?, "Del")); - try testing.expect(p.fs.failures != failures); -} - -test "Undo after a load under another name (Get file, Edit e) puts the old name back with the old text and its clean state; Putall then writes nothing over the loaded file" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "other.txt", .data = "other\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "keep.txt", .data = "keep\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var other_buf: [4200]u8 = undefined; - const other = try std.fmt.bufPrint(&other_buf, "{s}/other.txt", .{dir}); - var keep_buf: [4200]u8 = undefined; - const keep = try std.fmt.bufPrint(&keep_buf, "{s}/keep.txt", .{dir}); - for ([_][]const u8{ "Get keep.txt", "Edit e keep.txt" }) |load| { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - var name: [4300]u8 = undefined; - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&name, "{s}\n", .{other})); - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expectEqualStrings("other\n", pane.file.?.content); - try testing.expect(!pane_files.dirtyOf(pane)); - // Loaded: keep.txt's text under its name. - try testing.expect(p.executeBuiltinLine(0, load)); - try testing.expectEqualStrings("keep\n", pane.file.?.content); - try testing.expectEqualStrings(keep, pane.file.?.path); - if (std.mem.startsWith(u8, load, "Get")) try testing.expect(!pane_files.dirtyOf(pane)); - // Undo: other.txt's text, other.txt's name, clean as it was. - try testing.expect(p.executeBuiltinLine(0, "Undo")); - try testing.expectEqualStrings("other\n", pane.file.?.content); - try testing.expectEqualStrings(other, pane.file.?.path); - try testing.expect(!pane_files.dirtyOf(pane)); - // Putall saves nothing, and nothing lands on keep.txt. - while (p.nextEffect()) |_| {} - try testing.expect(p.executeBuiltinLine(0, "Putall")); - while (p.nextEffect()) |e| try testing.expect(e != .save_file); - // Redo: keep.txt again. - try testing.expect(p.executeBuiltinLine(0, "Redo")); - try testing.expectEqualStrings(keep, pane.file.?.path); - try testing.expectEqualStrings("keep\n", pane.file.?.content); - } -} - -test "a Get file that fails changes nothing: no such file, a directory, or unsaved edits asked about once" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "here.txt", .data = "here\n" }); - try tmp.dir.createDirPath(testing.io, "sub"); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - var name: [4300]u8 = undefined; - const here = try std.fmt.bufPrint(&name, "{s}/here.txt\n", .{dir}); - _ = wr(p, Node.of(serial, .name), here); - try testing.expect(p.executeBuiltinLine(0, "Get")); - const undo_len = pane.file.?.history.undo_len; - for ([_][]const u8{ "Get nosuch.txt", "Get sub" }) |line| { - const failures = p.fs.failures; - try testing.expect(p.executeBuiltinLine(0, line)); - try testing.expect(p.fs.failures != failures); - try testing.expectEqualStrings(here[0 .. here.len - 1], pane.file.?.path); - try testing.expectEqualStrings("here\n", pane.file.?.content); - try testing.expectEqual(undo_len, pane.file.?.history.undo_len); - } - // Edited, the first Get asks and renames nothing. - _ = wr(p, Node.of(serial, .body), "more\n"); - try testing.expect(p.executeBuiltinLine(0, "Get other.txt")); - try testing.expectEqualStrings(here[0 .. here.len - 1], pane.file.?.path); - try testing.expectEqualStrings("here\nmore\n", pane.file.?.content); + try testing.expectEqualStrings("one\ntwo\n", p.fallback.get(b.file.?.path).?); } test "Zerox twins dump their text once and come back on one buffer from a Restore" { @@ -1809,36 +1633,6 @@ test "Zerox twins dump their text once and come back on one buffer from a Restor try testing.expect(std.mem.startsWith(u8, files[0].content, "one\ntwo\nunsaved ")); } -test "a bad Tab width and a bad Incl directory are refused invalid, changing nothing; a twin's rename is each twin's" { - defer config.tab_now = config.tab_width; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - for ([_][]const u8{ "Tab 0\n", "Tab 17\n", "Tab x\n" }) |line| { - try testing.expectEqual(E.INVAL, wr(p, root_ctl, line).errno()); - // Run from a pane, as a tag click runs it: said invalid too. - const r = wr(p, Node.of(serialOf(p), .exec), line); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expect(std.mem.indexOf(u8, r.reply.ename, "invalid tab width") != null); - } - try testing.expectEqual(config.tab_width, config.tab_now); - const before = p.incl.items.len; - try testing.expectEqual(E.INVAL, wr(p, root_ctl, "Incl rel/dir\n").errno()); - try testing.expectEqual(E.INVAL, wr(p, root_ctl, "Incl - /usr/include\n").errno()); - if (comptime pardes.hosted) try testing.expectEqual(E.NOENT, wr(p, root_ctl, "Incl /tmp/pardes-surely-no-such-dir\n").errno()); - try testing.expectEqual(before, p.incl.items.len); - // A twin renamed: a rename record for each. - const first = serialOf(p); - try testing.expect(p.executeBuiltinLine(0, "Zerox")); - const twin = for (p.panes) |slot| { - if (slot) |pane| if (pane.serial != first and pane.file != null) break pane.serial; - } else return error.NoTwin; - _ = wr(p, Node.of(first, .name), "/tmp/pardes-twin-renamed.txt\n"); - var rec: [64]u8 = undefined; - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&rec, "rename {d} /tmp/pardes-twin-renamed.txt\n", .{first}))); - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&rec, "rename {d} /tmp/pardes-twin-renamed.txt\n", .{twin}))); -} - test "Undo to unsaved text under its old name says nothing changed on disk when nothing did, and Save then writes" { var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); @@ -1872,73 +1666,10 @@ test "Undo to unsaved text under its old name says nothing changed on disk when } } -test "round 39's small refusals: one name an open, small scratches unasked, twins saved once, bare Tab on ctl, and errnos in words a mount reads" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "f.txt", .data = "f\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "noread.txt", .data = "secret\n" }); - try tmp.dir.createDirPath(testing.io, "sub"); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - var line: [4300]u8 = undefined; - // One name an open: a second line, in the write or after, is refused. - try testing.expectEqual(E.INVAL, wr(p, Node.of(serial, .name), "/tmp/pardes-a\nb").errno()); - const before = try testing.allocator.dupe(u8, pane.file.?.path); - defer testing.allocator.free(before); - const h = call(p, .{ .tag = 1, .op = .open, .node = Node.of(serial, .name), .omode = 1 }).reply.handle; - try testing.expectEqual(Status.ok, call(p, .{ .tag = 2, .op = .write, .node = Node.of(serial, .name), .handle = h, .data = "/tmp/pardes-one.txt\n" }).reply.status); - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 3, .op = .write, .node = Node.of(serial, .name), .handle = h, .data = "b" }).errno()); - _ = call(p, .{ .tag = 4, .op = .release, .node = Node.of(serial, .name), .handle = h, .opened = true }); - tree.runClosedLines(p); - try testing.expectEqualStrings("/tmp/pardes-one.txt", pane.file.?.path); - // Bare Tab on the root ctl says the width, as from a pane. - try testing.expectEqual(Status.ok, wr(p, @backingInt(tree.TopFile.ctl), "Tab\n").reply.status); - // Errnos a mount reads from the words. - _ = wr(p, Node.of(serial, .name), try std.fmt.bufPrint(&line, "{s}/f.txt\n", .{dir})); - try testing.expect(p.executeBuiltinLine(0, "Get")); - try testing.expectEqual(E.ISDIR, wr(p, Node.of(serial, .exec), "Get sub\n").errno()); - if (comptime pardes.hosted) { - var noread_buf: [4300]u8 = undefined; - _ = std.c.chmod(try std.fmt.bufPrintSentinel(&noread_buf, "{s}/noread.txt", .{dir}, 0), 0); - try testing.expectEqual(@as(u16, 13), wr(p, Node.of(serial, .exec), "Get noread.txt\n").errno()); - try testing.expectEqual(E.NOTDIR, wr(p, Node.of(serial, .exec), try std.fmt.bufPrint(&line, "Incl {s}/f.txt\n", .{dir})).errno()); - const zero = wr(p, Node.of(serial, .look), "/dev/zero\n"); - _ = zero; - try testing.expect(th.logHas(p, "not a regular file")); - } - // Twins saved once by Putall. - _ = wr(p, Node.of(serial, .body), "edited\n"); - try testing.expect(p.executeBuiltinLine(0, "Zerox")); - while (p.nextEffect()) |_| {} - try testing.expect(p.executeBuiltinLine(0, "Putall")); - var saves: usize = 0; - while (p.nextEffect()) |e| if (e == .save_file) { - saves += 1; - }; - try testing.expectEqual(@as(usize, 1), saves); - // A scratch of 2 bytes is loaded over unasked. - try testing.expect(p.executeBuiltinLine(0, "New")); - const scratch = p.panes[p.active].?; - _ = pane_files.spliceBody(p, scratch, 0, 0, "hi") orelse return error.NoScratch; - const failures = p.fs.failures; - try testing.expect(p.executeBuiltinLine(p.active, try std.fmt.bufPrint(&line, "Get {s}/f.txt", .{dir}))); - try testing.expectEqual(failures, p.fs.failures); - try testing.expectEqualStrings("f\n", scratch.file.?.content); -} - -test "Get of a file not there says so without asking about unsaved edits; the init file saved says a bad line as startup does" { +test "the init file saved says a bad line as startup does" { + // Get of a file not there is test/behaviour/panes.zig's. const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .startup_config = "Theme dusk\n", .startup_config_path = "/tmp/pardes-init-test/init" }); defer p.deinit(); - const pane = try p.setTestFile("x\n"); - _ = wr(p, Node.of(pane.serial, .body), "unsaved\n"); - try testing.expect(p.executeBuiltinLine(0, "Get /tmp/pardes-surely-no-such-file.txt")); - try testing.expect(th.logHas(p, "no such file")); - try testing.expect(!th.logHas(p, "again to discard")); - try testing.expect(pane.discard_warned == null); pardes.exec.reapplyInitFile(p, "/tmp/pardes-init-test/init", "Theme acme\nnosuchword 1\nNewcol\n"); try testing.expect(th.logHas(p, "err - init file line 2: unknown word \"nosuchword 1\"")); try testing.expect(!th.logHas(p, "init file line 3")); diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index 7febd667..9103508c 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -399,3 +399,261 @@ test "panes: ctl reads acme's fields in acme's order, undo and redo flags includ try s.write(ctl, "Undo\n"); try std.testing.expectEqualStrings("1", field(try s.read(ctl), 9)); } + +/// 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 `i`th blank-separated field of a pane's ctl read. +fn ctlField(s: *Session, n: u32, i: usize) ![]const u8 { + var it = std.mem.tokenizeScalar(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), ' '); + for (0..i) |_| _ = it.next(); + return it.next() orelse error.NoField; +} + +test "panes: get reloads the pane from disk and reports a missing file without losing dirty contents" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" }); + var s = try Session.initFile(std.testing.allocator, "in memory\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/note.txt\n", .{dir})); + try s.write(ctl, "get\n"); + try s.expect(body, "from disk\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); + try std.testing.expectEqualStrings("1", try ctlField(&s, n, 8)); // an undo step + // Renamed but never edited, it was clean: nothing thrown away, nothing + // logged as reloaded over edits. + try std.testing.expect(!has(try s.read("/log"), s.at("changed {d} reloaded\n", .{n}))); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/missing.txt\n", .{dir})); + try s.write(body, "unsaved\n"); + // Unsaved text is asked about once, as acme's get asks winclean. + try std.testing.expectEqual(@as(u16, 5), try s.refused(ctl, "get\n")); + try std.testing.expect(std.mem.endsWith(u8, s.said, ": Modified (get again to discard)")); + try std.testing.expect(has(try s.read("/log"), s.at("unsaved {d} {s}/missing.txt\n", .{ n, dir }))); + try std.testing.expectEqual(@as(u16, 2), try s.refused(ctl, "get\n")); + try s.expect(body, "from disk\nunsaved\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n"); +} + +test "panes: Get reads the pane's file again, refusing unsaved edits once, and Get file loads that file into the pane" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.txt", .data = "the other\n" }); + var s = try Session.initFile(std.testing.allocator, "in memory\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/note.txt\n", .{dir})); + try s.write(exec, "Get\n"); + try s.expect(body, "from disk\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); + // Edited: asked once, then read again. + try s.write(body, "unsaved\n"); + try std.testing.expect(try s.refused(exec, "Get\n") != 0); + try s.expect(body, "from disk\nunsaved\n"); + try s.write(exec, "Get\n"); + try s.expect(body, "from disk\n"); + // Get file: that file, by a name from the pane's directory. + try s.write(exec, "Get other.txt\n"); + try s.expect(body, "the other\n"); + try s.expect(s.at("/pane/{d}/name", .{n}), s.at("{s}/other.txt\n", .{dir})); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); +} + +test "panes: Undo after a load under another name (Get file, Edit e) puts the old name back with the old text and its clean state; Putall then writes nothing over the loaded file" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.txt", .data = "other\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "keep.txt", .data = "keep\n" }); + for ([_][]const u8{ "Get keep.txt\n", "Edit e keep.txt\n" }) |load| { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + const name = s.at("/pane/{d}/name", .{n}); + const dirty = s.at("/pane/{d}/dirty", .{n}); + try s.write(name, s.at("{s}/other.txt\n", .{dir})); + try s.write(exec, "Get\n"); + try s.expect(body, "other\n"); + try s.expect(dirty, "0\n"); + // Loaded: keep.txt's text under its name. + try s.write(exec, load); + try s.expect(body, "keep\n"); + try s.expect(name, s.at("{s}/keep.txt\n", .{dir})); + if (std.mem.startsWith(u8, load, "Get")) try s.expect(dirty, "0\n"); + // Undo: other.txt's text, other.txt's name, clean as it was. + try s.write(exec, "Undo\n"); + try s.expect(body, "other\n"); + try s.expect(name, s.at("{s}/other.txt\n", .{dir})); + try s.expect(dirty, "0\n"); + // Putall saves nothing, and nothing lands on keep.txt. + try s.write("/ctl", "Putall\n"); + try std.testing.expectEqual(@as(usize, 0), s.saved); + // Redo: keep.txt again. + try s.write(exec, "Redo\n"); + try s.expect(name, s.at("{s}/keep.txt\n", .{dir})); + try s.expect(body, "keep\n"); + } +} + +test "panes: a Get file that fails changes nothing: no such file, a directory, or unsaved edits asked about once" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "here.txt", .data = "here\n" }); + try tmp.dir.createDirPath(std.testing.io, "sub"); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + const name = s.at("/pane/{d}/name", .{n}); + const here = s.at("{s}/here.txt\n", .{dir}); + try s.write(name, here); + try s.write(exec, "Get\n"); + for ([_][]const u8{ "Get nosuch.txt\n", "Get sub\n" }) |line| { + try std.testing.expect(try s.refused(exec, line) != 0); + try s.expect(name, here); + try s.expect(body, "here\n"); + } + // No undo step left by them: one Undo takes back the first Get. + try s.write(exec, "Undo\n"); + try s.expect(body, "x\n"); + try s.write(exec, "Redo\n"); + // Edited, the first Get asks and renames nothing. + try s.write(body, "more\n"); + try std.testing.expect(try s.refused(exec, "Get other.txt\n") != 0); + try s.expect(name, here); + try s.expect(body, "here\nmore\n"); +} + +/// The serials /index lists with a name ending `suffix`, in its order. +fn panesNamed(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; +} + +test "panes: Zerox makes a second pane on one buffer: one text, undo and dirty state, its own cursor; Del of one asks nothing" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var s = try Session.initFile(std.testing.allocator, "one\ntwo\nthree\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const a = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{a}), s.at("{s}/z.txt\n", .{dir})); + try s.write(s.at("/pane/{d}/exec", .{a}), "Zerox\n"); + // Both in the index, each its serial, one name. + const twins = try panesNamed(&s, "/z.txt"); + try std.testing.expectEqual(@as(usize, 2), twins.len); + const b = if (twins[0] == a) twins[1] else twins[0]; + // b's cursor on `three`; a line written above it in a moves it down. + try s.write(s.at("/pane/{d}/dot", .{b}), "9 9"); + try s.write(s.at("/pane/{d}/addr", .{a}), "0"); + try s.write(s.at("/pane/{d}/data", .{a}), "zero\n"); + try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n"); + try std.testing.expectEqual(.{ @as(u32, 14), @as(u32, 14) }, try s.range(s.at("/pane/{d}/dot", .{b}))); + try s.expect(s.at("/pane/{d}/dirty", .{a}), "1\n"); + try s.expect(s.at("/pane/{d}/dirty", .{b}), "1\n"); + // A write through b is a's too; Undo in a takes it back from both. + try s.write(s.at("/pane/{d}/body", .{b}), "four\n"); + try s.expect(s.at("/pane/{d}/body", .{a}), "zero\none\ntwo\nthree\nfour\n"); + try s.write(s.at("/pane/{d}/exec", .{a}), "Undo\n"); + try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n"); + // Del of a asks nothing: b holds the text. + try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n"); + try std.testing.expectEqualSlices(u32, &.{b}, try panesNamed(&s, "/z.txt")); + try s.expect(s.at("/pane/{d}/dirty", .{b}), "1\n"); + try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n"); + // Alone now, it is no twin; Del asks about it as about any pane. + try std.testing.expect(try s.refused(s.at("/pane/{d}/ctl", .{b}), "Del\n") != 0); + try std.testing.expectEqualSlices(u32, &.{b}, try panesNamed(&s, "/z.txt")); +} + +test "panes: a twin renamed logs a rename for each twin" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const a = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{a}), "Zerox\n"); + const twins = try panesNamed(&s, "/test.txt"); + try std.testing.expectEqual(@as(usize, 2), twins.len); + try s.write(s.at("/pane/{d}/name", .{a}), "/tmp/pardes-twin-renamed.txt\n"); + for (twins) |n| try std.testing.expect(has(try s.read("/log"), s.at("rename {d} /tmp/pardes-twin-renamed.txt\n", .{n}))); +} + +test "panes: small refusals: one name an open, small scratches loaded over unasked, twins saved once, and errnos in words a mount reads" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "noread.txt", .data = "secret\n" }); + try tmp.dir.createDirPath(std.testing.io, "sub"); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const name = s.at("/pane/{d}/name", .{n}); + const exec = s.at("/pane/{d}/exec", .{n}); + // One name an open: a second line, in the write or after, is refused. + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/pardes-a\nb")); + const f = try s.hold(name, 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(f, "/tmp/pardes-one.txt\n")); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(f, "b")); + s.clunk(f); + try s.expect(name, "/tmp/pardes-one.txt\n"); + // Errnos a mount reads from the words. + try s.write(name, s.at("{s}/f.txt\n", .{dir})); + try s.write(exec, "Get\n"); + try std.testing.expectEqual(@as(u16, 21), try s.refused(exec, "Get sub\n")); // EISDIR + const noread = s.at("{s}/noread.txt\x00", .{dir}); + _ = std.c.chmod(noread[0 .. noread.len - 1 :0], 0); + try std.testing.expectEqual(@as(u16, 13), try s.refused(exec, "Get noread.txt\n")); // EACCES + try std.testing.expectEqual(@as(u16, 20), try s.refused(exec, s.at("Incl {s}/f.txt\n", .{dir}))); // ENOTDIR + _ = try s.refused(s.at("/pane/{d}/look", .{n}), "/dev/zero\n"); + try std.testing.expect(has(try s.read("/log"), "not a regular file")); + // Twins saved once by Putall. + try s.write(s.at("/pane/{d}/body", .{n}), "edited\n"); + try s.write(exec, "Zerox\n"); + const saved = s.saved; + try s.write("/ctl", "Putall\n"); + try std.testing.expectEqual(saved + 1, s.saved); + // A scratch of 2 bytes is loaded over unasked. + try s.write(exec, "New\n"); + const scratch = try s.focused(); + try s.write(s.at("/pane/{d}/body", .{scratch}), "hi"); + try s.write(s.at("/pane/{d}/exec", .{scratch}), s.at("Get {s}/f.txt\n", .{dir})); + try s.expect(s.at("/pane/{d}/body", .{scratch}), "f\n"); +} + +test "panes: Get of a file not there says so without asking about unsaved edits" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\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}/body", .{n}), "unsaved\n"); + _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "Get /tmp/pardes-surely-no-such-file.txt\n"); + const log = try s.read("/log"); + try std.testing.expect(has(log, "no such file")); + try std.testing.expect(!has(log, "again to discard")); + // Not asked, so the next Get of a file there is asked about, not run. + try std.testing.expect(try s.refused(s.at("/pane/{d}/exec", .{n}), s.at("Get {s}/f.txt\n", .{dir})) != 0); + try s.expect(s.at("/pane/{d}/body", .{n}), "x\nunsaved\n"); +} diff --git a/test/behaviour/root_ctl.zig b/test/behaviour/root_ctl.zig index c00c3cfe..3e546237 100644 --- a/test/behaviour/root_ctl.zig +++ b/test/behaviour/root_ctl.zig @@ -92,3 +92,34 @@ test "root-ctl: Kill with a name and nothing running says so and does not fail, try s.write("/ctl", "Kill zzz\n"); try std.testing.expect(has(try s.read("/log"), "Kill: nothing running")); } + +test "root-ctl: a bad Tab width and a bad Incl directory are refused invalid, changing nothing" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const before = try s.read("/ctl"); + // Bare, Incl lists its directories. + try s.write("/ctl", "Incl\n"); + const listed = try lastIncl(&s); + for ([_][]const u8{ "Tab 0\n", "Tab 17\n", "Tab x\n" }) |line| { + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", line)); + // Run from a pane, as a tag click runs it: said invalid too. + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), line)); + try std.testing.expect(has(s.said, "invalid tab width")); + } + try s.expect("/ctl", before); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Incl rel/dir\n")); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Incl - /usr/include\n")); + try std.testing.expectEqual(@as(u16, 2), try s.refused("/ctl", "Incl /tmp/pardes-surely-no-such-dir\n")); + // The same list again: none taken. + try s.write("/ctl", "Incl\n"); + try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, try s.read("/log"), listed)); + // Bare Tab on the root ctl says the width, as from a pane. + try s.write("/ctl", "Tab\n"); +} + +/// The log's one `msg - Incl:` record, whole. +fn lastIncl(s: *Session) ![]const u8 { + const log = try s.read("/log"); + const at = std.mem.indexOf(u8, log, "\nmsg - Incl:") orelse return error.NoIncl; + return log[at .. std.mem.indexOfScalarPos(u8, log, at + 1, '\n').? + 1]; +} -- cgit v1.3 From fb9b3a3304e0bdbeffc68e56dfe2ad6511a6a64e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 10:35:50 -0300 Subject: test: nineteen of src/ninep/pane.zig's tests through the 9P tree alone: in test/behaviour/flags.zig a > into an empty body one undo step (Undo empties it), a write that changes nothing no step and no new qid version (an empty : > data, a line written over itself, Edit's s/d/d/), and an open's hundred writes one edit (version +1, one Undo); in addresses.zig a 9P write moving dot as acme's textinsert (dot written, read back), an addr pair out of order or range refused in the address form's words, every address on a rune (a combining mark, a lone \r, a match inside a rune), and truncating data or xdata deleting only the addressed range; in panes.zig name refusing a directory (EISDIR), two lines on a held open or not, a component over 255 bytes by the write that holds it, a name cut across writes applied at its newline or close, a scratch promoted by name without touching its body, its file on disk or its undo, relative names from the file's directory and from a pane/new scratch's session directory and a /virtual one, errors appending to one +Errors (found in /index), and a truncated body's stat; in tags.zig a pane tag read whole and appended to up to 4096 bytes, and a truncated tag cleared, undone by u in the tag, clicked on, and a > dropping its one trailing newline; in look_and_exec.zig a pane's tagexec; each checked to fail against a break (a truncated empty body's step joined, spliceBody's no-op return gone, every held write put in at once, shiftOne growing nothing at dot's start, a pair's range unchecked, #n not snapped to a rune, data's truncation to the end, is_dir false, o.named never set, a held name's component limit 1255, a pane open's closing line dropped, a rename pushing an undo step, names resolved from /, pane/new taking the active pane's directory, a new +Errors every write, the tag's room one more, the tag's clear not remembered, the results read with a blank, an empty truncation of body); two old tests trimmed to what is an effect (a long path's failed Save; the watch a rename asks for); the line-index U test, the clock-held writes X test and the terminal and shell ones (pty bytes, output, spawns) stay Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/ninep/pane.zig | 505 ++------------------------------------- test/behaviour/addresses.zig | 91 +++++++ test/behaviour/flags.zig | 60 +++++ test/behaviour/look_and_exec.zig | 10 + test/behaviour/panes.zig | 185 ++++++++++++++ test/behaviour/tags.zig | 53 ++++ 6 files changed, 420 insertions(+), 484 deletions(-) (limited to 'src') diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index bf9dc381..cfc93ee5 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -312,56 +312,6 @@ pub fn spliceBody(p: *Pardes, pane: *Pane, q0: usize, q1: usize, bytes: []const return take; } -test "a shell's > into an empty pane's body is one undo step, as into any other" { - const p = try withFile(testing.allocator, ""); - defer p.deinit(); - const pane = p.panes[0].?; - const f = &pane.file.?; - const body = Node.of(pane.serial, .body); - const steps = f.history.undo_len; - const h = call(p, .{ .tag = 1, .op = .open, .node = body, .omode = 1 }).reply.handle; - _ = call(p, .{ .tag = 2, .op = .setattr, .node = body, .truncate = true }); - try testing.expectEqual(tree.Status.ok, call(p, .{ .tag = 3, .op = .write, .node = body, .handle = h, .data = "fresh\n" }).reply.status); - _ = call(p, .{ .tag = 4, .op = .release, .node = body, .handle = h, .opened = true }); - try testing.expectEqualStrings("fresh\n", f.content); - try testing.expectEqual(steps + 1, f.history.undo_len); -} - -test "a write that changes nothing, an empty range deleted or a range replaced by itself, makes no undo step" { - const p = try withFile(testing.allocator, "abc\ndef\n"); - defer p.deinit(); - const pane = p.panes[0].?; - const serial = pane.serial; - const f = &pane.file.?; - const steps = f.history.undo_len; - const revision = f.revision; - const data = Node.of(serial, .data); - // Each as a shell does it, on an open of its own: `: > data` after - // an empty address, then `echo abc > data` over the line it holds. - _ = wr(p, Node.of(serial, .addr), "#2,#2"); - const h = call(p, .{ .tag = 1, .op = .open, .node = data, .omode = 1 }).reply.handle; - try testing.expectEqual(tree.Status.ok, call(p, .{ .tag = 2, .op = .setattr, .node = data, .truncate = true }).reply.status); - _ = call(p, .{ .tag = 3, .op = .release, .node = data, .handle = h, .opened = true }); - for ([_][]const u8{ "abc\n", "abd\n" }) |text| { - _ = wr(p, Node.of(serial, .addr), "1"); - const w = call(p, .{ .tag = 4, .op = .open, .node = data, .omode = 1 }).reply.handle; - try testing.expectEqual(tree.Status.ok, call(p, .{ .tag = 5, .op = .write, .node = data, .handle = w, .data = text }).reply.status); - _ = call(p, .{ .tag = 6, .op = .release, .node = data, .handle = w, .opened = true }); - if (text[2] == 'c') { - try testing.expectEqualStrings("abc\ndef\n", f.content); - try testing.expectEqual(steps, f.history.undo_len); - try testing.expectEqual(revision, f.revision); - } - } - // Edit's `s/d/d/` is the same nothing. - const undone = f.history.undo_len; - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(serial, .ctl), "Edit ,s/d/d/\n").reply.status); - try testing.expectEqual(undone, f.history.undo_len); - // A real change is still one. - try testing.expectEqualStrings("abd\ndef\n", f.content); - try testing.expectEqual(steps + 1, f.history.undo_len); -} - test "rows through the line index are the ones counting newlines gives, at every offset" { const p = try withFile(testing.allocator, "héllo\nwo\u{301}rld\n\n last line é\ntail"); defer p.deinit(); @@ -1114,38 +1064,6 @@ test "a body write to a terminal pane types at its shell" { try testing.expectEqual(Status.ok, r.reply.status); } -test "tag reads the whole tag and writes append to the editable tail" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const pane = p.panes[0].?; - const node = Node.of(pane.serial, .tag); - - const whole = rd(p, node, 0, 4096); - try testing.expect(std.mem.startsWith(u8, whole.bytes, "/test.txt")); - try testing.expect(std.mem.indexOf(u8, whole.bytes, "Del") != null); - try testing.expectEqual(@as(u64, whole.bytes.len), call(p, .{ .tag = 1, .op = .getattr, .node = node }).reply.attr.size); - - const before = rd(p, node, 0, 4096).bytes.len; - const w = wr(p, node, " Mine"); - try testing.expectEqual(@as(u32, 5), w.reply.written); - try testing.expect(std.mem.endsWith(u8, tagline.curTail(pane), " Mine")); - const after = rd(p, node, 0, 4096); - try testing.expectEqual(before + 5, after.bytes.len); - try testing.expect(std.mem.endsWith(u8, after.bytes, " Mine")); - - // A tag is a text like the body: a newline written to it stays one, and - // the tag reads back taller by it. - try testing.expectEqual(@as(u32, 10), wr(p, node, "\nNext Line").reply.written); - try testing.expect(std.mem.endsWith(u8, tagline.curTail(pane), " Mine\nNext Line")); - try testing.expect(std.mem.endsWith(u8, rd(p, node, 0, 4096).bytes, " Mine\nNext Line")); - - if (pane.tag.own) |own| p.gpa.free(own); - pane.tag.own = try p.gpa.alloc(u8, limits.max_tag_tail); - @memset(pane.tag.own.?, 'x'); - try testing.expectEqual(E.NOSPC, wr(p, node, "x").errno()); -} - test "data never splits a rune, in either direction" { const gpa = testing.allocator; const p = try withFile(gpa, "\u{00e9}x\n"); @@ -1179,32 +1097,6 @@ test "the writes of one open are one undo step, as bash's line-buffered printf m } } -test "an open's writes in a row are held and go in as one edit, seen by the next request" { - const p = try withFile(testing.allocator, "one\n"); - defer p.deinit(); - const serial = serialOf(p); - const body = Node.of(serial, .body); - const h = call(p, .{ .tag = 1, .op = .open, .node = body, .omode = 1 }).reply.handle; - const revision = p.panes[0].?.file.?.revision; - for (0..100) |_| _ = call(p, .{ .tag = 2, .op = .write, .node = body, .handle = h, .data = "0123456789\n" }); - // Held: nothing edited yet. - try testing.expectEqual(revision, p.panes[0].?.file.?.revision); - // Any other request puts them in, once. - try testing.expectEqual(@as(usize, 4 + 1100), rd(p, body, 0, 4096).bytes.len); - try testing.expectEqual(revision + 1, p.panes[0].?.file.?.revision); - _ = call(p, .{ .tag = 3, .op = .release, .node = body, .handle = h, .opened = true }); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Undo").reply.status); - try testing.expectEqualStrings("one\n", rd(p, body, 0, 64).bytes); - // data inserts at its address go on the same way. - const data = Node.of(serial, .data); - _ = wr(p, Node.of(serial, .addr), "#0"); - const d = call(p, .{ .tag = 4, .op = .open, .node = data, .omode = 1 }).reply.handle; - _ = call(p, .{ .tag = 5, .op = .write, .node = data, .handle = d, .data = "a" }); - _ = call(p, .{ .tag = 5, .op = .write, .node = data, .handle = d, .data = "b" }); - _ = call(p, .{ .tag = 6, .op = .release, .node = data, .handle = d, .opened = true }); - try testing.expectEqualStrings("abone\n", rd(p, body, 0, 64).bytes); -} - test "writes held before the shell's clock is first seen go in at its first step" { const p = try withFile(testing.allocator, "one\n"); defer p.deinit(); @@ -1266,97 +1158,12 @@ test "after a failed address, one that goes from it says there is none" { try testing.expectEqual(Status.ok, wr(p, addr, ".").reply.status); } -test "a 9P write moves dot only as acme's textinsert does, and scrolls only the view" { - const Case = struct { at: u32, want: State.Range }; - // dot is `cd` of `abcdef\n` (2..4); two bytes go in at `at`. - for ([_]Case{ - .{ .at = 0, .want = .{ .q0 = 4, .q1 = 6 } }, // before: shifted - .{ .at = 2, .want = .{ .q0 = 2, .q1 = 6 } }, // at its start: grown - .{ .at = 3, .want = .{ .q0 = 2, .q1 = 6 } }, // inside: grown - .{ .at = 4, .want = .{ .q0 = 2, .q1 = 4 } }, // at its end: left - .{ .at = 6, .want = .{ .q0 = 2, .q1 = 4 } }, // after: left - }) |c| { - const p = try withFile(testing.allocator, "abcdef\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[p.paneBySerial(serial).?].?; - setDot(pane, .{ .q0 = 2, .q1 = 4 }); - var addr: [16]u8 = undefined; - _ = wr(p, Node.of(serial, .addr), try std.fmt.bufPrint(&addr, "#{d}", .{c.at})); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .data), "XX").reply.status); - try testing.expectEqual(c.want, dotOf(pane)); - } - // A body append leaves dot, and a sel write after it replaces dot, not - // the text appended. - const p = try withFile(testing.allocator, "abcdef\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[p.paneBySerial(serial).?].?; - setDot(pane, .{ .q0 = 2, .q1 = 4 }); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .body), "more\n").reply.status); - try testing.expectEqual(State.Range{ .q0 = 2, .q1 = 4 }, dotOf(pane)); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .sel), "YY").reply.status); - try testing.expectEqualStrings("abYYef\nmore\n", fileOf(pane).?.content); -} - -test "an addr pair out of order or out of range is refused as the address form is, not clamped" { - const p = try withFile(testing.allocator, "abcdef\n"); - defer p.deinit(); - const serial = serialOf(p); - const addr = Node.of(serial, .addr); - try testing.expectEqualStrings(addressing.e_order, wr(p, addr, "5 2").reply.ename); - try testing.expectEqualStrings(addressing.e_order, wr(p, addr, "#5,#2").reply.ename); - try testing.expectEqualStrings(addressing.e_range, wr(p, addr, "2 99").reply.ename); - try testing.expectEqualStrings(addressing.e_range, wr(p, addr, "#2,#99").reply.ename); - try testing.expectEqual(Status.ok, wr(p, addr, "2 4").reply.status); - try testing.expectEqual(State.Range{ .q0 = 2, .q1 = 4 }, p.panes[p.paneBySerial(serial).?].?.fs.addr); -} - -test "name refuses a directory, EISDIR, and leaves the name as it was" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const node = Node.of(serial, .name); - for ([_][]const u8{ "/\n", "/tmp/\n", "sub/\n", "/tmp\n" }) |dir| { - if (comptime !pardes.hosted) if (std.mem.eql(u8, dir, "/tmp\n")) continue; - const r = wr(p, node, dir); - try testing.expectEqual(E.ISDIR, r.errno()); - try testing.expect(std.mem.indexOf(u8, r.reply.ename, "is a directory") != null); - } - try testing.expectEqualStrings("/test.txt", nameOf(p, p.panes[p.paneBySerial(serial).?].?)); -} - -test "a write of two lines to name is refused EINVAL, on a held open or not" { +// The rest moved to test/behaviour/panes.zig; a Save's failure and the +// watches a rename asks for are effects a host performs. +test "a long path's failed Save keeps its reason" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); const serial = serialOf(p); - const node = Node.of(serial, .name); - try testing.expectEqual(E.INVAL, wr(p, node, "/tmp/a\n/tmp/b\n").errno()); - const h = call(p, .{ .tag = 1, .op = .open, .node = node, .omode = 1 }).reply.handle; - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "/tmp/a\n/tmp/b\n" }).errno()); - _ = call(p, .{ .tag = 3, .op = .release, .node = node, .handle = h, .opened = true }); - try testing.expectEqualStrings("/test.txt", nameOf(p, p.panes[p.paneBySerial(serial).?].?)); - // Line by line, as bash writes it: the second line is refused. - const h2 = call(p, .{ .tag = 1, .op = .open, .node = node, .omode = 1 }).reply.handle; - try testing.expectEqual(Status.ok, call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h2, .data = "/tmp/pardes-a\n" }).reply.status); - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h2, .data = "/tmp/pardes-b\n" }).errno()); - _ = call(p, .{ .tag = 3, .op = .release, .node = node, .handle = h2, .opened = true }); - try testing.expectEqualStrings("/tmp/pardes-a", nameOf(p, p.panes[p.paneBySerial(serial).?].?)); -} - -test "a name with a component over 255 bytes is refused, and a long path's failed Save keeps its reason" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const refused = wr(p, Node.of(serial, .name), "/tmp/" ++ &@as([256]u8, @splat('c')) ++ "/f.txt\n"); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expectEqualStrings("invalid file name: a component over 255 bytes", refused.reply.ename); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .name), "/tmp/" ++ &@as([255]u8, @splat('c')) ++ "\n").reply.status); - // Held with no newline, it is refused by that write, not at the close. - const held = call(p, .{ .tag = 1, .op = .open, .node = Node.of(serial, .name), .omode = 1 }).reply.handle; - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 2, .op = .write, .node = Node.of(serial, .name), .handle = held, .data = "/tmp/" ++ &@as([256]u8, @splat('e')) }).errno()); - _ = call(p, .{ .tag = 3, .op = .release, .node = Node.of(serial, .name), .handle = held, .opened = true }); - try testing.expectEqual(@as(usize, 0), p.fs.closed_lines.items.len); // A Save of a 3000-byte path the host refuses: the waiting write's // reason is at the end, the path giving up its middle. // (Under /tmp: under / a user's Save is refused permission, not @@ -1372,28 +1179,25 @@ test "a name with a component over 255 bytes is refused, and a long path's faile try testing.expect(std.mem.endsWith(u8, late, "/f.txt: no such directory")); } -test "a name cut across writes is one name, applied once at its newline or its close" { - const p = try withFile(testing.allocator, "x\n"); +test "a scratch renamed to a file watches it only after its Save, and a virtual name never" { + const p = try withFile(testing.allocator, "opener\n"); defer p.deinit(); - const serial = serialOf(p); - const node = Node.of(serial, .name); - const h = call(p, .{ .tag = 1, .op = .open, .node = node, .omode = 1 }).reply.handle; - try testing.expect(h != 0); - // A piece that fills its Twrite goes on in the next. - p.fs.write_room = "/tmp/pardes-na".len; - _ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "/tmp/pardes-na" }); - p.fs.write_room = 0; - try testing.expectEqualStrings("/test.txt", nameOf(p, p.panes[p.paneBySerial(serial).?].?)); - _ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "me.txt\n" }); - try testing.expectEqualStrings("/tmp/pardes-name.txt", nameOf(p, p.panes[p.paneBySerial(serial).?].?)); - _ = call(p, .{ .tag = 3, .op = .release, .node = node, .handle = h, .opened = true }); - // No newline: held, whatever the write's size, and applied at the close. - const h3 = call(p, .{ .tag = 1, .op = .open, .node = node, .omode = 1 }).reply.handle; - _ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h3, .data = "/tmp/pardes-closed.txt" }); - try testing.expectEqualStrings("/tmp/pardes-name.txt", nameOf(p, p.panes[p.paneBySerial(serial).?].?)); - _ = call(p, .{ .tag = 3, .op = .release, .node = node, .handle = h3, .opened = true }); - tree.runClosedLines(p); - try testing.expectEqualStrings("/tmp/pardes-closed.txt", nameOf(p, p.panes[p.paneBySerial(serial).?].?)); + const serial = try th.newPane(p); + const pane = p.panes[p.paneBySerial(serial).?].?; + const name = Node.of(serial, .name); + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + var directory_buf: [4096]u8 = undefined; + const directory = directory_buf[0..try tmp.dir.realPath(testing.io, &directory_buf)]; + var line: [4120]u8 = undefined; + const renamed = wr(p, name, try std.fmt.bufPrint(&line, "{s}/renamed.zig\n", .{directory})); + try testing.expectEqual(false, renamed.watch.?); + try testing.expect(pane.file.?.watch_after_save); + const virtual = try th.newPane(p); + _ = wr(p, Node.of(virtual, .name), try std.fmt.bufPrint(&line, "/virtual/pane/{d}/body\n", .{serial})); + const unchanged = wr(p, Node.of(virtual, .name), "./body\n"); + try testing.expect(unchanged.watch == null); + try testing.expect(!p.panes[p.paneBySerial(virtual).?].?.file.?.watch_after_save); } test "a replace through data, truncate then write, is one undo step" { @@ -1424,120 +1228,6 @@ test "writes under mark 0 are one undo step, even with no history before them" { try testing.expectEqualStrings("one\ntwo\nthree\n", rd(p, body, 0, 64).bytes); } -test "truncating the tag clears its editable tail" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const pane = p.panes[0].?; - const tag = Node.of(pane.serial, .tag); - _ = wr(p, tag, " Mine"); - try testing.expect(tagline.curTail(pane).len > 0); - const cleared = call(p, .{ .tag = 1, .op = .setattr, .node = tag, .truncate = true }); - try testing.expectEqual(Status.ok, cleared.reply.status); - // Done at the first read or write after it (or the close). - _ = rd(p, tag, 0, 4096); - try testing.expectEqualStrings("", pane.tag.own.?); - try testing.expect(std.mem.indexOf(u8, rd(p, tag, 0, 4096).bytes, " Mine") == null); - // The tag keeps its undo: undoing in it brings back what was cleared. - pardes.edit.doUndo(p, &pane.tag); - try testing.expect(std.mem.endsWith(u8, tagline.curTail(pane), " Mine")); - _ = call(p, .{ .tag = 2, .op = .setattr, .node = tag, .truncate = true }); - _ = rd(p, tag, 0, 4096); - try testing.expectEqualStrings("", pane.tag.own.?); - - // Written back without a leading space, the text still stands apart - // from the path: the computed prefix brings the separator, so Look and - // Exec find `Get`, not `test.txtGet`. - _ = wr(p, tag, "Get"); - try testing.expectEqualStrings("Get", pane.tag.own.?); - const whole = rd(p, tag, 0, 4096).bytes; - try testing.expectEqualStrings("/test.txt Get", whole); - const word = config.wordBounds(whole, whole.len - "Get".len); - try testing.expectEqualStrings("Get", whole[word.lo..word.hi]); - // and a click on the G puts the tag's cursor on it - const at = tagline.tagOffsetAt(p, pane, 0, "/test.txt ".len).?; - try testing.expectEqualStrings("Get", whole[at.col..]); - - // `echo Make > tag`: the newline ending the truncating write's text is - // dropped, one only; a newline between its writes stays. - _ = call(p, .{ .tag = 3, .op = .release, .node = tag }); - _ = call(p, .{ .tag = 4, .op = .setattr, .node = tag, .truncate = true }); - _ = wr(p, tag, "Make\n"); - try testing.expectEqualStrings("Make", pane.tag.own.?); - _ = wr(p, tag, "More\n\n"); - try testing.expectEqualStrings("Make\nMore\n", pane.tag.own.?); - _ = call(p, .{ .tag = 5, .op = .release, .node = tag }); - // `>>` keeps what it is given. - _ = wr(p, tag, " Tail\n"); - try testing.expectEqualStrings("Make\nMore\n Tail\n", pane.tag.own.?); -} - -test "name reads the file name and writing it promotes a scratch without touching its body" { - const gpa = testing.allocator; - const p = try withFile(gpa, "opener\n"); - defer p.deinit(); - const serial = try th.newPane(p); - const pane = p.panes[p.paneBySerial(serial).?].?; - const name = Node.of(serial, .name); - const body = Node.of(serial, .body); - var scratch_name: [4200]u8 = undefined; - const scratch_line = try std.fmt.bufPrint(&scratch_name, "{s}\n", .{nameOf(p, pane)}); - try testing.expectEqualStrings(scratch_line, rd(p, name, 0, 4096).bytes); - try testing.expectEqual(@as(u64, scratch_line.len), call(p, .{ .tag = 1, .op = .getattr, .node = name }).reply.attr.size); - _ = wr(p, body, "scratch "); - _ = wr(p, body, "work\n"); - const undo_len = pane.file.?.history.undo_len; - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "renamed.zig", .data = "existing target\n" }); - var directory_buf: [4096]u8 = undefined; - const directory = directory_buf[0..try tmp.dir.realPath(testing.io, &directory_buf)]; - var path_buffer: [4096]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buffer, "{s}/renamed.zig", .{directory}); - var line: [4104]u8 = undefined; - const renamed = wr(p, name, try std.fmt.bufPrint(&line, "{s}\n", .{path})); - try testing.expectEqual(Status.ok, renamed.reply.status); - try testing.expectEqual(false, renamed.watch.?); - try testing.expectEqualStrings("scratch work\n", pane.file.?.content); - try testing.expectEqual(undo_len, pane.file.?.history.undo_len); - try testing.expect(pane.file.?.output == null and dirtyOf(pane)); - try testing.expect(pane.file.?.watch_after_save); - try testing.expectEqualStrings(path, std.mem.trimEnd(u8, rd(p, name, 0, 4096).bytes, "\n")); - const target = try fs.readFile(gpa, path); - defer gpa.free(target); - try testing.expectEqualStrings("existing target\n", target); - for ([_][]const u8{ "", "\n" }) |bad| - try testing.expectEqual(E.INVAL, wr(p, name, bad).errno()); - // One name, and what no file name holds: a second line, a NUL. - for ([_][2][]const u8{ - .{ "two\nlines\n", "a newline" }, - .{ "nul\x00.zig\n", "a NUL" }, - }) |c| { - const refused = wr(p, name, c[0]); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expectStringStartsWith(refused.reply.ename, "bad character in file name: "); - try testing.expect(std.mem.indexOf(u8, refused.reply.ename, c[1]) != null); - } - // Any other byte a file name holds is taken, and reads back as written: - // blanks at its ends, a tab, a control byte, DEL, C1, bytes not UTF-8. - for ([_][]const u8{ " lead.zig ", "tab\t.zig", "c\x01.zig", "del\x7f.zig", "c1\xc2\x85.zig", "bad\xff.zig" }) |odd| { - try testing.expectEqual(Status.ok, wr(p, name, try std.fmt.bufPrint(&line, "{s}\n", .{odd})).reply.status); - const read_back = std.mem.trimEnd(u8, rd(p, name, 0, 4096).bytes, "\n"); - try testing.expect(std.mem.endsWith(u8, read_back, odd)); - var again: [4200]u8 = undefined; - @memcpy(again[0..read_back.len], read_back); - try testing.expectEqual(Status.ok, wr(p, name, again[0..read_back.len]).reply.status); - try testing.expectEqualStrings(again[0..read_back.len], pane.file.?.path); - } - // A name that is one line and UTF-8 is taken, é and all. - try testing.expectEqual(Status.ok, wr(p, name, "caf\xc3\xa9.zig\n").reply.status); - try testing.expect(std.mem.endsWith(u8, pane.file.?.path, "/caf\xc3\xa9.zig")); - try testing.expectEqual(Status.ok, wr(p, name, try std.fmt.bufPrint(&line, "{s}\n", .{path})).reply.status); - try testing.expectEqualStrings(path, pane.file.?.path); - try testing.expectEqual(Status.ok, wr(p, name, "two words.zig\n").reply.status); - const spaced = try std.fmt.bufPrint(&path_buffer, "{s}/two words.zig", .{directory}); - try testing.expectEqualStrings(spaced, pane.file.?.path); -} - test "name refreshes cached syntax for unchanged contents" { if (!pardes.syntax.enabled) return error.SkipZigTest; pardes.syntax.start(testing.allocator); @@ -1555,58 +1245,6 @@ test "name refreshes cached syntax for unchanged contents" { try testing.expectEqualStrings("fn check() void {}\n", pane.file.?.content); } -test "relative names use the file directory and can name a new target" { - const gpa = testing.allocator; - const p = try withFile(gpa, "retained body\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "note.txt", .data = "from pane directory\n" }); - var directory_buf: [4096]u8 = undefined; - const directory = directory_buf[0..try tmp.dir.realPath(testing.io, &directory_buf)]; - const pane = p.panes[0].?; - const name = Node.of(pane.serial, .name); - const ctl_node = Node.of(pane.serial, .ctl); - var line: [4140]u8 = undefined; - try testing.expectEqual(Status.ok, wr(p, name, try std.fmt.bufPrint(&line, "{s}/old.txt\n", .{directory})).reply.status); - try testing.expectEqual(Status.ok, wr(p, name, "child/../note.txt\n").reply.status); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "get\n").reply.status); - try testing.expectEqualStrings("from pane directory\n", pane.file.?.content); - try testing.expectEqualStrings(directory, Pardes.paneDir(pane)); - try testing.expectEqual(Status.ok, wr(p, name, "./created.txt\n").reply.status); - var expected_buf: [4096]u8 = undefined; - const expected = try std.fmt.bufPrint(&expected_buf, "{s}/created.txt", .{directory}); - try testing.expectEqualStrings(expected, pane.file.?.path); - try testing.expect(!dirtyOf(pane)); // a name alone is no edit -} - -test "relative names follow a pane/new scratch's session directory and virtual directories" { - const p = try withFile(testing.allocator, "source body\n"); - defer p.deinit(); - const source = p.panes[0].?; - try testing.expectEqual(Status.ok, wr(p, Node.of(source.serial, .name), "/elsewhere/source.zig\n").reply.status); - // pane/new's scratch is the session's, not the active pane's. - const session = "/project/src"; - @memcpy(p.session_dir[0..session.len], session); - p.session_dir_len = session.len; - const serial = try th.newPane(p); - const scratch = p.panes[p.paneBySerial(serial).?].?; - const name = Node.of(serial, .name); - try testing.expectEqualStrings("/project/src", Pardes.paneDir(scratch)); - try testing.expectEqual(Status.ok, wr(p, name, "../out/./notes.txt\n").reply.status); - try testing.expectEqualStrings("/project/out/notes.txt", scratch.file.?.path); - try testing.expectEqualStrings("/project/out", Pardes.paneDir(scratch)); - var line: [256]u8 = undefined; - try testing.expectEqual(Status.ok, wr(p, name, try std.fmt.bufPrint(&line, "/virtual/pane/{d}/./body\n", .{source.serial})).reply.status); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "get\n").reply.status); - try testing.expectEqualStrings(source.file.?.content, scratch.file.?.content); - const unchanged = wr(p, name, "./body\n"); - try testing.expectEqual(Status.ok, unchanged.reply.status); - try testing.expect(unchanged.watch == null); - try testing.expect(!dirtyOf(scratch)); - try testing.expect(!scratch.file.?.watch_after_save); -} - test "owned cwd name promotion releases the former directory" { const p = try th.withTerm(testing.allocator); defer p.deinit(); @@ -1622,96 +1260,6 @@ test "owned cwd name promotion releases the former directory" { try testing.expectEqualStrings("/old/directory", Pardes.paneDir(pane)); } -test "errors append to one +Errors buffer per directory" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - - const live = for (p.panes) |slot| { - if (slot) |q| if (q.file) |f| if (f.output) |o| if (std.meta.activeTag(o.from) == .errors) break q; - } else null; - try testing.expect(live == null); - - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .errors), "boom\n").reply.status); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .errors), "again\n").reply.status); - - var found: usize = 0; - for (p.panes) |slot| { - const q = slot orelse continue; - const f = q.file orelse continue; - const o = f.output orelse continue; - if (std.meta.activeTag(o.from) != .errors) continue; - found += 1; - try testing.expectEqualStrings("boom\nagain\n", f.content); - try testing.expectEqualStrings("/+Errors", f.path); - } - try testing.expectEqual(@as(usize, 1), found); -} - -test "truncating data deletes only the addressed range, so > data replaces it" { - const p = try withFile(testing.allocator, "hello world\nsecond line\n"); - defer p.deinit(); - const serial = serialOf(p); - for ([_]PaneFile{ .data, .xdata }) |file| { - _ = call(p, .{ .tag = 1, .op = .setattr, .node = Node.of(serial, .body), .truncate = true }); - _ = wr(p, Node.of(serial, .body), "hello world\nsecond line\n"); - _ = wr(p, Node.of(serial, .addr), "#0,#5"); - try testing.expectEqual(Status.ok, call(p, .{ .tag = 2, .op = .setattr, .node = Node.of(serial, file), .truncate = true }).reply.status); - try testing.expectEqualStrings(" world\nsecond line\n", p.panes[0].?.file.?.content); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, file), "HOWDY").reply.status); - try testing.expectEqualStrings("HOWDY world\nsecond line\n", p.panes[0].?.file.?.content); - } -} - -test "setattr truncation empties the body and answers fresh attributes" { - const gpa = testing.allocator; - const p = try withFile(gpa, "one\ntwo\n"); - defer p.deinit(); - const serial = serialOf(p); - - const a = call(p, .{ .tag = 7, .op = .setattr, .node = Node.of(serial, .body), .truncate = true }); - try testing.expectEqual(Status.ok, a.reply.status); - try testing.expectEqual(@as(u64, 0), a.reply.attr.size); - try testing.expectEqualStrings("", p.panes[0].?.file.?.content); - - _ = wr(p, Node.of(serial, .body), "new text\n"); - try testing.expectEqualStrings("new text\n", p.panes[0].?.file.?.content); - - const noop = call(p, .{ .tag = 8, .op = .setattr, .node = Node.of(serial, .body) }); - try testing.expectEqual(@as(u64, 9), noop.reply.attr.size); -} - -test "addresses are in runes: a combining mark and a lone \\r are places of their own" { - const gpa = testing.allocator; - // e, U+0301 (two bytes, 1-2), x, \r, \n - const p = try withFile(gpa, "e\u{0301}x\r\n"); - defer p.deinit(); - const serial = serialOf(p); - const addr_node = Node.of(serial, .addr); - const dot = Node.of(serial, .dot); - const Case = struct { expr: []const u8, q0: u32, q1: u32 }; - for ([_]Case{ - .{ .expr = "#1,#3", .q0 = 1, .q1 = 3 }, // the mark alone - .{ .expr = "#2", .q0 = 1, .q1 = 1 }, // inside it: back to its start - .{ .expr = "#4,#5", .q0 = 4, .q1 = 5 }, // the \r alone - .{ .expr = "1:2", .q0 = 1, .q1 = 1 }, // line:col the same - .{ .expr = "1:3", .q0 = 1, .q1 = 1 }, - .{ .expr = "0/\xCC/", .q0 = 1, .q1 = 3 }, // a match inside a rune covers it - }) |c| { - try testing.expectEqual(Status.ok, wr(p, addr_node, c.expr).reply.status); - try testing.expectEqual(c.q0, p.panes[0].?.fs.addr.q0); - try testing.expectEqual(c.q1, p.panes[0].?.fs.addr.q1); - } - // Copied to dot, it reads back the same. - _ = wr(p, addr_node, "#1,#3"); - try testing.expectEqual(Status.ok, wr(p, dot, " 1 3 ").reply.status); - p.sync(); - try testing.expectEqualStrings(" 1 3 ", rd(p, dot, 0, 100).bytes); - _ = wr(p, addr_node, "#4,#5"); - try testing.expectEqualStrings("\r", rd(p, Node.of(serial, .xdata), 0, 100).bytes); -} - test "a limit write that fails keeps the limit a truncation cleared before it" { const gpa = testing.allocator; const p = try withFile(gpa, "one two\n"); @@ -1727,17 +1275,6 @@ test "a limit write that fails keeps the limit a truncation cleared before it" { try testing.expectEqualStrings("", rd(p, limit, 0, 100).bytes); } -test "a pane's tagexec runs a word as a click in its tag, and reads back what it touched" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .tagexec), "Msg from the tag\n").reply.status); - try testing.expect(th.logHas(p, "from the tag")); - var want: [16]u8 = undefined; - try testing.expectEqualStrings(try std.fmt.bufPrint(&want, "{d}\n", .{serial}), rd(p, Node.of(serial, .tagexec), 0, 64).bytes); -} - test "index names a command's pane cmd, a shell's term" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); diff --git a/test/behaviour/addresses.zig b/test/behaviour/addresses.zig index a1474c3d..dde36f59 100644 --- a/test/behaviour/addresses.zig +++ b/test/behaviour/addresses.zig @@ -63,3 +63,94 @@ test "addresses: sel reads the selection, and a write to sel replaces it and is try s.expect(body, "ab\ntwo\n"); try s.expect(sel, "ab"); } + +test "addresses: a 9P write moves dot only as acme's textinsert does" { + // dot is `cd` of `abcdef\n` (2 4); two bytes go in at `at`. + for ([_][3]u32{ + .{ 0, 4, 6 }, // before: shifted + .{ 2, 2, 6 }, // at its start: grown + .{ 3, 2, 6 }, // inside: grown + .{ 4, 2, 4 }, // at its end: left + .{ 6, 2, 4 }, // after: left + }) |c| { + var s = try Session.initFile(std.testing.allocator, "abcdef\n"); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + try s.write(dot, "2 4"); + try s.write(s.at("/pane/{d}/addr", .{n}), s.at("#{d}", .{c[0]})); + try s.write(s.at("/pane/{d}/data", .{n}), "XX"); + try expectEqual(.{ c[1], c[2] }, try s.range(dot)); + } + // A body append leaves dot, and a sel write after it replaces dot, not + // the text appended. + var s = try Session.initFile(std.testing.allocator, "abcdef\n"); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + try s.write(dot, "2 4"); + try s.write(s.at("/pane/{d}/body", .{n}), "more\n"); + try expectEqual(.{ @as(u32, 2), @as(u32, 4) }, try s.range(dot)); + try s.write(s.at("/pane/{d}/sel", .{n}), "YY"); + try s.expect(s.at("/pane/{d}/body", .{n}), "abYYef\nmore\n"); +} + +test "addresses: a pair out of order or out of range is refused as the address form is, not clamped" { + var s = try Session.initFile(std.testing.allocator, "abcdef\n"); + defer s.deinit(); + const addr = s.at("/pane/{d}/addr", .{try s.focused()}); + for ([_][2][]const u8{ + .{ "5 2", "addresses out of order" }, + .{ "#5,#2", "addresses out of order" }, + .{ "2 99", "address out of range" }, + .{ "#2,#99", "address out of range" }, + }) |c| { + try expectEqual(@as(u16, 22), try s.refused(addr, c[0])); + try std.testing.expectEqualStrings(c[1], s.said); + } + try s.write(addr, "2 4"); + try expectEqual(.{ @as(u32, 2), @as(u32, 4) }, try s.range(addr)); +} + +test "addresses: every address lands on a rune, and a combining mark and a lone \\r are places of their own" { + // e, U+0301 (two bytes, 1-2), x, \r, \n + var s = try Session.initFile(std.testing.allocator, "e\u{0301}x\r\n"); + defer s.deinit(); + const n = try s.focused(); + const addr = s.at("/pane/{d}/addr", .{n}); + for ([_]struct { []const u8, u32, u32 }{ + .{ "#1,#3", 1, 3 }, // the mark alone + .{ "#2", 1, 1 }, // inside it: back to its start + .{ "#4,#5", 4, 5 }, // the \r alone + .{ "1:2", 1, 1 }, // line:col the same + .{ "1:3", 1, 1 }, + .{ "0/\xCC/", 1, 3 }, // a match inside a rune covers it + }) |c| { + try s.write(addr, c[0]); + try expectEqual(.{ c[1], c[2] }, try s.range(addr)); + } + // Copied to dot, it reads back the same. + const dot = s.at("/pane/{d}/dot", .{n}); + try s.write(addr, "#1,#3"); + try s.write(dot, " 1 3 "); + try s.expect(dot, " 1 3 "); + try s.write(addr, "#4,#5"); + try s.expect(s.at("/pane/{d}/xdata", .{n}), "\r"); +} + +test "addresses: truncating data or xdata deletes only the addressed range, so > data replaces it" { + var s = try Session.initFile(std.testing.allocator, ""); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + for ([_][]const u8{ "data", "xdata" }) |file| { + try s.replace(body, "hello world\nsecond line\n"); + try s.write(s.at("/pane/{d}/addr", .{n}), "#0,#5"); + const h = try s.hold(s.at("/pane/{d}/{s}", .{ n, file }), 1); + try s.truncate(h); + try s.expect(body, " world\nsecond line\n"); + try expectEqual(@as(u16, 0), try s.writeFid(h, "HOWDY")); + s.clunk(h); + try s.expect(body, "HOWDY world\nsecond line\n"); + } +} diff --git a/test/behaviour/flags.zig b/test/behaviour/flags.zig index 656d219f..0c8b6660 100644 --- a/test/behaviour/flags.zig +++ b/test/behaviour/flags.zig @@ -40,3 +40,63 @@ test "flags: dirty, mark and scroll read and write the state acme kept behind ct try t.expect(term_dirty, "0\n"); try std.testing.expectEqual(@as(u16, 22), try t.refused(term_dirty, "1")); } + +test "flags: a > into an empty body is one undo step, as into any other" { + var s = try Session.initFile(std.testing.allocator, ""); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + try s.replace(body, "fresh\n"); + try s.expect(body, "fresh\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + try s.expect(body, ""); +} + +test "flags: a write that changes nothing, an empty range deleted or a range replaced by itself, is no undo step and no new version" { + var s = try Session.initFile(std.testing.allocator, "abc\ndef\n"); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + const addr = s.at("/pane/{d}/addr", .{n}); + const data = s.at("/pane/{d}/data", .{n}); + const ctl = s.at("/pane/{d}/ctl", .{n}); + // One real change, so a step made after it would be the one undone. + try s.write(addr, "1"); + try s.write(data, "abd\n"); + const version = (try s.stat(body)).version; + // Each as a shell does it: `: > data` after an empty address, `echo + // abd > data` over the line it holds, and Edit's `s/d/d/`. + try s.write(addr, "#2,#2"); + const h = try s.hold(data, 1); + try s.truncate(h); + s.clunk(h); + try s.write(addr, "1"); + try s.write(data, "abd\n"); + try s.write(ctl, "Edit ,s/d/d/\n"); + try s.expect(body, "abd\ndef\n"); + try std.testing.expectEqual(version, (try s.stat(body)).version); + try s.write(ctl, "Undo\n"); + try s.expect(body, "abc\ndef\n"); +} + +test "flags: an open's writes in a row go in as one edit, seen by the next request" { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + const version = (try s.stat(body)).version; + const h = try s.hold(body, 1); + for (0..100) |_| try std.testing.expectEqual(@as(u16, 0), try s.writeFid(h, "0123456789\n")); + try std.testing.expectEqual(@as(usize, 4 + 1100), (try s.read(body)).len); + try std.testing.expectEqual(version + 1, (try s.stat(body)).version); + s.clunk(h); + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + try s.expect(body, "one\n"); + // data inserts at its address go on the same way. + try s.write(s.at("/pane/{d}/addr", .{n}), "#0"); + const d = try s.hold(s.at("/pane/{d}/data", .{n}), 1); + _ = try s.writeFid(d, "a"); + _ = try s.writeFid(d, "b"); + s.clunk(d); + try s.expect(body, "abone\n"); +} diff --git a/test/behaviour/look_and_exec.zig b/test/behaviour/look_and_exec.zig index 98cab342..0777d385 100644 --- a/test/behaviour/look_and_exec.zig +++ b/test/behaviour/look_and_exec.zig @@ -323,3 +323,13 @@ test "look-and-exec: a +PdfSections row's first number is the page: a section no _ = try s.refused(look, "design.pdf:1:999\n"); try expectEqual(@as(usize, 2), try errs(&s)); } + +test "look-and-exec: a pane's tagexec runs a word as a click in its tag, and reads back what it touched" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const tagexec = s.at("/pane/{d}/tagexec", .{n}); + try s.write(tagexec, "Msg from the tag\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "from the tag") != null); + try s.expect(tagexec, s.at("{d}\n", .{n})); +} diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index a46f8432..553b1ca9 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -363,3 +363,188 @@ test "panes: a Mini that cannot read its file leaves the panes, the last Mini's try s.expect("/index", index); try s.expect(s.at("/pane/{d}/body", .{mini}), body); } + +test "panes: name refuses a directory, EISDIR, and leaves the name as it was" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const name = s.at("/pane/{d}/name", .{try s.focused()}); + for ([_][]const u8{ "/\n", "/tmp/\n", "sub/\n", "/tmp\n" }) |dir| { + try std.testing.expectEqual(@as(u16, 21), try s.refused(name, dir)); + try std.testing.expect(std.mem.indexOf(u8, s.said, "is a directory") != null); + } + try s.expect(name, "/test.txt\n"); +} + +test "panes: a write of two lines to name is refused EINVAL, on a held open or not" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const name = s.at("/pane/{d}/name", .{try s.focused()}); + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/a\n/tmp/b\n")); + const h = try s.hold(name, 1); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(h, "/tmp/a\n/tmp/b\n")); + s.clunk(h); + try s.expect(name, "/test.txt\n"); + // Line by line, as bash writes it: the second line is refused. + const h2 = try s.hold(name, 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(h2, "/tmp/pardes-a\n")); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(h2, "/tmp/pardes-b\n")); + s.clunk(h2); + try s.expect(name, "/tmp/pardes-a\n"); +} + +test "panes: a name with a component over 255 bytes is refused by the write that holds it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const name = s.at("/pane/{d}/name", .{try s.focused()}); + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/" ++ &@as([256]u8, @splat('c')) ++ "/f.txt\n")); + try std.testing.expectEqualStrings("invalid file name: a component over 255 bytes", s.said); + const longest = "/tmp/" ++ &@as([255]u8, @splat('c')); + try s.write(name, longest ++ "\n"); + // Held with no newline, it is refused by that write, and the close + // has nothing left to apply. + const held = try s.hold(name, 1); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(held, "/tmp/" ++ &@as([256]u8, @splat('e')))); + s.clunk(held); + try s.expect(name, longest ++ "\n"); +} + +test "panes: a name cut across writes is one name, applied once at its newline or its close" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const name = s.at("/pane/{d}/name", .{try s.focused()}); + const h = try s.hold(name, 1); + _ = try s.writeFid(h, "/tmp/pardes-na"); + try s.expect(name, "/test.txt\n"); + _ = try s.writeFid(h, "me.txt\n"); + try s.expect(name, "/tmp/pardes-name.txt\n"); + s.clunk(h); + // No newline: held, and applied at the close. + const h3 = try s.hold(name, 1); + _ = try s.writeFid(h3, "/tmp/pardes-closed.txt"); + try s.expect(name, "/tmp/pardes-name.txt\n"); + s.clunk(h3); + try s.expect(name, "/tmp/pardes-closed.txt\n"); +} + +test "panes: name reads the file name, and a write promotes a scratch without touching its body or its file" { + const gpa = std.testing.allocator; + var s = try Session.initFile(gpa, "opener\n"); + defer s.deinit(); + const n = try s.newPane(); + const name = s.at("/pane/{d}/name", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + const scratch = try s.read(name); + try std.testing.expect(std.mem.endsWith(u8, scratch, "/+New\n")); + try std.testing.expectEqual(@as(u64, scratch.len), (try s.stat(name)).size); + try s.write(body, "scratch "); + try s.write(body, "work\n"); + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "renamed.zig", .data = "existing target\n" }); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + const path = s.at("{s}/renamed.zig", .{dir}); + try s.write(name, s.at("{s}\n", .{path})); + try s.expect(body, "scratch work\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n"); + try s.expect(name, s.at("{s}\n", .{path})); + var target_buf: [64]u8 = undefined; + try std.testing.expectEqualStrings("existing target\n", try tmp.dir.readFile(std.testing.io, "renamed.zig", &target_buf)); + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "\n")); + // One name, and what no file name holds: a second line, a NUL. + for ([_][2][]const u8{ + .{ "two\nlines\n", "invalid file name: one name a write, on one line" }, + .{ "nul\x00.zig\n", "bad character in file name: a NUL" }, + }) |c| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, c[0])); + try std.testing.expectEqualStrings(c[1], s.said); + } + // Any other byte a file name holds is taken, and reads back as written: + // blanks at its ends, a tab, a control byte, DEL, C1, bytes not UTF-8. + for ([_][]const u8{ " lead.zig ", "tab\t.zig", "c\x01.zig", "del\x7f.zig", "c1\xc2\x85.zig", "bad\xff.zig" }) |odd| { + try s.write(name, s.at("{s}\n", .{odd})); + const read_back = std.mem.trimEnd(u8, try s.read(name), "\n"); + try std.testing.expect(std.mem.endsWith(u8, read_back, odd)); + try s.write(name, read_back); + try s.expect(name, s.at("{s}\n", .{read_back})); + } + // A name that is one line and UTF-8 is taken, é and all. + try s.write(name, "caf\xc3\xa9.zig\n"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(name), "/caf\xc3\xa9.zig\n")); + try s.write(name, "two words.zig\n"); + try s.expect(name, s.at("{s}/two words.zig\n", .{dir})); + // A rename is no edit: Undo takes back the last write. + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + try s.expect(body, "scratch "); +} + +test "panes: relative names go from the file's directory and can name a new target" { + var s = try Session.initFile(std.testing.allocator, "retained body\n"); + defer s.deinit(); + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from pane directory\n" }); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + const n = try s.focused(); + const name = s.at("/pane/{d}/name", .{n}); + try s.write(name, s.at("{s}/old.txt\n", .{dir})); + try s.write(name, "child/../note.txt\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "get\n"); + try s.expect(s.at("/pane/{d}/body", .{n}), "from pane directory\n"); + try s.write(name, "./created.txt\n"); + try s.expect(name, s.at("{s}/created.txt\n", .{dir})); + // A name alone is no edit. + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); +} + +test "panes: relative names follow a pane/new scratch's session directory, and a virtual one" { + var s = try Session.initFile(std.testing.allocator, "source body\n"); + defer s.deinit(); + const source = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{source}), "/elsewhere/source.zig\n"); + // pane/new's scratch is the session's, not the active pane's. + const n = try s.newPane(); + const name = s.at("/pane/{d}/name", .{n}); + const session = std.fs.path.dirname(std.mem.trimEnd(u8, try s.read(name), "\n")).?; + try std.testing.expect(!std.mem.eql(u8, session, "/elsewhere")); + const up = std.fs.path.dirname(session) orelse ""; + try s.write(name, "../out/./notes.txt\n"); + try s.expect(name, s.at("{s}/out/notes.txt\n", .{up})); + try s.write(name, "again.txt\n"); + try s.expect(name, s.at("{s}/out/again.txt\n", .{up})); + try s.write(name, s.at("/virtual/pane/{d}/./body\n", .{source})); + try s.write(s.at("/pane/{d}/ctl", .{n}), "get\n"); + try s.expect(s.at("/pane/{d}/body", .{n}), "source body\n"); + try s.write(name, "./body\n"); + try s.expect(name, s.at("/virtual/pane/{d}/body\n", .{source})); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); +} + +test "panes: errors appends to one +Errors pane for the pane's directory" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), "+Errors") == null); + try s.write(s.at("/pane/{d}/errors", .{n}), "boom\n"); + try s.write(s.at("/pane/{d}/errors", .{n}), "again\n"); + const index = try s.read("/index"); + try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, index, " /+Errors ")); + const at = std.mem.indexOf(u8, index, " /+Errors ").?; + const line = index[(if (std.mem.lastIndexOfScalar(u8, index[0..at], '\n')) |i| i + 1 else 0)..]; + const serial = line[0..std.mem.indexOfScalar(u8, line, ' ').?]; + try s.expect(s.at("/pane/{s}/body", .{serial}), "boom\nagain\n"); +} + +test "panes: truncating body empties it, and a stat says the length after" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const body = s.at("/pane/{d}/body", .{try s.focused()}); + const h = try s.hold(body, 1); + try s.truncate(h); + try std.testing.expectEqual(@as(u64, 0), (try s.stat(body)).size); + try s.expect(body, ""); + s.clunk(h); + try s.write(body, "new text\n"); + try std.testing.expectEqual(@as(u64, 9), (try s.stat(body)).size); +} diff --git a/test/behaviour/tags.zig b/test/behaviour/tags.zig index 6c1c6fcd..f4a5a469 100644 --- a/test/behaviour/tags.zig +++ b/test/behaviour/tags.zig @@ -397,3 +397,56 @@ test "tags: a name typed into the tag's path survives Esc, pending, and the next try s.expect(name, "/ztest.txt\n"); try std.testing.expect(std.mem.startsWith(u8, try s.read(s.at("/pane/{d}/tag", .{n})), "/ztest.txt ")); } + +test "tags: a pane's tag reads whole, its path first, and a write appends to its own text up to 4096 bytes" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const path = s.at("/pane/{d}/tag", .{n}); + const whole = try tag(&s, n); + try std.testing.expect(std.mem.startsWith(u8, whole, "/test.txt")); + try std.testing.expect(std.mem.indexOf(u8, whole, "Del") != null); + try std.testing.expectEqual(@as(u64, whole.len), (try s.stat(path)).size); + try s.write(path, " Mine"); + try std.testing.expectEqualStrings(s.at("{s} Mine", .{whole}), try tag(&s, n)); + // A tag is a text like the body: a newline written to it stays one. + try s.write(path, "\nNext Line"); + try std.testing.expectEqualStrings(s.at("{s} Mine\nNext Line", .{whole}), try tag(&s, n)); + // Full, it takes not a byte more. + try s.replace(path, &@as([4096]u8, @splat('x'))); + try std.testing.expectEqual(@as(u16, 28), try s.refused(path, "x")); +} + +test "tags: truncating a tag clears its own text, u in the tag undoes it, and > drops the one newline ending it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const path = s.at("/pane/{d}/tag", .{n}); + try s.write(path, " Mine"); + // Done at the first read or write after it (or the close). + const h = try s.hold(path, 1); + try s.truncate(h); + try std.testing.expect(std.mem.indexOf(u8, try tag(&s, n), "Mine") == null); + s.clunk(h); + try std.testing.expectEqualStrings("/test.txt", std.mem.trimEnd(u8, try tag(&s, n), " ")); + // The tag keeps its undo: undoing in it brings back what was cleared. + try s.keys(":u:"); + try std.testing.expect(std.mem.endsWith(u8, try tag(&s, n), " Mine")); + // Written back without a leading space, the text still stands apart + // from the path, and a click on its first letter lands on it. + try s.replace(path, "Get"); + try std.testing.expectEqualStrings("/test.txt Get", try tag(&s, n)); + try s.click("Get", 0, 1); + try std.testing.expectEqual(@as(usize, "/test.txt ".len), try tagCol(&s, "/test.txt")); + // `echo Make > tag`: the newline ending the truncating write's text is + // dropped, one only; a newline between its writes stays. + const m = try s.hold(path, 1); + try s.truncate(m); + _ = try s.writeFid(m, "Make\n"); + _ = try s.writeFid(m, "More\n\n"); + s.clunk(m); + try std.testing.expectEqualStrings("/test.txt Make\nMore\n", try tag(&s, n)); + // `>>` keeps what it is given. + try s.write(path, " Tail\n"); + try std.testing.expectEqualStrings("/test.txt Make\nMore\n Tail\n", try tag(&s, n)); +} -- cgit v1.3 From 52bfa3f2aa8f8158d4d7ca720d81ea0c654f94bf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 10:28:16 -0300 Subject: test: three of src/pardes.zig's tests through the 9P tree alone: in test/behaviour/columns_and_tags.zig a column tag lighting the word under the mouse, taking the caret where pressed, sweeping three cells in the selection's colour, typed at its caret (Home, Help) and scrolling its text from the column's edge, never its caret (each column); in test/behaviour/mouse.zig a click on either half of a wide character landing before it in the text (dot) and on a tag's path (a name drafted, Right stepping a grapheme, two cells then one), a two-byte λ typed in a tag and taken out by one backspace, and a sweep pressed on the line numbers taking only the text (unlit numbers, a Look in it finding its next place, a chord cutting it); Session's bg (a cell's background from /screen's styles); each checked to fail against a break (no tag hover, the header scrolled one cell too far, byteAtDisplay rounding a wide glyph's second half past it, Right stepping a byte in insert, the three clamps that keep a sweep's first row off the line numbers dropped) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/pardes.zig | 112 ------------------------------------ test/behaviour/columns_and_tags.zig | 57 ++++++++++++++++++ test/behaviour/mouse.zig | 64 +++++++++++++++++++++ test/session.zig | 12 ++++ 4 files changed, 133 insertions(+), 112 deletions(-) (limited to 'src') diff --git a/src/pardes.zig b/src/pardes.zig index b47119c4..70bcf7c9 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -2918,48 +2918,6 @@ test "every line of the board's boot buffer renders whole" { // ---- the boundary types ---- -test "Unicode display cells map back to body and tag byte cursors" { - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .cols = 60, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("a界e\u{301}z\n"); - const f = &pane.file.?; - p.gpa.free(f.path); - f.path = try p.gpa.dupe(u8, "界e\u{301}.txt"); - - var frame = std.heap.ArenaAllocator.init(gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - const rect = p.rects[0]; - const text_x = rect.x + config.GUTTER + config.PREFIX_W; - const body_y = rect.y + BOX_H; - // The second screen cell is the trailing half of the wide CJK grapheme. - // Both halves map to its one byte boundary. - p.update(.{ .mouse = .{ .button = config.select_button, .kind = .press, .col = text_x + 2, .row = body_y } }); - p.update(.{ .mouse = .{ .button = config.select_button, .kind = .release, .col = text_x + 2, .row = body_y } }); - try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col); - - // A click in the wide path glyph drafts a name with its caret at the - // grapheme start; arrow motion then advances by the full UTF-8 cluster. - tagline.draftName(p, pane, 1); - try std.testing.expectEqual(@as(i32, 0), pane.input.cur_col); - edit.insertKey(p, &pane.input, .{ .cp = Key.right }); - try std.testing.expectEqual(@as(i32, 3), pane.input.cur_col); - edit.insertKey(p, &pane.input, .{ .cp = Key.right }); - try std.testing.expectEqual(@as(i32, 6), pane.input.cur_col); - pane.closePrompt(); - - const before = try gpa.dupe(u8, tagline.curTail(pane)); - defer gpa.free(before); - tagline.enterTag(p, pane); - const insertion = pane.tag.cur_col; - edit.insertKey(p, &pane.tag, .{ .cp = 'λ', .text = "λ" }); - try std.testing.expectEqual(insertion + 2, pane.tag.cur_col); - edit.insertKey(p, &pane.tag, .{ .cp = Key.backspace }); - try std.testing.expectEqual(insertion, pane.tag.cur_col); - try std.testing.expectEqualStrings(before, tagline.curTail(pane)); -} - test "Tab N draws every pane's tab N cells wide and clicks count past it; bare Tab says the width; the ctl read and a Dump keep it" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .cols = 60, .rows = 12 }); @@ -3106,63 +3064,6 @@ test "editable workspace and column tags are typed into and persist" { try std.testing.expectEqualStrings("", tagline.columnTag(empty, 0)); } -test "column tags render edit hover and scroll from the column edge" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 20, .tty_only = true }); - defer p.deinit(); - _ = try p.setTestFile("untouched\n"); - p.newColumn(0); - p.newScratchIn(0, p.activeColumn().?); - p.sync(); - p.acknowledgePanelPresentation(&.{}); - var frame = std.heap.ArenaAllocator.init(std.testing.allocator); - defer frame.deinit(); - for (0..p.ncol) |column| { - const x = p.col_x[column] + TAG_TEXT_INSET; - const width = p.col_w[column] - TAG_TEXT_INSET; - const initial = try p.render(frame.allocator()); - for ("New", 0..) |byte, offset| - try std.testing.expectEqual(byte, initial.at(x + @as(u16, @intCast(offset)), TOPBAR_H).text[0]); - p.update(.{ .mouse = .{ .button = .none, .kind = .motion, .col = x, .row = TOPBAR_H } }); - const hovered = try p.render(frame.allocator()); - try std.testing.expectEqual(Color{ .rgb = p.theme().sel_bg }, hovered.at(x, TOPBAR_H).style.bg); - p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = x, .row = TOPBAR_H } }); - try std.testing.expectEqual(column, p.header_column.?); - try std.testing.expectEqual(@as(i32, 0), tagline.focusedHeader(p).?.cur_col); - const at_start = try p.render(frame.allocator()); - try std.testing.expectEqual(x, at_start.cursor.?.x); - p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = x + 3, .row = TOPBAR_H } }); - p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = x + 3, .row = TOPBAR_H } }); - const selected = try p.render(frame.allocator()); - for (0..3) |offset| - try std.testing.expectEqual(Color{ .rgb = p.theme().sel_bg }, selected.at(x + @as(u16, @intCast(offset)), TOPBAR_H).style.bg); - p.update(.{ .key = .{ .cp = Key.home } }); - p.update(.{ .key = .{ .cp = 'H', .text = "Help " } }); - try std.testing.expect(std.mem.startsWith(u8, tagline.columnTag(p, column), "Help New Tty")); - const edited = try p.render(frame.allocator()); - try std.testing.expectEqual(x + 5, edited.cursor.?.x); - // Only the text viewport participates in caret reveal. - const long: [80]u8 = @splat('a'); - if (p.col_tags[column].own) |own| p.gpa.free(own); - p.col_tags[column].own = try p.gpa.dupe(u8, long[0 .. width - 1]); - p.update(.{ .key = .{ .cp = Key.end } }); - const at_end = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(usize, 0), p.header_scroll); - try std.testing.expectEqual(x + width - 1, at_end.cursor.?.x); - p.update(.{ .key = .{ .cp = 'b', .text = "b" } }); - const revealed = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(usize, 1), p.header_scroll); - try std.testing.expectEqual(x + width - 1, revealed.cursor.?.x); - p.update(.{ .key = .{ .cp = Key.home } }); - const home = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(usize, 0), p.header_scroll); - try std.testing.expectEqual(x, home.cursor.?.x); - p.update(.{ .key = .{ .cp = Key.escape } }); - p.update(.pointer_leave); - _ = frame.reset(.retain_capacity); - } - try std.testing.expectEqualStrings("untouched\n", p.panes[0].?.file.?.content); -} - test "column tag first-cell execution keeps New local to the clicked column" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 20, .tty_only = true }); defer p.deinit(); @@ -9180,19 +9081,6 @@ test "board heap: every allocation failure during boot is a clean OutOfMemory" { ); } -test "a sweep pressed on the line numbers copies only the text" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("alpha beta\ngamma delta\n"); - const numbers: i32 = panes.File.gutterWidth(pane); - const top: i32 = pane.tag_rows; - pane.sel[0] = .{ .state = .done, .c0 = 1, .c1 = numbers + 4, .r0 = top, .r1 = top + 1 }; - // Live, while the drag is held, and captured, once it is released. - try std.testing.expectEqualStrings("alpha beta\ngamma", try edit.selectionText(p, pane, pane.sel[0])); - try edit.capturePointerSelection(p, pane, 0); - try std.testing.expectEqualStrings("alpha beta\ngamma", try edit.selectionText(p, pane, pane.sel[0])); -} - test "jump history clears selections and lands at the recorded cursor" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); defer p.deinit(); diff --git a/test/behaviour/columns_and_tags.zig b/test/behaviour/columns_and_tags.zig index d485ee6e..ac8e7034 100644 --- a/test/behaviour/columns_and_tags.zig +++ b/test/behaviour/columns_and_tags.zig @@ -233,3 +233,60 @@ test "columns-and-tags: a refused Delcol changes nothing: no pane, no focus" { try s.expect("/focus", focus); try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "unsaved ") != null); } + +/// Column `i`'s x and width, from /layout. +fn columnBox(s: *Session, i: usize) !struct { usize, usize } { + var lines = std.mem.tokenizeScalar(u8, try s.read("/layout"), '\n'); + for (0..i) |_| _ = lines.next(); + var fields = std.mem.tokenizeScalar(u8, lines.next().?, ' '); + _ = fields.next(); // serial + _ = fields.next(); // index + return .{ try std.fmt.parseInt(usize, fields.next().?, 10), try std.fmt.parseInt(usize, fields.next().?, 10) }; +} + +test "columns-and-tags: a column tag lights the word under the mouse, is clicked and swept into, typed at its caret, and scrolls from its column's edge" { + var s = try Session.initFile(std.testing.allocator, "untouched\n"); + defer s.deinit(); + const file = try s.focused(); + try s.write("/ctl", "Newcol\n"); + try s.write(s.at("/col/{d}/exec", .{try s.column(1)}), "New\n"); + for (0..2) |i| { + const tag = s.at("/col/{d}/tag", .{try s.column(i)}); + const left, const w = try columnBox(&s, i); + // Its text starts two cells into the column. + const x = left + 2; + const width = w - 2; + const plain = try s.bg(x, 1); + try s.write("/mousein", s.at("m {d} 1 0\n", .{x})); + const lit = try s.bg(x, 1); + try std.testing.expect(!std.mem.eql(u8, plain, lit)); + try expectEqualStrings(plain, try s.bg(x + 4, 1)); // Tty, another word + // Pressed, the keyboard is on the tag, its caret where it was pressed. + try s.write("/mousein", s.at("m {d} 1 1\n", .{x})); + try s.expect("/focus", ""); + try std.testing.expectEqual(.{ x, @as(usize, 1) }, (try s.cursor()).?); + // Swept over three cells: those three in the selection's colour. + try s.write("/mousein", s.at("m {d} 1 1\nm {d} 1 0\n", .{ x + 3, x + 3 })); + for (0..3) |k| try expectEqualStrings(lit, try s.bg(x + k, 1)); + try std.testing.expect(!std.mem.eql(u8, lit, try s.bg(x + 3, 1))); + try s.keys("\u{F00D}Help "); + try std.testing.expect(std.mem.startsWith(u8, try s.read(tag), "Help New Tty")); + try std.testing.expectEqual(x + 5, (try s.cursor()).?[0]); + // A tag one short of its room: its caret at the end is drawn on the + // last cell, and one more letter scrolls the text, never the caret. + const as: [80]u8 = @splat('a'); + const row = as[0 .. width - 1]; + try s.replace(tag, row); + try s.keys("\u{F018}"); + try std.testing.expectEqual(x + width - 1, (try s.cursor()).?[0]); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), row) != null); + try s.keys("b"); + try std.testing.expectEqual(x + width - 1, (try s.cursor()).?[0]); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), s.at("{s}b", .{row[1..]})) != null); + try s.keys("\u{F00D}"); + try std.testing.expectEqual(x, (try s.cursor()).?[0]); + try s.keys("\x1b\x1b"); + try s.write("/mousein", "m 0 23 0\n"); + } + try s.expect(s.at("/pane/{d}/body", .{file}), "untouched\n"); +} diff --git a/test/behaviour/mouse.zig b/test/behaviour/mouse.zig index 0abe15dc..aba0f2d5 100644 --- a/test/behaviour/mouse.zig +++ b/test/behaviour/mouse.zig @@ -131,3 +131,67 @@ test "mouse: a B1 sweep over several lines is a stream, as acme's, in a body and try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 0\n", .{ tx + 3, ty, lx + 1, ly, lx + 1, ly, lx + 1, ly }); try s.expect(tag, "/test.txt Save Tty Colok Edit"); } + +test "mouse: a click on either half of a wide character lands before it, in the text and on a tag's path, and the caret steps over it whole" { + var s = try Session.initFile(std.testing.allocator, "a界e\u{301}z\n"); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + const x, const y = try s.where("a界"); + // 界 is two cells, bytes 1 to 4; é two runes, bytes 4 to 7. + for ([_]usize{ 1, 2 }) |into| { + try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + into, y, x + into, y }); + try std.testing.expectEqual(.{ @as(u32, 1), @as(u32, 1) }, try s.range(dot)); + } + try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + 4, y, x + 4, y }); + try std.testing.expectEqual(.{ @as(u32, 7), @as(u32, 7) }, try s.range(dot)); + // On the tag's path, the second half of 界 drafts the name with its + // caret before it; Right steps a whole grapheme, two cells then one. + try s.write(s.at("/pane/{d}/name", .{n}), "/界e\u{301}.txt\n"); + const px, const py = try s.where("/界"); + try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ px + 2, py, px + 2, py }); + try std.testing.expectEqual(.{ px + 1, py }, (try s.cursor()).?); + try s.keys("\u{F012}"); + try std.testing.expectEqual(.{ px + 3, py }, (try s.cursor()).?); + try s.keys("\u{F012}"); + try std.testing.expectEqual(.{ px + 4, py }, (try s.cursor()).?); + try s.keys("\x1b\x1b"); + try s.expect(s.at("/pane/{d}/name", .{n}), "/界e\u{301}.txt\n"); + // Typed past the tag's words, a two-byte λ goes in and one backspace + // takes it out whole. + const tag = try s.read(s.at("/pane/{d}/tag", .{n})); + try s.click("Collapse Del", "Collapse Del".len + 1, 1); + const caret = (try s.cursor()).?; + try s.keys("λ"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/tag", .{n})), "λ")); + try std.testing.expectEqual(.{ caret[0] + 1, caret[1] }, (try s.cursor()).?); + try s.keys("\x08"); + try std.testing.expectEqual(caret, (try s.cursor()).?); + try s.expect(s.at("/pane/{d}/tag", .{n}), tag); +} + +test "mouse: a sweep pressed on the line numbers takes only the text: drawn while held and once let go, looked for by a click in it, cut by a chord" { + var s = try Session.initFile(std.testing.allocator, "alpha beta\ngamma delta\nalpha beta\ngamma\n"); + defer s.deinit(); + const n = try s.focused(); + const x, const y = try s.where("alpha"); + const plain = try s.bg(x - 2, y); + try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\n", .{ x - 2, y, x + 4, y + 1 }); + for (0..2) |released| { + // The number pressed on, and the room after the line, stay unlit. + try std.testing.expectEqualStrings(plain, try s.bg(x - 2, y)); + try std.testing.expectEqualStrings(plain, try s.bg(x - 2, y + 1)); + const lit = try s.bg(x, y); + try std.testing.expect(!std.mem.eql(u8, plain, lit)); + try std.testing.expectEqualStrings(lit, try s.bg(x + 4, y + 1)); + try std.testing.expectEqualStrings(plain, try s.bg(x + 5, y + 1)); + if (released == 0) try mouse(&s, "m {d} {d} 0\n", .{ x + 4, y + 1 }); + } + // A Look clicked in it looks for the text swept: its next place, the + // third and fourth lines. + try mouse(&s, "m {d} {d} 4\nm {d} {d} 0\n", .{ x + 1, y, x + 1, y }); + try std.testing.expectEqual(.{ @as(u32, 23), @as(u32, 39) }, try s.range(s.at("/pane/{d}/dot", .{n}))); + // A chord cuts what was swept: the text, no number. + try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 1\nm {d} {d} 0\n", .{ x - 2, y, x + 4, y + 1, x + 4, y + 1, x + 4, y + 1, x + 4, y + 1 }); + try s.expect(s.at("/pane/{d}/body", .{n}), " delta\nalpha beta\ngamma\n"); +} diff --git a/test/session.zig b/test/session.zig index 16a131fd..5dd506ec 100644 --- a/test/session.zig +++ b/test/session.zig @@ -314,6 +314,18 @@ pub const Session = struct { var it = std.mem.splitScalar(u8, tag[lo + 2 .. close], '/'); return .{ try std.fmt.parseInt(u32, it.next().?, 10), try std.fmt.parseInt(u32, it.next() orelse return error.NoPage, 10) }; } + + // core2: + + /// The background /screen draws at a cell, as its JSON (`{"rgb":[255,255,255]}`). + pub fn bg(s: *Session, col: usize, row: usize) ![]const u8 { + const a = s.arena.allocator(); + const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try s.read("/screen"), .{}); + const cols: usize = @intCast(json.object.get("cols").?.integer); + const cell = json.object.get("cells").?.array.items[row * cols + col]; + const style = json.object.get("styles").?.array.items[@intCast(cell.array.items[1].integer)]; + return std.json.Stringify.valueAlloc(a, style.object.get("bg").?, .{}); + } }; fn refusal(path: []const u8, r: Reply) error{Refused} { -- cgit v1.3 From ff0b8a382de7a1994a6912dc71ba28a8ecdaf474 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 10:45:36 -0300 Subject: test: syntax colours and Save's prompt through the 9P tree alone, Session.styles added (each cell's style as /screen draws it, as JSON text): in test/behaviour/settings.zig Colors off drawing a source plain and on again colouring its fn and if, the layout as it was, an edit made while off included, and SyntaxBold keywords coloured and bold in every row a detached session's size grows a pane into; in panes.zig a rename colouring a source in its new name's language, its text unchanged; in keys.zig Save's prompt on a scratch made by New from a file's pane starting from its directory, one slash after it (/dír space/, and / alone); each checked to fail against a break (the Colors toggle not marking syntax dirty, the painted stretch four rows from the scroll, a rename not marking syntax dirty, the prompt's slash always added, the prompt from the session's directory); test/panes.zig's Colors and growing tests and pane.zig's cached-syntax rename test deleted, and the Save prompt test trimmed to an owned directory ending in a slash or empty (a shell's, T2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/ninep/pane.zig | 17 -------- test/behaviour/keys.zig | 15 +++++++ test/behaviour/panes.zig | 17 ++++++++ test/behaviour/settings.zig | 72 +++++++++++++++++++++++++++++++ test/panes.zig | 101 ++------------------------------------------ test/session.zig | 17 ++++++++ 6 files changed, 125 insertions(+), 114 deletions(-) (limited to 'src') diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index cfc93ee5..28f1882d 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -1228,23 +1228,6 @@ test "writes under mark 0 are one undo step, even with no history before them" { try testing.expectEqualStrings("one\ntwo\nthree\n", rd(p, body, 0, 64).bytes); } -test "name refreshes cached syntax for unchanged contents" { - if (!pardes.syntax.enabled) return error.SkipZigTest; - pardes.syntax.start(testing.allocator); - defer pardes.syntax.stop(); - const p = try withFile(testing.allocator, "fn check() void {}\n"); - defer p.deinit(); - const pane = p.panes[0].?; - panes.File.refreshHighlights(p); - try testing.expectEqual(@as(usize, 0), pane.file.?.highlights.len); - try testing.expectEqual(Status.ok, wr(p, Node.of(pane.serial, .name), "renamed.zig\n").reply.status); - panes.File.refreshHighlights(p); - try testing.expect(pane.file.?.highlights.len >= 2); - try testing.expectEqual(@backingInt(pardes.syntax.Syn.keyword), pane.file.?.highlights[0]); - try testing.expectEqual(@backingInt(pardes.syntax.Syn.keyword), pane.file.?.highlights[1]); - try testing.expectEqualStrings("fn check() void {}\n", pane.file.?.content); -} - test "owned cwd name promotion releases the former directory" { const p = try th.withTerm(testing.allocator); defer p.deinit(); diff --git a/test/behaviour/keys.zig b/test/behaviour/keys.zig index 41a2821a..989489d7 100644 --- a/test/behaviour/keys.zig +++ b/test/behaviour/keys.zig @@ -225,3 +225,18 @@ test "keys: SPC j n and SPC j p walk Themes' rows, whole, never its section head try s.keys("\t"); try std.testing.expectEqualStrings(names.items[brk - 1]["Theme ".len..], try setting(&s, "Theme")); } + +test "keys: Save's prompt on a scratch starts from the scratch's directory, one slash after it" { + for ([_][2][]const u8{ .{ "/dír space/f.txt", " Save /dír space/zq" }, .{ "/f.txt", " Save /zq" } }) |c| { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}\n", .{c[0]})); + // New from the file's pane: a scratch in its directory. + try s.write(s.at("/pane/{d}/exec", .{n}), "New\n"); + try std.testing.expect(try s.focused() != n); + try s.keys(":\tzq"); + _ = try s.where(c[1]); + try s.keys("\x1b"); + } +} diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index 553b1ca9..9dfc6abd 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -548,3 +548,20 @@ test "panes: truncating body empties it, and a stat says the length after" { try s.write(body, "new text\n"); try std.testing.expectEqual(@as(u64, 9), (try s.stat(body)).size); } + +test "panes: a rename colours a source its new name's language names, its text unchanged" { + if (!@import("pardes_config").syntax_highlighting) return error.SkipZigTest; + var s = try Session.initFile(std.testing.allocator, "fn check() void {}\n"); + defer s.deinit(); + const n = try s.focused(); + const x, const y = try s.where("fn check"); + const screen = try s.screen(); + const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]); + const plain = try s.styles(); + try std.testing.expectEqualStrings(plain[y * cols + x], plain[y * cols + x + 3]); + try s.write(s.at("/pane/{d}/name", .{n}), "renamed.zig\n"); + const coloured = try s.styles(); + try std.testing.expectEqualStrings(coloured[y * cols + x], coloured[y * cols + x + 1]); + try std.testing.expect(!std.mem.eql(u8, coloured[y * cols + x], plain[y * cols + x])); + try s.expect(s.at("/pane/{d}/body", .{n}), "fn check() void {}\n"); +} diff --git a/test/behaviour/settings.zig b/test/behaviour/settings.zig index ff24704a..5ba227bc 100644 --- a/test/behaviour/settings.zig +++ b/test/behaviour/settings.zig @@ -101,3 +101,75 @@ test "settings: Config without a configuration directory says so and opens nothi try s.expect("/index", index); try expectHas(try s.read("/log"), "Config: no startup file here: no configuration directory"); } + +const syntax = @import("pardes_config").syntax_highlighting; + +/// The style /screen draws the cell `into` cells into the first place +/// `text` is drawn. +fn styleOf(s: *Session, text: []const u8, into: usize) ![]const u8 { + const x, const y = try s.where(text); + const screen = try s.screen(); + const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]); + return (try s.styles())[y * cols + x + into]; +} + +/// How many ` fn ` and ` if ` keywords the screen draws, each of whose two +/// cells must be drawn in `want`. +fn keywordsIn(s: *Session, want: []const u8) !usize { + const screen = try s.screen(); + const styles = try s.styles(); + const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]); + var seen: usize = 0; + var rows = std.mem.splitScalar(u8, screen, '\n'); + var y: usize = 0; + while (rows.next()) |row| : (y += 1) for ([_][]const u8{ " fn ", " if " }) |word| { + var from: usize = 0; + while (std.mem.indexOfPos(u8, row, from, word)) |i| : (from = i + 1) { + const x = try std.unicode.utf8CountCodepoints(row[0..i]) + 1; + for (0..2) |k| try std.testing.expectEqualStrings(want, styles[y * cols + x + k]); + seen += 1; + } + }; + return seen; +} + +test "settings: Colors off draws a source plain, and on again colours its keywords, the layout as it was, an edit made while off included" { + if (!syntax) return error.SkipZigTest; + for ([_]bool{ false, true }) |edit_while_off| { + var s = try Session.initFile(std.testing.allocator, "pub fn first() void { if (true) {} }\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{n}), "/colors.zig\n"); + var ident: []const u8 = "first"; + if (edit_while_off) { + try std.testing.expect(!std.mem.eql(u8, try styleOf(&s, " fn ", 1), try styleOf(&s, "first", 0))); + try s.write("/ctl", "Colors off\n"); + try s.replace(s.at("/pane/{d}/body", .{n}), "pub fn edited() void { if (false) {} }\n"); + ident = "edited"; + } else try s.write("/ctl", "Colors off\n"); + try std.testing.expectEqual(@as(usize, 2), try keywordsIn(&s, try styleOf(&s, ident, 0))); + const layout = try s.read("/layout"); + try s.write("/ctl", "Colors on\n"); + try s.expect("/layout", layout); + const kw = try styleOf(&s, " fn ", 1); + try std.testing.expect(!std.mem.eql(u8, kw, try styleOf(&s, ident, 0))); + try std.testing.expect(!has(kw, "\"bold\":true")); + try std.testing.expectEqual(@as(usize, 2), try keywordsIn(&s, kw)); + } +} + +test "settings: SyntaxBold keywords stay coloured and bold in every row a pane grows into" { + if (!syntax) return error.SkipZigTest; + var s = try Session.initFile(std.testing.allocator, std.mem.asBytes(&@as([100][33]u8, @splat("pub fn f() void { if (true) {} }\n".*)))); + defer s.deinit(); + try s.write(s.at("/pane/{d}/name", .{try s.focused()}), "/resize.zig\n"); + try s.write("/ctl", "SyntaxBold on\n"); + s.detach(); + try s.write("/ctl", "size 80 8\n"); + const kw = try styleOf(&s, " fn ", 1); + try std.testing.expect(has(kw, "\"bold\":true")); + for ([_]usize{ 50, 12, 70 }) |rows| { + try s.write("/ctl", s.at("size 80 {d}\n", .{rows})); + try std.testing.expect(try keywordsIn(&s, kw) >= 2 * (rows - 4)); + } +} diff --git a/test/panes.zig b/test/panes.zig index db816d41..c996ca00 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -1397,11 +1397,13 @@ test "a prompt that cannot be opened leaves the open one as it was" { } } -test "Save's prompt starts from the pane's directory" { +// A file's directory is test/behaviour/keys.zig's; an owned directory (a +// shell's) ending in a slash, or none, only a real shell gives. +test "Save's prompt starts from an owned directory ending in a slash, or none" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); const pane = try p.setTestFile("one\n"); - for ([_][]const u8{ "/dír space", "/dír space/", "" }) |dir| { + for ([_][]const u8{ "/dír space/", "" }) |dir| { try pane.setOwnedCwd(dir); const slash = if (dir.len == 0 or dir[dir.len - 1] != '/') "/" else ""; p.startPrompt(pane, &pane.body, .save); @@ -3772,101 +3774,6 @@ test "cancelled normal gestures leave no count or subprefix through raw tty mode } } -test "Colors restores syntax after rendering disabled files and disabled edits" { - if (!syntax.enabled) return error.SkipZigTest; - const gpa = std.testing.allocator; - for ([_]bool{ false, true }) |edit_while_disabled| { - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("pub fn first() void { if (true) {} }\n"); - const file = &pane.file.?; - gpa.free(file.path); - file.path = try gpa.dupe(u8, "/colors.zig"); - p.settings.colors = edit_while_disabled; - p.presentation.enabled = false; - var frame = std.heap.ArenaAllocator.init(gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - if (edit_while_disabled) { - try std.testing.expect(file.highlights.len > 0); - p.update(.{ .command = "Colors" }); - panes.File.setContent(p, file, try gpa.dupe(u8, "pub fn edited() void { if (false) {} }\n")); - _ = frame.reset(.retain_capacity); - _ = try p.render(frame.allocator()); - } - try std.testing.expect(!p.settings.colors); - try std.testing.expectEqual(@as(usize, 0), file.highlights.len); - try std.testing.expect(!file.syntax_dirty); - const rect = p.rects[0]; - const cols = pane.cols; - const rows = pane.rows; - p.update(.{ .command = "Colors" }); - try std.testing.expectEqual(rect, p.rects[0]); - try std.testing.expectEqual(cols, pane.cols); - try std.testing.expectEqual(rows, pane.rows); - _ = frame.reset(.retain_capacity); - const surface = try p.render(frame.allocator()); - try std.testing.expect(p.settings.colors); - try std.testing.expect(file.highlights.len > 0); - try std.testing.expect(!file.syntax_dirty); - var seen: usize = 0; - for (surface.cells[0 .. surface.cells.len - 1], 0..) |*first, i| { - const second = &surface.cells[i + 1]; - if (!(std.mem.eql(u8, first.grapheme(), "f") and std.mem.eql(u8, second.grapheme(), "n")) and - !(std.mem.eql(u8, first.grapheme(), "i") and std.mem.eql(u8, second.grapheme(), "f"))) continue; - seen += 1; - for ([_]*pardes.Cell{ first, second }) |cell| { - try std.testing.expectEqual(pardes.Color{ .rgb = p.theme().kw }, cell.style.fg); - try std.testing.expect(!cell.style.bold); - } - } - try std.testing.expectEqual(@as(usize, 2), seen); - } -} - -test "growing a file pane colors every newly visible Zig keyword" { - if (!syntax.enabled) return error.SkipZigTest; - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 8 }); - defer p.deinit(); - const pane = try p.setTestFile(std.mem.asBytes(&@as([100][33]u8, @splat("pub fn f() void { if (true) {} }\n".*)))); - const file = &pane.file.?; - gpa.free(file.path); - file.path = try gpa.dupe(u8, "/resize.zig"); - p.update(.{ .command = "SyntaxBold" }); - try std.testing.expect(p.settings.syntax_bold); - p.settings.colors = true; - p.presentation.enabled = false; - var frame = std.heap.ArenaAllocator.init(gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - const initial_end = file.highlight_start + file.highlights.len; - for ([_]u16{ 50, 12, 70 }) |rows| { - p.update(.{ .resize = .{ .cols = 80, .rows = rows } }); - _ = frame.reset(.retain_capacity); - const surface = try p.render(frame.allocator()); - const rect = p.rects[0]; - const body_y = bodyY(p, rect); - var seen: usize = 0; - for (body_y..body_y + bodyH(p, rect)) |y| { - for (rect.x + config.GUTTER..rect.x + rect.w - 1) |x| { - const first = surface.at(@intCast(x), @intCast(y)); - const second = surface.at(@intCast(x + 1), @intCast(y)); - if (!(std.mem.eql(u8, first.grapheme(), "f") and std.mem.eql(u8, second.grapheme(), "n")) and - !(std.mem.eql(u8, first.grapheme(), "i") and std.mem.eql(u8, second.grapheme(), "f"))) continue; - seen += 1; - for ([_]*pardes.Cell{ first, second }) |cell| { - try std.testing.expectEqual(pardes.Color{ .rgb = p.theme().kw }, cell.style.fg); - try std.testing.expect(cell.style.bold); - } - } - } - try std.testing.expect(seen >= 2 * (rows - 4)); - try std.testing.expect(file.highlight_start + file.highlights.len > initial_end); - try std.testing.expect(!file.syntax_dirty); - } -} - test "Zig keywords keep every rendered byte colored across line-number widths" { if (!syntax.enabled) return error.SkipZigTest; const gpa = std.testing.allocator; diff --git a/test/session.zig b/test/session.zig index 16a131fd..6f36785c 100644 --- a/test/session.zig +++ b/test/session.zig @@ -314,6 +314,23 @@ pub const Session = struct { var it = std.mem.splitScalar(u8, tag[lo + 2 .. close], '/'); return .{ try std.fmt.parseInt(u32, it.next().?, 10), try std.fmt.parseInt(u32, it.next() orelse return error.NoPage, 10) }; } + + // pane2: + + /// Each cell's style as /screen draws it, as JSON text (compare two + /// cells by it), row-major as `screen` draws the cells: row `y`'s cell + /// `x` is `[y * cols + x]`. + pub fn styles(s: *Session) ![]const []const u8 { + const a = s.arena.allocator(); + const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try s.read("/screen"), .{}); + const kinds = json.object.get("styles").?.array.items; + const named = try a.alloc([]const u8, kinds.len); + for (kinds, named) |style, *text| text.* = try std.json.Stringify.valueAlloc(a, style, .{}); + const cells = json.object.get("cells").?.array.items; + const out = try a.alloc([]const u8, cells.len); + for (cells, out) |cell, *style| style.* = named[@intCast(cell.array.items[1].integer)]; + return out; + } }; fn refusal(path: []const u8, r: Reply) error{Refused} { -- cgit v1.3 From a0ec419725ebb4094d811f5206eb0d95bbc5add0 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 10:45:49 -0300 Subject: test: src/pardes.zig's three window-mode tests through the 9P tree alone, in test/behaviour/modes.zig (guide.typ's Modes): Ctrl-w outlining the pane with the keyboard (its tag in the outline's colour, each box its letter), h j k l moving only the outline, Enter, Esc (after SPC w), : (its tag the keyboard's) and a letter giving a pane the keyboard, other keys changing nothing; d deleting the outlined pane (between two it waits for k or j, Esc taking it back; the rows and the outline going to the pane that took them, one neighbour closing at once), c collapsing and restoring it (the ctl read's collapsed), the mode staying on until Enter; window keys walking from the top pane up to its column's tag (normal mode, w moving its caret), across columns and back to the caret it had, up to the workspace tag, down again, and : back to the body; each checked to fail against a break (h j k l moving the keyboard too, a letter choosing nothing, the outline's tint not painted, the outline dropped after d, c not collapsing, k past a column's top pane doing nothing) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/pardes.zig | 151 --------------------------------------------- test/behaviour/modes.zig | 157 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+), 151 deletions(-) (limited to 'src') diff --git a/src/pardes.zig b/src/pardes.zig index 70bcf7c9..ba09b3b6 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -3133,45 +3133,6 @@ test "s in a header selects in the header's own text" { try std.testing.expectEqual(@as(u16, 0), pane.body.nsel); } -test "window keys walk from a pane up through the column and workspace tags and back" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 20, .tty_only = true }); - defer p.deinit(); - _ = try p.setTestFile("body\n"); - p.newColumn(0); - p.newScratchIn(0, p.activeColumn().?); - p.sync(); - p.active = p.col_panes[0][0]; - const window = struct { - fn key(core: *Pardes, letter: u21) void { - core.update(.{ .key = .{ .cp = 'w', .ctrl = true } }); - core.update(.{ .key = .{ .cp = letter } }); - } - }; - // Up from the top pane: its column's tag, in normal mode; `:` there is - // the way back to the body. - window.key(p, 'k'); - try std.testing.expect(p.header_focus and p.header_column.? == 0); - try std.testing.expectEqual(Text.Mode.normal, p.col_tags[0].mode); - p.update(.{ .key = .{ .cp = 'w' } }); - try std.testing.expect(p.col_tags[0].cur_col > 0); - const remembered = p.col_tags[0].cur_col; - window.key(p, 'l'); - try std.testing.expect(p.header_focus and p.header_column.? == 1); - window.key(p, 'h'); - try std.testing.expectEqual(remembered, p.col_tags[0].cur_col); - window.key(p, 'k'); - try std.testing.expect(p.header_focus and p.header_column == null); - window.key(p, 'j'); - try std.testing.expect(p.header_focus and p.header_column.? == 0); - window.key(p, 'j'); - try std.testing.expect(!p.header_focus); - try std.testing.expectEqual(p.col_panes[0][0], p.active); - window.key(p, 'k'); - p.update(.{ .key = .{ .cp = ':' } }); - try std.testing.expect(!p.header_focus and p.panes[p.active].?.focus == .body); - try std.testing.expectEqualStrings("body\n", p.panes[0].?.file.?.content); -} - pub const Mouse = struct { /// The host has inverted a compact body layer. Physical col/row still /// route pane chrome; these coordinates address the pane's logical text. @@ -8409,63 +8370,6 @@ test "with ProgramEsc raw tty Escape only leaves at a shell prompt" { try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); } -test "window mode moves an outline and focuses it on Enter, Esc or a label" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 30 }); - defer p.deinit(); - _ = try p.setTestFile("left\n"); - _ = try p.newShell(1, ""); - try std.testing.expect(layout.splitColumn(p, 0, 1, false)); - p.newScratchBelow(1); - p.sync(); - const lower = for (p.panes, 0..) |slot, id| { - if (slot != null and id > 1) break id; - } else return error.TestExpectedScratch; - p.active = 0; - // Ctrl-w enters it; h/j/k/l move only the outline. - p.update(.{ .key = .{ .cp = 'w', .ctrl = true } }); - try std.testing.expectEqual(@as(?usize, 0), p.window_pick); - p.update(.{ .key = .{ .cp = 'l', .text = "l" } }); - try std.testing.expect(p.window_pick != null and p.window_pick.? != 0); - p.update(.{ .key = .{ .cp = 'j', .text = "j" } }); - try std.testing.expectEqual(@as(?usize, lower), p.window_pick); - try std.testing.expectEqual(@as(usize, 0), p.active); - p.update(.{ .key = .{ .cp = Key.enter } }); - try std.testing.expectEqual(lower, p.active); - try std.testing.expect(p.window_pick == null); - // SPC w enters it too, and Esc also leaves it on the outlined pane. - p.update(.{ .key = .{ .cp = ' ', .text = " " } }); - p.update(.{ .key = .{ .cp = 'w', .text = "w" } }); - p.update(.{ .key = .{ .cp = 'h', .text = "h" } }); - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expectEqual(@as(usize, 0), p.active); - try std.testing.expect(p.window_pick == null); - // Other keys change nothing. - p.update(.{ .key = .{ .cp = 'w', .ctrl = true } }); - p.update(.{ .key = .{ .cp = '!', .text = "!" } }); - try std.testing.expectEqual(@as(?usize, 0), p.window_pick); - p.update(.{ .key = .{ .cp = Key.escape } }); - // `:` focuses the outlined pane's tag. - p.update(.{ .key = .{ .cp = 'w', .ctrl = true } }); - p.update(.{ .key = .{ .cp = 'l', .text = "l" } }); - p.update(.{ .key = .{ .cp = ':', .text = ":" } }); - try std.testing.expect(p.active != 0 and p.panes[p.active].?.focus == .tag); - p.update(.{ .key = .{ .cp = Key.escape } }); - // A label goes straight there: the left column's pane is "a". - try std.testing.expectEqual(@as(?u8, 'a'), layout.windowLabel(p, 0)); - try std.testing.expect(p.executeBuiltinLine(p.active, "Window")); - p.update(.{ .key = .{ .cp = 'a', .text = "a" } }); - try std.testing.expectEqual(@as(usize, 0), p.active); - // Painted: the outlined tag's band in the selection colour, each pane's - // letter in its tag's box. - try std.testing.expect(p.executeBuiltinLine(p.active, "Window")); - var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); - defer arena.deinit(); - const shown = try p.render(arena.allocator()); - try std.testing.expectEqual(p.theme().sel_bg, shown.tag_layers[0].bg); - const box = shown.at(p.rects[0].x, p.tagTop(p.panes[0].?, p.rects[0])); - try std.testing.expectEqualStrings("a", box.text[0..box.len]); -} - test "the Del question shows only once its delay is up: a quick SPC d j never sees it" { if (platform == .web) return; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 40 }); @@ -8516,61 +8420,6 @@ test "the Del question shows only once its delay is up: a quick SPC d j never se try std.testing.expect(p.panes[mid] == null); } -test "window mode d deletes the outlined pane and asks k or j, c collapses it, and the mode stays" { - if (platform == .web) return; - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 40 }); - defer p.deinit(); - p.settings.placement = .pardes; // the neighbours below are what is under test - const top = p.active; - p.newScratchBelow(top); - const mid = p.active; - p.newScratchBelow(mid); - const low = p.active; - p.sync(); - const h = [_]u16{ p.rects[top].h, p.rects[mid].h, p.rects[low].h }; - // Neither key is a pane's label. - try std.testing.expect(std.mem.indexOfAny(u8, layout.window_labels, "hjkldc") == null); - - // Ctrl-w from the bottom pane, the outline up to the middle one. - p.update(.{ .key = .{ .cp = 'w', .ctrl = true } }); - p.update(.{ .key = .{ .cp = 'k', .text = "k" } }); - try std.testing.expectEqual(@as(?usize, mid), p.window_pick); - // c folds the outlined pane to its tag and back, the mode staying. - p.update(.{ .key = .{ .cp = 'c', .text = "c" } }); - try std.testing.expect(p.panes[mid].?.collapsed); - try std.testing.expectEqual(@as(?usize, mid), p.window_pick); - p.update(.{ .key = .{ .cp = 'c', .text = "c" } }); - try std.testing.expect(!p.panes[mid].?.collapsed); - p.sync(); - try std.testing.expectEqual(h[1], p.rects[mid].h); - - // d with panes above and below asks, as SPC d does; Esc takes it back - // and the outline stays where it was. - p.update(.{ .key = .{ .cp = 'd', .text = "d" } }); - try std.testing.expectEqual(Pane.Prompt.del_side, p.panes[mid].?.prompt); - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expect(p.panes[mid] != null and p.panes[mid].?.prompt == .none); - try std.testing.expectEqual(@as(?usize, mid), p.window_pick); - // j: the pane below takes the rows, and the outline goes with them. - p.update(.{ .key = .{ .cp = 'd', .text = "d" } }); - p.update(.{ .key = .{ .cp = 'j', .text = "j" } }); - p.sync(); - try std.testing.expect(p.panes[mid] == null); - try std.testing.expectEqual(h[0], p.rects[top].h); - try std.testing.expectEqual(h[1] + h[2], p.rects[low].h); - try std.testing.expectEqual(@as(?usize, low), p.window_pick); - // With one neighbour there is nothing to ask: d closes it at once, - // the outline on the pane that grew. - p.update(.{ .key = .{ .cp = 'd', .text = "d" } }); - p.sync(); - try std.testing.expect(p.panes[low] == null); - try std.testing.expectEqual(@as(?usize, top), p.window_pick); - // Enter leaves the mode on it. - p.update(.{ .key = .{ .cp = Key.enter } }); - try std.testing.expect(p.window_pick == null); - try std.testing.expectEqual(top, p.active); -} - test "raw tty Ctrl-B switches to editor mode without sending child input" { if (platform == .web) return; const p = try Pardes.init(std.testing.allocator, .{ .file = "mise.toml" }); diff --git a/test/behaviour/modes.zig b/test/behaviour/modes.zig index dde3ffb6..f29a4b56 100644 --- a/test/behaviour/modes.zig +++ b/test/behaviour/modes.zig @@ -102,3 +102,160 @@ test "modes: Esc back into a file leaves its view where it was, and a look centr const drawn_at = line - try topLine(&s, "/test.txt"); try std.testing.expect(drawn_at + 1 >= rows / 2 and drawn_at <= rows / 2 + 1); } + +/// Three panes named /a.txt (left column), /b.txt over /c.txt (right), +/// the keyboard on /a.txt; their serials. +fn threePanes(s: *Session) ![3]u32 { + const a = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{a}), "/a.txt\n"); + try s.write("/ctl", "Newcol\n"); + try s.write(s.at("/col/{d}/exec", .{try s.column(1)}), "New\n"); + const b = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{b}), "New\n"); + const c = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{b}), "/b.txt\n"); + try s.write(s.at("/pane/{d}/name", .{c}), "/c.txt\n"); + try s.write("/focus", s.at("{d}\n", .{a})); + return .{ a, b, c }; +} + +/// The panes whose tag (`name Save`) is drawn in `lit`, the outline's colour. +fn outlined(s: *Session, lit: []const u8, names: []const []const u8) ![]const u8 { + var found: []const u8 = ""; + for (names) |name| { + const x, const y = try s.where(s.at("{s} Save", .{name})); + if (std.mem.eql(u8, lit, try s.bg(x, y))) found = s.at("{s}{s}", .{ found, name }); + } + return found; +} + +test "modes: Ctrl-w outlines a pane, h j k l move the outline, and Enter, Esc, : or a pane's letter gives it the keyboard" { + var s = try Session.initFile(std.testing.allocator, "left\n"); + defer s.deinit(); + const a, const b, const c = try threePanes(&s); + const names = &[_][]const u8{ "/a.txt", "/b.txt", "/c.txt" }; + try s.keys("\x17"); + const x, const y = try s.where("/a.txt Save"); + const lit = try s.bg(x, y); + try std.testing.expectEqualStrings("/a.txt", try outlined(&s, lit, names)); + // Each pane's box shows its letter: the left column's is a. + try std.testing.expectEqualStrings("a /a.txt", (try s.screen())[y * 81 ..][0.."a /a.txt".len]); + try s.keys("l"); + try std.testing.expectEqualStrings("/b.txt", try outlined(&s, lit, names)); + try s.keys("j"); + try std.testing.expectEqualStrings("/c.txt", try outlined(&s, lit, names)); + try expectEqual(a, try s.focused()); + try s.keys("\n"); + try expectEqual(c, try s.focused()); + try std.testing.expectEqualStrings("", try outlined(&s, lit, names)); + // SPC w enters it too, and Esc leaves it on the outlined pane. + try s.keys(" wh\x1b"); + try expectEqual(a, try s.focused()); + // Other keys change nothing. + try s.keys("\x17!"); + try std.testing.expectEqualStrings("/a.txt", try outlined(&s, lit, names)); + try s.keys("\x1b"); + // `:` gives the outlined pane's tag the keyboard. + try s.keys("\x17l:"); + try expectEqual(b, try s.focused()); + try expectEqual((try s.where("/b.txt Save"))[1], (try s.cursor()).?[1]); + try s.keys("\x1b"); + // A letter goes straight there. + try s.keys("\x17a"); + try expectEqual(a, try s.focused()); + try std.testing.expectEqualStrings("", try outlined(&s, lit, names)); +} + +fn listed(s: *Session, n: u32) !bool { + var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + while (rows.next()) |row| if (std.mem.startsWith(u8, row, s.at("{d} ", .{n}))) return true; + return false; +} + +test "modes: in window mode d deletes the outlined pane, asking k or j between two, c collapses it, and the mode stays on" { + var s = try Session.initFile(std.testing.allocator, "top\n"); + defer s.deinit(); + // The neighbours below are what is under test: each New right under its asker. + try s.write("/ctl", "Placement pardes\n"); + const top = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{top}), "New\n"); + const mid = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{mid}), "New\n"); + const low = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{mid}), "/mid\n"); + try s.write(s.at("/pane/{d}/name", .{low}), "/low\n"); + const names = &[_][]const u8{ "/test.txt", "/mid", "/low" }; + const row_top = (try s.where("/test.txt Save"))[1]; + const row_mid = (try s.where("/mid Save"))[1]; + const row_low = (try s.where("/low Save"))[1]; + // From the bottom pane, the outline up to the middle one. + const mx, const my = try s.where("/mid Save"); + const plain = try s.bg(mx, my); + try s.keys("\x17k"); + const lit = try s.bg(mx, my); + try std.testing.expect(!std.mem.eql(u8, plain, lit)); + try std.testing.expectEqualStrings("/mid", try outlined(&s, lit, names)); + // c folds it to its tag and back, the mode staying. + try s.keys("c"); + try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/ctl", .{mid})), " collapsed ") != null); + try std.testing.expectEqualStrings("/mid", try outlined(&s, lit, names)); + try s.keys("c"); + try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/ctl", .{mid})), " collapsed ") == null); + try expectEqual(row_low, (try s.where("/low Save"))[1]); + // d between two panes asks, as SPC d does; Esc takes it back, the + // outline where it was. + try s.keys("d\x1b"); + try std.testing.expect(try listed(&s, mid)); + try std.testing.expectEqualStrings("/mid", try outlined(&s, lit, names)); + // j: the pane below takes the rows, and the outline goes with them. + try s.keys("dj"); + try std.testing.expect(!try listed(&s, mid)); + try expectEqual(row_top, (try s.where("/test.txt Save"))[1]); + try expectEqual(row_mid, (try s.where("/low Save"))[1]); + try std.testing.expectEqualStrings("/low", try outlined(&s, lit, &.{ "/test.txt", "/low" })); + // With one neighbour nothing is asked: d closes it, the outline on the + // pane that grew. + try s.keys("d"); + try std.testing.expect(!try listed(&s, low)); + try std.testing.expectEqualStrings("/test.txt", try outlined(&s, lit, &.{"/test.txt"})); + // Enter leaves the mode on it. + try s.keys("\n"); + try expectEqual(top, try s.focused()); + try std.testing.expectEqualStrings("", try outlined(&s, lit, &.{"/test.txt"})); +} + +test "modes: window keys walk from a pane up through its column's tag and the workspace's, across, and back down" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const a = try s.focused(); + try s.write("/ctl", "Newcol\n"); + try s.write(s.at("/col/{d}/exec", .{try s.column(1)}), "New\n"); + try s.write("/focus", s.at("{d}\n", .{a})); + const right = 40; // two columns on 80 cells: the right one's first + // Up from the top pane: its column's tag, in normal mode (w moves). + try s.keys("\x17k"); + try s.expect("/focus", ""); + const x0, const y0 = (try s.cursor()).?; + try expectEqual(@as(usize, 1), y0); + try std.testing.expect(x0 < right); + try s.keys("w"); + const remembered = (try s.cursor()).?; + try std.testing.expect(remembered[0] > x0); + // Across to the right column's tag, and back to where the caret was. + try s.keys("\x17l"); + try std.testing.expect((try s.cursor()).?[0] >= right); + try s.keys("\x17h"); + try expectEqual(remembered, (try s.cursor()).?); + // Up again: the workspace tag; down, the column's, then the pane. + try s.keys("\x17k"); + try expectEqual(@as(usize, 0), (try s.cursor()).?[1]); + try s.keys("\x17j"); + try expectEqual(@as(usize, 1), (try s.cursor()).?[1]); + try s.keys("\x17j"); + try expectEqual(a, try s.focused()); + // `:` in a column's tag is the way back to the body. + try s.keys("\x17k:"); + try expectEqual(a, try s.focused()); + try expectEqual((try s.where("body"))[1], (try s.cursor()).?[1]); + try s.expect(s.at("/pane/{d}/body", .{a}), "body\n"); +} -- cgit v1.3 From 350e7cc22b85130cbe95a114c668945b1dc12fd7 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 10:52:24 -0300 Subject: test: twenty-two more of src/ninep/ctl.zig's tests through the 9P tree alone: in test/behaviour/look_and_exec.zig a look opening a spaced path at its line and column leaving the source as it was, a batch validated whole before any virtual file opens, /status's lines, mode and refusal with look and exec validating batches and reading back their panes, a look at a file it may not read, a trailing colon dropped and a failed address opening nothing, a file:/re/ miss logged as its file's pane, a look into another pane leaving the source's dot, a word looked for in its own pane (wrapping, then LookWord list's +Search), a second LookWord list look refilling its +Search, @pN: and a line past the end said one way, an exec over the command limit, a builtin failing through exec, tagexec and a column's exec with one err and no msg, and Look . from a gone directory; in rules.zig every EINVAL's words, the log's too; in limits.zig a ctl line over 1 MiB refused once; in panes.zig acme's ctl words and +Unsaved dropping a closed pane's row; in the new from_acme.zig Put, Delete and acme's words refused; in settings.zig GUI-only settings and DumpDir read back; in root_ctl.zig Joincol and Theme's refusals and a theme's name in any case; in placement.zig a look from a pane made after Delcol placed in a column; in edit.zig Edit ,p logged with no room for +Errors; the status test kept for its window's pointer line, and Joincol's for themesNear's unit checks, each trimmed; each checked to fail against a break (status's pane count, a look's open failure words, the trailing colon kept, e_no_match, the empty name's words, the 1 MiB cap doubled, acme's Put renamed, the GUI-only words, themeIndex by case, command_max 1023, a repeat counted by two, Look .'s no such directory words, dropUnsaved doing nothing, lookWord not wrapping, limit=addr not taking, Edit's text not logged when unplaced, @pN: with no address a miss, a look into another pane clearing the source's dot, a column taken as the cursor's, the batch's control-character check and command's checking pass both skipped, LookWord's +Search never shared, atFoot placing nothing) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/ninep/ctl.zig | 558 +-------------------------------------- test/behaviour.zig | 1 + test/behaviour/edit.zig | 12 + test/behaviour/from_acme.zig | 28 ++ test/behaviour/limits.zig | 22 ++ test/behaviour/look_and_exec.zig | 261 ++++++++++++++++++ test/behaviour/panes.zig | 45 ++++ test/behaviour/placement.zig | 20 ++ test/behaviour/root_ctl.zig | 18 ++ test/behaviour/rules.zig | 22 ++ test/behaviour/settings.zig | 20 ++ 11 files changed, 458 insertions(+), 549 deletions(-) create mode 100644 test/behaviour/from_acme.zig (limited to 'src') diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index fc09c730..f23f2579 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -1466,118 +1466,16 @@ test "commands lists every builtin with its argument and the ctl that takes it" } -test "status reads the editor, and look and exec validate whole batches" { - const gpa = testing.allocator; - const p = try withFile(gpa, "Msg root\n"); +test "status in a window says whether the pointer is over it" { + // The rest of status is test/behaviour/look_and_exec.zig's: a session + // there is a terminal's view. + const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); - // A terminal's view first (a window's build starts windowed). - p.windowed = false; - const status = rd(p, root_status, 0, 4096); - try testing.expectEqual(Status.ok, status.reply.status); - var lines = std.mem.tokenizeScalar(u8, status.bytes, '\n'); - try testing.expect(std.mem.startsWith(u8, lines.next().?, "pid ")); - var expected: [64]u8 = undefined; - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "version {s}", .{pardes.version}), lines.next().?); - var live: usize = 0; - for (p.panes) |slot| live += @intFromBool(slot != null); - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "panes {d}", .{live}), lines.next().?); - try testing.expect(lines.next() == null); - // A window says whether the pointer is over it. p.windowed = true; - var again = std.mem.tokenizeScalar(u8, rd(p, root_status, 0, 4096).bytes, '\n'); - for (0..3) |_| _ = again.next(); - try testing.expectEqualStrings("pointer out", again.next().?); + var lines = std.mem.tokenizeScalar(u8, rd(p, root_status, 0, 4096).bytes, '\n'); + for (0..3) |_| _ = lines.next(); + try testing.expectEqualStrings("pointer out", lines.next().?); p.windowed = false; - const stat = call(p, .{ .tag = 1, .op = .getattr, .node = root_status }); - try testing.expectEqual(@as(u64, status.bytes.len), stat.reply.attr.size); - try testing.expectEqual(@as(u16, 0o444), stat.reply.attr.mode); - try testing.expectEqual(E.PERM, wr(p, root_status, "anything\n").errno()); - try testing.expectEqualStrings("", rd(p, root_exec, 0, 4096).bytes); - - const serial = p.next_serial; - for ([_][]const u8{ "Msg a\x00b", "Msg x\nMsg \x01y\n" }) |invalid| { - try testing.expectEqual(E.INVAL, wr(p, root_exec, invalid).errno()); - try testing.expectEqual(serial, p.next_serial); - } - // An empty write is a no-op, not a command. - try testing.expectEqual(Status.ok, wr(p, root_exec, "\n \n").reply.status); - try testing.expectEqualStrings("", rd(p, root_exec, 0, 4096).bytes); - try testing.expectEqualStrings(status.bytes, rd(p, root_status, 0, 4096).bytes); - - const pane = p.panes[0].?; - try testing.expectEqual(Status.ok, wr(p, root_exec, "Msg hello there\n").reply.status); - try testing.expectEqualStrings("hello there", pane.msg[0..pane.msg_len]); - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "{d}\n", .{pane.serial}), rd(p, root_exec, 0, 4096).bytes); - - try testing.expectEqual(Status.ok, wr(p, root_look, "/virtual/index\n").reply.status); - const opened = p.panes[p.active].?; - try testing.expectEqualStrings("/virtual/index", opened.file.?.path); - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "{d}\n", .{opened.serial}), rd(p, root_look, 0, 4096).bytes); - - try testing.expectEqual(Status.ok, wr(p, root_exec, "New\n").reply.status); - const made = p.panes[p.active].?; - try testing.expect(made.serial != opened.serial); - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "{d}\n", .{made.serial}), rd(p, root_exec, 0, 4096).bytes); -} - -test "look opens spaced paths and locations without editing the source pane" { - if (!fs.platform_has_fs) return error.SkipZigTest; - const gpa = testing.allocator; - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "two words.zig", .data = "first\nsecond\nthird\n" }); - var path_buf: [4096]u8 = undefined; - const path = path_buf[0..try tmp.dir.realPathFile(testing.io, "two words.zig", &path_buf)]; - const p = try withFile(gpa, "source stays intact\n"); - defer p.deinit(); - const source = p.panes[0].?; - const look_node = Node.of(source.serial, .look); - const revision = source.file.?.revision; - const undo_len = source.file.?.history.undo_len; - source.body.cur_col = 3; - var clicked: [4200]u8 = undefined; - try testing.expectEqual(Status.ok, wr(p, look_node, try std.fmt.bufPrint(&clicked, "{s}:2:3\n", .{path})).reply.status); - const opened = p.panes[p.active].?; - try testing.expect(opened != source); - try testing.expectEqualStrings(path, opened.file.?.path); - try testing.expectEqualStrings("first\nsecond\nthird\n", opened.file.?.content); - try testing.expectEqual(@as(i32, 1), opened.body.cur_row); - try testing.expectEqual(@as(i32, 2), opened.body.cur_col); - try testing.expectEqualStrings("source stays intact\n", source.file.?.content); - try testing.expectEqual(revision, source.file.?.revision); - try testing.expectEqual(undo_len, source.file.?.history.undo_len); - try testing.expectEqual(@as(i32, 3), source.body.cur_col); - var expected: [16]u8 = undefined; - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "{d}\n", .{opened.serial}), rd(p, root_look, 0, 4096).bytes); - - p.active = 0; - try testing.expectEqual(Status.ok, wr(p, root_look, try std.fmt.bufPrint(&clicked, "{s}:3:2\n", .{path})).reply.status); - try testing.expect(p.panes[p.active].? == opened); - try testing.expectEqual(@as(i32, 2), opened.body.cur_row); - try testing.expectEqual(@as(i32, 1), opened.body.cur_col); - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "{d}\n", .{opened.serial}), rd(p, root_look, 0, 4096).bytes); -} - -test "look validates the whole batch before opening virtual files" { - const gpa = testing.allocator; - const p = try withFile(gpa, "source\n"); - defer p.deinit(); - const source = p.panes[0].?; - const look_node = Node.of(source.serial, .look); - const serial = p.next_serial; - for ([_][]const u8{ - "/virtual/index\x00ignored", "/virtual/index\n/virtual/\x01index\n", - }) |invalid| { - try testing.expectEqual(E.INVAL, wr(p, look_node, invalid).errno()); - try testing.expectEqual(serial, p.next_serial); - try testing.expectEqual(@as(usize, 0), p.active); - try testing.expectEqualStrings("source\n", source.file.?.content); - } - try testing.expectEqual(Status.ok, wr(p, look_node, "/n/self/index\n").reply.status); - const opened = p.panes[p.active].?; - try testing.expectEqualStrings("/virtual/index", opened.file.?.path); - try testing.expect(std.mem.indexOf(u8, opened.file.?.content, "/test.txt") != null); - try testing.expectEqualStrings("source\n", source.file.?.content); } @@ -1795,55 +1693,6 @@ test "a refusal's +Unsaved goes in the session's directory when the active pane' try testing.expectEqualStrings(p.sessionDir(), std.fs.path.dirname(unsaved).?); } -test "a look at a file it may not read fails the write naming the file" { - if (comptime !pardes.hosted) return; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "locked.txt", .data = "secret\n", .flags = .{ .permissions = .fromMode(0o000) } }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - const path = try std.fmt.bufPrint(&line, "{s}/locked.txt", .{dir}); - var want: [4200]u8 = undefined; - const expected = try std.fmt.bufPrint(&want, "look: {s}: permission denied", .{path}); - var with_newline: [4300]u8 = undefined; - const refused = wr(p, root_look, try std.fmt.bufPrint(&with_newline, "{s}\n", .{path})); - if (refused.reply.status == .ok) return error.SkipZigTest; // root reads anything - try testing.expectEqualStrings(expected, refused.reply.ename); -} - -test "a look line's trailing colon is dropped, and a look whose address fails never leaves its file open" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - const count = struct { - fn of(pp: *Pardes) usize { - var n: usize = 0; - for (pp.panes) |slot| n += @intFromBool(slot != null); - return n; - } - }.of; - const before = count(p); - // Failing addresses: nothing opened, the write still answered. - inline for ([_][]const u8{ "{s}/a.txt:/nomatch/\n", "{s}/a.txt:9:\n" }) |fmt| { - _ = wr(p, root_look, try std.fmt.bufPrint(&line, fmt, .{dir})); - try testing.expectEqual(before, count(p)); - } - // `a.txt:2:3:` is `a.txt:2:3`: it opens there. - try testing.expectEqual(Status.ok, wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/a.txt:2:3:\n", .{dir})).reply.status); - try testing.expectEqual(before + 1, count(p)); - const opened = p.panes[p.active].?; - try testing.expect(std.mem.endsWith(u8, opened.file.?.path, "/a.txt")); - try testing.expectEqual(@as(i32, 1), opened.body.cur_row); -} - test "a builtin's Verbose announcement is logged only when it succeeds, its failure said now or by the host later" { const gpa = testing.allocator; @@ -2019,32 +1868,10 @@ test "after a ThemeFile the root ctl and DumpConfig say ThemeFile and its path, try testing.expectEqual(Status.ok, wr(p, root_ctl, "ThemeFile " ++ path ++ "\n").reply.status); } -test "Joincol with no column to the right and Theme with no such theme say so" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - const joined = wr(p, root_ctl, "Joincol\n"); - try testing.expectEqual(tree.Status.err, joined.reply.status); - try testing.expectStringStartsWith(joined.reply.ename, "Joincol: no column to the right"); - const themed = wr(p, root_ctl, "Theme drak\n"); - try testing.expectEqual(E.INVAL, themed.errno()); - try testing.expectStringStartsWith(themed.reply.ename, "bad value in control message; like it: "); - try testing.expect(std.mem.indexOf(u8, themed.reply.ename, ": dr") != null); - try testing.expect(std.mem.endsWith(u8, themed.reply.ename, "\"drak\"")); - // A click on the word says it on the message row. - _ = wr(p, Node.of(serialOf(p), .exec), "Theme drak\n"); - const pane = p.panes[p.active].?; - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "no theme \"drak\"") != null); - // Within the 128 bytes an Rerror carries, the quoted line kept. - try testing.expect(themed.reply.ename.len <= 128); - // A name in any case is the theme. - try testing.expectEqual(Status.ok, wr(p, root_ctl, "Theme DRACULA\n").reply.status); - try testing.expectEqualStrings("dracula", pardes.themes[p.settings.theme].name); - // A misspelling far from any first letter's list still finds its like. +test "themesNear finds a misspelling's like far from any first letter, and every theme sharing the first letter when they fit" { + // Joincol and Theme's refusals are test/behaviour/root_ctl.zig's. var near: [170]u8 = undefined; try testing.expect(std.mem.indexOf(u8, pardes.colors.themesNear(&near, "9dracula"), "dracula") != null); - // Every theme sharing the first letter, when they fit. var tested = false; for ("abcdefghijklmnopqrstuvwxyz") |letter| { var count: usize = 0; @@ -2081,26 +1908,6 @@ test "a look whose pane is refused a place fails with that alone, not a missing try testing.expectEqual(@as(usize, 1), th.logCount(p, "\nerr ")); } -test "a look under pardes's placement, from a pane pane/new made after Delcol, opens its file" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "f.txt", .data = "file\n" }); - var dir: [4096]u8 = undefined; - const at = dir[0..try tmp.dir.realPath(testing.io, &dir)]; - var line: [4200]u8 = undefined; - const first = serialOf(p); - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(first, .exec), "Placement\n").reply.status); - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(first, .exec), "Delcol\n").reply.status); - const made = try th.newPane(p); - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(made, .look), try std.fmt.bufPrint(&line, "{s}/f.txt:#4\n", .{at})).reply.status); - // The pane pane/new made is placed, and so is the one its look opened. - for (p.panes, 0..) |slot, i| if (slot != null) try testing.expect(layout.findPane(p, i) != null); - try testing.expect(p.panes[p.active].?.serial != made); -} - test "a Restore of a file that is no dump fails the write before any warning, and a failed Dump fails its write" { if (comptime !pardes.hosted) return error.SkipZigTest; const p = try withFile(testing.allocator, "x\n"); @@ -2133,58 +1940,6 @@ test "a Restore of a file that is no dump fails the write before any warning, an try testing.expectEqualStrings("Dump /dev/null/pardes.dump.zon: /dev/null is not a directory", p.fs.late_failure[0..p.fs.late_failure_len]); } -test "a setting this frontend cannot show says GUI-only, and DumpDir reads back the directory in effect" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - if (!builtins.viewCapabilities(p).lift) { - try testing.expectEqualStrings("Lift is GUI-only, invalid here \"Lift\"", wr(p, root_ctl, "Lift on\n").reply.ename); - try testing.expectEqualStrings("GripWidth is GUI-only, invalid here \"GripWidth\"", wr(p, root_ctl, "GripWidth 120\n").reply.ename); - // A builtin the GUI alone has, written to a ctl or an exec, never - // runs as a shell command. - if (comptime !pardes.font_picker) { - try testing.expectEqualStrings("Fonts is GUI-only, invalid here \"Fonts\"", wr(p, root_ctl, "Fonts\n").reply.ename); - const ran = wr(p, root_exec, "Fonts\n"); - try testing.expectEqual(E.INVAL, ran.errno()); - try testing.expectEqualStrings("Fonts is GUI-only, invalid here", ran.reply.ename); - } - } - try testing.expectEqualStrings("unknown control message \"Liftt\"", wr(p, root_ctl, "Liftt on\n").reply.ename); - var buf: [900]u8 = undefined; - if (pardes.dump.directory(&buf, "")) |dir| { - var want: [920]u8 = undefined; - const line = try std.fmt.bufPrint(&want, "\nDumpDir {s}\n", .{dir}); - try testing.expect(std.mem.indexOf(u8, rd(p, root_ctl, 0, 1 << 16).bytes, line) != null); - } -} - -test "an exec line over the command limit fails the write and says the limit; a builtin's may be longer" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const long = "echo " ++ &@as([pardes.exec.command_max]u8, @splat('y')) ++ "\n"; - const refused = wr(p, root_exec, long); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expectEqualStrings(e_too_long, refused.reply.ename); - var rec: [96]u8 = undefined; - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&rec, "err {d} exec: invalid command line: a command line is at most 1024 bytes", .{serialOf(p)}))); - try testing.expectEqualStrings(e_control, wr(p, root_exec, "echo a\x01b\n").reply.ename); - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&rec, "err {d} exec: invalid command line: it holds", .{serialOf(p)}))); - try testing.expectEqual(Status.ok, wr(p, root_exec, "Msg " ++ &@as([1100]u8, @splat('z')) ++ "\n").reply.status); -} - -test "Edit's p and = never fail for want of room for +Errors: the text is logged instead" { - const p = try withFile(testing.allocator, "one\ntwo\n"); - defer p.deinit(); - p.update(.{ .resize = .{ .cols = 80, .rows = 6 } }); - const ctl_node = Node.of(serialOf(p), .ctl); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "Edit ,p\n").reply.status); - try testing.expect(th.logHas(p, " one\n") and th.logHas(p, " two\n")); - p.sync(); - var count: usize = 0; - for (p.panes) |slot| count += @intFromBool(slot != null); - try testing.expectEqual(@as(usize, 1), count); // the +Errors that had no room is gone -} - test "Mount dials its peer and fails, mounting nothing, when none answers; a look through a peer gone says dial failed" { const p = try withFile(testing.allocator, "x\n"); @@ -2560,31 +2315,6 @@ test "Grep, Symbols and Diagnostics share their directory's one +Search" { try testing.expectEqual(@as(usize, 1), searches); } -test "+Unsaved drops a pane once it has closed" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const a = try th.newPane(p); - _ = wr(p, Node.of(a, .name), "/tmp/pardes-unsaved-a.txt\n"); - _ = wr(p, Node.of(a, .body), "edited\n"); - const b = try th.newPane(p); - _ = wr(p, Node.of(b, .name), "/tmp/pardes-unsaved-b.txt\n"); - _ = wr(p, Node.of(b, .body), "edited\n"); - const root_ctl = @backingInt(tree.TopFile.ctl); - try testing.expectEqual(Status.err, wr(p, root_ctl, "Exit\n").reply.status); - var listed: ?*pardes.Pane = null; - for (p.panes) |slot| if (slot) |q| if (q.file) |f| if (f.output) |o| if (o.from == .unsaved) { - listed = q; - }; - const list = listed.?; - try testing.expect(std.mem.indexOf(u8, list.file.?.content, "/tmp/pardes-unsaved-a.txt: Modified") != null); - // a closes (discarded): its row goes; b's stays. - _ = wr(p, Node.of(a, .ctl), "Del\n"); - _ = wr(p, Node.of(a, .ctl), "Del\n"); - p.sync(); - try testing.expect(p.paneBySerial(a) == null); - try testing.expect(std.mem.indexOf(u8, list.file.?.content, "/tmp/pardes-unsaved-a.txt: Modified") == null); -} - test "a buffer whose file was deleted on disk is dirty, and Del asks first" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -2609,23 +2339,6 @@ test "a failure said while a 9P write waits on it posts no msg, only the write's try testing.expect(p.fs.late_failure_len > 0); } -test "a look that misses file:/re/ names the file, and is logged as its pane's" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "f.txt", .data = "alpha\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/f.txt\n", .{dir})); - p.sync(); - const f_serial = p.panes[p.active].?.serial; - _ = wr(p, Node.of(serialOf(p), .look), try std.fmt.bufPrint(&line, "{s}/f.txt:/zzqq/\n", .{dir})); - var want: [4300]u8 = undefined; - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&want, "err {d} look: {s}/f.txt: no match for regexp", .{ f_serial, dir }))); -} - test "with no pane at all, the root look opens its file and the root exec runs in the session's directory" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -2730,61 +2443,6 @@ test "a Restore of a name in the DumpDir asks for, and logs, the whole path it r } -test "acme's words run as pardes's (Put is Save, Delete a Del that does not ask) or are refused, never run as commands" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - for ([_][]const u8{ "Snarf\n", "Indent\n", "Local\n", "Abort\n" }) |line| { - for ([_]u64{ Node.of(serial, .ctl), Node.of(serial, .exec) }) |node| { - const r = wr(p, node, line); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expect(std.mem.indexOf(u8, r.reply.ename, "is not a pardes builtin") != null); - } - } - // Load is Restore: whatever it says, never acme's word refused. - const load = wr(p, Node.of(serial, .exec), "Load /tmp/pardes-surely-no-dump.zon\n"); - try testing.expect(std.mem.indexOf(u8, load.reply.ename, "is not a pardes builtin") == null); - // Put saves, as Save does. - _ = wr(p, Node.of(serial, .name), "/tmp/pardes-put.txt\n"); - const put = wr(p, Node.of(serial, .exec), "Put\n"); - try testing.expectEqual(Status.ok, put.reply.status); - try testing.expect(put.saved); - // Delete closes an edited pane without asking. - const made = try th.newPane(p); - _ = wr(p, Node.of(made, .name), "/tmp/pardes-delete.txt\n"); - _ = wr(p, Node.of(made, .body), "edited\n"); - try testing.expectEqual(Status.ok, wr(p, Node.of(made, .ctl), "Delete\n").reply.status); - p.sync(); - try testing.expect(p.paneBySerial(made) == null); -} - -test "a pane's ctl takes acme's words: done by the files that replace them, or refused with why" { - const p = try withFile(testing.allocator, "one two\n"); - defer p.deinit(); - const serial = serialOf(p); - const ctl_node = Node.of(serial, .ctl); - const pane = p.panes[p.paneBySerial(serial).?].?; - try testing.expectEqual(Status.ok, wr(p, ctl_node, "name /tmp/pardes-acme-ctl.txt\n").reply.status); - try testing.expectEqualStrings("/tmp/pardes-acme-ctl.txt", pane.file.?.path); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "dirty\n").reply.status); - try testing.expectEqualStrings("1", std.mem.trimEnd(u8, rd(p, Node.of(serial, .dirty), 0, 8).bytes, "\n")); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "clean\n").reply.status); - try testing.expectEqualStrings("0", std.mem.trimEnd(u8, rd(p, Node.of(serial, .dirty), 0, 8).bytes, "\n")); - _ = wr(p, Node.of(serial, .addr), "/two/"); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "dot=addr\nshow\nnomark\nmark\nlimit=addr\n").reply.status); - try testing.expectEqual(@as(u32, 4), pane_files.dotOf(pane).q0); - try testing.expect(pane.fs.limit != null); - try testing.expectEqual(Status.ok, wr(p, ctl_node, "cleartag\n").reply.status); - for ([_][]const u8{ "menu", "nomenu", "dump echo hi", "dumpdir /tmp", "font Go Mono" }) |line| { - const r = wr(p, ctl_node, line); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expect(std.mem.startsWith(u8, r.reply.ename, "invalid: acme's ")); - } - // Checked whole: a refused word anywhere and nothing ran. - try testing.expectEqual(E.INVAL, wr(p, ctl_node, "dirty\nmenu\n").errno()); - try testing.expectEqualStrings("0", std.mem.trimEnd(u8, rd(p, Node.of(serial, .dirty), 0, 8).bytes, "\n")); -} - test "a write to addr, limit or mark asks for no frame; one to body does" { const p = try withFile(testing.allocator, "one\n"); defer p.deinit(); @@ -2799,28 +2457,6 @@ test "a write to addr, limit or mark asks for no frame; one to body does" { try testing.expect(p.needs_frame); } -test "every EINVAL a write gets says why, in its err record too; DEL is a control character in a line" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - const log = @backingInt(tree.TopFile.log); - const h = call(p, .{ .tag = 1, .op = .open, .node = log, .omode = 2 }).reply.handle; - const Case = struct { node: u64, data: []const u8, words: []const u8, handle: u32 = 0 }; - for ([_]Case{ - .{ .node = root_exec, .data = "echo a\x7fb\n", .words = "control character" }, - .{ .node = root_look, .data = "a\x7fb\n", .words = "control character" }, - .{ .node = Node.of(serial, .name), .data = "\n", .words = "an empty name" }, - .{ .node = log, .data = "bogus\n", .words = "takes `follow`", .handle = h }, - }) |c| { - const refused = call(p, .{ .tag = 2, .op = .write, .node = c.node, .handle = c.handle, .data = c.data }); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expect(std.mem.indexOf(u8, refused.reply.ename, c.words) != null); - } - _ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = h }); - try testing.expect(!th.logHas(p, "Invalid argument")); - try testing.expect(th.logHas(p, "an empty name")); -} - test "/index shows a name as the log does: a newline in it is \\n, a control byte, C1 or byte not UTF-8 \\xNN" { const p = try th.withTerm(testing.allocator); defer p.deinit(); @@ -2841,31 +2477,6 @@ test "/index shows a name as the log does: a newline in it is \\n, a control byt try testing.expect(std.mem.indexOf(u8, rd(p, @backingInt(tree.TopFile.index), 0, 4096).bytes, "/tmp/back\\\\nslash") != null); } -test "a served-tree pane spoils no Grep, and Look . from a gone directory is ENOENT, not /virtual/" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "hit.txt", .data = "needle here\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - _ = wr(p, Node.of(serialOf(p), .name), try std.fmt.bufPrint(&line, "{s}/other.txt\n", .{dir})); - // A pane of the served tree. - const virtual = try th.newPane(p); - _ = wr(p, Node.of(virtual, .name), "/virtual/index\n"); - // One named the served tree's root, whose directory is `/`. - const top = try th.newPane(p); - _ = wr(p, Node.of(top, .name), "/virtual\n"); - try testing.expectEqual(Status.ok, wr(p, Node.of(serialOf(p), .exec), "Grep needle\n").reply.status); - // `.` from a pane whose directory is gone. - const gone = try th.newPane(p); - _ = wr(p, Node.of(gone, .name), "/tmp/pardes-no-such-dir-zz/f.txt\n"); - const looked = wr(p, Node.of(gone, .look), ".\n"); - try testing.expectEqual(E.NOENT, looked.errno()); - try testing.expectStringStartsWith(looked.reply.ename, "look: /tmp/pardes-no-such-dir-zz: no such directory"); -} - test "a column takes panes until their documented minimums fill it, not until none can be halved" { const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 50 }); defer p.deinit(); @@ -3335,39 +2946,6 @@ test "a file changed on disk under unsaved edits is not reloaded: it says so, ge try testing.expect(wr(p, Node.of(serial, .ctl), "Save\n").saved); } -test "a looked word is found in its own pane, next place after the dot, wrapping, and opens no pane" { - const p = try withFile(testing.allocator, "foo x foo y\n"); - defer p.deinit(); - const serial = serialOf(p); - const look_node = Node.of(serial, .look); - const dot = Node.of(serial, .dot); - var count: usize = 0; - for (p.panes) |slot| count += @intFromBool(slot != null); - for ([_][]const u8{ " 0 3 ", " 6 9 ", " 0 3 " }) |want| { - try testing.expectEqual(Status.ok, wr(p, look_node, "foo\n").reply.status); - try testing.expectEqualStrings(want, rd(p, dot, 0, 64).bytes); - } - var after: usize = 0; - for (p.panes) |slot| after += @intFromBool(slot != null); - try testing.expectEqual(count, after); - // LookWord list: the +Search listing, as before. - try testing.expect(p.executeBuiltinLine(0, "LookWord list")); - try testing.expectEqual(Status.ok, wr(p, look_node, "foo\n").reply.status); - after = 0; - for (p.panes) |slot| after += @intFromBool(slot != null); - try testing.expect(after > count); - // ...and look reads back that +Search pane, where the places are. - var listed = false; - var rows = std.mem.tokenizeScalar(u8, rd(p, look_node, 0, 256).bytes, '\n'); - while (rows.next()) |row| { - const made = p.panes[p.paneBySerial(try std.fmt.parseInt(u32, row, 10)).?].?; - if (made.file) |f| if (f.output) |o| if (o.from == .search) { - listed = true; - }; - } - try testing.expect(listed); -} - test "every word's /commands description is its own doc comment's first sentence, no citation" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -3478,37 +3056,6 @@ test "get names the path it could not read, logs the edits it threw away, and a _ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = h }); } -test "a builtin that fails through exec, tagexec or a column's exec fails the write with one err and no msg" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - p.update(.tick); // the pane's `new` is logged first - while (p.nextEffect()) |_| {} - const serial = serialOf(p); - for ([_]u64{ root_exec, Node.of(serial, .exec), @backingInt(tree.TopFile.tagexec), Node.ofCol(pardes.layout.columnSerial(p, 0), .exec) }) |node| { - const failed = wr(p, node, "Unmount zzz\n"); - try testing.expectEqual(Status.err, failed.reply.status); - try testing.expect(std.mem.indexOf(u8, failed.reply.ename, "Unmount: no such mount zzz") != null); - } - const log = @backingInt(tree.TopFile.log); - const h = call(p, .{ .tag = 1, .op = .open, .node = log }).reply.handle; - const text = call(p, .{ .tag = 2, .op = .read, .node = log, .handle = h, .size = 1 << 16 }).bytes; - try testing.expect(std.mem.indexOf(u8, text, "msg ") == null); - // The root's exec ran at the pane, so its err and the pane's exec's are - // the same record, counted. - try testing.expectEqual(@as(usize, 3), std.mem.count(u8, text, "Unmount: no such mount zzz")); - try testing.expect(std.mem.indexOf(u8, text, "mount zzz (x2)") != null); - _ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = h }); - // A control character is refused with its reason. - try testing.expectEqualStrings(e_control, wr(p, root_exec, "Msg a\x01b\n").reply.ename); - // A look miss: one err, no msg. - _ = wr(p, @backingInt(tree.TopFile.look), "zzqqnotthere\n"); - try testing.expect(th.logHas(p, "look: no match for \"zzqqnotthere\"")); - try testing.expect(!th.logHas(p, "msg 1 Look: no match")); - // The same miss again is that record counted, as any repeated err is. - _ = wr(p, @backingInt(tree.TopFile.look), "zzqqnotthere\n"); - try testing.expect(th.logHas(p, "look: no match for \"zzqqnotthere\" (x2)\n")); -} - test "a word written to a raw terminal's look is listed as in normal mode" { const p = try th.withTerm(testing.allocator); defer p.deinit(); @@ -3522,30 +3069,6 @@ test "a word written to a raw terminal's look is listed as in normal mode" { try testing.expect(made.file.?.output.?.from == .search); } -test "a look that goes to another pane leaves the looking pane's dot alone" { - const p = try withFile(testing.allocator, "alpha bravo\ncharlie\n"); - defer p.deinit(); - const source = serialOf(p); - const other = try th.newPane(p); - _ = wr(p, Node.of(other, .body), "one\ntwo\nthree\n"); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "f.txt", .data = "f one\nf two\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - for ([_][]const u8{ "@p{d}:2", "@p{d}:/three/", "{s}/f.txt:2" }) |form| { - @import("pane.zig").setDot(p.panes[p.paneBySerial(source).?].?, .{ .q0 = 6, .q1 = 11 }); - const text = if (form[0] == '@') - (if (std.mem.eql(u8, form, "@p{d}:2")) try std.fmt.bufPrint(&line, "@p{d}:2\n", .{other}) else try std.fmt.bufPrint(&line, "@p{d}:/three/\n", .{other})) - else - try std.fmt.bufPrint(&line, "{s}/f.txt:2\n", .{dir}); - try testing.expectEqual(Status.ok, wr(p, Node.of(source, .look), text).reply.status); - try testing.expect(p.active != p.paneBySerial(source).?); - try testing.expectEqualStrings(" 6 11 ", rd(p, Node.of(source, .dot), 0, 64).bytes); - } -} - test "@p:
takes any address over the terminal's lines, selecting the match" { const p = try th.withTerm(testing.allocator); defer p.deinit(); @@ -3713,45 +3236,6 @@ test "Edit on an image pane is refused as a data write there is, EINVAL, before try testing.expectEqual(E.INVAL, wr(p, Node.of(doc.serial, .name), "/tmp/other.ppm\n").errno()); } -test "`@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`" { - const p = try withFile(testing.allocator, "one\ntwo\n"); - defer p.deinit(); - const s = serialOf(p); - var buf: [64]u8 = undefined; - p.look_missed = false; - pardes.look.lookAt(p, 0, try std.fmt.bufPrint(&buf, "@p{d}:", .{s})); - try testing.expect(!p.look_missed); - const f = pane_files.fileOf(p.panes[0].?).?; - for ([_][]const u8{ ":99999", try std.fmt.bufPrint(&buf, "{s}:99999", .{f.path}) }) |looked| { - pardes.look.lookAt(p, 0, looked); - const said = p.panes[0].?.msg[0..p.panes[0].?.msg_len]; - try testing.expect(std.mem.endsWith(u8, said, "/test.txt:99999 has no line 99999")); - } -} - -test "a second LookWord list look in a directory refills its +Search rather than stacking another" { - if (comptime !pardes.hosted) return; - const gpa = testing.allocator; - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "words.txt", .data = "alpha beta\nalpha beta\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var path_buf: [4200]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "{s}/words.txt", .{dir}); - const p = try Pardes.init(gpa, .{ .file = path, .tty_only = true, .cols = 80, .rows = 30 }); - defer p.deinit(); - _ = wr(p, @backingInt(tree.TopFile.ctl), "LookWord list\n"); - pardes.look.lookAt(p, 0, "alpha"); - pardes.look.lookAt(p, 0, "beta"); - var searches: usize = 0; - for (p.panes) |slot| if (slot) |pn| if (pn.file) |f| if (std.mem.endsWith(u8, f.path, "+Search")) { - searches += 1; - try testing.expectEqualStrings("beta", f.output.?.arg()); - }; - try testing.expectEqual(@as(usize, 1), searches); -} - test "Petscii, Palette and Ascii take the state their tag word names, set rather than flipped" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -3828,30 +3312,6 @@ test "Lspinfo's answer is the +Lsp pane it fills, as a Rename preview's is its + } -test "a ctl line over 1 MiB is refused once, EINVAL, and its tail is not run as a line of its own" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const node = Node.of(serialOf(p), .ctl); - const h = call(p, .{ .tag = 1, .op = .open, .node = node, .omode = 1 }).reply.handle; - const chunk: [4096]u8 = @splat('z'); - var refused: usize = 0; - var off: u64 = 0; - for (0..270) |_| { - const r = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .off = off, .data = &chunk }); - if (r.reply.status == .err) { - refused += 1; - try testing.expectEqual(E.INVAL, r.errno()); - } - off += chunk.len; - } - try testing.expectEqual(Status.ok, call(p, .{ .tag = 3, .op = .write, .node = node, .handle = h, .off = off, .data = "zzz\nEdit ,d\n" }).reply.status); - try testing.expectEqual(@as(usize, 1), refused); - try testing.expect(!th.logHas(p, "unknown control message")); - // What came after the long line's newline ran. - try testing.expectEqualStrings("", pane_files.fileOf(p.panes[0].?).?.content); - _ = call(p, .{ .tag = 4, .op = .release, .node = node, .handle = h }); -} - test "DumpDir refuses a relative directory and one that may not be written, up front; a Dump there says permission denied" { if (comptime !pardes.hosted) return; const p = try withFile(testing.allocator, "x\n"); diff --git a/test/behaviour.zig b/test/behaviour.zig index 72806b21..c6e1dc9d 100644 --- a/test/behaviour.zig +++ b/test/behaviour.zig @@ -8,6 +8,7 @@ test { _ = @import("behaviour/edit.zig"); _ = @import("behaviour/event.zig"); _ = @import("behaviour/flags.zig"); + _ = @import("behaviour/from_acme.zig"); _ = @import("behaviour/keys.zig"); _ = @import("behaviour/limits.zig"); _ = @import("behaviour/log.zig"); diff --git a/test/behaviour/edit.zig b/test/behaviour/edit.zig index bfb17533..83b1b464 100644 --- a/test/behaviour/edit.zig +++ b/test/behaviour/edit.zig @@ -45,3 +45,15 @@ test "edit: Edit through a pane's ctl renames every foo in one undo step, and a try s.write(ctl, "Undo"); try s.expect(body, "foo x foo y foo\n"); } + +test "edit: Edit's p and = never fail for want of room for +Errors: the text is logged instead" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + s.detach(); + try s.write("/ctl", "size 80 6\n"); + const index = try s.read("/index"); + try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Edit ,p\n"); + const log = try s.read("/log"); + try std.testing.expect(std.mem.indexOf(u8, log, " one\n") != null and std.mem.indexOf(u8, log, " two\n") != null); + try s.expect("/index", index); // the +Errors that had no room is gone +} diff --git a/test/behaviour/from_acme.zig b/test/behaviour/from_acme.zig new file mode 100644 index 00000000..62d4fc55 --- /dev/null +++ b/test/behaviour/from_acme.zig @@ -0,0 +1,28 @@ +//! docs/typ/reference.typ, Coming from acme . +const std = @import("std"); +const Session = @import("session").Session; + +test "from-acme: acme's words run as pardes's (Put is Save, Delete a Del that does not ask) or are refused, never run as commands" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + for ([_][]const u8{ "Snarf\n", "Indent\n", "Local\n", "Abort\n" }) |line| { + for ([_][]const u8{ s.at("/pane/{d}/ctl", .{n}), s.at("/pane/{d}/exec", .{n}) }) |path| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(path, line)); + try std.testing.expect(std.mem.indexOf(u8, s.said, "is not a pardes builtin") != null); + } + } + // Load is Restore: whatever it says, never acme's word refused. + _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "Load /tmp/pardes-surely-no-dump.zon\n"); + try std.testing.expect(std.mem.indexOf(u8, s.said, "is not a pardes builtin") == null); + // Put saves, as Save does. + try s.write(s.at("/pane/{d}/name", .{n}), "/tmp/pardes-put.txt\n"); + try s.write(s.at("/pane/{d}/exec", .{n}), "Put\n"); + try std.testing.expectEqual(@as(usize, 1), s.saved); + // Delete closes an edited pane without asking. + const made = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{made}), "/tmp/pardes-delete.txt\n"); + try s.write(s.at("/pane/{d}/body", .{made}), "edited\n"); + try s.write(s.at("/pane/{d}/ctl", .{made}), "Delete\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), "/tmp/pardes-delete.txt") == null); +} diff --git a/test/behaviour/limits.zig b/test/behaviour/limits.zig index 08c864eb..d84d4490 100644 --- a/test/behaviour/limits.zig +++ b/test/behaviour/limits.zig @@ -16,3 +16,25 @@ test "limits: a builtin that needs a pane says so when every slot is taken" { try std.testing.expectEqualStrings("help: no space for a pane: 64 max", s.said); try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "help: no space for a pane") != null); } + +test "limits: a ctl line over 1 MiB is refused once, EINVAL, and its tail is not run as a line of its own" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = try s.hold(s.at("/pane/{d}/ctl", .{n}), 1); + const chunk: [4096]u8 = @splat('z'); + var refused: usize = 0; + for (0..270) |_| { + const errno = try s.writeFid(ctl, &chunk); + if (errno != 0) { + refused += 1; + try std.testing.expectEqual(@as(u16, 22), errno); + } + } + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ctl, "zzz\nEdit ,d\n")); + s.clunk(ctl); + try std.testing.expectEqual(@as(usize, 1), refused); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "unknown control message") == null); + // What came after the long line's newline ran. + try s.expect(s.at("/pane/{d}/body", .{n}), ""); +} diff --git a/test/behaviour/look_and_exec.zig b/test/behaviour/look_and_exec.zig index 360055d2..2ce95354 100644 --- a/test/behaviour/look_and_exec.zig +++ b/test/behaviour/look_and_exec.zig @@ -433,3 +433,264 @@ test "look-and-exec: a builtin that takes no argument, written with one to an ex try std.testing.expect(std.mem.startsWith(u8, s.said, "wrong #args in control message")); }; } + +/// 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)]}); +} + +test "look-and-exec: a look opens spaced paths and locations without editing the source pane" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "two words.zig", .data = "first\nsecond\nthird\n" }); + var s = try Session.initFile(std.testing.allocator, "source stays intact\n"); + defer s.deinit(); + const path = s.at("{s}/two words.zig", .{try tmpPath(&s, &tmp)}); + const source = try s.focused(); + const ctl = try s.read(s.at("/pane/{d}/ctl", .{source})); + try s.write(s.at("/pane/{d}/dot", .{source}), "3 3"); + try s.write(s.at("/pane/{d}/look", .{source}), s.at("{s}:2:3\n", .{path})); + const opened = try s.focused(); + try std.testing.expect(opened != source); + try s.expect(s.at("/pane/{d}/name", .{opened}), s.at("{s}\n", .{path})); + try s.expect(s.at("/pane/{d}/body", .{opened}), "first\nsecond\nthird\n"); + 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]); + 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. + try s.write("/focus", s.at("{d}\n", .{source})); + try s.write("/look", s.at("{s}:3:2\n", .{path})); + try expectEqual(opened, try s.focused()); + try expectEqual(@as(u32, 14), (try s.range(s.at("/pane/{d}/dot", .{opened})))[0]); // line 3, column 2 + try s.expect("/look", s.at("{d}\n", .{opened})); +} + +test "look-and-exec: a look validates the whole batch before opening virtual files" { + var s = try Session.initFile(std.testing.allocator, "source\n"); + defer s.deinit(); + const source = try s.focused(); + const look = s.at("/pane/{d}/look", .{source}); + const index = try s.read("/index"); + for ([_][]const u8{ "/virtual/index\x00ignored", "/virtual/index\n/virtual/\x01index\n" }) |invalid| { + try expectEqual(@as(u16, 22), try s.refused(look, invalid)); + try s.expect("/index", index); + try expectEqual(source, try s.focused()); + try s.expect(s.at("/pane/{d}/body", .{source}), "source\n"); + } + try s.write(look, "/n/self/index\n"); + const opened = try s.focused(); + try s.expect(s.at("/pane/{d}/name", .{opened}), "/virtual/index\n"); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/body", .{opened})), "/test.txt")); + try s.expect(s.at("/pane/{d}/body", .{source}), "source\n"); +} + +test "look-and-exec: status reads the editor, and look and exec validate whole batches" { + var s = try Session.initFile(std.testing.allocator, "Msg root\n"); + defer s.deinit(); + const status = try s.read("/status"); + var rows = std.mem.tokenizeScalar(u8, status, '\n'); + try std.testing.expect(std.mem.startsWith(u8, rows.next().?, "pid ")); + try std.testing.expect(std.mem.startsWith(u8, rows.next().?, "version ")); + try std.testing.expectEqualStrings(s.at("panes {d}", .{lines(try s.read("/index"))}), rows.next().?); + try std.testing.expect(rows.next() == null); + const st = try s.stat("/status"); + try expectEqual(@as(u64, status.len), st.size); + try expectEqual(@as(u16, 0o444), st.mode & 0o777); + try expectEqual(@as(u16, 1), try s.refused("/status", "anything\n")); + try s.expect("/exec", ""); + const index = try s.read("/index"); + for ([_][]const u8{ "Msg a\x00b", "Msg x\nMsg \x01y\n" }) |invalid| { + try expectEqual(@as(u16, 22), try s.refused("/exec", invalid)); + try s.expect("/index", index); + } + // An empty write is a no-op, not a command. + try s.write("/exec", "\n \n"); + try s.expect("/exec", ""); + try s.expect("/status", status); + const n = try s.focused(); + try s.write("/exec", "Msg hello there\n"); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} hello there\n", .{n}))); + try s.expect("/exec", s.at("{d}\n", .{n})); + try s.write("/look", "/virtual/index\n"); + const opened = try s.focused(); + try s.expect(s.at("/pane/{d}/name", .{opened}), "/virtual/index\n"); + try s.expect("/look", s.at("{d}\n", .{opened})); + try s.write("/exec", "New\n"); + const made = try s.focused(); + try std.testing.expect(made != opened); + try s.expect("/exec", s.at("{d}\n", .{made})); +} + +test "look-and-exec: a look at a file it may not read fails the write naming the file" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "locked.txt", .data = "secret\n", .flags = .{ .permissions = .fromMode(0o000) } }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const path = s.at("{s}/locked.txt", .{try tmpPath(&s, &tmp)}); + if (try s.refused("/look", s.at("{s}\n", .{path})) == 0) return error.SkipZigTest; // root reads anything + try std.testing.expectEqualStrings(s.at("look: {s}: permission denied", .{path}), s.said); +} + +test "look-and-exec: a look line's trailing colon is dropped, and a look whose address fails never leaves its file open" { + 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(); + const dir = try tmpPath(&s, &tmp); + const before = lines(try s.read("/index")); + // Failing addresses: nothing opened, the write still answered. + for ([_][]const u8{ s.at("{s}/a.txt:/nomatch/\n", .{dir}), s.at("{s}/a.txt:9:\n", .{dir}) }) |line| { + _ = try s.refused("/look", line); + try expectEqual(before, lines(try s.read("/index"))); + } + // `a.txt:2:3:` is `a.txt:2:3`: it opens there. + try s.write("/look", s.at("{s}/a.txt:2:3:\n", .{dir})); + try expectEqual(before + 1, lines(try s.read("/index"))); + const opened = try s.focused(); + try s.expect(s.at("/pane/{d}/name", .{opened}), s.at("{s}/a.txt\n", .{dir})); + try expectEqual(@as(u32, 6), (try s.range(s.at("/pane/{d}/dot", .{opened})))[0]); +} + +test "look-and-exec: a look that misses file:/re/ names the file, and is logged as its pane's" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "alpha\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const first = try s.focused(); + try s.write("/look", s.at("{s}/f.txt\n", .{dir})); + const f = try s.focused(); + _ = try s.refused(s.at("/pane/{d}/look", .{first}), s.at("{s}/f.txt:/zzqq/\n", .{dir})); + try std.testing.expect(has(try s.read("/log"), s.at("err {d} look: {s}/f.txt: no match for regexp", .{ f, dir }))); +} + +test "look-and-exec: a look that goes to another pane leaves the looking pane's dot alone" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f one\nf two\n" }); + var s = try Session.initFile(std.testing.allocator, "alpha bravo\ncharlie\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const source = try s.focused(); + const other = try s.newPane(); + try s.write(s.at("/pane/{d}/body", .{other}), "one\ntwo\nthree\n"); + for ([_][]const u8{ s.at("@p{d}:2\n", .{other}), s.at("@p{d}:/three/\n", .{other}), s.at("{s}/f.txt:2\n", .{dir}) }) |line| { + try s.write(s.at("/pane/{d}/dot", .{source}), "6 11"); + try s.write(s.at("/pane/{d}/look", .{source}), line); + try std.testing.expect(try s.focused() != source); + try expectEqual(.{ @as(u32, 6), @as(u32, 11) }, try s.range(s.at("/pane/{d}/dot", .{source}))); + } +} + +test "look-and-exec: a looked word is found in its own pane, next place after the dot, wrapping, and opens no pane; LookWord list lists it in +Search" { + var s = try Session.initFile(std.testing.allocator, "foo x foo y\n"); + defer s.deinit(); + const n = try s.focused(); + const look = s.at("/pane/{d}/look", .{n}); + const index = try s.read("/index"); + for ([_]u32{ 0, 6, 0 }) |at| { + try s.write(look, "foo\n"); + try expectEqual(.{ at, at + 3 }, try s.range(s.at("/pane/{d}/dot", .{n}))); + } + try s.expect("/index", index); + try s.write("/ctl", "LookWord list\n"); + try s.write(look, "foo\n"); + try std.testing.expect(lines(try s.read("/index")) > lines(index)); + // ...and look reads back that +Search pane, where the places are. + const search = try paneNamed(&s, "/+Search"); + try std.testing.expect(has(try s.read(look), s.at("{d}\n", .{search}))); +} + +test "look-and-exec: a second LookWord list look in a directory refills its +Search rather than stacking another" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "words.txt", .data = "alpha beta\nalpha beta\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/words.txt\n", .{try tmpPath(&s, &tmp)})); + try s.write(s.at("/pane/{d}/exec", .{n}), "Get\n"); + try s.write("/ctl", "LookWord list\n"); + try s.write(s.at("/pane/{d}/look", .{n}), "alpha\n"); + const first = try paneNamed(&s, "/+Search"); + 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")); +} + +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`" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const look = s.at("/pane/{d}/look", .{n}); + const before = try errs(&s); + try s.write(look, s.at("@p{d}:\n", .{n})); + try expectEqual(before, try errs(&s)); + try s.write(look, ":99999\n"); + try s.write(look, "/test.txt:99999\n"); + try std.testing.expect(has(try s.read("/log"), "/test.txt:99999 has no line 99999 (x2)\n")); +} + +test "look-and-exec: an exec line over the command limit fails the write and says the limit; a builtin's may be longer" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const long = "echo " ++ &@as([1024]u8, @splat('y')) ++ "\n"; + try expectEqual(@as(u16, 22), try s.refused("/exec", long)); + try std.testing.expectEqualStrings("invalid command line: a command line is at most 1024 bytes", s.said); + try std.testing.expect(has(try s.read("/log"), s.at("err {d} exec: invalid command line: a command line is at most 1024 bytes", .{n}))); + _ = try s.refused("/exec", "echo a\x01b\n"); + try std.testing.expectEqualStrings("invalid command line: it holds a control character (or DEL) other than a tab", s.said); + try std.testing.expect(has(try s.read("/log"), s.at("err {d} exec: invalid command line: it holds", .{n}))); + try s.write("/exec", "Msg " ++ &@as([1100]u8, @splat('z')) ++ "\n"); +} + +test "look-and-exec: a builtin that fails through exec, tagexec or a column's exec fails the write with one err and no msg" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + // Another failure first: the pane's `new` is logged after it. + _ = try s.refused("/ctl", "Unmount yyy\n"); + for ([_][]const u8{ "/exec", s.at("/pane/{d}/exec", .{n}), "/tagexec", s.at("/col/{d}/exec", .{try s.column(0)}) }) |path| { + try std.testing.expect(try s.refused(path, "Unmount zzz\n") != 0); + try std.testing.expect(has(s.said, "Unmount: no such mount zzz")); + } + const log = try s.read("/log"); + try std.testing.expect(!has(log, "msg ")); + // The root's exec ran at the pane, so its err and the pane's exec's are + // 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 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\"")); + try std.testing.expect(!has(try s.read("/log"), "msg ")); + try s.write("/look", "zzqqnotthere\n"); + try std.testing.expect(has(try s.read("/log"), "look: no match for \"zzqqnotthere\" (x2)\n")); +} + +test "look-and-exec: a served-tree pane spoils no Grep, and Look . from a gone directory is ENOENT, not /virtual/" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "hit.txt", .data = "needle here\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{first}), s.at("{s}/other.txt\n", .{try tmpPath(&s, &tmp)})); + // A pane of the served tree, and one named its root, whose directory is `/`. + try s.write(s.at("/pane/{d}/name", .{try s.newPane()}), "/virtual/index\n"); + try s.write(s.at("/pane/{d}/name", .{try s.newPane()}), "/virtual\n"); + try s.write(s.at("/pane/{d}/exec", .{first}), "Grep needle\n"); + // `.` from a pane whose directory is gone. + const gone = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{gone}), "/tmp/pardes-no-such-dir-zz/f.txt\n"); + try expectEqual(@as(u16, 2), try s.refused(s.at("/pane/{d}/look", .{gone}), ".\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "look: /tmp/pardes-no-such-dir-zz: no such directory")); +} diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index 9103508c..7bcf13d0 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -657,3 +657,48 @@ test "panes: Get of a file not there says so without asking about unsaved edits" try std.testing.expect(try s.refused(s.at("/pane/{d}/exec", .{n}), s.at("Get {s}/f.txt\n", .{dir})) != 0); try s.expect(s.at("/pane/{d}/body", .{n}), "x\nunsaved\n"); } + +test "panes: a pane's ctl takes acme's words: done by the files that replace them, or refused with why" { + var s = try Session.initFile(std.testing.allocator, "one two\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const dirty = s.at("/pane/{d}/dirty", .{n}); + try s.write(ctl, "name /tmp/pardes-acme-ctl.txt\n"); + try s.expect(s.at("/pane/{d}/name", .{n}), "/tmp/pardes-acme-ctl.txt\n"); + try s.write(ctl, "dirty\n"); + try s.expect(dirty, "1\n"); + try s.write(ctl, "clean\n"); + try s.expect(dirty, "0\n"); + try s.write(s.at("/pane/{d}/addr", .{n}), "/two/"); + try s.write(ctl, "dot=addr\nshow\nnomark\nmark\nlimit=addr\n"); + try std.testing.expectEqual(@as(u32, 4), (try s.range(s.at("/pane/{d}/dot", .{n})))[0]); + try std.testing.expectEqual(.{ @as(u32, 4), @as(u32, 7) }, try s.range(s.at("/pane/{d}/limit", .{n}))); + try s.write(ctl, "cleartag\n"); + for ([_][]const u8{ "menu\n", "nomenu\n", "dump echo hi\n", "dumpdir /tmp\n", "font Go Mono\n" }) |line| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, line)); + try std.testing.expect(std.mem.startsWith(u8, s.said, "invalid: acme's ")); + } + // Checked whole: a refused word anywhere and nothing ran. + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "dirty\nmenu\n")); + try s.expect(dirty, "0\n"); +} + +test "panes: +Unsaved drops a pane once it has closed" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const a = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{a}), "/tmp/pardes-unsaved-a.txt\n"); + try s.write(s.at("/pane/{d}/body", .{a}), "edited\n"); + const b = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{b}), "/tmp/pardes-unsaved-b.txt\n"); + try s.write(s.at("/pane/{d}/body", .{b}), "edited\n"); + try std.testing.expect(try s.refused("/ctl", "Exit\n") != 0); + const list = s.at("/pane/{d}/body", .{(try panesNamed(&s, "/+Unsaved"))[0]}); + try std.testing.expect(has(try s.read(list), "/tmp/pardes-unsaved-a.txt: Modified")); + // a closes (discarded): its row goes. + _ = try s.refused(s.at("/pane/{d}/ctl", .{a}), "Del\n"); + try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n"); + try std.testing.expectEqual(@as(usize, 0), (try panesNamed(&s, "/pardes-unsaved-a.txt")).len); + try std.testing.expect(!has(try s.read(list), "/tmp/pardes-unsaved-a.txt: Modified")); +} diff --git a/test/behaviour/placement.zig b/test/behaviour/placement.zig index 079b5a5e..893cd9a4 100644 --- a/test/behaviour/placement.zig +++ b/test/behaviour/placement.zig @@ -59,3 +59,23 @@ test "placement: Placement pardes puts a new pane right under the asking one, an try s.write("/ctl", "Placement\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPlacement acme\n") != null); } + +test "placement: a look under pardes's placement, from a pane pane/new made after Delcol, opens its file in a column" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "file\n" }); + var buf: [4096]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{first}), "Placement\n"); + try s.write(s.at("/pane/{d}/exec", .{first}), "Delcol\n"); + const made = try s.newPane(); + try s.write(s.at("/pane/{d}/look", .{made}), s.at("{s}/f.txt:#4\n", .{dir})); + const opened = try s.focused(); + try std.testing.expect(opened != made); + // The pane pane/new made is placed, and so is the one its look opened. + const cols = try s.columns(); + for ([_]u32{ made, opened }) |n| try std.testing.expect(std.mem.indexOf(u8, cols, s.at(":{d}", .{n})) != null or std.mem.indexOf(u8, cols, s.at(",{d}", .{n})) != null); +} diff --git a/test/behaviour/root_ctl.zig b/test/behaviour/root_ctl.zig index 3e546237..12f2b56a 100644 --- a/test/behaviour/root_ctl.zig +++ b/test/behaviour/root_ctl.zig @@ -123,3 +123,21 @@ fn lastIncl(s: *Session) ![]const u8 { const at = std.mem.indexOf(u8, log, "\nmsg - Incl:") orelse return error.NoIncl; return log[at .. std.mem.indexOfScalarPos(u8, log, at + 1, '\n').? + 1]; } + +test "root-ctl: Joincol with no column to the right and Theme with no such theme say so; a theme's name is any case" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try std.testing.expect(try s.refused("/ctl", "Joincol\n") != 0); + try std.testing.expect(std.mem.startsWith(u8, s.said, "Joincol: no column to the right")); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Theme drak\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "bad value in control message; like it: ")); + try std.testing.expect(has(s.said, ": dr")); + try std.testing.expect(std.mem.endsWith(u8, s.said, "\"drak\"")); + // Within the 128 bytes an Rerror carries, the quoted line kept. + try std.testing.expect(s.said.len <= 128); + // A click on the word says it in the log. + _ = try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), "Theme drak\n"); + try std.testing.expect(has(try s.read("/log"), "no theme \"drak\"")); + try s.write("/ctl", "Theme DRACULA\n"); + try std.testing.expect(has(try s.read("/ctl"), "\nTheme dracula\n")); +} diff --git a/test/behaviour/rules.zig b/test/behaviour/rules.zig index 4b853a40..8f5597ae 100644 --- a/test/behaviour/rules.zig +++ b/test/behaviour/rules.zig @@ -37,3 +37,25 @@ test "rules: body reads at any offset and writes append; a stat says its length, try std.testing.expect((try s.stat(body)).version != st.version); try std.testing.expectEqual(@as(u32, 0), (try s.stat(s.at("/pane/{d}/tag", .{try s.focused()}))).version); } + +test "rules: every EINVAL a write gets says why, in its err record too; DEL is a control character in a line" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + for ([_][2][]const u8{ + .{ "/exec", "echo a\x7fb\n" }, + .{ "/look", "a\x7fb\n" }, + }) |c| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(c[0], c[1])); + try std.testing.expect(std.mem.indexOf(u8, s.said, "control character") != null); + } + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/name", .{n}), "\n")); + try std.testing.expect(std.mem.indexOf(u8, s.said, "an empty name") != null); + const log = try s.hold("/log", 2); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(log, "bogus\n")); + try std.testing.expect(std.mem.indexOf(u8, s.said, "takes `follow`") != null); + s.clunk(log); + const text = try s.read("/log"); + try std.testing.expect(std.mem.indexOf(u8, text, "Invalid argument") == null); + try std.testing.expect(std.mem.indexOf(u8, text, "an empty name") != null); +} diff --git a/test/behaviour/settings.zig b/test/behaviour/settings.zig index b850a532..f3ed57de 100644 --- a/test/behaviour/settings.zig +++ b/test/behaviour/settings.zig @@ -112,3 +112,23 @@ test "settings: a bad setting value is quoted, so two different bad values log a try std.testing.expect(has(log, "takes on, off \"nope\"\n")); try std.testing.expect(!has(log, "(x2)")); } + +test "settings: a setting this frontend cannot show says GUI-only, never run as a command, and DumpDir reads back" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + if (comptime @import("pardes_config").platform != .gui) { + _ = try s.refused("/ctl", "Lift on\n"); + try std.testing.expectEqualStrings("Lift is GUI-only, invalid here \"Lift\"", s.said); + _ = try s.refused("/ctl", "GripWidth 120\n"); + try std.testing.expectEqualStrings("GripWidth is GUI-only, invalid here \"GripWidth\"", s.said); + // A builtin the GUI alone has, written to a ctl or an exec, never + // runs as a shell command. + _ = try s.refused("/ctl", "Fonts\n"); + try std.testing.expectEqualStrings("Fonts is GUI-only, invalid here \"Fonts\"", s.said); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/exec", "Fonts\n")); + try std.testing.expectEqualStrings("Fonts is GUI-only, invalid here", s.said); + } + _ = try s.refused("/ctl", "Liftt on\n"); + try std.testing.expectEqualStrings("unknown control message \"Liftt\"", s.said); + try std.testing.expect(has(try s.read("/ctl"), "\nDumpDir /")); +} -- cgit v1.3 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 --- src/ninep/ctl.zig | 328 ----------------------------------------- test/behaviour/other_files.zig | 229 ++++++++++++++++++++++++++++ test/behaviour/settings.zig | 42 ++++++ 3 files changed, 271 insertions(+), 328 deletions(-) (limited to 'src') diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index f23f2579..ec7cb3bc 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -2495,261 +2495,6 @@ test "a column takes panes until their documented minimums fill it, not until no } } -test "Jumplist fills its one +Jumps again, as Recent does, never a second" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - var count: [2]usize = undefined; - for (&count) |*n| { - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .exec), "Jumplist\n").reply.status); - p.sync(); - n.* = 0; - for (p.panes) |slot| if (slot) |pane| if (pane.file) |f| if (f.output) |o| { - n.* += @intFromBool(std.meta.eql(o.from, panes.Output.Origin{ .cmd = .Jumplist })); - }; - } - try testing.expectEqual(@as(usize, 1), count[0]); - try testing.expectEqual(@as(usize, 1), count[1]); -} - -test "a Jumplist entry is (closed) only while no pane has its file open" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "one\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "b.txt", .data = "two\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/a.txt\n", .{dir})); - p.sync(); - const a = p.panes[p.active].?.serial; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/b.txt\n", .{dir})); - p.sync(); - _ = wr(p, Node.of(a, .ctl), "Del\n"); - p.sync(); - var closed: usize = 0; - for (p.jumps[0..p.njumps]) |j| closed += @intFromBool(j.serial == 0); - try testing.expect(closed > 0); - // Open again, by a look: no entry is closed any more. - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/a.txt\n", .{dir})); - p.sync(); - for (p.jumps[0..p.njumps]) |j| try testing.expect(j.serial != 0); -} - -test "Recent's row for a file still open is where its dot is now, not where it last closed" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - try testing.expectEqual(Status.ok, wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/a.txt:3\n", .{dir})).reply.status); - p.sync(); - const pane = p.panes[p.active].?; - pane.body.vsel.active = false; // the look's selection of line 3 let go - pane.body.cur_row = 1; - pane.body.cur_col = 2; - try testing.expectEqual(Status.ok, wr(p, root_exec, "Recent\n").reply.status); - const rows = p.panes[p.paneBySerial(try std.fmt.parseInt(u32, std.mem.trimEnd(u8, rd(p, root_exec, 0, 64).bytes, "\n"), 10)).?].?; - // A query pane under the file (line 1 `Recent `), its rows said from - // its directory. - try testing.expect(std.mem.startsWith(u8, rows.file.?.content, "Recent \n")); - try testing.expect(std.mem.indexOf(u8, rows.file.?.content, "\na.txt:2:3 open\n") != null); -} - -test "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" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "b.txt", .data = "bee\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - const a_path = try std.fmt.bufPrint(&line, "{s}/a.txt", .{dir}); - var a_buf: [4200]u8 = undefined; - const a = a_buf[0..a_path.len]; - @memcpy(a, a_path); - // Opened, its dot on line 3, then closed. - try testing.expectEqual(Status.ok, wr(p, root_look, try std.fmt.bufPrint(&line, "{s}:3\n", .{a})).reply.status); - p.sync(); - const opened = p.active; - try testing.expectEqualStrings(a, p.panes[opened].?.file.?.path); - var want: [4300]u8 = undefined; - try testing.expect(std.mem.indexOf(u8, rd(p, @backingInt(tree.TopFile.recent), 0, 8192).bytes, try std.fmt.bufPrint(&want, "open {s}\n", .{a})) != null); - // Another file, so the jumplist has somewhere to come back from. - try testing.expectEqual(Status.ok, wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/b.txt\n", .{dir})).reply.status); - p.sync(); - try testing.expectEqual(Status.ok, wr(p, Node.of(p.panes[opened].?.serial, .ctl), "Del\n").reply.status); - p.sync(); - try testing.expect(std.mem.indexOf(u8, rd(p, @backingInt(tree.TopFile.recent), 0, 8192).bytes, try std.fmt.bufPrint(&want, "closed {s}\n", .{a})) != null); - // Recent lists it, closed, at its dot; a look at the row opens it there. - try testing.expectEqual(Status.ok, wr(p, root_exec, "Recent\n").reply.status); - const rows = p.panes[p.paneBySerial(try std.fmt.parseInt(u32, std.mem.trimEnd(u8, rd(p, root_exec, 0, 64).bytes, "\n"), 10)).?].?; - // Its query pane's rows are said from its directory, a.txt's. - const at = std.mem.indexOf(u8, rows.file.?.content, "\na.txt:3:").? + 1; - const row = rows.file.?.content[at..std.mem.indexOfScalarPos(u8, rows.file.?.content, at, '\n').?]; - try testing.expect(std.mem.endsWith(u8, row, " (closed)")); - try testing.expectEqual(Status.ok, wr(p, Node.of(rows.serial, .look), try std.fmt.bufPrint(&line, "{s}\n", .{row[0 .. row.len - " (closed)".len]})).reply.status); - p.sync(); - const back = p.panes[p.active].?; - try testing.expectEqualStrings(a, back.file.?.path); - try testing.expectEqual(@as(i32, 2), back.body.cur_row); - // Closed again; the jumplist keeps its entry, closed, and a jump back - // to it opens it again. - _ = wr(p, Node.of(back.serial, .ctl), "Del\n"); - p.sync(); - var closed_entry = false; - for (p.jumps[0..p.njumps]) |j| closed_entry = closed_entry or j.serial == 0; - try testing.expect(closed_entry); - // 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. - p.settings.jump_scope = .all; - var tries: usize = 0; - while (tries < p.njumps) : (tries += 1) { - p.jumpBy(-1); - p.sync(); - if (p.panes[p.active].?.file) |f| if (std.mem.eql(u8, f.path, a)) break; - } - try testing.expectEqualStrings(a, p.panes[p.active].?.file.?.path); -} - -test "Back and Forward over a closed file's entries: one reopen is that file's pane in every entry, so the next step moves" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - var long: [400]u8 = undefined; - var n: usize = 0; - for (0..100) |_| { - @memcpy(long[n..][0..4], "row\n"); - n += 4; - } - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = long[0..n] }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "b.txt", .data = "bee\n" }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "c.txt", .data = "sea\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/b.txt\n", .{dir})); - p.sync(); - const b_serial = p.panes[p.active].?.serial; - // Two entries for a.txt, far apart in it. - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/a.txt:1\n", .{dir})); - p.sync(); - const a = p.active; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/a.txt:90\n", .{dir})); - p.sync(); - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/c.txt\n", .{dir})); - p.sync(); - const c_serial = p.panes[p.active].?.serial; - _ = wr(p, Node.of(p.panes[a].?.serial, .ctl), "Del\n"); - p.sync(); - var closed: usize = 0; - for (p.jumps[0..p.njumps]) |j| closed += @intFromBool(j.serial == 0); - try testing.expectEqual(@as(usize, 2), closed); - // Back reopens it at line 90: both its entries are that pane now. - p.jumpBy(-1); - p.sync(); - const re = p.panes[p.active].?; - try testing.expect(std.mem.endsWith(u8, re.file.?.path, "/a.txt")); - try testing.expectEqual(@as(i32, 89), re.body.cur_row); - for (p.jumps[0..p.njumps]) |j| try testing.expect(j.serial != 0); - // Back again goes to its line 1, in the same pane, then to b. - p.jumpBy(-1); - p.sync(); - try testing.expectEqual(re.serial, p.panes[p.active].?.serial); - try testing.expectEqual(@as(i32, 0), re.body.cur_row); - p.jumpBy(-1); - p.sync(); - try testing.expectEqual(b_serial, p.panes[p.active].?.serial); - // And Forward walks it all again, to c. - p.jumpBy(1); - p.sync(); - p.jumpBy(1); - p.sync(); - try testing.expectEqual(@as(i32, 89), re.body.cur_row); - p.jumpBy(1); - p.sync(); - try testing.expectEqual(c_serial, p.panes[p.active].?.serial); -} - -test "Back and Forward written to exec keep to the focused file, and JumpScope all, written to ctl, takes every place in order" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))) }); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "b.txt", .data = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))) }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - // Each a look, as a user's jumps are: b1 a1 b40 a40 b80 a80. - for ([_][]const u8{ "b.txt:1", "a.txt:1", "b.txt:40", "a.txt:40", "b.txt:80", "a.txt:80" }) |place| { - try testing.expectEqual(Status.ok, wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/{s}\n", .{ dir, place })).reply.status); - p.sync(); - } - const Step = struct { word: []const u8, file: []const u8, line: i32 }; - const walk = struct { - fn run(q: *Pardes, steps: []const Step) !void { - for (steps) |s| { - var cmd: [16]u8 = undefined; - try testing.expectEqual(Status.ok, wr(q, root_exec, try std.fmt.bufPrint(&cmd, "{s}\n", .{s.word})).reply.status); - q.sync(); - const pane = q.panes[q.active].?; - try testing.expect(std.mem.endsWith(u8, pane.file.?.path, s.file)); - try testing.expectEqual(s.line, pane.body.cur_row + 1); - } - } - }.run; - // a's places first, b only once a has none older; Forward mirrors it - // from b and ends at a80, where it began. - try walk(p, &.{ - .{ .word = "Back", .file = "/a.txt", .line = 40 }, - .{ .word = "Back", .file = "/a.txt", .line = 1 }, - .{ .word = "Back", .file = "/b.txt", .line = 1 }, - .{ .word = "Forward", .file = "/b.txt", .line = 40 }, - .{ .word = "Forward", .file = "/b.txt", .line = 80 }, - .{ .word = "Forward", .file = "/a.txt", .line = 80 }, - }); - try testing.expectEqual(Status.ok, wr(p, @backingInt(tree.TopFile.ctl), "JumpScope all\n").reply.status); - try testing.expect(std.mem.indexOf(u8, rd(p, @backingInt(tree.TopFile.ctl), 0, 8192).bytes, "JumpScope all\n") != null); - try walk(p, &.{ - .{ .word = "Back", .file = "/b.txt", .line = 80 }, - .{ .word = "Back", .file = "/a.txt", .line = 40 }, - .{ .word = "Forward", .file = "/b.txt", .line = 80 }, - }); -} - -test "Recent keeps only files on disk: not a name never saved, nor the served tree's" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "real.txt", .data = "real\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - const names = [_][]const u8{ try std.fmt.allocPrint(testing.allocator, "{s}/c", .{dir}), try testing.allocator.dupe(u8, "/virtual/zz"), try std.fmt.allocPrint(testing.allocator, "{s}/real.txt", .{dir}) }; - defer for (names) |n| testing.allocator.free(n); - for (names) |name| { - const made = try th.newPane(p); - _ = wr(p, Node.of(made, .name), try std.fmt.bufPrint(&line, "{s}\n", .{name})); - p.sync(); - _ = wr(p, Node.of(made, .ctl), "Del\n"); - _ = wr(p, Node.of(made, .ctl), "Del\n"); - p.sync(); - } - try testing.expect(p.recent.get(names[0]) == null); - try testing.expect(p.recent.get(names[1]) == null); - try testing.expect(p.recent.get(names[2]) != null); -} - test "a new file saved shows in /recent, open, while it is" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -2785,79 +2530,6 @@ test "/recent shows a name as /index does, a newline in it escaped" { try testing.expect(std.mem.indexOf(u8, text, "closed /tmp/a\\nb\\\\c\n") != null); } -test "Back over a closed file deleted since skips it, says so, and goes on to the place before" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - for ([_][]const u8{ "a.txt", "gone.txt", "c.txt" }) |name| try tmp.dir.writeFile(testing.io, .{ .sub_path = name, .data = "text\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/a.txt\n", .{dir})); - p.sync(); - const a_serial = p.panes[p.active].?.serial; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/gone.txt\n", .{dir})); - p.sync(); - const gone = p.active; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/c.txt\n", .{dir})); - p.sync(); - const c_id = p.active; - _ = wr(p, Node.of(p.panes[gone].?.serial, .ctl), "Del\n"); - p.sync(); - try tmp.dir.deleteFile(testing.io, "gone.txt"); - p.active = c_id; - p.jumpBy(-1); - p.sync(); - try testing.expectEqual(a_serial, p.panes[p.active].?.serial); - try testing.expect(std.mem.indexOf(u8, p.panes[c_id].?.msg[0..p.panes[c_id].?.msg_len], "gone.txt: deleted, skipped") != null); -} - -test "a remembered place is clamped to the file as it is now, its selection's two ends kept where they fit" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "c.txt", .data = "one\ntwo\nthree\nfour\nfive\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var line: [4200]u8 = undefined; - _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/c.txt:2\n", .{dir})); - p.sync(); - const c = p.panes[p.active].?; - // A selection over lines 2 to 4, then closed. - @import("pane.zig").setDot(c, .{ .q0 = 4, .q1 = 19 }); - try testing.expect(c.body.vsel.active); - _ = wr(p, Node.of(c.serial, .ctl), "Del\n"); - p.sync(); - // The file shrinks to two lines while it is closed. - try tmp.dir.writeFile(testing.io, .{ .sub_path = "c.txt", .data = "one\ntwo\n" }); - _ = wr(p, root_exec, "Recent\n"); - const rows = p.panes[p.paneBySerial(try std.fmt.parseInt(u32, std.mem.trimEnd(u8, rd(p, root_exec, 0, 64).bytes, "\n"), 10)).?].?; - const at = std.mem.indexOf(u8, rows.file.?.content, "c.txt:").?; - const row = rows.file.?.content[at..std.mem.indexOfScalarPos(u8, rows.file.?.content, at, '\n').?]; - // Both ends are in the row, a range. - try testing.expect(std.mem.indexOfScalar(u8, row, '-') != null); - var full: [4400]u8 = undefined; - const loc = try std.fmt.bufPrint(&full, "{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 testing.expectEqual(Status.ok, wr(p, Node.of(rows.serial, .look), loc).reply.status); - p.sync(); - const back = p.panes[p.active].?; - try testing.expect(std.mem.endsWith(u8, back.file.?.path, "/c.txt")); - try testing.expect(back.body.cur_row <= 1); - try testing.expect(back.body.vsel.active); - // Its ends clamped to the last character, `two`'s `o`, not its newline. - try testing.expect(back.body.cur_col <= 2 and back.body.vsel.col <= 2); - try testing.expect(std.mem.indexOf(u8, back.msg[0..back.msg_len], "c.txt has 2 lines now: at its last") != null); - // The same row with the file open: the same place, said the same way. - _ = wr(p, Node.of(rows.serial, .look), loc); - p.sync(); - try testing.expectEqual(back.serial, p.panes[p.active].?.serial); - try testing.expect(back.body.cur_row <= 1); - try testing.expect(std.mem.indexOf(u8, back.msg[0..back.msg_len], "at its last") != null); -} - test "size is monotonic: growing is never refused, and a size once taken is taken again" { const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 60 }); defer p.deinit(); 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}))); +} diff --git a/test/behaviour/settings.zig b/test/behaviour/settings.zig index f3ed57de..df70dbe6 100644 --- a/test/behaviour/settings.zig +++ b/test/behaviour/settings.zig @@ -132,3 +132,45 @@ test "settings: a setting this frontend cannot show says GUI-only, never run as try std.testing.expectEqualStrings("unknown control message \"Liftt\"", s.said); try std.testing.expect(has(try s.read("/ctl"), "\nDumpDir /")); } + +test "settings: Back and Forward written to exec keep to the focused file, and JumpScope all, written to ctl, takes every place in order" { + 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 = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))) }); + var buf: [4096]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + // Each a look, as a user's jumps are: b1 a1 b40 a40 b80 a80. + for ([_][]const u8{ "b.txt:1", "a.txt:1", "b.txt:40", "a.txt:40", "b.txt:80", "a.txt:80" }) |place| try s.write("/look", s.at("{s}/{s}\n", .{ dir, place })); + const Step = struct { []const u8, []const u8, u32 }; + const walk = struct { + fn run(q: *Session, steps: []const Step) !void { + for (steps) |st| { + try q.write("/exec", q.at("{s}\n", .{st[0]})); + const n = try q.focused(); + try std.testing.expect(std.mem.endsWith(u8, try q.read(q.at("/pane/{d}/name", .{n})), q.at("{s}\n", .{st[1]}))); + // Each line is `row\n`: the line the dot is on. + try std.testing.expectEqual(st[2] - 1, (try q.range(q.at("/pane/{d}/dot", .{n})))[0] / 4); + } + } + }.run; + // a's places first, b only once a has none older; Forward mirrors it + // from b and ends at a80, where it began. + try walk(&s, &.{ + .{ "Back", "/a.txt", 40 }, + .{ "Back", "/a.txt", 1 }, + .{ "Back", "/b.txt", 1 }, + .{ "Forward", "/b.txt", 40 }, + .{ "Forward", "/b.txt", 80 }, + .{ "Forward", "/a.txt", 80 }, + }); + try s.write("/ctl", "JumpScope all\n"); + try std.testing.expect(has(try s.read("/ctl"), "JumpScope all\n")); + try walk(&s, &.{ + .{ "Back", "/b.txt", 80 }, + .{ "Back", "/a.txt", 40 }, + .{ "Forward", "/b.txt", 80 }, + }); +} -- cgit v1.3 From 273cbd0df9d8edf4725fdff5f9da560b35b63058 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 10:52:01 -0300 Subject: test: src/pardes.zig's jump tests through the 9P tree alone, in test/behaviour/modes.zig (guide.typ's Modes, Ctrl-o and Ctrl-i): a jumplist made as a user makes one (each place a pane's dot moved there and the keyboard given it; A and B files, C a Zerox twin, O the +Help output; the starting shell deleted, its places with it), walked by Ctrl-o, Ctrl-i (Kctl i), SPC j o and SPC j i, and the Back and Forward builtins, read from /focus and the dot's line, each walk's end said on screen: JumpScope file keeping to the focused file and leaving it only once it runs out, Ctrl-i its mirror and retracing a run of Ctrl-o, JumpScope all in jump order (bare JumpScope file again), a pane with no file stepping globally, a file in two panes keeping to its own pane first, every way taking the same steps, a step dropping the selections, select mode and the extra cursors and landing on the cursor the place was left with (nothing further on leaving the selection), and hopping two panes by Esc, window mode and clicks making no places; the old test of a missing entry's skip kept trimmed (its entries injected), and the side-button and jumpBy one kept, /mousein having no side buttons; each checked to fail against a break (JumpScope file off, JumpScope all ignored, a pane's own places not first, an output pane keeping to itself, Forward always global, Forward stepping back, the hop merge off, the source's selection not cleared) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/pardes.zig | 155 ++------------------------------- test/behaviour/modes.zig | 219 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+), 146 deletions(-) (limited to 'src') diff --git a/src/pardes.zig b/src/pardes.zig index ba09b3b6..2b4fef70 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -8517,56 +8517,6 @@ test "a Rename the language server finds nothing for says so, and changes nothin try std.testing.expectEqualStrings("const value=1;\n", pane.file.?.content); } -test "hopping between two panes does not grow the jump stack" { - if (platform == .web) return; - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }); - p.settings.placement = .pardes; // two panes of room, not acme's fit to the doc's two lines - defer p.deinit(); - p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); - p.update(.{ .key = .{ .cp = 'n', .alt = true } }); // a shell under the doc - const doc = 0; - const shell = p.active; - try std.testing.expect(shell != doc); - const depth = p.njumps; - - // Esc is Last — the everyday two-pane hop. Each press used to - // append, so sixty-four of them left the stack remembering nothing else. - for (0..8) |_| { - const was = p.active; - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expect(p.active != was); - } - try std.testing.expectEqual(depth, p.njumps); - - // ...and so are the other two ways to switch panes. - for (0..8) |_| { - const want = if (p.active == doc) shell else doc; - const dir: u21 = if (p.active == doc) 'j' else 'k'; - p.update(.{ .key = .{ .cp = ' ' } }); - p.update(.{ .key = .{ .cp = 'w' } }); - p.update(.{ .key = .{ .cp = dir } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - try std.testing.expectEqual(want, p.active); - } - try std.testing.expectEqual(depth, p.njumps); - - for (0..8) |_| { - const other = if (p.active == doc) shell else doc; - const r = p.rects[other]; - p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = r.x + config.GUTTER + 1, .row = r.y + BOX_H + 1 } }); - p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = r.x + config.GUTTER + 1, .row = r.y + BOX_H + 1 } }); - try std.testing.expectEqual(other, p.active); - } - try std.testing.expectEqual(depth, p.njumps); - - const before = p.active; - exec.runBuiltin(p, .Back, p.active, "", null); - p.sync(); - try std.testing.expect(p.active != before); - try std.testing.expectEqual(depth, p.njumps); -} - /// Every `.write` a keystroke produced, joined — what the pane's program would /// actually have read off its pty. fn drainWrites(p: *Pardes, buf: []u8) []const u8 { @@ -8930,35 +8880,15 @@ test "board heap: every allocation failure during boot is a clean OutOfMemory" { ); } -test "jump history clears selections and lands at the recorded cursor" { +test "jump history skips an entry whose pane slot or serial is gone, leaving the selection" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); defer p.deinit(); const pane = try p.setTestFile("alpha beta\ngamma delta\nepsilon zeta\n"); p.njumps = 2; p.jcur = 1; - p.jumps[0] = .{ .pane = 0, .serial = pane.serial, .line = 3, .col = 8 }; p.jumps[1] = .{ .pane = 0, .serial = pane.serial, .line = 1, .col = 1 }; - for ([_]u21{ 'o', 'i' }, 0..) |key, index| { - pane.body.setRange(pane.file.?.content, 0, .{ .anchor = 1, .head = 5 }, true); - pane.sel[0] = .{ .state = .done, .c0 = 2, .c1 = 5, .r0 = 1, .r1 = 1 }; - try edit.capturePointerSelection(p, pane, 0); - pane.body.select = true; - p.update(.{ .key = .{ .cp = key, .ctrl = true } }); - try std.testing.expectEqual(index, p.jcur); - try std.testing.expectEqual(@as(usize, 2), p.njumps); - try std.testing.expectEqual(@as(u32, 3), p.jumps[0].line); - try std.testing.expect(!pane.body.vsel.active and !pane.body.msel.active and !pane.body.select); - try std.testing.expectEqual(@as(u16, 0), pane.body.nsel); - try std.testing.expectEqual(.none, pane.sel[0].state); - try std.testing.expect(pane.pointer_selections[0] == null); - try std.testing.expectEqual(@as(i32, if (index == 0) 2 else 0), pane.body.cur_row); - try std.testing.expectEqual(@as(i32, if (index == 0) 7 else 0), pane.body.cur_col); - } pane.body.setRange(pane.file.?.content, 0, .{ .anchor = 1, .head = 5 }, true); const selected = pane.body.primaryRange(pane.file.?.content, 0); - p.jumpBy(1); // No forward entry: selection stays untouched. - try std.testing.expectEqualDeep(selected, pane.body.primaryRange(pane.file.?.content, 0)); - try std.testing.expect(std.mem.indexOf(u8, p.panes[p.active].?.msg[0..p.panes[p.active].?.msg_len], "nothing further on") != null); for ([_]Loc{ .{ .pane = 15, .serial = pane.serial, .line = 1, .col = 1 }, .{ .pane = 0, .serial = pane.serial + 1, .line = 1, .col = 1 }, @@ -8970,7 +8900,7 @@ test "jump history clears selections and lands at the recorded cursor" { } } -/// A session for the JumpScope tests: four panes of 100 lines in one +/// A session for the side-button jump test: four panes of 100 lines in one /// column, A (pane 0) and B each on a file of its own, C on A's file, and O /// an output buffer, no file behind it. `order` is the jumplist, a letter a /// place, each letter's n-th place its line 10 + 40n: "ABABA" is A10 B10 A50 @@ -8980,7 +8910,7 @@ const JumpsTest = struct { ids: [4]usize = .{ 0, 0, 0, 0 }, const letters = "ABCO"; - const Way = enum { call, key, builtin, mouse }; + const Way = enum { call, mouse }; fn init(order: []const u8) !JumpsTest { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 80 }); @@ -9021,14 +8951,12 @@ const JumpsTest = struct { return t; } - /// One step `delta` through `way`: the function, Ctrl-o or Ctrl-i, the - /// mouse's side button pressed and let go, or the builtin's word. + /// One step `delta` through `way`: the function, or the mouse's side + /// button pressed and let go. fn step(t: JumpsTest, way: Way, delta: i32) void { const back = delta < 0; switch (way) { .call => t.p.jumpBy(delta), - .key => t.p.update(.{ .key = .{ .cp = if (back) 'o' else 'i', .ctrl = true } }), - .builtin => _ = t.p.executeBuiltinLine(t.p.active, if (back) "Back" else "Forward"), .mouse => for ([_]Mouse.Kind{ .press, .release }) |kind| t.p.update(.{ .mouse = .{ .button = if (back) .back else .forward, .kind = kind, .col = 0, .row = 0 } }), } @@ -9054,75 +8982,10 @@ const JumpsTest = struct { } }; -test "JumpScope file: Back keeps to the focused file, A50 then A10, and leaves it only once it runs out that way" { - const t = try JumpsTest.init("BABABA"); - defer t.p.deinit(); - try std.testing.expectEqual(config.Runtime.JumpScope.file, t.p.settings.jump_scope); - var buf: [128]u8 = undefined; - // B90, B50 passed over, kept in their places: A has nothing older than - // A10, so the step after it is the nearest place of any file, B10. - try std.testing.expectEqualStrings("A50 A10 B10", try t.walk(.call, -1, &buf)); - try std.testing.expectEqual(@as(usize, 6), t.p.njumps); - // Forward is the mirror, from B10 in B: B's own places first, then A90, - // the newest, where the Backs started. - try std.testing.expectEqualStrings("B50 B90 A90", try t.walk(.call, 1, &buf)); - try std.testing.expectEqual(@as(usize, 5), t.p.jcur); -} - -test "JumpScope file: Forward retraces a run of Backs inside one file step for step, back to where it began" { - const t = try JumpsTest.init("ABABA"); - defer t.p.deinit(); - var buf: [128]u8 = undefined; - // A1 B1 A2 B2 A3 from A3: nothing older than A1 at all, so Back ends there. - try std.testing.expectEqualStrings("A50 A10", try t.walk(.call, -1, &buf)); - try std.testing.expectEqual(@as(usize, 0), t.p.jcur); - try std.testing.expectEqualStrings("A50 A90", try t.walk(.call, 1, &buf)); - try std.testing.expectEqual(@as(usize, 4), t.p.jcur); - // One Back, one Forward: the same entry, whichever were passed over. - t.step(.call, -1); - try std.testing.expectEqual(@as(usize, 2), t.p.jcur); - t.step(.call, 1); - try std.testing.expectEqual(@as(usize, 4), t.p.jcur); - try std.testing.expectEqual(@as(usize, 5), t.p.njumps); -} - -test "JumpScope all: Back and Forward take every place in the order it was jumped to" { - const t = try JumpsTest.init("ABABA"); - defer t.p.deinit(); - try std.testing.expect(t.p.executeBuiltinLine(t.p.active, "JumpScope all")); - try std.testing.expectEqual(config.Runtime.JumpScope.all, t.p.settings.jump_scope); - var buf: [128]u8 = undefined; - try std.testing.expectEqualStrings("B50 A50 B10 A10", try t.walk(.call, -1, &buf)); - try std.testing.expectEqualStrings("B10 A50 B50 A90", try t.walk(.call, 1, &buf)); - // Bare, the word steps back to file. - try std.testing.expect(t.p.executeBuiltinLine(t.p.active, "JumpScope")); - try std.testing.expectEqual(config.Runtime.JumpScope.file, t.p.settings.jump_scope); -} - -test "JumpScope file: from a pane with no file, Back and Forward are global" { - const t = try JumpsTest.init("OAOBO"); - defer t.p.deinit(); - var buf: [128]u8 = undefined; - // O90 keeps to nothing: B10, the next older of all. From B, nothing of - // B is older, so O50; from O, A10; from A, O10. - try std.testing.expectEqualStrings("B10 O50 A10 O10", try t.walk(.call, -1, &buf)); - try std.testing.expectEqualStrings("A10 O50 B10 O90", try t.walk(.call, 1, &buf)); -} - -test "JumpScope file: a file in two panes keeps to the pane first, then the file in the other pane" { - // C is A's file in a second pane: C10 B10 A10 C50 A50. - const t = try JumpsTest.init("CBACA"); - defer t.p.deinit(); - var buf: [128]u8 = undefined; - // From A50: A10 in this pane, over the nearer C50; then none of A's - // own is older, so the file's in C, C10, over B10. - try std.testing.expectEqualStrings("A10 C10", try t.walk(.call, -1, &buf)); - // From C10 in C: C50, its own, then the file's newest, A50. - try std.testing.expectEqualStrings("C50 A50", try t.walk(.call, 1, &buf)); -} - -test "Back and Forward take the same steps from Ctrl-o and Ctrl-i, the mouse's side buttons, the builtins and jumpBy" { - for ([_]JumpsTest.Way{ .call, .key, .builtin, .mouse }) |way| { +// The keys and builtins are test/behaviour/modes.zig's; a mouse's side +// buttons have no /mousein bits. +test "Back and Forward take the same steps from the mouse's side buttons and jumpBy" { + for ([_]JumpsTest.Way{ .call, .mouse }) |way| { const t = try JumpsTest.init("BABABA"); defer t.p.deinit(); var buf: [128]u8 = undefined; diff --git a/test/behaviour/modes.zig b/test/behaviour/modes.zig index f29a4b56..b1641c6f 100644 --- a/test/behaviour/modes.zig +++ b/test/behaviour/modes.zig @@ -259,3 +259,222 @@ test "modes: window keys walk from a pane up through its column's tag and the wo try expectEqual((try s.where("body"))[1], (try s.cursor()).?[1]); try s.expect(s.at("/pane/{d}/body", .{a}), "body\n"); } + +/// The jump tests' session: a pane a letter of `order`, A and B each a file +/// of 100 lines, C a Zerox of A, O the +Help output (no file behind it), +/// and the jumplist `order`, a letter a place, each letter's n-th place its +/// line 1 + 40n ("ABABA" is A1 B1 A41 B41 A81): each made by moving its +/// pane's dot there and giving it the keyboard, as a jump does (a new +/// pane's first, where it opens). The shell the session started with is +/// closed after, its places with it. +const Jumps = struct { + s: Session, + ids: [4]u32 = @splat(0), + + const letters = "ABCO"; + + fn init(order: []const u8) !Jumps { + var t: Jumps = .{ .s = try Session.initTerm(std.testing.allocator) }; + errdefer t.s.deinit(); + const s = &t.s; + const shell = try s.focused(); + var seen: [4]u32 = @splat(0); + for (order) |letter| { + const which = std.mem.indexOfScalar(u8, letters, letter).?; + if (t.ids[which] == 0) t.ids[which] = switch (letter) { + 'O' => o: { + try s.write(s.at("/pane/{d}/exec", .{shell}), "Help\n"); + break :o try s.focused(); + }, + else => made: { + // C is A's twin, and A C's when C came first. + const twin = t.ids[if (letter == 'A') 2 else if (letter == 'C') 0 else 1]; + if (letter != 'B' and twin != 0) { + try s.write(s.at("/pane/{d}/exec", .{twin}), "Zerox\n"); + break :made try s.focused(); + } + const n = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{n}), if (letter == 'B') "/b.txt\n" else "/a.txt\n"); + try s.write(s.at("/pane/{d}/body", .{n}), std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*)))); + break :made n; + }, + }; + const n = t.ids[which]; + try s.write(s.at("/pane/{d}/addr", .{n}), s.at("{d}\n", .{1 + 40 * seen[which]})); + try s.write(s.at("/pane/{d}/ctl", .{n}), "dot=addr\n"); + try s.write("/focus", s.at("{d}\n", .{n})); + seen[which] += 1; + } + try s.write(s.at("/pane/{d}/ctl", .{shell}), "delete\n"); + return t; + } + + /// Where the keyboard is: its pane's letter and the dot's line, `A50`. + fn here(t: *Jumps) ![]const u8 { + const s = &t.s; + const n = try s.focused(); + const at, _ = try s.range(s.at("/pane/{d}/dot", .{n})); + const body = try s.read(s.at("/pane/{d}/body", .{n})); + const which = std.mem.indexOfScalar(u32, &t.ids, n).?; + return s.at("{c}{d}", .{ letters[which], std.mem.count(u8, body[0..at], "\n") + 1 }); + } + + const Way = enum { key, leader, builtin }; + + /// Steps back or on until the list ends that way, as it says; the + /// places landed on, "A50 A10 B10". + fn walk(t: *Jumps, way: Way, back: bool) ![]const u8 { + const s = &t.s; + var out: []const u8 = ""; + for (0..64) |_| { + const before = try t.here(); + switch (way) { + .key => try s.keys(if (back) "\x0f" else "\u{F017}i"), + .leader => try s.keys(if (back) " jo" else " ji"), + .builtin => try s.write("/exec", if (back) "Back\n" else "Forward\n"), + } + const now = try t.here(); + if (std.mem.eql(u8, before, now)) break; + out = if (out.len == 0) now else s.at("{s} {s}", .{ out, now }); + } else return error.NoEnd; + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), if (back) "nothing further back" else "nothing further on") != null); + return out; + } +}; + +test "modes: Ctrl-o keeps to the focused file (JumpScope file), A41 then A1, and leaves it only once it runs out that way; Ctrl-i mirrors it" { + var t = try Jumps.init("BABABA"); + defer t.s.deinit(); + try std.testing.expect(std.mem.indexOf(u8, try t.s.read("/ctl"), "\nJumpScope file\n") != null); + // B81, B41 passed over, kept in their places: A has nothing older than + // A1, so the step after it is the nearest place of any file, B1. + try std.testing.expectEqualStrings("A41 A1 B1", try t.walk(.key, true)); + // Forward is the mirror, from B1 in B: B's own places first, then A81, + // the newest, where the Backs started. + try std.testing.expectEqualStrings("B41 B81 A81", try t.walk(.key, false)); +} + +test "modes: Ctrl-i retraces a run of Ctrl-o inside one file step for step, back to where it began" { + var t = try Jumps.init("ABABA"); + defer t.s.deinit(); + // A1 B1 A41 B41 A81 from A81: nothing older than A1 at all. + try std.testing.expectEqualStrings("A41 A1", try t.walk(.key, true)); + try std.testing.expectEqualStrings("A41 A81", try t.walk(.key, false)); + // One back, one on: the same place, whichever were passed over. + try t.s.keys("\x0f"); + try std.testing.expectEqualStrings("A41", try t.here()); + try t.s.keys("\u{F017}i"); + try std.testing.expectEqualStrings("A81", try t.here()); + // And the steps made no places of their own. + try std.testing.expectEqualStrings("A41 A1", try t.walk(.key, true)); +} + +test "modes: with JumpScope all, Ctrl-o and Ctrl-i take every place in the order it was jumped to, and bare JumpScope is file again" { + var t = try Jumps.init("ABABA"); + defer t.s.deinit(); + try t.s.write("/ctl", "JumpScope all\n"); + try std.testing.expectEqualStrings("B41 A41 B1 A1", try t.walk(.key, true)); + try std.testing.expectEqualStrings("B1 A41 B41 A81", try t.walk(.key, false)); + try t.s.write("/ctl", "JumpScope\n"); + try std.testing.expect(std.mem.indexOf(u8, try t.s.read("/ctl"), "\nJumpScope file\n") != null); +} + +test "modes: from a pane with no file, Ctrl-o and Ctrl-i step through every place" { + var t = try Jumps.init("OAOBO"); + defer t.s.deinit(); + // O81 keeps to nothing: B1, the next older of all. From B, nothing of + // B is older, so O41; from O, A1; from A, O1. + try std.testing.expectEqualStrings("B1 O41 A1 O1", try t.walk(.key, true)); + try std.testing.expectEqualStrings("A1 O41 B1 O81", try t.walk(.key, false)); +} + +test "modes: Ctrl-o in a file shown in two panes keeps to its own pane first, then the file in the other" { + // C is A's file in a second pane: C1 B1 A1 C41 A41. + var t = try Jumps.init("CBACA"); + defer t.s.deinit(); + // From A41: A1 in this pane, over the nearer C41; then none of A's own + // is older, so the file's in C, C1, over B1. + try std.testing.expectEqualStrings("A1 C1", try t.walk(.key, true)); + // From C1 in C: C41, its own, then the file's newest, A41. + try std.testing.expectEqualStrings("C41 A41", try t.walk(.key, false)); +} + +test "modes: Back and Forward take the same steps from Ctrl-o and Ctrl-i, SPC j o and SPC j i, and the builtins" { + for ([_]Jumps.Way{ .key, .leader, .builtin }) |way| { + var t = try Jumps.init("BABABA"); + defer t.s.deinit(); + try std.testing.expectEqualStrings("A41 A1 B1", try t.walk(way, true)); + try std.testing.expectEqualStrings("B41 B81 A81", try t.walk(way, false)); + } +} + +test "modes: Ctrl-o and Ctrl-i drop the selections, select mode and the extra cursors, and land where the place was left" { + const rows = std.mem.asBytes(&@as([97][4]u8, @splat("row\n".*))); + var s = try Session.initFile(std.testing.allocator, "alpha beta\ngamma delta\nepsilon zeta\n" ++ rows); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + // A place on line 3, at its eighth column (byte 30), then line 60. + try s.write(s.at("/pane/{d}/addr", .{n}), "#30\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "dot=addr\n"); + try s.keys("60G"); + // Each step lands on the cursor the place was left with. + var back_to: u32 = 30; + for ([_][]const u8{ "\x0f", "\u{F017}i" }) |key| { + // A line selected, a second cursor under it, select mode on. + try s.keys("xCv"); + try std.testing.expectEqualStrings("select", try mode(&s, n)); + const leaving = (try s.range(dot))[1] - 1; // the cursor: its last character + try s.keys(key); + try std.testing.expectEqualStrings("normal", try mode(&s, n)); + try std.testing.expectEqual(back_to, (try s.range(dot))[0]); + back_to = leaving; + try s.expect(s.at("/pane/{d}/sel", .{n}), ""); + // One cursor: a Z typed goes in once. + try s.keys("iZ\x1b"); + try expectEqual(@as(usize, 1), std.mem.count(u8, try s.read(s.at("/pane/{d}/body", .{n})), "Z")); + try s.keys("u"); + } + // No place further on: said, and the selection left as it was. + try s.keys("x"); + const sel = try s.read(s.at("/pane/{d}/sel", .{n})); + try s.keys("\u{F017}i"); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), "nothing further on") != null); + try s.expect(s.at("/pane/{d}/sel", .{n}), sel); +} + +test "modes: hopping between two panes by Esc, window mode or a click makes no places, so one Ctrl-o goes back across and the next has nowhere to go" { + var s = try Session.initFile(std.testing.allocator, std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*)))); + defer s.deinit(); + // Two panes of room, not acme's fit to the doc. + try s.write("/ctl", "Placement pardes\n"); + const doc = try s.focused(); + try s.keys("\u{F015}n"); // a shell under the doc + const shell = try s.focused(); + if (std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/ctl", .{shell})), "mode:tty") != null) try s.keys("\x02"); + for (0..8) |_| { + const was = try s.focused(); + try s.keys("\x1b"); + try expectEqual(if (was == doc) shell else doc, try s.focused()); + } + for (0..8) |_| { + const want = if (try s.focused() == doc) shell else doc; + try s.keys(if (want == shell) " wj\n" else " wk\n"); + try expectEqual(want, try s.focused()); + } + const doc_row = (try s.where("row"))[1]; + const shell_row = (try s.where("Tty Save Mode"))[1] + 1; + for (0..8) |_| { + const other = if (try s.focused() == doc) shell else doc; + const row = if (other == doc) doc_row else shell_row; + try s.write("/mousein", s.at("m 10 {d} 1\nm 10 {d} 0\n", .{ row, row })); + try expectEqual(other, try s.focused()); + } + const before = try s.focused(); + try s.keys("\x0f"); + try std.testing.expect(before != try s.focused()); + const after = try s.focused(); + try s.keys("\x0f"); + try expectEqual(after, try s.focused()); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), "nothing further back") != null); +} -- cgit v1.3 From fa3031d6ded24d4ba7ab5bab21e17387e48306aa Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 11:05:00 -0300 Subject: test: src/pardes.zig's two acme-placement tests through the 9P tree alone, in test/behaviour/placement.zig: a look from a pane's text going under its three lines when most of it is blank, and, both panes full, halving the biggest (the new pane after it in /layout, its tag at the old pane's middle row, its bottom the old pane's); a look from the middle of a column (its top and bottom panes dragged by their boxes to two and three rows, under a placement's minimum) splitting only the pane it came from, the short ones keeping their rows, through a Newcol too; geometry read from where /screen draws each tag, on a 100x60 screen set by size; each checked to fail against a break (no placement under the text, the biggest split at a third, every pane in the column reweighted at a split) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/pardes.zig | 73 ------------------------------------ test/behaviour/placement.zig | 88 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 73 deletions(-) (limited to 'src') diff --git a/src/pardes.zig b/src/pardes.zig index 2b4fef70..f78c2c0d 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -1195,79 +1195,6 @@ test "first document keeps its originating shell and only opens a column when it } } -test "acme placement: under a pane's text when it has many blank rows, else halving the biggest" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 60 }); - defer p.deinit(); - const top = p.active; - _ = try p.setTestFile("one\ntwo\nthree\n"); - p.sync(); - p.setActiveColumn(0); - // Most of the pane is blank: a document looked from it starts under its - // three lines (makenewwindow, util.c:482-486). - const second = p.freeSlot().?; - _ = try p.openTutorView(second); - exec.placeNew(p, top, top, second, .doc); - p.sync(); - const text_end = p.bodyTop(p.panes[top].?, p.rects[top]) + 3; - try std.testing.expectEqual(text_end, p.rects[second].y); - // Fill both with text: no blank space is big, so the biggest pane is - // halved. - const full = std.mem.asBytes(&@as([80][2]u8, @splat("x\n".*))); - for ([_]usize{ top, second }) |id| panes.File.setContent(p, &p.panes[id].?.file.?, try p.gpa.dupe(u8, full)); - p.sync(); - const big = if (p.rects[second].h >= p.rects[top].h) second else top; - const was = p.rects[big]; - const third = p.freeSlot().?; - _ = try p.openTutorView(third); - exec.placeNew(p, second, second, third, .doc); - p.sync(); - try std.testing.expectEqual(big, p.col_panes[0][layout.findPane(p, third).?.idx - 1]); - try std.testing.expectEqual(was.y + was.h / 2, p.rects[third].y); - try std.testing.expectEqual(was.y + was.h, p.rects[third].y + p.rects[third].h); -} - -test "acme placement: a pane made from the middle of a column moves only the one it splits, short ones round it kept short" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 60 }); - defer p.deinit(); - const top = p.active; - _ = try p.setTestFile("one\n"); - p.sync(); - p.setActiveColumn(0); - const middle = p.freeSlot().?; - _ = try p.openTutorView(middle); - exec.placeNew(p, top, top, middle, .doc); - const bottom = p.freeSlot().?; - _ = try p.openTutorView(bottom); - exec.placeNew(p, middle, middle, bottom, .doc); - p.sync(); - // A short pane above and below, dragged under a placement's minimum, - // and the middle one full of text, so it is the one halved. - const full = std.mem.asBytes(&@as([80][2]u8, @splat("x\n".*))); - for ([_]usize{ top, middle, bottom }) |id| panes.File.setContent(p, &p.panes[id].?.file.?, try p.gpa.dupe(u8, full)); - try std.testing.expectEqualSlices(usize, &.{ top, middle, bottom }, p.col_panes[0][0..3]); - const total = p.rects[top].h + p.rects[middle].h + p.rects[bottom].h; - p.panes[top].?.vweight = 2; - p.panes[bottom].?.vweight = 2; - p.panes[middle].?.vweight = @floatFromInt(total - 4); - p.sync(); - const was_top = p.rects[top]; - const was_bottom = p.rects[bottom]; - try std.testing.expect(was_top.h < layout.minRows(p, top)); - const made = p.freeSlot().?; - _ = try p.openTutorView(made); - exec.placeNew(p, middle, middle, made, .doc); - p.sync(); - try std.testing.expectEqual(layout.findPane(p, middle).?.idx + 1, layout.findPane(p, made).?.idx); - try std.testing.expectEqual(was_top, p.rects[top]); - try std.testing.expectEqual(was_bottom, p.rects[bottom]); - // A new column narrows this one, and the short panes keep their rows. - try std.testing.expect(p.executeBuiltinLine(made, "Newcol")); - p.sync(); - try std.testing.expectEqual(@as(usize, 2), p.ncol); - try std.testing.expectEqual(was_top.h, p.rects[top].h); - try std.testing.expectEqual(was_bottom.h, p.rects[bottom].h); -} - test "an empty column shows its theme's empty_col under its tag: acme's white, else the border" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 20 }); defer p.deinit(); diff --git a/test/behaviour/placement.zig b/test/behaviour/placement.zig index 079b5a5e..5b2d55c7 100644 --- a/test/behaviour/placement.zig +++ b/test/behaviour/placement.zig @@ -59,3 +59,91 @@ test "placement: Placement pardes puts a new pane right under the asking one, an try s.write("/ctl", "Placement\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPlacement acme\n") != null); } + +/// Each pane's tag row and its rows down to the next tag, or the screen's +/// end, as /screen draws them; `names` top to bottom in one column. +fn spans(s: *Session, comptime n: usize, names: [n][]const u8) ![n][2]usize { + var out: [n][2]usize = undefined; + for (names, 0..) |name, i| out[i][0] = try tagRow(s, name); + const rows = std.mem.count(u8, try s.screen(), "\n") + 1; + for (0..n) |i| out[i][1] = (if (i + 1 < n) out[i + 1][0] else rows) - out[i][0]; + return out; +} + +test "placement: a look from a pane's text goes under its text when it has many blank rows, else halves the biggest pane" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + for ([_][]const u8{ "second.txt", "third.txt" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "x\n" }); + var dir: [4096]u8 = undefined; + const at = dir[0..try tmp.dir.realPath(std.testing.io, &dir)]; + var s = try Session.initFile(std.testing.allocator, "one\ntwo\nthree\n"); + defer s.deinit(); + s.detach(); + try s.write("/ctl", "size 100 60\n"); + const top = try s.focused(); + // Most of the pane is blank: the look starts under its three lines + // (makenewwindow, util.c:482-486). + try s.write(s.at("/pane/{d}/look", .{top}), s.at("{s}/second.txt\n", .{at})); + const second = try s.focused(); + const shown = s.at("{s}/second.txt", .{at}); + try std.testing.expectEqual((try tagRow(&s, "/test.txt")) + 1 + 3, try tagRow(&s, shown)); + // Both full of text: no blank space is big, so the biggest is halved. + const full = std.mem.asBytes(&@as([80][2]u8, @splat("x\n".*))); + for ([_]u32{ top, second }) |n| try s.replace(s.at("/pane/{d}/body", .{n}), full); + const was = try spans(&s, 2, .{ "/test.txt", shown }); + const big: usize = if (was[1][1] >= was[0][1]) 1 else 0; + try s.write(s.at("/pane/{d}/look", .{second}), s.at("{s}/third.txt\n", .{at})); + const third = try s.focused(); + const ids = [_]u32{ top, second }; + try std.testing.expect(std.mem.indexOf(u8, try s.columns(), s.at("{d},{d}", .{ ids[big], third })) != null); + const made = s.at("{s}/third.txt", .{at}); + const now = if (big == 1) try spans(&s, 3, .{ "/test.txt", shown, made }) else try spans(&s, 3, .{ "/test.txt", made, shown }); + try std.testing.expectEqual(was[big][0] + was[big][1] / 2, now[big + 1][0]); + try std.testing.expectEqual(was[big][0] + was[big][1], now[big + 1][0] + now[big + 1][1]); +} + +test "placement: a look from the middle of a column splits only the pane it came from, short ones round it kept short, through a Newcol too" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + for ([_][]const u8{ "mid", "low", "made" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "x\n" }); + var dir: [4096]u8 = undefined; + const at = dir[0..try tmp.dir.realPath(std.testing.io, &dir)]; + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + s.detach(); + try s.write("/ctl", "size 100 60\n"); + const top = try s.focused(); + try s.write(s.at("/pane/{d}/look", .{top}), s.at("{s}/mid\n", .{at})); + const mid = try s.focused(); + try s.write(s.at("/pane/{d}/look", .{mid}), s.at("{s}/low\n", .{at})); + const low = try s.focused(); + const full = std.mem.asBytes(&@as([80][2]u8, @splat("x\n".*))); + for ([_]u32{ top, mid, low }) |n| try s.replace(s.at("/pane/{d}/body", .{n}), full); + // Short names, so a narrowed column draws each tag on one row. + try s.write(s.at("/pane/{d}/name", .{mid}), "/mid\n"); + try s.write(s.at("/pane/{d}/name", .{low}), "/low\n"); + const names = [_][]const u8{ "/test.txt", "/mid", "/low", "/made" }; + // The top and bottom panes dragged by their boxes to two and three + // rows, under a placement's minimum; the middle one full of text, so + // the one halved. + const rows = std.mem.count(u8, try s.screen(), "\n") + 1; + const r = try spans(&s, 3, names[0..3].*); + try s.write("/mousein", s.at("m 0 {d} 1\nm 0 {d} 1\nm 0 {d} 0\n", .{ r[1][0], r[0][0] + 2, r[0][0] + 2 })); + try s.write("/mousein", s.at("m 0 {d} 1\nm 0 {d} 1\nm 0 {d} 0\n", .{ r[2][0], rows - 3, rows - 3 })); + const was = try spans(&s, 3, names[0..3].*); + try std.testing.expectEqual(@as(usize, 2), was[0][1]); + try std.testing.expectEqual(@as(usize, 3), was[2][1]); + try s.write(s.at("/pane/{d}/look", .{mid}), s.at("{s}/made\n", .{at})); + const made = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{made}), "/made\n"); + try std.testing.expectEqualStrings(s.at("{d}:{d},{d},{d},{d}", .{ try s.column(0), top, mid, made, low }), try s.columns()); + const now = try spans(&s, 4, .{ names[0], names[1], names[3], names[2] }); + try std.testing.expectEqual(was[0], now[0]); + try std.testing.expectEqual(was[2], now[3]); + // A new column narrows this one, and the short panes keep their rows. + try s.write(s.at("/pane/{d}/exec", .{made}), "Newcol\n"); + try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, try s.columns(), ":")); + const narrowed = try spans(&s, 4, .{ names[0], names[1], names[3], names[2] }); + try std.testing.expectEqual(was[0][1], narrowed[0][1]); + try std.testing.expectEqual(was[2][1], narrowed[3][1]); +} -- 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 'src') 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