-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathselection-manager.test.ts
More file actions
533 lines (383 loc) · 15.9 KB
/
Copy pathselection-manager.test.ts
File metadata and controls
533 lines (383 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/**
* Selection Manager Tests
*
* Tests for text selection functionality including:
* - Basic selection operations
* - Absolute coordinate system for scroll persistence
* - Selection clearing behavior
* - Auto-scroll during drag selection
* - Copy functionality with scrollback
*
* Test Isolation Pattern:
* Uses createIsolatedTerminal() to ensure each test gets its own WASM instance.
*/
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
import type { Terminal } from './terminal';
import { createIsolatedTerminal } from './test-helpers';
/**
* Helper to set selection using absolute coordinates
*/
function setSelectionAbsolute(
term: Terminal,
startCol: number,
startAbsRow: number,
endCol: number,
endAbsRow: number
): void {
const selMgr = (term as any).selectionManager;
if (selMgr) {
(selMgr as any).selectionStart = { col: startCol, absoluteRow: startAbsRow };
(selMgr as any).selectionEnd = { col: endCol, absoluteRow: endAbsRow };
}
}
/**
* Helper to convert viewport row to absolute row
*/
function viewportToAbsolute(term: Terminal, viewportRow: number): number {
const scrollbackLength = term.wasmTerm?.getScrollbackLength() ?? 0;
const viewportY = term.getViewportY();
return scrollbackLength + viewportRow - Math.floor(viewportY);
}
describe('SelectionManager', () => {
let container: HTMLElement | null = null;
beforeEach(async () => {
if (typeof document !== 'undefined') {
container = document.createElement('div');
document.body.appendChild(container);
}
});
afterEach(() => {
if (container && container.parentNode) {
container.parentNode.removeChild(container);
container = null;
}
});
describe('Construction', () => {
test('creates without errors', async () => {
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
expect(term).toBeDefined();
});
});
describe('API', () => {
test('has required public methods', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
const selMgr = (term as any).selectionManager;
expect(typeof selMgr.getSelection).toBe('function');
expect(typeof selMgr.hasSelection).toBe('function');
expect(typeof selMgr.clearSelection).toBe('function');
expect(typeof selMgr.selectAll).toBe('function');
expect(typeof selMgr.getSelectionCoords).toBe('function');
expect(typeof selMgr.dispose).toBe('function');
expect(typeof selMgr.getDirtySelectionRows).toBe('function');
expect(typeof selMgr.clearDirtySelectionRows).toBe('function');
term.dispose();
});
});
describe('Selection with absolute coordinates', () => {
test('hasSelection returns false when no selection', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
const selMgr = (term as any).selectionManager;
expect(selMgr.hasSelection()).toBe(false);
term.dispose();
});
test('hasSelection returns true when selection exists', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
term.write('Hello World\r\n');
// Set selection using absolute coordinates
setSelectionAbsolute(term, 0, 0, 5, 0);
const selMgr = (term as any).selectionManager;
expect(selMgr.hasSelection()).toBe(true);
term.dispose();
});
test('hasSelection returns true for single cell programmatic selection', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
// Programmatic single-cell selection should be valid
// (e.g., triple-click on single-char line, or select(col, row, 1))
setSelectionAbsolute(term, 5, 0, 5, 0);
const selMgr = (term as any).selectionManager;
expect(selMgr.hasSelection()).toBe(true);
term.dispose();
});
test('clearSelection clears selection and marks rows dirty', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
term.write('Line 1\r\nLine 2\r\nLine 3\r\n');
const scrollbackLen = term.wasmTerm!.getScrollbackLength();
setSelectionAbsolute(term, 0, scrollbackLen, 5, scrollbackLen + 2);
const selMgr = (term as any).selectionManager;
expect(selMgr.hasSelection()).toBe(true);
selMgr.clearSelection();
expect(selMgr.hasSelection()).toBe(false);
// Dirty rows should be marked for redraw
const dirtyRows = selMgr.getDirtySelectionRows();
expect(dirtyRows.size).toBeGreaterThan(0);
term.dispose();
});
});
describe('Selection text extraction', () => {
test('getSelection returns empty string when no selection', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
const selMgr = (term as any).selectionManager;
expect(selMgr.getSelection()).toBe('');
term.dispose();
});
test('getSelection extracts text from screen buffer', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
term.write('Hello World\r\n');
const scrollbackLen = term.wasmTerm!.getScrollbackLength();
// Select "Hello" (first 5 characters)
setSelectionAbsolute(term, 0, scrollbackLen, 4, scrollbackLen);
const selMgr = (term as any).selectionManager;
expect(selMgr.getSelection()).toBe('Hello');
term.dispose();
});
test('getSelection extracts multi-line text', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
term.write('Line 1\r\nLine 2\r\nLine 3\r\n');
const scrollbackLen = term.wasmTerm!.getScrollbackLength();
// Select all three lines
setSelectionAbsolute(term, 0, scrollbackLen, 5, scrollbackLen + 2);
const selMgr = (term as any).selectionManager;
const text = selMgr.getSelection();
expect(text).toContain('Line 1');
expect(text).toContain('Line 2');
expect(text).toContain('Line 3');
term.dispose();
});
test('getSelection extracts text from scrollback', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24, scrollback: 1000 });
term.open(container);
// Write enough lines to create scrollback
for (let i = 0; i < 50; i++) {
term.write(`Line ${i.toString().padStart(3, '0')}\r\n`);
}
const scrollbackLen = term.wasmTerm!.getScrollbackLength();
expect(scrollbackLen).toBeGreaterThan(0);
// Select from scrollback (first few lines)
setSelectionAbsolute(term, 0, 0, 10, 2);
const selMgr = (term as any).selectionManager;
const text = selMgr.getSelection();
expect(text).toContain('Line 000');
expect(text).toContain('Line 001');
expect(text).toContain('Line 002');
term.dispose();
});
test('getSelection extracts text spanning scrollback and screen', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24, scrollback: 1000 });
term.open(container);
// Write enough lines to fill scrollback and screen
for (let i = 0; i < 50; i++) {
term.write(`Line ${i.toString().padStart(3, '0')}\r\n`);
}
const scrollbackLen = term.wasmTerm!.getScrollbackLength();
// Select spanning scrollback and screen
// End of scrollback through beginning of screen
setSelectionAbsolute(term, 0, scrollbackLen - 2, 10, scrollbackLen + 2);
const selMgr = (term as any).selectionManager;
const text = selMgr.getSelection();
// Should contain lines from both regions
expect(text.split('\n').length).toBeGreaterThanOrEqual(4);
term.dispose();
});
});
describe('Selection persistence during scroll', () => {
test('selection coordinates are preserved when scrolling', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24, scrollback: 1000 });
term.open(container);
// Write content
for (let i = 0; i < 50; i++) {
term.write(`Line ${i.toString().padStart(3, '0')}\r\n`);
}
const scrollbackLen = term.wasmTerm!.getScrollbackLength();
// Set selection at specific absolute position
const startAbsRow = scrollbackLen + 5;
const endAbsRow = scrollbackLen + 10;
setSelectionAbsolute(term, 0, startAbsRow, 10, endAbsRow);
const selMgr = (term as any).selectionManager;
const textBefore = selMgr.getSelection();
// Scroll up
term.scrollLines(-10);
// Selection should still return the same text
const textAfter = selMgr.getSelection();
expect(textAfter).toBe(textBefore);
term.dispose();
});
test('selection coords convert correctly after scrolling', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24, scrollback: 1000 });
term.open(container);
// Write content
for (let i = 0; i < 50; i++) {
term.write(`Line ${i.toString().padStart(3, '0')}\r\n`);
}
const scrollbackLen = term.wasmTerm!.getScrollbackLength();
// Set selection in screen buffer area
setSelectionAbsolute(term, 0, scrollbackLen, 10, scrollbackLen + 5);
const selMgr = (term as any).selectionManager;
// Get viewport coords before scroll
const coordsBefore = selMgr.getSelectionCoords();
expect(coordsBefore).not.toBeNull();
// Scroll up 10 lines
term.scrollLines(-10);
// Get viewport coords after scroll - they should have shifted
const coordsAfter = selMgr.getSelectionCoords();
expect(coordsAfter).not.toBeNull();
// Viewport row should have increased by the scroll amount
expect(coordsAfter!.startRow).toBe(coordsBefore!.startRow + 10);
expect(coordsAfter!.endRow).toBe(coordsBefore!.endRow + 10);
term.dispose();
});
test('selection outside viewport returns null coords but preserves text', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24, scrollback: 1000 });
term.open(container);
// Write content
for (let i = 0; i < 100; i++) {
term.write(`Line ${i.toString().padStart(3, '0')}\r\n`);
}
// Select near the bottom of the buffer
const scrollbackLen = term.wasmTerm!.getScrollbackLength();
setSelectionAbsolute(term, 0, scrollbackLen + 10, 10, scrollbackLen + 15);
const selMgr = (term as any).selectionManager;
const text = selMgr.getSelection();
// Scroll to top - selection should be way off screen
term.scrollToTop();
// Coords should be null (off screen) but text should still work
const coords = selMgr.getSelectionCoords();
expect(coords).toBeNull();
// Text extraction should still work
expect(selMgr.getSelection()).toBe(text);
term.dispose();
});
});
describe('Dirty row tracking', () => {
test('getDirtySelectionRows returns empty set initially', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
const selMgr = (term as any).selectionManager;
expect(selMgr.getDirtySelectionRows().size).toBe(0);
term.dispose();
});
test('clearSelection marks selection rows as dirty', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
term.write('Test content\r\n');
const scrollbackLen = term.wasmTerm!.getScrollbackLength();
setSelectionAbsolute(term, 0, scrollbackLen, 5, scrollbackLen + 3);
const selMgr = (term as any).selectionManager;
selMgr.clearSelection();
const dirtyRows = selMgr.getDirtySelectionRows();
expect(dirtyRows.size).toBeGreaterThan(0);
term.dispose();
});
test('clearDirtySelectionRows clears the set', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
term.write('Test\r\n');
const scrollbackLen = term.wasmTerm!.getScrollbackLength();
setSelectionAbsolute(term, 0, scrollbackLen, 5, scrollbackLen);
const selMgr = (term as any).selectionManager;
selMgr.clearSelection();
expect(selMgr.getDirtySelectionRows().size).toBeGreaterThan(0);
selMgr.clearDirtySelectionRows();
expect(selMgr.getDirtySelectionRows().size).toBe(0);
term.dispose();
});
});
describe('Backward selection', () => {
test('handles selection from right to left', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
term.write('Hello World\r\n');
const scrollbackLen = term.wasmTerm!.getScrollbackLength();
// Select backwards (end before start)
setSelectionAbsolute(term, 10, scrollbackLen, 0, scrollbackLen);
const selMgr = (term as any).selectionManager;
const text = selMgr.getSelection();
expect(text).toBe('Hello World');
term.dispose();
});
test('handles selection from bottom to top', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
term.write('Line 1\r\nLine 2\r\nLine 3\r\n');
const scrollbackLen = term.wasmTerm!.getScrollbackLength();
// Select backwards (end row before start row)
setSelectionAbsolute(term, 5, scrollbackLen + 2, 0, scrollbackLen);
const selMgr = (term as any).selectionManager;
const text = selMgr.getSelection();
expect(text).toContain('Line 1');
expect(text).toContain('Line 2');
expect(text).toContain('Line 3');
term.dispose();
});
});
describe('selectAll', () => {
test('selectAll selects entire viewport', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
term.write('Hello\r\nWorld\r\n');
const selMgr = (term as any).selectionManager;
selMgr.selectAll();
expect(selMgr.hasSelection()).toBe(true);
const coords = selMgr.getSelectionCoords();
expect(coords).not.toBeNull();
expect(coords!.startRow).toBe(0);
expect(coords!.startCol).toBe(0);
expect(coords!.endRow).toBe(23); // rows - 1
term.dispose();
});
});
describe('select() API', () => {
test('select() creates selection at specified position', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
term.write('Hello World\r\n');
const selMgr = (term as any).selectionManager;
selMgr.select(0, 0, 5);
expect(selMgr.hasSelection()).toBe(true);
expect(selMgr.getSelection()).toBe('Hello');
term.dispose();
});
});
describe('selectLines() API', () => {
test('selectLines() selects entire lines', async () => {
if (!container) return;
const term = await createIsolatedTerminal({ cols: 80, rows: 24 });
term.open(container);
term.write('Line 1\r\nLine 2\r\nLine 3\r\n');
const selMgr = (term as any).selectionManager;
selMgr.selectLines(0, 1);
expect(selMgr.hasSelection()).toBe(true);
const text = selMgr.getSelection();
expect(text).toContain('Line 1');
expect(text).toContain('Line 2');
term.dispose();
});
});
});