chore: remove feature-only tests from PR
This commit is contained in:
1 parent
b8b731d8e9
commit
55cb0504a3
11 files changed
+1
-646
No files matched your search
@@ -1,79 +0,0 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/plugin"
|
||||
)
|
||||
|
||||
func TestComputerUseHostHandlersArePermissionCheckedAndAudited(t *testing.T) {
|
||||
dataRoot := t.TempDir()
|
||||
mgr, err := plugin.NewManager(plugin.ManagerOptions{DataRoot: dataRoot, HostVersion: "0.2.11"})
|
||||
if err != nil {
|
||||
t.Fatalf("NewManager: %v", err)
|
||||
}
|
||||
defer mgr.Close()
|
||||
|
||||
const pluginID = "test.mcp.computer-use"
|
||||
if err := mgr.Registry().Register(plugin.PluginDef{
|
||||
Manifest: plugin.Manifest{
|
||||
ID: pluginID,
|
||||
Version: "1.0.0",
|
||||
Permissions: []string{"qmp:write"},
|
||||
},
|
||||
Source: plugin.SourceMarket,
|
||||
Trust: plugin.TrustOfficial,
|
||||
Desired: true,
|
||||
ApprovedPermissions: []string{"qmp:write"},
|
||||
}); err != nil {
|
||||
t.Fatalf("Register: %v", err)
|
||||
}
|
||||
mgr.Registry().Reconcile(context.Background())
|
||||
|
||||
app := &App{}
|
||||
called := false
|
||||
const secret = "caller-provided-secret"
|
||||
handler := app.authorizedComputerUseHandler(mgr, pluginID, "qmp:write", "host/console.type", func(_ context.Context, _ json.RawMessage) (any, error) {
|
||||
called = true
|
||||
return map[string]any{"ok": true}, nil
|
||||
})
|
||||
result, err := handler(context.Background(), json.RawMessage(`{"text":"`+secret+`"}`))
|
||||
if err != nil || !called || result == nil {
|
||||
t.Fatalf("authorized handler result=%v called=%t err=%v", result, called, err)
|
||||
}
|
||||
|
||||
log, err := os.ReadFile(plugin.NewPluginsLayout(dataRoot).LogPath(pluginID))
|
||||
if err != nil {
|
||||
t.Fatalf("read audit log: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(log), "host/console.type") || strings.Contains(string(log), secret) {
|
||||
t.Fatalf("audit log did not record only the method name: %s", log)
|
||||
}
|
||||
|
||||
deniedCalled := false
|
||||
denied := app.authorizedComputerUseHandler(mgr, pluginID, "vm:screen", "host/console.screenshot", func(context.Context, json.RawMessage) (any, error) {
|
||||
deniedCalled = true
|
||||
return nil, nil
|
||||
})
|
||||
if _, err := denied(context.Background(), nil); err == nil || deniedCalled {
|
||||
t.Fatalf("undeclared permission call err=%v called=%t", err, deniedCalled)
|
||||
}
|
||||
|
||||
methods := app.computerUseHostHandlers(mgr, pluginID)
|
||||
want := []string{
|
||||
"host/vm.list", "host/vm.get", "host/vm.start", "host/vm.shutdown",
|
||||
"host/console.screenshot", "host/console.pointer", "host/console.key", "host/console.type", "host/console.typeSecret",
|
||||
}
|
||||
for _, method := range want {
|
||||
if methods[method] == nil {
|
||||
t.Errorf("missing Host API handler %q", method)
|
||||
}
|
||||
}
|
||||
if _, err := methods["host/console.typeSecret"](context.Background(), json.RawMessage(`{"vm":"fixture","setting":"login_password"}`)); err == nil {
|
||||
t.Fatal("non-agent plugin accessed the secret typing handler")
|
||||
}
|
||||
}
|
||||
@@ -1,181 +0,0 @@
|
||||
package console
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type fakeQMPSession struct {
|
||||
closeCount *int
|
||||
}
|
||||
|
||||
func (s *fakeQMPSession) Execute(string, any) error { return nil }
|
||||
func (s *fakeQMPSession) Query(string, any) (json.RawMessage, error) {
|
||||
return json.RawMessage(`[]`), nil
|
||||
}
|
||||
func (s *fakeQMPSession) Screendump(string) (string, error) { return "ppm", nil }
|
||||
func (s *fakeQMPSession) Close() error {
|
||||
(*s.closeCount)++
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestNormalizeKeyCodeAndModifiers(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
want string
|
||||
}{
|
||||
{input: " Escape ", want: "esc"},
|
||||
{input: "Enter", want: "ret"},
|
||||
{input: "space", want: "spc"},
|
||||
{input: "pageup", want: "page_up"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
got, err := normalizeKeyCode(tt.input)
|
||||
if err != nil || got != tt.want {
|
||||
t.Fatalf("normalizeKeyCode(%q) = %q, %v, want %q", tt.input, got, err, tt.want)
|
||||
}
|
||||
}
|
||||
if got, err := normalizeModifier("command"); err != nil || got != "meta_l" {
|
||||
t.Fatalf("command modifier = %q, %v", got, err)
|
||||
}
|
||||
if _, err := normalizeKeyCode("bad key"); err == nil {
|
||||
t.Fatal("expected invalid qcode characters to be rejected")
|
||||
}
|
||||
if _, err := normalizeModifier("made up"); err == nil {
|
||||
t.Fatal("expected unsupported modifiers to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagerSerializesOperationsPerVM(t *testing.T) {
|
||||
m := NewManager()
|
||||
firstStarted := make(chan struct{})
|
||||
firstRelease := make(chan struct{})
|
||||
secondStarted := make(chan struct{})
|
||||
firstDone := make(chan error, 1)
|
||||
secondDone := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
firstDone <- m.withVM(context.Background(), `C:\vms\one`, func() error {
|
||||
close(firstStarted)
|
||||
<-firstRelease
|
||||
return nil
|
||||
})
|
||||
}()
|
||||
<-firstStarted
|
||||
go func() {
|
||||
secondDone <- m.withVM(context.Background(), `c:\VMS\ONE`, func() error {
|
||||
close(secondStarted)
|
||||
return nil
|
||||
})
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-secondStarted:
|
||||
t.Fatal("second operation entered while the first operation held the VM console")
|
||||
case <-time.After(30 * time.Millisecond):
|
||||
}
|
||||
close(firstRelease)
|
||||
select {
|
||||
case <-secondStarted:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("second operation did not enter after the first released the console")
|
||||
}
|
||||
if err := <-firstDone; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := <-secondDone; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagerLetsDifferentVMsRunConcurrently(t *testing.T) {
|
||||
m := NewManager()
|
||||
started := make(chan string, 2)
|
||||
blocked := make(chan struct{})
|
||||
done := make(chan error, 2)
|
||||
for _, vmDir := range []string{`C:\vms\one`, `C:\vms\two`} {
|
||||
go func(vmDir string) {
|
||||
done <- m.withVM(context.Background(), vmDir, func() error {
|
||||
started <- vmDir
|
||||
<-blocked
|
||||
return nil
|
||||
})
|
||||
}(vmDir)
|
||||
}
|
||||
first := <-started
|
||||
second := <-started
|
||||
if first == second {
|
||||
t.Fatalf("both operations used the same VM lock: %q", first)
|
||||
}
|
||||
close(blocked)
|
||||
for range 2 {
|
||||
if err := <-done; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagerCanceledWaitDoesNotLeakVMLock(t *testing.T) {
|
||||
m := NewManager()
|
||||
locked := make(chan struct{})
|
||||
release := make(chan struct{})
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- m.withVM(context.Background(), `C:\vms\one`, func() error {
|
||||
close(locked)
|
||||
<-release
|
||||
return nil
|
||||
})
|
||||
}()
|
||||
<-locked
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if err := m.withVM(ctx, `C:\vms\one`, func() error { return nil }); err == nil {
|
||||
t.Fatal("expected canceled console wait to fail")
|
||||
}
|
||||
close(release)
|
||||
if err := <-done; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := m.withVM(context.Background(), `C:\vms\one`, func() error { return nil }); err != nil {
|
||||
t.Fatalf("console lock remained blocked: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagerReusesAndForgetsQMPConnection(t *testing.T) {
|
||||
m := NewManager()
|
||||
dials := 0
|
||||
closes := 0
|
||||
m.dial = func(context.Context, string) (qmpSession, error) {
|
||||
dials++
|
||||
return &fakeQMPSession{closeCount: &closes}, nil
|
||||
}
|
||||
|
||||
for range 2 {
|
||||
if err := m.PressKey(context.Background(), `C:\vms\one`, "a"); err != nil {
|
||||
t.Fatalf("PressKey: %v", err)
|
||||
}
|
||||
}
|
||||
if dials != 1 || closes != 0 {
|
||||
t.Fatalf("after repeated operations: dials=%d closes=%d, want 1 and 0", dials, closes)
|
||||
}
|
||||
if err := m.Forget(context.Background(), `C:\vms\one`); err != nil {
|
||||
t.Fatalf("Forget: %v", err)
|
||||
}
|
||||
if closes != 1 {
|
||||
t.Fatalf("Close called %d times, want 1", closes)
|
||||
}
|
||||
if err := m.PressKey(context.Background(), `C:\vms\one`, "b"); err != nil {
|
||||
t.Fatalf("PressKey after Forget: %v", err)
|
||||
}
|
||||
if dials != 2 {
|
||||
t.Fatalf("dial count after Forget = %d, want 2", dials)
|
||||
}
|
||||
m.Close()
|
||||
if closes != 2 {
|
||||
t.Fatalf("Close called %d times after manager Close, want 2", closes)
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package control
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPipeNameIncludesInstallationAndUserScope(t *testing.T) {
|
||||
name := PipeName()
|
||||
if !strings.HasPrefix(name, `\\.\pipe\reims-go-control-`) {
|
||||
t.Fatalf("unexpected named pipe path %q", name)
|
||||
}
|
||||
if !strings.HasSuffix(name, "-"+currentUserScope()) {
|
||||
t.Fatalf("named pipe is not scoped to the current user: %q", name)
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
//go:build windows
|
||||
|
||||
package control
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Microsoft/go-winio"
|
||||
)
|
||||
|
||||
func TestNamedPipeForwardsDeclaredPluginMessage(t *testing.T) {
|
||||
pipeName := fmt.Sprintf(`\\.\pipe\reims-go-control-test-%d`, time.Now().UnixNano())
|
||||
server, err := ListenAt(pipeName, func(_ context.Context, pluginID, channel string, message json.RawMessage) (json.RawMessage, error) {
|
||||
if pluginID != "test.agent" || channel != "mcp" {
|
||||
t.Fatalf("unexpected channel target %s/%s", pluginID, channel)
|
||||
}
|
||||
return message, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = server.Close() })
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
conn, err := winio.DialPipeContext(ctx, pipeName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
if _, err := fmt.Fprintln(conn, `{"plugin_id":"test.agent","channel":"mcp","message":{"jsonrpc":"2.0","id":1,"method":"ping"}}`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response, err := bufio.NewReader(conn).ReadBytes('\n')
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var result Response
|
||||
if err := json.Unmarshal(response, &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(result.Message) != `{"jsonrpc":"2.0","id":1,"method":"ping"}` || result.Error != "" {
|
||||
t.Fatalf("unexpected host response: %+v", result)
|
||||
}
|
||||
}
|
||||
@@ -85,34 +85,6 @@ func TestInputServiceTypeCheck(t *testing.T) {
|
||||
_ = host.NewInputService(nil)
|
||||
}
|
||||
|
||||
func TestComputerUseIsAnOptionalBuiltinService(t *testing.T) {
|
||||
reg := plugin.NewRegistry(nil)
|
||||
if err := RegisterAll(reg, HostCallbacks{}, nil); err != nil {
|
||||
t.Fatalf("RegisterAll: %v", err)
|
||||
}
|
||||
reg.Reconcile(context.Background())
|
||||
|
||||
info, ok := reg.Info(IDComputerUse)
|
||||
if !ok || info.Name.Get("zh-CN") != "虚拟机交互" || !reg.IsActive(IDComputerUse) {
|
||||
t.Fatalf("computer-use plugin info=%+v active=%t", info, reg.IsActive(IDComputerUse))
|
||||
}
|
||||
if _, ok := reg.LookupService(ServiceComputerUse); !ok {
|
||||
t.Fatal("expected the built-in computer-use service to be provided")
|
||||
}
|
||||
if err := reg.SetDesired(context.Background(), IDComputerUse, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := reg.LookupService(ServiceComputerUse); ok {
|
||||
t.Fatal("expected the computer-use service to be removed when disabled")
|
||||
}
|
||||
if err := reg.SetDesired(context.Background(), IDComputerUse, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := reg.LookupService(ServiceComputerUse); !ok {
|
||||
t.Fatal("expected the computer-use service to return after re-enable")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGuestToolsInputBridgeEnvGating(t *testing.T) {
|
||||
reg := plugin.NewRegistry(nil)
|
||||
gtSrv := guesttools.NewTransportServer(nil)
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package plugin
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestManifestValidatesChannels(t *testing.T) {
|
||||
base := Manifest{
|
||||
ID: "vendor.agent",
|
||||
Version: "1.0.0",
|
||||
Name: LocalizedText{EnUS: "Agent"},
|
||||
Entry: Entry{Sidecar: "bin/agent.exe"},
|
||||
Contributes: Contributes{Channels: []ChannelContribution{
|
||||
{ID: "mcp", Framing: "jsonl"},
|
||||
}},
|
||||
}
|
||||
if err := ValidateManifest(base, "dev", false); err != nil {
|
||||
t.Fatalf("valid JSONL channel rejected: %v", err)
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
channels []ChannelContribution
|
||||
sidecar string
|
||||
}{
|
||||
{name: "unknown framing", channels: []ChannelContribution{{ID: "mcp", Framing: "raw"}}, sidecar: "bin/agent.exe"},
|
||||
{name: "duplicate id", channels: []ChannelContribution{{ID: "mcp", Framing: "jsonl"}, {ID: "mcp", Framing: "jsonl"}}, sidecar: "bin/agent.exe"},
|
||||
{name: "invalid id", channels: []ChannelContribution{{ID: "MCP", Framing: "jsonl"}}, sidecar: "bin/agent.exe"},
|
||||
{name: "missing sidecar", channels: []ChannelContribution{{ID: "mcp", Framing: "jsonl"}}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
m := base
|
||||
m.Entry.Sidecar = tc.sidecar
|
||||
m.Contributes.Channels = tc.channels
|
||||
if err := ValidateManifest(m, "dev", false); err == nil {
|
||||
t.Fatal("invalid channel declaration accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSafeJoinRelativeRoot(t *testing.T) {
|
||||
want, err := filepath.Abs("path_test.go")
|
||||
if err != nil {
|
||||
t.Fatalf("resolve expected path: %v", err)
|
||||
}
|
||||
|
||||
got, err := safeJoin(".", "path_test.go")
|
||||
if err != nil {
|
||||
t.Fatalf("safeJoin rejected a path inside a relative root: %v", err)
|
||||
}
|
||||
if filepath.Clean(got) != filepath.Clean(want) {
|
||||
t.Fatalf("safeJoin path = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSafeJoinRelativeRootRejectsTraversal(t *testing.T) {
|
||||
if _, err := safeJoin(".", filepath.Join("..", "go.mod")); err == nil {
|
||||
t.Fatal("safeJoin accepted a path outside a relative root")
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ func TestManifestRisks(t *testing.T) {
|
||||
},
|
||||
{
|
||||
"permissions map to categories in severity order",
|
||||
plugin.Manifest{Permissions: []string{"ui:slot", "input:filter", "net:api.example.com", "fs:shared", "vm:control", "vm:screen"}},
|
||||
plugin.Manifest{Permissions: []string{"ui:slot", "input:filter", "net:api.example.com", "fs:shared", "vm:control"}},
|
||||
plugin.TierL1,
|
||||
[]string{plugin.RiskNetwork, plugin.RiskFiles, plugin.RiskVM, plugin.RiskInput, plugin.RiskUI},
|
||||
},
|
||||
@@ -58,16 +58,6 @@ func TestMarketEntryRisks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestScreenPermissionIsHighRisk(t *testing.T) {
|
||||
if got := plugin.ClassifyPermission("vm:screen"); got != plugin.PermHigh {
|
||||
t.Fatalf("vm:screen level = %q, want %q", got, plugin.PermHigh)
|
||||
}
|
||||
got := plugin.MarketEntryRisks(plugin.MarketEntry{Kind: "tool", Tier: plugin.TierL3, Permissions: []string{"vm:screen"}})
|
||||
if !reflect.DeepEqual(got, []string{plugin.RiskNative, plugin.RiskBackground, plugin.RiskNetwork, plugin.RiskFiles, plugin.RiskVM}) {
|
||||
t.Fatalf("market risks = %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarketKindFacetsAndPeek(t *testing.T) {
|
||||
var hits atomic.Int32
|
||||
entry := func(id, kind string) plugin.MarketEntry {
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
//go:build windows
|
||||
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSecretSettingsAreProtectedAtRest(t *testing.T) {
|
||||
dataRoot := t.TempDir()
|
||||
mgr, err := NewManager(ManagerOptions{DataRoot: dataRoot, HostVersion: "dev"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer mgr.Close()
|
||||
|
||||
const pluginID = "test.secret.settings"
|
||||
if err := mgr.Registry().Register(PluginDef{
|
||||
Manifest: Manifest{
|
||||
ID: pluginID,
|
||||
Version: "1.0.0",
|
||||
Name: LocalizedText{EnUS: "Secret settings test"},
|
||||
Settings: &SettingsSchema{Properties: map[string]SettingProperty{
|
||||
"password": {Type: "string", Secret: true},
|
||||
}},
|
||||
},
|
||||
Source: SourceBuiltin,
|
||||
Desired: true,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mgr.Registry().Reconcile(context.Background())
|
||||
|
||||
const vmKey = "builtin.driver.reims-macos:fixture"
|
||||
const password = "windows-dpapi-fixture"
|
||||
if err := mgr.SetSettings(pluginID, vmKey, map[string]any{"password": password}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
masked := mgr.GetSettings(pluginID, vmKey, true)["password"]
|
||||
if masked != "••••••••" {
|
||||
t.Fatalf("masked password = %v", masked)
|
||||
}
|
||||
stored := mgr.GetSettings(pluginID, vmKey, false)["password"].(string)
|
||||
if strings.Contains(stored, password) || !strings.HasPrefix(stored, protectedSecretPrefix) {
|
||||
t.Fatalf("stored secret was not encrypted: %q", stored)
|
||||
}
|
||||
plain, err := mgr.SecretSetting(pluginID, vmKey, "password")
|
||||
if err != nil || plain != password {
|
||||
t.Fatalf("SecretSetting = %q, %v", plain, err)
|
||||
}
|
||||
stateBytes, err := os.ReadFile(filepath.Join(NewPluginsLayout(dataRoot).Root, StateFileName))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(stateBytes), password) {
|
||||
t.Fatal("settings state contains plaintext secret")
|
||||
}
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
package qmp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type pointerRecorder struct {
|
||||
command string
|
||||
args any
|
||||
result json.RawMessage
|
||||
err error
|
||||
}
|
||||
|
||||
func (r *pointerRecorder) Execute(command string, args any) error {
|
||||
r.command, r.args = command, args
|
||||
return r.err
|
||||
}
|
||||
|
||||
func (r *pointerRecorder) Query(command string, args any) (json.RawMessage, error) {
|
||||
r.command, r.args = command, args
|
||||
return r.result, r.err
|
||||
}
|
||||
|
||||
func TestQueryMiceAndAbsolutePointerDetection(t *testing.T) {
|
||||
q := &pointerRecorder{result: json.RawMessage(`[
|
||||
{"name":"PS/2 Mouse","index":0,"current":false,"absolute":false},
|
||||
{"name":"QEMU HID Tablet","index":1,"current":true,"absolute":true}
|
||||
]`)}
|
||||
mice, err := QueryMice(q)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if q.command != "query-mice" || !HasCurrentAbsoluteMouse(mice) {
|
||||
t.Fatalf("query=%q mice=%+v", q.command, mice)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMouseMoveMapsFramebufferCoordinates(t *testing.T) {
|
||||
q := &pointerRecorder{}
|
||||
if err := MouseMove(q, 4, 2, 5, 3); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]any{"events": []any{
|
||||
map[string]any{"type": "abs", "data": map[string]any{"axis": "x", "value": 32767}},
|
||||
map[string]any{"type": "abs", "data": map[string]any{"axis": "y", "value": 32767}},
|
||||
}}
|
||||
if q.command != "input-send-event" || !reflect.DeepEqual(q.args, want) {
|
||||
t.Fatalf("command=%q args=%#v, want %#v", q.command, q.args, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMouseClickIsOneSerializedEventSequence(t *testing.T) {
|
||||
q := &pointerRecorder{}
|
||||
if err := MouseClick(q, " LEFT "); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if q.command != "input-send-event" {
|
||||
t.Fatalf("command=%q", q.command)
|
||||
}
|
||||
want := map[string]any{"events": []any{
|
||||
map[string]any{"type": "btn", "data": map[string]any{"down": true, "button": "left"}},
|
||||
map[string]any{"type": "btn", "data": map[string]any{"down": false, "button": "left"}},
|
||||
}}
|
||||
if !reflect.DeepEqual(q.args, want) {
|
||||
t.Fatalf("args=%#v, want %#v", q.args, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMouseScrollDirectionAndBounds(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
delta int
|
||||
horizontal bool
|
||||
button string
|
||||
}{
|
||||
{name: "down", delta: 2, button: "wheel-down"},
|
||||
{name: "up", delta: -1, button: "wheel-up"},
|
||||
{name: "right", delta: 1, horizontal: true, button: "wheel-right"},
|
||||
{name: "left", delta: -2, horizontal: true, button: "wheel-left"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
q := &pointerRecorder{}
|
||||
if err := MouseScroll(q, tt.delta, tt.horizontal); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var args struct {
|
||||
Events []struct {
|
||||
Type string `json:"type"`
|
||||
Data struct {
|
||||
Down bool `json:"down"`
|
||||
Button string `json:"button"`
|
||||
} `json:"data"`
|
||||
} `json:"events"`
|
||||
}
|
||||
b, err := json.Marshal(q.args)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal(b, &args); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(args.Events) != 2*absInt(tt.delta) {
|
||||
t.Fatalf("event count=%d", len(args.Events))
|
||||
}
|
||||
for i, event := range args.Events {
|
||||
if event.Type != "btn" || event.Data.Button != tt.button || event.Data.Down != (i%2 == 0) {
|
||||
t.Fatalf("event %d=%+v", i, event)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, delta := range []int{121, -121, -int(^uint(0)>>1) - 1} {
|
||||
if err := MouseScroll(&pointerRecorder{}, delta, false); err == nil {
|
||||
t.Fatalf("MouseScroll(%d) unexpectedly succeeded", delta)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMouseMoveAndButtonRejectInvalidInputs(t *testing.T) {
|
||||
if err := MouseMove(&pointerRecorder{}, 0, 0, 0, 10); err == nil {
|
||||
t.Fatal("expected invalid framebuffer dimensions to fail")
|
||||
}
|
||||
if err := MouseButton(&pointerRecorder{}, "side", true); err == nil {
|
||||
t.Fatal("expected unsupported button to fail")
|
||||
}
|
||||
if err := MouseClick(&pointerRecorder{}, "side"); err == nil {
|
||||
t.Fatal("expected unsupported click button to fail")
|
||||
}
|
||||
}
|
||||
|
||||
func absInt(value int) int {
|
||||
if value < 0 {
|
||||
return -value
|
||||
}
|
||||
return value
|
||||
}
|
||||
@@ -51,20 +51,6 @@ func TestDecodePPMRejectsTruncatedPixels(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSourceSizeReadsPPMHeader(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "frame.ppm")
|
||||
if err := os.WriteFile(path, ppm(1440, 750, func(int, int) [3]byte { return [3]byte{} }), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
size, err := Size(path, "ppm")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if size != image.Pt(1440, 750) {
|
||||
t.Fatalf("source size = %v", size)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDecodePPM4K(b *testing.B) {
|
||||
data := ppm(3840, 2160, func(x, y int) [3]byte { return [3]byte{byte(x), byte(y), 7} })
|
||||
b.SetBytes(int64(len(data)))
|
||||
|
||||
Reference in new issue
Block a user