╭ changed ─────────╮ │src/test/event.zig│ ╰──────────────────╯ ╭───────────╮ │← all files│ ╰───────────╯ ╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │805 const event_id_to_user = try Repo.DB.HashMap(.read_only).init(event_id_to_user_cursor); │ │806 │ │807 try std.testing.expect(null == try event_id_to_user.getCursor(hash.hashInt(repo_opts.hash, &user_event_id))); │ │808 + } │ │809 +} │ │810 + │ │811 +test "repos and users paginate in creation order" { │ │812 + const io = std.testing.io; │ │813 + const allocator = std.testing.allocator; │ │814 + const temp_dir_name = "temp-event-order"; │ │815 + │ │816 + const cwd = std.Io.Dir.cwd(); │ │817 + var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); │ │818 + if (temp_dir_or_err) |*temp_dir| { │ │819 + temp_dir.close(io); │ │820 + try cwd.deleteTree(io, temp_dir_name); │ │821 + } else |_| {} │ │822 + var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); │ │823 + defer cwd.deleteTree(io, temp_dir_name) catch {}; │ │824 + defer temp_dir.close(io); │ │825 + │ │826 + const cwd_path = try std.process.currentPathAlloc(io, allocator); │ │827 + defer allocator.free(cwd_path); │ │828 + const work_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); │ │829 + defer allocator.free(work_path); │ │830 + │ │831 + const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; │ │832 + const Repo = rp.Repo(.xit, repo_opts); │ │833 + var repo = try Repo.init(io, allocator, .{ .path = work_path }); │ │834 + defer repo.deinit(io, allocator); │ │835 + │ │836 + var arena = std.heap.ArenaAllocator.init(allocator); │ │837 + defer arena.deinit(); │ │838 + │ │839 + var prng = std.Random.DefaultPrng.init(std.testing.random_seed); │ │840 + const user_id = evt.EventWithId.randomId(prng.random()); │ │841 + var repo_ids: [4][evt.event_id_size]u8 = undefined; │ │842 + for (&repo_ids) |*id| id.* = evt.EventWithId.randomId(prng.random()); │ │843 + │ │844 + var pw_buf: [evt.User.password_hash_max_len]u8 = undefined; │ │845 + const pw = try evt.User.hashPassword("pw", &pw_buf, io); │ │846 + │ │847 + // asserts the timestamp->repo-id map holds exactly `expected` ids in that order │ │848 + const Check = struct { │ │849 + fn order( │ │850 + comptime DB: type, │ │851 + comptime hash_kind: hash.HashKind, │ │852 + moment: DB.HashMap(.read_only), │ │853 + expected: []const *const [evt.event_id_size]u8, │ │854 + ) !void { │ │855 + const cursor = try moment.getCursor(hash.hashInt(hash_kind, "timestamp->repo-id")) orelse return error.NotFound; │ │856 + const map = try DB.SortedMap(.read_only).init(cursor); │ │857 + try std.testing.expectEqual(expected.len, try map.count()); │ │858 + for (expected, 0..) |id, i| { │ │859 + const kv = (try map.getIndexKeyValuePair(@intCast(i))) orelse return error.NotFound; │ │860 + var buf: [evt.event_id_size]u8 = undefined; │ │861 + _ = try kv.value_cursor.readBytes(&buf); │ │862 + try std.testing.expectEqualSlices(u8, id, &buf); │ │863 + } │ │864 + } │ │865 + }; │ │866 + │ │867 + // one user, then four repos, each with its own creation timestamp (user@100, │ │868 + // repos@101..104) │ │869 + const events = [_]evt.EventWithId{ │ │870 + .{ .id = std.fmt.bytesToHex(user_id, .lower), .timestamp = 100, .event = .{ .user = .{ .name = "alice", .display_name = "Alice", .email = "alice@example.test", .password_hash = pw } } },│ │871 + .{ .id = std.fmt.bytesToHex(repo_ids[0], .lower), .timestamp = 101, .event = .{ .repo = .{ .user_id = &user_id, .name = "repo0", .description = "d0", .enable_issue = true } } }, │ │872 + .{ .id = std.fmt.bytesToHex(repo_ids[1], .lower), .timestamp = 102, .event = .{ .repo = .{ .user_id = &user_id, .name = "repo1", .description = "d1", .enable_issue = true } } }, │ │873 + .{ .id = std.fmt.bytesToHex(repo_ids[2], .lower), .timestamp = 103, .event = .{ .repo = .{ .user_id = &user_id, .name = "repo2", .description = "d2", .enable_issue = true } } }, │ │874 + .{ .id = std.fmt.bytesToHex(repo_ids[3], .lower), .timestamp = 104, .event = .{ .repo = .{ .user_id = &user_id, .name = "repo3", .description = "d3", .enable_issue = true } } }, │ │875 + }; │ │876 + try evt.commitAndConsume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }, &events); │ │877 + │ │878 + { │ │879 + const moment = try evt.currentMoment(repo_opts, &repo); │ │880 + try Check.order(Repo.DB, repo_opts.hash, moment, &.{ &repo_ids[0], &repo_ids[1], &repo_ids[2], &repo_ids[3] }); │ │881 + │ │882 + // the single user shows up in its own ordered map │ │883 + const ucur = try moment.getCursor(hash.hashInt(repo_opts.hash, "timestamp->user-id")) orelse return error.NotFound; │ │884 + const umap = try Repo.DB.SortedMap(.read_only).init(ucur); │ │885 + try std.testing.expectEqual(1, try umap.count()); │ │886 + } │ │887 + │ │888 + // delete repo1 -> dense order (no tombstone) │ │889 + try evt.commitAndConsume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }, &[_]evt.EventWithId{ │ │890 + .{ .id = std.fmt.bytesToHex(repo_ids[1], .lower), .timestamp = 200, .event = .{ .repo = null } }, │ │891 + }); │ │892 + { │ │893 + const moment = try evt.currentMoment(repo_opts, &repo); │ │894 + try Check.order(Repo.DB, repo_opts.hash, moment, &.{ &repo_ids[0], &repo_ids[2], &repo_ids[3] }); │ │895 + } │ │896 + │ │897 + // update repo0 at a later timestamp -> keeps its original slot │ │898 + try evt.commitAndConsume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }, &[_]evt.EventWithId{ │ │899 + .{ .id = std.fmt.bytesToHex(repo_ids[0], .lower), .timestamp = 300, .event = .{ .repo = .{ .user_id = &user_id, .name = "repo0", .description = "updated", .enable_issue = true } } }, │ │900 + }); │ │901 + { │ │902 + const moment = try evt.currentMoment(repo_opts, &repo); │ │903 + try Check.order(Repo.DB, repo_opts.hash, moment, &.{ &repo_ids[0], &repo_ids[2], &repo_ids[3] }); │ │904 + │ │905 + // the value really was updated │ │906 + const e2r_cur = try moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->repo")) orelse return error.NotFound; │ │907 + const e2r = try Repo.DB.HashMap(.read_only).init(e2r_cur); │ │908 + const rc = try e2r.getCursor(hash.hashInt(repo_opts.hash, &repo_ids[0])) orelse return error.NotFound; │ │909 + const rm = try Repo.DB.HashMap(.read_only).init(rc); │ │910 + const re = try evt.read(evt.Repo, Repo.DB, repo_opts.hash, &arena, rm); │ │911 + try std.testing.expectEqualStrings("updated", re.description); │ │912 } │ │913 } │ │914 │ ╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯