╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
867 868 { 869 var json: std.Io.Writer.Allocating = .init(std.testing.allocator); 870 defer json.deinit(); 871 872 for (events_to_consume) |event| { 873 json.clearRetainingCapacity(); 874 875 try std.json.Stringify.value(event, .{}, &json.writer); 876 877 // commit the event into a special branch 878 _ = try repo.commitAtRef(io, allocator, .{ .message = json.written() }, null, .{ .kind = .head, .name = "haxy/meta" }); 879 } 880 } 881 882 // 883 // consume events into the database 884 // 885 886 { 887 try evt.consume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }); 888 889 const history = try Repo.DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 890 891 // read the moment we just created 892 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 893 const moment = try Repo.DB.HashMap(.read_only).init(moment_cursor); 894 895 // get the last object id 896 const last_object_id_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id")) orelse return error.NotFound; 897 var last_object_id: [hash.byteLen(repo_opts.hash)]u8 = undefined; 898 _ = try last_object_id_cursor.readBytes(&last_object_id); 899 900 const haxy_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy")) orelse return error.NotFound; 901 const haxy = try Repo.DB.ArrayList(.read_only).init(haxy_cursor); 902 903 try std.testing.expectEqual(1, try haxy.count()); 904 905 const haxy_moments_cursor = try haxy.getCursor(-1) orelse return error.NotFound; 906 const haxy_moments = try Repo.DB.HashMap(.read_only).init(haxy_moments_cursor); 907 908 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &last_object_id)) orelse return error.NotFound; 909 const haxy_moment = try Repo.DB.HashMap(.read_only).init(haxy_moment_cursor); 910 911 // get the map of users 912 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->user")) orelse return error.NotFound; 913 const event_id_to_user = try Repo.DB.HashMap(.read_only).init(event_id_to_user_cursor); 914 915 // get the user out of the map that was edited 916 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(repo_opts.hash, &user_event_id)) orelse return error.NotFound; 917 const user_map = try Repo.DB.HashMap(.read_only).init(user_cursor); 918 const user = try evt.EventData.read(Repo.DB, repo_opts.hash, arena.allocator(), user_map, .user); 919 920 // the password was correctly edited 921 try std.testing.expectEqualStrings(events_to_consume[1].data.user.password_hash, user.user.password_hash); 922 923 // get the map of repos 924 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->repo")) orelse return error.NotFound; 925 const event_id_to_repo = try Repo.DB.HashMap(.read_only).init(event_id_to_repo_cursor); 926 927 // get the repo out of the map 928 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(repo_opts.hash, &repo_event_id)) orelse return error.NotFound; 929 const repo_map = try Repo.DB.HashMap(.read_only).init(repo_cursor); 930 const repo_event = try evt.EventData.read(Repo.DB, repo_opts.hash, arena.allocator(), repo_map, .repo); 931 932 try std.testing.expectEqualSlices(u8, &user_event_id, repo_event.repo.user_id); 933 try std.testing.expectEqualStrings("ziglings", repo_event.repo.name); 934 } 935 } 936