Files
Reims-Go/internal/runtime/selection_test.go
T
Nwflower 43291667a6 feat(ui,host): default host memory specs, dedupe Vulkan GPUs
Sync the host's SMBIOS Type 17 memory spec by default and drop the toggle:
the frontend switch, the App.SetSyncHostMemorySMBIOS binding and the
sync_host_memory_smbios config field are gone, so macOS About This Mac always
reports the host's real RAM speed and generation. A manual -smbios type=17
argument still wins, and a host without a readable speed keeps the default.

Deduplicate repeated Vulkan physical devices on the Runtime page when several
ICD manifests enumerate the same GPU, preferring the healthiest/newest probe
while keeping genuinely distinct adapters. Move Appearance & language to the
top of Settings.

model: deepseek/deepseek-v4.1-flash
agent: lead (DeepSeek Harness)
2026-12-04 06:32:51 -07:00

193 lines
7.5 KiB
Go

package runtime
import (
"bytes"
"context"
"errors"
"os"
"path/filepath"
"runtime"
"slices"
"testing"
"github.com/Nwflower/reims-go/internal/config"
"github.com/Nwflower/reims-go/internal/host"
"github.com/Nwflower/reims-go/internal/i18n"
)
func TestSelectionFollowsRuntimeAndToggle(t *testing.T) {
cfg := &config.Config{DataRoot: t.TempDir()}
var targets []string
sync := func(_ context.Context, root, path string) error {
if root != cfg.RuntimesDir() {
t.Fatalf("unexpected runtimes root %q", root)
}
targets = append(targets, path)
return nil
}
selectRuntime := func(client *config.Config, commit string, toggle *bool) {
t.Helper()
if err := saveSelection(context.Background(), client, commit, toggle, sync, nil); err != nil {
t.Fatal(err)
}
}
current := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "current")}
backup := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "backup")}
writeFakeRuntime(t, current.Dir, Manifest{Commit: "current"})
writeFakeRuntime(t, backup.Dir, Manifest{Commit: "backup"})
on, off := true, false
selectRuntime(cfg, "current", nil) // An older config has ASLR off.
stale := *cfg
selectRuntime(cfg, "", &on)
selectRuntime(&stale, "backup", nil) // CLI picks up the desktop preference.
selectRuntime(cfg, "current", nil)
selectRuntime(cfg, "", &off)
selectRuntime(&stale, "backup", nil)
want := []string{current.QemuPath(), backup.QemuPath(), current.QemuPath(), ""}
if !slices.Equal(targets, want) {
t.Fatalf("synced paths = %q, want %q", targets, want)
}
saved, err := config.Load(filepath.Join(cfg.DataRoot, "reims-go.json"))
if err != nil || saved.QEMUASLR || saved.ActiveRuntime != "backup" {
t.Fatalf("saved selection = %+v, %v", saved, err)
}
}
func TestSelectionFailureKeepsPreviousRuntime(t *testing.T) {
cfg := &config.Config{DataRoot: t.TempDir(), ActiveRuntime: "old", QEMUASLR: true}
for _, commit := range []string{"old", "new"} {
writeFakeRuntime(t, filepath.Join(cfg.RuntimesDir(), commit), Manifest{Commit: commit})
}
if err := config.Save("", cfg); err != nil {
t.Fatal(err)
}
calls := 0
err := saveSelection(context.Background(), cfg, "new", nil, func(context.Context, string, string) error {
calls++
return host.ErrElevationDeclined
}, nil)
if !errors.Is(err, host.ErrElevationDeclined) || calls != 1 {
t.Fatalf("declined activation: calls = %d, error = %v", calls, err)
}
// Block the commit lock so the successful host update must be rolled back.
if err := os.Mkdir(filepath.Join(cfg.RuntimesDir(), ".reims-go-runtime.lock"), 0o755); err != nil {
t.Fatal(err)
}
var applied []string
err = saveSelection(context.Background(), cfg, "new", nil, func(_ context.Context, _, path string) error {
applied = append(applied, path)
return nil
}, nil)
old := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "old")}
next := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "new")}
if err == nil || !slices.Equal(applied, []string{next.QemuPath(), old.QemuPath()}) {
t.Fatalf("rollback paths = %v, error = %v", applied, err)
}
saved, err := config.Load(filepath.Join(cfg.DataRoot, "reims-go.json"))
if err != nil || cfg.ActiveRuntime != "old" || saved.ActiveRuntime != "old" || !saved.QEMUASLR {
t.Fatalf("failed selection was committed: %+v, %v", saved, err)
}
}
func TestSelectionRevalidatesAfterConsent(t *testing.T) {
cfg := config.Config{DataRoot: t.TempDir(), ActiveRuntime: "old", QEMUASLR: true}
for _, id := range []string{"old", "new"} {
writeFakeRuntime(t, filepath.Join(cfg.RuntimesDir(), id), Manifest{Commit: id})
}
if err := config.Save("", &cfg); err != nil {
t.Fatal(err)
}
var paths []string
err := saveSelection(t.Context(), &cfg, "new", nil, func(_ context.Context, _, path string) error {
paths = append(paths, path)
if len(paths) == 1 {
prefs := cfg
prefs.AutoPinPCores = false
if err := SavePreferences(&prefs); err != nil {
t.Fatal(err)
}
_, err := Remove(&prefs, "new")
return err
}
return nil
}, nil)
old := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "old")}
if !errors.Is(err, ErrNotFound) || len(paths) != 2 || paths[1] != old.QemuPath() {
t.Fatalf("removed during consent: paths = %q, err = %v", paths, err)
}
saved, err := config.LoadExisting(filepath.Join(cfg.DataRoot, "reims-go.json"))
if err != nil || saved.ActiveRuntime != "old" || saved.AutoPinPCores {
t.Fatalf("lost settings while awaiting consent: %+v, %v", saved, err)
}
}
func TestActivationWarnsOnImagePolicyConflict(t *testing.T) {
cfg := config.Config{DataRoot: t.TempDir(), QEMUASLR: true}
writeFakeRuntime(t, filepath.Join(cfg.RuntimesDir(), "new"), Manifest{Commit: "new"})
conflict := func(context.Context, string, string) error { return host.ErrQEMUASLRImagePolicy }
var warning error
if err := saveSelection(t.Context(), &cfg, "new", nil, conflict, func(err error) { warning = err }); err != nil {
t.Fatal(err)
}
var coded *i18n.CodedError
if !errors.As(warning, &coded) || coded.Code != "E_QEMU_ASLR_IMAGE_POLICY" {
t.Fatalf("activation warning = %v", warning)
}
saved, err := config.LoadExisting(filepath.Join(cfg.DataRoot, "reims-go.json"))
if err != nil || saved.ActiveRuntime != "new" || !saved.QEMUASLR {
t.Fatalf("conflicted activation = %+v, %v", saved, err)
}
on := true
err = saveSelection(t.Context(), &cfg, "", &on, conflict, nil)
if !errors.As(err, &coded) || coded.Code != "E_QEMU_ASLR_IMAGE_POLICY" {
t.Fatalf("explicit repair error = %v", err)
}
}
func TestLaunchPolicyRejectsSupersededRuntime(t *testing.T) {
cfg := config.Config{DataRoot: t.TempDir(), ActiveRuntime: "backup", QEMUASLR: true}
current := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "current")}
writeFakeRuntime(t, current.Dir, Manifest{Commit: "current"})
writeFakeRuntime(t, filepath.Join(cfg.RuntimesDir(), "backup"), Manifest{Commit: "backup"})
if err := config.Save("", &cfg); err != nil {
t.Fatal(err)
}
stale := cfg
stale.ActiveRuntime, stale.QEMUASLR = "current", false
called := false
denied := errors.New("access denied")
apply := func(_, _ string) error { called = true; return denied }
if err := checkLaunchASLR(t.Context(), stale, current.QemuPath(), nil, apply); err == nil || called {
t.Fatal("launch would reapply policy to the superseded runtime")
}
backup := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "backup")}
var warning bytes.Buffer
if err := checkLaunchASLR(t.Context(), stale, backup.QemuPath(), &warning, apply); err != nil || !called || !bytes.Contains(warning.Bytes(), []byte(denied.Error())) {
t.Fatalf("launch should continue with a policy warning: %v, %q", err, warning.String())
}
}
func TestNonWindowsIgnoresSavedQEMUASLR(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("non-Windows host")
}
cfg := config.Config{DataRoot: t.TempDir(), ActiveRuntime: "current", QEMUASLR: true}
for _, commit := range []string{"current", "backup"} {
writeFakeRuntime(t, filepath.Join(cfg.RuntimesDir(), commit), Manifest{Commit: commit})
}
if err := config.Save("", &cfg); err != nil {
t.Fatal(err)
}
if err := Activate(t.Context(), &cfg, "backup", nil); err != nil {
t.Fatal(err)
}
current := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "current")}
if err := CheckLaunchASLR(t.Context(), cfg, current.QemuPath(), nil); err != nil {
t.Fatalf("Windows policy blocked launch on %s: %v", runtime.GOOS, err)
}
saved, err := config.LoadExisting(filepath.Join(cfg.DataRoot, "reims-go.json"))
if err != nil || !saved.QEMUASLR || saved.ActiveRuntime != "backup" {
t.Fatalf("lost runtime selection or portable preference: %+v, %v", saved, err)
}
}