//! docs/typ/reference.typ, The 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"); }