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>
172 lines
6.1 KiB
TypeScript
172 lines
6.1 KiB
TypeScript
import type { VMEndpoint, VMInfo, VMListItem } from '../types'
|
|
|
|
// Guest family, driver capabilities and endpoint ordering for non-macOS guests.
|
|
// Everything here is pure so node:test can load it (scripts/test-endpoints.mjs).
|
|
|
|
const BUILTIN_MACOS_DRIVER = 'builtin.driver.reims-macos'
|
|
|
|
export type GuestFamily = 'macos' | 'linux' | 'windows' | 'android' | ''
|
|
|
|
const GUEST_FAMILIES: readonly string[] = ['macos', 'linux', 'windows', 'android']
|
|
|
|
/** The builtin macOS driver (or a legacy VM entry without a driver id). */
|
|
export function isMacOSDriver(driverID?: string | null): boolean {
|
|
return !driverID || driverID === BUILTIN_MACOS_DRIVER
|
|
}
|
|
|
|
interface GuestRef {
|
|
driver_id?: string
|
|
guest_os?: { family?: string } | null
|
|
}
|
|
|
|
/**
|
|
* The guest family of a VM: what the driver reports, else macOS for the builtin
|
|
* macOS driver, else empty (unknown).
|
|
*/
|
|
export function guestFamilyOf(vm?: GuestRef | null): GuestFamily {
|
|
if (!vm) return ''
|
|
const family = vm.guest_os?.family
|
|
if (family && GUEST_FAMILIES.includes(family)) return family as GuestFamily
|
|
return isMacOSDriver(vm.driver_id) ? 'macos' : ''
|
|
}
|
|
|
|
export function hasCap(caps: readonly string[] | null | undefined, capability: string): boolean {
|
|
return !!caps && caps.includes(capability)
|
|
}
|
|
|
|
/** Hosted VMs are run by their driver: no process, window, PID or QMP socket on the host side. */
|
|
export function isHosted(caps: readonly string[] | null | undefined): boolean {
|
|
return hasCap(caps, 'hosted')
|
|
}
|
|
|
|
export interface VMFeatures {
|
|
macos: boolean
|
|
hosted: boolean
|
|
snapshot: boolean
|
|
clone: boolean
|
|
resize: boolean
|
|
affinity: boolean
|
|
fullscreen: boolean
|
|
screenshot: boolean
|
|
sendText: boolean
|
|
suspend: boolean
|
|
/** A VM window the host can bring forward. */
|
|
window: boolean
|
|
}
|
|
|
|
/**
|
|
* What the UI may offer for a VM. macOS VMs keep every action they always had;
|
|
* every other VM is gated strictly by its driver capabilities.
|
|
*/
|
|
export function vmFeatures(vm?: { driver_id?: string; capabilities?: readonly string[] | null } | null): VMFeatures {
|
|
const caps = vm?.capabilities
|
|
if (isMacOSDriver(vm?.driver_id)) {
|
|
return {
|
|
macos: true, hosted: false, snapshot: true, clone: true, resize: true, affinity: true,
|
|
fullscreen: true, screenshot: true, sendText: true, suspend: true, window: true,
|
|
}
|
|
}
|
|
const hosted = isHosted(caps)
|
|
return {
|
|
macos: false,
|
|
hosted,
|
|
snapshot: hasCap(caps, 'snapshot'),
|
|
clone: hasCap(caps, 'cow_clone'),
|
|
resize: hasCap(caps, 'resize'),
|
|
affinity: hasCap(caps, 'affinity') && !hosted,
|
|
fullscreen: !hosted,
|
|
screenshot: hasCap(caps, 'console') || (hasCap(caps, 'qmp') && !hosted),
|
|
sendText: hasCap(caps, 'console') || (hasCap(caps, 'qmp') && !hosted),
|
|
suspend: hasCap(caps, 'suspend'),
|
|
window: !hosted,
|
|
}
|
|
}
|
|
|
|
/** Highest priority first: the first ready endpoint in this order is the primary hosted action. */
|
|
export const ENDPOINT_PRIORITY = ['vmconnect', 'rdp', 'wsl', 'ssh', 'adb'] as const
|
|
|
|
type EndpointKind = (typeof ENDPOINT_PRIORITY)[number]
|
|
|
|
export type EndpointAction = 'open' | 'terminal' | 'vscode' | 'config' | 'edit' | 'shell' | 'copy'
|
|
|
|
const ACTIONS: Record<EndpointKind, readonly EndpointAction[]> = {
|
|
ssh: ['terminal', 'vscode', 'config'],
|
|
wsl: ['terminal', 'vscode'],
|
|
rdp: ['open'],
|
|
vmconnect: ['open', 'edit'],
|
|
adb: ['shell', 'copy'],
|
|
}
|
|
|
|
function rank(kind: string): number {
|
|
const i = (ENDPOINT_PRIORITY as readonly string[]).indexOf(kind)
|
|
return i < 0 ? ENDPOINT_PRIORITY.length : i
|
|
}
|
|
|
|
function isEndpointKind(kind: string): kind is EndpointKind {
|
|
return rank(kind) < ENDPOINT_PRIORITY.length
|
|
}
|
|
|
|
/** Every action a kind offers; the first one is its default action. */
|
|
export function endpointActions(kind: string): EndpointAction[] {
|
|
return isEndpointKind(kind) ? [...ACTIONS[kind]] : []
|
|
}
|
|
|
|
/** What a primary header button runs for this kind. */
|
|
export function defaultEndpointAction(kind: string): EndpointAction | undefined {
|
|
return endpointActions(kind)[0]
|
|
}
|
|
|
|
/** Actions left once the default one is promoted to the header. */
|
|
export function secondaryEndpointActions(kind: string): EndpointAction[] {
|
|
return endpointActions(kind).slice(1)
|
|
}
|
|
|
|
interface EndpointSplit {
|
|
/** The highest-priority ready endpoint; absent when none is ready. */
|
|
primary?: VMEndpoint
|
|
/** All other endpoints in priority order, never the primary one. */
|
|
rest: VMEndpoint[]
|
|
}
|
|
|
|
/** Endpoints in priority order (unknown kinds last); the order the page lists them in. */
|
|
export function orderEndpoints(endpoints: readonly VMEndpoint[] | null | undefined): VMEndpoint[] {
|
|
return (endpoints ?? []).filter((ep): ep is VMEndpoint => !!ep).sort((a, b) => rank(a.kind) - rank(b.kind))
|
|
}
|
|
|
|
export function splitEndpoints(endpoints: readonly VMEndpoint[] | null | undefined): EndpointSplit {
|
|
const list = (endpoints ?? []).filter((ep): ep is VMEndpoint => !!ep)
|
|
let primaryIdx = -1
|
|
list.forEach((ep, i) => {
|
|
if (!ep.ready || !isEndpointKind(ep.kind)) return
|
|
if (primaryIdx < 0 || rank(ep.kind) < rank(list[primaryIdx].kind)) primaryIdx = i
|
|
})
|
|
const rest = orderEndpoints(list.filter((_, i) => i !== primaryIdx))
|
|
return primaryIdx < 0 ? { rest } : { primary: list[primaryIdx], rest }
|
|
}
|
|
|
|
/** A stable React key for an endpoint row. */
|
|
export function endpointKey(ep: VMEndpoint): string {
|
|
return [ep.kind, ep.host ?? '', ep.port ?? '', ep.distro ?? '', ep.vm ?? '', ep.serial ?? ''].join('|')
|
|
}
|
|
|
|
/**
|
|
* The VM model the page shows for a non-macOS VM: the detail the driver gave
|
|
* (if loaded), with the state, phase, readiness and endpoints taken from the
|
|
* polled list entry, which is the fresher source (a missing field there means
|
|
* "none", not "unchanged"). Capabilities and guest OS fall back to the detail.
|
|
*/
|
|
export function liveVMInfo(item: VMListItem, info?: VMInfo | null): VMInfo {
|
|
const base: VMInfo = info && info.name === item.name && (info.driver_id ?? '') === (item.driver_id ?? '')
|
|
? info
|
|
: { name: item.name, driver_id: item.driver_id ?? '', state: item.state }
|
|
return {
|
|
...base,
|
|
state: item.state,
|
|
capabilities: item.capabilities ?? base.capabilities,
|
|
guest_os: item.guest_os ?? base.guest_os,
|
|
phase: item.phase,
|
|
ready: item.ready,
|
|
endpoints: item.endpoints,
|
|
}
|
|
}
|