const std = @import("std");
const support = @import("support.zig");
const expectExit = support.expectExit;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const runZit = support.runZit;
test "ticket set: an id prefix names the ticket, an unknown one and a malformed one 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);
var made = try runZit(gpa, io, tmp.dir, &.{ "ticket", "new", "--user", "u", "title=bug" });
defer freeResult(gpa, &made);
try expectExit(made.term, 0);
const id = made.stdout[0..40];
// The short id people copy from a forge: ten characters.
var by_prefix = try runZit(gpa, io, tmp.dir, &.{ "ticket", "set", id[0..10], "--user", "u", "status=open" });
defer freeResult(gpa, &by_prefix);
try expectExit(by_prefix.term, 0);
try std.testing.expectEqual(@as(usize, 65), by_prefix.stdout.len);
// It recorded against the full id: the shortest accepted prefix finds the same ticket.
var shortest = try runZit(gpa, io, tmp.dir, &.{ "ticket", "set", id[0..4], "--user", "u", "status=closed" });
defer freeResult(gpa, &shortest);
try expectExit(shortest.term, 0);
// The full id still works.
var full = try runZit(gpa, io, tmp.dir, &.{ "ticket", "set", id, "--user", "u", "status=open" });
defer freeResult(gpa, &full);
try expectExit(full.term, 0);
// A prefix no ticket carries is an unknown ticket, exit 1.
const miss: []const u8 = if (id[0] == '0') "ffffffff" else "00000000";
var unknown = try runZit(gpa, io, tmp.dir, &.{ "ticket", "set", miss, "--user", "u", "status=open" });
defer freeResult(gpa, &unknown);
try expectExit(unknown.term, 1);
try std.testing.expect(std.mem.indexOf(u8, unknown.stderr, "unknown ticket") != null);
// Too short, or not hex: refused as not a ticket id, not looked up.
var too_short = try runZit(gpa, io, tmp.dir, &.{ "ticket", "set", id[0..3], "--user", "u", "status=open" });
defer freeResult(gpa, &too_short);
try expectExit(too_short.term, 1);
try std.testing.expect(std.mem.indexOf(u8, too_short.stderr, "is not a ticket id") != null);
var not_hex = try runZit(gpa, io, tmp.dir, &.{ "ticket", "set", "zzzzzzzz", "--user", "u", "status=open" });
defer freeResult(gpa, ¬_hex);
try expectExit(not_hex.term, 1);
try std.testing.expect(std.mem.indexOf(u8, not_hex.stderr, "is not a ticket id") != null);
}