diff --git a/src/test.zig b/src/test.zig index f33b95a..275cd4f 100644 --- a/src/test.zig +++ b/src/test.zig @@ -10,6 +10,7 @@ const Allocator = std.mem.Allocator; const LocalDate = date.LocalDate; const ChainDb = main.ChainDb; const LinkDb = main.LinkDb; +const Link = main.Link; var allocator = std.heap.c_allocator; const print_output = false; @@ -50,6 +51,11 @@ const TmpDb = struct { } } + fn unloadFiles(self: *Self) void { + if (self.files) |*fs| fs.close(); + self.files = null; + } + fn deinit(self: *Self) void { if (self.files) |*fs| fs.close(); self.tmpdir.cleanup(); @@ -103,6 +109,44 @@ test "basic" { try expectEqual(@as(i64, 1704063600), link_db.links.items[0].timestamp); } +test "linking same day twice" { + var db = try TmpDb.init(); + defer db.deinit(); + + const commands = [_][]const u8{ + "", + "add Foo daily", + "link 1 20240101", + }; + + for (commands) |arg| { + try runCommand(db.path, arg); + } + + { + defer db.unloadFiles(); + + var link_db = db.linkDb(); + try link_db.materialize(1); + + const meta = link_db.meta; + try expectEqual(meta.len, 1); + + try expectEqualSlices(Link, link_db.links.items, &.{ Link{ .chain_id = 0, .timestamp = 1704063600, .tags = 0}}); + } + + try runCommand(db.path, "link 1 20240101"); + { + var link_db = db.linkDb(); + try link_db.materialize(1); + + const meta = link_db.meta; + try expectEqual(meta.len, 1); + + try expectEqualSlices(Link, link_db.links.items, &.{ Link{ .chain_id = 0, .timestamp = 1704063600, .tags = 0}}); + } +} + test "parse date" { try testDateParse(try LocalDate.init(2023, 1, 1), "20230101"); }