d3ro-voice/apps/desktop/tests/main/services/win32-unicode-input.test.ts

124 lines
5.3 KiB
TypeScript

import { createRequire } from 'node:module'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { ErrorCode } from '@d3ro/core/errors'
const nodeRequire = createRequire(import.meta.url)
const koffiPath = nodeRequire.resolve('koffi')
const originalKoffi = nodeRequire.cache[koffiPath]
const sendInput = vi.fn<(count: number, buffer: Buffer, size: number) => number>()
let input: typeof import('../../../src/main/utils/win32-unicode-input')
beforeEach(async () => {
vi.resetModules()
vi.spyOn(process, 'platform', 'get').mockReturnValue('win32')
sendInput.mockReset().mockImplementation((count) => count)
// Production loads koffi with CommonJS require; vi.mock cannot intercept it.
// Replace its cache entry before importing so no native library can receive keys.
nodeRequire.cache[koffiPath] = {
id: koffiPath,
filename: koffiPath,
loaded: true,
exports: {
struct: vi.fn((name: string) => name),
union: vi.fn((name: string) => name),
sizeof: vi.fn(() => 40),
offsetof: vi.fn(() => 8),
load: vi.fn(() => ({ func: vi.fn(() => sendInput) }))
}
} as unknown as NodeModule
input = await import('../../../src/main/utils/win32-unicode-input')
})
afterEach(() => {
if (originalKoffi) nodeRequire.cache[koffiPath] = originalKoffi
else delete nodeRequire.cache[koffiPath]
vi.restoreAllMocks()
})
function events(): Array<{ vk: number; scan: number; flags: number }> {
const [count, buffer, size] = sendInput.mock.calls[0]
expect(size).toBe(40)
expect(buffer.length).toBe(count * size)
return Array.from({ length: count }, (_, index) => {
const base = index * size
expect(buffer.readUInt32LE(base)).toBe(1)
return {
vk: buffer.readUInt16LE(base + 8),
scan: buffer.readUInt16LE(base + 10),
flags: buffer.readUInt32LE(base + 12)
}
})
}
describe('Win32 input batches', () => {
it('encodes Hangul and both UTF-16 surrogate units as Unicode down/up pairs', () => {
expect(input.sendWin32UnicodeText('안😀')).toBe(true)
expect(sendInput).toHaveBeenCalledTimes(1)
expect(events()).toEqual([
{ vk: 0, scan: 0xc548, flags: 4 }, { vk: 0, scan: 0xc548, flags: 6 },
{ vk: 0, scan: 0xd83d, flags: 4 }, { vk: 0, scan: 0xd83d, flags: 6 },
{ vk: 0, scan: 0xde00, flags: 4 }, { vk: 0, scan: 0xde00, flags: 6 }
])
})
it('puts composition commit, requested deletion plus commit space, and replacement in one batch', () => {
expect(input.sendWin32UnicodeReplacement(2, '안', { commitComposition: true })).toBe(true)
expect(sendInput).toHaveBeenCalledTimes(1)
expect(events()).toEqual([
{ vk: 0x20, scan: 0, flags: 0 }, { vk: 0x20, scan: 0, flags: 2 },
{ vk: 8, scan: 0, flags: 0 }, { vk: 8, scan: 0, flags: 2 },
{ vk: 8, scan: 0, flags: 0 }, { vk: 8, scan: 0, flags: 2 },
{ vk: 8, scan: 0, flags: 0 }, { vk: 8, scan: 0, flags: 2 },
{ vk: 0, scan: 0xc548, flags: 4 }, { vk: 0, scan: 0xc548, flags: 6 }
])
})
it('permits fallback only when the native call reports no inserted events', () => {
sendInput.mockReturnValue(0)
expect(input.sendWin32UnicodeText('hello')).toBe(false)
expect(input.sendWin32UnicodeReplacement(2, '안')).toBe(false)
})
it.each([1, 2, 4, 5])('rejects a replacement after %i of 6 events instead of replaying deletions', (sent) => {
sendInput.mockReturnValue(sent)
expect(() => input.sendWin32UnicodeReplacement(2, '안')).toThrow(`${sent}/6 events; retry blocked`)
expect(sendInput).toHaveBeenCalledTimes(1)
})
it('rejects partially inserted ordinary text instead of duplicating it', () => {
sendInput.mockReturnValue(2)
expect(() => input.sendWin32UnicodeText('안녕')).toThrow('2/4 events; retry blocked')
expect(sendInput).toHaveBeenCalledTimes(1)
})
it('blocks retry when the native result is unknown', () => {
sendInput.mockImplementation(() => { throw new Error('native failure') })
expect(() => input.sendWin32UnicodeReplacement(2, '안')).toThrow('unknown insertion result')
expect(() => input.sendWin32UnicodeText('안')).toThrow('unknown insertion result')
expect(sendInput).toHaveBeenCalledTimes(2)
})
it('propagates partial insertion through the actual keyboard adapter and service without fallback', async () => {
const { NutKeyboardPort } = await import('../../../src/main/services/text-insert/nut-keyboard-port')
const { TextInsertService } = await import('../../../src/main/services/TextInsertService')
const keyboard = new NutKeyboardPort('win32')
const backspace = vi.spyOn(keyboard, 'backspace')
const type = vi.spyOn(keyboard, 'type')
const service = new TextInsertService({ keyboard, platform: 'win32' })
const completed = vi.fn()
service.on('insert-completed', completed)
sendInput.mockReturnValue(2)
await expect(service.replaceBeforeCaret(2, '안', 'keyboard')).rejects.toMatchObject({
code: ErrorCode.TextInsertKeySimulationFailed
})
expect(sendInput).toHaveBeenCalledTimes(1)
expect(backspace).not.toHaveBeenCalled()
expect(type).not.toHaveBeenCalled()
expect(completed).not.toHaveBeenCalled()
sendInput.mockImplementation((count) => count)
await expect(service.replaceBeforeCaret(2, '안', 'keyboard')).resolves.toMatchObject({ success: true })
expect(completed).toHaveBeenCalledTimes(1)
})
})