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
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ extension RunnerTests {
#if !os(macOS)
switch resolveBlockingSystemModal(deadline: deadline) {
case .resolved(let modal):
return runnerAlert(modal)
return runnerAlert(
root: modal.root,
ownerApp: modal.ownerApp,
actions: modal.actions,
source: .blockingSystemModal,
window: modal.window
)
case .unresolved:
return nil
case .absent:
Expand All @@ -49,15 +55,15 @@ extension RunnerTests {
// now-gone host as `activeApp`, and its `alerts` query raises
// kAXErrorServerNotFound. safeElementsQuery absorbs it and reports no alert.
if let alert = firstExistingElement(in: safeElementsQuery { activeApp.alerts.allElementsBoundByIndex }) {
return runnerAlert(root: alert, ownerApp: activeApp, source: .appAlert)
return runnerAlert(root: alert, ownerApp: activeApp, actions: actionableElements(in: alert), source: .appAlert)
}
if let dialog = firstExistingElement(in: safeElementsQuery {
activeApp.otherElements.matching(identifier: AlertSurface.safariDialogIdentifier).allElementsBoundByIndex
}) {
return runnerAlert(root: dialog, ownerApp: activeApp, source: .safariDialog)
return runnerAlert(root: dialog, ownerApp: activeApp, actions: actionableElements(in: dialog), source: .safariDialog)
}
if let popup = firstDismissPopupWindow(in: activeApp) {
return runnerAlert(root: popup, ownerApp: activeApp, source: .dismissPopup)
return runnerAlert(root: popup, ownerApp: activeApp, actions: actionableElements(in: popup), source: .dismissPopup)
}
return nil
}
Expand Down Expand Up @@ -232,30 +238,18 @@ extension RunnerTests {
operation()
}

private func runnerAlert(_ modal: ResolvedBlockingSystemModal) -> RunnerAlert? {
let buttons = modal.actions.filter { isEnabledElement($0) }
guard !buttons.isEmpty else {
return nil
}
return RunnerAlert(
root: modal.root,
ownerApp: modal.ownerApp,
buttons: buttons,
source: .blockingSystemModal,
window: modal.window
)
}

private func runnerAlert(
root: XCUIElement,
ownerApp: XCUIApplication,
source: AlertSurfaceSource
actions: [XCUIElement],
source: AlertSurfaceSource,
window: XCUIElement? = nil
) -> RunnerAlert? {
let buttons = actionableElements(in: root).filter { isEnabledElement($0) }
let buttons = actions.filter { isEnabledElement($0) }
guard !buttons.isEmpty else {
return nil
}
return RunnerAlert(root: root, ownerApp: ownerApp, buttons: buttons, source: source, window: nil)
return RunnerAlert(root: root, ownerApp: ownerApp, buttons: buttons, source: source, window: window)
}

private func firstExistingElement(in elements: [XCUIElement]) -> XCUIElement? {
Expand Down Expand Up @@ -346,11 +340,7 @@ extension RunnerTests {
}

private func isEnabledElement(_ element: XCUIElement) -> Bool {
var enabled = false
_ = RunnerObjCExceptionCatcher.catchException({
enabled = element.exists && element.isEnabled
})
return enabled
safely("ALERT_BUTTON_ENABLED", false) { element.exists && element.isEnabled }
}

// A snapshot can expose an alert's button a beat before the owning app has made it
Expand Down Expand Up @@ -379,10 +369,6 @@ extension RunnerTests {
return override(deadline)
}
#endif
var hittable = false
_ = RunnerObjCExceptionCatcher.catchException({
hittable = button.exists && button.isHittable
})
return hittable
return safely("ALERT_BUTTON_HITTABLE", false) { button.exists && button.isHittable }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ extension RunnerTests {
private func safeIsActionableCandidate(_ candidate: XCUIElement, seen: inout Set<String>) -> Bool {
safely("MODAL_ACTION", false) {
if !candidate.exists || !candidate.isHittable { return false }
if !actionableTypes.contains(candidate.elementType) { return false }
let frame = candidate.frame
if frame.isNull || frame.isEmpty { return false }
let key = "\(candidate.elementType.rawValue)-\(frame.origin.x)-\(frame.origin.y)-\(frame.size.width)-\(frame.size.height)-\(candidate.label)"
Expand Down
41 changes: 4 additions & 37 deletions packages/daemon/src/execution/handlers/snapshot-alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
MAX_ALERT_WINDOW_MS,
} from '@agent-device/contracts/alert-contract';
import { AppError } from '@agent-device/kernel/errors';
import type { BoundDeviceRuntime } from '@agent-device/contracts/platform-runtime';
import {
alertAcceptUse,
alertDismissUse,
Expand Down Expand Up @@ -74,53 +73,21 @@ async function resolveBoundAlertRuntime(
if (action === 'wait') {
const admission = await admitRuntimeUse({ ...shared, use: alertWaitUse });
if (admission.type === 'response') return { ok: false, response: admission.response };
const runtime = admission.runtime;
return { ok: true, execute: (input) => executeAwaitAlert(runtime, input) };
return { ok: true, execute: (input) => admission.runtime.operations.awaitAlert(input) };
}
if (action === 'accept') {
const admission = await admitRuntimeUse({ ...shared, use: alertAcceptUse });
if (admission.type === 'response') return { ok: false, response: admission.response };
const runtime = admission.runtime;
return { ok: true, execute: (input) => executeAcceptAlert(runtime, input) };
return { ok: true, execute: (input) => admission.runtime.operations.acceptAlert(input) };
}
if (action === 'dismiss') {
const admission = await admitRuntimeUse({ ...shared, use: alertDismissUse });
if (admission.type === 'response') return { ok: false, response: admission.response };
const runtime = admission.runtime;
return { ok: true, execute: (input) => executeDismissAlert(runtime, input) };
return { ok: true, execute: (input) => admission.runtime.operations.dismissAlert(input) };
}
const admission = await admitRuntimeUse({ ...shared, use: alertReadUse });
if (admission.type === 'response') return { ok: false, response: admission.response };
const runtime = admission.runtime;
return { ok: true, execute: (input) => executeReadAlert(runtime, input) };
}

async function executeReadAlert(
runtime: BoundDeviceRuntime<typeof alertReadUse>,
input: AlertRuntimeInput,
): Promise<Record<string, unknown>> {
return await runtime.operations.readAlert(input);
}

async function executeAwaitAlert(
runtime: BoundDeviceRuntime<typeof alertWaitUse>,
input: AlertRuntimeInput,
): Promise<Record<string, unknown>> {
return await runtime.operations.awaitAlert(input);
}

async function executeAcceptAlert(
runtime: BoundDeviceRuntime<typeof alertAcceptUse>,
input: AlertRuntimeInput,
): Promise<Record<string, unknown>> {
return await runtime.operations.acceptAlert(input);
}

async function executeDismissAlert(
runtime: BoundDeviceRuntime<typeof alertDismissUse>,
input: AlertRuntimeInput,
): Promise<Record<string, unknown>> {
return await runtime.operations.dismissAlert(input);
return { ok: true, execute: (input) => admission.runtime.operations.readAlert(input) };
}

export async function handleAlertCommand(
Expand Down
Loading