Skip to content
zit Artifact [b7191e8e2a]
Login

Artifact [b7191e8e2a]

Artifact b7191e8e2a4549d5827e0f8441d43853637e5a589c867152663c8feed4bddcda:


const std = @import("std");
const repeat = @import("zit").repeat;
const logstore = @import("zit").logstore;
const storage = @import("zit").storage;
const support = @import("support.zig");
const addManaged = support.addManaged;
const collectArtifactDigests = support.collectArtifactDigests;
const commitHashDup = support.commitHashDup;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const freeDigests = support.freeDigests;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const statField = support.statField;
const writeFile = support.writeFile;

test "stat: a BARE store answers project-code under -R, with no checkout, port, or child process" {
    // The blocker this issue names: a repository's project code was obtainable
    // only by speaking the sync protocol to a server you started yourself.
    // A consumer needing one constant had to find a free port, spawn `zit
    // serve`, poll for readiness, POST a clone container and scrape the push
    // card -- a subprocess and a listening socket per repository, with a
    // five-second worst case at startup.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.createDir(io, "wt", .default_dir);
    var wt = try tmp.dir.openDir(io, "wt", .{});
    defer wt.close(io);
    try initRepo(gpa, io, wt);
    try writeFile(io, wt, "f.txt", "x\n");
    try addManaged(gpa, io, wt, &.{"f.txt"});
    var c = try runCommitEnv(gpa, io, wt, .{ .user = "u" }, &.{ "commit", "-m", "one" });
    defer freeResult(gpa, &c);
    try expectExitDiag(c.term, 0, c);

    // The code as the checkout reports it, to compare against.
    var in_wt = try runZit(gpa, io, wt, &.{"stat"});
    defer freeResult(gpa, &in_wt);
    try expectExitDiag(in_wt.term, 0, in_wt);
    const pc_line = "project-code ";
    const at = std.mem.indexOf(u8, in_wt.stdout, pc_line).?;
    const end = std.mem.indexOfScalarPos(u8, in_wt.stdout, at, '\n').?;
    const code = in_wt.stdout[at + pc_line.len .. end];
    try std.testing.expect(code.len == 40 or code.len == 64);

    // A BARE store: a directory holding `.zit` and nothing else -- no checkout
    // record, which is exactly what a forge serves.
    try tmp.dir.createDir(io, "bare", .default_dir);
    var bare = try tmp.dir.openDir(io, "bare", .{});
    defer bare.close(io);
    const store_bytes = try wt.readFileAlloc(io, ".zit", gpa, .limited(64 * 1024 * 1024));
    defer gpa.free(store_bytes);
    try writeFile(io, bare, ".zit", store_bytes);

    var bare_stat = try runZit(gpa, io, tmp.dir, &.{ "stat", "-R", "bare" });
    defer freeResult(gpa, &bare_stat);
    try expectExitDiag(bare_stat.term, 0, bare_stat);
    // Same identity, read without a working tree.
    const b_at = std.mem.indexOf(u8, bare_stat.stdout, pc_line).?;
    const b_end = std.mem.indexOfScalarPos(u8, bare_stat.stdout, b_at, '\n').?;
    try std.testing.expectEqualStrings(code, bare_stat.stdout[b_at + pc_line.len .. b_end]);
    try std.testing.expect(std.mem.indexOf(u8, bare_stat.stdout, "hash-policy ") != null);
    // And the census still answers, so one read serves both questions.
    try std.testing.expect(std.mem.indexOf(u8, bare_stat.stdout, "artifacts ") != null);
}

test "stat: the no-checkout diagnostic no longer claims .zit is missing when it is present" {
    // The second defect this issue flags. Discovery requires a CHECKOUT, but the
    // message said "no .zit found" -- in a directory where `.zit` was sitting
    // right there. An operator reading it had every reason to suspect a corrupt
    // repository rather than a missing working tree.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.createDir(io, "wt", .default_dir);
    var wt = try tmp.dir.openDir(io, "wt", .{});
    defer wt.close(io);
    try initRepo(gpa, io, wt);
    const store_bytes = try wt.readFileAlloc(io, ".zit", gpa, .limited(64 * 1024 * 1024));
    defer gpa.free(store_bytes);
    try tmp.dir.createDir(io, "bare", .default_dir);
    var bare = try tmp.dir.openDir(io, "bare", .{});
    defer bare.close(io);
    try writeFile(io, bare, ".zit", store_bytes);

    var st = try runZit(gpa, io, bare, &.{"status"});
    defer freeResult(gpa, &st);
    try expectExit(st.term, 1);
    // It must not assert the absent thing is `.zit`, because `.zit` is here.
    try std.testing.expect(std.mem.indexOf(u8, st.stderr, "no .zit found") == null);
    try std.testing.expect(std.mem.indexOf(u8, st.stderr, ".zit-checkout") != null);
    // And it names the remedy that actually works from here.
    try std.testing.expect(std.mem.indexOf(u8, st.stderr, "-R") != null);
}

/// Independent oracles for the two measured byte fields, sourced from the
/// store's own per-rid index rather than from `storestat.walk` — which is what
/// the verb totals through. Sharing no arithmetic with the code under test is
/// the whole point: a `content-bytes` the verb computed some other way has to
/// match a number assembled here from `payloadLenOf`, frame by frame.
const StatOracle = struct {
    /// Sum of the on-disk payload length of every non-config record.
    content: u64,
    /// Sum of each of those frames' header + padding (`frameSpanOf` less the
    /// payload), which is what `frame-overhead-bytes` must equal.
    overhead: u64,
    artifacts: u64,
};

fn statOracle(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !StatOracle {
    var file = try dir.openFile(io, ".zit", .{ .mode = .read_only });
    defer file.close(io);
    const st = try file.stat(io);
    var fs = storage.FileStorage.init(io, file, st.size);
    var log = try logstore.LogStore.openReadOnly(gpa, fs.storageFile());
    defer log.deinit(gpa);

    const rids = try log.ridsInPersistedOrder(gpa);
    defer gpa.free(rids);
    var o = StatOracle{ .content = 0, .overhead = 0, .artifacts = 0 };
    for (rids) |rid| {
        if (log.kindOf(rid) == .config) continue;
        const payload = log.payloadLenOf(rid);
        o.content += payload;
        o.overhead += log.frameSpanOf(rid) - payload;
        o.artifacts += 1;
    }
    return o;
}

/// Builds a repository with two committed generations of one file plus a
/// second file, so the store holds manifests, full-form blobs, and at least
/// one delta frame.
fn statCorpus(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !void {
    try initRepo(gpa, io, dir);
    try writeFile(io, dir, "poem.txt", repeat("the sea is calm tonight\n", 40));
    try writeFile(io, dir, "notes.txt", repeat("alpha beta gamma\n", 20));
    try addManaged(gpa, io, dir, &.{ "poem.txt", "notes.txt" });
    const h1 = try commitHashDup(gpa, io, dir, "first");
    gpa.free(h1);
    try writeFile(io, dir, "poem.txt", repeat("the sea is calm tonight\n", 39) ++ "the tide is full\n");
    const h2 = try commitHashDup(gpa, io, dir, "second");
    gpa.free(h2);
}

test "e2e stat: content bytes are measured by the frame walk and never the file size" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try statCorpus(gpa, io, tmp.dir);

    var res = try runZit(gpa, io, tmp.dir, &.{"stat"});
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    try std.testing.expectEqualStrings("", res.stderr);

    // store-bytes is the file; nothing else may claim to be it.
    var f = try tmp.dir.openFile(io, ".zit", .{ .mode = .read_only });
    defer f.close(io);
    const st = try f.stat(io);
    try std.testing.expectEqual(st.size, try statField(res.stdout, "store-bytes"));

    // THE honesty property, against an oracle the verb does not share code
    // with: content-bytes must equal the sum of the stored frames' payload
    // lengths, taken from the store's own index (`statOracle`). A figure
    // derived from the `.zit` size fails here — `st.size` itself, `st.size`
    // less the header, the total on-disk frame span, any of them — because the
    // payload total is assembled frame by frame and never touches the size.
    //
    // What this cannot distinguish, and no test on the printed value can: a
    // figure that subtracts the walk's OWN overhead and config totals from the
    // size. Exact accounting makes that arithmetically identical to the
    // measurement — it IS the measurement, routed through a subtraction. The
    // property under test is the number, not the expression that produced it.
    const oracle = try statOracle(gpa, io, tmp.dir);
    const content = try statField(res.stdout, "content-bytes");
    try std.testing.expect(oracle.content > 0); // the corpus really has content
    try std.testing.expectEqual(oracle.content, content);
    try std.testing.expect(content < st.size);

    // frame-overhead-bytes is independently sourced the same way, so the
    // overhead cannot be back-computed as "whatever makes the file add up"
    // while content is wrong by the same amount.
    const overhead = try statField(res.stdout, "frame-overhead-bytes");
    try std.testing.expect(oracle.overhead > 0);
    try std.testing.expectEqual(oracle.overhead, overhead);

    // Exact accounting, reproduced at the CLI surface: every byte of the file
    // is either the header, a frame payload, or frame overhead. With content
    // and overhead both pinned above, this pins config-bytes too.
    const header = try statField(res.stdout, "header-bytes");
    const config = try statField(res.stdout, "config-bytes");
    try std.testing.expectEqual(st.size, header + content + overhead + config);
    try std.testing.expect(config > 0); // the config record is a real frame

    // The artifact census comes from the same walk as the bytes.
    const digests = try collectArtifactDigests(gpa, io, tmp.dir);
    defer freeDigests(gpa, digests);
    try std.testing.expectEqual(@as(u64, digests.len), try statField(res.stdout, "artifacts"));
    try std.testing.expectEqual(oracle.artifacts, @as(u64, digests.len));
    const deltas = try statField(res.stdout, "deltas");
    const full = try statField(res.stdout, "full-form");
    try std.testing.expectEqual(@as(u64, digests.len), deltas + full);
    try std.testing.expect(deltas > 0); // the second poem.txt generation deltas
}

test "e2e stat: --artifacts enumerates every stored hash, one per line, sorted" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try statCorpus(gpa, io, tmp.dir);

    var res = try runZit(gpa, io, tmp.dir, &.{ "stat", "--artifacts" });
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    try std.testing.expectEqualStrings("", res.stderr);

    const digests = try collectArtifactDigests(gpa, io, tmp.dir);
    defer freeDigests(gpa, digests);

    var expected: std.ArrayListUnmanaged(u8) = .empty;
    defer expected.deinit(gpa);
    for (digests) |d| {
        var hex: [2 * logstore.max_hash_len + 1]u8 = undefined;
        const line = try std.fmt.bufPrint(&hex, "{x}\n", .{d});
        try expected.appendSlice(gpa, line);
    }
    // Byte-for-byte: same set, same sorted order, one hash per line.
    try std.testing.expectEqualStrings(expected.items, res.stdout);
}

test "e2e stat: --type=delta selects the delta-form artifacts only" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try statCorpus(gpa, io, tmp.dir);

    var all = try runZit(gpa, io, tmp.dir, &.{ "stat", "--artifacts" });
    defer freeResult(gpa, &all);
    try expectExit(all.term, 0);

    var only_delta = try runZit(gpa, io, tmp.dir, &.{ "stat", "--artifacts", "--type=delta" });
    defer freeResult(gpa, &only_delta);
    try expectExit(only_delta.term, 0);

    var only_full = try runZit(gpa, io, tmp.dir, &.{ "stat", "--artifacts", "--type=full" });
    defer freeResult(gpa, &only_full);
    try expectExit(only_full.term, 0);

    const n = std.mem.count(u8, all.stdout, "\n");
    const nd = std.mem.count(u8, only_delta.stdout, "\n");
    const nf = std.mem.count(u8, only_full.stdout, "\n");
    try std.testing.expect(nd > 0);
    try std.testing.expect(nf > 0);
    try std.testing.expectEqual(n, nd + nf); // the filter partitions, it does not drop
    // The counts agree with the summary from the same walk.
    var summary = try runZit(gpa, io, tmp.dir, &.{"stat"});
    defer freeResult(gpa, &summary);
    try std.testing.expectEqual(@as(u64, nd), try statField(summary.stdout, "deltas"));
    try std.testing.expectEqual(@as(u64, nf), try statField(summary.stdout, "full-form"));
}

/// The verbatim `zit stat` help — an independent transcription of `main.zig`'s
/// `stat_help`, the same house convention every other verb's help is held to,
/// so a drift between the two is caught here. The field list is the verb's
/// documented contract; editing one side alone must not be silent.
const stat_help_expected =
    \\usage: zit stat [--artifacts] [--type=full|delta] [-R <path>] [-h|--help]
    \\
    \\Report what the repository store actually holds. Every number is read
    \\from the frames in `.zit` — nothing is estimated, and nothing is derived
    \\from the file's size.
    \\
    \\Output is one 'field value' line per fact, so it reads by machine:
    \\
    \\  project-code          this repository's shared sync identity, the
    \\                        value a peer matches on and the first input to
    \\                        an authenticated push's derived secret. Empty on
    \\                        a repository that predates project codes.
    \\  hash-policy           which names this repository mints
    \\  store-bytes           the `.zit` file itself, in bytes
    \\  header-bytes          the store header
    \\  content-bytes         artifact payload bytes, summed frame by frame
    \\  frame-overhead-bytes  frame headers and 8-byte padding for those frames
    \\  config-bytes          the repository's config record
    \\  shun-bytes            the repository's shun table
    \\  shun-rows             rows in that table
    \\  user-bytes            the repository's user table
    \\  user-frames           frames in that table -- rows plus any
    \\                        supersede markers a pack has not yet reclaimed,
    \\                        so this can exceed what `zit users` prints
    \\  artifacts             stored artifacts (the config record is not one)
    \\  full-form             artifacts stored whole
    \\  full-form-bytes       content-bytes held by those artifacts
    \\  deltas                artifacts stored against a base
    \\  delta-bytes           content-bytes held by those artifacts
    \\
    \\-R names a repository with no working tree, so a bare store answers all
    \\of the above without a checkout, a port, or a child process.
    \\
    \\content-bytes and store-bytes are different measurements and must not be
    \\substituted for one another: the store also carries the header, a header
    \\per frame, and padding, so it is always the larger number. The five byte
    \\fields plus header-bytes account for the file exactly.
    \\
    \\full-form-bytes and delta-bytes PARTITION content-bytes; they are not
    \\further components of the file and must not be added to the accounting
    \\above. Read them against full-form/deltas rather than instead of them: a
    \\store is routinely a minority full-form by artifact and a large majority
    \\full-form by byte, because an artifact is stored whole exactly when
    \\delta did not pay -- the large, poorly-compressible ones.
    \\
    \\options:
    \\  -h, --help    show this help and exit
    \\  --artifacts   list the stored artifact hashes instead, one per line,
    \\                in ascending hash order
    \\  --type=KIND   with --artifacts, list only 'full' or 'delta' artifacts
    \\
    \\exit status:
    \\  0  the report was written
    \\  1  this is not a zit repository, or `.zit` could not be read
    \\  2  usage error (an unknown option, a repeated --type, a --type without
    \\     --artifacts, or an unexpected argument)
    \\
;

test "e2e stat: --help prints the verbatim help to stdout, exit 0, outside a repository too" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    // Help precedes every other reading of the command line: before the
    // repository lookup, and ahead of a trailing bad token or a real option.
    const invocations = [_][]const []const u8{
        &.{ "stat", "--help" },
        &.{ "stat", "-h" },
        &.{ "stat", "--help", "foo" },
        &.{ "stat", "-h", "--all" },
        &.{ "stat", "--artifacts", "--help" },
    };
    for (invocations) |argv| {
        var res = try runZit(gpa, io, tmp.dir, argv);
        defer freeResult(gpa, &res);
        try expectExit(res.term, 0);
        try std.testing.expectEqualStrings(stat_help_expected, res.stdout);
        try std.testing.expectEqualStrings("", res.stderr);
    }

    // `zit help stat` routes to the same block, not a second copy of it.
    try initRepo(gpa, io, tmp.dir);
    var topic = try runZit(gpa, io, tmp.dir, &.{ "help", "stat" });
    defer freeResult(gpa, &topic);
    try expectExit(topic.term, 0);
    try std.testing.expectEqualStrings(stat_help_expected, topic.stdout);
}

test "e2e stat: a repeated --type is a usage error, not a silent last-wins" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try statCorpus(gpa, io, tmp.dir);

    // Two --type values cannot both be honoured, and answering the second one
    // silently would report a roll the user did not ask for.
    var res = try runZit(gpa, io, tmp.dir, &.{ "stat", "--artifacts", "--type=full", "--type=delta" });
    defer freeResult(gpa, &res);
    try expectExit(res.term, 2);
    try std.testing.expectEqualStrings("", res.stdout);
    try std.testing.expect(std.mem.indexOf(u8, res.stderr, "--type") != null);
    try std.testing.expect(std.mem.indexOf(u8, res.stderr, "more than once") != null);

    // The same value twice is still two --type options; it is not a special case.
    var same = try runZit(gpa, io, tmp.dir, &.{ "stat", "--artifacts", "--type=full", "--type=full" });
    defer freeResult(gpa, &same);
    try expectExit(same.term, 2);
    try std.testing.expectEqualStrings("", same.stdout);
}