summaryrefslogtreecommitdiff
path: root/test/behaviour/root_ctl.zig
blob: c00c3cfe6e80764b9789fe518b1785054e2594b9 (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
//! 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\"" },
        .{ "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"));
}

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"));
}