Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

Test file transfer progress tracking

Mel2ea88678b7ce
diff --git a/src/files/app_items.zig b/src/files/app_items.zig
index 0357a7e..431ad36 100644
--- a/src/files/app_items.zig
+++ b/src/files/app_items.zig
@@ -291,6 +291,69 @@ test "transfer ghost filenames stay muted until manifest completion" {
try std.testing.expectEqual(normal, filenameForeground(item));
}
+test "transfer ghost byte display follows pending and matching live file progress" {
+ const path = "/destination/file.txt";
+ var item: ViewItem = .{
+ .name = "file.txt",
+ .path = path,
+ .is_dir = false,
+ .is_symlink = false,
+ .is_broken = false,
+ .bytes = 4096,
+ .mtime = 0,
+ .icon_name = "text-x-generic",
+ .transfer_ghost = true,
+ .transfer_state = .pending,
+ };
+
+ // A pending ghost must not advertise bytes that have not arrived.
+ try std.testing.expectEqual(@as(?i64, 0), currentTransferBytes(item, null));
+
+ var progress: ops.ProgressInfo = .{ .state = .running };
+ @memcpy(progress.current_file_path[0..path.len], path);
+ progress.current_file_path_len = path.len;
+ progress.current_file_bytes = 384;
+ progress.current_file_size = 4096;
+
+ item.transfer_state = .copying;
+ try std.testing.expectEqual(@as(?i64, 384), currentTransferBytes(item, progress));
+
+ // A different active path must never leak its byte count into this ghost.
+ const other_path = "/destination/other.txt";
+ @memcpy(progress.current_file_path[0..other_path.len], other_path);
+ progress.current_file_path_len = other_path.len;
+ try std.testing.expect(currentTransferBytes(item, progress) == null);
+
+ // Pausing preserves the current file's visible byte count.
+ @memcpy(progress.current_file_path[0..path.len], path);
+ progress.current_file_path_len = path.len;
+ progress.state = .paused;
+ progress.current_file_bytes = 512;
+ try std.testing.expectEqual(@as(?i64, 512), currentTransferBytes(item, progress));
+
+ // Counters larger than the ViewItem's signed size representation clamp.
+ progress.current_file_bytes = std.math.maxInt(u64);
+ try std.testing.expectEqual(
+ @as(?i64, std.math.maxInt(i64)),
+ currentTransferBytes(item, progress),
+ );
+
+ // Finished jobs and completed entries use the normal filesystem size.
+ progress.state = .finished;
+ try std.testing.expect(currentTransferBytes(item, progress) == null);
+ item.transfer_state = .completed;
+ progress.state = .running;
+ try std.testing.expect(currentTransferBytes(item, progress) == null);
+
+ // Directories and ordinary files never consume per-file ghost progress.
+ item.transfer_state = .copying;
+ item.is_dir = true;
+ try std.testing.expect(currentTransferBytes(item, progress) == null);
+ item.is_dir = false;
+ item.transfer_ghost = false;
+ try std.testing.expect(currentTransferBytes(item, progress) == null);
+}
+
pub fn renderFilenameTip(self: *App, cr: *c.cairo_t) void {
if (self.inlineTorrentHit(self.mouse_x, self.mouse_y) != null or self.audioButtonHit(self.mouse_x, self.mouse_y) != null) return;
const index = self.itemAt(self.mouse_x, self.mouse_y) orelse return;
diff --git a/src/files/app_transfer_tree.zig b/src/files/app_transfer_tree.zig
index 1da714b..27ce6d7 100644
--- a/src/files/app_transfer_tree.zig
+++ b/src/files/app_transfer_tree.zig
@@ -189,6 +189,69 @@ test "transfer tree aggregates descendant file progress" {
try std.testing.expectApproxEqAbs(@as(f64, 0.8), aggregate.fraction(), 0.0001);
}
+test "transfer tree aggregates deeply nested progress without mixing roots or outcomes" {
+ const allocator = std.testing.allocator;
+ var manifest = ops.TransferManifest.init(allocator);
+ defer manifest.deinit();
+
+ try manifest.append("/source-a", "", "/destination/a", 0, .directory, 0);
+ try manifest.append("/source-a/one", "one", "/destination/a/one", 0, .directory, 0);
+ try manifest.append("/source-a/one/two", "one/two", "/destination/a/one/two", 0, .directory, 0);
+ try manifest.append("/source-a/one/two/active.bin", "one/two/active.bin", "/destination/a/one/two/active.bin", 10, .file, 0);
+ try manifest.append("/source-a/one/done.bin", "one/done.bin", "/destination/a/one/done.bin", 20, .file, 0);
+ try manifest.append("/source-a/pending.bin", "pending.bin", "/destination/a/pending.bin", 30, .file, 0);
+ try manifest.append("/source-a/failed.bin", "failed.bin", "/destination/a/failed.bin", 40, .file, 0);
+ try manifest.append("/source-a/skipped.bin", "skipped.bin", "/destination/a/skipped.bin", 50, .file, 0);
+ try manifest.append("/source-a/cancelled.bin", "cancelled.bin", "/destination/a/cancelled.bin", 60, .file, 0);
+ try manifest.append("/source-b", "", "/destination/b", 0, .directory, 1);
+ try manifest.append("/source-b/other.bin", "other.bin", "/destination/b/other.bin", 70, .file, 1);
+
+ manifest.entries.items[3].state = .copying;
+ manifest.entries.items[4].state = .completed;
+ manifest.entries.items[6].state = .failed;
+ manifest.entries.items[7].state = .skipped;
+ manifest.entries.items[8].state = .cancelled;
+ manifest.entries.items[10].state = .completed;
+
+ var tree = try projection.build(allocator, manifest.entries.items);
+ defer tree.deinit();
+
+ var state = State.init(allocator);
+ defer state.deinit();
+
+ // Root includes every visible descendant, but excludes skipped/cancelled
+ // payloads. Failed payload remains in the denominator.
+ const root = state.aggregateProgress(&tree, 0, "/source-a/one/two/active.bin", 4, 10);
+ try std.testing.expectEqual(@as(u64, 100), root.total_bytes);
+ try std.testing.expectEqual(@as(u64, 24), root.completed_bytes);
+ try std.testing.expect(root.active);
+ try std.testing.expect(root.failed);
+ try std.testing.expectApproxEqAbs(@as(f64, 0.24), root.fraction(), 0.0001);
+
+ // The middle directory aggregates only its own subtree.
+ const middle = state.aggregateProgress(&tree, 1, "/source-a/one/two/active.bin", 4, 10);
+ try std.testing.expectEqual(@as(u64, 30), middle.total_bytes);
+ try std.testing.expectEqual(@as(u64, 24), middle.completed_bytes);
+ try std.testing.expect(middle.active);
+ try std.testing.expect(!middle.failed);
+ try std.testing.expectApproxEqAbs(@as(f64, 0.8), middle.fraction(), 0.0001);
+
+ // The deepest directory sees only the active file.
+ const deepest = state.aggregateProgress(&tree, 2, "/source-a/one/two/active.bin", 4, 10);
+ try std.testing.expectEqual(@as(u64, 10), deepest.total_bytes);
+ try std.testing.expectEqual(@as(u64, 4), deepest.completed_bytes);
+ try std.testing.expect(deepest.active);
+ try std.testing.expectApproxEqAbs(@as(f64, 0.4), deepest.fraction(), 0.0001);
+
+ // A second selected root never inherits the first root's active progress.
+ const other_root = state.aggregateProgress(&tree, 9, "/source-a/one/two/active.bin", 4, 10);
+ try std.testing.expectEqual(@as(u64, 70), other_root.total_bytes);
+ try std.testing.expectEqual(@as(u64, 70), other_root.completed_bytes);
+ try std.testing.expect(!other_root.active);
+ try std.testing.expect(!other_root.failed);
+ try std.testing.expectEqual(@as(f64, 1.0), other_root.fraction());
+}
+
test "transfer tree hides skipped and cancelled entries" {
const allocator = std.testing.allocator;
var manifest = ops.TransferManifest.init(allocator);
diff --git a/src/files/ops.zig b/src/files/ops.zig
index 1d97896..ed7886b 100644
--- a/src/files/ops.zig
+++ b/src/files/ops.zig
@@ -48,6 +48,66 @@ test "transfer manifest subtree and root outcome states remain isolated" {
try std.testing.expectEqual(TransferEntryState.completed, runner.manifest.entries.items[2].state);
}
+test "JobRunner tracks current file bytes independently and resets on file changes" {
+ const a = std.testing.allocator;
+ const runner = try JobRunner.init(a);
+ defer runner.deinit();
+
+ runner.state = .running;
+ runner.kind = .copy;
+ runner.beginFileProgress("/destination/first.bin", 1024);
+
+ var progress = runner.pollProgress();
+ try std.testing.expectEqualStrings(
+ "/destination/first.bin",
+ progress.current_file_path[0..progress.current_file_path_len],
+ );
+ try std.testing.expectEqual(@as(u64, 0), progress.current_file_bytes);
+ try std.testing.expectEqual(@as(u64, 1024), progress.current_file_size);
+
+ runner.addFileTransferredBytes(256);
+ progress = runner.pollProgress();
+ try std.testing.expectEqual(@as(u64, 256), progress.current_file_bytes);
+ try std.testing.expectEqual(@as(u64, 256), progress.transferred_bytes);
+
+ runner.addFileTransferredBytes(128);
+ progress = runner.pollProgress();
+ try std.testing.expectEqual(@as(u64, 384), progress.current_file_bytes);
+ try std.testing.expectEqual(@as(u64, 384), progress.transferred_bytes);
+
+ // Starting the next file resets only its per-file counter.
+ runner.beginFileProgress("/destination/second.bin", 64);
+ progress = runner.pollProgress();
+ try std.testing.expectEqualStrings(
+ "/destination/second.bin",
+ progress.current_file_path[0..progress.current_file_path_len],
+ );
+ try std.testing.expectEqual(@as(u64, 0), progress.current_file_bytes);
+ try std.testing.expectEqual(@as(u64, 64), progress.current_file_size);
+ try std.testing.expectEqual(@as(u64, 384), progress.transferred_bytes);
+
+ runner.addFileTransferredBytes(64);
+ progress = runner.pollProgress();
+ try std.testing.expectEqual(@as(u64, 64), progress.current_file_bytes);
+ try std.testing.expectEqual(@as(u64, 448), progress.transferred_bytes);
+}
+
+test "JobRunner saturates current file byte counters instead of overflowing" {
+ const a = std.testing.allocator;
+ const runner = try JobRunner.init(a);
+ defer runner.deinit();
+
+ runner.state = .running;
+ runner.beginFileProgress("/destination/large.bin", std.math.maxInt(u64));
+
+ runner.addFileTransferredBytes(std.math.maxInt(u64) - 10);
+ runner.addFileTransferredBytes(20);
+
+ const progress = runner.pollProgress();
+ try std.testing.expectEqual(std.math.maxInt(u64), progress.current_file_bytes);
+ try std.testing.expectEqual(std.math.maxInt(u64), progress.transferred_bytes);
+}
+
pub const JobState = enum {
idle,
running,