package web import ( "encoding/json" "testing" ) // The non-controlling browser draws the remote cursor itself, so the relayed // "cursor" message must carry the position, not just the shape index. Without // x/y an older page still works (it ignores unknown fields), but the new // overlay has nothing to place. func TestOnCursorChangeCarriesPosition(t *testing.T) { c := &wsClient{ send: make(chan wsMsg, 4), closed: make(chan struct{}), token: "tok", watchIDs: map[string]struct{}{"42": {}}, } h := &wsHub{clients: map[*wsClient]struct{}{c: {}}} h.OnCursorChange("42", 5, 1280, 720) var msg struct { Cmd string `json:"cmd"` ID string `json:"id"` Index int `json:"index"` X *int `json:"x"` Y *int `json:"y"` } m := <-c.send if m.binary { t.Fatal("cursor message must be a text frame") } if err := json.Unmarshal(m.data, &msg); err != nil { t.Fatalf("unmarshal %s: %v", m.data, err) } if msg.Cmd != "cursor" || msg.ID != "42" || msg.Index != 5 { t.Fatalf("message = %+v", msg) } if msg.X == nil || msg.Y == nil || *msg.X != 1280 || *msg.Y != 720 { t.Fatalf("position missing: x=%v y=%v (raw %s)", msg.X, msg.Y, m.data) } // A viewer not watching the device must not receive anything. other := &wsClient{ send: make(chan wsMsg, 1), closed: make(chan struct{}), token: "tok", watchIDs: map[string]struct{}{"7": {}}, } h.clients[other] = struct{}{} h.OnCursorChange("42", 5, 1, 1) select { case <-other.send: t.Fatal("non-watching client received a cursor message") default: } }