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