@e2e-dev/desktop 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +79 -2
- package/dist/.build.json +1 -0
- package/dist/binary.d.ts +25 -0
- package/dist/binary.js +209 -0
- package/dist/cli.d.ts +82 -0
- package/dist/cli.js +170 -0
- package/dist/desktop.d.ts +25 -0
- package/dist/desktop.js +27 -0
- package/dist/engine-entry.d.ts +12 -0
- package/dist/engine-entry.js +8 -0
- package/dist/engine.d.ts +18 -0
- package/dist/engine.js +109 -0
- package/dist/fixtures.d.ts +6 -0
- package/dist/fixtures.js +2 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +18 -0
- package/dist/keys.d.ts +8 -0
- package/dist/keys.js +79 -0
- package/dist/nodes.d.ts +95 -0
- package/dist/nodes.js +290 -0
- package/dist/options.d.ts +67 -0
- package/dist/options.js +53 -0
- package/dist/png.d.ts +24 -0
- package/dist/png.js +144 -0
- package/dist/runner-test.d.ts +15 -0
- package/dist/runner-test.js +45 -0
- package/dist/surface.d.ts +270 -0
- package/dist/surface.js +1439 -0
- package/package.json +71 -3
package/dist/nodes.js
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-desktop's snapshot tree projected onto the contract's semantic
|
|
3
|
+
* nodes. agent-desktop already speaks one closed role and state vocabulary
|
|
4
|
+
* on every OS; this maps it onto the ARIA names `screen.getByRole` and the
|
|
5
|
+
* agent use on every other engine, so `getByRole('textbox')` means the same
|
|
6
|
+
* field in a browser, on a phone, and in a desktop app.
|
|
7
|
+
*/
|
|
8
|
+
/** The observation root's id: one window, the same id across observations, as the contract requires. */
|
|
9
|
+
export const ROOT_ID = 'root';
|
|
10
|
+
/**
|
|
11
|
+
* ARIA's name for each agent-desktop role that has one. A role missing here
|
|
12
|
+
* keeps agent-desktop's own token: it is still one closed vocabulary, just
|
|
13
|
+
* not one a browser shares.
|
|
14
|
+
*/
|
|
15
|
+
const ROLE_MAP = {
|
|
16
|
+
alert: 'alert',
|
|
17
|
+
alertdialog: 'alertdialog',
|
|
18
|
+
application: 'application',
|
|
19
|
+
article: 'article',
|
|
20
|
+
banner: 'banner',
|
|
21
|
+
button: 'button',
|
|
22
|
+
cell: 'cell',
|
|
23
|
+
checkbox: 'checkbox',
|
|
24
|
+
column: 'columnheader',
|
|
25
|
+
combobox: 'combobox',
|
|
26
|
+
complementary: 'complementary',
|
|
27
|
+
contentinfo: 'contentinfo',
|
|
28
|
+
definition: 'definition',
|
|
29
|
+
dialog: 'dialog',
|
|
30
|
+
disclosure: 'button',
|
|
31
|
+
document: 'document',
|
|
32
|
+
form: 'form',
|
|
33
|
+
grid: 'grid',
|
|
34
|
+
group: 'group',
|
|
35
|
+
heading: 'heading',
|
|
36
|
+
image: 'image',
|
|
37
|
+
incrementor: 'spinbutton',
|
|
38
|
+
link: 'link',
|
|
39
|
+
list: 'list',
|
|
40
|
+
listbox: 'listbox',
|
|
41
|
+
log: 'log',
|
|
42
|
+
main: 'main',
|
|
43
|
+
marquee: 'marquee',
|
|
44
|
+
math: 'math',
|
|
45
|
+
menu: 'menu',
|
|
46
|
+
menubutton: 'button',
|
|
47
|
+
menuitem: 'menuitem',
|
|
48
|
+
navigation: 'navigation',
|
|
49
|
+
note: 'note',
|
|
50
|
+
option: 'option',
|
|
51
|
+
outline: 'tree',
|
|
52
|
+
paragraph: 'paragraph',
|
|
53
|
+
popover: 'dialog',
|
|
54
|
+
progressbar: 'progressbar',
|
|
55
|
+
radiobutton: 'radio',
|
|
56
|
+
radiogroup: 'radiogroup',
|
|
57
|
+
region: 'region',
|
|
58
|
+
row: 'row',
|
|
59
|
+
scrollbar: 'scrollbar',
|
|
60
|
+
search: 'search',
|
|
61
|
+
separator: 'separator',
|
|
62
|
+
sheet: 'dialog',
|
|
63
|
+
slider: 'slider',
|
|
64
|
+
statictext: 'text',
|
|
65
|
+
status: 'status',
|
|
66
|
+
switch: 'switch',
|
|
67
|
+
tab: 'tab',
|
|
68
|
+
tablist: 'tablist',
|
|
69
|
+
tabpanel: 'tabpanel',
|
|
70
|
+
table: 'table',
|
|
71
|
+
term: 'term',
|
|
72
|
+
textfield: 'textbox',
|
|
73
|
+
timer: 'timer',
|
|
74
|
+
toolbar: 'toolbar',
|
|
75
|
+
tooltip: 'tooltip',
|
|
76
|
+
treeitem: 'treeitem',
|
|
77
|
+
webarea: 'document',
|
|
78
|
+
window: 'window',
|
|
79
|
+
};
|
|
80
|
+
/** Roles whose checked state is a fact whenever it is not stated: an unchecked box reports no `checked`. */
|
|
81
|
+
const CHECKABLE_ROLES = new Set(['checkbox', 'switch', 'radiobutton']);
|
|
82
|
+
/** Roles whose name labels what they hold, so it is never their text. */
|
|
83
|
+
const INPUT_ROLES = new Set(['textfield', 'combobox', 'slider', 'incrementor', 'datefield', 'timefield', 'colorwell']);
|
|
84
|
+
/** Roles agent-desktop reads text into `value`, which the contract calls `text`. */
|
|
85
|
+
const TEXT_ROLES = new Set(['statictext', 'heading']);
|
|
86
|
+
/** Roles a swipe on the observation root scrolls, in preference order. */
|
|
87
|
+
const SCROLLABLE_ROLES = new Set(['scrollarea', 'list', 'listbox', 'table', 'outline', 'grid', 'webarea']);
|
|
88
|
+
const STATE_NAMES = ['focused', 'disabled', 'secure', 'expanded', 'selected', 'hidden', 'pressed'];
|
|
89
|
+
/**
|
|
90
|
+
* A label: a whitespace-only one is no label. Nothing is cut here: `locate`
|
|
91
|
+
* answers single-node reads, which carry the whole value, and the harness
|
|
92
|
+
* cuts what it shows the agent.
|
|
93
|
+
*/
|
|
94
|
+
function label(value) {
|
|
95
|
+
return value === undefined || value.trim() === '' ? undefined : value;
|
|
96
|
+
}
|
|
97
|
+
/** The names macOS gives a window's title-bar buttons, which have no title of their own. */
|
|
98
|
+
const WINDOW_CONTROLS = new Set(['Close', 'Minimize', 'Zoom', 'Full Screen']);
|
|
99
|
+
/** The largest a title-bar button is, in points: macOS draws them 14 to 16 points across. */
|
|
100
|
+
const WINDOW_CONTROL_SIZE = 20;
|
|
101
|
+
/**
|
|
102
|
+
* A window's own title-bar button: a button straight under the window, with
|
|
103
|
+
* no developer id, named only for what it does to the window, and the size
|
|
104
|
+
* of a title-bar button. Left out, so `getByRole('button', 'Close')` finds
|
|
105
|
+
* the app's Close button and not the window's; an app's own button of the
|
|
106
|
+
* same name is larger, and one with no bounds is kept.
|
|
107
|
+
*/
|
|
108
|
+
function windowControl(raw) {
|
|
109
|
+
const bounds = raw.bounds;
|
|
110
|
+
return (raw.role === 'button' &&
|
|
111
|
+
raw.native_id === undefined &&
|
|
112
|
+
raw.name !== undefined &&
|
|
113
|
+
WINDOW_CONTROLS.has(raw.name) &&
|
|
114
|
+
label(raw.value) === undefined &&
|
|
115
|
+
label(raw.description) === undefined &&
|
|
116
|
+
bounds !== undefined &&
|
|
117
|
+
bounds.width <= WINDOW_CONTROL_SIZE &&
|
|
118
|
+
bounds.height <= WINDOW_CONTROL_SIZE);
|
|
119
|
+
}
|
|
120
|
+
/** A node agent-desktop reports but nothing reads: a wrapper with no name, value, id, ref, or text of its own. */
|
|
121
|
+
function transparent(raw) {
|
|
122
|
+
// A secure node is never dropped: its bounds are what a screenshot masks.
|
|
123
|
+
if (raw.ref_id !== undefined || raw.native_id !== undefined || raw.states?.includes('secure') === true)
|
|
124
|
+
return false;
|
|
125
|
+
if (label(raw.name) !== undefined || label(raw.value) !== undefined)
|
|
126
|
+
return false;
|
|
127
|
+
return raw.role === 'group' || raw.role === 'unknown' || raw.role === 'layoutitem' || raw.role === 'matte' || raw.role === 'splitter';
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* What makes a node the same node in the next snapshot, among its parent's
|
|
131
|
+
* children: its role and its developer id, else its role and name (or, for
|
|
132
|
+
* static text, its text). A field's value is not part of it, so a field
|
|
133
|
+
* keeps its id while it is typed into, and a status with a developer id
|
|
134
|
+
* keeps its id while its text changes. `named` adds the name to the
|
|
135
|
+
* developer id, for siblings a toolkit gives one id (AppKit menu items share
|
|
136
|
+
* `menuItemSelected:`).
|
|
137
|
+
*/
|
|
138
|
+
function identityOf(node, named = false) {
|
|
139
|
+
const name = node.name ?? (TEXT_ROLES.has(node.role) ? node.value : undefined) ?? '';
|
|
140
|
+
const id = node.native_id?.value;
|
|
141
|
+
// A tagged tuple, so no id or name, whatever characters it holds, can spell another kind's key.
|
|
142
|
+
if (id === undefined)
|
|
143
|
+
return JSON.stringify(['name', node.role, name]);
|
|
144
|
+
return JSON.stringify(named ? ['id+name', node.role, id, name] : ['id', node.role, id]);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Projects one snapshot. `idFor` turns a node's identity (its parent's id,
|
|
148
|
+
* and its own role and developer id or name) into the id the harness sees:
|
|
149
|
+
* the caller hands out the same id for the same identity across
|
|
150
|
+
* observations, so a control keeps its id for as long as it exists, as the
|
|
151
|
+
* contract asks. A node with a look-alike sibling has no identity
|
|
152
|
+
* (`undefined`) and gets a new id, which no later snapshot gives out again.
|
|
153
|
+
* The window is always `root`.
|
|
154
|
+
* Wrappers nothing reads (anonymous groups) are dropped
|
|
155
|
+
* and their children lifted, so the model reads the controls, not the
|
|
156
|
+
* scaffolding, and the window's title-bar buttons are left out.
|
|
157
|
+
*/
|
|
158
|
+
export function projectSnapshot(raw, idFor) {
|
|
159
|
+
const window = raw.tree.bounds ?? { x: 0, y: 0, width: 0, height: 0 };
|
|
160
|
+
const bindings = new Map();
|
|
161
|
+
const secureRects = [];
|
|
162
|
+
let unmaskable = false;
|
|
163
|
+
let scrollTarget;
|
|
164
|
+
const project = (node, id, stable) => {
|
|
165
|
+
const rawStates = new Set(node.states ?? []);
|
|
166
|
+
const role = ROLE_MAP[node.role] ?? (node.role === 'unknown' ? undefined : node.role);
|
|
167
|
+
const secure = rawStates.has('secure');
|
|
168
|
+
const states = {};
|
|
169
|
+
for (const state of STATE_NAMES)
|
|
170
|
+
if (rawStates.has(state))
|
|
171
|
+
states[state] = true;
|
|
172
|
+
if (rawStates.has('offscreen'))
|
|
173
|
+
states.hidden = true;
|
|
174
|
+
// A mixed checkbox is neither checked nor unchecked: no state, so check() and uncheck() always reach the control.
|
|
175
|
+
if (CHECKABLE_ROLES.has(node.role) && !rawStates.has('indeterminate'))
|
|
176
|
+
states.checked = rawStates.has('checked') || node.value === '1';
|
|
177
|
+
else if (rawStates.has('checked'))
|
|
178
|
+
states.checked = true;
|
|
179
|
+
if (node.role === 'combobox' || node.role === 'disclosure' || node.role === 'menubutton') {
|
|
180
|
+
if (node.available_actions?.includes('Expand') === true || node.available_actions?.includes('Collapse') === true)
|
|
181
|
+
states.expanded = rawStates.has('expanded');
|
|
182
|
+
}
|
|
183
|
+
const isText = TEXT_ROLES.has(node.role);
|
|
184
|
+
const name = label(node.name ?? (isText ? node.value : undefined));
|
|
185
|
+
// A control shows its name (a button's title) as its text, as on a device; an input's name is a label, not what it holds.
|
|
186
|
+
const text = isText ? label(node.value ?? node.name) : INPUT_ROLES.has(node.role) ? undefined : name;
|
|
187
|
+
// A checkbox's value is its state ("0", "1"), already in `checked`; a secure field's is never reported.
|
|
188
|
+
const value = secure || isText || CHECKABLE_ROLES.has(node.role) ? undefined : node.value;
|
|
189
|
+
const rect = node.bounds === undefined ? undefined : { x: node.bounds.x - window.x, y: node.bounds.y - window.y, width: node.bounds.width, height: node.bounds.height };
|
|
190
|
+
if (secure) {
|
|
191
|
+
// A box with no area masks nothing, so it proves nothing about a field that may still show.
|
|
192
|
+
if (rect === undefined || rect.width <= 0 || rect.height <= 0)
|
|
193
|
+
unmaskable = true;
|
|
194
|
+
else
|
|
195
|
+
secureRects.push(rect);
|
|
196
|
+
}
|
|
197
|
+
const attributes = {};
|
|
198
|
+
const placeholder = label(node.placeholder);
|
|
199
|
+
if (placeholder !== undefined)
|
|
200
|
+
attributes.placeholder = placeholder;
|
|
201
|
+
const hint = label(node.hint);
|
|
202
|
+
if (hint !== undefined)
|
|
203
|
+
attributes.hint = hint;
|
|
204
|
+
const description = label(node.description);
|
|
205
|
+
if (description !== undefined)
|
|
206
|
+
attributes.description = description;
|
|
207
|
+
if (node.native_id !== undefined)
|
|
208
|
+
attributes['native-id-kind'] = node.native_id.kind;
|
|
209
|
+
if (scrollTarget === undefined && node.ref_id !== undefined && SCROLLABLE_ROLES.has(node.role))
|
|
210
|
+
scrollTarget = id;
|
|
211
|
+
bindings.set(id, {
|
|
212
|
+
id,
|
|
213
|
+
...(node.ref_id === undefined ? {} : { ref: node.ref_id }),
|
|
214
|
+
...(role === undefined ? {} : { role }),
|
|
215
|
+
...(name === undefined ? {} : { name }),
|
|
216
|
+
states,
|
|
217
|
+
actions: node.available_actions ?? [],
|
|
218
|
+
...(raw.window === undefined ? {} : { windowId: raw.window.id }),
|
|
219
|
+
...(rect === undefined ? {} : { rect }),
|
|
220
|
+
stable,
|
|
221
|
+
});
|
|
222
|
+
const kept = [];
|
|
223
|
+
const collect = (child) => {
|
|
224
|
+
if (transparent(child))
|
|
225
|
+
for (const grandchild of child.children ?? [])
|
|
226
|
+
collect(grandchild);
|
|
227
|
+
else
|
|
228
|
+
kept.push(child);
|
|
229
|
+
};
|
|
230
|
+
for (const child of node.children ?? [])
|
|
231
|
+
if (id !== ROOT_ID || !windowControl(child))
|
|
232
|
+
collect(child);
|
|
233
|
+
const tally = (named) => {
|
|
234
|
+
const counts = new Map();
|
|
235
|
+
for (const child of kept)
|
|
236
|
+
counts.set(identityOf(child, named), (counts.get(identityOf(child, named)) ?? 0) + 1);
|
|
237
|
+
return counts;
|
|
238
|
+
};
|
|
239
|
+
const plain = tally(false);
|
|
240
|
+
const named = tally(true);
|
|
241
|
+
// Siblings sharing a developer id tell themselves apart by name. A
|
|
242
|
+
// look-alike that still matches has no identity: if the first of two
|
|
243
|
+
// "Delete" buttons goes, the second must not take over its id, so each
|
|
244
|
+
// look-alike gets a new id every snapshot.
|
|
245
|
+
const children = kept.map((child) => {
|
|
246
|
+
const identity = plain.get(identityOf(child)) === 1 ? identityOf(child) : named.get(identityOf(child, true)) === 1 ? identityOf(child, true) : undefined;
|
|
247
|
+
// A child's id is built on its parent's: under a look-alike, whose id is new every snapshot, so is the child's.
|
|
248
|
+
return project(child, idFor(identity === undefined ? undefined : `${id}/${identity}`), stable && identity !== undefined);
|
|
249
|
+
});
|
|
250
|
+
return {
|
|
251
|
+
ref: { id, revision: '' },
|
|
252
|
+
...(role === undefined ? {} : { role }),
|
|
253
|
+
...(name === undefined ? {} : { name }),
|
|
254
|
+
...(text === undefined ? {} : { text }),
|
|
255
|
+
...(value === undefined ? {} : { value }),
|
|
256
|
+
...(node.native_id === undefined || node.native_id.value === '' ? {} : { testId: node.native_id.value }),
|
|
257
|
+
...(secure ? { inputPurpose: 'password' } : {}),
|
|
258
|
+
...(Object.keys(states).length === 0 ? {} : { states }),
|
|
259
|
+
...(node.role === 'heading' ? headingLevel(node) : {}),
|
|
260
|
+
...(Object.keys(attributes).length === 0 ? {} : { attributes }),
|
|
261
|
+
...(rect === undefined ? {} : { rect }),
|
|
262
|
+
...(children.length === 0 ? {} : { children }),
|
|
263
|
+
};
|
|
264
|
+
};
|
|
265
|
+
const root = project(raw.tree, ROOT_ID, true);
|
|
266
|
+
return {
|
|
267
|
+
root,
|
|
268
|
+
bindings,
|
|
269
|
+
window,
|
|
270
|
+
viewport: { width: window.width, height: window.height },
|
|
271
|
+
secureRects: unmaskable ? undefined : secureRects,
|
|
272
|
+
truncated: raw.complete === false || raw.truncated === true || hasTruncatedSubtree(raw.tree),
|
|
273
|
+
...(raw.window === undefined ? {} : { windowId: raw.window.id }),
|
|
274
|
+
...(raw.window?.title === undefined || raw.window.title === '' ? {} : { title: raw.window.title }),
|
|
275
|
+
...(scrollTarget === undefined ? {} : { scrollTarget }),
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
/** A heading's level, when agent-desktop names it in the node's description (`heading level 2`). */
|
|
279
|
+
function headingLevel(node) {
|
|
280
|
+
const match = /\blevel (\d)\b/i.exec(node.description ?? '');
|
|
281
|
+
return match === null ? {} : { level: Number(match[1]) };
|
|
282
|
+
}
|
|
283
|
+
function hasTruncatedSubtree(node) {
|
|
284
|
+
if (node.subtree_truncated === true)
|
|
285
|
+
return true;
|
|
286
|
+
// At the depth limit agent-desktop counts the children it did not list.
|
|
287
|
+
if (node.children_count !== undefined && node.children_count > (node.children?.length ?? 0))
|
|
288
|
+
return true;
|
|
289
|
+
return (node.children ?? []).some(hasTruncatedSubtree);
|
|
290
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the desktop engine is given: its options, and the platform it runs on.
|
|
3
|
+
* The app under test is the target's `app` (`bundleId`, `appPath`,
|
|
4
|
+
* `launchArguments`); these options only say how agent-desktop drives it.
|
|
5
|
+
*/
|
|
6
|
+
/** The desktop operating system the engine drives, as its platform label: macOS only for now. */
|
|
7
|
+
export type DesktopPlatform = 'macos';
|
|
8
|
+
/**
|
|
9
|
+
* Options of the desktop engine. Every optional value also accepts
|
|
10
|
+
* `undefined`, so values read straight from `process.env` need no
|
|
11
|
+
* conditional spread.
|
|
12
|
+
*/
|
|
13
|
+
export interface DesktopOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Path to an agent-desktop executable to use instead of the pinned release
|
|
16
|
+
* the engine downloads on the first run. The executable's helper (on macOS
|
|
17
|
+
* `agent-desktop-macos-helper`) must sit beside it, as in a release archive
|
|
18
|
+
* or a Homebrew install.
|
|
19
|
+
*/
|
|
20
|
+
readonly binary?: string | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* What an observation captures. `full` (default) includes static text, so
|
|
23
|
+
* judgments and `toHaveText` can read labels; `interactive` keeps only the
|
|
24
|
+
* controls agent-desktop gives a ref.
|
|
25
|
+
*/
|
|
26
|
+
readonly snapshot?: 'full' | 'interactive' | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Allows physical input: the real pointer and keyboard, which take focus
|
|
29
|
+
* and move the cursor. Off by default: every action goes through the
|
|
30
|
+
* accessibility API and leaves the user's pointer and focus alone, and
|
|
31
|
+
* the app stays in the background. On, `app.open()` brings the app
|
|
32
|
+
* forward, which some apps need before they show a window, and the
|
|
33
|
+
* engine also offers `doubleTap`, `hover`, `press`, point-addressed taps,
|
|
34
|
+
* and keystrokes into whatever has focus (the keyboard capability).
|
|
35
|
+
*/
|
|
36
|
+
readonly headed?: boolean | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* How long the engine waits after an action the agent took before the agent
|
|
39
|
+
* observes again, in milliseconds; `false` skips the wait. A test's own
|
|
40
|
+
* steps never wait: `expect` polls their outcome. Default 150.
|
|
41
|
+
*/
|
|
42
|
+
readonly settle?: number | false | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* How many levels of a window's accessibility tree a read goes down, from
|
|
45
|
+
* 1 to 255. A read cut short at this depth cannot prove a control absent or
|
|
46
|
+
* the only one, so a locator on such a window reads again until its
|
|
47
|
+
* timeout: raise it for an app nested deeper. Default 24.
|
|
48
|
+
*/
|
|
49
|
+
readonly maxDepth?: number | undefined;
|
|
50
|
+
}
|
|
51
|
+
/** The options with defaults applied, checked once at config load. */
|
|
52
|
+
export interface ResolvedDesktopOptions {
|
|
53
|
+
readonly binary?: string;
|
|
54
|
+
readonly interactiveOnly: boolean;
|
|
55
|
+
readonly headed: boolean;
|
|
56
|
+
readonly settleMs: number;
|
|
57
|
+
readonly maxDepth: number;
|
|
58
|
+
}
|
|
59
|
+
/** Checks the option bag the way every config option is checked: unknown keys and wrong types fail the config load. */
|
|
60
|
+
export declare function resolveOptions(options: DesktopOptions | undefined): ResolvedDesktopOptions;
|
|
61
|
+
/**
|
|
62
|
+
* Fails on any OS but macOS. agent-desktop also runs on Windows and Linux,
|
|
63
|
+
* but there some of the engine's actions need physical input and secure
|
|
64
|
+
* fields are not always reported, so the engine refuses them until they are
|
|
65
|
+
* tested.
|
|
66
|
+
*/
|
|
67
|
+
export declare function requireMacOS(): void;
|
package/dist/options.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the desktop engine is given: its options, and the platform it runs on.
|
|
3
|
+
* The app under test is the target's `app` (`bundleId`, `appPath`,
|
|
4
|
+
* `launchArguments`); these options only say how agent-desktop drives it.
|
|
5
|
+
*/
|
|
6
|
+
import { ConfigurationError, rejectUnknownKeys } from '@e2e-dev/core/engine';
|
|
7
|
+
const OPTION_KEYS = Object.keys({ binary: true, snapshot: true, headed: true, settle: true, maxDepth: true });
|
|
8
|
+
const DEFAULT_SETTLE_MS = 150;
|
|
9
|
+
/** How deep a read goes by default: deeper than the controls of the apps tried, and quick to read. */
|
|
10
|
+
const DEFAULT_MAX_DEPTH = 24;
|
|
11
|
+
/** The deepest read agent-desktop takes. */
|
|
12
|
+
const MAX_DEPTH_LIMIT = 255;
|
|
13
|
+
/** Checks the option bag the way every config option is checked: unknown keys and wrong types fail the config load. */
|
|
14
|
+
export function resolveOptions(options) {
|
|
15
|
+
const bag = options ?? {};
|
|
16
|
+
if (typeof bag !== 'object' || bag === null)
|
|
17
|
+
throw new ConfigurationError('INVALID_CONFIG', 'desktop() options must be an object');
|
|
18
|
+
rejectUnknownKeys('desktop() options', bag, OPTION_KEYS);
|
|
19
|
+
const { binary, snapshot, headed, settle, maxDepth } = bag;
|
|
20
|
+
if (binary !== undefined && (typeof binary !== 'string' || binary === '')) {
|
|
21
|
+
throw new ConfigurationError('INVALID_CONFIG', 'desktop({ binary }) must be a path to an agent-desktop executable');
|
|
22
|
+
}
|
|
23
|
+
if (snapshot !== undefined && snapshot !== 'full' && snapshot !== 'interactive') {
|
|
24
|
+
throw new ConfigurationError('INVALID_CONFIG', `desktop({ snapshot }) must be 'full' or 'interactive', got ${JSON.stringify(snapshot)}`);
|
|
25
|
+
}
|
|
26
|
+
if (headed !== undefined && typeof headed !== 'boolean') {
|
|
27
|
+
throw new ConfigurationError('INVALID_CONFIG', 'desktop({ headed }) must be a boolean');
|
|
28
|
+
}
|
|
29
|
+
if (settle !== undefined && settle !== false && (!Number.isInteger(settle) || settle < 0)) {
|
|
30
|
+
throw new ConfigurationError('INVALID_CONFIG', 'desktop({ settle }) must be a whole number of milliseconds, or false');
|
|
31
|
+
}
|
|
32
|
+
if (maxDepth !== undefined && (!Number.isInteger(maxDepth) || maxDepth < 1 || maxDepth > MAX_DEPTH_LIMIT)) {
|
|
33
|
+
throw new ConfigurationError('INVALID_CONFIG', `desktop({ maxDepth }) must be a whole number from 1 to ${MAX_DEPTH_LIMIT}`);
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
...(binary === undefined ? {} : { binary }),
|
|
37
|
+
interactiveOnly: snapshot === 'interactive',
|
|
38
|
+
headed: headed === true,
|
|
39
|
+
settleMs: settle === false ? 0 : (settle ?? DEFAULT_SETTLE_MS),
|
|
40
|
+
maxDepth: maxDepth ?? DEFAULT_MAX_DEPTH,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Fails on any OS but macOS. agent-desktop also runs on Windows and Linux,
|
|
45
|
+
* but there some of the engine's actions need physical input and secure
|
|
46
|
+
* fields are not always reported, so the engine refuses them until they are
|
|
47
|
+
* tested.
|
|
48
|
+
*/
|
|
49
|
+
export function requireMacOS() {
|
|
50
|
+
if (process.platform !== 'darwin') {
|
|
51
|
+
throw new ConfigurationError('INVALID_CONFIG', `desktop() drives macOS apps only for now; this machine runs ${process.platform}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
package/dist/png.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Rect } from './nodes.ts';
|
|
2
|
+
/** An 8-bit RGBA image, rows top to bottom. */
|
|
3
|
+
export interface Rgba {
|
|
4
|
+
readonly width: number;
|
|
5
|
+
readonly height: number;
|
|
6
|
+
readonly data: Uint8Array;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The window's own pixels out of a window capture, every rectangle (in the
|
|
10
|
+
* window's image pixels) painted opaque black. macOS draws the window's
|
|
11
|
+
* shadow into the capture, unevenly on each side, so the window is found as
|
|
12
|
+
* the box of fully opaque pixels, which the shadow never is, and cropped to.
|
|
13
|
+
* A capture within a pixel of `expected` (the window's size in image
|
|
14
|
+
* pixels) is the window already; any other is cropped to its opaque box,
|
|
15
|
+
* which must then be within a pixel of it. Undefined otherwise: the
|
|
16
|
+
* rectangles would land somewhere else, and an image whose masking cannot
|
|
17
|
+
* be proven must not leave the engine.
|
|
18
|
+
*/
|
|
19
|
+
export declare function windowImage(bytes: Uint8Array, expected: {
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
}, rects: readonly Rect[]): Rgba | undefined;
|
|
23
|
+
/** PNG bytes of an image. */
|
|
24
|
+
export declare function encodePng(image: Rgba): Uint8Array;
|
package/dist/png.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/** Screenshot masking: a window capture decoded with @napi-rs/image, cropped to the window, its secure regions painted black, encoded again; the layout code turns the addon's pixels into 8-bit RGBA. */
|
|
2
|
+
import { endianness } from 'node:os';
|
|
3
|
+
import { FilterType, JsColorType, Transformer } from '@napi-rs/image';
|
|
4
|
+
const LITTLE_ENDIAN = endianness() === 'LE';
|
|
5
|
+
/** Channels per pixel, and whether each is a 16-bit sample, for every color type a decode reads: 8-bit RGB is widened by the addon before that. */
|
|
6
|
+
const LAYOUTS = {
|
|
7
|
+
[JsColorType.L8]: { channels: 1, wide: false },
|
|
8
|
+
[JsColorType.La8]: { channels: 2, wide: false },
|
|
9
|
+
[JsColorType.Rgba8]: { channels: 4, wide: false },
|
|
10
|
+
[JsColorType.L16]: { channels: 1, wide: true },
|
|
11
|
+
[JsColorType.La16]: { channels: 2, wide: true },
|
|
12
|
+
[JsColorType.Rgb16]: { channels: 3, wide: true },
|
|
13
|
+
[JsColorType.Rgba16]: { channels: 4, wide: true },
|
|
14
|
+
};
|
|
15
|
+
/** How far, in image pixels, the window in a capture may differ from the size its bounds give: points times scale, rounded. */
|
|
16
|
+
const SIZE_TOLERANCE = 1;
|
|
17
|
+
/** How far each mask reaches past its field, in image pixels, so a field off by the tolerance is still covered. */
|
|
18
|
+
const MASK_MARGIN = 2;
|
|
19
|
+
/**
|
|
20
|
+
* The window's own pixels out of a window capture, every rectangle (in the
|
|
21
|
+
* window's image pixels) painted opaque black. macOS draws the window's
|
|
22
|
+
* shadow into the capture, unevenly on each side, so the window is found as
|
|
23
|
+
* the box of fully opaque pixels, which the shadow never is, and cropped to.
|
|
24
|
+
* A capture within a pixel of `expected` (the window's size in image
|
|
25
|
+
* pixels) is the window already; any other is cropped to its opaque box,
|
|
26
|
+
* which must then be within a pixel of it. Undefined otherwise: the
|
|
27
|
+
* rectangles would land somewhere else, and an image whose masking cannot
|
|
28
|
+
* be proven must not leave the engine.
|
|
29
|
+
*/
|
|
30
|
+
export function windowImage(bytes, expected, rects) {
|
|
31
|
+
const image = decodeRgba(bytes);
|
|
32
|
+
const fits = (width, height) => Math.abs(width - expected.width) <= SIZE_TOLERANCE && Math.abs(height - expected.height) <= SIZE_TOLERANCE;
|
|
33
|
+
let window = image;
|
|
34
|
+
if (!fits(image.width, image.height)) {
|
|
35
|
+
const box = opaqueBox(image);
|
|
36
|
+
if (box === undefined || !fits(box.width, box.height))
|
|
37
|
+
return undefined;
|
|
38
|
+
window = crop(image, box);
|
|
39
|
+
}
|
|
40
|
+
for (const rect of rects) {
|
|
41
|
+
const x0 = Math.max(0, Math.floor(rect.x) - MASK_MARGIN);
|
|
42
|
+
const y0 = Math.max(0, Math.floor(rect.y) - MASK_MARGIN);
|
|
43
|
+
const x1 = Math.min(window.width, Math.ceil(rect.x + rect.width) + MASK_MARGIN);
|
|
44
|
+
const y1 = Math.min(window.height, Math.ceil(rect.y + rect.height) + MASK_MARGIN);
|
|
45
|
+
for (let y = y0; y < y1; y += 1) {
|
|
46
|
+
for (let x = x0; x < x1; x += 1) {
|
|
47
|
+
const at = (y * window.width + x) * 4;
|
|
48
|
+
window.data[at] = 0;
|
|
49
|
+
window.data[at + 1] = 0;
|
|
50
|
+
window.data[at + 2] = 0;
|
|
51
|
+
window.data[at + 3] = 255;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return window;
|
|
56
|
+
}
|
|
57
|
+
/** PNG bytes of an image. */
|
|
58
|
+
export function encodePng(image) {
|
|
59
|
+
return encodeRgba(image);
|
|
60
|
+
}
|
|
61
|
+
/** The smallest box holding every fully opaque pixel; undefined when there is none. */
|
|
62
|
+
function opaqueBox(image) {
|
|
63
|
+
let left = image.width;
|
|
64
|
+
let top = image.height;
|
|
65
|
+
let right = -1;
|
|
66
|
+
let bottom = -1;
|
|
67
|
+
for (let y = 0; y < image.height; y += 1) {
|
|
68
|
+
for (let x = 0; x < image.width; x += 1) {
|
|
69
|
+
if (image.data[(y * image.width + x) * 4 + 3] !== 255)
|
|
70
|
+
continue;
|
|
71
|
+
if (x < left)
|
|
72
|
+
left = x;
|
|
73
|
+
if (x > right)
|
|
74
|
+
right = x;
|
|
75
|
+
if (y < top)
|
|
76
|
+
top = y;
|
|
77
|
+
if (y > bottom)
|
|
78
|
+
bottom = y;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return right < 0 ? undefined : { x: left, y: top, width: right - left + 1, height: bottom - top + 1 };
|
|
82
|
+
}
|
|
83
|
+
function crop(image, box) {
|
|
84
|
+
const data = new Uint8Array(box.width * box.height * 4);
|
|
85
|
+
for (let row = 0; row < box.height; row += 1) {
|
|
86
|
+
const from = ((box.y + row) * image.width + box.x) * 4;
|
|
87
|
+
data.set(image.data.subarray(from, from + box.width * 4), row * box.width * 4);
|
|
88
|
+
}
|
|
89
|
+
return { width: box.width, height: box.height, data };
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Decodes PNG bytes to 8-bit RGBA, whatever the color type, bit depth, or
|
|
93
|
+
* palette; throws on bytes that are not a PNG. An 8-bit RGB screenshot is
|
|
94
|
+
* widened by the addon: opacity 1 keeps every sample and adds the opaque
|
|
95
|
+
* alpha channel, several times faster than `spread` does it.
|
|
96
|
+
*/
|
|
97
|
+
function decodeRgba(bytes) {
|
|
98
|
+
const decoder = new Transformer(bytes);
|
|
99
|
+
const { width, height, format, colorType } = decoder.metadataSync();
|
|
100
|
+
if (format !== 'png')
|
|
101
|
+
throw new Error(`not a PNG: the bytes are ${format}`);
|
|
102
|
+
const rgb = colorType === JsColorType.Rgb8;
|
|
103
|
+
const raw = (rgb ? decoder.opacity(1) : decoder).rawPixelsSync();
|
|
104
|
+
const layout = LAYOUTS[rgb ? JsColorType.Rgba8 : colorType];
|
|
105
|
+
if (layout === undefined)
|
|
106
|
+
throw new Error(`unsupported PNG color type ${colorType}`);
|
|
107
|
+
const { channels, wide } = layout;
|
|
108
|
+
if (raw.length !== width * height * channels * (wide ? 2 : 1))
|
|
109
|
+
throw new Error('the decoded PNG pixels do not fill its size');
|
|
110
|
+
const samples = new Uint8Array(raw.buffer, raw.byteOffset, raw.byteLength);
|
|
111
|
+
if (channels === 4 && !wide)
|
|
112
|
+
return { width, height, data: samples };
|
|
113
|
+
return { width, height, data: spread(wide ? narrow(samples) : samples, channels, width * height) };
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Encodes an image as PNG bytes, each row adaptively filtered: the addon's
|
|
117
|
+
* default writes no filter, which makes screenshots markedly larger.
|
|
118
|
+
*/
|
|
119
|
+
function encodeRgba(image) {
|
|
120
|
+
const png = Transformer.fromRgbaPixels(image.data, image.width, image.height).pngSync({ filterType: FilterType.Adaptive });
|
|
121
|
+
return new Uint8Array(png.buffer, png.byteOffset, png.byteLength);
|
|
122
|
+
}
|
|
123
|
+
/** 16-bit samples in native byte order as 8-bit samples, rounded. */
|
|
124
|
+
function narrow(bytes) {
|
|
125
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
126
|
+
const samples = new Uint8Array(bytes.length / 2);
|
|
127
|
+
for (let at = 0; at < samples.length; at += 1)
|
|
128
|
+
samples[at] = Math.round((view.getUint16(at * 2, LITTLE_ENDIAN) * 255) / 65535);
|
|
129
|
+
return samples;
|
|
130
|
+
}
|
|
131
|
+
/** 8-bit samples of `channels` per pixel (gray, gray and alpha, RGB) as RGBA; a gray or RGB image is opaque. */
|
|
132
|
+
function spread(samples, channels, pixels) {
|
|
133
|
+
const out = new Uint8Array(pixels * 4);
|
|
134
|
+
const gray = channels <= 2;
|
|
135
|
+
const alpha = channels === 2 || channels === 4;
|
|
136
|
+
for (let pixel = 0, from = 0, to = 0; pixel < pixels; pixel += 1, from += channels, to += 4) {
|
|
137
|
+
const r = samples[from];
|
|
138
|
+
out[to] = r;
|
|
139
|
+
out[to + 1] = gray ? r : samples[from + 1];
|
|
140
|
+
out[to + 2] = gray ? r : samples[from + 2];
|
|
141
|
+
out[to + 3] = alpha ? samples[from + channels - 1] : 255;
|
|
142
|
+
}
|
|
143
|
+
return out;
|
|
144
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The test runner's `test`, which the main entry extends, loaded only when
|
|
3
|
+
* `e2e` is installed. `e2e` is an optional peer: a program on
|
|
4
|
+
* `@e2e-dev/core` alone imports `desktop` from this package too.
|
|
5
|
+
*/
|
|
6
|
+
import type { test as RunnerTest } from 'e2e';
|
|
7
|
+
type Test = typeof RunnerTest;
|
|
8
|
+
/**
|
|
9
|
+
* `e2e`'s `test`, or, when `e2e` is not installed, a stand-in whose every
|
|
10
|
+
* call and member throws `RUNNER_NOT_INSTALLED`. Loaded with `require()`
|
|
11
|
+
* rather than a top-level `await import()`, so this entry stays loadable by
|
|
12
|
+
* `require()` from a CommonJS file.
|
|
13
|
+
*/
|
|
14
|
+
export declare function runnerTest(): Test;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The test runner's `test`, which the main entry extends, loaded only when
|
|
3
|
+
* `e2e` is installed. `e2e` is an optional peer: a program on
|
|
4
|
+
* `@e2e-dev/core` alone imports `desktop` from this package too.
|
|
5
|
+
*/
|
|
6
|
+
import { createRequire } from 'node:module';
|
|
7
|
+
import { ConfigurationError } from '@e2e-dev/core/engine';
|
|
8
|
+
const ownRequire = createRequire(import.meta.url);
|
|
9
|
+
/**
|
|
10
|
+
* `e2e`'s `test`, or, when `e2e` is not installed, a stand-in whose every
|
|
11
|
+
* call and member throws `RUNNER_NOT_INSTALLED`. Loaded with `require()`
|
|
12
|
+
* rather than a top-level `await import()`, so this entry stays loadable by
|
|
13
|
+
* `require()` from a CommonJS file.
|
|
14
|
+
*/
|
|
15
|
+
export function runnerTest() {
|
|
16
|
+
try {
|
|
17
|
+
ownRequire.resolve('e2e');
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
// An installed `e2e` whose entry file is gone throws MODULE_NOT_FOUND too, naming that file: rethrown, not reported as not installed.
|
|
21
|
+
if (error.code === 'MODULE_NOT_FOUND' && error.message.startsWith("Cannot find module 'e2e'"))
|
|
22
|
+
return missingRunner();
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
return ownRequire('e2e').test;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A `test` whose every call, and every member's call, throws
|
|
29
|
+
* `RUNNER_NOT_INSTALLED`, except `extend`, which returns it again so the
|
|
30
|
+
* main entry can extend it while it loads.
|
|
31
|
+
*/
|
|
32
|
+
function missingRunner() {
|
|
33
|
+
const fail = () => {
|
|
34
|
+
throw new ConfigurationError('RUNNER_NOT_INSTALLED', "`test` from '@e2e-dev/desktop' needs e2e, the test runner, which is not installed. Install e2e to write tests, or import desktop() from '@e2e-dev/desktop/engine' to drive a desktop app with @e2e-dev/core.");
|
|
35
|
+
};
|
|
36
|
+
// oxlint-disable-next-line e2e/no-chained-type-assertions -- the proxy answers every call and member of `test`, so it has its shape by construction
|
|
37
|
+
const missing = new Proxy(fail, {
|
|
38
|
+
get: (target, key) => {
|
|
39
|
+
if (key === 'extend')
|
|
40
|
+
return () => missing;
|
|
41
|
+
return typeof key === 'string' && !Reflect.has(target, key) ? fail : Reflect.get(target, key);
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
return missing;
|
|
45
|
+
}
|