Delete functions that no product code calls, and fold "simple plus WithOptions" pairs into the one signature callers actually use. Rename the live macOS lifecycle functions that were labelled Legacy. The architecture tests now share one source loader and reject functions without a product caller and catalog messages nobody reads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
170 lines
4.1 KiB
Go
170 lines
4.1 KiB
Go
package console
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/filelock"
|
|
)
|
|
|
|
type fakeQMPSession struct {
|
|
mu sync.Mutex
|
|
commands []string
|
|
closed atomic.Bool
|
|
onExec func(cmd string)
|
|
}
|
|
|
|
func (f *fakeQMPSession) Execute(cmd string, _ any) error {
|
|
f.mu.Lock()
|
|
f.commands = append(f.commands, cmd)
|
|
f.mu.Unlock()
|
|
if f.onExec != nil {
|
|
f.onExec(cmd)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeQMPSession) Query(cmd string, _ any) (json.RawMessage, error) {
|
|
return json.RawMessage(`[]`), f.Execute(cmd, nil)
|
|
}
|
|
|
|
func (f *fakeQMPSession) Screendump(string) (string, error) {
|
|
return "ppm", f.Execute("screendump", nil)
|
|
}
|
|
|
|
func (f *fakeQMPSession) Close() error {
|
|
f.closed.Store(true)
|
|
return nil
|
|
}
|
|
|
|
func TestManagerReleasesConnectionAfterOperation(t *testing.T) {
|
|
m := NewManager()
|
|
var sessions []*fakeQMPSession
|
|
m.dial = func(context.Context, string) (qmpSession, error) {
|
|
s := &fakeQMPSession{}
|
|
sessions = append(sessions, s)
|
|
return s, nil
|
|
}
|
|
|
|
dir := t.TempDir()
|
|
if _, err := m.Screendump(context.Background(), dir, "frame.ppm"); err != nil {
|
|
t.Fatalf("Screendump: %v", err)
|
|
}
|
|
if len(sessions) != 1 || !sessions[0].closed.Load() {
|
|
t.Fatalf("expected QMP connection to close immediately after Screendump, sessions=%d closed=%v",
|
|
len(sessions), len(sessions) > 0 && sessions[0].closed.Load())
|
|
}
|
|
|
|
if err := m.SendAbsoluteClickAndPaste(context.Background(), dir, 0.5, 0.5, false, "meta_l", 5*time.Millisecond); err != nil {
|
|
t.Fatalf("SendAbsoluteClickAndPaste: %v", err)
|
|
}
|
|
if len(sessions) != 2 || !sessions[1].closed.Load() {
|
|
t.Fatalf("expected second QMP connection to close immediately after SendAbsoluteClickAndPaste")
|
|
}
|
|
}
|
|
|
|
func TestManagerSerializesSameVMOperations(t *testing.T) {
|
|
m := NewManager()
|
|
var active atomic.Int32
|
|
var maxActive atomic.Int32
|
|
m.dial = func(context.Context, string) (qmpSession, error) {
|
|
return &fakeQMPSession{
|
|
onExec: func(string) {
|
|
cur := active.Add(1)
|
|
for {
|
|
prev := maxActive.Load()
|
|
if cur <= prev || maxActive.CompareAndSwap(prev, cur) {
|
|
break
|
|
}
|
|
}
|
|
time.Sleep(15 * time.Millisecond)
|
|
active.Add(-1)
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
dir := t.TempDir()
|
|
var wg sync.WaitGroup
|
|
for range 4 {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
if _, err := m.Screendump(context.Background(), dir, "frame.ppm"); err != nil {
|
|
t.Errorf("Screendump: %v", err)
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
if got := maxActive.Load(); got != 1 {
|
|
t.Fatalf("expected max 1 concurrent QMP operation per VM, got %d", got)
|
|
}
|
|
}
|
|
|
|
func TestManagerQMPOperationHoldsLifecycleFileLock(t *testing.T) {
|
|
m := NewManager()
|
|
entered := make(chan struct{})
|
|
release := make(chan struct{})
|
|
m.dial = func(context.Context, string) (qmpSession, error) {
|
|
return &fakeQMPSession{onExec: func(string) {
|
|
close(entered)
|
|
<-release
|
|
}}, nil
|
|
}
|
|
|
|
dir := t.TempDir()
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
_, err := m.Screendump(context.Background(), dir, "frame.ppm")
|
|
done <- err
|
|
}()
|
|
<-entered
|
|
|
|
lockFile, err := os.OpenFile(filepath.Join(dir, vmLifecycleLockFile), os.O_CREATE|os.O_RDWR, 0o600)
|
|
if err != nil {
|
|
close(release)
|
|
t.Fatal(err)
|
|
}
|
|
locked, err := filelock.TryLock(lockFile)
|
|
if err != nil {
|
|
close(release)
|
|
lockFile.Close()
|
|
t.Fatal(err)
|
|
}
|
|
if locked {
|
|
filelock.Unlock(lockFile)
|
|
close(release)
|
|
lockFile.Close()
|
|
t.Fatal("QMP operation did not hold the VM lifecycle file lock")
|
|
}
|
|
close(release)
|
|
if err := <-done; err != nil {
|
|
t.Fatalf("Screendump: %v", err)
|
|
}
|
|
lockFile.Close()
|
|
|
|
lockFile, err = os.OpenFile(filepath.Join(dir, vmLifecycleLockFile), os.O_CREATE|os.O_RDWR, 0o600)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer lockFile.Close()
|
|
locked, err = filelock.TryLock(lockFile)
|
|
if err != nil || !locked {
|
|
t.Fatalf("QMP operation did not release the VM lifecycle lock: locked=%v err=%v", locked, err)
|
|
}
|
|
filelock.Unlock(lockFile)
|
|
}
|
|
|
|
func TestSendPasteShortcutComboRejectsBadComboBeforeDialing(t *testing.T) {
|
|
err := NewManager().SendPasteShortcutCombo(t.Context(), t.TempDir(), "ctrl+")
|
|
if err == nil || !strings.Contains(err.Error(), "empty key") {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
}
|