|
1 | 1 | // @vitest-environment jsdom |
2 | 2 | import { afterEach, describe, expect, it } from 'vitest'; |
3 | 3 | import { Terminal } from '@xterm/xterm'; |
| 4 | +import { openCopyEditor, setCopyFormat } from './copy-editor'; |
4 | 5 | import { finalizedSelection } from './copy-text-fixtures'; |
5 | | -import { __resetMouseSelectionForTests, getMouseSelectionState, setSelection } from './mouse-selection'; |
| 6 | +import { __resetMouseSelectionForTests, getMouseSelectionState, offerProgramCopy, setSelection } from './mouse-selection'; |
6 | 7 | import { watchSelection } from './selection-watch'; |
7 | 8 |
|
8 | 9 | /** Cancel-on-change, compared on each render (docs/specs/mouse-and-clipboard.md |
@@ -37,9 +38,53 @@ describe('watchSelection', () => { |
37 | 38 | watch.dispose(); |
38 | 39 | }); |
39 | 40 |
|
| 41 | + it.each([ |
| 42 | + ['linewise', {}], |
| 43 | + ['block', { shape: 'block' }], |
| 44 | + ['program', { owner: 'program' }], |
| 45 | + ] as const)('carries a %s selection when older scrollback is trimmed', async (kind, over) => { |
| 46 | + const id = 'trim-' + kind; |
| 47 | + const terminal = new Terminal({ cols: 20, rows: 3, scrollback: 3, allowProposedApi: true }); |
| 48 | + live.push(terminal); |
| 49 | + await write(terminal, 'old0\r\nold1\r\nkeep A\r\nkeep B\r\ntail0\r\ntail1'); |
| 50 | + const watch = watchSelection(id, terminal); |
| 51 | + setSelection(id, finalizedSelection({ startRow: 2, endRow: 3, endCol: 5, ...over })); |
| 52 | + openCopyEditor(id, terminal); |
| 53 | + if (kind === 'program') { |
| 54 | + offerProgramCopy(id, 'source text'); |
| 55 | + setCopyFormat(id, 'program'); |
| 56 | + } |
| 57 | + const markers = terminal.markers.map((m) => m.id); |
| 58 | + await write(terminal, '\r\ntail2'); |
| 59 | + watch.onRender(); |
| 60 | + expect(getMouseSelectionState(id).selection).toMatchObject({ startRow: 1, endRow: 2, endCol: 5 }); |
| 61 | + // Following the trim reuses the markers that moved with it. |
| 62 | + expect(terminal.markers.map((m) => m.id)).toEqual(markers); |
| 63 | + expect(getMouseSelectionState(id).copyEditor).not.toBeNull(); |
| 64 | + if (kind === 'program') { |
| 65 | + expect(getMouseSelectionState(id)).toMatchObject({ programCopy: 'source text', copyEditor: { format: 'program' } }); |
| 66 | + } |
| 67 | + await write(terminal, '\r\ntail3\r\ntail4'); |
| 68 | + watch.onRender(); |
| 69 | + expect(getMouseSelectionState(id).selection).toBeNull(); |
| 70 | + watch.dispose(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('cancels trimmed selected rows even when their replacements have identical text', async () => { |
| 74 | + const terminal = new Terminal({ cols: 20, rows: 3, scrollback: 1, allowProposedApi: true }); |
| 75 | + live.push(terminal); |
| 76 | + await write(terminal, 'same\r\nsame\r\nsame\r\nsame'); |
| 77 | + const watch = watchSelection('trim-identical', terminal); |
| 78 | + setSelection('trim-identical', finalizedSelection({ endCol: 3 })); |
| 79 | + await write(terminal, '\r\nsame'); |
| 80 | + watch.onRender(); |
| 81 | + expect(getMouseSelectionState('trim-identical').selection).toBeNull(); |
| 82 | + watch.dispose(); |
| 83 | + }); |
| 84 | + |
40 | 85 | it('releases its anchor and stops watching once disposed', async () => { |
41 | 86 | const { terminal, watch } = await watched('w-2'); |
42 | | - expect(terminal.markers).toHaveLength(2); |
| 87 | + expect(terminal.markers).toHaveLength(3); |
43 | 88 | watch.dispose(); |
44 | 89 | expect(terminal.markers).toHaveLength(0); |
45 | 90 | setSelection('w-2', finalizedSelection({ endCol: 2 })); |
|
0 commit comments