Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 40 additions & 14 deletions packages/core/src/app-flow/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function agentDeviceInstallCommand(app: string, appPath: string): string {
export { agentDeviceOpenCommand } from './walk-commands.ts'

type AgentDeviceClient = Awaited<ReturnType<import('./host.ts').AppFlowAgentDeviceModule['createAgentDeviceClient']>>
type LaunchMode = 'native-run' | 'expo-go'
export type LaunchMode = 'native-run' | 'expo-go'

export interface CaptureDevice {
client: AgentDeviceClient
Expand Down Expand Up @@ -834,18 +834,42 @@ function installedAppLaunchTarget(app: string): string {
return formattedApp?.[1]?.trim() || app
}

async function listInstalledApps(
client: AgentDeviceClient,
platform: 'ios' | 'android'
/**
* The apps installed on the sweep's device.
*
* Pass the booted device's id. Before a session is open the daemon resolves the target from the
* request alone, so an unscoped listing fails with AMBIGUOUS_MATCH as soon as a second simulator
* is booted. That failure used to be swallowed as an empty list, and the sweep then reported the
* freshly built app as "not installed".
*/
export async function listInstalledApps(
client: Pick<AgentDeviceClient, 'apps'>,
platform: 'ios' | 'android',
deviceId?: string
): Promise<string[]> {
return (await client.apps.list({ platform }).catch(() => [])) as string[]
try {
return (await client.apps.list({ platform, ...deviceSelector(platform, deviceId) })) as string[]
} catch (error) {
log('apps.list', `failed — ${errorMessage(error)}`)
return []
}
}

/** The request field that pins a call to one device: `udid` on iOS, `serial` on Android. */
function deviceSelector(
platform: 'ios' | 'android',
deviceId: string | undefined
): { udid?: string; serial?: string } {
if (!deviceId) return {}
return platform === 'android' ? { serial: deviceId } : { udid: deviceId }
}

async function ensureExpoGoInstalled(
client: AgentDeviceClient,
ctx: CaptureContext
ctx: CaptureContext,
deviceId: string
): Promise<{ launchTarget: string; installPath: string | null }> {
const apps = await listInstalledApps(client, ctx.platform)
const apps = await listInstalledApps(client, ctx.platform, deviceId)
const existing = matchInstalledApp(apps, [EXPO_GO_APP_NAME, 'host.exp.Exponent'])
if (existing) {
log('Expo Go already installed', existing)
Expand All @@ -856,7 +880,8 @@ async function ensureExpoGoInstalled(
log('apps.install', appPath)
const result = await client.apps.install({
appPath,
platform: ctx.platform
platform: ctx.platform,
...deviceSelector(ctx.platform, deviceId)
})
return {
launchTarget: result.app || result.appId || EXPO_GO_APP_NAME,
Expand Down Expand Up @@ -918,7 +943,7 @@ function isMissingSessionError(error: unknown): boolean {
}

async function openAppSession(
client: AgentDeviceClient,
client: Pick<AgentDeviceClient, 'apps'>,
app: string,
options?: {
url?: string | null
Expand All @@ -945,6 +970,7 @@ async function openAppSession(
await client.apps.open({
app: launchTarget,
platform: 'ios',
...(options?.udid ? { udid: options.udid } : {}),
...(url ? { url } : {}),
...(options?.relaunch ? { relaunch: true } : {})
})
Expand Down Expand Up @@ -1209,8 +1235,8 @@ async function launchBoundApp(device: CaptureDevice, timing?: PhaseLedger): Prom
await dismissOpenInAppPrompt(device, device.launchUrl)
}

async function openInstalledApp(
client: AgentDeviceClient,
export async function openInstalledApp(
client: Pick<AgentDeviceClient, 'apps'>,
ctx: CaptureContext,
mode: LaunchMode,
options: {
Expand Down Expand Up @@ -1240,7 +1266,7 @@ async function openInstalledApp(
return { launchTarget, launchUrl, useCustomScheme: false }
}

const apps = await listInstalledApps(client, ctx.platform)
const apps = await listInstalledApps(client, ctx.platform, options.udid)
const nativeTarget = matchInstalledApp(apps, [ctx.bundleId, ctx.appName, ctx.slug])
if (!nativeTarget) {
const lookedFor =
Expand Down Expand Up @@ -1311,7 +1337,7 @@ async function resolveReusableNativeBuild(
)
return null
}
const apps = await listInstalledApps(client, ctx.platform)
const apps = await listInstalledApps(client, ctx.platform, deviceId)
const installed = matchInstalledApp(apps, [recorded])
if (!installed) {
log('checkInstalledBuild', `${recorded} is no longer installed — rebuilding`)
Expand Down Expand Up @@ -1401,7 +1427,7 @@ export async function bootDevice(ctx: CaptureContext): Promise<CaptureDevice> {
}
}
} else {
const expoGo = await ensureExpoGoInstalled(client, ctx)
const expoGo = await ensureExpoGoInstalled(client, ctx, bootResult.id)
expoGoTarget = expoGo.launchTarget
if (expoGo.installPath) {
setupCommands.push(agentDeviceInstallCommand(expoGo.launchTarget, expoGo.installPath))
Expand Down
63 changes: 63 additions & 0 deletions packages/core/test/app-flow-device-selection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import {
listInstalledApps,
openInstalledApp,
type CaptureContext
} from '../src/app-flow/capture.ts'

const UDID = 'BCB87837-5051-41B7-9E91-74DDAADA1C0F'

const ctx: CaptureContext = {
projectPath: '/tmp/project',
projectId: 'project',
scheme: 'example',
appName: 'Example',
bundleId: 'com.example.app',
slug: 'example',
platform: 'ios',
sessionName: 'session'
}

/** A daemon with two booted simulators: any request that does not name one is ambiguous. */
function twoBootedSimulators() {
const calls: Array<{ command: string; options: Record<string, unknown> }> = []
const requireDevice = (command: string, options: Record<string, unknown>) => {
calls.push({ command, options })
if (!options.udid) {
throw new Error('2 devices match this request equally; select one explicitly.')
}
}
const client = {
apps: {
list: async (options: Record<string, unknown> = {}) => {
requireDevice('apps.list', options)
return ['Example (com.example.app)']
},
open: async (options: Record<string, unknown> = {}) => {
requireDevice('apps.open', options)
return {}
}
}
}
return { client: client as never, calls }
}

test('listInstalledApps scopes the listing to the booted device', async () => {
const { client, calls } = twoBootedSimulators()
assert.deepEqual(await listInstalledApps(client, 'ios', UDID), ['Example (com.example.app)'])
assert.equal(calls[0]?.options.udid, UDID)
})

test('openInstalledApp finds and opens the native build on the booted device when another simulator is booted', async () => {
const { client, calls } = twoBootedSimulators()
const result = await openInstalledApp(client, ctx, 'native-run', { udid: UDID, metroPort: 8081 })
assert.equal(result.launchTarget, 'com.example.app')
assert.deepEqual(
calls.map((call) => [call.command, call.options.udid]),
[
['apps.list', UDID],
['apps.open', UDID]
]
)
})
Loading