summaryrefslogtreecommitdiff
path: root/test/behaviour/tree.zig
blob: 5680658920eb8a210272148e3de7a4ff50471b33 (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
//! docs/typ/reference.typ, The tree <tree>.
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 first column's serial, /layout's first word.
fn firstColumn(s: *Session) ![]const u8 {
    return std.mem.sliceTo(try s.read("/layout"), ' ');
}

test "tree: index lists serial kind dirty name column, and stats 0, a view generated by each read" {
    var s = try Session.initFile(std.testing.allocator, "hello\nthere\n");
    defer s.deinit();
    const n = try s.focused();
    const col = try firstColumn(&s);
    const clean = s.at("{d} text 0 /test.txt {s}\n", .{ n, col });
    const index = try s.read("/index");
    const at = std.mem.indexOf(u8, index, clean) orelse return error.NotListed;
    const st = try s.stat("/index");
    try std.testing.expectEqual(@as(u64, 0), st.size);
    try std.testing.expectEqual(@as(u16, 0o444), st.mode);
    try s.write(s.at("/pane/{d}/body", .{n}), "more\n");
    try std.testing.expect(has(try s.read("/index"), s.at("{d} text 1 /test.txt {s}\n", .{ n, col })));
    // Read from an offset, as any file.
    const kind = at + std.mem.indexOfScalar(u8, clean, ' ').? + 1;
    try std.testing.expectEqualStrings("text", try s.readAt("/index", kind, 4));
    const made = try s.newPane();
    const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{made})), "\n");
    try std.testing.expect(std.mem.endsWith(u8, try s.read("/index"), s.at("\n{d} text 0 {s} {s}\n", .{ made, name, col })));
}

test "tree: a terminal is listed as term with its directory as name, which a write to name cannot change" {
    var s = try Session.initTerm(std.testing.allocator);
    defer s.deinit();
    const n = try s.focused();
    const dir = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n");
    try std.testing.expect(std.mem.startsWith(u8, dir, "/"));
    try std.testing.expect(has(try s.read("/index"), s.at("{d} term 0 {s} {s}\n", .{ n, dir, try firstColumn(&s) })));
    try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/name", .{n}), "/elsewhere\n"));
    try std.testing.expect(std.mem.startsWith(u8, s.said, "invalid rename: a terminal is named"));
}

test "tree: a body takes a 60 KB write whole, a truncating one replaces it, and a pane not there is refused" {
    var s = try Session.initFile(std.testing.allocator, "initial\n");
    defer s.deinit();
    var payload: std.ArrayList(u8) = .empty;
    for (0..3000) |_| try payload.appendSlice(s.arena.allocator(), "const value = \"λ\";\n");
    const n = try s.newPane();
    const body = s.at("/pane/{d}/body", .{n});
    try s.write(body, payload.items);
    try s.expect(body, payload.items);
    try s.replace(body, "replaced\n");
    try s.expect(body, "replaced\n");
    try std.testing.expectError(error.Refused, s.read("/pane/4294967295/body"));
    try s.expect(body, "replaced\n");
}