Files
claude-code-haha/native/cu-helper/Tests/CuHelperTests/InjectionPathParityTests.swift
程序员阿江(Relakkes) b8a90626ce feat(computer-use): native macOS engine, on top of main and nothing else
Rebuilt against main so the branch carries the Computer Use work and no other
divergence. Three unrelated efforts had been sitting uncommitted in this
worktree and were swept into an earlier commit; they are preserved on
cu-worktree-full-backup and belong on their own branches — adapter control
credentials, Electron asar sealing, and the sidecar code-loading audit. Every
file outside Computer Use now matches main exactly.

The engine
  A Swift helper drives apps through the accessibility tree, with coordinate
  actuation for the Chromium and Electron apps whose tree is a bare window
  frame. Ten primitives matching the shape Codex uses, so an app's guidance and
  the model's habits transfer.

  Coordinate actions resolve their target window once and refuse when none can
  be named. The unbound event they used to fall back to is discarded by custom
  renderers, so a minimized target produced a whole session of "Action
  completed" with nothing behind it.

  Input acceptance is established for typing and key presses as well as clicks:
  each MCP call is seconds apart, so the keyboard cannot inherit the focus a
  click established. The synthetic focus notification is gated on the target
  not already being active — sent unconditionally it names window 0 at an app
  that already owns a key window, and nine window-bound clicks were discarded
  with the traffic lights fully lit.

State the model can trust
  An off-screen target says so, and says which tools still reach it: element
  actions need no on-screen geometry, so an app with a real tree can still be
  driven from the Dock. A fully covered window is recovered once, then left
  alone — burying it again is the user wanting their screen back. A repeated
  capture is reported with the cause that actually applies rather than both,
  because coverage is something we compute.

Signing
  The helper is signed under a stable identity before electron-builder sees it,
  and excluded from re-signing: macOS ties Accessibility and Screen Recording
  grants to the signing identity, so rotating it drops both on every update.

Discoverability
  The desktop slash menu falls back to a directory scan while a session's CLI
  has not started, which is when the menu is first opened. Built-ins and
  bundled skills live in the binary, so /computer-use was absent until after
  the first message.
2026-08-05 22:06:23 +08:00

108 lines
5.5 KiB
Swift

import XCTest
/// Every synthetic mouse event the helper emits must go through the
/// window-bound builder.
///
/// This test exists because of a real, expensive failure: the window-targeting
/// fix was implemented in `Injection.makeMouse`, but a COORDINATE click never
/// goes through `Injection` — `CommandRouter.handleClick` calls
/// `AXAction.clickAtPoint`, which falls through to `AXAction.clickPoint`, which
/// had its **own** parallel event builder that was never updated. The product
/// shipped three times still broken while the tests were green, because the
/// tests only ever exercised the half that was fixed.
///
/// So this asserts on the SOURCE: no file may construct a raw mouse
/// `CGEvent(mouseEventSource:mouseType:…)` without also consulting
/// `WindowTargetedEvent` in the same file. A new call site that forgets the
/// window binding fails here rather than silently degrading on Chromium apps.
final class InjectionPathParityTests: XCTestCase {
private func sourceFiles() throws -> [(name: String, text: String)] {
// Tests run from the package root; walk to the sources directory.
let root = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent() // CuHelperTests
.deletingLastPathComponent() // Tests
.deletingLastPathComponent() // cu-helper
.appendingPathComponent("Sources/cu-helper")
let names = try FileManager.default.contentsOfDirectory(atPath: root.path)
return try names.filter { $0.hasSuffix(".swift") }.map {
($0, try String(contentsOf: root.appendingPathComponent($0), encoding: .utf8))
}
}
func testEveryRawMouseEventBuilderAlsoUsesWindowTargeting() throws {
var offenders: [String] = []
for file in try sourceFiles() {
guard file.text.contains("mouseEventSource:") else { continue }
// WindowTargetedEvent itself is the fallback of last resort.
if file.name == "WindowTargetedEvent.swift" { continue }
if !file.text.contains("WindowTargetedEvent") {
offenders.append(file.name)
}
}
XCTAssertEqual(
offenders,
[],
"these build raw mouse events without window binding — Chromium/CEF apps will silently ignore them"
)
}
/// A burst is allocated up front (so a mid-burst failure cannot strand a
/// held button) but must carry SEND-time timestamps, so it has to be posted
/// through the stamping helper rather than a bare `postToPid`.
///
/// Scoped to loop bodies over an allocated `events` array. The held-input
/// teardown paths post a single retained event on a cleanup path where a
/// stale timestamp is harmless and re-stamping would obscure when the
/// button was actually pressed.
func testMouseBurstsPostThroughTheTimestampingHelper() throws {
var offenders: [String] = []
for file in try sourceFiles() where file.text.contains("mouseEventSource:") {
if file.name == "WindowTargetedEvent.swift" { continue }
let lines = file.text.split(separator: "\n", omittingEmptySubsequences: false)
for (index, line) in lines.enumerated() {
guard line.trimmingCharacters(in: .whitespaces) == "event.postToPid(pid)" else { continue }
// Look back a few lines for the burst-loop signature.
let window = lines[max(0, index - 3)..<index].joined(separator: " ")
if window.contains("for event in events") {
offenders.append("\(file.name):\(index + 1)")
}
}
}
XCTAssertEqual(offenders, [], "post burst events via WindowTargetedEvent.post")
}
/// Coordinate actuation must REFUSE when it cannot name a window, never fall
/// through to an unbound event.
///
/// Same class of failure as the one above, one layer down. `makeMouse` used
/// to resolve the window itself and, on failure, quietly build a raw event
/// instead — which Chromium discards. Against a minimized target that made
/// every click and every keystroke of a session a no-op that still reported
/// "Action completed". The unbound builder is still there for callers that
/// legitimately have no target, so the guarantee has to be that the
/// coordinate entry points ask for a window first.
func testCoordinateActuationRefusesWhenNoWindowCanBeNamed() throws {
let axAction = try sourceFiles().first { $0.name == "AXAction.swift" }
let text = try XCTUnwrap(axAction?.text, "AXAction.swift is missing")
XCTAssertTrue(
text.contains("requireBindableWindow"),
"coordinate actuation must resolve a window through the throwing guard"
)
// Both coordinate entry points — a drag that silently degrades is the
// same bug wearing a different verb.
let guardedCalls = text.components(separatedBy: "try requireBindableWindow").count - 1
XCTAssertGreaterThanOrEqual(
guardedCalls,
2,
"clickPoint and drag must each resolve their window through the guard"
)
// And the refusal must name the off-screen case specifically, because
// "that did not work" sends the model looking for better coordinates.
XCTAssertTrue(
text.contains("target_window_offscreen"),
"the refusal must distinguish an off-screen target from a missed coordinate"
)
}
}