╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ admin.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
274 .{ .id = std.fmt.bytesToHex(repo_ids[2], .lower), .author = author, .timestamp = 5_000, .event = .{ .repo = .{ .user_id = &user_id, .name = "repo2", .description = "d2" } } }, 275 .{ .id = std.fmt.bytesToHex(repo_ids[3], .lower), .author = author, .timestamp = 20, .event = .{ .repo = .{ .user_id = &user_id, .name = "repo3", .description = "d3" } } }, 276 }; 277 try evt.consume(.server, .admin, .xit, repo_opts, io, allocator, &repo, evt.events_ref, &events); 278 279 { 280 const moment = try evt.currentMoment(repo_opts, &repo); 281 try Check.order(Repo.DB, repo_opts.hash, moment, &.{ &repo_ids[3], &repo_ids[2], &repo_ids[1], &repo_ids[0] }); 282 283 // the single user shows up in its own ordered set 284 const ucur = try moment.getCursor(hash.hashInt(repo_opts.hash, "user-id-set")) orelse return error.NotFound; 285 const uset = try Repo.DB.SortedSet(.read_only).init(ucur); 286 try std.testing.expectEqual(1, try uset.count()); 287 } 288 289 // removing repo1 retains its place in the canonical order 290 try evt.consume(.server, .admin, .xit, repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{ 291 .{ .id = std.fmt.bytesToHex(repo_ids[1], .lower), .author = author, .timestamp = 200, .event = .{ .repo = null } }, 292 }); 293 { 294 const moment = try evt.currentMoment(repo_opts, &repo); 295 try Check.order(Repo.DB, repo_opts.hash, moment, &.{ &repo_ids[3], &repo_ids[2], &repo_ids[1], &repo_ids[0] }); 296 } 297 298 // update repo0 at a later timestamp -> keeps its original place 299 try evt.consume(.server, .admin, .xit, repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{ 300 .{ .id = std.fmt.bytesToHex(repo_ids[0], .lower), .author = author, .timestamp = 300, .event = .{ .repo = .{ .user_id = &user_id, .name = "repo0", .description = "updated" } } }, 301 }); 302 { 303 const moment = try evt.currentMoment(repo_opts, &repo); 304 try Check.order(Repo.DB, repo_opts.hash, moment, &.{ &repo_ids[3], &repo_ids[2], &repo_ids[1], &repo_ids[0] }); 305 306 // the value really was updated 307 const e2r_cur = try moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->repo")) orelse return error.NotFound; 308 const e2r = try Repo.DB.HashMap(.read_only).init(e2r_cur); 309 const rc = try e2r.getCursor(hash.hashInt(repo_opts.hash, &repo_ids[0])) orelse return error.NotFound; 310 const rm = try Repo.DB.HashMap(.read_only).init(rc); 311 const re = try evt.read(evt.Repo.Record, Repo.DB, repo_opts.hash, &arena, rm); 312 try std.testing.expectEqualStrings("updated", re.event.description); 313 } 314 } 315 316 test "fork query and removal lifecycle" { 317 const io = std.testing.io; 318 const allocator = std.testing.allocator; 319 320 var temp = std.testing.tmpDir(.{}); 321 defer temp.cleanup(); 322 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 323 defer allocator.free(temp_path); 324 325 const repos_dir = try std.fs.path.join(allocator, &.{ temp_path, "repos" }); 326 defer allocator.free(repos_dir); 327 const admin_path = try std.fs.path.join(allocator, &.{ temp_path, "admin" }); 328 defer allocator.free(admin_path); 329 330 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 331 const user_id = evt.EventWithId.randomId(prng.random()); 332 const repo_id = evt.EventWithId.randomId(prng.random()); 333 const repo_id_hex = std.fmt.bytesToHex(repo_id, .lower); 334 const fork_id = evt.EventWithId.randomId(prng.random()); 335 const fork_id_hex = std.fmt.bytesToHex(fork_id, .lower); 336 337 var admin = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_path }); 338 defer admin.deinit(io, allocator); 339 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 340 .{ 341 .id = std.fmt.bytesToHex(user_id, .lower), 342 .timestamp = 1, 343 .author = author, 344 .event = .{ .user = .{ 345 .name = author.name, 346 .email = author.email, 347 .password_hash = "", 348 } }, 349 }, 350 .{ 351 .id = std.fmt.bytesToHex(repo_id, .lower), 352 .timestamp = 1, 353 .author = author, 354 .event = .{ .repo = .{ 355 .user_id = &user_id, 356 .name = "repo", 357 .description = "", 358 } }, 359 }, 360 }); 361 362 const target_path = try std.fs.path.join(allocator, &.{ repos_dir, &repo_id_hex }); 363 defer allocator.free(target_path); 364 var target = try rp.Repo(.xit, fork_repo_opts).init(io, allocator, .{ .path = target_path }); 365 defer target.deinit(io, allocator); 366 { 367 const file = try target.core.work_dir.createFile(io, "README", .{}); 368 defer file.close(io); 369 try file.writeStreamingAll(io, "repo\n"); 370 } 371 try target.add(io, allocator, &.{"README"}); 372 _ = try target.commit(io, allocator, .{ .message = "initial" }); 373 374 const draft_path = try fork.create(fork_repo_opts, io, allocator, repos_dir, &admin, .{ 375 .target_branch = "master", 376 .id = fork_id_hex, 377 .user_id = user_id, 378 .repo_id = repo_id, 379 .title = "add a feature", 380 .description = "a draft patch", 381 .tags = "enhancement", 382 .author = author, 383 .timestamp = 2, 384 }); 385 defer allocator.free(draft_path); 386 387 var arena = std.heap.ArenaAllocator.init(allocator); 388 defer arena.deinit(); 389 var moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 390 var record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, &fork_id)) orelse return error.NotFound; 391 try std.testing.expectEqualSlices(u8, &user_id, record.event.user_id); 392 try std.testing.expectEqualSlices(u8, &repo_id, record.event.repo_id); 393 try std.testing.expectEqual(.draft, record.event.stage); 394 try std.testing.expectEqual(1, try indexedForkCount(moment, evt.Fork.user_id_to_fork_id_set_key, &user_id)); 395 const draft_key = evt.Fork.draftKey(&repo_id, &user_id); 396 try std.testing.expectEqual(1, try indexedForkCount(moment, evt.Fork.repo_user_to_draft_id_set_key, &draft_key)); 397 398 var draft_repo = try rp.Repo(.xit, fork_repo_opts).open(io, allocator, .{ .path = draft_path }); 399 defer draft_repo.deinit(io, allocator); 400 const draft_moment = try evt.currentMoment(fork_repo_opts, &draft_repo); 401 const draft = (try evt.Patch.readById(evt.EventDB(fork_repo_opts.hash), fork_repo_opts.hash, draft_moment, &arena, &fork_id)) orelse return error.NotFound; 402 try std.testing.expectEqualStrings("add a feature", draft.event.title); 403 try std.testing.expectEqual(null, draft.event.revision); 404 405 var wrong_user_id = user_id; 406 wrong_user_id[0] ^= 1; 407 try std.testing.expectError(error.InvalidPatchDraft, fork.remove(io, allocator, repos_dir, &admin, &fork_id_hex, &wrong_user_id, author)); 408 try std.Io.Dir.accessAbsolute(io, draft_path, .{}); 409 410 try fork.remove(io, allocator, repos_dir, &admin, &fork_id_hex, &user_id, author); 411 try std.testing.expectError(error.FileNotFound, std.Io.Dir.accessAbsolute(io, draft_path, .{})); 412 _ = arena.reset(.retain_capacity); 413 moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 414 record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, &fork_id)) orelse return error.NotFound; 415 try std.testing.expect(record.removed); 416 try std.testing.expectEqual(0, try indexedForkCount(moment, evt.Fork.user_id_to_fork_id_set_key, &user_id)); 417 try std.testing.expectEqual(0, try indexedForkCount(moment, evt.Fork.repo_user_to_draft_id_set_key, &draft_key)); 418 419 try fork.remove(io, allocator, repos_dir, &admin, &fork_id_hex, &user_id, author); 420 } 421 422 fn indexedForkCount( 423 moment: evt.AdminDB.HashMap(.read_only), 424 index_key: []const u8, 425 parent_id: []const u8, 426 ) !u64 { 427 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 428 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 429 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 430 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 431 return try ids.count(); 432 } 433