- Each plugin connect connection gets a session id that is forwarded with channel/<id>/message; when the connection ends the host notifies the sidecar with channel/<id>/closed so it can cancel calls and release input. - host/console.screenshot crops a region from the full-resolution frame, optionally measured in a reference image size, and full frames now honor max_width up to 2560. - host/console.key can hold or release one key, and host/console.release lifts keys and pointer buttons in one call. Driver consoles stay press-only. - host/vm.shutdown accepts force, and host/vm.snapshot.list needs only vm:read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
73 lines
2.6 KiB
Go
73 lines
2.6 KiB
Go
package app
|
||
|
||
import (
|
||
"image"
|
||
"image/color"
|
||
"testing"
|
||
)
|
||
|
||
func TestComputerUseRegionFrameCropsSourcePixels(t *testing.T) {
|
||
full := image.NewRGBA(image.Rect(0, 0, 400, 300))
|
||
for y := 100; y < 150; y++ {
|
||
for x := 200; x < 260; x++ {
|
||
full.SetRGBA(x, y, color.RGBA{R: 255, A: 255})
|
||
}
|
||
}
|
||
frame, applied, err := computerUseRegionFrame(full, computerUseRegion{X: 200, Y: 100, Width: 60, Height: 50}, 1280)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if applied != (computerUseRegion{X: 200, Y: 100, Width: 60, Height: 50}) {
|
||
t.Fatalf("实际区域 = %+v", applied)
|
||
}
|
||
b := frame.Bounds()
|
||
if b.Dx() != 60 || b.Dy() != 50 {
|
||
t.Fatalf("区域未保持源分辨率:%dx%d", b.Dx(), b.Dy())
|
||
}
|
||
if r, _, _, _ := frame.At(b.Min.X, b.Min.Y).RGBA(); r>>8 != 255 {
|
||
t.Fatal("裁剪结果不是所选区域的像素")
|
||
}
|
||
}
|
||
|
||
func TestComputerUseRegionFrameClipsAndShrinks(t *testing.T) {
|
||
full := image.NewRGBA(image.Rect(0, 0, 4000, 2000))
|
||
frame, applied, err := computerUseRegionFrame(full, computerUseRegion{X: 1000, Y: 1500, Width: 3500, Height: 1000}, 1500)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if applied != (computerUseRegion{X: 1000, Y: 1500, Width: 3000, Height: 500}) {
|
||
t.Fatalf("越界区域未裁到屏幕内:%+v", applied)
|
||
}
|
||
if got := frame.Bounds().Dx(); got != 1500 {
|
||
t.Fatalf("宽区域未缩放到 max_width:%d", got)
|
||
}
|
||
if _, _, err := computerUseRegionFrame(full, computerUseRegion{X: 5000, Y: 0, Width: 10, Height: 10}, 1280); err == nil {
|
||
t.Fatal("屏幕外的区域应报错")
|
||
}
|
||
}
|
||
|
||
func TestComputerUseRegionToSourceScalesFromReferenceImage(t *testing.T) {
|
||
got := computerUseRegionToSource(computerUseRegion{X: 100, Y: 50, Width: 33, Height: 20}, 1280, 720, image.Pt(2560, 1440))
|
||
if got != (computerUseRegion{X: 200, Y: 100, Width: 66, Height: 40}) {
|
||
t.Fatalf("缩放后的区域 = %+v", got)
|
||
}
|
||
odd := computerUseRegionToSource(computerUseRegion{X: 1, Y: 1, Width: 1, Height: 1}, 3, 3, image.Pt(4, 4))
|
||
if odd.X != 1 || odd.Y != 1 || odd.Width < 1 || odd.Height < 1 {
|
||
t.Fatalf("取整后丢失了所选像素:%+v", odd)
|
||
}
|
||
same := computerUseRegion{X: 5, Y: 6, Width: 7, Height: 8}
|
||
if computerUseRegionToSource(same, 0, 0, image.Pt(100, 100)) != same {
|
||
t.Fatal("未给出参考尺寸时应按源像素处理")
|
||
}
|
||
}
|
||
|
||
func TestNormalizeReleaseButtons(t *testing.T) {
|
||
got, err := normalizeReleaseButtons([]string{" Left", "middle"})
|
||
if err != nil || len(got) != 2 || got[0] != "left" || got[1] != "middle" {
|
||
t.Fatalf("got %v, %v", got, err)
|
||
}
|
||
if _, err := normalizeReleaseButtons([]string{"wheel-up"}); err == nil {
|
||
t.Fatal("滚轮不是可按住的按键,应被拒绝")
|
||
}
|
||
}
|