Skip to content

The agent that grows with you

fix(tui): tighten IME test per teknium1 review (#55415)

hanhvse2f7850ec389
diff --git a/ui-tui/src/__tests__/imeVietnameseTelex.test.tsx b/ui-tui/src/__tests__/imeVietnameseTelex.test.tsx
index 74499f639f6..59d2be77825 100644
--- a/ui-tui/src/__tests__/imeVietnameseTelex.test.tsx
+++ b/ui-tui/src/__tests__/imeVietnameseTelex.test.tsx
@@ -2,7 +2,7 @@ import { EventEmitter } from 'events'
import { renderSync } from '@hermes/ink'
import React, { useState } from 'react'
-import { describe, expect, it } from 'vitest'
+import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
import { TextInput } from '../components/textInput.js'
@@ -41,19 +41,16 @@ class FakeTty extends EventEmitter {
}
setRawMode(mode: boolean): this {
this.isRaw = mode
-
return this
}
write(chunk: string | Uint8Array, cb?: (err?: Error | null) => void): boolean {
this.chunks.push(typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString('utf8'))
cb?.()
-
return true
}
}
const tick = () => new Promise<void>(resolve => setImmediate(resolve))
-const wait = (ms: number) => new Promise<void>(resolve => setTimeout(resolve, ms))
function Harness({ initial = '', onValue }: { initial?: string; onValue: (value: string) => void }) {
const [value, setValue] = useState(initial)
@@ -67,7 +64,14 @@ function Harness({ initial = '', onValue }: { initial?: string; onValue: (value:
})
}
-async function drive(reads: string[], { initial = '', gapMs = 0 }: { initial?: string; gapMs?: number } = {}): Promise<string> {
+// Core driver: feeds reads, optionally advancing fake timers between reads to
+// simulate the small macrotask gaps real IME reads arrive with. Returns the
+// final value seen by the parent immediately after the last read (no trailing
+// wait) so a passing assertion proves the commit was synchronous, not deferred.
+async function drive(
+ reads: string[],
+ { initial = '', gapMs = 0 }: { initial?: string; gapMs?: number } = {}
+): Promise<string> {
const stdout = new FakeTty()
const stdin = new FakeTty()
const stderr = new FakeTty()
@@ -88,11 +92,18 @@ async function drive(reads: string[], { initial = '', gapMs = 0 }: { initial?: s
await tick()
if (gapMs) {
- await wait(gapMs)
+ // Advance the fake clock to flush any pending FRAME_BATCH_MS timers
+ // between reads (mirrors the real macrotask gap), then let microtasks run.
+ vi.advanceTimersByTime(gapMs)
+ await tick()
}
}
- await wait(60)
+ // Assert IMMEDIATELY after the final read — no trailing 60ms wait and
+ // WITHOUT advancing the fake clock past the deferred key-burst window.
+ // If the value is already correct here, the multi-char insert committed
+ // synchronously; the old deferred path (scheduleKeyBurstCommit, 16ms)
+ // has NOT flushed yet, so a stale/dropped tail would still be visible.
return values.at(-1) ?? ''
} finally {
@@ -104,6 +115,15 @@ async function drive(reads: string[], { initial = '', gapMs = 0 }: { initial?: s
const NNBSP = '\u202f'
describe('Vietnamese Telex IME recomposition', () => {
+ beforeEach(() => {
+ // Only fake setTimeout/setInterval/Date — NOT setImmediate (used by tick()).
+ vi.useFakeTimers({ toFake: ['setTimeout', 'setInterval', 'Date'] })
+ })
+
+ afterEach(() => {
+ vi.useRealTimers()
+ })
+
it('applies a parser-split backspace plus composed character through useInput', async () => {
// OpenKey fuses the erase + recomposed glyph into a single stdin read.
expect(await drive(['\x7fô'], { initial: 'o' })).toBe('ô')
@@ -115,6 +135,7 @@ describe('Vietnamese Telex IME recomposition', () => {
// already delivered the final value (the deferred path dropped "nh" here).
const reads = ['h', 'a', 'n', 'h', NNBSP, '\x7f\x7f', '\x7f\x7f', '\u1EA1nh']
+ // No gapMs, no advanceTimersMs — we assert BEFORE the 16ms FRAME_BATCH_MS could fire.
expect(await drive(reads)).toBe('h\u1EA1nh')
})
@@ -139,3 +160,80 @@ describe('Vietnamese Telex IME recomposition', () => {
expect(await drive(reads)).toBe('h\u1EA1nh')
})
})
+
+describe('Fast-echo suppression reset (60ms window)', () => {
+ beforeEach(() => {
+ // Only fake setTimeout/setInterval/Date — NOT setImmediate (used by tick()).
+ vi.useFakeTimers({ toFake: ['setTimeout', 'setInterval', 'Date'] })
+ })
+
+ afterEach(() => {
+ vi.useRealTimers()
+ })
+
+ it('suppresses fast-echo backspace for one keystroke after an Ink repaint (IME recompose)', async () => {
+ // Simulate: user types "ha" -> Ink commits normally -> then IME recompose arrives
+ // as NNBSP + backspaces + recomposed text. The first backspace after the Ink
+ // repaint must NOT fast-echo (would strand the NNBSP marker as a stray space).
+
+ // Type "ha" normally (each char goes through fast-echo append path)
+ let reads = ['h', 'a']
+ const stdout1 = new FakeTty()
+ const stdin1 = new FakeTty()
+ const stderr1 = new FakeTty()
+ const values1: string[] = []
+
+ const instance1 = renderSync(React.createElement(Harness, { initial: '', onValue: v => values1.push(v) }), {
+ patchConsole: false,
+ stderr: stderr1 as unknown as NodeJS.WriteStream,
+ stdin: stdin1 as unknown as NodeJS.ReadStream,
+ stdout: stdout1 as unknown as NodeJS.WriteStream
+ })
+
+ try {
+ await tick()
+ for (const r of reads) {
+ stdin1.send(r)
+ await tick()
+ }
+ // After "ha", fast-echo is enabled (inkRepaintedRef.current = false)
+ expect(values1.at(-1)).toBe('ha')
+
+ // Now simulate an IME recompose burst that forces an Ink repaint:
+ // NNBSP marker forces a full Ink render (syncParent=true in commit).
+ // The next backspace should be SUPPRESSED (fast-echo backspace disabled).
+ stdin1.send(NNBSP + '\x7f\x7f\u1EA1nh') // fused chunk: marker + 2x backspace + "ạnh"
+ await tick()
+
+ // The recomposed value must be committed synchronously (no dropped tail).
+ // The first backspace after the Ink repaint must NOT have written "\b \b" to stdout.
+ // We can't directly inspect stdout here, but we verify the FINAL value is correct.
+ expect(values1.at(-1)).toBe('h\u1EA1nh')
+
+ // Advance fake timers past the 60ms suppression window so the
+ // inkRepaintResetTimer fires and re-enables fast-echo backspace.
+ vi.advanceTimersByTime(60)
+ await tick()
+
+ // Now fast-echo backspace is RE-ENABLED. One backspace deletes exactly
+ // one grapheme ("h") off the end of "hạnh" -> "hạn".
+ stdin1.send('\x7f')
+ await tick()
+
+ expect(values1.at(-1)).toBe('h\u1EA1n')
+ } finally {
+ instance1.unmount()
+ instance1.cleanup()
+ }
+ })
+
+ it('does NOT suppress fast-echo backspace when no Ink repaint occurred (normal typing)', async () => {
+ // Normal ASCII typing never triggers the Ink-repaint suppression.
+ const reads = ['h', 'e', 'l', 'l', 'o']
+ expect(await drive(reads)).toBe('hello')
+
+ // Two backspaces off "hello" -> "hel" via the fast-echo path.
+ const reads2 = [...reads, '\x7f', '\x7f']
+ expect(await drive(reads2)).toBe('hel')
+ })
+})
\ No newline at end of file