Fix runtime selection and graceful shutdown fallback for Stambha
Windows CI / verify (push) In progress
Windows CI / verify (push) In progress
This commit was merged in pull request #6.
This commit is contained in:
1 parent
f63565ff3a
commit
edf1c004e6
7 files changed
+130
-19
No files matched your search
@@ -46,3 +46,54 @@ test('missing remote latest marks runtime as updatable', () => {
|
||||
assert.equal(s.updatable, true)
|
||||
assert.equal(s.switchRecommended, false)
|
||||
})
|
||||
|
||||
test('Stambha never supplies the online x86 runtime or satisfies its latest commit', () => {
|
||||
const arm = { ...rt('6a1f03f'), kind: 'stambha-hosted', manifest: {} }
|
||||
const x86 = { ...rt('2df30ff'), kind: 'reims-vgpu' }
|
||||
const onlyArm = describeRuntimeStatus([arm], remote(arm.commit))
|
||||
assert.equal(onlyArm.active, undefined)
|
||||
assert.equal(onlyArm.latestInstalled, false)
|
||||
assert.equal(onlyArm.updatable, true)
|
||||
const mixed = describeRuntimeStatus([arm, x86], remote(arm.commit))
|
||||
assert.equal(mixed.active, x86)
|
||||
assert.equal(mixed.latestInstalled, false)
|
||||
assert.equal(mixed.updatable, true)
|
||||
})
|
||||
|
||||
test('multiple unselected x86 runtimes require an explicit choice', () => {
|
||||
const s = describeRuntimeStatus([rt('first'), rt('second')], remote('first'))
|
||||
assert.equal(s.active, undefined)
|
||||
assert.equal(s.latestInstalled, true)
|
||||
})
|
||||
|
||||
test('setup and the host runtime page render an ARM-only installation without x86 controls', async () => {
|
||||
globalThis.window = {}
|
||||
try {
|
||||
const { render } = await loadTestModule(`
|
||||
import { createElement } from 'react'
|
||||
import { renderToStaticMarkup } from 'react-dom/server.edge'
|
||||
import { TooltipProvider } from './ui/controls'
|
||||
import { Setup } from './components/Setup'
|
||||
import { HostView } from './components/HostView'
|
||||
export function render(page, state) {
|
||||
const view = createElement(page === 'setup' ? Setup : HostView, { locale: 'en-US', state, stage: 'runtime' })
|
||||
return renderToStaticMarkup(createElement(TooltipProvider, null, view))
|
||||
}
|
||||
`)
|
||||
const state = {
|
||||
host: { os: 'windows', arch: 'amd64', whpx: 'ready', total_ram_gb: 16, avail_ram_gb: 12, free_disk_gb: 100 },
|
||||
config: { runtime_channel: 'stable' },
|
||||
runtimes: [{ ...rt('arm-preview-revision'), kind: 'stambha-hosted', manifest: { name: 'ARM Preview', runtime_api: '0.1.0' } }],
|
||||
}
|
||||
const setup = render('setup', state)
|
||||
assert.match(setup, /Online install/)
|
||||
assert.match(setup, />Done</)
|
||||
assert.doesNotMatch(setup, /arm-preview-revision|Current runtime|>Activate</)
|
||||
const host = render('host', state)
|
||||
assert.match(host, /Online install/)
|
||||
assert.match(host, /ARM Preview/)
|
||||
assert.doesNotMatch(host, /Current runtime|>Activate</)
|
||||
} finally {
|
||||
delete globalThis.window
|
||||
}
|
||||
})
|
||||
@@ -11,7 +11,7 @@ import { AppState, Locale, StorageBreakdown, Task } from '../types'
|
||||
import { Button, IconButton } from '../ui/controls'
|
||||
import { Switch } from '../ui/settings'
|
||||
import { api } from '../wails'
|
||||
import { sameRuntimeId } from '../lib/runtimeStatus'
|
||||
import { isDefaultRuntime, sameRuntimeId } from '../lib/runtimeStatus'
|
||||
import { RuntimeOnline } from './RuntimeOnline'
|
||||
|
||||
interface Props {
|
||||
@@ -249,9 +249,9 @@ export const HostView: React.FC<Props> = ({ state, locale, onRefresh, onNewVM, h
|
||||
<div className="row-main">
|
||||
<div className="row-title">
|
||||
<span className="mono">{stambha ? rt.manifest.name || rt.commit : rt.commit}</span>
|
||||
{(stambha || !rt.kind || rt.kind === 'reims-vgpu') &&
|
||||
{(stambha || isDefaultRuntime(rt)) &&
|
||||
<span className="badge rg-p-badge">{tr(stambha ? 'armRuntimeLabel' : 'x86RuntimeLabel')}</span>}
|
||||
{!stambha && latestRuntimeCommit && sameRuntimeId(rt.commit, latestRuntimeCommit) && (
|
||||
{isDefaultRuntime(rt) && latestRuntimeCommit && sameRuntimeId(rt.commit, latestRuntimeCommit) && (
|
||||
<span className="badge rg-p-badge badge-ok">{tr('runtimeLatestBadge')}</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -22,9 +22,6 @@ interface Props {
|
||||
|
||||
const gb = (b: number) => (b >= 1e9 ? `${(b / 1e9).toFixed(1)} GB` : `${Math.ceil(b / 1e6)} MB`)
|
||||
|
||||
/** "QEMU emulator version 11.1.90" → "QEMU 11.1.90". */
|
||||
const shortQemu = (v: string) => v.replace(/^QEMU emulator version\s*/i, 'QEMU ')
|
||||
|
||||
/**
|
||||
* The runtime rows: the active runtime and whether it is current (or, with
|
||||
* none installed, the newest release to install), then the alpha switch.
|
||||
@@ -47,6 +44,7 @@ export const RuntimeOnline: React.FC<Props> = ({ locale, channel, installed, tas
|
||||
|
||||
const latest = remote?.[0]
|
||||
const { active, latestCommit, activeIsLatest, updatable, switchRecommended } = describeRuntimeStatus(installed, latest)
|
||||
const qemuVersion = active?.manifest.qemu_version?.replace(/^QEMU emulator version\s*/i, 'QEMU ')
|
||||
const running = task?.status === 'running'
|
||||
|
||||
useEffect(() => {
|
||||
@@ -69,11 +67,11 @@ export const RuntimeOnline: React.FC<Props> = ({ locale, channel, installed, tas
|
||||
} else if (switchRecommended && active && latest) {
|
||||
sub = (
|
||||
<div className="row-sub">
|
||||
{shortQemu(active.manifest.qemu_version)} · {tr('runtimeRecommendLatest', { v: latest.commit })}
|
||||
{qemuVersion && <>{qemuVersion} · </>}{tr('runtimeRecommendLatest', { v: latest.commit })}
|
||||
</div>
|
||||
)
|
||||
} else if (active && !updatable) {
|
||||
sub = <div className="row-sub">{shortQemu(active.manifest.qemu_version)}</div>
|
||||
sub = qemuVersion && <div className="row-sub">{qemuVersion}</div>
|
||||
} else if (error) {
|
||||
const isRateLimit = /rate limit|HTTP 403|HTTP 429/i.test(error)
|
||||
const msg = isRateLimit ? tr('runtimeLookupRateLimit') : `${tr('runtimeLookupFailed')}: ${error}`
|
||||
|
||||
@@ -9,6 +9,7 @@ import { PopupSelect } from '../ui/settings'
|
||||
import { gpuCompatHint, recommendedMemoryGB, shortGPUName } from '../lib/format'
|
||||
import { describeHostWHPX } from '../lib/hypervisor'
|
||||
import { hasStambhaRuntime } from '../lib/endpoints'
|
||||
import { isDefaultRuntime } from '../lib/runtimeStatus'
|
||||
import { api } from '../wails'
|
||||
import { PluginAppIcon } from './PluginAppIcon'
|
||||
import { RuntimeOnline } from './RuntimeOnline'
|
||||
@@ -312,7 +313,7 @@ const RuntimeStep: React.FC<StepProps> = ({ locale, tr, state, runtimeTask, onCh
|
||||
onInstall={onInstallRuntime}
|
||||
onCancel={onCancelTask}
|
||||
/>
|
||||
{(state.runtimes ?? []).filter((rt) => !rt.active).map((rt) => (
|
||||
{(state.runtimes ?? []).filter((rt) => isDefaultRuntime(rt) && !rt.active).map((rt) => (
|
||||
<div className="row" key={rt.commit}>
|
||||
<div className="row-main"><div className="row-title mono">{rt.commit}</div></div>
|
||||
<div className="row-right">
|
||||
|
||||
@@ -15,8 +15,13 @@ interface RuntimeStatus {
|
||||
switchRecommended: boolean
|
||||
}
|
||||
|
||||
export function describeRuntimeStatus(installed: InstalledRuntime[], latest?: RemoteRuntime): RuntimeStatus {
|
||||
const active = installed.find((i) => i.active) ?? installed[0]
|
||||
export function isDefaultRuntime(rt: InstalledRuntime): boolean {
|
||||
return !rt.kind || rt.kind === 'reims-vgpu'
|
||||
}
|
||||
|
||||
export function describeRuntimeStatus(runtimes: InstalledRuntime[], latest?: RemoteRuntime): RuntimeStatus {
|
||||
const installed = runtimes.filter(isDefaultRuntime)
|
||||
const active = installed.find((i) => i.active) ?? (installed.length === 1 ? installed[0] : undefined)
|
||||
const latestCommit = latest?.commit ?? ''
|
||||
const activeIsLatest = !!active && !!latestCommit && sameRuntimeId(active.commit, latestCommit)
|
||||
const latestInstalled = !!latestCommit && installed.some((i) => sameRuntimeId(i.commit, latestCommit))
|
||||
|
||||
@@ -73,22 +73,41 @@ func (d *StambhaDriver) requestLoginWindowShutdown(ctx context.Context, conn *qm
|
||||
return true, err
|
||||
}
|
||||
if !qmp.HasCurrentAbsoluteMouse(mice) {
|
||||
return true, errors.New("the macOS login window needs an absolute pointer to use its power menu")
|
||||
return false, nil
|
||||
}
|
||||
menu := loginPowerMenu{icon: icon, scale: scale, clockRegion: clockRegion}
|
||||
return menu.tryShutdown(ctx, conn, frame, readFrame, engine.Recognize)
|
||||
}
|
||||
|
||||
type loginPowerMenu struct {
|
||||
icon image.Point
|
||||
scale int
|
||||
clockRegion image.Rectangle
|
||||
}
|
||||
|
||||
// A false result leaves the normal power-button path available. Once a click
|
||||
// on Shut Down was sent or its delivery became uncertain, never issue a second request.
|
||||
func (menu loginPowerMenu) tryShutdown(ctx context.Context, conn qmp.Executor, frame image.Image, readFrame func() (image.Image, error), recognize func(context.Context, image.Image) (ocr.Result, error)) (bool, error) {
|
||||
icon, scale := menu.icon, menu.scale
|
||||
clockRegion := menu.clockRegion
|
||||
bounds := frame.Bounds()
|
||||
iconArea := image.Rect(icon.X-7*scale, icon.Y-7*scale, icon.X+8*scale, icon.Y+8*scale)
|
||||
crop := image.Rect(bounds.Max.X-165*scale, bounds.Min.Y, bounds.Max.X, bounds.Min.Y+125*scale).Intersect(bounds)
|
||||
before := scalePowerMenu(frame, crop)
|
||||
menuCtx, cancel := context.WithTimeout(ctx, 7*time.Second)
|
||||
defer cancel()
|
||||
opened := false
|
||||
for menuCtx.Err() == nil {
|
||||
menu := scalePowerMenu(frame, crop)
|
||||
for {
|
||||
if menuCtx.Err() != nil {
|
||||
return false, ctx.Err()
|
||||
}
|
||||
currentMenu := scalePowerMenu(frame, crop)
|
||||
// The user may already have opened the power menu. In that case
|
||||
// select the verified entry without toggling the menu closed first.
|
||||
if !opened || menuAppeared(before, menu) {
|
||||
result, ocrErr := engine.Recognize(menuCtx, menu)
|
||||
if !opened || menuAppeared(before, currentMenu) {
|
||||
result, ocrErr := recognize(menuCtx, currentMenu)
|
||||
if ocrErr != nil {
|
||||
break
|
||||
return false, ctx.Err()
|
||||
}
|
||||
if entry, ok := shutdownMenuEntry(result.Lines); ok {
|
||||
point := crop.Min.Add(entry.Div(3))
|
||||
@@ -125,8 +144,9 @@ func (d *StambhaDriver) requestLoginWindowShutdown(ctx context.Context, conn *qm
|
||||
opened = true
|
||||
}
|
||||
if waitShutdownInput(menuCtx, 400*time.Millisecond) != nil {
|
||||
break
|
||||
return false, ctx.Err()
|
||||
}
|
||||
var err error
|
||||
frame, err = readFrame()
|
||||
if err != nil {
|
||||
return true, err
|
||||
@@ -141,7 +161,7 @@ func (d *StambhaDriver) requestLoginWindowShutdown(ctx context.Context, conn *qm
|
||||
return true, errors.New("could not identify the macOS Shut Down option. Open the power menu in the virtual machine to shut down")
|
||||
}
|
||||
|
||||
func clickGuestPowerMenu(ctx context.Context, conn *qmp.Conn, p image.Point, bounds image.Rectangle) error {
|
||||
func clickGuestPowerMenu(ctx context.Context, conn qmp.Executor, p image.Point, bounds image.Rectangle) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package stambhadriver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
@@ -83,3 +85,37 @@ func TestPowerMenuClickRejectsChangedFrame(t *testing.T) {
|
||||
t.Fatal("accepted changed or resized menu")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginPowerMenuFallback(t *testing.T) {
|
||||
frame := image.NewRGBA(image.Rect(0, 0, 640, 400))
|
||||
menu := loginPowerMenu{icon: image.Pt(600, 15), scale: 1, clockRegion: image.Rect(160, 24, 480, 135)}
|
||||
capture := func() (image.Image, error) { return frame, nil }
|
||||
|
||||
t.Run("unavailable OCR leaves the power button available", func(t *testing.T) {
|
||||
q := &shutdownRecorder{}
|
||||
recognize := func(context.Context, image.Image) (ocr.Result, error) {
|
||||
return ocr.Result{}, errors.New("OCR unavailable")
|
||||
}
|
||||
handled, err := menu.tryShutdown(context.Background(), q, frame, capture, recognize)
|
||||
if handled || err != nil || len(q.commands) != 0 {
|
||||
t.Fatalf("handled=%v err=%v input=%v", handled, err, q.commands)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("cancellation after the action press does not request power again", func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
q := &shutdownRecorder{onHold: cancel}
|
||||
recognize := func(context.Context, image.Image) (ocr.Result, error) {
|
||||
return ocr.Result{Lines: []ocr.Line{
|
||||
{Text: "Restart", Rect: image.Rect(95, 125, 235, 155)},
|
||||
{Text: "Shut Down", Rect: image.Rect(95, 200, 305, 230)},
|
||||
{Text: "Sleep", Rect: image.Rect(95, 275, 195, 305)},
|
||||
}}, nil
|
||||
}
|
||||
handled, err := menu.tryShutdown(ctx, q, frame, capture, recognize)
|
||||
if !handled || !errors.Is(err, context.Canceled) || len(q.commands) != 2 {
|
||||
t.Fatalf("handled=%v err=%v input=%v", handled, err, q.commands)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user