Files
orca/tests/e2e/terminal-hangul-terminating-digit-native.spec.ts
OrcaWinandm4air f5ece0f912 test(e2e): retry Hangul-digit attempts the input method never delivered (#26566)
* test(e2e): retry Hangul-digit attempts the IME itself never delivered

The nested Wayland lane failed ~1 in 6 repetitions on main because mutter+IBus
occasionally discarded the preedit or the digit keydown before Chromium saw it.
Each attempt now checks what the page actually received; only those upstream
drops are retried, and any line Orca loses after the page received it still fails.

* test(e2e): keep the engagement receipt call the gate contract pins

---------

Co-authored-by: m4air <m4air@Mac.localdomain>
2026-10-08 05:32:06 -07:00

306 lines
13 KiB
TypeScript

/**
* Drives a real Hangul input method through a real compositor and asserts the bytes that reach
* the pty. Written to reproduce #15299, where a digit typed straight after a Hangul syllable was
* dropped under Wayland but not under X11.
*
* CI runs the default xdotool injector under X11, checking exact Hangul-plus-digit PTY bytes.
* That path passed even before the Wayland fix; it does not prove #15299 is fixed.
* Reproducing #15299 still requires the nested Wayland session below.
*
* Run the Terminal IME E2E workflow on the candidate branch for hosted native validation.
* Its runner owns the isolated Xvfb display, nested compositor and IBus session.
*
* Nested Wayland prerequisites:
*
* - Nested, not headless. A headless mutter never answers RemoteDesktop.CreateSession, so there
* is no way to inject input; nested makes the whole compositor an X window that xdotool can
* type into. ydotool does not help either - headless has no evdev seat.
* - Pick an unused display. Attaching to a stale Xvfb silently hands the run someone else's
* session, and a stale ibus-daemon wins the XIM selection over the new one.
* - The session script must not exit: ibus-daemon dies with its parent.
* - The owned window starts hidden, then the guarded native fixture presents it on hosted CI's
* isolated display so the compositor can deliver real input.
* - Send Escape before the byte reader starts, or GNOME's overview keeps focus and the Escape
* bytes corrupt the first line.
* - Check the session's ibus-daemon is not running --panel=disable before trusting anything that
* depends on seeing a candidate window. No panel means no lookup table is ever drawn, so a
* candidate-selection run measures nothing and reads as "the IME ignored the key".
* - Launch the browser with --password-store=basic --use-mock-keychain. A gnome-keyring unlock
* dialog or a Chrome update bubble grabs the keyboard, xdotool keys never reach the page, and
* the empty event log looks exactly like a broken IME.
* - Click the input to focus it. windowactivate alone does not give web contents real focus.
*
* The keys and expected text are environment-tunable, so other IME issues can reuse this
* unchanged rather than writing another one.
*/
import { execFileSync } from 'node:child_process'
import { randomUUID } from 'node:crypto'
import { mkdirSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import type { Page, TestInfo } from '@stablyai/playwright-test'
import { test, expect } from './helpers/orca-app'
import { presentNativeIbusWindow } from './terminal-native-ibus-window'
import { appendImeEngagementReceipt } from './terminal-ime-engagement-receipt'
import { installTerminalWaylandInputDiagnostics } from './terminal-wayland-input-diagnostics'
import { ensureTerminalVisible, waitForActiveWorktree, waitForSessionReady } from './helpers/store'
import {
focusActiveTerminalInput,
sendToTerminal,
waitForActivePanePtyId,
waitForActiveTerminalManager
} from './helpers/terminal'
import {
disposeTerminalImeBoundaryProbe,
installTerminalImeBoundaryProbe,
readTerminalImeBoundaryTrace,
type TerminalImeBoundaryTrace
} from './terminal-ime-boundary-probe'
import { readImeDomDeliveredText } from './terminal-ime-dom-delivery'
import {
createTerminalImeByteReader,
removeTerminalImeByteReader,
startTerminalImeByteReader,
waitForTerminalImeBytes
} from './terminal-ime-byte-reader'
const NATIVE_COMMAND_TIMEOUT_MS = 10_000
const REPETITIONS = Number(process.env.ORCA_E2E_DIGIT_REPETITIONS ?? 3)
// Why: nested mutter + IBus occasionally discards a preedit or a keydown before Chromium sees it;
// those attempts are retried, bounded so a broken IME session still fails.
const MAX_ATTEMPTS = REPETITIONS * 2
const INJECTOR = process.env.ORCA_E2E_IME_INJECTOR ?? 'xdotool'
const WAYLAND_INJECT = process.env.ORCA_E2E_WAYLAND_INJECT ?? '/tmp/ime15299/wayland-inject.py'
// The nested compositor is one X11 window; keys land on it and mutter routes
// them to the focused Wayland client through the IME.
const NESTED_FOCUS_CMD = process.env.ORCA_E2E_NESTED_FOCUS_CMD ?? '/tmp/ime15299/focus-nested.sh'
// Dubeolsik: d=ㅇ, k=ㅏ, so `d k` composes 아 and the digit terminates it.
const KEY_TOKENS = (process.env.ORCA_E2E_DIGIT_KEYS ?? 'd k 1 Return').split(' ').filter(Boolean)
const EXPECTED_LINE = process.env.ORCA_E2E_DIGIT_EXPECTED ?? '아1'
test.use({
orcaAppExtraEnv: {
GTK_IM_MODULE: 'ibus',
IBUS_ENABLE_SYNC_MODE: '1',
QT_IM_MODULE: 'ibus',
XMODIFIERS: '@im=ibus',
...(process.env.ORCA_E2E_WAYLAND_INPUT_DIAGNOSTICS === '1' ? { WAYLAND_DEBUG: 'client' } : {}),
...(process.env.ORCA_E2E_EXTRA_APP_ENV
? (JSON.parse(process.env.ORCA_E2E_EXTRA_APP_ENV) as Record<string, string>)
: {})
},
orcaAppExtraArgs: (process.env.ORCA_E2E_EXTRA_APP_ARGS ?? '').split(' ').filter(Boolean)
})
function injectKeys(tokens: string[]): void {
if (INJECTOR === 'wayland') {
execFileSync('python3', [WAYLAND_INJECT, ...tokens], {
stdio: 'pipe',
timeout: NATIVE_COMMAND_TIMEOUT_MS
})
return
}
for (const token of tokens) {
execFileSync('xdotool', ['key', '--clearmodifiers', token], {
stdio: 'pipe',
timeout: NATIVE_COMMAND_TIMEOUT_MS
})
}
}
/** GNOME Shell parks keyboard focus in the overview until a window is up, so
* Escape has to run before the byte reader starts or those Escapes land in it. */
function leaveNestedOverview(): void {
execFileSync(NESTED_FOCUS_CMD, [], { stdio: 'pipe', timeout: NATIVE_COMMAND_TIMEOUT_MS })
for (let index = 0; index < 2; index += 1) {
execFileSync('xdotool', ['key', 'Escape'], {
stdio: 'pipe',
timeout: NATIVE_COMMAND_TIMEOUT_MS
})
}
}
function captureNestedScreen(name: string): void {
const dir = path.join(process.cwd(), 'test-results', 'terminal-ime-evidence')
mkdirSync(dir, { recursive: true })
execFileSync('import', ['-window', 'root', path.join(dir, `${name}.png`)], {
stdio: 'pipe',
timeout: NATIVE_COMMAND_TIMEOUT_MS
})
}
async function focusNativeTerminalWindow(page: Page): Promise<void> {
await focusActiveTerminalInput(page)
const title = `ORCA_NATIVE_IBUS_${randomUUID()}`
await page.evaluate((nextTitle) => {
document.title = nextTitle
}, title)
await expect.poll(() => page.title(), { timeout: 5_000 }).toBe(title)
if (INJECTOR === 'nested') {
execFileSync(NESTED_FOCUS_CMD, [], {
stdio: 'pipe',
timeout: NATIVE_COMMAND_TIMEOUT_MS
})
} else if (INJECTOR !== 'wayland') {
execFileSync('xdotool', ['search', '--onlyvisible', '--name', title, 'windowfocus', '--sync'], {
stdio: 'pipe',
timeout: NATIVE_COMMAND_TIMEOUT_MS
})
}
execFileSync('ibus', ['engine', 'hangul'], {
stdio: 'pipe',
timeout: NATIVE_COMMAND_TIMEOUT_MS
})
const engine = execFileSync('ibus', ['engine'], {
encoding: 'utf8',
timeout: NATIVE_COMMAND_TIMEOUT_MS
}).trim()
expect(engine).toBe('hangul')
}
async function writeEvidence(
page: Page,
testInfo: TestInfo,
name: string,
extra: Record<string, unknown>
): Promise<void> {
const trace = await readTerminalImeBoundaryTrace(page)
const payload = { ...extra, injector: INJECTOR, keyTokens: KEY_TOKENS, trace }
const body = `${JSON.stringify(payload, null, 2)}\n`
await testInfo.attach(`${name}.json`, { body, contentType: 'application/json' })
const dir = path.join(process.cwd(), 'test-results', 'terminal-ime-evidence')
mkdirSync(dir, { recursive: true })
writeFileSync(path.join(dir, `${name}.json`), body)
}
test.describe('Hangul terminating digit @headful', () => {
test.skip(process.env.ORCA_E2E_NATIVE_IBUS_HANGUL !== '1', 'native ibus harness only')
test('a digit typed right after a Hangul syllable reaches the pty', async ({
electronApp,
orcaPage: page,
testRepoPath
}, testInfo) => {
const diagnostics =
process.env.ORCA_E2E_WAYLAND_INPUT_DIAGNOSTICS === '1'
? await installTerminalWaylandInputDiagnostics(electronApp, page)
: null
let completed = false
let diagnosticError: unknown
try {
const launchDiagnostics = await electronApp.evaluate(({ app: electron, BrowserWindow }) => ({
waylandDisplay: process.env.WAYLAND_DISPLAY ?? null,
display: process.env.DISPLAY ?? null,
xdgRuntimeDir: process.env.XDG_RUNTIME_DIR ?? null,
ozonePlatform: electron.commandLine.getSwitchValue('ozone-platform'),
disableGpu: electron.commandLine.hasSwitch('disable-gpu'),
background: process.env.ORCA_BACKGROUND_LAUNCH,
nativeIsolatedDisplay: process.env.ORCA_E2E_NATIVE_IBUS_XVFB,
windows: BrowserWindow.getAllWindows().map((window) => ({
visible: window.isVisible(),
minimized: window.isMinimized(),
bounds: window.getBounds()
}))
}))
console.log(`[digit-diag] ${JSON.stringify(launchDiagnostics)}`)
await presentNativeIbusWindow(electronApp, page)
if (INJECTOR === 'nested') {
expect(launchDiagnostics.ozonePlatform).toBe('wayland')
expect(launchDiagnostics.waylandDisplay).toBeTruthy()
await page.waitForTimeout(2_000)
}
await waitForSessionReady(page)
await waitForActiveWorktree(page)
await ensureTerminalVisible(page)
await waitForActiveTerminalManager(page, 30_000)
const ptyId = await waitForActivePanePtyId(page)
const reader = createTerminalImeByteReader(testRepoPath, MAX_ATTEMPTS)
let receivedBytes: string[] = []
const attempts: {
line: string | null
domDelivered: string
trace: TerminalImeBoundaryTrace
}[] = []
const expectedHex = Buffer.from(`${EXPECTED_LINE}\n`).toString('hex')
try {
if (INJECTOR === 'nested') {
leaveNestedOverview()
await page.waitForTimeout(1_500)
captureNestedScreen('nested-before-reader')
}
await startTerminalImeByteReader(page, ptyId, reader)
await diagnostics?.attachTerminal()
await focusNativeTerminalWindow(page)
if (INJECTOR === 'nested') {
captureNestedScreen('nested-before-typing')
}
const expectedLine = `${EXPECTED_LINE}\n`
let accepted = 0
let trace: TerminalImeBoundaryTrace = { dom: [], onData: [] }
for (let attempt = 0; attempt < MAX_ATTEMPTS && accepted < REPETITIONS; attempt += 1) {
await installTerminalImeBoundaryProbe(page)
injectKeys(KEY_TOKENS)
const lineIndex = receivedBytes.length
receivedBytes = await waitForTerminalImeBytes(page, reader, 10_000, lineIndex + 1).catch(
() => receivedBytes
)
const attemptTrace = await readTerminalImeBoundaryTrace(page)
const domDelivered = readImeDomDeliveredText(attemptTrace)
const lineHex = receivedBytes[lineIndex]
const line = lineHex === undefined ? null : Buffer.from(lineHex, 'hex').toString('utf8')
attempts.push({ line, domDelivered, trace: attemptTrace })
if (line === expectedLine) {
accepted += 1
trace = attemptTrace
continue
}
// Why: whenever the page received every key the pty line must match; only input the
// IME/compositor itself never delivered (a discarded preedit, a lost keydown) is retried.
expect(
domDelivered,
`Orca lost input the page received: ${JSON.stringify(line)}`
).not.toBe(expectedLine)
if (line === null) {
// Why: Ctrl-U drops the partial line a lost Enter leaves in the reader's tty buffer.
await sendToTerminal(page, ptyId, '\x15')
}
}
if (INJECTOR === 'nested') {
captureNestedScreen('nested-after-typing')
}
const summary = attempts.map(({ line, domDelivered }) => ({ line, domDelivered }))
expect(accepted, `attempts: ${JSON.stringify(summary)}`).toBe(REPETITIONS)
appendImeEngagementReceipt(testInfo.title, trace)
completed = true
} finally {
await writeEvidence(page, testInfo, 'hangul-terminating-digit', {
expectedHex,
expectedLine: EXPECTED_LINE,
receivedBytes,
decoded: receivedBytes.map((hex) => Buffer.from(hex, 'hex').toString('utf8')),
attempts
}).catch(() => undefined)
await disposeTerminalImeBoundaryProbe(page).catch(() => undefined)
await sendToTerminal(page, ptyId, '\x03').catch(() => undefined)
removeTerminalImeByteReader(reader)
}
} finally {
if (diagnostics) {
try {
await diagnostics.write(testInfo)
} catch (error) {
diagnosticError = error
console.error('Could not retain passive Wayland diagnostics', error)
} finally {
await diagnostics
.dispose()
.catch((error) => console.error('Could not dispose passive Wayland diagnostics', error))
}
}
}
if (completed && diagnosticError) {
throw diagnosticError
}
})
})