Capture still used the old snapshot code: a file copied in Explorer looked empty and was cleared, and Excel's own formats were dropped on restore. It now uses the shared clipboard snapshot policy and leaves the clipboard alone when it couldn't put everything back.
81 lines
3.3 KiB
TypeScript
81 lines
3.3 KiB
TypeScript
// tests/main/services/screen-context-redteam-r1-3.test.ts
|
|
// 선택 텍스트 캡처 회귀 테스트:
|
|
// - 핫키 수정자(Alt 등)를 쥔 상태면 Ctrl+C 를 보내지 않고 클립보드도 건드리지 않는다
|
|
// - 캡처 후 클립보드는 저장한 모든 형식(text/html/rtf/image)을 한 번에 복원한다
|
|
|
|
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
|
import { clipboard } from 'electron'
|
|
|
|
vi.mock('../../../src/main/services/LoggerService', () => ({
|
|
getLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }),
|
|
}))
|
|
|
|
vi.mock('../../../src/main/services/ConfigService', () => ({
|
|
configGet: vi.fn(() => true),
|
|
configSet: vi.fn(),
|
|
}))
|
|
|
|
const modifiers = vi.hoisted(() => ({ released: true }))
|
|
vi.mock('../../../src/main/services/modifier-state', () => ({
|
|
waitForModifiersReleased: vi.fn(async () => modifiers.released),
|
|
}))
|
|
|
|
const nut = vi.hoisted(() => ({ pressKey: vi.fn(async () => undefined), releaseKey: vi.fn(async () => undefined) }))
|
|
vi.mock('@nut-tree-fork/nut-js', () => ({
|
|
keyboard: nut,
|
|
Key: { LeftControl: 'LeftControl', C: 'C' },
|
|
}))
|
|
|
|
type Module = typeof import('../../../src/main/services/ScreenContextService')
|
|
let getScreenContextService: Module['getScreenContextService']
|
|
|
|
// 공용 electron 목에는 clipboard.clear 가 없다 — 이 테스트에서만 채운다
|
|
const clipboardClear = vi.fn()
|
|
Object.assign(clipboard, { clear: clipboardClear })
|
|
|
|
beforeEach(async () => {
|
|
vi.resetModules()
|
|
vi.clearAllMocks()
|
|
modifiers.released = true
|
|
getScreenContextService = (await import('../../../src/main/services/ScreenContextService')).getScreenContextService
|
|
})
|
|
|
|
describe('ScreenContextService.captureSelectedText', () => {
|
|
it('수정자를 쥐고 있으면 Ctrl+C 를 보내지 않고 클립보드도 비우지 않는다', async () => {
|
|
modifiers.released = false
|
|
const text = await getScreenContextService().captureSelectedText()
|
|
expect(text).toBeNull()
|
|
expect(nut.pressKey).not.toHaveBeenCalled()
|
|
expect(clipboardClear).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('수정자를 뗀 뒤에는 복사하고, 모든 형식을 한 번에 복원한다', async () => {
|
|
const image = { isEmpty: () => false, toPNG: () => Buffer.alloc(1) }
|
|
vi.mocked(clipboard.readText).mockReturnValueOnce('원래 텍스트').mockReturnValueOnce('선택한 문장')
|
|
vi.mocked(clipboard.readHTML).mockReturnValueOnce('<b>원래</b>')
|
|
vi.mocked(clipboard.readRTF).mockReturnValueOnce('{\\rtf1 원래}')
|
|
vi.mocked(clipboard.readImage).mockReturnValueOnce(image as unknown as Electron.NativeImage)
|
|
|
|
const text = await getScreenContextService().captureSelectedText()
|
|
|
|
expect(text).toBe('선택한 문장')
|
|
expect(nut.pressKey).toHaveBeenCalledTimes(1)
|
|
expect(clipboard.write).toHaveBeenCalledWith({
|
|
text: '원래 텍스트',
|
|
html: '<b>원래</b>',
|
|
rtf: '{\\rtf1 원래}',
|
|
image,
|
|
})
|
|
})
|
|
|
|
it('탐색기에서 복사한 파일처럼 되돌릴 수 없는 클립보드면 캡처를 건너뛰고 클립보드를 건드리지 않는다', async () => {
|
|
vi.mocked(clipboard.availableFormats).mockReturnValueOnce(['FileNameW'])
|
|
|
|
const text = await getScreenContextService().captureSelectedText()
|
|
|
|
expect(text).toBeNull()
|
|
expect(nut.pressKey).not.toHaveBeenCalled()
|
|
expect(clipboardClear).not.toHaveBeenCalled()
|
|
expect(clipboard.write).not.toHaveBeenCalled()
|
|
})
|
|
})
|