Skip to content

Commit aefc271

Browse files
committed
fix(apple,android): attach the session-app reason to per-app setting refusals
Both owners already refused an appless `settings permission` and an appless iOS `settings location`; each site now states the shared reason instead of leaving prose as the only signal. Both platforms use one reason because the recovery is the same whichever setting asked. The Apple and Android error assertions gained reason and dispatched so the behavior is keyed on details, not on the message. Refs #3181
1 parent 4e12086 commit aefc271

6 files changed

Lines changed: 96 additions & 7 deletions

File tree

‎packages/platform-android/src/__tests__/settings-permission.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { androidRevokedPermissionWarning } from '../settings-permission.ts';
55
import { ANDROID_EMULATOR } from './test-utils/device-fixtures.ts';
66
import { assertRejectsAppError } from './test-utils/app-error.ts';
77
import { withFakeAdb } from './test-utils/fake-adb.ts';
8+
import { PUBLISHED_ERROR_REASONS } from '@agent-device/kernel/errors';
89

910
// #1796. Two invariants decide every case here:
1011
// * `pm` defaults grant/revoke and the permission-flag operations to UserHandle.USER_SYSTEM,
@@ -314,13 +315,18 @@ test.each([
314315
);
315316
});
316317

317-
test('setAndroidSetting permission requires an app in session', async () => {
318+
test('setAndroidSetting permission requires an app in session with the published reason', async () => {
318319
await assertRejectsAppError(
319320
() =>
320321
setAndroidSetting(ANDROID_EMULATOR, 'permission', 'deny', undefined, {
321322
permissionTarget: 'camera',
322323
}),
323-
{ code: 'INVALID_ARGS', message: /requires an active app in session/ },
324+
{
325+
code: 'INVALID_ARGS',
326+
message: /requires an active app in session/,
327+
reason: PUBLISHED_ERROR_REASONS.sessionAppRequired,
328+
dispatched: 'no',
329+
},
324330
);
325331
});
326332

‎packages/platform-android/src/__tests__/test-utils/app-error.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import assert from 'node:assert/strict';
22
import { AppError, normalizeError } from '@agent-device/kernel/errors';
33

4-
type ExpectedAppError = { code: string; message?: RegExp; hint?: string | RegExp };
4+
type ExpectedAppError = {
5+
code: string;
6+
message?: RegExp;
7+
hint?: string | RegExp;
8+
/** The machine-readable `details.reason` the refusal must carry; key behavior on this, not on `message`. */
9+
reason?: string;
10+
/** The `details.dispatched` disclosure the producer must have proven. */
11+
dispatched?: 'no' | 'unknown';
12+
};
513

614
function assertAppError(error: unknown, expected: ExpectedAppError): true {
715
assert.ok(
@@ -10,6 +18,16 @@ function assertAppError(error: unknown, expected: ExpectedAppError): true {
1018
);
1119
assert.equal(error.code, expected.code);
1220
if (expected.message) assert.match(error.message, expected.message);
21+
if (expected.reason !== undefined) {
22+
assert.equal(
23+
error.details?.reason,
24+
expected.reason,
25+
`expected details.reason ${expected.reason}, got ${JSON.stringify(error.details)}`,
26+
);
27+
}
28+
if (expected.dispatched !== undefined) {
29+
assert.equal(error.details?.dispatched, expected.dispatched);
30+
}
1331
if (expected.hint !== undefined) {
1432
const { hint } = normalizeError(error);
1533
assert.ok(typeof hint === 'string', `expected a hint on ${error.code}, got ${String(hint)}`);

‎packages/platform-android/src/settings.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
AppError,
3+
sessionAppRequiredDetails,
34
summarizeCommandAttemptFailures,
45
type CommandAttemptFailure,
56
} from '@agent-device/kernel/errors';
@@ -162,7 +163,11 @@ export async function setAndroidSetting(
162163
}
163164
case 'permission': {
164165
if (!appPackage) {
165-
throw new AppError('INVALID_ARGS', 'permission setting requires an active app in session');
166+
throw new AppError(
167+
'INVALID_ARGS',
168+
'permission setting requires an active app in session',
169+
sessionAppRequiredDetails(),
170+
);
166171
}
167172
return await setAndroidPermission(device, appPackage, state, options);
168173
}

‎packages/platform-apple/src/__tests__/app-error.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ type ExpectedAppError = {
88
normalizedMessage?: RegExp;
99
/** `null` asserts the failure attached no hint of its own, so normalization falls back to the code default. */
1010
hint?: string | RegExp | null;
11+
/** The machine-readable `details.reason` the refusal must carry; key behavior on this, not on `message`. */
12+
reason?: string;
13+
/** The `details.dispatched` disclosure the producer must have proven. */
14+
dispatched?: 'no' | 'unknown';
1115
};
1216

1317
function assertAppError(error: unknown, expected: ExpectedAppError): true {
@@ -17,6 +21,16 @@ function assertAppError(error: unknown, expected: ExpectedAppError): true {
1721
);
1822
assert.equal(error.code, expected.code);
1923
if (expected.message) assert.match(error.message, expected.message);
24+
if (expected.reason !== undefined) {
25+
assert.equal(
26+
error.details?.reason,
27+
expected.reason,
28+
`expected details.reason ${expected.reason}, got ${JSON.stringify(error.details)}`,
29+
);
30+
}
31+
if (expected.dispatched !== undefined) {
32+
assert.equal(error.details?.dispatched, expected.dispatched);
33+
}
2034
if (expected.normalizedMessage) {
2135
assert.match(normalizeError(error).message, expected.normalizedMessage);
2236
}

‎packages/platform-apple/src/core/__tests__/app-settings.test.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const simulatorActual = await vi.importActual<typeof import('../simulator.ts')>(
2828
import { setIosSetting } from '../app-settings.ts';
2929
import { withMockedMacOsHelper } from './macos-helper-test-utils.ts';
3030
import { ensureBootedSimulator } from '../simulator.ts';
31-
import { AppError } from '@agent-device/kernel/errors';
31+
import { AppError, PUBLISHED_ERROR_REASONS } from '@agent-device/kernel/errors';
3232
import { runCmd } from '@agent-device/host-kit/command';
3333
import { retryWithPolicy } from '@agent-device/host-kit/retry';
3434
import { assertRejectsAppError } from '../../__tests__/app-error.ts';
@@ -315,6 +315,43 @@ test('setIosSetting location set sends simulator latitude and longitude', async
315315
);
316316
});
317317

318+
test('setIosSetting permission requires an app in session with the published reason', async () => {
319+
await withFakeAppleTool(
320+
(args) => {
321+
if (isSimctlListDevices(args)) return BOOTED_SIM_LIST_JSON;
322+
return unexpectedArgs(args);
323+
},
324+
async () => {
325+
await assertRejectsAppError(
326+
() =>
327+
setIosSetting(IOS_TEST_SIMULATOR, 'permission', 'grant', undefined, {
328+
permissionTarget: 'camera',
329+
}),
330+
{
331+
code: 'INVALID_ARGS',
332+
reason: PUBLISHED_ERROR_REASONS.sessionAppRequired,
333+
dispatched: 'no',
334+
},
335+
);
336+
},
337+
);
338+
});
339+
340+
test('setIosSetting location refuses an appless session with the published reason', async () => {
341+
mockEnsureBootedSimulator.mockResolvedValue(undefined);
342+
343+
await withFakeAppleTool(
344+
(args) => unexpectedArgs(args),
345+
async () => {
346+
await assertRejectsAppError(() => setIosSetting(IOS_TEST_SIMULATOR, 'location', 'on'), {
347+
code: 'INVALID_ARGS',
348+
reason: PUBLISHED_ERROR_REASONS.sessionAppRequired,
349+
dispatched: 'no',
350+
});
351+
},
352+
);
353+
});
354+
318355
test('setIosSetting appearance toggle flips current simulator appearance', async () => {
319356
await withFakeAppleTool(
320357
(args) => {

‎packages/platform-apple/src/core/app-settings.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { isIosFamily, isMacOs, type DeviceInfo } from '@agent-device/kernel/device';
1414
import {
1515
AppError,
16+
sessionAppRequiredDetails,
1617
summarizeCommandAttemptFailures,
1718
type CommandAttemptFailure,
1819
} from '@agent-device/kernel/errors';
@@ -139,7 +140,11 @@ export async function setIosSetting(
139140
}
140141
const enabled = parseSettingState(state);
141142
if (!appBundleId) {
142-
throw new AppError('INVALID_ARGS', 'location setting requires an active app in session');
143+
throw new AppError(
144+
'INVALID_ARGS',
145+
'location setting requires an active app in session',
146+
sessionAppRequiredDetails(),
147+
);
143148
}
144149
const action = enabled ? 'grant' : 'revoke';
145150
await runSimctlForDevice(device, ['privacy', device.id, action, 'location', appBundleId]);
@@ -167,7 +172,11 @@ export async function setIosSetting(
167172
}
168173
case 'permission': {
169174
if (!appBundleId) {
170-
throw new AppError('INVALID_ARGS', 'permission setting requires an active app in session');
175+
throw new AppError(
176+
'INVALID_ARGS',
177+
'permission setting requires an active app in session',
178+
sessionAppRequiredDetails(),
179+
);
171180
}
172181
const action = mapIosPermissionAction(parsePermissionAction(state));
173182
const target = parseIosPermissionTarget(options?.permissionTarget, options?.permissionMode);

0 commit comments

Comments
 (0)