Files
claude-code-haha/native/cu-helper/Tests/CuHelperTests/VirtualCursorStyleTests.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

111 lines
5.1 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import CoreGraphics
import XCTest
@testable import cc_haha_computer_use
/// The arrow's contract is geometric, and one property carries the whole point
/// of having a pointer at all: the TIP must sit exactly at the origin, because
/// the layer's `position` is set to the pixel being acted on. If the tip drifts
/// off (0,0), the cursor visibly points somewhere the click will not land —
/// which is worse than no cursor, since the user would trust it.
final class VirtualCursorStyleTests: XCTestCase {
func testTipSitsExactlyAtTheOrigin() {
let box = VirtualCursorStyle.arrowPath().boundingBox
XCTAssertEqual(box.minX, 0, accuracy: 0.0001)
XCTAssertEqual(box.minY, 0, accuracy: 0.0001)
}
/// Scaling must not move the hot-spot or distort the aspect ratio — the
/// silhouette has to stay recognisably the system pointer at any size.
func testScalesAboutTheTipKeepingAspectRatio() {
let small = VirtualCursorStyle.arrowPath(height: 11).boundingBox
let large = VirtualCursorStyle.arrowPath(height: 44).boundingBox
XCTAssertEqual(small.minX, 0, accuracy: 0.0001)
XCTAssertEqual(large.minX, 0, accuracy: 0.0001)
XCTAssertEqual(large.height / small.height, 4, accuracy: 0.01)
XCTAssertEqual(
large.width / small.width,
4,
accuracy: 0.01,
"width must scale with height or the arrow skews"
)
}
/// A pointer is taller than it is wide; a shape that fails this is a blob,
/// not an arrow — which is exactly the regression this file exists to stop.
func testSilhouetteIsPointerShapedNotRound() {
let box = VirtualCursorStyle.arrowPath().boundingBox
XCTAssertGreaterThan(box.height, box.width)
XCTAssertLessThan(box.width / box.height, 0.8)
}
/// The path must actually enclose area just below the tip — a degenerate or
/// mis-ordered polygon can still pass a bounding-box check while rendering
/// as nothing.
func testPathEnclosesAreaBeneathTheTip() {
let path = VirtualCursorStyle.arrowPath()
XCTAssertTrue(path.contains(CGPoint(x: 2, y: 6)))
// Far to the right of the shoulder is outside the silhouette.
XCTAssertFalse(path.contains(CGPoint(x: 20, y: 2)))
}
/// Arrow is the default; the orb is opt-in. Codex ships both, but only one
/// of them can point at a pixel.
func testArrowIsTheDefaultStyle() {
XCTAssertEqual(VirtualCursorStyle(rawValue: "arrow"), .arrow)
XCTAssertEqual(VirtualCursorStyle(rawValue: "fog"), .fog)
XCTAssertNil(VirtualCursorStyle(rawValue: "orb"))
}
/// The idle motions exist because a model turn freezes the screen for 8–14
/// seconds and a still cursor cannot be told apart from a hung one. Their
/// numbers are a balance, and both ends of it are worth failing over.
func testIdleBobStaysTooSmallToMisstateTheClickPoint() {
// The tip is a promise about which pixel is about to be clicked. A bob
// large enough to read as aim breaks that promise for the sake of a
// decoration — the exact trade this constant exists to prevent.
XCTAssertLessThanOrEqual(VirtualCursorStyle.idleBobAmplitude, 2.0)
// ...but zero is not "subtle", it is the frozen cursor we started with.
XCTAssertGreaterThan(VirtualCursorStyle.idleBobAmplitude, 0.5)
}
func testHaloIsUnmistakablyLargerThanTheSilhouette() {
let box = VirtualCursorStyle.arrowPath().boundingBox
let diameter = VirtualCursorStyle.haloDiameter(forArrowHeight: box.maxY)
// A halo that only just clears the outline reads as a rendering
// artefact — a fringe — rather than a deliberate aura.
XCTAssertGreaterThan(diameter, box.height * 1.5)
// And one that swallows the screen stops being a cursor.
XCTAssertLessThan(diameter, box.height * 4)
}
func testHaloScalesWithTheArrow() {
// Both are driven off one height so a resized cursor keeps its
// proportions instead of growing a fixed-size blob.
let small = VirtualCursorStyle.haloDiameter(forArrowHeight: 11)
let large = VirtualCursorStyle.haloDiameter(forArrowHeight: 44)
XCTAssertEqual(large / small, 4, accuracy: 0.01)
}
func testBreathAndBobDoNotSharePhase() {
// Two motions locked to one beat read as a single mechanical pulse; the
// point of having both is that they drift against each other.
let bob = VirtualCursorStyle.idleBobPeriod
let breath = VirtualCursorStyle.haloBreathPeriod
XCTAssertNotEqual(bob, breath)
let ratio = max(bob, breath) / min(bob, breath)
XCTAssertNotEqual(ratio, ratio.rounded(), accuracy: 0.05)
}
func testIdlePeriodsStayInTheCalmRange() {
// Fast enough to read as alive, slow enough not to nag: anything under
// a second is a twitch, anything over four reads as frozen again.
for period in [VirtualCursorStyle.idleBobPeriod,
VirtualCursorStyle.haloBreathPeriod] {
XCTAssertGreaterThan(period, 1.0)
XCTAssertLessThan(period, 4.0)
}
}
}