summaryrefslogtreecommitdiff
path: root/test/behaviour/settings.zig
blob: 027fd1c988fd6cacef31c6d42509c37e7cadcc93 (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


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

test "settings: every on/off setting the root ctl reads flips when its word is written bare, and Theme and Shell take a value" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    var lines = std.mem.tokenizeScalar(u8, try s.read("/ctl"), '\n');
    var flipped: usize = 0;
    while (lines.next()) |line| {
        const space = std.mem.indexOfScalar(u8, line, ' ') orelse continue;
        const word, const was = .{ line[0..space], line[space + 1 ..] };
        const on = std.mem.eql(u8, was, "on");
        if (!on and !std.mem.eql(u8, was, "off")) continue;
        try s.write("/ctl", s.at("{s}\n", .{word}));
        try std.testing.expectEqualStrings(s.at("{s} {s}", .{ word, if (on) "off" else "on" }), try setting(&s, word));
        flipped += 1;
    }
    try std.testing.expect(flipped > 20);
    for ([_][]const u8{ "Colors off", "Wrap off", "Tagbottom on", "Debug on" }) |want|
        try std.testing.expectEqualStrings(want, try setting(&s, want[0..std.mem.indexOfScalar(u8, want, ' ').?]));
    try s.write("/ctl", "Theme acme\n");
    try s.write("/ctl", "Shell /bin/sh\n");
    try std.testing.expectEqualStrings("Theme acme", try setting(&s, "Theme"));
    try std.testing.expectEqualStrings("Shell /bin/sh", try setting(&s, "Shell"));
}