|
| 1 | +import type { |
| 2 | + DeviceInventoryRequest, |
| 3 | + ProviderDeviceInventorySource, |
| 4 | +} from '@agent-device/contracts/device'; |
| 5 | +import { |
| 6 | + type DeviceInventorySource, |
| 7 | + type InventoryPlatformModule, |
| 8 | + createPlatformModuleRegistry, |
| 9 | +} from '@agent-device/contracts/platform-module'; |
| 10 | +import type { |
| 11 | + DeviceInventoryHost, |
| 12 | + PlatformRequestScope, |
| 13 | +} from '@agent-device/contracts/platform-runtime-host'; |
| 14 | +import { resolveTargetDeviceSelection } from '../dispatch-resolve.ts'; |
| 15 | +import { withDeviceInventoryContext } from '../device-inventory-context.ts'; |
| 16 | +import { PLATFORMS, type DeviceInfo, type Platform } from '@agent-device/kernel/device'; |
| 17 | +import { describe, expect, test, vi } from 'vitest'; |
| 18 | +import { createComposedDeviceInventoryGateways } from '../composed-inventory-gateways.ts'; |
| 19 | + |
| 20 | +const scope: PlatformRequestScope = Object.freeze({ |
| 21 | + signal: new AbortController().signal, |
| 22 | + diagnostics: Object.freeze({ emit: () => {} }), |
| 23 | + progress: Object.freeze({ report: () => {} }), |
| 24 | +}); |
| 25 | + |
| 26 | +const host: DeviceInventoryHost = Object.freeze({ |
| 27 | + commands: Object.freeze({ |
| 28 | + which: async () => undefined, |
| 29 | + run: async () => ({ stdout: '', stderr: '', exitCode: 0 }), |
| 30 | + }), |
| 31 | + appleTools: Object.freeze({ |
| 32 | + isXcrunAvailable: async () => false, |
| 33 | + run: async () => ({ stdout: '', stderr: '', exitCode: 0 }), |
| 34 | + }), |
| 35 | + toolchains: Object.freeze({ prepare: async () => {} }), |
| 36 | + files: Object.freeze({ |
| 37 | + isExecutable: async () => false, |
| 38 | + createTemporaryTextFile: async () => { |
| 39 | + throw new Error('unused'); |
| 40 | + }, |
| 41 | + }), |
| 42 | + hostOs: 'darwin', |
| 43 | + hostName: 'test-host', |
| 44 | + homeDirectory: '/tmp/test-home', |
| 45 | + observations: Object.freeze({ deviceBooted: async () => {} }), |
| 46 | +}); |
| 47 | + |
| 48 | +describe('composed device inventory gateway', () => { |
| 49 | + test('an authoritative provider inventory, including empty, loads no local host or source', async () => { |
| 50 | + for (const devices of [[], [device('android', 'remote')]]) { |
| 51 | + const local = inventoryWorld(); |
| 52 | + const provider: ProviderDeviceInventorySource = { |
| 53 | + discover: async () => ({ kind: 'inventory', devices }), |
| 54 | + }; |
| 55 | + const gateways = createComposedDeviceInventoryGateways({ |
| 56 | + registry: local.registry, |
| 57 | + loadHost: local.loadHost, |
| 58 | + provider, |
| 59 | + }); |
| 60 | + |
| 61 | + expect(await gateways.providerFirst.discover({ platform: 'android' }, scope)).toEqual( |
| 62 | + devices, |
| 63 | + ); |
| 64 | + expect(local.hostLoads()).toBe(0); |
| 65 | + expect(local.sourceLoads()).toEqual([]); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + test('declined providers fall through, while malformed outcomes fail closed', async () => { |
| 70 | + const local = inventoryWorld(); |
| 71 | + const declined = createComposedDeviceInventoryGateways({ |
| 72 | + registry: local.registry, |
| 73 | + loadHost: local.loadHost, |
| 74 | + provider: { discover: async () => ({ kind: 'declined' }) }, |
| 75 | + }); |
| 76 | + expect(await declined.providerFirst.discover({ platform: 'android' }, scope)).toEqual([ |
| 77 | + device('android'), |
| 78 | + ]); |
| 79 | + |
| 80 | + const malformed = createComposedDeviceInventoryGateways({ |
| 81 | + registry: local.registry, |
| 82 | + loadHost: local.loadHost, |
| 83 | + provider: { discover: async () => undefined } as unknown as ProviderDeviceInventorySource, |
| 84 | + }); |
| 85 | + await expect( |
| 86 | + malformed.providerFirst.discover({ platform: 'android' }, scope), |
| 87 | + ).rejects.toMatchObject({ details: { reason: 'provider_inventory_contract_violation' } }); |
| 88 | + }); |
| 89 | + |
| 90 | + test('forwards the request signal into an in-flight provider source', async () => { |
| 91 | + const controller = new AbortController(); |
| 92 | + const requestScope = { ...scope, signal: controller.signal }; |
| 93 | + const observed = vi.fn(); |
| 94 | + const provider: ProviderDeviceInventorySource = { |
| 95 | + discover: async (_request, signal) => { |
| 96 | + observed(signal); |
| 97 | + return await new Promise((_resolve, reject) => { |
| 98 | + signal.addEventListener('abort', () => reject(signal.reason), { once: true }); |
| 99 | + }); |
| 100 | + }, |
| 101 | + }; |
| 102 | + const local = inventoryWorld(); |
| 103 | + const gateways = createComposedDeviceInventoryGateways({ |
| 104 | + registry: local.registry, |
| 105 | + loadHost: local.loadHost, |
| 106 | + provider, |
| 107 | + }); |
| 108 | + const pending = gateways.providerFirst.discover({ platform: 'android' }, requestScope); |
| 109 | + controller.abort(new Error('cancelled')); |
| 110 | + |
| 111 | + await expect(pending).rejects.toThrow('cancelled'); |
| 112 | + expect(observed).toHaveBeenCalledWith(controller.signal); |
| 113 | + expect(local.hostLoads()).toBe(0); |
| 114 | + }); |
| 115 | + |
| 116 | + test('keeps local projection policy out of the provider request', async () => { |
| 117 | + const received: unknown[] = []; |
| 118 | + const provider: ProviderDeviceInventorySource = { |
| 119 | + discover: async (request) => { |
| 120 | + received.push(request); |
| 121 | + return { kind: 'inventory', devices: [] }; |
| 122 | + }, |
| 123 | + }; |
| 124 | + const local = inventoryWorld(); |
| 125 | + const gateways = createComposedDeviceInventoryGateways({ |
| 126 | + registry: local.registry, |
| 127 | + loadHost: local.loadHost, |
| 128 | + provider, |
| 129 | + }); |
| 130 | + |
| 131 | + await gateways.providerFirst.discover( |
| 132 | + { |
| 133 | + platform: 'ios', |
| 134 | + kind: 'simulator', |
| 135 | + booted: true, |
| 136 | + iosSimulatorSetPath: '/tmp/isolated-simulators', |
| 137 | + androidSerialAllowlist: ['emulator-5554'], |
| 138 | + }, |
| 139 | + scope, |
| 140 | + ); |
| 141 | + |
| 142 | + expect(received).toEqual([ |
| 143 | + { |
| 144 | + platform: 'ios', |
| 145 | + iosSimulatorSetPath: '/tmp/isolated-simulators', |
| 146 | + androidSerialAllowlist: ['emulator-5554'], |
| 147 | + }, |
| 148 | + ]); |
| 149 | + expect(local.hostLoads()).toBe(0); |
| 150 | + }); |
| 151 | + |
| 152 | + test('applies public Apple aliases and all internal projection filters centrally', async () => { |
| 153 | + const devices = [ |
| 154 | + { ...device('apple', 'ios-default'), appleOs: 'ios' as const, target: undefined }, |
| 155 | + { ...device('apple', 'mac'), appleOs: 'macos' as const, target: 'desktop' as const }, |
| 156 | + { ...device('apple', 'shutdown'), appleOs: 'ios' as const, booted: false }, |
| 157 | + device('android', 'other-family'), |
| 158 | + ]; |
| 159 | + const provider: ProviderDeviceInventorySource = { |
| 160 | + discover: async () => ({ kind: 'inventory', devices }), |
| 161 | + }; |
| 162 | + const local = inventoryWorld(); |
| 163 | + const gateways = createComposedDeviceInventoryGateways({ |
| 164 | + registry: local.registry, |
| 165 | + loadHost: local.loadHost, |
| 166 | + provider, |
| 167 | + }); |
| 168 | + |
| 169 | + const projected = await gateways.providerFirst.discover( |
| 170 | + { platform: 'ios', target: 'mobile', kind: 'device', booted: true }, |
| 171 | + scope, |
| 172 | + ); |
| 173 | + expect(projected.map(({ id }) => id)).toEqual(['ios-default']); |
| 174 | + }); |
| 175 | + |
| 176 | + test('loads only the selected family and retries failed host and source loads', async () => { |
| 177 | + let hostAttempts = 0; |
| 178 | + let androidAttempts = 0; |
| 179 | + const local = inventoryWorld({ |
| 180 | + android: async () => { |
| 181 | + androidAttempts += 1; |
| 182 | + if (androidAttempts === 1) throw new Error('source load failed'); |
| 183 | + return source([device('android')]); |
| 184 | + }, |
| 185 | + }); |
| 186 | + const gateways = createComposedDeviceInventoryGateways({ |
| 187 | + registry: local.registry, |
| 188 | + loadHost: async () => { |
| 189 | + hostAttempts += 1; |
| 190 | + if (hostAttempts === 1) throw new Error('host load failed'); |
| 191 | + return host; |
| 192 | + }, |
| 193 | + }); |
| 194 | + |
| 195 | + await expect(gateways.localOnly.discover({ platform: 'android' }, scope)).rejects.toThrow( |
| 196 | + 'host load failed', |
| 197 | + ); |
| 198 | + await expect(gateways.localOnly.discover({ platform: 'android' }, scope)).rejects.toThrow( |
| 199 | + 'source load failed', |
| 200 | + ); |
| 201 | + expect(await gateways.localOnly.discover({ platform: 'android' }, scope)).toEqual([ |
| 202 | + device('android'), |
| 203 | + ]); |
| 204 | + expect(hostAttempts).toBe(2); |
| 205 | + expect(androidAttempts).toBe(2); |
| 206 | + expect(local.sourceLoads()).toEqual(['android', 'android']); |
| 207 | + }); |
| 208 | + |
| 209 | + test('unfiltered local discovery is fail-soft and preserves selector order', async () => { |
| 210 | + const local = inventoryWorld({ |
| 211 | + harmonyos: async () => source([], new Error('hdc unavailable')), |
| 212 | + }); |
| 213 | + const gateways = createComposedDeviceInventoryGateways({ |
| 214 | + registry: local.registry, |
| 215 | + loadHost: local.loadHost, |
| 216 | + }); |
| 217 | + |
| 218 | + const devices = await gateways.localOnly.discover({}, scope); |
| 219 | + expect(devices.map(({ platform }) => platform)).toEqual(['android', 'apple', 'vega', 'linux']); |
| 220 | + expect(local.sourceLoads()).toEqual(['android', 'harmonyos', 'apple', 'vega', 'linux']); |
| 221 | + }); |
| 222 | + |
| 223 | + test('explicit local failures propagate and family sources cannot return another family', async () => { |
| 224 | + const failing = inventoryWorld({ android: async () => source([], new Error('adb failed')) }); |
| 225 | + await expect( |
| 226 | + createComposedDeviceInventoryGateways({ |
| 227 | + registry: failing.registry, |
| 228 | + loadHost: failing.loadHost, |
| 229 | + }).localOnly.discover({ platform: 'android' }, scope), |
| 230 | + ).rejects.toThrow('adb failed'); |
| 231 | + |
| 232 | + const lying = inventoryWorld({ |
| 233 | + android: async () => source([device('linux', 'wrong-family')]), |
| 234 | + }); |
| 235 | + await expect( |
| 236 | + createComposedDeviceInventoryGateways({ |
| 237 | + registry: lying.registry, |
| 238 | + loadHost: lying.loadHost, |
| 239 | + }).localOnly.discover({ platform: 'android' }, scope), |
| 240 | + ).rejects.toMatchObject({ |
| 241 | + details: { expectedFamily: 'android', actualFamily: 'linux' }, |
| 242 | + }); |
| 243 | + }); |
| 244 | + |
| 245 | + test('narrows app-based simulator selection through the injected probe', async () => { |
| 246 | + // The composer builds on the caller-supplied probe; production wiring attaches the real |
| 247 | + // Apple one and src/platform-runtime-device-inventory.test.ts pins that composition. |
| 248 | + const installedApps: Record<string, string | undefined> = { 'sim-b': 'com.example.demo' }; |
| 249 | + const calls: string[] = []; |
| 250 | + const local = inventoryWorld({ |
| 251 | + apple: async () => source([simulator('sim-a'), simulator('sim-b')]), |
| 252 | + }); |
| 253 | + const gateways = createComposedDeviceInventoryGateways({ |
| 254 | + registry: local.registry, |
| 255 | + loadHost: local.loadHost, |
| 256 | + findInstalledApp: async (device) => { |
| 257 | + calls.push(device.id); |
| 258 | + return installedApps[device.id]; |
| 259 | + }, |
| 260 | + }); |
| 261 | + |
| 262 | + const selection = await withDeviceInventoryContext({ ...gateways, requestScope: scope }, () => |
| 263 | + resolveTargetDeviceSelection( |
| 264 | + { platform: 'ios' }, |
| 265 | + { |
| 266 | + appleSimulatorAppTarget: 'com.example.demo', |
| 267 | + }, |
| 268 | + ), |
| 269 | + ); |
| 270 | + |
| 271 | + expect(selection.device.id).toBe('sim-b'); |
| 272 | + expect(selection.reason).toBe('single-app-installed-local'); |
| 273 | + expect(selection.source).toBe('local'); |
| 274 | + expect(selection.candidateCount).toBe(1); |
| 275 | + expect([...calls].sort()).toEqual(['sim-a', 'sim-b']); |
| 276 | + }); |
| 277 | +}); |
| 278 | + |
| 279 | +function inventoryWorld( |
| 280 | + overrides: Partial<Record<Platform, () => Promise<DeviceInventorySource>>> = {}, |
| 281 | +) { |
| 282 | + const loaded: Platform[] = []; |
| 283 | + let hosts = 0; |
| 284 | + const modules: InventoryPlatformModule[] = PLATFORMS.map((family) => ({ |
| 285 | + family, |
| 286 | + loadInventory: async () => { |
| 287 | + loaded.push(family); |
| 288 | + return await (overrides[family]?.() ?? Promise.resolve(source([device(family)]))); |
| 289 | + }, |
| 290 | + })); |
| 291 | + return { |
| 292 | + registry: createPlatformModuleRegistry(modules), |
| 293 | + loadHost: async () => { |
| 294 | + hosts += 1; |
| 295 | + return host; |
| 296 | + }, |
| 297 | + hostLoads: () => hosts, |
| 298 | + sourceLoads: () => [...loaded], |
| 299 | + }; |
| 300 | +} |
| 301 | + |
| 302 | +function source(devices: readonly DeviceInfo[], failure?: Error): DeviceInventorySource { |
| 303 | + return { |
| 304 | + discover: async (_request: Readonly<DeviceInventoryRequest>) => { |
| 305 | + if (failure) throw failure; |
| 306 | + return devices; |
| 307 | + }, |
| 308 | + }; |
| 309 | +} |
| 310 | + |
| 311 | +function device(platform: Platform, id = `${platform}-device`): DeviceInfo { |
| 312 | + return { |
| 313 | + platform, |
| 314 | + id, |
| 315 | + name: id, |
| 316 | + kind: 'device', |
| 317 | + target: platform === 'vega' ? 'tv' : 'mobile', |
| 318 | + booted: true, |
| 319 | + }; |
| 320 | +} |
| 321 | + |
| 322 | +function simulator(id: string): DeviceInfo { |
| 323 | + return { |
| 324 | + platform: 'apple', |
| 325 | + id, |
| 326 | + name: id, |
| 327 | + kind: 'simulator', |
| 328 | + appleOs: 'ios', |
| 329 | + target: 'mobile', |
| 330 | + booted: true, |
| 331 | + }; |
| 332 | +} |
0 commit comments