Debian Codegraph @

hyprtester/src/tests/main/swallow.cpp

raw
1 #include <thread>
2 #include "tests.hpp"
3 #include "../../shared.hpp"
4 #include "../../hyprctlCompat.hpp"
5 #include "../shared.hpp"
6
7 using namespace Hyprutils::OS;
8 using namespace Hyprutils::Memory;
9
10 static void awaitKittyPrompt(const std::string& name) {
11 // wait until we see the shell prompt, meaning it's ready for test inputs
12 for (int i = 0; i < 10; i++) {
13 std::string output = Tests::execAndGet(std::format("kitten @ --to unix:/tmp/kitty_{}.sock get-text --extent all", name));
14 if (output.rfind('$') == std::string::npos) {
15 std::this_thread::sleep_for(std::chrono::milliseconds(200));
16 continue;
17 }
18 return;
19 }
20 NLog::log("{}Error: timed out waiting for kitty prompt", Colors::RED);
21 }
22
23 static CUniquePointer<CProcess> spawnRemoteControlKitty(const std::string& name) {
24 auto kittyProc =
25 Tests::spawnKitty("kitty_swallowee", {"-o", "allow_remote_control=yes", "--listen-on", std::format("unix:/tmp/kitty_{}.sock", name), "--config", "NONE", "/bin/sh"});
26 // wait a bit to ensure shell prompt is sent, we are going to read the text after it
27 std::this_thread::sleep_for(std::chrono::milliseconds(100));
28 if (kittyProc)
29 awaitKittyPrompt(name);
30 return kittyProc;
31 }
32
33 static bool spawnSwallower(const std::string& parent, const std::string& name) {
34 auto cmd = std::format("kitten @ --to unix:/tmp/kitty_{}.sock launch --type=background "
35 "kitty -o allow_remote_control=yes --class kitty_{} --listen-on unix:/tmp/kitty_{}.sock --config NONE /bin/sh",
36 parent, name, name);
37 auto result = Tests::execAndGet(cmd);
38 if (result == "error")
39 return false;
40
41 // wait a bit to ensure shell prompt is sent, we are going to read the text after it
42 std::this_thread::sleep_for(std::chrono::milliseconds(100));
43 awaitKittyPrompt(name);
44 return true;
45 }
46
47 static bool spawnKittyOsWindow(const std::string& parent) {
48 auto cmd = std::format("kitten @ --to unix:/tmp/kitty_{}.sock launch --type=os-window", parent);
49 auto result = Tests::execAndGet(cmd);
50
51 if (result == "error")
52 return false;
53
54 std::this_thread::sleep_for(std::chrono::milliseconds(100));
55 return true;
56 }
57
58 static int swallowingCount() {
59 int notSwallowing = Tests::countOccurrences(getFromSocket("/clients"), "swallowing: 0\n");
60 return Tests::windowCount() - notSwallowing;
61 }
62
63 static std::string getActiveWindowID() {
64 std::string activeWindow = getFromSocket("/activewindow");
65 std::string::size_type start = 7; // Length of "Window "
66 std::string::size_type end = activeWindow.find(" ->");
67 if (end == std::string::npos) {
68 return "error";
69 }
70 return activeWindow.substr(start, end - start);
71 }
72
73 TEST_CASE(swallow) {
74 NLog::log("{}Testing window swallowing", Colors::GREEN);
75
76 // test on workspace "swallow"
77 NLog::log("{}Switching to workspace `swallow`", Colors::YELLOW);
78 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = 'name:swallow' })"));
79
80 ASSERT(Tests::windowCount(), 0);
81
82 OK(getFromSocket("/eval hl.config({ misc = { enable_swallow = true } })"));
83 OK(getFromSocket("/eval hl.config({ misc = { swallow_regex = '^(kitty_swallowee)$' } })"));
84
85 // Initial kitty window that will be swallowed
86 spawnRemoteControlKitty("swallowee");
87
88 ASSERT(Tests::windowCount(), 1);
89 ASSERT(swallowingCount(), 0);
90
91 // Get the window ID
92 std::string swalloweeID = getActiveWindowID();
93 if (swalloweeID == "error") {
94 FAIL_TEST("Could not get window ID");
95 }
96 NLog::log("{}Got swalloweeID: {}", Colors::YELLOW, swalloweeID);
97
98 // Spawn a child process that should swallow the initial kitty window
99 ASSERT(spawnSwallower("swallowee", "swallower"), true);
100
101 {
102 // Verify that the initial window is swallowed
103 std::string clients = getFromSocket("/clients");
104 ASSERT_COUNT_STRING(clients, "swallowing: 0\n", 1);
105 ASSERT_COUNT_STRING(clients, std::format("swallowing: {}\n", swalloweeID), 1);
106
107 std::string workspaces = getFromSocket("/workspaces");
108 ASSERT_CONTAINS(workspaces, "windows: 1\n");
109 }
110
111 // Un-swallow the initial window
112 OK(getFromSocket("/dispatch hl.dsp.window.toggle_swallow()"));
113
114 {
115 // Verify that the initial window is un-swallowed
116 std::string clients = getFromSocket("/clients");
117 ASSERT_COUNT_STRING(clients, "swallowing: 0\n", 1);
118 ASSERT_COUNT_STRING(clients, std::format("swallowing: {}\n", swalloweeID), 1);
119
120 std::string workspaces = getFromSocket("/workspaces");
121 ASSERT_CONTAINS(workspaces, "windows: 2\n");
122 }
123
124 // Open new window of the swallower kitty
125 ASSERT(spawnKittyOsWindow("swallower"), true);
126
127 {
128 // Verify that the initial has NOT been re-swallowed
129 std::string clients = getFromSocket("/clients");
130 ASSERT_COUNT_STRING(clients, "swallowing: 0\n", 2);
131 ASSERT_COUNT_STRING(clients, std::format("swallowing: {}\n", swalloweeID), 1);
132
133 std::string workspaces = getFromSocket("/workspaces");
134 ASSERT_CONTAINS(workspaces, "windows: 3\n");
135 }
136
137 // Re-swallow the initial window
138 OK(getFromSocket("/dispatch hl.dsp.focus({ last = true })"));
139 OK(getFromSocket("/dispatch hl.dsp.window.toggle_swallow()"));
140
141 {
142 // Verify that the initial has been re-swallowed
143 std::string clients = getFromSocket("/clients");
144 ASSERT_COUNT_STRING(clients, "swallowing: 0\n", 2);
145 ASSERT_COUNT_STRING(clients, std::format("swallowing: {}\n", swalloweeID), 1);
146
147 std::string workspaces = getFromSocket("/workspaces");
148 ASSERT_CONTAINS(workspaces, "windows: 2\n");
149 }
150 }