summaryrefslogtreecommitdiff
path: root/test/behaviour/root_ctl.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/behaviour/root_ctl.zig')
-rw-r--r--test/behaviour/root_ctl.zig123
1 files changed, 123 insertions, 0 deletions
diff --git a/test/behaviour/root_ctl.zig b/test/behaviour/root_ctl.zig
index b9909eef..7cf3025e 100644
--- a/test/behaviour/root_ctl.zig
+++ b/test/behaviour/root_ctl.zig
@@ -140,3 +140,126 @@ test "root-ctl: Joincol with no column to the right and Theme with no such theme
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);
+}