1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! differential test: parse a sealed segment produced by upstream jetstream
//! (bootstrapped from the simulator) and check against the ground truth in
//! tests/fixtures/seg_0000000000.inspect.txt (values hardcoded below).
//!
//! regenerate fixtures: build upstream's `jetstream` binary, run the
//! simulator, `jetstream serve --relay-url=http://localhost:7777
//! --plc-url=http://localhost:7777 --max-backfill-repos=50`, wait for
//! steady state, copy <data-dir>/segments/seg_0000000000.jss +
//! `jetstream inspect-segment` output. skipped when the fixture is absent.
const std = @import("std");
const segment = @import("segment.zig");
const zat = @import("zat");
const testing = std.testing;
const fixture_path = "tests/fixtures/seg_0000000000.jss";
fn loadFixture(allocator: std.mem.Allocator) !?[]u8 {
var threaded: std.Io.Threaded = .init(allocator, .{});
defer threaded.deinit();
const io = threaded.io();
return std.Io.Dir.cwd().readFileAlloc(io, fixture_path, allocator, .limited(4 << 20)) catch null;
}
test "fixture: header + checksum match upstream inspect output" {
const bytes = (try loadFixture(testing.allocator)) orelse return error.SkipZigTest;
defer testing.allocator.free(bytes);
var sealed = try segment.Sealed.parse(testing.allocator, bytes);
defer sealed.deinit(testing.allocator);
try testing.expectEqual(@as(u64, 0x356b4de698e8df94), sealed.header.checksum);
try testing.expectEqual(@as(u32, 3), sealed.header.block_count);
try testing.expectEqual(@as(u32, 7018), sealed.header.event_count);
try testing.expectEqual(@as(u32, 100), sealed.header.unique_did_count);
try testing.expectEqual(@as(u64, 1), sealed.header.min_seq);
try testing.expectEqual(@as(u64, 8575), sealed.header.max_seq);
try testing.expectEqual(@as(u64, 0x78199), sealed.header.footer_offset);
// per-block stats from inspect --blocks table
const expect_blocks = [_]struct { offset: u64, comp: u32, uncomp: u32, events: u32, min_seq: u64, max_seq: u64 }{
.{ .offset = 0x100, .comp = 203396, .uncomp = 781999, .events = 2799, .min_seq = 1, .max_seq = 4096 },
.{ .offset = 0x31b8c, .comp = 117330, .uncomp = 426425, .events = 1500, .min_seq = 4097, .max_seq = 5596 },
.{ .offset = 0x4e5e6, .comp = 170923, .uncomp = 698431, .events = 2719, .min_seq = 5597, .max_seq = 8575 },
};
for (expect_blocks, sealed.block_index) |want, got| {
try testing.expectEqual(want.offset, got.offset);
try testing.expectEqual(want.comp, got.compressed_size);
try testing.expectEqual(want.uncomp, got.uncompressed_size);
try testing.expectEqual(want.events, got.event_count);
try testing.expectEqual(want.min_seq, got.min_seq);
try testing.expectEqual(want.max_seq, got.max_seq);
}
}
test "fixture: all blocks decode; events are well-formed and ordered" {
const bytes = (try loadFixture(testing.allocator)) orelse return error.SkipZigTest;
defer testing.allocator.free(bytes);
var sealed = try segment.Sealed.parse(testing.allocator, bytes);
defer sealed.deinit(testing.allocator);
var total: usize = 0;
var last_seq: u64 = 0;
var commit_payloads: usize = 0;
for (0..sealed.block_index.len) |i| {
var block = try sealed.readBlock(testing.allocator, i);
defer block.deinit(testing.allocator);
const entry = sealed.block_index[i];
try testing.expectEqual(@as(usize, entry.event_count), block.events.len);
for (block.events) |ev| {
try testing.expect(ev.seq > last_seq); // strictly increasing globally
last_seq = ev.seq;
try testing.expect(ev.seq >= entry.min_seq and ev.seq <= entry.max_seq);
try testing.expect(ev.witnessed_at >= entry.min_witnessed_at and ev.witnessed_at <= entry.max_witnessed_at);
try testing.expect(ev.did.len > 0);
switch (ev.kind) {
.create, .update => {
try testing.expect(ev.collection.len > 0 and ev.rkey.len > 0);
try testing.expect(ev.payload.len > 0);
// payload must be intact DAG-CBOR
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
_ = try zat.cbor.decodeAll(arena.allocator(), ev.payload);
commit_payloads += 1;
},
.delete => try testing.expectEqual(@as(usize, 0), ev.payload.len),
else => {},
}
}
total += block.events.len;
}
try testing.expectEqual(@as(usize, 7018), total);
try testing.expect(commit_payloads > 0);
}
test "fixture: block checksum corruption is rejected" {
const bytes = (try loadFixture(testing.allocator)) orelse return error.SkipZigTest;
defer testing.allocator.free(bytes);
var sealed = try segment.Sealed.parse(testing.allocator, bytes);
const first = sealed.block_index[0];
sealed.deinit(testing.allocator);
// The JSS checksum authenticates header + footer metadata. The zstd frame
// checksum independently authenticates the compressed block body, so this
// mutation deliberately leaves Sealed.parse valid and must fail only when
// the block is decompressed.
const checksum_byte = first.offset + 8 + first.compressed_size - 1;
bytes[checksum_byte] ^= 0x01;
var corrupted = try segment.Sealed.parse(testing.allocator, bytes);
defer corrupted.deinit(testing.allocator);
try testing.expectError(error.DecompressFailed, corrupted.readBlock(testing.allocator, 0));
}
test "fixture: collection index matches upstream inspect output" {
const bytes = (try loadFixture(testing.allocator)) orelse return error.SkipZigTest;
defer testing.allocator.free(bytes);
var sealed = try segment.Sealed.parse(testing.allocator, bytes);
defer sealed.deinit(testing.allocator);
var ci = try sealed.readCollectionIndex(testing.allocator);
defer ci.deinit(testing.allocator);
try testing.expectEqual(@as(usize, 7), ci.entries.len);
// [id] nsid events (from inspect; ids are table positions)
const expected = [_]struct { id: usize, nsid: []const u8, count: u32 }{
.{ .id = 0, .nsid = "app.bsky.actor.profile", .count = 327 },
.{ .id = 1, .nsid = "app.bsky.feed.like", .count = 1288 },
.{ .id = 2, .nsid = "app.bsky.feed.post", .count = 4105 },
.{ .id = 3, .nsid = "app.bsky.feed.repost", .count = 323 },
.{ .id = 4, .nsid = "app.bsky.graph.follow", .count = 724 },
.{ .id = 5, .nsid = "$identity", .count = 0 },
.{ .id = 6, .nsid = "$sync", .count = 0 },
};
for (expected) |want| {
try testing.expectEqualStrings(want.nsid, ci.entries[want.id].nsid);
try testing.expectEqual(want.count, ci.entries[want.id].count);
}
// bitmask cross-check: recount per-collection events by walking blocks
var counts: [7]u32 = @splat(0);
for (0..sealed.block_index.len) |i| {
var block = try sealed.readBlock(testing.allocator, i);
defer block.deinit(testing.allocator);
for (block.events) |ev| {
for (ci.entries, 0..) |entry, id| {
if (std.mem.eql(u8, entry.nsid, ev.collection)) {
counts[id] += 1;
// no false negatives: the block bitmask must claim this id
try testing.expect(ci.blockHasCollection(i, id));
}
}
}
}
for (expected) |want| {
if (want.nsid[0] != '$') try testing.expectEqual(want.count, counts[want.id]);
}
}