//! A pull or a served push stores a file version that arrived whole as a delta
//! of the same path's version in the check-in it descends from, found by the
//! lineage walk pack uses (store/lineage.zig).
//!
//! Every fixture separates the versions of a file by unrelated blobs, in
//! arrival order, so the recent-frame window `putArtifact` falls back to never
//! holds the predecessor: a delta here is the lineage hint's doing.
const std = @import("std");
const artifact = @import("../artifact/artifact.zig");
const logstore = @import("../store/logstore.zig");
const repostate = @import("../repo/repostate.zig");
const storage = @import("../store/storage.zig");
const receive = @import("receive.zig");
const gpa = std.testing.allocator;
const Fixture = struct {
tmp: std.testing.TmpDir,
file: std.Io.File,
fs: storage.FileStorage,
log: logstore.LogStore,
ms: repostate.MemStore,
/// Store rids at or below this are durable; a persist writes the rest.
persisted: repostate.Rid = 0,
owned: std.ArrayListUnmanaged([]u8) = .empty,
fn init(self: *Fixture) !void {
const io = std.testing.io;
self.tmp = std.testing.tmpDir(.{});
self.file = try self.tmp.dir.createFile(io, "log", .{ .read = true });
self.fs = storage.FileStorage.init(io, self.file, 0);
self.log = try logstore.LogStore.open(gpa, self.fs.storageFile(), 0);
self.ms = repostate.MemStore.init(gpa);
self.persisted = 0;
self.owned = .empty;
}
fn deinit(self: *Fixture) void {
for (self.owned.items) |b| gpa.free(b);
self.owned.deinit(gpa);
self.ms.deinit();
self.log.deinit(gpa);
self.file.close(std.testing.io);
self.tmp.cleanup();
}
/// Receive `bytes` whole (no wire form), returning its name.
fn receiveWhole(self: *Fixture, bytes: []const u8) ![64]u8 {
var name: [64]u8 = undefined;
_ = artifact.artifactName(.sha3_256, bytes, &name);
_ = try self.ms.store().put(&name, bytes);
return name;
}
/// Keep `bytes` alive for the fixture's lifetime.
fn keep(self: *Fixture, bytes: []u8) ![]u8 {
try self.owned.append(gpa, bytes);
return bytes;
}
/// Persist everything received since the last persist, as a pull does.
fn persist(self: *Fixture) !void {
_ = try receive.persistStoreOpen(gpa, &self.log, self.ms.store(), null, self.persisted, null, null);
try self.log.commit();
self.persisted = @intCast(self.ms.store().count());
}
fn ridOf(self: *Fixture, bytes: []const u8) logstore.Rid {
var digest: [32]u8 = undefined;
std.crypto.hash.sha3.Sha3_256.hash(bytes, &digest, .{});
return self.log.lookup(&digest).?;
}
fn expectContent(self: *Fixture, bytes: []const u8) !void {
const got = (try self.log.content(gpa, self.ridOf(bytes))).?;
defer gpa.free(got);
try std.testing.expectEqualSlices(u8, bytes, got);
}
fn expectDeltaOf(self: *Fixture, child: []const u8, base: []const u8) !void {
try self.expectContent(child);
const rid = self.ridOf(child);
try std.testing.expectEqual(logstore.FrameKind.delta, self.log.kindOf(rid));
var buf: [logstore.max_hash_len]u8 = undefined;
const got = self.log.deltaBaseHashOf(rid, &buf).?;
var want: [32]u8 = undefined;
std.crypto.hash.sha3.Sha3_256.hash(base, &want, .{});
try std.testing.expectEqualSlices(u8, &want, got);
}
fn expectWhole(self: *Fixture, bytes: []const u8) !void {
try self.expectContent(bytes);
try std.testing.expect(self.log.kindOf(self.ridOf(bytes)) != .delta);
}
/// Unrelated blobs the window fills up with.
fn receiveFillers(self: *Fixture, seed: u64) !void {
var prng = std.Random.DefaultPrng.init(seed);
for (0..logstore.max_base_candidates + 2) |_| {
const b = try self.keep(try gpa.alloc(u8, 8 * 1024));
prng.random().bytes(b);
_ = try self.receiveWhole(b);
}
}
};
/// Version `v` of a 16 KB text file: one line differs per version.
fn fileVersion(v: usize) ![]u8 {
var out: std.ArrayListUnmanaged(u8) = .empty;
errdefer out.deinit(gpa);
for (0..400) |i| {
var line: [64]u8 = undefined;
const l = if (i == 7 * v % 400)
try std.fmt.bufPrint(&line, "line {d:0>4} edited in version {d}\n", .{ i, v })
else
try std.fmt.bufPrint(&line, "line {d:0>4} of the shared text, unchanged\n", .{i});
try out.appendSlice(gpa, l);
}
return out.toOwnedSlice(gpa);
}
fn fcard(filename: []const u8, hash: []const u8) artifact.FCard {
return .{ .filename = filename, .hash = hash, .perm = null, .old_name = null, .mode = .regular };
}
/// A check-in manifest over flat `f` cards or tree `s` cards, with an optional
/// primary parent.
fn checkinBytes(comment: []const u8, timestamp: []const u8, f: []const artifact.FCard, s: []const artifact.SCard, parent: ?[]const u8) ![]u8 {
var parents: [1]artifact.PCard = undefined;
var p: []const artifact.PCard = &.{};
if (parent) |name| {
parents[0] = .{ .parents = name, .count = 1 };
p = parents[0..1];
}
var m: artifact.Manifest = .{
.arena_state = std.heap.ArenaAllocator.init(gpa),
.kind = .manifest,
.a = null,
.b = null,
.c = .{ .comment = comment },
.d = .{ .timestamp = timestamp },
.e = null,
.g = null,
.h = null,
.i = null,
.k = null,
.l = null,
.n = null,
.r = null,
.u = .{ .login = "tester" },
.z = null,
.w = null,
.f = f,
.j = &.{},
.m = &.{},
.p = p,
.q = &.{},
.s = s,
.t = &.{},
};
defer m.deinit();
return artifact.create(&m, gpa);
}
test "receive: versions of one file received whole are stored as deltas of their predecessors" {
var fx: Fixture = undefined;
try fx.init();
defer fx.deinit();
var versions: [4][]u8 = undefined;
var names: [4][64]u8 = undefined;
for (0..4) |v| {
versions[v] = try fx.keep(try fileVersion(v));
_ = artifact.artifactName(.sha3_256, versions[v], &names[v]);
}
// The check-ins, oldest first, each naming its parent.
var checkins: [4][]u8 = undefined;
var cnames: [4][64]u8 = undefined;
for (0..4) |v| {
const files = [_]artifact.FCard{fcard("a.txt", &names[v])};
var ts: [19]u8 = undefined;
_ = try std.fmt.bufPrint(&ts, "2026-08-01T00:00:0{d}", .{v});
checkins[v] = try fx.keep(try checkinBytes("edit", &ts, &files, &.{}, if (v == 0) null else &cnames[v - 1]));
_ = artifact.artifactName(.sha3_256, checkins[v], &cnames[v]);
}
// Arrival order a peer may use: newest first, unrelated blobs between.
var v: usize = 4;
while (v > 0) {
v -= 1;
_ = try fx.receiveWhole(checkins[v]);
}
v = 4;
while (v > 0) {
v -= 1;
_ = try fx.receiveWhole(versions[v]);
try fx.receiveFillers(v);
}
try fx.persist();
try fx.expectWhole(versions[0]);
for (1..4) |i| try fx.expectDeltaOf(versions[i], versions[i - 1]);
}
test "receive: the predecessor may already be held, as the parent check-in's file" {
var fx: Fixture = undefined;
try fx.init();
defer fx.deinit();
const v0 = try fx.keep(try fileVersion(0));
const n0 = try fx.receiveWhole(v0);
const c0 = try fx.keep(try checkinBytes("root", "2026-08-01T00:00:00", &.{fcard("a.txt", &n0)}, &.{}, null));
const c0n = try fx.receiveWhole(c0);
try fx.persist();
try fx.receiveFillers(1);
const v1 = try fx.keep(try fileVersion(1));
const n1 = try fx.receiveWhole(v1);
try fx.receiveFillers(2);
_ = try fx.receiveWhole(try fx.keep(try checkinBytes("edit", "2026-08-01T00:00:01", &.{fcard("a.txt", &n1)}, &.{}, &c0n)));
try fx.persist();
try fx.expectDeltaOf(v1, v0);
}
test "receive: a file whose predecessor the store does not hold is stored whole" {
var fx: Fixture = undefined;
try fx.init();
defer fx.deinit();
// The parent check-in is held; the file version it names never arrived.
const v0 = try fx.keep(try fileVersion(0));
var n0: [64]u8 = undefined;
_ = artifact.artifactName(.sha3_256, v0, &n0);
const c0 = try fx.keep(try checkinBytes("root", "2026-08-01T00:00:00", &.{fcard("a.txt", &n0)}, &.{}, null));
const c0n = try fx.receiveWhole(c0);
try fx.persist();
const v1 = try fx.keep(try fileVersion(1));
const n1 = try fx.receiveWhole(v1);
try fx.receiveFillers(1);
_ = try fx.receiveWhole(try fx.keep(try checkinBytes("edit", "2026-08-01T00:00:01", &.{fcard("a.txt", &n1)}, &.{}, &c0n)));
try fx.persist();
try fx.expectWhole(v1);
}
test "receive: a renamed file is not paired across the rename, as pack does not pair it" {
var fx: Fixture = undefined;
try fx.init();
defer fx.deinit();
const v0 = try fx.keep(try fileVersion(0));
const n0 = try fx.receiveWhole(v0);
const c0 = try fx.keep(try checkinBytes("root", "2026-08-01T00:00:00", &.{fcard("a.txt", &n0)}, &.{}, null));
const c0n = try fx.receiveWhole(c0);
try fx.persist();
try fx.receiveFillers(1);
const v1 = try fx.keep(try fileVersion(1));
const n1 = try fx.receiveWhole(v1);
try fx.receiveFillers(2);
var renamed = fcard("b.txt", &n1);
renamed.perm = "w";
renamed.old_name = "a.txt";
_ = try fx.receiveWhole(try fx.keep(try checkinBytes("rename", "2026-08-01T00:00:01", &.{renamed}, &.{}, &c0n)));
try fx.persist();
try fx.expectWhole(v1);
}
/// 64 files under `d<i%4>/`, the first at a given version, the rest fixed.
const TreeFiles = struct {
names: [64][16]u8 = undefined,
bodies: [64][32]u8 = undefined,
body_lens: [64]usize = undefined,
hashes: [64][64]u8 = undefined,
cards: [64]artifact.FCard = undefined,
fn fill(self: *TreeFiles, v0_name: []const u8) []artifact.FCard {
for (0..64) |i| {
const n = std.fmt.bufPrint(&self.names[i], "d{d}/f{d:0>4}.c", .{ i % 4, i }) catch unreachable;
if (i == 0) {
self.cards[i] = fcard(n, v0_name);
} else {
const b = std.fmt.bufPrint(&self.bodies[i], "fixed body {d:0>16}\n", .{i}) catch unreachable;
self.body_lens[i] = b.len;
_ = artifact.artifactName(.sha3_256, b, &self.hashes[i]);
self.cards[i] = fcard(n, &self.hashes[i]);
}
}
// F cards sort by name; "d0/f0000.c" leads already.
std.mem.sort(artifact.FCard, &self.cards, {}, struct {
fn lt(_: void, a: artifact.FCard, b: artifact.FCard) bool {
return std.mem.lessThan(u8, a.filename, b.filename);
}
}.lt);
return &self.cards;
}
fn receiveBodies(self: *TreeFiles, fx: *Fixture) !void {
for (1..64) |i| {
_ = try fx.receiveWhole(self.bodies[i][0..self.body_lens[i]]);
}
}
};
/// Receive check-in `files` as a tree (k=1) or flat, returning its manifest.
fn receiveCheckin(fx: *Fixture, comment: []const u8, ts: []const u8, files: []const artifact.FCard, tree: bool, parent: ?[]const u8) ![64]u8 {
if (!tree) return fx.receiveWhole(try fx.keep(try checkinBytes(comment, ts, files, &.{}, parent)));
var t = try artifact.buildTree(gpa, files, 1, .sha3_256);
defer t.deinit();
try std.testing.expect(!t.isFlat());
for (t.subs) |sub| _ = try fx.receiveWhole(try fx.keep(try gpa.dupe(u8, sub.bytes)));
return fx.receiveWhole(try fx.keep(try checkinBytes(comment, ts, &.{}, t.s, parent)));
}
fn treeSwitch(parent_tree: bool, child_tree: bool) !void {
var fx: Fixture = undefined;
try fx.init();
defer fx.deinit();
const v0 = try fx.keep(try fileVersion(0));
const n0 = try fx.receiveWhole(v0);
var tf0: TreeFiles = .{};
const files0 = tf0.fill(&n0);
try tf0.receiveBodies(&fx);
const c0 = try receiveCheckin(&fx, "root", "2026-08-01T00:00:00", files0, parent_tree, null);
try fx.persist();
try fx.receiveFillers(1);
const v1 = try fx.keep(try fileVersion(1));
const n1 = try fx.receiveWhole(v1);
try fx.receiveFillers(2);
var tf1: TreeFiles = .{};
_ = try receiveCheckin(&fx, "edit", "2026-08-01T00:00:01", tf1.fill(&n1), child_tree, &c0);
try fx.persist();
try fx.expectDeltaOf(v1, v0);
}
test "receive: a tree check-in whose parent is flat pairs its files by path" {
try treeSwitch(false, true);
}
test "receive: a flat check-in whose parent is a tree pairs its files by path" {
try treeSwitch(true, false);
}
test "receive: a tree check-in whose parent is a tree pairs its files by path" {
try treeSwitch(true, true);
}
test "receive: a predecessor at the delta-chain write bound is not used as a base" {
var fx: Fixture = undefined;
try fx.init();
defer fx.deinit();
// A held chain v0 <- v1 <- ... deep enough that its last link sits at the
// write bound, each link also known to the holdings store as a pull's is.
const deep = logstore.delta_chain_write_max;
var prev: ?logstore.Rid = null;
var last: []u8 = undefined;
var last_name: [64]u8 = undefined;
for (0..deep + 1) |v| {
const bytes = try fx.keep(try fileVersion(v));
var digest: [32]u8 = undefined;
std.crypto.hash.sha3.Sha3_256.hash(bytes, &digest, .{});
prev = try fx.log.putArtifact(&digest, bytes, prev);
last_name = try fx.receiveWhole(bytes);
last = bytes;
}
const c0 = try fx.keep(try checkinBytes("root", "2026-08-01T00:00:00", &.{fcard("a.txt", &last_name)}, &.{}, null));
const c0n = try fx.receiveWhole(c0);
try fx.persist();
try std.testing.expectEqual(deep, fx.log.depthOf(fx.ridOf(last)));
try fx.receiveFillers(1);
const next = try fx.keep(try fileVersion(deep + 1));
const nn = try fx.receiveWhole(next);
try fx.receiveFillers(2);
_ = try fx.receiveWhole(try fx.keep(try checkinBytes("edit", "2026-08-01T00:00:01", &.{fcard("a.txt", &nn)}, &.{}, &c0n)));
try fx.persist();
try fx.expectWhole(next);
}
test "receive: a file and its predecessor sent as a reverse delta of it both land" {
// Fossil sends the OLDER version as a delta of the newer one. The newer
// one, received whole, must not wait on a predecessor that waits on it.
var fx: Fixture = undefined;
try fx.init();
defer fx.deinit();
const v0 = try fx.keep(try fileVersion(0));
const v1 = try fx.keep(try fileVersion(1));
var n0: [64]u8 = undefined;
_ = artifact.artifactName(.sha3_256, v0, &n0);
var n1: [64]u8 = undefined;
_ = artifact.artifactName(.sha3_256, v1, &n1);
const c0 = try fx.keep(try checkinBytes("root", "2026-08-01T00:00:00", &.{fcard("a.txt", &n0)}, &.{}, null));
const c0n = try fx.receiveWhole(c0);
_ = try fx.receiveWhole(try fx.keep(try checkinBytes("edit", "2026-08-01T00:00:01", &.{fcard("a.txt", &n1)}, &.{}, &c0n)));
// v0 first in arrival order, as a delta of v1; v1 whole after fillers.
const d = try @import("../artifact/delta.zig").create(gpa, v1, v0);
defer gpa.free(d);
const container = try fx.keep(try @import("../store/container.zig").encode(gpa, d, .default));
const r0 = try fx.ms.store().put(&n0, v0);
try fx.ms.store().retain(r0, .{ .delta = .{ .src = &n1, .container = container, .target_size = v0.len } });
try fx.receiveFillers(1);
_ = try fx.receiveWhole(v1);
try fx.persist();
try fx.expectContent(v0);
try fx.expectContent(v1);
}