//! Helpers, fixtures and constants shared by the end-to-end test files
//! under src/e2e/; each is used by more than one of them.
const std = @import("std");
const repeat = @import("zit").repeat;
const build_options = @import("build_options");
const test_child = @import("test_child");
// Private storage modules share the build graph's single `zit` module instance.
const logstore = @import("zit").logstore;
const storage = @import("zit").storage;
/// draft-fossil-submanifests-00 §6.3/§8.1 — the settings semantics the CLI
/// tests below assert against the stored config frame.
const submanifest = @import("zit").submanifest;
const checkout = @import("zit").checkout;
// The managed-path set is read back through the library to verify `add`'s
// on-disk effect (contracts/add.md); re-exported by the `zit` module.
const managed = @import("zit").managed;
/// The fully populated `.fossil-settings/` fixture this lane
/// and the fossil-interop smoke lane (S14) both build.
const settings_fixture = @import("zit").settings_fixture;
// The artifact reader used to decode a minted check-in back through the
// library — the `commit` e2e's state oracle is its own decoded manifest
// fields, never raw bytes compared to a hand-written expectation.
const artifact = @import("zit").artifact;
// The direct-commit core, driven in-process where argv cannot carry the input.
const zit_checkin = @import("zit").checkin;
// The §4.1 container codec and the sync vocabulary: the §9.3 configuration
// tests below hand-build a request card no zit client emits, so they frame it
// with the same codec the transport uses and name the settings with the same
// constants the wire does.
const container = @import("zit").container;
const sync = @import("zit").sync;
// The dispatched-verb enum, imported straight from the source it lives in (as
// help.zig does) so the help-audit guard below drives off the SAME list the
// dispatcher switches on — a verb added there without a help line fails the
// test, never drifts.
const zit = @import("zit");
const cli = @import("zit").cli;
const File = std.Io.File;
const net = std.Io.net;
/// Absolute path to the freshly built `zit` executable (a build-time
/// `LazyPath`, so the exe is always rebuilt before these tests run).
pub const zit_exe: []const u8 = build_options.zit_exe;
pub fn freeResult(gpa: std.mem.Allocator, res: *std.process.RunResult) void {
gpa.free(res.stdout);
gpa.free(res.stderr);
}
pub fn expectNonzeroExit(term: std.process.Child.Term) !void {
switch (term) {
.exited => |c| try std.testing.expect(c != 0),
else => {
std.debug.print("expected nonzero exit, got {any}\n", .{term});
return error.TestUnexpectedTerm;
},
}
}
pub fn expectExit(term: std.process.Child.Term, code: u8) !void {
switch (term) {
.exited => |c| try std.testing.expectEqual(code, c),
else => {
std.debug.print("expected exit {d}, got {any}\n", .{ code, term });
return error.TestUnexpectedTerm;
},
}
}
/// Like `expectExit`, but on mismatch prints the child's captured stdout and
/// stderr before failing. Reserved for the spawned-CLIENT assertions in the
/// server-interop scenarios (clone/pull/push against a live `zit serve`): there
/// the child's stderr is the ONLY evidence of *why* it exited non-zero — a push
/// that reached the wrong server, a sync that never converged — and plain
/// `expectExit` discards it, leaving a failure that is countable but not
/// diagnosable. Elsewhere `expectExit` stays: those call sites
/// assert the child's output separately, so the code alone is enough.
pub fn expectExitDiag(term: std.process.Child.Term, code: u8, res: std.process.RunResult) !void {
switch (term) {
.exited => |c| {
if (c == code) return;
std.debug.print(
"expected exit {d}, got {d}\n--- child stdout ---\n{s}\n--- child stderr ---\n{s}\n",
.{ code, c, res.stdout, res.stderr },
);
return error.TestUnexpectedExitCode;
},
else => {
std.debug.print(
"expected exit {d}, got {any}\n--- child stdout ---\n{s}\n--- child stderr ---\n{s}\n",
.{ code, term, res.stdout, res.stderr },
);
return error.TestUnexpectedTerm;
},
}
}
/// `update` narrates its receipt on stderr; a diagnostic is a line
/// that starts with `zit ` (every refusal and error does). This asserts none.
pub fn expectNoDiagnostic(stderr: []const u8) !void {
var it = std.mem.splitScalar(u8, stderr, '\n');
while (it.next()) |line| {
if (std.mem.startsWith(u8, line, "zit ")) {
std.debug.print("unexpected diagnostic: {s}\n", .{line});
return error.TestUnexpectedResult;
}
}
}
pub fn fileExists(io: std.Io, dir: std.Io.Dir, name: []const u8) bool {
var f = dir.openFile(io, name, .{}) catch return false;
f.close(io);
return true;
}
// --- add helpers ----------------------------------------------------------
/// Spawns `zit <tail...>` with a pinned environment and cwd bound to `tmp_dir`.
/// The generic sibling of `spawnInit` — any verb. Caller owns `result.stdout` /
/// `result.stderr`.
/// `runZit` with extra environment pairs. The global settings tier is located
/// through the environment, so a test that exercises it must be able to point
/// `$ZIT_CONFIG` at its own tmp directory -- the operator's real settings file
/// must never be read or written by the suite.
pub fn runZitEnv(
gpa: std.mem.Allocator,
io: std.Io,
tmp_dir: std.Io.Dir,
extra: []const [2][]const u8,
tail: []const []const u8,
) !std.process.RunResult {
var env = std.process.Environ.Map.init(gpa);
defer env.deinit();
try env.put("TZ", "UTC");
try env.put("HOME", "/zit-e2e-isolated-home-does-not-exist");
try env.put("PATH", "/usr/bin:/bin");
for (extra) |pair| try env.put(pair[0], pair[1]);
const cwd_abs = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(cwd_abs);
const abs_exe = try std.fs.path.resolve(gpa, &.{ cwd_abs, zit_exe });
defer gpa.free(abs_exe);
var argv_buf: [16][]const u8 = undefined;
argv_buf[0] = abs_exe;
std.debug.assert(tail.len <= argv_buf.len - 1);
for (tail, argv_buf[1..][0..tail.len]) |src, *dst| dst.* = src;
return std.process.run(gpa, io, .{
.argv = argv_buf[0 .. 1 + tail.len],
.cwd = .{ .dir = tmp_dir },
.environ_map = &env,
});
}
pub fn runZit(
gpa: std.mem.Allocator,
io: std.Io,
tmp_dir: std.Io.Dir,
tail: []const []const u8,
) !std.process.RunResult {
var env = std.process.Environ.Map.init(gpa);
defer env.deinit();
try env.put("TZ", "UTC");
try env.put("HOME", "/zit-e2e-isolated-home-does-not-exist");
try env.put("PATH", "/usr/bin:/bin");
const cwd_abs = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(cwd_abs);
const abs_exe = try std.fs.path.resolve(gpa, &.{ cwd_abs, zit_exe });
defer gpa.free(abs_exe);
var argv_buf: [32][]const u8 = undefined;
argv_buf[0] = abs_exe;
std.debug.assert(tail.len <= argv_buf.len - 1);
for (tail, argv_buf[1..][0..tail.len]) |src, *dst| dst.* = src;
const argv = argv_buf[0 .. 1 + tail.len];
return std.process.run(gpa, io, .{
.argv = argv,
.cwd = .{ .dir = tmp_dir },
.environ_map = &env,
});
}
/// Materializes a fresh repository in `dir` by running the real `zit init`
/// (exit 0 asserted) — the same path a user takes before `add`.
pub fn initRepo(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !void {
var res = try runZit(gpa, io, dir, &.{"init"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
/// Creates an empty regular file `name` under `dir`.
pub fn touch(io: std.Io, dir: std.Io.Dir, name: []const u8) !void {
var f = try dir.createFile(io, name, .{});
f.close(io);
}
/// Whether the `.zit-checkout` container carries the tagged section (legacy
/// sibling files answer for a pre-container checkout). The presence oracle for
/// the absorbed records — the sibling FILES no longer exist on a live checkout.
pub fn sectionExists(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, tag: checkout.SectionTag) bool {
const raw = checkout.readSectionAlloc(gpa, io, dir, tag) catch return false;
if (raw) |bytes| {
gpa.free(bytes);
return true;
}
return false;
}
/// The managed section's raw byte image ("" when absent) — the byte-identity
/// oracle the pre-container e2es got from reading `.zit-managed` whole.
pub fn readManagedImage(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) ![]u8 {
return (try checkout.readSectionAlloc(gpa, io, dir, .managed)) orelse try gpa.dupe(u8, "");
}
/// Plants an unparseable payload in the tagged section — the container-era
/// equivalent of writing garbage over the old sibling file. The container
/// itself stays valid; the section's own parser rejects the bytes.
pub fn corruptSection(io: std.Io, dir: std.Io.Dir, tag: checkout.SectionTag) !void {
try checkout.updateSections(io, dir, &.{.{ .tag = tag, .payload = "not a valid record" }});
}
/// Reads `.zit-managed` back through the library and asserts its membership is
/// exactly `expected` (order-independent — the set is sorted internally). This
/// readback is the on-disk oracle for every `add` e2e.
pub fn expectManaged(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
expected: []const []const u8,
) !void {
var set = try managed.ManagedSet.read(gpa, io, dir);
defer set.deinit();
try std.testing.expectEqual(expected.len, set.iterate().len);
for (expected) |key| try std.testing.expect(set.contains(key));
}
/// Marks `paths` managed by running the real `zit add` (exit 0 asserted) — the
/// same path a user takes to set up state before exercising `rm`.
pub fn addManaged(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
paths: []const []const u8,
) !void {
var argv_buf: [16][]const u8 = undefined;
argv_buf[0] = "add";
std.debug.assert(paths.len <= argv_buf.len - 1);
for (paths, argv_buf[1..][0..paths.len]) |src, *dst| dst.* = src;
var res = try runZit(gpa, io, dir, argv_buf[0 .. 1 + paths.len]);
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
/// Runs `/bin/sh -c <script>` under the same pinned, isolated environment as
/// `runZit`, with cwd bound to `tmp_dir` and the absolute `zit` path exposed as
/// `$ZIT`. Driving the binary through a real shell is the only faithful way to
/// exercise the output-composition guarantees (B-rm-14): `>> logfile` (O_APPEND)
/// and grouped/shared-fd redirections (`{ …; } > log`) inherit an OS file offset
/// that a process's stdout must honor. `std.process.run`'s captured pipe is not
/// seekable and so cannot reproduce these; the shell can. Caller owns the
/// returned `stdout`/`stderr`.
pub fn runShell(
gpa: std.mem.Allocator,
io: std.Io,
tmp_dir: std.Io.Dir,
script: []const u8,
) !std.process.RunResult {
var env = std.process.Environ.Map.init(gpa);
defer env.deinit();
try env.put("TZ", "UTC");
try env.put("HOME", "/zit-e2e-isolated-home-does-not-exist");
try env.put("PATH", "/usr/bin:/bin");
const cwd_abs = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(cwd_abs);
const abs_exe = try std.fs.path.resolve(gpa, &.{ cwd_abs, zit_exe });
defer gpa.free(abs_exe);
try env.put("ZIT", abs_exe);
return std.process.run(gpa, io, .{
.argv = &.{ "/bin/sh", "-c", script },
.cwd = .{ .dir = tmp_dir },
.environ_map = &env,
});
}
/// Reads `name` under `dir` fully and returns its bytes (caller frees). Used by
/// the composition tests to read back a redirected log file.
pub fn readFile(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, name: []const u8) ![]u8 {
return dir.readFileAlloc(io, name, gpa, .limited(1 << 16));
}
/// Names a filesystem holds but a new check-in must not: a control byte, a
/// character above U+FFFF, a private-use character. Each is valid UTF-8, so
/// every filesystem the suite runs on can create it.
pub const illegal_new_names = [_][]const u8{ "c\x01d", "n\nl", "smile\xf0\x9f\x98\x80", "priv\xee\x80\x80" };
/// Commits the current working tree (message `msg`, pinned author/epoch), asserts
/// exit 0, and returns the new check-in's 64-hex hash copied onto `gpa`.
pub fn commitHashDup(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, msg: []const u8) ![]const u8 {
var res = try runCommitEnv(gpa, io, dir, .{ .user = "u" }, &.{ "commit", "-m", msg });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const h = try checkinHash(res.stdout);
return gpa.dupe(u8, h);
}
/// `commitHashDup` at SOURCE_DATE_EPOCH `epoch`.
pub fn commitAtDup(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, msg: []const u8, epoch: []const u8) ![]const u8 {
var res = try runCommitEnv(gpa, io, dir, .{ .user = "u", .epoch = epoch }, &.{ "commit", "-m", msg });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const h = try checkinHash(res.stdout);
return gpa.dupe(u8, h);
}
/// `commitHashDup` with `--allow-fork`: for fixtures that DELIBERATELY build
/// a second head (merge diamonds, leaves/info fork scenes). The plain helper
/// stays guard-checked so ordinary fixture commits keep proving the fork-guard
/// refusal never fires on linear history.
pub fn commitForkDup(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, msg: []const u8) ![]const u8 {
var res = try runCommitEnv(gpa, io, dir, .{ .user = "u" }, &.{ "commit", "-m", msg, "--allow-fork" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
const h = try checkinHash(res.stdout);
return gpa.dupe(u8, h);
}
pub const MergeDiamond = struct {
base: []const u8,
other: []const u8,
current: []const u8,
pub fn deinit(self: *MergeDiamond, gpa: std.mem.Allocator) void {
gpa.free(self.base);
gpa.free(self.other);
gpa.free(self.current);
}
};
/// Builds a diamond over one file `poem`: root(base_content) → other(other_content)
/// on trunk, then `update base` and commit current(current_content). The two
/// leaves share `base` as their §3.3 merge base. On return the checkout sits at
/// `current` on a clean trunk with `poem` == current_content — ready to merge.
pub fn buildMergeDiamond(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
base_content: []const u8,
other_content: []const u8,
current_content: []const u8,
) !MergeDiamond {
try initRepo(gpa, io, dir);
try writeFile(io, dir, "poem", base_content);
try addManaged(gpa, io, dir, &.{"poem"});
const base = try commitHashDup(gpa, io, dir, "root");
errdefer gpa.free(base);
try writeFile(io, dir, "poem", other_content);
const other = try commitHashDup(gpa, io, dir, "other");
errdefer gpa.free(other);
var up = try runZit(gpa, io, dir, &.{ "update", base });
defer freeResult(gpa, &up);
try expectExit(up.term, 0);
try writeFile(io, dir, "poem", current_content);
const current = try commitForkDup(gpa, io, dir, "current");
return .{ .base = base, .other = other, .current = current };
}
/// Reads `.zit-checkout` and asserts its version and merge-parent fields.
pub fn expectMergeState(io: std.Io, dir: std.Io.Dir, version: []const u8, merge_parent: []const u8) !void {
var ckbuf: [checkout.max_ck_payload + 1]u8 = undefined;
const ck = (try checkout.read(io, dir, &ckbuf)).?;
try std.testing.expectEqualStrings(version, ck.version);
try std.testing.expectEqualStrings(merge_parent, ck.merge_parent);
}
/// Whether `name` has any execute bit set.
pub fn isExecutableFile(io: std.Io, dir: std.Io.Dir, name: []const u8) !bool {
const st = try dir.statFile(io, name, .{});
return st.permissions.toMode() & 0o111 != 0;
}
/// Four files and three forks of one root, plus the checkout's own fork: `x`
/// edits `b`, `y` edits `c`, `z` edits `d`, `current` edits `a`. Every fork
/// touches a different file, so any stack of merges is clean. On return the
/// checkout sits at `current`.
pub const MergeFan = struct {
root: []const u8,
x: []const u8,
y: []const u8,
z: []const u8,
current: []const u8,
pub fn deinit(self: *MergeFan, gpa: std.mem.Allocator) void {
for ([_][]const u8{ self.root, self.x, self.y, self.z, self.current }) |h| gpa.free(h);
}
};
pub fn buildMergeFan(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !MergeFan {
try initRepo(gpa, io, dir);
for ([_][]const u8{ "a", "b", "c", "d" }) |f| try writeFile(io, dir, f, "base\n");
try addManaged(gpa, io, dir, &.{ "a", "b", "c", "d" });
var fan: MergeFan = undefined;
fan.root = try commitHashDup(gpa, io, dir, "root");
const forks = [_]struct { file: []const u8, slot: *[]const u8 }{
.{ .file = "b", .slot = &fan.x },
.{ .file = "c", .slot = &fan.y },
.{ .file = "d", .slot = &fan.z },
.{ .file = "a", .slot = &fan.current },
};
for (forks) |f| {
var up = try runZit(gpa, io, dir, &.{ "update", fan.root });
defer freeResult(gpa, &up);
try expectExit(up.term, 0);
try writeFile(io, dir, f.file, f.file);
f.slot.* = try commitForkDup(gpa, io, dir, f.file);
}
return fan;
}
/// The `MERGED_WITH` lines `zit status` prints, in order, joined.
pub fn statusMergeLines(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) ![]u8 {
var st = try runZit(gpa, io, dir, &.{"status"});
defer freeResult(gpa, &st);
try expectExit(st.term, 0);
var out: std.ArrayList(u8) = .empty;
errdefer out.deinit(gpa);
var lines = std.mem.splitScalar(u8, st.stdout, '\n');
while (lines.next()) |line| {
if (std.mem.startsWith(u8, line, "MERGED_WITH ") or std.mem.startsWith(u8, line, "pending ")) {
try out.appendSlice(gpa, line);
try out.append(gpa, '\n');
}
}
return out.toOwnedSlice(gpa);
}
/// The P and Q card lines of check-in `hash`, in manifest order.
pub fn parentCards(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, hash: []const u8) ![]u8 {
var stored = try parseCheckin(gpa, io, dir, hash);
defer stored.deinit();
var out: std.ArrayList(u8) = .empty;
errdefer out.deinit(gpa);
var lines = std.mem.splitScalar(u8, stored.bytes, '\n');
while (lines.next()) |line| {
if (std.mem.startsWith(u8, line, "P ") or std.mem.startsWith(u8, line, "Q ")) {
try out.appendSlice(gpa, line);
try out.append(gpa, '\n');
}
}
return out.toOwnedSlice(gpa);
}
// ==========================================================================
// `zit status` end-to-end tests (contracts/status.md B-status-1..17). Each
// spawns the REAL binary in an isolated, pinned environment, asserts exit code
// + exact stdout/stderr (structural around the born hash, never a wall clock),
// and — status being read-only — reads `.zit-checkout`/`.zit-managed` back
// through the library to prove no mutation occurred. Teardown is unconditional.
// ==========================================================================
/// Overwrites `.zit-checkout` with a chosen state via the library's own writer,
/// so the born-state header (B-status-4) is reachable with no `commit` verb (the
/// checkout record is independent of the log). Used only by the status e2e.
pub fn seedCheckout(io: std.Io, dir: std.Io.Dir, version: []const u8, branch: []const u8) !void {
try checkout.write(io, dir, .{ .version = version, .branch = branch, .merge_parent = "", .special_op = "", .manifest_flags = "" });
}
/// Asserts, through the library, that `dir`'s checkout record still holds
/// exactly `version`/`branch` — the read-only oracle proving `status` did not
/// mutate `.zit-checkout`.
pub fn expectCheckout(io: std.Io, dir: std.Io.Dir, version: []const u8, branch: []const u8) !void {
var ckbuf: [checkout.max_ck_payload + 1]u8 = undefined;
const ck = (try checkout.read(io, dir, &ckbuf)).?;
try std.testing.expectEqualStrings(version, ck.version);
try std.testing.expectEqualStrings(branch, ck.branch);
}
/// Sets `name`'s permission bits through its own handle — `Dir.setPermissions`
/// applies to the directory itself, so a per-file chmod needs the open file.
pub fn chmodFile(io: std.Io, dir: std.Io.Dir, name: []const u8, mode: std.posix.mode_t) !void {
var f = try dir.openFile(io, name, .{});
defer f.close(io);
try f.setPermissions(io, File.Permissions.fromMode(mode));
}
pub fn expectFileContent(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, path: []const u8, want: []const u8) !void {
const got = try dir.readFileAlloc(io, path, gpa, .limited(1 << 20));
defer gpa.free(got);
try std.testing.expectEqualStrings(want, got);
}
pub fn appendDiffManifest(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
label: []const u8,
baseline: ?[]const u8,
files: []const artifact.FCard,
) ![]u8 {
var manifest: artifact.Manifest = .{
.arena_state = std.heap.ArenaAllocator.init(gpa),
.kind = .manifest,
.a = null,
.b = if (baseline) |hash| .{ .hash = hash } else null,
.c = .{ .comment = label },
.d = .{ .timestamp = "2020-01-01T00:00:00" },
.e = null,
.g = null,
.h = null,
.i = null,
.k = null,
.l = null,
.n = null,
.r = null,
.u = .{ .login = "u" },
.z = null,
.w = null,
.f = files,
.j = &.{},
.m = &.{},
.p = &.{},
.q = &.{},
.t = &.{},
};
defer manifest.deinit();
const bytes = try artifact.create(&manifest, gpa);
defer gpa.free(bytes);
return appendArtifact(gpa, io, dir, bytes);
}
/// Writes a legal non-leaf control artifact that mutates the named target's
/// tag state (a single T card). Synthetic control artifacts are necessary
/// because this public behavior cannot be produced by an existing CLI command.
pub fn appendTagControl(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
timestamp: []const u8,
sign: artifact.TSign,
name: []const u8,
target: []const u8,
value: ?[]const u8,
) ![]u8 {
const cards = [_]artifact.TCard{.{
.sign = sign,
.name = name,
.target = target,
.value = value,
}};
var control: artifact.Manifest = .{
.arena_state = std.heap.ArenaAllocator.init(gpa),
.kind = .control,
.a = null,
.b = null,
.c = null,
.d = .{ .timestamp = timestamp },
.e = null,
.g = null,
.h = null,
.i = null,
.k = null,
.l = null,
.n = null,
.r = null,
.u = .{ .login = "u" },
.z = null,
.w = null,
.f = &.{},
.j = &.{},
.m = &.{},
.p = &.{},
.q = &.{},
.t = &cards,
};
defer control.deinit();
const bytes = try artifact.create(&control, gpa);
defer gpa.free(bytes);
return appendArtifact(gpa, io, dir, bytes);
}
/// Writes a control artifact that applies an active `closed` tag to `target`
/// (repo-state §7.4). No CLI verb closes a leaf yet, so the closed state is
/// synthesized directly for the `leaves` enumeration tests.
pub fn appendClosedControl(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
timestamp: []const u8,
target: []const u8,
) ![]u8 {
return appendTagControl(gpa, io, dir, timestamp, .add, "closed", target, null);
}
// ==========================================================================
// `zit commit` end-to-end (behavior ids B-commit-1..20). Each test
// spawns the REAL binary in an isolated tmp dir with a pinned environment
// (TZ=UTC, isolated HOME, and — for reproducibility — a fixed
// SOURCE_DATE_EPOCH and USER), asserts exit code + stdout/stderr
// (structurally around the hash/timestamp), and reads the resulting `.zit` /
// `.zit-checkout` state back THROUGH THE LIBRARY. Teardown is unconditional.
// ==========================================================================
/// A fixed clock for reproducible check-ins: 1234567890 == 2009-02-13T23:31:30Z.
pub const commit_epoch = "1234567890";
/// Optional environment overrides for a `commit` spawn. Defaults pin a
/// reproducible clock and a known author; a test sets a field to null to
/// exercise its absence (e.g. `.{ .user = null }` for the author-missing path).
pub const CommitEnv = struct {
epoch: ?[]const u8 = commit_epoch,
user: ?[]const u8 = "e2e-user",
/// `ZIT_PGP_COMMAND` for `commit --sign`. A stub signer keeps the
/// suite deterministic and key-less: no gpg, no keyring, no expiry.
pgp: ?[]const u8 = null,
/// `$ZIT_CONFIG`, the operator-scope settings file. Set it to a path
/// inside the test's own tmp tree: the global tier is located through the
/// environment, and the suite must never read or write the real one.
zit_config: ?[]const u8 = null,
};
/// Spawns `zit <tail...>` with the pinned base environment plus the commit
/// overrides. Caller owns `result.stdout`/`result.stderr`.
/// `runZit` with stdin fed from `stdin_file` (a path under `tmp_dir`):
/// `std.process.run` pins child stdin to `.ignore`, so the redirect goes
/// through `sh -c` with the zit binary quoted absolute. The wiki body and
/// `cone test -`'s path list are the consumers.
pub fn runZitStdin(
gpa: std.mem.Allocator,
io: std.Io,
tmp_dir: std.Io.Dir,
stdin_file: []const u8,
tail: []const []const u8,
) !std.process.RunResult {
var env = std.process.Environ.Map.init(gpa);
defer env.deinit();
try env.put("TZ", "UTC");
try env.put("HOME", "/zit-e2e-isolated-home-does-not-exist");
try env.put("PATH", "/usr/bin:/bin");
const cwd_abs = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(cwd_abs);
const abs_exe = try std.fs.path.resolve(gpa, &.{ cwd_abs, zit_exe });
defer gpa.free(abs_exe);
var cmd: std.ArrayList(u8) = .empty;
defer cmd.deinit(gpa);
try cmd.appendSlice(gpa, "exec '");
try cmd.appendSlice(gpa, abs_exe);
try cmd.appendSlice(gpa, "'");
for (tail) |t| {
try cmd.appendSlice(gpa, " '");
try cmd.appendSlice(gpa, t);
try cmd.appendSlice(gpa, "'");
}
try cmd.appendSlice(gpa, " < '");
try cmd.appendSlice(gpa, stdin_file);
try cmd.appendSlice(gpa, "'");
return std.process.run(gpa, io, .{
.argv = &.{ "/bin/sh", "-c", cmd.items },
.cwd = .{ .dir = tmp_dir },
.environ_map = &env,
});
}
pub fn runCommitEnv(
gpa: std.mem.Allocator,
io: std.Io,
tmp_dir: std.Io.Dir,
env_opts: CommitEnv,
tail: []const []const u8,
) !std.process.RunResult {
var env = std.process.Environ.Map.init(gpa);
defer env.deinit();
try env.put("TZ", "UTC");
try env.put("HOME", "/zit-e2e-isolated-home-does-not-exist");
try env.put("PATH", "/usr/bin:/bin");
if (env_opts.epoch) |e| try env.put("SOURCE_DATE_EPOCH", e);
if (env_opts.user) |u| try env.put("USER", u);
if (env_opts.pgp) |p| try env.put("ZIT_PGP_COMMAND", p);
if (env_opts.zit_config) |c| try env.put("ZIT_CONFIG", c);
const cwd_abs = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(cwd_abs);
const abs_exe = try std.fs.path.resolve(gpa, &.{ cwd_abs, zit_exe });
defer gpa.free(abs_exe);
var argv_buf: [16][]const u8 = undefined;
argv_buf[0] = abs_exe;
std.debug.assert(tail.len <= argv_buf.len - 1);
for (tail, argv_buf[1..][0..tail.len]) |src, *dst| dst.* = src;
return std.process.run(gpa, io, .{
.argv = argv_buf[0 .. 1 + tail.len],
.cwd = .{ .dir = tmp_dir },
.environ_map = &env,
});
}
/// Writes `content` to `name` under `dir` (truncating any existing file), so a
/// second commit sees the edited bytes.
pub fn writeFile(io: std.Io, dir: std.Io.Dir, name: []const u8, content: []const u8) !void {
var f = try dir.createFile(io, name, .{});
defer f.close(io);
try f.writePositionalAll(io, content, 0);
}
/// Reads `.zit-checkout` through the library and returns whether `version`
/// equals `expected` (empty string ⇒ unborn).
pub fn expectCheckoutVersion(io: std.Io, dir: std.Io.Dir, expected: []const u8) !void {
var ckbuf: [checkout.max_ck_payload + 1]u8 = undefined;
const ck = (try checkout.read(io, dir, &ckbuf)).?;
try std.testing.expectEqualStrings(expected, ck.version);
}
/// Returns the checkout's current `version` copied onto `gpa` (caller frees).
pub fn checkoutVersionAlloc(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) ![]u8 {
var ckbuf: [checkout.max_ck_payload + 1]u8 = undefined;
const ck = (try checkout.read(io, dir, &ckbuf)).?;
return gpa.dupe(u8, ck.version);
}
/// A parsed check-in plus the raw manifest bytes it borrows. `Manifest`'s `hash`
/// (F card) and `parents` (P card) fields are RAW slices into these bytes, so the
/// bytes must outlive the manifest — `deinit` frees both, in order.
pub const Checkin = struct {
m: artifact.Manifest,
bytes: []u8,
gpa: std.mem.Allocator,
pub fn deinit(self: *Checkin) void {
self.m.deinit();
self.gpa.free(self.bytes);
}
};
/// Opens `.zit` read-only through the library and parses the artifact named
/// `hash` into a `Checkin` (caller `deinit`s). The e2e state oracle for a minted
/// check-in — never reads Fossil, only zit's own reader.
pub fn parseCheckin(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, hash: []const u8) !Checkin {
var file = try dir.openFile(io, ".zit", .{ .mode = .read_only });
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.openReadOnly(gpa, fs.storageFile());
defer log.deinit(gpa);
// The log keys frames by the 32-byte binary digest; the check-in `hash` is
// its 64-hex name, so decode before lookup (the same bridge `commit` uses).
var digest: [32]u8 = undefined;
if (hash.len != 64) return error.CheckinNotFound;
_ = try std.fmt.hexToBytes(&digest, hash);
const rid = log.lookup(&digest) orelse return error.CheckinNotFound;
const bytes = (try log.content(gpa, rid)) orelse return error.CheckinNotFound;
errdefer gpa.free(bytes);
const m = try artifact.parse(gpa, bytes);
return .{ .m = m, .bytes = bytes, .gpa = gpa };
}
/// Finds the F card named `name`, or null.
pub fn findF(m: artifact.Manifest, name: []const u8) ?artifact.FCard {
for (m.f) |f| if (std.mem.eql(u8, f.filename, name)) return f;
return null;
}
/// Extracts the 64-hex artifact hash from a `check-in <hash> on <branch>\n`
/// success line, asserting the fixed structure (never a wall clock).
pub fn checkinHash(stdout: []const u8) ![]const u8 {
const prefix = "check-in ";
try std.testing.expect(std.mem.startsWith(u8, stdout, prefix));
try std.testing.expect(std.mem.endsWith(u8, stdout, "\n"));
const rest = stdout[prefix.len .. stdout.len - 1];
const sp = std.mem.indexOfScalar(u8, rest, ' ') orelse return error.MalformedSuccessLine;
const hash = rest[0..sp];
try std.testing.expectEqual(@as(usize, 64), hash.len);
for (hash) |ch| try std.testing.expect(std.ascii.isHex(ch) and !std.ascii.isUpper(ch));
return hash;
}
// --- commit --branch / --close (repostate §6.2, artifact §4.7) --------------
/// True iff `m` carries a T card with the given sign and (sign-stripped) name.
pub fn hasTag(m: artifact.Manifest, sign: artifact.TSign, name: []const u8) bool {
for (m.t) |t| {
if (t.sign == sign and std.mem.eql(u8, t.name, name)) return true;
}
return false;
}
/// Commits `content` for `name` and returns the resulting check-in hash borrowed
/// from `commit_out` (kept alive by the caller). Two calls build a root→child
/// history the undo tests reverse.
pub fn commitFile(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
name: []const u8,
content: []const u8,
message: []const u8,
commit_out: *std.process.RunResult,
) ![]const u8 {
try writeFile(io, dir, name, content);
try addManaged(gpa, io, dir, &.{name});
commit_out.* = try runCommitEnv(gpa, io, dir, .{ .user = "u" }, &.{ "commit", "-m", message });
try expectExit(commit_out.term, 0);
return checkinHash(commit_out.stdout);
}
/// The `forks` array of a `sync`/`pull -F json` answer, after checking the
/// object carries `schema_version` 1. The caller owns `parsed`.
pub fn forksOfJson(gpa: std.mem.Allocator, stdout: []const u8, parsed: *std.json.Parsed(std.json.Value)) ![]std.json.Value {
parsed.* = std.json.parseFromSlice(std.json.Value, gpa, stdout, .{}) catch |e| {
std.debug.print("not one JSON object:\n{s}\n", .{stdout});
return e;
};
const obj = parsed.value.object;
try std.testing.expectEqual(@as(i64, 1), obj.get("schema_version").?.integer);
try std.testing.expect(obj.get("received").? == .integer);
try std.testing.expect(obj.get("rounds").? == .integer);
try std.testing.expect((obj.get("erasures_deferred") orelse return error.NoErasuresDeferred) == .integer);
return obj.get("forks").?.array.items;
}
pub fn appendArtifactAs(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
algo: artifact.HashAlgo,
bytes: []const u8,
) ![]u8 {
var name_buf: [64]u8 = undefined;
const name = artifact.artifactName(algo, bytes, &name_buf);
var digest: [32]u8 = undefined;
const decoded = digest[0 .. name.len / 2];
_ = try std.fmt.hexToBytes(decoded, name);
var file = try dir.openFile(io, ".zit", .{ .mode = .read_write });
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.open(gpa, fs.storageFile(), 0);
defer log.deinit(gpa);
_ = try log.put(decoded, bytes);
try log.commit();
return gpa.dupe(u8, name);
}
pub fn appendArtifact(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, bytes: []const u8) ![]u8 {
return appendArtifactAs(gpa, io, dir, .sha3_256, bytes);
}
pub fn appendSyntheticCheckinAs(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
algo: artifact.HashAlgo,
label: []const u8,
parents: ?[]const u8,
) ![]u8 {
const f = [_]artifact.FCard{.{
.filename = "f",
.hash = "0000000000000000000000000000000000000000000000000000000000000000",
.perm = null,
.old_name = null,
.mode = .regular,
}};
const p = [_]artifact.PCard{.{
.parents = parents,
.count = if (parents) |wire| std.mem.countScalar(u8, wire, ' ') + 1 else 0,
}};
var manifest: artifact.Manifest = .{
.arena_state = std.heap.ArenaAllocator.init(gpa),
.kind = .manifest,
.a = null,
.b = null,
.c = .{ .comment = label },
.d = .{ .timestamp = "2020-01-01T00:00:00" },
.e = null,
.g = null,
.h = null,
.i = null,
.k = null,
.l = null,
.n = null,
.r = null,
.u = .{ .login = "u" },
.z = null,
.w = null,
.f = &f,
.j = &.{},
.m = &.{},
.p = if (parents == null) &.{} else &p,
.q = &.{},
.t = &.{},
};
defer manifest.deinit();
const bytes = try artifact.create(&manifest, gpa);
defer gpa.free(bytes);
return appendArtifactAs(gpa, io, dir, algo, bytes);
}
pub fn appendSyntheticCheckin(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
label: []const u8,
parents: ?[]const u8,
) ![]u8 {
return appendSyntheticCheckinAs(gpa, io, dir, .sha3_256, label, parents);
}
// ==========================================================================
// `zit leaves` end-to-end (repo-state §7.1/§7.4/§7.5/§7.7 + CLI discipline).
// Each test spawns the REAL binary in an isolated tmp dir, asserts exit code
// and stdout/stderr, and builds its DAG through real commits (a fork via
// `commit` → `update` → `commit`) plus, where a closed leaf is needed, a
// synthesized `closed` control artifact (no CLI verb closes a leaf yet).
// Teardown is unconditional.
// ==========================================================================
/// Counts newline-terminated lines in `s` (the leaf listing is one line per
/// leaf, so this is the count a `| wc -l` pipe would report).
pub fn lineCount(s: []const u8) usize {
return std.mem.count(u8, s, "\n");
}
/// Builds a two-open-leaf trunk fork with distinct modification times: a root,
/// then `child1` (older), then — after `update` back to the root — `child2`
/// (newer), which becomes the current checkout. Returns both leaf IDs (caller
/// frees each).
pub fn buildFork(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !struct { child1: []const u8, child2: []const u8 } {
try initRepo(gpa, io, dir);
try writeFile(io, dir, "a.txt", "root\n");
try addManaged(gpa, io, dir, &.{"a.txt"});
var c0 = try runCommitEnv(gpa, io, dir, .{ .user = "u", .epoch = "900000000" }, &.{ "commit", "-m", "root" });
defer freeResult(gpa, &c0);
try expectExit(c0.term, 0);
const root = try gpa.dupe(u8, try checkinHash(c0.stdout));
defer gpa.free(root);
try writeFile(io, dir, "a.txt", "one\n");
var c1 = try runCommitEnv(gpa, io, dir, .{ .user = "u", .epoch = "1000000000" }, &.{ "commit", "-m", "child1" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
const child1 = try gpa.dupe(u8, try checkinHash(c1.stdout));
errdefer gpa.free(child1);
var up = try runZit(gpa, io, dir, &.{ "update", root });
defer freeResult(gpa, &up);
try expectExit(up.term, 0);
try writeFile(io, dir, "a.txt", "two\n");
var c2 = try runCommitEnv(gpa, io, dir, .{ .user = "u", .epoch = "2000000000" }, &.{ "commit", "-m", "child2", "--allow-fork" });
defer freeResult(gpa, &c2);
try expectExit(c2.term, 0);
const child2 = try gpa.dupe(u8, try checkinHash(c2.stdout));
return .{ .child1 = child1, .child2 = child2 };
}
/// Writes a config frame naming `branch` as the repository's main-branch
/// (repo-state §6.4), opening `.zit` read-write exactly as `appendArtifactAs`
/// does — so `info` can be shown to thread the configured default branch rather
/// than assuming the literal "trunk".
pub fn writeDefaultBranch(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, branch: []const u8) !void {
var file = try dir.openFile(io, ".zit", .{ .mode = .read_write });
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.open(gpa, fs.storageFile(), 0);
defer log.deinit(gpa);
try log.putConfig(.{ .hash_policy = 0, .default_branch = branch, .project_id = "p" });
try log.commit();
}
/// Lowercase-hex sha3-256 of `content` — the §7.2 artifact name of stored
/// content, copied onto `gpa` (caller frees). The RFC-derived blob-hash oracle.
pub fn sha3HexAlloc(gpa: std.mem.Allocator, content: []const u8) ![]u8 {
var digest: [32]u8 = undefined;
std.crypto.hash.sha3.Sha3_256.hash(content, &digest, .{});
const hex = "0123456789abcdef";
const out = try gpa.alloc(u8, 64);
for (digest, 0..) |b, i| {
out[i * 2] = hex[b >> 4];
out[i * 2 + 1] = hex[b & 0x0f];
}
return out;
}
// --- clone interop (sync §3.7/§4/§8) --------------------------------------
//
// The milestone e2e: a real `zit clone` client converging against a real
// `zit serve` server over loopback HTTP. Repo A is populated with a committed
// file; A is served; clone builds a fresh repo B; B's on-disk artifact set is
// read back through the library and must equal A's — proving the client store
// converged to the server's content over the wire, not from any fossil oracle.
/// Allocates a fresh loopback port for a child `zit serve` by binding
/// `127.0.0.1:0`, reading back the kernel-assigned port, then closing the
/// reservation listener so the child can bind it. Every server-interop scenario
/// draws its port here instead of a hardcoded constant, so overlapping runs of
/// this suite (or unrelated host load) never collide on a fixed port — the
/// defect behind the flaky push, where a concurrent run's `zit serve` failed to bind
/// a shared port and the client's push crossed to the wrong server.
///
/// The reservation listener closes before the child binds, so a close→child-bind
/// race window remains; it is unavoidable because a child server cannot inherit
/// the listener, and it is orders of magnitude narrower than a fixed-port
/// collision. This mirrors `freshLoopbackPort` in src/smoke.zig (the 0.1.1 work
/// that moved the Fossil smoke tests off fixed ports for the same reason);
/// duplicated rather than shared because e2e and smoke are independent test
/// binaries that already each carry their own `waitForListen`.
pub fn freshLoopbackPort(io: std.Io) !u16 {
const addr = try net.IpAddress.parseIp4("127.0.0.1", 0);
var server = try addr.listen(io, .{});
const port = server.socket.address.getPort();
server.deinit(io);
if (port == 0) return error.NoLoopbackPort;
return port;
}
/// Spawns `zit serve --port <port>` as a detached child whose cwd is `dir`
/// (so `discoverRoot` finds `dir/.zit`). stdout/stderr are discarded — the
/// test drives it purely over the socket. Caller `kill`s the returned child;
/// `test_child` stops it anyway if the test process dies first.
pub fn spawnServe(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, port: u16) !std.process.Child {
return spawnServeFronted(gpa, io, dir, port, false);
}
/// `spawnServe` with `--fronted` when asked.
pub fn spawnServeFronted(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, port: u16, fronted: bool) !std.process.Child {
return spawnServeFlag(gpa, io, dir, port, if (fronted) "--fronted" else null);
}
/// `spawnServe` with one extra flag, when given.
pub fn spawnServeFlag(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, port: u16, flag: ?[]const u8) !std.process.Child {
return spawnServeFlags(gpa, io, dir, port, if (flag) |f| &.{f} else &.{});
}
/// `spawnServe` with extra flags.
pub fn spawnServeFlags(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, port: u16, flags: []const []const u8) !std.process.Child {
var env = std.process.Environ.Map.init(gpa);
defer env.deinit();
try env.put("TZ", "UTC");
try env.put("HOME", "/zit-e2e-isolated-home-does-not-exist");
try env.put("PATH", "/usr/bin:/bin");
const cwd_abs = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(cwd_abs);
const abs_exe = try std.fs.path.resolve(gpa, &.{ cwd_abs, zit_exe });
defer gpa.free(abs_exe);
const port_str = try std.fmt.allocPrint(gpa, "{d}", .{port});
defer gpa.free(port_str);
// `spawn` builds the child image during this call; once it returns the
// parent no longer references the argv/env buffers, so the defers above
// free them safely after the child exists.
var argv_buf: [8][]const u8 = undefined;
const head = [_][]const u8{ abs_exe, "serve", "--port", port_str };
std.debug.assert(head.len + flags.len <= argv_buf.len);
@memcpy(argv_buf[0..head.len], &head);
@memcpy(argv_buf[head.len..][0..flags.len], flags);
return test_child.spawn(io, .{
.argv = argv_buf[0 .. head.len + flags.len],
.cwd = .{ .dir = dir },
.environ_map = &env,
.stdout = .ignore,
.stderr = .ignore,
});
}
/// Bounded wait for `zit serve` to bind: retry-connect to the loopback port
/// until a connection is accepted or a wall-clock deadline elapses (never a
/// fixed sleep). A successful TCP connect proves the listener is up (connect
/// completes on the listen backlog, before `accept`). The deadline guarantees
/// termination if the server never binds (e.g. the port is already in use).
pub fn waitForListen(io: std.Io, port: u16) !void {
const addr = net.IpAddress.parseIp4("127.0.0.1", port) catch return error.BadTestAddress;
const deadline_ns: i96 = 15_000_000_000; // 15 s cap — orders of magnitude of slack
const start = std.Io.Clock.now(.awake, io).nanoseconds;
while (true) {
if (addr.connect(io, .{ .mode = .stream })) |stream| {
stream.close(io);
return;
} else |_| {}
if (std.Io.Clock.now(.awake, io).nanoseconds - start > deadline_ns) return error.ServerNeverBound;
}
}
/// Reads every content-bearing, non-config artifact digest from `dir/.zit`
/// through the library (LogStore keys frames by the raw binary digest), sorted
/// so two repositories' sets compare directly. Caller frees via `freeDigests`.
pub fn collectArtifactDigests(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) ![][]u8 {
var file = try dir.openFile(io, ".zit", .{ .mode = .read_only });
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.openReadOnly(gpa, fs.storageFile());
defer log.deinit(gpa);
const rids = try log.ridsInPersistedOrder(gpa); // non-phantom, persisted order
defer gpa.free(rids);
var list: std.ArrayListUnmanaged([]u8) = .empty;
errdefer {
for (list.items) |h| gpa.free(h);
list.deinit(gpa);
}
for (rids) |rid| {
if (log.kindOf(rid) == .config) continue; // config is repo metadata, not synced
try list.append(gpa, try gpa.dupe(u8, log.hashOf(rid)));
}
const slice = try list.toOwnedSlice(gpa);
std.mem.sort([]u8, slice, {}, struct {
fn lt(_: void, a: []u8, b: []u8) bool {
return std.mem.order(u8, a, b) == .lt;
}
}.lt);
return slice;
}
pub fn freeDigests(gpa: std.mem.Allocator, digests: [][]u8) void {
for (digests) |h| gpa.free(h);
gpa.free(digests);
}
/// Reads a repository's stored sync project code (§9.1) from its `.zit`
/// config frame. Caller owns the returned bytes.
pub fn readProjectCode(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) ![]u8 {
var file = try dir.openFile(io, ".zit", .{ .mode = .read_only });
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.openReadOnly(gpa, fs.storageFile());
defer log.deinit(gpa);
var oc = (try log.readConfig(gpa)) orelse return error.TestUnexpectedResult;
defer oc.deinit(gpa);
return gpa.dupe(u8, oc.config.project_code);
}
/// Asserts two artifact-digest sets are identical (same count, same members).
/// Both inputs are sorted by `collectArtifactDigests`, so an elementwise walk
/// is a set-equality proof.
pub fn expectSameDigestSet(a: []const []const u8, b: []const []const u8) !void {
try std.testing.expectEqual(a.len, b.len);
for (a, b) |x, y| try std.testing.expectEqualSlices(u8, x, y);
}
/// An origin served by `zit serve`, and a clone of it (`dir`) with one
/// commit of its own to push.
pub const PushFixture = struct {
origin: std.testing.TmpDir,
client: std.testing.TmpDir,
dir: std.Io.Dir,
port: u16,
srv: std.process.Child,
pub fn init(gpa: std.mem.Allocator, io: std.Io) !PushFixture {
var origin = std.testing.tmpDir(.{});
errdefer origin.cleanup();
try initRepo(gpa, io, origin.dir);
try writeFile(io, origin.dir, "a.txt", "a\n");
try addManaged(gpa, io, origin.dir, &.{"a.txt"});
{
var c = try runCommitEnv(gpa, io, origin.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed", "--no-sync" });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
}
const port = try freshLoopbackPort(io);
var srv = try spawnServe(gpa, io, origin.dir, port);
errdefer srv.kill(io);
try waitForListen(io, port);
var client = std.testing.tmpDir(.{});
errdefer client.cleanup();
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{port});
defer gpa.free(url);
{
var r = try runZit(gpa, io, client.dir, &.{ "clone", url, "c", "--once" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
var dir = try client.dir.openDir(io, "c", .{});
errdefer dir.close(io);
try writeFile(io, dir, "a.txt", "b\n");
{
var c = try runCommitEnv(gpa, io, dir, .{ .user = "u" }, &.{ "commit", "-m", "two", "--no-sync" });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
}
return .{ .origin = origin, .client = client, .dir = dir, .port = port, .srv = srv };
}
pub fn deinit(f: *PushFixture, io: std.Io) void {
f.srv.kill(io);
f.dir.close(io);
f.client.cleanup();
f.origin.cleanup();
}
};
// --- §9.3 configuration exchange: the project-scope submanifest settings ---
//
// draft-fossil-submanifests-00 §6.3 requires the partition settings to reach
// every writer of a project ("configuration propagating to clones ... is
// REQUIRED"); §8.1 says the same of `enable-submanifests`, and §6.4 adds
// `submanifest-auto-retune`. [FossilSync] §9.3's scalar
// `/config` channel is the transport. These tests run TWO real repositories
// over real HTTP — the gap the feature closes is exactly "the clone comes up
// with different settings from the origin", which nothing below the CLI can
// observe.
/// The stored `submanifest-auto-retune`, read back through the library. The
/// §6.4 setting is not in `submanifest.Settings` (which models only §6.3's
/// partition state), so it is read off the config frame directly.
pub fn readAutoRetune(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !bool {
var file = try dir.openFile(io, ".zit", .{ .mode = .read_only });
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.openReadOnly(gpa, fs.storageFile());
defer log.deinit(gpa);
var oc = (try log.readConfig(gpa)) orelse return error.TestUnexpectedResult;
defer oc.deinit(gpa);
return oc.config.submanifest_auto_retune;
}
/// POST one hand-built request-card stream to a running `zit serve` and return
/// the decoded reply cards. A raw socket rather than `zit push`: the §9.3 r30
/// `config` card is one no zit client emits, so there is no verb that produces
/// this request and the wire has to be written by hand. Caller owns the result.
pub fn postXferCards(gpa: std.mem.Allocator, io: std.Io, port: u16, request_cards: []const u8) ![]u8 {
return postXferCardsHeaders(gpa, io, port, request_cards, "");
}
/// `postXferCards` with extra request-header lines (each `\r\n`-terminated),
/// the channel a front uses to hand `zit serve --fronted` its rule.
pub fn postXferCardsHeaders(gpa: std.mem.Allocator, io: std.Io, port: u16, request_cards: []const u8, extra_headers: []const u8) ![]u8 {
const got = try postXferCardsHead(gpa, io, port, request_cards, extra_headers);
gpa.free(got.head);
return got.body;
}
/// A response's head and decoded body, both the caller's to free.
pub const XferHead = struct { head: []u8, body: []u8 };
/// `postXferCardsHeaders`, keeping the response head too, for a test that
/// reads a response header.
pub fn postXferCardsHead(gpa: std.mem.Allocator, io: std.Io, port: u16, request_cards: []const u8, extra_headers: []const u8) !XferHead {
const framed = try container.encode(gpa, request_cards, .default);
defer gpa.free(framed);
const stream = try dialServe(io, port);
defer stream.close(io);
{
var wbuf: [4096]u8 = undefined;
var w = stream.writer(io, &wbuf);
try w.interface.print(
"POST /xfer HTTP/1.1\r\nHost: z\r\nContent-Type: application/x-fossil\r\n" ++
"Content-Length: {d}\r\n{s}Connection: close\r\n\r\n",
.{ framed.len, extra_headers },
);
try w.interface.writeAll(framed);
try w.interface.flush();
}
var rbuf: [4096]u8 = undefined;
var r = stream.reader(io, &rbuf);
// `keep_alive = false` on the server side means Content-Length plus a close,
// so the read ends at EOF.
const raw = try r.interface.allocRemaining(gpa, .limited(1 << 20));
defer gpa.free(raw);
const sep = std.mem.indexOf(u8, raw, "\r\n\r\n") orelse return error.TestUnexpectedResult;
const head = try gpa.dupe(u8, raw[0..sep]);
errdefer gpa.free(head);
return .{ .head = head, .body = try container.decode(gpa, raw[sep + 4 ..], 1 << 20) };
}
/// The count of artifacts B holds straight after its clone — the pull's starting
/// subset. Read back through the library so the incremental assertion (A grew
/// past this) rests on B's real on-disk state, not an assumed number.
pub fn b_seed_len(gpa: std.mem.Allocator, io: std.Io, tmp_b: std.Io.Dir) usize {
var b_repo = tmp_b.openDir(io, "repo", .{}) catch return 0;
defer b_repo.close(io);
const d = collectArtifactDigests(gpa, io, b_repo) catch return 0;
defer freeDigests(gpa, d);
return d.len;
}
// --- real-socket robustness (serve accept/read loop) -----------------------
//
// `serve_fuzz` drives the in-memory `driveHttp` seam; the socket accept/read
// loop — the part that can HANG — is only exercised by the happy-path interop
// tests above. `zit serve` accepts one connection to completion before the next
// (single-threaded, `serve.zig`), so a peer that connects and then stalls its
// request — a byte-dribbling slow-loris, a half-sent body, an idle hold — would
// pin the accept loop and wedge the server for every other client (the hung
// spawned server `ci.yml` names as the feared failure). These four scenarios
// abuse a LIVE `zit serve` over loopback and assert, with bounded waits, that
// the server survives: a fresh request is still answered within a bounded
// window, and a full `clone` sync round still converges. The bound is the
// per-connection deadline in `serve.zig` (`default_conn_read_timeout_ns`); the
// stalled connection is abandoned when it wins, freeing the loop.
/// Open a raw loopback TCP connection to a live `zit serve` on `port`. Unlike a
/// `zit` client this sends bytes the harness controls verbatim, so it can stall
/// mid-request. Caller `close`s the returned stream.
pub fn dialServe(io: std.Io, port: u16) !net.Stream {
const addr = net.IpAddress.parseIp4("127.0.0.1", port) catch return error.BadTestAddress;
return addr.connect(io, .{ .mode = .stream });
}
/// A parent checkout holding a nested zit checkout at `vendor/lib`, a nested
/// fossil checkout at `vendor/fsl` (fossil recognizes a checkout database by
/// name and a size of at least 1024 bytes, vfile.c `vfile_top_of_checkout`), a
/// `junk/` directory whose `.fslckout` is too small to be one, and an ordinary
/// `sub/`. Each nested directory carries one untracked file of its own.
pub fn writeNestedCheckouts(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !void {
try initRepo(gpa, io, dir);
try touch(io, dir, "a.txt");
try dir.createDirPath(io, "sub");
try touch(io, dir, "sub/s.txt");
try dir.createDirPath(io, "vendor/lib");
{
var lib = try dir.openDir(io, "vendor/lib", .{});
defer lib.close(io);
try initRepo(gpa, io, lib);
}
try touch(io, dir, "vendor/lib/lib.c");
try dir.createDirPath(io, "vendor/fsl");
try writeFile(io, dir, "vendor/fsl/.fslckout", &(@as([1024]u8, @splat(0))));
try touch(io, dir, "vendor/fsl/f.c");
try dir.createDirPath(io, "junk");
try writeFile(io, dir, "junk/.fslckout", "not a checkout\n");
try touch(io, dir, "junk/j.c");
}
/// Extracts the 64-hex control-artifact hash from an `amend <hash>\n` success
/// line, asserting the fixed structure (never a wall clock).
pub fn amendHash(stdout: []const u8) ![]const u8 {
const prefix = "amend ";
try std.testing.expect(std.mem.startsWith(u8, stdout, prefix));
try std.testing.expect(std.mem.endsWith(u8, stdout, "\n"));
const hash = stdout[prefix.len .. stdout.len - 1];
try std.testing.expectEqual(@as(usize, 64), hash.len);
for (hash) |ch| try std.testing.expect(std.ascii.isHex(ch) and !std.ascii.isUpper(ch));
return hash;
}
/// Seeds a repository with one root check-in (comment "orig", file "f") and
/// returns its 64-hex hash (caller frees). The shared setup for the amend e2e.
pub fn seedRootCheckin(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) ![]u8 {
try initRepo(gpa, io, dir);
try writeFile(io, dir, "f", "v1\n");
try addManaged(gpa, io, dir, &.{"f"});
var root = try runCommitEnv(gpa, io, dir, .{ .user = "u" }, &.{ "commit", "-m", "orig" });
defer freeResult(gpa, &root);
try expectExit(root.term, 0);
return gpa.dupe(u8, try checkinHash(root.stdout));
}
/// The store of `from`, dropped over the freshly-initialized repository in
/// `to`, whose checkout record is still unborn. This is the state `zit clone`
/// leaves, reached without a server: `open` resolving its own tip is the
/// subject of both tests, and a transport in front of it would only add ways
/// for them to fail for unrelated reasons.
pub fn adoptStore(gpa: std.mem.Allocator, io: std.Io, from: std.Io.Dir, to: std.Io.Dir) !void {
const store = try readFile(gpa, io, from, ".zit");
defer gpa.free(store);
try writeFile(io, to, ".zit", store);
}
// ==========================================================================
// `zit add` / `zit rm` directory recursion (P3-F4). `add <dir>` scopes ONE
// `extras.sweep` of the checkout root by a directory prefix — the same walk,
// the same exclusions, no second walker; `rm <dir>` expands the argument over
// the TRACKED keys (`ManagedSet.iterate`) and never touches the disk, so a
// directory the user already deleted is still forgettable. Both verbs consume
// exactly one enumeration each.
// ==========================================================================
/// Creates the directory chain `path` under `dir`, component by component.
/// `createDir` is single-level; the recursion tests need nested trees.
pub fn mkDirPath(io: std.Io, dir: std.Io.Dir, path: []const u8) !void {
var it = std.mem.splitScalar(u8, path, '/');
var cur = try dir.openDir(io, ".", .{});
defer cur.close(io);
while (it.next()) |seg| {
if (seg.len == 0) continue;
cur.createDir(io, seg, .default_dir) catch |e| switch (e) {
error.PathAlreadyExists => {},
else => return e,
};
const child = try cur.openDir(io, seg, .{});
cur.close(io);
cur = child;
}
}
/// Creates every `dir/name` file, making the parent directories first.
pub fn touchTree(io: std.Io, dir: std.Io.Dir, paths: []const []const u8) !void {
for (paths) |p| {
if (std.mem.lastIndexOfScalar(u8, p, '/')) |i| try mkDirPath(io, dir, p[0..i]);
try touch(io, dir, p);
}
}
// ---------------------------------------------------------------------------
// `zit stat` — the store's own byte accounting and artifact roll.
// ---------------------------------------------------------------------------
/// Extracts the u64 value of a `key value\n` line from `zit stat` output.
/// Fails the test if the key is absent, so a dropped field cannot pass as a
/// zero.
pub fn statField(out: []const u8, key: []const u8) !u64 {
var lines = std.mem.splitScalar(u8, out, '\n');
while (lines.next()) |line| {
var it = std.mem.splitScalar(u8, line, ' ');
const k = it.next() orelse continue;
if (!std.mem.eql(u8, k, key)) continue;
const v = it.next() orelse return error.TestUnexpectedResult;
return std.fmt.parseInt(u64, v, 10);
}
std.debug.print("`zit stat` output has no '{s}' field:\n{s}\n", .{ key, out });
return error.TestUnexpectedResult;
}
// --- `-n N` entry limit and `-R` repository override -------------------------
//
// Two options over one shared set of read verbs. `-n` follows fossil, including
// its deliberate per-verb overload (`timeline -n 20` limits, `add -n` is a dry
// run); `-R` follows `fossil -R`, naming a repository that need not have a
// working tree attached.
/// Builds `n` check-ins in `dir`, each touching the same file. The commits land
/// as fast as the machine allows, so several routinely share a one-second
/// timestamp — which is the case the limit must still order correctly.
pub fn seedHistory(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, n: usize) !void {
try initRepo(gpa, io, dir);
try writeFile(io, dir, "f.txt", "0\n");
{
var res = try runZit(gpa, io, dir, &.{ "add", "-q", "f.txt" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
var i: usize = 0;
while (i < n) : (i += 1) {
var buf: [64]u8 = undefined;
const body = try std.fmt.bufPrint(&buf, "line {d}\n", .{i});
try writeFile(io, dir, "f.txt", body);
var msg_buf: [32]u8 = undefined;
const msg = try std.fmt.bufPrint(&msg_buf, "c{d}", .{i});
var res = try runCommitEnv(gpa, io, dir, .{ .user = "u" }, &.{ "commit", "-q", "-m", msg });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
}
}
pub fn countLines(s: []const u8) usize {
return std.mem.count(u8, s, "\n");
}
/// A store with two files in one check-in, made the ordinary way in `store`.
pub fn seedStdinStore(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, store: []const u8) !void {
try dir.createDir(io, store, .default_dir);
var s = try dir.openDir(io, store, .{});
defer s.close(io);
try initRepo(gpa, io, s);
try writeFile(io, s, "f.txt", "f bytes\n");
try writeFile(io, s, "g.txt", "g bytes\n");
var add = try runZit(gpa, io, s, &.{ "add", "-q", "f.txt", "g.txt" });
defer freeResult(gpa, &add);
try expectExitDiag(add.term, 0, add);
var c = try runCommitEnv(gpa, io, s, .{ .user = "u" }, &.{ "commit", "-q", "-m", "seed" });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
}
/// A checkout whose tip holds `c\x01d`, as a check-in stock Fossil made would:
/// the name enters the managed set the way a checkout of such a manifest puts
/// it there, since `zit add` refuses it.
pub fn seedFossilMadeName(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !void {
try initRepo(gpa, io, dir);
try writeFile(io, dir, "c\x01d", "odd\n");
try writeFile(io, dir, "ok.txt", "ok\n");
{
var set = try managed.ManagedSet.read(gpa, io, dir);
defer set.deinit();
_ = try set.insert("c\x01d");
_ = try set.insert("ok.txt");
try set.write(io, dir);
}
var c = try runCommitEnv(gpa, io, dir, .{ .user = "u" }, &.{ "commit", "-q", "-m", "seed" });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
}
// --- sub-manifest convergence across pull rounds ---------------------------
//
// draft-fossil-submanifests-00 §7: two zit peers converge a tree whose closure
// arrives in stages, over the REAL binaries and the REAL wire. §7.1 fixes the
// shape this test drives: "An interior sub-manifest's own S references become
// known only when it arrives, so a tree of depth d converges in at most d+1
// pull rounds." A's tree is completed between the two rounds, so B necessarily
// sees the intermediate state — a check-in it holds whose file set it cannot
// yet produce — and §5.2's "MUST NOT present a partially resolved tree as a
// complete file set" is asserted there, not assumed.
/// Appends the §6.2 Z self-checksum card to `body` and returns the whole
/// artifact. The digest is the MD5 of every byte before the `Z`, hex-encoded —
/// computed here rather than hard-coded, so the fixture stays correct if a
/// card above it is ever edited.
pub fn withZCard(gpa: std.mem.Allocator, body: []const u8) ![]u8 {
var digest: [16]u8 = undefined;
std.crypto.hash.Md5.hash(body, &digest, .{});
return std.fmt.allocPrint(gpa, "{s}Z {x}\n", .{ body, &digest });
}
/// One leaf sub-manifest holding `names` as its F run (submanifests §4.2: a
/// SUBMANIFEST carries F, S, and Z only, and never both F and S). Each F card
/// carries a hash because §4.4 forbids a hashless F card in a SUBMANIFEST.
pub fn leafSubManifest(gpa: std.mem.Allocator, names: []const []const u8) ![]u8 {
var body: std.ArrayList(u8) = .empty;
defer body.deinit(gpa);
for (names) |name| {
const blob = try sha3HexAlloc(gpa, name);
defer gpa.free(blob);
try body.print(gpa, "F {s} {s}\n", .{ name, blob });
}
return withZCard(gpa, body.items);
}
// ===========================================================================
// `zit submanifest` — the draft-fossil-submanifests-00 §6.3/§6.4/§8.1 operator
// surface, end to end through the real binary.
// ===========================================================================
/// Reads the two sub-manifest settings out of a repository's `.zit` config
/// frame — the durable state, not the verb's own report, so a claim that
/// "enabling persisted" is proved against storage rather than against stdout.
pub fn readSubmanifestSettings(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !submanifest.Settings {
var file = try dir.openFile(io, ".zit", .{ .mode = .read_only });
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.openReadOnly(gpa, fs.storageFile());
defer log.deinit(gpa);
var oc = (try log.readConfig(gpa)) orelse return error.TestUnexpectedResult;
defer oc.deinit(gpa);
return .of(oc.config);
}
/// Overwrite `submanifest-auto-retune` directly in the config frame, bypassing
/// the verb surface — there is no `zit submanifest` flag for this setting yet
/// (that CLI surface, and its propagation to a clone or peer, is out of scope
/// for the commit-path auto-dispatch this helper exists to test; §6.4's
/// opt-in setting still needs to be reachable by SOME writer for a test
/// to exercise it, the same reasoning `forcePinnedK` states for the
/// out-of-range case above).
pub fn forceAutoRetune(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, enabled: bool) !void {
var file = try dir.openFile(io, ".zit", .{ .mode = .read_write });
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.open(gpa, fs.storageFile(), 0);
defer log.deinit(gpa);
var oc = (try log.readConfig(gpa)) orelse return error.TestUnexpectedResult;
defer oc.deinit(gpa);
var next = oc.config;
next.submanifest_auto_retune = enabled;
try log.putConfig(next);
try log.commit();
}
/// Seeds `n` tracked files and commits them, returning the check-in hash.
/// `n = 40` partitions into a real tree at the k = 3 the tests pin; the count
/// is a TEST input, never a measured optimum.
pub fn seedManyFiles(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, n: usize) !void {
for (0..n) |i| {
var name_buf: [32]u8 = undefined;
const name = try std.fmt.bufPrint(&name_buf, "f{d:0>3}.txt", .{i});
try writeFile(io, dir, name, "x\n");
var add = try runZit(gpa, io, dir, &.{ "add", name });
defer freeResult(gpa, &add);
try expectExitDiag(add.term, 0, add);
}
}
// ---------------------------------------------------------------------------
// `zit shun` through the REAL binary.
//
// NO DRAFT SPECIFIES THIS VERB: sync §3.3 (docs/rfcs/
// draft-fossil-sync-protocol-00.txt L518-519) defines the repository shun table
// and L520-521 grants that local administration may add names to it; it names no command
// and no policy. Every expectation below is therefore pinned against zit's own
// stated policy — refuse by default, `--force` gates BOTH durable paths, the
// current checkout's check-in has no override — not against a section number.
// ---------------------------------------------------------------------------
/// The full lowercase-hex name of the artifact in `dir/.zit` whose reconstructed
/// content equals `body` (caller frees). Found by scanning rather than by
/// recomputing a hash, so the test never has to know which hash algorithm the
/// repository was initialized with. Fails if nothing holds those bytes — that is
/// the fixture's premise, not an assertion about shun.
pub fn artifactNameByContent(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
body: []const u8,
) ![]u8 {
var file = try dir.openFile(io, ".zit", .{});
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.openReadOnly(gpa, fs.storageFile());
defer log.deinit(gpa);
const rids = try log.ridsInPersistedOrder(gpa);
defer gpa.free(rids);
for (rids) |rid| {
const content = (try log.content(gpa, rid)) orelse continue;
defer gpa.free(content);
if (!std.mem.eql(u8, content, body)) continue;
const raw = log.hashOf(rid);
const hex = try gpa.alloc(u8, raw.len * 2);
const lower = "0123456789abcdef";
for (raw, 0..) |b, i| {
hex[i * 2] = lower[b >> 4];
hex[i * 2 + 1] = lower[b & 0x0f];
}
return hex;
}
return error.NoArtifactHoldsThoseBytes;
}
/// Reads `dir/.zit` whole (caller frees) — the byte-identical oracle a refused
/// destructive run must satisfy. A byte comparison, not a size comparison: a run
/// that appended a tombstone and then rewrote back to the same length would pass
/// the latter.
pub fn storeBytesAlloc(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) ![]u8 {
var file = try dir.openFile(io, ".zit", .{});
defer file.close(io);
const st = try file.stat(io);
const buf = try gpa.alloc(u8, @intCast(st.size));
errdefer gpa.free(buf);
const n = try file.readPositionalAll(io, buf, 0);
try std.testing.expectEqual(buf.len, n);
return buf;
}
/// Whether `dir/.zit` still holds an artifact named by the hex `name` — the
/// on-disk oracle for "the bytes are gone", read through the library.
pub fn storeHoldsArtifact(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, name: []const u8) !bool {
var file = try dir.openFile(io, ".zit", .{});
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.openReadOnly(gpa, fs.storageFile());
defer log.deinit(gpa);
return (try log.resolveHexPrefix(name)) != null;
}
/// The number of rows in `dir/.zit`'s shun table.
pub fn storeShunRowCount(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !usize {
var file = try dir.openFile(io, ".zit", .{});
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.openReadOnly(gpa, fs.storageFile());
defer log.deinit(gpa);
return log.shunList().len;
}
pub const doomed_body = "content an operator was ordered to remove\n";
pub const kept_body = "content with nothing against it\n";
/// Two-file repository, committed once. Returns the root check-in's 64-hex hash
/// (caller frees). Two files so a shun that removed everything cannot pass by
/// accident.
pub fn shunFixture(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) ![]const u8 {
try initRepo(gpa, io, dir);
try writeFile(io, dir, "doomed.txt", doomed_body);
try writeFile(io, dir, "keep.txt", kept_body);
try addManaged(gpa, io, dir, &.{ "doomed.txt", "keep.txt" });
return commitHashDup(gpa, io, dir, "root");
}
// --- shun on a bare store (-R) ----------------------------------------------
//
// The forge path. A forge holds bare stores and addresses them with -R; purge
// at the end of its moderation ladder IS shun, so every property the checkout
// verb has must hold with no working tree: the table, the rewrite, the clone
// carriage, the locks, and a per-hash record of what happened.
/// The artifact hash a machine verb printed: the last space-separated field of
/// its one stdout line (`wiki commit` prints only the hash, `technote add`
/// prints "<event-id> <hash>").
pub fn machineHash(stdout: []const u8) []const u8 {
const line = std.mem.trimEnd(u8, stdout, "\n");
const at = if (std.mem.lastIndexOfScalar(u8, line, ' ')) |sp| sp + 1 else 0;
return line[at..];
}
// --- pull with a repository shun table (sync §3.3 L553-556) ----------------
//
// The durable end of the phantom-scan shun filter, through the REAL binary: a
// repository that has shunned a hash the peer holds must not fetch it back, and
// must not fail for declining to. §3.3 filters the phantom SCAN, not phantom
// creation, so B still mints the phantom from A's `igot` — it simply never asks,
// on this pull or any later one.
//
// The shun rows are written straight into B's log through the library (the `zit
// shun` verb is a separate slice); everything else is the real CLI over loopback
// HTTP, so this is the guard that the built shun set actually reaches the pull
// client. Replace `shun.shunSet()` with `.none` at `pullRun`'s call site and this
// test fails.
/// Add one shun row naming `digest` to `dir/.zit` (sync §3.3 L544-545 — keyed by
/// the complete artifact name, so the whole digest, never a prefix).
pub fn shunDigestInRepo(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, digest: []const u8) !void {
var file = try dir.openFile(io, ".zit", .{ .mode = .read_write });
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
// `open` re-reads the stored policy from the header, so the kind passed here
// is only the empty-file seed value and never overrides B's own.
var log = try logstore.LogStore.open(gpa, fs.storageFile(), @backingInt(artifact.defaultPolicy(false, false)));
defer log.deinit(gpa);
try log.putShun(digest, "e2e operator request", 1);
try log.commit();
}
/// Three check-ins over src/ and tools/; returns the hashes of the first two.
/// tools/t reads "t1", "t2", "t3" at each.
pub fn seedHorizonConeOrigin(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !struct { h1: []const u8, h2: []const u8 } {
try initRepo(gpa, io, dir);
try dir.createDir(io, "src", .default_dir);
try dir.createDir(io, "tools", .default_dir);
try writeFile(io, dir, "src/a", "s1\n");
try writeFile(io, dir, "tools/t", "t1\n");
try addManaged(gpa, io, dir, &.{ "src/a", "tools/t" });
// Distinct times: on a tie the origin names the lowest hash, which need
// not be the tip.
const h1 = try commitAtDup(gpa, io, dir, "one", "1700000001");
errdefer gpa.free(h1);
try writeFile(io, dir, "src/a", "s2\n");
try writeFile(io, dir, "tools/t", "t2\n");
const h2 = try commitAtDup(gpa, io, dir, "two", "1700000002");
errdefer gpa.free(h2);
try writeFile(io, dir, "src/a", "s3\n");
try writeFile(io, dir, "tools/t", "t3\n");
const h3 = try commitAtDup(gpa, io, dir, "three", "1700000003");
gpa.free(h3);
return .{ .h1 = h1, .h2 = h2 };
}
/// A hand-built sub-manifest tree in a served store, holding only what a
/// sparse-cone section 6.2 server sends for one cone: the spine, the nodes
/// whose slots may reach the cone, and their blobs. zit's own server sends
/// every node it holds to a cone without a horizon, so leaving a node out of
/// its store is how these tests stand in for a server that walks the cone.
/// Blob content is the path.
pub const ConeTreeStore = struct {
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
names: std.ArrayList([]u8) = .empty,
/// Hold every artifact whatever `held` says: a dense origin, which must
/// itself withhold what such a server would not send.
dense: bool = false,
pub fn deinit(self: *ConeTreeStore) void {
for (self.names.items) |n| self.gpa.free(n);
self.names.deinit(self.gpa);
}
/// Name `bytes` and, when `held`, append them to the store.
pub fn add(self: *ConeTreeStore, bytes: []const u8, held: bool) ![]const u8 {
const name = if (held or self.dense) try appendArtifact(self.gpa, self.io, self.dir, bytes) else try sha3HexAlloc(self.gpa, bytes);
errdefer self.gpa.free(name);
try self.names.append(self.gpa, name);
return name;
}
/// `head`, one `letter` card per (name, hash) pair, `tail`, sealed.
pub fn node(self: *ConeTreeStore, head: []const u8, letter: u8, cards: []const [2][]const u8, tail: []const u8, held: bool) ![]const u8 {
var body: std.ArrayList(u8) = .empty;
defer body.deinit(self.gpa);
try body.appendSlice(self.gpa, head);
for (cards) |c| try body.print(self.gpa, "{c} {s} {s}\n", .{ letter, c[0], c[1] });
try body.appendSlice(self.gpa, tail);
const sealed = try withZCard(self.gpa, body.items);
defer self.gpa.free(sealed);
return self.add(sealed, held);
}
pub fn leaf(self: *ConeTreeStore, paths: []const []const u8, held: bool) ![]const u8 {
var cards: [4][2][]const u8 = undefined;
for (paths, 0..) |p, i| cards[i] = .{ p, try self.add(p, held) };
return self.node("", 'F', cards[0..paths.len], "", held);
}
pub fn checkin(self: *ConeTreeStore, head: []const u8, cards: []const [2][]const u8) ![]const u8 {
return self.node(head, 'S', cards, "T *branch * trunk\nT *sym-trunk *\nU e2e\n", true);
}
};
/// The tree under cone {lib, src} minus src/gen, two levels deep. `omit`
/// says which nodes the server lacks: the four a section 6.2 server never
/// sends -- wholly before the cone, out only by the bound its parent's next
/// card sets, wholly inside the exclusion, wholly after -- and, when
/// `omit_straddler`, the leaf straddling the cone boundary, which it must send.
pub const ConeTree = struct {
docs: []const u8,
low: []const u8,
straddle: []const u8,
gen: []const u8,
gen_tail: []const u8,
tip: []const u8,
};
pub fn buildConeTree(t: *ConeTreeStore, omit_straddler: bool) !ConeTree {
const docs = try t.leaf(&.{ "docs/a", "docs/b" }, false);
// Starts below `lib`: a slot ending at `lib/a` could still hold the path
// `lib` itself, so the slot before it must end at or below `lib`.
const lib = try t.leaf(&.{ "kit/a", "lib/a" }, true);
const mid = try t.leaf(&.{ "m/a", "n/a" }, false);
const straddle = try t.leaf(&.{ "rc/x", "src/a" }, !omit_straddler);
const gen = try t.leaf(&.{ "src/gen/a", "src/gen/b" }, false);
const gen_tail = try t.leaf(&.{ "src/gen/c", "src/gen/d" }, true);
const src = try t.leaf(&.{ "src/h", "src/z" }, true);
const zzz = try t.leaf(&.{"zzz/a"}, false);
const low = try t.node("", 'S', &.{ .{ "docs/a", docs }, .{ "kit/a", lib }, .{ "m/a", mid } }, "", true);
const src_inner = try t.node("", 'S', &.{ .{ "rc/x", straddle }, .{ "src/gen/a", gen }, .{ "src/gen/c", gen_tail }, .{ "src/h", src } }, "", true);
const high = try t.node("", 'S', &.{.{ "zzz/a", zzz }}, "", false);
const tip = try t.checkin("C one\nD 2026-10-21T12:00:00.000\n", &.{ .{ "docs/a", low }, .{ "rc/x", src_inner }, .{ "zzz/a", high } });
return .{ .docs = docs, .low = low, .straddle = straddle, .gen = gen, .gen_tail = gen_tail, .tip = tip };
}
/// A child of `tree.tip` adding `src/y`, whose new out-of-cone node the
/// server again lacks.
pub fn addConeTreeChild(t: *ConeTreeStore, tree: ConeTree) ![]const u8 {
const src2 = try t.leaf(&.{ "src/h", "src/y", "src/z" }, true);
const inner2 = try t.node("", 'S', &.{ .{ "rc/x", tree.straddle }, .{ "src/gen/a", tree.gen }, .{ "src/gen/c", tree.gen_tail }, .{ "src/h", src2 } }, "", true);
const zzz2 = try t.leaf(&.{ "zzz/a", "zzz/b" }, false);
const high2 = try t.node("", 'S', &.{.{ "zzz/a", zzz2 }}, "", false);
const head2 = try std.fmt.allocPrint(t.gpa, "C two\nD 2026-10-21T12:00:01.000\nP {s}\n", .{tree.tip});
defer t.gpa.free(head2);
return t.checkin(head2, &.{ .{ "docs/a", tree.low }, .{ "rc/x", inner2 }, .{ "zzz/a", high2 } });
}
pub const cone_tree_flags = [_][]const u8{ "--cone=lib", "--cone=src", "--cone-not=src/gen" };
pub fn coneTreeServer(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !struct { child: std.process.Child, url: []u8 } {
const port = try freshLoopbackPort(io);
var child = try spawnServe(gpa, io, dir, port);
errdefer child.kill(io);
try waitForListen(io, port);
return .{ .child = child, .url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{port}) };
}
/// Parse the `cursor CURSOR TOKEN` trailer off a frames invocation's stdout,
/// returning gpa-owned copies. The trailer is the final line by contract.
pub fn framesTrailer(gpa: std.mem.Allocator, stdout: []const u8) !struct { cursor: []const u8, token: []const u8 } {
var last: []const u8 = "";
var it = std.mem.splitScalar(u8, stdout, '\n');
while (it.next()) |line| {
if (std.mem.startsWith(u8, line, "cursor ")) last = line;
}
try std.testing.expect(last.len > 0);
var fields = std.mem.splitScalar(u8, last, ' ');
_ = fields.next(); // "cursor"
const cursor = fields.next() orelse return error.TestUnexpectedResult;
const token = fields.next() orelse return error.TestUnexpectedResult;
try std.testing.expect(fields.next() == null);
return .{ .cursor = try gpa.dupe(u8, cursor), .token = try gpa.dupe(u8, token) };
}
/// Hold the generation-rewrite lock from the test process, standing in for a
/// concurrent `pack` or `shun --force`.
pub fn holdRewriteLock(io: std.Io, dir: std.Io.Dir) !zit.storelock.Lock {
var sink = std.Io.Writer.Discarding.init(&.{});
return zit.storelock.acquire(io, dir, ".zit-tmp-pack.lock", 0, &sink.writer);
}
pub fn worktreeStorm(io: std.Io, dir: std.Io.Dir, tag: []const u8, count: usize, failures: *std.atomic.Value(u32)) void {
const gpa = std.heap.page_allocator;
var i: usize = 0;
while (i < count) : (i += 1) {
var name_buf: [64]u8 = undefined;
const name = std.fmt.bufPrint(&name_buf, "{s}-{d}", .{ tag, i }) catch unreachable;
writeFile(io, dir, name, name) catch {
_ = failures.fetchAdd(1, .monotonic);
return;
};
var add = runZit(gpa, io, dir, &.{ "add", name }) catch {
_ = failures.fetchAdd(1, .monotonic);
return;
};
freeResult(gpa, &add);
var res = runCommitEnv(gpa, io, dir, .{ .user = "storm" }, &.{ "commit", "-m", name, "--allow-fork", "--no-sync", "-q" }) catch {
_ = failures.fetchAdd(1, .monotonic);
return;
};
defer freeResult(gpa, &res);
switch (res.term) {
.exited => |c| if (c != 0) {
_ = failures.fetchAdd(1, .monotonic);
},
else => _ = failures.fetchAdd(1, .monotonic),
}
}
}
/// Deterministic stand-ins for the signer, honoring FOSSIL's calling
/// convention (`COMMAND OUTFILE INFILE`), which is what zit invokes: the
/// stub's own `$0`/`$1` are those two paths. No gpg, no keyring, no expiry.
pub const stub_signer = "sh -c 'printf -- \"-----BEGIN PGP SIGNED MESSAGE-----\\nHash: SHA256\\n\\n\" > \"$0\"; " ++
"cat \"$1\" >> \"$0\"; printf -- \"-----BEGIN PGP SIGNATURE-----\\nstubsig\\n-----END PGP SIGNATURE-----\\n\" >> \"$0\"'";
/// The SSH family: fossil detects a `ssh*` basename, passes only INFILE, and
/// expects the DETACHED signature at INFILE.sig — this stub writes exactly
/// that, so the armor assembly under test is zit's, as it is in fossil.
pub const ssh_stub_signer = "ssh_stub=1 sh -c 'printf -- \"-----BEGIN SSH SIGNATURE-----\\nstubsig\\n-----END SSH SIGNATURE-----\\n\" > \"$0.sig\"'";
// --- landing mergeability probe --------------------------------------------
//
// A forge must be able to ask "would this land cleanly?" WITHOUT attempting
// the merge, because a server that attempts one can end up holding a conflict.
// These pin the two properties that make the answer worth having: it agrees
// with the real merge engine, and it writes nothing.
/// Seed trunk + a branch whose edit is in a different region of the same file,
/// which a three-way merge resolves cleanly. This is the case a HASH-level
/// resolver would wrongly call a conflict, so it is the sharpest probe test.
pub fn seedContentMergeable(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !void {
try initRepo(gpa, io, dir);
var root_c: std.process.RunResult = undefined;
defer freeResult(gpa, &root_c);
_ = try commitFile(gpa, io, dir, "f.txt", "top\nMID\nbottom\n", "root", &root_c);
try writeFile(io, dir, "f.txt", "top\nMID\nBOTTOM-CHANGED\n");
var br = try runCommitEnv(gpa, io, dir, .{ .user = "u" }, &.{ "commit", "-m", "branch", "--branch", "feat/x" });
defer freeResult(gpa, &br);
try expectExit(br.term, 0);
var back = try runZit(gpa, io, dir, &.{ "update", "trunk" });
defer freeResult(gpa, &back);
try expectExit(back.term, 0);
try writeFile(io, dir, "f.txt", "TOP-CHANGED\nMID\nbottom\n");
var tr = try runCommitEnv(gpa, io, dir, .{ .user = "u" }, &.{ "commit", "-m", "trunk" });
defer freeResult(gpa, &tr);
try expectExit(tr.term, 0);
}
// --- landing accept against a bare store ------------------------------------
//
// The forge path. These pin the RFC rule the design rests on and the two
// properties a server must have: it never holds a half-finished merge, and it
// never mints an artifact a dense checkout would have minted differently.
/// Copy a store into a directory holding nothing else -- no checkout, no
/// worktree. What quarry serves.
pub fn bareStoreOf(gpa: std.mem.Allocator, io: std.Io, src: std.Io.Dir, dst: std.Io.Dir, name: []const u8) !void {
const bytes = try src.readFileAlloc(io, ".zit", gpa, .limited(64 * 1024 * 1024));
defer gpa.free(bytes);
try dst.createDir(io, name, .default_dir);
var d = try dst.openDir(io, name, .{});
defer d.close(io);
try writeFile(io, d, ".zit", bytes);
}
// --- the N card, per type ---------------------------------------------------
//
// artifact-format §4.7.12 gives the READ rule: on wiki, technote and forum
// artifacts the N card names the W body's mimetype, "defaulting to
// text/x-fossil-wiki when omitted". That is not the same rule as when the card
// is WRITTEN, and the draft does not state the write rule at all -- so these
// pin it against fossil's own source, which diverges BY TYPE:
//
// wiki (wiki.c:2084) write N only if present, non-empty, and != default
// forum (forum.c:1784) same
// technote (event.c:284) write N whenever present and non-empty -- NO
// default-suppression
//
// The divergence is fossil's. Tidying it into one rule would mint different
// bytes, and so different artifact ids, than fossil produces from identical
// input -- which is the entire compatibility claim.
pub fn nCardOf(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, hash: []const u8) !?[]u8 {
var art = try runZit(gpa, io, dir, &.{ "artifact", "--raw", hash });
defer freeResult(gpa, &art);
var it = std.mem.splitScalar(u8, art.stdout, '\n');
while (it.next()) |line| {
if (std.mem.startsWith(u8, line, "N ")) return try gpa.dupe(u8, line);
}
return null;
}
/// The first line of `zit info NAME`: the check-in's hash.
pub fn tipHashOf(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, name: []const u8) ![]u8 {
var info = try runZit(gpa, io, dir, &.{ "info", name });
defer freeResult(gpa, &info);
try expectExitDiag(info.term, 0, info);
const nl = std.mem.indexOfScalar(u8, info.stdout, '\n') orelse info.stdout.len;
return gpa.dupe(u8, info.stdout[0..nl]);
}
/// The `ZIT_MANIFEST_READS` count a verb reports, or null when it did not
/// report one. The seam prints on the last line of stderr.
pub fn manifestReadsOf(res: *const std.process.RunResult) ?u64 {
const marker = "manifest-reads: ";
const at = std.mem.lastIndexOf(u8, res.stderr, marker) orelse return null;
const rest = res.stderr[at + marker.len ..];
const end = std.mem.indexOfScalar(u8, rest, '\n') orelse rest.len;
return std.fmt.parseInt(u64, std.mem.trim(u8, rest[0..end], " \r"), 10) catch null;
}
/// The `fileindex-walk: N` the ZIT_FILEINDEX_WALK seam prints on stderr.
pub fn fileIndexWalkOf(res: *const std.process.RunResult) ?u64 {
const marker = "fileindex-walk: ";
const at = std.mem.lastIndexOf(u8, res.stderr, marker) orelse return null;
const rest = res.stderr[at + marker.len ..];
const end = std.mem.indexOfScalar(u8, rest, '\n') orelse rest.len;
return std.fmt.parseInt(u64, std.mem.trim(u8, rest[0..end], " \r"), 10) catch null;
}
/// Initialises a repository whose commits mint TREE check-ins: three files
/// under three top-level directories with `submanifest enable --k 1`, which
/// is three partition groups. Submanifests §6.2 step 2 mints flat below two
/// groups, so two files at the root — the obvious small fixture — never
/// produce a tree and prove nothing. Returns the seed check-in's hash.
pub fn initTreeRepo(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) ![]const u8 {
try initRepo(gpa, io, dir);
{
var en = try runZit(gpa, io, dir, &.{ "submanifest", "enable", "--k", "1" });
defer freeResult(gpa, &en);
try expectExitDiag(en.term, 0, en);
}
for ([_][]const u8{ "a", "b", "c" }) |d| try dir.createDirPath(io, d);
try writeFile(io, dir, "a/x.txt", "x1\n");
try writeFile(io, dir, "b/y.txt", "y1\n");
try writeFile(io, dir, "c/z.txt", "z1\n");
try addManaged(gpa, io, dir, &.{ "a/x.txt", "b/y.txt", "c/z.txt" });
return commitHashDup(gpa, io, dir, "seed");
}
/// The `fileindex-unknown: N` the ZIT_FILEINDEX_WALK seam prints on stderr:
/// how many check-ins the adopted index could not answer for.
pub fn fileIndexUnknownOf(res: *const std.process.RunResult) ?u64 {
const marker = "fileindex-unknown: ";
const at = std.mem.lastIndexOf(u8, res.stderr, marker) orelse return null;
const rest = res.stderr[at + marker.len ..];
const end = std.mem.indexOfScalar(u8, rest, '\n') orelse rest.len;
return std.fmt.parseInt(u64, std.mem.trim(u8, rest[0..end], " \r"), 10) catch null;
}
/// The parent-level fixture: root files, `src`'s own file, the cone
/// subtree `src/foo`, a sibling subtree `src/bar`, and `docs`. Two
/// check-ins: the second edits a root file, `src`'s file and the sibling.
pub const parents_fixture_files = [_][]const u8{
"README", "build.zig", "src/main.zig", "src/foo/a.zig", "src/foo/deep/b.zig", "src/bar/x.zig", "docs/d.txt",
};
/// Which of `parents_fixture_files` `--cone=src/foo --cone-parents` keeps.
pub const parents_fixture_kept = [_]bool{ true, true, true, true, true, false, false };
pub fn parentsFixture(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !struct { c1: []const u8, c2: []const u8 } {
try initRepo(gpa, io, dir);
for ([_][]const u8{ "src", "src/foo", "src/foo/deep", "src/bar", "docs" }) |d| try dir.createDir(io, d, .default_dir);
for (parents_fixture_files) |f| try writeFile(io, dir, f, f);
try addManaged(gpa, io, dir, &parents_fixture_files);
const c1 = try commitHashDup(gpa, io, dir, "base");
errdefer gpa.free(c1);
try writeFile(io, dir, "README", "README two\n");
try writeFile(io, dir, "src/main.zig", "src/main.zig two\n");
try writeFile(io, dir, "src/bar/x.zig", "src/bar/x.zig two\n");
const c2 = try commitHashDup(gpa, io, dir, "edit");
return .{ .c1 = c1, .c2 = c2 };
}
pub fn expectParentsCheckout(io: std.Io, dir: std.Io.Dir) !void {
for (parents_fixture_files, parents_fixture_kept) |f, kept| {
if (fileExists(io, dir, f) != kept) {
std.debug.print("\n{s}: expected {s}\n", .{ f, if (kept) "present" else "absent" });
return error.TestUnexpectedResult;
}
}
}
// ===========================================================================
// Renames. `zit mv` + commit records `F NEW HASH w OLD`; log PATH, annotate
// and crosslink follow it, keyed by the check-in that recorded it.
// ===========================================================================
/// `zit <argv>` in `dir` with `extra` in the environment, exit 0 asserted;
/// stdout, owned by `arena`.
pub fn renameRun(arena: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, extra: []const [2][]const u8, argv: []const []const u8) ![]const u8 {
const res = try runZitEnv(arena, io, dir, extra, argv);
try expectExitDiag(res.term, 0, res);
return res.stdout;
}
pub const no_index = [_][2][]const u8{.{ "ZIT_NO_FILE_INDEX", "1" }};
/// `zit artifact NAME --raw` prints bytes that hash back to NAME.
pub fn expectArtifactReadsBack(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, hash: []const u8) !void {
var res = try runZit(gpa, io, dir, &.{ "artifact", hash, "--raw" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
var name_buf: [64]u8 = undefined;
try std.testing.expectEqualStrings(hash, artifact.artifactName(.sha3_256, res.stdout, &name_buf));
}
pub fn expectVerifies(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !void {
var res = try runZit(gpa, io, dir, &.{"verify"});
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
}
/// Bytes a sync run moved on the wire, from its `ZIT_SYNC_STATS` line.
pub const WireBytes = struct { sent: u64, received: u64 };
/// `zit serve` on `served` for the length of one `zit VERB URL` run in
/// `client`; returns what the client sent and received.
pub fn exchangeWith(gpa: std.mem.Allocator, io: std.Io, served: std.Io.Dir, client: std.Io.Dir, verb: []const u8) !WireBytes {
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, served, port);
defer server.kill(io);
try waitForListen(io, port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{port});
defer gpa.free(url);
var res = try runZitEnv(gpa, io, client, &.{.{ "ZIT_SYNC_STATS", "1" }}, &.{ verb, url });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
const at = std.mem.indexOf(u8, res.stderr, "sync-stats: ") orelse return error.TestUnexpectedResult;
var it = std.mem.tokenizeAny(u8, res.stderr[at..], " ,;\n");
var got: WireBytes = .{ .sent = 0, .received = 0 };
var prev: []const u8 = "";
while (it.next()) |tok| {
if (std.mem.eql(u8, prev, "sent")) got.sent = try std.fmt.parseInt(u64, tok, 10);
if (std.mem.eql(u8, prev, "received")) {
got.received = try std.fmt.parseInt(u64, tok, 10);
break;
}
prev = tok;
}
return got;
}
// --- purge artifacts: thinning a file's history ------------------------------
//
// Removing chosen old versions of a file while every check-in stays: the
// versions go to the graveyard and the thinned list, the readers name the
// removal (exit 8) instead of calling it damage or showing an emptied file,
// and sync does not bring them back.
/// Version `i` (1-based) of the thinned file: long enough that a later version
/// is stored as a delta of an earlier one, so removing a middle version
/// exercises the rewrite's re-encoding of the versions that delta against it.
pub fn thinBody(gpa: std.mem.Allocator, i: usize) ![]u8 {
var out: std.ArrayList(u8) = .empty;
errdefer out.deinit(gpa);
for (0..200) |j| try out.print(gpa, "line {d} of a long note\n", .{j});
try out.print(gpa, "version {d}\n", .{i});
return out.toOwnedSlice(gpa);
}
/// Five check-ins, each a new version of n.md. `checkins[i]` and `blobs[i]`
/// are version i+1's check-in and blob name; the caller frees both.
pub const ThinHistory = struct {
checkins: [5][]u8,
blobs: [5][64]u8,
pub fn deinit(self: *ThinHistory, gpa: std.mem.Allocator) void {
for (self.checkins) |c| gpa.free(c);
}
};
pub fn thinFixture(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, init_argv: []const []const u8) !ThinHistory {
return thinFixtureWide(gpa, io, dir, init_argv, false);
}
/// `wide` adds 36 files in six directories, enough for `--submanifest 3` to
/// split each check-in into sub-manifest nodes.
pub fn thinFixtureWide(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, init_argv: []const []const u8, wide: bool) !ThinHistory {
var init = try runZit(gpa, io, dir, init_argv);
defer freeResult(gpa, &init);
try expectExitDiag(init.term, 0, init);
// A second file every check-in carries unchanged, so a tree check-in has
// more than one leaf entry to search past.
try writeFile(io, dir, "keep.txt", "unchanged\n");
if (wide) for (0..6) |d| {
const sub = try std.fmt.allocPrint(gpa, "d{d}", .{d});
defer gpa.free(sub);
try dir.createDir(io, sub, .default_dir);
for (0..6) |f| {
const name = try std.fmt.allocPrint(gpa, "d{d}/f{d}", .{ d, f });
defer gpa.free(name);
try writeFile(io, dir, name, name);
}
};
var h: ThinHistory = undefined;
var made: usize = 0;
errdefer for (h.checkins[0..made]) |c| gpa.free(c);
for (0..5) |i| {
const body = try thinBody(gpa, i + 1);
defer gpa.free(body);
try writeFile(io, dir, "n.md", body);
if (i == 0 and wide) {
var add = try runZit(gpa, io, dir, &.{"addremove"});
defer freeResult(gpa, &add);
try expectExitDiag(add.term, 0, add);
} else if (i == 0) try addManaged(gpa, io, dir, &.{ "n.md", "keep.txt" });
var c = try runCommitEnv(gpa, io, dir, .{ .user = "u" }, &.{ "commit", "-m", "a version" });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
h.checkins[i] = try tipHashOf(gpa, io, dir, "trunk");
made += 1;
var d: [32]u8 = undefined;
std.crypto.hash.sha3.Sha3_256.hash(body, &d, .{});
h.blobs[i] = std.fmt.bytesToHex(d, .lower);
}
return h;
}
pub fn expectContains(hay: []const u8, needle: []const u8) !void {
if (std.mem.indexOf(u8, hay, needle) == null) {
std.debug.print("\nexpected to find \"{s}\" in:\n{s}\n", .{ needle, hay });
return error.TestExpectedSubstring;
}
}
// ---------------------------------------------------------------------------
// Signed erasures carried by sync. A is the owner's device, BOX a `zit serve`,
// B a second device and C an old clone that was offline while the erasure
// moved. The owner key is a fixed seed handed in per call through
// `$ZIT_ERASURE_KEY`, so no key file is involved unless a test says so.
// ---------------------------------------------------------------------------
pub const erasure_seed_hex = "1111111111111111111111111111111111111111111111111111111111111111";
pub fn erasurePublicHex() [64]u8 {
const seed = zit.erasure.parseKeyHex(32, erasure_seed_hex).?;
return zit.erasure.keyHex(zit.erasure.publicKeyOf(seed) catch unreachable);
}
pub const ErasureEnv = struct {
/// `SOURCE_DATE_EPOCH`: the CLI's clock, signing and not-before alike.
epoch: ?[]const u8 = null,
/// `$ZIT_ERASURE_KEY`.
key: ?[]const u8 = null,
/// `$XDG_CONFIG_HOME`, for the key file.
xdg: ?[]const u8 = null,
};
pub fn runErasureZit(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, e: ErasureEnv, tail: []const []const u8) !std.process.RunResult {
var env = std.process.Environ.Map.init(gpa);
defer env.deinit();
try env.put("TZ", "UTC");
try env.put("HOME", "/zit-e2e-isolated-home-does-not-exist");
try env.put("PATH", "/usr/bin:/bin");
try env.put("USER", "e2e-user");
if (e.epoch) |v| try env.put("SOURCE_DATE_EPOCH", v);
if (e.key) |v| try env.put("ZIT_ERASURE_KEY", v);
if (e.xdg) |v| try env.put("XDG_CONFIG_HOME", v);
const cwd_abs = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(cwd_abs);
const abs_exe = try std.fs.path.resolve(gpa, &.{ cwd_abs, zit_exe });
defer gpa.free(abs_exe);
var argv_buf: [16][]const u8 = undefined;
argv_buf[0] = abs_exe;
std.debug.assert(tail.len <= argv_buf.len - 1);
for (tail, argv_buf[1..][0..tail.len]) |src, *dst| dst.* = src;
return std.process.run(gpa, io, .{ .argv = argv_buf[0 .. 1 + tail.len], .cwd = .{ .dir = dir }, .environ_map = &env });
}
pub fn erasureOk(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, e: ErasureEnv, tail: []const []const u8) !void {
var r = try runErasureZit(gpa, io, dir, e, tail);
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
/// Origin A in `tmp/a`: two files over two check-ins, the owner key pinned.
/// Returns the blob names of secret.txt and other.txt (caller frees).
pub fn erasureOrigin(gpa: std.mem.Allocator, io: std.Io, root: std.Io.Dir) !struct { secret: []u8, other: []u8 } {
try root.createDir(io, "a", .default_dir);
var a = try root.openDir(io, "a", .{});
defer a.close(io);
try initRepo(gpa, io, a);
try writeFile(io, a, "secret.txt", "content the owner erases\n");
try writeFile(io, a, "other.txt", "content erased and then cancelled\n");
try writeFile(io, a, "keep.txt", "content nothing touches\n");
try addManaged(gpa, io, a, &.{ "secret.txt", "other.txt", "keep.txt" });
gpa.free(try commitHashDup(gpa, io, a, "seed"));
try writeFile(io, a, "keep.txt", "content nothing touches, edited\n");
gpa.free(try commitHashDup(gpa, io, a, "edit"));
const pub_hex = erasurePublicHex();
try erasureOk(gpa, io, a, .{}, &.{ "shun", "key", "pin", &pub_hex });
const secret = try artifactNameByContent(gpa, io, a, "content the owner erases\n");
errdefer gpa.free(secret);
return .{ .secret = secret, .other = try artifactNameByContent(gpa, io, a, "content erased and then cancelled\n") };
}
/// Serve `dir` and clone it into `root/name` (the server is stopped after).
pub fn erasureCloneOf(gpa: std.mem.Allocator, io: std.Io, root: std.Io.Dir, dir: std.Io.Dir, names: []const []const u8) !void {
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, dir, port);
defer server.kill(io);
try waitForListen(io, port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{port});
defer gpa.free(url);
for (names) |name| try erasureOk(gpa, io, root, .{}, &.{ "clone", url, name });
}