const std = @import("std");
const repeat = @import("zit").repeat;
const test_child = @import("test_child");
const logstore = @import("zit").logstore;
const storage = @import("zit").storage;
const container = @import("zit").container;
const zit = @import("zit");
const net = std.Io.net;
const support = @import("support.zig");
const ConeTreeStore = support.ConeTreeStore;
const PushFixture = support.PushFixture;
const addConeTreeChild = support.addConeTreeChild;
const addManaged = support.addManaged;
const artifactNameByContent = support.artifactNameByContent;
const buildConeTree = support.buildConeTree;
const collectArtifactDigests = support.collectArtifactDigests;
const commitHashDup = support.commitHashDup;
const coneTreeServer = support.coneTreeServer;
const cone_tree_flags = support.cone_tree_flags;
const dialServe = support.dialServe;
const doomed_body = support.doomed_body;
const erasureOk = support.erasureOk;
const erasurePublicHex = support.erasurePublicHex;
const erasure_seed_hex = support.erasure_seed_hex;
const expectContains = support.expectContains;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const expectFileContent = support.expectFileContent;
const expectParentsCheckout = support.expectParentsCheckout;
const expectSameDigestSet = support.expectSameDigestSet;
const fileExists = support.fileExists;
const forceAutoRetune = support.forceAutoRetune;
const freeDigests = support.freeDigests;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const initRepo = support.initRepo;
const parentsFixture = support.parentsFixture;
const readAutoRetune = support.readAutoRetune;
const readFile = support.readFile;
const readProjectCode = support.readProjectCode;
const readSubmanifestSettings = support.readSubmanifestSettings;
const runCommitEnv = support.runCommitEnv;
const runErasureZit = support.runErasureZit;
const runZit = support.runZit;
const runZitEnv = support.runZitEnv;
const seedHorizonConeOrigin = support.seedHorizonConeOrigin;
const shunFixture = support.shunFixture;
const spawnServe = support.spawnServe;
const storeHoldsArtifact = support.storeHoldsArtifact;
const tipHashOf = support.tipHashOf;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;
/// An HTTP gate in front of `zit serve`: every request without an
/// `Authorization` header is answered 401 with a Basic challenge; one with
/// the header is relayed byte for byte to the real server and its reply
/// relayed back. This is the shape of a forge that gates a READ, which
/// fossil's own server never does over HTTP, so no fossil can stand in for
/// it. Each request's Authorization value (or `-`) is appended to a log.
const basic_gate_proxy =
\\import re, socket, sys
\\lport, tport, log = int(sys.argv[1]), int(sys.argv[2]), sys.argv[3]
\\srv = socket.socket()
\\srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
\\srv.bind(("127.0.0.1", lport))
\\srv.listen(16)
\\def recv_http(c):
\\ data = b""
\\ while b"\r\n\r\n" not in data:
\\ chunk = c.recv(65536)
\\ if not chunk:
\\ return data
\\ data += chunk
\\ head, _, body = data.partition(b"\r\n\r\n")
\\ if re.search(rb"expect:\s*100-continue", head, re.I):
\\ c.sendall(b"HTTP/1.1 100 Continue\r\n\r\n")
\\ m = re.search(rb"content-length:\s*(\d+)", head, re.I)
\\ n = int(m.group(1)) if m else 0
\\ while len(body) < n:
\\ chunk = c.recv(65536)
\\ if not chunk:
\\ break
\\ body += chunk
\\ return head + b"\r\n\r\n" + body
\\while True:
\\ c, _ = srv.accept()
\\ req = recv_http(c)
\\ if not req:
\\ c.close()
\\ continue
\\ head = req.split(b"\r\n\r\n", 1)[0]
\\ auth = re.search(rb"^authorization:\s*(.*?)\r?$", head, re.I | re.M)
\\ with open(log, "ab") as f:
\\ f.write((auth.group(1) if auth else b"-") + b"\n")
\\ if not auth:
\\ c.sendall(b"HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: Basic realm=\"gate\"\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")
\\ c.close()
\\ continue
\\ u = socket.create_connection(("127.0.0.1", tport))
\\ u.sendall(req)
\\ while True:
\\ chunk = u.recv(65536)
\\ if not chunk:
\\ break
\\ c.sendall(chunk)
\\ u.close()
\\ c.close()
\\
;
fn spawnBasicGate(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, lport: u16, tport: u16) !std.process.Child {
try writeFile(io, dir, "gate.py", basic_gate_proxy);
var env = std.process.Environ.Map.init(gpa);
defer env.deinit();
try env.put("PATH", "/usr/bin:/bin");
const l = try std.fmt.allocPrint(gpa, "{d}", .{lport});
defer gpa.free(l);
const t = try std.fmt.allocPrint(gpa, "{d}", .{tport});
defer gpa.free(t);
// Through `env`, so `python3` is found on the PATH set above. A bare
// `python3` is looked up on the test process's own PATH, where a version
// manager's shim can come first and fail without the HOME this child
// does not get, leaving the gate unstarted.
return test_child.spawn(io, .{
.argv = &.{ "/usr/bin/env", "python3", "gate.py", l, t, "gate.log" },
.cwd = .{ .dir = dir },
.environ_map = &env,
.stdout = .ignore,
.stderr = .ignore,
});
}
test "cli clone, push: --resolve dials the pinned address for a name that does not resolve" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var fx = try PushFixture.init(gpa, io);
defer fx.deinit(io);
const url = try std.fmt.allocPrint(gpa, "http://zit-pin-test.invalid:{d}/", .{fx.port});
defer gpa.free(url);
const pin = try std.fmt.allocPrint(gpa, "zit-pin-test.invalid:{d}:127.0.0.1", .{fx.port});
defer gpa.free(pin);
// Without the pin the name is looked up, and nothing answers.
{
var r = try runZit(gpa, io, fx.dir, &.{ "push", url, "--once" });
defer freeResult(gpa, &r);
try expectExit(r.term, 1);
}
{
var r = try runZit(gpa, io, fx.dir, &.{ "push", url, "--once", "--resolve", pin });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
var other = std.testing.tmpDir(.{});
defer other.cleanup();
{
var r = try runZit(gpa, io, other.dir, &.{ "clone", url, "c", "--once", "--no-open", "--resolve", pin });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
// A malformed pin is a usage error.
{
var r = try runZit(gpa, io, fx.dir, &.{ "push", url, "--resolve", "zit-pin-test.invalid:1" });
defer freeResult(gpa, &r);
try expectExit(r.term, 2);
try std.testing.expect(std.mem.startsWith(u8, r.stderr, "zit push: invalid --resolve value 'zit-pin-test.invalid:1'"));
}
}
test "cli clone: a clone that cannot connect removes what it made and nothing else" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// A directory the clone created goes with it.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "clone", "http://127.0.0.1:1/", "fresh" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
try expectContains(res.stderr, "cannot connect");
try std.testing.expectError(error.FileNotFound, tmp.dir.access(io, "fresh", .{}));
}
// An existing directory stays, emptied of what the clone wrote; what was
// already there, zit-named or not, is untouched.
try tmp.dir.createDir(io, "empty", .default_dir);
try tmp.dir.createDir(io, "held", .default_dir);
try writeFile(io, tmp.dir, "held/notes.txt", "mine\n");
try writeFile(io, tmp.dir, "held/.zit-ci-comment", "mine too\n");
for ([_][]const u8{ "empty", "held" }) |dst| {
var res = try runZit(gpa, io, tmp.dir, &.{ "clone", "http://127.0.0.1:1/", dst });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
var d = try tmp.dir.openDir(io, dst, .{ .iterate = true });
defer d.close(io);
var names: std.ArrayList(u8) = .empty;
defer names.deinit(gpa);
var it = d.iterate();
while (try it.next(io)) |e| try names.print(gpa, "{s}\n", .{e.name});
if (std.mem.eql(u8, dst, "empty")) {
try std.testing.expectEqualStrings("", names.items);
} else {
try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, names.items, "\n"));
try expectContains(names.items, "notes.txt\n");
try expectContains(names.items, ".zit-ci-comment\n");
}
}
}
test "cli clone: plain http to another host says so on stderr, and loopback does not" {
// Credentials are refused over plaintext http to a remote host, but the
// exchange itself still crosses readable, a private repository's content
// included. `.invalid` never resolves (RFC 2606), so the clone fails to
// connect right after the note, with no server and no network needed.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var remote = try runZit(gpa, io, tmp.dir, &.{ "clone", "http://zit-e2e.invalid/r", "a" });
defer freeResult(gpa, &remote);
try expectExit(remote.term, 1);
try std.testing.expect(std.mem.indexOf(u8, remote.stderr, "http://zit-e2e.invalid/r is unencrypted http") != null);
// Loopback: a closed port fails the same way, and says nothing about http.
const port = try freshLoopbackPort(io);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/xfer", .{port});
defer gpa.free(url);
var local = try runZit(gpa, io, tmp.dir, &.{ "clone", url, "b" });
defer freeResult(gpa, &local);
try expectExit(local.term, 1);
try std.testing.expect(std.mem.indexOf(u8, local.stderr, "unencrypted") == null);
}
test "cli clone: a read gated by an HTTP Basic challenge is answered with the credential, and refused without one by name" {
// A forge in front of a private repository answers a clone's first,
// login-less round with 401 and a Basic challenge. The login card cannot
// answer that round (its secret needs the project code the first reply
// teaches), so the credential never reached the wire and the status was
// fatal. The transport now answers the challenge with the plaintext the
// verb holds and carries the header on every later round, as fossil's
// --httpauth does.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try writeFile(io, tmp_a.dir, "a.txt", "a\n");
try addManaged(gpa, io, tmp_a.dir, &.{"a.txt"});
const tip = try commitHashDup(gpa, io, tmp_a.dir, "seed");
defer gpa.free(tip);
const serve_port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, serve_port);
defer server.kill(io);
try waitForListen(io, serve_port);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
const gate_port = try freshLoopbackPort(io);
var gate = try spawnBasicGate(gpa, io, tmp_b.dir, gate_port, serve_port);
defer gate.kill(io);
try waitForListen(io, gate_port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{gate_port});
defer gpa.free(url);
// Anonymous: refused, and the refusal says what the peer asked for.
{
var r = try runZit(gpa, io, tmp_b.dir, &.{ "clone", url, "anon" });
defer freeResult(gpa, &r);
try expectExit(r.term, 1);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, "wants a login") != null);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, "--user") != null);
}
// Named: the challenge is answered and the clone lands.
{
var r = try runZit(gpa, io, tmp_b.dir, &.{ "clone", url, "repo", "--user", "u", "--password", "p" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
{
var repo = try tmp_b.dir.openDir(io, "repo", .{});
defer repo.close(io);
var r = try runZit(gpa, io, repo, &.{ "timeline", "-n", "1" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try std.testing.expect(std.mem.indexOf(u8, r.stdout, "seed") != null);
}
// The wire, as the gate saw it: the anonymous clone's one request bare;
// the named clone's first request bare and challenged, every request
// after it carrying `Basic base64("u:p")`.
const log = try tmp_b.dir.readFileAlloc(io, "gate.log", gpa, .limited(1 << 16));
defer gpa.free(log);
var lines = std.mem.splitScalar(u8, log, '\n');
try std.testing.expectEqualStrings("-", lines.next().?); // anonymous clone
try std.testing.expectEqualStrings("-", lines.next().?); // named clone, round 0, challenged
var carried: usize = 0;
while (lines.next()) |ln| {
if (ln.len == 0) continue;
try std.testing.expectEqualStrings("Basic dTpw", ln);
carried += 1;
}
try std.testing.expect(carried >= 2); // the retried round 0 and at least one later round
}
test "cli clone: a fresh clone converges to the served repo's artifacts over HTTP (sync §3.7/§8)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Repo A: init + a committed file, so A's log holds real artifacts (the
// file blob and its manifest) — genuine content for the wire to carry.
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try writeFile(io, tmp_a.dir, "hello.txt", "hello from zit over http\n");
try addManaged(gpa, io, tmp_a.dir, &.{"hello.txt"});
var commit = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &commit);
try expectExit(commit.term, 0);
// A's artifact-digest set is the convergence oracle: the RFC + the actual
// committed content decide it, never a second fossil.
const a_digests = try collectArtifactDigests(gpa, io, tmp_a.dir);
defer freeDigests(gpa, a_digests);
try std.testing.expect(a_digests.len >= 1);
// Serve A on a loopback port; wait (bounded) for the listener to accept.
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, port);
defer server.kill(io);
try waitForListen(io, port);
// Clone A into a fresh repo B over real HTTP, targeting the BARE ROOT path
// "/" (sync §4 L815-819 — the client sends the repo URL path unchanged and
// MUST NOT append /xfer; a conformant `fossil clone http://host:port/`
// POSTs to "/"). `zit serve` dispatches sync on method + Content-Type
// regardless of path, so the root URL clones. (The fossil-client-from-root
// case belongs in `zig build smoke`.)
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{port});
defer gpa.free(url);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
try std.testing.expectEqualStrings("", clone.stderr);
// THE PROOF: B exists and its on-disk artifact set equals A's — a real zit
// client store converged to a real zit server's content over the wire
// (sync §3.7). A partial/empty clone fails here; the assertion is not weakened.
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
const b_digests = try collectArtifactDigests(gpa, io, b_dir);
defer freeDigests(gpa, b_digests);
try expectSameDigestSet(a_digests, b_digests);
// §9.1 — B adopted A's project code: the shared identity travels in the
// clone reply's `push` card (§8.2/§8.3) and is persisted into B's config.
// Both are the same real, 40-hex, non-placeholder code (not all zeros).
const a_code = try readProjectCode(gpa, io, tmp_a.dir);
defer gpa.free(a_code);
const b_code = try readProjectCode(gpa, io, b_dir);
defer gpa.free(b_code);
try std.testing.expectEqual(@as(usize, 40), a_code.len);
try std.testing.expect(!std.mem.eql(u8, a_code, repeat("0", 40)));
try std.testing.expectEqualStrings(a_code, b_code);
}
test "cli clone/open: DIR derives from the URL, the clone opens itself, --no-open keeps a bare store, `open URL` clones" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try writeFile(io, tmp_a.dir, "hello.txt", "hello\n");
try addManaged(gpa, io, tmp_a.dir, &.{"hello.txt"});
var commit = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &commit);
try expectExit(commit.term, 0);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, port);
defer server.kill(io);
try waitForListen(io, port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/proj.fossil", .{port});
defer gpa.free(url);
// `zit clone URL` with no DIR: the last URL segment ('.fossil' dropped)
// names the directory, and the clone is followed by an open — the tree
// is materialized and the checkout record exists, without a second verb.
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", url });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
try std.testing.expect(std.mem.indexOf(u8, clone.stdout, "opened ") != null);
var b_dir = try tmp_b.dir.openDir(io, "proj", .{});
defer b_dir.close(io);
try b_dir.access(io, ".zit-checkout", .{});
try b_dir.access(io, "hello.txt", .{});
// --no-open: the store is cloned but no working tree is materialized.
var bare = try runZit(gpa, io, tmp_b.dir, &.{ "clone", url, "bare", "--no-open" });
defer freeResult(gpa, &bare);
try expectExitDiag(bare.term, 0, bare);
try std.testing.expect(std.mem.indexOf(u8, bare.stdout, "opened ") == null);
var bare_dir = try tmp_b.dir.openDir(io, "bare", .{});
defer bare_dir.close(io);
try std.testing.expectError(error.FileNotFound, bare_dir.access(io, "hello.txt", .{}));
// `zit open URL` is the same clone-then-open.
var tmp_c = std.testing.tmpDir(.{});
defer tmp_c.cleanup();
var open = try runZit(gpa, io, tmp_c.dir, &.{ "open", url });
defer freeResult(gpa, &open);
try expectExitDiag(open.term, 0, open);
var c_dir = try tmp_c.dir.openDir(io, "proj", .{});
defer c_dir.close(io);
try c_dir.access(io, ".zit-checkout", .{});
try c_dir.access(io, "hello.txt", .{});
}
test "cli clone: the clone URL is saved as the remote — canonical, password-free, --once opts out, config never crosses the wire" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Repo A carries a sentinel remote of its own: config frames are local
// state, so the sentinel must never appear in a clone's bytes.
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try writeFile(io, tmp_a.dir, "hello.txt", "remote-saving clone\n");
try addManaged(gpa, io, tmp_a.dir, &.{"hello.txt"});
var commit = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &commit);
try expectExit(commit.term, 0);
{
var setr = try runZit(gpa, io, tmp_a.dir, &.{ "remote", "http://config-leak-sentinel.example/x" });
defer freeResult(gpa, &setr);
try expectExit(setr.term, 0);
}
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, port);
defer server.kill(io);
try waitForListen(io, port);
// Clone with a password in the URL's userinfo. The connection still lands
// on loopback; the credential exists only to prove it cannot be stored.
const cred_url = try std.fmt.allocPrint(gpa, "http://user:[email protected]:{d}/", .{port});
defer gpa.free(cred_url);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
{
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", cred_url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
}
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
// The saved remote is the canonical form: username kept, password gone.
{
var rem = try runZit(gpa, io, b_dir, &.{"remote"});
defer freeResult(gpa, &rem);
try expectExit(rem.term, 0);
const want = try std.fmt.allocPrint(gpa, "http://[email protected]:{d}/\n", .{port});
defer gpa.free(want);
try std.testing.expectEqualStrings(want, rem.stdout);
}
// Byte-scan, not config read-back: the password and A's sentinel appear
// NOWHERE in the cloned store.
{
const store_bytes = try readFile(gpa, io, b_dir, ".zit");
defer gpa.free(store_bytes);
try std.testing.expect(std.mem.indexOf(u8, store_bytes, "hunter2secret") == null);
try std.testing.expect(std.mem.indexOf(u8, store_bytes, "config-leak-sentinel") == null);
}
// The acceptance headline: the very next pull needs no URL.
{
var pull = try runZit(gpa, io, b_dir, &.{"pull"});
defer freeResult(gpa, &pull);
try expectExitDiag(pull.term, 0, pull);
}
// --once: same clone, nothing saved, pull refuses with the NoRemote text.
var tmp_c = std.testing.tmpDir(.{});
defer tmp_c.cleanup();
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{port});
defer gpa.free(url);
{
var clone = try runZit(gpa, io, tmp_c.dir, &.{ "clone", url, "repo", "--once" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
}
var c_dir = try tmp_c.dir.openDir(io, "repo", .{});
defer c_dir.close(io);
{
var rem = try runZit(gpa, io, c_dir, &.{"remote"});
defer freeResult(gpa, &rem);
try expectExit(rem.term, 0);
try std.testing.expectEqualStrings("no remote set\n", rem.stdout);
}
{
// No URL and no saved remote is a usage error (exit 2), matching the
// earlier behavior of a bare `zit pull` in a remoteless repository.
var pull = try runZit(gpa, io, c_dir, &.{"pull"});
defer freeResult(gpa, &pull);
try expectExit(pull.term, 2);
try std.testing.expect(std.mem.indexOf(u8, pull.stderr, "no repository URL given and no saved remote") != null);
}
}
test "cli clone submanifests §6.3/§8.1: a clone adopts the origin's three project-scope settings" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Repo A: a real check-in, then all three settings moved off their
// defaults. `enable` is the verb surface; `submanifest-auto-retune` has no
// flag yet, so it is written through the config frame (see `forceAutoRetune`).
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try writeFile(io, tmp_a.dir, "hello.txt", "hello from zit over http\n");
try addManaged(gpa, io, tmp_a.dir, &.{"hello.txt"});
var commit = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &commit);
try expectExit(commit.term, 0);
{
var en = try runZit(gpa, io, tmp_a.dir, &.{ "submanifest", "enable", "--k", "3" });
defer freeResult(gpa, &en);
try expectExitDiag(en.term, 0, en);
}
try forceAutoRetune(gpa, io, tmp_a.dir, true);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.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);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
try std.testing.expectEqualStrings("", clone.stderr);
// THE PROOF: B came up with A's settings, not with the `init` defaults —
// §6.3's project scope, over the §9.3 scalar channel, between two real
// repositories. A clone that ignored the config card fails here.
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
const b = try readSubmanifestSettings(gpa, io, b_dir);
try std.testing.expect(b.enabled);
try std.testing.expectEqual(@as(u8, 3), b.k);
try std.testing.expect(try readAutoRetune(gpa, io, b_dir));
// The same three values as the operator sees them in the clone.
{
var show = try runZit(gpa, io, b_dir, &.{ "submanifest", "show" });
defer freeResult(gpa, &show);
try expectExitDiag(show.term, 0, show);
try std.testing.expect(std.mem.indexOf(u8, show.stdout, "enable-submanifests: on") != null);
try std.testing.expect(std.mem.indexOf(u8, show.stdout, "submanifest-k: 3") != null);
try std.testing.expect(std.mem.indexOf(u8, show.stdout, "submanifest-auto-retune: on") != null);
}
// And they are A's values, read back from A rather than restated.
const a = try readSubmanifestSettings(gpa, io, tmp_a.dir);
try std.testing.expectEqual(a.enabled, b.enabled);
try std.testing.expectEqual(a.k, b.k);
try std.testing.expectEqual(
try readAutoRetune(gpa, io, tmp_a.dir),
try readAutoRetune(gpa, io, b_dir),
);
}
test "cli clone submanifests §8.1: cloning a defaulted origin yields a defaulted clone" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Repo A: never configured. The origin's answer is three explicit
// default-valued rows, not silence, so this pins that adopting them leaves
// the clone exactly where `init` put it — off, unpinned, no auto-retune.
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try writeFile(io, tmp_a.dir, "hello.txt", "plain repository\n");
try addManaged(gpa, io, tmp_a.dir, &.{"hello.txt"});
var commit = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &commit);
try expectExit(commit.term, 0);
const a_digests = try collectArtifactDigests(gpa, io, tmp_a.dir);
defer freeDigests(gpa, a_digests);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.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);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
try std.testing.expectEqualStrings("", clone.stderr);
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
const b = try readSubmanifestSettings(gpa, io, b_dir);
try std.testing.expect(!b.enabled);
try std.testing.expect(!b.pinned());
try std.testing.expect(!try readAutoRetune(gpa, io, b_dir));
// The clone is otherwise unchanged: the configuration round costs the
// convergence nothing.
const b_digests = try collectArtifactDigests(gpa, io, b_dir);
defer freeDigests(gpa, b_digests);
try expectSameDigestSet(a_digests, b_digests);
}
/// The stored Git email domains, synced and local, read back through the
/// library.
fn readGitEmailDomains(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !struct { synced: []u8, local: []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);
const synced = try gpa.dupe(u8, oc.config.git_email_domain);
errdefer gpa.free(synced);
return .{ .synced = synced, .local = try gpa.dupe(u8, oc.config.git_email_domain_local) };
}
/// Set the repository's Git email domain in the config frame directly: the
/// verb that sets it, `zit git export --email-domain`, needs a Git binary.
fn forceGitEmailDomain(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, domain: []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);
var oc = (try log.readConfig(gpa)) orelse return error.TestUnexpectedResult;
defer oc.deinit(gpa);
var next = oc.config;
next.git_email_domain = domain;
try log.putConfig(next);
try log.commit();
}
test "cli clone §9.3: the origin's Git email domain comes with the clone" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// An exported commit's ID hashes its author's address, and the address
// is login@DOMAIN: a clone that kept its own domain would export the same
// check-ins under other commit IDs.
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try writeFile(io, tmp_a.dir, "hello.txt", "served repository\n");
try addManaged(gpa, io, tmp_a.dir, &.{"hello.txt"});
var commit = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &commit);
try expectExit(commit.term, 0);
try forceGitEmailDomain(gpa, io, tmp_a.dir, "example.org");
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.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);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
try std.testing.expectEqualStrings("", clone.stderr);
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
const b = try readGitEmailDomains(gpa, io, b_dir);
defer gpa.free(b.synced);
defer gpa.free(b.local);
try std.testing.expectEqualStrings("example.org", b.synced);
try std.testing.expectEqualStrings("", b.local);
}
// ---------------------------------------------------------------------------
// sync §9.5 L2170-2174 — the SEND side of the shun filter, through the REAL
// binary over real loopback HTTP.
//
// "A subsequent read-authorized transfer path requests igot for every
// eligible holding after the Section 3.3 shun, phantom, private, and
// peer-has filters"
//
// `zit shun --force --defer` records the row WITHOUT reclaiming, so the bytes are
// still in the served store: only the send-side filter can keep them off the wire.
// That makes `--defer` the sharper fixture than `--force` alone — a reclaimed
// artifact would be absent for a reason that has nothing to do with §9.5.
//
// THE CLONE TEST BELOW IS NOT §9.5, and says so in one place rather than in its
// title: §9.5's sentence is scoped to the `send-catalog` pragma's igot inventory,
// §3.4 L588-590 hands clone to §8.1-§8.4, and §8.2 L1551-1553 licenses only the
// gap MECHANISM. Withholding a shunned holding from a clone batch is this
// implementation's extension — see the erratum candidate on `LogIndexStore.masked`
// — and the test pins the extension, not a clause.
// ---------------------------------------------------------------------------
/// Every digest in `all` except the one whose lowercase-hex spelling is `drop_hex`
/// — `collectArtifactDigests` yields RAW binary digests while a shun names an
/// artifact by its complete hex name, so the comparison happens in hex. Caller
/// frees the outer slice only; the element slices are borrowed from `all`.
fn digestsExcept(gpa: std.mem.Allocator, all: []const []const u8, drop_hex: []const u8) ![][]const u8 {
var out: std.ArrayListUnmanaged([]const u8) = .empty;
errdefer out.deinit(gpa);
for (all) |d| {
var hex_buf: [2 * 64]u8 = undefined;
if (d.len * 2 <= hex_buf.len) {
const hex = std.fmt.bufPrint(&hex_buf, "{x}", .{d}) catch unreachable;
if (std.mem.eql(u8, hex, drop_hex)) continue;
}
try out.append(gpa, d);
}
return out.toOwnedSlice(gpa);
}
test "cli clone: a deferred-shun artifact is served to nobody — the clone converges without it and without error (extension; erratum candidate vs sync §8.1/§8.2)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
const root = try shunFixture(gpa, io, tmp_a.dir);
defer gpa.free(root);
const doomed = try artifactNameByContent(gpa, io, tmp_a.dir, doomed_body);
defer gpa.free(doomed);
// Record the shun WITHOUT reclaiming: the bytes stay in A's store.
{
var res = try runZit(gpa, io, tmp_a.dir, &.{ "shun", "--force", "--defer", doomed });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
}
// The fixture's premise: masked but STILL STORED, so nothing but the send-side
// filter can keep it off the wire.
try std.testing.expect(try storeHoldsArtifact(gpa, io, tmp_a.dir, doomed));
const a_digests = try collectArtifactDigests(gpa, io, tmp_a.dir);
defer freeDigests(gpa, a_digests);
const want = try digestsExcept(gpa, a_digests, doomed);
defer gpa.free(want);
// The shunned artifact really is one of A's holdings, and A holds more.
try std.testing.expectEqual(a_digests.len - 1, want.len);
try std.testing.expect(want.len >= 1);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.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);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", "--no-open", url, "repo" });
defer freeResult(gpa, &clone);
// ABSENT, not an error: the clone completes clean. A protocol error or a
// dangling delta would be the CORRUPT outcome this filter exists to avoid.
try expectExitDiag(clone.term, 0, clone);
try std.testing.expectEqualStrings("", clone.stderr);
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
const b_digests = try collectArtifactDigests(gpa, io, b_dir);
defer freeDigests(gpa, b_digests);
try expectSameDigestSet(want, b_digests);
// …and stated the other way round, so a set comparison that somehow admitted
// it could not pass: B does not hold the shunned artifact at all.
try std.testing.expect(!try storeHoldsArtifact(gpa, io, b_dir, doomed));
}
test "cli clone --cone: local filtering over the wire — elision counted, verify healthy, convergence sound" {
// Sparse-cone RFC section 6.3 end to end against a cone-ignorant server:
// the client filters locally, persists spine + in-cone content, reports
// the elision count, verifies healthy, materializes the cone — and a
// dense re-clone FROM THE SERVER still sees everything (the sparse clone
// took nothing away from anyone).
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try tmp_a.dir.createDir(io, "src", .default_dir);
try tmp_a.dir.createDir(io, "tools", .default_dir);
try writeFile(io, tmp_a.dir, "src/a.zig", "in-cone content\n");
try writeFile(io, tmp_a.dir, "tools/big.bin", "out-of-cone content that should elide\n");
try addManaged(gpa, io, tmp_a.dir, &.{ "src/a.zig", "tools/big.bin" });
var commit = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &commit);
try expectExit(commit.term, 0);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.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);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", "--no-open", "--cone=src", url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
// Wave 3: the server is cone-aware and filters its reply (RFC 6.2), so
// the CLIENT has nothing left to elide -- the count reads zero while
// verify still reports the store's one elided phantom below. Against a
// cone-IGNORANT server this line reads "(1 elided by the cone; filtered
// locally...)": the client-side belt (RFC 6.3) is what both arms share.
// The aware server acknowledged every reply with `pragma cone-served`,
// so the clone streamed what it sent and filtered nothing itself.
try std.testing.expect(std.mem.indexOf(u8, clone.stdout, "(0 elided by the cone; applied by the peer; nothing filtered locally)") != null);
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
// The sparse store opens at the cone: in-cone file present, elided not.
var open_res = try runZit(gpa, io, b_dir, &.{ "open", "--cone=src" });
defer freeResult(gpa, &open_res);
try expectExitDiag(open_res.term, 0, open_res);
const a_content = try readFile(gpa, io, b_dir, "src/a.zig");
defer gpa.free(a_content);
try std.testing.expectEqualStrings("in-cone content\n", a_content);
try std.testing.expect(!fileExists(io, b_dir, "tools/big.bin"));
// The elided blob's content is genuinely absent: cat refuses rather than
// invents (the store holds the manifest naming it, not its bytes).
var cat_res = try runZit(gpa, io, b_dir, &.{ "cat", "tools/big.bin" });
defer freeResult(gpa, &cat_res);
try expectExit(cat_res.term, 8);
// annotate says the same, and --partial does not bridge the cone.
for ([_][]const []const u8{ &.{ "annotate", "tools/big.bin" }, &.{ "annotate", "--partial", "tools/big.bin" } }) |argv| {
var res = try runZit(gpa, io, b_dir, argv);
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 8, res);
try expectContains(res.stderr, "elided by this store's sparse cone");
try std.testing.expectEqualStrings("", res.stdout);
}
// Convergence untouched: a DENSE clone from the same server still gets
// everything — sparseness was purely local policy.
var tmp_c = std.testing.tmpDir(.{});
defer tmp_c.cleanup();
var dense_clone = try runZit(gpa, io, tmp_c.dir, &.{ "clone", "--no-open", url, "repo" });
defer freeResult(gpa, &dense_clone);
try expectExitDiag(dense_clone.term, 0, dense_clone);
var c_dir = try tmp_c.dir.openDir(io, "repo", .{});
defer c_dir.close(io);
var open_c = try runZit(gpa, io, c_dir, &.{"open"});
defer freeResult(gpa, &open_c);
try expectExitDiag(open_c.term, 0, open_c);
const big = try readFile(gpa, io, c_dir, "tools/big.bin");
defer gpa.free(big);
try std.testing.expectEqualStrings("out-of-cone content that should elide\n", big);
}
/// What a release that predates horizons (0.6.x) admits at open, pinned here
/// rather than read from this build, because the question is what an OLD
/// reader does: it refuses a store whose header version is not 1, or that
/// holds a frame whose kind byte is past `user` (5) -- the fail-closed lever
/// `FrameKind.shun` was designed around. Everything else it reads, an
/// unparseable store cone included, which it takes for dense.
fn preHorizonReaderAdmits(store: []const u8) bool {
const header_len = 64;
const frame_fixed_overhead = 26;
const last_known_kind = 5;
if (store.len < header_len) return false;
if (!std.mem.eql(u8, store[0..4], "zitL")) return false;
if (std.mem.readInt(u16, store[4..6], .little) != 1) return false;
var offset: usize = header_len;
while (offset + 6 <= store.len) {
const payload_len = std.mem.readInt(u32, store[offset..][0..4], .little);
if (store[offset + 4] > last_known_kind) return false;
const hash_len = store[offset + 5];
offset += std.mem.alignForward(usize, frame_fixed_overhead + hash_len + payload_len, 8);
}
return true;
}
/// Admission by the release that predates horizons, of the checkout `sub`:
/// its store, or the derived image beside it. That release validates the
/// image against the frames alone and answers from it without reading the
/// store header, so an image in the format it knows (`ZITCACHE`, format 5)
/// admits it whatever the header says.
fn preHorizonReaderAdmitsStore(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, sub: []const u8) !bool {
const path = try std.fmt.allocPrint(gpa, "{s}/.zit", .{sub});
defer gpa.free(path);
const store = try dir.readFileAlloc(io, path, gpa, .limited(1 << 24));
defer gpa.free(store);
if (preHorizonReaderAdmits(store)) return true;
const image_path = try std.fmt.allocPrint(gpa, "{s}/.zit-cache/repo", .{sub});
defer gpa.free(image_path);
const image = dir.readFileAlloc(io, image_path, gpa, .limited(1 << 24)) catch |e| switch (e) {
error.FileNotFound => return false,
else => return e,
};
defer gpa.free(image);
const pre_horizon_image_format = 5;
return image.len >= 10 and std.mem.eql(u8, image[0..8], "ZITCACHE") and
std.mem.readInt(u16, image[8..10], .little) == pre_horizon_image_format;
}
/// Read the checkout `sub` once, so every derived sidecar exists before the
/// admission check: the misread this guards lives in those sidecars too.
fn warmSidecars(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, sub: []const u8) !void {
var d = try dir.openDir(io, sub, .{});
defer d.close(io);
var tl = try runZit(gpa, io, d, &.{"timeline"});
defer freeResult(gpa, &tl);
try expectExitDiag(tl.term, 0, tl);
}
test "cli clone --horizon: a release that predates horizons refuses every horizon state, and reads every other" {
// Horizon draft section 3.3 versions the horizon cone so that an older
// implementation "refuses rather than misreads" it. The cone cannot do
// that alone: 0.6 reads a store cone it cannot parse as DENSE (the
// sparse-cone loss rule), and over a horizon clone dense is a misread --
// a diff across the horizon prints elided files as empty, leaves and
// branch drop what the seed supplied, and serve hands a downstream zit
// clone an incomplete store that verifies with 0 faults, all exit 0. So
// the store header carries the horizon: every way into a horizon stamps
// it, every way that keeps one (deepen, re-seed, pack) keeps it, and every
// way back out lowers it, while dense and path-cone stores never carry it.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try tmp_a.dir.createDir(io, "src", .default_dir);
try writeFile(io, tmp_a.dir, "src/f.txt", "one\n");
try writeFile(io, tmp_a.dir, "g.txt", "outside the path cone\n");
try addManaged(gpa, io, tmp_a.dir, &.{ "src/f.txt", "g.txt" });
var first: ?[]u8 = null;
defer if (first) |f| gpa.free(f);
for ([_][]const u8{ "one", "two", "three" }, 0..) |msg, i| {
if (i != 0) try writeFile(io, tmp_a.dir, "src/f.txt", msg);
var c = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", msg });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
if (i == 0) first = try tipHashOf(gpa, io, tmp_a.dir, "trunk");
}
const tip = try tipHashOf(gpa, io, tmp_a.dir, "trunk");
defer gpa.free(tip);
const horizon_arg = try std.fmt.allocPrint(gpa, "--horizon={s}", .{tip});
defer gpa.free(horizon_arg);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.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);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
const cases = [_]struct { dir: []const u8, args: []const []const u8, admitted: bool }{
.{ .dir = "dense", .args = &.{}, .admitted = true },
.{ .dir = "path-cone", .args = &.{"--cone=src"}, .admitted = true },
.{ .dir = "content", .args = &.{horizon_arg}, .admitted = false },
.{ .dir = "structural", .args = &.{ horizon_arg, "--structural" }, .admitted = false },
.{ .dir = "cone-and-horizon", .args = &.{ "--cone=src", horizon_arg }, .admitted = false },
};
for (cases) |case| {
errdefer std.debug.print("clone case: {s}\n", .{case.dir});
var argv: std.ArrayList([]const u8) = .empty;
defer argv.deinit(gpa);
try argv.append(gpa, "clone");
try argv.appendSlice(gpa, case.args);
try argv.appendSlice(gpa, &.{ url, case.dir });
var clone = try runZit(gpa, io, tmp_b.dir, argv.items);
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
try warmSidecars(gpa, io, tmp_b.dir, case.dir);
try std.testing.expectEqual(case.admitted, try preHorizonReaderAdmitsStore(gpa, io, tmp_b.dir, case.dir));
}
// The dense clone's header is byte-for-byte a store's that never met a
// horizon: the stamp is the horizon's alone.
{
const dense = try tmp_b.dir.readFileAlloc(io, "dense/.zit", gpa, .limited(1 << 24));
defer gpa.free(dense);
try std.testing.expectEqual(zit.logstore.version, std.mem.readInt(u16, dense[4..6], .little));
}
// Keeping a horizon keeps the stamp: a deepen below C (which, at the
// structural depth, also drops and re-fetches the seed), then a pack.
var structural = try tmp_b.dir.openDir(io, "structural", .{});
defer structural.close(io);
var deepen = try runZit(gpa, io, structural, &.{ "update", first.? });
defer freeResult(gpa, &deepen);
try expectExitDiag(deepen.term, 0, deepen);
try std.testing.expect(std.mem.indexOf(u8, deepen.stderr, "deepening") != null);
try warmSidecars(gpa, io, tmp_b.dir, "structural");
try std.testing.expect(!try preHorizonReaderAdmitsStore(gpa, io, tmp_b.dir, "structural"));
var pack = try runZit(gpa, io, structural, &.{"pack"});
defer freeResult(gpa, &pack);
try expectExitDiag(pack.term, 0, pack);
try warmSidecars(gpa, io, tmp_b.dir, "structural");
try std.testing.expect(!try preHorizonReaderAdmitsStore(gpa, io, tmp_b.dir, "structural"));
// A path request on a horizon store narrows the paths and keeps the
// history cut -- the clone above already took `--cone` through its open
// -- so the store stays one 0.6 refuses.
var content = try tmp_b.dir.openDir(io, "content", .{});
defer content.close(io);
var narrow = try runZit(gpa, io, content, &.{ "update", "--cone=src" });
defer freeResult(gpa, &narrow);
try expectExitDiag(narrow.term, 0, narrow);
for ([_][]const u8{ "content", "cone-and-horizon" }) |sub| {
var d = try tmp_b.dir.openDir(io, sub, .{});
defer d.close(io);
var v = try runZit(gpa, io, d, &.{"verify"});
defer freeResult(gpa, &v);
try expectExitDiag(v.term, 0, v);
try std.testing.expect(std.mem.indexOf(u8, v.stdout, "cone of 1 member and horizon at") != null);
try std.testing.expect(!try preHorizonReaderAdmitsStore(gpa, io, tmp_b.dir, sub));
}
// Every way out lowers it: `update --dense` drops the whole stored cone,
// and any other policy write without a horizon goes through the same
// `writeStoreCone`. The store is then one 0.6 reads, and verify agrees
// nothing broke.
var widen = try runZit(gpa, io, content, &.{ "update", "--dense" });
defer freeResult(gpa, &widen);
try expectExitDiag(widen.term, 0, widen);
try warmSidecars(gpa, io, tmp_b.dir, "content");
try std.testing.expect(try preHorizonReaderAdmitsStore(gpa, io, tmp_b.dir, "content"));
var both = try tmp_b.dir.openDir(io, "cone-and-horizon", .{});
defer both.close(io);
zit.repocache.writeStoreCone(gpa, io, both, null);
try std.testing.expect(try preHorizonReaderAdmitsStore(gpa, io, tmp_b.dir, "cone-and-horizon"));
var verify = try runZit(gpa, io, both, &.{"verify"});
defer freeResult(gpa, &verify);
try expectExitDiag(verify.term, 0, verify);
// And back in again, through the same write.
var again = try zit.cone.Cone.initHorizon(gpa, &.{}, &.{}, .{ .hash = tip, .rule = .descendants });
defer again.deinit(gpa);
zit.repocache.writeStoreCone(gpa, io, both, &again);
try warmSidecars(gpa, io, tmp_b.dir, "cone-and-horizon");
try std.testing.expect(!try preHorizonReaderAdmitsStore(gpa, io, tmp_b.dir, "cone-and-horizon"));
}
test "cli clone --structural: the seed the origin mints carries its own label, not the branch-move one" {
// Horizon draft section 5.2.2: the seed's U card must differ from the
// label on artifacts the origin means to be shared, so that an origin can
// refuse a pushed seed by it (section 5.2.3).
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
const seeded = try seedHorizonConeOrigin(gpa, io, tmp_a.dir);
defer gpa.free(seeded.h1);
defer gpa.free(seeded.h2);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.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);
const horizon_arg = try std.fmt.allocPrint(gpa, "--horizon={s}", .{seeded.h2});
defer gpa.free(horizon_arg);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", "--no-open", horizon_arg, "--structural", url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
const seed = zit.repocache.readHorizonSeed(gpa, io, b_dir) orelse return error.TestExpectedSeed;
defer gpa.free(seed);
try std.testing.expect(std.mem.indexOf(u8, seed, "\nU zit-seed\n") != null);
}
test "cli clone --horizon=NAME --depth=N: the origin resolves C, and only its hash is recorded" {
// Horizon draft section 6.2.7: a client that does not know C's hash
// names it. The origin resolves NAME from its own derivation, walks
// DEPTH primary parents back, and answers the hash, which the store
// records in place of the name.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
const seeded = try seedHorizonConeOrigin(gpa, io, tmp_a.dir);
defer gpa.free(seeded.h1);
defer gpa.free(seeded.h2);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.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);
const Case = struct { args: []const []const u8, c: []const u8, root_note: bool };
const cases = [_]Case{
.{ .args = &.{ "--horizon=trunk", "--depth=1", "--structural" }, .c = seeded.h2, .root_note = false },
.{ .args = &.{ "--horizon=trunk", "--depth=2", "--structural" }, .c = seeded.h1, .root_note = false },
.{ .args = &.{ "--horizon=tip", "--depth=7" }, .c = seeded.h1, .root_note = true },
};
for (cases) |c| {
errdefer std.debug.print("args: {s} {s}\n", .{ c.args[0], c.args[1] });
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var argv: std.ArrayList([]const u8) = .empty;
defer argv.deinit(gpa);
try argv.appendSlice(gpa, &.{ "clone", "--no-open", url, "repo" });
try argv.appendSlice(gpa, c.args);
var clone = try runZit(gpa, io, tmp_b.dir, argv.items);
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
try std.testing.expect(std.mem.indexOf(u8, clone.stderr, c.c) != null);
try std.testing.expectEqual(c.root_note, std.mem.indexOf(u8, clone.stderr, "the horizon is the root") != null);
try std.testing.expect(std.mem.indexOf(u8, clone.stderr, "did not resolve") == null);
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
var ver = try runZit(gpa, io, b_dir, &.{"verify"});
defer freeResult(gpa, &ver);
try expectExitDiag(ver.term, 0, ver);
const want = try std.fmt.allocPrint(gpa, "horizon at {s}", .{c.c});
defer gpa.free(want);
try std.testing.expect(std.mem.indexOf(u8, ver.stdout, want) != null);
try std.testing.expect(std.mem.indexOf(u8, ver.stdout, "trunk") == null);
// A structural clone's seed is minted for the resolved C.
const structural = c.args.len == 3;
if (zit.repocache.readHorizonSeed(gpa, io, b_dir)) |seed| {
defer gpa.free(seed);
const bind = try std.fmt.allocPrint(gpa, "T +horizon-seed {s} descendants\n", .{c.c});
defer gpa.free(bind);
try std.testing.expect(structural and std.mem.indexOf(u8, seed, bind) != null);
} else try std.testing.expect(!structural);
}
// A name the origin cannot resolve is refused, and leaves nothing behind.
var tmp_c = std.testing.tmpDir(.{ .iterate = true });
defer tmp_c.cleanup();
var bad = try runZit(gpa, io, tmp_c.dir, &.{ "clone", url, "repo", "--horizon=no-such-branch" });
defer freeResult(gpa, &bad);
try expectExitDiag(bad.term, 1, bad);
try std.testing.expect(std.mem.indexOf(u8, bad.stderr, "no-such-branch") != null);
var it = tmp_c.dir.iterate();
try std.testing.expect((try it.next(io)) == null);
}
test "cli clone --cone-not: the excluded subtree's blobs stay elided over the wire" {
// Goal: section 6.1's cone-not pragma composes with clone's local belt:
// an excluded subtree's exclusive blobs elide exactly as out-of-cone
// blobs do. Method: serve a repo with an in-cone and an excluded file,
// clone --cone/--cone-not, open, and probe both the tree and verify's
// sparse summary.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try tmp_a.dir.createDir(io, "src", .default_dir);
try tmp_a.dir.createDir(io, "src/vendor", .default_dir);
try writeFile(io, tmp_a.dir, "src/a.zig", "in-cone content\n");
try writeFile(io, tmp_a.dir, "src/vendor/big.bin", "excluded vendored bytes\n");
try addManaged(gpa, io, tmp_a.dir, &.{ "src/a.zig", "src/vendor/big.bin" });
var c1 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.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);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &.{
"clone", "--no-open", "--cone=src", "--cone-not=src/vendor", url, "repo",
});
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
var vf = try runZit(gpa, io, b_dir, &.{"verify"});
defer freeResult(gpa, &vf);
try expectExitDiag(vf.term, 0, vf);
try std.testing.expect(std.mem.indexOf(u8, vf.stdout, "sparse store") != null);
try std.testing.expect(std.mem.indexOf(u8, vf.stdout, "deliberately elided") != null);
var op = try runZit(gpa, io, b_dir, &.{ "open", "--cone=src", "--cone-not=src/vendor" });
defer freeResult(gpa, &op);
try expectExitDiag(op.term, 0, op);
try std.testing.expect(fileExists(io, b_dir, "src/a.zig"));
try std.testing.expect(!fileExists(io, b_dir, "src/vendor/big.bin"));
}
test "cli clone --cone from a server that omits out-of-cone sub-manifest nodes: elided, opened, pulled, widened" {
// Goal: sparse-cone section 5.1 end to end. A section 6.2 server sends
// the spine and the in-cone nodes only; the clone records the rest as
// elided phantoms, opens the cone, keeps status and pull working, refuses
// a whole-tree listing by the elision class, and widening fetches the
// nodes it now needs. Method: a hand-built two-level tree served from a
// store that lacks the four nodes such a server skips.
const gpa = std.testing.allocator;
const io = std.testing.io;
var src_tmp = std.testing.tmpDir(.{});
defer src_tmp.cleanup();
try initRepo(gpa, io, src_tmp.dir);
var en = try runZit(gpa, io, src_tmp.dir, &.{ "submanifest", "enable", "--k", "1" });
defer freeResult(gpa, &en);
try expectExitDiag(en.term, 0, en);
var t: ConeTreeStore = .{ .gpa = gpa, .io = io, .dir = src_tmp.dir };
defer t.deinit();
const tree = try buildConeTree(&t, false);
var server = try coneTreeServer(gpa, io, src_tmp.dir);
defer gpa.free(server.url);
var server_live = true;
defer if (server_live) server.child.kill(io);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &(.{"clone"} ++ cone_tree_flags ++ .{ server.url, "repo" }));
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
try std.testing.expect(std.mem.indexOf(u8, clone.stdout, "(4 elided by the cone;") != null);
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
// The cone materialized; nothing outside it did.
for ([_][]const u8{ "lib/a", "src/a", "src/h", "src/z" }) |p| try expectFileContent(gpa, io, b_dir, p, p);
for ([_][]const u8{ "kit/a", "rc/x", "src/gen/c", "docs/a", "m/a" }) |p| try std.testing.expect(!fileExists(io, b_dir, p));
// Elision is health: the four nodes plus the out-of-cone blobs that held
// leaves name (kit/a, rc/x, src/gen/c, src/gen/d).
var ver = try runZit(gpa, io, b_dir, &.{"verify"});
defer freeResult(gpa, &ver);
try expectExitDiag(ver.term, 0, ver);
try std.testing.expect(std.mem.indexOf(u8, ver.stdout, "8 artifacts deliberately elided") != null);
var st = try runZit(gpa, io, b_dir, &.{"status"});
defer freeResult(gpa, &st);
try expectExitDiag(st.term, 0, st);
try std.testing.expect(std.mem.indexOf(u8, st.stdout, "nothing to report") != null);
// A whole-tree read cannot be answered, and says why without calling it
// damage (section 6.4).
var ls = try runZit(gpa, io, b_dir, &.{"ls"});
defer freeResult(gpa, &ls);
try expectExitDiag(ls.term, 1, ls);
try std.testing.expect(std.mem.indexOf(u8, ls.stderr, "elided by the store's sparse cone") != null);
try std.testing.expect(std.mem.indexOf(u8, ls.stderr, "corrupt") == null);
// A new check-in whose out-of-cone side is again a node the server
// lacks: the pull accepts it, and update reaches it.
server.child.kill(io);
server_live = false;
const tip2 = try addConeTreeChild(&t, tree);
var server2 = try coneTreeServer(gpa, io, src_tmp.dir);
defer gpa.free(server2.url);
defer server2.child.kill(io);
var remote = try runZit(gpa, io, b_dir, &.{ "remote", server2.url });
defer freeResult(gpa, &remote);
try expectExitDiag(remote.term, 0, remote);
var pull = try runZit(gpa, io, b_dir, &.{"pull"});
defer freeResult(gpa, &pull);
try expectExitDiag(pull.term, 0, pull);
var up = try runZit(gpa, io, b_dir, &.{ "update", tip2 });
defer freeResult(gpa, &up);
try expectExitDiag(up.term, 0, up);
try expectFileContent(gpa, io, b_dir, "src/y", "src/y");
// Widening fetches what it now needs, nodes included, once the peer
// holds them.
_ = try t.leaf(&.{ "docs/a", "docs/b" }, true);
try std.testing.expectEqualStrings(tree.docs, t.names.items[t.names.items.len - 1]);
var widen = try runZit(gpa, io, b_dir, &.{ "cone", "add", "docs" });
defer freeResult(gpa, &widen);
try expectExitDiag(widen.term, 0, widen);
try expectFileContent(gpa, io, b_dir, "docs/a", "docs/a");
try expectFileContent(gpa, io, b_dir, "src/y", "src/y");
}
test "cli clone --cone refuses when the server omits a node straddling the cone boundary" {
// Goal: only a node every slot places outside the cone is elidable. A
// leaf whose slot starts below the cone and reaches into it is one the
// cone needs, so its absence stays a hole and the clone says so.
const gpa = std.testing.allocator;
const io = std.testing.io;
var src_tmp = std.testing.tmpDir(.{});
defer src_tmp.cleanup();
try initRepo(gpa, io, src_tmp.dir);
var en = try runZit(gpa, io, src_tmp.dir, &.{ "submanifest", "enable", "--k", "1" });
defer freeResult(gpa, &en);
try expectExitDiag(en.term, 0, en);
var t: ConeTreeStore = .{ .gpa = gpa, .io = io, .dir = src_tmp.dir };
defer t.deinit();
_ = try buildConeTree(&t, true);
var server = try coneTreeServer(gpa, io, src_tmp.dir);
defer gpa.free(server.url);
defer server.child.kill(io);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &(.{ "clone", "--no-open" } ++ cone_tree_flags ++ .{ server.url, "repo" }));
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 1, clone);
try std.testing.expect(std.mem.indexOf(u8, clone.stdout, "(4 elided by the cone;") != null);
try std.testing.expect(std.mem.indexOf(u8, clone.stderr, "1 sub-manifest references are missing") != null);
}
/// Clone `url` into `name` under `flags` twice: streamed, as an origin that
/// filtered lets it, and through the batched local filter
/// (`ZIT_CLONE_BATCHED`), each into the same path, since `init` derives the
/// project code its config frames carry from it. The two stores must be the
/// same bytes. `streamed_says` must be in the streamed report and not in the
/// batched one, so the comparison never passes by both sides batching.
fn expectStreamedCloneEqualsBatched(
gpa: std.mem.Allocator,
io: std.Io,
dir: std.Io.Dir,
url: []const u8,
flags: []const []const u8,
name: []const u8,
streamed_says: []const u8,
) !void {
var argv: [12][]const u8 = undefined;
argv[0] = "clone";
@memcpy(argv[1..][0..flags.len], flags);
argv[1 + flags.len] = url;
argv[2 + flags.len] = name;
const store_path = try std.fmt.allocPrint(gpa, "{s}/.zit", .{name});
defer gpa.free(store_path);
var st = try runZit(gpa, io, dir, argv[0 .. 3 + flags.len]);
defer freeResult(gpa, &st);
try expectExitDiag(st.term, 0, st);
const st_bytes = try dir.readFileAlloc(io, store_path, gpa, .limited(1 << 24));
defer gpa.free(st_bytes);
try dir.deleteTree(io, name);
const batched_env = [_][2][]const u8{.{ "ZIT_CLONE_BATCHED", "1" }};
var ba = try runZitEnv(gpa, io, dir, &batched_env, argv[0 .. 3 + flags.len]);
defer freeResult(gpa, &ba);
try expectExitDiag(ba.term, 0, ba);
const ba_bytes = try dir.readFileAlloc(io, store_path, gpa, .limited(1 << 24));
defer gpa.free(ba_bytes);
try dir.deleteTree(io, name);
if (std.mem.indexOf(u8, st.stdout, streamed_says) == null or std.mem.indexOf(u8, ba.stdout, streamed_says) != null) {
std.debug.print("\n{s}: only the streamed clone should say '{s}'\n--- streamed ---\n{s}--- batched ---\n{s}", .{ name, streamed_says, st.stdout, ba.stdout });
return error.TestUnexpectedResult;
}
if (!std.mem.eql(u8, st_bytes, ba_bytes)) {
std.debug.print("\n{s}: streamed {d} bytes, batched {d}\n--- streamed ---\n{s}--- batched ---\n{s}", .{ name, st_bytes.len, ba_bytes.len, st.stdout, ba.stdout });
return error.TestUnexpectedResult;
}
}
test "cli clone from an origin that filtered for it streams and stores what the batched local filter stores" {
// Goal: a horizon or cone clone whose origin said it filtered for the
// request persists round by round, and the store it leaves is the one
// the batched path (classify the converged store, persist the keep set)
// leaves, byte for byte. Method: one history with edits in and out of
// the cone, a horizon below its tip, cloned both ways at every horizon
// depth with and without paths, by paths alone, and from the cone-tree
// origin that withholds the sub-manifest nodes no slot reaches.
const gpa = std.testing.allocator;
const io = std.testing.io;
var src_tmp = std.testing.tmpDir(.{});
defer src_tmp.cleanup();
try initRepo(gpa, io, src_tmp.dir);
var en = try runZit(gpa, io, src_tmp.dir, &.{ "submanifest", "enable", "--k", "1" });
defer freeResult(gpa, &en);
try expectExitDiag(en.term, 0, en);
for ([_][]const u8{ "src", "src/a", "src/b", "doc", "lib" }) |d| try src_tmp.dir.createDirPath(io, d);
var hashes: [5][]const u8 = undefined;
var made: usize = 0;
defer for (hashes[0..made]) |h| gpa.free(h);
var body: std.ArrayList(u8) = .empty;
defer body.deinit(gpa);
for (0..hashes.len) |i| {
try body.print(gpa, "revision {d}: one long line every revision keeps, so the wire carries deltas\n", .{i});
for ([_][]const u8{ "src/a/x", "src/b/y", "doc/r", "lib/l" }, 0..) |f, j| {
if ((i + j) % 3 == 2) continue; // not every file every time
try writeFile(io, src_tmp.dir, f, body.items);
}
var add = try runZit(gpa, io, src_tmp.dir, &.{ "add", "src", "doc", "lib" });
defer freeResult(gpa, &add);
try expectExitDiag(add.term, 0, add);
const msg = try std.fmt.allocPrint(gpa, "c{d}", .{i});
defer gpa.free(msg);
hashes[i] = try commitHashDup(gpa, io, src_tmp.dir, msg);
made += 1;
}
const horizon = try std.fmt.allocPrint(gpa, "--horizon={s}", .{hashes[2]});
defer gpa.free(horizon);
var server = try coneTreeServer(gpa, io, src_tmp.dir);
defer gpa.free(server.url);
defer server.child.kill(io);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
const cases = [_]struct { name: []const u8, flags: []const []const u8, says: []const u8 }{
.{ .name = "content", .flags = &.{horizon}, .says = "origin served: content; nothing elided locally" },
.{ .name = "structural", .flags = &.{ horizon, "--structural" }, .says = "origin served: structural; nothing elided locally" },
.{ .name = "content-cone", .flags = &.{ horizon, "--cone=src", "--cone-not=src/b" }, .says = "origin served: content; nothing elided locally" },
.{ .name = "structural-cone", .flags = &.{ horizon, "--structural", "--cone=src" }, .says = "origin served: structural; nothing elided locally" },
.{ .name = "paths", .flags = &.{ "--cone=src", "--cone=lib" }, .says = "applied by the peer; nothing filtered locally" },
};
for (cases) |c| try expectStreamedCloneEqualsBatched(gpa, io, tmp_b.dir, server.url, c.flags, c.name, c.says);
// The origin that holds every node of the cone tree withholds the four
// no slot reaches: streamed, they are named and elided, never gaps.
var tree_tmp = std.testing.tmpDir(.{});
defer tree_tmp.cleanup();
try initRepo(gpa, io, tree_tmp.dir);
var en2 = try runZit(gpa, io, tree_tmp.dir, &.{ "submanifest", "enable", "--k", "1" });
defer freeResult(gpa, &en2);
try expectExitDiag(en2.term, 0, en2);
var t: ConeTreeStore = .{ .gpa = gpa, .io = io, .dir = tree_tmp.dir, .dense = true };
defer t.deinit();
const tree = try buildConeTree(&t, false);
const tree_horizon = try std.fmt.allocPrint(gpa, "--horizon={s}", .{tree.tip});
defer gpa.free(tree_horizon);
var tree_server = try coneTreeServer(gpa, io, tree_tmp.dir);
defer gpa.free(tree_server.url);
defer tree_server.child.kill(io);
try expectStreamedCloneEqualsBatched(gpa, io, tmp_b.dir, tree_server.url, &(.{tree_horizon} ++ cone_tree_flags), "tree-content-cone", "origin served: content; nothing elided locally");
try expectStreamedCloneEqualsBatched(gpa, io, tmp_b.dir, tree_server.url, &(.{ tree_horizon, "--structural" } ++ cone_tree_flags), "tree-structural-cone", "origin served: structural; nothing elided locally");
}
test "cli clone --cone --horizon from a dense origin: every manifest held, only the nodes on paths into the cone" {
// Goal: under a content horizon with path members, a dense zit origin
// withholds the sub-manifest nodes no slot reaches, as a section 6.2
// server does, and the clone, its pull, update, status, verify, commit,
// push and deepen all work over what it holds. Method: the hand-built
// two-level tree served from a store holding every node; the clone must
// see exactly what a server lacking the four out-of-cone nodes sends.
const gpa = std.testing.allocator;
const io = std.testing.io;
var src_tmp = std.testing.tmpDir(.{});
defer src_tmp.cleanup();
try initRepo(gpa, io, src_tmp.dir);
var en = try runZit(gpa, io, src_tmp.dir, &.{ "submanifest", "enable", "--k", "1" });
defer freeResult(gpa, &en);
try expectExitDiag(en.term, 0, en);
var t: ConeTreeStore = .{ .gpa = gpa, .io = io, .dir = src_tmp.dir, .dense = true };
defer t.deinit();
const tree = try buildConeTree(&t, false);
const horizon_tip = try std.fmt.allocPrint(gpa, "--horizon={s}", .{tree.tip});
defer gpa.free(horizon_tip);
var server = try coneTreeServer(gpa, io, src_tmp.dir);
defer gpa.free(server.url);
var server_live = true;
defer if (server_live) server.child.kill(io);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &(.{ "clone", horizon_tip } ++ cone_tree_flags ++ .{ server.url, "repo" }));
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
try std.testing.expect(std.mem.indexOf(u8, clone.stdout, "origin served: content; nothing elided locally") != null);
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
for ([_][]const u8{ "lib/a", "src/a", "src/h", "src/z" }) |p| try expectFileContent(gpa, io, b_dir, p, p);
for ([_][]const u8{ "kit/a", "rc/x", "src/gen/c", "docs/a", "m/a" }) |p| try std.testing.expect(!fileExists(io, b_dir, p));
// The four withheld nodes and the out-of-cone blobs the held leaves name.
var ver = try runZit(gpa, io, b_dir, &.{"verify"});
defer freeResult(gpa, &ver);
try expectExitDiag(ver.term, 0, ver);
try std.testing.expect(std.mem.indexOf(u8, ver.stdout, "8 artifacts deliberately elided") != null);
var st = try runZit(gpa, io, b_dir, &.{"status"});
defer freeResult(gpa, &st);
try expectExitDiag(st.term, 0, st);
try std.testing.expect(std.mem.indexOf(u8, st.stdout, "nothing to report") != null);
// A new check-in on the origin: the pull takes it without its new
// out-of-cone node, and update reaches it.
server.child.kill(io);
server_live = false;
const tip2 = try addConeTreeChild(&t, tree);
var server2 = try coneTreeServer(gpa, io, src_tmp.dir);
defer gpa.free(server2.url);
defer server2.child.kill(io);
var remote = try runZit(gpa, io, b_dir, &.{ "remote", server2.url });
defer freeResult(gpa, &remote);
try expectExitDiag(remote.term, 0, remote);
var pull = try runZit(gpa, io, b_dir, &.{"pull"});
defer freeResult(gpa, &pull);
try expectExitDiag(pull.term, 0, pull);
var up = try runZit(gpa, io, b_dir, &.{ "update", tip2 });
defer freeResult(gpa, &up);
try expectExitDiag(up.term, 0, up);
try expectFileContent(gpa, io, b_dir, "src/y", "src/y");
// A diff narrowed to a path inside the cone reads only the nodes on
// paths into it; one that spans the excluded subtree says why it cannot.
var df = try runZit(gpa, io, b_dir, &.{ "diff", "--brief", "--from", tree.tip, "--to", tip2, "src/y" });
defer freeResult(gpa, &df);
try expectExitDiag(df.term, 0, df);
try std.testing.expect(std.mem.indexOf(u8, df.stdout, "src/y") != null);
var df_wide = try runZit(gpa, io, b_dir, &.{ "diff", "--brief", "--from", tree.tip, "--to", tip2, "src" });
defer freeResult(gpa, &df_wide);
try expectExitDiag(df_wide.term, 1, df_wide);
try std.testing.expect(std.mem.indexOf(u8, df_wide.stderr, "sparse cone") != null);
var ver2 = try runZit(gpa, io, b_dir, &.{"verify"});
defer freeResult(gpa, &ver2);
try expectExitDiag(ver2.term, 0, ver2);
// A commit inside the cone builds its tree over the withheld nodes and
// pushes a check-in the origin holds whole: a dense clone opens it.
try writeFile(io, b_dir, "src/h", "src/h changed\n");
var ci = try runZit(gpa, io, b_dir, &.{ "commit", "--user", "e2e", "-m", "change src/h" });
defer freeResult(gpa, &ci);
try expectExitDiag(ci.term, 0, ci);
var push = try runZit(gpa, io, b_dir, &.{"push"});
defer freeResult(gpa, &push);
try expectExitDiag(push.term, 0, push);
var tmp_d = std.testing.tmpDir(.{});
defer tmp_d.cleanup();
var dense = try runZit(gpa, io, tmp_d.dir, &.{ "clone", server2.url, "repo" });
defer freeResult(gpa, &dense);
try expectExitDiag(dense.term, 0, dense);
var d_dir = try tmp_d.dir.openDir(io, "repo", .{});
defer d_dir.close(io);
try expectFileContent(gpa, io, d_dir, "src/h", "src/h changed\n");
for ([_][]const u8{ "docs/a", "m/a", "src/gen/a", "zzz/a", "zzz/b", "src/y" }) |p| try expectFileContent(gpa, io, d_dir, p, p);
var dver = try runZit(gpa, io, d_dir, &.{"verify"});
defer freeResult(gpa, &dver);
try expectExitDiag(dver.term, 0, dver);
// Deepen: a clone at the child's horizon updates to its parent, whose
// manifest and in-cone nodes it already holds, fetching the blobs.
const horizon_tip2 = try std.fmt.allocPrint(gpa, "--horizon={s}", .{tip2});
defer gpa.free(horizon_tip2);
var tmp_e = std.testing.tmpDir(.{});
defer tmp_e.cleanup();
var clone2 = try runZit(gpa, io, tmp_e.dir, &(.{ "clone", horizon_tip2 } ++ cone_tree_flags ++ .{ server2.url, "repo" }));
defer freeResult(gpa, &clone2);
try expectExitDiag(clone2.term, 0, clone2);
var e_dir = try tmp_e.dir.openDir(io, "repo", .{});
defer e_dir.close(io);
var deepen = try runZit(gpa, io, e_dir, &.{ "update", tree.tip });
defer freeResult(gpa, &deepen);
try expectExitDiag(deepen.term, 0, deepen);
try expectFileContent(gpa, io, e_dir, "src/z", "src/z");
try std.testing.expect(!fileExists(io, e_dir, "src/y"));
var ever = try runZit(gpa, io, e_dir, &.{"verify"});
defer freeResult(gpa, &ever);
try expectExitDiag(ever.term, 0, ever);
// A structural clone of the same cone withholds the same nodes, and its
// commit fetches them the same way.
var tmp_s = std.testing.tmpDir(.{});
defer tmp_s.cleanup();
var clone3 = try runZit(gpa, io, tmp_s.dir, &(.{ "clone", horizon_tip2, "--structural" } ++ cone_tree_flags ++ .{ server2.url, "repo" }));
defer freeResult(gpa, &clone3);
try expectExitDiag(clone3.term, 0, clone3);
try std.testing.expect(std.mem.indexOf(u8, clone3.stdout, "origin served: structural") != null);
var s_dir = try tmp_s.dir.openDir(io, "repo", .{});
defer s_dir.close(io);
try writeFile(io, s_dir, "src/z", "src/z changed\n");
var sci = try runZit(gpa, io, s_dir, &.{ "commit", "--user", "e2e", "-m", "change src/z" });
defer freeResult(gpa, &sci);
try expectExitDiag(sci.term, 0, sci);
var sver = try runZit(gpa, io, s_dir, &.{"verify"});
defer freeResult(gpa, &sver);
try expectExitDiag(sver.term, 0, sver);
}
test "cli clone --cone-file: members from a file, comments, blanks, and '!' exclusions" {
// Goal: the --cone-file grammar end to end: one member per line, '#'
// comments and blank lines skipped, a '!' prefix marking an exclusion;
// the resulting sparse store matches the equivalent flag form. Method:
// serve a three-path repo, clone through a cone file, open and probe.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try tmp_a.dir.createDir(io, "src", .default_dir);
try tmp_a.dir.createDir(io, "tools", .default_dir);
try writeFile(io, tmp_a.dir, "src/a.zig", "in\n");
try writeFile(io, tmp_a.dir, "tools/t.bin", "out\n");
try writeFile(io, tmp_a.dir, "README", "root file\n");
try addManaged(gpa, io, tmp_a.dir, &.{ "src/a.zig", "tools/t.bin", "README" });
var c1 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.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);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
try writeFile(io, tmp_b.dir, "cone.txt", "# my sparse profile\nsrc\n!src/generated\n\nREADME\n");
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", "--no-open", "--cone-file=cone.txt", url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
var op = try runZit(gpa, io, b_dir, &.{ "open", "--cone=src", "--cone=README" });
defer freeResult(gpa, &op);
try expectExitDiag(op.term, 0, op);
try std.testing.expect(fileExists(io, b_dir, "src/a.zig"));
try std.testing.expect(fileExists(io, b_dir, "README"));
try std.testing.expect(!fileExists(io, b_dir, "tools/t.bin"));
}
/// A stand-in for a server that knows no cone pragma, as stock Fossil does
/// not: listens on loopback, drops every `pragma cone*` card from each
/// request it relays to a `zit serve` on `upstream`, and copies the reply
/// back. Dropping a card is exactly what ignoring an unknown pragma does, so
/// the clone behind it must filter locally. One request per connection.
const ConeBlindProxy = struct {
io: std.Io,
listener: net.Server,
port: u16,
upstream: u16,
stopping: std.atomic.Value(bool) = .init(false),
thread: std.Thread = undefined,
fn start(self: *ConeBlindProxy) !void {
self.thread = try std.Thread.spawn(.{}, run, .{self});
}
fn stop(self: *ConeBlindProxy) void {
self.stopping.store(true, .release);
if (dialServe(self.io, self.port)) |s| s.close(self.io) else |_| {}
self.thread.join();
self.listener.deinit(self.io);
}
fn run(self: *ConeBlindProxy) void {
while (!self.stopping.load(.acquire)) {
const conn = self.listener.accept(self.io) catch return;
defer conn.close(self.io);
if (self.stopping.load(.acquire)) return;
self.relay(conn) catch {};
}
}
fn relay(self: *ConeBlindProxy, conn: net.Stream) !void {
const a = std.heap.page_allocator;
var rbuf: [16 * 1024]u8 = undefined;
var r = conn.reader(self.io, &rbuf);
var head: std.ArrayListUnmanaged(u8) = .empty;
defer head.deinit(a);
var body_len: usize = 0;
while (true) {
const line = try r.interface.takeDelimiterInclusive('\n');
if (std.mem.eql(u8, line, "\r\n")) break;
const colon = std.mem.indexOfScalar(u8, line, ':') orelse 0;
if (colon != 0 and std.ascii.eqlIgnoreCase(line[0..colon], "content-length")) {
body_len = try std.fmt.parseInt(usize, std.mem.trim(u8, line[colon + 1 ..], " \r\n"), 10);
continue; // restated below for the rewritten body
}
try head.appendSlice(a, line);
}
const body = try r.interface.readAlloc(a, body_len);
defer a.free(body);
const plain = try container.decode(a, body, 64 << 20);
defer a.free(plain);
var kept: std.ArrayListUnmanaged(u8) = .empty;
defer kept.deinit(a);
var lines = std.mem.splitScalar(u8, plain, '\n');
var first = true;
while (lines.next()) |l| {
if (std.mem.startsWith(u8, l, "pragma cone")) continue;
if (!first) try kept.append(a, '\n');
first = false;
try kept.appendSlice(a, l);
}
const rewritten = try container.encode(a, kept.items, .default);
defer a.free(rewritten);
try head.print(a, "Content-Length: {d}\r\n\r\n", .{rewritten.len});
const up = try dialServe(self.io, self.upstream);
defer up.close(self.io);
var ubuf: [4096]u8 = undefined;
var uw = up.writer(self.io, &ubuf);
try uw.interface.writeAll(head.items);
try uw.interface.writeAll(rewritten);
try uw.interface.flush();
var urbuf: [16 * 1024]u8 = undefined;
var ur = up.reader(self.io, &urbuf);
const reply = try ur.interface.allocRemaining(a, .limited(64 << 20));
defer a.free(reply);
var wbuf: [4096]u8 = undefined;
var w = conn.writer(self.io, &wbuf);
try w.interface.writeAll(reply);
try w.interface.flush();
}
};
test "cli clone --cone-parents: a served and a locally filtered clone keep the same parent-level files" {
// Goal: the parents property end to end over the wire, both ways. A zit
// origin receives `pragma cone-parents` and filters; behind a proxy that
// drops every cone pragma, as stock Fossil ignores them, the clone filters
// locally with the same predicate. Both keep the root's files and src's
// own file, never src/bar's, and store the flagged cone.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
const fx = try parentsFixture(gpa, io, tmp_a.dir);
defer gpa.free(fx.c1);
defer gpa.free(fx.c2);
const serve_port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, serve_port);
defer server.kill(io);
try waitForListen(io, serve_port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{serve_port});
defer gpa.free(url);
const addr = try net.IpAddress.parseIp4("127.0.0.1", 0);
var blind = ConeBlindProxy{ .io = io, .listener = try addr.listen(io, .{}), .port = 0, .upstream = serve_port };
blind.port = blind.listener.socket.address.getPort();
try blind.start();
defer blind.stop();
const blind_url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{blind.port});
defer gpa.free(blind_url);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var served = try runZit(gpa, io, tmp_b.dir, &.{ "clone", "--cone=src/foo", "--cone-parents", url, "served" });
defer freeResult(gpa, &served);
try expectExitDiag(served.term, 0, served);
try std.testing.expect(std.mem.indexOf(u8, served.stdout, "applied by the peer") != null);
var filtered = try runZit(gpa, io, tmp_b.dir, &.{ "clone", "--cone=src/foo", "--cone-parents", blind_url, "filtered" });
defer freeResult(gpa, &filtered);
try expectExitDiag(filtered.term, 0, filtered);
try std.testing.expect(std.mem.indexOf(u8, filtered.stdout, "filtered locally") != null);
var listings: [2][]u8 = undefined;
for ([_][]const u8{ "served", "filtered" }, 0..) |name, i| {
var d = try tmp_b.dir.openDir(io, name, .{});
defer d.close(io);
try expectParentsCheckout(io, d);
const readme = try readFile(gpa, io, d, "README");
defer gpa.free(readme);
try std.testing.expectEqualStrings("README two\n", readme);
// The stored cone carries the property: version 4, one inclusion.
const stored = try d.readFileAlloc(io, zit.repocache.store_cone_filename, gpa, .limited(1 << 16));
defer gpa.free(stored);
try std.testing.expectEqualSlices(u8, "\x00\x04\x01\x00\x07\x00src/foo", stored);
// The sibling's blob is not held: elided, not merely unmaterialized.
var cat = try runZit(gpa, io, d, &.{ "cat", "src/bar/x.zig" });
defer freeResult(gpa, &cat);
try expectExit(cat.term, 8);
var ls = try runZit(gpa, io, d, &.{"ls"});
defer freeResult(gpa, &ls);
try expectExitDiag(ls.term, 0, ls);
listings[i] = try gpa.dupe(u8, ls.stdout);
}
defer for (listings) |l| gpa.free(l);
try std.testing.expectEqualStrings(listings[0], listings[1]);
}
test "cli clone --cone-parents --horizon: a blob is kept when it is both at or after C and covered" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
const fx = try parentsFixture(gpa, io, tmp_a.dir);
defer gpa.free(fx.c1);
defer gpa.free(fx.c2);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.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);
const horizon = try std.fmt.allocPrint(gpa, "--horizon={s}", .{fx.c2});
defer gpa.free(horizon);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", "--cone=src/foo", "--cone-parents", horizon, url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
var d = try tmp_b.dir.openDir(io, "repo", .{});
defer d.close(io);
try expectParentsCheckout(io, d);
// At C and covered: held. Covered but below C (README's first version),
// or at C but uncovered (the sibling): not held.
for ([_]struct { path: []const u8, rev: []const u8, held: bool }{
.{ .path = "README", .rev = fx.c2, .held = true },
.{ .path = "src/main.zig", .rev = fx.c2, .held = true },
.{ .path = "README", .rev = fx.c1, .held = false },
.{ .path = "src/bar/x.zig", .rev = fx.c2, .held = false },
}) |c| {
var cat = try runZit(gpa, io, d, &.{ "cat", c.path, c.rev });
defer freeResult(gpa, &cat);
try expectExit(cat.term, if (c.held) 0 else 8);
}
var cone = zit.repocache.readStoreCone(gpa, io, d) orelse return error.NoStoreCone;
defer cone.deinit(gpa);
try std.testing.expect(cone.parents);
try std.testing.expect(cone.horizon != null);
}
test "cli clone --cone without --cone-parents stores the cone byte for byte as before" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
const fx = try parentsFixture(gpa, io, tmp_a.dir);
defer gpa.free(fx.c1);
defer gpa.free(fx.c2);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.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);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", "--cone=src/foo", "--cone-not=src/foo/deep", url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
var d = try tmp_b.dir.openDir(io, "repo", .{});
defer d.close(io);
const stored = try d.readFileAlloc(io, zit.repocache.store_cone_filename, gpa, .limited(1 << 16));
defer gpa.free(stored);
// The version-2 form, unchanged by the parents property's arrival.
try std.testing.expectEqualSlices(u8, "\x00\x02\x02\x00\x07\x00src/foo\x01\x0c\x00src/foo/deep", stored);
try std.testing.expect(!fileExists(io, d, "README"));
try std.testing.expect(!fileExists(io, d, "src/main.zig"));
try std.testing.expect(fileExists(io, d, "src/foo/a.zig"));
try std.testing.expect(!fileExists(io, d, "src/foo/deep/b.zig"));
}
test "cli clone --cone: a cone clone adopts a signed erasure, and widening the cone never fetches the erased name" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "a", .default_dir);
var a = try tmp.dir.openDir(io, "a", .{});
defer a.close(io);
try initRepo(gpa, io, a);
try a.createDir(io, "src", .default_dir);
try a.createDir(io, "tools", .default_dir);
try writeFile(io, a, "src/a.zig", "in-cone content\n");
try writeFile(io, a, "tools/secret.bin", "outside the cone, and erased\n");
try addManaged(gpa, io, a, &.{ "src/a.zig", "tools/secret.bin" });
gpa.free(try commitHashDup(gpa, io, a, "seed"));
const pub_hex = erasurePublicHex();
try erasureOk(gpa, io, a, .{}, &.{ "shun", "key", "pin", &pub_hex });
const secret = try artifactNameByContent(gpa, io, a, "outside the cone, and erased\n");
defer gpa.free(secret);
// Signed now, so the origin's own wall clock keeps it pending for 24 h
// and the origin still serves the bytes; the clone's clock is two days on.
const now = std.Io.Timestamp.now(io, .real).toSeconds();
var at_buf: [24]u8 = undefined;
var later_buf: [24]u8 = undefined;
const signed_at = try std.fmt.bufPrint(&at_buf, "{d}", .{now});
const later = try std.fmt.bufPrint(&later_buf, "{d}", .{now + 2 * zit.erasure.default_delay});
try erasureOk(gpa, io, a, .{ .epoch = signed_at, .key = erasure_seed_hex }, &.{ "shun", "--force", "--sign", secret });
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, a, 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);
try erasureOk(gpa, io, tmp.dir, .{ .epoch = later }, &.{ "clone", "--no-open", "--cone=src", url, "b" });
var b = try tmp.dir.openDir(io, "b", .{});
defer b.close(io);
{
var r = try runErasureZit(gpa, io, b, .{ .epoch = later }, &.{ "shun", "--list" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try expectContains(r.stdout, secret);
try expectContains(r.stdout, "erase active");
}
try erasureOk(gpa, io, b, .{ .epoch = later }, &.{ "open", "--cone=src" });
// Widened to the erased path: the deepen asks the origin, which would
// serve the bytes, and the clone never asks for them.
var r = try runErasureZit(gpa, io, b, .{ .epoch = later }, &.{ "update", "--cone=tools", "trunk" });
defer freeResult(gpa, &r);
try std.testing.expect(!try storeHoldsArtifact(gpa, io, b, secret));
}
test "cli clone --horizon: a horizon clone adopts a signed erasure, and deepening past the erased version never fetches it" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Run once with the erasure and once without: the second proves the same
// deepen does fetch the bytes, so the first cannot pass by doing nothing.
for ([_]bool{ true, false }) |erase| {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "a", .default_dir);
var a = try tmp.dir.openDir(io, "a", .{});
defer a.close(io);
try initRepo(gpa, io, a);
try writeFile(io, a, "a.txt", "the first version, erased later\n");
try addManaged(gpa, io, a, &.{"a.txt"});
const first = try commitHashDup(gpa, io, a, "one");
defer gpa.free(first);
const old = try artifactNameByContent(gpa, io, a, "the first version, erased later\n");
defer gpa.free(old);
try writeFile(io, a, "a.txt", "the second version\n");
gpa.free(try commitHashDup(gpa, io, a, "two"));
const pub_hex = erasurePublicHex();
try erasureOk(gpa, io, a, .{}, &.{ "shun", "key", "pin", &pub_hex });
// Signed now: the origin's wall clock keeps it pending and still
// serves the bytes; the clone's clock is two days on.
const now = std.Io.Timestamp.now(io, .real).toSeconds();
var at_buf: [24]u8 = undefined;
var later_buf: [24]u8 = undefined;
const signed_at = try std.fmt.bufPrint(&at_buf, "{d}", .{now});
const later = try std.fmt.bufPrint(&later_buf, "{d}", .{now + 2 * zit.erasure.default_delay});
if (erase) try erasureOk(gpa, io, a, .{ .epoch = signed_at, .key = erasure_seed_hex }, &.{ "shun", "--force", "--sign", old });
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, a, 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);
try erasureOk(gpa, io, tmp.dir, .{ .epoch = later }, &.{ "clone", "--horizon=tip", url, "hz" });
var hz = try tmp.dir.openDir(io, "hz", .{});
defer hz.close(io);
try std.testing.expect(!try storeHoldsArtifact(gpa, io, hz, old));
if (erase) {
var r = try runErasureZit(gpa, io, hz, .{ .epoch = later }, &.{ "shun", "--list" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try expectContains(r.stdout, old);
try expectContains(r.stdout, "erase active");
}
// Deepen past the erased version: `update` to the first check-in
// fetches what lies below the horizon from the saved remote.
var up = try runErasureZit(gpa, io, hz, .{ .epoch = later }, &.{ "update", first });
defer freeResult(gpa, &up);
if (!erase) {
try expectExitDiag(up.term, 0, up);
try std.testing.expect(try storeHoldsArtifact(gpa, io, hz, old));
continue;
}
try std.testing.expect(!try storeHoldsArtifact(gpa, io, hz, old));
var cat = try runErasureZit(gpa, io, hz, .{ .epoch = later }, &.{ "cat", "a.txt", first });
defer freeResult(gpa, &cat);
try expectExitDiag(cat.term, 8, cat);
try expectContains(cat.stderr, "was erased by the repository owner");
}
}