|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 8 | +import os from 'node:os'; |
| 9 | +import { |
| 10 | + isWindows10, |
| 11 | + isJetBrainsTerminal, |
| 12 | + supportsTrueColor, |
| 13 | + getCompatibilityWarnings, |
| 14 | +} from './compatibility.js'; |
| 15 | + |
| 16 | +vi.mock('node:os', () => ({ |
| 17 | + default: { |
| 18 | + platform: vi.fn(), |
| 19 | + release: vi.fn(), |
| 20 | + }, |
| 21 | +})); |
| 22 | + |
| 23 | +describe('compatibility', () => { |
| 24 | + const originalGetColorDepth = process.stdout.getColorDepth; |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + process.stdout.getColorDepth = originalGetColorDepth; |
| 28 | + vi.restoreAllMocks(); |
| 29 | + vi.unstubAllEnvs(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('isWindows10', () => { |
| 33 | + it('should return true for Windows 10 (build < 22000)', () => { |
| 34 | + vi.mocked(os.platform).mockReturnValue('win32'); |
| 35 | + vi.mocked(os.release).mockReturnValue('10.0.19041'); |
| 36 | + expect(isWindows10()).toBe(true); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should return false for Windows 11 (build >= 22000)', () => { |
| 40 | + vi.mocked(os.platform).mockReturnValue('win32'); |
| 41 | + vi.mocked(os.release).mockReturnValue('10.0.22000'); |
| 42 | + expect(isWindows10()).toBe(false); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return false for non-Windows platforms', () => { |
| 46 | + vi.mocked(os.platform).mockReturnValue('darwin'); |
| 47 | + vi.mocked(os.release).mockReturnValue('20.6.0'); |
| 48 | + expect(isWindows10()).toBe(false); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('isJetBrainsTerminal', () => { |
| 53 | + it('should return true when TERMINAL_EMULATOR is JetBrains-JediTerm', () => { |
| 54 | + vi.stubEnv('TERMINAL_EMULATOR', 'JetBrains-JediTerm'); |
| 55 | + expect(isJetBrainsTerminal()).toBe(true); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should return false for other terminals', () => { |
| 59 | + vi.stubEnv('TERMINAL_EMULATOR', 'something-else'); |
| 60 | + expect(isJetBrainsTerminal()).toBe(false); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should return false when TERMINAL_EMULATOR is not set', () => { |
| 64 | + vi.stubEnv('TERMINAL_EMULATOR', ''); |
| 65 | + expect(isJetBrainsTerminal()).toBe(false); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('supportsTrueColor', () => { |
| 70 | + it('should return true when COLORTERM is truecolor', () => { |
| 71 | + vi.stubEnv('COLORTERM', 'truecolor'); |
| 72 | + expect(supportsTrueColor()).toBe(true); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should return true when COLORTERM is 24bit', () => { |
| 76 | + vi.stubEnv('COLORTERM', '24bit'); |
| 77 | + expect(supportsTrueColor()).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should return true when getColorDepth returns >= 24', () => { |
| 81 | + vi.stubEnv('COLORTERM', ''); |
| 82 | + process.stdout.getColorDepth = vi.fn().mockReturnValue(24); |
| 83 | + expect(supportsTrueColor()).toBe(true); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should return false when true color is not supported', () => { |
| 87 | + vi.stubEnv('COLORTERM', ''); |
| 88 | + process.stdout.getColorDepth = vi.fn().mockReturnValue(8); |
| 89 | + expect(supportsTrueColor()).toBe(false); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('getCompatibilityWarnings', () => { |
| 94 | + beforeEach(() => { |
| 95 | + // Default to supporting true color to keep existing tests simple |
| 96 | + vi.stubEnv('COLORTERM', 'truecolor'); |
| 97 | + process.stdout.getColorDepth = vi.fn().mockReturnValue(24); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should return Windows 10 warning when detected', () => { |
| 101 | + vi.mocked(os.platform).mockReturnValue('win32'); |
| 102 | + vi.mocked(os.release).mockReturnValue('10.0.19041'); |
| 103 | + vi.stubEnv('TERMINAL_EMULATOR', ''); |
| 104 | + |
| 105 | + const warnings = getCompatibilityWarnings(); |
| 106 | + expect(warnings).toContain( |
| 107 | + 'Warning: Windows 10 detected. Some UI features like smooth scrolling may be degraded. Windows 11 is recommended for the best experience.', |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should return JetBrains warning when detected', () => { |
| 112 | + vi.mocked(os.platform).mockReturnValue('darwin'); |
| 113 | + vi.stubEnv('TERMINAL_EMULATOR', 'JetBrains-JediTerm'); |
| 114 | + |
| 115 | + const warnings = getCompatibilityWarnings(); |
| 116 | + expect(warnings).toContain( |
| 117 | + 'Warning: JetBrains terminal detected. You may experience rendering or scrolling issues. Using an external terminal (e.g., Windows Terminal, iTerm2) is recommended.', |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should return true color warning when not supported', () => { |
| 122 | + vi.mocked(os.platform).mockReturnValue('darwin'); |
| 123 | + vi.stubEnv('TERMINAL_EMULATOR', ''); |
| 124 | + vi.stubEnv('COLORTERM', ''); |
| 125 | + process.stdout.getColorDepth = vi.fn().mockReturnValue(8); |
| 126 | + |
| 127 | + const warnings = getCompatibilityWarnings(); |
| 128 | + expect(warnings).toContain( |
| 129 | + 'Warning: True color (24-bit) support not detected. Using a terminal with true color enabled will result in a better visual experience.', |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should return all warnings when all are detected', () => { |
| 134 | + vi.mocked(os.platform).mockReturnValue('win32'); |
| 135 | + vi.mocked(os.release).mockReturnValue('10.0.19041'); |
| 136 | + vi.stubEnv('TERMINAL_EMULATOR', 'JetBrains-JediTerm'); |
| 137 | + vi.stubEnv('COLORTERM', ''); |
| 138 | + process.stdout.getColorDepth = vi.fn().mockReturnValue(8); |
| 139 | + |
| 140 | + const warnings = getCompatibilityWarnings(); |
| 141 | + expect(warnings).toHaveLength(3); |
| 142 | + expect(warnings[0]).toContain('Windows 10 detected'); |
| 143 | + expect(warnings[1]).toContain('JetBrains terminal detected'); |
| 144 | + expect(warnings[2]).toContain('True color (24-bit) support not detected'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should return no warnings in a standard environment with true color', () => { |
| 148 | + vi.mocked(os.platform).mockReturnValue('darwin'); |
| 149 | + vi.stubEnv('TERMINAL_EMULATOR', ''); |
| 150 | + vi.stubEnv('COLORTERM', 'truecolor'); |
| 151 | + |
| 152 | + const warnings = getCompatibilityWarnings(); |
| 153 | + expect(warnings).toHaveLength(0); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments