╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const ui = @import("../ui.zig"); 4 const Commits = @import("../ui/Repo/Commits.zig"); 5 const Events = @import("../ui/Repo/Events.zig"); 6 const xit = @import("xit"); 7 8 test "web controls omit hidden inputs but retain off-screen inputs" { 9 const wgt = xit.xitui.widget; 10 var arena = std.heap.ArenaAllocator.init(std.testing.allocator); 11 defer arena.deinit(); 12 const allocator = arena.allocator(); 13 var session = ui.Session{ .arena = &arena, .page_arena = &arena, .is_terminal = false }; 14 var root = ui.Widget{ .box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }) }; 15 defer root.deinit(allocator); 16 var scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }) }, .{ .web_native = true }); 17 for ([_][]const u8{ "visible", "hidden", "offscreen" }, 0..) |name, i| { 18 var input = try wgt.TextInput.init(allocator, .{ .name = name, .render_content = false }); 19 input.getFocus().mode = .all; 20 try scroll.child.box.children.put(allocator, input.getFocus().id, .{ 21 .widget = .{ .text_input = input }, 22 .rect = null, 23 .min_size = .{ .width = 20, .height = 3 }, 24 .hidden = i == 1, 25 }); 26 } 27 for (scroll.child.box.children.values()) |*child| { 28 const input = &child.widget.text_input; 29 try session.text_inputs.put(allocator, input.getFocus().id, input); 30 } 31 try root.box.children.put(allocator, scroll.getFocus().id, .{ .widget = .{ .scroll = scroll }, .rect = null, .min_size = null }); 32 const focus = root.getFocus(); 33 try root.build(allocator, .{ .min_size = .{ .width = null, .height = null }, .max_size = .{ .width = 30, .height = 3 } }, focus); 34 35 const children = &root.box.children.values()[0].widget.scroll.child.box.children; 36 const offscreen = children.values()[2].rect orelse return error.MissingRect; 37 try std.testing.expect(offscreen.y >= 3); 38 const html = try @import("../web.zig").generateHtml(allocator, &root, &session); 39 try std.testing.expect(std.mem.indexOf(u8, html, "name=\"visible\"") != null); 40 try std.testing.expect(std.mem.indexOf(u8, html, "name=\"hidden\"") == null); 41 try std.testing.expect(std.mem.indexOf(u8, html, "name=\"offscreen\"") != null); 42 } 43 44 test "diff windows span the net changes across commits" { 45 const io = std.testing.io; 46 const allocator = std.testing.allocator; 47 const Diff = Commits.Diff; 48 const opts: xit.repo.RepoOpts(.xit) = .{ .is_test = true }; 49 var temp = std.testing.tmpDir(.{}); 50 defer temp.cleanup(); 51 const path = try temp.dir.realPathFileAlloc(io, ".", allocator); 52 defer allocator.free(path); 53 var repo = try xit.repo.Repo(.xit, opts).init(io, allocator, .{ .path = path }); 54 defer repo.deinit(io, allocator); 55 var arena = std.heap.ArenaAllocator.init(allocator); 56 defer arena.deinit(); 57 58 // the shared base is absent from the diff 59 { 60 const file = try repo.core.work_dir.createFile(io, "shared.txt", .{}); 61 defer file.close(io); 62 try file.writeStreamingAll(io, "shared\n"); 63 } 64 try repo.add(io, allocator, &.{"shared.txt"}); 65 const base = try repo.commit(io, allocator, .{ .message = "base" }); 66 67 // changes from earlier commits must survive into the combined diff 68 var head = base; 69 for (0..Diff.page_size + 1) |i| { 70 var name_buf: [32]u8 = undefined; 71 const name = try std.fmt.bufPrint(&name_buf, "file-{d:0>2}.txt", .{i}); 72 { 73 const file = try repo.core.work_dir.createFile(io, name, .{}); 74 defer file.close(io); 75 try file.writeStreamingAll(io, "added\n"); 76 } 77 try repo.add(io, allocator, &.{name}); 78 head = try repo.commit(io, allocator, .{ .message = "add file" }); 79 } 80 const first = try Diff.render(.xit, opts, io, allocator, arena.allocator(), &repo, &base, head, 0, ""); 81 try std.testing.expectEqual(Diff.page_size, first.hunks.len); 82 try std.testing.expect(first.start == 0 and first.has_more); 83 try std.testing.expectEqualStrings("file-00.txt", first.hunks[0].path orelse return error.MissingPath); 84 const last = try Diff.render(.xit, opts, io, allocator, arena.allocator(), &repo, &base, head, Diff.page_size, ""); 85 try std.testing.expectEqual(1, last.hunks.len); 86 try std.testing.expect(last.start > 0 and !last.has_more); 87 try std.testing.expectEqualStrings("file-10.txt", last.hunks[0].path orelse return error.MissingPath); 88 const filtered = try Diff.render(.xit, opts, io, allocator, arena.allocator(), &repo, &base, head, 0, "file-00.txt"); 89 try std.testing.expectEqual(1, filtered.hunks.len); 90 try std.testing.expect(!filtered.has_more); 91 92 // the standalone pane links to the next fork diff window in both clients 93 const id = "11111111111111111111111111111111"; 94 const data = Diff{ .route = .{ .fork = .{ .identity = "alice/project", .id = id } }, .window = first }; 95 for ([_]bool{ true, false }) |terminal| { 96 var session = ui.Session{ .arena = &arena, .page_arena = &arena, .is_terminal = terminal }; 97 session.data.current_page = ui.RoutablePage.forkDiffRoute("alice/project", id, 0, "") orelse return error.BadRoute; 98 var view = try Diff.View.init(allocator, &data, &session); 99 defer view.deinit(allocator); 100 const focus = view.getFocus(); 101 try view.build(allocator, .{ .min_size = .{ .width = null, .height = null }, .max_size = .{ .width = 100, .height = 30 } }, focus); 102 const box = &view.scroll.child.box; 103 try std.testing.expectEqual(first.hunks.len * 2 + 1, box.children.count()); 104 const file = ui.crossPageLink(focus, box.children.keys()[0], session.data) orelse return error.MissingFileLink; 105 try std.testing.expectEqualStrings("file-00.txt", file.fork_diff.path.slice()); 106 const next = ui.crossPageLink(focus, box.children.keys()[box.children.count() - 1], session.data) orelse return error.MissingLink; 107 try std.testing.expectEqual(Diff.page_size, next.fork_diff.start); 108 } 109 } 110 111 // the "next" row at the bottom of the commits list must be recognized as a 112 // cross-page link (so a click navigates), exactly like the diff pane's "next". 113 test "commits list next row is a cross-page link" { 114 const allocator = std.testing.allocator; 115 116 var arena = std.heap.ArenaAllocator.init(allocator); 117 defer arena.deinit(); 118 const identity = "alice/ziglings"; 119 const oid0 = "1111111111111111111111111111111111111111"; 120 const next_oid = "2222222222222222222222222222222222222222"; 121 122 const data = Commits{ 123 .location = .{ .repo = identity }, 124 .ref_or_oid = .object, 125 .ref_or_oid_value = oid0, 126 .commits = &.{ 127 .{ .oid = oid0, .date = "2024-01-01", .message = "first", .window = .{} }, 128 }, 129 .next_start = next_oid, 130 .header = try Commits.Header.init(arena.allocator(), .object, oid0), 131 }; 132 133 var session = ui.Session{ .arena = &arena, .page_arena = &arena, .is_terminal = true }; 134 session.data.current_page = ui.RoutablePage.repoCommitsRoute(identity, .object, oid0, 0, "").?; 135 136 var view = try Commits.View.init(allocator, &data, &session); 137 defer view.deinit(allocator); 138 139 const root_focus = view.getFocus(); 140 try view.build(allocator, .{ 141 .min_size = .{ .width = null, .height = null }, 142 .max_size = .{ .width = 120, .height = 60 }, 143 }, root_focus); 144 145 // the "next" row is the last child of the list box: the view's outer box 146 // holds the sub-header then the list/diff split, whose first child is the 147 // list scroll. 148 const content = &view.box.children.values()[1].widget.box; 149 const lb = &content.children.values()[0].widget.scroll.child.box; 150 const next_id = lb.children.keys()[lb.children.count() - 1]; 151 152 const route = ui.crossPageLink(root_focus, next_id, session.data); 153 try std.testing.expect(route != null); 154 } 155 156 // a ref name with a '/' is url-encoded in the route, so it survives the 157 // '/'-delimited url round-trip as a single segment (kind + value) rather than 158 // being mis-parsed as extra path. 159 test "encoded ref name survives the commits url round-trip" { 160 var arena = std.heap.ArenaAllocator.init(std.testing.allocator); 161 defer arena.deinit(); 162 const RP = ui.RoutablePage; 163 164 // the route layer holds the value already url-encoded ("feature%2Ffoo"). 165 const route = RP.repoCommitsRoute("alice/ziglings", .branch, "feature%2Ffoo", 0, "").?; 166 const url = try route.toUrl(&arena); 167 try std.testing.expectEqualStrings("/repo/alice/ziglings/commits/branch:feature%2Ffoo", url); 168 169 // parsing it back yields the same route, and the ref splits out intact. 170 const parsed = RP.fromUrl(url); 171 try std.testing.expect(parsed != null); 172 const parsed_route = parsed.?.repo_commits; 173 try std.testing.expectEqual(RP.RefOrOid.branch, parsed_route.ref_or_oid.?); 174 try std.testing.expectEqualStrings("feature%2Ffoo", parsed_route.value.slice()); 175 } 176 177 test "sync creates missing event branches and preserves head" { 178 const io = std.testing.io; 179 const allocator = std.testing.allocator; 180 181 var temp = std.testing.tmpDir(.{}); 182 defer temp.cleanup(); 183 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 184 defer allocator.free(temp_path); 185 186 const local_path = try std.fs.path.join(allocator, &.{ temp_path, "local" }); 187 defer allocator.free(local_path); 188 const remote_path = try std.fs.path.join(allocator, &.{ temp_path, "remote" }); 189 defer allocator.free(remote_path); 190 191 const Repo = xit.repo.Repo(.git, .{}); 192 const RemoteRepo = xit.repo.Repo(.git, .{}); 193 { 194 var remote = try RemoteRepo.init(io, allocator, .{ .path = remote_path }); 195 remote.deinit(io, allocator); 196 } 197 { 198 var local = try Repo.init(io, allocator, .{ .path = local_path }); 199 defer local.deinit(io, allocator); 200 try local.addConfig(io, allocator, .{ .name = "user.name", .value = "user" }); 201 try local.addConfig(io, allocator, .{ .name = "user.email", .value = "user@haxy" }); 202 { 203 const file = try local.core.work_dir.createFile(io, "keep.txt", .{}); 204 defer file.close(io); 205 try file.writeStreamingAll(io, "keep"); 206 } 207 try local.add(io, allocator, &.{"keep.txt"}); 208 _ = try local.commit(io, allocator, .{ .message = "master" }); 209 try local.addRemote(io, allocator, .{ .name = "origin", .value = remote_path }); 210 211 const id = [_]u8{1} ** evt.event_id_size; 212 try evt.consume(.local, .repo, .git, .{}, io, allocator, &local, evt.events_ref, &.{.{ 213 .id = std.fmt.bytesToHex(id, .lower), 214 .author = .{ .name = "haxy", .email = "user@haxy" }, 215 .event = .{ .issue = .{ .title = "sync", .description = "", .tags = "" } }, 216 }}); 217 } 218 219 try std.testing.expectEqual(null, try Events.sync(io, allocator, .{ .path = local_path, .repo_kind = .git })); 220 { 221 var local = try Repo.open(io, allocator, .{ .path = local_path }); 222 defer local.deinit(io, allocator); 223 const id = [_]u8{2} ** evt.event_id_size; 224 try evt.consume(.local, .repo, .git, .{}, io, allocator, &local, evt.events_ref, &.{.{ 225 .id = std.fmt.bytesToHex(id, .lower), 226 .author = .{ .name = "haxy", .email = "user@haxy" }, 227 .event = .{ .issue = .{ .title = "local", .description = "", .tags = "" } }, 228 }}); 229 } 230 { 231 var remote = try RemoteRepo.open(io, allocator, .{ .path = remote_path }); 232 defer remote.deinit(io, allocator); 233 const id = [_]u8{3} ** evt.event_id_size; 234 try evt.consume(.local, .repo, .git, .{}, io, allocator, &remote, evt.events_ref, &.{.{ 235 .id = std.fmt.bytesToHex(id, .lower), 236 .author = .{ .name = "haxy", .email = "user@haxy" }, 237 .event = .{ .issue = .{ .title = "remote", .description = "", .tags = "" } }, 238 }}); 239 } 240 try std.testing.expectEqual(null, try Events.sync(io, allocator, .{ .path = local_path, .repo_kind = .git })); 241 { 242 var local = try Repo.open(io, allocator, .{ .path = local_path }); 243 defer local.deinit(io, allocator); 244 try local.removeBranch(io, .{ .name = evt.events_ref.name }); 245 try evt.consume(.local, .repo, .git, .{}, io, allocator, &local, evt.events_ref, &.{}); 246 } 247 try std.testing.expectEqual(null, try Events.sync(io, allocator, .{ .path = local_path, .repo_kind = .git })); 248 249 var local = try Repo.open(io, allocator, .{ .path = local_path }); 250 defer local.deinit(io, allocator); 251 var head_buffer: [xit.ref.MAX_REF_CONTENT_SIZE]u8 = undefined; 252 const head = try local.head(io, &head_buffer); 253 switch (head) { 254 .ref => |ref| try std.testing.expectEqualStrings("master", ref.name), 255 .oid => return error.TestExpectedEqual, 256 } 257 const kept = try local.core.work_dir.openFile(io, "keep.txt", .{}); 258 kept.close(io); 259 const local_tip = (try local.readRef(io, evt.events_ref)) orelse return error.TestExpectedEqual; 260 261 var remote = try RemoteRepo.open(io, allocator, .{ .path = remote_path }); 262 defer remote.deinit(io, allocator); 263 const remote_tip = (try remote.readRef(io, evt.events_ref)) orelse return error.TestExpectedEqual; 264 try std.testing.expectEqualStrings(&local_tip, &remote_tip); 265 266 var start_oids = [_][local_tip.len]u8{local_tip}; 267 var commit_iter = try local.log(io, allocator, .{ .start_oids = &start_oids }); 268 defer commit_iter.deinit(); 269 var merge_commit = (try commit_iter.next(allocator)) orelse return error.TestExpectedEqual; 270 defer merge_commit.deinit(); 271 const parent_oids = merge_commit.content.commit.metadata.parent_oids orelse return error.TestExpectedEqual; 272 try std.testing.expectEqual(2, parent_oids.len); 273 274 var arena = std.heap.ArenaAllocator.init(allocator); 275 defer arena.deinit(); 276 const status = try Events.Header.local(.git, .{}, arena.allocator(), &local, io, null); 277 try std.testing.expectEqualStrings("nothing to sync", status.sync_status orelse return error.TestExpectedEqual); 278 } 279