The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
import { describe, expect, it, vi } from 'vitest'
import { getOverlayState, patchOverlayState, resetOverlayState } from '../app/overlayStore.js'
import { rememberServerRequest, resetServerRequestsForTests } from '../app/serverRequestStore.js'
import {
applyVoiceRecordResponse,
composerHasDraft,
dismissSensitivePrompt,
handleIdleHotkeyExit,
resolveCtrlCComposerAction,
resolveDoubleEscAction,
shouldDetachEditedHistoryInput,
shouldFallThroughForScroll
} from '../app/useInputHandlers.js'
const baseKey = {
downArrow: false,
pageDown: false,
pageUp: false,
shift: false,
upArrow: false,
wheelDown: false,
wheelUp: false
}
describe('shouldFallThroughForScroll — keep transcript scrolling alive during prompt overlays', () => {
it('falls through for wheel scrolls', () => {
expect(shouldFallThroughForScroll({ ...baseKey, wheelUp: true })).toBe(true)
expect(shouldFallThroughForScroll({ ...baseKey, wheelDown: true })).toBe(true)
})
it('falls through for PageUp / PageDown', () => {
expect(shouldFallThroughForScroll({ ...baseKey, pageUp: true })).toBe(true)
expect(shouldFallThroughForScroll({ ...baseKey, pageDown: true })).toBe(true)
})
it('falls through for Shift+ArrowUp / Shift+ArrowDown', () => {
expect(shouldFallThroughForScroll({ ...baseKey, shift: true, upArrow: true })).toBe(true)
expect(shouldFallThroughForScroll({ ...baseKey, shift: true, downArrow: true })).toBe(true)
})
it('does NOT fall through for plain arrows — those drive in-prompt selection', () => {
expect(shouldFallThroughForScroll({ ...baseKey, upArrow: true })).toBe(false)
expect(shouldFallThroughForScroll({ ...baseKey, downArrow: true })).toBe(false)
})
it('does NOT fall through for plain Shift — without an arrow it is a no-op', () => {
expect(shouldFallThroughForScroll({ ...baseKey, shift: true })).toBe(false)
})
it('does NOT fall through for unrelated state (no scroll keys held)', () => {
expect(shouldFallThroughForScroll(baseKey)).toBe(false)
})
})
describe('composerHasDraft — Ctrl+D exits only from an empty composer (#116443)', () => {
it('is false for an empty composer and true for text, multi-line buffer or attachments', () => {
expect(composerHasDraft({ input: '', inputBuf: [], tokens: [] })).toBe(false)
expect(composerHasDraft({ input: 'hi', inputBuf: [], tokens: [] })).toBe(true)
expect(composerHasDraft({ input: '', inputBuf: ['line 1'], tokens: [] })).toBe(true)
expect(composerHasDraft({ input: '', inputBuf: [], tokens: [{ kind: 'image' }] })).toBe(true)
})
})
describe('shouldDetachEditedHistoryInput', () => {
const history = ['older message', 'line one\nline two']
it('detaches a recalled entry as soon as the user edits it', () => {
expect(shouldDetachEditedHistoryInput(1, history, 'line one edited\nline two')).toBe(true)
})
it('keeps unchanged recalled entries in history navigation', () => {
expect(shouldDetachEditedHistoryInput(1, history, 'line one\nline two')).toBe(false)
})
it('does not detach an ordinary current draft', () => {
expect(shouldDetachEditedHistoryInput(null, history, 'new draft')).toBe(false)
})
})
describe('resolveCtrlCComposerAction — draft wins over interrupt', () => {
it('clears a non-empty composer even while the agent is streaming', () => {
expect(resolveCtrlCComposerAction({ busy: true, hasDraft: true, hasSession: true })).toBe('clear')
})
it('interrupts a running turn when the composer is empty', () => {
expect(resolveCtrlCComposerAction({ busy: true, hasDraft: false, hasSession: true })).toBe('interrupt')
})
it('clears an idle composer instead of exiting', () => {
expect(resolveCtrlCComposerAction({ busy: false, hasDraft: true, hasSession: true })).toBe('clear')
})
it('exits when idle with an empty composer', () => {
expect(resolveCtrlCComposerAction({ busy: false, hasDraft: false, hasSession: true })).toBe('exit')
})
it('does not interrupt a busy session that has no sid yet', () => {
expect(resolveCtrlCComposerAction({ busy: true, hasDraft: false, hasSession: false })).toBe('exit')
})
})
describe('resolveDoubleEscAction — double-Esc interrupts only a busy, draft-free turn (#62478)', () => {
it('interrupts the running turn when the composer is empty', () => {
expect(resolveDoubleEscAction({ busy: true, hasDraft: false, hasSession: true })).toBe('interrupt')
})
it('clears a draft instead of interrupting, even mid-stream', () => {
expect(resolveDoubleEscAction({ busy: true, hasDraft: true, hasSession: true })).toBe('clear')
})
it('does nothing while idle with an empty composer (single-Esc conventions unchanged)', () => {
expect(resolveDoubleEscAction({ busy: false, hasDraft: false, hasSession: true })).toBe('none')
})
it('does not interrupt a busy session that has no sid yet', () => {
expect(resolveDoubleEscAction({ busy: true, hasDraft: false, hasSession: false })).toBe('none')
})
it('keeps clearing an idle draft (existing discard convention)', () => {
expect(resolveDoubleEscAction({ busy: false, hasDraft: true, hasSession: true })).toBe('clear')
})
})
describe('handleIdleHotkeyExit', () => {
it('exits in normal terminals', () => {
const actions = { die: vi.fn(), sys: vi.fn() }
handleIdleHotkeyExit(actions, false)
expect(actions.die).toHaveBeenCalledTimes(1)
expect(actions.sys).not.toHaveBeenCalled()
})
it('asks the dashboard for a fresh chat instead of leaving a ghost session', () => {
const actions = { die: vi.fn(), sys: vi.fn() }
const requestDashboardNewSession = vi.fn()
handleIdleHotkeyExit(actions, true, requestDashboardNewSession)
expect(actions.die).not.toHaveBeenCalled()
expect(requestDashboardNewSession).toHaveBeenCalledTimes(1)
expect(actions.sys).toHaveBeenCalled()
})
})
describe('applyVoiceRecordResponse', () => {
it('reverts optimistic REC state when the gateway reports voice busy', () => {
const setProcessing = vi.fn()
const setRecording = vi.fn()
const sys = vi.fn()
applyVoiceRecordResponse({ status: 'busy' }, true, { setProcessing, setRecording }, sys)
expect(setRecording).toHaveBeenCalledWith(false)
expect(setProcessing).toHaveBeenCalledWith(true)
expect(sys).toHaveBeenCalled()
})
it('keeps optimistic REC state for successful recording starts', () => {
const setProcessing = vi.fn()
const setRecording = vi.fn()
applyVoiceRecordResponse({ status: 'recording' }, true, { setProcessing, setRecording }, vi.fn())
expect(setRecording).not.toHaveBeenCalled()
expect(setProcessing).not.toHaveBeenCalled()
})
it('reverts optimistic REC state when the gateway returns null', () => {
const setProcessing = vi.fn()
const setRecording = vi.fn()
applyVoiceRecordResponse(null, true, { setProcessing, setRecording }, vi.fn())
expect(setRecording).toHaveBeenCalledWith(false)
expect(setProcessing).toHaveBeenCalledWith(false)
})
})
describe('dismissSensitivePrompt', () => {
const openRequest = (id: string, method: string) => {
const respond = vi.fn()
rememberServerRequest({ fail: vi.fn(), id, method, params: {}, respond })
return respond
}
it('clears a sudo overlay and answers the server request with an empty value', () => {
resetOverlayState()
resetServerRequestsForTests()
patchOverlayState({ sudo: { requestId: 'srq-sudo' } })
const respond = openRequest('srq-sudo', 'sudo')
const sys = vi.fn()
dismissSensitivePrompt(getOverlayState(), vi.fn(), sys)
expect(getOverlayState().sudo).toBeNull()
expect(sys).toHaveBeenCalled()
expect(respond).toHaveBeenCalledWith({ value: '' })
})
it('clears a secret overlay even when its request already expired (nothing left to answer)', () => {
resetOverlayState()
resetServerRequestsForTests()
patchOverlayState({ secret: { envVar: 'API_KEY', prompt: 'Enter API key', requestId: 'srq-gone' } })
const sys = vi.fn()
dismissSensitivePrompt(getOverlayState(), vi.fn(), sys)
expect(getOverlayState().secret).toBeNull()
expect(sys).toHaveBeenCalled()
})
it('declines a vault save-login overlay with an empty value so the blocked wait resolves', () => {
resetOverlayState()
resetServerRequestsForTests()
patchOverlayState({ vaultSaveLogin: { origin: 'https://example.com', requestId: 'save-1', site: 'example.com' } })
const respond = openRequest('save-1', 'vault.save_login')
const sys = vi.fn()
dismissSensitivePrompt(getOverlayState(), vi.fn(), sys)
expect(getOverlayState().vaultSaveLogin).toBeNull()
expect(sys).toHaveBeenCalledWith('login for example.com not saved')
expect(respond).toHaveBeenCalledWith({ value: '' })
})
it('skips a vault verification-code overlay with an empty value', () => {
resetOverlayState()
resetServerRequestsForTests()
patchOverlayState({ vaultCode: { hint: '', requestId: 'code-1', site: 'github.com' } })
const respond = openRequest('code-1', 'vault.code')
const sys = vi.fn()
dismissSensitivePrompt(getOverlayState(), vi.fn(), sys)
expect(getOverlayState().vaultCode).toBeNull()
expect(sys).toHaveBeenCalledWith('verification code for github.com skipped')
expect(respond).toHaveBeenCalledWith({ value: '' })
})
})