Files
Reims-Go/internal/qmp/keys_test.go
T
Nwflower 17497aee73 feat: add offline helper disk, recovery mode switch, and QMP typing
- Pure-Go HFS+ offline helper disk (REIMSHELP) bypassing Recovery's missing pkgutil & read-only installer target, with automatic blank-disk APFS formatting and non-blank safety confirmation
- Recovery mode UX: rename Finish install to Exit recovery mode, add Enter recovery mode via OpenCore boot picker (--boot-selection), show Attach offline image on VM page with ~1 GB Apple online recovery fallback download
- Pre-apply WHPX kernel-irqchip=off when nested-virt probe fails and filter benign TSC deadline timer warnings
- QMP keyboard typing (type clipboard / one-click Recovery command) and offline-install network toggle
2026-11-28 07:53:40 -07:00

137 lines
3.5 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 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)
}
}