d3ro-voice/apps/desktop/tests/main/services/input-telemetry-focus-flicker.test.ts
Yun Chan 3cac14f668
Some checks failed
ci / 정본·보안·린트·타입·테스트 (push) Has been cancelled
deploy-site / deploy (push) Has been cancelled
ci / 워크스페이스 빌드 검증 (push) Has been cancelled
ci / 모바일 린트·타입·Jest (push) Has been cancelled
ci / Supabase Edge Functions + Cloudflare Worker (push) Has been cancelled
ci / .NET API 서버 테스트 (push) Has been cancelled
feat(desktop): typo auto-correct and context-aware sentence prediction
Typo auto-correct (INPUT-19)
- TypoCorrectionService + core typo-correction: 2-set Korean layout
  mistypes both ways, always-wrong spellings, near-misses of the user's
  frequent words, optional model check re-validated in core; applied by
  backspace N + paste and cancelled on new keys or window change.

Prediction quality (INPUT-20/21)
- gemma models are sent raw with the user's unfinished sentence pre-filled
  into the model turn, so the model truly continues the text (mid-word
  joins and spacing exact). Other models keep the instruction prompt.
- Context: text before the caret (opening lines and a fact list of names,
  dates, amounts outside the 400-char window), messages just sent in the
  same chat window (memory only, 30 min), personal-graph follow-ups and
  IDF-ranked related sentences with Korean stemming.
- Guards: first sentence only, restated-prefix stripping (also fixes the
  instruction path), repeat drop, no invented dates/names/numbers
  (one retry), plain -다 register, no emoji unless the user uses them,
  search-box keywords. Fill loop mixes instruction prompts for variety.
- Opt-in real-model probe: tests/integration/suggestion-quality.ollama.test.ts

Suggestions now actually show up
- Terminal (Claude Code): caret trust from the input line, not the whole
  screen (status line below the box blocked every keystroke); Korean
  composition counts as typing at the end; a grey placeholder filling the
  empty box at once is not user text.
- Chrome: UIA focus flicker while typing no longer resets "edited".
- KakaoTalk/RichEdit: trailing paragraph mark ignored for edit position
  and stripped so accepted text keeps its separating space.
- While the overlay is visible the suggestion shortcuts are captured so
  Ctrl+Alt+Enter no longer also types a newline; released when hidden.
- nut-js preloaded after boot (first accept waited 4.8 s).
2026-10-03 21:48:28 +09:00

105 lines
4.2 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 }))
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)
})
})