/** * Windows Optimizations Tests * Tests for Windows-specific performance optimizations */ const { WindowsRenderingOptimizations, WindowsKeyboardOptimizations, WindowsFileSystemOptimizations, WindowsPerformanceMonitor, } = require("../../../src/tui/utils/windowsOptimizations.js"); const { WindowsKeyboardHandler, createWindowsKeyboardHandler, WindowsKeyboardUtils, } = require("../../../src/tui/utils/windowsKeyboardHandlers.js"); // Mock Windows environment const mockWindowsEnvironment = (terminalType = "windows-terminal") => { Object.defineProperty(process, "platform", { value: "win32", writable: true, }); // Clear environment first delete process.env.WT_SESSION; delete process.env.TERM_PROGRAM; delete process.env.PSModulePath; delete process.env.COMSPEC; delete process.env.COLORTERM; switch (terminalType) { case "windows-terminal": process.env.WT_SESSION = "test-session"; process.env.TERM_PROGRAM = "Windows Terminal"; process.env.COLORTERM = "truecolor"; break; case "cmd": process.env.COMSPEC = "C:\\Windows\\system32\\cmd.exe"; break; case "powershell": process.env.PSModulePath = "C:\\Program Files\\PowerShell\\Modules"; process.env.TERM_PROGRAM = "PowerShell"; break; } }; describe("Windows Optimizations Tests", () => { const originalPlatform = process.platform; const originalEnv = process.env; afterEach(() => { process.env = originalEnv; Object.defineProperty(process, "platform", { value: originalPlatform, writable: true, }); // Clear rendering cache WindowsRenderingOptimizations.clearCache(); }); describe("Windows Rendering Optimizations", () => { test("should cache terminal capabilities for performance", () => { mockWindowsEnvironment("windows-terminal"); const startTime = Date.now(); // First call should detect and cache const capabilities1 = WindowsRenderingOptimizations.getCachedCapabilities(); const firstCallTime = Date.now() - startTime; // Second call should use cache const cacheStartTime = Date.now(); const capabilities2 = WindowsRenderingOptimizations.getCachedCapabilities(); const cacheCallTime = Date.now() - cacheStartTime; expect(capabilities1).toEqual(capabilities2); expect(cacheCallTime).toBeLessThan(10); // Cache should be very fast }); test("should provide optimized character sets for different terminals", () => { // Test Windows Terminal mockWindowsEnvironment("windows-terminal"); const wtCharSet = WindowsRenderingOptimizations.getOptimizedCharacterSet(); expect(wtCharSet.progress.filled).toBe("█"); expect(wtCharSet.status.success).toBe("✅"); // Test Command Prompt mockWindowsEnvironment("cmd"); WindowsRenderingOptimizations.clearCache(); const cmdCharSet = WindowsRenderingOptimizations.getOptimizedCharacterSet(); expect(cmdCharSet.progress.filled).toBe("#"); expect(cmdCharSet.status.success).toBe("v"); // Test PowerShell mockWindowsEnvironment("powershell"); WindowsRenderingOptimizations.clearCache(); const psCharSet = WindowsRenderingOptimizations.getOptimizedCharacterSet(); expect(psCharSet.progress.filled).toBe("█"); expect(psCharSet.status.success).toBe("✓"); }); test("should optimize strings for Command Prompt", () => { mockWindowsEnvironment("cmd"); const complexString = "█████░░░░░ ●✓ ►Test"; const optimized = WindowsRenderingOptimizations.optimizeString(complexString); expect(optimized).toBe("#####----- *v >Test"); }); test("should provide appropriate update frequencies", () => { // Command Prompt should have lowest frequency mockWindowsEnvironment("cmd"); WindowsRenderingOptimizations.clearCache(); expect(WindowsRenderingOptimizations.getOptimalUpdateFrequency()).toBe( 250 ); // PowerShell should have medium frequency mockWindowsEnvironment("powershell"); WindowsRenderingOptimizations.clearCache(); expect(WindowsRenderingOptimizations.getOptimalUpdateFrequency()).toBe( 100 ); // Windows Terminal should have highest frequency mockWindowsEnvironment("windows-terminal"); WindowsRenderingOptimizations.clearCache(); expect(WindowsRenderingOptimizations.getOptimalUpdateFrequency()).toBe( 50 ); }); }); describe("Windows Keyboard Optimizations", () => { test("should normalize Windows keyboard events", () => { const testCases = [ { input: "\r\n", expected: { name: "return" } }, { input: "\x03", expected: { name: "c", ctrl: true } }, { input: "\x1a", expected: { name: "z", ctrl: true } }, { input: "\x1b[1;5A", expected: { name: "up", ctrl: true } }, ]; testCases.forEach(({ input, expected }) => { const result = WindowsKeyboardOptimizations.normalizeKeyEvent( input, null ); expect(result.key).toMatchObject(expected); }); }); test("should create key debouncer", () => { const debouncer = WindowsKeyboardOptimizations.createKeyDebouncer(100); // First call should pass through const result1 = debouncer("a", { name: "a" }); expect(result1).toBeTruthy(); // Immediate second call with same key should be filtered const result2 = debouncer("a", { name: "a" }); expect(result2).toBeNull(); // Different key should pass through const result3 = debouncer("b", { name: "b" }); expect(result3).toBeTruthy(); }); }); describe("Windows File System Optimizations", () => { test("should normalize Windows file paths", () => { const testPaths = [ { input: "C:\\Users\\Test\\file.txt", expected: "C:/Users/Test/file.txt", }, { input: "\\\\server\\share\\file.txt", expected: "//server/share/file.txt", }, { input: "relative\\path\\file.txt", expected: "relative/path/file.txt", }, ]; testPaths.forEach(({ input, expected }) => { const result = WindowsFileSystemOptimizations.normalizePath(input); expect(result).toBe(expected); }); }); test("should provide Windows user directories", () => { // Mock Windows environment variables process.env.USERPROFILE = "C:\\Users\\TestUser"; process.env.APPDATA = "C:\\Users\\TestUser\\AppData\\Roaming"; process.env.LOCALAPPDATA = "C:\\Users\\TestUser\\AppData\\Local"; const dirs = WindowsFileSystemOptimizations.getUserDirectories(); expect(dirs.home).toBe("C:\\Users\\TestUser"); expect(dirs.appData).toBe("C:\\Users\\TestUser\\AppData\\Roaming"); expect(dirs.localAppData).toBe("C:\\Users\\TestUser\\AppData\\Local"); }); }); describe("Windows Performance Monitor", () => { test("should monitor rendering performance", () => { const monitor = WindowsPerformanceMonitor.createRenderingMonitor(); monitor.startFrame(); // Simulate some work const start = Date.now(); while (Date.now() - start < 10) { // Busy wait for 10ms } const stats = monitor.endFrame(); expect(stats.frameTime).toBeGreaterThan(5); expect(stats.totalFrames).toBe(1); expect(stats.fps).toBeGreaterThan(0); }); test("should monitor memory usage", () => { const usage = WindowsPerformanceMonitor.getMemoryUsage(); expect(typeof usage.heapUsed).toBe("number"); expect(typeof usage.heapTotal).toBe("number"); expect(typeof usage.external).toBe("number"); expect(typeof usage.rss).toBe("number"); expect(usage.heapUsed).toBeGreaterThan(0); expect(usage.heapTotal).toBeGreaterThan(usage.heapUsed); }); }); describe("Windows Keyboard Handler", () => { test("should create keyboard handler with Windows optimizations", () => { mockWindowsEnvironment("windows-terminal"); const handler = createWindowsKeyboardHandler({ debounceDelay: 25, enableEnhancedKeys: true, }); expect(handler).toBeInstanceOf(WindowsKeyboardHandler); expect(handler.debounceDelay).toBe(25); expect(handler.enableEnhancedKeys).toBe(true); }); test("should parse Windows Terminal enhanced keys", () => { mockWindowsEnvironment("windows-terminal"); const handler = new WindowsKeyboardHandler(); const testKeys = [ { input: "\x1b[1;5A", expected: { name: "up", ctrl: true } }, { input: "\x1b[1;2B", expected: { name: "down", shift: true } }, { input: "\x1b[1;3C", expected: { name: "right", meta: true } }, ]; testKeys.forEach(({ input, expected }) => { const result = handler.parseWindowsTerminalKeys(input); expect(result.key).toMatchObject(expected); }); }); test("should handle Command Prompt key limitations", () => { mockWindowsEnvironment("cmd"); const handler = new WindowsKeyboardHandler(); const testKeys = [ { input: "\x03", expected: { name: "c", ctrl: true } }, { input: "\r", expected: { name: "return" } }, { input: "\x08", expected: { name: "backspace" } }, ]; testKeys.forEach(({ input, expected }) => { const result = handler.parseCommandPromptKeys(input); expect(result.key).toMatchObject(expected); }); }); test("should provide keyboard handler statistics", () => { const handler = new WindowsKeyboardHandler(); const stats = handler.getStats(); expect(stats).toHaveProperty("isActive"); expect(stats).toHaveProperty("capabilities"); expect(stats).toHaveProperty("debounceDelay"); expect(stats).toHaveProperty("enableEnhancedKeys"); expect(stats).toHaveProperty("listenerCount"); }); }); describe("Windows Keyboard Utils", () => { test("should identify system shortcuts", () => { const systemShortcuts = [ { input: "\x03", key: { name: "c", ctrl: true } }, // Ctrl+C { input: "v", key: { name: "v", ctrl: true } }, // Ctrl+V { input: "z", key: { name: "z", ctrl: true } }, // Ctrl+Z ]; systemShortcuts.forEach(({ input, key }) => { const isSystem = WindowsKeyboardUtils.isSystemShortcut(input, key); expect(isSystem).toBe(true); }); }); test("should generate Windows-friendly key descriptions", () => { const testKeys = [ { input: "a", key: { name: "a", ctrl: true }, expected: "Ctrl+A" }, { input: "f4", key: { name: "f4", meta: true }, expected: "Alt+F4" }, { input: "tab", key: { name: "tab", ctrl: true, shift: true }, expected: "Ctrl+Shift+Tab", }, ]; testKeys.forEach(({ input, key, expected }) => { const description = WindowsKeyboardUtils.getKeyDescription(input, key); expect(description).toBe(expected); }); }); }); describe("Performance Benchmarks", () => { test("should perform character optimization efficiently", () => { mockWindowsEnvironment("cmd"); const testString = "█████░░░░░ ●✓ ►Test with lots of Unicode characters ▶️🔵"; const iterations = 1000; const startTime = Date.now(); for (let i = 0; i < iterations; i++) { WindowsRenderingOptimizations.optimizeString(testString); } const totalTime = Date.now() - startTime; const avgTime = totalTime / iterations; expect(avgTime).toBeLessThan(1); // Should average less than 1ms per optimization }); test("should handle rapid capability checks efficiently", () => { const terminals = ["windows-terminal", "cmd", "powershell"]; const iterations = 100; const startTime = Date.now(); for (let i = 0; i < iterations; i++) { const terminalType = terminals[i % terminals.length]; mockWindowsEnvironment(terminalType); WindowsRenderingOptimizations.clearCache(); WindowsRenderingOptimizations.getCachedCapabilities(); } const totalTime = Date.now() - startTime; expect(totalTime).toBeLessThan(500); // Should complete in less than 500ms }); }); });