diff --git a/bridge/native/NativeUIChildren.swift b/bridge/native/NativeUIChildren.swift new file mode 100644 index 000000000..8e2afcf9a --- /dev/null +++ b/bridge/native/NativeUIChildren.swift @@ -0,0 +1,11 @@ +struct ChildBatch { + let values: [Element] + let complete: Bool +} + +func captureChildValues(requestedCount: Int, copy: () -> [Element]?) -> ChildBatch { + guard requestedCount > 0 else { return ChildBatch(values: [], complete: true) } + let returned = copy() ?? [] + let values = Array(returned.prefix(requestedCount)) + return ChildBatch(values: values, complete: values.count == requestedCount) +} diff --git a/bridge/native/ReaNativeUI.swift b/bridge/native/ReaNativeUI.swift index d274a5f56..05a24ccf5 100644 --- a/bridge/native/ReaNativeUI.swift +++ b/bridge/native/ReaNativeUI.swift @@ -26,11 +26,12 @@ func childCount(_ element: AXUIElement) -> Int { var count = 0 return AXUIElementGetAttributeValueCount(element, kAXChildrenAttribute as CFString, &count) == .success ? count : 0 } -func children(_ element: AXUIElement, limit: Int) -> [AXUIElement] { - var value: CFArray? - let count = min(limit, childCount(element)) - if count == 0 { return [] } - return AXUIElementCopyAttributeValues(element, kAXChildrenAttribute as CFString, 0, count, &value) == .success ? value as? [AXUIElement] ?? [] : [] +func children(_ element: AXUIElement, count: Int) -> ChildBatch { + captureChildValues(requestedCount: count) { + var value: CFArray? + guard AXUIElementCopyAttributeValues(element, kAXChildrenAttribute as CFString, 0, count, &value) == .success else { return nil } + return value as? [AXUIElement] + } } func text(_ element: AXUIElement, _ key: String) -> Any { (attribute(element, key) as? String) ?? NSNull() as Any } func windowBounds(_ element: AXUIElement) -> CGRect? { @@ -115,9 +116,12 @@ func observe(_ request: Request) async throws -> [String: Any] { if path.count >= 32 { if totalChildren > 0 { truncated = true }; continue } let available = max(0, request.max_nodes - nodes.count - pending.count) let count = min(available, totalChildren) - let items = children(element, limit: count) + let batch = children(element, count: count) + if !batch.complete { truncated = true } if totalChildren > count { truncated = true } - for index in (0.. { if (executable === undefined) { root = await mkdtemp(join(tmpdir(), "rea-native-ui-")); const output = join(root, "observer"); + const main = join(root, "main.swift"); + await symlink( + fileURLToPath( + new URL("../../bridge/native/ReaNativeUI.swift", import.meta.url), + ), + main, + ); await execFileOutput( "/usr/bin/xcrun", [ @@ -21,8 +28,12 @@ export const createNativeUiHelperRuntime = () => { "-module-cache-path", join(root, "modules"), fileURLToPath( - new URL("../../bridge/native/ReaNativeUI.swift", import.meta.url), + new URL( + "../../bridge/native/NativeUIChildren.swift", + import.meta.url, + ), ), + main, "-o", output, ], diff --git a/tests/conformance/native/native-ui-children/main.swift b/tests/conformance/native/native-ui-children/main.swift new file mode 100644 index 000000000..d76181905 --- /dev/null +++ b/tests/conformance/native/native-ui-children/main.swift @@ -0,0 +1,18 @@ +let failed: ChildBatch = captureChildValues(requestedCount: 2) { nil } +precondition(failed.values.isEmpty, "AX retrieval failure must return no values") +precondition(!failed.complete, "AX retrieval failure must mark traversal incomplete") + +let short = captureChildValues(requestedCount: 3) { [10, 20] } +precondition(short.values == [10, 20], "Short retrieval must preserve returned children") +precondition(!short.complete, "Short retrieval must mark traversal incomplete") +var visited: [Int] = [] +for (index, value) in short.values.enumerated() { + visited.append(index * 10 + value) +} +precondition(visited == [10, 30], "Traversal must use actual returned indices") + +let complete = captureChildValues(requestedCount: 2) { [10, 20, 30] } +precondition(complete.values == [10, 20], "Retrieval must respect the requested node budget") +precondition(complete.complete, "A full bounded result must remain complete") + +print("Native UI child retrieval seam passed")