Skip to content
zit Artifact [7d51a6ccca]
Login

Artifact [7d51a6ccca]

Artifact 7d51a6cccaea8834030e9d2c6e589e0bc984f7a8ba9b7b72e954a5e8f6666731:


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;
const runZitStdin = support.runZitStdin;
const writeFile = support.writeFile;

test "cli forum: --mimetype mints the N card on both post and reply" {
    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", "post body\n");

    var post = try runZitStdin(gpa, io, tmp.dir, "body.txt", &.{
        "forum", "post", "--user", "quarry", "--title", "Thread", "--mimetype", "text/x-markdown",
    });
    defer freeResult(gpa, &post);
    try expectExit(post.term, 0);
    const post_hash = post.stdout[0..64];

    var post_art = try runZitStdin(gpa, io, tmp.dir, "body.txt", &.{ "artifact", "--raw", post_hash });
    defer freeResult(gpa, &post_art);
    try std.testing.expect(std.mem.indexOf(u8, post_art.stdout, "N text/x-markdown\n") != null);

    // A reply declares its own mimetype: it is a separate artifact with its
    // own body, and nothing propagates the thread root's N card to it.
    var reply = try runZitStdin(gpa, io, tmp.dir, "body.txt", &.{
        "forum", "reply", post_hash, "--user", "quarry", "--mimetype", "text/plain",
    });
    defer freeResult(gpa, &reply);
    try expectExit(reply.term, 0);

    var reply_art = try runZitStdin(gpa, io, tmp.dir, "body.txt", &.{ "artifact", "--raw", reply.stdout[0..64] });
    defer freeResult(gpa, &reply_art);
    try std.testing.expect(std.mem.indexOf(u8, reply_art.stdout, "N text/plain\n") != null);

    // Omitted, no N card is minted — the reader applies its own default.
    var bare = try runZitStdin(gpa, io, tmp.dir, "body.txt", &.{ "forum", "post", "--user", "quarry", "--title", "T2" });
    defer freeResult(gpa, &bare);
    try expectExit(bare.term, 0);
    var bare_art = try runZitStdin(gpa, io, tmp.dir, "body.txt", &.{ "artifact", "--raw", bare.stdout[0..64] });
    defer freeResult(gpa, &bare_art);
    try std.testing.expect(std.mem.indexOf(u8, bare_art.stdout, "\nN ") == null);
}

/// The cards of a stored artifact minus its D and Z lines: the two a forum
/// edit's bytes cannot share with fossil's for the same inputs (the clock,
/// and the checksum over it). Caller frees.
fn forumCardsOf(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, hash: []const u8) ![]u8 {
    var art = try runZit(gpa, io, dir, &.{ "artifact", "--raw", hash });
    defer freeResult(gpa, &art);
    try expectExit(art.term, 0);
    var out: std.ArrayList(u8) = .empty;
    errdefer out.deinit(gpa);
    var rest: []const u8 = art.stdout;
    while (rest.len != 0) {
        const nl = std.mem.indexOfScalar(u8, rest, '\n') orelse rest.len - 1;
        var line = rest[0 .. nl + 1];
        if (std.mem.startsWith(u8, line, "W ")) {
            // The W body is byte-counted and may hold anything, card-shaped
            // lines included: copy it whole.
            const size = try std.fmt.parseInt(usize, std.mem.trimEnd(u8, line[2..], "\n"), 10);
            line = rest[0 .. nl + 1 + size + 1];
        }
        if (!std.mem.startsWith(u8, line, "D ") and !std.mem.startsWith(u8, line, "Z ")) try out.appendSlice(gpa, line);
        rest = rest[line.len..];
    }
    return out.toOwnedSlice(gpa);
}

fn forumWrite(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, body_file: []const u8, args: []const []const u8) ![64]u8 {
    var res = try runZitStdin(gpa, io, dir, body_file, args);
    defer freeResult(gpa, &res);
    if (!(res.term == .exited and res.term.exited == 0)) std.debug.print("forum write failed: {s}\n", .{res.stderr});
    try expectExit(res.term, 0);
    // The machine contract: stdout is exactly the new hash and a newline.
    try std.testing.expectEqual(@as(usize, 65), res.stdout.len);
    return res.stdout[0..64].*;
}

fn expectForumCards(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, hash: []const u8, comptime fmt: []const u8, args: anytype) !void {
    const want = try std.fmt.allocPrint(gpa, fmt, args);
    defer gpa.free(want);
    const got = try forumCardsOf(gpa, io, dir, hash);
    defer gpa.free(got);
    try std.testing.expectEqualStrings(want, got);
}

// A forum edit is the artifact fossil's forum_post() (forum.c) writes with
// iEdit set: G is the thread root, an opener repeats its H (the edit form is
// prefilled with the edited version's title and mimetype), a reply repeats
// its I, and P names the version replaced. Card for card what fossil writes
// for the same inputs, D and Z aside.
test "cli forum edit: an opener edit carries G, H, N and P as fossil writes them" {
    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, "first.txt", "first body\n");
    try writeFile(io, tmp.dir, "edited.txt", "edited body\n");
    try writeFile(io, tmp.dir, "again.txt", "again\n");

    const p0 = try forumWrite(gpa, io, tmp.dir, "first.txt", &.{
        "forum", "post", "--user", "quarry", "--title", "Hello World", "--mimetype", "text/x-markdown",
    });
    // No --title or --mimetype: the edited version's are kept.
    const e1 = try forumWrite(gpa, io, tmp.dir, "edited.txt", &.{ "forum", "edit", &p0, "--user", "editor" });
    try expectForumCards(gpa, io, tmp.dir, &e1, "G {s}\nH Hello\\sWorld\nN text/x-markdown\nP {s}\nU editor\nW 12\nedited body\n\n", .{ &p0, &p0 });

    // A title change rides in H; the fossil-wiki default is never spelled out.
    // Naming the ORIGINAL again lands on the newest version: P is e1, so the
    // chain never forks.
    const e2 = try forumWrite(gpa, io, tmp.dir, "again.txt", &.{
        "forum", "edit", &p0, "--user", "editor", "--title", "New Title", "--mimetype", "text/x-fossil-wiki",
    });
    try expectForumCards(gpa, io, tmp.dir, &e2, "G {s}\nH New\\sTitle\nP {s}\nU editor\nW 6\nagain\n\n", .{ &p0, &e1 });

    // A middle version of the chain resolves to its newest too, and the
    // title it inherits is the newest's.
    const e3 = try forumWrite(gpa, io, tmp.dir, "edited.txt", &.{ "forum", "edit", &e1, "--user", "editor" });
    try expectForumCards(gpa, io, tmp.dir, &e3, "G {s}\nH New\\sTitle\nP {s}\nU editor\nW 12\nedited body\n\n", .{ &p0, &e2 });
}

test "cli forum edit: a reply edit repeats G and I, never carries H" {
    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", "body\n");
    try writeFile(io, tmp.dir, "edited.txt", "reply edited\n");

    const p0 = try forumWrite(gpa, io, tmp.dir, "body.txt", &.{ "forum", "post", "--user", "u", "--title", "T" });
    const r0 = try forumWrite(gpa, io, tmp.dir, "body.txt", &.{ "forum", "reply", &p0, "--user", "u" });
    // A reply to a reply: its I is the reply, G is still the thread root.
    const r1 = try forumWrite(gpa, io, tmp.dir, "body.txt", &.{ "forum", "reply", &r0, "--user", "u", "--mimetype", "text/x-markdown" });

    const e0 = try forumWrite(gpa, io, tmp.dir, "edited.txt", &.{ "forum", "edit", &r0, "--user", "u" });
    try expectForumCards(gpa, io, tmp.dir, &e0, "G {s}\nI {s}\nP {s}\nU u\nW 13\nreply edited\n\n", .{ &p0, &p0, &r0 });
    const e1 = try forumWrite(gpa, io, tmp.dir, "edited.txt", &.{ "forum", "edit", &r1, "--user", "u" });
    try expectForumCards(gpa, io, tmp.dir, &e1, "G {s}\nI {s}\nN text/x-markdown\nP {s}\nU u\nW 13\nreply edited\n\n", .{ &p0, &r0, &r1 });

    // A reply has no title to change: refused, as for `reply`.
    var titled = try runZitStdin(gpa, io, tmp.dir, "edited.txt", &.{ "forum", "edit", &r0, "--user", "u", "--title", "X" });
    defer freeResult(gpa, &titled);
    try expectExit(titled.term, 2);
    try std.testing.expectEqualStrings("", titled.stdout);
}

test "cli forum edit: --delete writes the empty edit fossil's Delete writes" {
    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", "body\n");
    try writeFile(io, tmp.dir, "empty.txt", "");

    const p0 = try forumWrite(gpa, io, tmp.dir, "body.txt", &.{ "forum", "post", "--user", "u", "--title", "T", "--mimetype", "text/x-markdown" });
    const r0 = try forumWrite(gpa, io, tmp.dir, "body.txt", &.{ "forum", "reply", &p0, "--user", "u", "--mimetype", "text/x-markdown" });
    const r1 = try forumWrite(gpa, io, tmp.dir, "body.txt", &.{ "forum", "edit", &r0, "--user", "u" });

    // A reply's deletion: no H, no N (fossil's delete form submits the
    // fossil-wiki default), an empty W, and P the newest version.
    const rd = try forumWrite(gpa, io, tmp.dir, "empty.txt", &.{ "forum", "edit", &r0, "--delete", "--user", "mod" });
    try expectForumCards(gpa, io, tmp.dir, &rd, "G {s}\nI {s}\nP {s}\nU mod\nW 0\n\n", .{ &p0, &p0, &r1 });

    // An opener's deletion empties its title too: `H` SP LF.
    const pd = try forumWrite(gpa, io, tmp.dir, "empty.txt", &.{ "forum", "edit", &p0, "--delete", "--user", "mod" });
    try expectForumCards(gpa, io, tmp.dir, &pd, "G {s}\nH \nP {s}\nU mod\nW 0\n\n", .{ &p0, &p0 });

    // The deletion is a version like any other: an edit after it restores
    // the post, and inherits the (empty) title fossil would prefill.
    const back = try forumWrite(gpa, io, tmp.dir, "body.txt", &.{ "forum", "edit", &p0, "--user", "u" });
    try expectForumCards(gpa, io, tmp.dir, &back, "G {s}\nH \nP {s}\nU u\nW 5\nbody\n\n", .{ &p0, &pd });
}

test "cli forum edit: refuses what fossil would not write, and what is not a post" {
    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", "body\n");
    try writeFile(io, tmp.dir, "empty.txt", "");
    try writeFile(io, tmp.dir, "blank.txt", " \n\t\n");

    const p0 = try forumWrite(gpa, io, tmp.dir, "body.txt", &.{ "forum", "post", "--user", "u", "--title", "T" });
    const wiki = try forumWrite(gpa, io, tmp.dir, "body.txt", &.{ "wiki", "commit", "Page", "--user", "u" });
    const unknown = "0000000000000000000000000000000000000000000000000000000000000000";

    const Case = struct { body: []const u8, args: []const []const u8, code: u8 };
    const cases = [_]Case{
        // An empty pipe must not delete: only --delete deletes.
        .{ .body = "empty.txt", .args = &.{ "forum", "edit", &p0, "--user", "u" }, .code = 1 },
        // Fossil refuses an all-whitespace edit that is not a delete.
        .{ .body = "blank.txt", .args = &.{ "forum", "edit", &p0, "--user", "u" }, .code = 1 },
        .{ .body = "body.txt", .args = &.{ "forum", "edit", &p0, "--delete", "--user", "u" }, .code = 1 },
        .{ .body = "empty.txt", .args = &.{ "forum", "edit", &p0, "--delete", "--title", "X", "--user", "u" }, .code = 2 },
        .{ .body = "empty.txt", .args = &.{ "forum", "edit", &p0, "--delete", "--mimetype", "text/plain", "--user", "u" }, .code = 2 },
        .{ .body = "body.txt", .args = &.{ "forum", "edit", unknown, "--user", "u" }, .code = 1 },
        .{ .body = "body.txt", .args = &.{ "forum", "edit", &wiki, "--user", "u" }, .code = 1 },
        .{ .body = "body.txt", .args = &.{ "forum", "edit", "--user", "u" }, .code = 2 },
        .{ .body = "empty.txt", .args = &.{ "forum", "post", "--delete", "--user", "u", "--title", "T" }, .code = 2 },
    };
    for (cases) |c| {
        var res = try runZitStdin(gpa, io, tmp.dir, c.body, c.args);
        defer freeResult(gpa, &res);
        try expectExit(res.term, c.code);
        try std.testing.expectEqualStrings("", res.stdout);
        try std.testing.expect(res.stderr.len != 0);
    }

    // Nothing was written: the next edit still replaces the original.
    const e = try forumWrite(gpa, io, tmp.dir, "body.txt", &.{ "forum", "edit", &p0, "--user", "u" });
    try expectForumCards(gpa, io, tmp.dir, &e, "G {s}\nH T\nP {s}\nU u\nW 5\nbody\n\n", .{ &p0, &p0 });
}