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
6 changes: 3 additions & 3 deletions packages/command-surface/src/capture/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ function expectInvalidArgs(run: () => unknown, message: string): void {

describe('settings CLI', () => {
test('reads and writes settings input', () => {
const input = settingsCliReader(['permission', 'grant', 'camera', 'limited'], flags());
const input = settingsCliReader(['permission', 'grant', 'photos', 'limited'], flags());
expect(input).toMatchObject({
setting: 'permission',
state: 'grant',
permission: 'camera',
permission: 'photos',
mode: 'limited',
});
expect(settingsDaemonWriter(input)).toMatchObject({
command: 'settings',
positionals: ['permission', 'grant', 'camera', 'limited'],
positionals: ['permission', 'grant', 'photos', 'limited'],
});
});

Expand Down
7 changes: 6 additions & 1 deletion packages/contracts/src/settings-catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ export const SETTINGS = {
{
kind: 'permission',
actions: PERMISSION_ACTIONS,
targets: MOBILE_PERMISSION_TARGETS,
targets: MOBILE_PERMISSION_TARGETS.filter((target) => target !== 'photos'),
},
{
kind: 'permission',
actions: PERMISSION_ACTIONS,
targets: ['photos'],
modes: PERMISSION_MODES,
},
{ kind: 'permission', actions: ['grant', 'reset'], targets: MACOS_PERMISSION_TARGETS },
Expand Down
47 changes: 45 additions & 2 deletions packages/contracts/src/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ describe('settings usage', () => {
'settings fingerprint <match|nonmatch>',
'settings clear-app-state [app-id]',
'settings reset-keychain clear',
`settings permission <grant|deny|reset> <${MOBILE_PERMISSION_TARGETS.join('|')}> [full|limited]`,
'settings permission <grant|deny|reset> <all|camera|microphone|contacts|contacts-limited|notifications|calendar|location|location-always|media-library|motion|reminders|siri>',
'settings permission <grant|deny|reset> photos [full|limited]',
'settings permission <grant|reset> <accessibility|screen-recording|input-monitoring>',
]);
});
Expand Down Expand Up @@ -119,7 +120,7 @@ describe('parseSettingsMutation', () => {
});
expectInvalidArgs(
() => parseSettingsMutation(['permission', 'grant', 'bluetooth']),
'settings permission requires a target:',
'settings permission requires a target: all|camera|microphone|contacts|contacts-limited|notifications|calendar|location|location-always|media-library|motion|reminders|siri|photos|accessibility|screen-recording|input-monitoring',
);
expectInvalidArgs(
() => parseSettingsMutation(['permission', 'grant', 'photos', 'some']),
Expand All @@ -138,6 +139,48 @@ describe('parseSettingsMutation', () => {
);
});

test('only photos takes a mode among the mobile targets', () => {
expectInvalidArgs(
() => parseSettingsMutation(['permission', 'grant', 'camera', 'limited']),
'settings permission camera takes no mode.',
);
});

test('an app id in the mode slot is refused with a typed reason that names the app', () => {
const appPositional = (permission: string, app: string) =>
expect.objectContaining({
code: 'INVALID_ARGS',
details: expect.objectContaining({
reason: 'setting_permission_app_positional',
dispatched: 'no',
permission,
app,
hint: `Pass the app with --app ${app}.`,
}),
});
for (const positionals of [
['permission', 'deny', 'notifications', 'com.example.app'],
['permission', 'grant', 'photos', 'com.example.app'],
['permission', 'grant', 'photos', 'limited', 'com.example.app'],
]) {
expect(() => parseSettingsMutation(positionals)).toThrow(
appPositional(positionals[2]!, 'com.example.app'),
);
}
});

test('a macOS target takes no --app, so a dotted value gets no --app spelling', () => {
expect(() =>
parseSettingsMutation(['permission', 'grant', 'accessibility', 'com.example.app']),
).toThrow(
expect.objectContaining({
code: 'INVALID_ARGS',
message: 'settings permission accessibility takes no mode.',
details: undefined,
}),
);
});

test('clear-app-state reads its app with or without the clear word', () => {
expect(parseSettingsMutation(['clear-app-state', 'com.Example.App'])).toEqual({
setting: 'clear-app-state',
Expand Down
46 changes: 33 additions & 13 deletions packages/contracts/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ const SETTINGS_REFUSAL_REASONS = {
*/
// The asked-for setting applies to the target itself, so the named app consumes nothing.
appNotConsumed: { reason: 'setting_app_not_consumed' },
// A permission's trailing positional reads as a bundle id or package, which belongs in --app.
permissionAppPositional: { reason: 'setting_permission_app_positional' },
} as const satisfies ReasonTable<'settings'>;

/**
Expand Down Expand Up @@ -211,7 +213,8 @@ function formUsage(form: SettingForm, readable: boolean): string {
return 'set <lat> <lon>';
case 'permission': {
const modes = form.modes ? ` [${form.modes.join('|')}]` : '';
return `<${form.actions.join('|')}> <${form.targets.join('|')}>${modes}`;
const targets = form.targets.length === 1 ? form.targets[0] : `<${form.targets.join('|')}>`;
return `<${form.actions.join('|')}> ${targets}${modes}`;
}
case 'app-id':
return '[app-id]';
Expand Down Expand Up @@ -312,13 +315,15 @@ function parsePermissionPositionals(
const forms = definition.forms.filter(
(form): form is Extract<SettingForm, { kind: 'permission' }> => form.kind === 'permission',
);
const targets = forms.flatMap((form) => form.targets);
const permission = findVocabularyName(targets, positionals[2]);
const permission = findVocabularyName(
forms.flatMap((form) => form.targets),
positionals[2],
);
const form = forms.find((candidate) => candidate.targets.includes(permission ?? ''));
if (permission === undefined || form === undefined) {
throw new AppError(
'INVALID_ARGS',
`settings permission requires a target: ${targets.join('|')}`,
`settings permission requires a target: ${forms.flatMap((candidate) => candidate.targets).join('|')}`,
);
}
if (findVocabularyName(form.actions, action) === undefined) {
Expand All @@ -329,15 +334,30 @@ function parsePermissionPositionals(
}
if (positionals.length === 3) return { permission };
const mode = findVocabularyName(form.modes ?? [], positionals[3]);
if (mode === undefined || positionals.length > 4) {
throw new AppError(
'INVALID_ARGS',
form.modes === undefined
? `settings permission ${permission} takes no mode.`
: `settings permission mode must be ${form.modes.join('|')}.`,
);
}
return { permission, mode };
if (mode !== undefined && positionals.length === 4) return { permission, mode };
const refusal = permissionModeRefusal(permission, form.modes, mode);
// A dotted value reads as a bundle id or package, which only an app-scoped target takes.
const app = (MOBILE_PERMISSION_TARGETS as readonly string[]).includes(permission)
? positionals.slice(3).find((value) => value.includes('.'))
: undefined;
if (app === undefined) throw new AppError('INVALID_ARGS', refusal);
throw new AppError('INVALID_ARGS', refusal, {
reason: SETTINGS_REFUSAL_REASONS.permissionAppPositional.reason,
dispatched: 'no',
permission,
app,
hint: `Pass the app with --app ${app}.`,
});
}

function permissionModeRefusal(
permission: string,
modes: readonly string[] | undefined,
mode: string | undefined,
): string {
if (modes === undefined) return `settings permission ${permission} takes no mode.`;
if (mode === undefined) return `settings permission mode must be ${modes.join('|')}.`;
return `settings permission ${permission} takes one mode.`;
}

function invalidSettingsArgs(): AppError {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/schema/cli-help-command-usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,9 @@ test('settings usage documents canonical faceid states', async () => {
assert.match(help, /match\|nonmatch\|enroll\|unenroll/);
assert.match(
help,
/camera\|microphone\|photos\|contacts\|contacts-limited\|notifications\|calendar\|location\|location-always\|media-library\|motion\|reminders\|siri/,
/camera\|microphone\|contacts\|contacts-limited\|notifications\|calendar\|location\|location-always\|media-library\|motion\|reminders\|siri>/,
);
assert.match(help, /permission <grant\|deny\|reset> photos \[full\|limited\]/);
assert.doesNotMatch(help, /validate\|unvalidate/);
});

Expand Down
2 changes: 1 addition & 1 deletion website/docs/docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,7 @@ agent-device trigger-app-event screenshot_taken '{"source":"qa"}'
Change OS settings and app permissions.

```bash
agent-device settings <wifi|bluetooth|airplane|mobile-data|location|animations> <on|off> | settings location set <lat> <lon> | settings appearance <light|dark|toggle> | settings text-size [extra-small|small|medium|large|extra-large|extra-extra-large|extra-extra-extra-large|accessibility-medium|accessibility-large|accessibility-extra-large|accessibility-extra-extra-large|accessibility-extra-extra-extra-large] | settings <faceid|touchid> <match|nonmatch|enroll|unenroll> | settings fingerprint <match|nonmatch> | settings clear-app-state [app-id] | settings reset-keychain clear | settings permission <grant|deny|reset> <all|camera|microphone|photos|contacts|contacts-limited|notifications|calendar|location|location-always|media-library|motion|reminders|siri> [full|limited] | settings permission <grant|reset> <accessibility|screen-recording|input-monitoring> [--app <id-or-name>]
agent-device settings <wifi|bluetooth|airplane|mobile-data|location|animations> <on|off> | settings location set <lat> <lon> | settings appearance <light|dark|toggle> | settings text-size [extra-small|small|medium|large|extra-large|extra-extra-large|extra-extra-extra-large|accessibility-medium|accessibility-large|accessibility-extra-large|accessibility-extra-extra-large|accessibility-extra-extra-extra-large] | settings <faceid|touchid> <match|nonmatch|enroll|unenroll> | settings fingerprint <match|nonmatch> | settings clear-app-state [app-id] | settings reset-keychain clear | settings permission <grant|deny|reset> <all|camera|microphone|contacts|contacts-limited|notifications|calendar|location|location-always|media-library|motion|reminders|siri> | settings permission <grant|deny|reset> photos [full|limited] | settings permission <grant|reset> <accessibility|screen-recording|input-monitoring> [--app <id-or-name>]
```

| Option | What it does |
Expand Down
1 change: 1 addition & 0 deletions website/docs/docs/error-reasons.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ Treat a reason that this page does not list as unknown, not as an error in the r
| Reason | Meaning |
| --- | --- |
| `setting_app_not_consumed` | The asked-for setting applies to the target itself, so the named app consumes nothing. |
| `setting_permission_app_positional` | A permission's trailing positional reads as a bundle id or package, which belongs in --app. |
| `android_bluetooth_adapter_missing` | The device declares no Bluetooth adapter, so there is nothing to turn on or off. |
| `android_bluetooth_command_unavailable` | The build predates `svc bluetooth`, so the adapter cannot be switched from the shell. |
| `bluetooth-not-applied` | The device kept Bluetooth in its prior state after `svc bluetooth` accepted the request. |
Expand Down
Loading