|
| 1 | +import { promises as fs } from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { test } from 'vitest'; |
| 4 | +import assert from 'node:assert/strict'; |
| 5 | + |
| 6 | +import type { DeviceInfo } from '@agent-device/kernel/device'; |
| 7 | +import { resolveIosSimulatorExpoDevClientScheme } from '../app-resolution.ts'; |
| 8 | +import { withFakeAppleTool, type FakeAppleToolResponse } from '../../__tests__/fake-apple-tool.ts'; |
| 9 | +import { mkdtempForTest } from '../../__tests__/tmp-dir.ts'; |
| 10 | +import { IOS_TEST_SIMULATOR } from './apple-core-stub-helpers.ts'; |
| 11 | + |
| 12 | +const DEV_CLIENT_BUNDLE_ID = 'com.example.devclient'; |
| 13 | + |
| 14 | +type InstalledApps = Readonly<{ |
| 15 | + listing: Record<string, unknown>; |
| 16 | + appPaths: Map<string, string>; |
| 17 | + schemesByPlist: Map<string, string[]>; |
| 18 | +}>; |
| 19 | + |
| 20 | +async function installApps(apps: Record<string, string[]>): Promise<InstalledApps> { |
| 21 | + const root = await mkdtempForTest('expo-dev-client-schemes-'); |
| 22 | + const listing: Record<string, unknown> = {}; |
| 23 | + const appPaths = new Map<string, string>(); |
| 24 | + const schemesByPlist = new Map<string, string[]>(); |
| 25 | + for (const [bundleId, schemes] of Object.entries(apps)) { |
| 26 | + const appPath = path.join(root, `${bundleId}.app`); |
| 27 | + await fs.mkdir(appPath, { recursive: true }); |
| 28 | + await fs.writeFile(path.join(appPath, 'Info.plist'), ''); |
| 29 | + listing[bundleId] = { ApplicationType: 'User', CFBundleDisplayName: bundleId, Path: appPath }; |
| 30 | + appPaths.set(bundleId, appPath); |
| 31 | + schemesByPlist.set(path.join(appPath, 'Info.plist'), schemes); |
| 32 | + } |
| 33 | + return { listing, appPaths, schemesByPlist }; |
| 34 | +} |
| 35 | + |
| 36 | +function answerContainer(installed: InstalledApps, bundleId: string | undefined, fails: boolean) { |
| 37 | + const appPath = bundleId === undefined ? undefined : installed.appPaths.get(bundleId); |
| 38 | + if (fails || appPath === undefined) return { exitCode: 2, stderr: 'no app' }; |
| 39 | + return `${appPath}\n`; |
| 40 | +} |
| 41 | + |
| 42 | +function answerSchemes(installed: InstalledApps, plistPath: string | undefined): string { |
| 43 | + const schemes = installed.schemesByPlist.get(plistPath ?? '') ?? []; |
| 44 | + return JSON.stringify({ CFBundleURLTypes: [{ CFBundleURLSchemes: schemes }] }); |
| 45 | +} |
| 46 | + |
| 47 | +/** Installs each app with the given URL schemes and answers the simctl/plutil probes for them. */ |
| 48 | +async function createInstalledApps( |
| 49 | + apps: Record<string, string[]>, |
| 50 | + options: { containerFails?: boolean } = {}, |
| 51 | +): Promise<(args: string[]) => FakeAppleToolResponse> { |
| 52 | + const installed = await installApps(apps); |
| 53 | + return (args) => { |
| 54 | + const [tool, command] = args; |
| 55 | + if (tool === 'simctl' && command === 'listapps') return JSON.stringify(installed.listing); |
| 56 | + if (tool === 'simctl' && command === 'get_app_container') { |
| 57 | + return answerContainer(installed, args[3], options.containerFails === true); |
| 58 | + } |
| 59 | + if (tool === 'plutil' && command === '-convert') return answerSchemes(installed, args[5]); |
| 60 | + return { stderr: `unexpected xcrun args: ${args.join(' ')}`, exitCode: 1 }; |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +test('an expo-dev-client resolves to its exp+ scheme when it is the scheme’s only owner', async () => { |
| 65 | + const tool = await createInstalledApps({ |
| 66 | + [DEV_CLIENT_BUNDLE_ID]: ['devclient', 'com.example.devclient', 'exp+dev-slug'], |
| 67 | + 'com.example.other': ['other'], |
| 68 | + }); |
| 69 | + |
| 70 | + await withFakeAppleTool(tool, async () => { |
| 71 | + assert.equal( |
| 72 | + await resolveIosSimulatorExpoDevClientScheme(IOS_TEST_SIMULATOR, DEV_CLIENT_BUNDLE_ID), |
| 73 | + 'exp+dev-slug', |
| 74 | + ); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +test.each([ |
| 79 | + ['a bare React Native app', { [DEV_CLIENT_BUNDLE_ID]: ['devclient'] }], |
| 80 | + ['an app with two exp+ schemes', { [DEV_CLIENT_BUNDLE_ID]: ['exp+dev-slug', 'exp+other-slug'] }], |
| 81 | + [ |
| 82 | + 'a scheme another installed variant also owns', |
| 83 | + { |
| 84 | + [DEV_CLIENT_BUNDLE_ID]: ['exp+dev-slug'], |
| 85 | + 'com.example.devclient.preview': ['exp+dev-slug'], |
| 86 | + }, |
| 87 | + ], |
| 88 | + ['a bare exp+ prefix', { [DEV_CLIENT_BUNDLE_ID]: ['exp+'] }], |
| 89 | +])('%s has no dev-client scheme', async (_name, apps) => { |
| 90 | + const tool = await createInstalledApps(apps); |
| 91 | + |
| 92 | + await withFakeAppleTool(tool, async () => { |
| 93 | + assert.equal( |
| 94 | + await resolveIosSimulatorExpoDevClientScheme(IOS_TEST_SIMULATOR, DEV_CLIENT_BUNDLE_ID), |
| 95 | + undefined, |
| 96 | + ); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +test('an app the simulator cannot locate has no dev-client scheme', async () => { |
| 101 | + const tool = await createInstalledApps( |
| 102 | + { [DEV_CLIENT_BUNDLE_ID]: ['exp+dev-slug'] }, |
| 103 | + { containerFails: true }, |
| 104 | + ); |
| 105 | + |
| 106 | + await withFakeAppleTool(tool, async () => { |
| 107 | + assert.equal( |
| 108 | + await resolveIosSimulatorExpoDevClientScheme(IOS_TEST_SIMULATOR, DEV_CLIENT_BUNDLE_ID), |
| 109 | + undefined, |
| 110 | + ); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +test('a physical device is never probed', async () => { |
| 115 | + const physical: DeviceInfo = { ...IOS_TEST_SIMULATOR, kind: 'device' }; |
| 116 | + |
| 117 | + await withFakeAppleTool( |
| 118 | + () => ({ stderr: 'must not run', exitCode: 1 }), |
| 119 | + async ({ calls }) => { |
| 120 | + assert.equal( |
| 121 | + await resolveIosSimulatorExpoDevClientScheme(physical, DEV_CLIENT_BUNDLE_ID), |
| 122 | + undefined, |
| 123 | + ); |
| 124 | + assert.deepEqual(calls, []); |
| 125 | + }, |
| 126 | + ); |
| 127 | +}); |
0 commit comments