d3ro-voice/apps/desktop/tests/main/services/input-telemetry-focus-flicker.test.ts

124 lines
5.1 KiB
TypeScript

// tests/main/services/input-telemetry-focus-flicker.test.ts
// 타이핑 중 UIA 포커스가 입력칸 ↔ 읽을 수 없는 요소(legacy)를 오가도 "편집한 칸" 판정을 잃지 않는다
// (2026-10-03 실측: Chrome 보류 사유 not-typing 122회).
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { FocusSnapshot } from '@d3ro/core/input-intelligence'
const uia = vi.hoisted(() => ({ next: null as FocusSnapshot | null }))
vi.mock('electron', () => ({ screen: { getPrimaryDisplay: () => ({ scaleFactor: 1 }) } }))
vi.mock('uiohook-napi', () => ({ uIOhook: { on: vi.fn(), removeListener: vi.fn() } }))
vi.mock('../../../src/main/db', () => ({ getDatabase: vi.fn() }))
vi.mock('../../../src/main/services/ConfigService', () => ({
configGet: (key: string) => (key === 'inputExcludedApps' ? [] : undefined),
configSet: vi.fn()
}))
vi.mock('../../../src/main/services/LoggerService', () => ({
getLogger: () => ({ debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() })
}))
vi.mock('../../../src/main/services/global-input-hook', () => ({ acquireGlobalInputHook: () => () => undefined }))
vi.mock('../../../src/main/services/KeyBindingService', () => ({ uiohookCodeToVk: () => 0x41 }))
vi.mock('../../../src/main/services/UiaContextService', () => ({
getUiaContextService: () => ({
getSnapshot: async () => uia.next,
isAvailable: () => true,
lastReason: null,
lastSuccessAt: 0
})
}))
vi.mock('../../../src/main/utils/win32-foreground', () => ({ getForegroundWindowInfo: () => null }))
vi.mock('../../../src/main/utils/win32-input-activity', () => ({ getLastNativeInputTick: () => 123 }))
import {
getInputTelemetryService,
resetInputTelemetryServiceForTests,
type TypingContext
} from '../../../src/main/services/InputTelemetryService'
interface Internal {
_running: boolean
_lastTypedAt: number
_lastClickAt: number
_foreground: { appName: string | null; windowTitle: string | null; hwnd: number | null }
_captureTextSnapshot(): Promise<TypingContext | null>
}
function snapshot(partial: Partial<FocusSnapshot>): FocusSnapshot {
return {
available: true,
isPassword: false,
isEditable: true,
isComposing: false,
hasSelection: false,
controlType: 'Edit',
controlName: '메시지',
textSource: 'value',
text: '',
caretOffset: null,
caretRect: null,
elementRect: null,
windowTitle: '채팅',
appName: 'chrome.exe',
processId: 1,
capturedAt: Date.now(),
...partial
}
}
describe('포커스 깜빡임', () => {
let service: Internal
beforeEach(() => {
resetInputTelemetryServiceForTests()
service = getInputTelemetryService() as unknown as Internal
service._running = true
service._foreground = { appName: 'chrome.exe', windowTitle: '채팅', hwnd: 42 }
})
async function capture(next: FocusSnapshot): Promise<TypingContext | null> {
uia.next = next
return service._captureTextSnapshot()
}
it('키보드로 치는 동안 읽을 수 없는 요소를 거쳐 같은 창 입력칸으로 돌아오면 편집한 칸으로 본다', async () => {
await capture(snapshot({ text: '' }))
await capture(snapshot({ isEditable: false, textSource: 'legacy', controlType: 'Document', controlName: '' }))
service._lastTypedAt = Date.now()
const context = await capture(snapshot({ text: '내일 회의 자료는 오늘 저녁까지' }))
expect(context?.editedSinceFocus).toBe(true)
})
it('클릭해서 들어온 칸은 예전처럼 기준선을 다시 잡는다 (옛 글로 제안하지 않는다)', async () => {
await capture(snapshot({ isEditable: false, textSource: 'legacy', controlType: 'Document', controlName: '' }))
service._lastTypedAt = Date.now() - 1000
service._lastClickAt = Date.now()
const context = await capture(snapshot({ controlName: '검색', text: '옛 검색어' }))
expect(context?.editedSinceFocus).toBe(false)
})
it('다른 창으로 옮겨 온 칸도 기준선을 다시 잡는다', async () => {
await capture(snapshot({ isEditable: false, textSource: 'legacy', controlType: 'Document', controlName: '' }))
service._lastTypedAt = Date.now()
service._foreground = { appName: 'notepad.exe', windowTitle: '메모', hwnd: 7 }
const context = await capture(snapshot({ appName: 'notepad.exe', text: '원래 있던 글' }))
expect(context?.editedSinceFocus).toBe(false)
})
it('검색창 등을 클릭한 직후 첫 스냅샷이 찍히기 전에 글자 키(ㅔㅊ 등)를 쳤다면 첫 스냅샷부터 editedSinceFocus 가 true 이다', async () => {
const internal = service as unknown as Internal & {
_handleMouseDown(event: { x: number; y: number; button: number }): void
_handleKeyDown(event: {
keycode: number
ctrlKey?: boolean
altKey?: boolean
shiftKey?: boolean
metaKey?: boolean
}): void
}
internal._handleMouseDown({ x: 100, y: 100, button: 1 })
internal._handleKeyDown({ keycode: 25 })
const context = await capture(snapshot({ appName: 'SearchHost.exe', controlName: '검색 상자', text: 'ㅔㅊ' }))
expect(context?.editedSinceFocus).toBe(true)
expect(context?.typedRecently).toBe(true)
})
})