Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 79 additions & 5 deletions src/commands/schema/cli-help-selectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ import path from 'node:path';
import { test } from 'vitest';
import { SELECTOR_KEY_NAMES } from '@agent-device/selectors';
import type { SnapshotNode } from '@agent-device/kernel/snapshot';
import {
isCommandName,
listCommandMetadata,
readInputFromCli,
} from '@agent-device/command-surface';
import { parseArgs } from '../../cli/parser/args.ts';
import { listSelectorChainMatches, parseSelectorChain } from '../../sdk/selectors.ts';
import { selectorsHelpTopic } from './cli-help-selectors.ts';

// `help selectors` and selectors.md state the selector rules in hand-written prose. These tests pin
// the selectors those texts show: each command example parses, each rule example still appears in
// both texts and gets the result the test expects from the parser and matcher, and each parser key
// is the subject of a rule. They do not read the prose around an example, so a reworded rule that
// keeps its example is not caught.
// both texts and gets the result the test expects from the parser and matcher, each parser key
// is the subject of a rule, and the commands named as taking a selector match the command surface.
// They do not read the prose around an example, so a reworded rule that keeps its example is not
// caught.

const HELP = selectorsHelpTopic.body;
const DOC = fs.readFileSync(
Expand Down Expand Up @@ -77,12 +84,79 @@ test('every selector key has a table row or a Boolean keys entry in selectors.md
}
});

const SELECTOR = 'label="Photo"';

/** Commands whose input takes its UI target as a `selector` field. */
function selectorTargetCommands(): string[] {
return listCommandMetadata()
.filter((meta) => 'selector' in (meta.inputSchema.properties ?? {}))
.map((meta) => meta.name)
.sort();
}

test('both texts list exactly the commands whose input has a selector target', () => {
const helpList = /\. ([a-z, ]+) take a selector as the target/.exec(HELP)?.[1] ?? '';
const docLine = DOC.split('\n').find((line) => line.endsWith(', as the target')) ?? '';
const docList = [...docLine.matchAll(/`([a-z]+)`/g)].map(([, name]) => name);
assert.deepEqual(helpList.split(/,? and |, /).sort(), selectorTargetCommands());
assert.deepEqual(docList.sort(), selectorTargetCommands());
});

// The other inputs that take a selector, each with the CLI spelling the texts use for it.
const OTHER_SELECTOR_INPUTS = [
{ spelling: 'find', field: 'query', argv: ['find', SELECTOR, 'exists'] },
{ spelling: 'scroll --until', field: 'until', argv: ['scroll', 'down', '--until', SELECTOR] },
{
spelling: 'screenshot --crop-on',
field: 'cropOn',
argv: ['screenshot', '--crop-on', SELECTOR],
},
{ spelling: 'gesture pinch', field: 'element', argv: ['gesture', 'pinch', '2', SELECTOR] },
{ spelling: 'gesture drag', field: 'source', argv: ['gesture', 'drag', SELECTOR, 'id="bin"'] },
{
spelling: 'gesture drag',
field: 'destination',
argv: ['gesture', 'drag', 'id="bin"', SELECTOR],
},
] as const;

test.each(OTHER_SELECTOR_INPUTS)(
'$spelling reads a selector into $field and both texts name it',
({ spelling, field, argv }) => {
const parsed = parseArgs([...argv], { strictFlags: true });
assert.ok(parsed.command && isCommandName(parsed.command));
const input = readInputFromCli(parsed.command, parsed.positionals, parsed.flags);
assert.equal(input[field], SELECTOR);
for (const [source, text] of Object.entries(SOURCES)) {
assert.ok(text.includes(spelling), `${source} does not name ${spelling}`);
}
},
);

// A selector-valued field documents itself as one. The target fields, and the snapshot options that
// steer target resolution, mention selectors without taking one.
const NOT_SELECTOR_VALUED = new Set(['selector', 'ref', 'x', 'y', 'scope', 'raw']);

test('every other input field described as a selector is listed above', () => {
const listed = new Set(OTHER_SELECTOR_INPUTS.map(({ field, argv }) => `${argv[0]}.${field}`));
for (const meta of listCommandMetadata()) {
for (const [field, schema] of Object.entries(meta.inputSchema.properties ?? {})) {
if (NOT_SELECTOR_VALUED.has(field)) continue;
const description = (schema as { description?: string }).description ?? '';
if (!/selector/i.test(description)) continue;
const name = `${meta.name}.${field}`;
assert.ok(listed.has(name), `${name} takes a selector but is not listed`);
}
}
});

const RULE_EXAMPLES: ReadonlyArray<{ example: string; check: (example: string) => void }> = [
{
// A bare boolean key is a term of its own.
example: 'checked',
// A bare boolean key is a term of its own, the same as key=true.
example: 'role=switch checked',
check: (example) =>
assert.deepEqual(parseSelectorChain(example).selectors[0]?.terms, [
{ key: 'role', value: 'switch' },
{ key: 'checked', value: true },
]),
},
Expand Down
2 changes: 1 addition & 1 deletion src/commands/schema/cli-help-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const selectorsHelpTopic = {
Selector syntax for commands that take a target. press, click, fill, longpress, hover, get, is, and wait take a selector as the target, gesture drag as its source and destination, and gesture pinch as the element to pinch. find takes one as its query, and scroll --until and screenshot --crop-on as the flag value.

Shape:
One or more terms separated by spaces. An element must match every term. A term is key=value, or a boolean key alone, which means key=true: checked is checked=true. Wrap the whole expression in single quotes so the shell passes it as one argument:
One or more terms separated by spaces. An element must match every term. A term is key=value, or a boolean key alone, which means key=true: role=switch checked is role=switch checked=true. Wrap the whole expression in single quotes so the shell passes it as one argument:
agent-device press 'role=button label="Continue"'
agent-device fill 'label="Email" editable=true' "qa@example.com"
agent-device wait 'id="checkout-total"' 5000
Expand Down
9 changes: 4 additions & 5 deletions website/docs/docs/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ Tips:
## Selector syntax

A selector expression is one or more terms separated by spaces, and an element must match every
term. A term is `key=value`, or a boolean key alone, which means `key=true`: `checked` is
`checked=true`. `agent-device help selectors` prints this syntax.
term. A term is `key=value`, or a boolean key alone, which means `key=true`: `role=switch checked`
is `role=switch checked=true`. `agent-device help selectors` prints this syntax.

These commands take a selector:

Expand Down Expand Up @@ -103,9 +103,8 @@ Text keys:
| `appname` | The owning app's name |
| `windowtitle` | The owning window's title |

Linux snapshots and the macOS `desktop`, `frontmost-app`, and `menubar` surfaces report `appname`
and `windowtitle`. A macOS app session reports them only with `AGENT_DEVICE_MACOS_APP_BACKEND=native`.
Elsewhere a non-empty value matches nothing.
Not every snapshot reports `appname` and `windowtitle`; `agent-device help selectors` lists the
ones that do.

Text values match the whole field, ignoring case and extra whitespace: `label="sign in"` matches
`Sign In` but not `Sign In Now`. For partial matches, use `find`.
Expand Down
Loading