216 lines
8.3 KiB
TypeScript
216 lines
8.3 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import type { FocusSnapshot } from '@d3ro/core/input-intelligence'
|
|
import type { ForegroundWindowInfo } from '../../../src/main/utils/win32-foreground'
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
snapshot: vi.fn<() => Promise<FocusSnapshot>>(),
|
|
foreground: vi.fn<() => ForegroundWindowInfo | null>(),
|
|
nativeInputTick: vi.fn<() => number | 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: (code: number) => code }))
|
|
vi.mock('../../../src/main/services/UiaContextService', () => ({
|
|
getUiaContextService: () => ({
|
|
getSnapshot: mocks.snapshot,
|
|
isAvailable: () => true,
|
|
lastReason: null,
|
|
lastSuccessAt: 0
|
|
})
|
|
}))
|
|
vi.mock('../../../src/main/utils/win32-foreground', () => ({
|
|
getForegroundWindowInfo: mocks.foreground,
|
|
isForegroundKoreanIme: () => false
|
|
}))
|
|
vi.mock('../../../src/main/utils/win32-input-activity', () => ({ getLastNativeInputTick: mocks.nativeInputTick }))
|
|
|
|
import {
|
|
getInputTelemetryService,
|
|
resetInputTelemetryServiceForTests,
|
|
type TypingContext
|
|
} from '../../../src/main/services/InputTelemetryService'
|
|
|
|
interface Internal {
|
|
_running: boolean
|
|
_lastKeyAt: number
|
|
_lastTypedAt: number
|
|
_lastFocusKey: string
|
|
_textAtFocus: string
|
|
_lastContext: TypingContext | null
|
|
_foreground: { appName: string | null; windowTitle: string | null; hwnd: number | null }
|
|
_captureTextSnapshot(): Promise<TypingContext | null>
|
|
_handleKeyDown(event: { keycode: number }): void
|
|
_handleMouseDown(event: { x: number; y: number; button: number }): void
|
|
_handleMouseUp(event: { x: number; y: number }): void
|
|
_handleWheel(event: { rotation: number }): void
|
|
}
|
|
|
|
function snapshot(overrides: Partial<FocusSnapshot> = {}): FocusSnapshot {
|
|
return {
|
|
available: true,
|
|
isPassword: false,
|
|
isEditable: true,
|
|
isComposing: false,
|
|
hasSelection: false,
|
|
controlType: 'Edit',
|
|
controlName: '메시지',
|
|
textSource: 'value',
|
|
text: '안녕하세요',
|
|
caretOffset: 5,
|
|
caretRect: null,
|
|
elementRect: null,
|
|
windowTitle: '채팅',
|
|
appName: 'chrome.exe',
|
|
processId: 1,
|
|
capturedAt: Date.now(),
|
|
...overrides
|
|
}
|
|
}
|
|
|
|
describe('입력 중 늦게 도착하는 UIA 스냅샷', () => {
|
|
let service: ReturnType<typeof getInputTelemetryService>
|
|
let internal: Internal
|
|
let publish: ReturnType<typeof vi.fn>
|
|
|
|
beforeEach(() => {
|
|
resetInputTelemetryServiceForTests()
|
|
vi.useFakeTimers()
|
|
vi.setSystemTime(10_000)
|
|
mocks.foreground.mockReset().mockReturnValue(null)
|
|
mocks.nativeInputTick.mockReset().mockReturnValue(123)
|
|
mocks.snapshot.mockReset()
|
|
service = getInputTelemetryService()
|
|
internal = service as unknown as Internal
|
|
internal._running = true
|
|
internal._foreground = { appName: 'chrome.exe', windowTitle: '채팅', hwnd: 42 }
|
|
internal._lastKeyAt = Date.now()
|
|
internal._lastTypedAt = Date.now()
|
|
internal._textAtFocus = '기존 기준선'
|
|
publish = vi.fn()
|
|
service.on('typed-context', publish)
|
|
})
|
|
|
|
afterEach(() => {
|
|
service.dispose()
|
|
vi.restoreAllMocks()
|
|
vi.useRealTimers()
|
|
})
|
|
|
|
function pending() {
|
|
let resolve!: (value: FocusSnapshot) => void
|
|
mocks.snapshot.mockImplementationOnce(() => new Promise((done) => { resolve = done }))
|
|
const request = internal._captureTextSnapshot()
|
|
return { request, resolve }
|
|
}
|
|
|
|
it('같은 밀리초에 친 새 글자도 감지해 오래된 텍스트를 발행하거나 기준선으로 저장하지 않는다', async () => {
|
|
const delayed = pending()
|
|
const at = internal._lastKeyAt
|
|
internal._handleKeyDown({ keycode: 0x41 })
|
|
expect(internal._lastKeyAt).toBe(at)
|
|
delayed.resolve(snapshot())
|
|
|
|
expect(await delayed.request).toBeNull()
|
|
expect(publish).not.toHaveBeenCalled()
|
|
expect(internal._lastContext).toBeNull()
|
|
expect(internal._textAtFocus).toBe('기존 기준선')
|
|
|
|
mocks.snapshot.mockResolvedValueOnce(snapshot({ text: '안녕하세요 추가', caretOffset: 8 }))
|
|
expect((await internal._captureTextSnapshot())?.prefix).toBe('안녕하세요 추가')
|
|
expect(publish).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it.each([
|
|
['마우스 클릭', (value: Internal) => value._handleMouseDown({ x: 2, y: 3, button: 1 })],
|
|
['선택 드래그 끝', (value: Internal) => value._handleMouseUp({ x: 2, y: 3 })],
|
|
['스크롤', (value: Internal) => value._handleWheel({ rotation: 1 })],
|
|
['합성 유니코드 키', (value: Internal) => value._handleKeyDown({ keycode: 0 })]
|
|
] as const)('%s 중 도착한 응답은 버린다', async (_name, change) => {
|
|
const delayed = pending()
|
|
change(internal)
|
|
delayed.resolve(snapshot())
|
|
expect(await delayed.request).toBeNull()
|
|
expect(publish).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('통계에서 제외한 프로그램 백스페이스도 스냅샷을 무효화한다', async () => {
|
|
service.expectProgrammaticKeys(1)
|
|
const delayed = pending()
|
|
internal._handleKeyDown({ keycode: 0x08 })
|
|
delayed.resolve(snapshot())
|
|
expect(await delayed.request).toBeNull()
|
|
expect(publish).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('키 이벤트 없이 포그라운드 창이 바뀌어도 이전 창의 텍스트를 발행하지 않는다', async () => {
|
|
const delayed = pending()
|
|
mocks.foreground.mockReturnValue({
|
|
hwnd: 99, title: '다른 창', appName: 'notes.exe', processId: 2, appPath: null, bounds: null
|
|
})
|
|
delayed.resolve(snapshot())
|
|
expect(await delayed.request).toBeNull()
|
|
expect(publish).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('중지 후 재시작해도 이전 세션의 응답을 적용하지 않는다', async () => {
|
|
const delayed = pending()
|
|
service.stop()
|
|
service.start()
|
|
delayed.resolve(snapshot())
|
|
expect(await delayed.request).toBeNull()
|
|
expect(publish).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('마지막 키보다 먼저 캡처된 응답은 버린다', async () => {
|
|
mocks.snapshot.mockResolvedValueOnce(snapshot({ capturedAt: Date.now() - 1 }))
|
|
expect(await internal._captureTextSnapshot()).toBeNull()
|
|
expect(publish).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('메인 스레드에 키 콜백이 아직 도착하지 않아도 네이티브 입력 변화로 취소한다', async () => {
|
|
const revision = service.inputRevision
|
|
const delayed = pending()
|
|
mocks.nativeInputTick.mockReturnValue(124)
|
|
delayed.resolve(snapshot())
|
|
expect(service.inputRevision).toBe(revision)
|
|
expect(await delayed.request).toBeNull()
|
|
expect(publish).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('네이티브 tick이 감소하거나 래핑되어도 변화로 처리한다', async () => {
|
|
mocks.nativeInputTick.mockReturnValue(0xffffffff)
|
|
const delayed = pending()
|
|
mocks.nativeInputTick.mockReturnValue(0)
|
|
delayed.resolve(snapshot())
|
|
expect(await delayed.request).toBeNull()
|
|
expect(publish).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('Windows에서 네이티브 상태를 못 읽으면 문맥에 null을 명시해 자동 교정이 거절할 수 있게 한다', async () => {
|
|
vi.spyOn(process, 'platform', 'get').mockReturnValue('win32')
|
|
mocks.nativeInputTick.mockReturnValue(null)
|
|
mocks.snapshot.mockResolvedValueOnce(snapshot())
|
|
expect((await internal._captureTextSnapshot())?.nativeInputTick).toBeNull()
|
|
})
|
|
|
|
it('겹친 타이머들은 UIA 조회와 문맥 발행을 한 번만 한다', async () => {
|
|
const delayed = pending()
|
|
const duplicate = internal._captureTextSnapshot()
|
|
delayed.resolve(snapshot())
|
|
const [first, second] = await Promise.all([delayed.request, duplicate])
|
|
expect(first).toBe(second)
|
|
expect(first?.inputRevision).toBe(service.inputRevision)
|
|
expect(mocks.snapshot).toHaveBeenCalledTimes(1)
|
|
expect(publish).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|