|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const { warning } = vi.hoisted(() => ({ warning: vi.fn() })) |
| 9 | + |
| 10 | +vi.mock('@sim/emcn', () => ({ |
| 11 | + useToast: () => ({ toast: { warning } }), |
| 12 | +})) |
| 13 | + |
| 14 | +import { SIM_SELECTION_MIME } from '@/lib/copilot/chat/selection-clipboard' |
| 15 | +import { PasteAdmissionGuard } from '@/app/_shell/paste-admission-guard' |
| 16 | + |
| 17 | +let host: HTMLDivElement |
| 18 | +let root: Root |
| 19 | + |
| 20 | +function dispatchPaste(target: Element, text: string, selectionContext?: string): Event { |
| 21 | + const event = new Event('paste', { |
| 22 | + bubbles: true, |
| 23 | + cancelable: true, |
| 24 | + composed: true, |
| 25 | + }) |
| 26 | + Object.defineProperty(event, 'clipboardData', { |
| 27 | + value: { |
| 28 | + getData: (type: string) => { |
| 29 | + if (type === 'text/plain') return text |
| 30 | + if (type === SIM_SELECTION_MIME) return selectionContext ?? '' |
| 31 | + return '' |
| 32 | + }, |
| 33 | + }, |
| 34 | + }) |
| 35 | + target.dispatchEvent(event) |
| 36 | + return event |
| 37 | +} |
| 38 | + |
| 39 | +beforeEach(() => { |
| 40 | + host = document.createElement('div') |
| 41 | + document.body.appendChild(host) |
| 42 | + root = createRoot(host) |
| 43 | + act(() => root.render(<PasteAdmissionGuard />)) |
| 44 | +}) |
| 45 | + |
| 46 | +afterEach(() => { |
| 47 | + act(() => root.unmount()) |
| 48 | + host.remove() |
| 49 | + vi.clearAllMocks() |
| 50 | +}) |
| 51 | + |
| 52 | +describe('PasteAdmissionGuard', () => { |
| 53 | + it('rejects an oversized native paste before the target handler runs', () => { |
| 54 | + const input = document.createElement('textarea') |
| 55 | + input.dataset.pasteMaxBytes = '4' |
| 56 | + host.appendChild(input) |
| 57 | + const targetHandler = vi.fn() |
| 58 | + input.addEventListener('paste', targetHandler) |
| 59 | + |
| 60 | + const event = dispatchPaste(input, '12345') |
| 61 | + |
| 62 | + expect(event.defaultPrevented).toBe(true) |
| 63 | + expect(targetHandler).not.toHaveBeenCalled() |
| 64 | + expect(warning).toHaveBeenCalledOnce() |
| 65 | + }) |
| 66 | + |
| 67 | + it('does not reject a small payload because the existing native value is large', () => { |
| 68 | + const input = document.createElement('textarea') |
| 69 | + input.dataset.pasteMaxBytes = '6' |
| 70 | + input.value = '123456' |
| 71 | + host.appendChild(input) |
| 72 | + |
| 73 | + input.setSelectionRange(6, 6) |
| 74 | + expect(dispatchPaste(input, 'a').defaultPrevented).toBe(false) |
| 75 | + }) |
| 76 | + |
| 77 | + it('honors a surface-specific character contract', () => { |
| 78 | + const input = document.createElement('textarea') |
| 79 | + input.dataset.pasteMaxBytes = '100' |
| 80 | + input.dataset.pasteMaxCharacters = '2' |
| 81 | + host.appendChild(input) |
| 82 | + |
| 83 | + expect(dispatchPaste(input, '💡💡').defaultPrevented).toBe(true) |
| 84 | + }) |
| 85 | + |
| 86 | + it('does not reject a small payload because contenteditable text is already large', () => { |
| 87 | + const editable = document.createElement('div') |
| 88 | + editable.contentEditable = 'true' |
| 89 | + editable.dataset.pasteMaxBytes = '6' |
| 90 | + editable.textContent = '123456' |
| 91 | + host.appendChild(editable) |
| 92 | + |
| 93 | + expect(dispatchPaste(editable, 'a').defaultPrevented).toBe(false) |
| 94 | + }) |
| 95 | + |
| 96 | + it('lets a compact Sim selection reference bypass its large plain-text representation', () => { |
| 97 | + const input = document.createElement('textarea') |
| 98 | + input.dataset.pasteMaxBytes = '4' |
| 99 | + host.appendChild(input) |
| 100 | + const selectionContext = JSON.stringify({ |
| 101 | + kind: 'table_selection', |
| 102 | + tableId: 'table-1', |
| 103 | + tableName: 'Large table', |
| 104 | + rowIds: ['row-1'], |
| 105 | + label: 'Large table (1 row)', |
| 106 | + }) |
| 107 | + |
| 108 | + expect(dispatchPaste(input, '12345', selectionContext).defaultPrevented).toBe(false) |
| 109 | + }) |
| 110 | +}) |
0 commit comments