Skip to content

Commit 893307f

Browse files
committed
fix: remove cast
1 parent 9ad3f1e commit 893307f

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

src/tools/input.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import type {McpContext, TextSnapshotNode} from '../McpContext.js';
88
import {zod} from '../third_party/index.js';
9-
import type {ElementHandle, KeyInput} from '../third_party/index.js';
9+
import type {ElementHandle} from '../third_party/index.js';
1010
import {parseKey} from '../utils/keyboard.js';
1111

1212
import {ToolCategory} from './categories.js';
@@ -288,12 +288,12 @@ export const pressKey = defineTool({
288288
},
289289
handler: async (request, response, context) => {
290290
const page = context.getSelectedPage();
291-
const tokens = parseKey(request.params.key) as KeyInput[];
291+
const tokens = parseKey(request.params.key);
292292
const [key, ...modifiers] = tokens;
293293

294294
await context.waitForEventsAfterAction(async () => {
295-
for (let i = modifiers.length - 1; i >= 0; i--) {
296-
await page.keyboard.down(modifiers[i]);
295+
for (const modifier of modifiers.toReversed()) {
296+
await page.keyboard.down(modifier);
297297
}
298298
await page.keyboard.press(key);
299299
for (const modifier of modifiers) {

src/utils/keyboard.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
import type {KeyInput} from '../third_party';
77

8+
// See the KeyInput type for the list of supported keys.
89
const validKeys = new Set([
910
'0',
1011
'1',
@@ -273,7 +274,7 @@ function throwIfInvalidKey(key: string): KeyInput {
273274
}
274275

275276
/**
276-
* Returns the key, followed by modifiers.
277+
* Returns the primary key, followed by modifiers in original order.
277278
*/
278279
export function parseKey(keyInput: string): [KeyInput, ...KeyInput[]] {
279280
let key = '';
@@ -297,5 +298,5 @@ export function parseKey(keyInput: string): [KeyInput, ...KeyInput[]] {
297298
throw new Error(`Key ${keyInput} contains duplicate keys.`);
298299
}
299300

300-
return result.reverse() as [KeyInput, ...KeyInput[]];
301+
return [result.at(-1), ...result.slice(0, -1)] as [KeyInput, ...KeyInput[]];
301302
}

tests/tools/input.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ describe('input', () => {
436436

437437
describe('press_key', () => {
438438
it('parses keys', () => {
439-
assert.deepStrictEqual(parseKey('Shift+A'), ['A', 'Shift']);
440-
assert.deepStrictEqual(parseKey('Shift++'), ['+', 'Shift']);
439+
assert.deepStrictEqual(parseKey('Shift+A'), ['Shift', 'A']);
440+
assert.deepStrictEqual(parseKey('Shift++'), ['Shift', '+']);
441441
assert.deepStrictEqual(parseKey('Shift'), ['Shift']);
442442
assert.deepStrictEqual(parseKey('KeyA'), ['KeyA']);
443443
});

0 commit comments

Comments
 (0)