mirror of
https://github.com/NanmiCoder/claude-code-haha.git
synced 2026-10-10 20:03:13 +08:00
b8a90626ce
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.
230 lines
7.4 KiB
Swift
230 lines
7.4 KiB
Swift
import AppKit
|
|
import XCTest
|
|
@testable import cc_haha_computer_use
|
|
|
|
final class TextEditingTests: XCTestCase {
|
|
func testSelectRangeUsesUTF16AndContext() throws {
|
|
let range = try TextEditing.range(
|
|
in: "🙂 quick brown fox",
|
|
text: "brown",
|
|
prefix: "quick ",
|
|
suffix: " fox"
|
|
)
|
|
|
|
XCTAssertEqual(range.location, 9)
|
|
XCTAssertEqual(range.length, 5)
|
|
}
|
|
|
|
func testCaretAfterCollapsesAtEnd() throws {
|
|
let selected = NSRange(location: 6, length: 5)
|
|
|
|
XCTAssertEqual(
|
|
TextEditing.selectionRange(selected, mode: .cursorAfter),
|
|
NSRange(location: 11, length: 0)
|
|
)
|
|
}
|
|
|
|
func testTextAndCaretBeforeSelectionModes() {
|
|
let selected = NSRange(location: 6, length: 5)
|
|
|
|
XCTAssertEqual(
|
|
TextEditing.selectionRange(selected, mode: .text),
|
|
selected
|
|
)
|
|
XCTAssertEqual(
|
|
TextEditing.selectionRange(selected, mode: .cursorBefore),
|
|
NSRange(location: 6, length: 0)
|
|
)
|
|
}
|
|
|
|
func testInsertionReplacesSelection() throws {
|
|
let edit = try TextEditing.replacingSelection(
|
|
in: "hello world",
|
|
range: NSRange(location: 6, length: 5),
|
|
with: "Codex"
|
|
)
|
|
|
|
XCTAssertEqual(edit.value, "hello Codex")
|
|
XCTAssertEqual(edit.caret, NSRange(location: 11, length: 0))
|
|
}
|
|
|
|
func testRepeatedTextWithoutContextFailsClosedAsAmbiguous() {
|
|
XCTAssertThrowsError(
|
|
try TextEditing.range(in: "one target two target", text: "target")
|
|
) {
|
|
XCTAssertEqual(($0 as? CUError)?.code, "ambiguous_text")
|
|
}
|
|
}
|
|
|
|
func testOverlappingRepeatedTextFailsClosedAsAmbiguous() {
|
|
XCTAssertThrowsError(
|
|
try TextEditing.range(in: "aaa", text: "aa")
|
|
) {
|
|
XCTAssertEqual(($0 as? CUError)?.code, "ambiguous_text")
|
|
}
|
|
}
|
|
|
|
func testPrefixDisambiguatesRepeatedText() throws {
|
|
let range = try TextEditing.range(
|
|
in: "first target then second target",
|
|
text: "target",
|
|
prefix: "second "
|
|
)
|
|
|
|
XCTAssertEqual(range, NSRange(location: 25, length: 6))
|
|
}
|
|
|
|
func testSuffixDisambiguatesRepeatedText() throws {
|
|
let range = try TextEditing.range(
|
|
in: "target alpha then target omega",
|
|
text: "target",
|
|
suffix: " omega"
|
|
)
|
|
|
|
XCTAssertEqual(range, NSRange(location: 18, length: 6))
|
|
}
|
|
|
|
func testReplacingSelectionRejectsOutOfBoundsUTF16Range() {
|
|
XCTAssertThrowsError(
|
|
try TextEditing.replacingSelection(
|
|
in: "🙂 ok",
|
|
range: NSRange(location: 2, length: 99),
|
|
with: "nope"
|
|
)
|
|
) {
|
|
XCTAssertEqual(($0 as? CUError)?.code, "invalid_selection")
|
|
}
|
|
}
|
|
|
|
func testCaretFailureRollsBackBeforeRetrySafeFallback() {
|
|
let edit = TextEditing.Edit(
|
|
value: "hello Codex",
|
|
caret: NSRange(location: 11, length: 0)
|
|
)
|
|
var writtenValues: [String] = []
|
|
var caretWrites: [NSRange] = []
|
|
|
|
let outcome = TextEditing.commitOnce(
|
|
edit,
|
|
originalValue: "hello world",
|
|
originalSelection: NSRange(location: 6, length: 5),
|
|
writeValue: {
|
|
writtenValues.append($0)
|
|
return true
|
|
},
|
|
writeCaret: {
|
|
caretWrites.append($0)
|
|
return caretWrites.count > 1
|
|
}
|
|
)
|
|
|
|
XCTAssertEqual(outcome, .rolledBack)
|
|
XCTAssertEqual(writtenValues, ["hello Codex", "hello world"])
|
|
XCTAssertEqual(
|
|
caretWrites,
|
|
[
|
|
NSRange(location: 11, length: 0),
|
|
NSRange(location: 6, length: 5),
|
|
]
|
|
)
|
|
}
|
|
|
|
func testFailedValueRollbackTreatsCommittedEditAsSuccess() {
|
|
let edit = TextEditing.Edit(
|
|
value: "hello Codex",
|
|
caret: NSRange(location: 11, length: 0)
|
|
)
|
|
var valueWriteCount = 0
|
|
|
|
let outcome = TextEditing.commitOnce(
|
|
edit,
|
|
originalValue: "hello world",
|
|
originalSelection: NSRange(location: 6, length: 5),
|
|
writeValue: { _ in
|
|
valueWriteCount += 1
|
|
return valueWriteCount == 1
|
|
},
|
|
writeCaret: { _ in false }
|
|
)
|
|
|
|
XCTAssertEqual(outcome, .valueOnly)
|
|
XCTAssertEqual(valueWriteCount, 2)
|
|
}
|
|
|
|
func testRestoredValueWithUnknownSelectionStopsForFreshState() {
|
|
let edit = TextEditing.Edit(
|
|
value: "hello Codex",
|
|
caret: NSRange(location: 11, length: 0)
|
|
)
|
|
|
|
let outcome = TextEditing.commitOnce(
|
|
edit,
|
|
originalValue: "hello world",
|
|
originalSelection: NSRange(location: 6, length: 5),
|
|
writeValue: { _ in true },
|
|
writeCaret: { _ in false }
|
|
)
|
|
|
|
XCTAssertEqual(outcome, .stateUnknown)
|
|
}
|
|
|
|
@MainActor
|
|
func testClipboardLeaseRestoresOriginallyEmptyPasteboard() throws {
|
|
let pasteboard = NSPasteboard.withUniqueName()
|
|
pasteboard.clearContents()
|
|
let lease = ClipboardLease(pasteboard: pasteboard)
|
|
|
|
try lease.writeTemporaryString("temporary")
|
|
XCTAssertEqual(pasteboard.string(forType: .string), "temporary")
|
|
XCTAssertTrue(lease.restoreIfUnchanged())
|
|
|
|
XCTAssertTrue(pasteboard.pasteboardItems?.isEmpty ?? true)
|
|
XCTAssertNil(pasteboard.string(forType: .string))
|
|
}
|
|
|
|
@MainActor
|
|
func testClipboardLeaseRestoresAllItemsAndTypeData() throws {
|
|
let pasteboard = NSPasteboard.withUniqueName()
|
|
let customType = NSPasteboard.PasteboardType("com.cc-haha.test.binary")
|
|
let secondType = NSPasteboard.PasteboardType("com.cc-haha.test.second")
|
|
let binary = Data([0x00, 0x7f, 0xff])
|
|
let secondData = Data([0x01, 0x02])
|
|
let first = NSPasteboardItem()
|
|
first.setString("original", forType: .string)
|
|
first.setData(binary, forType: customType)
|
|
let second = NSPasteboardItem()
|
|
second.setData(secondData, forType: secondType)
|
|
pasteboard.clearContents()
|
|
XCTAssertTrue(pasteboard.writeObjects([first, second]))
|
|
let lease = ClipboardLease(pasteboard: pasteboard)
|
|
|
|
try lease.writeTemporaryString("temporary")
|
|
XCTAssertTrue(lease.restoreIfUnchanged())
|
|
|
|
let restored = try XCTUnwrap(pasteboard.pasteboardItems)
|
|
XCTAssertEqual(restored.count, 2)
|
|
XCTAssertEqual(restored[0].string(forType: .string), "original")
|
|
XCTAssertEqual(restored[0].data(forType: customType), binary)
|
|
XCTAssertEqual(restored[1].data(forType: secondType), secondData)
|
|
}
|
|
|
|
@MainActor
|
|
func testClipboardLeaseNeverOverwritesExternalCopy() throws {
|
|
let pasteboard = NSPasteboard.withUniqueName()
|
|
pasteboard.clearContents()
|
|
pasteboard.setString("original", forType: .string)
|
|
let lease = ClipboardLease(pasteboard: pasteboard)
|
|
|
|
try lease.writeTemporaryString("temporary")
|
|
pasteboard.clearContents()
|
|
pasteboard.setString("user copied while paste was in flight", forType: .string)
|
|
|
|
XCTAssertFalse(lease.temporaryWriteIsCurrent())
|
|
XCTAssertFalse(lease.restoreIfUnchanged())
|
|
XCTAssertEqual(
|
|
pasteboard.string(forType: .string),
|
|
"user copied while paste was in flight"
|
|
)
|
|
}
|
|
}
|