Files

89 lines
3.5 KiB
Go

package main
import (
"bytes"
"encoding/json"
"strings"
"testing"
)
type lookupInput struct{ bytes.Buffer }
func (*lookupInput) Close() error { return nil }
func TestInvalidDiscoveryPIDStopsBeforeDriver(t *testing.T) {
for _, pid := range []any{0, -1, 1.5, "123", true, float64(4294967296)} {
result := newAdapter(nil).handle("list_windows", obj{"app": "Brave", "pid": pid})
if result["isError"] != true || object(object(result["structuredContent"])["artemis"])["execution"] != "not_started" {
t.Fatalf("invalid PID %v reached driver or was reported as no windows: %v", pid, result)
}
if !strings.Contains(str(object(items(result["content"])[0])["text"]), "omit pid") {
t.Fatalf("missing recovery instruction for %v", pid)
}
}
}
func TestAppDiscoveryOmitsNullPIDAtDriverBoundary(t *testing.T) {
input := &lookupInput{}
lines := make(chan []byte, 1)
lines <- []byte(`{"id":1,"result":{"content":[],"structuredContent":{"windows":[{"app_name":"brave.exe","pid":72,"window_id":81,"title":"Browser"}]}}}`)
a := newAdapter(&peer{input: input, lines: lines})
args := obj{"app": " Brave ", "pid": nil, "on_screen_only": true}
result := a.handle("list_windows", args)
if result["isError"] == true || len(items(object(result["structuredContent"])["windows"])) != 1 {
t.Fatalf("failed app-only discovery: %v", result)
}
var sent obj
if err := json.Unmarshal(input.Bytes(), &sent); err != nil {
t.Fatal(err)
}
forwarded := object(object(sent["params"])["arguments"])
if _, present := forwarded["pid"]; present || forwarded["app"] != "Brave" || forwarded["on_screen_only"] != true {
t.Fatalf("incorrect upstream query: %v", forwarded)
}
if args["app"] != " Brave " || len(args) != 3 {
t.Fatal("changed original arguments used in trace")
}
}
func TestConflictingFiltersPreserveScopeAndRemoveForegroundHint(t *testing.T) {
query := obj{"app": "Brave", "pid": 14492, "on_screen_only": true}
result := windowListing(obj{
"content": []any{textBlock(`No windows. Current frontmost app is "studio64.exe" (pid 14492).`)},
"structuredContent": obj{"windows": []any{}},
}, query)
data := object(result["structuredContent"])
lookup := object(data["lookup"])
next := object(lookup["suggested_arguments"])
if len(items(data["windows"])) != 0 || lookup["status"] != "no_match" || object(lookup["filters"])["pid"] != 14492 {
t.Fatal("original filter or no-match result was hidden")
}
if _, exists := next["pid"]; exists || next["app"] != "Brave" || next["on_screen_only"] != true {
t.Fatalf("incorrect app-only recovery: %v", next)
}
text := str(object(items(result["content"])[0])["text"])
if strings.Contains(text, "studio64.exe") || !strings.Contains(text, "pid omitted") {
t.Fatalf("misleading recovery: %s", text)
}
}
func TestEmptyVisibleQuerySuggestsSameTargetOffScreen(t *testing.T) {
query := obj{"pid": 12, "on_screen_only": true}
result := windowListing(obj{"structuredContent": obj{"windows": []any{}}}, query)
next := object(object(object(result["structuredContent"])["lookup"])["suggested_arguments"])
if next["pid"] != 12 || next["on_screen_only"] != false || query["on_screen_only"] != true {
t.Fatalf("changed target or original query: %v", next)
}
}
func TestLookupErrorsNeverBecomeEmptySuccess(t *testing.T) {
for _, result := range []obj{
{"isError": true, "content": []any{textBlock("driver unavailable")}},
{"structuredContent": obj{}},
} {
if windowListing(result, obj{"app": "Brave"})["isError"] != true {
t.Fatal("observation failure became a successful empty list")
}
}
}