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
38 changes: 33 additions & 5 deletions packages/provider-revyl/src/facts-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type RevylOperation = keyof ReturnType<typeof revylFacts>['operations'];

/**
* Every operation cell Revyl's facts report, with the verdict each one has to carry. `available` is
* a cell the facts admit; `mode` is a cell refused because the worker has no action for it; `kind` is
* a cell the facts admit; `subjects` is a cell admitted for the subjects it lists; `mode` is a cell refused because the worker has no action for it; `kind` is
* a cell refused because the rented device is the wrong shape for the command at all.
*
* The map is typed over the whole catalog, so a cell added to or dropped from `revylFacts` is a
Expand All @@ -40,6 +40,8 @@ const EXPECTED_CELLS = {
prepareApplicationOpen: 'available',
resolveOpenTarget: 'available',
scrollDirection: 'available',
setOrientation: 'available',
setSetting: 'subjects',
tapPoint: 'available',

acceptAlert: 'mode',
Expand Down Expand Up @@ -106,8 +108,6 @@ const EXPECTED_CELLS = {
sendPushNotification: 'mode',
sendTelephonyEvent: 'mode',
setFoldPose: 'mode',
setOrientation: 'mode',
setSetting: 'mode',
setViewport: 'mode',
shutdownTarget: 'mode',
sleep: 'mode',
Expand All @@ -123,7 +123,7 @@ const EXPECTED_CELLS = {
volumeUp: 'mode',
wake: 'mode',
writeClipboard: 'mode',
} as const satisfies Readonly<Record<RevylOperation, 'available' | 'mode' | 'kind'>>;
} as const satisfies Readonly<Record<RevylOperation, 'available' | 'subjects' | 'mode' | 'kind'>>;

/** The typed reason one verdict of the matrix carries. */
const REFUSAL_REASONS = { mode: 'unsupported-provider-mode', kind: 'unsupported-device-kind' };
Expand All @@ -138,13 +138,18 @@ test('every operation cell reports the verdict its matrix row promises', () => {
);
for (const [cell, expected] of Object.entries(EXPECTED_CELLS) as [
RevylOperation,
'available' | 'mode' | 'kind',
'available' | 'subjects' | 'mode' | 'kind',
][]) {
const fact = operations[cell];
if (expected === 'available') {
assert.deepEqual(fact, { available: true }, cell);
continue;
}
if (expected === 'subjects') {
assert.equal(fact.available, true, cell);
assert.ok(fact.available && fact.subjects !== undefined, cell);
continue;
}
assert.equal(fact.available, false, cell);
assert.equal(fact.reason, REFUSAL_REASONS[expected], cell);
}
Expand Down Expand Up @@ -174,6 +179,29 @@ test('back is an Android button; iOS refuses it with the substitute', () => {
assert.match(ios.available ? '' : (ios.hint ?? ''), /only on Android/);
});

test('settings admit what the worker has actions for, and the keychain only on iOS', () => {
const android: DeviceInfo = {
...IOS_DEVICE,
platform: 'android',
appleOs: undefined,
id: 'revyl:android:lease-1',
kind: 'emulator',
};
const subjects = (device: DeviceInfo) => {
const fact = revylFacts(device).operations.setSetting;
assert.ok(fact.available && fact.subjects !== undefined);
return Object.fromEntries(
Object.entries(fact.subjects).map(([name, subject]) => [name, subject.available]),
);
};
const served = { appearance: true, location: true, permission: true, 'clear-app-state': true };
assert.deepEqual(subjects(IOS_DEVICE), { ...served, 'reset-keychain': true });
assert.deepEqual(subjects(android), { ...served, 'reset-keychain': false });
const setSetting = revylFacts(android).operations.setSetting;
assert.ok(setSetting.available);
assert.equal(setSetting.unlisted?.reason, 'unsupported-provider-mode');
});

test('the facts report the canonical device shape beside the provider mode', () => {
assert.deepEqual(revylFacts(IOS_DEVICE).device, {
family: 'apple',
Expand Down
2 changes: 2 additions & 0 deletions packages/provider-revyl/src/facts-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
liveSessionUnavailable,
revylInteractionOperationFacts,
revylNavigationOperationFacts,
revylSettingsOperationFacts,
revylUnsupportedOperationFacts,
REVYL_FOLD_UNAVAILABLE,
REVYL_PUSH_UNAVAILABLE,
Expand Down Expand Up @@ -176,6 +177,7 @@ export function revylFacts(device: DeviceInfo): RuntimeFacts<PlatformRuntimeOper
...elementTextRuntimeOperationFacts({ readTextAtPoint: elementTextUnavailable }),
...revylNavigationOperationFacts(device),
...revylUnsupportedOperationFacts(),
...revylSettingsOperationFacts(device),
...audioProbeRuntimeOperationFacts({
capture: audioProbeUnavailable,
query: audioProbeUnavailable,
Expand Down
48 changes: 34 additions & 14 deletions packages/provider-revyl/src/interaction-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ const clipboardUnavailable = unavailableFact(
'unsupported-provider-mode',
'Revyl does not expose the pasteboard.',
);
/** Revyl keeps the device in the orientation its session started in. */
const orientationUnavailable = unavailableFact(
/** Revyl's `/set_orientation` locks a rotation; no action hands rotation back to the sensor. */
const orientationUnlockUnavailable = unavailableFact(
'unsupported-provider-mode',
'Revyl does not expose orientation control.',
'Revyl sets an orientation but cannot hand rotation back to the sensor.',
);
/** Revyl's worker answers `/back` only on Android: iOS has no system back button to press. */
const iosBackUnavailable = unavailableFact(
Expand All @@ -85,9 +85,18 @@ const systemButtonUnavailable = unavailableFact(
'unsupported-provider-mode',
'Revyl exposes the home button only.',
);
const settingsUnavailable = unavailableFact(
/**
* The worker has no action for Wi-Fi, mobile data, Bluetooth, animations, text size, or biometrics,
* and refuses airplane mode on the devices it rents.
*/
const settingUnavailable = unavailableFact(
'unsupported-provider-mode',
'Revyl does not expose settings control.',
'Revyl sets appearance, location, permissions, and app state; it does not expose this setting.',
);
/** `/clear_keychain` resets a simulator keychain, and an Android emulator has none. */
const keychainUnavailable = unavailableFact(
'unsupported-device-kind',
'reset-keychain resets an iOS simulator keychain; an Android emulator has none.',
);
/** Revyl's worker has no push action, so a push and an app event refuse with one reason. */
export const REVYL_PUSH_UNAVAILABLE = unavailableFact(
Expand Down Expand Up @@ -179,8 +188,8 @@ export function revylNavigationOperationFacts(
back: device.platform === 'android' ? available : iosBackUnavailable,
}),
...orientationRuntimeOperationFacts({
orientation: orientationUnavailable,
unlock: orientationUnavailable,
orientation: available,
unlock: orientationUnlockUnavailable,
}),
...systemButtonRuntimeOperationFacts({
unsupported: systemButtonUnavailable,
Expand All @@ -199,13 +208,6 @@ export function revylUnsupportedOperationFacts(liveSession?: RuntimeOperationUna
...clipboardRuntimeOperationFacts({
unsupported: liveSession ?? clipboardUnavailable,
}),
// `target` admits the device as a settings target; `unsupported` then refuses every setting, for the
// provider-mode reason on a live session and the session reason on a gone one.
...settingsRuntimeOperationFacts({
target: liveSession ?? available,
unsupported: liveSession ?? settingsUnavailable,
write: {},
}),
...appEventRuntimeOperationFacts({
triggerAppEvent: liveSession ?? REVYL_PUSH_UNAVAILABLE,
}),
Expand All @@ -220,6 +222,24 @@ export function revylUnsupportedOperationFacts(liveSession?: RuntimeOperationUna
});
}

/**
* The settings a live session writes through the worker's own actions. Each one is a device action
* of its own rather than a point action, and `reset-keychain` exists only on an iOS simulator.
*/
export function revylSettingsOperationFacts(device: Pick<DeviceInfo, 'platform'>) {
return settingsRuntimeOperationFacts({
target: available,
unsupported: settingUnavailable,
write: {
appearance: available,
location: available,
permission: available,
'clear-app-state': available,
'reset-keychain': device.platform === 'apple' ? available : keychainUnavailable,
},
});
}

/**
* Binds the operations that reach the device through the session's interactor: the two captures,
* focus, touch and scroll. Refused cells stay absent from the binding, so a refusal is the facts'
Expand Down
156 changes: 156 additions & 0 deletions packages/provider-revyl/src/interactor-settings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import assert from 'node:assert/strict';
import { afterEach, test } from 'vitest';
import { AppError } from '@agent-device/kernel/errors';
import type { Interactor } from '@agent-device/contracts/interactor-types';
import {
revylLease,
startRevylFixtureBackend,
type RevylFixtureBackend,
} from './revyl-backend.fixtures.ts';

const openBackends: RevylFixtureBackend[] = [];

afterEach(async () => {
await Promise.all(openBackends.splice(0).map((fixture) => fixture.close()));
});

/** The interactor of one live session against a fake Revyl backend, with that backend. */
async function liveInteractor(
platform: 'ios' | 'android',
options?: Parameters<typeof startRevylFixtureBackend>[0],
): Promise<Readonly<{ fixture: RevylFixtureBackend; interactor: Interactor }>> {
const fixture = await startRevylFixtureBackend(options);
openBackends.push(fixture);
const runtime = fixture.createRuntime();
await runtime.leaseLifecycle?.allocate?.(revylLease(platform));
const device = (
await runtime.deviceInventoryProvider?.({
leaseProvider: 'revyl',
leaseId: 'lease-1',
platform,
})
)?.[0];
if (!device) throw new Error('Expected the live session to be listed.');
const interactor = runtime.getInteractor(device);
if (!interactor) throw new Error('Expected a live session interactor.');
return { fixture, interactor };
}

const refusedForMode = (error: unknown) =>
error instanceof AppError && error.details?.reason === 'unsupported-provider-mode';

test('orientation sends the worker name of each rotation on a worker that rotates precisely', async () => {
const { fixture, interactor } = await liveInteractor('android', { preciseOrientation: true });
assert.deepEqual(await interactor.setOrientation('landscape-left'), {
orientation: 'landscape-left',
});
await interactor.setOrientation('portrait-upside-down');

assert.deepEqual(
fixture.requests
.filter((request) => request.path.endsWith('/set_orientation'))
.map((request) => request.body),
[{ orientation: 'landscape_left' }, { orientation: 'upside_down' }],
);
});

test('a worker without precise orientation rotates to portrait and landscape-left only', async () => {
const { fixture, interactor } = await liveInteractor('ios');
await interactor.setOrientation('portrait');
await interactor.setOrientation('landscape-left');
for (const orientation of ['landscape-right', 'portrait-upside-down'] as const) {
await assert.rejects(interactor.setOrientation(orientation), refusedForMode);
}
assert.deepEqual(
fixture.requests
.filter((request) => request.path.endsWith('/set_orientation'))
.map((request) => request.body),
[{ orientation: 'portrait' }, { orientation: 'landscape' }],
);
});

test('appearance sets the named mode, and toggle flips the mode the worker reports', async () => {
const { fixture, interactor } = await liveInteractor('ios', {
respond: {
'GET /api/v1/execution/device-proxy/run-1/appearance': { body: { appearance: 'dark' } },
},
});
assert.deepEqual(await interactor.setSetting('appearance', 'dark'), { appearance: 'dark' });
assert.deepEqual(await interactor.setSetting('appearance', 'toggle'), { appearance: 'light' });

assert.deepEqual(
fixture.requests
.filter((request) => request.path.endsWith('/set_appearance'))
.map((request) => request.body),
[{ appearance: 'dark' }, { appearance: 'light' }],
);
});

test('location sets the coordinates and refuses switching location services', async () => {
const { fixture, interactor } = await liveInteractor('android');
await interactor.setSetting('location', 'set', undefined, { latitude: 43.65, longitude: -79.38 });
await assert.rejects(interactor.setSetting('location', 'off'), refusedForMode);

assert.deepEqual(fixture.bodyFor('set_location'), { latitude: 43.65, longitude: -79.38 });
assert.equal(fixture.paths().filter((request) => request.endsWith('/set_location')).length, 1);
});

test('a permission change names the app and the Maestro permission the worker takes', async () => {
const { fixture, interactor } = await liveInteractor('android');
await interactor.setSetting('permission', 'grant', 'com.example.app', {
permissionTarget: 'media-library',
});
await interactor.setSetting('permission', 'reset', 'com.example.app', {
permissionTarget: 'camera',
});

assert.deepEqual(
fixture.requests
.filter((request) => request.path.endsWith('/set_permissions'))
.map((request) => request.body),
[
{ bundle_id: 'com.example.app', permissions: { medialibrary: 'allow' } },
{ bundle_id: 'com.example.app', permissions: { camera: 'unset' } },
],
);
});

test('a permission without a Maestro name, or with a mode, is refused before the worker', async () => {
const { fixture, interactor } = await liveInteractor('ios');
for (const options of [
{ permissionTarget: 'location-always' },
{ permissionTarget: 'photos', permissionMode: 'limited' },
]) {
await assert.rejects(
interactor.setSetting('permission', 'grant', 'com.example.app', options),
refusedForMode,
);
}
assert.equal(
fixture.paths().some((request) => request.endsWith('/set_permissions')),
false,
);
});

test('an app setting falls back to the launched app and refuses when none is known', async () => {
const { fixture, interactor } = await liveInteractor('ios');
await assert.rejects(
interactor.setSetting('clear-app-state', 'clear'),
(error: unknown) => error instanceof AppError && error.code === 'INVALID_ARGS',
);
await interactor.open('com.example.app');
await interactor.setSetting('clear-app-state', 'clear');
await interactor.setSetting('reset-keychain', 'clear');

assert.deepEqual(fixture.bodyFor('clear_app_state'), { bundle_id: 'com.example.app' });
assert.equal(fixture.paths().at(-1), 'POST /api/v1/execution/device-proxy/run-1/clear_keychain');
});

test('a setting the worker has no action for is refused rather than sent', async () => {
const { fixture, interactor } = await liveInteractor('android');
await assert.rejects(interactor.setSetting('airplane', 'on'), refusedForMode);
assert.equal(
fixture.paths().some((request) => request.includes('airplane')),
false,
);
});
Loading
Loading