Skip to content
zit Artifact [35dbe8c2bb]
Login

Artifact [35dbe8c2bb]

Artifact 35dbe8c2bbd4f3e11680aac27d4ff88466f925e151d5b50d44c15456d879b562:


const std = @import("std");
const logstore = @import("zit").logstore;
const storage = @import("zit").storage;
const zit = @import("zit");
const File = std.Io.File;
const support = @import("support.zig");
const addManaged = support.addManaged;
const appendArtifact = support.appendArtifact;
const checkinHash = support.checkinHash;
const chmodFile = support.chmodFile;
const commitHashDup = support.commitHashDup;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const fileExists = support.fileExists;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const readFile = support.readFile;
const renameRun = support.renameRun;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const runZitEnv = support.runZitEnv;
const tipHashOf = support.tipHashOf;
const withZCard = support.withZCard;
const writeFile = support.writeFile;

// ==========================================================================
// `zit ls` end-to-end. Lists a check-in's effective file set, one
// `<blob-id> <filename>` line per file in canonical §4.5.3 order. The blob id
// oracle is the artifact §7.2 sha3-256 of the file content, computed here from
// the RFC rule — never captured from zit or Fossil.
// ==========================================================================

/// Appends one `ls` line — `<sha3-256 hex> SP <name> LF` — computed from the
/// §7.2 hash-over-content rule, so the e2e expectation is RFC-derived.
fn appendLsLine(gpa: std.mem.Allocator, list: *std.ArrayList(u8), name: []const u8, content: []const u8) !void {
    // `ls` lists names, as Fossil's does; the hash rides under
    // --long. `content` is kept by the callers for the --long checks.
    _ = content;
    try list.appendSlice(gpa, name);
    try list.append(gpa, '\n');
}

test "cli ls §8.2: prints each effective file's hash and name for a resolved check-in" {
    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 writeFile(io, tmp.dir, "b.txt", "beta\n");
    try addManaged(gpa, io, tmp.dir, &.{ "a.txt", "b.txt" });
    var commit = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "root" });
    defer freeResult(gpa, &commit);
    try expectExit(commit.term, 0);
    const hash = try checkinHash(commit.stdout);

    // The effective set is both files, in canonical filename order (a.txt < b.txt),
    // each `<sha3-256(content)> <name>`.
    var expected: std.ArrayList(u8) = .empty;
    defer expected.deinit(gpa);
    try appendLsLine(gpa, &expected, "a.txt", "alpha\n");
    try appendLsLine(gpa, &expected, "b.txt", "beta\n");

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

test "cli ls: no VERSION lists the current checkout; unborn checkout is a failure" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;

    // (a) After a commit the checkout names the new check-in; bare `zit ls` lists
    // exactly that check-in's files.
    {
        var tmp = std.testing.tmpDir(.{});
        defer tmp.cleanup();
        try initRepo(gpa, io, tmp.dir);
        try writeFile(io, tmp.dir, "only.txt", "content\n");
        try addManaged(gpa, io, tmp.dir, &.{"only.txt"});
        var commit = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "root" });
        defer freeResult(gpa, &commit);
        try expectExit(commit.term, 0);

        var expected: std.ArrayList(u8) = .empty;
        defer expected.deinit(gpa);
        try appendLsLine(gpa, &expected, "only.txt", "content\n");

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

    // (b) A fresh repository's checkout is unborn: bare `zit ls` has nothing to
    // list — exit 1, diagnostic on stderr, empty stdout.
    {
        var tmp = std.testing.tmpDir(.{});
        defer tmp.cleanup();
        try initRepo(gpa, io, tmp.dir);

        var res = try runZit(gpa, io, tmp.dir, &.{"ls"});
        defer freeResult(gpa, &res);
        try expectExit(res.term, 1);
        try std.testing.expectEqualStrings("", res.stdout);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "unborn") != null);
    }
}

test "cli ls §8.3: an unknown version is a resolve failure (exit 1); not-a-repo is a failure" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;

    // (a) In a real repo, a name no check-in carries is a §8.3 resolution failure:
    // exit 1, diagnostic on stderr, empty stdout.
    {
        var tmp = std.testing.tmpDir(.{});
        defer tmp.cleanup();
        try initRepo(gpa, io, tmp.dir);
        try writeFile(io, tmp.dir, "a.txt", "root\n");
        try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
        var commit = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "root" });
        defer freeResult(gpa, &commit);
        try expectExit(commit.term, 0);

        var res = try runZit(gpa, io, tmp.dir, &.{ "ls", "nosuch" });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 1);
        try std.testing.expectEqualStrings("", res.stdout);
        try std.testing.expectEqualStrings("zit ls: check-in 'nosuch' not found\n", res.stderr);
    }

    // (b) Outside any repository, `zit ls` fails to discover a repo: exit 1,
    // not-a-repo diagnostic on stderr, empty stdout.
    {
        var tmp = std.testing.tmpDir(.{});
        defer tmp.cleanup();
        var res = try runZit(gpa, io, tmp.dir, &.{"ls"});
        defer freeResult(gpa, &res);
        try expectExit(res.term, 1);
        try std.testing.expectEqualStrings("", res.stdout);
        try std.testing.expect(std.mem.indexOf(u8, res.stderr, "no zit checkout here") != null);
    }
}

test "cli ls: --long reports the permission token, size and content class; --under bounds the listing" {
    // Goal: the three facts a file listing needs. The permission token is the
    // one that cannot be recovered by spending more — it lives in the F card,
    // and the blob it names does not know it is executable — so it is the
    // assertion that matters most here.
    if (comptime !File.Permissions.has_executable_bit) return error.SkipZigTest;
    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, "readme.md", "plain text\n");
    try tmp.dir.createDir(io, "tools", .default_dir);
    try writeFile(io, tmp.dir, "tools/run.sh", "#!/bin/sh\necho hi\n");
    try tmp.dir.createDir(io, "src", .default_dir);
    try writeFile(io, tmp.dir, "src/blob.bin", "a\x00b\n");
    try chmodFile(io, tmp.dir, "tools/run.sh", 0o755);
    try addManaged(gpa, io, tmp.dir, &.{ "readme.md", "tools/run.sh", "src/blob.bin" });
    var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
    defer freeResult(gpa, &c);
    try expectExitDiag(c.term, 0, c);

    // The default listing is untouched: two fields, no content read.
    var plain = try runZit(gpa, io, tmp.dir, &.{"ls"});
    defer freeResult(gpa, &plain);
    try expectExit(plain.term, 0);
    var plain_lines = std.mem.tokenizeScalar(u8, plain.stdout, '\n');
    while (plain_lines.next()) |line| {
        var f = std.mem.tokenizeScalar(u8, line, ' ');
        _ = f.next(); // hash
        _ = f.next(); // filename
        try std.testing.expect(f.next() == null); // and nothing else
    }

    var long = try runZit(gpa, io, tmp.dir, &.{ "ls", "--long" });
    defer freeResult(gpa, &long);
    try expectExitDiag(long.term, 0, long);

    // The executable carries its token; the others carry the '-' placeholder.
    try std.testing.expect(std.mem.indexOf(u8, long.stdout, " x text tools/run.sh") != null);
    try std.testing.expect(std.mem.indexOf(u8, long.stdout, " - text readme.md") != null);
    // A NUL in the content classifies as binary, not text.
    try std.testing.expect(std.mem.indexOf(u8, long.stdout, " - binary src/blob.bin") != null);
    // Size is measured, not guessed: "plain text\n" is 11 bytes.
    try std.testing.expect(std.mem.indexOf(u8, long.stdout, " 11 - text readme.md") != null);

    // --under bounds the listing to one directory, on a segment boundary.
    var under = try runZit(gpa, io, tmp.dir, &.{ "ls", "--long", "--under=src" });
    defer freeResult(gpa, &under);
    try expectExit(under.term, 0);
    try std.testing.expect(std.mem.indexOf(u8, under.stdout, "src/blob.bin") != null);
    try std.testing.expect(std.mem.indexOf(u8, under.stdout, "readme.md") == null);
    try std.testing.expect(std.mem.indexOf(u8, under.stdout, "tools/run.sh") == null);
}

test "cli ls: --long records each blob's class once, and a later listing reads it back unchanged" {
    // The class is a function of a blob's bytes, so a listing that has read a
    // blob records it. The next listing reuses that classification and reads
    // only the size header. The answer must not move, and a recorded class
    // must not hide a blob the store no longer surfaces.
    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, "readme.md", "plain text\n");
    try writeFile(io, tmp.dir, "blob.bin", "a\x00b\n");
    try writeFile(io, tmp.dir, "copy.md", "plain text\n"); // the same blob twice
    try addManaged(gpa, io, tmp.dir, &.{ "readme.md", "blob.bin", "copy.md" });
    var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
    defer freeResult(gpa, &c);
    try expectExitDiag(c.term, 0, c);
    // A later check-in, so the first one's blobs are not the tip's alone and
    // `shun` will take one of them.
    try writeFile(io, tmp.dir, "later.txt", "later\n");
    try addManaged(gpa, io, tmp.dir, &.{"later.txt"});
    var c2 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "later" });
    defer freeResult(gpa, &c2);
    try expectExitDiag(c2.term, 0, c2);

    const memo = ".zit-cache/blobclass";
    // --no-cache reads afresh and records nothing.
    var bypass = try runZit(gpa, io, tmp.dir, &.{ "ls", "--long", "--no-cache" });
    defer freeResult(gpa, &bypass);
    try expectExitDiag(bypass.term, 0, bypass);
    try std.testing.expect(!fileExists(io, tmp.dir, memo));

    var cold = try runZit(gpa, io, tmp.dir, &.{ "ls", "--long" });
    defer freeResult(gpa, &cold);
    try expectExitDiag(cold.term, 0, cold);
    try std.testing.expectEqualStrings(bypass.stdout, cold.stdout);
    // One record per distinct blob listed: readme.md and copy.md share one.
    const stored = try readFile(gpa, io, tmp.dir, memo);
    defer gpa.free(stored);
    try std.testing.expectEqual(zit.blobclass.header_len + 3 * zit.blobclass.record_len + 4, stored.len);

    var warm = try runZit(gpa, io, tmp.dir, &.{ "ls", "--long" });
    defer freeResult(gpa, &warm);
    try expectExitDiag(warm.term, 0, warm);
    try std.testing.expectEqualStrings(cold.stdout, warm.stdout);
    try std.testing.expect(std.mem.indexOf(u8, warm.stdout, " 4 - binary blob.bin") != null);
    try std.testing.expect(std.mem.indexOf(u8, warm.stdout, " 11 - text readme.md") != null);
    // Nothing new was learned, so the record was not rewritten.
    const restored = try readFile(gpa, io, tmp.dir, memo);
    defer gpa.free(restored);
    try std.testing.expectEqualSlices(u8, stored, restored);

    // A damaged record is a miss, not an error, and the answer is unchanged.
    try writeFile(io, tmp.dir, memo, "not a memo");
    var damaged = try runZit(gpa, io, tmp.dir, &.{ "ls", "--long" });
    defer freeResult(gpa, &damaged);
    try expectExitDiag(damaged.term, 0, damaged);
    try std.testing.expectEqualStrings(cold.stdout, damaged.stdout);

    // Shun the binary blob, whose class is now recorded: the listing still
    // reports it absent.
    const line_start = std.mem.indexOf(u8, warm.stdout, " - binary blob.bin").?;
    const row = warm.stdout[0..line_start];
    const row_start = if (std.mem.lastIndexOfScalar(u8, row, '\n')) |i| i + 1 else 0;
    const blob = row[row_start..std.mem.indexOfScalarPos(u8, row, row_start, ' ').?];
    var sh = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", blob });
    defer freeResult(gpa, &sh);
    try expectExitDiag(sh.term, 0, sh);
    var after = try runZit(gpa, io, tmp.dir, &.{ "ls", "--long" });
    defer freeResult(gpa, &after);
    try expectExitDiag(after.term, 0, after);
    const want = try std.fmt.allocPrint(gpa, "{s} 0 - absent blob.bin\n", .{blob});
    defer gpa.free(want);
    try std.testing.expect(std.mem.indexOf(u8, after.stdout, want) != null);
    try std.testing.expect(std.mem.indexOf(u8, after.stdout, " 11 - text readme.md") != null);
}

test "cli ls: a memoized class answers --long without reading content, so verify, not ls, finds a corrupt delta basis" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    const basis = "basis content for a delta\n";
    const target = "basis content for a delta, changed\n";
    var basis_hash: [32]u8 = undefined;
    var target_hash: [32]u8 = undefined;
    std.crypto.hash.sha3.Sha3_256.hash(basis, &basis_hash, .{});
    std.crypto.hash.sha3.Sha3_256.hash(target, &target_hash, .{});
    var basis_offset: u64 = undefined;
    {
        var file = try tmp.dir.openFile(io, ".zit", .{ .mode = .read_write });
        defer file.close(io);
        var fs = storage.FileStorage.init(io, file, (try file.stat(io)).size);
        var log = try logstore.LogStore.open(gpa, fs.storageFile(), 0);
        defer log.deinit(gpa);
        const base_rid = try log.put(&basis_hash, basis);
        basis_offset = log.offsetOf(base_rid);
        const raw_delta = try zit.delta.create(gpa, basis, target);
        defer gpa.free(raw_delta);
        _ = try log.putDelta(&target_hash, base_rid, raw_delta);
        try log.commit();
    }
    const target_name = std.fmt.bytesToHex(target_hash, .lower);
    const body = try std.fmt.allocPrint(gpa, "C fixture\nD 2020-01-01T00:00:00\nF file.txt {s}\nU tester\n", .{target_name});
    defer gpa.free(body);
    const manifest = try withZCard(gpa, body);
    defer gpa.free(manifest);
    const checkin = try appendArtifact(gpa, io, tmp.dir, manifest);
    defer gpa.free(checkin);
    var warm = try runZit(gpa, io, tmp.dir, &.{ "ls", checkin, "--long" });
    defer freeResult(gpa, &warm);
    try expectExitDiag(warm.term, 0, warm);
    try std.testing.expect(std.mem.indexOf(u8, warm.stdout, " - text file.txt") != null);
    // Damage only the basis payload, leaving the target frame and log tail
    // intact. The target's size header still reads, but reconstruction fails.
    {
        var file = try tmp.dir.openFile(io, ".zit", .{ .mode = .read_write });
        defer file.close(io);
        // Frame header: fixed 22 bytes plus this SHA3-256 digest.
        try file.writePositionalAll(io, "X", basis_offset + 22 + 32);
        try file.sync(io);
    }
    // The memoized listing reads the target's size header and its recorded
    // class, as Fossil's `ls -v` reads its blob table: it never touches the
    // basis, so it cannot see the damage and does not claim to.
    var cached = try runZitEnv(gpa, io, tmp.dir, &ls_content_seam, &.{ "ls", checkin, "--long" });
    defer freeResult(gpa, &cached);
    try expectExitDiag(cached.term, 0, cached);
    try std.testing.expectEqualStrings(warm.stdout, cached.stdout);
    try std.testing.expectEqual(@as(u64, 0), lsContentReadsOf(&cached) orelse return error.NoCounter);
    // A listing that does reconstruct still reports what it could not read.
    var uncached = try runZit(gpa, io, tmp.dir, &.{ "ls", checkin, "--long", "--no-cache" });
    defer freeResult(gpa, &uncached);
    try expectExitDiag(uncached.term, 0, uncached);
    try std.testing.expect(std.mem.indexOf(u8, uncached.stdout, " 0 - absent file.txt") != null);
    // Proving the content is readable is `verify`'s job.
    var ver = try runZit(gpa, io, tmp.dir, &.{"verify"});
    defer freeResult(gpa, &ver);
    try std.testing.expect(ver.term != .exited or ver.term.exited != 0);
}

/// Asks a verb to report on exit how many listed blobs `ls` reconstructed.
const ls_content_seam = [_][2][]const u8{.{ "ZIT_LS_CONTENT_READS", "1" }};

/// The `ZIT_LS_CONTENT_READS` count a verb reports, or null when it did not
/// report one.
fn lsContentReadsOf(res: *const std.process.RunResult) ?u64 {
    const marker = "ls-content-reads: ";
    const at = std.mem.lastIndexOf(u8, res.stderr, marker) orelse return null;
    const rest = res.stderr[at + marker.len ..];
    const end = std.mem.indexOfScalar(u8, rest, '\n') orelse rest.len;
    return std.fmt.parseInt(u64, std.mem.trim(u8, rest[0..end], " \r"), 10) catch null;
}

/// Runs `argv` in `dir` under the content-read seam and returns stdout and
/// the count, failing on a non-zero exit.
fn lsLongRun(arena: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, argv: []const []const u8) !struct { []const u8, u64 } {
    const res = try runZitEnv(arena, io, dir, &ls_content_seam, argv);
    try expectExitDiag(res.term, 0, res);
    return .{ res.stdout, lsContentReadsOf(&res) orelse return error.NoCounter };
}

/// The blob id `ls --long` printed for `name`.
fn lsLongBlobOf(listing: []const u8, name: []const u8) ![]const u8 {
    var lines = std.mem.splitScalar(u8, listing, '\n');
    while (lines.next()) |line| {
        const sp = std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue;
        if (std.mem.eql(u8, line[sp + 1 ..], name)) return line[0..std.mem.indexOfScalar(u8, line, ' ').?];
    }
    return error.NotListed;
}

/// A flat store whose history has delta chains, a binary file, an
/// executable, and a tip check-in carrying a symlink card, which `commit`
/// never mints.
fn lsLongScene(arena: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !void {
    _ = try renameRun(arena, io, dir, &.{}, &.{"init"});
    for ([_][]const u8{ "p", "tools", "bin" }) |d| try dir.createDir(io, d, .default_dir);
    var i: usize = 0;
    while (i < 120) : (i += 1) {
        const name = try std.fmt.allocPrint(arena, "p/{d:0>3}.txt", .{i});
        try writeFile(io, dir, name, name);
    }
    var big: std.ArrayList(u8) = .empty;
    var blob: std.ArrayList(u8) = .empty;
    i = 0;
    while (i < 400) : (i += 1) {
        try big.print(arena, "line {d} of a file that is edited a little at a time\n", .{i});
        try blob.print(arena, "{d}\x00\x01", .{i});
    }
    try writeFile(io, dir, "big.txt", big.items);
    try writeFile(io, dir, "bin/blob.bin", blob.items);
    try writeFile(io, dir, "readme.md", "plain text\n");
    try writeFile(io, dir, "tools/run.sh", "#!/bin/sh\necho hi\n");
    try chmodFile(io, dir, "tools/run.sh", 0o755);
    _ = try renameRun(arena, io, dir, &.{}, &.{ "add", "p", "big.txt", "bin", "readme.md", "tools" });
    var head = try commitHashDup(arena, io, dir, "seed");
    // Each edit stores a delta on the previous version, so the head's big.txt
    // and blob.bin sit at the end of a chain.
    i = 0;
    while (i < 6) : (i += 1) {
        try big.print(arena, "edit {d}\n", .{i});
        try blob.print(arena, "\x00edit {d}", .{i});
        try writeFile(io, dir, "big.txt", big.items);
        try writeFile(io, dir, "bin/blob.bin", blob.items);
        head = try commitHashDup(arena, io, dir, try std.fmt.allocPrint(arena, "edit {d}", .{i}));
    }
    // The head's file set plus a symlink, as a hand-written child of the head.
    const listed = try renameRun(arena, io, dir, &.{}, &.{ "ls", head, "--size" });
    const target = try appendArtifact(arena, io, dir, "target/path\n");
    var body: std.ArrayList(u8) = .empty;
    try body.appendSlice(arena, "C symlink\\sadded\nD 2099-01-01T00:00:00\n");
    var rows = std.mem.tokenizeScalar(u8, listed, '\n');
    while (rows.next()) |row| {
        var f = std.mem.tokenizeScalar(u8, row, ' ');
        const hash = f.next().?;
        _ = f.next(); // size
        const perm = f.next().?;
        const name = f.next().?;
        if (std.mem.eql(u8, perm, "-")) try body.print(arena, "F {s} {s}\n", .{ name, hash }) else try body.print(arena, "F {s} {s} {s}\n", .{ name, hash, perm });
    }
    try body.print(arena, "F zz-link {s} l\nP {s}\nU u\n", .{ target, head });
    _ = try appendArtifact(arena, io, dir, try withZCard(arena, body.items));
}

/// `listing` with `name`'s row rewritten to the one an absent blob gets.
fn lsLongAbsent(arena: std.mem.Allocator, listing: []const u8, name: []const u8) ![]const u8 {
    var out: std.ArrayList(u8) = .empty;
    var lines = std.mem.splitScalar(u8, listing, '\n');
    var found = false;
    while (lines.next()) |line| {
        if (line.len == 0) continue;
        const sp = std.mem.lastIndexOfScalar(u8, line, ' ').?;
        if (std.mem.eql(u8, line[sp + 1 ..], name)) {
            var f = std.mem.tokenizeScalar(u8, line, ' ');
            const hash = f.next().?;
            _ = f.next(); // size
            const perm = f.next().?;
            try out.print(arena, "{s} 0 {s} absent {s}\n", .{ hash, perm, name });
            found = true;
        } else try out.print(arena, "{s}\n", .{line});
    }
    if (!found) return error.NotListed;
    return out.items;
}

test "cli ls: a second --long reconstructs no content and prints what reconstructing prints, flat and k=7, purged and shunned included" {
    var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.createDir(io, "flat", .default_dir);
    var flat = try tmp.dir.openDir(io, "flat", .{});
    defer flat.close(io);
    try lsLongScene(arena, io, flat);
    // The same history as a k=7 tree store, converted before the flat store
    // loses anything.
    _ = try renameRun(arena, io, tmp.dir, &.{}, &.{ "import-fossil", "flat", "tree", "--submanifest", "--k", "7" });
    var tree = try tmp.dir.openDir(io, "tree", .{});
    defer tree.close(io);
    {
        const raw = try renameRun(arena, io, tree, &.{}, &.{ "artifact", try tipHashOf(arena, io, tree, "tip") });
        try std.testing.expect(std.mem.indexOf(u8, raw, "\nS ") != null);
    }

    for ([_]std.Io.Dir{ flat, tree }) |dir| {
        // The oldest check-in holding big.txt, whose version gets purged.
        const versions = try renameRun(arena, io, dir, &.{}, &.{ "log", "tip", "big.txt", "-F", "json" });
        const doc = try std.json.parseFromSliceLeaky(std.json.Value, arena, versions, .{});
        const rows = doc.object.get("checkins").?.array.items;
        try std.testing.expect(rows.len >= 7);
        const oldest = rows[rows.len - 1].object;
        const seed = oldest.get("checkin").?.string;
        const purged = oldest.get("uuid").?.string;

        var warm: [2][]const u8 = undefined;
        for ([_][]const u8{ "tip", seed }, 0..) |v, n| {
            // Reconstructing every blob and remembering nothing: the reading
            // a recorded class must reproduce.
            const fresh, const fresh_reads = try lsLongRun(arena, io, dir, &.{ "ls", v, "--long", "--no-cache" });
            try std.testing.expect(fresh_reads >= 120);
            const cold, _ = try lsLongRun(arena, io, dir, &.{ "ls", v, "--long" });
            try std.testing.expectEqualStrings(fresh, cold);
            const again, const again_reads = try lsLongRun(arena, io, dir, &.{ "ls", v, "--long" });
            try std.testing.expectEqualStrings(cold, again);
            try std.testing.expectEqual(@as(u64, 0), again_reads);
            warm[n] = again;
        }
        // Every row shape the listing distinguishes is present at the tip.
        try std.testing.expect(std.mem.indexOf(u8, warm[0], " - binary bin/blob.bin\n") != null);
        try std.testing.expect(std.mem.indexOf(u8, warm[0], " 18 x text tools/run.sh\n") != null);
        try std.testing.expect(std.mem.indexOf(u8, warm[0], " 12 l text zz-link\n") != null);
        try std.testing.expect(std.mem.indexOf(u8, warm[0], " 11 - text readme.md\n") != null);

        // Content absent by policy, with its class already recorded: the
        // header read must still say absent, as reconstructing did.
        const shunned = try lsLongBlobOf(warm[0], "readme.md");
        _ = try renameRun(arena, io, dir, &.{}, &.{ "purge", "artifacts", "--force", purged });
        _ = try renameRun(arena, io, dir, &.{}, &.{ "shun", "--force", shunned });
        const want_tip = try lsLongAbsent(arena, warm[0], "readme.md");
        const want_seed = try lsLongAbsent(arena, try lsLongAbsent(arena, warm[1], "readme.md"), "big.txt");
        for ([_][]const u8{ "tip", seed }, [_][]const u8{ want_tip, want_seed }) |v, want| {
            const after, const after_reads = try lsLongRun(arena, io, dir, &.{ "ls", v, "--long" });
            try std.testing.expectEqualStrings(want, after);
            try std.testing.expectEqual(@as(u64, 0), after_reads);
            const fresh, _ = try lsLongRun(arena, io, dir, &.{ "ls", v, "--long", "--no-cache" });
            try std.testing.expectEqualStrings(want, fresh);
        }
    }
}