132 lines
4.2 KiB
Go
132 lines
4.2 KiB
Go
package builtin
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"runtime"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/appwindow"
|
|
"github.com/Nwflower/reims-go/internal/guesttools"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
func TestAppWindowPluginDefaultOffAndCLIState(t *testing.T) {
|
|
reg := plugin.NewRegistry(nil)
|
|
t.Cleanup(reg.DisposeAll)
|
|
if err := RegisterAll(reg, HostCallbacks{}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reg.Reconcile(t.Context())
|
|
info, ok := reg.Info(IDAppWindow)
|
|
if !ok || info.Enabled || info.State != plugin.StateOff {
|
|
t.Fatalf("default app window plugin state: %+v", info)
|
|
}
|
|
assertAppWindowContributions(t, reg, false)
|
|
if err := reg.SetDesired(t.Context(), IDAppWindow, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reg.IsActive(IDAppWindow) {
|
|
t.Fatalf("CLI cannot enable app window plugin state: %s", reg.StateOf(IDAppWindow))
|
|
}
|
|
assertAppWindowContributions(t, reg, false)
|
|
}
|
|
|
|
func TestAppWindowPluginOwnsWindowAcrossDependencyChanges(t *testing.T) {
|
|
if runtime.GOOS != "windows" || runtime.GOARCH != "amd64" {
|
|
t.Skip("native app window command is available on Windows x64")
|
|
}
|
|
for _, dependency := range []string{IDGuestTools, IDComputerUse} {
|
|
t.Run(dependency, func(t *testing.T) {
|
|
reg := plugin.NewRegistry(nil)
|
|
t.Cleanup(reg.DisposeAll)
|
|
var owner *appwindow.Sessions
|
|
var closed chan struct{}
|
|
cb := HostCallbacks{
|
|
OpenAppWindow: func(ctx context.Context, sessions *appwindow.Sessions, tools *guesttools.Server, console ComputerUseService, vm string) error {
|
|
if tools == nil || console == nil || vm != "fixture" {
|
|
t.Fatalf("invalid command arguments: tools=%v console=%v vm=%q", tools != nil, console != nil, vm)
|
|
}
|
|
owner = sessions
|
|
done := make(chan struct{})
|
|
closed = done
|
|
return sessions.Start(ctx, func(sessionCtx context.Context) (func(), error) {
|
|
return func() {
|
|
<-sessionCtx.Done()
|
|
close(done)
|
|
}, nil
|
|
})
|
|
},
|
|
}
|
|
if err := RegisterAll(reg, cb, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := reg.SetDesired(t.Context(), IDAppWindow, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertAppWindowContributions(t, reg, true)
|
|
if _, err := reg.InvokeCommand(t.Context(), IDAppWindow, "open", map[string]any{"vm": 17}); err == nil {
|
|
t.Fatal("native window command accepted a non-string VM name")
|
|
}
|
|
open := func() {
|
|
t.Helper()
|
|
if _, err := reg.InvokeCommand(t.Context(), IDAppWindow, "open", map[string]any{"vm": "fixture"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
open()
|
|
first := owner
|
|
if err := reg.SetDesired(t.Context(), dependency, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if reg.StateOf(IDAppWindow) != plugin.StatePending {
|
|
t.Fatalf("app window plugin remained active without %s", dependency)
|
|
}
|
|
select {
|
|
case <-closed:
|
|
default:
|
|
t.Fatalf("window was not closed when %s was disabled", dependency)
|
|
}
|
|
assertAppWindowContributions(t, reg, false)
|
|
if err := first.Start(t.Context(), func(context.Context) (func(), error) {
|
|
t.Fatal("disposed scope still accepted a window")
|
|
return nil, nil
|
|
}); !errors.Is(err, appwindow.ErrSessionsClosed) {
|
|
t.Fatalf("disposed session owner returned %v", err)
|
|
}
|
|
|
|
if err := reg.SetDesired(t.Context(), dependency, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assertAppWindowContributions(t, reg, true)
|
|
open()
|
|
if owner == first {
|
|
t.Fatal("reactivation reused the disposed session owner")
|
|
}
|
|
if err := reg.SetDesired(t.Context(), IDAppWindow, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
select {
|
|
case <-closed:
|
|
default:
|
|
t.Fatal("disabling the app window plugin left a window open")
|
|
}
|
|
assertAppWindowContributions(t, reg, false)
|
|
})
|
|
}
|
|
}
|
|
|
|
func assertAppWindowContributions(t *testing.T, reg *plugin.Registry, want bool) {
|
|
t.Helper()
|
|
hasCommand := slices.ContainsFunc(reg.Commands(), func(cmd plugin.CommandContribution) bool {
|
|
return cmd.PluginID == IDAppWindow && cmd.ID == "open"
|
|
})
|
|
hasMenu := slices.ContainsFunc(reg.Menus("vm.toolbar"), func(item plugin.MenuContribution) bool {
|
|
return item.PluginID == IDAppWindow && item.Command == "open" && item.When == "vm.macos.running"
|
|
})
|
|
if hasCommand != want || hasMenu != want {
|
|
t.Fatalf("app window command=%v menu=%v, want %v", hasCommand, hasMenu, want)
|
|
}
|
|
}
|