Compare commits

...
+169
View File
@@ -0,0 +1,169 @@
/**
* Test for Alt+- keyboard handling specifically
* This tests both the evaluateKeyboardEvent function and potential browser integration issues
*/
import { assert } from 'chai';
import { evaluateKeyboardEvent } from 'common/input/Keyboard';
import { IKeyboardResult, IKeyboardEvent } from 'common/Types';
function createMockKeyboardEvent(partialEvent: Partial<IKeyboardEvent>): IKeyboardEvent {
return {
altKey: false,
ctrlKey: false,
shiftKey: false,
metaKey: false,
keyCode: 0,
code: '',
key: '',
type: 'keydown',
...partialEvent
};
}
describe('Alt+- keyboard handling', () => {
it('should return correct escape sequence for Alt+- on all platforms', () => {
const event = createMockKeyboardEvent({
altKey: true,
keyCode: 189,
key: '-',
type: 'keydown'
});
// Test on non-Mac (Linux/Windows)
const resultLinux = evaluateKeyboardEvent(event, false, false, false);
assert.equal(resultLinux.key, '\x1b-', 'Alt+- should produce \\x1b- on Linux');
assert.equal(resultLinux.cancel, false, 'Alt+- should not be cancelled on Linux');
// Test on Windows
const resultWindows = evaluateKeyboardEvent(event, false, false, false);
assert.equal(resultWindows.key, '\x1b-', 'Alt+- should produce \\x1b- on Windows');
// Test on Mac with macOptionIsMeta: true
const resultMacMeta = evaluateKeyboardEvent(event, false, true, true);
assert.equal(resultMacMeta.key, '\x1b-', 'Alt+- should produce \\x1b- on Mac with macOptionIsMeta');
// Test on Mac with macOptionIsMeta: false (third level shift)
const resultMacNoMeta = evaluateKeyboardEvent(event, false, true, false);
assert.equal(resultMacNoMeta.key, undefined, 'Alt+- should not produce escape sequence on Mac without macOptionIsMeta');
});
it('should handle Alt+- with shift key (underscore)', () => {
const event = createMockKeyboardEvent({
altKey: true,
shiftKey: true,
keyCode: 189,
key: '_',
type: 'keydown'
});
const result = evaluateKeyboardEvent(event, false, false, false);
assert.equal(result.key, '\x1b_', 'Alt+Shift+- should produce \\x1b_');
});
it('should verify keyCode 189 maps to minus character', () => {
// This test verifies that keyCode 189 is correctly mapped in KEYCODE_KEY_MAPPINGS
const event = createMockKeyboardEvent({
altKey: true,
keyCode: 189,
key: '-',
type: 'keydown'
});
const result = evaluateKeyboardEvent(event, false, false, false);
assert.isDefined(result.key, 'Result should have a key');
assert.equal(result.key, '\x1b-', 'Should produce the correct escape sequence');
});
it('should handle various browser scenarios for Alt+-', () => {
// Test with different keyCode values that might be used for minus
const keyCodes = [189, 109]; // 189 is standard minus, 109 is numpad minus
keyCodes.forEach(keyCode => {
const event = createMockKeyboardEvent({
altKey: true,
keyCode: keyCode,
key: '-',
type: 'keydown'
});
const result = evaluateKeyboardEvent(event, false, false, false);
if (keyCode === 189) {
assert.equal(result.key, '\x1b-', `keyCode ${keyCode} should produce \\x1b-`);
} else {
// For numpad minus or other codes, the behavior might be different
// but we should still handle them gracefully
assert.isDefined(result, `keyCode ${keyCode} should return a result`);
}
});
});
it('should not be affected by composition state for regular Alt combinations', () => {
// This tests that Alt+- is not treated as a composition character
const event = createMockKeyboardEvent({
altKey: true,
keyCode: 189,
key: '-',
type: 'keydown'
});
// Ensure the keyCode is not 229 (composition character)
assert.notEqual(event.keyCode, 229, 'Alt+- should not use composition keyCode');
const result = evaluateKeyboardEvent(event, false, false, false);
assert.equal(result.key, '\x1b-', 'Alt+- should work normally without composition interference');
});
describe('Browser integration considerations', () => {
it('should document known scenarios where Alt+- might not work', () => {
// This test documents potential causes for the reported issue
// NOTE: These are integration issues, not keyboard evaluation issues
const possibleCauses = [
'Terminal not properly focused when testing',
'Custom key event handlers preventing default behavior',
'IME or composition methods intercepting events',
'Browser-specific keyboard layout differences',
'Third-party browser extensions interfering'
];
// The keyboard evaluation should always work correctly
const event = createMockKeyboardEvent({
altKey: true,
keyCode: 189,
key: '-',
type: 'keydown'
});
const result = evaluateKeyboardEvent(event, false, false, false);
assert.equal(result.key, '\x1b-', 'Keyboard evaluation should always work');
// Document that integration issues are separate concerns
assert.isTrue(possibleCauses.length > 0, 'There are known integration scenarios to consider');
});
it('should handle different keyboard layouts gracefully', () => {
// Some keyboard layouts might use different keyCodes for minus
const potentialMinusKeyCodes = [189, 173, 109]; // Various minus key codes
potentialMinusKeyCodes.forEach(keyCode => {
const event = createMockKeyboardEvent({
altKey: true,
keyCode: keyCode,
key: '-',
type: 'keydown'
});
const result = evaluateKeyboardEvent(event, false, false, false);
if (keyCode === 189) {
// Standard minus key should always work
assert.equal(result.key, '\x1b-', `Standard minus (keyCode ${keyCode}) should work`);
} else {
// Other keys might not be mapped, but should not throw errors
assert.isDefined(result, `KeyCode ${keyCode} should return a valid result`);
}
});
});
});
});