|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Exercise existing WaveEditor resize/minimize/MCP/normal-exit behavior.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import ctypes |
| 7 | +from ctypes import wintypes |
| 8 | +import json |
| 9 | +import os |
| 10 | +import queue |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | +import threading |
| 14 | +import time |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | + |
| 18 | +RESPONSE_TIMEOUT_SECONDS = 30.0 |
| 19 | +WINDOW_TIMEOUT_SECONDS = 30.0 |
| 20 | +SW_MINIMIZE = 6 |
| 21 | +SW_RESTORE = 9 |
| 22 | +SWP_NOMOVE = 0x0002 |
| 23 | +SWP_NOZORDER = 0x0004 |
| 24 | +SWP_NOACTIVATE = 0x0010 |
| 25 | + |
| 26 | + |
| 27 | +def fail(message: str) -> None: |
| 28 | + raise RuntimeError(message) |
| 29 | + |
| 30 | + |
| 31 | +def wait_until(predicate, timeout: float, message: str) -> None: |
| 32 | + deadline = time.monotonic() + timeout |
| 33 | + while time.monotonic() < deadline: |
| 34 | + if predicate(): |
| 35 | + return |
| 36 | + time.sleep(0.05) |
| 37 | + fail(message) |
| 38 | + |
| 39 | + |
| 40 | +class RpcSession: |
| 41 | + def __init__(self, process: subprocess.Popen[str]): |
| 42 | + self.process = process |
| 43 | + self.responses: queue.Queue[str | None] = queue.Queue() |
| 44 | + self.stderr_lines: list[str] = [] |
| 45 | + self.next_id = 0 |
| 46 | + threading.Thread(target=self._read_stdout, daemon=True).start() |
| 47 | + threading.Thread(target=self._read_stderr, daemon=True).start() |
| 48 | + |
| 49 | + def _read_stdout(self) -> None: |
| 50 | + assert self.process.stdout is not None |
| 51 | + try: |
| 52 | + for line in self.process.stdout: |
| 53 | + self.responses.put(line) |
| 54 | + finally: |
| 55 | + self.responses.put(None) |
| 56 | + |
| 57 | + def _read_stderr(self) -> None: |
| 58 | + assert self.process.stderr is not None |
| 59 | + for line in self.process.stderr: |
| 60 | + self.stderr_lines.append(line.rstrip()) |
| 61 | + |
| 62 | + def send(self, method: str, params=None): |
| 63 | + assert self.process.stdin is not None |
| 64 | + self.next_id += 1 |
| 65 | + request = {"jsonrpc": "2.0", "id": self.next_id, "method": method} |
| 66 | + if params is not None: |
| 67 | + request["params"] = params |
| 68 | + os.write( |
| 69 | + self.process.stdin.fileno(), |
| 70 | + (json.dumps(request, separators=(",", ":")) + "\n").encode("utf-8"), |
| 71 | + ) |
| 72 | + try: |
| 73 | + line = self.responses.get(timeout=RESPONSE_TIMEOUT_SECONDS) |
| 74 | + except queue.Empty: |
| 75 | + fail(f"timed out waiting for {method}") |
| 76 | + if line is None: |
| 77 | + fail(f"WaveEditor closed stdout while waiting for {method}") |
| 78 | + response = json.loads(line) |
| 79 | + if response.get("id") != self.next_id: |
| 80 | + fail(f"unexpected response id for {method}: {response!r}") |
| 81 | + if "error" in response: |
| 82 | + fail(f"{method} returned {response['error']!r}") |
| 83 | + return response.get("result") |
| 84 | + |
| 85 | + |
| 86 | +def find_process_window(user32, process_id: int) -> int | None: |
| 87 | + result: list[int] = [] |
| 88 | + callback_type = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM) |
| 89 | + |
| 90 | + @callback_type |
| 91 | + def visit_window(window, _parameter): |
| 92 | + owner_process_id = wintypes.DWORD() |
| 93 | + user32.GetWindowThreadProcessId(window, ctypes.byref(owner_process_id)) |
| 94 | + if owner_process_id.value == process_id and user32.IsWindowVisible(window): |
| 95 | + result.append(int(window)) |
| 96 | + return False |
| 97 | + return True |
| 98 | + |
| 99 | + user32.EnumWindows(visit_window, 0) |
| 100 | + return result[0] if result else None |
| 101 | + |
| 102 | + |
| 103 | +def main() -> int: |
| 104 | + if len(sys.argv) != 2: |
| 105 | + raise SystemExit("expected path to WaveEditor.exe") |
| 106 | + if sys.platform != "win32": |
| 107 | + raise SystemExit("WaveEditor window lifecycle contracts require Windows") |
| 108 | + |
| 109 | + repository = Path(__file__).resolve().parents[1] |
| 110 | + executable = Path(sys.argv[1]).resolve() |
| 111 | + if not executable.is_file(): |
| 112 | + raise SystemExit(f"executable not found: {executable}") |
| 113 | + |
| 114 | + process = subprocess.Popen( |
| 115 | + [str(executable)], |
| 116 | + cwd=repository, |
| 117 | + stdin=subprocess.PIPE, |
| 118 | + stdout=subprocess.PIPE, |
| 119 | + stderr=subprocess.PIPE, |
| 120 | + text=True, |
| 121 | + bufsize=1, |
| 122 | + ) |
| 123 | + session = RpcSession(process) |
| 124 | + user32 = ctypes.WinDLL("user32", use_last_error=True) |
| 125 | + user32.IsIconic.argtypes = [wintypes.HWND] |
| 126 | + user32.IsIconic.restype = wintypes.BOOL |
| 127 | + user32.ShowWindow.argtypes = [wintypes.HWND, ctypes.c_int] |
| 128 | + user32.ShowWindow.restype = wintypes.BOOL |
| 129 | + user32.SetWindowPos.argtypes = [ |
| 130 | + wintypes.HWND, |
| 131 | + wintypes.HWND, |
| 132 | + ctypes.c_int, |
| 133 | + ctypes.c_int, |
| 134 | + ctypes.c_int, |
| 135 | + ctypes.c_int, |
| 136 | + wintypes.UINT, |
| 137 | + ] |
| 138 | + user32.SetWindowPos.restype = wintypes.BOOL |
| 139 | + |
| 140 | + normal_exit_requested = False |
| 141 | + try: |
| 142 | + methods = session.send("rpc.discover") |
| 143 | + if "editor.app.request_exit" not in methods: |
| 144 | + fail("rpc.discover omitted editor.app.request_exit") |
| 145 | + |
| 146 | + window_holder: list[int | None] = [None] |
| 147 | + |
| 148 | + def capture_window() -> bool: |
| 149 | + window_holder[0] = find_process_window(user32, process.pid) |
| 150 | + return window_holder[0] is not None |
| 151 | + |
| 152 | + wait_until( |
| 153 | + capture_window, |
| 154 | + WINDOW_TIMEOUT_SECONDS, |
| 155 | + "timed out waiting for the WaveEditor window", |
| 156 | + ) |
| 157 | + window = wintypes.HWND(window_holder[0]) |
| 158 | + |
| 159 | + if not user32.SetWindowPos( |
| 160 | + window, |
| 161 | + None, |
| 162 | + 0, |
| 163 | + 0, |
| 164 | + 1200, |
| 165 | + 720, |
| 166 | + SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE, |
| 167 | + ): |
| 168 | + fail(f"SetWindowPos failed with Win32 error {ctypes.get_last_error()}") |
| 169 | + time.sleep(0.25) |
| 170 | + frame_stats = session.send("editor.profile.frame_stats") |
| 171 | + if not isinstance(frame_stats, dict): |
| 172 | + fail("editor.profile.frame_stats did not return an object after resize") |
| 173 | + |
| 174 | + user32.ShowWindow(window, SW_MINIMIZE) |
| 175 | + wait_until( |
| 176 | + lambda: bool(user32.IsIconic(window)), |
| 177 | + WINDOW_TIMEOUT_SECONDS, |
| 178 | + "WaveEditor did not enter minimized state", |
| 179 | + ) |
| 180 | + hierarchy = session.send("editor.hierarchy.list") |
| 181 | + if not isinstance(hierarchy, list): |
| 182 | + fail("editor.hierarchy.list did not return an array while minimized") |
| 183 | + if hierarchy: |
| 184 | + session.send("editor.hierarchy.select", [hierarchy[0]["id"]]) |
| 185 | + |
| 186 | + user32.ShowWindow(window, SW_RESTORE) |
| 187 | + wait_until( |
| 188 | + lambda: not bool(user32.IsIconic(window)), |
| 189 | + WINDOW_TIMEOUT_SECONDS, |
| 190 | + "WaveEditor did not restore from minimized state", |
| 191 | + ) |
| 192 | + session.send("editor.profile.frame_stats") |
| 193 | + |
| 194 | + user32.ShowWindow(window, SW_MINIMIZE) |
| 195 | + wait_until( |
| 196 | + lambda: bool(user32.IsIconic(window)), |
| 197 | + WINDOW_TIMEOUT_SECONDS, |
| 198 | + "WaveEditor did not enter the second minimized state", |
| 199 | + ) |
| 200 | + session.send("editor.app.request_exit") |
| 201 | + normal_exit_requested = True |
| 202 | + process.wait(timeout=RESPONSE_TIMEOUT_SECONDS) |
| 203 | + if process.returncode != 0: |
| 204 | + fail(f"WaveEditor returned {process.returncode} after normal request_exit") |
| 205 | + finally: |
| 206 | + if process.poll() is None: |
| 207 | + if not normal_exit_requested: |
| 208 | + try: |
| 209 | + session.send("editor.app.request_exit") |
| 210 | + except (BrokenPipeError, OSError, RuntimeError): |
| 211 | + pass |
| 212 | + try: |
| 213 | + process.wait(timeout=5.0) |
| 214 | + except subprocess.TimeoutExpired: |
| 215 | + process.terminate() |
| 216 | + try: |
| 217 | + process.wait(timeout=5.0) |
| 218 | + except subprocess.TimeoutExpired: |
| 219 | + process.kill() |
| 220 | + process.wait(timeout=5.0) |
| 221 | + |
| 222 | + print( |
| 223 | + "WaveEditor window lifecycle contracts passed: resize, minimize, " |
| 224 | + "main-thread MCP drain, restore, and minimized request_exit" |
| 225 | + ) |
| 226 | + return 0 |
| 227 | + |
| 228 | + |
| 229 | +if __name__ == "__main__": |
| 230 | + try: |
| 231 | + raise SystemExit(main()) |
| 232 | + except (OSError, RuntimeError, subprocess.SubprocessError) as error: |
| 233 | + print(f"WaveEditor window lifecycle contracts failed: {error}", file=sys.stderr) |
| 234 | + raise SystemExit(1) |
0 commit comments