import { setupTest, startEngine, startClientInTmux, openCmdFifo, writeFifoCommand, wait, tmuxSendKeys, tmuxCapturePane, isProcessAlive, execSync, teardownTest } from './test_utils'; export async function testStressRandomUsage(): Promise { console.log("\nTest: STRESS RANDOM USAGE (10,000 keys, stability check)"); setupTest(); const engine = await startEngine(); await startClientInTmux(); openCmdFifo(); await wait(500); // Pre‑add channels for more variety for (let i = 0; i < 7; i++) { writeFifoCommand("add"); await wait(100); } const KEY_ACTIONS = ['h','j','k','l','t','d','s','S','a','A','r','b','u']; const TOTAL = 5000; const KEY_DELAY_MS = 20; const CHECK_INTERVAL = 500; console.log(` Starting stress loop: ${TOTAL} keys at ~20 keys/second...`); let keysSent = 0; const startTime = Date.now(); for (let i = 0; i < TOTAL; i++) { const key = KEY_ACTIONS[Math.floor(Math.random() * KEY_ACTIONS.length)]; tmuxSendKeys("looper", "0", key); await wait(KEY_DELAY_MS); keysSent++; if (keysSent % CHECK_INTERVAL === 0) { // Wait a little for TUI to settle await wait(500); // Check engine alive if (engine.pid && !isProcessAlive(engine.pid)) { console.log(` FAIL: Engine died at key ${keysSent}`); try { const stderr = execSync("tail -20 /tmp/engine_stderr.log", { encoding: "utf-8" }).trim(); console.log(" Engine stderr:", stderr); } catch {} teardownTest(); throw new Error("Engine crash during stress test"); } // Wait a little more for TUI to settle and pane to be captured fully await wait(1000); // Retry pane capture up to 5 times with small delays if it doesn't contain "Selected:" let pane = ""; for (let retry = 0; retry < 5; retry++) { pane = tmuxCapturePane("looper", "0"); if (pane && pane.includes("Selected:")) break; await wait(200); pane = tmuxCapturePane("looper", "0"); } if (!pane || !pane.includes("Selected:")) { console.log(` FAIL: TUI pane appears corrupted at key ${keysSent}`); console.log(" Pane:\n" + (pane ? pane.slice(0, 1000) : "(empty)")); teardownTest(); throw new Error("TUI corruption during stress test"); } } } const elapsed = ((Date.now() - startTime) / 1000).toFixed(1); console.log(` Stress loop finished in ${elapsed}s`); await wait(500); if (engine.pid && !isProcessAlive(engine.pid)) { console.log(" FAIL: Engine died after test"); teardownTest(); throw new Error("Engine crash"); } console.log(" PASS: Stress test completed (no crash or corruption)"); engine.kill(); teardownTest(); }