refactor(ioscompanion): neutralize HID events behind the transport seam

This commit is contained in:
pj committed 2026-06-07 15:39:58 +05:30
1 parent 4792626515
commit fba77d54e7
3 files changed
+145 -47

No files matched your search

@@ -6,7 +6,31 @@ import (
pb "github.com/priyanshujain/sanderling/internal/driver/ioscompanion/companionpb"
)
func TestTouchBuilders(t *testing.T) {
func TestBuildersSetKindAndFields(t *testing.T) {
cases := []struct {
name string
event HIDEvent
want HIDEvent
}{
{"touch down", TouchDown(12, 34), HIDEvent{Kind: HIDKindTouchDown, X: 12, Y: 34}},
{"touch up", TouchUp(12, 34), HIDEvent{Kind: HIDKindTouchUp, X: 12, Y: 34}},
{"key down", KeyDown(225), HIDEvent{Kind: HIDKindKeyDown, Usage: 225}},
{"key up", KeyUp(225), HIDEvent{Kind: HIDKindKeyUp, Usage: 225}},
{"delay", Delay(250), HIDEvent{Kind: HIDKindDelay, Milliseconds: 250}},
{"swipe", SwipeEvent(1, 2, 3, 4, 0.5), HIDEvent{
Kind: HIDKindSwipe, FromX: 1, FromY: 2, ToX: 3, ToY: 4, Seconds: 0.5,
}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if c.event != c.want {
t.Errorf("event = %+v, want %+v", c.event, c.want)
}
})
}
}
func TestTouchEventsToProto(t *testing.T) {
cases := []struct {
name string
event HIDEvent
@@ -17,9 +41,13 @@ func TestTouchBuilders(t *testing.T) {
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
press := c.event.event.GetPress()
message, err := hidEventToProto(c.event)
if err != nil {
t.Fatalf("hidEventToProto: %v", err)
}
press := message.GetPress()
if press == nil {
t.Fatalf("expected press event, got %#v", c.event.event.GetEvent())
t.Fatalf("expected press event, got %#v", message.GetEvent())
}
if press.GetDirection() != c.direction {
t.Errorf("direction = %v, want %v", press.GetDirection(), c.direction)
@@ -35,7 +63,7 @@ func TestTouchBuilders(t *testing.T) {
}
}
func TestKeyBuilders(t *testing.T) {
func TestKeyEventsToProto(t *testing.T) {
cases := []struct {
name string
event HIDEvent
@@ -46,7 +74,11 @@ func TestKeyBuilders(t *testing.T) {
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
press := c.event.event.GetPress()
message, err := hidEventToProto(c.event)
if err != nil {
t.Fatalf("hidEventToProto: %v", err)
}
press := message.GetPress()
if press == nil {
t.Fatalf("expected press event")
}
@@ -64,8 +96,12 @@ func TestKeyBuilders(t *testing.T) {
}
}
func TestDelayConvertsToSeconds(t *testing.T) {
delay := Delay(250).event.GetDelay()
func TestDelayToProtoConvertsToSeconds(t *testing.T) {
message, err := hidEventToProto(Delay(250))
if err != nil {
t.Fatalf("hidEventToProto: %v", err)
}
delay := message.GetDelay()
if delay == nil {
t.Fatalf("expected delay event")
}
@@ -74,8 +110,12 @@ func TestDelayConvertsToSeconds(t *testing.T) {
}
}
func TestSwipeEvent(t *testing.T) {
swipe := SwipeEvent(1, 2, 3, 4, 0.5).event.GetSwipe()
func TestSwipeEventToProto(t *testing.T) {
message, err := hidEventToProto(SwipeEvent(1, 2, 3, 4, 0.5))
if err != nil {
t.Fatalf("hidEventToProto: %v", err)
}
swipe := message.GetSwipe()
if swipe == nil {
t.Fatalf("expected swipe event")
}
@@ -89,3 +129,9 @@ func TestSwipeEvent(t *testing.T) {
t.Errorf("duration = %v, want 0.5", swipe.GetDuration())
}
}
func TestUnknownKindToProtoErrors(t *testing.T) {
if _, err := hidEventToProto(HIDEvent{Kind: HIDEventKind(99)}); err == nil {
t.Fatal("expected error for unknown event kind")
}
}