Skip to content

Commit 731ae55

Browse files
committed
Refactor in terms of user intents
Reflect the latest changes to the AT Driver specification, where "press keys" has been restructured as a parameters of a new "interaction" command designed for "user intents."
1 parent 6eb43aa commit 731ae55

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

shared/install/macos.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { promisify } = require('util');
77

88
const debug = require('debug')('install');
99

10-
const { 'interaction.pressKeys': pressKeys } = require('../modules/macos/interaction');
10+
const { 'interaction.userIntent': userIntent } = require('../modules/macos/interaction');
1111

1212
const LSREGISTER_EXECUTABLE_PATH =
1313
'/System/Library/Frameworks/CoreServices.framework/Versions/Current/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister';
@@ -134,7 +134,7 @@ exports.uninstall = async function () {
134134
*/
135135
const canPressKeys = async () => {
136136
try {
137-
await pressKeys(null, { keys: ['shift'] });
137+
await userIntent(null, { name: 'pressKeys', keys: ['shift'] });
138138
} catch ({}) {
139139
return false;
140140
}

shared/modules/macos/interaction.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
const { runScript, renderScript } = require('../../helpers/macos/applescript');
66
const { parseCodePoints } = require('../../helpers/macos/parseCodePoints');
77

8-
const pressKeys = /** @type {ATDriverModules.InteractionPressKeys} */ (
9-
async function (websocket, { keys }) {
8+
const userIntent = /** @type {ATDriverModules.InteractionUserIntent} */ (
9+
async function (websocket, { name, keys }) {
10+
if (name !== 'pressKeys') {
11+
throw new Error('unkown user intent');
12+
}
1013
await runScript(renderScript(parseCodePoints(keys)));
1114
return {};
1215
}
1316
);
1417

1518
module.exports = /** @type {ATDriverModules.Interaction} */ ({
16-
'interaction.pressKeys': pressKeys,
19+
'interaction.userIntent': userIntent,
1720
});

shared/modules/types.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@
2424

2525
/**
2626
* @typedef ATDriverModules.InteractionPressKeysParameters
27+
* @property {"pressKeys"} name
2728
* @property {ATDriverModules.InteractionPressKeysKeyCombination} keys
2829
*/
2930

3031
/**
31-
* @typedef {ATDriverModules.Command<ATDriverModules.InteractionPressKeysParameters, {}>} ATDriverModules.InteractionPressKeys
32+
* @typedef {ATDriverModules.Command<ATDriverModules.InteractionPressKeysParameters, {}>} ATDriverModules.InteractionUserIntent
3233
*/
3334

3435
/**
3536
* @typedef {{
36-
* "interaction.pressKeys": ATDriverModules.InteractionPressKeys
37+
* "interaction.userIntent": ATDriverModules.InteractionUserIntent
3738
* }} ATDriverModules.Interaction
3839
*/
3940

shared/modules/win32/interaction.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ const keyboardActionsMap = {
6969

7070
const keycodeMatch = /[\ue000-\ue05d]/;
7171

72-
const pressKeys = /** @type {ATDriverModules.InteractionPressKeys} */ (
73-
(websocket, { keys }) => {
72+
const userIntent = /** @type {ATDriverModules.InteractionUserIntent} */ (
73+
(websocket, { name, keys }) => {
74+
if (name !== 'pressKeys') {
75+
throw new Error('unkown user intent');
76+
}
7477
const robotjsKeys = keys.map(key => {
7578
if (keyboardActionsMap[key]) return keyboardActionsMap[key];
7679
if (keycodeMatch.test(key))
@@ -93,5 +96,5 @@ const pressKeys = /** @type {ATDriverModules.InteractionPressKeys} */ (
9396
);
9497

9598
module.exports = /** @type {ATDriverModules.Interaction} */ ({
96-
'interaction.pressKeys': pressKeys,
99+
'interaction.userIntent': userIntent,
97100
});

test/test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,14 @@ suite('at-driver', () => {
163163
});
164164

165165
test('rejects Command messages with omitted "id"', async () => {
166-
websocket.send('{"method": "interaction.pressKeys", "params": {"keys": ["A"]}}');
166+
websocket.send('{"method": "interaction.userIntent", "params": {"name": "pressKeys", "keys": ["A"]}}');
167167
const message = await Promise.race([whenClosed, nextMessage(websocket)]);
168168

169169
assert.deepEqual(message, {
170170
id: null,
171171
error: 'unknown error',
172172
message:
173-
'Command missing required "id": "{"method": "interaction.pressKeys", "params": {"keys": ["A"]}}".',
173+
'Command missing required "id": "{"method": "interaction.userIntent", "params": {"name": "pressKeys", "keys": ["A"]}}".',
174174
});
175175
});
176176

@@ -184,8 +184,8 @@ suite('at-driver', () => {
184184
});
185185
});
186186

187-
test('accepts valid "pressKey" Command', async () => {
188-
websocket.send('{"id": 83, "method": "interaction.pressKeys", "params": {"keys": [" "]}}');
187+
test('accepts valid "userIntent" Command', async () => {
188+
websocket.send('{"id": 83, "method": "interaction.userIntent", "params": {"name": "pressKeys", "keys": [" "]}}');
189189
const message = await Promise.race([whenClosed, nextMessage(websocket)]);
190190

191191
assert.deepEqual(message, {
@@ -194,9 +194,9 @@ suite('at-driver', () => {
194194
});
195195
});
196196

197-
test('rejects invalid "pressKey" Command', async () => {
197+
test('rejects invalid "userIntent" Command', async () => {
198198
websocket.send(
199-
'{"id": 902, "method": "interaction.pressKeys", "params": {"keys": ["df daf% ?"]}}',
199+
'{"id": 902, "method": "interaction.userIntent", "params": {"name": "pressKeys", "keys": ["df daf% ?"]}}',
200200
);
201201
const message = await Promise.race([whenClosed, nextMessage(websocket)]);
202202

0 commit comments

Comments
 (0)