const std = @import("std");
const repeat = @import("zit").repeat;
const support = @import("support.zig");
const expectExit = support.expectExit;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const runZit = support.runZit;
const runZitStdin = support.runZitStdin;
const writeFile = support.writeFile;
test "attach: add, list, replace, rm, size limit, stdin, wiki target, prefix strip, stub" {
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, "shot.png", "PNG bytes v1");
// A ticket to attach to.
var t = try runZit(gpa, io, tmp.dir, &.{ "ticket", "new", "--user", "u", "title=bug" });
defer freeResult(gpa, &t);
try expectExit(t.term, 0);
const ticket_id = std.mem.trim(u8, t.stdout[0..std.mem.indexOfScalar(u8, t.stdout, ' ').?], " \n");
// add: prints the A artifact's hash, then the blob's hash.
var a1 = try runZit(gpa, io, tmp.dir, &.{ "attach", "add", ticket_id, "shot.png", "--user", "u", "--comment", "first" });
defer freeResult(gpa, &a1);
try expectExit(a1.term, 0);
var lines = std.mem.splitScalar(u8, std.mem.trim(u8, a1.stdout, "\n"), '\n');
const a1_hash = lines.next().?;
const blob1 = lines.next().?;
try std.testing.expectEqual(@as(usize, 64), a1_hash.len);
try std.testing.expectEqual(@as(usize, 64), blob1.len);
try std.testing.expect(lines.next() == null);
// The artifact round-trips: type attachment, one A card naming the blob.
var art = try runZit(gpa, io, tmp.dir, &.{ "artifact", a1_hash });
defer freeResult(gpa, &art);
try expectExit(art.term, 0);
try std.testing.expect(std.mem.indexOf(u8, art.stdout, "type: attachment") != null);
try std.testing.expect(std.mem.indexOf(u8, art.stdout, blob1) != null);
// list shows it with the blob's size.
var l1 = try runZit(gpa, io, tmp.dir, &.{ "attach", "list", ticket_id });
defer freeResult(gpa, &l1);
try expectExit(l1.term, 0);
try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, l1.stdout, "\n"));
try std.testing.expect(std.mem.startsWith(u8, l1.stdout, "shot.png 12 - "));
try std.testing.expect(std.mem.indexOf(u8, l1.stdout, blob1) != null);
// Same name again replaces: one entry, the new blob.
try writeFile(io, tmp.dir, "shot.png", "PNG bytes v2!");
var a2 = try runZit(gpa, io, tmp.dir, &.{ "attach", "add", ticket_id, "shot.png", "--user", "u", "--name", "some/dir/shot.png", "--mimetype", "image/png" });
defer freeResult(gpa, &a2);
try expectExit(a2.term, 0);
var l2 = try runZit(gpa, io, tmp.dir, &.{ "attach", "list", ticket_id });
defer freeResult(gpa, &l2);
try expectExit(l2.term, 0);
try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, l2.stdout, "\n"));
try std.testing.expect(std.mem.startsWith(u8, l2.stdout, "shot.png 13 image/png "));
try std.testing.expect(std.mem.indexOf(u8, l2.stdout, blob1) == null);
// rm: the association ends; nothing leaves the store.
var r = try runZit(gpa, io, tmp.dir, &.{ "attach", "rm", ticket_id, "shot.png", "--user", "u" });
defer freeResult(gpa, &r);
try expectExit(r.term, 0);
var l3 = try runZit(gpa, io, tmp.dir, &.{ "attach", "list", ticket_id });
defer freeResult(gpa, &l3);
try expectExit(l3.term, 0);
try std.testing.expectEqualStrings("", l3.stdout);
var still = try runZit(gpa, io, tmp.dir, &.{ "artifact", blob1 });
defer freeResult(gpa, &still);
try expectExit(still.term, 0);
// Over the default 1 MiB limit refuses and stores nothing; 0 lifts it.
const big = try gpa.alloc(u8, (1 << 20) + 1);
defer gpa.free(big);
@memset(big, 'x');
try writeFile(io, tmp.dir, "big.bin", big);
var over = try runZit(gpa, io, tmp.dir, &.{ "attach", "add", ticket_id, "big.bin", "--user", "u" });
defer freeResult(gpa, &over);
try std.testing.expect(over.term.exited != 0);
try std.testing.expect(std.mem.indexOf(u8, over.stderr, "attachment-size-limit is 1048576 bytes") != null);
try std.testing.expectEqualStrings("", over.stdout);
var lift = try runZit(gpa, io, tmp.dir, &.{ "settings", "attachment-size-limit", "0" });
defer freeResult(gpa, &lift);
try expectExit(lift.term, 0);
var ok_big = try runZit(gpa, io, tmp.dir, &.{ "attach", "add", ticket_id, "big.bin", "--user", "u" });
defer freeResult(gpa, &ok_big);
try expectExit(ok_big.term, 0);
// A wiki-name target, from stdin, needs --name.
try writeFile(io, tmp.dir, "notes.txt", "hello wiki\n");
var noname = try runZitStdin(gpa, io, tmp.dir, "notes.txt", &.{ "attach", "add", "Home", "-", "--user", "u" });
defer freeResult(gpa, &noname);
try std.testing.expect(noname.term.exited != 0);
var w = try runZitStdin(gpa, io, tmp.dir, "notes.txt", &.{ "attach", "add", "Home", "-", "--name", "notes.txt", "--user", "u" });
defer freeResult(gpa, &w);
try expectExit(w.term, 0);
var lw = try runZit(gpa, io, tmp.dir, &.{ "attach", "list", "Home" });
defer freeResult(gpa, &lw);
try expectExit(lw.term, 0);
try std.testing.expect(std.mem.startsWith(u8, lw.stdout, "notes.txt 11 - "));
// A quarry stub lists as external with the size it points at,
// and `zit artifact` on the blob says so.
const stub = "quarry-attachment 1\nsha256 " ++ (repeat("4f", 32)) ++ "\nsize 18349022\ntype video/mp4\nname screen-recording.mp4\n";
try writeFile(io, tmp.dir, "stub.txt", stub);
var sa = try runZitStdin(gpa, io, tmp.dir, "stub.txt", &.{ "attach", "add", ticket_id, "-", "--name", "screen-recording.mp4", "--mimetype", "video/mp4", "--user", "u" });
defer freeResult(gpa, &sa);
try expectExit(sa.term, 0);
var sl = std.mem.splitScalar(u8, std.mem.trim(u8, sa.stdout, "\n"), '\n');
_ = sl.next().?;
const stub_blob = sl.next().?;
var ls = try runZit(gpa, io, tmp.dir, &.{ "attach", "list", ticket_id });
defer freeResult(gpa, &ls);
try expectExit(ls.term, 0);
try std.testing.expect(std.mem.indexOf(u8, ls.stdout, "screen-recording.mp4 18349022 video/mp4 external:sha256:" ++ (repeat("4f", 32))) != null);
var sd = try runZit(gpa, io, tmp.dir, &.{ "artifact", stub_blob });
defer freeResult(gpa, &sd);
try expectExit(sd.term, 0);
try std.testing.expect(std.mem.indexOf(u8, sd.stdout, "attachment-stub: screen-recording.mp4, 18349022 bytes external") != null);
// The whole-repository listing names the target first.
var all = try runZit(gpa, io, tmp.dir, &.{ "attach", "list" });
defer freeResult(gpa, &all);
try expectExit(all.term, 0);
try std.testing.expect(std.mem.indexOf(u8, all.stdout, "Home notes.txt 11 ") != null);
}