d3ro-voice/packages/core/__tests__/input-suggestion-redteam-r3-1.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

123 lines
5.6 KiB
TypeScript

// 제안 정책 회귀 (r3-1):
// - 케어렛을 모르는 필드에서 문서 중간을 고쳤으면 제안을 닫는다 (caret-unknown)
// - 단어를 끝낸 뒤(뒤 공백) 로컬 기억이 단어 조각을 붙이지 않는다
// - 학습 제외 앱은 출처와 무관하게 학습하지 않는다
import { describe, expect, it } from 'vitest'
import {
decideSuggestion,
isEditAtTextEnd,
withoutRichEditEndMark,
shouldLearnFromApp,
shouldPersistSuggestionPrefix,
type SuggestionPolicyInput
} from '../src/input-intelligence'
import { buildLocalSuggestionCandidateEntries, joinSuggestion, normalizeSessionPrefix } from '../src/suggestion-text'
function policy(overrides: Partial<SuggestionPolicyInput> = {}): SuggestionPolicyInput {
return {
enabled: true,
modelAvailable: true,
overlayVisible: false,
composing: false,
hasSelection: false,
isPassword: false,
isEditable: true,
appName: 'notepad.exe',
excludedApps: [],
editedSinceFocus: true,
typedRecently: true,
prefix: '오늘 회의에서 논의한 내용을 정리해서',
idleMs: 2000,
triggerDelayMs: 600,
minPrefixChars: 8,
sinceLastRequestMs: 60_000,
minIntervalMs: 5000,
requestsThisMinute: 0,
maxRequestsPerMinute: 12,
requestsToday: 0,
dailyBudget: 500,
...overrides
}
}
describe('케어렛 신뢰도', () => {
it('문서 끝에서 쓰거나 지운 편집만 끝 편집으로 본다', () => {
expect(isEditAtTextEnd('abc', 'abcd')).toBe(true)
expect(isEditAtTextEnd('abc', 'ab')).toBe(true)
expect(isEditAtTextEnd('abc', 'abc')).toBe(true)
expect(isEditAtTextEnd('첫 문장. 둘째 문장.', '첫 문장이. 둘째 문장.')).toBe(false)
expect(isEditAtTextEnd('aXb', 'ab')).toBe(false)
})
it('값 끝에 줄끝 문자를 붙여 주는 입력칸(RichEdit·카카오톡)에서도 끝 편집을 알아본다', () => {
expect(isEditAtTextEnd('안녕\r', '안녕하\r')).toBe(true)
expect(isEditAtTextEnd('오늘 회의 자\r\n', '오늘 회의 자료\r\n')).toBe(true)
// 줄끝 문자 앞의 진짜 중간 편집은 여전히 중간 편집이다
expect(isEditAtTextEnd('첫 문장. 둘째\r', '첫 문장이. 둘째\r')).toBe(false)
})
it('caretReliable=false 면 요청하지 않고 떠 있는 세션도 닫는다', () => {
expect(decideSuggestion(policy({ caretReliable: false }))).toEqual({ action: 'clear', reason: 'caret-unknown' })
expect(decideSuggestion(policy({ caretReliable: false, overlayVisible: true }))).toEqual({
action: 'clear',
reason: 'caret-unknown'
})
})
it('생략하거나 true 면 기존처럼 요청한다', () => {
expect(decideSuggestion(policy()).action).toBe('request')
expect(decideSuggestion(policy({ caretReliable: true })).action).toBe('request')
})
})
describe('단어를 끝낸 뒤 로컬 기억 이음새', () => {
const hints = (phrase: string) => ({ continuationHints: [], relatedHints: [], phraseHints: [phrase] })
it("'내일 회의 ' 뒤에 '록 공유드립니다' 조각을 만들지 않는다", () => {
const raw = '내일 회의 '
const entries = buildLocalSuggestionCandidateEntries(normalizeSessionPrefix(raw), hints('회의록 공유드립니다'), 3, 160, {
wordFinished: true
})
expect(entries.map((entry) => entry.text)).not.toContain('록 공유드립니다')
})
it("'check the ' 뒤에 'n we can ship' 조각을 만들지 않는다 (원문을 넘기면 자동 판단)", () => {
const entries = buildLocalSuggestionCandidateEntries('check the ', hints('then we can ship'), 3, 160)
expect(entries.map((entry) => entry.text)).not.toContain('n we can ship')
})
it('단어 중간에서 멈췄으면 기존처럼 붙인다', () => {
const entries = buildLocalSuggestionCandidateEntries('내일 회의', hints('회의록 공유드립니다'), 3, 160)
expect(entries[0]).toEqual({ text: '록 공유드립니다', attachToPrefix: true })
expect('내일 회의' + joinSuggestion('내일 회의', entries[0].text, 'attach')).toBe('내일 회의록 공유드립니다')
})
})
describe('학습 제외 앱 정본', () => {
it('터미널·에디터는 출처와 무관하게 학습하지 않는다', () => {
expect(shouldLearnFromApp('WindowsTerminal.exe')).toBe(false)
expect(shouldLearnFromApp('Code.exe')).toBe(false)
expect(shouldLearnFromApp('KakaoTalk.exe')).toBe(true)
expect(shouldLearnFromApp(null)).toBe(true)
})
it('제안 접두 저장도 같은 규칙을 쓴다', () => {
expect(shouldPersistSuggestionPrefix({ learnTypedText: true, appName: 'WindowsTerminal.exe' })).toBe(false)
expect(shouldPersistSuggestionPrefix({ learnTypedText: true, appName: 'KakaoTalk.exe' })).toBe(true)
})
})
describe('RichEdit 문단 끝 표시', () => {
const base = {
available: true, isPassword: false, isEditable: true, isComposing: false, hasSelection: false,
textSource: 'value' as const, caretRect: null, elementRect: null, windowTitle: null, appName: 'KakaoTalk.exe',
processId: 1, capturedAt: 0
}
it('값 끝의 \r 하나를 걷어내 이음새 공백이 빠지지 않게 한다 (사용자가 넣은 줄바꿈은 남긴다)', () => {
expect(withoutRichEditEndMark({ ...base, text: '오늘 저녁에 시간 되면\r', caretOffset: null }).text).toBe('오늘 저녁에 시간 되면')
expect(withoutRichEditEndMark({ ...base, text: '첫 줄\r\r', caretOffset: null }).text).toBe('첫 줄\r')
expect(withoutRichEditEndMark({ ...base, text: '표준 줄바꿈\r\n', caretOffset: null }).text).toBe('표준 줄바꿈\r\n')
expect(withoutRichEditEndMark({ ...base, text: 'abc\r', caretOffset: 4 }).caretOffset).toBe(3)
})
})