diff options
30 files changed, 1790 insertions, 1879 deletions
@@ -1880,6 +1880,9 @@ pub fn build(b: *std.Build) void { .link_libc = true, }); behaviour_module.addImport("session", session_module); + // The build's options alone (platform), never the core: a test of a + // window's word knows which build it runs in. + behaviour_module.addImport("pardes_config", core_module.import_table.get("pardes_config").?); const behaviour_tests = b.addTest(.{ .root_module = behaviour_module, .filters = test_filters }); if (test_nonce) |nonce| behaviour_tests.root_module.addImport("test_rebuild_nonce", nonce); test_build.dependOn(&behaviour_tests.step); diff --git a/docs/next-tests.md b/docs/next-tests.md index 9e234dfa..3f85e030 100644 --- a/docs/next-tests.md +++ b/docs/next-tests.md @@ -101,20 +101,38 @@ What the M tests reach into, and the public surface for it: ## Progress -Left to migrate, a file a row (`grep -c '^test "' <file>` before and -after; M as the inventory classed them): +After the first fan-out (2026-11-03: four migrators, each followed by a +reviewer that ran its own breaks; 108 tests in test/behaviour/): -| file | M at the start | moved | checked against | +| area | moved | M left | X kept | |---|---|---|---| -| src/ninep/ctl.zig | 82 | 9, 1 X kept | `collapsed ` left off the ctl read; Putall saving nothing; a look's blanks trimmed; size taking a screen too small | +| src/ninep/ctl.zig, pane.zig | 38 (9 by hand) | about 72 | 6 | +| test/panes.zig | 30 | 15 | 16 | +| src/pardes.zig | 28 | 17 | 36 | +| test/pdf.zig, test/output.zig | 14 | 28 | 13 | -Migrated so far, in test/behaviour/: other_files (kbdin, mousein), panes -(collapsed, name's leading blank, Putall), edit (an Edit block to a pane's -ctl, the root's and exec, one left open at its clunk an err in the log), -look_and_exec (a look's leading blanks, a bare :N), columns_and_tags (a -look after Newcol), root_ctl (size). X, left where it is: a failure said -while a 9P write waits (it checks the 9P server's own flags). +What the M left need, most first: +- **Effects performed:** Session counts a save and drops it; a save + record, /recent after a save, and a terminal's pty bytes come only once + a host performs the effect. A Session option that performs saves (and + keeps pty writes) unlocks a group in each area. +- **A real host (T2):** terminals' output and shells, command panes + finishing, Tty and pty/ctl exec, a missing interpreter. +- **Image panes:** they need the decoder worker Session does not start. +- **LSP:** a fake language server. +- **Not reached yet:** jumps and Back/Forward, Zerox, Get, Restore, + DumpConfig, ThemeFile, and name edge cases. +Traps the migrators found in Session, each now in its tests' code: +- **It never ticks the core,** so a record a step makes (a pane's `new`) + shows in /log only at the next request. +- **A last line with no newline is held until the clunk;** its failure + goes to /log only, and the write reports success. End ctl lines with + `\n`. +- **`write` appends (`>>`); `replace` truncates first (`>`).** +- **`read` sets no `said`.** + +## Phases ## Phases 0. Inventory. Done, above. diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index cc300476..bd460098 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -1415,22 +1415,6 @@ const root_status = @backingInt(tree.TopFile.status); const root_look = @backingInt(tree.TopFile.look); const root_exec = @backingInt(tree.TopFile.exec); -test "a control character in a write to any ctl refuses the whole write, and a quoted line shows a tab as a blank" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const col = layout.columnSerial(p, 0); - for ([_]u64{ @backingInt(tree.TopFile.ctl), Node.of(serialOf(p), .ctl), Node.ofCol(col, .ctl) }) |node| { - const r = wr(p, node, "Wrap off\nbo\x01gus\n"); - try testing.expectEqual(E.INVAL, r.errno()); - try testing.expectEqualStrings(e_control, r.reply.ename); - } - const quoted = wr(p, Node.of(serialOf(p), .ctl), "bogus\tword\n"); - try testing.expectEqual(E.INVAL, quoted.errno()); - // The offending word alone is quoted: a tab ends it. - try testing.expect(std.mem.indexOf(u8, quoted.reply.ename, "\"bogus\"") != null); - try testing.expect(std.mem.indexOfScalar(u8, quoted.reply.ename, '\t') == null); -} - test "pane ctl read is acme's fields -- index's five, width in cells, font, tab width, undo, redo -- then whether it is current" { const gpa = testing.allocator; const p = try withFile(gpa, "x\n"); @@ -1503,189 +1487,6 @@ test "Undo and Redo on a pane's ctl step its body through its edits" { try testing.expect(th.logHas(p, "Undo: nothing to undo (x4)\n")); } -test "focus reads the pane with the keyboard, and a serial written to it moves the keyboard there" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const first = serialOf(p); - const second = try th.newPane(p); - const focus = @backingInt(tree.TopFile.focus); - var line: [16]u8 = undefined; - try testing.expectEqualStrings(try std.fmt.bufPrint(&line, "{d}\n", .{second}), rd(p, focus, 0, 64).bytes); - try testing.expectEqual(@as(u64, rd(p, focus, 0, 64).bytes.len), call(p, .{ .tag = 1, .op = .getattr, .node = focus }).reply.attr.size); - try testing.expect(std.mem.indexOf(u8, rd(p, Node.of(first, .ctl), 0, 4096).bytes, " notcurrent ") != null); - - // A header has the keyboard: no pane does, and a write takes it back. - tagline.enterHeader(p, null); - try testing.expectEqualStrings("", rd(p, focus, 0, 64).bytes); - try testing.expectEqual(@as(u64, 0), call(p, .{ .tag = 1, .op = .getattr, .node = focus }).reply.attr.size); - try testing.expect(std.mem.indexOf(u8, rd(p, Node.of(second, .ctl), 0, 4096).bytes, " notcurrent ") != null); - try testing.expectEqual(Status.ok, wr(p, focus, try std.fmt.bufPrint(&line, "{d}\n", .{first})).reply.status); - try testing.expect(!p.header_focus); - try testing.expectEqual(p.paneBySerial(first).?, p.active); - try testing.expect(std.mem.indexOf(u8, rd(p, Node.of(first, .ctl), 0, 4096).bytes, " current ") != null); - try testing.expect(std.mem.indexOf(u8, rd(p, Node.of(second, .ctl), 0, 4096).bytes, " notcurrent ") != null); - - try testing.expectEqualStrings(e_no_pane, wr(p, focus, "99999\n").reply.ename); - try testing.expectEqual(E.NOENT, wr(p, focus, "99999").errno()); - for ([_][]const u8{ "", "two", "-1", "1 2" }) |bad| try testing.expectEqual(E.INVAL, wr(p, focus, bad).errno()); - try testing.expectEqual(p.paneBySerial(first).?, p.active); - - // Focus gives the keyboard only: a folded pane stays folded. - const folded = p.paneBySerial(second).?; - layout.toggleCollapse(p, folded); - try testing.expect(p.panes[folded].?.collapsed); - try testing.expectEqual(Status.ok, wr(p, focus, try std.fmt.bufPrint(&line, "{d}", .{second})).reply.status); - try testing.expectEqual(folded, p.active); - try testing.expect(p.panes[folded].?.collapsed); -} - -test "the pane ctl takes acme's verbs and the pane's builtins, and refuses the rest in Plan 9's words" { - const gpa = testing.allocator; - const p = try withFile(gpa, "one\ntwo\n"); - defer p.deinit(); - const serial = serialOf(p); - const ctl_node = Node.of(serial, .ctl); - // A builtin is its own capitalised word: `GET` and `DEL` are none; - // acme's own words are done or refused as acme's (the test below). - for ([_][]const u8{ "lock x", "bogus", "DEL", "GET", "get x", "look /tmp", "exec Del" }) |bad| { - const refused = wr(p, ctl_node, bad); - try testing.expectEqual(E.INVAL, refused.errno()); - var want: [64]u8 = undefined; - try testing.expectEqualStrings(try std.fmt.bufPrint(&want, "unknown control message \"{s}\"", .{firstWord(bad)}), refused.reply.ename); - } - try testing.expectEqualStrings("not a window control message \"Newcol\": write it to /ctl", wr(p, ctl_node, "Newcol").reply.ename); - try testing.expectEqualStrings("not a window control message \"Verbose\": write it to /ctl", wr(p, ctl_node, "Verbose off").reply.ename); - try testing.expectEqualStrings("wrong #args in control message \"Collapse\"", wr(p, ctl_node, "Collapse now").reply.ename); - // Checked whole first: a bad line anywhere and nothing ran. - try testing.expectEqual(E.INVAL, wr(p, ctl_node, "Msg first\nbogus").errno()); - try testing.expect(p.panes[0].?.msg_len == 0 or !std.mem.eql(u8, p.panes[0].?.msg[0..p.panes[0].?.msg_len], "first")); - - // A pane's builtin runs at that pane. - try testing.expectEqual(Status.ok, wr(p, ctl_node, "Msg from ctl\n").reply.status); - const pane = p.panes[p.paneBySerial(serial).?].?; - try testing.expectEqualStrings("from ctl", pane.msg[0..pane.msg_len]); - // One that means nothing bare is refused before anything runs. - try testing.expectEqualStrings("wrong #args in control message \"Find\"", wr(p, ctl_node, "Find").reply.ename); - try testing.expectEqual(E.INVAL, wr(p, ctl_node, "Msg first\nMsg").errno()); - try testing.expect(!std.mem.eql(u8, pane.msg[0..pane.msg_len], "first")); - try testing.expect(pane.prompt == .none); - try testing.expect(!p.fs.no_prompt); - // Save with no name on a scratch would ask for one: refused, not asked. - const other = try th.newPane(p); - try testing.expectEqualStrings(e_prompt ++ " \"Save\"", wr(p, Node.of(other, .ctl), "Save").reply.ename); - try testing.expect(p.panes[p.paneBySerial(other).?].?.prompt == .none); - // A line after the one that closed the pane has nowhere to run. - try testing.expectEqual(E.NOENT, wr(p, Node.of(other, .ctl), "Del\nMsg after").errno()); - try testing.expect(p.paneBySerial(other) == null); -} - -test "the root ctl reads the settings as a write takes them, and takes the session's builtins" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - const first = rd(p, root_ctl, 0, 1 << 16); - try testing.expectEqual(Status.ok, first.reply.status); - const settings = try gpa.dupe(u8, first.bytes); - defer gpa.free(settings); - try testing.expectEqual(@as(u64, settings.len), call(p, .{ .tag = 1, .op = .getattr, .node = root_ctl }).reply.attr.size); - try testing.expect(std.mem.indexOf(u8, settings, "Verbose on\n") != null); - try testing.expect(std.mem.indexOf(u8, settings, "LocationsConfig ") != null); - try testing.expect(std.mem.indexOf(u8, settings, "CallstackDepth 8\n") != null); - try testing.expectEqual(Status.ok, wr(p, root_ctl, "CallstackDepth").reply.status); - // Settings only: nothing /status or the DumpConfig window says. - try testing.expect(std.mem.indexOf(u8, settings, "Platform") == null); - try testing.expect(std.mem.indexOf(u8, settings, "requested") == null); - - // Every line written back is taken, and changes nothing. - try testing.expectEqual(Status.ok, wr(p, root_ctl, settings).reply.status); - try testing.expectEqualStrings(settings, rd(p, root_ctl, 0, 1 << 16).bytes); - - try testing.expectEqual(Status.ok, wr(p, root_ctl, "Verbose off\n").reply.status); - try testing.expect(!p.settings.verbose); - try testing.expect(std.mem.indexOf(u8, rd(p, root_ctl, 0, 1 << 16).bytes, "Verbose off\n") != null); - const columns = p.ncol; - try testing.expectEqual(Status.ok, wr(p, root_ctl, "Newcol\n").reply.status); - try testing.expectEqual(columns + 1, p.ncol); - - try testing.expectEqualStrings("not a session control message \"Del\": write it to pane/<n>/ctl", wr(p, root_ctl, "Del").reply.ename); - try testing.expectEqualStrings("unknown control message \"Nonsense\"", wr(p, root_ctl, "Nonsense 1").reply.ename); - try testing.expectEqualStrings("bad value in control message; takes on, off \"maybe\"", wr(p, root_ctl, "Verbose maybe").reply.ename); - try testing.expectEqualStrings("bad value in control message; takes acme, pardes \"east\"", wr(p, root_ctl, "Placement east").reply.ename); - // A number's refusal names its range, a path's what it takes. - try testing.expectEqualStrings("bad value in control message; takes 0-100 (a percentage) \"200\"", wr(p, root_ctl, "InactiveDim 200").reply.ename); - try testing.expectEqualStrings("bad value in control message; takes 0-60000 (milliseconds) \"x\"", wr(p, root_ctl, "MessageLinger x").reply.ename); - for (config.Runtime.settings) |setting| if (setting.action != .theme) - try testing.expect(config.Runtime.takes(setting.action) != null); - const no_theme = wr(p, root_ctl, "Theme no-such-theme").reply.ename; - try testing.expectStringStartsWith(no_theme, "bad value in control message; like it: n"); - try testing.expect(std.mem.endsWith(u8, no_theme, "; Themes lists all \"no-such-theme\"")); - try testing.expectEqualStrings("wrong #args in control message \"Newcol\"", wr(p, root_ctl, "Newcol 2").reply.ename); - try testing.expectEqualStrings("wrong #args in control message \"Theme\"", wr(p, root_ctl, "Theme").reply.ename); - // A bare required word fails the check, so the line before never runs. - try testing.expectEqual(E.INVAL, wr(p, root_ctl, "Verbose on\nMount").errno()); - try testing.expect(!p.settings.verbose); - // A builtin that fails as it runs fails the write, quoting its error and - // its line; the lines before it have taken effect, as in acme. - const failed = wr(p, root_ctl, "Verbose on\nMount x /tmp/pardes-no-such-sock-zz\nVerbose off"); - try testing.expectEqual(E.NOENT, failed.errno()); - try testing.expectStringStartsWith(failed.reply.ename, "Mount x: /tmp/pardes-no-such-sock-zz: no such socket"); - try testing.expect(p.settings.verbose); - try testing.expect(!p.fs.no_prompt); - try testing.expectEqual(Status.ok, wr(p, root_ctl, "Verbose off").reply.status); -} - -test "a second lock is refused until the holder unlocks or closes, and binds nobody else" { - const p = try withFile(testing.allocator, "one\ntwo\n"); - defer p.deinit(); - const serial = serialOf(p); - const ctl_node = Node.of(serial, .ctl); - const w = struct { - fn ctl(pp: *Pardes, node: u64, h: u32, data: []const u8) th.Answer { - return call(pp, .{ .tag = 4, .op = .write, .node = node, .handle = h, .data = data }); - } - }; - const a = call(p, .{ .tag = 1, .op = .open, .node = ctl_node, .omode = 2 }).reply.handle; - const b = call(p, .{ .tag = 2, .op = .open, .node = ctl_node, .omode = 1 }).reply.handle; - try testing.expect(a != 0 and b != 0 and a != b); - // Reading the status line holds nothing, so it cannot lock either. - try testing.expectEqual(@as(u32, 0), call(p, .{ .tag = 3, .op = .open, .node = ctl_node }).reply.handle); - try testing.expectEqual(E.INVAL, wr(p, ctl_node, "lock\n").errno()); - - try testing.expectEqual(Status.ok, w.ctl(p, ctl_node, a, "lock\n").reply.status); - try testing.expectEqual(Status.ok, w.ctl(p, ctl_node, a, "lock\n").reply.status); - // Another open's lock is refused at once, and so is its unlock; writes - // to the pane's other files are not. - try testing.expectEqualStrings(e_locked, w.ctl(p, ctl_node, b, "lock\n").reply.ename); - try testing.expectEqual(E.BUSY, w.ctl(p, ctl_node, b, "lock\n").errno()); - try testing.expectEqualStrings(e_not_locked, w.ctl(p, ctl_node, b, "unlock\n").reply.ename); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .addr), "1").reply.status); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .data), "ONE\n").reply.status); - try testing.expectEqualStrings("ONE\ntwo\n", p.panes[0].?.file.?.content); - - // Once the holder unlocks, the other's try takes it. - try testing.expectEqual(Status.ok, w.ctl(p, ctl_node, a, "unlock\n").reply.status); - try testing.expectEqual(Status.ok, w.ctl(p, ctl_node, b, "lock\n").reply.status); - try testing.expectEqual(E.BUSY, w.ctl(p, ctl_node, a, "lock\nunlock\n").errno()); - // Closing the holder gives it up. - _ = call(p, .{ .tag = 5, .op = .release, .node = ctl_node, .handle = b }); - try testing.expectEqual(Status.ok, w.ctl(p, ctl_node, a, "lock\nunlock\nlock\n").reply.status); - try testing.expectEqual(E.INVAL, w.ctl(p, ctl_node, a, "unlock\nunlock\n").errno()); - try testing.expectEqual(@as(?u32, a), p.panes[0].?.fs.lock); - - // The lock lives and dies with the pane. - const other = try th.newPane(p); - const other_ctl = Node.of(other, .ctl); - const c = call(p, .{ .tag = 6, .op = .open, .node = other_ctl, .omode = 1 }).reply.handle; - try testing.expectEqual(Status.ok, w.ctl(p, other_ctl, c, "lock\n").reply.status); - try testing.expectEqual(Status.ok, th.rmdir(p, Node.of(other, .dir)).reply.status); - try testing.expectEqual(E.NOENT, w.ctl(p, other_ctl, c, "lock\n").errno()); - for ([_]u64{ ctl_node, other_ctl }, [_]u32{ a, c }) |node, h| - _ = call(p, .{ .tag = 7, .op = .release, .node = node, .handle = h }); - try testing.expect(p.panes[0].?.fs.lock == null); - for (p.fs.opens) |o| try testing.expect(o.node == 0); -} - 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(); @@ -1718,81 +1519,6 @@ test "dirty goes back to 0 when the text is the saved text again" { try testing.expectEqualStrings("1\n", rd(p, dirty, 0, 8).bytes); } -test "a message from a root ctl write is the session's in the log" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - p.update(.tick); // the pane's `new` is logged before the log is opened - while (p.nextEffect()) |_| {} - const log = @backingInt(tree.TopFile.log); - const f = call(p, .{ .tag = 1, .op = .open, .node = log }).reply.handle; - const frozen = call(p, .{ .tag = 2, .op = .read, .node = log, .handle = f, .size = 8192 }).bytes.len; - _ = call(p, .{ .tag = 3, .op = .write, .node = log, .handle = f, .data = "follow" }); - try testing.expectEqual(Status.ok, wr(p, @backingInt(tree.TopFile.ctl), "Kill\n").reply.status); - // What it said, the session's; its own words name it, so its Verbose - // announcement gives way to them. - try testing.expectEqualStrings("msg - Kill: nothing running\n", call(p, .{ .tag = 5, .op = .read, .node = log, .handle = f, .off = frozen, .size = 4096 }).bytes); -} - -test "a look takes acme's :addr and file:addr, and a miss changes nothing" { - const gpa = testing.allocator; - const p = try withFile(gpa, "foo x foo y\nbar\n"); - defer p.deinit(); - const serial = serialOf(p); - const look_node = Node.of(serial, .look); - const dot = Node.of(serial, .dot); - // :addr is an address in this pane, evaluated from its dot. - try testing.expectEqual(Status.ok, wr(p, look_node, ":/y/\n").reply.status); - try testing.expectEqualStrings(" 10 11 ", rd(p, dot, 0, 64).bytes); - try testing.expectEqual(Status.ok, wr(p, look_node, ":#4\n").reply.status); - try testing.expectEqualStrings(" 4 4 ", rd(p, dot, 0, 64).bytes); - try testing.expectEqual(Status.ok, wr(p, look_node, ":/foo/\n").reply.status); // from #4: the second foo - try testing.expectEqualStrings(" 6 9 ", rd(p, dot, 0, 64).bytes); - // file:addr names the pane's own file here. - var line: [256]u8 = undefined; - const path = p.panes[0].?.file.?.path; - try testing.expectEqual(Status.ok, wr(p, look_node, try std.fmt.bufPrint(&line, "{s}:/bar/\n", .{path})).reply.status); - try testing.expectEqualStrings(" 12 15 ", rd(p, dot, 0, 64).bytes); - // A miss: no buffer, the selection kept, and it says so. - const panes_before = p.panes.len - std.mem.count(?*pardes.Pane, &p.panes, &.{null}); - try testing.expectEqual(Status.ok, wr(p, look_node, "zzqq\n").reply.status); - try testing.expectEqual(panes_before, p.panes.len - std.mem.count(?*pardes.Pane, &p.panes, &.{null})); - try testing.expectEqualStrings(" 12 15 ", rd(p, dot, 0, 64).bytes); - try testing.expectStringStartsWith(p.panes[0].?.msg[0..p.panes[0].?.msg_len], "Look: no match"); - // A hit answers the pane the text is selected in, not its hits buffer. - try testing.expectEqual(Status.ok, wr(p, look_node, "foo\n").reply.status); - var own: [16]u8 = undefined; - try testing.expectEqualStrings(try std.fmt.bufPrint(&own, "{d}\n", .{serial}), rd(p, look_node, 0, 64).bytes); - // A pattern with a blank in it; 0/re/ searches from the top, whatever dot is. - try testing.expectEqual(Status.ok, wr(p, look_node, ":/foo y/\n").reply.status); - try testing.expectEqualStrings(" 6 11 ", rd(p, dot, 0, 64).bytes); - try testing.expectEqual(Status.ok, wr(p, look_node, try std.fmt.bufPrint(&line, "{s}:0/foo/\n", .{path})).reply.status); - try testing.expectEqualStrings(" 0 3 ", rd(p, dot, 0, 64).bytes); - // file:N selects the line; a line past the end moves nothing and reads back empty. - try testing.expectEqual(Status.ok, wr(p, look_node, try std.fmt.bufPrint(&line, "{s}:2\n", .{path})).reply.status); - try testing.expectEqualStrings(" 12 16 ", rd(p, dot, 0, 64).bytes); - try testing.expectEqual(Status.ok, wr(p, look_node, try std.fmt.bufPrint(&line, "{s}:99\n", .{path})).reply.status); - try testing.expectEqualStrings(" 12 16 ", rd(p, dot, 0, 64).bytes); - try testing.expectEqualStrings("", rd(p, look_node, 0, 64).bytes); - try testing.expectStringStartsWith(p.panes[0].?.msg[0..p.panes[0].?.msg_len], "Look: "); -} - -test "the root look's file:/re/ goes on from the dot, and a miss leaves the dot as it was" { - const gpa = testing.allocator; - const p = try withFile(gpa, "foo x foo y\nfoo\n"); - defer p.deinit(); - const dot = Node.of(serialOf(p), .dot); - const root = @backingInt(tree.TopFile.look); - var line: [256]u8 = undefined; - const path = p.panes[0].?.file.?.path; - const again = try std.fmt.bufPrint(&line, "{s}:/foo/\n", .{path}); - for ([_][]const u8{ " 0 3 ", " 6 9 ", " 12 15 ", " 0 3 " }) |want| { - try testing.expectEqual(Status.ok, wr(p, root, again).reply.status); - try testing.expectEqualStrings(want, rd(p, dot, 0, 64).bytes); - } - try testing.expectEqual(Status.ok, wr(p, root, try std.fmt.bufPrint(&line, "{s}:/zzqq/\n", .{path})).reply.status); - try testing.expectEqualStrings(" 0 3 ", rd(p, dot, 0, 64).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"); @@ -1807,31 +1533,6 @@ test "a look's miss repeats the path whole up to 256 bytes, then gives up its mi 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 "exec runs a builtin at the pane and records the pane it acted on" { - const gpa = testing.allocator; - const p = try withFile(gpa, "Msg fs-ran\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .exec), "Msg from exec\n").reply.status); - try testing.expectEqualStrings("from exec", pane.msg[0..pane.msg_len]); - var expected: [16]u8 = undefined; - const own = try std.fmt.bufPrint(&expected, "{d}\n", .{serial}); - try testing.expectEqualStrings(own, rd(p, root_exec, 0, 4096).bytes); - // Either command file reads back what the last one did. - try testing.expectEqualStrings(own, rd(p, Node.of(serial, .look), 0, 4096).bytes); - try testing.expectEqual(@as(u64, own.len), call(p, .{ .tag = 1, .op = .getattr, .node = root_exec }).reply.attr.size); - - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .exec), "New\n").reply.status); - const made = p.panes[p.active].?; - try testing.expect(made.serial != serial); - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "{d}\n", .{made.serial}), rd(p, root_exec, 0, 4096).bytes); - - try testing.expectEqual(Status.ok, wr(p, Node.of(made.serial, .exec), "Del\n").reply.status); - try testing.expect(p.paneBySerial(made.serial) == null); - try testing.expectEqualStrings(try std.fmt.bufPrint(&expected, "{d}\n", .{made.serial}), rd(p, root_exec, 0, 4096).bytes); -} - test "status reads the editor, and look and exec validate whole batches" { const gpa = testing.allocator; const p = try withFile(gpa, "Msg root\n"); @@ -2335,20 +2036,6 @@ test "Save path of a file pane logs a save record naming that path" { try testing.expect(th.logHas(p, try std.fmt.bufPrint(&want, "save {d} /tmp/pardes-save-copy.txt\n", .{serial}))); } -test "Mount with no dial is wrong #args, and a dial that is no address says bad dial address" { - if (comptime !pardes.hosted) return; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - const bare = wr(p, root_ctl, "Mount work\n"); - try testing.expectEqual(E.INVAL, bare.errno()); - try testing.expectStringStartsWith(bare.reply.ename, "wrong #args"); - const bad = wr(p, root_ctl, "Mount work tcp!nohost\n"); - try testing.expectEqual(E.INVAL, bad.errno()); - try testing.expectStringStartsWith(bad.reply.ename, "Mount: bad dial address: tcp!nohost"); - try testing.expectEqual(@as(usize, 0), p.fs.mounts.items.len); -} - test "an address looked at in an image pane names the image, never an empty subject" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -2463,17 +2150,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 "Pager is a setting of the root ctl: pardes by default, off taken, anything else refused with its choices" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - try testing.expect(std.mem.indexOf(u8, rd(p, root_ctl, 0, 1 << 16).bytes, "Pager pardes\n") != null); - try testing.expectEqual(Status.ok, wr(p, root_ctl, "Pager off\n").reply.status); - try testing.expectEqual(config.Runtime.Pager.off, p.settings.pager); - try testing.expect(std.mem.indexOf(u8, rd(p, root_ctl, 0, 1 << 16).bytes, "Pager off\n") != null); - try testing.expectEqualStrings("bad value in control message; takes pardes, off \"less\"", wr(p, root_ctl, "Pager less\n").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(); @@ -2516,85 +2192,6 @@ test "a look line's trailing colon is dropped, and a look whose address fails ne } -test "a refused Del written back through event fails that write, EIO, with its err" { - const gpa = testing.allocator; - const p = try withFile(gpa, "text\n"); - defer p.deinit(); - const serial = serialOf(p); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .body), "more\n").reply.status); // unsaved - const event = Node.of(serial, .event); - const h = call(p, .{ .tag = 1, .op = .open, .node = event }).reply.handle; - defer _ = call(p, .{ .tag = 9, .op = .release, .node = event, .handle = h }); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .exec), "Del\n").reply.status); - const record = call(p, .{ .tag = 2, .op = .read, .node = event, .handle = h, .size = 256 }).bytes; - var copy: [256]u8 = undefined; - @memcpy(copy[0..record.len], record); - const back = wr(p, event, copy[0..record.len]); - try testing.expectEqual(E.IO, back.errno()); - try testing.expect(std.mem.indexOf(u8, back.reply.ename, "Modified") != null); - try testing.expect(p.paneBySerial(serial) != null); -} - -test "a pane's own look and exec go to the event reader holding it, which writes them back to act" { - const gpa = testing.allocator; - const p = try withFile(gpa, "Msg routed\n"); - defer p.deinit(); - const serial = serialOf(p); - const event = Node.of(serial, .event); - const h = call(p, .{ .tag = 1, .op = .open, .node = event }).reply.handle; - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(serial, .exec), "Msg routed\n").reply.status); - // Not done: the reader has it, as it would a middle click. - const pane = p.panes[0].?; - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "routed") == null); - const record = call(p, .{ .tag = 2, .op = .read, .node = event, .handle = h, .size = 256 }).bytes; - // Flag 1: its first word is a builtin; no 2, no expansion follows. - try testing.expectEqualStrings("FX0 0 1 10 Msg routed\n", record); - // Written back, it is done. - try testing.expectEqual(tree.Status.ok, wr(p, event, record).reply.status); - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "routed") != null); - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(serial, .look), "one.txt:3\n").reply.status); - try testing.expectEqualStrings("FL0 0 4 9 one.txt:3\n", call(p, .{ .tag = 3, .op = .read, .node = event, .handle = h, .size = 256 }).bytes); - // The root's is a click at the pane with the keyboard, this one. - try testing.expectEqual(tree.Status.ok, wr(p, root_exec, "Msg rooted\n").reply.status); - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "rooted") == null); - try testing.expectEqualStrings("FX0 0 1 10 Msg rooted\n", call(p, .{ .tag = 5, .op = .read, .node = event, .handle = h, .size = 256 }).bytes); - // A click in its tag is the reader's too, an `x` record, as acme's is; - // written back, it is done. - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(serial, .tagexec), "Msg tagged\n").reply.status); - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "tagged") == null); - const tagged = call(p, .{ .tag = 6, .op = .read, .node = event, .handle = h, .size = 256 }).bytes; - try testing.expectEqualStrings("Fx0 0 1 10 Msg tagged\n", tagged); - try testing.expectEqual(tree.Status.ok, wr(p, event, tagged).reply.status); - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "tagged") != null); - _ = call(p, .{ .tag = 4, .op = .release, .node = event, .handle = h }); - // With no reader, the root's and the pane's act at once. - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(serial, .exec), "Msg direct\n").reply.status); - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "direct") != null); -} - -test "look and exec read back what the last command touched, a ctl write's too" { - const gpa = testing.allocator; - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - var want: [32]u8 = undefined; - _ = wr(p, root_exec, "Msg first\n"); - try testing.expectEqualStrings(try std.fmt.bufPrint(&want, "{d}\n", .{serial}), rd(p, root_exec, 0, 64).bytes); - // A pane made through ctl is what both read next, not the Msg's pane. - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(serial, .ctl), "Tty\n").reply.status); - const made = p.panes[p.active].?.serial; - try testing.expect(made != serial); - try testing.expectEqualStrings(try std.fmt.bufPrint(&want, "{d}\n", .{made}), rd(p, root_exec, 0, 64).bytes); - try testing.expectEqualStrings(try std.fmt.bufPrint(&want, "{d}\n", .{made}), rd(p, @backingInt(tree.TopFile.look), 0, 64).bytes); - // A ctl write that makes nothing names the pane it ran at. - try testing.expectEqual(tree.Status.ok, wr(p, @backingInt(tree.TopFile.ctl), "Verbose off\n").reply.status); - try testing.expectEqualStrings(try std.fmt.bufPrint(&want, "{d}\n", .{made}), rd(p, root_exec, 0, 64).bytes); - // Closed, it is no pane to go to: a fresh read of look is empty. - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(made, .ctl), "Del\n").reply.status); - try testing.expect(p.paneBySerial(made) == null); - try testing.expectEqualStrings("", rd(p, @backingInt(tree.TopFile.look), 0, 64).bytes); -} - 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; const p = try withFile(gpa, "x\n"); @@ -2921,42 +2518,6 @@ test "a setting this frontend cannot show says GUI-only, and DumpDir reads back } } -test "Shell refuses a path that is no executable, and bare it goes back to the default" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const root_ctl = @backingInt(tree.TopFile.ctl); - const refused = wr(p, root_ctl, "Shell /nonexistent/zzsh\n"); - try testing.expectEqual(Status.err, refused.reply.status); - try testing.expect(std.mem.indexOf(u8, refused.reply.ename, "Shell: shell \"/nonexistent/zzsh\" not found") != null); - try testing.expect(th.logHas(p, "shell \"/nonexistent/zzsh\" not found")); - // Its words say a directory, which a mount reads as EISDIR too. - try testing.expectEqual(E.ISDIR, wr(p, root_ctl, "Shell /etc\n").errno()); - try testing.expect(th.logHas(p, "Shell: not a shell: /etc is a directory")); - try testing.expectEqualStrings("", p.settings.shell.requested.get()); - try testing.expectEqual(Status.ok, wr(p, root_ctl, "Shell /bin/sh\n").reply.status); - try testing.expectEqualStrings("/bin/sh", p.settings.shell.requested.get()); - try testing.expectEqual(Status.ok, wr(p, root_ctl, "Shell\n").reply.status); - try testing.expectEqualStrings("", p.settings.shell.requested.get()); - try testing.expect(th.logHas(p, "Shell: the default again, ")); - var want: [300]u8 = undefined; - try testing.expect(std.mem.indexOf(u8, rd(p, root_ctl, 0, 8192).bytes, try std.fmt.bufPrint(&want, "Shell {s}\n", .{config.defaultShell()})) != null); -} - -test "a builtin that fails a ctl write logs only its err, so the same failure again is counted" { - 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 ctl_node = @backingInt(tree.TopFile.ctl); - for (0..3) |_| try testing.expectEqual(Status.err, wr(p, ctl_node, "Unmount zzz\n").reply.status); - 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.endsWith(u8, text, "err - ctl: Unmount: no such mount zzz (x3)\n")); - try testing.expect(std.mem.indexOf(u8, text, "msg - Unmount") == null); - _ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = h }); -} - 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(); @@ -3011,17 +2572,6 @@ test "Mount dials its peer and fails, mounting nothing, when none answers; a loo try testing.expect(th.logHas(p, "look: /n/gone/f.txt: dial failed: no answer")); } -test "a root exec that fails logs its err against the pane it ran at" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const f = &p.panes[0].?.file.?; - f.saved_revision = f.revision -% 1; - f.saved_hash = 0; - try testing.expect(wr(p, root_exec, "Exit\n").errno() != 0); - var want: [32]u8 = undefined; - try testing.expect(th.logHas(p, try std.fmt.bufPrint(&want, "err {d} exec: ", .{serialOf(p)}))); -} - test "Tty in a directory that is not there fails and makes no pane; a shell that never started answers a run" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -3585,24 +3135,6 @@ test "a Restore of a name in the DumpDir asks for, and logs, the whole path it r try testing.expectEqualStrings(try std.fmt.bufPrint(&want, "{s}/x.dump.zon", .{dir}), p.restore_req.?); } -test "a line starting with # runs as nothing at an exec or a ctl, silently" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const serial = serialOf(p); - // The setup's own announcements are made by the first write's update. - _ = wr(p, @backingInt(tree.TopFile.ctl), "# settle\n"); - const before = p.next_serial; - const log_before = try pardes.ctlfs.events.freezeLog(p); - defer p.gpa.free(log_before.bytes); - for ([_]u64{ @backingInt(tree.TopFile.exec), Node.of(serial, .exec), @backingInt(tree.TopFile.ctl), Node.of(serial, .ctl), Node.ofCol(layout.columnSerial(p, 0), .ctl) }) |node| { - try testing.expectEqual(Status.ok, wr(p, node, "# Platform: tty\n # indented too\n").reply.status); - } - try testing.expectEqual(before, p.next_serial); - const log_after = try pardes.ctlfs.events.freezeLog(p); - defer p.gpa.free(log_after.bytes); - try testing.expectEqualStrings(log_before.bytes, log_after.bytes); -} - 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"); @@ -3797,17 +3329,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 tag write refuses NUL and the other control characters" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - for ([_]u64{ @backingInt(tree.TopFile.tag), tree.Node.ofCol(pardes.layout.columnSerial(p, 0), .tag), Node.of(serialOf(p), .tag) }) |node| { - const refused = wr(p, node, " a\x00b"); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expectStringStartsWith(refused.reply.ename, "invalid tag text"); - try testing.expectEqual(E.INVAL, wr(p, node, " \x7f").errno()); - } -} - 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(); @@ -4603,93 +4124,6 @@ test "@p<terminal>:<address> takes any address over the terminal's lines, select try testing.expectEqualStrings("", rd(p, root, 0, 64).bytes); } -test "a bad command inside an Edit block written whole is named, not the } after it" { - const p = try withFile(testing.allocator, "abc\n"); - defer p.deinit(); - const r = wr(p, Node.of(serialOf(p), .ctl), "Edit {\n,s/a/b/\nzq\n}\n"); - try testing.expectEqual(Status.err, r.reply.status); - try testing.expect(std.mem.indexOf(u8, r.reply.ename, "\"}\"") == null); - try testing.expect(std.mem.indexOf(u8, r.reply.ename, "z") != null); - try testing.expectEqualStrings("abc\n", p.panes[0].?.file.?.content); -} - -test "a refused Delcol changes nothing: no pane, no focus" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - _ = wr(p, @backingInt(tree.TopFile.ctl), "Newcol\n"); - const made = try th.newPane(p); - _ = wr(p, Node.of(made, .name), "/tmp/pardes-delcol-refused.txt\n"); - _ = wr(p, Node.of(made, .body), "edit\n"); - p.sync(); - const col = layout.findPane(p, p.paneBySerial(made).?).?.col; - const active = p.active; - const active_column = p.active_column; - var count: usize = 0; - for (p.panes) |slot| count += @intFromBool(slot != null); - try testing.expectEqual(Status.err, wr(p, Node.ofCol(layout.columnSerial(p, col), .ctl), "Delcol\n").reply.status); - p.sync(); - var after: usize = 0; - for (p.panes) |slot| after += @intFromBool(slot != null); - try testing.expectEqual(count, after); - try testing.expectEqual(active, p.active); - try testing.expectEqual(active_column, p.active_column); - try testing.expect(th.logHas(p, "unsaved ")); -} - -test "pager answers its +Pager on the open that asked, whatever another client exec'd meanwhile" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const pager = @backingInt(tree.TopFile.pager); - const exec = @backingInt(tree.TopFile.exec); - const asked = call(p, .{ .tag = 1, .op = .open, .node = pager, .omode = 2 }).reply.handle; - try testing.expectEqual(Status.ok, call(p, .{ .tag = 2, .op = .write, .node = pager, .handle = asked, .data = "/tmp\n" }).reply.status); - const paged = for (p.panes) |slot| { - const pn = slot orelse continue; - if (pn.file) |f| if (std.mem.eql(u8, f.path, "/tmp/+Pager")) break pn.serial; - } else return error.NoPager; - // Another client execs between the request and its read: a pane of its own. - const racer = call(p, .{ .tag = 3, .op = .open, .node = exec, .omode = 2 }).reply.handle; - try testing.expectEqual(Status.ok, call(p, .{ .tag = 4, .op = .write, .node = exec, .handle = racer, .data = "New\n" }).reply.status); - var line: [16]u8 = undefined; - const want = try std.fmt.bufPrint(&line, "{d}\n", .{paged}); - // A fresh open reads the session's last answer, the racer's pane: what - // `pardes -` once read, and wrote its text into. - try testing.expect(!std.mem.eql(u8, want, rd(p, exec, 0, 64).bytes)); - try testing.expectEqualStrings(want, call(p, .{ .tag = 5, .op = .read, .node = pager, .handle = asked, .size = 64 }).bytes); - _ = call(p, .{ .tag = 6, .op = .release, .node = pager, .handle = asked, .opened = true }); - // Refused, an open answers nothing, not the session's last answer. - const refused = call(p, .{ .tag = 7, .op = .open, .node = pager, .omode = 2 }).reply.handle; - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 8, .op = .write, .node = pager, .handle = refused, .data = "relative\n" }).errno()); - try testing.expectEqualStrings("", call(p, .{ .tag = 9, .op = .read, .node = pager, .handle = refused, .size = 64 }).bytes); - _ = call(p, .{ .tag = 10, .op = .release, .node = pager, .handle = refused, .opened = true }); - // Resolved: `/tmp/../tmp/.` is /tmp, a +Pager of its own beside the - // one made above, which keeps its page. - const resolved = call(p, .{ .tag = 11, .op = .open, .node = pager, .omode = 2 }).reply.handle; - try testing.expectEqual(Status.ok, call(p, .{ .tag = 12, .op = .write, .node = pager, .handle = resolved, .data = "/tmp/../tmp/.\n" }).reply.status); - const second = call(p, .{ .tag = 13, .op = .read, .node = pager, .handle = resolved, .size = 64 }).bytes; - try testing.expect(!std.mem.eql(u8, want, second)); - const second_pane = p.panes[p.paneBySerial(try std.fmt.parseInt(u32, std.mem.trim(u8, second, "\n"), 10)).?].?; - try testing.expectEqualStrings("/tmp/+Pager", second_pane.file.?.path); - try testing.expect(p.paneBySerial(paged) != null); - _ = call(p, .{ .tag = 14, .op = .release, .node = pager, .handle = resolved, .opened = true }); - // One that may not be written is refused, permission denied (Linux's /proc/1). - if (comptime @import("builtin").os.tag == .linux) try testing.expectEqual(E.PERM, wr(p, pager, "/proc/1\n").errno()); - // One directory a write, absolute, and there. - try testing.expectEqual(E.INVAL, wr(p, pager, "/a\n/b\n").errno()); - // One directory an open: one refused is not followed by another, as a - // mount sends two lines in two writes. (After one taken, what follows - // is the paged text: the test below.) - const twice = call(p, .{ .tag = 20, .op = .open, .node = pager, .omode = 2 }).reply.handle; - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 21, .op = .write, .node = pager, .handle = twice, .data = "relative\n" }).errno()); - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 22, .op = .write, .node = pager, .handle = twice, .data = "/tmp\n" }).errno()); - _ = call(p, .{ .tag = 23, .op = .release, .node = pager, .handle = twice }); - const relative = wr(p, pager, "relative\n"); - try testing.expectEqual(E.INVAL, relative.errno()); - try testing.expectEqualStrings("pager: invalid directory \"relative\": must be absolute", relative.reply.ename); - try testing.expectEqual(E.NOENT, wr(p, pager, "/tmp/pardes-surely-missing-dir\n").errno()); - try testing.expectEqual(E.NOTDIR, wr(p, pager, "/etc/passwd\n").errno()); -} - test "what a /pager open writes after its directory is the text: plain in the body, its colours the pane's, clean, in by the read" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -5033,17 +4467,6 @@ test "a command or a terminal refused for room is refused before anything is log } } -test "a Mount of a socket path that is not there says no such socket, ENOENT" { - if (comptime !pardes.hosted) return; - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const r = wr(p, @backingInt(tree.TopFile.ctl), "Mount peer unix!/tmp/pardes-surely-no-socket\n"); - try testing.expectEqual(E.NOENT, r.errno()); - try testing.expect(std.mem.indexOf(u8, r.reply.ename, "no such socket") != null); - try testing.expectEqual(@as(usize, 0), p.fs.mounts.items.len); - try testing.expectEqual(E.INVAL, wr(p, @backingInt(tree.TopFile.ctl), "Mount peer rel/x.sock\n").errno()); -} - test "the init file reads past a BOM, and every line it fails is a notice and an err, never silent" { const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .startup_config = "\xEF\xBB\xBFTheme dusk\nnosuchword 1\nTheme nosuchtheme\n# a comment\nInactiveDim 30 # not a comment, a bad value\n" }); defer p.deinit(); diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index 33f816fe..bf9dc381 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -1098,88 +1098,6 @@ const withFile = th.withFile; const serialOf = th.serialOf; const Status = tree.Status; -test "index lists serial kind dirty flag and name, and stats 0, a view generated by each read" { - const gpa = testing.allocator; - const p = try withFile(gpa, "hello\nthere\n"); - defer p.deinit(); - const pane = p.panes[0].?; - const index = @backingInt(tree.TopFile.index); - - const a = rd(p, index, 0, 4096); - try testing.expectEqual(Status.ok, a.reply.status); - var want: [64]u8 = undefined; - const clean_row = try std.fmt.bufPrint(&want, "{d} text 0 /test.txt {d}\n", .{ pane.serial, columnOf(p, 0) }); - try testing.expect(std.mem.indexOf(u8, a.bytes, clean_row) != null); - const at = std.mem.indexOf(u8, a.bytes, clean_row).?; - const stat = call(p, .{ .tag = 1, .op = .getattr, .node = index }); - try testing.expectEqual(@as(u64, 0), stat.reply.attr.size); - try testing.expectEqual(@as(u16, 0o444), stat.reply.attr.mode); - - pane.file.?.saved_revision = pane.file.?.revision -% 1; - var dirty_want: [64]u8 = undefined; - const dirty_row = try std.fmt.bufPrint(&dirty_want, "{d} text 1 /test.txt {d}\n", .{ pane.serial, columnOf(p, 0) }); - try testing.expect(std.mem.indexOf(u8, rd(p, index, 0, 4096).bytes, dirty_row) != null); - const kind = at + std.mem.indexOfScalar(u8, clean_row, ' ').? + 1; - try testing.expectEqualStrings("text", rd(p, index, kind, 4).bytes); - - const serial = try th.newPane(p); - const both = rd(p, index, 0, 4096); - var expected: [4200]u8 = undefined; - const made = p.panes[p.paneBySerial(serial).?].?; - try testing.expect(std.mem.endsWith(u8, both.bytes, try std.fmt.bufPrint(&expected, "\n{d} text 0 {s} {d}\n", .{ serial, nameOf(p, made), columnOf(p, p.paneBySerial(serial).?) }))); -} - -test "a terminal is listed as term with its directory as name" { - const p = try th.withTerm(testing.allocator); - defer p.deinit(); - const pane = p.panes[0].?; - p.setCwd(0, "/work/dir"); - var want: [64]u8 = undefined; - try testing.expect(std.mem.indexOf( - u8, - rd(p, @backingInt(tree.TopFile.index), 0, 4096).bytes, - try std.fmt.bufPrint(&want, "{d} term 0 /work/dir {d}\n", .{ pane.serial, columnOf(p, 0) }), - ) != null); - try testing.expectEqualStrings("/work/dir\n", rd(p, Node.of(pane.serial, .name), 0, 4096).bytes); - const refused = wr(p, Node.of(pane.serial, .name), "/elsewhere\n"); - try testing.expectEqual(E.INVAL, refused.errno()); - try testing.expect(std.mem.startsWith(u8, refused.reply.ename, "invalid rename: a terminal is named")); -} - -test "body reads at any offset and writes append" { - const gpa = testing.allocator; - const p = try withFile(gpa, "one\ntwo\n"); - defer p.deinit(); - const serial = serialOf(p); - const body = Node.of(serial, .body); - - try testing.expectEqualStrings("one\ntwo\n", rd(p, body, 0, 100).bytes); - try testing.expectEqualStrings("two\n", rd(p, body, 4, 100).bytes); - try testing.expectEqualStrings("wo", rd(p, body, 5, 2).bytes); - try testing.expectEqualStrings("", rd(p, body, 999, 2).bytes); - try testing.expect(rd(p, body, 0, 100).bytes.ptr == p.panes[0].?.file.?.content.ptr); - - const w = call(p, .{ .tag = 5, .op = .write, .node = body, .off = 0, .data = "three\n" }); - try testing.expectEqual(@as(u32, 6), w.reply.written); - try testing.expectEqualStrings("one\ntwo\nthree\n", p.panes[0].?.file.?.content); - - const short = wr(p, body, "a\xC3"); - try testing.expectEqual(@as(u32, 1), short.reply.written); - try testing.expectEqualStrings("one\ntwo\nthree\na", p.panes[0].?.file.?.content); - - const stat = call(p, .{ .tag = 6, .op = .getattr, .node = body }); - try testing.expectEqual(@as(u64, "one\ntwo\nthree\na".len), stat.reply.attr.size); - try testing.expectEqual(@as(u16, 0o666), stat.reply.attr.mode); - try testing.expect(stat.reply.attr.mtime >= p.fs.started); - - // The qid version is the pane's revision, so a stat sees an edit land. - try testing.expectEqual(p.panes[0].?.file.?.revision, stat.reply.attr.version); - _ = wr(p, body, "more\n"); - const later = call(p, .{ .tag = 7, .op = .getattr, .node = body }); - try testing.expect(later.reply.attr.version != stat.reply.attr.version); - try testing.expectEqual(@as(u32, 0), call(p, .{ .tag = 8, .op = .getattr, .node = Node.of(serial, .tag) }).reply.attr.version); -} - test "a body write to a terminal pane types at its shell" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 10 }); @@ -1228,32 +1146,6 @@ test "tag reads the whole tag and writes append to the editable tail" { try testing.expectEqual(E.NOSPC, wr(p, node, "x").errno()); } -test "data and xdata read from addr, move it, and write through it" { - const gpa = testing.allocator; - const p = try withFile(gpa, "one\ntwo\n"); - defer p.deinit(); - const serial = serialOf(p); - const addr = Node.of(serial, .addr); - const data = Node.of(serial, .data); - const xdata = Node.of(serial, .xdata); - - _ = wr(p, addr, "#0"); - try testing.expectEqualStrings("one", rd(p, data, 0, 3).bytes); - try testing.expectEqual(@as(u32, 3), p.panes[0].?.fs.addr.q0); - try testing.expectEqual(@as(u32, 3), p.panes[0].?.fs.addr.q1); - - _ = wr(p, addr, "1"); - try testing.expectEqualStrings("one\n", rd(p, xdata, 0, 100).bytes); - _ = wr(p, addr, "1"); - try testing.expectEqualStrings("one\ntwo\n", rd(p, data, 0, 100).bytes); - - _ = wr(p, addr, "1"); - const w = wr(p, data, "ONE\n"); - try testing.expectEqual(@as(u32, 4), w.reply.written); - try testing.expectEqualStrings("ONE\ntwo\n", p.panes[0].?.file.?.content); - try testing.expectEqual(@as(u32, 4), p.panes[0].?.fs.addr.q0); -} - test "data never splits a rune, in either direction" { const gpa = testing.allocator; const p = try withFile(gpa, "\u{00e9}x\n"); @@ -1268,98 +1160,6 @@ test "data never splits a rune, in either direction" { try testing.expectEqual(@as(u32, 1), wr(p, Node.of(serial, .data), "a\xC3").reply.written); } -test "sel reads the selection and writing sel replaces it" { - const gpa = testing.allocator; - const p = try withFile(gpa, "one\ntwo\n"); - defer p.deinit(); - const serial = serialOf(p); - const addr_node = Node.of(serial, .addr); - const dot = Node.of(serial, .dot); - const sel = Node.of(serial, .sel); - - // Copying addr onto dot is the whole of acme's `dot=addr`, and back again - // of `addr=dot`; each range file reads exactly what the other takes. - _ = wr(p, addr_node, "#0,#3"); - try testing.expectEqual(Status.ok, wr(p, dot, " 0 3 ").reply.status); - try testing.expectEqualStrings(" 0 3 ", rd(p, dot, 0, 100).bytes); - try testing.expectEqual(range_len, call(p, .{ .tag = 1, .op = .getattr, .node = dot }).reply.attr.size); - try testing.expectEqualStrings("one", rd(p, sel, 0, 100).bytes); - try testing.expectEqual(@as(u64, 3), call(p, .{ .tag = 1, .op = .getattr, .node = sel }).reply.attr.size); - - _ = wr(p, addr_node, "#0"); - _ = wr(p, addr_node, " 0 3 "); - try testing.expectEqual(@as(u32, 0), p.panes[0].?.fs.addr.q0); - try testing.expectEqual(@as(u32, 3), p.panes[0].?.fs.addr.q1); - - // A truncation empties dot once its open goes, nothing written after - // it; an address expression addresses it. - const h = call(p, .{ .tag = 8, .op = .open, .node = dot, .omode = 1 }).reply.handle; - _ = call(p, .{ .tag = 9, .op = .setattr, .node = dot, .truncate = true }); - try testing.expectEqualStrings("one", rd(p, sel, 0, 100).bytes); - _ = call(p, .{ .tag = 10, .op = .release, .node = dot, .handle = h, .opened = true }); - try testing.expectEqualStrings("", rd(p, sel, 0, 100).bytes); - try testing.expectEqual(Status.ok, wr(p, dot, "#0,#3").reply.status); - - try testing.expectEqual(Status.ok, wr(p, sel, "ONE").reply.status); - try testing.expectEqualStrings("ONE\ntwo\n", p.panes[0].?.file.?.content); - // What was written is selected, as acme's wrsel leaves it. - try testing.expectEqualStrings("ONE", rd(p, sel, 0, 100).bytes); - // A write of another open replaces it; one open's writes run on. - _ = wr(p, sel, "!"); - try testing.expectEqualStrings("!\ntwo\n", p.panes[0].?.file.?.content); - const hs = call(p, .{ .tag = 3, .op = .open, .node = sel, .omode = 1 }).reply.handle; - _ = call(p, .{ .tag = 4, .op = .write, .node = sel, .handle = hs, .data = "a" }); - _ = call(p, .{ .tag = 5, .op = .write, .node = sel, .handle = hs, .data = "b" }); - _ = call(p, .{ .tag = 6, .op = .release, .node = sel, .handle = hs, .opened = true }); - try testing.expectEqualStrings("ab\ntwo\n", p.panes[0].?.file.?.content); - try testing.expectEqualStrings("ab", rd(p, sel, 0, 100).bytes); -} - -test "the flag files read and write the state acme kept behind ctl verbs" { - const gpa = testing.allocator; - const p = try withFile(gpa, "one\ntwo\n"); - defer p.deinit(); - const serial = serialOf(p); - const pane = p.panes[0].?; - const pf = &p.panes[0].?.fs; - const dirty = Node.of(serial, .dirty); - const mark = Node.of(serial, .mark); - const scroll = Node.of(serial, .scroll); - - for ([_]u64{ dirty, mark, scroll }) |node| { - try testing.expectEqual(@as(u64, 2), call(p, .{ .tag = 1, .op = .getattr, .node = node }).reply.attr.size); - for ([_][]const u8{ "", "yes", "2", "0 1", "true" }) |bad| { - try testing.expectEqual(E.INVAL, wr(p, node, bad).errno()); - try testing.expectEqualStrings("bad flag: takes 1 or 0", wr(p, node, bad).reply.ename); - } - } - - try testing.expectEqualStrings("0\n", rd(p, dirty, 0, 8).bytes); - try testing.expectEqualStrings("1\n", rd(p, mark, 0, 8).bytes); - try testing.expectEqualStrings("1\n", rd(p, scroll, 0, 8).bytes); - - try testing.expectEqual(Status.ok, wr(p, dirty, "1\n").reply.status); - try testing.expect(dirtyOf(pane)); - try testing.expectEqualStrings("1\n", rd(p, dirty, 0, 8).bytes); - _ = wr(p, dirty, "0"); - try testing.expect(!dirtyOf(pane)); - - _ = wr(p, mark, "0"); - _ = wr(p, scroll, "0"); - try testing.expect(pf.nomark and pf.noscroll); - try testing.expectEqualStrings("0\n", rd(p, mark, 0, 8).bytes); - try testing.expectEqualStrings("0\n", rd(p, scroll, 0, 8).bytes); - _ = wr(p, mark, "1"); - _ = wr(p, scroll, "1"); - try testing.expect(!pf.nomark and !pf.noscroll); - - // A terminal has no file, so its dirty flag is readable but not writable. - const term = try th.withTerm(gpa); - defer term.deinit(); - try testing.expectEqualStrings("0\n", rd(term, Node.of(serialOf(term), .dirty), 0, 8).bytes); - try testing.expectEqual(E.INVAL, wr(term, Node.of(serialOf(term), .dirty), "1").errno()); -} - test "the writes of one open are one undo step, as bash's line-buffered printf makes them; the next open is another" { const p = try withFile(testing.allocator, "one\n"); defer p.deinit(); diff --git a/src/pardes.zig b/src/pardes.zig index ff64cb9f..b47119c4 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -710,34 +710,6 @@ test "pane-tag Exec prefers Tty and argument builtins before shell fallback" { try std.testing.expectEqualStrings("DefinitelyNotABuiltin\r", sent[0..sent_len]); } -test "Collapse builtin keeps content and restores the pane from its default tag" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 24, .tty_only = true }); - defer p.deinit(); - const pane = try p.setTestFile("keep this text\n"); - p.sync(); - const height = p.rects[0].h; - const revision = pane.file.?.revision; - for ([_][]const u8{ tagline.pane_tail, tagline.file_pane_tail, tagline.pdf_pane_tail, tagline.terminal_pane_tail }) |tail| - try std.testing.expect(std.mem.endsWith(u8, tail, " Collapse Del")); - - try std.testing.expect(p.executeBuiltinLine(0, "Collapse")); - p.sync(); - try std.testing.expect(pane.collapsed); - try std.testing.expectEqual(@as(u16, BOX_H), p.rects[0].h); - try std.testing.expectEqual(@as(f32, @floatFromInt(height)), pane.vweight); - try std.testing.expectEqual(revision, pane.file.?.revision); - try std.testing.expectEqualStrings("keep this text\n", pane.file.?.content); - try std.testing.expect((try p.render(p.scratch.allocator())).cursor == null); - tagline.enterTag(p, pane); - const tag_cursor = (try p.render(p.scratch.allocator())).cursor orelse return error.MissingTagCursor; - try std.testing.expectEqual(p.rects[0].y, tag_cursor.y); - pane.focus = .body; - try std.testing.expect(p.executeBuiltinLine(0, "Collapse")); - p.sync(); - try std.testing.expect(!pane.collapsed); - try std.testing.expectEqual(height, p.rects[0].h); -} - test "LocationsConfig command reports partial updates and survives restore" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); @@ -851,42 +823,6 @@ test "TreeContext command and custom tag survive a workspace round trip" { try std.testing.expectEqualStrings(" Save Keep Del", tagline.curTail(saved)); } -test "Togglettymode builtin and keyboard chord share mode transitions including edited tags" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20, .tty_only = true }); - defer p.deinit(); - const pane = p.panes[0].?; - try std.testing.expect(std.mem.indexOf(u8, tagline.curTail(pane), " Mode ") != null); - try pane.appendTag(" Togglettymode"); - try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); - try std.testing.expect(p.executeBuiltinLine(0, "Togglettymode")); - try std.testing.expectEqual(Text.Mode.normal, pane.body.mode); - p.update(.{ .key = .{ .cp = p.tty_toggle, .ctrl = true } }); - try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); - // The tag has its own mode: the body keeps tty while the tag is typed into. - tagline.enterTag(p, pane); - try std.testing.expect(pane.focus == .tag); - try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); - try std.testing.expect(p.executeBuiltinLine(0, "Togglettymode")); - try std.testing.expectEqual(Text.Mode.normal, pane.body.mode); - try std.testing.expect(pane.focus == .body); - tagline.enterTag(p, pane); - p.update(.{ .key = .{ .cp = p.tty_toggle, .ctrl = true } }); - try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); - try std.testing.expect(pane.focus == .body); - - p.acknowledgePanelPresentation(&.{}); - const tag = try tagline.tagText(p, p.scratch.allocator(), pane); - const x = p.rects[0].x + TAG_TEXT_INSET + @as(u16, @intCast(std.mem.indexOf(u8, tag, "Togglettymode").?)); - const y = p.rects[0].y; - p.update(.{ .mouse = .{ .button = .middle, .kind = .press, .col = x, .row = y } }); - p.update(.{ .mouse = .{ .button = .middle, .kind = .release, .col = x, .row = y } }); - try std.testing.expectEqual(Text.Mode.normal, pane.body.mode); - const file = try p.setTestFile("unchanged\n"); - try std.testing.expect(p.executeBuiltinLine(0, "Togglettymode")); - try std.testing.expectEqual(Text.Mode.normal, file.body.mode); - try std.testing.expectEqualStrings("unchanged\n", file.file.?.content); -} - test "Filter is ordered after Del and toggles only its terminal or +Pager pane" { const testing = std.testing; const del = std.meta.stringToEnum(Builtin, "Del") orelse return error.MissingDelBuiltin; @@ -978,29 +914,6 @@ test "compact tag commands stay beside their own prefix across layout changes" { try std.testing.expectEqualStrings(above_tag, try tagline.tagText(p, p.scratch.allocator(), above)); } -test "Joincol folds the active column into its right neighbor, keeping its panes" { - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - - const right = p.freeSlot().?; - _ = try p.newShell(right, ""); - try std.testing.expect(layout.splitColumn(p, 0, right, false)); - while (p.nextEffect()) |_| {} - try std.testing.expectEqual(@as(usize, 2), p.ncol); - - p.active = 0; // the left column is current - layout.joinCol(p, 0); - try std.testing.expectEqual(@as(usize, 1), p.ncol); - const lf = layout.findPane(p, 0) orelse return error.LostLeftPane; - const rf = layout.findPane(p, right) orelse return error.LostRightPane; - try std.testing.expectEqual(lf.col, rf.col); - - layout.joinCol(p, 0); // no right neighbor left: inert - try std.testing.expectEqual(@as(usize, 1), p.ncol); -} - test "Tty spawns a raw shell in the caller's directory" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); @@ -1282,24 +1195,6 @@ test "first document keeps its originating shell and only opens a column when it } } -test "acme placement: an empty active column takes the new pane whole" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 32 }); - defer p.deinit(); - try std.testing.expectEqual(config.Runtime.Placement.acme, p.settings.placement); - p.newColumn(0); // its tag has the keyboard: it is the active column - tagline.exitHeader(p); - p.sync(); - // A Look from the shell's text still lands in the active column, as - // acme's makenewwindow puts it in activecol first (util.c:456). - _ = try p.openTutorView(1); - exec.placeNew(p, 0, 0, 1, .doc); - try std.testing.expectEqualSlices(usize, &.{1}, p.col_panes[1][0..p.col_n[1]]); - try std.testing.expectEqual(@as(usize, 1), p.active); - try std.testing.expectEqual(@as(?usize, 1), p.activeColumn()); - // Never a new column. - try std.testing.expectEqual(@as(usize, 2), p.ncol); -} - 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(); @@ -1373,38 +1268,6 @@ test "acme placement: a pane made from the middle of a column moves only the one try std.testing.expectEqual(was_bottom.h, p.rects[bottom].h); } -test "acme placement: New and 9P's new take the bottom half of the last pane" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); - defer p.deinit(); - p.newScratchBelow(p.active); - const last = p.active; - p.sync(); - const was = p.rects[last]; - p.newScratchFor(last, .fs); - const made = p.active; - p.sync(); - try std.testing.expectEqual(layout.findPane(p, last).?.idx + 1, layout.findPane(p, made).?.idx); - try std.testing.expectEqual(was.y + was.h, p.rects[made].y + p.rects[made].h); - try std.testing.expect(p.rects[made].y > was.y + BOX_H); -} - -test "Placement pardes brings back the old rules, and says so in DumpConfig" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); - defer p.deinit(); - try std.testing.expect(p.executeBuiltinLine(p.active, "Placement pardes")); - try std.testing.expectEqual(config.Runtime.Placement.pardes, p.settings.placement); - const top = p.active; - p.sync(); - const was = p.rects[top]; - p.newScratchBelow(top); - p.sync(); - // Directly under the asking pane, as New always put it. - try std.testing.expectEqual(@as(usize, 1), layout.findPane(p, p.active).?.idx); - try std.testing.expectEqual(was.y + was.h, p.rects[p.active].y + p.rects[p.active].h); - try std.testing.expect(p.executeBuiltinLine(p.active, "Placement")); - try std.testing.expectEqual(config.Runtime.Placement.acme, p.settings.placement); -} - 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(); @@ -1442,24 +1305,6 @@ test "BootShell keep leaves the untouched boot shell a dragged document joins; r } } -test "a pane its whole text fits in never scrolls to keep a margin" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 30 }); - defer p.deinit(); - const pane = try p.setTestFile("one\ntwo\nthree\n"); - // A second pane under it, as acme's placement puts one: right under the - // three lines, which leaves the file exactly three body rows. - const below = p.freeSlot().?; - _ = try p.openTutorView(below); - exec.placeNew(p, 0, 0, below, .doc); - p.active = 0; - p.sync(); - try std.testing.expectEqual(@as(u16, 3), p.rects[0].h - pane.tag_rows); - for ("jj") |c| p.update(.{ .key = .{ .cp = c, .text = &.{c} } }); - p.sync(); - try std.testing.expectEqual(@as(i32, 2), pane.body.cur_row); - try std.testing.expectEqual(@as(i32, 0), pane.scroll()); -} - test "dragging a column's only pane to another leaves that column in place, empty" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 32 }); defer p.deinit(); @@ -1485,31 +1330,6 @@ test "dragging a column's only pane to another leaves that column in place, empt try std.testing.expectEqual(@as(usize, 1), p.col_n[0]); } -test "pane defaults keep creation of scratch panes in column tags" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - for ([_][]const u8{ tagline.pane_tail, tagline.file_pane_tail, tagline.pdf_pane_tail, tagline.terminal_pane_tail, config.topbar_str }) |tag| { - var words = std.mem.tokenizeScalar(u8, tag, ' '); - while (words.next()) |word| try std.testing.expect(!std.mem.eql(u8, word, "New")); - } - try std.testing.expectEqualStrings("New Tty Find Grep Joincol Delcol", tagline.columnTag(p, 0)); -} - -test "an unsaved file's tag is its name alone: the grip, not a mark, says it is unsaved" { - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const pane = try p.setTestFile("before\n"); - const clean = try tagline.tagText(p, p.scratch.allocator(), pane); - panes.File.setContent(p, &pane.file.?, try gpa.dupe(u8, "after\n")); - const dirty = try tagline.tagText(p, p.scratch.allocator(), pane); - try std.testing.expectEqualStrings(clean, dirty); - try std.testing.expect(std.mem.indexOf(u8, dirty, " *") == null); - // The dirty file and index's flag still say it. - try std.testing.expect(ctlfs.pane.dirtyOf(pane)); -} - test "unknown Exec from a document runs as a command pane in its directory, typed into no terminal" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{}); @@ -2766,18 +2586,6 @@ test "a host with no methods at all is a complete in-process pardes" { try std.testing.expect(p.quit); } -test "a builtin that needs a pane reports capacity failure when every slot is full" { - const p = try Pardes.init(std.testing.allocator, .{ .shells = 3, .cols = 100, .rows = 30 }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - for (0..MAX_PANES) |id| if (p.panes[id] == null) { - _ = try p.newDocPane(id); - }; - const help = std.meta.stringToEnum(Builtin, "Help") orelse return error.MissingHelpBuiltin; - exec.runBuiltin(p, help, 0, "", null); - try std.testing.expect(std.mem.indexOf(u8, p.panes[0].?.msg[0..p.panes[0].?.msg_len], "help: no space for a pane") != null); -} - test "Font keeps requested and acknowledged faces as separate plain state" { if (comptime !font_picker) return; // there is no Font builtin on tty or web const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 }); @@ -2866,26 +2674,6 @@ test "Font size suffix reaches native request and invalid input leaves it intact try std.testing.expectEqual(@as(u16, 1850), p.settings.font.effective_size_hundredths); } -test "Pet command is SDL-only optional and validates its argument" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 }); - defer p.deinit(); - try std.testing.expectEqual(.off, p.settings.pet); - if (comptime platform != .gui) { - // A terminal's view has the word and takes it not. - try std.testing.expect(p.executeBuiltinLine(p.active, "Pet cat")); - try std.testing.expectEqual(.off, p.settings.pet); - return; - } - try std.testing.expect(p.executeBuiltinLine(p.active, "Pet cat")); - try std.testing.expectEqual(.cat, p.settings.pet); - try std.testing.expect(p.executeBuiltinLine(p.active, "Pet dog")); - try std.testing.expectEqual(.cat, p.settings.pet); - try std.testing.expect(p.executeBuiltinLine(p.active, "Pet frog")); - try std.testing.expectEqual(.frog, p.settings.pet); - try std.testing.expect(p.executeBuiltinLine(p.active, "Pet off")); - try std.testing.expectEqual(.off, p.settings.pet); -} - test "startup config overrides restored styling before initFromDump returns" { const source = try Pardes.init(std.testing.allocator, .{ .startup_config = "Theme dark\n" }); defer source.deinit(); @@ -3213,33 +3001,6 @@ fn pardes_ctlfs_ctlText(p: *Pardes, pane: *Pane) []const u8 { return th.rd(p, ctlfs.Node.of(pane.serial, .ctl), 0, 4096).bytes; } -test "a name typed into the tag's path survives Esc, pending, and the next Enter renames; a second Esc drops it" { - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true }); - defer p.deinit(); - const pane = try p.setTestFile("x\n"); - for ([_]bool{ true, false }) |enter| { - tagline.enterTag(p, pane); - pane.tag.mode = .insert; - pane.tag.cur_row = 0; - pane.tag.cur_col = 1; - p.update(.{ .key = .{ .cp = 'z', .text = "z" } }); - try std.testing.expect(pane.prompt == .name); - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expect(pane.prompt == .name); - // A stray key changes nothing while it waits. - p.update(.{ .key = .{ .cp = 'q', .text = "q" } }); - if (enter) { - p.update(.{ .key = .{ .cp = Key.enter } }); - try std.testing.expectEqualStrings("/ztest.txt", pane.file.?.path); - } else { - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expect(pane.prompt != .name); - try std.testing.expectEqualStrings("/ztest.txt", pane.file.?.path); - } - } -} - test "tabbed file aligns syntax cursor and mouse at the nearest insertion gap" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .cols = 60, .rows = 12 }); @@ -3345,21 +3106,6 @@ test "editable workspace and column tags are typed into and persist" { try std.testing.expectEqualStrings("", tagline.columnTag(empty, 0)); } -test "column tags follow their column through split and close" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20, .tty_only = true }); - defer p.deinit(); - _ = try p.setTestFile("left\n"); - p.col_tags[0].own = try p.gpa.dupe(u8, "Left Find"); - _ = exec.execute(p, 0, "Newcol"); - try std.testing.expectEqual(@as(usize, 2), p.ncol); - try std.testing.expectEqualStrings("Left Find", tagline.columnTag(p, 0)); - p.col_tags[1].own = try p.gpa.dupe(u8, "Right Grep"); - p.active = p.col_panes[0][0]; - layout.joinCol(p, 0); - try std.testing.expectEqual(@as(usize, 1), p.ncol); - try std.testing.expectEqualStrings("Right Grep", tagline.columnTag(p, 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(); @@ -3437,43 +3183,6 @@ test "column tag first-cell execution keeps New local to the clicked column" { try std.testing.expectEqualStrings("untouched\n", p.panes[0].?.file.?.content); } -test "header clicks beyond text append and long UTF-8 tags reveal their caret" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 50, .rows = 12, .tty_only = true }); - defer p.deinit(); - _ = try p.setTestFile("body\n"); - p.col_tags[0].own = try p.gpa.dupe(u8, "New"); - p.acknowledgePanelPresentation(&.{}); - p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = 40, .row = TOPBAR_H } }); - p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = 40, .row = TOPBAR_H } }); - p.update(.{ .key = .{ .cp = 'x', .text = " extended" } }); - try std.testing.expectEqualStrings("New extended", tagline.columnTag(p, 0)); - p.update(.{ .paste = " 界界界界界界界界界界界界界界界界界界界界界界界界界界界界界界" }); - const frame = try p.render(p.scratch.allocator()); - try std.testing.expect(p.header_scroll > 0); - try std.testing.expect(frame.cursor != null); - try std.testing.expectEqual(TOPBAR_H, frame.cursor.?.y); - try std.testing.expect(frame.cursor.?.x < p.screen_w); - p.update(.{ .key = .{ .cp = Key.home } }); - _ = try p.render(p.scratch.allocator()); - try std.testing.expectEqual(@as(usize, 0), p.header_scroll); -} - -test "a newline typed into a column tag is a space: the tag stays one row" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 50, .rows = 12, .tty_only = true }); - defer p.deinit(); - _ = try p.setTestFile("body\n"); - p.col_tags[0].own = try p.gpa.dupe(u8, "New"); - p.acknowledgePanelPresentation(&.{}); - p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = 40, .row = TOPBAR_H } }); - p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = 40, .row = TOPBAR_H } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - p.update(.{ .key = .{ .cp = 'S', .text = "Snarf" } }); - _ = try p.render(p.scratch.allocator()); - try std.testing.expectEqualStrings("New Snarf", tagline.columnTag(p, 0)); - try std.testing.expectEqual(COLBAR_H, p.columnBarHeight()); - try std.testing.expectEqual(TOPBAR_H + COLBAR_H, p.rects[0].y); -} - test "tiny screens keep pane space before adding a column tag" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 8, .rows = 2, .tty_only = true }); defer p.deinit(); @@ -3504,18 +3213,6 @@ test "a screen too short for the column row releases a column tag's focus withou try std.testing.expectEqualStrings("unchanged body\n", pane.file.?.content); } -test "a header's execute key runs the word under its cursor and lets go of the keyboard first" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 12, .tty_only = true }); - defer p.deinit(); - _ = try p.setTestFile("body\n"); - tagline.enterHeader(p, null); - p.global_tag.cur_col = @intCast(std.mem.indexOf(u8, config.topbar_str, "Dump").?); - p.update(.{ .key = .{ .cp = Key.tab } }); - try std.testing.expect(!p.header_focus); - try std.testing.expect(p.dump_out != null); - try std.testing.expectEqual(@as(usize, 1), p.col_n[0]); -} - test "s in a header selects in the header's own text" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 12, .tty_only = true }); defer p.deinit(); @@ -3535,20 +3232,6 @@ test "s in a header selects in the header's own text" { try std.testing.expectEqual(@as(u16, 0), pane.body.nsel); } -test "Enter in a header runs the word under its cursor, as the execute key does" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 12, .tty_only = true }); - defer p.deinit(); - _ = try p.setTestFile("body\n"); - try std.testing.expectEqual(@as(usize, 1), p.ncol); - tagline.enterHeader(p, null); - p.global_tag.cur_col = @intCast(std.mem.indexOf(u8, config.topbar_str, "Newcol").?); - p.update(.{ .key = .{ .cp = Key.enter } }); - try std.testing.expectEqual(@as(usize, 2), p.ncol); - // and the workspace tag lets go of the keyboard: the new, empty - // column's tag has it - try std.testing.expect(p.header_focus and p.header_column.? == 1); -} - 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(); @@ -8320,31 +8003,6 @@ pub const Pardes = struct { } }; -test "Esc alternates between two panes of the SAME kind" { - if (platform == .web) return; - const gpa = std.testing.allocator; - var cwdbuf: [4096]u8 = undefined; - const cwd = std.mem.span(@as([*:0]u8, @ptrCast(std.c.getcwd(&cwdbuf, cwdbuf.len) orelse return))); - var pathbuf: [4096]u8 = undefined; - const boot = try std.fmt.bufPrint(&pathbuf, "{s}/mise.toml", .{cwd}); - - const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 40, .file = boot }); - defer p.deinit(); - p.update(.{ .resize = .{ .cols = 80, .rows = 40 } }); - const a = p.active; - exec.runBuiltin(p, .Look, a, "", "build.zig"); - p.sync(); - const b = p.active; - try std.testing.expect(b != a); - try std.testing.expect(!p.panes[a].?.isTerminal() and !p.panes[b].?.isTerminal()); - - for (0..6) |_| { - const was = p.active; - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expectEqual(if (was == a) b else a, p.active); - } -} - test "a boot file that will not open boots an errors pane rather than failing the launch" { if (platform == .web) return; const gpa = std.testing.allocator; @@ -8609,62 +8267,6 @@ test "a prompt is typed on its own line in the notice band and leaves the tag ro try std.testing.expect(pane.prompt == .none and pane.input.own == null); } -test "Newcol makes an empty column, and the next pane made from its tag fills it" { - if (platform == .web) return; - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 }); - defer p.deinit(); - const first = p.active; - const panes_before = p.col_n[0]; - try std.testing.expect(p.executeBuiltinLine(first, "Newcol")); - try std.testing.expectEqual(@as(usize, 2), p.ncol); - try std.testing.expectEqual(@as(usize, 0), p.col_n[1]); - try std.testing.expectEqual(panes_before, p.col_n[0]); - // The keyboard is on the new column's tag; no pane has it. - try std.testing.expect(p.header_focus and p.header_column.? == 1); - try std.testing.expectEqual(first, p.active); - p.sync(); - try std.testing.expect(p.executeBuiltinLine(first, "Tty")); - const shell = p.active; - try std.testing.expect(shell != first); - try std.testing.expectEqual(@as(usize, 1), p.col_n[1]); - try std.testing.expectEqual(shell, p.col_panes[1][0]); - try std.testing.expect(!p.header_focus); - - // The next Tty splits as it always has: nothing is empty any more. - p.sync(); - try std.testing.expect(p.executeBuiltinLine(shell, "Tty")); - try std.testing.expectEqual(@as(usize, 2), p.col_n[1]); -} - -test "a column tag's command acts on that column" { - if (platform == .web) return; - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 }); - defer p.deinit(); - const first = p.active; - try std.testing.expect(p.executeBuiltinLine(first, "Newcol")); - try std.testing.expect(p.executeBuiltinLine(first, "Newcol")); - try std.testing.expectEqual(@as(usize, 3), p.ncol); - p.sync(); - // New from the middle column's tag lands in it, not below `first`. - p.exec_column = 1; - try std.testing.expect(p.executeBuiltinLine(first, "New")); - p.exec_column = null; - try std.testing.expectEqual(@as(usize, 1), p.col_n[1]); - try std.testing.expectEqual(p.active, p.col_panes[1][0]); - // Delcol from the empty right column's tag closes that one. - p.exec_column = 2; - try std.testing.expect(p.executeBuiltinLine(first, "Delcol")); - p.exec_column = null; - try std.testing.expectEqual(@as(usize, 2), p.ncol); - try std.testing.expectEqual(@as(usize, 1), p.col_n[1]); - // A pane's own Delcol closes its column whatever tag has the keyboard. - try std.testing.expect(p.executeBuiltinLine(first, "Newcol")); - try std.testing.expect(p.header_focus); - try std.testing.expect(p.executeBuiltinLine(p.col_panes[1][0], "Delcol")); - try std.testing.expectEqual(@as(usize, 2), p.ncol); - try std.testing.expectEqual(@as(usize, 0), p.col_n[1]); -} - test "Del from a key asks which neighbour takes the rows; nothing else asks" { if (platform == .web) return; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 40 }); @@ -8780,101 +8382,6 @@ test "Del from a key asks which neighbour takes the rows; nothing else asks" { try std.testing.expect(p.panes[low] == null); } -test "a Look that finds nothing says so" { - if (platform == .web) return; - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); - defer p.deinit(); - p.newScratchBelow(p.active); - const id = p.active; - p.sync(); - look.lookAt(p, id, "/no/such/zzqq"); - try std.testing.expectEqualStrings("Look: no match for \"/no/such/zzqq\"", p.panes[id].?.msg[0..p.panes[id].?.msg_len]); -} - -test "focus on a closed pane goes to the pane that takes its place, not the one focused before" { - if (platform == .web) return; - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); - defer p.deinit(); - const shell = p.active; - p.newColumn(shell); - p.newScratchIn(shell, p.activeColumn().?); - const right = p.active; - p.sync(); - p.newScratchBelow(shell); - const low = p.active; - p.sync(); - // The dogfood case: `right` was focused just before `low`, but `shell` - // grows into `low`'s rows, so `shell` gets the focus. - try std.testing.expect(p.executeBuiltinLine(low, "Del")); - p.sync(); - try std.testing.expect(p.panes[low] == null); - try std.testing.expectEqual(shell, p.active); - - // Closing a pane that has no focus leaves the focus where it is. - p.newScratchBelow(shell); - const other = p.active; - p.sync(); - p.active = right; - p.sync(); - try std.testing.expect(p.executeBuiltinLine(other, "Del")); - try std.testing.expectEqual(right, p.active); - - // Delcol: the column that takes the width holds, level with the closed - // pane, the pane that takes its place; `right` was focused just before. - p.newScratchBelow(right); - const right_low = p.active; - p.newScratchBelow(shell); - const left_low = p.active; - p.active = right; - p.sync(); - p.active = left_low; - p.sync(); - try p.removeColumn(layout.findPane(p, left_low).?.col); - p.sync(); - try std.testing.expectEqual(right_low, p.active); -} - -test "closing the last pane in a column leaves the column empty, its tag focused" { - if (platform == .web) return; - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 24 }); - defer p.deinit(); - const shell = p.active; - p.newColumn(shell); - p.newScratchIn(shell, p.activeColumn().?); - const lone = p.active; - try std.testing.expect(lone != shell); - try std.testing.expectEqual(@as(usize, 2), p.ncol); - const at = layout.findPane(p, lone).?; - try std.testing.expectEqual(@as(usize, 1), p.col_n[at.col]); - p.sync(); - - try p.removePane(lone, null); - p.sync(); - - // The column outlives the pane, as acme's does: still two columns, the - // emptied one with no pane and the keyboard on its tag. - try std.testing.expectEqual(@as(usize, 2), p.ncol); - try std.testing.expectEqual(@as(usize, 0), p.col_n[at.col]); - try std.testing.expect(p.panes[lone] == null); - try std.testing.expect(p.header_focus and p.header_column.? == at.col); - try std.testing.expectEqual(shell, p.active); - try std.testing.expect(!p.quit); - - // Down from it goes nowhere: nothing is below. - try std.testing.expect(p.executeBuiltinLine(shell, "Down")); - try std.testing.expect(p.header_focus and p.header_column.? == at.col); - - // ...and Delcol is what actually takes the column away. - try p.removeColumn(at.col); - p.sync(); - try std.testing.expectEqual(@as(usize, 1), p.ncol); - try std.testing.expectEqual(shell, p.active); - - // The session's last pane closing ends it. - try p.removePane(shell, null); - try std.testing.expect(p.quit); -} - test "the bare tty startup opens a text pane under the shell, unfocused" { if (platform == .web) return; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .boot = .tty }); @@ -8944,53 +8451,6 @@ test "tty output follows only from the bottom" { try std.testing.expect(panes.terminal.scrollbar(sp).offset > live); } -test "Esc back into a file leaves its view where it was" { - if (platform == .web) return; - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "src/memory.zig" }); - defer p.deinit(); - // Rows are lines: the centring below counts lines, and a wrapped line - // above the cursor would push it out of the view it centres. - p.settings.wrap = false; - p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); - p.update(.{ .key = .{ .cp = 'n', .alt = true } }); // a shell under the doc - const shell = p.active; - p.update(.{ .key = .{ .cp = Key.escape } }); // back onto the doc - p.sync(); - const doc = p.active; - try std.testing.expect(doc != shell); - const dp = p.panes[doc].?; - try std.testing.expect(dp.file != null); - - // Ride the cursor down until the view has scrolled: it now sits in the - // bottom band, which is exactly where a recenter is visible. - for (0..20) |_| p.update(.{ .key = .{ .cp = 'j' } }); - p.sync(); - const view = dp.file.?.scroll; - const row = dp.body.cur_row; - try std.testing.expect(view > 0); - - p.update(.{ .key = .{ .cp = Key.escape } }); // out to the shell - p.sync(); - try std.testing.expectEqual(shell, p.active); - p.update(.{ .key = .{ .cp = Key.escape } }); // ...and back - p.sync(); - try std.testing.expectEqual(doc, p.active); - - // Nothing moved. The old landing recentred the line under the cursor, so - // coming back repainted the whole screen to show what was already on it. - try std.testing.expectEqual(view, dp.file.?.scroll); - try std.testing.expectEqual(row, dp.body.cur_row); - // ...and the cursor is still on screen, which is all `.nearest` promises. - try std.testing.expect(dp.body.cur_row >= @as(i32, @intCast(view))); - try std.testing.expect(dp.body.cur_row < @as(i32, @intCast(view)) + @as(i32, dp.rows)); - - // The other arm still centres: a look target, a `:NN`, a search hit. - look.focusPaneLine(p, doc, .{ .line = @intCast(row + 1), .col = 1 }, .center); - try std.testing.expect(dp.file.?.scroll != view); - try std.testing.expectEqual(@as(usize, @intCast(row)) -| @as(usize, dp.rows) / 2, dp.file.?.scroll); -} - test "raw tty Esc backs out and Shift-Esc is the program's Esc" { if (platform == .web) return; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24, .file = "mise.toml" }); @@ -9048,43 +8508,6 @@ test "with ProgramEsc raw tty Escape only leaves at a shell prompt" { try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); } -test "Shift-Esc backs out to the previous pane in every mode, never into raw" { - if (platform == .web) return; - const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24, .file = "mise.toml" }); - defer p.deinit(); - p.update(.{ .key = .{ .cp = 'n', .alt = true } }); // a shell under the doc - const shell = p.active; - const sp = p.panes[shell].?; - // A terminal in normal mode: out, still normal, never raw. - if (sp.body.mode == .tty) p.update(.{ .key = .{ .cp = 'b', .ctrl = true } }); - try std.testing.expectEqual(Text.Mode.normal, sp.body.mode); - p.update(.{ .key = .{ .cp = Key.escape, .shift = true } }); - try std.testing.expectEqual(@as(usize, 0), p.active); - try std.testing.expectEqual(Text.Mode.normal, sp.body.mode); - // A text pane in insert mode: out of insert, and out of the pane. - const doc = p.panes[0].?; - p.update(.{ .key = .{ .cp = 'i', .text = "i" } }); - try std.testing.expectEqual(Text.Mode.insert, doc.body.mode); - p.update(.{ .key = .{ .cp = Key.escape, .shift = true } }); - try std.testing.expectEqual(Text.Mode.normal, doc.body.mode); - try std.testing.expect(p.active != 0); -} - -test "Esc in a column or the workspace tag gives the keyboard back to a pane, as Shift-Esc does" { - if (platform == .web) return; - const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24, .file = "mise.toml" }); - defer p.deinit(); - p.update(.{ .key = .{ .cp = 'n', .alt = true } }); // a pane to go back to - for ([_]?usize{ 0, null }) |column| { - tagline.enterHeader(p, column); - try std.testing.expect(tagline.focusedHeader(p) != null); - const was = p.active; - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expect(tagline.focusedHeader(p) == null); - try std.testing.expect(p.active != was); - } -} - 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(); @@ -10026,27 +9449,6 @@ test "core animation keeps wall time at 60, 120 and 144 Hz and across a stall" { try std.testing.expectEqual(@as(?u64, null), p.nextWake()); } -test "a pane closed while its question stands ends the question in the log" { - const th = @import("ninep/testing.zig"); - const p = try th.withFile(std.testing.allocator, "x\n"); - defer p.deinit(); - p.settings.placement = .pardes; - p.newScratchBelow(0); - const closing = p.active; - p.newScratchBelow(closing); - p.sync(); - p.active = closing; // a middle pane: Del from a key asks which side - const serial = p.panes[closing].?.serial; - p.update(.{ .key = .{ .cp = ' ' } }); - p.update(.{ .key = .{ .cp = 'd' } }); - try std.testing.expectEqual(Pane.Prompt.del_side, p.panes[closing].?.prompt); - // A bare answer says what this question takes. - try std.testing.expectEqualStrings("answer takes k j or -", th.wr(p, ctlfs.Node.of(serial, .ctl), "answer\n").reply.ename); - try p.removePane(closing, null); - var want: [64]u8 = undefined; - try std.testing.expect(th.logHas(p, try std.fmt.bufPrint(&want, "answer {d} -\ndel {d}", .{ serial, serial }))); -} - test "a Save the host could not do logs no save record" { const th = @import("ninep/testing.zig"); const p = try th.withFile(std.testing.allocator, "x\n"); @@ -10070,31 +9472,3 @@ test "a Save the host could not do logs no save record" { p.perform(.{ .save_file = .{ .pane = 0 } }); try std.testing.expect(th.logHas(p, "\nsave ")); } - -test "Delcol on the last column leaves the window, drawn and typed into, and Newcol starts it again" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - while (p.ncol > 0) { - p.exec_column = 0; - _ = p.executeBuiltinLine(p.active, "Delcol"); - p.exec_column = null; - p.sync(); - } - try std.testing.expect(!p.quit); - try std.testing.expect(p.anyPane() == null); - // Drawn, and keys go to the workspace tag. - _ = try p.render(p.frame_arena.allocator()); - p.update(.{ .key = .{ .cp = 'x', .text = "x" } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - p.sync(); - _ = try p.render(p.frame_arena.allocator()); - try std.testing.expect(!p.quit); - _ = p.executeBuiltinLine(p.active, "Newcol"); - p.sync(); - try std.testing.expectEqual(@as(usize, 1), p.ncol); - _ = p.executeBuiltinLine(p.active, "New"); - p.sync(); - try std.testing.expect(p.anyPane() != null); - _ = try p.render(p.frame_arena.allocator()); -} diff --git a/test/behaviour.zig b/test/behaviour.zig index f48c6da7..d9b15183 100644 --- a/test/behaviour.zig +++ b/test/behaviour.zig @@ -3,13 +3,25 @@ //! named by the label first. This module imports test/session.zig alone, //! so a test sees the session as a 9P client does. test { + _ = @import("behaviour/addresses.zig"); _ = @import("behaviour/columns_and_tags.zig"); _ = @import("behaviour/edit.zig"); + _ = @import("behaviour/event.zig"); + _ = @import("behaviour/flags.zig"); _ = @import("behaviour/keys.zig"); + _ = @import("behaviour/limits.zig"); + _ = @import("behaviour/log.zig"); _ = @import("behaviour/look_and_exec.zig"); + _ = @import("behaviour/modes.zig"); + _ = @import("behaviour/mounts.zig"); _ = @import("behaviour/mouse.zig"); _ = @import("behaviour/other_files.zig"); _ = @import("behaviour/panes.zig"); + _ = @import("behaviour/placement.zig"); _ = @import("behaviour/root_ctl.zig"); + _ = @import("behaviour/rules.zig"); + _ = @import("behaviour/settings.zig"); _ = @import("behaviour/tags.zig"); + _ = @import("behaviour/terminals.zig"); + _ = @import("behaviour/tree.zig"); } diff --git a/test/behaviour/addresses.zig b/test/behaviour/addresses.zig new file mode 100644 index 00000000..a1474c3d --- /dev/null +++ b/test/behaviour/addresses.zig @@ -0,0 +1,65 @@ +//! docs/typ/reference.typ, Addresses and data <addresses>. +const std = @import("std"); +const Session = @import("session").Session; +const expectEqual = std.testing.expectEqual; + +test "addresses: data and xdata read from addr, data moving it, and a write goes through it" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const addr = s.at("/pane/{d}/addr", .{n}); + const data = s.at("/pane/{d}/data", .{n}); + try s.write(addr, "#0"); + try std.testing.expectEqualStrings("one", try s.readAt(data, 0, 3)); + try expectEqual(.{ @as(u32, 3), @as(u32, 3) }, try s.range(addr)); + try s.write(addr, "1"); + try s.expect(s.at("/pane/{d}/xdata", .{n}), "one\n"); + try s.write(addr, "1"); + try s.expect(data, "one\ntwo\n"); + try s.write(addr, "1"); + try s.write(data, "ONE\n"); + try s.expect(s.at("/pane/{d}/body", .{n}), "ONE\ntwo\n"); + try expectEqual(@as(u32, 4), (try s.range(addr))[0]); +} + +test "addresses: sel reads the selection, and a write to sel replaces it and is selected" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const addr = s.at("/pane/{d}/addr", .{n}); + const dot = s.at("/pane/{d}/dot", .{n}); + const sel = s.at("/pane/{d}/sel", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + // Copying addr onto dot is the whole of acme's `dot=addr`, and back again + // of `addr=dot`; each range file reads exactly what the other takes. + try s.write(addr, "#0,#3"); + try s.write(dot, " 0 3 "); + try s.expect(dot, " 0 3 "); + try expectEqual(@as(u64, 24), (try s.stat(dot)).size); + try s.expect(sel, "one"); + try expectEqual(@as(u64, 3), (try s.stat(sel)).size); + try s.write(addr, "#0"); + try s.write(addr, " 0 3 "); + try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(addr)); + // A truncation empties dot once its open goes, nothing written after + // it; an address expression addresses it. + const h = try s.hold(dot, 1); + try s.truncate(h); + try s.expect(sel, "one"); + s.clunk(h); + try s.expect(sel, ""); + try s.write(dot, "#0,#3"); + try s.write(sel, "ONE"); + try s.expect(body, "ONE\ntwo\n"); + // What was written is selected, as acme's wrsel leaves it. + try s.expect(sel, "ONE"); + // A write of another open replaces it; one open's writes run on. + try s.write(sel, "!"); + try s.expect(body, "!\ntwo\n"); + const w = try s.hold(sel, 1); + try expectEqual(@as(u16, 0), try s.writeFid(w, "a")); + try expectEqual(@as(u16, 0), try s.writeFid(w, "b")); + s.clunk(w); + try s.expect(body, "ab\ntwo\n"); + try s.expect(sel, "ab"); +} diff --git a/test/behaviour/columns_and_tags.zig b/test/behaviour/columns_and_tags.zig index 25ba4c64..d485ee6e 100644 --- a/test/behaviour/columns_and_tags.zig +++ b/test/behaviour/columns_and_tags.zig @@ -1,6 +1,7 @@ //! docs/typ/reference.typ, Columns and tags <columns-and-tags>. const std = @import("std"); const Session = @import("session").Session; +const expectEqualStrings = std.testing.expectEqualStrings; /// /layout's line for the last column. fn lastColumn(s: *Session) ![]const u8 { @@ -31,3 +32,204 @@ test "columns-and-tags: a look after Newcol fills the empty column the keyboard try std.testing.expect(std.mem.indexOf(u8, col, " full ") != null); try std.testing.expect(std.mem.endsWith(u8, col, s.at(" {d}", .{try s.focused()}))); } + +test "columns-and-tags: Joincol folds a column into its right neighbour, keeping its panes, and with none it is inert" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const left = try s.focused(); + const left_col = try s.column(0); + try s.write("/ctl", "Newcol\n"); + const right_col = try s.column(1); + try s.write(s.at("/col/{d}/ctl", .{right_col}), "New\n"); + const right = try s.focused(); + try s.write(s.at("/col/{d}/ctl", .{left_col}), "Joincol\n"); + try expectEqualStrings(s.at("{d}:{d},{d}", .{ right_col, right, left }), try s.columns()); + _ = try s.refused(s.at("/col/{d}/ctl", .{right_col}), "Joincol\n"); + try expectEqualStrings("Joincol: no column to the right", s.said); + try expectEqualStrings(s.at("{d}:{d},{d}", .{ right_col, right, left }), try s.columns()); +} + +test "columns-and-tags: Newcol makes an empty column with the keyboard on its tag, and the next pane made fills it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + try s.write("/ctl", "Newcol\n"); + const left, const col = .{ try s.column(0), try s.column(1) }; + try expectEqualStrings(s.at("{d}:{d} {d}:", .{ left, first, col }), try s.columns()); + try s.expect("/focus", ""); + try s.write(s.at("/pane/{d}/exec", .{first}), "Tty\n"); + const shell = try s.focused(); + try expectEqualStrings(s.at("{d}:{d} {d}:{d}", .{ left, first, col, shell }), try s.columns()); + // The next Tty splits as it always has: nothing is empty any more. + try s.write(s.at("/pane/{d}/exec", .{shell}), "Tty\n"); + try expectEqualStrings(s.at("{d}:{d} {d}:{d},{d}", .{ left, first, col, shell, try s.focused() }), try s.columns()); +} + +test "columns-and-tags: a column's exec acts on that column, and a pane's Delcol on its own" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + try s.write("/ctl", "Newcol\n"); + try s.write("/ctl", "Newcol\n"); + const left, const middle = .{ try s.column(0), try s.column(1) }; + // New from the middle column's tag lands in it, not below the pane + // with the keyboard. + try s.write(s.at("/col/{d}/exec", .{middle}), "New\n"); + const made = try s.focused(); + try expectEqualStrings(s.at("{d}:{d} {d}:{d} {d}:", .{ left, first, middle, made, try s.column(2) }), try s.columns()); + // Delcol from the empty right column's tag closes that one. + try s.write(s.at("/col/{d}/exec", .{try s.column(2)}), "Delcol\n"); + try expectEqualStrings(s.at("{d}:{d} {d}:{d}", .{ left, first, middle, made }), try s.columns()); + // A pane's own Delcol closes its column whatever tag has the keyboard. + try s.write("/ctl", "Newcol\n"); + try s.expect("/focus", ""); + const right = try s.column(2); + try s.write(s.at("/pane/{d}/exec", .{made}), "Delcol\n"); + try expectEqualStrings(s.at("{d}:{d} {d}:", .{ left, first, right }), try s.columns()); +} + +test "columns-and-tags: closing a column's last pane leaves it empty, its tag with the keyboard; Delcol takes it away" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + try s.write("/ctl", "Newcol\n"); + const left, const col = .{ try s.column(0), try s.column(1) }; + try s.write(s.at("/col/{d}/ctl", .{col}), "New\n"); + try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Del\n"); + // The column outlives the pane, as acme's does. + try expectEqualStrings(s.at("{d}:{d} {d}:", .{ left, first, col }), try s.columns()); + try s.expect("/focus", ""); + // Down from it goes nowhere: nothing is below. + try s.write(s.at("/pane/{d}/exec", .{first}), "Down\n"); + try s.expect("/focus", ""); + // The tag with the keyboard is the emptied column's: what is typed lands there. + try s.keys("iMine\x1b"); + try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/col/{d}/tag", .{col})), "Mine") != null); + try s.write(s.at("/col/{d}/ctl", .{col}), "Delcol\n"); + try expectEqualStrings(s.at("{d}:{d}", .{ left, first }), try s.columns()); + try std.testing.expectEqual(first, try s.focused()); +} + +test "columns-and-tags: a column's tag follows its column through Newcol and Joincol" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write(s.at("/col/{d}/tag", .{try s.column(0)}), " Left"); + try s.write(s.at("/pane/{d}/exec", .{try s.focused()}), "Newcol\n"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/col/{d}/tag", .{try s.column(0)})), " Left")); + try s.write(s.at("/col/{d}/tag", .{try s.column(1)}), " Right"); + try s.write(s.at("/col/{d}/ctl", .{try s.column(0)}), "Joincol\n"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/col/{d}/tag", .{try s.column(0)})), " Right")); +} + +test "columns-and-tags: Delcol on the last column leaves the window, drawn and typed into, and Newcol starts it again" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write(s.at("/col/{d}/ctl", .{try s.column(0)}), "Delcol\n"); + try expectEqualStrings("", try s.columns()); + try s.expect("/index", ""); + // Drawn, and keys go to the workspace tag. + try s.keys("x\n"); + try std.testing.expect(std.mem.startsWith(u8, try s.screen(), " Newcol ")); + try s.write("/ctl", "Newcol\n"); + try s.write(s.at("/col/{d}/ctl", .{try s.column(0)}), "New\n"); + try expectEqualStrings(s.at("{d}:{d}", .{ try s.column(0), try s.focused() }), try s.columns()); +} + +test "columns-and-tags: New is a column tag's word, in no pane's tag nor the workspace's" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + var t = try Session.initTerm(std.testing.allocator); + defer t.deinit(); + // The file pane's, read before 9P's new takes the keyboard; a scratch + // made by New, and 9P's new (a +New file pane). + const file = try s.read(s.at("/pane/{d}/tag", .{try s.focused()})); + const made = try s.newPane(); + try s.write(s.at("/col/{d}/exec", .{try s.column(0)}), "New\n"); + const scratch = try s.focused(); + try std.testing.expect(made != scratch); + for ([_][]const u8{ + try s.read("/tag"), + file, + try s.read(s.at("/pane/{d}/tag", .{made})), + try s.read(s.at("/pane/{d}/tag", .{scratch})), + try t.read(t.at("/pane/{d}/tag", .{try t.focused()})), + }) |tag| { + var words = std.mem.tokenizeScalar(u8, tag, ' '); + while (words.next()) |word| try std.testing.expect(!std.mem.eql(u8, word, "New")); + } + try expectEqualStrings("New Tty Find Grep Joincol Delcol", try s.read(s.at("/col/{d}/tag", .{try s.column(0)}))); +} + +test "columns-and-tags: a click past a column tag's text types after it, a newline typed there a space, and a long tag scrolls to its caret" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const tag = s.at("/col/{d}/tag", .{try s.column(0)}); + // Its row, then the pane's tag: the column's stays one row. + const rows = " New Tty Find Grep Joincol Delcol"; + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), rows ++ " ") != null); + try s.click("Delcol", 20, 1); + try s.keys("\nSnarf"); + try expectEqualStrings("New Tty Find Grep Joincol Delcol Snarf", try s.read(tag)); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), rows ++ " Snarf ") != null); + // The pane's tag on the row right under it: the column's is one row. + try std.testing.expectEqual((try s.where(rows))[1] + 1, (try s.where("/test.txt Save"))[1]); + // Wider than the screen: the tag scrolls to keep its caret drawn. + try s.keys(" 界界界界界界界界界界界界界界界界界界界界界界界界界界界界界界"); + const col, const row = (try s.cursor()).?; + try std.testing.expectEqual(@as(usize, 1), row); + try std.testing.expect(col < 80); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), rows) == null); + try s.keys("\u{F00D}"); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), rows) != null); +} + +test "columns-and-tags: Enter in a tag in normal mode runs the word under its cursor, and Tab does too, letting go of the keyboard first" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const first = try s.focused(); + try s.click("Newcol", 1, 1); + try s.keys("\x1b\n"); + try expectEqualStrings(s.at("{d}:{d} {d}:", .{ try s.column(0), first, try s.column(1) }), try s.columns()); + // The workspace tag lets go of the keyboard: the new, empty column's + // tag has it. + try s.expect("/focus", ""); + try s.keys("iMine\x1b"); + try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/col/{d}/tag", .{try s.column(1)})), "Mine") != null); + try s.click("Tutor", 1, 1); + try s.keys("\x1b\t"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/name", .{try s.focused()})), "/+Tutor\n")); +} + +test "columns-and-tags: a tag write refuses NUL and the other control characters, the workspace's, a column's and a pane's" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const col = std.mem.sliceTo(try s.read("/layout"), ' '); + for ([_][]const u8{ "/tag", s.at("/col/{s}/tag", .{col}), s.at("/pane/{d}/tag", .{try s.focused()}) }) |tag| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(tag, " a\x00b")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "invalid tag text")); + try std.testing.expectEqual(@as(u16, 22), try s.refused(tag, " \x7f")); + } +} + +test "columns-and-tags: a refused Delcol changes nothing: no pane, no focus" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/ctl", "Newcol\n"); + const made = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{made}), "/tmp/pardes-delcol-refused.txt\n"); + try s.write(s.at("/pane/{d}/body", .{made}), "edit\n"); + // Its column, /index's last field. + const col = col: { + var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + while (rows.next()) |row| if (std.mem.startsWith(u8, row, s.at("{d} ", .{made}))) break :col row[std.mem.lastIndexOfScalar(u8, row, ' ').? + 1 ..]; + return error.NotListed; + }; + const index = try s.read("/index"); + const layout = try s.read("/layout"); + const focus = try s.read("/focus"); + try std.testing.expect(try s.refused(s.at("/col/{s}/ctl", .{col}), "Delcol\n") != 0); + try s.expect("/index", index); + try s.expect("/layout", layout); + try s.expect("/focus", focus); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "unsaved ") != null); +} diff --git a/test/behaviour/edit.zig b/test/behaviour/edit.zig index e95471c6..3d1a731e 100644 --- a/test/behaviour/edit.zig +++ b/test/behaviour/edit.zig @@ -19,3 +19,13 @@ test "edit: a write of an Edit block carries it whole, to a pane's ctl, the root try s.expect(body, "<foo] x\n<foo] y\nend\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "unmatched") != null); } + +test "edit: a bad command inside an Edit block written whole is named, not the } after it" { + var s = try Session.initFile(std.testing.allocator, "abc\n"); + defer s.deinit(); + const n = try s.focused(); + try std.testing.expect(try s.refused(s.at("/pane/{d}/ctl", .{n}), "Edit {\n,s/a/b/\nzq\n}\n") != 0); + try std.testing.expect(std.mem.indexOf(u8, s.said, "\"}\"") == null); + try std.testing.expect(std.mem.indexOf(u8, s.said, "z") != null); + try s.expect(s.at("/pane/{d}/body", .{n}), "abc\n"); +} diff --git a/test/behaviour/event.zig b/test/behaviour/event.zig new file mode 100644 index 00000000..8aebe8f4 --- /dev/null +++ b/test/behaviour/event.zig @@ -0,0 +1,55 @@ +//! docs/typ/reference.typ, event <event>. +const std = @import("std"); +const Session = @import("session").Session; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +test "event: a pane's own look and exec go to the event reader holding it, which writes them back to act" { + var s = try Session.initFile(std.testing.allocator, "Msg routed\n"); + defer s.deinit(); + const n = try s.focused(); + const ev = try s.hold(s.at("/pane/{d}/event", .{n}), 2); + try s.write(s.at("/pane/{d}/exec", .{n}), "Msg routed\n"); + // Not done: the reader has it, as it would a middle click. + try std.testing.expect(!has(try s.read("/log"), "routed")); + const record = try s.readFid(ev); + // Flag 1: its first word is a builtin; no 2, no expansion follows. + try std.testing.expectEqualStrings("FX0 0 1 10 Msg routed\n", record); + // Written back, it is done. + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ev, record)); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} routed\n", .{n}))); + try s.write(s.at("/pane/{d}/look", .{n}), "one.txt:3\n"); + try std.testing.expectEqualStrings("FL0 0 4 9 one.txt:3\n", try s.readFid(ev)); + // The root's is a click at the pane with the keyboard, this one. + try s.write("/exec", "Msg rooted\n"); + try std.testing.expect(!has(try s.read("/log"), "rooted")); + try std.testing.expectEqualStrings("FX0 0 1 10 Msg rooted\n", try s.readFid(ev)); + // A click in its tag is the reader's too, an `x` record, as acme's is; + // written back, it is done. + try s.write(s.at("/pane/{d}/tagexec", .{n}), "Msg tagged\n"); + try std.testing.expect(!has(try s.read("/log"), "tagged")); + const tagged = try s.readFid(ev); + try std.testing.expectEqualStrings("Fx0 0 1 10 Msg tagged\n", tagged); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ev, tagged)); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} tagged\n", .{n}))); + s.clunk(ev); + // With no reader, the root's and the pane's act at once. + try s.write(s.at("/pane/{d}/exec", .{n}), "Msg direct\n"); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} direct\n", .{n}))); +} + +test "event: a refused Del written back through event fails that write, EIO, with its err" { + var s = try Session.initFile(std.testing.allocator, "text\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/body", .{n}), "more\n"); // unsaved + const ev = try s.hold(s.at("/pane/{d}/event", .{n}), 2); + defer s.clunk(ev); + try s.write(s.at("/pane/{d}/exec", .{n}), "Del\n"); + const record = try s.readFid(ev); + try std.testing.expectEqual(@as(u16, 5), try s.writeFid(ev, record)); + try std.testing.expect(has(s.said, "Modified")); + _ = try s.read(s.at("/pane/{d}/ctl", .{n})); // still there +} diff --git a/test/behaviour/flags.zig b/test/behaviour/flags.zig new file mode 100644 index 00000000..656d219f --- /dev/null +++ b/test/behaviour/flags.zig @@ -0,0 +1,42 @@ +//! docs/typ/reference.typ, Flags and undo <flags>. +const std = @import("std"); +const Session = @import("session").Session; + +test "flags: dirty, mark and scroll read and write the state acme kept behind ctl verbs, 1 or 0" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const dirty = s.at("/pane/{d}/dirty", .{n}); + const mark = s.at("/pane/{d}/mark", .{n}); + const scroll = s.at("/pane/{d}/scroll", .{n}); + for ([_][]const u8{ dirty, mark, scroll }) |flag| { + try std.testing.expectEqual(@as(u64, 2), (try s.stat(flag)).size); + for ([_][]const u8{ "", "yes", "2", "0 1", "true" }) |bad| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(flag, bad)); + try std.testing.expectEqualStrings("bad flag: takes 1 or 0", s.said); + } + } + try s.expect(dirty, "0\n"); + try s.expect(mark, "1\n"); + try s.expect(scroll, "1\n"); + // dirty as /index shows it. + try s.write(dirty, "1\n"); + try s.expect(dirty, "1\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 1 ", .{n})) != null); + try s.write(dirty, "0"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 0 ", .{n})) != null); + try s.write(mark, "0"); + try s.write(scroll, "0"); + try s.expect(mark, "0\n"); + try s.expect(scroll, "0\n"); + try s.write(mark, "1"); + try s.write(scroll, "1"); + try s.expect(mark, "1\n"); + try s.expect(scroll, "1\n"); + // A terminal has no file, so its dirty flag is readable but not writable. + var t = try Session.initTerm(std.testing.allocator); + defer t.deinit(); + const term_dirty = t.at("/pane/{d}/dirty", .{try t.focused()}); + try t.expect(term_dirty, "0\n"); + try std.testing.expectEqual(@as(u16, 22), try t.refused(term_dirty, "1")); +} diff --git a/test/behaviour/keys.zig b/test/behaviour/keys.zig index 81087297..41a2821a 100644 --- a/test/behaviour/keys.zig +++ b/test/behaviour/keys.zig @@ -1,4 +1,5 @@ -//! docs/typ/guide.typ, Keys <keys>. + + const std = @import("std"); const Session = @import("session").Session; @@ -133,3 +134,94 @@ test "keys: a count or a prefix a click or a change of pane cancels leaves nothi } } } + +test "keys: a PDF shown as text turns pages for the moving keys, counted, and the editing keys do nothing" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const pages = (try s.pdfPage(n))[1]; + try std.testing.expect(pages > 3); + // j k, Ctrl-d Ctrl-u, Ctrl-f Ctrl-b: a page each, counted. + for ([_][]const u8{ "2j", "2k", "2\x04", "2\x15", "2\x06", "2\x02" }, 0..) |keys, i| { + try s.keys(keys); + try std.testing.expectEqual(@as(u32, if (i % 2 == 0) 3 else 1), (try s.pdfPage(n))[0]); + } + for ([_]struct { []const u8, u32 }{ .{ "ge", pages }, .{ "gg", 1 }, .{ "3gg", 3 }, .{ "2G", 2 } }) |step| { + try s.keys(step[0]); + try std.testing.expectEqual(step[1], (try s.pdfPage(n))[0]); + } + // Consumed, with their count: the page stays, no selection begins, and + // the next j is one page. + try s.keys("4dv|"); + try std.testing.expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:normal ")); + try s.keys("j"); + try std.testing.expectEqual(@as(u32, 3), (try s.pdfPage(n))[0]); +} + +test "keys: SPC j n and SPC j p in plain text select its places, never a comment's slashes, round the end, opening nothing" { + var s = try Session.initFile(std.testing.allocator, "// ordinary comment\n/// documentation comment\n(//) [///] punctuation\n/\n// /tmp\n//tmp\n"); + defer s.deinit(); + const n = try s.focused(); + const index = try s.read("/index"); + for ([_]struct { []const u8, []const u8, u32 }{ + .{ " jn", "/", 69 }, .{ " jn", "/tmp", 74 }, .{ " jn", "//tmp", 79 }, .{ " jn", "/", 69 }, + .{ " jp", "//tmp", 79 }, .{ " jp", "/tmp", 74 }, .{ " jp", "/", 69 }, + }) |step| { + try s.keys(step[0]); + try std.testing.expectEqual(n, try s.focused()); + try s.expect(s.at("/pane/{d}/sel", .{n}), step[1]); + try std.testing.expectEqual(step[2], (try s.range(s.at("/pane/{d}/dot", .{n})))[0]); + try s.expect("/index", index); + } +} + +/// The value the root ctl's read gives `word`. +fn setting(s: *Session, word: []const u8) ![]const u8 { + var lines = std.mem.splitScalar(u8, try s.read("/ctl"), '\n'); + while (lines.next()) |line| if (std.mem.startsWith(u8, line, word) and line.len > word.len and line[word.len] == ' ') return line[word.len + 1 ..]; + return error.NoSuchSetting; +} + +test "keys: SPC j n and SPC j p walk Themes' rows, whole, never its section headings, and Tab sets the one selected" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const text = try s.focused(); + // An imported theme's, the list still begins with the natives. + try s.write("/ctl", "Theme dark\n"); + try s.write(s.at("/pane/{d}/exec", .{text}), "Themes\n"); + // Listed beside the text, which keeps the keyboard. + try std.testing.expectEqual(text, try s.focused()); + const list = text + 1; + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 0 /+Themes ", .{list})) != null); + const body = try s.read(s.at("/pane/{d}/body", .{list})); + // A heading, then a row a theme; each section after the first a blank + // line and its heading. + var lines = std.mem.splitScalar(u8, body, '\n'); + try std.testing.expectEqualStrings("# Pardes themes", lines.next().?); + var names: std.ArrayList([]const u8) = .empty; + var first_break: ?usize = null; + while (lines.next()) |line| { + if (line.len == 0) { + const heading = lines.next() orelse break; // the newline ending the last row + try std.testing.expect(std.mem.startsWith(u8, heading, "# ")); + first_break = first_break orelse names.items.len; + continue; + } + try std.testing.expect(std.mem.startsWith(u8, line, "Theme ")); + try names.append(s.arena.allocator(), line); + } + const brk = first_break orelse return error.NoSections; + try std.testing.expect(std.mem.indexOf(u8, body[0..std.mem.indexOf(u8, body, "\n\n").?], "\nTheme acme\n") != null); + // Across the first section's end and back, the theme as it was. + for (names.items[0 .. brk + 1]) |row| { + try s.keys(" jn"); + try std.testing.expectEqual(list, try s.focused()); + try s.expect(s.at("/pane/{d}/sel", .{list}), row); + try std.testing.expectEqualStrings("dark", try setting(&s, "Theme")); + } + try s.keys(" jp"); + try s.expect(s.at("/pane/{d}/sel", .{list}), names.items[brk - 1]); + try s.keys("\t"); + try std.testing.expectEqualStrings(names.items[brk - 1]["Theme ".len..], try setting(&s, "Theme")); +} diff --git a/test/behaviour/limits.zig b/test/behaviour/limits.zig new file mode 100644 index 00000000..08c864eb --- /dev/null +++ b/test/behaviour/limits.zig @@ -0,0 +1,18 @@ +//! docs/typ/reference.typ, Limits <limits>. +const std = @import("std"); +const Session = @import("session").Session; + +test "limits: a builtin that needs a pane says so when every slot is taken" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + // Room on the screen for every pane, so the slots run out first. + s.detach(); + try s.write("/ctl", "size 100 400\n"); + var made: usize = 1; + while (s.newPane()) |_| made += 1 else |_| {} + try std.testing.expectEqual(@as(usize, 64), made); + _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "Help\n"); + 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); +} diff --git a/test/behaviour/log.zig b/test/behaviour/log.zig new file mode 100644 index 00000000..21bccdf0 --- /dev/null +++ b/test/behaviour/log.zig @@ -0,0 +1,36 @@ +//! docs/typ/reference.typ, The log <log>. +const std = @import("std"); +const Session = @import("session").Session; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +test "log: a message from a root ctl write is the session's, and its own words stand for its Verbose announcement" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/ctl", "Kill\n"); + const log = try s.read("/log"); + try std.testing.expect(has(log, "\nmsg - Kill: nothing running\n")); + try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, log, "Kill")); +} + +test "log: a builtin that fails a ctl write logs only its err, so the same failure again is counted" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + // Another failure first: the pane's `new` is logged after it. + _ = try s.refused("/ctl", "Unmount yyy\n"); + for (0..3) |_| try std.testing.expect(try s.refused("/ctl", "Unmount zzz\n") != 0); + const log = try s.read("/log"); + try std.testing.expect(std.mem.endsWith(u8, log, "err - ctl: Unmount: no such mount zzz (x3)\n")); + try std.testing.expect(!has(log, "msg - Unmount")); +} + +test "log: a root exec that fails logs its err against the pane it ran at" { + 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}/body", .{n}), "unsaved\n"); + try std.testing.expect(try s.refused("/exec", "Exit\n") != 0); + try std.testing.expect(has(try s.read("/log"), s.at("err {d} exec: ", .{n}))); +} diff --git a/test/behaviour/look_and_exec.zig b/test/behaviour/look_and_exec.zig index 3c76988e..98cab342 100644 --- a/test/behaviour/look_and_exec.zig +++ b/test/behaviour/look_and_exec.zig @@ -37,6 +37,133 @@ test "look-and-exec: a bare :N or :N:M look addresses the pane itself, from its try expectEqual(.{ @as(u32, 0), @as(u32, 6) }, try s.range(dot)); } +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +fn lines(text: []const u8) usize { + return std.mem.count(u8, text, "\n"); +} + +/// Whether /index lists the pane `serial`. +fn listed(s: *Session, serial: u32) !bool { + var it = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + while (it.next()) |line| { + var words = std.mem.tokenizeScalar(u8, line, ' '); + if (std.mem.eql(u8, words.next() orelse "", s.at("{d}", .{serial}))) return true; + } + return false; +} + +test "look-and-exec: a look takes acme's :addr and file:addr, and a miss changes nothing" { + var s = try Session.initFile(std.testing.allocator, "foo x foo y\nbar\n"); + defer s.deinit(); + const n = try s.focused(); + const look = s.at("/pane/{d}/look", .{n}); + const dot = s.at("/pane/{d}/dot", .{n}); + const path = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + // :addr is an address in this pane, evaluated from its dot. + try s.write(look, ":/y/\n"); + try expectEqual(.{ @as(u32, 10), @as(u32, 11) }, try s.range(dot)); + try s.write(look, ":#4\n"); + try expectEqual(.{ @as(u32, 4), @as(u32, 4) }, try s.range(dot)); + try s.write(look, ":/foo/\n"); // from #4: the second foo + try expectEqual(.{ @as(u32, 6), @as(u32, 9) }, try s.range(dot)); + // file:addr names the pane's own file here. + try s.write(look, s.at("{s}:/bar/\n", .{path})); + try expectEqual(.{ @as(u32, 12), @as(u32, 15) }, try s.range(dot)); + // A miss: no pane made, the selection kept, and it says so. + const panes = lines(try s.read("/index")); + try s.write(look, "zzqq\n"); + try expectEqual(panes, lines(try s.read("/index"))); + try expectEqual(.{ @as(u32, 12), @as(u32, 15) }, try s.range(dot)); + try std.testing.expect(has(try s.read("/log"), "no match for \"zzqq\"")); + // A hit answers the pane the text is selected in, not a hits pane. + try s.write(look, "foo\n"); + try s.expect(look, s.at("{d}\n", .{n})); + // A pattern with a blank in it; 0/re/ searches from the top, whatever dot is. + try s.write(look, ":/foo y/\n"); + try expectEqual(.{ @as(u32, 6), @as(u32, 11) }, try s.range(dot)); + try s.write(look, s.at("{s}:0/foo/\n", .{path})); + try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(dot)); + // file:N selects the line; a line past the end moves nothing and reads back empty. + try s.write(look, s.at("{s}:2\n", .{path})); + try expectEqual(.{ @as(u32, 12), @as(u32, 16) }, try s.range(dot)); + try s.write(look, s.at("{s}:99\n", .{path})); + try expectEqual(.{ @as(u32, 12), @as(u32, 16) }, try s.range(dot)); + try s.expect(look, ""); + try std.testing.expect(has(try s.read("/log"), s.at("look: {s}:99 has no line 99\n", .{path}))); +} + +test "look-and-exec: the root look's file:/re/ goes on from the dot, and a miss leaves the dot as it was" { + var s = try Session.initFile(std.testing.allocator, "foo x foo y\nfoo\n"); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + const path = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + for ([_]u32{ 0, 6, 12, 0 }) |at| { + try s.write("/look", s.at("{s}:/foo/\n", .{path})); + try expectEqual(.{ at, at + 3 }, try s.range(dot)); + } + try s.write("/look", s.at("{s}:/zzqq/\n", .{path})); + try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(dot)); +} + +test "look-and-exec: exec runs a builtin at the pane, and look and exec read back the pane it acted on" { + var s = try Session.initFile(std.testing.allocator, "Msg fs-ran\n"); + defer s.deinit(); + const n = try s.focused(); + const own = s.at("{d}\n", .{n}); + try s.write(s.at("/pane/{d}/exec", .{n}), "Msg from exec\n"); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} from exec\n", .{n}))); + try s.expect("/exec", own); + try std.testing.expectEqual(@as(u64, own.len), (try s.stat("/exec")).size); + try s.expect(s.at("/pane/{d}/look", .{n}), own); + try s.write(s.at("/pane/{d}/exec", .{n}), "New\n"); + const made = try s.focused(); + try std.testing.expect(made != n); + try s.expect("/exec", s.at("{d}\n", .{made})); + try s.write(s.at("/pane/{d}/exec", .{made}), "Del\n"); + try std.testing.expect(try listed(&s, n) and !try listed(&s, made)); + try s.expect("/exec", s.at("{d}\n", .{made})); +} + +test "look-and-exec: look and exec read back what the last command touched, a ctl write's too" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write("/exec", "Msg first\n"); + try s.expect("/exec", s.at("{d}\n", .{n})); + // A pane made through ctl is what both read next, not the Msg's pane. + try s.write(s.at("/pane/{d}/ctl", .{n}), "Tty\n"); + const made = try s.focused(); + try std.testing.expect(made != n); + try s.expect("/exec", s.at("{d}\n", .{made})); + try s.expect("/look", s.at("{d}\n", .{made})); + // A ctl write that makes nothing names the pane it ran at. + try s.write("/ctl", "Verbose off\n"); + try s.expect("/exec", s.at("{d}\n", .{made})); + // Closed, it is no pane to go to: a fresh read of look is empty. + try s.write(s.at("/pane/{d}/ctl", .{made}), "Del\n"); + try std.testing.expect(!try listed(&s, made)); + try s.expect("/look", ""); +} + +test "look-and-exec: a line starting with # runs as nothing at an exec or a ctl, silently" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + // The setup's own announcements are made by the first write. + try s.write("/ctl", "# settle\n"); + const index = try s.read("/index"); + const log = try s.read("/log"); + const col = std.mem.sliceTo(try s.read("/layout"), ' '); + for ([_][]const u8{ "/exec", s.at("/pane/{d}/exec", .{n}), "/ctl", s.at("/pane/{d}/ctl", .{n}), s.at("/col/{s}/ctl", .{col}) }) |path| + try s.write(path, "# Platform: tty\n # indented too\n"); + try s.expect("/index", index); + try s.expect("/log", log); +} + test "look-and-exec: a look replaces the selection it was made from, for an address, a range and text found" { var s = try Session.initFile(std.testing.allocator, "alpha\nbravo\ncharlie\nneedle\n"); defer s.deinit(); @@ -93,3 +220,106 @@ test "look-and-exec: a look into another pane leaves the source's dot and replac try expectEqual(.{ @as(u32, 13), @as(u32, 13) }, try s.range(dest_dot)); } } + +test "look-and-exec: a look that finds nothing says so" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.newPane(); + try s.write(s.at("/pane/{d}/look", .{n}), "/no/such/zzqq\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("err {d} look: no match for \"/no/such/zzqq\"\n", .{n})) != null); +} + +/// The log's `err` records, counted. +fn errs(s: *Session) !usize { + return std.mem.count(u8, try s.read("/log"), "\nerr "); +} + +/// The serial of the pane /index names with `suffix`. +fn paneNamed(s: *Session, suffix: []const u8) !u32 { + var rows = std.mem.splitScalar(u8, try s.read("/index"), '\n'); + while (rows.next()) |line| { + // serial kind dirty name column + const name = line[0 .. std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue]; + if (std.mem.endsWith(u8, name, suffix)) return std.fmt.parseInt(u32, line[0..std.mem.indexOfScalar(u8, line, ' ').?], 10); + } + return error.NoSuchPane; +} + +test "look-and-exec: `:N` on a PDF turns to page N, from the pane and from the root's look, and `@pN:N` logs no err" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const look = s.at("/pane/{d}/look", .{n}); + const pages = (try s.pdfPage(n))[1]; + try std.testing.expect(pages >= 3); + try s.write(look, ":2\n"); + try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); + try s.write(look, s.at("@p{d}:1\n", .{n})); + try expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]); + try s.write("/look", s.at("@p{d}:2\n", .{n})); + try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); + try expectEqual(@as(usize, 0), try errs(&s)); + // `:N:H` is hit H of its search on page N, as a search row's + // `file.pdf:N:H` is: with no search there is no hit 3, a miss that turns + // no page. + try s.write(look, ":1\n"); + _ = try s.refused(look, ":2:3\n"); + try expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no search hit 3 on page 2") != null); + try s.write(look, ":2\n"); + _ = try s.refused(look, ":0\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no page 0") != null); + // A byte offset or a pattern is no PDF address: refused with words. + _ = try s.refused(look, ":#3\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "its address is a page, :N") != null); + const before = try errs(&s); + _ = try s.refused(look, s.at(":{d}\n", .{pages + 1})); + try expectEqual(before + 1, try errs(&s)); + try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); +} + +test "look-and-exec: a look at a PDF's page past its last is a miss, never the last page, and opens nothing" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [4096]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + try std.Io.Dir.cwd().copyFile("docs/design.pdf", tmp.dir, "other.pdf", std.testing.io, .{}); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const look = s.at("/pane/{d}/look", .{n}); + const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + // Open: it stays on its page. + try s.write(look, ":2\n"); + _ = try s.refused(look, s.at("{s}:999\n", .{name})); + try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no page 999") != null); + // Not open: nothing opens. + const index = try s.read("/index"); + _ = try s.refused(look, s.at("{s}/other.pdf:999\n", .{dir})); + try s.expect("/index", index); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "other.pdf has no page 999") != null); +} + +test "look-and-exec: a +PdfSections row's first number is the page: a section not there, or on another page, is a miss said" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + try s.write(s.at("/pane/{d}/exec", .{n}), "PdfSections\n"); + const out = try paneNamed(&s, "/+PdfSections"); + const body = try s.read(s.at("/pane/{d}/body", .{out})); + const row = body[0..std.mem.indexOfScalar(u8, body, '\n').?]; + const place = row[0..std.mem.indexOfScalar(u8, row, ' ').?]; // design.pdf:P:S + var fields = std.mem.splitScalar(u8, place, ':'); + _ = fields.next(); + const page = try std.fmt.parseInt(u32, fields.next().?, 10); + const look = s.at("/pane/{d}/look", .{out}); + // As listed: found, no miss. + try s.write(look, s.at("{s}\n", .{place})); + try expectEqual(@as(usize, 0), try errs(&s)); + // The section on another page than the row says, and one not there. + _ = try s.refused(look, s.at("design.pdf:{d}:1\n", .{page + 1})); + try expectEqual(@as(usize, 1), try errs(&s)); + _ = try s.refused(look, "design.pdf:1:999\n"); + try expectEqual(@as(usize, 2), try errs(&s)); +} diff --git a/test/behaviour/modes.zig b/test/behaviour/modes.zig new file mode 100644 index 00000000..dde3ffb6 --- /dev/null +++ b/test/behaviour/modes.zig @@ -0,0 +1,104 @@ +//! docs/typ/guide.typ, Modes <modes>. +const std = @import("std"); +const Session = @import("session").Session; +const expectEqual = std.testing.expectEqual; + +fn mode(s: *Session, pane: u32) ![]const u8 { + const ctl = try s.read(s.at("/pane/{d}/ctl", .{pane})); + const at = std.mem.lastIndexOf(u8, ctl, " mode:").? + " mode:".len; + return std.mem.trimEnd(u8, ctl[at..], " "); +} + +test "modes: Esc in normal mode alternates between two panes of the same kind" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = "b\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, "a\n"); + defer s.deinit(); + const a = try s.focused(); + try s.write("/look", s.at("{s}/b.txt\n", .{at})); + const b = try s.focused(); + try std.testing.expect(a != b); + for (0..6) |_| { + const was = try s.focused(); + try s.keys("\x1b"); + try expectEqual(if (was == a) b else a, try s.focused()); + } +} + +test "modes: Esc in a column or the workspace tag gives the keyboard back to a pane, as Shift-Esc does" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + _ = try s.newPane(); + for ([_][]const u8{ "New Tty", "Newcol" }) |tag| { + const was = try s.focused(); + // A click types into the tag; Esc is normal mode there, the next + // one is out of it. + try s.click(tag, 0, 1); + try s.keys("\x1b"); + try s.expect("/focus", ""); + try s.keys("\x1b"); + try std.testing.expect(was != try s.focused()); + } +} + +test "modes: Shift-Esc backs out to the previous pane in every mode, never into raw" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const doc = try s.focused(); + try s.keys("\u{F015}n"); // a shell under the doc + const shell = try s.focused(); + if (std.mem.eql(u8, try mode(&s, shell), "tty")) try s.keys("\x02"); + try std.testing.expectEqualStrings("normal", try mode(&s, shell)); + // A terminal in normal mode: out, still normal, never raw. + try s.keys("\u{F016}\x1b"); + try expectEqual(doc, try s.focused()); + try std.testing.expectEqualStrings("normal", try mode(&s, shell)); + // A text pane in insert mode: out of insert, and out of the pane. + try s.keys("i"); + try std.testing.expectEqualStrings("insert", try mode(&s, doc)); + try s.keys("\u{F016}\x1b"); + try std.testing.expectEqualStrings("normal", try mode(&s, doc)); + try std.testing.expect(doc != try s.focused()); +} + +/// The first line number drawn under the tag that starts with `tag`. +fn topLine(s: *Session, tag: []const u8) !usize { + const row = (try s.where(tag))[1]; + var rows = std.mem.splitScalar(u8, try s.screen(), '\n'); + for (0..row + 1) |_| _ = rows.next(); + var words = std.mem.tokenizeScalar(u8, rows.next().?, ' '); + return std.fmt.parseInt(usize, words.next().?, 10); +} + +test "modes: Esc back into a file leaves its view where it was, and a look centres its line" { + var s = try Session.initFile(std.testing.allocator, std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*)))); + defer s.deinit(); + const doc = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{doc}); + try s.keys("\u{F015}n"); // a shell under the doc + const shell = try s.focused(); + try s.keys("\x1b"); + try expectEqual(doc, try s.focused()); + // Ride the cursor down until the view has scrolled. + try s.keys(&@as([20]u8, @splat('j'))); + const top = try topLine(&s, "/test.txt"); + const at = try s.range(dot); + try std.testing.expect(top > 1); + try s.keys("\x1b"); + try expectEqual(shell, try s.focused()); + try s.keys("\x1b"); + try expectEqual(doc, try s.focused()); + // Nothing moved: the old landing recentred the line under the cursor. + try expectEqual(top, try topLine(&s, "/test.txt")); + try expectEqual(at, try s.range(dot)); + // The other arm still centres: a look's `:NN`. + const line = 60; + try s.write(s.at("/pane/{d}/look", .{doc}), s.at(":{d}\n", .{line})); + const rows = (try s.where("Tty Save Mode"))[1] - (try s.where("/test.txt"))[1] - 1; + // Drawn mid-pane, a row either way. + const drawn_at = line - try topLine(&s, "/test.txt"); + try std.testing.expect(drawn_at + 1 >= rows / 2 and drawn_at <= rows / 2 + 1); +} diff --git a/test/behaviour/mounts.zig b/test/behaviour/mounts.zig new file mode 100644 index 00000000..57685a38 --- /dev/null +++ b/test/behaviour/mounts.zig @@ -0,0 +1,25 @@ +//! docs/typ/reference.typ, Paths and mounts <mounts>. +const std = @import("std"); +const Session = @import("session").Session; +const expectEqual = std.testing.expectEqual; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +test "mounts: Mount with no dial is wrong #args, any other x!y a bad dial address, a socket not there no such socket, and none mounts anything" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try expectEqual(@as(u16, 22), try s.refused("/ctl", "Mount work\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "wrong #args")); + try expectEqual(@as(u16, 22), try s.refused("/ctl", "Mount work tcp!nohost\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "Mount: bad dial address: tcp!nohost")); + try expectEqual(@as(u16, 2), try s.refused("/ctl", "Mount peer unix!/tmp/pardes-surely-no-socket\n")); + try std.testing.expect(has(s.said, "no such socket")); + try expectEqual(@as(u16, 22), try s.refused("/ctl", "Mount peer rel/x.sock\n")); + // Nothing was mounted under either name. + for ([_][]const u8{ "Unmount work\n", "Unmount peer\n" }) |line| { + try expectEqual(@as(u16, 2), try s.refused("/ctl", line)); + try std.testing.expect(has(s.said, "no such mount")); + } +} diff --git a/test/behaviour/mouse.zig b/test/behaviour/mouse.zig index 13c9b631..0abe15dc 100644 --- a/test/behaviour/mouse.zig +++ b/test/behaviour/mouse.zig @@ -70,10 +70,10 @@ test "mouse: a click past a line's or the file's end puts the caret where insert try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + 2 + 15, y + case.click_row, x + 2 + 15, y + case.click_row }); try std.testing.expectEqual(.{ case.dot, case.dot }, try s.range(s.at("/pane/{d}/dot", .{n}))); // The caret is drawn where it is, and insertion draws it there too. - try std.testing.expectEqual(.{ x + 2 + case.col, y + case.row }, try s.cursor()); + try std.testing.expectEqual(.{ x + 2 + case.col, y + case.row }, (try s.cursor()).?); if (!already_insert) try s.keys("i"); try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:insert ")); - try std.testing.expectEqual(.{ x + 2 + case.col, y + case.row }, try s.cursor()); + try std.testing.expectEqual(.{ x + 2 + case.col, y + case.row }, (try s.cursor()).?); try s.keys("Z"); try s.expect(s.at("/pane/{d}/body", .{n}), case.result); } @@ -99,9 +99,9 @@ test "mouse: a click on a tab's or a wrapped wide glyph's cells puts the caret o const at: u32 = @intCast(try std.unicode.utf8CountCodepoints(source[0..std.mem.indexOf(u8, source, "界").?])); try std.testing.expectEqual(.{ at, at }, try s.range(s.at("/pane/{d}/dot", .{n}))); // Drawn on the glyph's first cell, in insert mode too. - try std.testing.expectEqual(.{ x, y }, try s.cursor()); + try std.testing.expectEqual(.{ x, y }, (try s.cursor()).?); if (!already_insert) try s.keys("i"); - try std.testing.expectEqual(.{ x, y }, try s.cursor()); + try std.testing.expectEqual(.{ x, y }, (try s.cursor()).?); try s.keys("Z"); const byte = std.mem.indexOf(u8, source, "界").?; try s.expect(s.at("/pane/{d}/body", .{n}), s.at("{s}Z{s}", .{ source[0..byte], source[byte..] })); diff --git a/test/behaviour/other_files.zig b/test/behaviour/other_files.zig index c0f9c08f..ef1fab7e 100644 --- a/test/behaviour/other_files.zig +++ b/test/behaviour/other_files.zig @@ -28,3 +28,55 @@ test "other-files: kbdin's Kalt rune is Alt on the key after it alone, and an Al try s.keys("i\u{F015}zy\x1b"); try s.expect(s.at("/pane/{d}/body", .{try s.focused()}), "yx\n"); } + +test "other-files: pager answers its +Pager on the open that asked, whatever another client exec'd meanwhile, and refuses a directory it cannot page in" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const asked = try s.hold("/pager", 2); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(asked, "/tmp\n")); + // Another client execs between the request and its read: a pane of its own. + const racer = try s.hold("/exec", 2); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(racer, "New\n")); + s.clunk(racer); + const paged = try s.readFid(asked); + s.clunk(asked); + // A fresh open reads the session's last answer, the racer's pane. + try std.testing.expect(!std.mem.eql(u8, paged, try s.read("/exec"))); + try s.expect(s.at("/pane/{s}/name", .{std.mem.trimEnd(u8, paged, "\n")}), "/tmp/+Pager\n"); + // Refused, an open answers nothing, not the session's last answer. + const refused = try s.hold("/pager", 2); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(refused, "relative\n")); + try std.testing.expectEqualStrings("", try s.readFid(refused)); + s.clunk(refused); + // Resolved: `/tmp/../tmp/.` is /tmp, a +Pager of its own beside the + // one made above, which keeps its page. + const resolved = try s.hold("/pager", 2); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(resolved, "/tmp/../tmp/.\n")); + const second = try s.readFid(resolved); + s.clunk(resolved); + try std.testing.expect(!std.mem.eql(u8, paged, second)); + try s.expect(s.at("/pane/{s}/name", .{std.mem.trimEnd(u8, second, "\n")}), "/tmp/+Pager\n"); + try s.expect(s.at("/pane/{s}/name", .{std.mem.trimEnd(u8, paged, "\n")}), "/tmp/+Pager\n"); + // One that may not be written is refused, permission denied (Linux's /proc/1). + try std.testing.expectEqual(@as(u16, 1), try s.refused("/pager", "/proc/1\n")); + // One directory an open: one refused is not followed by another, as a + // mount sends two lines in two writes. + const twice = try s.hold("/pager", 2); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(twice, "relative\n")); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(twice, "/tmp\n")); + s.clunk(twice); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/pager", "relative\n")); + try std.testing.expectEqualStrings("pager: invalid directory \"relative\": must be absolute", s.said); + try std.testing.expectEqual(@as(u16, 2), try s.refused("/pager", "/tmp/pardes-surely-missing-dir\n")); + try std.testing.expectEqual(@as(u16, 20), try s.refused("/pager", "/etc/passwd\n")); +} + +test "other-files: /recent keeps a PDF as it keeps a file: open, then closed" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/recent"), s.at("open {s}\n", .{name})) != null); + 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); +} diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index 01820a07..a46f8432 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -49,3 +49,317 @@ test "panes: Putall saves every pane with unsaved edits to a file, one each, and try s.write("/ctl", "Putall\n"); try std.testing.expectEqual(@as(usize, 1), s.saved); } + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +test "panes: focus reads the pane with the keyboard, and a serial written to it moves the keyboard there, and only that" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + const second = try s.newPane(); + try s.expect("/focus", s.at("{d}\n", .{second})); + // A stat says the length a read gives. + try std.testing.expectEqual(@as(u64, s.at("{d}\n", .{second}).len), (try s.stat("/focus")).size); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{first})), " notcurrent ")); + // A column's tag has the keyboard: no pane does, and a write takes it back. + try s.write("/ctl", "Newcol\n"); + try s.expect("/focus", ""); + try std.testing.expectEqual(@as(u64, 0), (try s.stat("/focus")).size); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " notcurrent ")); + try s.write("/focus", s.at("{d}\n", .{first})); + try s.expect("/focus", s.at("{d}\n", .{first})); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{first})), " current ")); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " notcurrent ")); + try std.testing.expectEqual(@as(u16, 2), try s.refused("/focus", "99999\n")); + try std.testing.expectEqualStrings("no such pane", s.said); + for ([_][]const u8{ "", "two", "-1", "1 2" }) |bad| try std.testing.expectEqual(@as(u16, 22), try s.refused("/focus", bad)); + try s.expect("/focus", s.at("{d}\n", .{first})); + // Focus gives the keyboard only: a folded pane stays folded. + try s.write(s.at("/pane/{d}/ctl", .{second}), "Collapse\n"); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " collapsed ")); + try s.write("/focus", s.at("{d}", .{second})); + try s.expect("/focus", s.at("{d}\n", .{second})); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " collapsed ")); +} + +test "panes: the pane ctl takes acme's verbs and the pane's builtins, and refuses the rest in Plan 9's words" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + // A builtin is its own capitalised word: `GET` and `DEL` are none. + for ([_][]const u8{ "lock x", "bogus", "DEL", "GET", "get x", "look /tmp", "exec Del" }) |bad| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, s.at("{s}\n", .{bad}))); + try std.testing.expectEqualStrings(s.at("unknown control message \"{s}\"", .{std.mem.sliceTo(bad, ' ')}), s.said); + } + _ = try s.refused(ctl, "Newcol\n"); + try std.testing.expectEqualStrings("not a window control message \"Newcol\": write it to /ctl", s.said); + _ = try s.refused(ctl, "Verbose off\n"); + try std.testing.expectEqualStrings("not a window control message \"Verbose\": write it to /ctl", s.said); + _ = try s.refused(ctl, "Collapse now\n"); + try std.testing.expectEqualStrings("wrong #args in control message \"Collapse\"", s.said); + // Checked whole first: a bad line anywhere and nothing ran. + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Msg first\nbogus\n")); + // A pane's builtin runs at that pane. + try s.write(ctl, "Msg from ctl\n"); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} from ctl\n", .{n}))); + // One that means nothing bare is refused before anything runs. + _ = try s.refused(ctl, "Find\n"); + try std.testing.expectEqualStrings("wrong #args in control message \"Find\"", s.said); + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Msg first\nMsg\n")); + try std.testing.expect(!has(try s.read("/log"), "first")); + // Save with no name on a scratch would ask for one: refused, not asked. + const other = try s.newPane(); + _ = try s.refused(s.at("/pane/{d}/ctl", .{other}), "Save\n"); + try std.testing.expectEqualStrings("control message needs its argument \"Save\"", s.said); + // A line after the one that closed the pane has nowhere to run. + try std.testing.expectEqual(@as(u16, 2), try s.refused(s.at("/pane/{d}/ctl", .{other}), "Del\nMsg after\n")); + try std.testing.expectError(error.Refused, s.read(s.at("/pane/{d}/ctl", .{other}))); +} + +test "panes: a second lock is refused until the holder unlocks or closes, and binds nobody else" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const a = try s.hold(ctl, 2); + const b = try s.hold(ctl, 1); + // An open that only reads the status line holds nothing to lock with. + const reader = try s.hold(ctl, 0); + try std.testing.expect(try s.writeFid(reader, "lock\n") != 0); + s.clunk(reader); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\n")); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\n")); + // Another open's lock is refused at once, and so is its unlock; writes + // to the pane's other files are not. + try std.testing.expectEqual(@as(u16, 16), try s.writeFid(b, "lock\n")); + try std.testing.expectEqualStrings("file in use: another open of this ctl holds the lock", s.said); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(b, "unlock\n")); + try std.testing.expectEqualStrings("pane not locked by this open", s.said); + try s.write(s.at("/pane/{d}/addr", .{n}), "1"); + try s.write(s.at("/pane/{d}/data", .{n}), "ONE\n"); + try s.expect(s.at("/pane/{d}/body", .{n}), "ONE\ntwo\n"); + // Once the holder unlocks, the other's try takes it. + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "unlock\n")); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(b, "lock\n")); + try std.testing.expectEqual(@as(u16, 16), try s.writeFid(a, "lock\nunlock\n")); + // Closing the holder gives it up. + s.clunk(b); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\nunlock\nlock\n")); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(a, "unlock\nunlock\n")); + // Still a's: another open is refused it. + const c = try s.hold(ctl, 1); + try std.testing.expectEqual(@as(u16, 16), try s.writeFid(c, "lock\n")); + s.clunk(c); + // The lock lives and dies with the pane. + const other = try s.newPane(); + const d = try s.hold(s.at("/pane/{d}/ctl", .{other}), 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(d, "lock\n")); + try s.write(s.at("/pane/{d}/ctl", .{other}), "Del\n"); + try std.testing.expectEqual(@as(u16, 2), try s.writeFid(d, "lock\n")); + s.clunk(d); + s.clunk(a); + // a's close gave its lock up too. + const e = try s.hold(ctl, 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(e, "lock\n")); + s.clunk(e); +} + +test "panes: an unsaved file's tag is its name alone, its dirty file says it is unsaved" { + var s = try Session.initFile(std.testing.allocator, "before\n"); + defer s.deinit(); + const n = try s.focused(); + const tag = s.at("/pane/{d}/tag", .{n}); + const clean = try s.read(tag); + try s.write(s.at("/pane/{d}/body", .{n}), "after\n"); + try std.testing.expectEqualStrings(clean, try s.read(tag)); + try std.testing.expect(std.mem.indexOf(u8, clean, " *") == null); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n"); +} + +test "panes: a pane closed while its question stands ends the question in the log" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/ctl", "Placement pardes\n"); + const top = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{top}), "New\n"); + const closing = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{closing}), "New\n"); + // A middle pane: Del from a key asks which side. + try s.write("/focus", s.at("{d}\n", .{closing})); + try s.keys(" d"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("ask {d} del k j\n", .{closing})) != null); + // A bare answer says what this question takes. + const ctl = s.at("/pane/{d}/ctl", .{closing}); + _ = try s.refused(ctl, "answer\n"); + try std.testing.expectEqualStrings("answer takes k j or -", s.said); + try s.write(ctl, "Del\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("answer {d} -\ndel {d}", .{ closing, closing })) != null); +} + +test "panes: focus on a closed pane goes to the pane that takes its place, not the one focused before" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const left = try s.focused(); + try s.write("/ctl", "Newcol\n"); + try s.write(s.at("/col/{d}/ctl", .{try s.column(1)}), "New\n"); + const right = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{left}), "New\n"); + const low = try s.focused(); + // `right` was focused just before `low`, but `left` grows into `low`'s + // rows, so `left` gets the focus. + try s.write(s.at("/pane/{d}/ctl", .{low}), "Del\n"); + try std.testing.expectEqual(left, try s.focused()); + // Closing a pane that has no focus leaves the focus where it is. + try s.write(s.at("/pane/{d}/exec", .{left}), "New\n"); + const other = try s.focused(); + try s.write("/focus", s.at("{d}\n", .{right})); + try s.write(s.at("/pane/{d}/ctl", .{other}), "Del\n"); + try std.testing.expectEqual(right, try s.focused()); + // Delcol: the column that takes the width holds, level with the closed + // pane, the pane that takes its place; `right` was focused just before. + try s.write(s.at("/pane/{d}/exec", .{right}), "New\n"); + const right_low = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{left}), "New\n"); + const left_low = try s.focused(); + try s.write("/focus", s.at("{d}\n", .{right})); + try s.write("/focus", s.at("{d}\n", .{left_low})); + try s.write(s.at("/col/{d}/ctl", .{try s.column(0)}), "Delcol\n"); + try std.testing.expectEqual(right_low, try s.focused()); +} + +test "panes: Collapse keeps the text and folds the pane to its tag, and again restores it" { + var s = try Session.initFile(std.testing.allocator, "keep this text\n"); + defer s.deinit(); + var t = try Session.initTerm(std.testing.allocator); + defer t.deinit(); + const n = try s.focused(); + for ([_][]const u8{ + try s.read(s.at("/pane/{d}/tag", .{n})), + try s.read(s.at("/pane/{d}/tag", .{try s.newPane()})), + try t.read(t.at("/pane/{d}/tag", .{try t.focused()})), + }) |tag| try std.testing.expect(std.mem.endsWith(u8, tag, " Collapse Del")); + try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Del\n"); + const ctl = s.at("/pane/{d}/ctl", .{n}); + try s.write(ctl, "Collapse\n"); + // Folded to its tag: no text drawn, no cursor in it, the text kept. + try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), " collapsed ") != null); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), "keep this text") == null); + try std.testing.expectEqual(null, try s.cursor()); + try s.expect(s.at("/pane/{d}/body", .{n}), "keep this text\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); + // Its tag still takes the keyboard, the cursor on the tag's row. + try s.keys(":"); + try std.testing.expectEqual((try s.where("/test.txt"))[1], (try s.cursor()).?[1]); + try s.write(ctl, "Collapse\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), " collapsed ") == null); + try std.testing.expectEqual((try s.where("/test.txt"))[1] + 1, (try s.where("1 keep this text"))[1]); +} + +test "panes: a pane its whole text fits in never scrolls to keep a margin" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\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(); + const n = try s.focused(); + // A second pane under it, as acme's placement puts one: right under the + // three lines, which leaves the file exactly three body rows. + try s.write(s.at("/pane/{d}/look", .{n}), s.at("{s}/f.txt\n", .{at})); + try s.write("/focus", s.at("{d}\n", .{n})); + try s.keys("jj"); + const top = (try s.where("/test.txt"))[1]; + try std.testing.expectEqual(top + 4, (try s.where(at[0..@min(at.len, 20)]))[1]); + // On the last line, and line 1 still the first drawn. + try std.testing.expectEqual(.{ @as(u32, 8), @as(u32, 8) }, try s.range(s.at("/pane/{d}/dot", .{n}))); + try std.testing.expectEqual(top + 1, (try s.where("1 one"))[1]); +} + +/// `name` in `dir`: a one-page PDF with no outline and no text. +fn plainPdf(dir: std.Io.Dir, name: []const u8) !void { + const objects = [_][]const u8{ + "<< /Type /Catalog /Pages 2 0 R >>", + "<< /Type /Pages /Count 1 /Kids [3 0 R] >>", + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << >> >>", + }; + var buf: [1024]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + var at: [objects.len]usize = undefined; + try w.writeAll("%PDF-1.4\n"); + for (objects, 0..) |object, i| { + at[i] = w.end; + try w.print("{d} 0 obj\n{s}\nendobj\n", .{ i + 1, object }); + } + const xref = w.end; + try w.print("xref\n0 {d}\n0000000000 65535 f \n", .{objects.len + 1}); + for (at) |off| try w.print("{d:0>10} 00000 n \n", .{off}); + try w.print("trailer\n<< /Size {d} /Root 1 0 R >>\nstartxref\n{d}\n%%EOF\n", .{ objects.len + 1, xref }); + try dir.writeFile(std.testing.io, .{ .sub_path = name, .data = w.buffered() }); +} + +test "panes: a PDF's body is the text of the page shown, each line of it a line, never a heading run into the text under it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const body = s.at("/pane/{d}/body", .{n}); + try std.testing.expect(std.mem.startsWith(u8, try s.read(body), "Pardes: A Text Environment\nArchitecture and implementation of the rewrite\n")); + try s.write(s.at("/pane/{d}/look", .{n}), ":2\n"); + try std.testing.expect(!std.mem.startsWith(u8, try s.read(body), "Pardes: A Text Environment\n")); +} + +test "panes: PdfTint steps a PDF's tint, read in its ctl and never in its tag, and only a PDF pane takes it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const text = try s.focused(); + const n = try s.lookFile("docs/design.pdf"); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const tag = try s.read(s.at("/pane/{d}/tag", .{n})); + try std.testing.expect(std.mem.endsWith(u8, tag, " PdfSections PdfTint PdfFit Collapse Del")); + for ([_][]const u8{ "full", "disabled", "filtered" }) |want| { + try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), "fit:width ") != null); + try s.write(ctl, "PdfTint\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), s.at(" tint:{s} ", .{want})) != null); + try s.expect(s.at("/pane/{d}/tag", .{n}), tag); + // Another pane's PdfTint is refused and leaves the PDF's alone. + try std.testing.expectEqual(@as(u16, 5), try s.refused(s.at("/pane/{d}/ctl", .{text}), "PdfTint\n")); + try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), s.at(" tint:{s} ", .{want})) != null); + } +} + +test "panes: PdfSections on a PDF with no outline says so and opens nothing" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try plainPdf(tmp.dir, "plain.pdf"); + 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 n = try s.lookFile(s.at("{s}/plain.pdf", .{dir})); + try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[1]); + const index = try s.read("/index"); + for (0..2) |_| { + _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "PdfSections\n"); + try s.expect("/index", index); + } + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "PdfSections: this PDF has no outline") != null); +} + +test "panes: a Mini that cannot read its file leaves the panes, the last Mini's snapshot and the keyboard as they were" { + var s = try Session.initFile(std.testing.allocator, "abc\n"); + defer s.deinit(); + const text = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{text}); + try s.write(exec, s.at("Mini /virtual/pane/{d}/body\n", .{text})); + const mini = try s.focused(); + try std.testing.expect(mini != text); + const body = try s.read(s.at("/pane/{d}/body", .{mini})); + const index = try s.read("/index"); + try std.testing.expectEqual(@as(u16, 2), try s.refused(exec, "Mini /virtual/does-not-exist\n")); + try std.testing.expectEqual(mini, try s.focused()); + try s.expect("/index", index); + try s.expect(s.at("/pane/{d}/body", .{mini}), body); +} diff --git a/test/behaviour/placement.zig b/test/behaviour/placement.zig new file mode 100644 index 00000000..079b5a5e --- /dev/null +++ b/test/behaviour/placement.zig @@ -0,0 +1,61 @@ +//! docs/typ/reference.typ, Placement <placement>. +const std = @import("std"); +const Session = @import("session").Session; + +test "placement: an empty active column takes the new pane whole, a look from another column's text too" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\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, "x\n"); + defer s.deinit(); + const first = try s.focused(); + // Its tag has the keyboard: it is the active column, and stays so + // when Esc gives the keyboard back. + try s.write("/ctl", "Newcol\n"); + try s.keys("\x1b"); + try std.testing.expectEqual(first, try s.focused()); + // A look from the first pane's text still lands in the active column, + // as acme's makenewwindow puts it in activecol first (util.c:456). + try s.write(s.at("/pane/{d}/look", .{first}), s.at("{s}/f.txt\n", .{at})); + const made = try s.focused(); + try std.testing.expectEqualStrings(s.at("{d}:{d} {d}:{d}", .{ try s.column(0), first, try s.column(1), made }), try s.columns()); +} + +/// The screen row the tag starting `name` is drawn on. +fn tagRow(s: *Session, name: []const u8) !usize { + return (try s.where(s.at("{s} Save", .{name})))[1]; +} + +test "placement: New and 9P's new take the bottom half of the column's last pane" { + 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}), "New\n"); + const last = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{last}), "/last\n"); + const was = try tagRow(&s, "/last"); + const made = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{made}), "/made\n"); + try std.testing.expectEqualStrings(s.at("{d}:{d},{d},{d}", .{ try s.column(0), first, last, made }), try s.columns()); + // The last pane keeps its top and gives up its bottom half. + try std.testing.expectEqual(was, try tagRow(&s, "/last")); + const rows = std.mem.count(u8, try s.screen(), "\n") + 1; + try std.testing.expect(try tagRow(&s, "/made") >= was + (rows - was) / 2); +} + +test "placement: Placement pardes puts a new pane right under the asking one, and bare it steps back to acme" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/ctl", "Placement pardes\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPlacement pardes\n") != null); + const top = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{top}), "New\n"); + const below = try s.focused(); + // Directly under the asking pane, as New always put it. + try s.write(s.at("/pane/{d}/exec", .{top}), "New\n"); + try std.testing.expectEqualStrings(s.at("{d}:{d},{d},{d}", .{ try s.column(0), top, try s.focused(), below }), try s.columns()); + try s.write("/ctl", "Placement\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPlacement acme\n") != null); +} diff --git a/test/behaviour/root_ctl.zig b/test/behaviour/root_ctl.zig index bdf912da..90838d90 100644 --- a/test/behaviour/root_ctl.zig +++ b/test/behaviour/root_ctl.zig @@ -29,3 +29,59 @@ test "root-ctl: size sets the screen of a session no frontend is attached to, an try std.testing.expect(std.mem.indexOf(u8, s.said, "too small for the panes") != null); try std.testing.expectEqual(.{ @as(usize, 120), @as(usize, 40) }, try size(&s)); } + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +fn columns(s: *Session) !usize { + return std.mem.count(u8, try s.read("/layout"), "\n") - 1; // and `active` +} + +test "root-ctl: the root ctl reads the settings as a write takes them, and takes the session's builtins" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const settings = try s.read("/ctl"); + try std.testing.expectEqual(@as(u64, settings.len), (try s.stat("/ctl")).size); + try std.testing.expect(has(settings, "Verbose on\n")); + try std.testing.expect(has(settings, "LocationsConfig ")); + try std.testing.expect(has(settings, "CallstackDepth 8\n")); + try s.write("/ctl", "CallstackDepth\n"); + // Settings only: nothing /status or the DumpConfig window says. + try std.testing.expect(!has(settings, "Platform")); + try std.testing.expect(!has(settings, "requested")); + // Every line written back is taken, and changes nothing. + try s.write("/ctl", settings); + try s.expect("/ctl", settings); + try s.write("/ctl", "Verbose off\n"); + try std.testing.expect(has(try s.read("/ctl"), "Verbose off\n")); + const before = try columns(&s); + try s.write("/ctl", "Newcol\n"); + try std.testing.expectEqual(before + 1, try columns(&s)); + const refusals = [_]struct { []const u8, []const u8 }{ + .{ "Del", "not a session control message \"Del\": write it to pane/<n>/ctl" }, + .{ "Nonsense 1", "unknown control message \"Nonsense\"" }, + .{ "Verbose maybe", "bad value in control message; takes on, off \"maybe\"" }, + .{ "Placement east", "bad value in control message; takes acme, pardes \"east\"" }, + // A number's refusal names its range. + .{ "InactiveDim 200", "bad value in control message; takes 0-100 (a percentage) \"200\"" }, + .{ "MessageLinger x", "bad value in control message; takes 0-60000 (milliseconds) \"x\"" }, + .{ "Newcol 2", "wrong #args in control message \"Newcol\"" }, + .{ "Theme", "wrong #args in control message \"Theme\"" }, + }; + for (refusals) |r| { + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", s.at("{s}\n", .{r[0]}))); + try std.testing.expectEqualStrings(r[1], s.said); + } + _ = try s.refused("/ctl", "Theme no-such-theme\n"); + try std.testing.expect(std.mem.startsWith(u8, s.said, "bad value in control message; like it: n")); + try std.testing.expect(std.mem.endsWith(u8, s.said, "; Themes lists all \"no-such-theme\"")); + // A bare required word fails the check, so the line before never runs. + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Verbose on\nMount\n")); + try std.testing.expect(has(try s.read("/ctl"), "Verbose off\n")); + // A builtin that fails as it runs fails the write, quoting its error and + // its line; the lines before it have taken effect, as in acme. + try std.testing.expectEqual(@as(u16, 2), try s.refused("/ctl", "Verbose on\nMount x /tmp/pardes-no-such-sock-zz\nVerbose off\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "Mount x: /tmp/pardes-no-such-sock-zz: no such socket")); + try std.testing.expect(has(try s.read("/ctl"), "Verbose on\n")); +} diff --git a/test/behaviour/rules.zig b/test/behaviour/rules.zig new file mode 100644 index 00000000..4b853a40 --- /dev/null +++ b/test/behaviour/rules.zig @@ -0,0 +1,39 @@ +//! docs/typ/reference.typ, Rules for every file <rules>. +const std = @import("std"); +const Session = @import("session").Session; + +test "rules: a control character in a write to any ctl refuses the whole write, and a quoted line shows a tab as a blank" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + // The first column's serial, /layout's first word. + const col = std.mem.sliceTo(try s.read("/layout"), ' '); + for ([_][]const u8{ "/ctl", s.at("/pane/{d}/ctl", .{n}), s.at("/col/{s}/ctl", .{col}) }) |ctl| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Wrap off\nbo\x01gus\n")); + try std.testing.expectEqualStrings("invalid command line: it holds a control character (or DEL) other than a tab", s.said); + } + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/ctl", .{n}), "bogus\tword\n")); + // The offending word alone is quoted: a tab ends it. + try std.testing.expect(std.mem.indexOf(u8, s.said, "\"bogus\"") != null); + try std.testing.expect(std.mem.indexOfScalar(u8, s.said, '\t') == null); +} + +test "rules: body reads at any offset and writes append; a stat says its length, and its version moves with each edit" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const body = s.at("/pane/{d}/body", .{try s.focused()}); + try std.testing.expectEqualStrings("two\n", try s.readAt(body, 4, 100)); + try std.testing.expectEqualStrings("wo", try s.readAt(body, 5, 2)); + try std.testing.expectEqualStrings("", try s.readAt(body, 999, 2)); + try s.write(body, "three\n"); + try s.expect(body, "one\ntwo\nthree\n"); + // A rune cut at the write's end is not taken. + try s.write(body, "a\xC3"); + try s.expect(body, "one\ntwo\nthree\na"); + const st = try s.stat(body); + try std.testing.expectEqual(@as(u64, "one\ntwo\nthree\na".len), st.size); + try std.testing.expectEqual(@as(u16, 0o666), st.mode); + try s.write(body, "more\n"); + 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); +} diff --git a/test/behaviour/settings.zig b/test/behaviour/settings.zig new file mode 100644 index 00000000..ff24704a --- /dev/null +++ b/test/behaviour/settings.zig @@ -0,0 +1,103 @@ + + +const std = @import("std"); +const Session = @import("session").Session; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +/// The root ctl's line for `word`. +fn setting(s: *Session, word: []const u8) ![]const u8 { + var lines = std.mem.splitScalar(u8, try s.read("/ctl"), '\n'); + while (lines.next()) |line| if (std.mem.startsWith(u8, line, word) and line.len > word.len and line[word.len] == ' ') return line; + return error.NoSuchSetting; +} + +test "settings: Pager is pardes by default, off taken, anything else refused with its choices" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try std.testing.expectEqualStrings("Pager pardes", try setting(&s, "Pager")); + try s.write("/ctl", "Pager off\n"); + try std.testing.expectEqualStrings("Pager off", try setting(&s, "Pager")); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Pager less\n")); + try std.testing.expectEqualStrings("bad value in control message; takes pardes, off \"less\"", s.said); + try std.testing.expectEqualStrings("Pager off", try setting(&s, "Pager")); +} + +test "settings: Shell refuses a path that is no executable, and bare it goes back to the default" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const default = try setting(&s, "Shell"); + try std.testing.expect(try s.refused("/ctl", "Shell /nonexistent/zzsh\n") != 0); + try std.testing.expect(has(s.said, "Shell: shell \"/nonexistent/zzsh\" not found")); + try std.testing.expect(has(try s.read("/log"), "shell \"/nonexistent/zzsh\" not found")); + // Its words say a directory, which a mount reads as EISDIR too. + try std.testing.expectEqual(@as(u16, 21), try s.refused("/ctl", "Shell /etc\n")); + try std.testing.expect(has(try s.read("/log"), "Shell: not a shell: /etc is a directory")); + try std.testing.expectEqualStrings(default, try setting(&s, "Shell")); + try s.write("/ctl", "Shell /bin/sh\n"); + try std.testing.expectEqualStrings("Shell /bin/sh", try setting(&s, "Shell")); + try s.write("/ctl", "Shell\n"); + try std.testing.expect(has(try s.read("/log"), "Shell: the default again, ")); + try std.testing.expectEqualStrings(default, try setting(&s, "Shell")); +} + +test "settings: Pet is the window's: a terminal's view refuses it, from a pane and the root; a window takes only its pets" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + // Which build, from its options, not from the answer: a terminal's view + // taking Pet must fail here, not pass as a window. + if (comptime @import("pardes_config").platform == .gui) { + try s.write("/ctl", "Pet cat\n"); + // A window's: dog is no pet of its, and off is. + try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet cat\n") != null); + _ = try s.refused("/ctl", "Pet dog\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet cat\n") != null); + try s.write("/ctl", "Pet frog\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet frog\n") != null); + try s.write("/ctl", "Pet off\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet off\n") != null); + return; + } + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Pet cat\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "Pet is GUI-only, invalid here")); + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), "Pet cat\n")); + try std.testing.expectEqualStrings("Pet is GUI-only, invalid here", s.said); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "Pet ") == null); +} + +fn expectHas(text: []const u8, part: []const u8) !void { + if (std.mem.indexOf(u8, text, part) == null) { + std.debug.print("not in it: {s}\n", .{part}); + return error.TestExpectedText; + } +} + +test "settings: DumpConfig reports the settings in its one +DumpConfig pane, a second refreshing it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const text = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{text}); + try s.write(exec, "DumpConfig\n"); + const out = try s.focused(); + try std.testing.expect(out != text); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 0 /+DumpConfig ", .{out})) != null); + const report = try s.read(s.at("/pane/{d}/body", .{out})); + for ([_][]const u8{ "Theme orchard\n", "Last shell spawned: (none)\n", "Shell change pending: on\n", "\nShell ", "LocationsConfig ", "no per-user config path" }) |line| try expectHas(report, line); + try s.write("/ctl", "Theme acme\n"); + try s.write("/focus", s.at("{d}", .{text})); + try s.write(exec, "DumpConfig\n"); + try std.testing.expectEqual(out, try s.focused()); + try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, try s.read("/index"), "/+DumpConfig ")); + try expectHas(try s.read(s.at("/pane/{d}/body", .{out})), "Theme acme\n"); +} + +test "settings: Config without a configuration directory says so and opens nothing" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const index = try s.read("/index"); + try std.testing.expectEqual(@as(u16, 5), try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), "Config\n")); + try s.expect("/index", index); + try expectHas(try s.read("/log"), "Config: no startup file here: no configuration directory"); +} diff --git a/test/behaviour/tags.zig b/test/behaviour/tags.zig index 9a8430d7..6c1c6fcd 100644 --- a/test/behaviour/tags.zig +++ b/test/behaviour/tags.zig @@ -1,4 +1,5 @@ -//! docs/typ/guide.typ, Tags <tags>. + + const std = @import("std"); const Session = @import("session").Session; @@ -15,7 +16,7 @@ fn tag(s: *Session, n: u32) ![]const u8 { /// `path`, which must be on its row. fn tagCol(s: *Session, path: []const u8) !usize { const x, const y = try s.where(path); - const cx, const cy = try s.cursor(); + const cx, const cy = (try s.cursor()).?; try std.testing.expectEqual(y, cy); return cx - x; } @@ -89,7 +90,7 @@ test "tags: a tag remembers its cursor, starts on Save, and a click moves it" { try std.testing.expectEqual(@as(usize, 3), try tagCol(&s, "/test.txt")); // `:` in the tag's normal mode is the way back to the body. try s.keys(":"); - try std.testing.expect((try s.cursor())[1] != (try s.where("/test.txt"))[1]); + try std.testing.expect((try s.cursor()).?[1] != (try s.where("/test.txt"))[1]); try s.write("/focus", other); try s.keys(":" ++ home ++ right ++ right ++ right ++ right ++ right ++ ":"); try s.write("/focus", focus); @@ -204,14 +205,14 @@ test "tags: a tag with a newline in it is as tall as its lines, and each line is const x, const y = try s.where("/test.txt"); try std.testing.expectEqual(.{ x, y + 1 }, try s.where("Second")); // The caret is on the second line, after what was typed. - try std.testing.expectEqual(.{ x + 6, y + 1 }, try s.cursor()); + try std.testing.expectEqual(.{ x + 6, y + 1 }, (try s.cursor()).?); // The body starts under (or over) the whole tag. const by = (try s.where("1 body"))[1]; try std.testing.expect(if (bottom) by < y else by == y + 2); // A click on the second line puts the tag's cursor on it. try s.keys("\x1b:"); try s.write("/mousein", s.at("m {d} {d} 1\nm {d} {d} 0\n", .{ x + 2, y + 1, x + 2, y + 1 })); - try std.testing.expectEqual(.{ x + 2, y + 1 }, try s.cursor()); + try std.testing.expectEqual(.{ x + 2, y + 1 }, (try s.cursor()).?); try s.keys("X"); try std.testing.expectEqualStrings("/test.txt Save Tty Collapse Del\nSeXcond", try tag(&s, n)); try s.expect(s.at("/pane/{d}/body", .{n}), "body\n"); @@ -230,7 +231,7 @@ test "tags: a tag with more lines than rows scrolls to keep its caret in view" { // To the last line: the tag scrolls so it is the bottom row, caret on it. try s.keys(":ge"); const lx, const ly = try s.where("L20"); - try std.testing.expectEqual(.{ lx, ly }, try s.cursor()); + try std.testing.expectEqual(.{ lx, ly }, (try s.cursor()).?); // What the pointer sees is what is shown: its first row is line 13. try std.testing.expectEqual(.{ x, y }, try s.where("L13")); try std.testing.expectError(error.NotOnScreen, s.where("L12")); @@ -240,7 +241,7 @@ test "tags: a tag with more lines than rows scrolls to keep its caret in view" { // Back to the first line brings the prefix back into view. try s.keys("\x1bgg"); try std.testing.expectEqual(.{ x, y }, try s.where("/test.txt")); - try std.testing.expectEqual(y, (try s.cursor())[1]); + try std.testing.expectEqual(y, (try s.cursor()).?[1]); // A column tag is one row, as acme's: its lines are joined. var layout = std.mem.tokenizeScalar(u8, try s.read("/layout"), ' '); const col_tag = s.at("/col/{s}/tag", .{layout.next().?}); @@ -259,7 +260,7 @@ test "tags: a long pane tag wraps onto rows, and a click on a wrapped row maps t try s.write(s.at("/pane/{d}/tag", .{n}), " SyntaxBold"); const before = try tag(&s, n); try s.keys(":A"); - try std.testing.expect((try s.cursor())[0] < 50); + try std.testing.expect((try s.cursor()).?[0] < 50); // SyntaxBold is on a row of its own, below the path's. try std.testing.expect((try s.where("SyntaxBold"))[1] > (try s.where("/an/"))[1]); // Its last character, which a wrapped row's offset not mapped back misses. @@ -315,7 +316,7 @@ test "tags: a name drafted in the tag commits with Enter, cancels with two Escs, try s.expect(name, if (commit) "/new name.zig\n" else "/test.txt\n"); try std.testing.expectEqualStrings(s.at("{s} Save Tty Collapse Del SyntaxBold", .{if (commit) "/new name.zig" else "/test.txt"}), try tag(&s, n)); // The keyboard is the body's again. - try std.testing.expect((try s.cursor())[1] != (try s.where("Save Tty"))[1]); + try std.testing.expect((try s.cursor()).?[1] != (try s.where("Save Tty"))[1]); } try std.testing.expectEqual(@as(usize, 0), s.saved); // A name alone is no edit: the pane stays clean. @@ -380,3 +381,19 @@ test "tags: an oversized drafted name is refused at commit and the draft stays t try s.expect(s.at("/pane/{d}/body", .{n}), "untouched\n"); _ = try s.where("/xxxx"); } + +test "tags: a name typed into the tag's path survives Esc, pending, and the next Enter renames; a second Esc drops it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const name = s.at("/pane/{d}/name", .{n}); + // A click on the path types into it; a stray key while it waits + // changes nothing. + try s.click("/test.txt", 1, 1); + try s.keys("z\x1bq\n"); + try s.expect(name, "/ztest.txt\n"); + try s.click("/ztest.txt", 1, 1); + try s.keys("z\x1bq\x1b"); + 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 ")); +} diff --git a/test/behaviour/terminals.zig b/test/behaviour/terminals.zig new file mode 100644 index 00000000..f6bbdfa0 --- /dev/null +++ b/test/behaviour/terminals.zig @@ -0,0 +1,46 @@ +//! docs/typ/guide.typ, Terminals <terminals>. +const std = @import("std"); +const Session = @import("session").Session; + +fn mode(s: *Session, pane: u32) ![]const u8 { + const ctl = try s.read(s.at("/pane/{d}/ctl", .{pane})); + const at = std.mem.lastIndexOf(u8, ctl, " mode:").? + " mode:".len; + return std.mem.trimEnd(u8, ctl[at..], " "); +} + +test "terminals: Togglettymode and Ctrl-B share the mode's transitions, from the body or its tag, and a text pane refuses it" { + var s = try Session.initTerm(std.testing.allocator); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/tag", .{n})), " Mode ") != null); + try s.write(s.at("/pane/{d}/tag", .{n}), " Togglettymode"); + try std.testing.expectEqualStrings("tty", try mode(&s, n)); + try s.write(ctl, "Togglettymode\n"); + try std.testing.expectEqualStrings("normal", try mode(&s, n)); + try s.keys("\x02"); + try std.testing.expectEqualStrings("tty", try mode(&s, n)); + // The tag has its own mode: the body keeps tty while the tag is typed + // into, and either way of toggling gives the body the keyboard back. + const tag_row = (try s.where("Togglettymode"))[1]; + try s.click("Togglettymode", 0, 1); + try std.testing.expectEqualStrings("tty", try mode(&s, n)); + try std.testing.expectEqual(tag_row, (try s.cursor()).?[1]); + try s.write(ctl, "Togglettymode\n"); + try std.testing.expectEqualStrings("normal", try mode(&s, n)); + try std.testing.expect((try s.cursor()).?[1] > tag_row); + try s.click("Togglettymode", 0, 1); + try s.keys("\x02"); + try std.testing.expectEqualStrings("tty", try mode(&s, n)); + try std.testing.expect((try s.cursor()).?[1] > tag_row); + // A middle click on the word in the tag. + try s.click("Togglettymode", 0, 2); + try std.testing.expectEqualStrings("normal", try mode(&s, n)); + // A text pane refuses it, normal, its text as it was. + const file = try s.newPane(); + try s.write(s.at("/pane/{d}/body", .{file}), "unchanged\n"); + _ = try s.refused(s.at("/pane/{d}/ctl", .{file}), "Togglettymode\n"); + try std.testing.expectEqualStrings("Togglettymode: only a terminal pane takes it", s.said); + try std.testing.expectEqualStrings("normal", try mode(&s, file)); + try s.expect(s.at("/pane/{d}/body", .{file}), "unchanged\n"); +} diff --git a/test/behaviour/tree.zig b/test/behaviour/tree.zig new file mode 100644 index 00000000..7f9b37a4 --- /dev/null +++ b/test/behaviour/tree.zig @@ -0,0 +1,44 @@ +//! docs/typ/reference.typ, The tree <tree>. +const std = @import("std"); +const Session = @import("session").Session; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +/// The first column's serial, /layout's first word. +fn firstColumn(s: *Session) ![]const u8 { + return std.mem.sliceTo(try s.read("/layout"), ' '); +} + +test "tree: index lists serial kind dirty name column, and stats 0, a view generated by each read" { + var s = try Session.initFile(std.testing.allocator, "hello\nthere\n"); + defer s.deinit(); + const n = try s.focused(); + const col = try firstColumn(&s); + const clean = s.at("{d} text 0 /test.txt {s}\n", .{ n, col }); + const index = try s.read("/index"); + const at = std.mem.indexOf(u8, index, clean) orelse return error.NotListed; + const st = try s.stat("/index"); + try std.testing.expectEqual(@as(u64, 0), st.size); + try std.testing.expectEqual(@as(u16, 0o444), st.mode); + try s.write(s.at("/pane/{d}/body", .{n}), "more\n"); + try std.testing.expect(has(try s.read("/index"), s.at("{d} text 1 /test.txt {s}\n", .{ n, col }))); + // Read from an offset, as any file. + const kind = at + std.mem.indexOfScalar(u8, clean, ' ').? + 1; + try std.testing.expectEqualStrings("text", try s.readAt("/index", kind, 4)); + const made = try s.newPane(); + const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{made})), "\n"); + try std.testing.expect(std.mem.endsWith(u8, try s.read("/index"), s.at("\n{d} text 0 {s} {s}\n", .{ made, name, col }))); +} + +test "tree: a terminal is listed as term with its directory as name, which a write to name cannot change" { + var s = try Session.initTerm(std.testing.allocator); + defer s.deinit(); + const n = try s.focused(); + const dir = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + try std.testing.expect(std.mem.startsWith(u8, dir, "/")); + try std.testing.expect(has(try s.read("/index"), s.at("{d} term 0 {s} {s}\n", .{ n, dir, try firstColumn(&s) }))); + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/name", .{n}), "/elsewhere\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "invalid rename: a terminal is named")); +} diff --git a/test/output.zig b/test/output.zig index 2b8bf1f5..3cfece45 100644 --- a/test/output.zig +++ b/test/output.zig @@ -495,60 +495,12 @@ test "Mini syntax colors survive toggles themes and rendering without source acc } } -test "Mini failures leave its existing snapshot and focus unchanged" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - const source = try p.setTestFile("abc\n"); - var path_buf: [128]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "/virtual/pane/{d}/body", .{source.serial}); - try panes.mini.open(p, 0, path); - const id = p.active; - const mini = p.panes[id].?; - const body = mini.file.?.content.ptr; - const next = p.freeSlot(); - try std.testing.expectError(error.FileNotFound, panes.mini.open(p, 0, "/virtual/does-not-exist")); - try std.testing.expectEqual(id, p.active); - try std.testing.expectEqual(body, mini.file.?.content.ptr); - try std.testing.expectEqual(next, p.freeSlot()); -} - -test "DumpConfig prints the startup path and refreshes its one output" { +test "DumpConfig prints the startup path" { const path = "/home/pardes-test/.config/pardes/init"; const p = try Pardes.init(std.testing.allocator, .{ .startup_config_path = path }); defer p.deinit(); - - try std.testing.expect(p.executeBuiltinLine(0, "DumpConfig")); - const opened = p.active; - const out = p.panes[opened].?.file.?; - for ([_][]const u8{ - "Startup config: " ++ path ++ "\n", - "Theme orchard\n", - "Last shell spawned: (none)\n", - "Shell change pending: on\n", - "LocationsConfig ", - }) |line| try std.testing.expect(std.mem.indexOf(u8, out.content, line) != null); - var default_line: [256]u8 = undefined; - try std.testing.expect(std.mem.indexOf(u8, out.content, try std.fmt.bufPrint(&default_line, "Shell {s}\n", .{config.defaultShell()})) != null); - try std.testing.expectEqualStrings(config.dump_config_buffer, std.fs.path.basename(out.path)); - try std.testing.expectEqual(panes.Output.Origin{ .cmd = .DumpConfig }, out.output.?.from); - - try std.testing.expect(p.executeBuiltinLine(0, "DumpConfig")); - try std.testing.expectEqual(opened, p.active); - var buffers: usize = 0; - for (p.panes) |slot| { - const f = (slot orelse continue).file orelse continue; - const origin = (f.output orelse continue).from; - buffers += @intFromBool(std.meta.eql(origin, panes.Output.Origin{ .cmd = .DumpConfig })); - } - try std.testing.expectEqual(@as(usize, 1), buffers); -} - -test "DumpConfig reports the absence of a per-user config path" { - const p = try Pardes.init(std.testing.allocator, .{}); - defer p.deinit(); try std.testing.expect(p.executeBuiltinLine(0, "DumpConfig")); - const report = p.panes[p.active].?.file.?.content; - try std.testing.expect(std.mem.indexOf(u8, report, "no per-user config path") != null); + try std.testing.expect(std.mem.indexOf(u8, p.panes[p.active].?.file.?.content, "Startup config: " ++ path ++ "\n") != null); } /// The file Config names, in a fresh temporary directory: `pardes/init` @@ -621,14 +573,6 @@ test "Config with no startup file opens a pane of its name, and Save makes its d try std.testing.expectEqual(std.Io.File.Kind.directory, made.kind); } -test "Config without a configuration directory says so and opens nothing" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - const before = p.freeSlot(); - try std.testing.expect(p.executeBuiltinLine(p.active, "Config")); - try std.testing.expectEqual(before, p.freeSlot()); -} - test "EffectCode lists readable virtual sources used by this backend" { if (comptime !builtins.EffectCode.enabled) return; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); @@ -771,31 +715,6 @@ fn selectedOutputText(pane: *const pardes.Pane) ?[]const u8 { return line[lo..@min(line.len, hi + 1)]; } -test "LookNext/LookPrev in ordinary text skips comment slashes and retains real paths" { - if (platform == .web) return error.SkipZigTest; - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 20 }); - defer p.deinit(); - const pane = try p.setTestFile("// ordinary comment\n/// documentation comment\n(//) [///] punctuation\n/\n// /tmp\n//tmp\n"); - try std.testing.expect(pane.file.?.output == null); - pardes.look.armLookWalk(p, 0); - const available = p.freeSlot(); - for ([_]struct { key: u21, row: i32, text: []const u8 }{ - .{ .key = 'n', .row = 3, .text = "/" }, - .{ .key = 'n', .row = 4, .text = "/tmp" }, - .{ .key = 'n', .row = 5, .text = "//tmp" }, - .{ .key = 'n', .row = 3, .text = "/" }, - .{ .key = 'N', .row = 5, .text = "//tmp" }, - .{ .key = 'N', .row = 4, .text = "/tmp" }, - .{ .key = 'N', .row = 3, .text = "/" }, - }) |step| { - lookKey(p, step.key); - try std.testing.expectEqual(@as(usize, 0), p.active); - try std.testing.expectEqual(step.row, pane.body.cur_row); - try std.testing.expectEqualStrings(step.text, selectedOutputText(pane) orelse ""); - try std.testing.expectEqual(available, p.freeSlot()); - } -} - test "LookNext/LookPrev skips formatted context and selects comment match locations" { if (!pardes.syntax.enabled) return error.SkipZigTest; for ([_]panes.Output.Origin{ .search, .{ .query = .references } }) |origin| { @@ -1005,62 +924,6 @@ test "LookPrev exactly reverses LookNext across output panes and the ring seam" try std.testing.expect(saw_first and saw_second); } -test "Themes lists the ring in sections and skips headings in both directions" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); - defer p.deinit(); - const source = try p.setTestFile("notes\n"); - pardes.colors.setThemeIndex(p, pardes.themes.len - 1); - const original_theme = p.settings.theme; - try std.testing.expect(p.executeBuiltinLine(0, "Themes")); - const tid = source.search_pane orelse return error.MissingThemeList; - const list = p.panes[tid].?; - const content = list.file.?.content; - try std.testing.expectEqual(@as(usize, 0), p.active); - try std.testing.expectEqual(@as(i32, 0), list.body.cur_row); - try std.testing.expectEqual(@as(usize, 0), list.file.?.scroll); - try std.testing.expectEqualStrings("# Pardes themes", modal.lineSlice(content, 0)); - // Every theme in ring order, each section's heading after a blank line - // where colors.groupAt says one starts. - var row: usize = 1; - var sections: usize = 1; - var first_break: ?usize = null; - for (pardes.themes, 0..) |theme, i| { - if (i > 0) if (pardes.colors.groupAt(i)) |title| { - try std.testing.expectEqualStrings("", modal.lineSlice(content, row)); - try std.testing.expect(std.mem.startsWith(u8, modal.lineSlice(content, row + 1), "# ")); - try std.testing.expectEqualStrings(title, modal.lineSlice(content, row + 1)["# ".len..]); - row += 2; - sections += 1; - if (first_break == null) first_break = i; - }; - const line = modal.lineSlice(content, row); - try std.testing.expect(std.mem.startsWith(u8, line, "Theme ")); - try std.testing.expectEqualStrings(theme.name, line["Theme ".len..]); - row += 1; - } - try std.testing.expectEqual(pardes.themes.len + 1 + 2 * (sections - 1), std.mem.count(u8, content, "\n")); - // acme is a current theme: it is listed before any break. - const acme = for (pardes.themes, 0..) |t, i| { - if (std.mem.eql(u8, t.name, "acme")) break i; - } else return error.MissingAcme; - const brk = first_break orelse return error.NoSections; - try std.testing.expect(acme < brk); - // Opening an imported theme's selector must still begin with the natives. - // Walk across the section break without selecting or executing a heading. - for (pardes.themes[0 .. brk + 1]) |theme| { - lookKey(p, 'n'); - try std.testing.expectEqual(tid, p.active); - const selected = selectedOutputText(list) orelse return error.MissingThemeSection; - try std.testing.expectEqualStrings(theme.name, selected["Theme ".len..]); - try std.testing.expectEqual(original_theme, p.settings.theme); - } - lookKey(p, 'N'); - const previous = selectedOutputText(list) orelse return error.MissingThemeSection; - try std.testing.expectEqualStrings(pardes.themes[brk - 1].name, previous["Theme ".len..]); - p.update(.{ .key = .{ .cp = Key.tab } }); - try std.testing.expectEqual(brk - 1, p.settings.theme); -} - test "LookNext/LookPrev selects command outputs by whole row" { if (platform == .web) return; var cwd_buf: [4096]u8 = undefined; diff --git a/test/pdf.zig b/test/pdf.zig index fca0c240..a068fc40 100644 --- a/test/pdf.zig +++ b/test/pdf.zig @@ -278,56 +278,6 @@ test "PDF feature gates keep argv and builtin behavior coherent" { } } -test "a +PdfSections row's first number is the page: a section not there, or on another page, is a miss said" { - if (!pdf_enabled or platform == .web) return; - const gpa = std.testing.allocator; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - const fixture = try pdf_impl.makeOutlineTestPdf(gpa); - defer gpa.free(fixture); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "outline.pdf", .data = fixture }); - var path_buf: [256]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/outline.pdf", .{tmp.sub_path}); - const p = try Pardes.init(gpa, .{ .file = path, .cols = 80, .rows = 28 }); - defer p.deinit(); - panes.Pdf.openSections(p, 0); - const out_id = p.panes[0].?.search_pane orelse return error.MissingPdfSectionsOutput; - const out = p.panes[out_id].?; - const row = std.mem.sliceTo(out.file.?.content, '\n'); - const place = row[0 .. std.mem.indexOfScalar(u8, row, ' ') orelse row.len]; // outline.pdf:P:1 - var fields = std.mem.splitScalar(u8, place, ':'); - _ = fields.next(); - const page = try std.fmt.parseInt(usize, fields.next().?, 10); - var line: [128]u8 = undefined; - // As listed: found, no miss. - p.look_missed = false; - pardes.look.lookAt(p, out_id, place); - try std.testing.expect(!p.look_missed); - // The section on another page than the row says, and one not there. - for ([_][]const u8{ try std.fmt.bufPrint(&line, "outline.pdf:{d}:1", .{page + 1}), "outline.pdf:1:999" }) |bad| { - var copy: [128]u8 = undefined; - @memcpy(copy[0..bad.len], bad); - p.look_missed = false; - const failures = p.fs.failures; - pardes.look.lookAt(p, out_id, copy[0..bad.len]); - try std.testing.expect(p.look_missed); - try std.testing.expect(p.fs.failures != failures); - } -} - -test "a PDF's body puts each line of its text on a line of its own, never a heading run into the text under it" { - if (!pdf_enabled or platform == .web) return; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - const p = try PdfLinkTests.init(tmp); - defer p.deinit(); - const text = p.panes[0].?.pdf.?.ensureText(p.pdf_gpa); - // The page's three text runs, each its own line. - try std.testing.expect(std.mem.indexOf(u8, text, "target.txt\n") != null); - try std.testing.expect(std.mem.indexOf(u8, text, "https://example.com/same\n") != null); - try std.testing.expect(std.mem.indexOf(u8, text, "label\n") != null); -} - test "PdfSections Look follows the exact owning PDF, not an equal path" { if (!pdf_enabled or platform == .web) return; const gpa = std.testing.allocator; @@ -377,144 +327,6 @@ test "PdfSections Look follows the exact owning PDF, not an equal path" { try std.testing.expectEqual(first_output_id, p.active); } -test "PdfSections on a PDF with no outline says so and opens nothing" { - if (!pdf_enabled or platform == .web) return; - const gpa = std.testing.allocator; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - const fixture = try pdf_impl.makeNoOutlineTestPdf(gpa); - defer gpa.free(fixture); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "plain.pdf", .data = fixture }); - var path_buf: [256]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/plain.pdf", .{tmp.sub_path}); - const p = try Pardes.init(gpa, .{ .file = path }); - defer p.deinit(); - const pane = p.panes[0].?; - for (0..2) |_| { - panes.Pdf.openSections(p, 0); - try std.testing.expect(pane.search_pane == null); - try std.testing.expectEqualStrings("PdfSections: this PDF has no outline", pane.msg[0..pane.msg_len]); - } -} - -test "Recent keeps a PDF as it keeps a file: open, then closed at its page" { - if (!pdf_enabled or platform == .web) return; - const gpa = std.testing.allocator; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - const fixture = try pdf_impl.makeNoOutlineTestPdf(gpa); - defer gpa.free(fixture); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "plain.pdf", .data = fixture }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; - var path_buf: [4200]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "{s}/plain.pdf", .{dir}); - const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 28 }); - defer p.deinit(); - pardes.look.lookAt(p, p.active, path); - p.update(.{ .resize = .{ .cols = 80, .rows = 28 } }); // an update's end announces it - var out: std.Io.Writer.Allocating = .init(gpa); - defer out.deinit(); - try panes.Output.recentText(p, &out.writer, true); - var want: [4300]u8 = undefined; - try std.testing.expect(std.mem.indexOf(u8, out.written(), try std.fmt.bufPrint(&want, "{s}:1:1 open\n", .{path})) != null); -} - -test "`:N` on a PDF turns to page N, from the pane and from the root's look, and `@pN:N` logs no err" { - if (!pdf_enabled or platform == .web) return; - const gpa = std.testing.allocator; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - const fixture = try pdf_impl.makeOutlineTestPdf(gpa); - defer gpa.free(fixture); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "outline.pdf", .data = fixture }); - var path_buf: [256]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/outline.pdf", .{tmp.sub_path}); - const p = try Pardes.init(gpa, .{ .file = path, .cols = 80, .rows = 28 }); - defer p.deinit(); - const doc = p.panes[0].?; - const pages = doc.pdf.?.page_count; - try std.testing.expect(pages >= 2); - const failures = p.fs.failures; - pardes.look.lookAt(p, 0, ":2"); - try std.testing.expect(!p.look_missed); - try std.testing.expectEqual(@as(usize, 1), doc.pdf.?.page); - var line: [64]u8 = undefined; - pardes.look.lookAt(p, 0, try std.fmt.bufPrint(&line, "@p{d}:1", .{doc.serial})); - try std.testing.expect(!p.look_missed); - try std.testing.expectEqual(@as(usize, 0), doc.pdf.?.page); - try std.testing.expectEqual(failures, p.fs.failures); - // Over 9P too: the root's look at `@pN:2` answers ok and logs no err. - const ctlfs = pardes.ctlfs; - const written = p.serveFs(.{ .tag = 1, .op = .write, .node = @backingInt(ctlfs.TopFile.look), .data = try std.fmt.bufPrint(&line, "@p{d}:2\n", .{doc.serial}) }); - try std.testing.expectEqual(ctlfs.Status.ok, written.status); - try std.testing.expectEqual(@as(usize, 1), doc.pdf.?.page); - const log = @backingInt(ctlfs.TopFile.log); - const h = p.serveFs(.{ .tag = 2, .op = .open, .node = log }).handle; - const record = p.fsPayload(p.serveFs(.{ .tag = 3, .op = .read, .node = log, .handle = h, .size = 1 << 16 })); - try std.testing.expect(std.mem.indexOf(u8, record, "\nerr ") == null); - _ = p.serveFs(.{ .tag = 4, .op = .release, .node = log, .handle = h }); - while (p.nextEffect()) |_| {} - // `:N:H` on the pane is hit H of its search on page N, as `file.pdf:N:H` - // (a search row) is: with no search there is no hit 3, a miss that - // turns no page. - p.look_missed = false; - pardes.look.lookAt(p, 0, ":1"); - pardes.look.lookAt(p, 0, ":2:3"); - try std.testing.expect(p.look_missed); - try std.testing.expect(std.mem.indexOf(u8, doc.msg[0..doc.msg_len], "has no search hit 3 on page 2") != null); - try std.testing.expectEqual(@as(usize, 0), doc.pdf.?.page); - pardes.look.lookAt(p, 0, ":2"); - // Page 0 is no page: the has-no-page miss. - p.look_missed = false; - pardes.look.lookAt(p, 0, ":0"); - try std.testing.expect(p.look_missed); - try std.testing.expect(std.mem.indexOf(u8, doc.msg[0..doc.msg_len], "has no page 0") != null); - p.look_missed = false; - // A byte offset or a pattern is no PDF address: refused with words. - pardes.look.lookAt(p, 0, ":#3"); - try std.testing.expect(p.look_missed); - try std.testing.expect(std.mem.indexOf(u8, doc.msg[0..doc.msg_len], "its address is a page, :N") != null); - p.look_missed = false; - pardes.look.lookAt(p, 0, try std.fmt.bufPrint(&line, ":{d}", .{pages + 1})); - try std.testing.expect(p.look_missed); - try std.testing.expectEqual(@as(usize, 1), doc.pdf.?.page); // where the 9P look left it -} - -test "a look at a PDF's page past its last is a miss, never the last page" { - if (!pdf_enabled or platform == .web) return; - const gpa = std.testing.allocator; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - const fixture = try pdf_impl.makeNoOutlineTestPdf(gpa); - defer gpa.free(fixture); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "plain.pdf", .data = fixture }); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.pdf", .data = fixture }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; - var path_buf: [4200]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "{s}/plain.pdf", .{dir}); - const p = try Pardes.init(gpa, .{ .file = path, .cols = 80, .rows = 28 }); - defer p.deinit(); - const doc = p.panes[0].?; - try std.testing.expect(doc.hasPdf()); - var line: [4300]u8 = undefined; - // Open: it stays on its page. - const page = doc.pdf.?.page; - pardes.look.lookAt(p, 0, try std.fmt.bufPrint(&line, "{s}:999", .{path})); - try std.testing.expect(p.look_missed); - try std.testing.expectEqual(page, doc.pdf.?.page); - // Not open: nothing opens. - p.look_missed = false; - var before: usize = 0; - for (p.panes) |slot| before += @intFromBool(slot != null); - pardes.look.lookAt(p, 0, try std.fmt.bufPrint(&line, "{s}/other.pdf:999", .{dir})); - try std.testing.expect(p.look_missed); - var after: usize = 0; - for (p.panes) |slot| after += @intFromBool(slot != null); - try std.testing.expectEqual(before, after); -} - test "SDL PDF raster policy is materially denser than Kitty" { try std.testing.expect( pardes.sdl_pdf_raster_policy.dpi >= pardes.kitty_pdf_raster_policy.dpi * 2, @@ -1334,7 +1146,7 @@ fn copySourceRect( return out; } -test "PDF normal adapter consumes unsupported actions and navigates page fallback" { +test "PDF normal mode keeps `:`, SPC and `/` on their shared paths" { if (!pdf_enabled or platform == .web) return; const p = try Pardes.init(std.testing.allocator, .{ @@ -1344,61 +1156,8 @@ test "PDF normal adapter consumes unsupported actions and navigates page fallbac }); defer p.deinit(); const pane = p.panes[0].?; - const pv = &pane.pdf.?; - try std.testing.expect(pv.page_count > 3); p.native_images = false; - // Every vertical vocabulary falls back to counted page changes when the - // shell cannot place native pixels. - p.update(.{ .key = .{ .cp = '2' } }); - p.update(.{ .key = .{ .cp = 'j' } }); - try std.testing.expectEqual(@as(usize, 2), pv.page); - p.update(.{ .key = .{ .cp = '2' } }); - p.update(.{ .key = .{ .cp = 'k' } }); - try std.testing.expectEqual(@as(usize, 0), pv.page); - p.update(.{ .key = .{ .cp = '2' } }); - p.update(.{ .key = .{ .cp = 'd', .ctrl = true } }); - try std.testing.expectEqual(@as(usize, 2), pv.page); - p.update(.{ .key = .{ .cp = '2' } }); - p.update(.{ .key = .{ .cp = 'u', .ctrl = true } }); - try std.testing.expectEqual(@as(usize, 0), pv.page); - p.update(.{ .key = .{ .cp = '2' } }); - p.update(.{ .key = .{ .cp = 'f', .ctrl = true } }); - try std.testing.expectEqual(@as(usize, 2), pv.page); - p.update(.{ .key = .{ .cp = '2' } }); - p.update(.{ .key = .{ .cp = 'b', .ctrl = true } }); - try std.testing.expectEqual(@as(usize, 0), pv.page); - - // Prefix actions and the counted text goto-line action map to pages. - p.update(.{ .key = .{ .cp = 'g' } }); - p.update(.{ .key = .{ .cp = 'e' } }); - try std.testing.expectEqual(pv.page_count - 1, pv.page); - p.update(.{ .key = .{ .cp = 'g' } }); - p.update(.{ .key = .{ .cp = 'g' } }); - try std.testing.expectEqual(@as(usize, 0), pv.page); - p.update(.{ .key = .{ .cp = '3' } }); - p.update(.{ .key = .{ .cp = 'g' } }); - p.update(.{ .key = .{ .cp = 'g' } }); - try std.testing.expectEqual(@as(usize, 2), pv.page); - p.update(.{ .key = .{ .cp = '2' } }); - p.update(.{ .key = .{ .cp = 'G' } }); - try std.testing.expectEqual(@as(usize, 1), pv.page); - - // Editing/selection actions are consumed no-ops: parser state clears, - // placeholder cells never acquire a range, and PDF state stays intact. - const page_before_noop = pv.page; - const revision_before_noop = pv.next_raster_revision; - p.update(.{ .key = .{ .cp = '4' } }); - p.update(.{ .key = .{ .cp = 'd' } }); - p.update(.{ .key = .{ .cp = 'v' } }); - p.update(.{ .key = .{ .cp = '|' } }); - try std.testing.expectEqual(page_before_noop, pv.page); - try std.testing.expectEqual(revision_before_noop, pv.next_raster_revision); - try std.testing.expectEqual(@as(u32, 0), pane.body.normal.count); - try std.testing.expectEqual(.none, pane.body.normal.prefix); - try std.testing.expect(!pane.body.vsel.active and !pane.body.msel.active and pane.body.nsel == 0); - try std.testing.expect(pane.focus == .body); - // Cross-pane BODY-NORMAL actions keep their established shared paths. p.update(.{ .key = .{ .cp = ':' } }); try std.testing.expect(pane.focus == .tag); @@ -1552,7 +1311,7 @@ test "PDF continuous strip renders every intersecting short page" { } } -test "PdfTint cycles pane-local state without adding tag clutter" { +test "PDF tint resolves a theme's null page colors to its chrome" { if (!pdf_enabled or platform == .web) return; const p = try Pardes.init(std.testing.allocator, .{ @@ -1561,66 +1320,6 @@ test "PdfTint cycles pane-local state without adding tag clutter" { .rows = 24, }); defer p.deinit(); - const pane = p.panes[0].?; - const pv = &pane.pdf.?; - const tint_builtin = std.meta.stringToEnum(Builtin, "PdfTint") orelse - return error.MissingPdfTintBuiltin; - - try std.testing.expectEqual(PdfTintMode.filtered, pv.tint); - const initial_tag = try pardes.tagline.tagText(p, p.scratch.allocator(), pane); - try std.testing.expect(std.mem.startsWith(u8, initial_tag, pv.path)); - try std.testing.expect(std.mem.endsWith(u8, initial_tag, " PdfSections PdfTint PdfFit Collapse Del")); - try std.testing.expect(std.mem.indexOf(u8, initial_tag, "filtered") == null); - - try std.testing.expect(pardes.test_api.runBuiltin( - p, - @tagName(tint_builtin), - 0, - "", - null, - )); - try std.testing.expectEqual(PdfTintMode.full, pv.tint); - const full_tag = try pardes.tagline.tagText(p, p.scratch.allocator(), pane); - try std.testing.expectEqualStrings(initial_tag, full_tag); - try std.testing.expect(std.mem.indexOf(u8, full_tag, "full") == null); - - // Running the same pane-scoped word in a terminal cannot mutate the PDF - // next to it. A FILE boot is one pane now, so that terminal is asked for - // here rather than inherited from init. - _ = try p.newShell(1, ""); - _ = layout.splitColumn(p, 0, 1, false); - p.sync(); // rects for the new column, exactly as the two-pane boot did - try std.testing.expect(!hasPdf(p.panes[1].?)); - try std.testing.expect(pardes.test_api.runBuiltin( - p, - @tagName(tint_builtin), - 1, - "", - null, - )); - try std.testing.expectEqual(PdfTintMode.full, pv.tint); - - try std.testing.expect(pardes.test_api.runBuiltin( - p, - @tagName(tint_builtin), - 0, - "", - null, - )); - try std.testing.expectEqual(PdfTintMode.disabled, pv.tint); - const disabled_tag = try pardes.tagline.tagText(p, p.scratch.allocator(), pane); - try std.testing.expectEqualStrings(initial_tag, disabled_tag); - try std.testing.expect(std.mem.indexOf(u8, disabled_tag, "disabled") == null); - try std.testing.expect(pardes.test_api.runBuiltin( - p, - @tagName(tint_builtin), - 0, - "", - null, - )); - try std.testing.expectEqual(PdfTintMode.filtered, pv.tint); - try std.testing.expectEqual(PdfFitMode.width, pv.fit); - // `dark` intentionally leaves page bg/fg null. PDF tint resolves those // deterministically to its chrome colors rather than host defaults. const dark_index = for (themes, 0..) |th, i| { diff --git a/test/session.zig b/test/session.zig index f3d96918..16a131fd 100644 --- a/test/session.zig +++ b/test/session.zig @@ -183,20 +183,62 @@ pub const Session = struct { return @ptrCast(@alignCast(s.core)); } - // panes: - /// Where `text` is first drawn on the screen: its column and row. - pub fn where(s: *Session, text: []const u8) !struct { usize, usize } { - return s.find(text); + // ninep: + + /// An open kept until `clunk`, as an event reader or a lock holder + /// keeps one. + pub const Fid = struct { node: u64, handle: u32 }; + + /// `path` opened (omode 0 read, 1 write, 2 both) and held. + pub fn hold(s: *Session, path: []const u8, omode: u8) !Fid { + const node, const h = try s.open(path, omode); + return .{ .node = node, .handle = h }; } - /// The cell the screen's cursor is drawn on, its column and row. - pub fn cursor(s: *Session) !struct { usize, usize } { - const json = try std.json.parseFromSliceLeaky(std.json.Value, s.arena.allocator(), try s.read("/screen"), .{}); - const c = json.object.get("cursor").?; - if (c != .object) return error.NoCursor; - return .{ @intCast(c.object.get("x").?.integer), @intCast(c.object.get("y").?.integer) }; + /// One read on a held open (an event record, an answer). + pub fn readFid(s: *Session, f: Fid) ![]const u8 { + const r = s.serve(.{ .tag = 0, .op = .read, .node = f.node, .handle = f.handle, .size = 1 << 16 }); + if (r.status != .ok) return refusal("a held open", r); + return s.arena.allocator().dupe(u8, pardesOf(s).fsPayload(r)); + } + + /// One write on a held open: 0 when taken, else its errno, its words in `said`. + pub fn writeFid(s: *Session, f: Fid, data: []const u8) !u16 { + const r = s.serve(.{ .tag = 0, .op = .write, .node = f.node, .handle = f.handle, .data = data }); + if (r.status == .ok) return 0; + s.said = try s.arena.allocator().dupe(u8, r.ename); + return r.errno; } + pub fn clunk(s: *Session, f: Fid) void { + _ = s.serve(.{ .tag = 0, .op = .release, .node = f.node, .handle = f.handle, .opened = true }); + } + + /// A held open's file truncated, as an OTRUNC open (`> data`) does. + pub fn truncate(s: *Session, f: Fid) !void { + const r = s.serve(.{ .tag = 0, .op = .setattr, .node = f.node, .truncate = true }); + if (r.status != .ok) return refusal("truncate", r); + } + + /// One read of `size` bytes at `off`, on an open of its own. + pub fn readAt(s: *Session, path: []const u8, off: u64, size: u32) ![]const u8 { + const f = try s.hold(path, 0); + defer s.clunk(f); + const r = s.serve(.{ .tag = 0, .op = .read, .node = f.node, .handle = f.handle, .off = off, .size = size }); + if (r.status != .ok) return refusal(path, r); + return s.arena.allocator().dupe(u8, pardesOf(s).fsPayload(r)); + } + + /// What a stat of `path` answers: its size, mode and qid version. + pub fn stat(s: *Session, path: []const u8) !struct { size: u64, mode: u16, version: u32 } { + const f = try s.hold(path, 0); + defer s.clunk(f); + const r = s.serve(.{ .tag = 0, .op = .getattr, .node = f.node }); + if (r.status != .ok) return refusal(path, r); + return .{ .size = r.attr.size, .mode = r.attr.mode, .version = r.attr.version }; + } + + // panes: /// `data` written to `path` truncated first, as a shell's `>` does. pub fn replace(s: *Session, path: []const u8, data: []const u8) !void { const node, const h = try s.open(path, 1); @@ -206,6 +248,72 @@ pub const Session = struct { const w = s.serve(.{ .tag = 0, .op = .write, .node = node, .handle = h, .data = data }); if (w.status != .ok) return refusal(path, w); } + + // core: + + /// /layout as its columns, left to right, `column:pane,pane` each and + /// a blank between (`1:2,5 3:`, column 3 empty). + pub fn columns(s: *Session) ![]const u8 { + const a = s.arena.allocator(); + var out: std.ArrayList(u8) = .empty; + var lines = std.mem.tokenizeScalar(u8, try s.read("/layout"), '\n'); + while (lines.next()) |line| { + if (std.mem.startsWith(u8, line, "active ")) continue; + var words = std.mem.tokenizeScalar(u8, line, ' '); + if (out.items.len > 0) try out.append(a, ' '); + try out.print(a, "{s}:", .{words.next().?}); + for (0..5) |_| _ = words.next(); // index x width current empty|full + var sep: []const u8 = ""; + while (words.next()) |pane| : (sep = ",") try out.print(a, "{s}{s}", .{ sep, pane }); + } + return out.items; + } + + /// The serial of the `i`th column from the left. + pub fn column(s: *Session, i: usize) !u32 { + var cols = std.mem.tokenizeScalar(u8, try s.columns(), ' '); + for (0..i) |_| _ = cols.next(); + const col = cols.next() orelse return error.NoColumn; + return std.fmt.parseInt(u32, col[0..std.mem.indexOfScalar(u8, col, ':').?], 10); + } + + /// Where /screen draws the cursor, column and row; null when it draws none. + pub fn cursor(s: *Session) !?struct { usize, usize } { + const json = try std.json.parseFromSliceLeaky(std.json.Value, s.arena.allocator(), try s.read("/screen"), .{}); + const c = switch (json.object.get("cursor").?) { + .object => |o| o, + else => return null, + }; + return .{ @intCast(c.get("x").?.integer), @intCast(c.get("y").?.integer) }; + } + + /// Where `text` is first drawn on /screen, column and row. + pub fn where(s: *Session, text: []const u8) !struct { usize, usize } { + return s.find(text); + } + + // pdf: + + /// A Look of `path` at the root (one relative to the working directory + /// made absolute first); the serial of the pane with the keyboard after. + /// A PDF in a build without them (-Dmupdf=false: no PdfTint in + /// /commands) skips the test. + pub fn lookFile(s: *Session, path: []const u8) !u32 { + if (std.mem.endsWith(u8, path, ".pdf") and std.mem.indexOf(u8, try s.read("/commands"), "PdfTint") == null) return error.SkipZigTest; + var buf: [4096]u8 = undefined; + const abs = if (std.fs.path.isAbsolute(path)) path else s.at("{s}/{s}", .{ buf[0..try std.Io.Dir.cwd().realPath(std.testing.io, &buf)], path }); + try s.write("/look", s.at("{s}\n", .{abs})); + return s.focused(); + } + + /// The page a PDF pane shows and its pages, from its tag's `[page/pages]`. + pub fn pdfPage(s: *Session, n: u32) !struct { u32, u32 } { + const tag = try s.read(s.at("/pane/{d}/tag", .{n})); + const lo = std.mem.indexOf(u8, tag, " [") orelse return error.NoPage; + const close = std.mem.indexOfScalarPos(u8, tag, lo, ']') orelse return error.NoPage; + 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) }; + } }; fn refusal(path: []const u8, r: Reply) error{Refused} { |
