import { setupTest, startEngine, startClientInTmux, openCmdFifo, writeFifoCommand, wait, tmuxSendKeys, waitForPaneText, teardownTest } from './test_utils'; export async function testKeyPressLatency(): Promise { console.log("\nTest: KEY PRESS LATENCY (50 toggles, check for exponential slowdown)"); setupTest(); const engine = await startEngine(); await startClientInTmux(); openCmdFifo(); await wait(500); const ITERATIONS = 50; const LATENCY_WARN = 500; // warn if >500ms const LATENCY_FAIL = 5000; // fail if >5s let latencies: number[] = []; let prevState = "IDLE"; for (let i = 0; i < ITERATIONS; i++) { // Determine which state we expect after toggle const expectNext = (prevState === "IDLE") ? "R" : "L"; const startTime = Date.now(); tmuxSendKeys("looper", "0", "t"); const pane = await waitForPaneText(expectNext, 10000); const elapsed = Date.now() - startTime; latencies.push(elapsed); // Log periodic summary if (i % 10 === 9) { const avg = latencies.slice(i-9, i+1).reduce((a,b)=>a+b,0) / 10; console.log(` Iteration ${i+1}: avg last 10 = ${avg.toFixed(0)} ms, last = ${elapsed} ms`); } if (elapsed > LATENCY_FAIL) { console.log(` FAIL: Iteration ${i+1} latency ${elapsed} ms exceeds ${LATENCY_FAIL} ms`); engine.kill(); teardownTest(); throw new Error(`Latency exceeded fail threshold at iteration ${i+1}`); } if (elapsed > LATENCY_WARN) { console.log(` WARN: Iteration ${i+1} latency ${elapsed} ms > ${LATENCY_WARN} ms (possible slowdown)`); } // Toggle state for next expectation prevState = (prevState === "IDLE") ? "LOOPING" : "IDLE"; await wait(200); // brief cooldown } // Check for trend: if last 10 avg > 3x first 10 avg → exponential const first10Avg = latencies.slice(0,10).reduce((a,b)=>a+b,0) / 10; const last10Avg = latencies.slice(-10).reduce((a,b)=>a+b,0) / 10; console.log(` First 10 avg: ${first10Avg.toFixed(0)} ms, Last 10 avg: ${last10Avg.toFixed(0)} ms`); if (last10Avg > 3 * first10Avg && last10Avg > 500) { console.log(` FAIL: Latency grew from ${first10Avg.toFixed(0)} ms to ${last10Avg.toFixed(0)} ms (exponential pattern)`); engine.kill(); teardownTest(); throw new Error("Exponential latency increase"); } console.log(" PASS: No exponential latency growth"); engine.kill(); teardownTest(); }