const std = @import("std");
const support = @import("support.zig");
const addManaged = support.addManaged;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const framesTrailer = support.framesTrailer;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const initRepo = support.initRepo;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const spawnServe = support.spawnServe;
const stub_signer = support.stub_signer;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;
/// Count `artifact HASH SIZE` header lines in a frames stdout. Counts only
/// well-formed header positions (start of stream or just past a payload), but
/// a plain prefix count suffices for these fixtures (no payload embeds one at
/// line start followed by hex + decimal).
fn framesArtifactCount(stdout: []const u8) usize {
var n: usize = 0;
var it = std.mem.splitScalar(u8, stdout, '\n');
while (it.next()) |line| {
if (std.mem.startsWith(u8, line, "artifact ")) n += 1;
}
return n;
}
test "cli frames: over a packed delta chain every artifact still matches artifact --raw" {
// Goal: the reader that a plaintext memo sits behind. `pack` re-deltas
// the store, so the stream's artifacts resolve through delta bases rather
// than standing alone — the path where a wrong or stale memo would show
// up. Method: commit a growing file repeatedly so successive blobs delta
// well, pack to force that encoding, then compare EVERY streamed payload
// against `artifact --raw` for the same hash.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// A file that grows by an appended line each time: consecutive versions
// share nearly all their bytes, which is what makes `pack` choose deltas
// and what makes consecutive artifacts share a base.
var body: std.ArrayList(u8) = .empty;
defer body.deinit(gpa);
try body.appendSlice(gpa, "line 0 — enough shared text that a delta beats a fresh frame\n");
try writeFile(io, tmp.dir, "a.txt", body.items);
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var round: usize = 1;
while (round < 8) : (round += 1) {
var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "grow" });
defer freeResult(gpa, &c);
try expectExit(c.term, 0);
try body.print(gpa, "line {d} — enough shared text that a delta beats a fresh frame\n", .{round});
try writeFile(io, tmp.dir, "a.txt", body.items);
}
var last = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "grow" });
defer freeResult(gpa, &last);
try expectExit(last.term, 0);
var packed_run = try runZit(gpa, io, tmp.dir, &.{"pack"});
defer freeResult(gpa, &packed_run);
try expectExit(packed_run.term, 0);
var stream = try runZit(gpa, io, tmp.dir, &.{"frames"});
defer freeResult(gpa, &stream);
try expectExitDiag(stream.term, 0, stream);
try std.testing.expect(framesArtifactCount(stream.stdout) >= 16); // 8 blobs + 8 check-ins
// Walk the stream's `artifact HASH LEN` headers and check each payload
// against the single-artifact reader. A memo that ever served the wrong
// base would surface here as a mismatch on the first delta-resolved
// artifact, not as a crash.
var checked: usize = 0;
var pos: usize = 0;
while (std.mem.indexOfPos(u8, stream.stdout, pos, "artifact ")) |hit| {
// Only a header at a line start counts; payload bytes may contain the
// same word.
if (hit != 0 and stream.stdout[hit - 1] != '\n') {
pos = hit + 1;
continue;
}
const line_end = std.mem.indexOfScalarPos(u8, stream.stdout, hit, '\n') orelse break;
var it = std.mem.tokenizeScalar(u8, stream.stdout[hit..line_end], ' ');
_ = it.next(); // "artifact"
const hash = it.next() orelse break;
const len_text = it.next() orelse break;
const len = std.fmt.parseInt(usize, len_text, 10) catch break;
const payload = stream.stdout[line_end + 1 ..][0..len];
var raw = try runZit(gpa, io, tmp.dir, &.{ "artifact", "--raw", hash });
defer freeResult(gpa, &raw);
try expectExit(raw.term, 0);
try std.testing.expectEqualStrings(raw.stdout, payload);
checked += 1;
pos = line_end + 1 + len;
}
try std.testing.expect(checked >= 16);
}
test "cli frames: bootstrap census, idle poll, incremental append, byte-identity with artifact --raw" {
// Goal: the incremental read loop. --since=0 streams the census; a current
// cursor answers exit 3 with a trailer and no artifacts; an append is
// seen exactly once; each payload is byte-identical to
// `artifact HASH --raw`. Method: drive one repo through two commits,
// polling between, and cross-check one payload.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "a.txt", "alpha\n");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "one" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
var boot = try runZit(gpa, io, tmp.dir, &.{"frames"});
defer freeResult(gpa, &boot);
try expectExitDiag(boot.term, 0, boot);
try std.testing.expectEqual(@as(usize, 2), framesArtifactCount(boot.stdout)); // blob + check-in
const t0 = try framesTrailer(gpa, boot.stdout);
defer gpa.free(t0.cursor);
defer gpa.free(t0.token);
// Idle poll: exit 3, no artifacts, the trailer still names the position.
const since0 = try std.fmt.allocPrint(gpa, "--since={s}", .{t0.cursor});
defer gpa.free(since0);
const verify0 = try std.fmt.allocPrint(gpa, "--verify={s}", .{t0.token});
defer gpa.free(verify0);
var idle = try runZit(gpa, io, tmp.dir, &.{ "frames", since0, verify0 });
defer freeResult(gpa, &idle);
try expectExit(idle.term, 3);
try std.testing.expectEqual(@as(usize, 0), framesArtifactCount(idle.stdout));
const t_idle = try framesTrailer(gpa, idle.stdout);
defer gpa.free(t_idle.cursor);
defer gpa.free(t_idle.token);
try std.testing.expectEqualStrings(t0.cursor, t_idle.cursor);
// Append two artifacts (a blob and its check-in); the poll sees exactly
// those, exit 0.
try writeFile(io, tmp.dir, "b.txt", "beta\n");
var ad = try runZit(gpa, io, tmp.dir, &.{ "add", "b.txt" });
defer freeResult(gpa, &ad);
try expectExitDiag(ad.term, 0, ad);
var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "two" });
defer freeResult(gpa, &c2);
try expectExit(c2.term, 0);
var incr = try runZit(gpa, io, tmp.dir, &.{ "frames", since0, verify0 });
defer freeResult(gpa, &incr);
try expectExitDiag(incr.term, 0, incr);
try std.testing.expectEqual(@as(usize, 2), framesArtifactCount(incr.stdout));
// Byte-identity: the first emitted payload equals `artifact HASH --raw`.
var lines = std.mem.splitScalar(u8, incr.stdout, '\n');
const header = lines.next().?;
try std.testing.expect(std.mem.startsWith(u8, header, "artifact "));
var hf = std.mem.splitScalar(u8, header, ' ');
_ = hf.next();
const hash = hf.next().?;
const size = try std.fmt.parseInt(usize, hf.next().?, 10);
const payload_start = header.len + 1;
const payload = incr.stdout[payload_start .. payload_start + size];
var raw = try runZit(gpa, io, tmp.dir, &.{ "artifact", hash, "--raw" });
defer freeResult(gpa, &raw);
try expectExitDiag(raw.term, 0, raw);
try std.testing.expectEqualStrings(raw.stdout, payload);
}
test "cli frames: sync arrivals appear after the cursor; a shun reclaim invalidates it, re-read recovers" {
// Goal: the read loop's other acceptance half. Artifacts arriving by pull are
// appends the next poll emits; a reclaiming shun rewrites the store and
// the poll answers the distinct cursor-invalid exit (4) with the
// re-read remedy; --since=0 then works. Method: two repos over HTTP.
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", "alpha\n");
try addManaged(gpa, io, tmp_a.dir, &.{"a.txt"});
var c1 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "one" });
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", 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 boot = try runZit(gpa, io, b_dir, &.{"frames"});
defer freeResult(gpa, &boot);
try expectExitDiag(boot.term, 0, boot);
const t0 = try framesTrailer(gpa, boot.stdout);
defer gpa.free(t0.cursor);
defer gpa.free(t0.token);
const since0 = try std.fmt.allocPrint(gpa, "--since={s}", .{t0.cursor});
defer gpa.free(since0);
const verify0 = try std.fmt.allocPrint(gpa, "--verify={s}", .{t0.token});
defer gpa.free(verify0);
// New history lands upstream; a pull appends it here; the poll sees it.
try writeFile(io, tmp_a.dir, "b.txt", "beta\n");
var ad = try runZit(gpa, io, tmp_a.dir, &.{ "add", "b.txt" });
defer freeResult(gpa, &ad);
try expectExitDiag(ad.term, 0, ad);
var c2 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "two" });
defer freeResult(gpa, &c2);
try expectExit(c2.term, 0);
var pull = try runZit(gpa, io, b_dir, &.{"pull"});
defer freeResult(gpa, &pull);
try expectExitDiag(pull.term, 0, pull);
var incr = try runZit(gpa, io, b_dir, &.{ "frames", since0, verify0 });
defer freeResult(gpa, &incr);
try expectExitDiag(incr.term, 0, incr);
try std.testing.expectEqual(@as(usize, 2), framesArtifactCount(incr.stdout));
const t1 = try framesTrailer(gpa, incr.stdout);
defer gpa.free(t1.cursor);
defer gpa.free(t1.token);
const since1 = try std.fmt.allocPrint(gpa, "--since={s}", .{t1.cursor});
defer gpa.free(since1);
const verify1 = try std.fmt.allocPrint(gpa, "--verify={s}", .{t1.token});
defer gpa.free(verify1);
// A reclaiming shun (of the first blob) rewrites the store: the cursor
// dies with the distinct exit code and the stated remedy.
var boot_lines = std.mem.splitScalar(u8, boot.stdout, '\n');
const first_header = boot_lines.next().?;
var hf = std.mem.splitScalar(u8, first_header, ' ');
_ = hf.next();
const first_hash = hf.next().?;
var sh = try runZit(gpa, io, b_dir, &.{ "shun", "--force", first_hash });
defer freeResult(gpa, &sh);
try expectExitDiag(sh.term, 0, sh);
var dead = try runZit(gpa, io, b_dir, &.{ "frames", since1, verify1 });
defer freeResult(gpa, &dead);
try expectExit(dead.term, 4);
try std.testing.expect(std.mem.indexOf(u8, dead.stderr, "re-read from --since=0") != null);
// The from-zero re-read works and skips the reclaimed artifact: its
// header line is gone (the hash still APPEARS inside the surviving
// check-in's F card, which is correct -- the reference outlives the
// bytes; only the emission is suppressed).
var reread = try runZit(gpa, io, b_dir, &.{"frames"});
defer freeResult(gpa, &reread);
try expectExitDiag(reread.term, 0, reread);
const reclaimed_header = try std.fmt.allocPrint(gpa, "artifact {s} ", .{first_hash});
defer gpa.free(reclaimed_header);
try std.testing.expect(std.mem.indexOf(u8, reread.stdout, reclaimed_header) == null);
}
test "cli frames: --structural emits only what the crosslink links, signed manifests included, cursor unchanged" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// Commit one: an ordinary blob + manifest.
try writeFile(io, tmp.dir, "a.txt", "alpha\n");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "one" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
// The cursor between the commits, taken from an UNFILTERED poll.
var mid = try runZit(gpa, io, tmp.dir, &.{"frames"});
defer freeResult(gpa, &mid);
try expectExitDiag(mid.term, 0, mid);
const tm = try framesTrailer(gpa, mid.stdout);
defer gpa.free(tm.cursor);
defer gpa.free(tm.token);
// Commit two: a blob + a CLEARSIGNED manifest. The signed case is pinned
// deliberately: a wrapped artifact's first bytes are armor, not a card,
// and the first draft of the structural gate's pre-screen rejected
// exactly these — 1,087 of them on the sqlite corpus.
try writeFile(io, tmp.dir, "b.txt", "beta\n");
var ad = try runZit(gpa, io, tmp.dir, &.{ "add", "b.txt" });
defer freeResult(gpa, &ad);
try expectExitDiag(ad.term, 0, ad);
var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u", .pgp = stub_signer }, &.{ "commit", "-m", "two", "--sign" });
defer freeResult(gpa, &c2);
try expectExit(c2.term, 0);
// Full census: 4 artifacts. Structural census: only the two manifests —
// and its trailer is IDENTICAL to the full stream's, because the cursor
// advances over skipped frames exactly as over emitted ones.
var full = try runZit(gpa, io, tmp.dir, &.{"frames"});
defer freeResult(gpa, &full);
try expectExitDiag(full.term, 0, full);
try std.testing.expectEqual(@as(usize, 4), framesArtifactCount(full.stdout));
var structural = try runZit(gpa, io, tmp.dir, &.{ "frames", "--structural" });
defer freeResult(gpa, &structural);
try expectExitDiag(structural.term, 0, structural);
try std.testing.expectEqual(@as(usize, 2), framesArtifactCount(structural.stdout));
const tf = try framesTrailer(gpa, full.stdout);
defer gpa.free(tf.cursor);
defer gpa.free(tf.token);
const ts = try framesTrailer(gpa, structural.stdout);
defer gpa.free(ts.cursor);
defer gpa.free(ts.token);
try std.testing.expectEqualStrings(tf.cursor, ts.cursor);
try std.testing.expectEqualStrings(tf.token, ts.token);
// Every structural payload is byte-identical to its frame in the full
// stream: the flag filters, it never rewrites.
try std.testing.expect(framesStructuralSubset(structural.stdout, full.stdout));
// Incremental: from the mid-point cursor, --structural sees exactly the
// signed manifest — the blob is traversed, not emitted.
const since = try std.fmt.allocPrint(gpa, "--since={s}", .{tm.cursor});
defer gpa.free(since);
const verify = try std.fmt.allocPrint(gpa, "--verify={s}", .{tm.token});
defer gpa.free(verify);
var incr = try runZit(gpa, io, tmp.dir, &.{ "frames", since, verify, "--structural" });
defer freeResult(gpa, &incr);
try expectExitDiag(incr.term, 0, incr);
try std.testing.expectEqual(@as(usize, 1), framesArtifactCount(incr.stdout));
try std.testing.expect(std.mem.indexOf(u8, incr.stdout, "-----BEGIN PGP SIGNED MESSAGE-----") != null);
}
/// Every `artifact HASH SIZE` frame of `subset_stream` appears byte-identical
/// (header and payload) in `full_stream`.
fn framesStructuralSubset(subset_stream: []const u8, full_stream: []const u8) bool {
var pos: usize = 0;
while (pos < subset_stream.len) {
const nl = std.mem.indexOfScalarPos(u8, subset_stream, pos, '\n') orelse return true;
const header = subset_stream[pos..nl];
if (!std.mem.startsWith(u8, header, "artifact ")) return true; // trailer reached
var hf = std.mem.splitScalar(u8, header, ' ');
_ = hf.next();
_ = hf.next();
const size = std.fmt.parseInt(usize, hf.next() orelse return false, 10) catch return false;
const frame_end = nl + 1 + size + 1; // payload + trailing newline
if (frame_end > subset_stream.len) return false;
if (std.mem.indexOf(u8, full_stream, subset_stream[pos..frame_end]) == null) return false;
pos = frame_end;
}
return true;
}