File size: 3,987 Bytes
9332ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect } from 'vitest';
import { formatKeyBinding, formatCommand } from './keybindingUtils.js';
import { Command, KeyBinding } from './keyBindings.js';

describe('keybindingUtils', () => {
  describe('formatKeyBinding', () => {
    const testCases: Array<{
      name: string;
      binding: KeyBinding;
      expected: {
        darwin: string;
        win32: string;
        linux: string;
        default: string;
      };
    }> = [
      {
        name: 'simple key',
        binding: new KeyBinding('a'),
        expected: { darwin: 'A', win32: 'A', linux: 'A', default: 'A' },
      },
      {
        name: 'named key (return)',
        binding: new KeyBinding('enter'),
        expected: {
          darwin: 'Enter',
          win32: 'Enter',
          linux: 'Enter',
          default: 'Enter',
        },
      },
      {
        name: 'named key (escape)',
        binding: new KeyBinding('escape'),
        expected: { darwin: 'Esc', win32: 'Esc', linux: 'Esc', default: 'Esc' },
      },
      {
        name: 'ctrl modifier',
        binding: new KeyBinding('ctrl+c'),
        expected: {
          darwin: 'Ctrl+C',
          win32: 'Ctrl+C',
          linux: 'Ctrl+C',
          default: 'Ctrl+C',
        },
      },
      {
        name: 'cmd modifier',
        binding: new KeyBinding('cmd+z'),
        expected: {
          darwin: 'Cmd+Z',
          win32: 'Win+Z',
          linux: 'Super+Z',
          default: 'Cmd/Win+Z',
        },
      },
      {
        name: 'alt/option modifier',
        binding: new KeyBinding('alt+left'),
        expected: {
          darwin: 'Option+Left',
          win32: 'Alt+Left',
          linux: 'Alt+Left',
          default: 'Alt+Left',
        },
      },
      {
        name: 'shift modifier',
        binding: new KeyBinding('shift+up'),
        expected: {
          darwin: 'Shift+Up',
          win32: 'Shift+Up',
          linux: 'Shift+Up',
          default: 'Shift+Up',
        },
      },
      {
        name: 'multiple modifiers (ctrl+shift)',
        binding: new KeyBinding('ctrl+shift+z'),
        expected: {
          darwin: 'Ctrl+Shift+Z',
          win32: 'Ctrl+Shift+Z',
          linux: 'Ctrl+Shift+Z',
          default: 'Ctrl+Shift+Z',
        },
      },
      {
        name: 'all modifiers',
        binding: new KeyBinding('ctrl+alt+shift+cmd+a'),
        expected: {
          darwin: 'Ctrl+Option+Shift+Cmd+A',
          win32: 'Ctrl+Alt+Shift+Win+A',
          linux: 'Ctrl+Alt+Shift+Super+A',
          default: 'Ctrl+Alt+Shift+Cmd/Win+A',
        },
      },
    ];

    testCases.forEach(({ name, binding, expected }) => {
      describe(`${name}`, () => {
        it('formats correctly for darwin', () => {
          expect(formatKeyBinding(binding, 'darwin')).toBe(expected.darwin);
        });
        it('formats correctly for win32', () => {
          expect(formatKeyBinding(binding, 'win32')).toBe(expected.win32);
        });
        it('formats correctly for linux', () => {
          expect(formatKeyBinding(binding, 'linux')).toBe(expected.linux);
        });
        it('formats correctly for default', () => {
          expect(formatKeyBinding(binding, 'default')).toBe(expected.default);
        });
      });
    });
  });

  describe('formatCommand', () => {
    it('formats default commands (using default platform behavior)', () => {
      expect(formatCommand(Command.QUIT, undefined, 'default')).toBe('Ctrl+C');
      expect(formatCommand(Command.SUBMIT, undefined, 'default')).toBe('Enter');
      expect(
        formatCommand(Command.TOGGLE_BACKGROUND_SHELL, undefined, 'default'),
      ).toBe('Ctrl+B');
    });

    it('returns empty string for unknown commands', () => {
      expect(
        formatCommand(
          'unknown.command' as unknown as Command,
          undefined,
          'default',
        ),
      ).toBe('');
    });
  });
});