-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathtest.js
More file actions
1824 lines (1453 loc) · 53.4 KB
/
Copy pathtest.js
File metadata and controls
1824 lines (1453 loc) · 53.4 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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable max-lines */
import {test} from 'node:test';
import assert from 'node:assert/strict';
import Terminal from 'terminal.js';
import {createLogUpdate} from './index.js';
const setup = options => {
const terminal = new Terminal(options);
terminal.rows = options.rows;
terminal.columns = options.columns;
terminal.state.setMode('crlf', true);
const log = createLogUpdate(terminal);
return {terminal, log};
};
const makeCapturingStream = terminal => ({
rows: terminal.rows,
columns: terminal.columns,
output: '',
write(chunk) {
this.output += chunk;
terminal.write(chunk);
},
});
// Shared test utilities for partial rendering analysis
const ESC = '\u001B[';
const ERASE_LINE = `${ESC}2K`;
const SYNCHRONIZED_OUTPUT_ENABLE = '\u001B[?2026h';
const SYNCHRONIZED_OUTPUT_DISABLE = '\u001B[?2026l';
const countEraseLines = output => output.split(ERASE_LINE).length - 1;
const assertTerminalLines = (terminal, expectedLines) => {
for (const [index, expectedLine] of expectedLines.entries()) {
assert.equal(terminal.state.getLine(index).str, expectedLine);
}
};
const setupPartialTest = ({rows = 20, columns = 40, isTTY} = {}) => {
const {terminal} = setup({rows, columns});
const stream = makeCapturingStream(terminal);
if (isTTY !== undefined) {
stream.isTTY = isTTY;
}
const log = createLogUpdate(stream);
return {terminal, stream, log};
};
test('output a single line', () => {
const {terminal, log} = setup({rows: 10, columns: 80});
log('goodbye, world');
assert.equal(terminal.state.getLine(0).str, 'goodbye, world');
});
test('uses synchronized output sequences', () => {
const {terminal, stream, log} = setupPartialTest({isTTY: true});
log('hello world');
assert.ok(stream.output.includes(SYNCHRONIZED_OUTPUT_ENABLE));
assert.ok(stream.output.includes(SYNCHRONIZED_OUTPUT_DISABLE));
assert.ok(stream.output.indexOf(SYNCHRONIZED_OUTPUT_ENABLE) < stream.output.indexOf(SYNCHRONIZED_OUTPUT_DISABLE));
assert.ok(stream.output.startsWith(SYNCHRONIZED_OUTPUT_ENABLE));
assert.ok(stream.output.endsWith(SYNCHRONIZED_OUTPUT_DISABLE));
assert.equal(stream.output.split(SYNCHRONIZED_OUTPUT_ENABLE).length - 1, 1);
assert.equal(stream.output.split(SYNCHRONIZED_OUTPUT_DISABLE).length - 1, 1);
assert.equal(terminal.state.getLine(0).str, 'hello world');
});
test('synchronized output wraps every update', () => {
const terminal = new Terminal({rows: 10, columns: 40});
terminal.rows = 10;
terminal.columns = 40;
terminal.state.setMode('crlf', true);
const writes = [];
const stream = {
rows: terminal.rows,
columns: terminal.columns,
isTTY: true,
write(chunk) {
writes.push(chunk);
terminal.write(chunk);
},
};
const log = createLogUpdate(stream);
log('One');
log('Two');
log.clear();
log('Three');
log.persist('Persisted');
for (const chunk of writes) {
assert.ok(chunk.startsWith(SYNCHRONIZED_OUTPUT_ENABLE));
assert.ok(chunk.endsWith(SYNCHRONIZED_OUTPUT_DISABLE));
assert.equal(chunk.split(SYNCHRONIZED_OUTPUT_ENABLE).length - 1, 1);
assert.equal(chunk.split(SYNCHRONIZED_OUTPUT_DISABLE).length - 1, 1);
}
});
test('does not use synchronized output on non-tty streams', () => {
const {stream, log} = setupPartialTest();
log('hello world');
assert.ok(!stream.output.includes(SYNCHRONIZED_OUTPUT_ENABLE));
assert.ok(!stream.output.includes(SYNCHRONIZED_OUTPUT_DISABLE));
const {stream: falseStream, log: falseLog} = setupPartialTest({isTTY: false});
falseLog('hello again');
assert.ok(!falseStream.output.includes(SYNCHRONIZED_OUTPUT_ENABLE));
assert.ok(!falseStream.output.includes(SYNCHRONIZED_OUTPUT_DISABLE));
});
test('update a single line', () => {
const {terminal, log} = setup({rows: 10, columns: 80});
log('greetings, world');
log('hello, world');
log('goodbye, world');
assert.equal(terminal.state.getLine(0).str, 'goodbye, world');
});
test('output a wrapped line', () => {
const {terminal, log} = setup({rows: 3, columns: 20});
log('this is a very long line that will wrap to the next line');
assert.equal(terminal.state.getLine(0).str, 'line that will wrap ');
assert.equal(terminal.state.getLine(1).str, 'to the next line');
});
test('output beyond terminal height', () => {
const {terminal, log} = setup({rows: 3, columns: 20});
log('line 1\nline 2\nline 3');
assert.equal(terminal.state.getLine(0).str, 'line 2');
assert.equal(terminal.state.getLine(1).str, 'line 3');
assert.equal(terminal.state.getLine(2).str, '');
});
test('output beyond terminal height with color', () => {
const {terminal, log} = setup({rows: 3, columns: 20});
log('\u001B[32m√\u001B[39mline 1\nline 2\nline 3');
assert.equal(terminal.state.getLine(0).str, 'line 2');
assert.equal(terminal.state.getLine(1).str, 'line 3');
assert.equal(terminal.state.getLine(2).str, '');
});
test('output beyond terminal height with multi-line color', () => {
const {terminal, log} = setup({rows: 3, columns: 20});
log('\u001B[32m√line 1\nline 2\nline 3\u001B[39m');
assert.equal(terminal.state.getLine(0).str, 'line 2');
assert.equal(terminal.state.getLine(1).str, 'line 3');
assert.equal(terminal.state.getLine(2).str, '');
});
const zeroWidthPrefixCases = [
['zero-width space', '\u200Bword'],
['leading zero-width joiner', '\u200Dword'],
['standalone combining mark', '\u0301word'],
];
test('height clipping with zero-width prefixes does not over-erase on the next update', () => {
for (const [name, text] of zeroWidthPrefixCases) {
const {terminal, stream, log} = setupPartialTest({rows: 3, columns: 20});
log(`A\n${text}\nB\nC`);
assertTerminalLines(terminal, ['B', 'C', '']);
stream.output = '';
log('D');
assert.equal(countEraseLines(stream.output), 3, name);
}
});
const zeroWidthFollowUpCases = [
{
name: 'leading blank line',
text: '\u200Bword',
assertion({stream, message}) {
assert.equal(stream.output, 'B\nC\n', message);
},
},
{
name: 'clear',
text: '\u200Dword',
assertion({terminal, stream, log, message}) {
stream.output = '';
log.clear();
assert.equal(countEraseLines(stream.output), 3, message);
assertTerminalLines(terminal, ['', '', '']);
},
},
{
name: 'persist',
text: '\u0301word',
assertion({terminal, stream, log, message}) {
stream.output = '';
log.persist('Persisted');
assert.equal(countEraseLines(stream.output), 3, message);
assert.ok(stream.output.endsWith('Persisted\n'), message);
assertTerminalLines(terminal, ['Persisted', '', '']);
},
},
];
test('zero-width prefix clipping follow-up operations stay aligned', () => {
for (const {name, text, assertion} of zeroWidthFollowUpCases) {
const {terminal, stream, log} = setupPartialTest({rows: 3, columns: 20});
log(`A\n${text}\nB\nC`);
assertion({
terminal,
stream,
log,
message: name,
});
}
});
const blankRewriteCases = [
{
name: 'rewriting a blank content line keeps the cursor aligned for the next update',
updates: ['A\nB', 'A\n\n', 'A\nX'],
expectedLines: ['A', 'X', ''],
},
{
name: 'rewriting a blank middle line preserves the suffix on later updates',
updates: ['A\nB\nC', 'A\n\nC', 'A\nX\nC'],
expectedLines: ['A', 'X', 'C', ''],
},
{
name: 'rewriting a blank last content line keeps the cursor aligned for later updates',
updates: ['A\nB\nC', 'A\nB\n', 'A\nB\nX'],
expectedLines: ['A', 'B', 'X', ''],
},
{
name: 'adding only trailing blank lines keeps the cursor aligned for later updates',
updates: ['A', 'A\n\n', 'A\nB'],
expectedLines: ['A', 'B', ''],
},
];
for (const {name, updates, expectedLines} of blankRewriteCases) {
test(name, () => {
const {terminal, log} = setupPartialTest({rows: 5, columns: 20});
for (const update of updates) {
log(update);
}
assertTerminalLines(terminal, expectedLines);
});
}
const setupHalfwidthKanaFrame = () => {
const context = setupPartialTest({rows: 3, columns: 20});
context.log('ガ\nB\nC');
return context;
};
test('height clipping does not over-remove halfwidth kana graphemes', () => {
const {terminal, stream} = setupHalfwidthKanaFrame();
assert.equal(stream.output, 'B\nC\n');
assertTerminalLines(terminal, ['B', 'C', '']);
});
const halfwidthKanaFollowUpCases = [
{
name: 'height clipping with halfwidth kana keeps erase count aligned on the next update',
assertion({terminal, stream, log}) {
stream.output = '';
log('Done');
assert.equal(countEraseLines(stream.output), 3);
assertTerminalLines(terminal, ['Done', '', '']);
},
},
{
name: 'height clipping with halfwidth kana keeps clear aligned',
assertion({terminal, stream, log}) {
stream.output = '';
log.clear();
assert.equal(countEraseLines(stream.output), 3);
assertTerminalLines(terminal, ['', '', '']);
},
},
{
name: 'height clipping with halfwidth kana keeps persist aligned',
assertion({terminal, stream, log}) {
stream.output = '';
log.persist('Persisted');
assert.equal(countEraseLines(stream.output), 3);
assert.ok(stream.output.endsWith('Persisted\n'));
assertTerminalLines(terminal, ['Persisted', '', '']);
},
},
];
for (const {name, assertion} of halfwidthKanaFollowUpCases) {
test(name, () => {
const context = setupHalfwidthKanaFrame();
assertion(context);
});
}
test('all-blank clipped frame does not set previousLineCount above terminalHeight', () => {
const {terminal, stream, log} = setupPartialTest({rows: 3, columns: 20});
// '\n\n\n\n' produces 5 split segments, clipped to 3 rows.
// The clipped text is all-blank; getFrameHeight must count it consistently
// with split('\n').length so previousLineCount does not exceed terminalHeight.
log('\n\n\n\n');
stream.output = '';
log('X');
// If previousLineCount were wrong (4 instead of 3), eraseLines would over-erase
// into scrollback. Exactly 3 erase-line sequences are expected.
assert.equal(countEraseLines(stream.output), 3);
assertTerminalLines(terminal, ['X', '', '']);
});
test('shrinking a clipped blank frame does not leave the next render one row too low', () => {
const {terminal, log} = setupPartialTest({rows: 4, columns: 20});
log('😀\n\n\n\n');
log('');
log('é');
assertTerminalLines(terminal, ['é', '', '', '']);
});
test('height clipping to one row erases previous output and recovers on resize', () => {
const {terminal, stream, log} = setupPartialTest({rows: 2, columns: 20});
log('A');
terminal.rows = 1;
stream.rows = 1;
stream.output = '';
log('你');
// On a 1-row terminal, every frame needs at least 2 rows (content + trailing newline),
// so the frame clips to empty and the previous output is erased.
terminal.rows = 2;
stream.rows = 2;
log('é');
assertTerminalLines(terminal, ['é', '']);
});
test('adding only trailing blank lines keeps clear aligned', () => {
const {terminal, log} = setupPartialTest({rows: 3, columns: 20});
log('A');
log('A\n\n');
log.clear();
log('Fresh');
assertTerminalLines(terminal, ['Fresh', '', '']);
});
test('adding only trailing blank lines keeps persist aligned', () => {
const {terminal, log} = setupPartialTest({rows: 3, columns: 20});
log('A');
log('A\n\n');
log.persist('Persisted');
log('Fresh');
assertTerminalLines(terminal, ['Persisted', 'Fresh', '']);
});
test('appending after a preserved blank line renders below it', () => {
const {terminal, stream, log} = setupPartialTest({rows: 6, columns: 20});
log('A');
stream.output = '';
log('A\n\nX');
assertTerminalLines(terminal, ['A', '', 'X', '']);
});
test('growing output', () => {
const {terminal, log} = setup({rows: 4, columns: 20});
log('one line');
assert.equal(terminal.state.getLine(0).str, 'one line');
log('one line\ntwo line');
assert.equal(terminal.state.getLine(0).str, 'one line');
assert.equal(terminal.state.getLine(1).str, 'two line');
log('one line\ntwo line\nred line');
assert.equal(terminal.state.getLine(0).str, 'one line');
assert.equal(terminal.state.getLine(1).str, 'two line');
assert.equal(terminal.state.getLine(2).str, 'red line');
assert.equal(terminal.state.getLine(3).str, '');
log('one line\ntwo line\nred line\nblue line');
assert.equal(terminal.state.getLine(0).str, 'two line');
assert.equal(terminal.state.getLine(1).str, 'red line');
assert.equal(terminal.state.getLine(2).str, 'blue line');
assert.equal(terminal.state.getLine(3).str, ''); // Always a blank last line
});
test('growing output from top', () => {
const {terminal, log} = setup({rows: 4, columns: 20});
log('blue line');
assert.equal(terminal.state.getLine(0).str, 'blue line');
log('red line\nblue line');
assert.equal(terminal.state.getLine(0).str, 'red line');
assert.equal(terminal.state.getLine(1).str, 'blue line');
log('two line\nred line\nblue line');
assert.equal(terminal.state.getLine(0).str, 'two line');
assert.equal(terminal.state.getLine(1).str, 'red line');
assert.equal(terminal.state.getLine(2).str, 'blue line');
assert.equal(terminal.state.getLine(3).str, '');
log('one line\ntwo line\nred line\nblue line');
assert.equal(terminal.state.getLine(0).str, 'two line');
assert.equal(terminal.state.getLine(1).str, 'red line');
assert.equal(terminal.state.getLine(2).str, 'blue line');
assert.equal(terminal.state.getLine(3).str, ''); // Always a blank last line
});
test('shrinking output', () => {
const {terminal, log} = setup({rows: 4, columns: 20});
log('one line\ntwo line\nred line\nblue line');
assert.equal(terminal.state.getLine(0).str, 'two line');
assert.equal(terminal.state.getLine(1).str, 'red line');
assert.equal(terminal.state.getLine(2).str, 'blue line');
assert.equal(terminal.state.getLine(3).str, ''); // Always a blank last line
log('one line\ntwo line\nred line');
assert.equal(terminal.state.getLine(0).str, 'one line');
assert.equal(terminal.state.getLine(1).str, 'two line');
assert.equal(terminal.state.getLine(2).str, 'red line');
assert.equal(terminal.state.getLine(3).str, '');
log('one line\ntwo line');
assert.equal(terminal.state.getLine(0).str, 'one line');
assert.equal(terminal.state.getLine(1).str, 'two line');
log('one line');
assert.equal(terminal.state.getLine(0).str, 'one line');
});
test('lots of updates', () => {
const {terminal, log} = setup({rows: 4, columns: 20});
for (let i = 0; i < 100; i++) {
log([
`[1] ${(i ** 3) + 11}`,
`[2] ${(i ** 2) + (10 * i) - 100}`,
].join('\n'));
}
assert.equal(terminal.state.getLine(0).str, '[1] 970310');
assert.equal(terminal.state.getLine(1).str, '[2] 10691');
});
test('log.done', () => {
const {terminal, log} = setup({rows: 10, columns: 80});
log('Fun families are all similar');
log('Happy families are all similar');
log.done();
log('Happy families are all alike; every unhappy family has its own way');
log('Happy families are all alike; every unhappy family is unhappy in its own way');
log.done();
assert.equal(terminal.state.getLine(0).str, 'Happy families are all similar');
assert.equal(terminal.state.getLine(1).str, 'Happy families are all alike; every unhappy family is unhappy in its own way');
});
test('log.clear', () => {
const {terminal, log} = setup({rows: 10, columns: 80});
log('Info');
log.clear();
log.done();
log('Second Info');
assert.equal(terminal.state.getLine(0).str, 'Second Info');
});
test('clear after hard wrapped text', () => {
const {terminal, log} = setup({rows: 10, columns: 20});
// Log text that will be hard wrapped
log('This is a very long line that will be hard wrapped across multiple terminal lines');
// The text should be wrapped across multiple lines
assert.equal(terminal.state.getLine(0).str, 'This is a very long ');
assert.equal(terminal.state.getLine(1).str, 'line that will be ha');
assert.equal(terminal.state.getLine(2).str, 'rd wrapped across mu');
assert.equal(terminal.state.getLine(3).str, 'ltiple terminal line');
assert.equal(terminal.state.getLine(4).str, 's');
// Now update with shorter text
log('Short text');
// The previous wrapped lines should be cleared
assert.equal(terminal.state.getLine(0).str, 'Short text');
assert.equal(terminal.state.getLine(1).str, '');
assert.equal(terminal.state.getLine(2).str, '');
assert.equal(terminal.state.getLine(3).str, '');
assert.equal(terminal.state.getLine(4).str, '');
});
test('clear after hard wrapped text with ANSI codes', () => {
const {terminal, log} = setup({rows: 10, columns: 20});
// Log text with ANSI codes that will be hard wrapped
log('\u001B[32mThis is a very long green line that will be hard wrapped across multiple terminal lines\u001B[39m');
// Now update with shorter text
log('Short text');
// The previous wrapped lines should be cleared
assert.equal(terminal.state.getLine(0).str, 'Short text');
assert.equal(terminal.state.getLine(1).str, '');
assert.equal(terminal.state.getLine(2).str, '');
assert.equal(terminal.state.getLine(3).str, '');
assert.equal(terminal.state.getLine(4).str, '');
});
test('defaultWidth option used when stream has no columns', () => {
// Create a stream without columns property
const terminal = new Terminal({rows: 10});
terminal.rows = 10;
delete terminal.columns; // Explicitly remove columns property
terminal.state.setMode('crlf', true);
// Create log with defaultWidth
const log = createLogUpdate(terminal, {defaultWidth: 30});
// Log text that's longer than 30 characters
log('This text should wrap at exactly 30 characters width');
// Should be wrapped at 30 characters
assert.equal(terminal.state.getLine(0).str, 'This text should wrap at exact');
assert.equal(terminal.state.getLine(1).str, 'ly 30 characters width');
});
test('defaultWidth not used when stream has columns', () => {
// Create a terminal with columns set to 20
const terminal = new Terminal({rows: 10, columns: 20});
terminal.rows = 10;
terminal.columns = 20;
terminal.state.setMode('crlf', true);
// Create log with defaultWidth that should be ignored
const log = createLogUpdate(terminal, {defaultWidth: 50});
// Log text that's longer than 20 but shorter than 50
log('This text is exactly 35 characters');
// Should be wrapped at 20 characters (stream.columns), not 50 (defaultWidth)
assert.equal(terminal.state.getLine(0).str, 'This text is exactly');
assert.equal(terminal.state.getLine(1).str, ' 35 characters');
});
test('defaultHeight option used when stream has no rows', () => {
// Create a stream without rows property
const terminal = new Terminal({columns: 20});
terminal.columns = 20;
delete terminal.rows; // Explicitly remove rows property
terminal.state.setMode('crlf', true);
// Create log with defaultHeight of 3 (instead of default 24)
const log = createLogUpdate(terminal, {defaultHeight: 3});
// Log 5 lines of text - should be truncated to 3 lines
log('Line 1\nLine 2\nLine 3\nLine 4\nLine 5');
// Should only show last 3 lines due to defaultHeight: 3
// (Note: log adds a newline, so we have 6 total lines, remove first 3)
assert.equal(terminal.state.getLine(0).str, 'Line 4');
assert.equal(terminal.state.getLine(1).str, 'Line 5');
assert.equal(terminal.state.getLine(2).str, '');
});
test('defaultHeight not used when stream has rows', () => {
// Create a terminal with rows set to 2
const terminal = new Terminal({rows: 2, columns: 20});
terminal.rows = 2;
terminal.columns = 20;
terminal.state.setMode('crlf', true);
// Create log with defaultHeight that should be ignored
const log = createLogUpdate(terminal, {defaultHeight: 5});
// Log 4 lines of text
log('Line 1\nLine 2\nLine 3\nLine 4');
// Should be truncated to 2 lines (stream.rows), not 5 (defaultHeight)
// (Note: log adds a newline, so we have 5 total lines, remove first 3)
assert.equal(terminal.state.getLine(0).str, 'Line 4');
assert.equal(terminal.state.getLine(1).str, '');
});
test('height fitting truncates content that exceeds terminal height', () => {
const {terminal, log} = setup({rows: 3, columns: 80});
// Log content that exceeds terminal height
log('Line 1\nLine 2\nLine 3\nLine 4\nLine 5');
// Should only show the last 3 lines (terminal height)
assert.equal(terminal.state.getLine(0).str, 'Line 4');
assert.equal(terminal.state.getLine(1).str, 'Line 5');
assert.equal(terminal.state.getLine(2).str, '');
});
test('height fitting behavior causes scrollback pollution when disabled', () => {
const {terminal, log} = setup({rows: 2, columns: 80});
// Simulate what would happen without height fitting
// First update: 4 lines (2 go to scrollback, 2 visible)
log('A1\nA2\nA3\nA4');
// Current visible: A3, A4
assert.equal(terminal.state.getLine(0).str, 'A4');
assert.equal(terminal.state.getLine(1).str, '');
// Second update: 4 lines again (would push 2 more to scrollback)
log('B1\nB2\nB3\nB4');
// Current visible: B3, B4
assert.equal(terminal.state.getLine(0).str, 'B4');
assert.equal(terminal.state.getLine(1).str, '');
// This demonstrates why height fitting exists - without it,
// scrollback would contain: A1, A2, A3, A4, B1, B2
// And user would see intermediate states mixed with final states
});
test('persist method writes to scrollback without height fitting', () => {
const {log} = setup({rows: 3, columns: 80});
// Use persist to write content that exceeds terminal height
log.persist('Line 1\nLine 2\nLine 3\nLine 4\nLine 5');
// Content should be written without height fitting
// Note: terminal.js simulates visible area, but full content was written
assert.ok(true);
});
test('persist method clears previous update before writing', () => {
const {terminal, log} = setup({rows: 3, columns: 80});
// First do a normal update
log('This will be cleared');
assert.equal(terminal.state.getLine(0).str, 'This will be cleared');
// Now persist should clear the update and write new content
log.persist('Permanent line 1');
log.persist('Permanent line 2');
// Both persist calls should have written their content
assert.ok(true);
});
test('mixing persist and normal updates', () => {
const {terminal, log} = setup({rows: 4, columns: 80});
// Normal update
log('Updating...');
assert.equal(terminal.state.getLine(0).str, 'Updating...');
// Persist some output
log.persist('✓ Task 1 complete');
// Another normal update
log('Processing task 2...');
// Another persist
log.persist('✓ Task 2 complete');
// Final update
log('All done!');
assert.ok(true);
});
test('persist method accepts multiple arguments', () => {
const {terminal} = setup({rows: 3, columns: 80});
const log = createLogUpdate(terminal);
// Test multiple arguments like console.log
log.persist('Status:', 'SUCCESS', '✓');
assert.ok(true);
});
test('persist method wraps long lines', () => {
const {terminal} = setup({rows: 3, columns: 20});
const log = createLogUpdate(terminal);
// Persist a long line that needs wrapping
log.persist('This is a very long line that will be wrapped');
// Should be wrapped but not height-truncated
assert.ok(true);
});
test('partial updates: append-only update causes no erase', () => {
const terminal = new Terminal({rows: 10, columns: 40});
terminal.rows = 10;
terminal.columns = 40;
terminal.state.setMode('crlf', true);
class Stream {
output = '';
constructor(target) {
this.target = target;
this.rows = target.rows;
this.columns = target.columns;
}
write(chunk) {
this.output += chunk;
this.target.write(chunk);
}
}
const stream = new Stream(terminal);
const log = createLogUpdate(stream);
log('A\nB');
stream.output = '';
log('A\nB\nC');
const ESC = '\u001B[';
const ERASE_LINE = `${ESC}2K`;
const eraseCount = stream.output.split(ERASE_LINE).length - 1;
// Appending rewrites the trailing blank to reposition it correctly
assert.equal(eraseCount, 1);
assert.ok(stream.output.includes('C'));
});
test('width change falls back to full erase', () => {
const terminal = new Terminal({rows: 10, columns: 20});
terminal.rows = 10;
terminal.columns = 20;
terminal.state.setMode('crlf', true);
class Stream {
output = '';
constructor(target) {
this.target = target;
this.rows = target.rows;
this.columns = target.columns;
}
write(chunk) {
this.output += chunk;
this.target.write(chunk);
}
}
const stream = new Stream(terminal);
const log = createLogUpdate(stream);
log('One\nTwo\nThree');
stream.output = '';
// Simulate width change
stream.columns = 30;
terminal.columns = 30;
log('One\nTwo\nFour');
const ESC = '\u001B[';
const ERASE_LINE = `${ESC}2K`;
// We expect a full erase of previous 4 lines (3 + trailing blank)
const eraseCount = stream.output.split(ERASE_LINE).length - 1;
// At least 4, some terminals may add extra clears; assert lower-bound
assert.ok(eraseCount >= 4);
});
test('partial updates: only redraw changed tail', () => {
const terminal = new Terminal({rows: 10, columns: 80});
terminal.rows = 10;
terminal.columns = 80;
terminal.state.setMode('crlf', true);
class CapturingStream {
output = '';
constructor(target) {
this.target = target;
this.rows = target.rows;
this.columns = target.columns;
}
write(chunk) {
this.output += chunk;
this.target.write(chunk);
}
}
const stream = new CapturingStream(terminal);
const log = createLogUpdate(stream);
// First render 3 lines
log('Alpha\nBeta\nGamma');
// Capture only the second render
stream.output = '';
// Change only the last line
log('Alpha\nBeta\nDelta');
// Expect not to fully clear all 4 lines (3 + trailing newline)
const ESC = '\u001B[';
const ERASE_LINE = `${ESC}2K`;
const eraseLineMatches = stream.output.split(ERASE_LINE).length - 1;
// Should clear exactly 1 line: the changed line (suffix preserved)
assert.equal(eraseLineMatches, 1);
// Should move up exactly 1 line from the blank line to the changed line
// CSI n A where n is 1
const CURSOR_UP_1 = `${ESC}1A`;
const cursorUpMatches = stream.output.split(CURSOR_UP_1).length - 1;
assert.ok(cursorUpMatches > 0);
});
test('partial updates: unchanged suffix is preserved (clear only middle block)', () => {
const terminal = new Terminal({rows: 10, columns: 80});
terminal.rows = 10;
terminal.columns = 80;
terminal.state.setMode('crlf', true);
class CapturingStream {
output = '';
constructor(target) {
this.target = target;
this.rows = target.rows;
this.columns = target.columns;
}
write(chunk) {
this.output += chunk;
this.target.write(chunk);
}
}
const stream = new CapturingStream(terminal);
const log = createLogUpdate(stream);
// First render 3 lines
log('Top\nMiddle\nBottom');
// Capture only the second render
stream.output = '';
// Change only the middle line, keep top and bottom unchanged
log('Top\nChanged\nBottom');
const ESC = '\u001B[';
const ERASE_LINE = `${ESC}2K`;
const eraseCount = stream.output.split(ERASE_LINE).length - 1;
// Should clear exactly 1 line (the changed middle line), not the entire tail
assert.equal(eraseCount, 1);
});
test('wrap-first height fitting with long wrapped line', () => {
const {terminal, log} = setup({rows: 3, columns: 10});
// Single long line that wraps across more than terminal height
log('abcdefghijklmnopqrstuvwxyz');
// After wrapping and height fitting, we should only see the last two wrapped segments
// plus the trailing blank line due to the final newline
assert.equal(terminal.state.getLine(0).str, 'klmnopqrst');
assert.equal(terminal.state.getLine(1).str, 'uvwxyz');
assert.equal(terminal.state.getLine(2).str, '');
});
test('prefix-zero diff falls back to full erase', () => {
const terminal = new Terminal({rows: 10, columns: 20});
terminal.rows = 10;
terminal.columns = 20;
terminal.state.setMode('crlf', true);
class Stream {
output = '';
constructor(target) {
this.target = target;
this.rows = target.rows;
this.columns = target.columns;
}
write(chunk) {
this.output += chunk;
this.target.write(chunk);
}
}
const stream = new Stream(terminal);
const log = createLogUpdate(stream);
log('A\nB\nC');
stream.output = '';
log('X\nY\nZ');
const ESC = '\u001B[';
const ERASE_LINE = `${ESC}2K`;
const eraseCount = stream.output.split(ERASE_LINE).length - 1;
// Previous had 3 lines + trailing blank = 4
assert.ok(eraseCount >= 4);
});
test('no-op update produces no output', () => {
const terminal = new Terminal({rows: 5, columns: 20});
terminal.rows = 5;
terminal.columns = 20;
terminal.state.setMode('crlf', true);
class Stream {
output = '';
constructor(target) {
this.target = target;
this.rows = target.rows;
this.columns = target.columns;
}
write(chunk) {
this.output += chunk;
this.target.write(chunk);
}
}
const stream = new Stream(terminal);
const log = createLogUpdate(stream);
log('Hello\nWorld');
stream.output = '';
log('Hello\nWorld');
// No escape activity and no writes
assert.equal(stream.output, '');
});
test('no-op after wrapping produces no output', () => {
const terminal = new Terminal({rows: 3, columns: 10});
terminal.rows = 3;
terminal.columns = 10;
terminal.state.setMode('crlf', true);
class Stream {
output = '';
constructor(target) {
this.target = target;
this.rows = target.rows;
this.columns = target.columns;
}
write(chunk) {
this.output += chunk;
this.target.write(chunk);
}
}
const stream = new Stream(terminal);
const log = createLogUpdate(stream);
const long = 'abcdefghijABCDEFGHIJklmno';
log(long);
stream.output = '';
log(long);
// No additional writes for identical wrapped content
assert.equal(stream.output, '');
});
test('change only second line clears exactly one line', () => {
const terminal = new Terminal({rows: 5, columns: 20});
terminal.rows = 5;
terminal.columns = 20;
terminal.state.setMode('crlf', true);
class Stream {
output = '';
constructor(target) {
this.target = target;
this.rows = target.rows;
this.columns = target.columns;
}
write(chunk) {
this.output += chunk;
this.target.write(chunk);
}
}
const stream = new Stream(terminal);
const log = createLogUpdate(stream);