d3ro-voice/apps/desktop/tests/main/services/input-telemetry-redteam-r1-1.test.ts

326 lines
14 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { eq } from 'drizzle-orm'
import { createTestDb } from '../../helpers/createTestDb'
import { personalPhrases, phraseEdges, typingSamples } from '../../../src/main/db/schema'
const holder = vi.hoisted(() => ({
db: null as unknown,
config: new Map<string, unknown>()
}))
vi.mock('electron', () => ({ screen: { getPrimaryDisplay: () => ({ scaleFactor: 1 }) } }))
vi.mock('uiohook-napi', () => ({ uIOhook: { on: vi.fn(), removeListener: vi.fn() } }))
vi.mock('../../../src/main/db', async (importOriginal) => ({
...(await importOriginal<typeof import('../../../src/main/db')>()),
getDatabase: () => holder.db
}))
vi.mock('../../../src/main/services/ConfigService', () => ({
configGet: (key: string) => holder.config.get(key),
configSet: (key: string, value: unknown) => holder.config.set(key, value)
}))
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: () => null }))
vi.mock('../../../src/main/services/UiaContextService', () => ({
getUiaContextService: () => ({ isAvailable: () => false, lastReason: 'test', lastSuccessAt: 0 })
}))
vi.mock('../../../src/main/utils/win32-foreground', () => ({ getForegroundWindowInfo: () => null }))
import {
getInputTelemetryService,
resetInputTelemetryServiceForTests
} from '../../../src/main/services/InputTelemetryService'
import {
getPersonalGraphService,
resetPersonalGraphServiceForTests
} from '../../../src/main/services/PersonalGraphService'
let testDb: ReturnType<typeof createTestDb>
interface TelemetryInternal {
_foreground: { appName: string | null; windowTitle: string | null; hwnd: number | null }
_applyTypedDelta(snapshot: { windowTitle: string | null; controlName?: string; controlType?: string; text: string }): void
}
function snapshot(text: string) {
return { windowTitle: '채널', controlName: '메시지', controlType: 'Edit', text }
}
function typedSamples(): string[] {
return testDb.db
.select()
.from(typingSamples)
.all()
.filter((row) => row.source === 'typed')
.map((row) => row.text)
}
beforeEach(() => {
testDb = createTestDb()
holder.db = testDb.db
holder.config.clear()
holder.config.set('inputLearnTypedText', true)
holder.config.set('inputExcludedApps', [])
resetInputTelemetryServiceForTests()
resetPersonalGraphServiceForTests()
const telemetry = getInputTelemetryService() as unknown as TelemetryInternal
telemetry._foreground.appName = 'Slack.exe'
})
afterEach(() => {
testDb.close()
})
describe('버그 3: 프로그램이 삽입한 텍스트를 타이핑으로 다시 학습하지 않는다', () => {
const ACCEPTED = '다음 회의는 내일 오후 세 시에 하겠습니다'
it('예고된 제안 수락 삽입은 diff 에서 typed 로 학습하지 않는다', () => {
const telemetry = getInputTelemetryService()
const internal = telemetry as unknown as TelemetryInternal
internal._applyTypedDelta(snapshot('안녕하세요 여러분 '))
telemetry.expectProgrammaticInsert(ACCEPTED)
internal._applyTypedDelta(snapshot(`안녕하세요 여러분 ${ACCEPTED}`))
telemetry.flushPendingLearning()
expect(typedSamples()).toEqual([])
})
it('삽입 앞뒤로 직접 친 부분만 학습한다', () => {
const telemetry = getInputTelemetryService()
const internal = telemetry as unknown as TelemetryInternal
internal._applyTypedDelta(snapshot('시작 '))
telemetry.expectProgrammaticInsert(ACCEPTED)
internal._applyTypedDelta(snapshot(`시작 ${ACCEPTED} 확인 부탁드립니다`))
telemetry.flushPendingLearning()
expect(typedSamples()).toEqual(['확인 부탁드립니다'])
})
it('예고는 한 번만 소비된다 — 같은 문장을 나중에 직접 치면 학습한다', () => {
const telemetry = getInputTelemetryService()
const internal = telemetry as unknown as TelemetryInternal
internal._applyTypedDelta(snapshot(''))
internal._applyTypedDelta(snapshot('첫 줄 '))
telemetry.expectProgrammaticInsert(ACCEPTED)
internal._applyTypedDelta(snapshot(`첫 줄 ${ACCEPTED}`))
internal._applyTypedDelta(snapshot(`첫 줄 ${ACCEPTED}\n${ACCEPTED}`))
telemetry.flushPendingLearning()
expect(typedSamples()).toContain(ACCEPTED)
})
it('삽입이 실패해 예고를 취소하면 이후 직접 친 같은 글은 학습한다', () => {
const telemetry = getInputTelemetryService()
const internal = telemetry as unknown as TelemetryInternal
internal._applyTypedDelta(snapshot('앞 '))
const cancel = telemetry.expectProgrammaticInsert(ACCEPTED)
cancel()
internal._applyTypedDelta(snapshot(`앞 ${ACCEPTED}`))
telemetry.flushPendingLearning()
expect(typedSamples()).toEqual([ACCEPTED])
})
it('키보드 방식으로 치는 도중 잡힌 앞부분도 학습하지 않고 나머지를 기다린다', () => {
const telemetry = getInputTelemetryService()
const internal = telemetry as unknown as TelemetryInternal
internal._applyTypedDelta(snapshot(''))
internal._applyTypedDelta(snapshot('x '))
telemetry.expectProgrammaticInsert(ACCEPTED)
internal._applyTypedDelta(snapshot(`x ${ACCEPTED.slice(0, 8)}`))
internal._applyTypedDelta(snapshot(`x ${ACCEPTED}`))
telemetry.flushPendingLearning()
expect(typedSamples()).toEqual([])
})
})
describe('타이핑 학습은 스냅샷 조각이 아니라 문장 단위로 저장한다', () => {
it('0.4~0.8초 간격 스냅샷이 단어 중간에서 끊겨도 하나의 표본으로 합친다', () => {
const telemetry = getInputTelemetryService()
const internal = telemetry as unknown as TelemetryInternal
internal._applyTypedDelta(snapshot(''))
internal._applyTypedDelta(snapshot('칭찬받고 초콜릿을 받은 아이. 아빠'))
internal._applyTypedDelta(snapshot('칭찬받고 초콜릿을 받은 아이. 아빠도 잘했으니까 하나 줘야죠'))
expect(typedSamples()).toEqual([])
telemetry.flushPendingLearning()
expect(typedSamples()).toEqual(['칭찬받고 초콜릿을 받은 아이. 아빠도 잘했으니까 하나 줘야죠'])
})
it('친 뒤 지운 오타는 남기지 않고, 칸을 비우면(전송) 비우기 직전 글을 저장한다', () => {
const telemetry = getInputTelemetryService()
const internal = telemetry as unknown as TelemetryInternal
internal._applyTypedDelta(snapshot(''))
internal._applyTypedDelta(snapshot('내일 회의 갑ㅅ'))
internal._applyTypedDelta(snapshot('내일 회의 '))
internal._applyTypedDelta(snapshot('내일 회의 같이 가자'))
internal._applyTypedDelta(snapshot(''))
expect(typedSamples()).toEqual(['내일 회의 같이 가자'])
})
it('문장 끝 부호에서 바로 저장한다', () => {
const internal = getInputTelemetryService() as unknown as TelemetryInternal
internal._applyTypedDelta(snapshot(''))
internal._applyTypedDelta(snapshot('확인했습니다.'))
expect(typedSamples()).toEqual(['확인했습니다.'])
})
it('짧은 조각·주소·덜 조합된 자모·깨진 이모지는 거른다', () => {
const telemetry = getInputTelemetryService()
const internal = telemetry as unknown as TelemetryInternal
for (const text of ['처음', 'https://youtube.com/shorts/32P7siSb5LY', 'd=ud&', '아냐 ㅇ']) {
internal._applyTypedDelta(snapshot(''))
internal._applyTypedDelta(snapshot(text))
telemetry.flushPendingLearning()
}
internal._applyTypedDelta(snapshot(''))
internal._applyTypedDelta(snapshot('마음은 고마운데 아빠 표정이 \uf602'))
telemetry.flushPendingLearning()
expect(typedSamples()).toEqual(['마음은 고마운데 아빠 표정이'])
})
it('띄어쓰기 없는 질문과 ㅋㅋ·ㅠㅠ 같은 말투는 남긴다', () => {
const telemetry = getInputTelemetryService()
const internal = telemetry as unknown as TelemetryInternal
for (const text of ['언제쯤일까요?', '서버실 소음 ㅠㅠㅠ', '오늘 진짜 웃겼다 ㅋㅋ']) {
internal._applyTypedDelta(snapshot(''))
internal._applyTypedDelta(snapshot(text))
telemetry.flushPendingLearning()
}
expect(typedSamples()).toEqual(['언제쯤일까요?', '서버실 소음 ㅠㅠㅠ', '오늘 진짜 웃겼다 ㅋㅋ'])
})
it('이미 쌓인 조각·주소·깨진 이모지 표본과 문구를 정리하고 음성 표본은 건드리지 않는다', () => {
const now = Date.now()
const row = (text: string, source: 'typed' | 'voice') => ({
id: crypto.randomUUID(),
text,
wordCount: 1,
charCount: text.length,
appName: 'Slack.exe',
windowTitle: null,
source,
createdAt: now
})
testDb.db
.insert(typingSamples)
.values([
row('처음', 'typed'),
row('mail.google.com/mail/u/0/#inbox', 'typed'),
row('겠당ㅇ', 'typed'),
row('아빠 표정이 \uf602', 'typed'),
row('언제쯤일까요?', 'typed'),
row('네 확인했습니다', 'typed'),
row('처음', 'voice')
])
.run()
const phrase = (text: string) => ({
id: crypto.randomUUID(),
phrase: text,
count: 1,
source: 'typed' as const,
lastUsedAt: now,
createdAt: now,
appName: 'Slack.exe'
})
testDb.db
.insert(personalPhrases)
.values([phrase('램 가격이 다시 낮아질 ㅇ'), phrase('회의 자료 공유드립니다')])
.run()
;(getInputTelemetryService() as unknown as { _pruneUnlearnableCorpus(): void })._pruneUnlearnableCorpus()
const left = testDb.db.select().from(typingSamples).all().map((r) => `${r.source}:${r.text}`)
expect(left.sort()).toEqual(['typed:네 확인했습니다', 'typed:언제쯤일까요?', 'voice:처음'])
expect(testDb.db.select().from(personalPhrases).all().map((r) => r.phrase)).toEqual(['회의 자료 공유드립니다'])
})
})
describe('앱 안에서만 배우는 앱 (Agent Switchboard, 2026-10-25)', () => {
it('표본에는 앱 이름과 함께 남지만 공용 문구·그래프에는 들어가지 않는다', () => {
const telemetry = getInputTelemetryService()
const internal = telemetry as unknown as TelemetryInternal
internal._foreground.appName = 'Agent Switchboard.exe'
internal._applyTypedDelta(snapshot(''))
internal._applyTypedDelta(snapshot('그래서 말인데 개발자 도구로 열어서 확인해 줄래.'))
const rows = testDb.db.select().from(typingSamples).all()
expect(rows.map((row) => [row.appName, row.text])).toEqual([
['Agent Switchboard.exe', '그래서 말인데 개발자 도구로 열어서 확인해 줄래.']
])
expect(testDb.db.select().from(personalPhrases).all()).toEqual([])
expect(testDb.db.select().from(phraseEdges).all()).toEqual([])
})
it('이어 쓴 글 찾기는 앱을 주면 그 앱 표본에서만 찾는다', () => {
const telemetry = getInputTelemetryService()
const internal = telemetry as unknown as TelemetryInternal
for (const [app, text] of [
['Agent Switchboard.exe', '그래서 말인데 개발자 도구로 열어서 확인해 줄래.'],
['KakaoTalk.exe', '그래서 말인데 내일 저녁에 만나자.']
]) {
internal._foreground.appName = app
internal._applyTypedDelta(snapshot(''))
internal._applyTypedDelta(snapshot(text))
}
expect(telemetry.findContinuationHints('그래서 말인데', 3, { appName: 'Agent Switchboard.exe' })).toEqual([
'개발자 도구로 열어서 확인해 줄래.'
])
expect(telemetry.findContinuationHints('그래서 말인데', 3)).toHaveLength(2)
})
})
describe('버그 9: 문구 삭제는 그 문장의 그래프 엣지도 지운다', () => {
const ANCHOR = '오늘 회의 결과를 공유드립니다'
const PRIVATE = '계좌 비밀번호는 따로 보내드리겠습니다'
it('삭제한 문장이 follows 이웃·통계·조회에 남지 않는다', () => {
const graph = getPersonalGraphService()
graph.indexText(`${ANCHOR}. ${PRIVATE}.`, { source: 'typed', appName: 'Slack.exe' })
expect(graph.retrieveContext('오늘 회의 결과를').related).toContain(PRIVATE)
const row = testDb.db
.select()
.from(personalPhrases)
.where(eq(personalPhrases.phrase, PRIVATE))
.get()
expect(row).toBeDefined()
expect(getInputTelemetryService().deletePhrase(row?.id ?? '')).toBe(true)
const edges = testDb.db.select().from(phraseEdges).all()
expect(edges.filter((edge) => edge.fromPhrase === PRIVATE || edge.toPhrase === PRIVATE)).toEqual([])
expect(graph.retrieveContext('오늘 회의 결과를').related).not.toContain(PRIVATE)
expect(graph.getStats().topEdges.some((edge) => edge.to === PRIVATE || edge.from === PRIVATE)).toBe(false)
expect(graph.query('오늘 회의').neighbors.some((neighbor) => neighbor.text === PRIVATE)).toBe(false)
})
it('없는 id 삭제는 false 이고 다른 엣지를 건드리지 않는다', () => {
const graph = getPersonalGraphService()
graph.indexText(`${ANCHOR}. ${PRIVATE}.`, { source: 'typed', appName: 'Slack.exe' })
const before = testDb.db.select().from(phraseEdges).all().length
expect(getInputTelemetryService().deletePhrase('missing')).toBe(false)
expect(testDb.db.select().from(phraseEdges).all()).toHaveLength(before)
})
it('removePhrase 는 양방향 엣지를 모두 지우고 지운 수를 돌려준다', () => {
const graph = getPersonalGraphService()
graph.indexText(`${ANCHOR}. ${PRIVATE}. 확인 후 다시 연락드리겠습니다.`, { source: 'typed' })
const removed = graph.removePhrase(PRIVATE)
expect(removed).toBeGreaterThanOrEqual(2)
const remaining = testDb.db.select().from(phraseEdges).all()
expect(remaining.every((edge) => edge.fromPhrase !== PRIVATE && edge.toPhrase !== PRIVATE)).toBe(true)
})
})