|
| 1 | +import std; |
| 2 | +import huxerui; |
| 3 | +import huxerui.terminal; |
| 4 | + |
| 5 | +namespace ht = huxerui::terminal; |
| 6 | + |
| 7 | +namespace { |
| 8 | + |
| 9 | +void Require(bool condition, std::string_view message) { |
| 10 | + if (!condition) { |
| 11 | + throw std::runtime_error(std::string(message)); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +struct AsyncState { |
| 16 | + std::mutex mutex; |
| 17 | + std::optional<ht::TerminalSession> view_session; |
| 18 | + bool finished = false; |
| 19 | + std::string error; |
| 20 | + |
| 21 | + void Finish(std::string msg = {}) { |
| 22 | + std::lock_guard lock(mutex); |
| 23 | + finished = true; |
| 24 | + error = std::move(msg); |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +AsyncState* g_state = nullptr; |
| 29 | + |
| 30 | +class MemoryBackend final : public ht::TerminalBackend { |
| 31 | +public: |
| 32 | + explicit MemoryBackend(std::vector<std::string> chunks) : chunks_(std::move(chunks)) {} |
| 33 | + |
| 34 | + huxerui::Task<ht::TerminalResult<ht::TerminalRead>> ReadAsync(std::size_t) override { |
| 35 | + if (index_ >= chunks_.size()) { |
| 36 | + co_return ht::TerminalResult<ht::TerminalRead>::Success( |
| 37 | + ht::TerminalExit{ht::TerminalExitReason::Exited, 7}); |
| 38 | + } |
| 39 | + const std::string& text = chunks_[index_++]; |
| 40 | + huxerui::Bytes bytes(text.size()); |
| 41 | + std::memcpy(bytes.data(), text.data(), text.size()); |
| 42 | + co_return ht::TerminalResult<ht::TerminalRead>::Success(std::move(bytes)); |
| 43 | + } |
| 44 | + |
| 45 | + huxerui::Task<ht::TerminalResult<void>> WriteAsync(huxerui::Bytes) override { |
| 46 | + co_return ht::TerminalResult<void>::Success(); |
| 47 | + } |
| 48 | + |
| 49 | + huxerui::Task<ht::TerminalResult<void>> ResizeAsync(ht::TerminalSize size) override { |
| 50 | + seen_sizes.push_back(size); |
| 51 | + co_return ht::TerminalResult<void>::Success(); |
| 52 | + } |
| 53 | + |
| 54 | + huxerui::Task<ht::TerminalResult<void>> CloseAsync() override { |
| 55 | + closed = true; |
| 56 | + co_return ht::TerminalResult<void>::Success(); |
| 57 | + } |
| 58 | + |
| 59 | + std::vector<ht::TerminalSize> seen_sizes; |
| 60 | + bool closed = false; |
| 61 | + |
| 62 | +private: |
| 63 | + std::vector<std::string> chunks_; |
| 64 | + std::size_t index_ = 0; |
| 65 | +}; |
| 66 | + |
| 67 | +huxerui::Task<void> RunChecks() { |
| 68 | + try { |
| 69 | + // Negative check: invalid command name rejected cleanly. |
| 70 | + auto bad_cmd = ht::CreateCommandSession({ |
| 71 | + .prompt = "$ ", |
| 72 | + .commands = {{.name = ""}}, |
| 73 | + }); |
| 74 | + Require(!bad_cmd.Succeeded(), "empty command name must fail"); |
| 75 | + Require(bad_cmd.Error().code == ht::TerminalErrorCode::InvalidConfiguration, |
| 76 | + "expected InvalidConfiguration for empty command name"); |
| 77 | + std::cout << "PASS negative: empty command name rejected\n"; |
| 78 | + |
| 79 | + // Positive check: command session creation & close. |
| 80 | + auto cmd_res = ht::CreateCommandSession({ |
| 81 | + .prompt = "$ ", |
| 82 | + .commands = {{ |
| 83 | + .name = "ping", |
| 84 | + .handler = [](ht::TerminalCommandContext ctx) -> huxerui::Task<int> { |
| 85 | + (void)co_await ctx.output.WriteAsync("pong\n"); |
| 86 | + co_return 0; |
| 87 | + }, |
| 88 | + }}, |
| 89 | + }); |
| 90 | + Require(cmd_res.Succeeded(), "CreateCommandSession failed"); |
| 91 | + auto cmd_session = std::move(cmd_res).Value(); |
| 92 | + Require(cmd_session.Snapshot().status == ht::TerminalStatus::Open, "command session expected Open"); |
| 93 | + { |
| 94 | + std::lock_guard lock(g_state->mutex); |
| 95 | + g_state->view_session = cmd_session; |
| 96 | + } |
| 97 | + std::cout << "PASS positive: CreateCommandSession Open\n"; |
| 98 | + |
| 99 | + // Positive check: transport session processing ANSI + UTF-8 multibyte text through libvterm. |
| 100 | + auto transport_res = co_await ht::OpenTransportAsync(std::make_unique<MemoryBackend>(std::vector<std::string>{ |
| 101 | + "\x1b[32mhello-\xE4\xB8\xAD\xE6\x96\x87\x1b[0m\r\n", |
| 102 | + "line-2\r\n", |
| 103 | + })); |
| 104 | + Require(transport_res.Succeeded(), "OpenTransportAsync failed"); |
| 105 | + auto transport = std::move(transport_res).Value(); |
| 106 | + const auto wait_end = std::chrono::steady_clock::now() + std::chrono::seconds{5}; |
| 107 | + while ((transport.Snapshot().status == ht::TerminalStatus::Open || |
| 108 | + transport.Snapshot().status == ht::TerminalStatus::Closing) && |
| 109 | + std::chrono::steady_clock::now() < wait_end) { |
| 110 | + co_await huxerui::Delay(std::chrono::milliseconds{10}); |
| 111 | + } |
| 112 | + const auto tsnap = transport.Snapshot(); |
| 113 | + Require(tsnap.status == ht::TerminalStatus::Exited, |
| 114 | + "transport expected Exited, got status=" + std::to_string(static_cast<int>(tsnap.status))); |
| 115 | + Require(tsnap.exit.has_value() && tsnap.exit->code == 7, "transport exit code expected 7"); |
| 116 | + (void)co_await transport.CloseAsync(); |
| 117 | + std::cout << "PASS positive: OpenTransportAsync Exited with code 7\n"; |
| 118 | + |
| 119 | +#if defined(__linux__) && !defined(__ANDROID__) |
| 120 | + // Positive check: real POSIX PTY shell (-lutil openpty + waitpid exit code propagation). |
| 121 | + auto shell_res = co_await ht::OpenShellAsync({ |
| 122 | + .executable = "/bin/sh", |
| 123 | + .arguments = {"-c", "printf 'PTY_OK\\n'; exit 42"}, |
| 124 | + }); |
| 125 | + Require(shell_res.Succeeded(), "OpenShellAsync(/bin/sh) failed"); |
| 126 | + auto shell = std::move(shell_res).Value(); |
| 127 | + for (int i = 0; i < 200 && |
| 128 | + (shell.Snapshot().status == ht::TerminalStatus::Open || |
| 129 | + shell.Snapshot().status == ht::TerminalStatus::Closing); |
| 130 | + ++i) { |
| 131 | + co_await huxerui::Delay(std::chrono::milliseconds{10}); |
| 132 | + } |
| 133 | + const auto ssnap = shell.Snapshot(); |
| 134 | + Require(ssnap.status == ht::TerminalStatus::Exited, "shell expected Exited"); |
| 135 | + Require(ssnap.exit.has_value() && ssnap.exit->code == 42, "shell exit code expected 42"); |
| 136 | + (void)co_await shell.CloseAsync(); |
| 137 | + std::cout << "PASS positive: OpenShellAsync PTY Exited with code 42\n"; |
| 138 | +#endif |
| 139 | + |
| 140 | + auto closed = co_await cmd_session.CloseAsync(); |
| 141 | + Require(closed.Succeeded(), "cmd_session CloseAsync failed"); |
| 142 | + Require(cmd_session.Snapshot().status == ht::TerminalStatus::Closed, "expected Closed status"); |
| 143 | + |
| 144 | + g_state->Finish(); |
| 145 | + } catch (const std::exception& ex) { |
| 146 | + g_state->Finish(ex.what()); |
| 147 | + } catch (...) { |
| 148 | + g_state->Finish("unknown exception in RunChecks"); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +huxerui::View SmokeRoot() { |
| 153 | + const huxerui::TaskScope tasks = huxerui::UseTaskScope(); |
| 154 | + huxerui::Lifecycle([tasks] { |
| 155 | + tasks.Launch([]() -> huxerui::Task<void> { co_await RunChecks(); }); |
| 156 | + }); |
| 157 | + std::optional<ht::TerminalSession> session; |
| 158 | + { |
| 159 | + std::lock_guard lock(g_state->mutex); |
| 160 | + session = g_state->view_session; |
| 161 | + } |
| 162 | + if (session) { |
| 163 | + return ht::Terminal(*session).With(huxerui::Grow()); |
| 164 | + } |
| 165 | + return huxerui::Spacer(); |
| 166 | +} |
| 167 | + |
| 168 | +class HeadlessPlatform final : public huxerui::UiWindow, public huxerui::Runtime { |
| 169 | +public: |
| 170 | + HeadlessPlatform() |
| 171 | + : huxerui::Runtime([this](std::function<void()> cb) { |
| 172 | + { |
| 173 | + std::lock_guard lock(mutex_); |
| 174 | + callbacks_.push_back(std::move(cb)); |
| 175 | + } |
| 176 | + cv_.notify_all(); |
| 177 | + }) {} |
| 178 | + |
| 179 | + ~HeadlessPlatform() override { |
| 180 | + UiWindow::Retire(); |
| 181 | + huxerui::Runtime::Retire(); |
| 182 | + } |
| 183 | + |
| 184 | + void Start(const huxerui::Application& app) { |
| 185 | + InitializeApplication(app); |
| 186 | + InitializeWindow(*this, app.root_factory, app.options.window); |
| 187 | + } |
| 188 | + |
| 189 | + void OnRuntimeStopped() override {} |
| 190 | + |
| 191 | + void RequestFrameAt(double) override { |
| 192 | + { |
| 193 | + std::lock_guard lock(mutex_); |
| 194 | + frame_requested_ = true; |
| 195 | + } |
| 196 | + cv_.notify_all(); |
| 197 | + } |
| 198 | + |
| 199 | + double Now() const noexcept override { |
| 200 | + return std::chrono::duration<double>( |
| 201 | + std::chrono::steady_clock::now().time_since_epoch()) |
| 202 | + .count(); |
| 203 | + } |
| 204 | + |
| 205 | + huxerui::FontMetrics Metrics(const huxerui::Font&) override { return {}; } |
| 206 | + huxerui::TextRunMetrics MeasureRun( |
| 207 | + std::string_view, const huxerui::TextStyle&, const huxerui::TextShapingOptions&) override { |
| 208 | + return {}; |
| 209 | + } |
| 210 | + huxerui::TextLayoutMetrics MeasureText( |
| 211 | + const huxerui::AttributedText&, const huxerui::TextStyle&, float, |
| 212 | + const huxerui::TextLayoutOptions&) override { |
| 213 | + return {}; |
| 214 | + } |
| 215 | + |
| 216 | + bool RunNext(huxerui::UiWindow& window, std::chrono::steady_clock::time_point deadline) { |
| 217 | + std::function<void()> cb; |
| 218 | + bool build_frame = false; |
| 219 | + { |
| 220 | + std::unique_lock lock(mutex_); |
| 221 | + cv_.wait_until(lock, deadline, [this] { |
| 222 | + return !callbacks_.empty() || frame_requested_; |
| 223 | + }); |
| 224 | + if (!callbacks_.empty()) { |
| 225 | + cb = std::move(callbacks_.front()); |
| 226 | + callbacks_.pop_front(); |
| 227 | + } else if (frame_requested_) { |
| 228 | + frame_requested_ = false; |
| 229 | + build_frame = true; |
| 230 | + } else { |
| 231 | + return false; |
| 232 | + } |
| 233 | + } |
| 234 | + if (cb) { |
| 235 | + cb(); |
| 236 | + } else if (build_frame) { |
| 237 | + window.BuildFrame(); |
| 238 | + } |
| 239 | + return true; |
| 240 | + } |
| 241 | + |
| 242 | +private: |
| 243 | + std::mutex mutex_; |
| 244 | + std::condition_variable cv_; |
| 245 | + std::deque<std::function<void()>> callbacks_; |
| 246 | + bool frame_requested_ = false; |
| 247 | +}; |
| 248 | + |
| 249 | +} // namespace |
| 250 | + |
| 251 | +int main() { |
| 252 | + try { |
| 253 | + AsyncState state; |
| 254 | + g_state = &state; |
| 255 | + { |
| 256 | + const huxerui::Application app{ |
| 257 | + SmokeRoot, |
| 258 | + {.application_hooks = {ht::Install}}, |
| 259 | + }; |
| 260 | + HeadlessPlatform platform; |
| 261 | + platform.Start(app); |
| 262 | + huxerui::UiWindow& window = platform; |
| 263 | + window.SetWindowMetrics({.viewport = {480.0F, 240.0F}}); |
| 264 | + window.BuildFrame(); |
| 265 | + |
| 266 | + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds{15}; |
| 267 | + while (true) { |
| 268 | + { |
| 269 | + std::lock_guard lock(state.mutex); |
| 270 | + if (state.finished) { |
| 271 | + break; |
| 272 | + } |
| 273 | + } |
| 274 | + if (!platform.RunNext(window, deadline)) { |
| 275 | + state.Finish("timed out waiting for terminal checks"); |
| 276 | + break; |
| 277 | + } |
| 278 | + } |
| 279 | + } |
| 280 | + std::lock_guard lock(state.mutex); |
| 281 | + Require(state.finished && state.error.empty(), |
| 282 | + state.error.empty() ? "terminal checks did not complete" : state.error); |
| 283 | + std::cout << "PASS: all terminal-consumer checks succeeded\n"; |
| 284 | + return 0; |
| 285 | + } catch (const std::exception& ex) { |
| 286 | + std::cerr << "FAIL: " << ex.what() << '\n'; |
| 287 | + return 1; |
| 288 | + } |
| 289 | +} |
0 commit comments