package main import "testing" func TestUnknownAndRefusedAreNotConfirmed(t *testing.T) { cases := []struct{ effect, execution, side string }{{"confirmed", "completed", "confirmed"}, {"partial", "partial", "confirmed"}, {"unverifiable", "completed", "possible"}, {"suspected_noop", "completed", "possible"}, {"refused", "not_started", "none"}, {"", "unknown", "possible"}} for _, c := range cases { r := actionReceipt(obj{"structuredContent": obj{"effect": c.effect}}, true) if r["execution"] != c.execution || r["side_effect"] != c.side || r["retryable"] != false { t.Fatalf("%s: %v", c.effect, r) } } } func TestAttachmentCannotEscape(t *testing.T) { for _, s := range []string{"../out.png", "..\\out.png", "C:out.png", "a.png:stream", "", "a.png ", "a.jpg"} { if validFilename(s) { t.Fatal(s) } } if !validFilename("留档.png") { t.Fatal("plain Unicode PNG rejected") } } func TestUnexposedAndMissingTargetRejected(t *testing.T) { a := newAdapter(nil) for _, c := range []struct { name string args obj }{{"run_script", obj{}}, {"click", obj{"pid": 1, "window_id": 2, "scope": "desktop"}}, {"get_window_state", obj{"pid": 1}}, {"click", obj{"pid": 0, "window_id": 2}}} { if a.validate(c.name, c.args) == nil { t.Fatal(c) } } } func TestSupportedArguments(t *testing.T) { a := newAdapter(nil) if err := a.validate("click", obj{"pid": 1, "window_id": 2, "element_token": "s00000001:0"}); err != nil { t.Fatal(err) } } func TestBackgroundPreflightVsActuatorUncertainty(t *testing.T) { r := obj{"isError": true, "structuredContent": obj{"code": "background_unavailable", "event_kind": "mouse_scroll"}} if actionReceipt(r, true)["execution"] != "not_started" { t.Fatal("preflight refusal must be explicit") } object(r["structuredContent"])["cause"] = "unknown actuator failure" if actionReceipt(r, true)["execution"] != "unknown" { t.Fatal("actuator uncertainty must not be hidden") } } func TestClickModesAreDisjoint(t *testing.T) { a := newAdapter(nil) if a.validate("click", obj{"pid": 1, "window_id": 2, "element_token": "s1:0", "capture_id": "capture"}) == nil { t.Fatal("mixed click modes") } if a.validate("click_at", obj{"pid": 1, "window_id": 2, "x": 5, "y": 5}) == nil { 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, "capture_id": "current"}, } { 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") } } }