From 6c82d06cd2a15d724b8e82e1128fa15dc9e35d05 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 1 Oct 2026 07:56:48 -0300 Subject: Restore reads a clean file from disk, and an unsaved one whose file changed since the Dump is said changed, its first Save asking before overwriting it A Restore put back the dump's copy of every file pane and armed the watch with the disk as it now was, so a file changed on disk after the Dump came back as its old text, marked clean, and the next Save wrote the old text over the new without a word. A clean file pane's text is now not in the dump: Restore reads its file (an older dump's copy is read past too, and used only when the file is gone). An unsaved pane keeps its text once, base64, the plain copy beside it dropped, with the hash of the file it was read against (`disk_hash`); a Restore that finds another there says the file changed on disk, and Save asks once before overwriting it, as for a change seen while it was open. file_watch.zig's own tests were never collected (no test root imported it): main.zig's test block does now, and the restored watch test says what a clean restore now reads. Co-Authored-By: Claude Opus 5.5 --- src/ninep/ctl.zig | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src/ninep') diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index f4aff960..1a2dbdbc 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -4213,3 +4213,43 @@ test "Petscii, Palette and Ascii take the state their tag word names, set rather _ = wr(p, ctl_node, "Petscii maybe\n"); try testing.expect(th.logHas(p, "Petscii takes on or off")); } + +test "a Restore reads a clean file from disk; an unsaved one whose file changed since is said so, and its first Save asks" { + if (comptime !pardes.hosted) return; + const gpa = testing.allocator; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "one\n" }); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; + var path_buf: [4200]u8 = undefined; + const path = try std.fmt.bufPrint(&path_buf, "{s}/a.txt", .{dir}); + const p = try Pardes.init(gpa, .{ .file = path, .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + // Clean: the dump keeps no text, and the newer disk is what comes back. + try pardes.dump.dumpState(p); + try testing.expect(std.mem.indexOf(u8, p.dump_out.?, "b25lCg==") == null); // "one\n" in base64 + try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "two\n" }); + { + const restored = try pardes.dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon"); + defer restored.deinit(); + const f = pane_files.fileOf(restored.panes[0].?).?; + try testing.expectEqualStrings("two\n", f.content); + try testing.expectEqual(f.saved_revision, f.revision); + } + // Unsaved: its text comes back, the disk changed under it is said, and + // Save asks once before overwriting it. + const f = pane_files.fileOf(p.panes[0].?).?; + const edited = try gpa.dupe(u8, "edited\n"); + pardes.panes.File.setContent(p, f, edited); + try testing.expect(f.revision != f.saved_revision); + try pardes.dump.dumpState(p); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "three\n" }); + const restored = try pardes.dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon"); + defer restored.deinit(); + const rf = pane_files.fileOf(restored.panes[0].?).?; + try testing.expectEqualStrings("edited\n", rf.content); + try testing.expect(rf.disk_newer != null); + pardes.exec.saveFile(restored, 0); + try testing.expect(th.logHas(restored, "modified on disk since read (Save again to overwrite)")); +} -- cgit v1.3