|
1 | 1 | import assert from 'node:assert/strict'; |
2 | 2 | import { describe, test } from 'vitest'; |
3 | 3 | import { MAESTRO_PERMISSION_VALUES } from '@agent-device/maestro'; |
4 | | -import { parseAndroidPermissionTarget } from '@agent-device/platform-android/mechanics'; |
5 | | -import { mapMaestroSetPermissions } from '../set-permissions-mapping.ts'; |
| 4 | +import { |
| 5 | + MAESTRO_ANDROID_PERMISSION_TARGETS, |
| 6 | + MAESTRO_IOS_PERMISSION_TARGETS, |
| 7 | + MOBILE_PERMISSION_TARGETS, |
| 8 | +} from '@agent-device/contracts/settings'; |
| 9 | +import { AppError } from '@agent-device/kernel/errors'; |
| 10 | +import { |
| 11 | + MAESTRO_PERMISSION_ALIASES, |
| 12 | + mapMaestroSetPermissions, |
| 13 | + type MaestroPermissionMutation, |
| 14 | +} from '../set-permissions-mapping.ts'; |
| 15 | + |
| 16 | +function assertIosHintListsAdmissionSet(name: string): void { |
| 17 | + try { |
| 18 | + mapMaestroSetPermissions({ [name]: 'allow' }, 'ios'); |
| 19 | + } catch (error) { |
| 20 | + assert.ok(error instanceof AppError && error.code === 'UNSUPPORTED_OPERATION'); |
| 21 | + const hint = String((error.details as { hint?: unknown } | undefined)?.hint ?? ''); |
| 22 | + for (const supported of MAESTRO_IOS_PERMISSION_TARGETS) { |
| 23 | + assert.ok(hint.includes(supported), `iOS hint omits ${supported} for ${name}: ${hint}`); |
| 24 | + } |
| 25 | + assert.ok(!hint.includes('contacts-limited'), `iOS hint lists native-only name: ${hint}`); |
| 26 | + assert.ok(!hint.includes('location-always'), `iOS hint lists native-only name: ${hint}`); |
| 27 | + return; |
| 28 | + } |
| 29 | + assert.fail(`expected iOS ${name} to be unsupported`); |
| 30 | +} |
| 31 | + |
| 32 | +function mapAndroidOrNull(name: string, value: string): MaestroPermissionMutation[] | null { |
| 33 | + try { |
| 34 | + return mapMaestroSetPermissions({ [name]: value }, 'android'); |
| 35 | + } catch (error) { |
| 36 | + assert.ok( |
| 37 | + error instanceof AppError && |
| 38 | + (error.code === 'UNSUPPORTED_OPERATION' || error.code === 'INVALID_ARGS'), |
| 39 | + `unexpected ${String(error)} for Android ${name}:${value}`, |
| 40 | + ); |
| 41 | + return null; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function assertAndroidMutationServable( |
| 46 | + mutation: MaestroPermissionMutation, |
| 47 | + servable: ReadonlySet<string>, |
| 48 | +): void { |
| 49 | + assert.ok( |
| 50 | + servable.has(mutation.permission), |
| 51 | + `Android mutation ${mutation.permission} not in ${[...servable].join(',')}`, |
| 52 | + ); |
| 53 | + assert.equal(mutation.mode, undefined); |
| 54 | +} |
| 55 | + |
| 56 | +function checkAndroidName( |
| 57 | + name: string, |
| 58 | + servable: ReadonlySet<string>, |
| 59 | + checkedPlain: Set<string>, |
| 60 | +): number { |
| 61 | + let checked = 0; |
| 62 | + for (const value of MAESTRO_PERMISSION_VALUES) { |
| 63 | + const mutations = mapAndroidOrNull(name, value); |
| 64 | + if (!mutations) continue; |
| 65 | + for (const mutation of mutations) { |
| 66 | + assertAndroidMutationServable(mutation, servable); |
| 67 | + checked += 1; |
| 68 | + if (value === 'allow' || value === 'deny' || value === 'unset') { |
| 69 | + checkedPlain.add(`${mutation.permission}:${value}`); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + return checked; |
| 74 | +} |
| 75 | + |
| 76 | +function assertAndroidPlainCoverage(checkedPlain: ReadonlySet<string>): void { |
| 77 | + for (const target of MAESTRO_ANDROID_PERMISSION_TARGETS) { |
| 78 | + for (const value of ['allow', 'deny', 'unset'] as const) { |
| 79 | + assert.ok( |
| 80 | + checkedPlain.has(`${target}:${value}`), |
| 81 | + `expected Android ${target}:${value} to be checked`, |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
6 | 86 |
|
7 | 87 | describe('mapMaestroSetPermissions', () => { |
8 | 88 | test('maps single permissions to grant/deny/reset', () => { |
@@ -122,35 +202,26 @@ describe('mapMaestroSetPermissions', () => { |
122 | 202 | } |
123 | 203 | }); |
124 | 204 |
|
125 | | - test('every mutation emitted for Android passes the Android parser', () => { |
126 | | - // Pins the adapter to mutations the backend serves: a value the adapter |
127 | | - // accepts must never fail halfway through `settings permission`. |
128 | | - const names = [ |
129 | | - 'all', |
130 | | - 'camera', |
131 | | - 'microphone', |
132 | | - 'photos', |
133 | | - 'contacts', |
134 | | - 'notifications', |
135 | | - 'calendar', |
136 | | - 'location', |
137 | | - 'media-library', |
138 | | - 'contacts-limited', |
139 | | - 'location-always', |
140 | | - 'health', |
141 | | - ]; |
142 | | - for (const name of names) { |
143 | | - for (const value of MAESTRO_PERMISSION_VALUES) { |
144 | | - let mutations; |
145 | | - try { |
146 | | - mutations = mapMaestroSetPermissions({ [name]: value }, 'android'); |
147 | | - } catch { |
148 | | - continue; |
149 | | - } |
150 | | - for (const mutation of mutations) { |
151 | | - parseAndroidPermissionTarget(mutation.permission, mutation.mode); |
152 | | - } |
153 | | - } |
| 205 | + test('iOS unsupported hint lists the Maestro admission set', () => { |
| 206 | + // The admission set and the hint share one Maestro iOS list in contracts: |
| 207 | + // a native-only spelling must fail without appearing as supported. |
| 208 | + for (const name of ['contacts-limited', 'location-always', 'speech']) { |
| 209 | + assertIosHintListsAdmissionSet(name); |
154 | 210 | } |
155 | 211 | }); |
| 212 | + |
| 213 | + test('every mutation emitted for Android is a backend-servable pair', () => { |
| 214 | + // Pins the adapter to mutations the backend serves: a value the adapter |
| 215 | + // accepts must never fail halfway through `settings permission`. The |
| 216 | + // backend table is typed from the same contracts list, so membership plus |
| 217 | + // a missing mode is the whole contract on Android (granular values are |
| 218 | + // refused above, never emitted). |
| 219 | + const names = [...MOBILE_PERMISSION_TARGETS, ...Object.keys(MAESTRO_PERMISSION_ALIASES)]; |
| 220 | + const servable = new Set<string>(MAESTRO_ANDROID_PERMISSION_TARGETS); |
| 221 | + const checkedPlain = new Set<string>(); |
| 222 | + let checked = 0; |
| 223 | + for (const name of names) checked += checkAndroidName(name, servable, checkedPlain); |
| 224 | + assert.ok(checked > 0, 'expected at least one Android mutation to be checked'); |
| 225 | + assertAndroidPlainCoverage(checkedPlain); |
| 226 | + }); |
156 | 227 | }); |
0 commit comments