mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-12-03 15:42:16 +00:00
test(tui): extract cursor-layout + fast-echo helpers for real unit tests
textInputCursorSourceOfTruth.test.ts read textInput.tsx as text and
regexed it to check that cursorLayout() is called with curRef.current
(not the stale cur React state), and that the fast-echo backspace/append
stdout writes are paired with noteCursorAdvance calls.
Extract three pure functions from textInput.tsx:
- resolveCursorLayout(display, cur, curRefCurrent, columns): wraps
cursorLayout(display, curRefCurrent, columns), making the
curRef.current-over-cur choice a directly testable pure call instead of
a regex match on the render-site call expression.
- fastBackspaceEffect(current, cursor) / fastAppendEffect(current, cursor,
text): return a single object bundling {newValue, newCursor, write,
advanceDelta} for each fast-echo path. Bundling the stdout write and the
noteCursorAdvance delta into one return value makes the pairing
impossible to silently drift apart (a caller can't get without
), instead of relying on the two call sites appearing near
each other in source text.
textInput.tsx's render site and backspace/append handlers now call these
helpers directly, preserving exact existing behavior (the same '\b \b'
write sequence, noteCursorAdvance(-1)/noteCursorAdvance(text.length) calls).
textInputCursorSourceOfTruth.test.ts imports and calls the three pure
functions directly with a deliberately stale cur vs a fresh curRefCurrent
to reconstruct the exact regression scenario, and asserts the bundled
effect objects -- no readFileSync, no regex against textInput.tsx's
source text. Full ui-tui suite (107 files, 1117 tests) still green.
This commit is contained in:
parent
bffc098d09
commit
d6c76cfbfa
2 changed files with 167 additions and 39 deletions
|
|
@ -1,13 +1,7 @@
|
|||
import { readFileSync } from 'node:fs'
|
||||
import { dirname, join } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
// Locate textInput.tsx relative to this test file so the assertion
|
||||
// survives moves of the test fixture itself.
|
||||
const TEXT_INPUT_PATH = join(dirname(fileURLToPath(import.meta.url)), '..', 'components', 'textInput.tsx')
|
||||
const source = readFileSync(TEXT_INPUT_PATH, 'utf8')
|
||||
import { cursorLayout } from '../lib/inputMetrics.js'
|
||||
import { fastAppendEffect, fastBackspaceEffect, resolveCursorLayout } from '../components/textInput.js'
|
||||
|
||||
// Closes Copilot follow-up on PR #26717: the original cursor-drift
|
||||
// fix bumped Ink's displayCursor / cursorDeclaration on fast-echo, but
|
||||
|
|
@ -18,33 +12,89 @@ const source = readFileSync(TEXT_INPUT_PATH, 'utf8')
|
|||
// bump. The fix is structural: read `curRef.current` (always
|
||||
// up-to-date) when computing the layout, not the `cur` state.
|
||||
//
|
||||
// This file pins that invariant. Switching back to `cur` state — or
|
||||
// re-introducing a memo keyed on `cur` that uses `curRef.current`
|
||||
// inside but stops re-computing on rerender — is a regression and
|
||||
// should be caught here, not via a flaky integration test that mounts
|
||||
// Ink + stdin.
|
||||
describe('textInput cursor-layout source of truth', () => {
|
||||
it('reads curRef.current (not the cur React state) for cursorLayout', () => {
|
||||
// The line we care about. We allow whitespace / formatting drift,
|
||||
// but the call itself must use `curRef.current`.
|
||||
expect(source).toMatch(/cursorLayout\(\s*display\s*,\s*curRef\.current\s*,\s*columns\s*\)/)
|
||||
// These tests exercise the real, exported `resolveCursorLayout`,
|
||||
// `fastBackspaceEffect`, and `fastAppendEffect` helpers that
|
||||
// `textInput.tsx` calls at its render site and fast-echo call sites —
|
||||
// no source-text regex, no readFileSync.
|
||||
describe('resolveCursorLayout', () => {
|
||||
it('uses curRefCurrent (the fresh ref value), not the stale cur state', () => {
|
||||
// Simulate the exact bug scenario: `cur` (React state) is stale —
|
||||
// it still reflects the value before a fast-echo append — while
|
||||
// `curRef.current` has already advanced past it.
|
||||
const display = 'hello world'
|
||||
const staleCur = 5
|
||||
const freshCurRefCurrent = 11
|
||||
const columns = 80
|
||||
|
||||
const result = resolveCursorLayout(display, staleCur, freshCurRefCurrent, columns)
|
||||
const expected = cursorLayout(display, freshCurRefCurrent, columns)
|
||||
|
||||
expect(result).toEqual(expected)
|
||||
})
|
||||
|
||||
it('does not pass the bare `cur` React state into cursorLayout', () => {
|
||||
// Any `cursorLayout(display, cur, columns)` invocation would
|
||||
// reintroduce the stale-declaration window.
|
||||
expect(source).not.toMatch(/cursorLayout\(\s*display\s*,\s*cur\s*,\s*columns\s*\)/)
|
||||
it('does not match the layout computed from the stale cur value', () => {
|
||||
const display = 'hello world'
|
||||
const staleCur = 5
|
||||
const freshCurRefCurrent = 11
|
||||
const columns = 80
|
||||
|
||||
const result = resolveCursorLayout(display, staleCur, freshCurRefCurrent, columns)
|
||||
const staleLayout = cursorLayout(display, staleCur, columns)
|
||||
|
||||
expect(result).not.toEqual(staleLayout)
|
||||
})
|
||||
|
||||
it('keeps the fast-echo notifier calls paired with the stdout writes', () => {
|
||||
// Both fast-echo paths must call noteCursorAdvance, otherwise Ink
|
||||
// never learns about the out-of-band write and drifts again. We
|
||||
// tolerate explanatory comments in between (the rationale block is
|
||||
// intentionally long), but the pairing itself must hold.
|
||||
const backspacePattern = /stdout!\.write\(['"`]\\b \\b['"`]\)[\s\S]{0,1000}?noteCursorAdvance\(-1\)/
|
||||
expect(source).toMatch(backspacePattern)
|
||||
it('matches cursorLayout(display, curRefCurrent, columns) even when cur and curRefCurrent agree', () => {
|
||||
const display = 'hello'
|
||||
const cur = 5
|
||||
const columns = 80
|
||||
|
||||
const appendPattern = /stdout!\.write\(text\)[\s\S]{0,1000}?noteCursorAdvance\(text\.length\)/
|
||||
expect(source).toMatch(appendPattern)
|
||||
expect(resolveCursorLayout(display, cur, cur, columns)).toEqual(cursorLayout(display, cur, columns))
|
||||
})
|
||||
})
|
||||
|
||||
describe('fastBackspaceEffect', () => {
|
||||
it('removes the last character, moves the cursor back one, and pairs the write with the advance delta', () => {
|
||||
const effect = fastBackspaceEffect('hello', 5)
|
||||
|
||||
expect(effect.newValue).toBe('hell')
|
||||
expect(effect.newCursor).toBe(4)
|
||||
expect(effect.removed).toBe('o')
|
||||
// Both the stdout write and the noteCursorAdvance delta live on the
|
||||
// same returned object — a caller cannot apply `write` without also
|
||||
// having `advanceDelta` in hand, so the pairing can't silently drift.
|
||||
expect(effect.write).toBe('\b \b')
|
||||
expect(effect.advanceDelta).toBe(-1)
|
||||
})
|
||||
|
||||
it('handles deleting from the middle of the fast-echo-eligible tail', () => {
|
||||
const effect = fastBackspaceEffect('abc', 3)
|
||||
|
||||
expect(effect.newValue).toBe('ab')
|
||||
expect(effect.newCursor).toBe(2)
|
||||
expect(effect.removed).toBe('c')
|
||||
expect(effect.write).toBe('\b \b')
|
||||
expect(effect.advanceDelta).toBe(-1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('fastAppendEffect', () => {
|
||||
it('appends the text, advances the cursor by the inserted length, and pairs the write with the advance delta', () => {
|
||||
const effect = fastAppendEffect('hello', 5, ' world')
|
||||
|
||||
expect(effect.newValue).toBe('hello world')
|
||||
expect(effect.newCursor).toBe(11)
|
||||
// The stdout write is exactly the inserted text, and the
|
||||
// noteCursorAdvance delta is bundled into the same object.
|
||||
expect(effect.write).toBe(' world')
|
||||
expect(effect.advanceDelta).toBe(' world'.length)
|
||||
})
|
||||
|
||||
it('advance delta always matches the inserted text length, not a hardcoded value', () => {
|
||||
const effect = fastAppendEffect('x', 1, 'abc')
|
||||
|
||||
expect(effect.newValue).toBe('xabc')
|
||||
expect(effect.advanceDelta).toBe(3)
|
||||
expect(effect.write).toBe('abc')
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue