Skip to content
zit Artifact [27e9cf71b2]
Login

Artifact [27e9cf71b2]

Artifact 27e9cf71b20239ad0c79d866dabdb1c459cf2b515ee792187e6d2ff45dd81b0a:


const std = @import("std");
const artifact = @import("zit").artifact;
const zit_checkin = @import("zit").checkin;
const zit = @import("zit");
const support = @import("support.zig");
const ThinHistory = support.ThinHistory;
const addManaged = support.addManaged;
const appendArtifact = support.appendArtifact;
const checkinHash = support.checkinHash;
const expectContains = support.expectContains;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const initRepo = support.initRepo;
const renameRun = support.renameRun;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const runZitEnv = support.runZitEnv;
const seedStdinStore = support.seedStdinStore;
const spawnServe = support.spawnServe;
const thinFixture = support.thinFixture;
const tipHashOf = support.tipHashOf;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;

// ===========================================================================
// `zit annotate PATH` (contracts/annotate.md B-annotate-1..16). Per-line
// provenance of one tracked file at the current check-out, driven through the
// real binary over real multi-commit histories (a linear chain and a merge
// DAG built by commit → update → commit → merge → commit).
// ===========================================================================

/// The verbatim `zit annotate` help — must match `help.zig`'s `annotate_help`
/// byte-for-byte (drift fails B-annotate-12).
const annotate_help_expected =
    \\usage: zit annotate PATH [VERSION] [--full] [-n N] [-l] [--no-follow]
    \\                    [--partial]
    \\
    \\Show, for each line of PATH, the check-in that introduced that line and
    \\when. PATH is an exact repo-relative path to a tracked file; each output
    \\line is the introducing check-in's 12-character artifact-ID prefix, the
    \\ISO date it was recorded (UTC), then the file line.
    \\
    \\VERSION selects the annotated check-in the same way 'zit info' does; with
    \\it omitted, the current checkout is annotated. With -R and a VERSION no
    \\working tree is required.
    \\
    \\Provenance is computed over the file's whole version history: a line is
    \\attributed to the oldest check-in that carries it. A line that arrived from
    \\a merged branch is credited to its origin on that branch, never to the merge
    \\check-in itself. Only committed content is annotated — uncommitted edits in
    \\the working file are not shown.
    \\
    \\Renames are followed: lines the file carried before a rename keep the
    \\check-ins that wrote them under the old name. When this store elides
    \\the old name's content (below a horizon, or outside a sparse cone) the
    \\annotation stops and says so rather than crediting those lines to the
    \\rename. --no-follow reads PATH by name alone, so a rename credits every
    \\line it carried to the renaming check-in.
    \\
    \\A version of PATH removed by policy (purged or shunned) stops the
    \\annotation with exit 8. --partial reads through it instead: each line
    \\is still credited to the oldest version that carries it, but a line
    \\first seen in the version just above the gap may have come from the
    \\missing one, so it is printed with a '~' before the artifact ID. Other
    \\lines are exact and unmarked. The annotated version itself, and history
    \\elided by this store's horizon or cone, are not bridged.
    \\
    \\options:
    \\  -n, --limit N walk at most N versions of PATH (at least 3; 0 means
    \\                30; 'none' for all), newest first; a line the oldest
    \\                version walked already carried is printed with its
    \\                check-in and date columns blank, as fossil does
    \\  -l, --log     list the versions walked (newest first) before the
    \\                lines, separated by a '---' line
    \\      --full    emit the full artifact ID per line instead of the
    \\                12-character prefix (stable as an index key)
    \\      --no-follow
    \\                do not follow the file across renames
    \\      --partial continue past purged or shunned versions, marking
    \\                the lines that cannot be traced through them with '~'
    \\  -R, --repo <path>
    \\                read this repository instead of the one found from the
    \\                current directory; <path> is the directory holding '.zit'
    \\                (or the '.zit' file itself). No working tree is required.
    \\  -h, --help    show this help and exit
    \\
    \\exit status:
    \\  0  the annotation was written
    \\  1  PATH is not tracked at the annotated check-in, its content is
    \\     binary, a version's content is missing from the store, VERSION does
    \\     not resolve, or this is not a zit repository
    \\  2  usage error (a missing PATH, an unknown option, or a third positional)
    \\  8  a version's content is absent by policy: purged ('zit purge
    \\     artifacts'), shunned, or elided by this store's horizon or cone;
    \\     stderr names which, and the remedy. --partial continues past a
    \\     purged or shunned version instead
    \\
    \\aliases: blame, praise
    \\
;

/// Commits the working tree at a pinned epoch/author and returns the new
/// check-in's 64-hex hash on `gpa` (caller frees). The epoch fixes both the
/// event mtime (deterministic chain order) and the displayed ISO date.
fn annotateCommit(
    gpa: std.mem.Allocator,
    io: std.Io,
    dir: std.Io.Dir,
    epoch: []const u8,
    msg: []const u8,
) ![]const u8 {
    var res = try runCommitEnv(gpa, io, dir, .{ .user = "u", .epoch = epoch }, &.{ "commit", "-m", msg });
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    return gpa.dupe(u8, try checkinHash(res.stdout));
}

/// `annotateCommit` with `--allow-fork` — the annotate fixtures' second-head
/// commits (the fork guard refuses a deliberate fork without it).
fn annotateForkCommit(
    gpa: std.mem.Allocator,
    io: std.Io,
    dir: std.Io.Dir,
    epoch: []const u8,
    msg: []const u8,
) ![]const u8 {
    var res = try runCommitEnv(gpa, io, dir, .{ .user = "u", .epoch = epoch }, &.{ "commit", "-m", msg, "--allow-fork" });
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    return gpa.dupe(u8, try checkinHash(res.stdout));
}

test "cli annotate B-annotate-1/-2/-5: linear add/edit/append history, oldest-origin per line, deterministic" {
    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, "poem", "A\nB\nC\n");
    try addManaged(gpa, io, tmp.dir, &.{"poem"});
    const h0 = try annotateCommit(gpa, io, tmp.dir, "1000000000", "root"); // 2001-09-09
    defer gpa.free(h0);
    try writeFile(io, tmp.dir, "poem", "A\nB1\nC\n");
    const h1 = try annotateCommit(gpa, io, tmp.dir, "1100000000", "edit"); // 2004-11-09
    defer gpa.free(h1);
    try writeFile(io, tmp.dir, "poem", "A\nB1\nC\nD\n");
    const h2 = try annotateCommit(gpa, io, tmp.dir, "1200000000", "append"); // 2008-01-10
    defer gpa.free(h2);

    // A,C unchanged since root → h0 ; B1 introduced at h1 ; D appended at h2.
    const expected = try std.fmt.allocPrint(
        gpa,
        "{s} 2001-09-09     1: A\n{s} 2004-11-09     2: B1\n{s} 2001-09-09     3: C\n{s} 2008-01-10     4: D\n",
        .{ h0[0..12], h1[0..12], h0[0..12], h2[0..12] },
    );
    defer gpa.free(expected);

    var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "poem" });
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    try std.testing.expectEqualStrings(expected, res.stdout);
    try std.testing.expectEqualStrings("", res.stderr); // B-annotate-15 stream discipline

    // B-annotate-5: a second run is byte-identical.
    var res2 = try runZit(gpa, io, tmp.dir, &.{ "annotate", "poem" });
    defer freeResult(gpa, &res2);
    try std.testing.expectEqualStrings(res.stdout, res2.stdout);
}

test "cli annotate B-annotate-11: uncommitted working-tree edits are invisible" {
    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, "poem", "A\nB\nC\n");
    try addManaged(gpa, io, tmp.dir, &.{"poem"});
    const h0 = try annotateCommit(gpa, io, tmp.dir, "1000000000", "root");
    defer gpa.free(h0);

    const expected = try std.fmt.allocPrint(
        gpa,
        "{s} 2001-09-09     1: A\n{s} 2001-09-09     2: B\n{s} 2001-09-09     3: C\n",
        .{ h0[0..12], h0[0..12], h0[0..12] },
    );
    defer gpa.free(expected);

    // Edit the working file WITHOUT committing — annotate reads checked-in bytes.
    try writeFile(io, tmp.dir, "poem", "totally\ndifferent\nuncommitted\ncontent\n");
    var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "poem" });
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    try std.testing.expectEqualStrings(expected, res.stdout);
}

test "cli annotate B-annotate-3/-4: merge pass-through and merge-only resolution lines" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();

    // base A,B,C. current edits C→C1 (primary side). other edits A→A1 (merge
    // side). Merge is clean (disjoint), then a resolution edit inserts R (in no
    // parent). Expected: A1→other, B→base(oldest), R→merge, C1→current.
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "poem", "A\nB\nC\n");
    try addManaged(gpa, io, tmp.dir, &.{"poem"});
    const base = try annotateCommit(gpa, io, tmp.dir, "1000000000", "base"); // 2001-09-09
    defer gpa.free(base);

    try writeFile(io, tmp.dir, "poem", "A\nB\nC1\n");
    const current = try annotateCommit(gpa, io, tmp.dir, "1100000000", "current"); // 2004-11-09
    defer gpa.free(current);

    var up = try runZit(gpa, io, tmp.dir, &.{ "update", base });
    defer freeResult(gpa, &up);
    try expectExit(up.term, 0);

    try writeFile(io, tmp.dir, "poem", "A1\nB\nC\n");
    const other = try annotateForkCommit(gpa, io, tmp.dir, "1200000000", "other"); // 2008-01-10
    defer gpa.free(other);

    var back = try runZit(gpa, io, tmp.dir, &.{ "update", current });
    defer freeResult(gpa, &back);
    try expectExit(back.term, 0);

    var merged = try runZit(gpa, io, tmp.dir, &.{ "merge", other });
    defer freeResult(gpa, &merged);
    try expectExit(merged.term, 0); // clean, disjoint edits

    // A resolution edit present in NEITHER parent: R.
    try writeFile(io, tmp.dir, "poem", "A1\nB\nR\nC1\n");
    const merge_ci = try annotateCommit(gpa, io, tmp.dir, "1300000000", "merge"); // 2011-03-13
    defer gpa.free(merge_ci);

    const expected = try std.fmt.allocPrint(
        gpa,
        "{s} 2008-01-10     1: A1\n{s} 2001-09-09     2: B\n{s} 2011-03-13     3: R\n{s} 2004-11-09     4: C1\n",
        .{ other[0..12], base[0..12], merge_ci[0..12], current[0..12] },
    );
    defer gpa.free(expected);

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

test "cli annotate topology: a child with an EARLIER mtime than its parent still attributes by ancestry" {
    // Non-monotone clocks: the child commit is BACKDATED before its parent. The
    // shared line L was introduced by the parent and must stay charged to the
    // parent (older by topology), not the too-recent child. The old mtime-ascending
    // chain order processed the child first and misattributed L to the child.
    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, "poem", "L\n");
    try addManaged(gpa, io, tmp.dir, &.{"poem"});
    const parent = try annotateCommit(gpa, io, tmp.dir, "1200000000", "parent"); // 2008-01-10, LATER
    defer gpa.free(parent);

    try writeFile(io, tmp.dir, "poem", "L\nM\n");
    const child = try annotateCommit(gpa, io, tmp.dir, "1000000000", "child"); // 2001-09-09, EARLIER
    defer gpa.free(child);

    // L → parent (older by topology, despite its LATER mtime) ; M → child.
    const expected = try std.fmt.allocPrint(
        gpa,
        "{s} 2008-01-10     1: L\n{s} 2001-09-09     2: M\n",
        .{ parent[0..12], child[0..12] },
    );
    defer gpa.free(expected);

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

test "cli annotate topology: a merge BACKDATED before its parents credits the primary parent's line, not the merge" {
    // Both branch tips independently add the identical line X; the merge keeps it.
    // The merge check-in is BACKDATED before every parent (non-monotone). Attribution
    // must pass X through the PRIMARY parent (current), never charge it to the merge.
    // The old mtime-ascending order processed the earliest-mtime merge FIRST — before
    // its parents were resolved — and wrongly charged X (and A) to the merge itself.
    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, "poem", "A\nB\n");
    try addManaged(gpa, io, tmp.dir, &.{"poem"});
    const base = try annotateCommit(gpa, io, tmp.dir, "1000000000", "base"); // 2001-09-09
    defer gpa.free(base);

    // Primary branch inserts X.
    try writeFile(io, tmp.dir, "poem", "A\nX\nB\n");
    const current = try annotateCommit(gpa, io, tmp.dir, "1200000000", "current"); // 2008-01-10
    defer gpa.free(current);

    // Other branch independently inserts the identical X, plus a distinct Z (so the
    // merge actually changes the working tree — otherwise there is nothing to commit).
    var up = try runZit(gpa, io, tmp.dir, &.{ "update", base });
    defer freeResult(gpa, &up);
    try expectExit(up.term, 0);
    try writeFile(io, tmp.dir, "poem", "A\nX\nB\nZ\n");
    const other = try annotateForkCommit(gpa, io, tmp.dir, "1300000000", "other"); // 2011-03-13
    defer gpa.free(other);

    // Merge other into current, then commit the merge BACKDATED before all parents.
    var back = try runZit(gpa, io, tmp.dir, &.{ "update", current });
    defer freeResult(gpa, &back);
    try expectExit(back.term, 0);
    var merged = try runZit(gpa, io, tmp.dir, &.{ "merge", other });
    defer freeResult(gpa, &merged);
    try expectExit(merged.term, 0); // X agrees on both sides, Z only on other ⇒ clean
    const merge_ci = try annotateCommit(gpa, io, tmp.dir, "500000000", "merge"); // 1985-11-05, EARLIEST
    defer gpa.free(merge_ci);

    // A,B → base ; X → current (the PRIMARY parent, preferred over other) ; Z → other.
    // Nothing is charged to the backdated merge itself.
    const expected = try std.fmt.allocPrint(
        gpa,
        "{s} 2001-09-09     1: A\n{s} 2008-01-10     2: X\n{s} 2001-09-09     3: B\n{s} 2011-03-13     4: Z\n",
        .{ base[0..12], current[0..12], base[0..12], other[0..12] },
    );
    defer gpa.free(expected);

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

test "cli annotate B-annotate-9: a binary file refuses with exit 1, nothing on stdout" {
    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, "blob.bin", "before\x00after\n"); // a NUL ⇒ binary
    try addManaged(gpa, io, tmp.dir, &.{"blob.bin"});
    const h0 = try annotateCommit(gpa, io, tmp.dir, "1000000000", "add binary");
    defer gpa.free(h0);

    var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "blob.bin" });
    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, "binary") != null);
}

test "cli annotate B-annotate-10: untracked-but-present vs nonexistent, both exit 1" {
    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, "poem", "A\n");
    try addManaged(gpa, io, tmp.dir, &.{"poem"});
    const h0 = try annotateCommit(gpa, io, tmp.dir, "1000000000", "root");
    defer gpa.free(h0);

    // Present on disk but never added → untracked.
    try writeFile(io, tmp.dir, "scratch.txt", "not added\n");
    var untracked = try runZit(gpa, io, tmp.dir, &.{ "annotate", "scratch.txt" });
    defer freeResult(gpa, &untracked);
    try expectExit(untracked.term, 1);
    try std.testing.expectEqualStrings("", untracked.stdout);
    try std.testing.expect(std.mem.indexOf(u8, untracked.stderr, "not tracked") != null);

    // Neither on disk nor tracked → nonexistent.
    var missing = try runZit(gpa, io, tmp.dir, &.{ "annotate", "ghost.txt" });
    defer freeResult(gpa, &missing);
    try expectExit(missing.term, 1);
    try std.testing.expectEqualStrings("", missing.stdout);
    try std.testing.expect(std.mem.indexOf(u8, missing.stderr, "no such tracked file") != null);
}

test "cli annotate B-annotate-12: -h and --help print the verbatim help, exit 0" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);

    for ([_][]const u8{ "-h", "--help" }) |flag| {
        var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", flag });
        defer freeResult(gpa, &res);
        try expectExit(res.term, 0);
        try std.testing.expectEqualStrings(annotate_help_expected, res.stdout);
        try std.testing.expectEqualStrings("", res.stderr);
    }
    // Help wins even over an otherwise-invalid trailing token.
    var win = try runZit(gpa, io, tmp.dir, &.{ "annotate", "--help", "-z" });
    defer freeResult(gpa, &win);
    try expectExit(win.term, 0);
    try std.testing.expectEqualStrings(annotate_help_expected, win.stdout);
}

test "cli annotate B-annotate-13: missing PATH, unknown option, or a third positional is a usage error, exit 2" {
    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 cases = [_][]const []const u8{
        &.{"annotate"}, // missing PATH
        &.{ "annotate", "-x" }, // unknown option
        &.{ "annotate", "a", "b", "c" }, // third positional (the second is VERSION)
    };
    for (cases) |argv| {
        var res = try runZit(gpa, io, tmp.dir, argv);
        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, "try 'zit annotate --help'") != null);
    }
}

test "cli annotate B-annotate-14: outside a repository fails with the not-a-repo diagnostic, exit 1" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{}); // NOT initialized
    defer tmp.cleanup();

    var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "poem" });
    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 annotate B-annotate-16: the top-level help names the annotate verb" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);

    var res = try runZit(gpa, io, tmp.dir, &.{"help"});
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    try std.testing.expect(std.mem.indexOf(u8, res.stdout, "annotate") != null);
}

test "cli annotate VERSION and --full: bare-repo annotate at a named check-in, full IDs join against info" {
    // Goal: annotate's two halves. VERSION resolves like cat's (here: a
    // 64-hex hash and 'tip'), works with -R and no working tree, and the
    // default bare invocation is unchanged. --full swaps the 12-char prefix
    // for the full artifact ID, byte-identical lines otherwise. Method: a
    // two-commit history where the second commit appends a line, then
    // annotate at both versions and cross-check prefix vs full output.
    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, "f.txt", "alpha\n");
    try addManaged(gpa, io, tmp.dir, &.{"f.txt"});
    const c1 = try annotateCommit(gpa, io, tmp.dir, "1577836800", "one"); // 2020-01-01
    defer gpa.free(c1);
    try writeFile(io, tmp.dir, "f.txt", "alpha\nbeta\n");
    const c2 = try annotateCommit(gpa, io, tmp.dir, "1577923200", "two"); // 2020-01-02
    defer gpa.free(c2);

    // VERSION = the first check-in: only its one line, attributed to c1.
    var v1 = try runZit(gpa, io, tmp.dir, &.{ "annotate", "f.txt", c1 });
    defer freeResult(gpa, &v1);
    try expectExitDiag(v1.term, 0, v1);
    const v1_expected = try std.fmt.allocPrint(gpa, "{s} 2020-01-01     1: alpha\n", .{c1[0..12]});
    defer gpa.free(v1_expected);
    try std.testing.expectEqualStrings(v1_expected, v1.stdout);

    // Bare default (no VERSION) still annotates the current checkout.
    var cur = try runZit(gpa, io, tmp.dir, &.{ "annotate", "f.txt" });
    defer freeResult(gpa, &cur);
    try expectExitDiag(cur.term, 0, cur);

    // 'tip' + --full from a BARE store (-R, no working tree): both lines,
    // full 64-hex IDs joining exactly against the commit hashes.
    var bare = std.testing.tmpDir(.{});
    defer bare.cleanup();
    const cwd = try std.process.currentPathAlloc(io, gpa);
    defer gpa.free(cwd);
    const repo_arg = try std.fs.path.join(gpa, &.{ cwd, ".zig-cache", "tmp", &tmp.sub_path });
    defer gpa.free(repo_arg);
    var full = try runZit(gpa, io, bare.dir, &.{ "annotate", "-R", repo_arg, "f.txt", "tip", "--full" });
    defer freeResult(gpa, &full);
    try expectExitDiag(full.term, 0, full);
    const full_expected = try std.fmt.allocPrint(
        gpa,
        "{s} 2020-01-01     1: alpha\n{s} 2020-01-02     2: beta\n",
        .{ c1, c2 },
    );
    defer gpa.free(full_expected);
    try std.testing.expectEqualStrings(full_expected, full.stdout);

    // The full IDs are prefix-consistent with the default rendering.
    try std.testing.expect(std.mem.startsWith(u8, full.stdout, cur.stdout[0..12]));

    // An unresolvable VERSION refuses with the resolve diagnostic, exit 1.
    var bad = try runZit(gpa, io, tmp.dir, &.{ "annotate", "f.txt", "no-such-branch" });
    defer freeResult(gpa, &bad);
    try expectExit(bad.term, 1);
    try std.testing.expect(std.mem.indexOf(u8, bad.stderr, "not found") != null);
}

test "cli annotate: a version removed from the middle of a history refuses (exit 8), never credits the next one" {
    // Before this, annotate read the absent version as "not added yet" and
    // attributed every line it carried to the next version, with exit 0.
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var h = try thinFixture(gpa, io, tmp.dir, &.{"init"});
    defer h.deinit(gpa);
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", &h.blobs[3] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "n.md" });
    defer freeResult(gpa, &res);
    try expectExitDiag(res.term, 8, res);
    try expectContains(res.stderr, "shunned");
    try std.testing.expectEqualStrings("", res.stdout);
}

// --- annotate --partial: past a version absent by policy ---------------------

/// Five check-ins of n.md where version i appends the line "added in i", so
/// every line has a distinct origin. Built on `ThinHistory` for the names.
fn partialFixture(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !ThinHistory {
    var init = try runZit(gpa, io, dir, &.{"init"});
    defer freeResult(gpa, &init);
    try expectExitDiag(init.term, 0, init);
    var h: ThinHistory = undefined;
    var made: usize = 0;
    errdefer for (h.checkins[0..made]) |c| gpa.free(c);
    var body: std.ArrayList(u8) = .empty;
    defer body.deinit(gpa);
    for (0..5) |i| {
        try body.print(gpa, "added in {d}\n", .{i + 1});
        try writeFile(io, dir, "n.md", body.items);
        if (i == 0) try addManaged(gpa, io, dir, &.{"n.md"});
        var c = try runCommitEnv(gpa, io, dir, .{ .user = "u" }, &.{ "commit", "-m", "a version" });
        defer freeResult(gpa, &c);
        try expectExitDiag(c.term, 0, c);
        h.checkins[i] = try tipHashOf(gpa, io, dir, "trunk");
        made += 1;
        var d: [32]u8 = undefined;
        std.crypto.hash.sha3.Sha3_256.hash(body.items, &d, .{});
        h.blobs[i] = std.fmt.bytesToHex(d, .lower);
    }
    return h;
}

/// The lines of an annotate (or blame) listing, each without its newline.
fn annotateLines(arena: std.mem.Allocator, out: []const u8) ![]const []const u8 {
    var lines: std.ArrayList([]const u8) = .empty;
    var it = std.mem.splitScalar(u8, std.mem.trimEnd(u8, out, "\n"), '\n');
    while (it.next()) |l| try lines.append(arena, l);
    return lines.items;
}

/// An exact line moved onto another origin and marked approximate: the
/// origin's hash and date columns from `origin_line`, the rest from `line`.
fn approxLine(arena: std.mem.Allocator, origin_line: []const u8, line: []const u8) ![]const u8 {
    const cols = 12 + 1 + 10; // hash, space, date
    return std.fmt.allocPrint(arena, "~{s}{s}", .{ origin_line[0..cols], line[cols..] });
}

fn expectAnnotateLines(want: []const []const u8, got_out: []const u8, arena: std.mem.Allocator) !void {
    const got = try annotateLines(arena, got_out);
    if (got.len != want.len) {
        std.debug.print("\nwant {d} lines, got:\n{s}\n", .{ want.len, got_out });
        return error.TestUnexpectedResult;
    }
    for (want, got) |w, g| try std.testing.expectEqualStrings(w, g);
}

test "cli annotate --partial: past purged versions, lines that cannot be traced go to the version above the gap, marked" {
    const gpa = std.testing.allocator;
    var arena_state = std.heap.ArenaAllocator.init(gpa);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var h = try partialFixture(gpa, io, tmp.dir);
    defer h.deinit(gpa);

    var whole = try runZit(gpa, io, tmp.dir, &.{ "annotate", "n.md" });
    defer freeResult(gpa, &whole);
    try expectExitDiag(whole.term, 0, whole);
    const e = try annotateLines(arena, try arena.dupe(u8, whole.stdout));
    try std.testing.expectEqual(@as(usize, 5), e.len);
    var whole_blame = try runZit(gpa, io, tmp.dir, &.{ "blame", "n.md" });
    defer freeResult(gpa, &whole_blame);
    try expectExitDiag(whole_blame.term, 0, whole_blame);
    const eb = try annotateLines(arena, try arena.dupe(u8, whole_blame.stdout));

    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "artifacts", "--force", "--reason", "thinning", &h.blobs[1], &h.blobs[3] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    // Plain annotate stays exact: it refuses.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "n.md" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 8, res);
        try expectContains(res.stderr, "purged");
        try std.testing.expectEqualStrings("", res.stdout);
    }
    // Version 1's line traces through the gap exactly. Versions 3 and 5 sit
    // directly above a gap: what they introduced cannot be told from what
    // the purged version below introduced, so their lines are marked.
    const want = [_][]const u8{
        e[0],
        try approxLine(arena, e[2], e[1]),
        try std.fmt.allocPrint(arena, "~{s}", .{e[2]}),
        try approxLine(arena, e[4], e[3]),
        try std.fmt.allocPrint(arena, "~{s}", .{e[4]}),
    };
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "--partial", "n.md" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try expectAnnotateLines(&want, res.stdout, arena);
        try expectContains(res.stderr, "approximate");
    }
    {
        const want_blame = [_][]const u8{
            eb[0],
            try approxLine(arena, eb[2], eb[1]),
            try std.fmt.allocPrint(arena, "~{s}", .{eb[2]}),
            try approxLine(arena, eb[4], eb[3]),
            try std.fmt.allocPrint(arena, "~{s}", .{eb[4]}),
        };
        var res = try runZit(gpa, io, tmp.dir, &.{ "blame", "n.md", "--partial" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
        try expectAnnotateLines(&want_blame, res.stdout, arena);
    }
    // The annotated version itself purged: there is nothing to annotate.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "--partial", "n.md", h.checkins[3] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 8, res);
        try expectContains(res.stderr, "purged");
        try std.testing.expectEqualStrings("", res.stdout);
    }
}

test "cli annotate --partial: past a shunned middle version, and only the version above it is approximate" {
    const gpa = std.testing.allocator;
    var arena_state = std.heap.ArenaAllocator.init(gpa);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var h = try partialFixture(gpa, io, tmp.dir);
    defer h.deinit(gpa);

    var whole = try runZit(gpa, io, tmp.dir, &.{ "annotate", "n.md" });
    defer freeResult(gpa, &whole);
    try expectExitDiag(whole.term, 0, whole);
    const e = try annotateLines(arena, try arena.dupe(u8, whole.stdout));

    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", &h.blobs[2] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "n.md" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 8, res);
        try expectContains(res.stderr, "shunned");
    }
    const want = [_][]const u8{
        e[0],
        e[1],
        try approxLine(arena, e[3], e[2]),
        try std.fmt.allocPrint(arena, "~{s}", .{e[3]}),
        e[4],
    };
    var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "--partial", "n.md" });
    defer freeResult(gpa, &res);
    try expectExitDiag(res.term, 0, res);
    try expectAnnotateLines(&want, res.stdout, arena);
}

test "cli annotate --partial: content missing with no record of why is damage, exit 1" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var h = try partialFixture(gpa, io, tmp.dir);
    defer h.deinit(gpa);
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "artifacts", "--force", "--reason", "thinning", &h.blobs[1] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    // Without the thinned list the version is gone for no recorded reason.
    try tmp.dir.deleteFile(io, ".zit-thinned");
    for ([_][]const []const u8{ &.{ "annotate", "n.md" }, &.{ "annotate", "--partial", "n.md" } }) |argv| {
        var res = try runZit(gpa, io, tmp.dir, argv);
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 1, res);
        try expectContains(res.stderr, "is missing");
        try std.testing.expectEqualStrings("", res.stdout);
    }
}

// --- annotate -n N: Fossil's version limit ------------------------------------

/// Six versions of n.md, version i appending "added in i", each followed by a
/// check-in that touches only o.txt: the limit counts versions of the file,
/// so the check-ins between them must not count. Returns the blob of each
/// version.
fn limitFixture(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) ![6][64]u8 {
    var init = try runZit(gpa, io, dir, &.{"init"});
    defer freeResult(gpa, &init);
    try expectExitDiag(init.term, 0, init);
    var blobs: [6][64]u8 = undefined;
    var body: std.ArrayList(u8) = .empty;
    defer body.deinit(gpa);
    var other: std.ArrayList(u8) = .empty;
    defer other.deinit(gpa);
    for (0..6) |i| {
        try body.print(gpa, "added in {d}\n", .{i + 1});
        try writeFile(io, dir, "n.md", body.items);
        if (i == 0) try addManaged(gpa, io, dir, &.{"n.md"});
        var c = try runCommitEnv(gpa, io, dir, .{ .user = "u" }, &.{ "commit", "-m", "a version" });
        defer freeResult(gpa, &c);
        try expectExitDiag(c.term, 0, c);
        var d: [32]u8 = undefined;
        std.crypto.hash.sha3.Sha3_256.hash(body.items, &d, .{});
        blobs[i] = std.fmt.bytesToHex(d, .lower);
        try other.print(gpa, "unrelated {d}\n", .{i});
        try writeFile(io, dir, "o.txt", other.items);
        if (i == 0) try addManaged(gpa, io, dir, &.{"o.txt"});
        var o = try runCommitEnv(gpa, io, dir, .{ .user = "u" }, &.{ "commit", "-m", "elsewhere" });
        defer freeResult(gpa, &o);
        try expectExitDiag(o.term, 0, o);
    }
    return blobs;
}

/// Fossil's line for an origin beyond the limit: the attribution columns
/// (through the ':' that ends them) blanked, the rest of `line` kept.
fn beyondLine(arena: std.mem.Allocator, line: []const u8, cols: usize) ![]const u8 {
    const out = try arena.dupe(u8, line);
    @memset(out[0..cols], ' ');
    return out;
}

fn annotateOut(gpa: std.mem.Allocator, arena: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, argv: []const []const u8) ![]const u8 {
    var res = try runZit(gpa, io, dir, argv);
    defer freeResult(gpa, &res);
    try expectExitDiag(res.term, 0, res);
    try std.testing.expectEqualStrings("", res.stderr);
    return arena.dupe(u8, res.stdout);
}

test "cli annotate -n N: N versions of the file are walked; lines older than the last one reached are left unattributed, as fossil does" {
    const gpa = std.testing.allocator;
    var arena_state = std.heap.ArenaAllocator.init(gpa);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    _ = try limitFixture(gpa, io, tmp.dir);

    const whole_out = try annotateOut(gpa, arena, io, tmp.dir, &.{ "annotate", "n.md" });
    const e = try annotateLines(arena, whole_out);
    try std.testing.expectEqual(@as(usize, 6), e.len);
    const cols = 12 + 1 + 10; // hash, space, date
    // Four versions walked (6, 5, 4, 3): what 6, 5 and 4 added is exact; the
    // lines version 3 already carried may be older than anything walked.
    const want = [_][]const u8{
        try beyondLine(arena, e[0], cols),
        try beyondLine(arena, e[1], cols),
        try beyondLine(arena, e[2], cols),
        e[3],
        e[4],
        e[5],
    };
    try expectAnnotateLines(&want, try annotateOut(gpa, arena, io, tmp.dir, &.{ "annotate", "-n", "4", "n.md" }), arena);
    try expectAnnotateLines(&want, try annotateOut(gpa, arena, io, tmp.dir, &.{ "annotate", "--limit", "4", "n.md" }), arena);
    // Fossil walks at least three versions whatever the limit says.
    const three = try annotateLines(arena, try annotateOut(gpa, arena, io, tmp.dir, &.{ "annotate", "-n", "1", "n.md" }));
    try std.testing.expectEqualStrings(e[4], three[4]);
    try std.testing.expectEqualStrings(try beyondLine(arena, e[3], cols), three[3]);
    // The whole history within the limit, or no limit, is the plain answer;
    // Fossil reads 0 as its default count of 30.
    for ([_][]const u8{ "6", "7", "1000", "none", "0" }) |n| {
        try std.testing.expectEqualStrings(whole_out, try annotateOut(gpa, arena, io, tmp.dir, &.{ "annotate", "-n", n, "n.md" }));
    }
    // blame blanks its user column with the rest.
    const eb = try annotateLines(arena, try annotateOut(gpa, arena, io, tmp.dir, &.{ "blame", "n.md" }));
    const bcols = cols + 1 + 13 + 1; // space, user, ':'
    const blamed = try annotateLines(arena, try annotateOut(gpa, arena, io, tmp.dir, &.{ "blame", "-n", "4", "n.md" }));
    try std.testing.expectEqualStrings(try beyondLine(arena, eb[1], bcols), blamed[1]);
    try std.testing.expectEqualStrings(eb[3], blamed[3]);
    // --full widens the blank to the full ID's columns.
    const full = try annotateLines(arena, try annotateOut(gpa, arena, io, tmp.dir, &.{ "annotate", "--full", "-n", "4", "n.md" }));
    try std.testing.expect(std.mem.allEqual(u8, full[0][0 .. 64 + 11], ' '));
    try std.testing.expectEqualStrings("     1: added in 1", full[0][64 + 11 ..]);
    // -l lists the versions walked, not every check-in between them.
    const logged = try annotateOut(gpa, arena, io, tmp.dir, &.{ "annotate", "-l", "-n", "4", "n.md" });
    try std.testing.expectEqual(@as(usize, 4), std.mem.count(u8, logged, "version "));
    const all_logged = try annotateOut(gpa, arena, io, tmp.dir, &.{ "annotate", "-l", "n.md" });
    try std.testing.expectEqual(@as(usize, 6), std.mem.count(u8, all_logged, "version "));
    // With no derived cache to say which check-ins changed the file, each
    // is told from its manifest, and the answer is the same.
    {
        try tmp.dir.deleteTree(io, ".zit-cache");
        try tmp.dir.createDir(io, ".zit-cache", .default_dir);
        const lock = try std.process.run(arena, io, .{ .argv = &.{ "/bin/chmod", "a-w", ".zit-cache" }, .cwd = .{ .dir = tmp.dir } });
        try expectExitDiag(lock.term, 0, lock);
        defer _ = std.process.run(arena, io, .{ .argv = &.{ "/bin/chmod", "u+w", ".zit-cache" }, .cwd = .{ .dir = tmp.dir } }) catch {};
        try expectAnnotateLines(&want, try annotateOut(gpa, arena, io, tmp.dir, &.{ "annotate", "-n", "4", "n.md" }), arena);
        try std.testing.expectEqualStrings(logged, try annotateOut(gpa, arena, io, tmp.dir, &.{ "annotate", "-l", "-n", "4", "n.md" }));
    }
    // A count, or "none"; fossil's "Xs" time budget is not offered.
    for ([_][]const u8{ "x", "5s", "-3" }) |bad| {
        var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "-n", bad, "n.md" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 2, res);
    }
}

test "cli annotate -n N: versions beyond the limit are never read, and --partial bridges a gap inside it" {
    const gpa = std.testing.allocator;
    var arena_state = std.heap.ArenaAllocator.init(gpa);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    const blobs = try limitFixture(gpa, io, tmp.dir);
    const e = try annotateLines(arena, try annotateOut(gpa, arena, io, tmp.dir, &.{ "annotate", "n.md" }));
    const limited = try annotateOut(gpa, arena, io, tmp.dir, &.{ "annotate", "-n", "4", "n.md" });
    const cols = 12 + 1 + 10;

    // Version 2 lies beyond four versions: purging it changes nothing there.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "artifacts", "--force", "--reason", "thinning", &blobs[1] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    try std.testing.expectEqualStrings(limited, try annotateOut(gpa, arena, io, tmp.dir, &.{ "annotate", "-n", "4", "n.md" }));
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "n.md" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 8, res);
    }

    // Version 5 purged inside the window: refused without --partial; with it,
    // version 6 reads through to 4, and what 5 or 6 added is approximate.
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "purge", "artifacts", "--force", "--reason", "thinning", &blobs[4] });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 0, res);
    }
    {
        var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "-n", "4", "n.md" });
        defer freeResult(gpa, &res);
        try expectExitDiag(res.term, 8, res);
    }
    const want = [_][]const u8{
        try beyondLine(arena, e[0], cols),
        try beyondLine(arena, e[1], cols),
        try beyondLine(arena, e[2], cols),
        e[3],
        try approxLine(arena, e[5], e[4]),
        try std.fmt.allocPrint(arena, "~{s}", .{e[5]}),
    };
    var res = try runZit(gpa, io, tmp.dir, &.{ "annotate", "--partial", "-n", "4", "n.md" });
    defer freeResult(gpa, &res);
    try expectExitDiag(res.term, 0, res);
    try expectAnnotateLines(&want, res.stdout, arena);
    try expectContains(res.stderr, "approximate");
}

// --- annotate memory: bounded by the file, not by its history ------------

/// Tracks the most heap live at once through it.
const AnnotatePeakAllocator = struct {
    child: std.mem.Allocator,
    live: usize = 0,
    peak: usize = 0,

    fn allocator(self: *AnnotatePeakAllocator) std.mem.Allocator {
        return .{ .ptr = self, .vtable = &.{ .alloc = alloc, .resize = resize, .remap = remap, .free = free } };
    }

    fn grew(self: *AnnotatePeakAllocator, old: usize, new: usize) void {
        self.live = self.live - old + new;
        self.peak = @max(self.peak, self.live);
    }

    fn alloc(ctx: *anyopaque, len: usize, a: std.mem.Alignment, ra: usize) ?[*]u8 {
        const self: *AnnotatePeakAllocator = @ptrCast(@alignCast(ctx));
        const p = self.child.rawAlloc(len, a, ra) orelse return null;
        self.grew(0, len);
        return p;
    }

    fn resize(ctx: *anyopaque, mem: []u8, a: std.mem.Alignment, new_len: usize, ra: usize) bool {
        const self: *AnnotatePeakAllocator = @ptrCast(@alignCast(ctx));
        if (!self.child.rawResize(mem, a, new_len, ra)) return false;
        self.grew(mem.len, new_len);
        return true;
    }

    fn remap(ctx: *anyopaque, mem: []u8, a: std.mem.Alignment, new_len: usize, ra: usize) ?[*]u8 {
        const self: *AnnotatePeakAllocator = @ptrCast(@alignCast(ctx));
        const p = self.child.rawRemap(mem, a, new_len, ra) orelse return null;
        self.grew(mem.len, new_len);
        return p;
    }

    fn free(ctx: *anyopaque, mem: []u8, a: std.mem.Alignment, ra: usize) void {
        const self: *AnnotatePeakAllocator = @ptrCast(@alignCast(ctx));
        self.child.rawFree(mem, a, ra);
        self.live -= mem.len;
    }
};

/// The file's text: one line per entry, each naming its id and revision, so
/// an edit changes exactly one line.
fn annotateBigText(a: std.mem.Allocator, lines: []const [2]u32) ![]u8 {
    var out: std.ArrayList(u8) = .empty;
    for (lines) |l| try out.print(a, "line {d} revision {d} of a long-lived file\n", .{ l[0], l[1] });
    return out.toOwnedSlice(a);
}

fn noElision(_: ?*anyopaque, _: std.mem.Allocator, _: []const u8) ?zit.blame.Elision {
    return null;
}

test "annotate: the heap peak follows the file's size, not how many versions are walked" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try seedStdinStore(gpa, io, tmp.dir, "store");
    var s = try tmp.dir.openDir(io, "store", .{});
    defer s.close(io);
    var scratch = std.heap.ArenaAllocator.init(gpa);
    defer scratch.deinit();
    const a = scratch.allocator();

    // ~4,000 lines growing to ~4,900 over 300 versions; a side branch of ten
    // edits forks at version 150 and is merged back five versions later.
    var prng = std.Random.DefaultPrng.init(0x325);
    const rand = prng.random();
    var next_id: u32 = 0;
    var trunk: std.ArrayList([2]u32) = .empty;
    for (0..4000) |_| {
        try trunk.append(a, .{ next_id, 0 });
        next_id += 1;
    }
    var diag: zit_checkin.Diag = .{};
    var ctx: zit_checkin.Context = undefined;
    try ctx.open(gpa, io, s, .{}, &diag);
    defer ctx.deinit();
    const Step = struct {
        fn commit(c: *zit_checkin.Context, d: *zit_checkin.Diag, al: std.mem.Allocator, n: usize, parents: []const []const u8, branch: ?[]const u8, lines: []const [2]u32) ![]const u8 {
            const bytes = try annotateBigText(al, lines);
            const out = zit_checkin.commit(c, .{
                .parents = if (parents.len == 0) null else parents,
                .branch = branch,
                .user = "u",
                .comment = "edit",
                .now = .{ .epoch = 1_700_000_000 + @as(i64, @intCast(n)) * 60 },
                .edits = &.{.{ .put = .{ .path = "big.txt", .bytes = bytes } }},
                .no_checks = true,
            }, d) catch |e| {
                std.debug.print("commit {d}: {s}: {s}\n", .{ n, @errorName(e), d.text() });
                return e;
            };
            return switch (out) {
                .recorded => |name| try al.dupe(u8, name.slice()),
                else => error.TestUnexpectedResult,
            };
        }
    };
    var tip: ?[]const u8 = null;
    var side_tip: []const u8 = undefined;
    var side_edits: std.AutoHashMapUnmanaged(u32, u32) = .empty;
    const versions = 300;
    var n: usize = 0;
    while (n < versions) : (n += 1) {
        if (n == 150) {
            var side = try trunk.clone(a);
            side_tip = tip.?;
            for (0..10) |j| {
                for (0..3) |_| {
                    const i = rand.uintLessThan(usize, side.items.len);
                    side.items[i][1] += 1;
                    try side_edits.put(a, side.items[i][0], side.items[i][1]);
                }
                side_tip = try Step.commit(&ctx, &diag, a, 1000 + j, &.{side_tip}, if (j == 0) "side" else null, side.items);
            }
        }
        for (0..3) |_| {
            try trunk.insert(a, rand.uintLessThan(usize, trunk.items.len + 1), .{ next_id, 0 });
            next_id += 1;
        }
        for (0..2) |_| trunk.items[rand.uintLessThan(usize, trunk.items.len)][1] += 1;
        if (n == 155) {
            for (trunk.items) |*l| if (side_edits.get(l[0])) |rev| {
                l[1] = rev;
            };
            tip = try Step.commit(&ctx, &diag, a, n, &.{ tip.?, side_tip }, null, trunk.items);
        } else {
            tip = try Step.commit(&ctx, &diag, a, n, if (tip) |t| &.{t} else &.{}, null, trunk.items);
        }
    }
    const file_bytes = (try annotateBigText(a, trunk.items)).len;

    var repo: zit.Repository = undefined;
    try repo.open(gpa, io, s, .{ .mode = .fast_read });
    defer repo.deinit(gpa);
    _ = try repo.logStore(gpa); // opened before the count: the store's own index is not the walk's
    const cur = repo.checkinRid(tip.?).?;
    const rids = try repo.ancestryLinearized(gpa, cur);
    defer gpa.free(rids);
    try std.testing.expect(rids.len > versions);
    var index = (try zit.filelog.loadPathIndex(gpa, io, s, &repo, try repo.logStore(gpa))).?;
    defer index.deinit(gpa);

    // The walk over the whole ancestry, then the file index's reading each
    // version's card, then reading the record body; all three agree.
    var first: ?[]const u8 = null;
    defer if (first) |f| gpa.free(f);
    const reads = zit.blame.max_card_reads;
    defer zit.blame.max_card_reads = reads;
    for ([_]?*zit.filelog.PathIndex{ null, &index, &index }, 0..) |ix, run| {
        zit.blame.max_card_reads = if (run == 2) 0 else std.math.maxInt(usize);
        try s.deleteTree(io, zit.blamecache.dirname);
        var counted: AnnotatePeakAllocator = .{ .child = gpa };
        const cgpa = counted.allocator();
        var arena = std.heap.ArenaAllocator.init(cgpa);
        defer arena.deinit();
        const result = try zit.blame.annotate(cgpa, arena.allocator(), io, &repo, "big.txt", cur, .{}, .{ .of = noElision }, ix);
        const ann = switch (result) {
            .ok => |o| o,
            .refused => return error.TestUnexpectedResult,
        };
        try std.testing.expectEqual(trunk.items.len, ann.origins.len);
        var answer: std.ArrayList(u8) = .empty;
        defer answer.deinit(gpa);
        for (ann.origins) |o| try answer.print(gpa, "{s} {any}\n", .{ repo.hashOf(o.rid), o.beyond });
        if (first) |f| try std.testing.expectEqualStrings(f, answer.items) else first = try answer.toOwnedSlice(gpa);
        // Holding every version's text, line arrays and diff working sets came
        // to ~2,000 file sizes here. Now it is the plaintext memo's budget for
        // this file (4 MiB or 16 file sizes) plus the versions on the walk's
        // frontier and one line diff between them, whatever the history.
        const bound = @max(4 << 20, 16 * file_bytes) + 16 * file_bytes;
        if (counted.peak >= bound) {
            std.debug.print("annotate peaked at {d} bytes for a {d}-byte file\n", .{ counted.peak, file_bytes });
            return error.TestUnexpectedResult;
        }
    }
}

// --- annotate's blame cache ----------------------------------------------------

/// A history of one file through everything the cache must answer exactly
/// as the walk does: a newline dropped and restored, a fork merged back, a
/// revert to an earlier version's bytes, and a rename. Three users, so blame
/// shows the cached logins. `c[i]` is check-in i; f.txt becomes g.txt at 8.
const BlameCacheHistory = struct {
    c: [10][]const u8,
    /// f.txt's blob at check-in 2, a middle version on trunk.
    middle_blob: [64]u8,

    fn deinit(h: *BlameCacheHistory, gpa: std.mem.Allocator) void {
        for (h.c) |x| gpa.free(x);
    }
};

fn blameCacheCommit(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, user: []const u8, epoch: []const u8, fork: bool) ![]const u8 {
    var res = if (fork)
        try runCommitEnv(gpa, io, dir, .{ .user = user, .epoch = epoch }, &.{ "commit", "-m", "edit", "--allow-fork" })
    else
        try runCommitEnv(gpa, io, dir, .{ .user = user, .epoch = epoch }, &.{ "commit", "-m", "edit" });
    defer freeResult(gpa, &res);
    try expectExitDiag(res.term, 0, res);
    return gpa.dupe(u8, try checkinHash(res.stdout));
}

fn blameCacheRun(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, argv: []const []const u8) !void {
    var res = try runZit(gpa, io, dir, argv);
    defer freeResult(gpa, &res);
    try expectExitDiag(res.term, 0, res);
}

fn blameCacheFixture(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !BlameCacheHistory {
    try initRepo(gpa, io, dir);
    var h: BlameCacheHistory = undefined;
    var made: usize = 0;
    errdefer for (h.c[0..made]) |x| gpa.free(x);
    const Step = struct { file: []const u8, text: []const u8, user: []const u8, from: ?usize = null, merge: ?usize = null, fork: bool = false };
    const steps = [_]Step{
        .{ .file = "f.txt", .text = "one\ntwo\nthree\n", .user = "alice" },
        .{ .file = "f.txt", .text = "one\nTWO\nthree", .user = "bob" }, // no trailing newline
        .{ .file = "f.txt", .text = "one\nTWO\nthree\nfour\n", .user = "alice" },
        .{ .file = "f.txt", .text = "zero\none\nTWO\nthree", .user = "carol", .from = 1, .fork = true },
        .{ .file = "f.txt", .text = "zero\none\nTWO\nthree\nfour\n", .user = "bob", .from = 2, .merge = 3 },
        .{ .file = "f.txt", .text = "one\ntwo\nthree\n", .user = "alice" }, // check-in 0's bytes again
        .{ .file = "f.txt", .text = "one\ntwo\nthree", .user = "carol" },
        .{ .file = "f.txt", .text = "one\ntwo\nthree\nfive\n", .user = "bob" },
        .{ .file = "g.txt", .text = "one\ntwo\nthree\nfive\n", .user = "alice" }, // the rename
        .{ .file = "g.txt", .text = "one\ntwo\nsix\nthree\nfive\n", .user = "carol" },
    };
    for (steps, 0..) |s, i| {
        if (s.from) |f| try blameCacheRun(gpa, io, dir, &.{ "update", h.c[f] });
        if (s.merge) |m| try blameCacheRun(gpa, io, dir, &.{ "merge", h.c[m] });
        if (i == 8) try blameCacheRun(gpa, io, dir, &.{ "mv", "f.txt", "g.txt" });
        try writeFile(io, dir, s.file, s.text);
        if (i == 0) try addManaged(gpa, io, dir, &.{"f.txt"});
        var epoch_buf: [16]u8 = undefined;
        const epoch = try std.fmt.bufPrint(&epoch_buf, "{d}", .{1_700_000_000 + i * 3600});
        h.c[i] = try blameCacheCommit(gpa, io, dir, s.user, epoch, s.fork);
        made += 1;
        if (i == 2) {
            var d: [32]u8 = undefined;
            std.crypto.hash.sha3.Sha3_256.hash(s.text, &d, .{});
            h.middle_blob = std.fmt.bytesToHex(d, .lower);
        }
    }
    return h;
}

/// One run's whole answer: exit, stdout and stderr.
const BlameCacheAnswer = struct { code: u8, out: []const u8, err: []const u8 };

fn blameCacheAnswer(arena: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, argv: []const []const u8) !BlameCacheAnswer {
    const gpa = std.testing.allocator;
    var res = try runZit(gpa, io, dir, argv);
    defer freeResult(gpa, &res);
    const code: u8 = switch (res.term) {
        .exited => |c| c,
        else => return error.TestUnexpectedTerm,
    };
    return .{ .code = code, .out = try arena.dupe(u8, res.stdout), .err = try arena.dupe(u8, res.stderr) };
}

fn expectSameAnswer(want: BlameCacheAnswer, got: BlameCacheAnswer) !void {
    try std.testing.expectEqual(want.code, got.code);
    try std.testing.expectEqualStrings(want.out, got.out);
    try std.testing.expectEqualStrings(want.err, got.err);
}

/// The answer with no cache member at all.
fn coldAnswer(arena: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, argv: []const []const u8) !BlameCacheAnswer {
    try dir.deleteTree(io, zit.blamecache.dirname);
    try dir.deleteTree(io, zit.diffcache.dirname);
    return blameCacheAnswer(arena, io, dir, argv);
}

fn blameMember(arena: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, path: []const u8) !?[]u8 {
    var buf: [zit.blamecache.dirname.len + 1 + 16]u8 = undefined;
    return dir.readFileAlloc(io, zit.blamecache.memberPath(&buf, path), arena, .limited(zit.blamecache.max_file_bytes)) catch |e| switch (e) {
        error.FileNotFound => null,
        else => e,
    };
}

fn putBlameMember(io: std.Io, dir: std.Io.Dir, path: []const u8, bytes: []const u8) !void {
    var buf: [zit.blamecache.dirname.len + 1 + 16]u8 = undefined;
    try dir.writeFile(io, .{ .sub_path = zit.blamecache.memberPath(&buf, path), .data = bytes });
}

test "annotate cache: warm answers are byte-identical to cold over merges, a rename, a revert and newline flips" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var arena_state = std.heap.ArenaAllocator.init(gpa);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var h = try blameCacheFixture(gpa, io, tmp.dir);
    defer h.deinit(gpa);

    const Target = struct { path: []const u8, at: usize };
    // Oldest first, so each later run can reuse an earlier one's entry.
    const targets = [_]Target{
        .{ .path = "f.txt", .at = 1 }, .{ .path = "f.txt", .at = 3 }, .{ .path = "f.txt", .at = 4 },
        .{ .path = "f.txt", .at = 5 }, .{ .path = "f.txt", .at = 6 }, .{ .path = "f.txt", .at = 7 },
        .{ .path = "g.txt", .at = 8 }, .{ .path = "g.txt", .at = 9 },
    };
    for ([_][]const u8{ "annotate", "blame" }) |verb| {
        var cold: [targets.len]BlameCacheAnswer = undefined;
        for (targets, &cold) |t, *c| {
            c.* = try coldAnswer(arena, io, tmp.dir, &.{ verb, t.path, h.c[t.at] });
            try std.testing.expectEqual(@as(u8, 0), c.code);
            // A clean unlimited answer leaves its entry behind.
            try std.testing.expect((try blameMember(arena, io, tmp.dir, t.path)) != null);
        }
        try tmp.dir.deleteTree(io, zit.blamecache.dirname);
        for (0..2) |_| for (targets, cold) |t, c| {
            try expectSameAnswer(c, try blameCacheAnswer(arena, io, tmp.dir, &.{ verb, t.path, h.c[t.at] }));
        };
        // Newest first: every target already has its entry, so each run is
        // answered by seeding it, and a seeded answer writes nothing.
        const f_held = (try blameMember(arena, io, tmp.dir, "f.txt")).?;
        const g_held = (try blameMember(arena, io, tmp.dir, "g.txt")).?;
        var k = targets.len;
        while (k > 0) {
            k -= 1;
            try expectSameAnswer(cold[k], try blameCacheAnswer(arena, io, tmp.dir, &.{ verb, targets[k].path, h.c[targets[k].at] }));
        }
        try std.testing.expectEqualSlices(u8, f_held, (try blameMember(arena, io, tmp.dir, "f.txt")).?);
        try std.testing.expectEqualSlices(u8, g_held, (try blameMember(arena, io, tmp.dir, "g.txt")).?);
    }
}

test "annotate cache: a damaged block, a truncation or another path's file is a miss, rebuilt to the same bytes" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var arena_state = std.heap.ArenaAllocator.init(gpa);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var h = try blameCacheFixture(gpa, io, tmp.dir);
    defer h.deinit(gpa);

    const g_argv: []const []const u8 = &.{ "blame", "g.txt", h.c[9] };
    const f_argv: []const []const u8 = &.{ "blame", "f.txt", h.c[4] };
    const f_cold = try coldAnswer(arena, io, tmp.dir, f_argv);
    const f_good = (try blameMember(arena, io, tmp.dir, "f.txt")).?;
    // One entry, so every block of the file is the target's.
    const g_cold = try coldAnswer(arena, io, tmp.dir, g_argv);
    const g_good = (try blameMember(arena, io, tmp.dir, "g.txt")).?;
    try putBlameMember(io, tmp.dir, "f.txt", f_good);

    const body = zit.blamecache.bodyStart(g_good);
    // Every header byte, then one byte in each body block and in the table.
    var offsets: std.ArrayList(usize) = .empty;
    for (0..body) |i| try offsets.append(arena, i);
    var at = body;
    while (at < g_good.len) : (at += zit.blockcrc.block_len) try offsets.append(arena, @min(at + 7, g_good.len - 1));
    try offsets.append(arena, g_good.len - 1);
    const damaged = try arena.dupe(u8, g_good);
    for (offsets.items) |off| {
        @memcpy(damaged, g_good);
        damaged[off] ^= 0x20;
        try putBlameMember(io, tmp.dir, "g.txt", damaged);
        try expectSameAnswer(g_cold, try blameCacheAnswer(arena, io, tmp.dir, g_argv));
        try std.testing.expectEqualSlices(u8, g_good, (try blameMember(arena, io, tmp.dir, "g.txt")).?);
    }
    for ([_]usize{ 0, 1, body, g_good.len / 2, g_good.len - 1 }) |len| {
        try putBlameMember(io, tmp.dir, "g.txt", g_good[0..len]);
        try expectSameAnswer(g_cold, try blameCacheAnswer(arena, io, tmp.dir, g_argv));
        try std.testing.expectEqualSlices(u8, g_good, (try blameMember(arena, io, tmp.dir, "g.txt")).?);
    }
    // The two paths' files swapped: each names the other path.
    try putBlameMember(io, tmp.dir, "g.txt", f_good);
    try putBlameMember(io, tmp.dir, "f.txt", g_good);
    try expectSameAnswer(g_cold, try blameCacheAnswer(arena, io, tmp.dir, g_argv));
    try expectSameAnswer(f_cold, try blameCacheAnswer(arena, io, tmp.dir, f_argv));
    try std.testing.expectEqualSlices(u8, g_good, (try blameMember(arena, io, tmp.dir, "g.txt")).?);
    try std.testing.expectEqualSlices(u8, f_good, (try blameMember(arena, io, tmp.dir, "f.txt")).?);
}

test "annotate cache: shunning or purging a version below a cached one answers as the cold walk does" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var arena_state = std.heap.ArenaAllocator.init(gpa);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    for ([_][]const u8{ "shun", "purge" }) |how| {
        var tmp = std.testing.tmpDir(.{});
        defer tmp.cleanup();
        var h = try blameCacheFixture(gpa, io, tmp.dir);
        defer h.deinit(gpa);
        const argv: []const []const u8 = &.{ "annotate", "g.txt", h.c[9] };
        const before = try coldAnswer(arena, io, tmp.dir, argv);
        try std.testing.expectEqual(@as(u8, 0), before.code);
        try expectSameAnswer(before, try blameCacheAnswer(arena, io, tmp.dir, argv));
        if (std.mem.eql(u8, how, "shun")) {
            try blameCacheRun(gpa, io, tmp.dir, &.{ "shun", "--force", &h.middle_blob });
        } else {
            try blameCacheRun(gpa, io, tmp.dir, &.{ "purge", "artifacts", "--force", "--reason", "thinning", &h.middle_blob });
        }
        // The warm file is still there, and must not answer.
        try std.testing.expect((try blameMember(arena, io, tmp.dir, "g.txt")) != null);
        const warm = try blameCacheAnswer(arena, io, tmp.dir, argv);
        try std.testing.expectEqual(@as(u8, 8), warm.code);
        try expectSameAnswer(try coldAnswer(arena, io, tmp.dir, argv), warm);
        // --partial reads through the gap, the same with or without a cache.
        const partial: []const []const u8 = &.{ "annotate", "--partial", "g.txt", h.c[9] };
        try expectSameAnswer(try coldAnswer(arena, io, tmp.dir, partial), try blameCacheAnswer(arena, io, tmp.dir, partial));
    }
}

test "annotate cache: --partial and a cut -n neither write the cache nor change it, and -n past the history writes the full answer" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var arena_state = std.heap.ArenaAllocator.init(gpa);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var h = try blameCacheFixture(gpa, io, tmp.dir);
    defer h.deinit(gpa);
    // Three versions of ten leave lines below the window: not the full
    // answer, so not cached.
    const limited = [_][]const []const u8{
        &.{ "annotate", "--partial", "g.txt", h.c[9] },
        &.{ "annotate", "-n", "3", "g.txt", h.c[9] },
        &.{ "blame", "--partial", "f.txt", h.c[7] },
    };
    try tmp.dir.deleteTree(io, zit.blamecache.dirname);
    for (limited) |argv| try blameCacheRun(gpa, io, tmp.dir, argv);
    try std.testing.expect((try blameMember(arena, io, tmp.dir, "g.txt")) == null);
    try std.testing.expect((try blameMember(arena, io, tmp.dir, "f.txt")) == null);
    try blameCacheRun(gpa, io, tmp.dir, &.{ "annotate", "g.txt", h.c[8] });
    const held = (try blameMember(arena, io, tmp.dir, "g.txt")).?;
    for (limited) |argv| try blameCacheRun(gpa, io, tmp.dir, argv);
    try std.testing.expectEqualSlices(u8, held, (try blameMember(arena, io, tmp.dir, "g.txt")).?);
    // Fossil's default 30 reaches past all ten: nothing is cut, and the
    // entry it writes is the one the unlimited walk writes.
    try tmp.dir.deleteTree(io, zit.blamecache.dirname);
    try blameCacheRun(gpa, io, tmp.dir, &.{ "annotate", "-n", "0", "g.txt", h.c[9] });
    const by_limit = (try blameMember(arena, io, tmp.dir, "g.txt")).?;
    try tmp.dir.deleteTree(io, zit.blamecache.dirname);
    try blameCacheRun(gpa, io, tmp.dir, &.{ "annotate", "g.txt", h.c[9] });
    try std.testing.expectEqualSlices(u8, (try blameMember(arena, io, tmp.dir, "g.txt")).?, by_limit);
    // A binary file is refused before the cache is consulted.
    try writeFile(io, tmp.dir, "logo.bin", "\x89PNG\x00\x01\n");
    try addManaged(gpa, io, tmp.dir, &.{"logo.bin"});
    gpa.free(try blameCacheCommit(gpa, io, tmp.dir, "alice", "1800000000", false));
    var bin = try runZit(gpa, io, tmp.dir, &.{ "annotate", "logo.bin" });
    defer freeResult(gpa, &bin);
    try expectExitDiag(bin.term, 1, bin);
    try std.testing.expect((try blameMember(arena, io, tmp.dir, "logo.bin")) == null);
}

/// One commit of a `-n` history: `text` written to `file` after updating to
/// step `from` and merging step `merge`, or renaming `rename` onto `file`.
const LimitStep = struct {
    text: []const u8,
    file: []const u8 = "f.txt",
    from: ?usize = null,
    merge: ?usize = null,
    /// Several merges, in this order.
    merges: []const usize = &.{},
    rename: ?[]const u8 = null,
};

/// Commit `steps` in order, an hour apart, and return their hashes.
fn limitHistory(a: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, steps: []const LimitStep) ![]const []const u8 {
    const gpa = std.testing.allocator;
    try initRepo(gpa, io, dir);
    const out = try a.alloc([]const u8, steps.len);
    const users = [_][]const u8{ "alice", "bob", "carol" };
    for (steps, 0..) |s, i| {
        if (s.from) |f| try blameCacheRun(gpa, io, dir, &.{ "update", out[f] });
        if (s.merge) |m| try blameCacheRun(gpa, io, dir, &.{ "merge", out[m] });
        for (s.merges) |m| try blameCacheRun(gpa, io, dir, &.{ "merge", out[m] });
        if (s.rename) |old| try blameCacheRun(gpa, io, dir, &.{ "mv", old, s.file });
        try writeFile(io, dir, s.file, s.text);
        if (i == 0) try addManaged(gpa, io, dir, &.{s.file});
        var epoch_buf: [16]u8 = undefined;
        const epoch = try std.fmt.bufPrint(&epoch_buf, "{d}", .{1_700_000_000 + i * 3600});
        const hash = try blameCacheCommit(gpa, io, dir, users[i % users.len], epoch, s.from != null);
        defer gpa.free(hash);
        out[i] = try a.dupe(u8, hash);
    }
    return out;
}

/// Ten lines, `l0` to `l9`, with the given lines replaced.
fn limitText(a: std.mem.Allocator, edits: []const struct { usize, []const u8 }) ![]const u8 {
    var lines: [10][]const u8 = undefined;
    for (&lines, 0..) |*l, i| l.* = try std.fmt.allocPrint(a, "l{d}", .{i});
    for (edits) |e| lines[e[0]] = e[1];
    var out: std.ArrayList(u8) = .empty;
    for (lines) |l| {
        try out.appendSlice(a, l);
        try out.append(a, '\n');
    }
    return out.items;
}

const limit_env = [_][2][]const u8{.{ "ZIT_FILEINDEX_WALK", "1" }};

/// A run with the evidence lines on: its answer without them, how many
/// versions the walk attributed, and how many edges it diffed rather than
/// replayed from the diff cache.
fn limitRun(a: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, argv: []const []const u8) !struct { BlameCacheAnswer, usize, usize } {
    const res = try runZitEnv(a, io, dir, &limit_env, argv);
    const code: u8 = switch (res.term) {
        .exited => |c| c,
        else => return error.TestUnexpectedTerm,
    };
    // The evidence must be there to count: a missing line is not a zero.
    try std.testing.expect(std.mem.indexOf(u8, res.stderr, "fileindex-annotate-diffs: ") != null);
    return .{
        .{ .code = code, .out = res.stdout, .err = try withoutEvidence(a, res.stderr) },
        evidenceCount(res.stderr, "fileindex-annotate-walked: "),
        evidenceCount(res.stderr, "fileindex-annotate-diffs: "),
    };
}

/// For every N from 3 past the history's length, `-n N` and `-l -n N` of
/// `path` at `tip` answer alike cold, warm after the cold run, from the
/// target's own cache entry and from `parent`'s, each with and without the
/// diff cache. With `follow`, a cold run past the history leaves an entry
/// behind and the target's entry answers it without diffing a version;
/// with `seeds`, it answers every count so. Whatever the window, masked or
/// not, a run after one that covered its edges diffs none of them.
fn expectLimitedFromCache(a: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, path: []const u8, tip: []const u8, parent: []const u8, steps: usize, follow: bool, seeds: bool) !void {
    var n: usize = 3;
    while (n <= steps + 2) : (n += 1) {
        const count = try std.fmt.allocPrint(a, "{d}", .{n});
        for ([_]bool{ false, true }) |list| {
            var argv: std.ArrayList([]const u8) = .empty;
            try argv.append(a, "annotate");
            if (list) try argv.append(a, "-l");
            if (!follow) try argv.append(a, "--no-follow");
            try argv.appendSlice(a, &.{ "-n", count, path, tip });
            const cold = try coldAnswer(a, io, dir, argv.items);
            try std.testing.expectEqual(@as(u8, 0), cold.code);
            if (follow and n > steps) try std.testing.expect((try blameMember(a, io, dir, path)) != null);
            const after_cold = try limitRun(a, io, dir, argv.items);
            try expectSameAnswer(cold, after_cold[0]);
            // The cold run kept every edge it diffed, so this one diffs none.
            expectEqualCount(0, after_cold[2], argv.items, "edges diffed after the cold run") catch |e| return e;

            // The diff cache alone, as the cold run and the one above left it.
            try dir.deleteTree(io, zit.blamecache.dirname);
            const diffs_only = try limitRun(a, io, dir, argv.items);
            try expectSameAnswer(cold, diffs_only[0]);
            try expectEqualCount(0, diffs_only[2], argv.items, "edges diffed from the diff cache alone");

            // After one unlimited walk, the window answers alike, every
            // edge it shares with that walk replayed.
            try dir.deleteTree(io, zit.blamecache.dirname);
            try dir.deleteTree(io, zit.diffcache.dirname);
            try blameCacheRun(std.testing.allocator, io, dir, &.{ "annotate", path, tip });
            try dir.deleteTree(io, zit.blamecache.dirname);
            const after_full = try limitRun(a, io, dir, argv.items);
            expectSameAnswer(cold, after_full[0]) catch |e| {
                std.debug.print("\n{s}: the unlimited walk's diff cache answers differently\n", .{try std.mem.join(a, " ", argv.items)});
                return e;
            };

            // The blame cache without the diff cache.
            try dir.deleteTree(io, zit.blamecache.dirname);
            try dir.deleteTree(io, zit.diffcache.dirname);
            try blameCacheRun(std.testing.allocator, io, dir, &.{ "annotate", path, tip });
            try dir.deleteTree(io, zit.diffcache.dirname);
            const seeded = try limitRun(a, io, dir, argv.items);
            expectSameAnswer(cold, seeded[0]) catch |e| {
                std.debug.print("\n{s}: the target's cache entry answers differently\n", .{try std.mem.join(a, " ", argv.items)});
                return e;
            };
            if (follow and (seeds or n > steps)) try std.testing.expectEqual(@as(usize, 0), seeded[1]);

            try dir.deleteTree(io, zit.blamecache.dirname);
            try blameCacheRun(std.testing.allocator, io, dir, &.{ "annotate", path, parent });
            const from_parent = try limitRun(a, io, dir, argv.items);
            expectSameAnswer(cold, from_parent[0]) catch |e| {
                std.debug.print("\n{s}: the parent's cache entry answers differently\n", .{try std.mem.join(a, " ", argv.items)});
                return e;
            };
            // And again with the diff cache every run above left.
            const both = try limitRun(a, io, dir, argv.items);
            try expectSameAnswer(cold, both[0]);
            try expectEqualCount(0, both[2], argv.items, "edges diffed with both caches warm");
        }
    }
}

fn expectEqualCount(want: usize, got: usize, argv: []const []const u8, what: []const u8) !void {
    if (want == got) return;
    std.debug.print("\n", .{});
    for (argv) |w| std.debug.print("{s} ", .{w});
    std.debug.print(": {d} {s}, want {d}\n", .{ got, what, want });
    return error.TestExpectedEqual;
}

test "annotate cache: -n answers from the cache as the walk does, over linear history, merges below the window, a criss-cross and a rename" {
    const io = std.testing.io;
    var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena_state.deinit();
    const a = arena_state.allocator();

    // Linear: each version edits one line and adds one.
    {
        var tmp = std.testing.tmpDir(.{});
        defer tmp.cleanup();
        var steps: [9]LimitStep = undefined;
        var text: []const u8 = try limitText(a, &.{});
        for (&steps, 0..) |*s, i| {
            if (i > 0) text = try std.fmt.allocPrint(a, "{s}n{d}\n", .{ text, i });
            if (i > 1) text = try std.mem.replaceOwned(u8, a, text, try std.fmt.allocPrint(a, "l{d}\n", .{i}), try std.fmt.allocPrint(a, "v{d}\n", .{i}));
            s.* = .{ .text = text };
        }
        const c = try limitHistory(a, io, tmp.dir, &steps);
        try expectLimitedFromCache(a, io, tmp.dir, "f.txt", c[8], c[7], steps.len, true, true);
    }
    // A side branch forked early and merged last: under a small N its
    // check-ins are older than the window, so the merge is cut.
    {
        var tmp = std.testing.tmpDir(.{});
        defer tmp.cleanup();
        const steps = [_]LimitStep{
            .{ .text = try limitText(a, &.{}) },
            .{ .text = try limitText(a, &.{.{ 1, "t1" }}) },
            .{ .text = try limitText(a, &.{ .{ 1, "t1" }, .{ 9, "s2" } }) },
            .{ .text = try limitText(a, &.{ .{ 1, "t1" }, .{ 2, "t3" } }), .from = 1 },
            .{ .text = try limitText(a, &.{ .{ 1, "t1" }, .{ 2, "t3" }, .{ 3, "t4" } }) },
            .{ .text = try limitText(a, &.{ .{ 1, "t1" }, .{ 2, "t3" }, .{ 3, "t4" }, .{ 4, "t5" } }) },
            .{ .text = try limitText(a, &.{ .{ 1, "t1" }, .{ 2, "t3" }, .{ 3, "t4" }, .{ 4, "t5" }, .{ 5, "t6" } }) },
            .{ .text = try limitText(a, &.{ .{ 1, "t1" }, .{ 2, "t3" }, .{ 3, "t4" }, .{ 4, "t5" }, .{ 5, "t6" }, .{ 9, "s2" } }), .merge = 2 },
        };
        const c = try limitHistory(a, io, tmp.dir, &steps);
        try expectLimitedFromCache(a, io, tmp.dir, "f.txt", c[7], c[6], steps.len, true, true);
    }
    // A side branch newer than trunk, merged onto trunk's older tip, both
    // sides adding the same line: the merge's primary parent lies below
    // the window while its merge parent is inside it. The full walk credits
    // the line to the primary side, which the window cannot see, so the
    // target's entry must not answer.
    {
        var tmp = std.testing.tmpDir(.{});
        defer tmp.cleanup();
        const steps = [_]LimitStep{
            .{ .text = try limitText(a, &.{}) },
            .{ .text = try limitText(a, &.{.{ 3, "X" }}) },
            .{ .text = try limitText(a, &.{ .{ 3, "X" }, .{ 6, "m2" } }) },
            .{ .text = try limitText(a, &.{.{ 9, "s3" }}), .from = 0 },
            .{ .text = try limitText(a, &.{ .{ 3, "X" }, .{ 9, "s3" } }) },
            .{ .text = try limitText(a, &.{ .{ 3, "X" }, .{ 6, "m2" }, .{ 9, "s3" } }), .from = 2, .merge = 4 },
        };
        const c = try limitHistory(a, io, tmp.dir, &steps);
        try expectLimitedFromCache(a, io, tmp.dir, "f.txt", c[5], c[4], steps.len, true, false);
        try expectLimitedFromCache(a, io, tmp.dir, "f.txt", c[5], c[2], steps.len, true, false);
    }
    // Two merge parents: the first, in P-card order, below the window, the
    // second inside it, and a line both carry that the primary does not.
    {
        var tmp = std.testing.tmpDir(.{});
        defer tmp.cleanup();
        const steps = [_]LimitStep{
            .{ .text = try limitText(a, &.{}) },
            .{ .text = try limitText(a, &.{.{ 3, "Y" }}) },
            .{ .text = try limitText(a, &.{.{ 6, "p2" }}), .from = 0 },
            .{ .text = try limitText(a, &.{.{ 9, "z3" }}), .from = 0 },
            .{ .text = try limitText(a, &.{ .{ 3, "Y" }, .{ 9, "z3" } }) },
            .{ .text = try limitText(a, &.{ .{ 6, "p2" }, .{ 7, "p5" } }), .from = 2 },
            .{ .text = try limitText(a, &.{ .{ 3, "Y" }, .{ 6, "p2" }, .{ 7, "p5" }, .{ 9, "z3" } }), .merges = &.{ 1, 4 } },
        };
        const c = try limitHistory(a, io, tmp.dir, &steps);
        try expectLimitedFromCache(a, io, tmp.dir, "f.txt", c[6], c[5], steps.len, true, false);
    }
    // A criss-cross: each side merges the other, then they meet again.
    {
        var tmp = std.testing.tmpDir(.{});
        defer tmp.cleanup();
        const steps = [_]LimitStep{
            .{ .text = try limitText(a, &.{}) },
            .{ .text = try limitText(a, &.{.{ 0, "a1" }}) },
            .{ .text = try limitText(a, &.{.{ 9, "b2" }}), .from = 0 },
            .{ .text = try limitText(a, &.{ .{ 0, "a1" }, .{ 9, "b2" } }), .from = 1, .merge = 2 },
            .{ .text = try limitText(a, &.{ .{ 0, "a1" }, .{ 7, "b4" }, .{ 9, "b2" } }), .from = 2, .merge = 1 },
            .{ .text = try limitText(a, &.{ .{ 0, "a1" }, .{ 2, "a5" }, .{ 9, "b2" } }), .from = 3 },
            .{ .text = try limitText(a, &.{ .{ 0, "a1" }, .{ 5, "b6" }, .{ 7, "b4" }, .{ 9, "b2" } }), .from = 4 },
            .{ .text = try limitText(a, &.{ .{ 0, "a1" }, .{ 2, "a5" }, .{ 5, "b6" }, .{ 7, "b4" }, .{ 9, "b2" } }), .from = 5, .merge = 6 },
        };
        const c = try limitHistory(a, io, tmp.dir, &steps);
        // Its merges' primary parents fall below some windows while their
        // merge parents do not, so not every count answers from the entry.
        try expectLimitedFromCache(a, io, tmp.dir, "f.txt", c[7], c[5], steps.len, true, false);
    }
    // A rename, followed and not.
    {
        var tmp = std.testing.tmpDir(.{});
        defer tmp.cleanup();
        var h = try blameCacheFixture(std.testing.allocator, io, tmp.dir);
        defer h.deinit(std.testing.allocator);
        // Check-in 4 merges a newer fork onto an older primary, as above.
        try expectLimitedFromCache(a, io, tmp.dir, "g.txt", h.c[9], h.c[8], h.c.len, true, false);
        try expectLimitedFromCache(a, io, tmp.dir, "g.txt", h.c[9], h.c[8], h.c.len, false, false);
    }
}

fn diffMember(arena: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, path: []const u8) !?[]u8 {
    var buf: [zit.diffcache.dirname.len + 1 + 16]u8 = undefined;
    return dir.readFileAlloc(io, zit.diffcache.memberPath(&buf, path), arena, .limited(zit.diffcache.max_file_bytes)) catch |e| switch (e) {
        error.FileNotFound => null,
        else => e,
    };
}

fn putDiffMember(io: std.Io, dir: std.Io.Dir, path: []const u8, bytes: []const u8) !void {
    var buf: [zit.diffcache.dirname.len + 1 + 16]u8 = undefined;
    try dir.writeFile(io, .{ .sub_path = zit.diffcache.memberPath(&buf, path), .data = bytes });
}

test "annotate diff cache: a damaged block, a truncation or another path's file is ignored and rewritten, the answer the same" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var arena_state = std.heap.ArenaAllocator.init(gpa);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var h = try blameCacheFixture(gpa, io, tmp.dir);
    defer h.deinit(gpa);

    const g_argv: []const []const u8 = &.{ "blame", "g.txt", h.c[9] };
    const f_argv: []const []const u8 = &.{ "blame", "f.txt", h.c[4] };
    const f_cold = try coldAnswer(arena, io, tmp.dir, f_argv);
    const f_good = (try diffMember(arena, io, tmp.dir, "f.txt")).?;
    const g_cold = try coldAnswer(arena, io, tmp.dir, g_argv);
    const g_good = (try diffMember(arena, io, tmp.dir, "g.txt")).?;
    try putDiffMember(io, tmp.dir, "f.txt", f_good);
    // Warm, the diff cache answers every edge and is left as it was.
    try tmp.dir.deleteTree(io, zit.blamecache.dirname);
    const warm = try limitRun(arena, io, tmp.dir, g_argv);
    try expectSameAnswer(g_cold, warm[0]);
    try std.testing.expectEqual(@as(usize, 0), warm[2]);
    try std.testing.expectEqualSlices(u8, g_good, (try diffMember(arena, io, tmp.dir, "g.txt")).?);

    const body = zit.diffcache.bodyStart(g_good);
    // Every header byte, then one byte in each body block and in the table.
    var offsets: std.ArrayList(usize) = .empty;
    for (0..body) |i| try offsets.append(arena, i);
    var at = body;
    while (at < g_good.len) : (at += zit.blockcrc.block_len) try offsets.append(arena, @min(at + 7, g_good.len - 1));
    try offsets.append(arena, g_good.len - 1);
    const damaged = try arena.dupe(u8, g_good);
    for (offsets.items) |off| {
        @memcpy(damaged, g_good);
        damaged[off] ^= 0x20;
        try putDiffMember(io, tmp.dir, "g.txt", damaged);
        // No blame cache, so the walk asks the diff cache for every edge.
        try tmp.dir.deleteTree(io, zit.blamecache.dirname);
        try expectSameAnswer(g_cold, try blameCacheAnswer(arena, io, tmp.dir, g_argv));
        try std.testing.expectEqualSlices(u8, g_good, (try diffMember(arena, io, tmp.dir, "g.txt")).?);
    }
    for ([_]usize{ 0, 1, body, g_good.len / 2, g_good.len - 1 }) |len| {
        try putDiffMember(io, tmp.dir, "g.txt", g_good[0..len]);
        try tmp.dir.deleteTree(io, zit.blamecache.dirname);
        try expectSameAnswer(g_cold, try blameCacheAnswer(arena, io, tmp.dir, g_argv));
        try std.testing.expectEqualSlices(u8, g_good, (try diffMember(arena, io, tmp.dir, "g.txt")).?);
    }
    // The two paths' files swapped: each names the other path.
    try putDiffMember(io, tmp.dir, "g.txt", f_good);
    try putDiffMember(io, tmp.dir, "f.txt", g_good);
    try tmp.dir.deleteTree(io, zit.blamecache.dirname);
    try expectSameAnswer(g_cold, try blameCacheAnswer(arena, io, tmp.dir, g_argv));
    try expectSameAnswer(f_cold, try blameCacheAnswer(arena, io, tmp.dir, f_argv));
    try std.testing.expectEqualSlices(u8, g_good, (try diffMember(arena, io, tmp.dir, "g.txt")).?);
    try std.testing.expectEqualSlices(u8, f_good, (try diffMember(arena, io, tmp.dir, "f.txt")).?);
}

test "annotate diff cache: an edge whose line counts disagree, every CRC right, is answered around and rewritten" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var arena_state = std.heap.ArenaAllocator.init(gpa);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var h = try blameCacheFixture(gpa, io, tmp.dir);
    defer h.deinit(gpa);
    const argv: []const []const u8 = &.{ "blame", "g.txt", h.c[9] };
    const cold = try coldAnswer(arena, io, tmp.dir, argv);
    const good = (try diffMember(arena, io, tmp.dir, "g.txt")).?;

    // The same edges, the first one claiming a line its child lacks.
    const engine_key = zit.diffcache.engineKey(zit.annotate.annotate_diff);
    const fp = std.mem.readInt(u32, good[14..18], .little);
    var f = (try zit.diffcache.File.parse(gpa, good, "g.txt", fp, engine_key)).?;
    defer f.deinit(gpa);
    const edges = try arena.alloc(zit.diffcache.Edge, f.n_edges);
    for (edges, 0..) |*e, k| {
        const got = try f.edgeAt(arena, k);
        e.* = .{ .parent = got.parent, .child = got.child, .triples = got.triples };
    }
    try std.testing.expect(edges.len > 1);
    edges[0].triples = try std.mem.concat(arena, zit.linediff.Triple, &.{ edges[0].triples, &.{.{ .copy = 0, .delete = 0, .insert = 1 }} });
    const poisoned = (try zit.diffcache.encode(gpa, "g.txt", fp, engine_key, edges, null)).?;
    defer gpa.free(poisoned);
    try putDiffMember(io, tmp.dir, "g.txt", poisoned);

    try tmp.dir.deleteTree(io, zit.blamecache.dirname);
    const first = try limitRun(arena, io, tmp.dir, argv);
    try expectSameAnswer(cold, first[0]);
    // The file was replaced, not left to send every later run the long way.
    try tmp.dir.deleteTree(io, zit.blamecache.dirname);
    const second = try limitRun(arena, io, tmp.dir, argv);
    try expectSameAnswer(cold, second[0]);
    try std.testing.expectEqual(@as(usize, 0), second[2]);
    try std.testing.expectEqualSlices(u8, good, (try diffMember(arena, io, tmp.dir, "g.txt")).?);
}

/// One in-process annotate of `tip`'s big.txt: what it answered, as
/// "hash user" per line, and what it cost.
const CachedAnnotate = struct { lines: []const []const u8, walked: usize, blocks: usize };

fn cachedAnnotate(arena: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, tip: []const u8, opts: zit.blame.Options) !CachedAnnotate {
    const gpa = std.testing.allocator;
    var repo: zit.Repository = undefined;
    try repo.open(gpa, io, dir, .{ .mode = .fast_read });
    defer repo.deinit(gpa);
    const cur = repo.checkinRid(tip).?;
    var scratch = std.heap.ArenaAllocator.init(gpa);
    defer scratch.deinit();
    var index = (try zit.filelog.loadPathIndex(gpa, io, dir, &repo, try repo.logStore(gpa))).?;
    defer index.deinit(gpa);
    const walked = zit.blame.versions_walked.load(.monotonic);
    const blocks = zit.blockcrc.blocks_checked.load(.monotonic);
    const indexed = zit.blame.indexed_walks.load(.monotonic);
    const result = try zit.blame.annotate(gpa, scratch.allocator(), io, &repo, "big.txt", cur, opts, .{ .of = noElision }, &index);
    // The file index answered, so the counts below are its walk's.
    try std.testing.expectEqual(indexed + 1, zit.blame.indexed_walks.load(.monotonic));
    const ann = switch (result) {
        .ok => |o| o,
        .refused => return error.TestUnexpectedResult,
    };
    const lines = try arena.alloc([]const u8, ann.origins.len);
    for (ann.origins, lines) |o, *l| l.* = try std.fmt.allocPrint(arena, "{s} {s} {any}", .{ repo.hashOf(o.rid), o.user orelse "-", o.beyond });
    return .{
        .lines = lines,
        .walked = zit.blame.versions_walked.load(.monotonic) - walked,
        .blocks = zit.blockcrc.blocks_checked.load(.monotonic) - blocks,
    };
}

test "annotate cache: after new commits only the new versions are walked, and the cache reads few blocks" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try seedStdinStore(gpa, io, tmp.dir, "store");
    var s = try tmp.dir.openDir(io, "store", .{});
    defer s.close(io);
    var arena_state = std.heap.ArenaAllocator.init(gpa);
    defer arena_state.deinit();
    const a = arena_state.allocator();

    var prng = std.Random.DefaultPrng.init(0xB1A3);
    const rand = prng.random();
    var next_id: u32 = 0;
    var trunk: std.ArrayList([2]u32) = .empty;
    for (0..3000) |_| {
        try trunk.append(a, .{ next_id, 0 });
        next_id += 1;
    }
    var diag: zit_checkin.Diag = .{};
    var ctx: zit_checkin.Context = undefined;
    try ctx.open(gpa, io, s, .{}, &diag);
    defer ctx.deinit();
    const Step = struct {
        fn commit(c: *zit_checkin.Context, d: *zit_checkin.Diag, al: std.mem.Allocator, n: usize, parents: []const []const u8, branch: ?[]const u8, lines: []const [2]u32) ![]const u8 {
            var user_buf: [8]u8 = undefined;
            const out = try zit_checkin.commit(c, .{
                .parents = if (parents.len == 0) null else parents,
                .branch = branch,
                .user = try std.fmt.bufPrint(&user_buf, "u{d}", .{n % 5}),
                .comment = "edit",
                .now = .{ .epoch = 1_700_000_000 + @as(i64, @intCast(n)) * 60 },
                .edits = &.{.{ .put = .{ .path = "big.txt", .bytes = try annotateBigText(al, lines) } }},
                .no_checks = true,
            }, d);
            return switch (out) {
                .recorded => |name| try al.dupe(u8, name.slice()),
                else => error.TestUnexpectedResult,
            };
        }
        fn edit(r: std.Random, t: *std.ArrayList([2]u32), al: std.mem.Allocator, id: *u32) !void {
            try t.insert(al, r.uintLessThan(usize, t.items.len + 1), .{ id.*, 0 });
            id.* += 1;
            t.items[r.uintLessThan(usize, t.items.len)][1] += 1;
        }
    };
    // Sixty versions; a side branch forks at 20 and merges back at 30.
    var tips: std.ArrayList([]const u8) = .empty;
    var side: std.ArrayList([2]u32) = .empty;
    var side_tip: []const u8 = undefined;
    for (0..60) |n| {
        if (n == 20) {
            side = try trunk.clone(a);
            side_tip = tips.items[19];
            for (0..5) |j| {
                side.items[rand.uintLessThan(usize, side.items.len)][1] += 7;
                side_tip = try Step.commit(&ctx, &diag, a, 500 + j, &.{side_tip}, if (j == 0) "side" else null, side.items);
            }
        }
        try Step.edit(rand, &trunk, a, &next_id);
        if (n == 30) {
            for (trunk.items, 0..) |*l, i| if (i < side.items.len and side.items[i][0] == l[0]) {
                l[1] = @max(l[1], side.items[i][1]);
            };
            try tips.append(a, try Step.commit(&ctx, &diag, a, n, &.{ tips.items[n - 1], side_tip }, null, trunk.items));
        } else {
            try tips.append(a, try Step.commit(&ctx, &diag, a, n, if (n == 0) &.{} else &.{tips.items[n - 1]}, null, trunk.items));
        }
    }

    const tip = tips.items[59];
    try s.deleteTree(io, zit.blamecache.dirname);
    const cold = try cachedAnnotate(a, io, s, tip, .{});
    try std.testing.expect(cold.walked >= 60);
    // The same version again: its own entry answers, nothing is diffed.
    const warm = try cachedAnnotate(a, io, s, tip, .{});
    try std.testing.expectEqual(@as(usize, 0), warm.walked);
    try std.testing.expectEqual(cold.lines.len, warm.lines.len);
    for (cold.lines, warm.lines) |x, y| try std.testing.expectEqualStrings(x, y);
    // --partial never reads the cache. A limit does, and past the history's
    // length its answer is the unlimited one.
    try std.testing.expect((try cachedAnnotate(a, io, s, tip, .{ .partial = true })).walked >= 60);
    const limited = try cachedAnnotate(a, io, s, tip, .{ .limit = 100 });
    try std.testing.expectEqual(@as(usize, 0), limited.walked);
    for (cold.lines, limited.lines) |x, y| try std.testing.expectEqualStrings(x, y);
    // Older versions fill the file, so one entry is a small part of it.
    const older = [_]usize{ 5, 10, 25, 40, 50, 55 };
    for (older) |i| _ = try cachedAnnotate(a, io, s, tips.items[i], .{});

    var at = tip;
    for ([_]usize{ 1, 10 }) |new| {
        for (0..new) |j| {
            try Step.edit(rand, &trunk, a, &next_id);
            at = try Step.commit(&ctx, &diag, a, 100 + tips.items.len + j, &.{at}, null, trunk.items);
            try tips.append(a, at);
        }
        var file_buf: [zit.blamecache.dirname.len + 1 + 16]u8 = undefined;
        const file_len = (try s.statFile(io, zit.blamecache.memberPath(&file_buf, "big.txt"), .{})).size;
        // The blocks counted are the blame cache's alone.
        try s.deleteTree(io, zit.diffcache.dirname);
        const after = try cachedAnnotate(a, io, s, at, .{});
        try std.testing.expectEqual(new, after.walked);
        try std.testing.expect(after.blocks * zit.blockcrc.block_len * 3 < file_len);
        try s.deleteTree(io, zit.blamecache.dirname);
        const fresh = try cachedAnnotate(a, io, s, at, .{});
        try std.testing.expect(fresh.walked > 60);
        for (fresh.lines, after.lines) |x, y| try std.testing.expectEqualStrings(x, y);
        // The cold walk just refilled the cache with `at` alone; put the
        // older entries back for the next round.
        for (older ++ [_]usize{59}) |i| _ = try cachedAnnotate(a, io, s, tips.items[i], .{});
    }
}

/// A check-in of a.txt holding `blob`, by `user`, with `parent` as its one
/// parent: the bytes, so a test can store it now or later.
fn blameCacheManifest(gpa: std.mem.Allocator, blob: []const u8, parent: []const u8, user: []const u8, date: []const u8) ![]u8 {
    const f = [_]artifact.FCard{.{ .filename = "a.txt", .hash = blob, .perm = null, .old_name = null, .mode = .regular }};
    const p = [_]artifact.PCard{.{ .parents = parent, .count = 1 }};
    var manifest: artifact.Manifest = .{
        .arena_state = std.heap.ArenaAllocator.init(gpa),
        .kind = .manifest,
        .a = null,
        .b = null,
        .c = .{ .comment = "edit" },
        .d = .{ .timestamp = date },
        .e = null,
        .g = null,
        .h = null,
        .i = null,
        .k = null,
        .l = null,
        .n = null,
        .r = null,
        .u = .{ .login = user },
        .z = null,
        .w = null,
        .f = &f,
        .j = &.{},
        .m = &.{},
        .p = &p,
        .q = &.{},
        .t = &.{},
    };
    defer manifest.deinit();
    return artifact.create(&manifest, gpa);
}

// --- annotate from the file index ----------------------------------------------

/// A seeded history for the index-driven annotate to be compared on: edits
/// on trunk and on side branches forked from older check-ins, merges that
/// take either side, both or neither (so a side's versions sit below a merge
/// that kept trunk's), a rename, a delete and re-add, a binary file, and,
/// when `skew`, check-ins dated before their parents. Padding files make a
/// `--submanifest` store several nodes deep. `checkins` lists every check-in.
const IndexAnnotateScene = struct {
    checkins: std.ArrayList([]const u8) = .empty,
    trunk: []const u8 = "",
    /// The side branches' heads, unmerged.
    heads: std.ArrayList([]const u8) = .empty,
    clock: i64 = 1_700_000_000,
    next_line: u32 = 0,
    n: usize = 0,

    fn text(scene: *IndexAnnotateScene, a: std.mem.Allocator, r: std.Random, old: ?[]const u8) ![]const u8 {
        var lines: std.ArrayList([]const u8) = .empty;
        if (old) |o| {
            var it = std.mem.splitScalar(u8, std.mem.trimEnd(u8, o, "\n"), '\n');
            while (it.next()) |l| try lines.append(a, l);
        }
        const edits = 1 + r.uintLessThan(usize, 3);
        for (0..edits) |_| {
            scene.next_line += 1;
            const fresh = try std.fmt.allocPrint(a, "line {d}", .{scene.next_line});
            switch (r.uintLessThan(u8, 4)) {
                0 => if (lines.items.len > 1) {
                    _ = lines.orderedRemove(r.uintLessThan(usize, lines.items.len));
                },
                1 => if (lines.items.len > 0) {
                    lines.items[r.uintLessThan(usize, lines.items.len)] = fresh;
                },
                else => try lines.insert(a, r.uintLessThan(usize, lines.items.len + 1), fresh),
            }
        }
        var out: std.ArrayList(u8) = .empty;
        for (lines.items) |l| try out.print(a, "{s}\n", .{l});
        return out.items;
    }

    fn commit(
        scene: *IndexAnnotateScene,
        a: std.mem.Allocator,
        ctx: *zit_checkin.Context,
        r: std.Random,
        skew: bool,
        parents: []const []const u8,
        branch: ?[]const u8,
        edits: []const zit_checkin.Edit,
    ) ![]const u8 {
        scene.n += 1;
        scene.clock += 60;
        // Now and then a check-in dated an hour before its parent.
        const when = if (skew and r.uintLessThan(u8, 5) == 0) scene.clock - 3600 else scene.clock;
        var diag: zit_checkin.Diag = .{};
        var user_buf: [8]u8 = undefined;
        const out = try zit_checkin.commit(ctx, .{
            .parents = if (parents.len == 0) null else parents,
            .branch = branch,
            .user = try std.fmt.bufPrint(&user_buf, "u{d}", .{scene.n % 3}),
            .comment = "edit",
            .now = .{ .epoch = when },
            .edits = edits,
            .no_checks = true,
            .allow_fork = true,
        }, &diag);
        const id = switch (out) {
            .recorded => |name| try a.dupe(u8, name.slice()),
            else => return error.TestUnexpectedResult,
        };
        try scene.checkins.append(a, id);
        return id;
    }
};

fn indexAnnotateScene(a: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, seed: u64, k: ?[]const u8, skew: bool) !IndexAnnotateScene {
    const gpa = std.testing.allocator;
    if (k) |kk| _ = try renameRun(a, io, dir, &.{}, &.{ "init", "--submanifest", kk }) else _ = try renameRun(a, io, dir, &.{}, &.{"init"});
    var prng = std.Random.DefaultPrng.init(seed);
    const r = prng.random();
    var scene: IndexAnnotateScene = .{};
    var ctx: zit_checkin.Context = undefined;
    var diag: zit_checkin.Diag = .{};
    try ctx.open(gpa, io, dir, .{}, &diag);
    defer ctx.deinit();

    const Files = std.StringArrayHashMapUnmanaged([]const u8);
    // Every check-in's files, so a side can fork from any of them.
    var at: std.StringHashMapUnmanaged(Files) = .empty;
    var trunk: Files = .empty;
    var seed_edits: std.ArrayList(zit_checkin.Edit) = .empty;
    for (0..40) |i| try seed_edits.append(a, .{ .put = .{ .path = try std.fmt.allocPrint(a, "p/{d:0>3}.txt", .{i}), .bytes = "pad\n" } });
    for ([_][]const u8{ "a.txt", "b.txt", "c.txt" }) |path| {
        const bytes = try scene.text(a, r, null);
        try trunk.put(a, path, bytes);
        try seed_edits.append(a, .{ .put = .{ .path = path, .bytes = bytes } });
    }
    try trunk.put(a, "bin.dat", "\x00one\n");
    try seed_edits.append(a, .{ .put = .{ .path = "bin.dat", .bytes = "\x00one\n" } });
    scene.trunk = try scene.commit(a, &ctx, r, skew, &.{}, null, seed_edits.items);
    try at.put(a, scene.trunk, try trunk.clone(a));

    const Side = struct { id: []const u8, files: Files };
    var sides: std.ArrayList(Side) = .empty;
    var renamed = false;
    for (0..34) |_| {
        const pick = r.uintLessThan(u8, 10);
        if (pick < 4 or (pick < 6 and sides.items.len == 0)) {
            // Edit one or two trunk files.
            var edits: std.ArrayList(zit_checkin.Edit) = .empty;
            const keys = trunk.keys();
            const path = keys[r.uintLessThan(usize, keys.len)];
            const bytes = if (std.mem.eql(u8, path, "bin.dat")) try std.fmt.allocPrint(a, "\x00{d}\n", .{scene.n}) else try scene.text(a, r, trunk.get(path).?);
            try trunk.put(a, path, bytes);
            try edits.append(a, .{ .put = .{ .path = path, .bytes = bytes } });
            if (r.boolean() and trunk.get("b.txt") != null and !std.mem.eql(u8, path, "b.txt")) {
                const b = try scene.text(a, r, trunk.get("b.txt").?);
                try trunk.put(a, "b.txt", b);
                try edits.append(a, .{ .put = .{ .path = "b.txt", .bytes = b } });
            }
            scene.trunk = try scene.commit(a, &ctx, r, skew, &.{scene.trunk}, null, edits.items);
            try at.put(a, scene.trunk, try trunk.clone(a));
        } else if (pick < 6) {
            // Edit a side branch.
            const s = &sides.items[r.uintLessThan(usize, sides.items.len)];
            const path = if (r.boolean()) "b.txt" else "c.txt";
            const bytes = try scene.text(a, r, s.files.get(path));
            try s.files.put(a, path, bytes);
            s.id = try scene.commit(a, &ctx, r, skew, &.{s.id}, null, &.{.{ .put = .{ .path = path, .bytes = bytes } }});
            try at.put(a, s.id, try s.files.clone(a));
        } else if (pick == 6 and sides.items.len < 3) {
            // Fork a side from any earlier check-in, with an edit.
            const from = scene.checkins.items[r.uintLessThan(usize, scene.checkins.items.len)];
            var files = try at.get(from).?.clone(a);
            const path = if (r.boolean()) "b.txt" else "c.txt";
            const bytes = try scene.text(a, r, files.get(path));
            try files.put(a, path, bytes);
            const name = try std.fmt.allocPrint(a, "side{d}", .{scene.n});
            const id = try scene.commit(a, &ctx, r, skew, &.{from}, name, &.{.{ .put = .{ .path = path, .bytes = bytes } }});
            try at.put(a, id, try files.clone(a));
            try sides.append(a, .{ .id = id, .files = files });
        } else if (pick == 7 and sides.items.len > 0) {
            // Merge a side: keep trunk's files, take the side's, or both.
            const which = r.uintLessThan(usize, sides.items.len);
            const s = sides.items[which];
            var edits: std.ArrayList(zit_checkin.Edit) = .empty;
            const how = r.uintLessThan(u8, 3);
            var it = s.files.iterator();
            while (it.next()) |e| {
                const mine = trunk.get(e.key_ptr.*) orelse continue;
                if (std.mem.eql(u8, mine, e.value_ptr.*) or std.mem.eql(u8, e.key_ptr.*, "bin.dat")) continue;
                const bytes = switch (how) {
                    // Trunk's: the side's versions sit below a merge that
                    // kept none of them.
                    0 => continue,
                    1 => e.value_ptr.*,
                    else => try std.mem.concat(a, u8, &.{ mine, e.value_ptr.* }),
                };
                try trunk.put(a, e.key_ptr.*, bytes);
                try edits.append(a, .{ .put = .{ .path = e.key_ptr.*, .bytes = bytes } });
            }
            scene.trunk = try scene.commit(a, &ctx, r, skew, &.{ scene.trunk, s.id }, null, edits.items);
            try at.put(a, scene.trunk, try trunk.clone(a));
            _ = sides.swapRemove(which);
        } else if (pick == 8 and !renamed) {
            // Rename a.txt, with an edit.
            const bytes = try scene.text(a, r, trunk.get("a.txt").?);
            _ = trunk.orderedRemove("a.txt");
            try trunk.put(a, "a2.txt", bytes);
            scene.trunk = try scene.commit(a, &ctx, r, skew, &.{scene.trunk}, null, &.{.{ .rename = .{ .old = "a.txt", .new = "a2.txt", .bytes = bytes } }});
            try at.put(a, scene.trunk, try trunk.clone(a));
            renamed = true;
        } else if (pick == 9) {
            // Delete c.txt, or put it back.
            if (trunk.get("c.txt") != null) {
                _ = trunk.orderedRemove("c.txt");
                scene.trunk = try scene.commit(a, &ctx, r, skew, &.{scene.trunk}, null, &.{.{ .delete = "c.txt" }});
            } else {
                const bytes = try scene.text(a, r, null);
                try trunk.put(a, "c.txt", bytes);
                scene.trunk = try scene.commit(a, &ctx, r, skew, &.{scene.trunk}, null, &.{.{ .put = .{ .path = "c.txt", .bytes = bytes } }});
            }
            try at.put(a, scene.trunk, try trunk.clone(a));
        }
    }
    for (sides.items) |s| try scene.heads.append(a, s.id);
    return scene;
}

/// Every annotate form, at `checkins`, for `paths`: with and without the
/// file index, cold and warm, and each reading the cache the other wrote.
/// Stdout, stderr and exit must agree byte for byte. Returns how many of
/// the indexed runs the index answered.
fn expectIndexedAnnotateAgrees(a: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, checkins: []const []const u8, paths: []const []const u8) !usize {
    const forms = [_][]const []const u8{
        &.{"annotate"},
        &.{ "annotate", "-n", "3" },
        &.{ "annotate", "-n", "0" },
        &.{ "annotate", "-l" },
        &.{ "annotate", "-l", "-n", "4" },
        &.{ "annotate", "--no-follow" },
        &.{ "annotate", "--partial" },
        &.{ "annotate", "--partial", "-n", "5", "-l" },
        &.{"blame"},
    };
    const indexed_env = [_][2][]const u8{.{ "ZIT_FILEINDEX_WALK", "1" }};
    const full_env = [_][2][]const u8{ .{ "ZIT_FILEINDEX_WALK", "1" }, .{ "ZIT_NO_FILE_INDEX", "1" } };
    var answered: usize = 0;
    for (checkins) |c| for (paths) |path| for (forms) |form| {
        var argv: std.ArrayList([]const u8) = .empty;
        try argv.appendSlice(a, form);
        try argv.appendSlice(a, &.{ path, c });
        // Cold without the index, then cold with it, warm with it, and the
        // full walk reading what the indexed one cached.
        try dir.deleteTree(io, zit.blamecache.dirname);
        const want = try runZitEnv(a, io, dir, &full_env, argv.items);
        try dir.deleteTree(io, zit.blamecache.dirname);
        const runs = [_]std.process.RunResult{
            try runZitEnv(a, io, dir, &indexed_env, argv.items),
            try runZitEnv(a, io, dir, &indexed_env, argv.items),
            try runZitEnv(a, io, dir, &full_env, argv.items),
        };
        for (runs, 0..) |got, i| {
            const want_err = try withoutEvidence(a, want.stderr);
            const got_err = try withoutEvidence(a, got.stderr);
            if (!std.meta.eql(want.term, got.term) or !std.mem.eql(u8, want.stdout, got.stdout) or !std.mem.eql(u8, want_err, got_err)) {
                std.debug.print("\n{s} at {s}, run {d}: the file index answers differently\n--- full walk ---\n{s}{s}\n--- indexed ---\n{s}{s}\n", .{
                    try std.mem.join(a, " ", argv.items), c[0..10], i, want.stdout, want.stderr, got.stdout, got.stderr,
                });
                return error.IndexedAnnotateDisagrees;
            }
            if (i < 2) answered += evidenceCount(got.stderr, "fileindex-annotate: ");
        }
        // The full walk never counts as the index's answer.
        try std.testing.expectEqual(@as(usize, 0), evidenceCount(want.stderr, "fileindex-annotate: "));
    };
    return answered;
}

/// `stderr` without the evidence lines `ZIT_FILEINDEX_WALK` adds.
fn withoutEvidence(a: std.mem.Allocator, stderr: []const u8) ![]const u8 {
    var out: std.ArrayList(u8) = .empty;
    var it = std.mem.splitScalar(u8, stderr, '\n');
    var first = true;
    while (it.next()) |l| {
        if (std.mem.startsWith(u8, l, "fileindex-")) continue;
        if (!first) try out.append(a, '\n');
        try out.appendSlice(a, l);
        first = false;
    }
    return out.items;
}

fn evidenceCount(stderr: []const u8, marker: []const u8) usize {
    const at = std.mem.lastIndexOf(u8, stderr, marker) orelse return 0;
    const rest = stderr[at + marker.len ..];
    const end = std.mem.indexOfScalar(u8, rest, '\n') orelse rest.len;
    return std.fmt.parseInt(usize, rest[0..end], 10) catch 0;
}

test "annotate from the file index: every form answers as the full walk does over branchy seeded histories, flat and k=7" {
    const io = std.testing.io;
    const shapes = [_]struct { seed: u64, k: ?[]const u8, skew: bool }{
        .{ .seed = 0xA11, .k = null, .skew = false },
        .{ .seed = 0xA11, .k = "7", .skew = false },
        .{ .seed = 0xB22, .k = null, .skew = true },
        .{ .seed = 0xC33, .k = "7", .skew = true },
    };
    for (shapes) |shape| {
        var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
        defer arena_state.deinit();
        const a = arena_state.allocator();
        var tmp = std.testing.tmpDir(.{});
        defer tmp.cleanup();
        var scene = try indexAnnotateScene(a, io, tmp.dir, shape.seed, shape.k, shape.skew);
        const paths = [_][]const u8{ "a.txt", "a2.txt", "b.txt", "c.txt", "bin.dat" };
        var at: std.ArrayList([]const u8) = .empty;
        try at.append(a, scene.trunk);
        try at.append(a, scene.checkins.items[scene.checkins.items.len / 2]);
        try at.appendSlice(a, scene.heads.items);
        const answered = try expectIndexedAnnotateAgrees(a, io, tmp.dir, at.items, &paths);
        // Most forms are the index's to answer; a fallback everywhere would
        // pass the comparison and prove nothing.
        try std.testing.expect(answered * 2 > at.items.len * paths.len * 9 * 2);

        // A horizon clone (phantoms below the horizon) and a cone clone
        // (blobs outside the cone elided) answer as their full walks do.
        {
            const port = try freshLoopbackPort(io);
            var server = try spawnServe(std.testing.allocator, io, tmp.dir, port);
            defer server.kill(io);
            try waitForListen(io, port);
            const url = try std.fmt.allocPrint(a, "http://127.0.0.1:{d}/", .{port});
            const mid = scene.checkins.items[scene.checkins.items.len / 2];
            var clones = std.testing.tmpDir(.{});
            defer clones.cleanup();
            const horizon = try std.fmt.allocPrint(a, "--horizon={s}", .{mid});
            _ = try renameRun(a, io, clones.dir, &.{}, &.{ "clone", "--no-open", horizon, url, "hz" });
            _ = try renameRun(a, io, clones.dir, &.{}, &.{ "clone", "--no-open", "--cone=p,b.txt", url, "cone" });
            for ([_][]const u8{ "hz", "cone" }) |name| {
                var d = try clones.dir.openDir(io, name, .{});
                defer d.close(io);
                _ = try expectIndexedAnnotateAgrees(a, io, d, &.{ scene.trunk, mid }, &paths);
            }
        }

        // After new commits, a cached older tip cuts the walk short.
        {
            var ctx: zit_checkin.Context = undefined;
            var diag: zit_checkin.Diag = .{};
            try ctx.open(std.testing.allocator, io, tmp.dir, .{}, &diag);
            defer ctx.deinit();
            var prng = std.Random.DefaultPrng.init(shape.seed + 1);
            const old_tip = scene.trunk;
            try tmp.dir.deleteTree(io, zit.blamecache.dirname);
            _ = try runZitEnv(a, io, tmp.dir, &.{}, &.{ "annotate", "b.txt", old_tip });
            for (0..2) |_| {
                const bytes = try scene.text(a, prng.random(), "kept\n");
                scene.trunk = try scene.commit(a, &ctx, prng.random(), false, &.{scene.trunk}, null, &.{.{ .put = .{ .path = "c.txt", .bytes = bytes } }});
            }
        }
        const warm = try runZitEnv(a, io, tmp.dir, &.{.{ "ZIT_FILEINDEX_WALK", "1" }}, &.{ "annotate", "b.txt", scene.trunk });
        try std.testing.expectEqual(@as(usize, 1), evidenceCount(warm.stderr, "fileindex-annotate: "));
        try tmp.dir.deleteTree(io, zit.blamecache.dirname);
        const cold = try runZitEnv(a, io, tmp.dir, &.{.{ "ZIT_NO_FILE_INDEX", "1" }}, &.{ "annotate", "b.txt", scene.trunk });
        try std.testing.expectEqualStrings(cold.stdout, warm.stdout);

        // A version gone from the middle of b.txt's history: shunned, then
        // also a purged one of a2.txt. Refusals and --partial's gaps are
        // the full walk's, and the answers still agree.
        const versions = try renameRun(a, io, tmp.dir, &.{}, &.{ "log", scene.trunk, "b.txt", "-F", "json" });
        const doc = try std.json.parseFromSliceLeaky(std.json.Value, a, versions, .{});
        const rows = doc.object.get("checkins").?.array.items;
        if (rows.len >= 3) {
            const blob = rows[rows.len / 2].object.get("uuid").?;
            if (blob == .string) _ = try renameRun(a, io, tmp.dir, &.{}, &.{ "shun", "--force", blob.string });
        }
        const a2 = try renameRun(a, io, tmp.dir, &.{}, &.{ "log", scene.trunk, "a2.txt", "-F", "json" });
        const a2_doc = try std.json.parseFromSliceLeaky(std.json.Value, a, a2, .{});
        const a2_rows = a2_doc.object.get("checkins").?.array.items;
        if (a2_rows.len >= 2) {
            const blob = a2_rows[a2_rows.len - 1].object.get("uuid").?;
            // An open side branch can keep it, and purge then refuses.
            if (blob == .string) _ = try runZitEnv(a, io, tmp.dir, &.{}, &.{ "purge", "artifacts", "--force", blob.string });
        }
        _ = try expectIndexedAnnotateAgrees(a, io, tmp.dir, &.{scene.trunk}, &paths);
    }
}

test "annotate cache: a parent the store lacks keeps the answer out of the cache, so filling it in is seen" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var arena_state = std.heap.ArenaAllocator.init(gpa);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "a.txt", "one\ntwo\n");
    try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
    const c0 = try blameCacheCommit(gpa, io, tmp.dir, "alice", "1700000000", false);
    defer gpa.free(c0);

    // P (bob) adds a line on c0; C (carol) adds one on P. Only C is stored.
    const p_text = "one\ntwo\nthree\n";
    const c_text = "one\ntwo\nthree\nfour\n";
    var p_blob_buf: [64]u8 = undefined;
    var c_blob_buf: [64]u8 = undefined;
    const p_blob = artifact.artifactName(.sha3_256, p_text, &p_blob_buf);
    const c_blob = artifact.artifactName(.sha3_256, c_text, &c_blob_buf);
    const p_bytes = try blameCacheManifest(gpa, p_blob, c0, "bob", "2024-01-01T00:00:00");
    defer gpa.free(p_bytes);
    var p_name_buf: [64]u8 = undefined;
    const p_name = artifact.artifactName(.sha3_256, p_bytes, &p_name_buf);
    const c_bytes = try blameCacheManifest(gpa, c_blob, p_name, "carol", "2024-01-02T00:00:00");
    defer gpa.free(c_bytes);
    gpa.free(try appendArtifact(gpa, io, tmp.dir, c_text));
    const c = try appendArtifact(gpa, io, tmp.dir, c_bytes);
    defer gpa.free(c);

    const argv: []const []const u8 = &.{ "blame", "a.txt", c };
    const orphaned = try coldAnswer(arena, io, tmp.dir, argv);
    try std.testing.expectEqual(@as(u8, 0), orphaned.code);
    try std.testing.expect((try blameMember(arena, io, tmp.dir, "a.txt")) == null);

    gpa.free(try appendArtifact(gpa, io, tmp.dir, p_text));
    gpa.free(try appendArtifact(gpa, io, tmp.dir, p_bytes));
    const warm = try blameCacheAnswer(arena, io, tmp.dir, argv);
    const cold = try coldAnswer(arena, io, tmp.dir, argv);
    try expectSameAnswer(cold, warm);
    try expectContains(cold.out, "alice");
    try expectContains(cold.out, "bob");
    try std.testing.expect(!std.mem.eql(u8, orphaned.out, cold.out));
}