(params: {
req: DaemonRequest;
sessionName: string;
sessionStore: SessionStore;
})
| 36 | 'macOS appstate requires an active session on the target device. Run open first (for example: open --session macos --platform macos "System Settings").'; |
| 37 | |
| 38 | async function handleAppStateCommand(params: { |
| 39 | req: DaemonRequest; |
| 40 | sessionName: string; |
| 41 | sessionStore: SessionStore; |
| 42 | }): Promise<DaemonResponse> { |
| 43 | const { req, sessionName, sessionStore } = params; |
| 44 | const session = sessionStore.get(sessionName); |
| 45 | const flags = req.flags ?? {}; |
| 46 | const normalizedPlatform = flags.platform; |
| 47 | |
| 48 | if (!session && hasExplicitSessionFlag(flags)) { |
| 49 | const message = |
| 50 | normalizedPlatform === 'ios' |
| 51 | ? `No active session "${sessionName}". Run open with --session ${sessionName} first.` |
| 52 | : `No active session "${sessionName}". Run open with --session ${sessionName} first, or omit --session to query by device selector.`; |
| 53 | return errorResponse('SESSION_NOT_FOUND', message); |
| 54 | } |
| 55 | |
| 56 | const guard = requireSessionOrExplicitSelector('appstate', session, flags); |
| 57 | if (guard) return guard; |
| 58 | |
| 59 | const shouldUseSessionStateForApple = |
| 60 | isApplePlatform(session?.device.platform) && selectorTargetsSessionDevice(flags, session); |
| 61 | const targetsIos = normalizedPlatform === 'ios'; |
| 62 | const targetsMacOs = normalizedPlatform === 'macos'; |
| 63 | |
| 64 | if (targetsIos && !shouldUseSessionStateForApple) { |
| 65 | return errorResponse('SESSION_NOT_FOUND', IOS_APPSTATE_SESSION_REQUIRED_MESSAGE); |
| 66 | } |
| 67 | if (targetsMacOs && !shouldUseSessionStateForApple) { |
| 68 | return errorResponse('SESSION_NOT_FOUND', MACOS_APPSTATE_SESSION_REQUIRED_MESSAGE); |
| 69 | } |
| 70 | |
| 71 | if (shouldUseSessionStateForApple && session) { |
| 72 | const appName = session.appName ?? session.appBundleId; |
| 73 | if (!session.appName && !session.appBundleId) { |
| 74 | if ( |
| 75 | isMacOs(session.device) && |
| 76 | session.surface && |
| 77 | session.surface !== 'app' && |
| 78 | session.surface !== 'frontmost-app' |
| 79 | ) { |
| 80 | return { |
| 81 | ok: true, |
| 82 | data: { |
| 83 | platform: publicPlatformString(session.device), |
| 84 | appName: session.surface, |
| 85 | appBundleId: session.appBundleId, |
| 86 | source: 'session', |
| 87 | surface: session.surface, |
| 88 | }, |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | const sessionPlatform = isMacOs(session.device) ? 'macOS' : 'iOS'; |
| 93 | return errorResponse( |
| 94 | 'COMMAND_FAILED', |
| 95 | `No foreground app is tracked for this ${sessionPlatform} session. Open an app in the session, then retry appstate.`, |
no test coverage detected