Files
Reims-Go/internal/qmp/keys_test.go
T
NwflowerandClaude Opus 5.5 b2237997f2 feat(input): send Chinese, emoji and host-IME text into the VM
- "Send text to VM..." dialog: arbitrary Unicode goes through Guest Tools
  (clipboard paste in the guest, agent v3 /v1/text-input long poll); without
  Guest Tools only US-keyboard ASCII is typed via QMP.
- Host IME / dictation: a low-level keyboard hook (internal/host/ime_*) catches
  VK_PACKET input aimed at a running VM window, coalesces bursts and forwards
  them in order through a per-VM queue (type_flow.go).
- reims-vgpu-window bridge: new runtime patch reims-vgpu-win32-unicode-input
  enables SDL text input and posts committed text to /v1/host-text; StartVM
  passes REIMS_GO_INPUT_URL/TOKEN to the window process. This also adds the
  patch file that patches/series.txt already listed.
- Guest Tools auth returns a field snapshot instead of copying the VM entry
  (it now holds a mutex and text queues).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 10:52:34 -07:00

157 lines
4.1 KiB
Go

package qmp
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"net"
"strings"
"testing"
)
func TestStrokesMapsUSLayout(t *testing.T) {
got, err := Strokes("sh /Volumes/A_b.sh\r\n")
if err != nil {
t.Fatal(err)
}
want := []Stroke{
{Key: "s"}, {Key: "h"}, {Key: "spc"}, {Key: "slash"},
{Key: "v", Shift: true}, {Key: "o"}, {Key: "l"}, {Key: "u"}, {Key: "m"}, {Key: "e"}, {Key: "s"},
{Key: "slash"}, {Key: "a", Shift: true}, {Key: "minus", Shift: true}, {Key: "b"},
{Key: "dot"}, {Key: "s"}, {Key: "h"}, {Key: "ret"},
}
if fmt.Sprint(got) != fmt.Sprint(want) {
t.Fatalf("got %v\nwant %v", got, want)
}
}
func TestStrokesCoversPrintableASCII(t *testing.T) {
for r := rune(0x20); r < 0x7f; r++ {
if _, ok := StrokeFor(r); !ok {
t.Errorf("no key for %q", r)
}
}
}
func TestStrokesRefusesWholeTextOnUntypeable(t *testing.T) {
_, err := Strokes("echo 你好")
var ue *UntypeableError
if !errors.As(err, &ue) || ue.Rune != '你' || ue.Pos != 6 {
t.Fatalf("err = %v", err)
}
if _, err := Strokes(strings.Repeat("a", MaxTypedRunes+1)); err == nil {
t.Fatal("over-long text accepted")
}
}
type recorder struct {
events []string
failAt int
}
func (r *recorder) Execute(command string, args any) error {
if r.failAt > 0 && len(r.events) >= r.failAt {
r.failAt = 0 // fail once; the cleanup that follows goes through
return errors.New("boom")
}
for _, ev := range args.(map[string]any)["events"].([]map[string]any) {
d := ev["data"].(map[string]any)
dir := "up"
if d["down"].(bool) {
dir = "down"
}
r.events = append(r.events, d["key"].(map[string]any)["data"].(string)+" "+dir)
}
return nil
}
func TestTypeStrokesHoldsShiftAcrossCapitals(t *testing.T) {
s, _ := Strokes("ABc")
r := &recorder{}
if err := TypeStrokes(context.Background(), r, s, 0, 0, nil); err != nil {
t.Fatal(err)
}
want := "shift down,a down,a up,b down,b up,shift up,c down,c up"
if got := strings.Join(r.events, ","); got != want {
t.Fatalf("got %s\nwant %s", got, want)
}
}
func TestTypeStrokesReleasesShiftOnError(t *testing.T) {
s, _ := Strokes("AB")
r := &recorder{failAt: 3} // shift down, a down, a up, then fail
if err := TypeStrokes(context.Background(), r, s, 0, 0, nil); err == nil {
t.Fatal("want error")
}
if last := r.events[len(r.events)-1]; last != "shift up" {
t.Fatalf("shift left down: %v", r.events)
}
}
func TestPressComboAndCleanup(t *testing.T) {
r := &recorder{}
if err := PressCombo(context.Background(), r, 0, "v", "meta_l"); err != nil {
t.Fatal(err)
}
want := "meta_l down,v down,v up,meta_l up"
if got := strings.Join(r.events, ","); got != want {
t.Fatalf("got %s\nwant %s", got, want)
}
// Failure after meta_l down + v down must release both v and meta_l.
r2 := &recorder{failAt: 2}
if err := PressCombo(context.Background(), r2, 0, "v", "meta_l"); err == nil {
t.Fatal("want error")
}
if got := strings.Join(r2.events, ","); !strings.HasSuffix(got, "v up,meta_l up") {
t.Fatalf("keys left down: %s", got)
}
}
func TestDialAndExecuteAgainstMockQMP(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer ln.Close()
got := make(chan []string, 1)
go func() {
c, err := ln.Accept()
if err != nil {
return
}
defer c.Close()
fmt.Fprint(c, `{"QMP":{"version":{}}}`+"\r\n")
sc := bufio.NewScanner(c)
var cmds []string
for sc.Scan() {
var req struct{ Execute string }
_ = json.Unmarshal(sc.Bytes(), &req)
cmds = append(cmds, req.Execute)
if req.Execute == "bad" {
fmt.Fprint(c, `{"error":{"class":"GenericError","desc":"nope"}}`+"\r\n")
continue
}
fmt.Fprint(c, `{"event":"RTC_CHANGE","data":{}}`+"\r\n"+`{"return":{}}`+"\r\n")
}
got <- cmds
}()
q, err := Dial(context.Background(), "tcp", ln.Addr().String())
if err != nil {
t.Fatal(err)
}
if err := q.Execute("input-send-event", map[string]any{"events": []any{}}); err != nil {
t.Fatal(err)
}
if err := q.Execute("bad", nil); err == nil || !strings.Contains(err.Error(), "nope") {
t.Fatalf("err = %v", err)
}
q.Close()
if cmds := <-got; strings.Join(cmds, ",") != "qmp_capabilities,input-send-event,bad" {
t.Fatalf("server saw %v", cmds)
}
}