diff options
Diffstat (limited to 'src/macos.zig')
| -rw-r--r-- | src/macos.zig | 751 |
1 files changed, 375 insertions, 376 deletions
diff --git a/src/macos.zig b/src/macos.zig index 9a734bcc..d550e2bd 100644 --- a/src/macos.zig +++ b/src/macos.zig @@ -241,169 +241,8 @@ const FileWatches = struct { } }; -test "mac file watch generations own paths, coalesce, and reject stale callbacks" { - var watches: FileWatches = .{}; - defer watches.deinit(std.testing.allocator); - - var first_path = [_]u8{ '/', 't', 'm', 'p', '/', 'a' }; - const first = try watches.replace( - std.testing.allocator, - 3, - &first_path, - 41, - .{ .text = 11 }, - ); - first_path[first_path.len - 1] = 'x'; - try std.testing.expectEqualStrings("/tmp/a", watches.entries[3].?.path); - try std.testing.expect(watches.notify(3, first)); - try std.testing.expect(!watches.notify(3, first)); - try std.testing.expect(watches.takeDirty(3)); - try std.testing.expect(!watches.takeDirty(3)); - - const second = try watches.replace( - std.testing.allocator, - 3, - "/tmp/b", - 42, - .{ .text = 22 }, - ); - try std.testing.expect(second != first); - try std.testing.expect(!watches.notify(3, first)); - try std.testing.expect(watches.notify(3, second)); - try std.testing.expectEqual(@as(u8, 1), watches.entries[3].?.retries_left); - try std.testing.expect(watches.takeDirty(3)); - try std.testing.expect(watches.retry(3, second)); - try std.testing.expect(!watches.retry(3, second)); - try std.testing.expect(watches.takeDirty(3)); - try std.testing.expect(watches.notify(3, second)); - try std.testing.expectEqual(@as(u8, 1), watches.entries[3].?.retries_left); - try std.testing.expect(watches.takeDirty(3)); - watches.restampText(3, "/tmp/not-b", 99); - try std.testing.expectEqual(@as(u64, 22), watches.entries[3].?.generation_on_disk.text); - watches.restampText(3, "/tmp/b", 33); - try std.testing.expectEqual(@as(u64, 33), watches.entries[3].?.generation_on_disk.text); - - const stopped = watches.stop(std.testing.allocator, 3); - try std.testing.expect(stopped != second); - try std.testing.expect(!watches.notify(3, second)); - try std.testing.expect(!watches.retry(3, second)); - try std.testing.expect(watches.entries[3] == null); - - _ = try watches.replace(std.testing.allocator, 4, "/tmp/book.pdf", 77, .{ .kind = null }); - watches.restampText(4, "/tmp/book.pdf", 123); - try std.testing.expect(watches.entries[4].?.generation_on_disk.kind == null); -} - const file_watcher_swift = @embedFile("macos/Sources/FileWatcher.swift"); -test "mac queued results never cancel a newer LSP request" { - const gpa = std.testing.allocator; - const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); - defer core.deinit(); - var st: State = .{ - .gpa = gpa, - .threaded = undefined, - .io = std.testing.io, - .core = core, - .runtime = .{}, - .config_arena = .init(gpa), - .prompt_rcs = .{}, - .inbox = .{ .gpa = gpa, .lsp_allocator = gpa, .io = std.testing.io }, - }; - defer st.config_arena.deinit(); - defer st.inbox.close(); - core.lspRequest(0, .status, ""); - const old_id = core.lsp_ask.wait.?.id; - core.lspRequest(0, .status, ""); - const current_id = core.lsp_ask.wait.?.id; - st.inbox.lsp_id = current_id; - st.inbox.lsp_task = .{ .id = current_id, .future = .{ .any_future = null, .result = {} } }; - // The inbox turns the superseded answer away. - st.inbox.push(.{ .lsp = .{ .id = old_id, .rows = try gpa.dupe(u8, "stale result\n") } }); - try std.testing.expectEqual(@as(usize, 0), st.inbox.completion_len); - _ = drainInbox(&st); - try std.testing.expect(st.inbox.lsp_task != null); - try std.testing.expectEqual(current_id, st.inbox.lsp_task.?.id); - try std.testing.expectEqual(current_id, core.lsp_ask.wait.?.id); - st.inbox.push(.{ .lsp = .{ .id = current_id, .rows = try gpa.dupe(u8, "") } }); - try std.testing.expect(drainInbox(&st)); - try std.testing.expect(st.inbox.lsp_task == null); - try std.testing.expect(core.lsp_ask.wait == null); -} - -test "mac worker setup failures finish matching LSP and pipe requests" { - const gpa = std.testing.allocator; - var failing_vtable = std.testing.io.vtable.*; - failing_vtable.concurrent = std.Io.failingConcurrent; - const failing_io: std.Io = .{ .userdata = std.testing.io.userdata, .vtable = &failing_vtable }; - for (0..2) |failure| { - const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); - defer core.deinit(); - const pane = try core.setTestFile("abc"); - var failing = std.testing.FailingAllocator.init(gpa, .{ - .fail_index = if (failure == 0) 0 else std.math.maxInt(usize), - }); - var st: State = .{ - .gpa = failing.allocator(), - .threaded = undefined, - .io = failing_io, - .core = core, - .runtime = .{}, - .config_arena = .init(gpa), - .prompt_rcs = .{}, - .inbox = .{ .gpa = failing.allocator(), .lsp_allocator = failing.allocator(), .io = failing_io }, - }; - defer st.config_arena.deinit(); - defer st.inbox.close(); - core.lspRequest(core.view().active, .status, ""); - const req: host_io.Lsp.Request = .{ - .id = core.lsp_ask.wait.?.id, - .kind = .status, - .pane = @intCast(core.view().active), - .offset = 0, - .arg = "", - }; - lspRequest(&st, req); - try std.testing.expect(core.lsp_ask.wait == null); - try std.testing.expectEqualStrings("abc", pane.file.?.content); - - pane.body.cur_col = 2; - pane.body.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; - core.update(.{ .key = .{ .cp = '|' } }); - core.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); - core.update(.{ .key = .{ .cp = pardes.Key.enter } }); - const pipe_id = core.pipe.wait.?.id; - pipeRequest(&st, pipe_id); - try std.testing.expect(core.pipe.wait == null); - try std.testing.expectEqualStrings("abc", pane.file.?.content); - try std.testing.expect(st.inbox.lsp_task == null); - try std.testing.expectEqual(@as(usize, 0), st.pipe_tasks.len); - } -} - -test "mac host watcher covers file and directory vnode events, debounce, and generation callback" { - try std.testing.expect(std.mem.indexOf( - u8, - file_watcher_swift, - "deletingLastPathComponent()", - ) != null); - try std.testing.expect(std.mem.indexOf( - u8, - file_watcher_swift, - "self.reopenFileSource()", - ) != null); - try std.testing.expect(std.mem.indexOf( - u8, - file_watcher_swift, - "DispatchQueue.main.asyncAfter", - ) != null); - try std.testing.expect(std.mem.indexOf( - u8, - file_watcher_swift, - "pardes_watch_changed(self.pane, self.generation)", - ) != null); -} - const Inbox = host_io.Inbox; const Msg = Inbox.Msg; @@ -499,7 +338,7 @@ fn initCore(runtime: ?*const Runtime, cols_arg: u16, rows_arg: u16) !void { .boot = .tty, .frame_allocator = allocs.frame, .image_allocator = allocs.image, - + .tree_sitter_allocator = allocs.tree_sitter, }; if (captureEnv(config_arena.allocator())) |*env| { @@ -656,38 +495,6 @@ fn spendTickTime(previous_ns: u64, now_ns: u64, bank_ns: u64) TickSpend { return .{ .elapsed_ns = elapsed, .steps = steps, .bank_ns = bank }; } -test "the animation clock spends real time, not callbacks" { - const frame = pardes.animation.frame_ns; - const expectEqual = std.testing.expectEqual; - - const first = spendTickTime(0, 999 * std.time.ns_per_s, 0); - try expectEqual(@as(u32, 1), first.steps); - try expectEqual(frame, first.elapsed_ns); - - const on_time = spendTickTime(1_000, 1_000 + frame, 0); - try expectEqual(@as(u32, 1), on_time.steps); - try expectEqual(@as(u64, 0), on_time.bank_ns); - - const late = spendTickTime(1_000, 1_000 + 2 * frame, 0); - try expectEqual(@as(u32, 2), late.steps); - - var bank: u64 = 0; - var steps: u32 = 0; - for (0..3) |_| { - const partial = spendTickTime(1_000, 1_000 + 6 * std.time.ns_per_ms, bank); - bank = partial.bank_ns; - steps += partial.steps; - } - try expectEqual(@as(u32, 1), steps); - try expectEqual(@as(u64, 2 * std.time.ns_per_ms), bank); - - const stall = spendTickTime(1_000, 1_000 + 10 * std.time.ns_per_s, 0); - try expectEqual(max_tick_catch_up_ns, stall.elapsed_ns); - try expectEqual(@as(u32, @intCast(max_tick_catch_up_ns / frame)), stall.steps); - - try expectEqual(@as(u32, 1), spendTickTime(5_000, 4_000, 0).steps); -} - export fn pardes_animating() bool { pardes.turn.wake(); defer pardes.turn.rest(); @@ -796,31 +603,6 @@ export fn pardes_fallback_font_name(index: u32, len: *u32) ?[*]const u8 { return name.ptr; } -test "tagline percent falls back before init and follows live core state" { - try std.testing.expectEqual(pardes.config.gui_tagline_font_percent, taglineFontPercent(null)); - - const core = try pardes.Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer core.deinit(); - const changed: u8 = if (pardes.config.gui_tagline_font_percent == 37) 38 else 37; - core.settings.font.tagline_percent = changed; - try std.testing.expectEqual(changed, taglineFontPercent(core)); -} - -test "the fallback preference order crosses the ABI intact and ends at the boundary" { - try std.testing.expectEqual(@as(u32, fonts.fallback_names.len), pardes_fallback_font_count()); - try std.testing.expect(pardes_fallback_font_count() > 0); - - for (fonts.fallback_names, 0..) |want, i| { - var len: u32 = 0; - const got = pardes_fallback_font_name(@intCast(i), &len) orelse return error.MissingFallbackName; - try std.testing.expectEqualStrings(want, got[0..len]); - } - - var len: u32 = 12345; - try std.testing.expect(pardes_fallback_font_name(pardes_fallback_font_count(), &len) == null); - try std.testing.expectEqual(@as(u32, 0), len); -} - export fn pardes_scene() Scene { pardes.turn.wake(); defer pardes.turn.rest(); @@ -1184,162 +966,6 @@ fn restoreCore(st: *State) bool { return true; } -test "mac Restore keeps host state and rejects callbacks from the old core" { - const gpa = std.testing.allocator; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - const core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); - var st: State = .{ - .gpa = gpa, - .threaded = undefined, - .io = std.testing.io, - .core = core, - .runtime = .{}, - .config_arena = .init(gpa), - .prompt_rcs = .{}, - .inbox = .{ .gpa = gpa, .lsp_allocator = gpa, .io = std.testing.io }, - .frame_len = 7, - .rotate_coasting = true, - }; - defer st.core.deinit(); - defer st.config_arena.deinit(); - defer st.file_watches.deinit(gpa); - defer st.inbox.close(); - const marker = try st.config_arena.allocator().dupe(u8, "host configuration"); - _ = try core.setTestFile("saved body\n"); - const old_serial = core.panes[0].?.serial; - st.gens[0] = 23; - const old_watch = try st.file_watches.replace(gpa, 0, "/test.txt", old_serial, .{ .text = 0 }); - try dump.dumpState(core); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "restore.zon", .data = core.dump_out.? }); - while (core.nextEffect()) |_| {} - - var command: [512]u8 = undefined; - core.update(.{ .command = try std.fmt.bufPrint(&command, "Restore .zig-cache/tmp/{s}/missing.zon", .{tmp.sub_path}) }); - try std.testing.expect(!restoreCore(&st)); - try std.testing.expectEqual(core, st.core); - try std.testing.expectEqual(old_watch, st.file_watches.entries[0].?.generation); - try std.testing.expect(st.rotate_coasting); - - core.lspRequest(0, .status, ""); - const old_request = core.lsp_ask.wait.?.id; - st.inbox.lsp_id = old_request; - st.inbox.lsp_task = .{ .id = old_request, .future = .{ .any_future = null, .result = {} } }; - st.inbox.push(.{ .lsp = .{ .id = old_request, .rows = try gpa.dupe(u8, "old result") } }); - for (0..selection_pipe.Tasks.capacity) |index| { - const id: u32 = @intCast(index); - try std.testing.expect(st.pipe_tasks.add(.{ .id = id, .future = .{ .any_future = null, .result = {} } })); - st.inbox.push(.{ .pipe = .{ - .id = id, - .success = false, - .outputs = &.{}, - .failure = .{ .kind = .exit, .code = 1, .stderr = try gpa.dupe(u8, "old filter failure") }, - } }); - } - st.inbox.acceptReader(0, 23); - _ = st.inbox.pushOutput(0, 23, try gpa.dupe(u8, "old PTY output")); - core.update(.{ .command = try std.fmt.bufPrint(&command, "Restore .zig-cache/tmp/{s}/restore.zon", .{tmp.sub_path}) }); - try std.testing.expect(restoreCore(&st)); - try std.testing.expect(st.core != core); - try std.testing.expect(st.core.panes[0].?.serial > old_serial); - try std.testing.expectEqualStrings("saved body\n", st.core.panes[0].?.file.?.content); - try std.testing.expectEqualStrings("host configuration", marker); - try std.testing.expectEqual(@as(usize, 0), st.frame_len); - try std.testing.expect(!st.rotate_coasting); - try std.testing.expectEqual(@as(u32, 24), st.gens[0]); - try std.testing.expect(!st.file_watches.notify(0, old_watch)); - try std.testing.expect(st.file_watches.generations[0] > old_watch); - try std.testing.expectEqual(@as(usize, 0), st.inbox.len); - try std.testing.expectEqual(@as(usize, 0), st.pipe_tasks.len); - try std.testing.expect(st.inbox.lsp_task == null); - try std.testing.expect(!drainInbox(&st)); - try std.testing.expect(!st.cwd_stale[0]); - - st.core.lspRequest(0, .status, ""); - const new_request = st.core.lsp_ask.wait.?.id; - try std.testing.expect(new_request > old_request); - st.inbox.lsp_id = new_request; - st.inbox.lsp_task = .{ .id = new_request, .future = .{ .any_future = null, .result = {} } }; - st.inbox.push(.{ .lsp = .{ .id = new_request, .rows = &.{} } }); - try std.testing.expect(drainInbox(&st)); - try std.testing.expect(st.inbox.lsp_task == null); - try std.testing.expect(st.core.lsp_ask.wait == null); - - const pane = st.core.panes[0].?; - pane.body.cur_col = 4; - pane.body.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; - st.core.update(.{ .key = .{ .cp = '|' } }); - st.core.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); - st.core.update(.{ .key = .{ .cp = pardes.Key.enter } }); - const pipe_id = st.core.pipe.wait.?.id; - try std.testing.expect(st.pipe_tasks.add(.{ .id = pipe_id, .future = .{ .any_future = null, .result = {} } })); - const output = try gpa.dupe(u8, "FRESH"); - const outputs = gpa.dupe([]u8, &.{output}) catch |err| { - gpa.free(output); - return err; - }; - st.inbox.push(.{ .pipe = .{ .id = pipe_id, .success = true, .outputs = outputs } }); - try std.testing.expect(drainInbox(&st)); - try std.testing.expect(st.core.pipe.wait == null); - try std.testing.expectEqual(@as(usize, 0), st.pipe_tasks.len); - try std.testing.expectEqualStrings("FRESH body\n", pane.file.?.content); -} - -test "mac Restore cancels a PTY reader waiting for output capacity" { - const gpa = std.testing.allocator; - const io = std.testing.io; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - var st: State = .{ - .gpa = gpa, - .threaded = undefined, - .io = io, - .core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }), - .runtime = .{}, - .config_arena = .init(gpa), - .prompt_rcs = .{}, - .inbox = .{ .gpa = gpa, .lsp_allocator = gpa, .io = io }, - }; - defer st.core.deinit(); - defer st.config_arena.deinit(); - defer st.file_watches.deinit(gpa); - defer st.inbox.close(); - try dump.dumpState(st.core); - try tmp.dir.writeFile(io, .{ .sub_path = "restore.zon", .data = st.core.dump_out.? }); - while (st.core.nextEffect()) |_| {} - st.inbox.acceptReader(1, 1); - for (0..Inbox.output_capacity) |_| _ = st.inbox.pushOutput(1, 1, try gpa.dupe(u8, "retained output")); - const child = try host_io.forkShell(null, 0, &st.prompt_rcs, "/bin/sh", "", 12, 40); - st.gens[0] = 1; - st.ptys[0] = .{ .file = child.file, .pid = child.pid, .gen = 1, .reader = .{ .any_future = null, .result = {} } }; - defer { - if (st.ptys[0]) |pt| if (pt.pid == child.pid) reap(&st, 0); - if (libc.waitpid(child.pid, null, posix.W.NOHANG) == 0) { - _ = libc.kill(child.pid, libc.SIG.KILL); - _ = libc.waitpid(child.pid, null, 0); - } - } - startReader(&st, &st.ptys[0].?, 0); - try std.testing.expect(host_io.writeFd(child.file.handle, "printf 'after-full'; exit\n")); - for (0..1000) |_| { - if (st.inbox.space.state.load(.acquire).waiters != 0) break; - try io.sleep(.fromMilliseconds(1), .awake); - } - try std.testing.expectEqual(@as(u16, 1), st.inbox.space.state.load(.acquire).waiters); - var command: [512]u8 = undefined; - st.core.update(.{ .command = try std.fmt.bufPrint(&command, "Restore .zig-cache/tmp/{s}/restore.zon", .{tmp.sub_path}) }); - try std.testing.expect(restoreCore(&st)); - try std.testing.expect(st.ptys[0] == null); - try std.testing.expectEqual(@as(usize, 0), st.inbox.len); - try std.testing.expectEqual(@as(u16, 0), st.inbox.space.state.load(.acquire).waiters); - st.inbox.acceptReader(0, st.gens[0]); - _ = st.inbox.pushOutput(0, st.gens[0], try gpa.dupe(u8, "fresh output")); - var batch = st.inbox.take(); - defer for (batch.slice()) |msg| msg.deinit(gpa, gpa); - try std.testing.expectEqual(@as(usize, 1), batch.len); - try std.testing.expectEqualStrings("fresh output", batch.items[0].output.bytes); -} - export fn pardes_animation_tick() bool { pardes.turn.wake(); defer pardes.turn.rest(); @@ -2471,6 +2097,380 @@ fn expectSameAbi(comptime C: type, comptime Z: type) !void { try std.testing.expectEqual(@sizeOf(c_fn.return_type.?), @sizeOf(z_fn.return_type.?)); } +test "mac file watch generations own paths, coalesce, and reject stale callbacks" { + var watches: FileWatches = .{}; + defer watches.deinit(std.testing.allocator); + + var first_path = [_]u8{ '/', 't', 'm', 'p', '/', 'a' }; + const first = try watches.replace( + std.testing.allocator, + 3, + &first_path, + 41, + .{ .text = 11 }, + ); + first_path[first_path.len - 1] = 'x'; + try std.testing.expectEqualStrings("/tmp/a", watches.entries[3].?.path); + try std.testing.expect(watches.notify(3, first)); + try std.testing.expect(!watches.notify(3, first)); + try std.testing.expect(watches.takeDirty(3)); + try std.testing.expect(!watches.takeDirty(3)); + + const second = try watches.replace( + std.testing.allocator, + 3, + "/tmp/b", + 42, + .{ .text = 22 }, + ); + try std.testing.expect(second != first); + try std.testing.expect(!watches.notify(3, first)); + try std.testing.expect(watches.notify(3, second)); + try std.testing.expectEqual(@as(u8, 1), watches.entries[3].?.retries_left); + try std.testing.expect(watches.takeDirty(3)); + try std.testing.expect(watches.retry(3, second)); + try std.testing.expect(!watches.retry(3, second)); + try std.testing.expect(watches.takeDirty(3)); + try std.testing.expect(watches.notify(3, second)); + try std.testing.expectEqual(@as(u8, 1), watches.entries[3].?.retries_left); + try std.testing.expect(watches.takeDirty(3)); + watches.restampText(3, "/tmp/not-b", 99); + try std.testing.expectEqual(@as(u64, 22), watches.entries[3].?.generation_on_disk.text); + watches.restampText(3, "/tmp/b", 33); + try std.testing.expectEqual(@as(u64, 33), watches.entries[3].?.generation_on_disk.text); + + const stopped = watches.stop(std.testing.allocator, 3); + try std.testing.expect(stopped != second); + try std.testing.expect(!watches.notify(3, second)); + try std.testing.expect(!watches.retry(3, second)); + try std.testing.expect(watches.entries[3] == null); + + _ = try watches.replace(std.testing.allocator, 4, "/tmp/book.pdf", 77, .{ .kind = null }); + watches.restampText(4, "/tmp/book.pdf", 123); + try std.testing.expect(watches.entries[4].?.generation_on_disk.kind == null); +} + +test "mac queued results never cancel a newer LSP request" { + const gpa = std.testing.allocator; + const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); + defer core.deinit(); + var st: State = .{ + .gpa = gpa, + .threaded = undefined, + .io = std.testing.io, + .core = core, + .runtime = .{}, + .config_arena = .init(gpa), + .prompt_rcs = .{}, + .inbox = .{ .gpa = gpa, .lsp_allocator = gpa, .io = std.testing.io }, + }; + defer st.config_arena.deinit(); + defer st.inbox.close(); + core.lspRequest(0, .status, ""); + const old_id = core.lsp_ask.wait.?.id; + core.lspRequest(0, .status, ""); + const current_id = core.lsp_ask.wait.?.id; + st.inbox.lsp_id = current_id; + st.inbox.lsp_task = .{ .id = current_id, .future = .{ .any_future = null, .result = {} } }; + // The inbox turns the superseded answer away. + st.inbox.push(.{ .lsp = .{ .id = old_id, .rows = try gpa.dupe(u8, "stale result\n") } }); + try std.testing.expectEqual(@as(usize, 0), st.inbox.completion_len); + _ = drainInbox(&st); + try std.testing.expect(st.inbox.lsp_task != null); + try std.testing.expectEqual(current_id, st.inbox.lsp_task.?.id); + try std.testing.expectEqual(current_id, core.lsp_ask.wait.?.id); + st.inbox.push(.{ .lsp = .{ .id = current_id, .rows = try gpa.dupe(u8, "") } }); + try std.testing.expect(drainInbox(&st)); + try std.testing.expect(st.inbox.lsp_task == null); + try std.testing.expect(core.lsp_ask.wait == null); +} + +test "mac worker setup failures finish matching LSP and pipe requests" { + const gpa = std.testing.allocator; + var failing_vtable = std.testing.io.vtable.*; + failing_vtable.concurrent = std.Io.failingConcurrent; + const failing_io: std.Io = .{ .userdata = std.testing.io.userdata, .vtable = &failing_vtable }; + for (0..2) |failure| { + const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); + defer core.deinit(); + const pane = try core.setTestFile("abc"); + var failing = std.testing.FailingAllocator.init(gpa, .{ + .fail_index = if (failure == 0) 0 else std.math.maxInt(usize), + }); + var st: State = .{ + .gpa = failing.allocator(), + .threaded = undefined, + .io = failing_io, + .core = core, + .runtime = .{}, + .config_arena = .init(gpa), + .prompt_rcs = .{}, + .inbox = .{ .gpa = failing.allocator(), .lsp_allocator = failing.allocator(), .io = failing_io }, + }; + defer st.config_arena.deinit(); + defer st.inbox.close(); + core.lspRequest(core.view().active, .status, ""); + const req: host_io.Lsp.Request = .{ + .id = core.lsp_ask.wait.?.id, + .kind = .status, + .pane = @intCast(core.view().active), + .offset = 0, + .arg = "", + }; + lspRequest(&st, req); + try std.testing.expect(core.lsp_ask.wait == null); + try std.testing.expectEqualStrings("abc", pane.file.?.content); + + pane.body.cur_col = 2; + pane.body.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; + core.update(.{ .key = .{ .cp = '|' } }); + core.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); + core.update(.{ .key = .{ .cp = pardes.Key.enter } }); + const pipe_id = core.pipe.wait.?.id; + pipeRequest(&st, pipe_id); + try std.testing.expect(core.pipe.wait == null); + try std.testing.expectEqualStrings("abc", pane.file.?.content); + try std.testing.expect(st.inbox.lsp_task == null); + try std.testing.expectEqual(@as(usize, 0), st.pipe_tasks.len); + } +} + +test "mac host watcher covers file and directory vnode events, debounce, and generation callback" { + try std.testing.expect(std.mem.indexOf( + u8, + file_watcher_swift, + "deletingLastPathComponent()", + ) != null); + try std.testing.expect(std.mem.indexOf( + u8, + file_watcher_swift, + "self.reopenFileSource()", + ) != null); + try std.testing.expect(std.mem.indexOf( + u8, + file_watcher_swift, + "DispatchQueue.main.asyncAfter", + ) != null); + try std.testing.expect(std.mem.indexOf( + u8, + file_watcher_swift, + "pardes_watch_changed(self.pane, self.generation)", + ) != null); +} + +test "the animation clock spends real time, not callbacks" { + const frame = pardes.animation.frame_ns; + const expectEqual = std.testing.expectEqual; + + const first = spendTickTime(0, 999 * std.time.ns_per_s, 0); + try expectEqual(@as(u32, 1), first.steps); + try expectEqual(frame, first.elapsed_ns); + + const on_time = spendTickTime(1_000, 1_000 + frame, 0); + try expectEqual(@as(u32, 1), on_time.steps); + try expectEqual(@as(u64, 0), on_time.bank_ns); + + const late = spendTickTime(1_000, 1_000 + 2 * frame, 0); + try expectEqual(@as(u32, 2), late.steps); + + var bank: u64 = 0; + var steps: u32 = 0; + for (0..3) |_| { + const partial = spendTickTime(1_000, 1_000 + 6 * std.time.ns_per_ms, bank); + bank = partial.bank_ns; + steps += partial.steps; + } + try expectEqual(@as(u32, 1), steps); + try expectEqual(@as(u64, 2 * std.time.ns_per_ms), bank); + + const stall = spendTickTime(1_000, 1_000 + 10 * std.time.ns_per_s, 0); + try expectEqual(max_tick_catch_up_ns, stall.elapsed_ns); + try expectEqual(@as(u32, @intCast(max_tick_catch_up_ns / frame)), stall.steps); + + try expectEqual(@as(u32, 1), spendTickTime(5_000, 4_000, 0).steps); +} + +test "tagline percent falls back before init and follows live core state" { + try std.testing.expectEqual(pardes.config.gui_tagline_font_percent, taglineFontPercent(null)); + + const core = try pardes.Pardes.init(std.testing.allocator, .{ .tty_only = true }); + defer core.deinit(); + const changed: u8 = if (pardes.config.gui_tagline_font_percent == 37) 38 else 37; + core.settings.font.tagline_percent = changed; + try std.testing.expectEqual(changed, taglineFontPercent(core)); +} + +test "the fallback preference order crosses the ABI intact and ends at the boundary" { + try std.testing.expectEqual(@as(u32, fonts.fallback_names.len), pardes_fallback_font_count()); + try std.testing.expect(pardes_fallback_font_count() > 0); + + for (fonts.fallback_names, 0..) |want, i| { + var len: u32 = 0; + const got = pardes_fallback_font_name(@intCast(i), &len) orelse return error.MissingFallbackName; + try std.testing.expectEqualStrings(want, got[0..len]); + } + + var len: u32 = 12345; + try std.testing.expect(pardes_fallback_font_name(pardes_fallback_font_count(), &len) == null); + try std.testing.expectEqual(@as(u32, 0), len); +} + +test "mac Restore keeps host state and rejects callbacks from the old core" { + const gpa = std.testing.allocator; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); + var st: State = .{ + .gpa = gpa, + .threaded = undefined, + .io = std.testing.io, + .core = core, + .runtime = .{}, + .config_arena = .init(gpa), + .prompt_rcs = .{}, + .inbox = .{ .gpa = gpa, .lsp_allocator = gpa, .io = std.testing.io }, + .frame_len = 7, + .rotate_coasting = true, + }; + defer st.core.deinit(); + defer st.config_arena.deinit(); + defer st.file_watches.deinit(gpa); + defer st.inbox.close(); + const marker = try st.config_arena.allocator().dupe(u8, "host configuration"); + _ = try core.setTestFile("saved body\n"); + const old_serial = core.panes[0].?.serial; + st.gens[0] = 23; + const old_watch = try st.file_watches.replace(gpa, 0, "/test.txt", old_serial, .{ .text = 0 }); + try dump.dumpState(core); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "restore.zon", .data = core.dump_out.? }); + while (core.nextEffect()) |_| {} + + var command: [512]u8 = undefined; + core.update(.{ .command = try std.fmt.bufPrint(&command, "Restore .zig-cache/tmp/{s}/missing.zon", .{tmp.sub_path}) }); + try std.testing.expect(!restoreCore(&st)); + try std.testing.expectEqual(core, st.core); + try std.testing.expectEqual(old_watch, st.file_watches.entries[0].?.generation); + try std.testing.expect(st.rotate_coasting); + + core.lspRequest(0, .status, ""); + const old_request = core.lsp_ask.wait.?.id; + st.inbox.lsp_id = old_request; + st.inbox.lsp_task = .{ .id = old_request, .future = .{ .any_future = null, .result = {} } }; + st.inbox.push(.{ .lsp = .{ .id = old_request, .rows = try gpa.dupe(u8, "old result") } }); + for (0..selection_pipe.Tasks.capacity) |index| { + const id: u32 = @intCast(index); + try std.testing.expect(st.pipe_tasks.add(.{ .id = id, .future = .{ .any_future = null, .result = {} } })); + st.inbox.push(.{ .pipe = .{ + .id = id, + .success = false, + .outputs = &.{}, + .failure = .{ .kind = .exit, .code = 1, .stderr = try gpa.dupe(u8, "old filter failure") }, + } }); + } + st.inbox.acceptReader(0, 23); + _ = st.inbox.pushOutput(0, 23, try gpa.dupe(u8, "old PTY output")); + core.update(.{ .command = try std.fmt.bufPrint(&command, "Restore .zig-cache/tmp/{s}/restore.zon", .{tmp.sub_path}) }); + try std.testing.expect(restoreCore(&st)); + try std.testing.expect(st.core != core); + try std.testing.expect(st.core.panes[0].?.serial > old_serial); + try std.testing.expectEqualStrings("saved body\n", st.core.panes[0].?.file.?.content); + try std.testing.expectEqualStrings("host configuration", marker); + try std.testing.expectEqual(@as(usize, 0), st.frame_len); + try std.testing.expect(!st.rotate_coasting); + try std.testing.expectEqual(@as(u32, 24), st.gens[0]); + try std.testing.expect(!st.file_watches.notify(0, old_watch)); + try std.testing.expect(st.file_watches.generations[0] > old_watch); + try std.testing.expectEqual(@as(usize, 0), st.inbox.len); + try std.testing.expectEqual(@as(usize, 0), st.pipe_tasks.len); + try std.testing.expect(st.inbox.lsp_task == null); + try std.testing.expect(!drainInbox(&st)); + try std.testing.expect(!st.cwd_stale[0]); + + st.core.lspRequest(0, .status, ""); + const new_request = st.core.lsp_ask.wait.?.id; + try std.testing.expect(new_request > old_request); + st.inbox.lsp_id = new_request; + st.inbox.lsp_task = .{ .id = new_request, .future = .{ .any_future = null, .result = {} } }; + st.inbox.push(.{ .lsp = .{ .id = new_request, .rows = &.{} } }); + try std.testing.expect(drainInbox(&st)); + try std.testing.expect(st.inbox.lsp_task == null); + try std.testing.expect(st.core.lsp_ask.wait == null); + + const pane = st.core.panes[0].?; + pane.body.cur_col = 4; + pane.body.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; + st.core.update(.{ .key = .{ .cp = '|' } }); + st.core.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); + st.core.update(.{ .key = .{ .cp = pardes.Key.enter } }); + const pipe_id = st.core.pipe.wait.?.id; + try std.testing.expect(st.pipe_tasks.add(.{ .id = pipe_id, .future = .{ .any_future = null, .result = {} } })); + const output = try gpa.dupe(u8, "FRESH"); + const outputs = gpa.dupe([]u8, &.{output}) catch |err| { + gpa.free(output); + return err; + }; + st.inbox.push(.{ .pipe = .{ .id = pipe_id, .success = true, .outputs = outputs } }); + try std.testing.expect(drainInbox(&st)); + try std.testing.expect(st.core.pipe.wait == null); + try std.testing.expectEqual(@as(usize, 0), st.pipe_tasks.len); + try std.testing.expectEqualStrings("FRESH body\n", pane.file.?.content); +} + +test "mac Restore cancels a PTY reader waiting for output capacity" { + const gpa = std.testing.allocator; + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var st: State = .{ + .gpa = gpa, + .threaded = undefined, + .io = io, + .core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }), + .runtime = .{}, + .config_arena = .init(gpa), + .prompt_rcs = .{}, + .inbox = .{ .gpa = gpa, .lsp_allocator = gpa, .io = io }, + }; + defer st.core.deinit(); + defer st.config_arena.deinit(); + defer st.file_watches.deinit(gpa); + defer st.inbox.close(); + try dump.dumpState(st.core); + try tmp.dir.writeFile(io, .{ .sub_path = "restore.zon", .data = st.core.dump_out.? }); + while (st.core.nextEffect()) |_| {} + st.inbox.acceptReader(1, 1); + for (0..Inbox.output_capacity) |_| _ = st.inbox.pushOutput(1, 1, try gpa.dupe(u8, "retained output")); + const child = try host_io.forkShell(null, 0, &st.prompt_rcs, "/bin/sh", "", 12, 40); + st.gens[0] = 1; + st.ptys[0] = .{ .file = child.file, .pid = child.pid, .gen = 1, .reader = .{ .any_future = null, .result = {} } }; + defer { + if (st.ptys[0]) |pt| if (pt.pid == child.pid) reap(&st, 0); + if (libc.waitpid(child.pid, null, posix.W.NOHANG) == 0) { + _ = libc.kill(child.pid, libc.SIG.KILL); + _ = libc.waitpid(child.pid, null, 0); + } + } + startReader(&st, &st.ptys[0].?, 0); + try std.testing.expect(host_io.writeFd(child.file.handle, "printf 'after-full'; exit\n")); + for (0..1000) |_| { + if (st.inbox.space.state.load(.acquire).waiters != 0) break; + try io.sleep(.fromMilliseconds(1), .awake); + } + try std.testing.expectEqual(@as(u16, 1), st.inbox.space.state.load(.acquire).waiters); + var command: [512]u8 = undefined; + st.core.update(.{ .command = try std.fmt.bufPrint(&command, "Restore .zig-cache/tmp/{s}/restore.zon", .{tmp.sub_path}) }); + try std.testing.expect(restoreCore(&st)); + try std.testing.expect(st.ptys[0] == null); + try std.testing.expectEqual(@as(usize, 0), st.inbox.len); + try std.testing.expectEqual(@as(u16, 0), st.inbox.space.state.load(.acquire).waiters); + st.inbox.acceptReader(0, st.gens[0]); + _ = st.inbox.pushOutput(0, st.gens[0], try gpa.dupe(u8, "fresh output")); + var batch = st.inbox.take(); + defer for (batch.slice()) |msg| msg.deinit(gpa, gpa); + try std.testing.expectEqual(@as(usize, 1), batch.len); + try std.testing.expectEqualStrings("fresh output", batch.items[0].output.bytes); +} + test "pardes.h declares every export the way it is defined" { const c = @import("pardes.h"); try expectSameAbi(@TypeOf(c.pardes_init), @TypeOf(pardes_init)); @@ -2855,4 +2855,3 @@ test "mac tag layer ABI preserves logical capacity and physical grip" { try std.testing.expectEqual(@as(u8, 'G'), canonical[18].text[0]); try std.testing.expectEqual(encodeCell(.{}).len, canonical[10].len); } - |
