const std = @import("std");
const support = @import("support.zig");
const expectExit = support.expectExit;
const expectNonzeroExit = support.expectNonzeroExit;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const runZitStdin = support.runZitStdin;
const writeFile = support.writeFile;
test "cli technote: --comment mints the C card and --tag mints a sorted sym T run" {
// Goal: the cards fossil writes and zit could not. Authoring the same note
// in fossil 2.28 with --technote-tags "alpha beta" yields exactly
// `C <title>` plus `T +sym-alpha *` / `T +sym-beta *`, so that is the
// shape pinned here — including the ascending order §4.5.2 requires,
// which is why the tags go in reversed.
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, "body.txt", "note body\n");
var t = try runZitStdin(gpa, io, tmp.dir, "body.txt", &.{
"technote", "add", "--user", "quarry",
"--comment", "My Technote Title", "--tag", "beta",
"--tag", "alpha",
});
defer freeResult(gpa, &t);
try expectExit(t.term, 0);
try std.testing.expectEqualStrings("", t.stderr);
// stdout is "<event-id> <artifact-hash>"; the hash is the machine result.
const space = std.mem.indexOfScalar(u8, t.stdout, ' ').?;
const hash = t.stdout[space + 1 ..][0..64];
var art = try runZitStdin(gpa, io, tmp.dir, "body.txt", &.{ "artifact", "--raw", hash });
defer freeResult(gpa, &art);
try expectExit(art.term, 0);
// The C card carries the title with §4.3 escapes, so spaces are `\s`.
try std.testing.expect(std.mem.indexOf(u8, art.stdout, "C My\\sTechnote\\sTitle\n") != null);
// Self-referential `*` target, singleton `+`, sym- prefixed, ascending.
const alpha = std.mem.indexOf(u8, art.stdout, "T +sym-alpha *\n").?;
const beta = std.mem.indexOf(u8, art.stdout, "T +sym-beta *\n").?;
try std.testing.expect(alpha < beta);
// Omitting them stays exactly as before: no C card, no T run.
var plain = try runZitStdin(gpa, io, tmp.dir, "body.txt", &.{ "technote", "add", "--user", "quarry" });
defer freeResult(gpa, &plain);
try expectExit(plain.term, 0);
const p_space = std.mem.indexOfScalar(u8, plain.stdout, ' ').?;
var p_art = try runZitStdin(gpa, io, tmp.dir, "body.txt", &.{ "artifact", "--raw", plain.stdout[p_space + 1 ..][0..64] });
defer freeResult(gpa, &p_art);
try expectExit(p_art.term, 0);
try std.testing.expect(std.mem.indexOf(u8, p_art.stdout, "\nC ") == null);
try std.testing.expect(!std.mem.startsWith(u8, p_art.stdout, "C "));
try std.testing.expect(std.mem.indexOf(u8, p_art.stdout, "\nT ") == null);
}
test "cli technote: an empty --comment, a repeated --tag, and a whitespace tag are refused" {
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, "body.txt", "note body\n");
// The C card is unattachable after the fact, so an empty title would be
// permanent; a repeated tag would mint a non-ascending run the artifact's
// own admission check rejects; whitespace could not survive fossil's
// space-separated tag field.
const cases = [_][]const []const u8{
&.{ "technote", "add", "--user", "quarry", "--comment", "" },
&.{ "technote", "add", "--user", "quarry", "--tag", "x", "--tag", "x" },
&.{ "technote", "add", "--user", "quarry", "--tag", "a b" },
&.{ "technote", "add", "--user", "quarry", "--tag", "" },
};
for (cases) |args| {
var res = try runZitStdin(gpa, io, tmp.dir, "body.txt", args);
defer freeResult(gpa, &res);
try expectNonzeroExit(res.term);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expect(res.stderr.len != 0);
}
}