summaryrefslogtreecommitdiff
path: root/test/behaviour/root_ctl.zig
blob: b9909eefb06907087e73d6413bae4936f992fc32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! docs/typ/reference.typ, The root ctl <root-ctl>.
const std = @import("std");
const Session = @import("session").Session;

/// The screen's size, as /screen draws it.
fn size(s: *Session) !struct { usize, usize } {
    const screen = try s.screen();
    const cols = try std.unicode.utf8CountCodepoints(screen[0 .. std.mem.indexOfScalar(u8, screen, '\n') orelse screen.len]);
    return .{ cols, std.mem.count(u8, screen, "\n") + 1 };
}

test "root-ctl: size sets the screen of a session no frontend is attached to, and only of one" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "size 120 40\n")); // a frontend's
    s.detach();
    try s.write("/ctl", "size 120 40\n");
    try std.testing.expectEqual(.{ @as(usize, 120), @as(usize, 40) }, try size(&s));
    try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "size 120\n"));
    try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "size\n"));
    _ = try s.refused("/ctl", "size 5 2\n");
    try std.testing.expect(std.mem.indexOf(u8, s.said, "at least 20x6, at most 4096x4096") != null);
    // The bad value quoted, never the word size.
    try std.testing.expect(std.mem.endsWith(u8, s.said, "\"5 2\""));
    // Too small for the panes it has, each its tag and two rows: refused,
    // the size as it was.
    for (0..4) |_| _ = try s.newPane();
    _ = try s.refused("/ctl", "size 80 8\n");
    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\"" },
        // A number's refusal names its range.
        .{ "SearchRows 200", "bad value in control message; takes 1-100 (rows) \"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"));
}

test "root-ctl: Kill with a name and nothing running says so and does not fail, as bare Kill does" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    try s.write("/ctl", "Kill zzz\n");
    try std.testing.expect(has(try s.read("/log"), "Kill: nothing running"));
}

test "root-ctl: a bad Tab width and a bad Incl directory are refused invalid, changing nothing" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const before = try s.read("/ctl");
    // Bare, Incl lists its directories.
    try s.write("/ctl", "Incl\n");
    const listed = try lastIncl(&s);
    for ([_][]const u8{ "Tab 0\n", "Tab 17\n", "Tab x\n" }) |line| {
        try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", line));
        // Run from a pane, as a tag click runs it: said invalid too.
        try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), line));
        try std.testing.expect(has(s.said, "invalid tab width"));
    }
    try s.expect("/ctl", before);
    try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Incl rel/dir\n"));
    try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Incl - /usr/include\n"));
    try std.testing.expectEqual(@as(u16, 2), try s.refused("/ctl", "Incl /tmp/pardes-surely-no-such-dir\n"));
    // The same list again: none taken.
    try s.write("/ctl", "Incl\n");
    try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, try s.read("/log"), listed));
    // Bare Tab on the root ctl says the width, as from a pane.
    try s.write("/ctl", "Tab\n");
}

/// The log's one `msg - Incl:` record, whole.
fn lastIncl(s: *Session) ![]const u8 {
    const log = try s.read("/log");
    const at = std.mem.indexOf(u8, log, "\nmsg - Incl:") orelse return error.NoIncl;
    return log[at .. std.mem.indexOfScalarPos(u8, log, at + 1, '\n').? + 1];
}

test "root-ctl: Joincol with no column to the right and Theme with no such theme say so; a theme's name is any case" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    try std.testing.expect(try s.refused("/ctl", "Joincol\n") != 0);
    try std.testing.expect(std.mem.startsWith(u8, s.said, "Joincol: no column to the right"));
    try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Theme drak\n"));
    try std.testing.expect(std.mem.startsWith(u8, s.said, "bad value in control message; like it: "));
    try std.testing.expect(has(s.said, ": dr"));
    try std.testing.expect(std.mem.endsWith(u8, s.said, "\"drak\""));
    // Within the 128 bytes an Rerror carries, the quoted line kept.
    try std.testing.expect(s.said.len <= 128);
    // A click on the word says it in the log.
    _ = try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), "Theme drak\n");
    try std.testing.expect(has(try s.read("/log"), "no theme \"drak\""));
    try s.write("/ctl", "Theme DRACULA\n");
    try std.testing.expect(has(try s.read("/ctl"), "\nTheme dracula\n"));
}