|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { |
| 4 | + isApplePlatform, |
| 5 | + DEVICE_TARGETS, |
| 6 | + PLATFORMS, |
| 7 | + type DeviceInfo, |
| 8 | + type DeviceKind, |
| 9 | + type DeviceTarget, |
| 10 | +} from '../../kernel/device.ts'; |
| 11 | +import { |
| 12 | + ANDROID_EMULATOR, |
| 13 | + ANDROID_TV_DEVICE, |
| 14 | + IOS_DEVICE, |
| 15 | + IOS_SIMULATOR, |
| 16 | + IPADOS_SIMULATOR, |
| 17 | + LINUX_DEVICE, |
| 18 | + MACOS_DEVICE, |
| 19 | + makeSession, |
| 20 | + TVOS_SIMULATOR, |
| 21 | + VISIONOS_SIMULATOR, |
| 22 | + WEB_DESKTOP_DEVICE, |
| 23 | +} from '../../__tests__/test-utils/index.ts'; |
| 24 | +import { getPlugin, tryGetPlugin } from '../../core/platform-plugin/plugin.ts'; |
| 25 | +import { registerBuiltinPlatformPlugins } from '../../core/interactors/register-builtins.ts'; |
| 26 | +import { |
| 27 | + withRequestPlatformProviderScope, |
| 28 | + type PlatformGatedProviderResolverKey, |
| 29 | + type PlatformProviderResolvers, |
| 30 | +} from '../request-platform-providers.ts'; |
| 31 | +import type { DaemonRequest } from '../types.ts'; |
| 32 | + |
| 33 | +// Phase 3 step b.3 (issue #974) parity gate for the daemon request-scope provider |
| 34 | +// facet. The per-platform GATE that each descriptor in `request-platform-providers.ts` |
| 35 | +// open-coded (`device.platform === 'android'`, `isApplePlatform(...)`, etc.) now flows |
| 36 | +// through the PlatformPlugin `providers.platformGatedResolvers` facet. The daemon still |
| 37 | +// OWNS the resolver invocation, wrapper composition, and concurrency isolation — only |
| 38 | +// the gate moved to data. An INDEPENDENT verbatim copy of the former gates below is the |
| 39 | +// BEFORE oracle, checked at the facet level AND end-to-end through |
| 40 | +// `withRequestPlatformProviderScope`. |
| 41 | + |
| 42 | +registerBuiltinPlatformPlugins(); |
| 43 | + |
| 44 | +// The platform-gated resolver keys, and the two ungated resolvers (no platform gate; |
| 45 | +// they apply on every platform and are NOT part of the facet). |
| 46 | +const GATED_KEYS: PlatformGatedProviderResolverKey[] = [ |
| 47 | + 'androidAdbProvider', |
| 48 | + 'appleRunnerProvider', |
| 49 | + 'appleToolProvider', |
| 50 | + 'linuxToolProvider', |
| 51 | + 'webProvider', |
| 52 | +]; |
| 53 | +const UNGATED_KEYS = ['appLogProvider', 'recordingProvider'] as const; |
| 54 | + |
| 55 | +// --- INDEPENDENT verbatim copy of the former per-descriptor platform gates --- |
| 56 | +function gatedResolversByHand(device: DeviceInfo): Set<PlatformGatedProviderResolverKey> { |
| 57 | + const applies = new Set<PlatformGatedProviderResolverKey>(); |
| 58 | + if (device.platform === 'android') applies.add('androidAdbProvider'); // was `!== 'android'` |
| 59 | + if (isApplePlatform(device.platform)) { |
| 60 | + applies.add('appleRunnerProvider'); // was `!isApplePlatform(...)` |
| 61 | + applies.add('appleToolProvider'); // was `!isApplePlatform(...)` |
| 62 | + } |
| 63 | + if (device.platform === 'linux') applies.add('linuxToolProvider'); // was `!== 'linux'` |
| 64 | + if (device.platform === 'web') applies.add('webProvider'); // was `!== 'web'` |
| 65 | + return applies; |
| 66 | +} |
| 67 | + |
| 68 | +// --- the exhaustive synthetic device matrix (every platform x kind x target) --- |
| 69 | +const DEVICE_KINDS_ALL: DeviceKind[] = ['simulator', 'emulator', 'device']; |
| 70 | +const DEVICE_TARGETS_ALL: (DeviceTarget | undefined)[] = [undefined, ...DEVICE_TARGETS]; |
| 71 | + |
| 72 | +function buildDeviceMatrix(): DeviceInfo[] { |
| 73 | + const devices: DeviceInfo[] = []; |
| 74 | + for (const platform of PLATFORMS) { |
| 75 | + for (const kind of DEVICE_KINDS_ALL) { |
| 76 | + for (const target of DEVICE_TARGETS_ALL) { |
| 77 | + devices.push({ |
| 78 | + platform, |
| 79 | + id: `${platform}-${kind}-${target ?? 'none'}`, |
| 80 | + name: `${platform} ${kind} ${target ?? 'none'}`, |
| 81 | + kind, |
| 82 | + ...(target ? { target } : {}), |
| 83 | + booted: true, |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return devices; |
| 89 | +} |
| 90 | + |
| 91 | +const SAMPLE_DEVICES: DeviceInfo[] = [ |
| 92 | + ANDROID_EMULATOR, |
| 93 | + ANDROID_TV_DEVICE, |
| 94 | + IOS_DEVICE, |
| 95 | + IOS_SIMULATOR, |
| 96 | + IPADOS_SIMULATOR, |
| 97 | + LINUX_DEVICE, |
| 98 | + MACOS_DEVICE, |
| 99 | + TVOS_SIMULATOR, |
| 100 | + VISIONOS_SIMULATOR, |
| 101 | + WEB_DESKTOP_DEVICE, |
| 102 | + ...buildDeviceMatrix(), |
| 103 | +]; |
| 104 | + |
| 105 | +test('providers.platformGatedResolvers facet is byte-identical to the former hand gate', () => { |
| 106 | + for (const device of SAMPLE_DEVICES) { |
| 107 | + const facet = tryGetPlugin(device.platform)?.providers; |
| 108 | + const expected = gatedResolversByHand(device); |
| 109 | + for (const key of GATED_KEYS) { |
| 110 | + const applies = facet?.platformGatedResolvers.includes(key) ?? false; |
| 111 | + assert.equal(applies, expected.has(key), `gate for ${key} on ${device.id}`); |
| 112 | + } |
| 113 | + } |
| 114 | +}); |
| 115 | + |
| 116 | +test('every family carries the providers facet with the resolvers it owns', () => { |
| 117 | + // Apple owns ios + macos (SAME plugin instance). Unlike appLog/perf, EVERY family |
| 118 | + // owns at least one platform-specific resolver, so all four carry the facet. |
| 119 | + assert.equal(getPlugin('apple'), getPlugin('apple')); |
| 120 | + assert.deepEqual([...getPlugin('apple').providers!.platformGatedResolvers].sort(), [ |
| 121 | + 'appleRunnerProvider', |
| 122 | + 'appleToolProvider', |
| 123 | + ]); |
| 124 | + assert.deepEqual( |
| 125 | + [...getPlugin('android').providers!.platformGatedResolvers], |
| 126 | + ['androidAdbProvider'], |
| 127 | + ); |
| 128 | + assert.deepEqual( |
| 129 | + [...getPlugin('linux').providers!.platformGatedResolvers], |
| 130 | + ['linuxToolProvider'], |
| 131 | + ); |
| 132 | + assert.deepEqual([...getPlugin('web').providers!.platformGatedResolvers], ['webProvider']); |
| 133 | +}); |
| 134 | + |
| 135 | +// End-to-end routing proof: drive the REAL `withRequestPlatformProviderScope` with a |
| 136 | +// spy for every resolver and assert exactly the gated resolvers the former hand gate |
| 137 | +// admitted are invoked (plus the two ungated resolvers, on every platform). Each spy |
| 138 | +// returns `undefined`, so no wrapper is composed — but the resolver is still called iff |
| 139 | +// its gate passed, which is precisely what the former `device.platform === …` branch |
| 140 | +// decided. Breaking the facet flips which resolvers run and fails this test. |
| 141 | +test('withRequestPlatformProviderScope invokes exactly the resolvers the former gate did', async () => { |
| 142 | + for (const device of SAMPLE_DEVICES) { |
| 143 | + const invoked = new Set<string>(); |
| 144 | + const spy = (key: string) => (): undefined => { |
| 145 | + invoked.add(key); |
| 146 | + return undefined; |
| 147 | + }; |
| 148 | + const providers: PlatformProviderResolvers = { |
| 149 | + androidAdbProvider: spy('androidAdbProvider'), |
| 150 | + appleRunnerProvider: spy('appleRunnerProvider'), |
| 151 | + appleToolProvider: spy('appleToolProvider'), |
| 152 | + linuxToolProvider: spy('linuxToolProvider'), |
| 153 | + webProvider: spy('webProvider'), |
| 154 | + appLogProvider: spy('appLogProvider'), |
| 155 | + recordingProvider: spy('recordingProvider'), |
| 156 | + }; |
| 157 | + |
| 158 | + await withRequestPlatformProviderScope( |
| 159 | + { |
| 160 | + req: request('snapshot'), |
| 161 | + existingSession: makeSession(`prov-${device.id}`, { device }), |
| 162 | + providers, |
| 163 | + }, |
| 164 | + async () => {}, |
| 165 | + ); |
| 166 | + |
| 167 | + const gatedInvoked = new Set(GATED_KEYS.filter((key) => invoked.has(key))); |
| 168 | + assert.deepEqual( |
| 169 | + [...gatedInvoked].sort(), |
| 170 | + [...gatedResolversByHand(device)].sort(), |
| 171 | + `gated resolvers invoked for ${device.id}`, |
| 172 | + ); |
| 173 | + for (const key of UNGATED_KEYS) { |
| 174 | + assert.ok(invoked.has(key), `ungated resolver ${key} must run for ${device.id}`); |
| 175 | + } |
| 176 | + } |
| 177 | +}); |
| 178 | + |
| 179 | +function request(command: string): DaemonRequest { |
| 180 | + return { |
| 181 | + token: 'test-token', |
| 182 | + session: 'default', |
| 183 | + command, |
| 184 | + positionals: [], |
| 185 | + flags: {}, |
| 186 | + meta: { requestId: `req-${command}` }, |
| 187 | + }; |
| 188 | +} |
0 commit comments