const std = @import("std");
const cli = @import("zit").cli;
const support = @import("support.zig");
const addManaged = support.addManaged;
const appendClosedControl = support.appendClosedControl;
const buildFork = support.buildFork;
const checkinHash = support.checkinHash;
const expectExit = support.expectExit;
const fileExists = support.fileExists;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const lineCount = support.lineCount;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const writeFile = support.writeFile;
test "cli leaves B-leaves-8: 'leaves' is listed in the command help and suggested for a near-miss" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// The top-level command list names the verb (discoverability).
var help = try runZit(gpa, io, tmp.dir, &.{"help"});
defer freeResult(gpa, &help);
try expectExit(help.term, 0);
try std.testing.expect(std.mem.indexOf(u8, help.stdout, "leaves") != null);
// A near-miss typo suggests the real verb (did-you-mean over known_verbs).
var typo = try runZit(gpa, io, tmp.dir, &.{"leavez"});
defer freeResult(gpa, &typo);
try expectExit(typo.term, 2);
try std.testing.expect(std.mem.indexOf(u8, typo.stderr, "did you mean 'leaves'?") != null);
}
test "cli leaves B-leaves-9: --help and 'help leaves' print the verbatim help to stdout, exit 0, empty stderr" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const expected =
\\usage: zit leaves [--all] [--no-cache] [-h|--help]
\\
\\List the open leaves of the check-in DAG — check-ins with no child on the
\\same branch that carry no active closed tag. Each line is Fossil's:
\\`(N) time [hash] comment (user: … tags: …)`, with the weekday and a
\\twelve-character hash; the checkout's leaf carries *CURRENT*, as in
\\timeline. Leaves are ordered most-recent first.
\\
\\options:
\\ --all also list closed leaves, tagged "closed"
\\ --no-cache bypass every derived sidecar (the cache AND the rebuild
\\ checkpoint) and re-derive from the full log, re-verifying
\\ whole-log integrity (the cache is refreshed)
\\ -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 success
\\ 1 operational failure (not a repository, unreadable state, a refused
\\ action) — the diagnostic on stderr names it
\\ 2 usage error (an unknown option or unexpected argument)
\\
;
for ([_][]const u8{ "--help", "-h" }) |flag| {
var res = try runZit(gpa, io, tmp.dir, &.{ "leaves", flag });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings(expected, res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
}
// `zit help leaves` teaches the identical text (DX D4).
var routed = try runZit(gpa, io, tmp.dir, &.{ "help", "leaves" });
defer freeResult(gpa, &routed);
try expectExit(routed.term, 0);
try std.testing.expectEqualStrings(expected, routed.stdout);
}
test "cli leaves B-leaves-10: an unknown option is a usage error (exit 2, stderr, empty stdout)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var res = try runZit(gpa, io, tmp.dir, &.{ "leaves", "-x" });
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit leaves: unknown option '-x'\ntry 'zit leaves --help'\n",
res.stderr,
);
}
test "cli leaves B-leaves-11: outside a repository fails exit 1 on stderr, empty stdout" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var res = try runZit(gpa, io, tmp.dir, &.{"leaves"});
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 leaves B-leaves-12: an unborn repository lists no leaves (empty stdout, 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);
var res = try runZit(gpa, io, tmp.dir, &.{"leaves"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
try std.testing.expectEqual(@as(usize, 0), lineCount(res.stdout));
}
test "cli leaves §7.1: a single-commit repo lists its lone check-in as the open leaf, marked '*'" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "a.txt", "root\n");
try addManaged(gpa, io, tmp.dir, &.{"a.txt"});
var commit = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "root" });
defer freeResult(gpa, &commit);
try expectExit(commit.term, 0);
const root = try checkinHash(commit.stdout);
var res = try runZit(gpa, io, tmp.dir, &.{"leaves"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
// Data on stdout, diagnostics on stderr — the streams never mix (cli-stream).
try std.testing.expectEqualStrings("", res.stderr);
var want: [96]u8 = undefined;
// Fossil's leaf line: one line, the root's short name, *CURRENT*.
_ = &want;
try std.testing.expectEqual(@as(usize, 1), lineCount(res.stdout));
try std.testing.expect(std.mem.indexOf(u8, res.stdout, cli.shortHash(root)) != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "*CURRENT*") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "tags: trunk") != null);
try std.testing.expectEqual(@as(usize, 1), lineCount(res.stdout));
}
test "cli leaves §7.5: two open leaves are ordered mtime-descending and the current checkout is marked '*'" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const fork = try buildFork(gpa, io, tmp.dir);
defer gpa.free(fork.child1);
defer gpa.free(fork.child2);
var res = try runZit(gpa, io, tmp.dir, &.{"leaves"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stderr);
// §7.5: newest first (child2, epoch 2e9), then child1 (epoch 1e9); child2 is
// the current checkout, so it is marked '*'.
var want: [200]u8 = undefined;
// mtime-descending: child2's line first, and *CURRENT* on it (Fossil's shape).
_ = &want;
try std.testing.expectEqual(@as(usize, 2), lineCount(res.stdout));
const at2 = std.mem.indexOf(u8, res.stdout, cli.shortHash(fork.child2)) orelse return error.MissingLeaf;
const at1 = std.mem.indexOf(u8, res.stdout, cli.shortHash(fork.child1)) orelse return error.MissingLeaf;
try std.testing.expect(at2 < at1);
const cur_at = std.mem.indexOf(u8, res.stdout, "*CURRENT*") orelse return error.MissingCurrent;
try std.testing.expect(cur_at < at1);
try std.testing.expectEqual(@as(usize, 2), lineCount(res.stdout));
}
test "cli leaves --no-cache forces a full derive and yields output identical to the cached path" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// A two-open-leaf fork gives a non-trivial derived view to compare across
// the cache-hit path and the forced full re-derive.
const fork = try buildFork(gpa, io, tmp.dir);
defer gpa.free(fork.child1);
defer gpa.free(fork.child2);
// The first read misses, derives from the full log, and writes the memo.
var warm = try runZit(gpa, io, tmp.dir, &.{"leaves"});
defer freeResult(gpa, &warm);
try expectExit(warm.term, 0);
try std.testing.expect(fileExists(io, tmp.dir, ".zit-cache"));
// With the cache present, the default read replays it (the O(1) fast path).
var cached = try runZit(gpa, io, tmp.dir, &.{"leaves"});
defer freeResult(gpa, &cached);
try expectExit(cached.term, 0);
// --no-cache bypasses `tryLoad` and re-scans the whole log. It is the same
// derivation, so the output is byte-identical — the flag changes only which
// path produced it, never what it produces.
var bypass = try runZit(gpa, io, tmp.dir, &.{ "leaves", "--no-cache" });
defer freeResult(gpa, &bypass);
try expectExit(bypass.term, 0);
try std.testing.expectEqualStrings("", bypass.stderr);
try std.testing.expectEqualStrings(cached.stdout, bypass.stdout);
// Non-empty ⇒ the equality is a real comparison, not two empty listings.
try std.testing.expectEqual(@as(usize, 2), lineCount(bypass.stdout));
// The bypass path just validated the whole log, so it refreshes the memo
// rather than leaving a stale one — the next reader gets a trustworthy cache.
try std.testing.expect(fileExists(io, tmp.dir, ".zit-cache"));
}
test "cli leaves §7.4/§7.7: --all lists a closed leaf marked (closed) that the default listing omits" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const fork = try buildFork(gpa, io, tmp.dir);
defer gpa.free(fork.child1);
defer gpa.free(fork.child2);
// Close the older leaf (child1) with a synthesized control artifact (§7.4).
const ctl = try appendClosedControl(gpa, io, tmp.dir, "2020-01-01T00:00:00", fork.child1);
defer gpa.free(ctl);
// Default listing: §7.4 open predicate excludes the closed leaf — only the
// open child2 remains (enumeration filters by leaf-table membership + the
// open test, never by name resolution — §7.7).
var def = try runZit(gpa, io, tmp.dir, &.{"leaves"});
defer freeResult(gpa, &def);
try expectExit(def.term, 0);
try std.testing.expectEqualStrings("", def.stderr);
var want_def: [96]u8 = undefined;
_ = &want_def;
try std.testing.expectEqual(@as(usize, 1), lineCount(def.stdout));
try std.testing.expect(std.mem.indexOf(u8, def.stdout, cli.shortHash(fork.child2)) != null);
try std.testing.expect(std.mem.indexOf(u8, def.stdout, cli.shortHash(fork.child1)) == null);
// --all: the closed leaf stays a leaf-table member and is listed, marked
// "(closed)"; still mtime-descending, still the checkout marked '*'.
var all = try runZit(gpa, io, tmp.dir, &.{ "leaves", "--all" });
defer freeResult(gpa, &all);
try expectExit(all.term, 0);
try std.testing.expectEqualStrings("", all.stderr);
var want_all: [220]u8 = undefined;
_ = &want_all;
try std.testing.expectEqual(@as(usize, 2), lineCount(all.stdout));
const a2 = std.mem.indexOf(u8, all.stdout, cli.shortHash(fork.child2)) orelse return error.MissingLeaf;
const a1 = std.mem.indexOf(u8, all.stdout, cli.shortHash(fork.child1)) orelse return error.MissingLeaf;
try std.testing.expect(a2 < a1);
const closed_at = std.mem.indexOf(u8, all.stdout, ", closed") orelse return error.MissingClosed;
try std.testing.expect(closed_at > a1);
}