Files
orca/tests/e2e/helpers/daemon-generation-processes.ts
T
Jinjing 7a695c70f1 test(e2e): harden triaged CI failures (#14656)
* test(e2e): harden triaged failures

* test(e2e): ship relay bundle to reusable shards

* test(e2e): tolerate expected IPC closures in daemon shutdown

A normal client exit can close the IPC channel before the finish ack
lands. Distinguish this from real failures by checking error codes,
only throwing if forced cleanup occurred or the error is not an IPC
closure.

* rm doc

* test(e2e): return termination status from legacy close handler

- terminateLegacyCloseClient now returns a discriminated union indicating
  whether the process had already exited ('already-exited') or termination
  was actually attempted ('termination-attempted')
- Allows finishLegacyCloseClient to only set forcedCleanup when termination
  was genuinely needed, not when the process exited cleanly on its own

* test(e2e): fix dispatch contract and voice mic locator

Point the release E2E contract at the renamed build step, and assert the
relabeled microphone through the Voice pane combobox even when Radix
leaves the listbox open.

* test(e2e): add contract test for relay artifact dispatch

Validate that the relay artifact built in CI is properly uploaded,
downloaded, and passed via ORCA_RELAY_PATH to E2E test runs.

* Distinguish between terminated and already-exited processes

Detect when processes have already exited instead of always reporting
termination success. Return booleans from cleanup functions to indicate
whether they actually signalled a process, catch tree-capture failures
when the root process exits before recording completes, and use these
signals to return accurate exit status from termination handlers.

* test(e2e): stabilize file creation and voice microphone tests

Use stable locators (aria-autocomplete, named triggers) and add retry
logic to handle file scans and device events that can interfere with
listbox state. Increase timeouts to allow async operations to complete.

* Add retry logic for transient GitHub API errors in PR body updates

GitHub API occasionally returns transient 5xx errors. Retry up to 3 times
with exponential backoff (1s, 2s, 4s) to improve reliability during
temporary service disruptions. Export updatePullRequest and add sleepImpl
parameter for test injection.

* Add tab search result retention during typing

Keep search results on screen while the deferred query catches up with
the live query. Re-validates results against the current input without
dropping rows prematurely, ensuring the user can select from what they see.

* Add proper types to tab search mock

Replace `unknown` with concrete types (`OpenTabSearchResult`,
`OpenTabSearchEntries`, `SearchableWorkspaceTab`) and use type guards
for discriminated unions to improve test type safety.
2026-08-17 23:28:38 -07:00

186 lines
6.4 KiB
TypeScript

import { execFile } from 'node:child_process'
import { promisify } from 'node:util'
const execFileAsync = promisify(execFile)
const PROCESS_QUERY_TIMEOUT_MS = 5_000
const PROCESS_QUERY_MAX_BYTES = 8 * 1024 * 1024
const processQueryOptions = {
encoding: 'utf8' as const,
timeout: PROCESS_QUERY_TIMEOUT_MS,
maxBuffer: PROCESS_QUERY_MAX_BYTES,
windowsHide: true
}
export type RecordedProcessIdentity = {
pid: number
startedAtMs: number
}
type ProcessRow = RecordedProcessIdentity & {
parentPid: number
}
export async function waitForCondition(
description: string,
predicate: () => boolean | Promise<boolean>,
timeoutMs = 10_000
): Promise<void> {
const deadline = Date.now() + timeoutMs
while (Date.now() <= deadline) {
if (await predicate()) {
return
}
await new Promise<void>((resolve) => setTimeout(resolve, 25))
}
throw new Error(`Timed out waiting for ${description}`)
}
async function readWindowsProcessRows(): Promise<ProcessRow[]> {
const command = [
'[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false);',
'$rows = Get-CimInstance Win32_Process | ForEach-Object {',
' [PSCustomObject]@{',
' pid = [int]$_.ProcessId;',
' parentPid = [int]$_.ParentProcessId;',
" startedAt = if ($null -eq $_.CreationDate) { $null } else { $_.CreationDate.ToUniversalTime().ToString('O', [System.Globalization.CultureInfo]::InvariantCulture) }",
' }',
'};',
'$rows | ConvertTo-Json -Compress'
].join(' ')
const { stdout } = await execFileAsync(
'powershell.exe',
['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', command],
processQueryOptions
)
const parsed = JSON.parse(stdout || '[]') as
| { pid?: unknown; parentPid?: unknown; startedAt?: unknown }
| { pid?: unknown; parentPid?: unknown; startedAt?: unknown }[]
const rows = Array.isArray(parsed) ? parsed : [parsed]
return rows.flatMap((row) => {
const pid = Number(row.pid)
const parentPid = Number(row.parentPid)
const startedAtMs = typeof row.startedAt === 'string' ? Date.parse(row.startedAt) : Number.NaN
return Number.isInteger(pid) && Number.isInteger(parentPid) && Number.isFinite(startedAtMs)
? [{ pid, parentPid, startedAtMs }]
: []
})
}
async function readPosixProcessRows(): Promise<ProcessRow[]> {
const { stdout } = await execFileAsync('ps', ['-axo', 'pid=,ppid=,lstart='], {
...processQueryOptions,
// Why: start identity must not depend on a CI host's locale or timezone.
env: { ...process.env, LANG: 'C', LC_ALL: 'C', TZ: 'UTC0' }
})
return stdout.split('\n').flatMap((line) => {
const match = /^\s*(\d+)\s+(\d+)\s+(.+?)\s*$/.exec(line)
if (!match) {
return []
}
const startedAtMs = Date.parse(`${match[3]} UTC`)
return Number.isFinite(startedAtMs)
? [{ pid: Number(match[1]), parentPid: Number(match[2]), startedAtMs }]
: []
})
}
async function readProcessRows(): Promise<ProcessRow[]> {
return process.platform === 'win32' ? readWindowsProcessRows() : readPosixProcessRows()
}
export async function recordProcessIdentity(pid: number): Promise<RecordedProcessIdentity> {
if (!Number.isInteger(pid) || pid <= 0) {
throw new Error(`Cannot record invalid fixture pid ${pid}`)
}
const row = (await readProcessRows()).find((candidate) => candidate.pid === pid)
if (!row) {
throw new Error(`Could not record process-start identity for fixture pid ${pid}`)
}
return { pid, startedAtMs: row.startedAtMs }
}
export async function processIdentityIsAlive(identity: RecordedProcessIdentity): Promise<boolean> {
const current = (await readProcessRows()).find((row) => row.pid === identity.pid)
return current !== undefined && current.startedAtMs === identity.startedAtMs
}
export async function processIdentityLiveness(
identities: readonly RecordedProcessIdentity[]
): Promise<Map<number, boolean>> {
const rowsByPid = new Map((await readProcessRows()).map((row) => [row.pid, row]))
return new Map(
identities.map((identity) => {
const current = rowsByPid.get(identity.pid)
return [identity.pid, current !== undefined && current.startedAtMs === identity.startedAtMs]
})
)
}
export async function recordProcessTree(
root: RecordedProcessIdentity
): Promise<RecordedProcessIdentity[]> {
const rows = await readProcessRows()
const currentRoot = rows.find((row) => row.pid === root.pid)
if (!currentRoot || currentRoot.startedAtMs !== root.startedAtMs) {
throw new Error(`Fixture root pid ${root.pid} changed incarnation before tree capture`)
}
const childrenByParent = new Map<number, ProcessRow[]>()
for (const row of rows) {
const children = childrenByParent.get(row.parentPid) ?? []
children.push(row)
childrenByParent.set(row.parentPid, children)
}
const recorded: RecordedProcessIdentity[] = [root]
const pending = [...(childrenByParent.get(root.pid) ?? [])]
while (pending.length > 0) {
const row = pending.pop()
if (!row) {
continue
}
recorded.push({ pid: row.pid, startedAtMs: row.startedAtMs })
pending.push(...(childrenByParent.get(row.pid) ?? []))
}
return recorded
}
async function terminateRecordedProcess(identity: RecordedProcessIdentity): Promise<boolean> {
try {
if (process.platform === 'win32') {
await execFileAsync('taskkill', ['/pid', String(identity.pid), '/f'], processQueryOptions)
} else {
process.kill(identity.pid, 'SIGKILL')
}
return true
} catch {
// The exact process incarnation may exit between validation and signalling.
return false
}
}
/** Resolves to whether cleanup actually signalled a recorded process. */
export async function terminateRecordedTree(
identities: RecordedProcessIdentity[]
): Promise<boolean> {
const unique = [...new Map(identities.map((identity) => [identity.pid, identity])).values()]
let signalled = false
for (const identity of unique.toReversed()) {
// Why: PID reuse between tree capture and cleanup must never authorize a
// signal to a process incarnation the fixture did not create.
if (!(await processIdentityIsAlive(identity))) {
continue
}
signalled = (await terminateRecordedProcess(identity)) || signalled
}
await waitForCondition(
'recorded fixture process tree to be absent',
async () => {
const liveness = await processIdentityLiveness(unique)
return unique.every((identity) => !liveness.get(identity.pid))
},
5_000
)
return signalled
}