const std = @import("std");
const repeat = @import("zit").repeat;
const logstore = @import("zit").logstore;
const checkout = @import("zit").checkout;
const support = @import("support.zig");
const collectArtifactDigests = support.collectArtifactDigests;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const fileExists = support.fileExists;
const freeDigests = support.freeDigests;
const freeResult = support.freeResult;
const readProjectCode = support.readProjectCode;
const readSubmanifestSettings = support.readSubmanifestSettings;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const writeFile = support.writeFile;
const zit_exe = support.zit_exe;
const success_prefix = "Initialized empty zit repository in ";
// --- helpers --------------------------------------------------------------
/// Spawns `zit init <tail...>` with a pinned environment and cwd bound to
/// `tmp_dir`. Caller owns `result.stdout` / `result.stderr`.
fn spawnInit(
gpa: std.mem.Allocator,
io: std.Io,
tmp_dir: std.Io.Dir,
tail: []const []const u8,
) !std.process.RunResult {
var env = std.process.Environ.Map.init(gpa);
defer env.deinit();
try env.put("TZ", "UTC");
try env.put("HOME", "/zit-e2e-isolated-home-does-not-exist");
try env.put("PATH", "/usr/bin:/bin");
// `zit_exe` is relative to the build root (the test's cwd); the child
// chdir's into `tmp_dir` before exec, so resolve it to an absolute path
// first. `resolve` returns it unchanged if it is already absolute.
const cwd_abs = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(cwd_abs);
const abs_exe = try std.fs.path.resolve(gpa, &.{ cwd_abs, zit_exe });
defer gpa.free(abs_exe);
var argv_buf: [8][]const u8 = undefined;
argv_buf[0] = abs_exe;
argv_buf[1] = "init";
std.debug.assert(tail.len <= argv_buf.len - 2);
for (tail, argv_buf[2..][0..tail.len]) |src, *dst| dst.* = src;
const argv = argv_buf[0 .. 2 + tail.len];
return std.process.run(gpa, io, .{
.argv = argv,
.cwd = .{ .dir = tmp_dir },
.environ_map = &env,
});
}
/// Asserts, through the library, that `dir` holds a freshly-initialized repo:
/// a `.zit` carrying the header plus a single config frame (its §9.1 project
/// code and defaults) and no content artifacts, and a `.zit-checkout` whose
/// trunk is unborn (`version == ""`, `branch == "trunk"`). The project code is
/// a real 40-hex value, never the all-zero placeholder. Returns the `.zit`
/// bytes (caller frees) so a same-directory caller can prove byte-identity.
fn expectFreshRepo(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) ![]u8 {
const bytes = try dir.readFileAlloc(io, ".zit", gpa, .limited(1 << 20));
errdefer gpa.free(bytes);
try std.testing.expect(bytes.len > logstore.header_len); // header + config frame
const digests = try collectArtifactDigests(gpa, io, dir);
defer freeDigests(gpa, digests);
try std.testing.expectEqual(@as(usize, 0), digests.len); // no content yet
const code = try readProjectCode(gpa, io, dir);
defer gpa.free(code);
try std.testing.expectEqual(@as(usize, 40), code.len); // §9.1 artifact-hash width
try std.testing.expect(!std.mem.eql(u8, code, repeat("0", 40))); // a real, non-placeholder code
var ckbuf: [checkout.max_ck_payload + 1]u8 = undefined;
const ck = (try checkout.read(io, dir, &ckbuf)).?;
try std.testing.expectEqualStrings("", ck.version);
try std.testing.expectEqualStrings("trunk", ck.branch);
return bytes;
}
// --- tests ----------------------------------------------------------------
test "cli init B-init-2/-3/-4: happy path writes the header, unborn trunk, prints the absolute path" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var res = try spawnInit(gpa, io, tmp.dir, &.{});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
// stdout: the success line; its last whitespace-delimited token is an
// absolute `.zit` path. Structural — no wall-clock, no hash asserted.
try std.testing.expect(std.mem.startsWith(u8, res.stdout, success_prefix));
try std.testing.expect(std.mem.endsWith(u8, res.stdout, "/.zit\n"));
const path = res.stdout[success_prefix.len .. res.stdout.len - 1];
try std.testing.expect(std.fs.path.isAbsolute(path));
// discipline: nothing on stderr for a success (B-init-12).
try std.testing.expectEqualStrings("", res.stderr);
const bytes = try expectFreshRepo(gpa, io, tmp.dir);
gpa.free(bytes);
}
test "cli init B-init-5: an explicit missing directory (and its parents) is created" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var res = try spawnInit(gpa, io, tmp.dir, &.{"sub/deep"});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(std.mem.endsWith(u8, res.stdout, "/sub/deep/.zit\n"));
try std.testing.expectEqualStrings("", res.stderr);
var sub = try tmp.dir.openDir(io, "sub/deep", .{});
defer sub.close(io);
const bytes = try expectFreshRepo(gpa, io, sub);
gpa.free(bytes);
}
test "cli init B-init-5: 'init .' produces the same observable state as the bare form" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_bare = std.testing.tmpDir(.{});
defer tmp_bare.cleanup();
var res_bare = try spawnInit(gpa, io, tmp_bare.dir, &.{});
defer freeResult(gpa, &res_bare);
try expectExit(res_bare.term, 0);
const bytes_bare = try expectFreshRepo(gpa, io, tmp_bare.dir);
defer gpa.free(bytes_bare);
var tmp_dot = std.testing.tmpDir(.{});
defer tmp_dot.cleanup();
var res_dot = try spawnInit(gpa, io, tmp_dot.dir, &.{"."});
defer freeResult(gpa, &res_dot);
try expectExit(res_dot.term, 0);
const bytes_dot = try expectFreshRepo(gpa, io, tmp_dot.dir);
defer gpa.free(bytes_dot);
// `zit init` and `zit init .` are equivalent: both parse to the cwd and
// yield a valid fresh repo (asserted above). They are NOT byte-identical —
// each repository gets its own §9.1 project code, so the two codes are
// well-formed and, being per-repo, distinct.
const code_bare = try readProjectCode(gpa, io, tmp_bare.dir);
defer gpa.free(code_bare);
const code_dot = try readProjectCode(gpa, io, tmp_dot.dir);
defer gpa.free(code_dot);
try std.testing.expect(!std.mem.eql(u8, code_bare, code_dot));
}
test "cli init B-init-6: -q suppresses the success line; on-disk state is identical" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_loud = std.testing.tmpDir(.{});
defer tmp_loud.cleanup();
var res_loud = try spawnInit(gpa, io, tmp_loud.dir, &.{});
defer freeResult(gpa, &res_loud);
try expectExit(res_loud.term, 0);
const bytes_loud = try expectFreshRepo(gpa, io, tmp_loud.dir);
defer gpa.free(bytes_loud);
var tmp_quiet = std.testing.tmpDir(.{});
defer tmp_quiet.cleanup();
var res_quiet = try spawnInit(gpa, io, tmp_quiet.dir, &.{"-q"});
defer freeResult(gpa, &res_quiet);
try expectExit(res_quiet.term, 0);
// quiet: nothing on stdout, nothing on stderr.
try std.testing.expectEqualStrings("", res_quiet.stdout);
try std.testing.expectEqualStrings("", res_quiet.stderr);
const bytes_quiet = try expectFreshRepo(gpa, io, tmp_quiet.dir);
defer gpa.free(bytes_quiet);
// `-q` changes only stdout: both runs create a full, valid fresh repo
// (asserted above via expectFreshRepo). The on-disk repos are not
// byte-identical because each carries its own §9.1 project code — a
// per-repo identity, not a run-to-run nondeterminism.
const code_loud = try readProjectCode(gpa, io, tmp_loud.dir);
defer gpa.free(code_loud);
const code_quiet = try readProjectCode(gpa, io, tmp_quiet.dir);
defer gpa.free(code_quiet);
try std.testing.expect(!std.mem.eql(u8, code_loud, code_quiet));
}
test "cli init B-init-7: an existing repository refuses with exit 1 and no change" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var first = try spawnInit(gpa, io, tmp.dir, &.{});
defer freeResult(gpa, &first);
try expectExit(first.term, 0);
const before = try expectFreshRepo(gpa, io, tmp.dir);
defer gpa.free(before);
var second = try spawnInit(gpa, io, tmp.dir, &.{});
defer freeResult(gpa, &second);
try expectExit(second.term, 1);
try std.testing.expectEqualStrings("", second.stdout);
try std.testing.expect(std.mem.startsWith(u8, second.stderr, "zit: a zit repository already exists at "));
try std.testing.expect(std.mem.endsWith(u8, second.stderr, "/.zit\n"));
// No change: the `.zit` bytes are byte-identical to after the first init.
const after = try tmp.dir.readFileAlloc(io, ".zit", gpa, .limited(1 << 20));
defer gpa.free(after);
try std.testing.expectEqualSlices(u8, before, after);
}
test "cli init B-init-7: an orphan .zit-checkout does not trigger already-exists" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// Pre-seed a stale checkout with NO `.zit` present.
try checkout.write(io, tmp.dir, .{ .version = "deadbeef", .branch = "feature", .merge_parent = "", .special_op = "", .manifest_flags = "" });
var res = try spawnInit(gpa, io, tmp.dir, &.{});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
// `.zit` created and the orphan checkout atomically overwritten to unborn.
const bytes = try expectFreshRepo(gpa, io, tmp.dir);
gpa.free(bytes);
}
test "cli init B-init-8: a target path that is a file reports not-a-directory, exit 1" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
{
var f = try tmp.dir.createFile(io, "notdir", .{});
f.close(io);
}
var res = try spawnInit(gpa, io, tmp.dir, &.{"notdir"});
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expect(std.mem.startsWith(u8, res.stderr, "zit: cannot initialize: "));
try std.testing.expect(std.mem.endsWith(u8, res.stderr, " is not a directory\n"));
// Nothing created: no `.zit` beside the offending file.
try std.testing.expect(!fileExists(io, tmp.dir, ".zit"));
}
test "cli init B-init-9: a failure after .zit is created rolls it back (all-or-nothing), exit 1" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// Force the checkout-write step to fail deterministically: a DIRECTORY
// named `.zit-checkout` cannot be atomically replaced by a file, so
// `checkout.write` returns error.Storage AFTER `.zit` was created — the
// exact rollback path.
try tmp.dir.createDir(io, checkout.filename, .default_dir);
var res = try spawnInit(gpa, io, tmp.dir, &.{});
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expect(std.mem.startsWith(u8, res.stderr, "zit: cannot initialize repository at "));
try std.testing.expect(std.mem.endsWith(u8, res.stderr, "\n"));
// Rollback: `.zit` must be absent so a re-run is not poisoned into B-init-7.
try std.testing.expect(!fileExists(io, tmp.dir, ".zit"));
}
test "cli init B-init-10: an unknown option prints usage to stderr, exit 2" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var res = try spawnInit(gpa, io, tmp.dir, &.{"-x"});
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit init: unknown option '-x'\ntry 'zit init --help'\n",
res.stderr,
);
try std.testing.expect(!fileExists(io, tmp.dir, ".zit"));
}
test "cli init B-init-10: a second positional prints usage to stderr, exit 2" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var res = try spawnInit(gpa, io, tmp.dir, &.{ "a", "b" });
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit init: unexpected argument 'b'\ntry 'zit init --help'\n",
res.stderr,
);
try std.testing.expect(!fileExists(io, tmp.dir, ".zit"));
}
/// Independent transcription of the B-init-11 help text (contracts/init.md).
/// Hand-copied from the contract — never generated from the binary — so the
/// binary's output is checked against the spec, not against itself.
const expected_init_help =
\\usage: zit init [-q|--quiet] [--submanifest K] [--] [directory]
\\
\\Create an empty zit repository in <directory> (default: the current
\\directory). Writes two files there:
\\
\\ .zit the repository — a single, self-contained content log
\\ .zit-checkout the working-state record (branch trunk, no check-in yet)
\\
\\The new repository has no check-ins: the default branch 'trunk' is unborn
\\until your first `zit commit`, which records the root check-in. Artifacts
\\are named with SHA3-256 by default.
\\
\\If <directory> does not exist it is created.
\\
\\--submanifest K starts the repository with sub-manifests on and the
\\partition parameter pinned to K, so every check-in from the first is a
\\tree. Use it for a tree that will grow wide, such as an application's
\\document store. K is 1 to 12; about half the base-2 logarithm of the
\\number of files the tree will hold works well, 7 for tens of thousands.
\\Clones of this repository adopt K. See 'zit submanifest --help'.
\\
\\options:
\\ -q, --quiet do not print the confirmation lines
\\ --submanifest K start with sub-manifests on, k pinned to K (1..12)
\\ -h, --help show this help and exit
\\
\\exit status:
\\ 0 repository created
\\ 1 a repository already exists, or the path could not be initialized
\\ 2 usage error (unknown option, extra argument, or K outside 1..12)
\\
\\aliases: new
\\
;
test "cli init B-init-11: --help and -h print the verbatim help to stdout, exit 0, no changes" {
const gpa = std.testing.allocator;
const io = std.testing.io;
inline for (.{ "--help", "-h" }) |flag| {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var res = try spawnInit(gpa, io, tmp.dir, &.{flag});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings(expected_init_help, res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
// help makes no changes.
try std.testing.expect(!fileExists(io, tmp.dir, ".zit"));
try std.testing.expect(!fileExists(io, tmp.dir, ".zit-checkout"));
}
}
test "cli init B-init-12: data goes to stdout and diagnostics to stderr, never mixed" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Success: stdout carries the line, stderr is empty.
{
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var res = try spawnInit(gpa, io, tmp.dir, &.{});
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expect(res.stdout.len > 0);
try std.testing.expectEqualStrings("", res.stderr);
}
// Usage error: stderr carries the diagnostic, stdout is empty.
{
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var res = try spawnInit(gpa, io, tmp.dir, &.{"-x"});
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expect(res.stderr.len > 0);
}
}
test "cli init --submanifest K: the operator pins k at creation, the first check-in is a tree, and an out-of-range K creates nothing" {
// An application's store that will grow wide starts with sub-manifests.
// The operator supplies k (§6.3): with no check-in there is nothing to
// select it from.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
for ([_][]const u8{ "0", "13", "seven" }) |bad| {
var r = try runZit(gpa, io, tmp.dir, &.{ "init", "--submanifest", bad, "bad" });
defer freeResult(gpa, &r);
try expectExit(r.term, 2);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, "--submanifest takes k from 1 to 12") != null);
try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "bad/.zit", .{}));
}
var init = try runZit(gpa, io, tmp.dir, &.{ "init", "--submanifest", "3", "repo" });
defer freeResult(gpa, &init);
try expectExitDiag(init.term, 0, init);
try std.testing.expect(std.mem.indexOf(u8, init.stdout, "submanifest-k pinned to 3") != null);
var repo = try tmp.dir.openDir(io, "repo", .{});
defer repo.close(io);
const pinned = try readSubmanifestSettings(gpa, io, repo);
try std.testing.expect(pinned.enabled);
try std.testing.expectEqual(@as(u8, 3), pinned.k);
for (0..6) |d| {
for (0..6) |f| {
const name = try std.fmt.allocPrint(gpa, "d{d}/f{d}", .{ d, f });
defer gpa.free(name);
if (f == 0) {
const dir = try std.fmt.allocPrint(gpa, "d{d}", .{d});
defer gpa.free(dir);
try repo.createDir(io, dir, .default_dir);
}
try writeFile(io, repo, name, name);
}
}
var add = try runZit(gpa, io, repo, &.{"addremove"});
defer freeResult(gpa, &add);
try expectExitDiag(add.term, 0, add);
var c = try runCommitEnv(gpa, io, repo, .{ .user = "u" }, &.{ "commit", "-m", "first" });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
var show = try runZit(gpa, io, repo, &.{ "submanifest", "show" });
defer freeResult(gpa, &show);
try expectExitDiag(show.term, 0, show);
try std.testing.expect(std.mem.indexOf(u8, show.stdout, "this repository holds S-bearing artifacts") != null);
}