Skip to content
zit Artifact [d8de704f52]
Login

Artifact [d8de704f52]

Artifact d8de704f525fe88a8ae24dcfdcfdc9798a45ff219422525658d0847cb0811e40:


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 bareStoreOf = support.bareStoreOf;
const commitHashDup = support.commitHashDup;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const fileExists = support.fileExists;
const framesTrailer = support.framesTrailer;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const initRepo = support.initRepo;
const machineHash = support.machineHash;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const runZitStdin = support.runZitStdin;
const spawnServe = support.spawnServe;
const storeHoldsArtifact = support.storeHoldsArtifact;
const storeShunRowCount = support.storeShunRowCount;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;

// ---------------------------------------------------------------------------
// `zit pack` reclaims deferred shuns (the pack filter). No `zit shun` verb exists
// yet, so these tests append the tombstone through the library — the same frame
// that verb will write — and then drive the REAL binary over it.
// ---------------------------------------------------------------------------

/// Appends a shun row naming the artifact in `dir/.zit` whose reconstructed
/// content equals `body`, and returns that artifact's raw digest (caller frees).
/// Finds the artifact by scanning rather than by recomputing a hash, so the test
/// does not have to know which hash algorithm the repository was initialized
/// with. Fails if no artifact holds those bytes — the premise, not an assertion
/// about pack.
fn shunArtifactByContent(
    gpa: std.mem.Allocator,
    io: std.Io,
    dir: std.Io.Dir,
    body: []const u8,
    reason: []const u8,
) ![]u8 {
    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);

    const rids = try log.ridsInPersistedOrder(gpa);
    defer gpa.free(rids);
    for (rids) |rid| {
        if (log.kindOf(rid) == .config) continue;
        const content = (try log.content(gpa, rid)) orelse continue;
        defer gpa.free(content);
        if (!std.mem.eql(u8, content, body)) continue;
        const digest = try gpa.dupe(u8, log.hashOf(rid));
        errdefer gpa.free(digest);
        try log.putShun(digest, reason, 1753000010);
        try log.commit();
        return digest;
    }
    return error.NoArtifactHoldsThoseBytes;
}

test "cli: zit pack reclaims a deferred shun, reports the count, and keeps the tombstone" {
    // A shun masks; a pack RECLAIMS. Between the two the bytes are still in the
    // store, which is the whole reason `zit pack` grew a filter — an operator who
    // was ordered to remove content needs one command that actually removes it,
    // and needs to be told how much it removed rather than inferring it from a
    // byte count that shrinks on an ordinary re-deltify too.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    // Two files so a pack that dropped everything cannot pass by accident.
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "doomed.txt", "content an operator was ordered to remove\n");
    try writeFile(io, tmp.dir, "keep.txt", "content with nothing against it\n");
    try addManaged(gpa, io, tmp.dir, &.{ "doomed.txt", "keep.txt" });
    const root = try commitHashDup(gpa, io, tmp.dir, "root");
    defer gpa.free(root);

    const doomed = try shunArtifactByContent(
        gpa,
        io,
        tmp.dir,
        "content an operator was ordered to remove\n",
        "takedown notice 4717",
    );
    defer gpa.free(doomed);

    // The premise: masked but STILL STORED. `cat` refuses it (the shun mask),
    // while the frame is physically there for the pack to reclaim.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "doomed.txt", root });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 8);
    }

    {
        var res = try runZit(gpa, io, tmp.dir, &.{"pack"});
        defer freeResult(gpa, &res);
        try expectExit(res.term, 0);
        try std.testing.expectEqualStrings("", res.stderr);
        // THE OPERATOR-VISIBLE PIN: the run says what it removed. Substring, not
        // the whole line — the artifact/byte counts are environment-dependent
        // and the exact wording is unit-tested in cmd/pack.zig.
        if (std.mem.indexOf(u8, res.stdout, "1 shunned reclaimed") == null) {
            std.debug.print("pack did not report the reclaim: {s}\n", .{res.stdout});
            return error.ReclaimCountNotReported;
        }
    }

    // The on-disk oracle, read back through the library: the bytes are gone from
    // the packed store — not merely masked — and the tombstone that named them is
    // not, so the artifact still reads as absent and a sync will not fetch it back.
    {
        var file = try tmp.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);
        try std.testing.expect(log.lookup(doomed) == null);
        try std.testing.expect(log.isShunned(doomed));
        const rows = log.shunList();
        try std.testing.expectEqual(@as(usize, 1), rows.len);
        try std.testing.expectEqualStrings("takedown notice 4717", rows[0].reason);
    }

    // The unshunned neighbour is still readable through the real binary, so the
    // reclaim removed exactly what it was told to and nothing adjacent.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "keep.txt", root });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 0);
        try std.testing.expectEqualStrings("content with nothing against it\n", res.stdout);
    }

    // A second pack has nothing left to reclaim and must say nothing about it.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{"pack"});
        defer freeResult(gpa, &res);
        try expectExit(res.term, 0);
        try std.testing.expect(std.mem.indexOf(u8, res.stdout, "shunned reclaimed") == null);
    }
}

test "cli pack -R: a bare store packs in place, reclaiming deferred shuns with the same generation swap" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.createDir(io, "src", .default_dir);
    var src = try tmp.dir.openDir(io, "src", .{});
    defer src.close(io);
    try initRepo(gpa, io, src);
    try bareStoreOf(gpa, io, src, tmp.dir, "s.zit");
    var bare = try tmp.dir.openDir(io, "s.zit", .{});
    defer bare.close(io);

    try writeFile(io, tmp.dir, "v1.txt", "a page to keep\n");
    try writeFile(io, tmp.dir, "n1.txt", "a technote to take down\n");
    var w1 = try runZitStdin(gpa, io, tmp.dir, "v1.txt", &.{ "wiki", "commit", "Page", "-R", "s.zit", "--user", "u" });
    defer freeResult(gpa, &w1);
    try expectExitDiag(w1.term, 0, w1);
    var t1 = try runZitStdin(gpa, io, tmp.dir, "n1.txt", &.{ "technote", "add", "-R", "s.zit", "--user", "u" });
    defer freeResult(gpa, &t1);
    try expectExitDiag(t1.term, 0, t1);
    const v1 = machineHash(w1.stdout);
    const n1 = machineHash(t1.stdout);

    var sh = try runZit(gpa, io, tmp.dir, &.{ "shun", "-R", "s.zit", "--defer", "--force", n1 });
    defer freeResult(gpa, &sh);
    try expectExitDiag(sh.term, 0, sh);
    try std.testing.expect(try storeHoldsArtifact(gpa, io, bare, n1));

    var boot = try runZit(gpa, io, tmp.dir, &.{ "frames", "-R", "s.zit" });
    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);

    {
        // The deferred row's bytes go; the row and everything else stay.
        var res = try runZit(gpa, io, tmp.dir, &.{ "pack", "-R", "s.zit" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try std.testing.expect(!try storeHoldsArtifact(gpa, io, bare, n1));
        try std.testing.expect(try storeHoldsArtifact(gpa, io, bare, v1));
        try std.testing.expectEqual(@as(usize, 1), try storeShunRowCount(gpa, io, bare));
    }
    {
        // A new generation, signalled exactly as a checkout's pack signals it.
        var poll = try runZit(gpa, io, tmp.dir, &.{ "frames", "-R", "s.zit", since0, verify0 });
        defer freeResult(gpa, &poll);
        try expectExitDiag(poll.term, 4, poll);
        var verified = try runZit(gpa, io, tmp.dir, &.{ "verify", "-R", "s.zit" });
        defer freeResult(gpa, &verified);
        try expectExitDiag(verified.term, 0, verified);
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "pack", "-R", "s.zit" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try std.testing.expectEqualStrings("already packed; nothing to do\n", res.stdout);
    }
    {
        // --drop-elided keeps its refusal: a bare store with no cone has
        // nothing elided.
        var res = try runZit(gpa, io, tmp.dir, &.{ "pack", "-R", "s.zit", "--drop-elided" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 1, res);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "needs a store cone") != null);
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "pack", "-R", "absent" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 1, res);
    }
}

test "cli pack: a repeat pack over an unchanged log is a no-op, and an append re-arms it" {
    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", repeat("alpha ", 200) ++ "\n");
    try writeFile(io, tmp.dir, "b.txt", repeat("alpha ", 199) ++ "beta\n");
    try addManaged(gpa, io, tmp.dir, &.{ "a.txt", "b.txt" });
    var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "t" }, &.{ "commit", "-m", "r" });
    defer freeResult(gpa, &c1);
    try expectExit(c1.term, 0);
    const t_ = std.testing;

    {
        var p = try runZit(gpa, io, tmp.dir, &.{"pack"});
        defer freeResult(gpa, &p);
        try expectExitDiag(p.term, 0, p);
        // A tiny store may pack to identical size, which renderPackStats
        // reports as "already packed: N artifacts walked ..." — still a REAL
        // walk. The stats line is the discriminator; the no-op gate prints
        // no stats at all.
        try t_.expect(std.mem.indexOf(u8, p.stdout, "artifacts walked") != null);
    }
    // Unchanged log: the recorded generation key matches, nothing is walked.
    {
        var p = try runZit(gpa, io, tmp.dir, &.{"pack"});
        defer freeResult(gpa, &p);
        try expectExitDiag(p.term, 0, p);
        try t_.expect(std.mem.indexOf(u8, p.stdout, "already packed; nothing to do") != null);
        try t_.expect(std.mem.indexOf(u8, p.stdout, "artifacts walked") == null);
    }
    // Any append (a commit here; a shun row or a pull equally) changes the
    // key, so the next pack runs for real — and re-records.
    try writeFile(io, tmp.dir, "a.txt", repeat("alpha ", 200) ++ "more\n");
    var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "t" }, &.{ "commit", "-m", "r2" });
    defer freeResult(gpa, &c2);
    try expectExit(c2.term, 0);
    {
        var p = try runZit(gpa, io, tmp.dir, &.{"pack"});
        defer freeResult(gpa, &p);
        try expectExitDiag(p.term, 0, p);
        try t_.expect(std.mem.indexOf(u8, p.stdout, "artifacts walked") != null);
    }
    // The ladder rule for the memo: deleting the advisory tier costs a real
    // pack, never an answer — and the repository still reads identically.
    try tmp.dir.deleteTree(io, ".zit-cache");
    {
        var p = try runZit(gpa, io, tmp.dir, &.{"pack"});
        defer freeResult(gpa, &p);
        try expectExitDiag(p.term, 0, p);
        try t_.expect(std.mem.indexOf(u8, p.stdout, "artifacts walked") != null);
    }
    var ls = try runZit(gpa, io, tmp.dir, &.{ "ls", "trunk" });
    defer freeResult(gpa, &ls);
    try expectExitDiag(ls.term, 0, ls);
    try t_.expect(std.mem.indexOf(u8, ls.stdout, "a.txt") != null);
    try t_.expect(std.mem.indexOf(u8, ls.stdout, "b.txt") != null);
}

test "cli pack --drop-elided: narrow-then-compact shrinks the store and harms nothing kept" {
    // Sparse-cone RFC section 5.4, the spec's only deletion. A sparse clone
    // (store cone src+tools) narrows to src via update --cone -- store policy
    // follows the worktree BECAUSE the store is already sparse -- then an
    // explicit pack --drop-elided reclaims tools' blob. The store shrinks,
    // verify stays healthy, in-cone content is untouched, and on a DENSE
    // store the flag refuses rather than guesses.
    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", "keep me\n");
    try writeFile(io, tmp_a.dir, "tools/big.bin", repeat("x", 4096) ++ "\n");
    try addManaged(gpa, io, tmp_a.dir, &.{ "src/a.zig", "tools/big.bin" });
    const head = try commitHashDup(gpa, io, tmp_a.dir, "seed");
    defer gpa.free(head);

    // Refusal on the dense origin: nothing is "elided" without a store cone.
    var refuse = try runZit(gpa, io, tmp_a.dir, &.{ "pack", "--drop-elided" });
    defer freeResult(gpa, &refuse);
    try expectExit(refuse.term, 1);
    try std.testing.expect(std.mem.indexOf(u8, refuse.stderr, "needs a store cone") != null);

    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=tools", 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=tools" });
    defer freeResult(gpa, &op);
    try expectExitDiag(op.term, 0, op);

    // Narrow to src: store policy follows (the store is sparse); tools'
    // blob is now elided-by-policy though still physically held.
    var nr = try runZit(gpa, io, b_dir, &.{ "update", "--cone=src", head });
    defer freeResult(gpa, &nr);
    try expectExitDiag(nr.term, 0, nr);
    try std.testing.expect(!fileExists(io, b_dir, "tools/big.bin"));
    const size_before = blk: {
        const st = try b_dir.statFile(io, ".zit", .{});
        break :blk st.size;
    };

    var compact = try runZit(gpa, io, b_dir, &.{ "pack", "--drop-elided" });
    defer freeResult(gpa, &compact);
    try expectExitDiag(compact.term, 0, compact);
    const size_after = blk: {
        const st = try b_dir.statFile(io, ".zit", .{});
        break :blk st.size;
    };
    try std.testing.expect(size_after < size_before);

    var cat_keep = try runZit(gpa, io, b_dir, &.{ "cat", "src/a.zig" });
    defer freeResult(gpa, &cat_keep);
    try expectExitDiag(cat_keep.term, 0, cat_keep);
    try std.testing.expectEqualStrings("keep me\n", cat_keep.stdout);
    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, "sparse store") != null);
}