const std = @import("std"); const Session = @import("session").Session; fn has(hay: []const u8, needle: []const u8) bool { return std.mem.indexOf(u8, hay, needle) != null; } /// The root ctl's line for `word`. fn setting(s: *Session, word: []const u8) ![]const u8 { var lines = std.mem.splitScalar(u8, try s.read("/ctl"), '\n'); while (lines.next()) |line| if (std.mem.startsWith(u8, line, word) and line.len > word.len and line[word.len] == ' ') return line; return error.NoSuchSetting; } test "settings: Pager is pardes by default, off taken, anything else refused with its choices" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); try std.testing.expectEqualStrings("Pager pardes", try setting(&s, "Pager")); try s.write("/ctl", "Pager off\n"); try std.testing.expectEqualStrings("Pager off", try setting(&s, "Pager")); try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Pager less\n")); try std.testing.expectEqualStrings("bad value in control message; takes pardes, off \"less\"", s.said); try std.testing.expectEqualStrings("Pager off", try setting(&s, "Pager")); } test "settings: Shell refuses a path that is no executable, and bare it goes back to the default" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const default = try setting(&s, "Shell"); try std.testing.expect(try s.refused("/ctl", "Shell /nonexistent/zzsh\n") != 0); try std.testing.expect(has(s.said, "Shell: shell \"/nonexistent/zzsh\" not found")); try std.testing.expect(has(try s.read("/log"), "shell \"/nonexistent/zzsh\" not found")); // Its words say a directory, which a mount reads as EISDIR too. try std.testing.expectEqual(@as(u16, 21), try s.refused("/ctl", "Shell /etc\n")); try std.testing.expect(has(try s.read("/log"), "Shell: not a shell: /etc is a directory")); try std.testing.expectEqualStrings(default, try setting(&s, "Shell")); try s.write("/ctl", "Shell /bin/sh\n"); try std.testing.expectEqualStrings("Shell /bin/sh", try setting(&s, "Shell")); try s.write("/ctl", "Shell\n"); try std.testing.expect(has(try s.read("/log"), "Shell: the default again, ")); try std.testing.expectEqualStrings(default, try setting(&s, "Shell")); } test "settings: Pet is the window's: a terminal's view refuses it, from a pane and the root; a window takes only its pets" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); // Which build, from its options, not from the answer: a terminal's view // taking Pet must fail here, not pass as a window. if (comptime @import("pardes_config").platform == .gui) { try s.write("/ctl", "Pet cat\n"); // A window's: dog is no pet of its, and off is. try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet cat\n") != null); _ = try s.refused("/ctl", "Pet dog\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet cat\n") != null); try s.write("/ctl", "Pet frog\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet frog\n") != null); try s.write("/ctl", "Pet off\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet off\n") != null); return; } try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Pet cat\n")); try std.testing.expect(std.mem.startsWith(u8, s.said, "Pet is GUI-only, invalid here")); try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), "Pet cat\n")); try std.testing.expectEqualStrings("Pet is GUI-only, invalid here", s.said); try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "Pet ") == null); } fn expectHas(text: []const u8, part: []const u8) !void { if (std.mem.indexOf(u8, text, part) == null) { std.debug.print("not in it: {s}\n", .{part}); return error.TestExpectedText; } } test "settings: DumpConfig reports the settings in its one +DumpConfig pane, a second refreshing it" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const text = try s.focused(); const exec = s.at("/pane/{d}/exec", .{text}); try s.write(exec, "DumpConfig\n"); const out = try s.focused(); try std.testing.expect(out != text); try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 0 /+DumpConfig ", .{out})) != null); const report = try s.read(s.at("/pane/{d}/body", .{out})); for ([_][]const u8{ "Theme orchard\n", "Last shell spawned: (none)\n", "Shell change pending: on\n", "\nShell ", "LocationsConfig ", "no per-user config path" }) |line| try expectHas(report, line); try s.write("/ctl", "Theme acme\n"); try s.write("/focus", s.at("{d}", .{text})); try s.write(exec, "DumpConfig\n"); try std.testing.expectEqual(out, try s.focused()); try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, try s.read("/index"), "/+DumpConfig ")); try expectHas(try s.read(s.at("/pane/{d}/body", .{out})), "Theme acme\n"); } test "settings: Config without a configuration directory says so and opens nothing" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const index = try s.read("/index"); try std.testing.expectEqual(@as(u16, 5), try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), "Config\n")); try s.expect("/index", index); try expectHas(try s.read("/log"), "Config: no startup file here: no configuration directory"); }