╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const hx = @import("haxy"); 3 const evt = hx.event; 4 const xit = hx.xit; 5 const rp = xit.repo; 6 const hash = xit.hash; 7 8 test "simple" { 9 const io = std.testing.io; 10 const allocator = std.testing.allocator; 11 const temp_dir_name = "temp-event-simple"; 12 13 // create the temp dir 14 const cwd = std.Io.Dir.cwd(); 15 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 16 if (temp_dir_or_err) |*temp_dir| { 17 temp_dir.close(io); 18 try cwd.deleteTree(io, temp_dir_name); 19 } else |_| {} 20 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 21 defer cwd.deleteTree(io, temp_dir_name) catch {}; 22 defer temp_dir.close(io); 23 24 const cwd_path = try std.process.currentPathAlloc(io, allocator); 25 defer allocator.free(cwd_path); 26 27 const work_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 28 defer allocator.free(work_path); 29 30 const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; 31 const Repo = rp.Repo(.xit, repo_opts); 32 var repo = try Repo.init(io, allocator, .{ .path = work_path }); 33 defer repo.deinit(io, allocator); 34 35 // 36 // define test events 37 // 38 39 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 40 41 const first_event_id = evt.randomId(prng.random()); 42 43 const events_to_consume = [_]evt.Event{ 44 .{ 45 .id = std.fmt.bytesToHex(first_event_id, .lower), 46 .data = .{ 47 .issue = .{ 48 .title = "Login form clears password on validation error", 49 .description = "Submitting an invalid email address resets the password field. Preserve the field value and show an inline validation message.", 50 .tags = "bug\x00priority-high\x00ui", 51 }, 52 }, 53 }, 54 // this event edits the previous one because it has the same id 55 .{ 56 .id = std.fmt.bytesToHex(first_event_id, .lower), 57 .data = .{ 58 .issue = .{ 59 .title = "Login form clears password on validation error", 60 .description = "Submitting an invalid email address resets the password field and removes typed input. Preserve the field value and show an inline validation message.", 61 .tags = "bug\x00priority-low\x00ui", 62 }, 63 }, 64 }, 65 .{ 66 .id = std.fmt.bytesToHex(evt.randomId(prng.random()), .lower), 67 .data = .{ 68 .issue = .{ 69 .title = "Search results ignore archived project filter", 70 .description = "Filtering search results to active projects still returns issues from archived projects. Apply the archived flag before ranking results.", 71 .tags = "bug\x00search\x00backend", 72 }, 73 }, 74 }, 75 .{ 76 .id = std.fmt.bytesToHex(evt.randomId(prng.random()), .lower), 77 .data = .{ 78 .issue = .{ 79 .title = "Issue list does not persist selected sort order", 80 .description = "Changing the issue list sort order is lost after refresh. Store the selected sort field and direction with the user's view preferences.", 81 .tags = "enhancement\x00frontend\x00preferences", 82 }, 83 }, 84 }, 85 }; 86 87 // 88 // insert issues as commits in the repo 89 // 90 91 var arena = std.heap.ArenaAllocator.init(allocator); 92 defer arena.deinit(); 93 94 { 95 var json: std.Io.Writer.Allocating = .init(std.testing.allocator); 96 defer json.deinit(); 97 98 for (events_to_consume) |event| { 99 json.clearRetainingCapacity(); 100 101 try std.json.Stringify.value(event, .{}, &json.writer); 102 103 // commit the event into a special branch 104 _ = try repo.commitAtRef(io, allocator, .{ .message = json.written() }, null, .{ .kind = .head, .name = "haxy/meta" }); 105 } 106 } 107 108 // 109 // read and parse all of the events from the repo 110 // 111 112 const Commit = struct { 113 parent_oid: ?[hash.byteLen(repo_opts.hash)]u8, 114 oid: [hash.byteLen(repo_opts.hash)]u8, 115 message: []const u8, 116 117 fn readCommitsFromRepo(inner_arena: *std.heap.ArenaAllocator, inner_repo: *Repo, branch_name: []const u8) ![]@This() { 118 const inner_allocator = inner_arena.allocator(); 119 120 var commits: std.ArrayList(@This()) = .empty; 121 122 const ref = try inner_repo.readRef(io, .{ .kind = .head, .name = branch_name }) orelse return error.RefNotFound; 123 var commit_iter = try inner_repo.log(io, inner_allocator, &.{ref}); 124 125 // read the message from each commit 126 while (try commit_iter.next()) |commit_object| { 127 try commit_object.object_reader.seekTo(commit_object.content.commit.message_position); 128 const message = try commit_object.object_reader.interface.allocRemaining(inner_allocator, .unlimited); 129 130 var commit: @This() = .{ 131 .parent_oid = undefined, 132 .oid = undefined, 133 .message = message, 134 }; 135 136 _ = try std.fmt.hexToBytes(&commit.oid, &commit_object.oid); 137 138 if (commit_object.content.commit.metadata.parent_oids) |parent_oids| { 139 if (parent_oids.len > 0) { 140 _ = try std.fmt.hexToBytes(&(commit.parent_oid orelse unreachable), &parent_oids[0]); 141 } else { 142 commit.parent_oid = null; 143 } 144 } else { 145 commit.parent_oid = null; 146 } 147 148 try commits.append(inner_allocator, commit); 149 } 150 151 return commits.items; 152 } 153 }; 154 155 var events: std.ArrayList(evt.RepoEvent(repo_opts.hash)) = .empty; 156 defer events.deinit(allocator); 157 158 { 159 const commits = try Commit.readCommitsFromRepo(&arena, &repo, "haxy/meta"); 160 161 // parse commit messages as JSON into event values. 162 // add events in reverse order so the earliest event is first. 163 for (0..commits.len) |i| { 164 const commit = commits[commits.len - i - 1]; 165 const event = try std.json.parseFromSliceLeaky(evt.Event, arena.allocator(), commit.message, .{}); 166 try events.append(allocator, .{ 167 .parent_oid = commit.parent_oid, 168 .oid = commit.oid, 169 .event = event, 170 }); 171 } 172 } 173 174 const first_oid = events.items[0].oid; 175 176 // 177 // consume events into the database 178 // 179 180 { 181 try evt.consume(Repo.DB, repo_opts.hash, io, &repo.core.db, repo.core.db_file, events.items); 182 183 const history = try Repo.DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 184 185 // read the moment we just created 186 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 187 const moment = try Repo.DB.HashMap(.read_only).init(moment_cursor); 188 189 const haxy_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy")) orelse return error.NotFound; 190 const haxy = try Repo.DB.HashMap(.read_only).init(haxy_cursor); 191 192 const object_id_to_views_cursor = try haxy.getCursor(hash.hashInt(repo_opts.hash, "object-id->views")) orelse return error.NotFound; 193 const object_id_to_views = try Repo.DB.HashMap(.read_only).init(object_id_to_views_cursor); 194 195 // get the last object id 196 const last_object_id_cursor = try haxy.getCursor(hash.hashInt(repo_opts.hash, "last-object-id")) orelse return error.NotFound; 197 var last_object_id: [hash.byteLen(repo_opts.hash)]u8 = undefined; 198 _ = try last_object_id_cursor.readBytes(&last_object_id); 199 200 // get the latest views (the views generated by the last object id) 201 const views_cursor = try object_id_to_views.getCursor(hash.bytesToInt(repo_opts.hash, &last_object_id)) orelse return error.NotFound; 202 const views = try Repo.DB.HashMap(.read_only).init(views_cursor); 203 204 // get the map of issues 205 const event_id_to_issue_cursor = try views.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse return error.NotFound; 206 const event_id_to_issue = try Repo.DB.HashMap(.read_only).init(event_id_to_issue_cursor); 207 208 // get the issue out of the map that was edited 209 const first_issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &first_event_id)) orelse return error.NotFound; 210 const first_issue_map = try Repo.DB.HashMap(.read_only).init(first_issue_cursor); 211 const first_issue = try evt.EventData.read(Repo.DB, repo_opts.hash, arena.allocator(), first_issue_map, .issue); 212 213 // the description was correctly edited 214 try std.testing.expectEqualStrings(events_to_consume[1].data.issue.description, first_issue.issue.description); 215 216 // the tags were correctly edited 217 try std.testing.expectEqualStrings(events_to_consume[1].data.issue.tags, first_issue.issue.tags); 218 } 219 220 // 221 // define new test events without the edit event 222 // and insert them into the repo 223 // 224 225 const rebased_events_to_consume = [_]evt.Event{ 226 events_to_consume[2], 227 events_to_consume[3], 228 }; 229 230 { 231 var json: std.Io.Writer.Allocating = .init(std.testing.allocator); 232 defer json.deinit(); 233 234 for (rebased_events_to_consume, 0..) |event, i| { 235 json.clearRetainingCapacity(); 236 237 try std.json.Stringify.value(event, .{}, &json.writer); 238 239 // commit the event into a special branch 240 _ = try repo.commitAtRef( 241 io, 242 allocator, 243 .{ 244 .message = json.written(), 245 // the first event should have the first oid as its parent, 246 // because we want to keep the very first commit. 247 // and the rest will have null which causes them 248 // to have the previous commit as their parent 249 .parent_oids = if (i == 0) &.{std.fmt.bytesToHex(first_oid, .lower)} else null, 250 }, 251 null, 252 .{ .kind = .head, .name = "haxy/meta" }, 253 ); 254 } 255 } 256 257 // 258 // read and parse all of the events from the repo 259 // 260 261 events.clearRetainingCapacity(); 262 263 { 264 const commits = try Commit.readCommitsFromRepo(&arena, &repo, "haxy/meta"); 265 266 // parse commit messages as JSON into event values. 267 // add events in reverse order so the earliest event is first. 268 for (0..commits.len) |i| { 269 const commit = commits[commits.len - i - 1]; 270 const event = try std.json.parseFromSliceLeaky(evt.Event, arena.allocator(), commit.message, .{}); 271 try events.append(allocator, .{ 272 .parent_oid = commit.parent_oid, 273 .oid = commit.oid, 274 .event = event, 275 }); 276 } 277 } 278 279 // 280 // consume events into the database 281 // 282 283 { 284 try evt.consume(Repo.DB, repo_opts.hash, io, &repo.core.db, repo.core.db_file, events.items); 285 286 const history = try Repo.DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 287 288 // read the moment we just created 289 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 290 const moment = try Repo.DB.HashMap(.read_only).init(moment_cursor); 291 292 const haxy_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy")) orelse return error.NotFound; 293 const haxy = try Repo.DB.HashMap(.read_only).init(haxy_cursor); 294 295 const object_id_to_views_cursor = try haxy.getCursor(hash.hashInt(repo_opts.hash, "object-id->views")) orelse return error.NotFound; 296 const object_id_to_views = try Repo.DB.HashMap(.read_only).init(object_id_to_views_cursor); 297 298 // get the last object id 299 const last_object_id_cursor = try haxy.getCursor(hash.hashInt(repo_opts.hash, "last-object-id")) orelse return error.NotFound; 300 var last_object_id: [hash.byteLen(repo_opts.hash)]u8 = undefined; 301 _ = try last_object_id_cursor.readBytes(&last_object_id); 302 303 // get the latest views (the views generated by the last object id) 304 const views_cursor = try object_id_to_views.getCursor(hash.bytesToInt(repo_opts.hash, &last_object_id)) orelse return error.NotFound; 305 const views = try Repo.DB.HashMap(.read_only).init(views_cursor); 306 307 // get the map of issues 308 const event_id_to_issue_cursor = try views.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse return error.NotFound; 309 const event_id_to_issue = try Repo.DB.HashMap(.read_only).init(event_id_to_issue_cursor); 310 311 // get the issue out of the map that was edited 312 const first_issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &first_event_id)) orelse return error.NotFound; 313 const first_issue_map = try Repo.DB.HashMap(.read_only).init(first_issue_cursor); 314 const first_issue = try evt.EventData.read(Repo.DB, repo_opts.hash, arena.allocator(), first_issue_map, .issue); 315 316 // the description is no longer edited 317 try std.testing.expectEqualStrings(events_to_consume[0].data.issue.description, first_issue.issue.description); 318 319 // the tags are no longer edited 320 try std.testing.expectEqualStrings(events_to_consume[0].data.issue.tags, first_issue.issue.tags); 321 } 322 } 323