fix: separate Cua keyboard targeting and verify browser actions

This commit is contained in:
chenmingxuan committed 2026-10-17 13:52:48 +08:00
1 parent 1c1d700e9b
commit 9c98b6bf8a
10 files changed
+591 -69

No files matched your search

+66
View File
@@ -59,3 +59,69 @@ func TestClickModesAreDisjoint(t *testing.T) {
t.Fatal("pixel action lacks capture")
}
}
func TestKeyboardRejectsAmbiguousTargetsBeforeWindowOrDriverAccess(t *testing.T) {
a := newAdapter(nil) // Any upstream access would panic.
for _, name := range []string{"press_key", "type_text"} {
for _, extra := range []obj{
{"element_token": "s00000001:9", "x": 576, "y": 25},
{"element_token": "s00000002:9", "x": 0, "y": 0},
{"x": 0, "y": 0}, {"element_token": nil},
} {
args := obj{"pid": 1, "window_id": 2}
if name == "press_key" {
args["key"] = "t"
args["modifiers"] = []any{"ctrl"}
} else {
args["text"] = "test"
}
for k, v := range extra {
args[k] = v
}
r := a.handle(name, args)
rec := object(object(r["structuredContent"])["artemis"])
if r["isError"] != true || rec["execution"] != "not_started" || rec["side_effect"] != "none" || rec["error_code"] != "invalid_input" {
t.Fatalf("%s ambiguous action was not rejected before delivery: %v", name, r)
}
}
}
}
func TestInputContractsHaveOneTargetingMode(t *testing.T) {
a := newAdapter(nil)
for _, name := range []string{"press_key", "type_text"} {
props := object(a.schemas[name]["properties"])
for _, key := range []string{"element_token", "x", "y"} {
if _, exists := props[key]; exists {
t.Fatalf("%s exposes ambiguous %s", name, key)
}
}
}
for name, args := range map[string]obj{
"press_key": {"pid": 1, "window_id": 2, "key": "t", "modifiers": []any{"ctrl"}, "delivery_mode": "foreground"},
"type_text": {"pid": 1, "window_id": 2, "text": "中文"},
"set_value": {"pid": 1, "window_id": 2, "element_token": "s00000001:9", "value": "value"},
"scroll": {"pid": 1, "window_id": 2, "direction": "down", "x": 100, "y": 100},
} {
if err := a.validate(name, args); err != nil {
t.Fatalf("valid %s: %v", name, err)
}
}
for _, token := range []any{nil, "", " "} {
if a.validate("set_value", obj{"pid": 1, "window_id": 2, "value": "v", "element_token": token}) == nil {
t.Fatal("set_value must target a concrete element")
}
}
if a.validate("set_value", obj{"pid": 1, "window_id": 2, "value": "v"}) == nil {
t.Fatal("set_value requires an element")
}
for _, extra := range []obj{{"x": 1}, {"x": 1, "y": 1, "element_token": "s00000001:9"}} {
args := obj{"pid": 1, "window_id": 2, "direction": "down"}
for k, v := range extra {
args[k] = v
}
if a.validate("scroll", args) == nil {
t.Fatal("ambiguous scroll accepted")
}
}
}