╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ undo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
4 const xit = @import("xit"); 5 const Undo = ui.Repo.Undo; 6 const allocator = std.testing.allocator; 7 const io = std.testing.io; 8 const constraint: xit.xitui.layout.Constraint = .{ .min_size = .{ .width = null, .height = null }, .max_size = .{ .width = 100, .height = 40 } }; 9 10 test "undo history paginates backwards and undo appends a restorable state" { 11 var temp = std.testing.tmpDir(.{}); 12 defer temp.cleanup(); 13 const path = try temp.dir.realPathFileAlloc(io, ".", allocator); 14 defer allocator.free(path); 15 const opts: xit.repo.RepoOpts(.xit) = .{ .is_test = true }; 16 var repo = try xit.repo.Repo(.xit, opts).init(io, allocator, .{ .path = path }); 17 defer repo.deinit(io, allocator); 18 var arena = std.heap.ArenaAllocator.init(allocator); 19 defer arena.deinit(); 20 const initial = try Undo.init(opts, &arena, &repo, null, "", null, .{ .location = .{ .repo = "" } }); 21 try std.testing.expectEqual(1, initial.count); 22 for (0..25) |index| { 23 const value = try arena.allocator().print("{d}", .{index}); 24 try repo.addConfig(io, allocator, .{ .name = "undo.value", .value = value }); 25 } 26 const first = try Undo.init(opts, &arena, &repo, null, "", null, .{ .location = .{ .repo = "" } }); 27 try std.testing.expectEqual(20, first.items.len); 28 try std.testing.expectEqual(25, first.items[0].index); 29 const second = try Undo.init(opts, &arena, &repo, null, "", first.next, .{ .location = .{ .repo = "" } }); 30 try std.testing.expectEqual(6, second.items.len); 31 try std.testing.expectEqual(5, second.items[0].index); 32 try std.testing.expectEqual(0, second.items[5].index); 33 try std.testing.expectEqual(null, second.next); 34 try std.testing.expectError(error.NotFound, Undo.init(opts, &arena, &repo, null, "", 26, .{ .location = .{ .repo = "" } })); 35 try std.testing.expectError(error.InvalidHistoryIndex, repo.undo(io, 0)); 36 try std.testing.expectError(error.InvalidHistoryIndex, repo.undo(io, 26)); 37 38 // undo the newest change, then a range, then undo that undo. 39 try repo.undo(io, 25); 40 try expectConfig(&repo, "23"); 41 try repo.undo(io, 10); 42 try expectConfig(&repo, "8"); 43 const undone = try Undo.init(opts, &arena, &repo, null, "", null, .{ .location = .{ .repo = "" } }); 44 try std.testing.expectEqual(28, undone.count); 45 try std.testing.expectEqualStrings("undo", undone.items[0].action); 46 try repo.undo(io, undone.items[0].index); 47 try expectConfig(&repo, "23"); 48 } 49 50 fn expectConfig(repo: anytype, expected: []const u8) !void { 51 var config = try repo.listConfig(io, allocator); 52 defer config.deinit(); 53 const section = config.local_sections.get("undo") orelse return error.MissingSection; 54 try std.testing.expectEqualStrings(expected, section.get("value") orelse return error.MissingValue); 55 } 56 57 test "repo undo visibility and fresh owner authorization" { 58 var temp = std.testing.tmpDir(.{}); 59 defer temp.cleanup(); 60 var arena = std.heap.ArenaAllocator.init(allocator); 61 defer arena.deinit(); 62 const aa = arena.allocator(); 63 const base = try temp.dir.realPathFileAlloc(io, ".", aa); 64 const admin_path = try std.Io.Dir.path.join(aa, &.{ base, "admin" }); 65 const users_dir = try std.Io.Dir.path.join(aa, &.{ base, "users" }); 66 var admin = try xit.repo.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_path }); 67 defer admin.deinit(io, allocator); 68 const owner: [evt.event_id_size]u8 = @splat(1); 69 const writer: [evt.event_id_size]u8 = @splat(2); 70 const reader: [evt.event_id_size]u8 = @splat(3); 71 const repo_id: [evt.event_id_size]u8 = @splat(4); 72 const author: evt.CommitAuthor = .{ .name = "test", .email = "test@example.test" }; 73 const writer_hex = std.fmt.bytesToHex(writer, .lower); 74 var repo_event: evt.Repo = .{ .user_id = std.fmt.bytesToHex(owner, .lower), .name = "demo", .description = "", .read_access = .public }; 75 const seed = [_]evt.EventWithId{ 76 .{ .id = std.fmt.bytesToHex(owner, .lower), .author = author, .event = .{ .user = .{ .name = "alice", .email = "alice@example.test", .password_hash = "unused" } } }, 77 .{ .id = writer_hex, .author = author, .event = .{ .user = .{ .name = "writer", .email = "writer@example.test", .password_hash = "unused" } } }, 78 .{ .id = std.fmt.bytesToHex(reader, .lower), .author = author, .event = .{ .user = .{ .name = "reader", .email = "reader@example.test", .password_hash = "unused" } } }, 79 }; 80 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &seed); 81 var user_repo = try evt.initUserRepo(io, allocator, try evt.userRepoPath(aa, users_dir, &owner)); 82 defer user_repo.deinit(io, allocator); 83 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, &user_repo, evt.events_ref, &.{ 84 .{ .id = std.fmt.bytesToHex(repo_id, .lower), .author = author, .event = .{ .repo = repo_event } }, 85 .{ .id = std.fmt.bytesToHex(evt.Grant.idOf(&repo_id, &writer), .lower), .author = author, .event = .{ .grant = .{ .target_id = std.fmt.bytesToHex(repo_id, .lower), .user_id = writer_hex, .role = .write } } }, 86 }); 87 const path = try evt.repoPath(aa, users_dir, &owner, &repo_id); 88 { 89 var repo = try xit.repo.Repo(.xit, .{}).init(io, allocator, .{ .path = path }); 90 defer repo.deinit(io, allocator); 91 try repo.addConfig(io, allocator, .{ .name = "undo.value", .value = "first" }); 92 try repo.addConfig(io, allocator, .{ .name = "undo.value", .value = "second" }); 93 } 94 for ([_]?[]const u8{ &owner, &writer, &reader, null }, [_]bool{ true, true, false, false }) |user, allowed| { 95 var session = try ui.Session.init(&arena, &admin, .{ .user_id = user }); 96 session.io = io; 97 session.users_dir = users_dir; 98 const files = ui.RoutablePage.repoFilesRoute("alice:demo", null, "", "", 0).?; 99 session.data.current_page = files; 100 const page = try ui.Repo.init(&arena, &session, files); 101 try std.testing.expectEqual(allowed, page.undo != null); 102 if (page.undo) |undo| try std.testing.expectEqual(user != null and std.mem.eql(u8, user.?, &owner), undo.handle.canUndo()); 103 var root = ui.Widget{ .repo = try ui.Repo.View.init(allocator, &page, &session) }; 104 defer root.deinit(allocator); 105 try root.build(allocator, constraint, root.getFocus()); 106 const html = try @import("../web.zig").generateHtml(allocator, &root, &session); 107 defer allocator.free(html); 108 try std.testing.expectEqual(allowed, std.mem.indexOf(u8, html, "/alice:demo/undo") != null); 109 const route = ui.RoutablePage.repoUndoRoute("alice:demo", null).?; 110 if (allowed) { 111 _ = try ui.Repo.init(&arena, &session, route); 112 } else { 113 try std.testing.expectError(error.NotFound, ui.Repo.init(&arena, &session, route)); 114 } 115 if (user == null or !std.mem.eql(u8, user.?, &owner)) { 116 try std.testing.expectError(error.Forbidden, Undo.perform(allocator, &session, ui.RoutablePage.repoUndoRoute("alice:demo", 2).?.repo_undo)); 117 } 118 } 119 var session = try ui.Session.init(&arena, &admin, .{ .user_id = &owner }); 120 session.io = io; 121 session.users_dir = users_dir; 122 try Undo.perform(allocator, &session, ui.RoutablePage.repoUndoRoute("alice:demo", 2).?.repo_undo); 123 try std.testing.expectEqual(null, session.next_page.?.repo_undo.index); 124 { 125 var repo = try xit.repo.Repo(.xit, .{}).open(io, allocator, .{ .path = path }); 126 defer repo.deinit(io, allocator); 127 try expectConfig(&repo, "first"); 128 const data = try Undo.init(.{}, &arena, &repo, null, "alice:demo", null, .{ .location = .{ .repo = "" } }); 129 try std.testing.expectEqualStrings("undo", data.items[0].action); 130 } 131 // keep the old session/page moment, but transfer ownership to the writer. 132 repo_event.user_id = writer_hex; 133 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, &user_repo, evt.events_ref, &.{.{ .id = std.fmt.bytesToHex(repo_id, .lower), .author = author, .event = .{ .repo = repo_event } }}); 134 session.next_page = null; 135 Undo.handleRequest(allocator, &session, ui.RoutablePage.repoUndoRoute("alice:demo", 1).?.repo_undo); 136 try std.testing.expectEqualStrings("Forbidden", session.data.undo_failure.?); 137 try std.testing.expectEqual(null, session.next_page); 138 } 139 140 test "local mode has no undo" { 141 inline for ([_]xit.repo.RepoKind{ .git, .xit }) |kind| { 142 var temp = std.testing.tmpDir(.{}); 143 defer temp.cleanup(); 144 var arena = std.heap.ArenaAllocator.init(allocator); 145 defer arena.deinit(); 146 const path = try temp.dir.realPathFileAlloc(io, ".", arena.allocator()); 147 { 148 var repo = try xit.repo.Repo(kind, .{}).init(io, allocator, .{ .path = path }); 149 defer repo.deinit(io, allocator); 150 } 151 var session = ui.Session{ .arena = &arena, .page_arena = &arena, .io = io, .local = .{ .path = path, .repo_kind = kind }, .data = .{ .host_kind = .local } }; 152 const data = try ui.Repo.init(&arena, &session, ui.RoutablePage.repoFilesRoute("", null, "", "", 0).?); 153 try std.testing.expect(data.undo == null); 154 try std.testing.expectError(error.NotFound, ui.Repo.init(&arena, &session, ui.RoutablePage.repoUndoRoute("", null).?)); 155 } 156 } 157 158 test "commit undo records use the formatted description" { 159 var temp = std.testing.tmpDir(.{}); 160 defer temp.cleanup(); 161 var arena = std.heap.ArenaAllocator.init(allocator); 162 defer arena.deinit(); 163 const path = try temp.dir.realPathFileAlloc(io, ".", arena.allocator()); 164 const opts: xit.repo.RepoOpts(.xit) = .{ .is_test = true }; 165 var repo = try xit.repo.Repo(.xit, opts).init(io, allocator, .{ .path = path }); 166 defer repo.deinit(io, allocator); 167 _ = try repo.commitAtRef(io, allocator, .{ .message = "example commit" }, null, .{ .kind = .head, .name = "master" }); 168 const data = try Undo.init(opts, &arena, &repo, null, "", null, .{ .location = .{ .repo = "" } }); 169 try std.testing.expect(std.mem.startsWith(u8, data.items[0].description, "commit -m \"example commit\"")); 170 } 171 172 test "malformed undo history is shown as an error" { 173 var temp = std.testing.tmpDir(.{}); 174 defer temp.cleanup(); 175 var arena = std.heap.ArenaAllocator.init(allocator); 176 defer arena.deinit(); 177 const aa = arena.allocator(); 178 179 // a server with one user who owns the repo 180 const base = try temp.dir.realPathFileAlloc(io, ".", aa); 181 const admin_path = try std.Io.Dir.path.join(aa, &.{ base, "admin" }); 182 const users_dir = try std.Io.Dir.path.join(aa, &.{ base, "users" }); 183 var admin = try xit.repo.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_path }); 184 defer admin.deinit(io, allocator); 185 const owner: [evt.event_id_size]u8 = @splat(1); 186 const repo_id: [evt.event_id_size]u8 = @splat(4); 187 const author: evt.CommitAuthor = .{ .name = "test", .email = "test@example.test" }; 188 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 189 .{ .id = std.fmt.bytesToHex(owner, .lower), .author = author, .event = .{ .user = .{ .name = "alice", .email = "alice@example.test", .password_hash = "unused" } } }, 190 }); 191 var user_repo = try evt.initUserRepo(io, allocator, try evt.userRepoPath(aa, users_dir, &owner)); 192 defer user_repo.deinit(io, allocator); 193 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, &user_repo, evt.events_ref, &.{ 194 .{ .id = std.fmt.bytesToHex(repo_id, .lower), .author = author, .event = .{ .repo = .{ .user_id = std.fmt.bytesToHex(owner, .lower), .name = "demo", .description = "", .read_access = .public } } }, 195 }); 196 const path = try evt.repoPath(aa, users_dir, &owner, &repo_id); 197 const Repo = xit.repo.Repo(.xit, .{}); 198 var repo = try Repo.init(io, allocator, .{ .path = path }); 199 defer repo.deinit(io, allocator); 200 201 // append an invalid undo record without damaging the repository data. 202 const Corrupt = struct { 203 pub fn run(_: @This(), cursor: *Repo.DB.Cursor(.read_write)) !void { 204 const moment = try Repo.DB.HashMap(.read_write).init(cursor.*); 205 try moment.put(xit.hash.hashInt(.sha1, "undo"), .{ .bytes = "bad" }); 206 } 207 }; 208 const history = try Repo.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 209 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Corrupt{}); 210 211 var session = try ui.Session.init(&arena, &admin, .{ .user_id = &owner }); 212 session.io = io; 213 session.users_dir = users_dir; 214 // tabs switch in-page, so every tab carries the undo data and its failure 215 const files = try ui.Repo.init(&arena, &session, ui.RoutablePage.repoFilesRoute("alice:demo", null, "", "", 0).?); 216 try std.testing.expectEqual(0, files.undo.?.items.len); 217 try std.testing.expectEqualStrings("InvalidUndoRecord", files.undo.?.failure.?); 218 const undo = try ui.Repo.init(&arena, &session, ui.RoutablePage.repoUndoRoute("alice:demo", null).?); 219 try std.testing.expectEqualStrings("InvalidUndoRecord", undo.undo.?.failure.?); 220 221 var root = ui.Widget{ .repo_undo = try Undo.View.init(allocator, &undo.undo.?, &session) }; 222 defer root.deinit(allocator); 223 try root.build(allocator, constraint, root.getFocus()); 224 const html = try @import("../web.zig").generateHtml(allocator, &root, &session); 225 defer allocator.free(html); 226 try std.testing.expect(std.mem.indexOf(u8, html, "InvalidUndoRecord") != null); 227 } 228