Files
orca/tests/e2e/helpers/windows-host-orcad-processes.ts
OrcaWinandOrcaWin 0184d8187d test: remove 98 low-value cases and 17 suites (#41124)
* test: remove 60 low-value cases and orphaned conversion fixtures

* docs(ci): list supported Windows SSH host cells

* test: recognize direct Windows orcad server entry

---------

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-11-11 06:08:53 -08:00

69 lines
2.6 KiB
TypeScript

// Observe the Windows host-cell account locally: the runner provisioned it and its sshd listens on loopback.
import path from 'node:path'
import { runProcess } from '@orca/process-host'
export type HostProcess = { pid: number; commandLine: string }
const POWERSHELL = path.join(
process.env.SystemRoot ?? 'C:\\Windows',
'System32',
'WindowsPowerShell',
'v1.0',
'powershell.exe'
)
// Why ExecutablePath: orcad runs on the pinned node.exe staged under the account's home.
const LIST_SCRIPT =
"$h=$env:ORCA_E2E_HOST_HOME.TrimEnd('\\','/')+'\\';" +
'@(Get-CimInstance Win32_Process | Where-Object {$_.ExecutablePath -and $_.ExecutablePath.StartsWith($h,[StringComparison]::OrdinalIgnoreCase)} | ForEach-Object {@{pid=[int]$_.ProcessId;commandLine=[string]$_.CommandLine}}) | ConvertTo-Json -Compress -Depth 3'
async function powershell(script: string, home: string): Promise<string> {
const result = await runProcess({
program: POWERSHELL,
args: ['-NoProfile', '-NonInteractive', '-Command', script],
env: { ...process.env, ORCA_E2E_HOST_HOME: home.replaceAll('/', '\\') },
timeoutMs: 60_000
})
if (result.code !== 0) {
throw new Error(`Host process query failed (${result.code}): ${result.stderr.slice(0, 2_000)}`)
}
return result.stdout.trim()
}
export async function listHostAccountProcesses(home: string): Promise<HostProcess[]> {
const text = await powershell(LIST_SCRIPT, home)
if (!text) {
return []
}
const parsed: unknown = JSON.parse(text)
const rows = Array.isArray(parsed) ? parsed : [parsed]
return rows.flatMap((row) =>
row && typeof row.pid === 'number'
? [{ pid: row.pid, commandLine: String(row.commandLine ?? '') }]
: []
)
}
export async function listHostOrcadProcesses(home: string): Promise<HostProcess[]> {
return (await listHostAccountProcesses(home)).filter((entry) => /orcad/iu.test(entry.commandLine))
}
/** The orcad server itself, without the terminal daemon and workers it forks from the same slot. */
export async function listHostOrcadServerProcesses(home: string): Promise<HostProcess[]> {
return (await listHostOrcadProcesses(home)).filter((entry) =>
/[\\/]orcad(?:-server)?\.js(?=["\s]|$)/iu.test(entry.commandLine)
)
}
/** Hard-kills the account's orcad server, as a crash would; its terminal daemon keeps running. */
export async function killHostOrcad(home: string): Promise<HostProcess[]> {
const victims = await listHostOrcadServerProcesses(home)
if (victims.length > 0) {
await powershell(
`Stop-Process -Force -ErrorAction SilentlyContinue -Id ${victims.map((entry) => entry.pid).join(',')}`,
home
)
}
return victims
}