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
5 changes: 5 additions & 0 deletions .changeset/set-permission-validate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@e2e-dev/mobile": patch
---

`device.setPermission` rejects an unknown permission name or state with `INVALID_ARGUMENT`, naming the nearest permission for a typo.
4 changes: 4 additions & 0 deletions docs/reference/mobile.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,10 @@ await app.open();
- `permission` is a [`DevicePermission`](#device-fixture); `state` is
`grant`, `deny`, or `reset`.

| Throws | When |
| --- | --- |
| `INVALID_ARGUMENT` | A permission name outside `DevicePermission` (naming the nearest one when it is a likely typo), or a state other than `grant`, `deny`, or `reset`. Fails before any device command. |

#### `setLocation`

```ts
Expand Down
37 changes: 29 additions & 8 deletions packages/mobile/src/surface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,22 +345,38 @@ const PERMISSION_STATES: readonly string[] = Object.keys({ grant: true, deny: tr
* skips it.
*/
export function assertPermissions(label: string, permissions: unknown, code: 'INVALID_CONFIG' | 'INVALID_ARGUMENT'): void {
const fail = (message: string): never => {
throw code === 'INVALID_CONFIG' ? new ConfigurationError(code, message) : new TestError(code, message);
};
// A Map or class instance carries no own keys to check, and the preset would apply none of it.
const prototype: unknown = typeof permissions === 'object' && permissions !== null ? Object.getPrototypeOf(permissions) : undefined;
if (prototype !== Object.prototype && prototype !== null) {
fail(`${label} must be a plain object of permission names to ${PERMISSION_STATES.join(', ')}`);
permissionError(code, `${label} must be a plain object of permission names to ${PERMISSION_STATES.join(', ')}`);
}
rejectUnknownKeys(label, permissions as object, DEVICE_PERMISSIONS, code);
for (const [name, state] of Object.entries(permissions as Record<string, unknown>)) {
if (state !== undefined && !PERMISSION_STATES.includes(state as string)) {
fail(`${label}.${name} must be one of ${PERMISSION_STATES.join(', ')}, got ${typeof state === 'string' ? `"${state}"` : String(state)}`);
}
if (state !== undefined) assertPermission(label, name, state, code);
}
}

/**
* Refuses one permission before any device command: a name that is not a
* string or not one agent-device sets, naming the nearest, or a state other
* than grant, deny, or reset, `undefined` included. The one owner of the
* permission messages, for `assertPermissions` and `device.setPermission`.
*/
function assertPermission(label: string, name: unknown, state: unknown, code: 'INVALID_CONFIG' | 'INVALID_ARGUMENT'): void {
if (typeof name !== 'string') {
permissionError(code, `${label} permission must be one of ${DEVICE_PERMISSIONS.join(', ')}, got type ${typeof name}`);
}
if (!(DEVICE_PERMISSIONS as readonly string[]).includes(name)) rejectUnknownKeys(label, { [name]: state }, DEVICE_PERMISSIONS, code);
if (!PERMISSION_STATES.includes(state as string)) {
permissionError(code, `${label}.${name} must be one of ${PERMISSION_STATES.join(', ')}, got ${typeof state === 'string' ? `"${state}"` : String(state)}`);
}
}

/** Throws a permission refusal as the error class its code belongs to. */
function permissionError(code: 'INVALID_CONFIG' | 'INVALID_ARGUMENT', message: string): never {
throw code === 'INVALID_CONFIG' ? new ConfigurationError(code, message) : new TestError(code, message);
}

/** Resolves the `transition` option: the default budget or a custom one. */
function transitionMs(transition: MobileOptions['transition']): number {
const budget = transition ?? DEFAULT_TRANSITION_MS;
Expand Down Expand Up @@ -829,8 +845,13 @@ export class AgentDeviceSurface {
for (const [permission, state] of entries) await this.permission(permission, state, app, signal);
}

/** One permission change for `device.setPermission`: on the pinned app, else on the app agent-device's session is on. */
/**
* One permission change for `device.setPermission`: on the pinned app, else
* on the app agent-device's session is on. The name and state are checked
* before any device command, a missing state refused rather than skipped.
*/
async setPermission(permission: DevicePermission, state: PermissionState, signal: AbortSignal): Promise<void> {
assertPermission('device.setPermission', permission, state, 'INVALID_ARGUMENT');
await this.permission(permission, state, this.pinnedApp, signal);
}

Expand Down
26 changes: 26 additions & 0 deletions packages/mobile/tests/unit/engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2320,6 +2320,32 @@ describe('device fixture', () => {
expect(h.fake.calls.length).toBe(before);
});

it('refuses a setPermission name or state it does not know before any device command, and still sends a valid one', async () => {
const h = harness();
await openAttempt(h);
const device = fixture(h);
const before = h.fake.calls.length;
await expect(device.setPermission('camra' as never, 'grant')).rejects.toMatchObject({
code: 'INVALID_ARGUMENT',
message: 'device.setPermission has unknown key "camra"; did you mean "camera"?',
});
await expect(device.setPermission('camera', 'allow' as never)).rejects.toMatchObject({
code: 'INVALID_ARGUMENT',
message: 'device.setPermission.camera must be one of grant, deny, reset, got "allow"',
});
await expect(device.setPermission('camera', undefined as never)).rejects.toMatchObject({
code: 'INVALID_ARGUMENT',
message: 'device.setPermission.camera must be one of grant, deny, reset, got undefined',
});
await expect(device.setPermission(42 as never, 'grant')).rejects.toMatchObject({
code: 'INVALID_ARGUMENT',
message: expect.stringMatching(/^device\.setPermission permission must be one of camera, .*, got type number$/),
});
expect(h.fake.calls.length).toBe(before);
await device.setPermission('camera', 'deny');
expect(h.fake.calls.slice(before).map((call) => call.method)).toEqual(['settings.update']);
});

it('switches Wi-Fi and airplane mode on Android; on iOS, where simctl only repaints the status bar, refuses going offline and restores with no device command', async () => {
const ios = harness();
await openAttempt(ios);
Expand Down
Loading