Skip to content
zit Artifact [9bf7d7e48d]
Login

Artifact [9bf7d7e48d]

Artifact 9bf7d7e48d3ec17c2526394818b4b99fcf0945262e0d32bee5fc514da9e95e32:


const std = @import("std");
const support = @import("support.zig");
const expectExitDiag = support.expectExitDiag;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const runZit = support.runZit;
const writeFile = support.writeFile;

test "cli users: a fossil user export imports, folds newest-mtime-wins, replaces on import, and survives pack" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    const t_ = std.testing;

    // A `fossil configuration export user` file, byte for byte as fossil
    // writes it: a `#` banner, then one card per row. `photo` is a BLOB, so
    // it arrives as SQLite's bare X'..' literal — the token shape that is not
    // quoted and that the parser must scan to whitespace rather than reject.
    try writeFile(io, tmp.dir, "users.txt",
        \\# The "user" configuration exported from
        \\# repository "tk.fossil"
        \\config /user 61
        \\1700000000 'alice' pw 'deadbeef' cap 's' info 'Admin' jx '{}'
        \\config /user 76
        \\1700000000 'bob' pw '' cap 'ei' info 'Dev' photo X'89504e470d0a1a0a' jx '{}'
        \\config /user 45
        \\1700000000 'nobody' pw '' cap 'gjorz' info ''
        \\
    );
    {
        var r = try runZit(gpa, io, tmp.dir, &.{ "config", "import", "users.txt" });
        defer freeResult(gpa, &r);
        try expectExitDiag(r.term, 0, r);
        try t_.expect(std.mem.indexOf(u8, r.stdout, "3 rows imported") != null);
    }
    {
        var u = try runZit(gpa, io, tmp.dir, &.{"users"});
        defer freeResult(gpa, &u);
        try expectExitDiag(u.term, 0, u);
        // Sorted by login; caps verbatim; the hash withheld by default.
        try t_.expectEqualStrings(
            "alice\ts\t1700000000\tAdmin\nbob\tei\t1700000000\tDev\nnobody\tgjorz\t1700000000\t\n",
            u.stdout,
        );
        try t_.expect(std.mem.indexOf(u8, u.stdout, "deadbeef") == null);
    }
    {
        var u = try runZit(gpa, io, tmp.dir, &.{ "users", "--raw" });
        defer freeResult(gpa, &u);
        try expectExitDiag(u.term, 0, u);
        try t_.expect(std.mem.indexOf(u8, u.stdout, "deadbeef") != null);
    }

    // MERGE folds newest-mtime-wins: alice's caps change under a newer mtime
    // and win; a STALE row for bob is ignored rather than applied.
    try writeFile(io, tmp.dir, "newer.txt",
        \\config /user 46
        \\1800000000 'alice' pw '' cap 'as' info 'Admin'
        \\config /user 43
        \\1600000000 'bob' pw '' cap 'zzz' info 'Old'
        \\
    );
    {
        var r = try runZit(gpa, io, tmp.dir, &.{ "config", "merge", "newer.txt" });
        defer freeResult(gpa, &r);
        try expectExitDiag(r.term, 0, r);
        var u = try runZit(gpa, io, tmp.dir, &.{"users"});
        defer freeResult(gpa, &u);
        try t_.expect(std.mem.indexOf(u8, u.stdout, "alice\tas\t1800000000") != null);
        try t_.expect(std.mem.indexOf(u8, u.stdout, "bob\tei\t") != null); // the stale row lost
        try t_.expectEqual(@as(usize, 3), std.mem.count(u8, u.stdout, "\n"));
    }

    // IMPORT replaces: the table becomes exactly the file's rows, so a login
    // the file omits is gone — fossil's own CONFIGSET_OVERWRITE.
    {
        var r = try runZit(gpa, io, tmp.dir, &.{ "config", "import", "newer.txt" });
        defer freeResult(gpa, &r);
        try expectExitDiag(r.term, 0, r);
        var u = try runZit(gpa, io, tmp.dir, &.{"users"});
        defer freeResult(gpa, &u);
        try t_.expectEqual(@as(usize, 2), std.mem.count(u8, u.stdout, "\n"));
        try t_.expect(std.mem.indexOf(u8, u.stdout, "nobody") == null);
    }

    // The table survives a pack, which folds the superseded rows away, and
    // the store still verifies.
    {
        var p = try runZit(gpa, io, tmp.dir, &.{"pack"});
        defer freeResult(gpa, &p);
        try expectExitDiag(p.term, 0, p);
        var u = try runZit(gpa, io, tmp.dir, &.{"users"});
        defer freeResult(gpa, &u);
        try t_.expectEqual(@as(usize, 2), std.mem.count(u8, u.stdout, "\n"));
        try t_.expect(std.mem.indexOf(u8, u.stdout, "alice\tas\t1800000000") != null);
        var v = try runZit(gpa, io, tmp.dir, &.{"verify"});
        defer freeResult(gpa, &v);
        try expectExitDiag(v.term, 0, v);
    }
}