summaryrefslogtreecommitdiff
path: root/test/behaviour/root_ctl.zig
blob: 7cf3025e02bfe63891fb9ea185ceca7487efc4dd (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//! 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"));
}

/// The labels /dumps lists, newest first.
fn labels(s: *Session) ![]const []const u8 {
    var out: std.ArrayList([]const u8) = .empty;
    var lines = std.mem.tokenizeScalar(u8, try s.read("/dumps"), '\n');
    while (lines.next()) |line| try out.append(s.arena.allocator(), line[0..std.mem.indexOfScalar(u8, line, ' ').?]);
    return out.items;
}

fn stateSize(dir: []const u8) !u64 {
    var buf: [4200]u8 = undefined;
    return (try std.Io.Dir.cwd().statFile(std.testing.io, try std.fmt.bufPrint(&buf, "{s}/pardes.xitdb", .{dir}), .{})).size;
}

test "root-ctl: Dump keeps the session in the state file under a label /dumps lists and the workspace tag names, and Restore of it brings it back" {
    var s = try Session.initFile(std.testing.allocator, "dumped text\n");
    defer s.deinit();
    const pane = try s.focused();
    try s.write("/ctl", "Dump\n");
    const listed = try labels(&s);
    try std.testing.expectEqual(@as(usize, 1), listed.len);
    try std.testing.expect(std.mem.startsWith(u8, listed[0], "pardes-"));
    // `<label> <date> <size> <panes>`: one pane, its text in its size.
    var words = std.mem.tokenizeScalar(u8, std.mem.trimEnd(u8, try s.read("/dumps"), "\n"), ' ');
    _ = words.next();
    try std.testing.expect(std.mem.endsWith(u8, words.next().?, "Z"));
    try std.testing.expect(try std.fmt.parseInt(u64, words.next().?, 10) > "dumped text\n".len);
    try std.testing.expectEqualStrings("1", words.next().?);
    try std.testing.expect(has(try s.read("/tag"), s.at("Restore {s}", .{listed[0]})));
    try s.write(s.at("/pane/{d}/body", .{pane}), "changed\n");
    // Unsaved text is asked about once; the second Restore goes ahead.
    if (try s.refused("/ctl", s.at("Restore {s}\n", .{listed[0]})) != 0) try s.write("/ctl", s.at("Restore {s}\n", .{listed[0]}));
    try s.expect(s.at("/pane/{d}/body", .{pane}), "dumped text\n");
    try std.testing.expect(has(try s.read("/log"), s.at("restore {s}\n", .{listed[0]})));
    // Bare, the last one this session made.
    try s.write("/ctl", "Restore\n");
    try s.expect(s.at("/pane/{d}/body", .{pane}), "dumped text\n");
}

test "root-ctl: a Restore of a label the state file keeps none of is refused in words, changing nothing" {
    var s = try Session.initFile(std.testing.allocator, "kept\n");
    defer s.deinit();
    try std.testing.expect(try s.refused("/ctl", "Restore pardes-19990101-000000\n") != 0);
    try std.testing.expect(has(s.said, "Restore: no dump labelled pardes-19990101-000000"));
    try std.testing.expect(try s.refused("/ctl", "Restore\n") != 0);
    try std.testing.expect(has(s.said, "Restore: no dump yet; name one"));
    try s.expect(s.at("/pane/{d}/body", .{try s.focused()}), "kept\n");
}

test "root-ctl: the state file keeps the newest 32 dumps: the 33rd drops the first" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    try s.write("/ctl", "Dump\n");
    const first = (try labels(&s))[0];
    for (0..32) |_| try s.write("/ctl", "Dump\n");
    const kept = try labels(&s);
    try std.testing.expectEqual(@as(usize, 32), kept.len);
    for (kept) |l| try std.testing.expect(!std.mem.eql(u8, l, first));
    try std.testing.expect(try s.refused("/ctl", s.at("Restore {s}\n", .{first})) != 0);
    try std.testing.expect(has(s.said, "no dump labelled"));
}

test "root-ctl: a text unchanged since the last Dump is not written again: the state file grows by the session, not the text" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var dir_buf: [std.fs.max_path_bytes]u8 = undefined;
    const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
    var s = try Session.initShared(std.testing.allocator, dir);
    defer s.deinit();
    const pane = try s.newPane();
    const text = try s.arena.allocator().alloc(u8, 1 << 20);
    for (text, 0..) |*b, i| b.* = if (i % 64 == 63) '\n' else 'a' + @as(u8, @intCast(i % 26));
    try s.write(s.at("/pane/{d}/body", .{pane}), text);
    const before = try stateSize(dir);
    try s.write("/ctl", "Dump\n");
    const once = try stateSize(dir);
    try std.testing.expect(once - before > text.len);
    try s.write("/ctl", "Dump\n");
    // The session's own maps, a few hundred KB: not the text again.
    const twice = try stateSize(dir);
    try std.testing.expect(twice - once < text.len / 2);
    try std.testing.expectEqual(@as(usize, 2), (try labels(&s)).len);
}

test "root-ctl: a Dump killed in the middle leaves the one before it whole, and the next one is made" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var dir_buf: [std.fs.max_path_bytes]u8 = undefined;
    const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
    var label_buf: [64]u8 = undefined;
    var label: []const u8 = "";
    {
        var s = try Session.initShared(std.testing.allocator, dir);
        defer s.deinit();
        try s.write(s.at("/pane/{d}/body", .{try s.newPane()}), "the dump before\n");
        try s.write("/ctl", "Dump\n");
        const listed = try labels(&s);
        label = label_buf[0..listed[0].len];
        @memcpy(label_buf[0..listed[0].len], listed[0]);
    }
    // What a Dump killed before its commit leaves: its bytes past the end
    // the file's header says is committed.
    {
        const file = try tmp.dir.openFile(std.testing.io, "pardes.xitdb", .{ .mode = .read_write });
        defer file.close(std.testing.io);
        const end = try file.length(std.testing.io);
        var junk: [70000]u8 = undefined;
        for (&junk, 0..) |*b, i| b.* = @truncate(i *% 131 +% 7);
        try file.writePositionalAll(std.testing.io, &junk, end);
    }
    var s = try Session.initShared(std.testing.allocator, dir);
    defer s.deinit();
    try std.testing.expectEqualStrings(label, (try labels(&s))[0]);
    try s.write("/ctl", s.at("Restore {s}\n", .{label}));
    try std.testing.expect(has(try s.read("/index"), "+New"));
    var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
    const scratch = while (rows.next()) |row| {
        if (has(row, "+New")) break row[0..std.mem.indexOfScalar(u8, row, ' ').?];
    } else return error.NoScratch;
    try s.expect(s.at("/pane/{s}/body", .{scratch}), "the dump before\n");
    try s.write("/ctl", "Dump\n");
    try std.testing.expectEqual(@as(usize, 2), (try labels(&s)).len);
}