96 lines
4.2 KiB
TypeScript
96 lines
4.2 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
import { TextInsertService } from '../../../src/main/services/TextInsertService'
|
|
import { TypoCorrectionService } from '../../../src/main/services/TypoCorrectionService'
|
|
import type { TypingContext } from '../../../src/main/services/InputTelemetryService'
|
|
|
|
vi.mock('../../../src/main/services/text-insert/electron-clipboard-port', () => ({ electronClipboardPort: {} }))
|
|
vi.mock('../../../src/main/services/ConfigService', () => ({
|
|
configGet: (key: string) => key === 'typoAutoCorrectEnabled' ? true :
|
|
key === 'typoAutoCorrectUseModel' ? false : [],
|
|
configSet: vi.fn()
|
|
}))
|
|
vi.mock('../../../src/main/services/LoggerService', () => ({
|
|
getLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() })
|
|
}))
|
|
|
|
function deferred() {
|
|
let resolve!: () => void
|
|
const promise = new Promise<void>((done) => { resolve = done })
|
|
return { promise, resolve }
|
|
}
|
|
|
|
function fixture() {
|
|
const state = { text: '오늘 잘 됬어요 ', lastKeyAt: 1_000, revision: 1, nativeTick: 5, hwnd: 7 }
|
|
const prior = deferred()
|
|
const replacementDone = deferred()
|
|
const keyboard = {
|
|
type: async () => prior.promise,
|
|
paste: vi.fn(async () => undefined),
|
|
backspace: vi.fn(async (count: number) => { state.text = state.text.slice(0, -count) }),
|
|
replaceBeforeCaret: vi.fn(async (count: number, text: string) => {
|
|
state.text = state.text.slice(0, -count) + text
|
|
return true
|
|
})
|
|
}
|
|
const insert = new TextInsertService({ keyboard })
|
|
const typo = new TypoCorrectionService({
|
|
replaceBeforeCaret: async (count, text, options) => {
|
|
try { return (await insert.replaceBeforeCaret(count, text, 'keyboard', options)).success }
|
|
finally { replacementDone.resolve() }
|
|
},
|
|
lastKeyAt: () => state.lastKeyAt,
|
|
inputRevision: () => state.revision,
|
|
nativeInputTick: () => state.nativeTick,
|
|
foregroundHandle: () => state.hwnd,
|
|
expectProgrammaticKeys: vi.fn(),
|
|
expectProgrammaticInsert: () => () => undefined,
|
|
recentTexts: () => [],
|
|
warmModel: () => null,
|
|
generate: async function* () { yield '없음' }
|
|
})
|
|
const context: TypingContext = {
|
|
prefix: state.text, fullText: state.text, caretOffset: state.text.length,
|
|
anchor: null, anchorKind: null, isPassword: false, isEditable: true,
|
|
isComposing: false, hasSelection: false, available: true,
|
|
appName: 'notepad.exe', windowTitle: 'Fixture', windowHandle: 7,
|
|
idleMs: 800, capturedAt: 1_800, inputRevision: 1, nativeInputTick: 5,
|
|
editedSinceFocus: true, typedRecently: true, caretReliable: true
|
|
}
|
|
return { state, prior, replacementDone, keyboard, insert, typo, context }
|
|
}
|
|
|
|
describe('real correction services preserve a text sink across delayed queue interleavings', () => {
|
|
it.each(['delivered-key', 'same-millisecond-key', 'native-event-before-js'] as const)(
|
|
'preserves the whole next Hangul word when %s arrives while correction waits', async (event) => {
|
|
const f = fixture()
|
|
const priorInsertion = f.insert.insertText('', 'keyboard')
|
|
expect(f.typo.handleTypingContext(f.context)).toBe('correcting')
|
|
await Promise.resolve()
|
|
const expected = f.state.text + '안녕하세요 '
|
|
f.state.text = expected
|
|
// Isolate each guard: identical millisecond/native ticks must not hide the
|
|
// revision case, and a native event may precede both JS-side counters.
|
|
if (event === 'delivered-key') f.state.lastKeyAt += 100
|
|
if (event === 'same-millisecond-key') f.state.revision += 1
|
|
if (event === 'native-event-before-js') f.state.nativeTick += 1
|
|
f.prior.resolve()
|
|
await priorInsertion
|
|
await f.replacementDone.promise
|
|
expect(f.state.text).toBe(expected)
|
|
expect(f.keyboard.replaceBeforeCaret).not.toHaveBeenCalled()
|
|
f.typo.dispose()
|
|
}
|
|
)
|
|
|
|
it('still replaces the intended typo if nothing changed during the same queue wait', async () => {
|
|
const f = fixture()
|
|
const priorInsertion = f.insert.insertText('', 'keyboard')
|
|
expect(f.typo.handleTypingContext(f.context)).toBe('correcting')
|
|
f.prior.resolve()
|
|
await priorInsertion
|
|
await f.replacementDone.promise
|
|
expect(f.state.text).toBe('오늘 잘 됐어요 ')
|
|
expect(f.keyboard.replaceBeforeCaret).toHaveBeenCalledOnce()
|
|
f.typo.dispose()
|
|
})
|
|
})
|