Files
Reims-Go/internal/app/quit_flow_test.go
T
NwflowerandClaude Opus 5.5 565577589a refactor: give each VM action one window binding
Every VM action had two bindings: one by name and one by driver and name.
The window used only the second, so the first is gone and the WithDriver
suffix with it. Unused bindings and frontend exports are removed too.

Tests now reject bindings the window never calls, stale generated
bindings and unused frontend exports. TypeScript rejects unused locals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 21:16:28 -07:00

155 lines
4.4 KiB
Go

package app
import (
"context"
"errors"
"os"
"path/filepath"
"slices"
"testing"
"testing/synctest"
"time"
"github.com/Nwflower/reims-go/internal/config"
"github.com/Nwflower/reims-go/internal/driver"
"github.com/Nwflower/reims-go/internal/i18n"
)
func TestStopListedVMs(t *testing.T) {
for _, tc := range []struct {
name, wantErr string
}{
{name: "all stopped"},
{name: "failure", wantErr: "无法关闭虚拟机「two」,已取消退出。"},
{name: "still running", wantErr: "无法关闭虚拟机「two」,已取消退出。"},
{name: "timeout", wantErr: "虚拟机「two」关机超时,已取消退出。"},
} {
t.Run(tc.name, func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
vms := []driver.VMListItem{
{Name: "one", State: "running"}, {Name: "off", State: "stopped"},
{Name: "two", State: "running"}, {Name: "saved", State: "suspended"}, {Name: "three", State: "running"},
}
var calls []string
err := stopListedVMs(t.Context(), i18n.ZhCN, func(context.Context) ([]driver.VMListItem, error) {
return vms, nil
}, func(ctx context.Context, vm driver.VMListItem) error {
name := vm.Name
calls = append(calls, name)
if name == "two" {
switch tc.name {
case "failure":
return errors.New("stop failed")
case "still running":
return nil
case "timeout":
started := time.Now()
<-ctx.Done()
if elapsed := time.Since(started); elapsed != 10*time.Second {
t.Errorf("shutdown timed out after %v, want 10s", elapsed)
}
return errors.New("exit status 1")
}
}
for i := range vms {
if vms[i].Name == name {
vms[i].State = "stopped"
}
}
return nil
})
want := []string{"one", "two", "three"}
if tc.wantErr != "" {
want = want[:2]
if err == nil || err.Error() != tc.wantErr {
t.Fatalf("error = %v, want %q", err, tc.wantErr)
}
} else if err != nil {
t.Fatal(err)
}
if !slices.Equal(calls, want) {
t.Fatalf("stop calls = %v, want %v", calls, want)
}
})
})
}
}
func TestQuitAppLeavesVMsRunning(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
a := &App{lastVMs: []driver.VMListItem{{Name: "one", State: "running"}}}
if err := a.beginVMStart(); err != nil {
t.Fatal(err)
}
defer a.vmStarts.Done()
done := make(chan struct{})
go func() {
a.QuitApp()
close(done)
}()
synctest.Wait()
select {
case <-done:
if !a.allowQuit || a.quitMode != "" {
t.Fatalf("tray exit: allowQuit=%v mode=%q", a.allowQuit, a.quitMode)
}
default:
t.Fatal("tray exit waited for VM operations")
}
})
}
func TestForceQuitAppWaitsForStartsAndCanRetry(t *testing.T) {
a := &App{cfg: &config.Config{DataRoot: t.TempDir(), Locale: i18n.EnUS}}
if err := a.beginVMStart(); err != nil {
t.Fatal(err)
}
quit := make(chan error, 1)
go func() { quit <- a.ForceQuitApp() }()
deadline := time.Now().Add(3 * time.Second)
for {
a.mu.Lock()
quitting := a.quitMode != ""
a.mu.Unlock()
if quitting {
break
}
if time.Now().After(deadline) {
t.Fatal("quit did not start")
}
time.Sleep(time.Millisecond)
}
for _, launch := range []func(driverID, name string) error{a.StartVM, a.ResumeVM} {
if err := launch("", "new"); err == nil || err.Error() != i18n.T(i18n.EnUS, "quitInProgress") {
t.Fatalf("launch during shutdown: %v", err)
}
}
a.QuitApp()
if !a.beforeClose(context.Background()) {
t.Fatal("another close bypassed the pending shutdown")
}
if err := a.ForceQuitApp(); err != nil {
t.Fatal(err)
}
if a.quitMode != "force" || a.allowQuit {
t.Fatalf("duplicate quit: mode=%q allowQuit=%v", a.quitMode, a.allowQuit)
}
// The in-flight start creates a VM entry after the quit request. Without
// a runtime, shutdown must now fail instead of assuming an empty library.
dir := filepath.Join(a.cfg.XDGDataDir(), "reims-ctl", "vms", "starting")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
a.vmStarts.Done()
if err := <-quit; err == nil || a.allowQuit || a.quitMode != "" {
t.Fatalf("failed shutdown: err=%v allowQuit=%v mode=%q", err, a.allowQuit, a.quitMode)
}
if err := os.Remove(dir); err != nil {
t.Fatal(err)
}
if err := a.ForceQuitApp(); err != nil || !a.allowQuit || a.beforeClose(context.Background()) {
t.Fatalf("retry with empty library: err=%v allowQuit=%v", err, a.allowQuit)
}
}