File size: 62,675 Bytes
d810ed8 |
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 |
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, beforeEach } from 'vitest';
import stripAnsi from 'strip-ansi';
import { renderHook, act } from '@testing-library/react';
import type {
Viewport,
TextBuffer,
TextBufferState,
TextBufferAction,
} from './text-buffer.js';
import {
useTextBuffer,
offsetToLogicalPos,
logicalPosToOffset,
textBufferReducer,
findWordEndInLine,
findNextWordStartInLine,
isWordCharStrict,
} from './text-buffer.js';
import { cpLen } from '../../utils/textUtils.js';
const initialState: TextBufferState = {
lines: [''],
cursorRow: 0,
cursorCol: 0,
preferredCol: null,
undoStack: [],
redoStack: [],
clipboard: null,
selectionAnchor: null,
};
describe('textBufferReducer', () => {
it('should return the initial state if state is undefined', () => {
const action = { type: 'unknown_action' } as unknown as TextBufferAction;
const state = textBufferReducer(initialState, action);
expect(state).toHaveOnlyValidCharacters();
expect(state).toEqual(initialState);
});
describe('set_text action', () => {
it('should set new text and move cursor to the end', () => {
const action: TextBufferAction = {
type: 'set_text',
payload: 'hello\nworld',
};
const state = textBufferReducer(initialState, action);
expect(state).toHaveOnlyValidCharacters();
expect(state.lines).toEqual(['hello', 'world']);
expect(state.cursorRow).toBe(1);
expect(state.cursorCol).toBe(5);
expect(state.undoStack.length).toBe(1);
});
it('should not create an undo snapshot if pushToUndo is false', () => {
const action: TextBufferAction = {
type: 'set_text',
payload: 'no undo',
pushToUndo: false,
};
const state = textBufferReducer(initialState, action);
expect(state).toHaveOnlyValidCharacters();
expect(state.lines).toEqual(['no undo']);
expect(state.undoStack.length).toBe(0);
});
});
describe('insert action', () => {
it('should insert a character', () => {
const action: TextBufferAction = { type: 'insert', payload: 'a' };
const state = textBufferReducer(initialState, action);
expect(state).toHaveOnlyValidCharacters();
expect(state.lines).toEqual(['a']);
expect(state.cursorCol).toBe(1);
});
it('should insert a newline', () => {
const stateWithText = { ...initialState, lines: ['hello'] };
const action: TextBufferAction = { type: 'insert', payload: '\n' };
const state = textBufferReducer(stateWithText, action);
expect(state).toHaveOnlyValidCharacters();
expect(state.lines).toEqual(['', 'hello']);
expect(state.cursorRow).toBe(1);
expect(state.cursorCol).toBe(0);
});
});
describe('backspace action', () => {
it('should remove a character', () => {
const stateWithText: TextBufferState = {
...initialState,
lines: ['a'],
cursorRow: 0,
cursorCol: 1,
};
const action: TextBufferAction = { type: 'backspace' };
const state = textBufferReducer(stateWithText, action);
expect(state).toHaveOnlyValidCharacters();
expect(state.lines).toEqual(['']);
expect(state.cursorCol).toBe(0);
});
it('should join lines if at the beginning of a line', () => {
const stateWithText: TextBufferState = {
...initialState,
lines: ['hello', 'world'],
cursorRow: 1,
cursorCol: 0,
};
const action: TextBufferAction = { type: 'backspace' };
const state = textBufferReducer(stateWithText, action);
expect(state).toHaveOnlyValidCharacters();
expect(state.lines).toEqual(['helloworld']);
expect(state.cursorRow).toBe(0);
expect(state.cursorCol).toBe(5);
});
});
describe('undo/redo actions', () => {
it('should undo and redo a change', () => {
// 1. Insert text
const insertAction: TextBufferAction = {
type: 'insert',
payload: 'test',
};
const stateAfterInsert = textBufferReducer(initialState, insertAction);
expect(stateAfterInsert).toHaveOnlyValidCharacters();
expect(stateAfterInsert.lines).toEqual(['test']);
expect(stateAfterInsert.undoStack.length).toBe(1);
// 2. Undo
const undoAction: TextBufferAction = { type: 'undo' };
const stateAfterUndo = textBufferReducer(stateAfterInsert, undoAction);
expect(stateAfterUndo).toHaveOnlyValidCharacters();
expect(stateAfterUndo.lines).toEqual(['']);
expect(stateAfterUndo.undoStack.length).toBe(0);
expect(stateAfterUndo.redoStack.length).toBe(1);
// 3. Redo
const redoAction: TextBufferAction = { type: 'redo' };
const stateAfterRedo = textBufferReducer(stateAfterUndo, redoAction);
expect(stateAfterRedo).toHaveOnlyValidCharacters();
expect(stateAfterRedo.lines).toEqual(['test']);
expect(stateAfterRedo.undoStack.length).toBe(1);
expect(stateAfterRedo.redoStack.length).toBe(0);
});
});
describe('create_undo_snapshot action', () => {
it('should create a snapshot without changing state', () => {
const stateWithText: TextBufferState = {
...initialState,
lines: ['hello'],
cursorRow: 0,
cursorCol: 5,
};
const action: TextBufferAction = { type: 'create_undo_snapshot' };
const state = textBufferReducer(stateWithText, action);
expect(state).toHaveOnlyValidCharacters();
expect(state.lines).toEqual(['hello']);
expect(state.cursorRow).toBe(0);
expect(state.cursorCol).toBe(5);
expect(state.undoStack.length).toBe(1);
expect(state.undoStack[0].lines).toEqual(['hello']);
expect(state.undoStack[0].cursorRow).toBe(0);
expect(state.undoStack[0].cursorCol).toBe(5);
});
});
});
// Helper to get the state from the hook
const getBufferState = (result: { current: TextBuffer }) => {
expect(result.current).toHaveOnlyValidCharacters();
return {
text: result.current.text,
lines: [...result.current.lines], // Clone for safety
cursor: [...result.current.cursor] as [number, number],
allVisualLines: [...result.current.allVisualLines],
viewportVisualLines: [...result.current.viewportVisualLines],
visualCursor: [...result.current.visualCursor] as [number, number],
visualScrollRow: result.current.visualScrollRow,
preferredCol: result.current.preferredCol,
};
};
describe('useTextBuffer', () => {
let viewport: Viewport;
beforeEach(() => {
viewport = { width: 10, height: 3 }; // Default viewport for tests
});
describe('Initialization', () => {
it('should initialize with empty text and cursor at (0,0) by default', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
const state = getBufferState(result);
expect(state.text).toBe('');
expect(state.lines).toEqual(['']);
expect(state.cursor).toEqual([0, 0]);
expect(state.allVisualLines).toEqual(['']);
expect(state.viewportVisualLines).toEqual(['']);
expect(state.visualCursor).toEqual([0, 0]);
expect(state.visualScrollRow).toBe(0);
});
it('should initialize with provided initialText', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'hello',
viewport,
isValidPath: () => false,
}),
);
const state = getBufferState(result);
expect(state.text).toBe('hello');
expect(state.lines).toEqual(['hello']);
expect(state.cursor).toEqual([0, 0]); // Default cursor if offset not given
expect(state.allVisualLines).toEqual(['hello']);
expect(state.viewportVisualLines).toEqual(['hello']);
expect(state.visualCursor).toEqual([0, 0]);
});
it('should initialize with initialText and initialCursorOffset', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'hello\nworld',
initialCursorOffset: 7, // Should be at 'o' in 'world'
viewport,
isValidPath: () => false,
}),
);
const state = getBufferState(result);
expect(state.text).toBe('hello\nworld');
expect(state.lines).toEqual(['hello', 'world']);
expect(state.cursor).toEqual([1, 1]); // Logical cursor at 'o' in "world"
expect(state.allVisualLines).toEqual(['hello', 'world']);
expect(state.viewportVisualLines).toEqual(['hello', 'world']);
expect(state.visualCursor[0]).toBe(1); // On the second visual line
expect(state.visualCursor[1]).toBe(1); // At 'o' in "world"
});
it('should wrap visual lines', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'The quick brown fox jumps over the lazy dog.',
initialCursorOffset: 2, // After '好'
viewport: { width: 15, height: 4 },
isValidPath: () => false,
}),
);
const state = getBufferState(result);
expect(state.allVisualLines).toEqual([
'The quick',
'brown fox',
'jumps over the',
'lazy dog.',
]);
});
it('should wrap visual lines with multiple spaces', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'The quick brown fox jumps over the lazy dog.',
viewport: { width: 15, height: 4 },
isValidPath: () => false,
}),
);
const state = getBufferState(result);
// Including multiple spaces at the end of the lines like this is
// consistent with Google docs behavior and makes it intuitive to edit
// the spaces as needed.
expect(state.allVisualLines).toEqual([
'The quick ',
'brown fox ',
'jumps over the',
'lazy dog.',
]);
});
it('should wrap visual lines even without spaces', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: '123456789012345ABCDEFG', // 4 chars, 12 bytes
viewport: { width: 15, height: 2 },
isValidPath: () => false,
}),
);
const state = getBufferState(result);
// Including multiple spaces at the end of the lines like this is
// consistent with Google docs behavior and makes it intuitive to edit
// the spaces as needed.
expect(state.allVisualLines).toEqual(['123456789012345', 'ABCDEFG']);
});
it('should initialize with multi-byte unicode characters and correct cursor offset', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: '你好世界', // 4 chars, 12 bytes
initialCursorOffset: 2, // After '好'
viewport: { width: 5, height: 2 },
isValidPath: () => false,
}),
);
const state = getBufferState(result);
expect(state.text).toBe('你好世界');
expect(state.lines).toEqual(['你好世界']);
expect(state.cursor).toEqual([0, 2]);
// Visual: "你好" (width 4), "世"界" (width 4) with viewport width 5
expect(state.allVisualLines).toEqual(['你好', '世界']);
expect(state.visualCursor).toEqual([1, 0]);
});
});
describe('Basic Editing', () => {
it('insert: should insert a character and update cursor', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
act(() => result.current.insert('a'));
let state = getBufferState(result);
expect(state.text).toBe('a');
expect(state.cursor).toEqual([0, 1]);
expect(state.visualCursor).toEqual([0, 1]);
act(() => result.current.insert('b'));
state = getBufferState(result);
expect(state.text).toBe('ab');
expect(state.cursor).toEqual([0, 2]);
expect(state.visualCursor).toEqual([0, 2]);
});
it('insert: should insert text in the middle of a line', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'abc',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.move('right'));
act(() => result.current.insert('-NEW-'));
const state = getBufferState(result);
expect(state.text).toBe('a-NEW-bc');
expect(state.cursor).toEqual([0, 6]);
});
it('newline: should create a new line and move cursor', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'ab',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.move('end')); // cursor at [0,2]
act(() => result.current.newline());
const state = getBufferState(result);
expect(state.text).toBe('ab\n');
expect(state.lines).toEqual(['ab', '']);
expect(state.cursor).toEqual([1, 0]);
expect(state.allVisualLines).toEqual(['ab', '']);
expect(state.viewportVisualLines).toEqual(['ab', '']); // viewport height 3
expect(state.visualCursor).toEqual([1, 0]); // On the new visual line
});
it('backspace: should delete char to the left or merge lines', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'a\nb',
viewport,
isValidPath: () => false,
}),
);
act(() => {
result.current.move('down');
});
act(() => {
result.current.move('end'); // cursor to [1,1] (end of 'b')
});
act(() => result.current.backspace()); // delete 'b'
let state = getBufferState(result);
expect(state.text).toBe('a\n');
expect(state.cursor).toEqual([1, 0]);
act(() => result.current.backspace()); // merge lines
state = getBufferState(result);
expect(state.text).toBe('a');
expect(state.cursor).toEqual([0, 1]); // cursor after 'a'
expect(state.allVisualLines).toEqual(['a']);
expect(state.viewportVisualLines).toEqual(['a']);
expect(state.visualCursor).toEqual([0, 1]);
});
it('del: should delete char to the right or merge lines', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'a\nb',
viewport,
isValidPath: () => false,
}),
);
// cursor at [0,0]
act(() => result.current.del()); // delete 'a'
let state = getBufferState(result);
expect(state.text).toBe('\nb');
expect(state.cursor).toEqual([0, 0]);
act(() => result.current.del()); // merge lines (deletes newline)
state = getBufferState(result);
expect(state.text).toBe('b');
expect(state.cursor).toEqual([0, 0]);
expect(state.allVisualLines).toEqual(['b']);
expect(state.viewportVisualLines).toEqual(['b']);
expect(state.visualCursor).toEqual([0, 0]);
});
});
describe('Drag and Drop File Paths', () => {
it('should prepend @ to a valid file path on insert', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => true }),
);
const filePath = '/path/to/a/valid/file.txt';
act(() => result.current.insert(filePath, { paste: true }));
expect(getBufferState(result).text).toBe(`@${filePath} `);
});
it('should not prepend @ to an invalid file path on insert', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
const notAPath = 'this is just some long text';
act(() => result.current.insert(notAPath, { paste: true }));
expect(getBufferState(result).text).toBe(notAPath);
});
it('should handle quoted paths', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => true }),
);
const filePath = "'/path/to/a/valid/file.txt'";
act(() => result.current.insert(filePath, { paste: true }));
expect(getBufferState(result).text).toBe(`@/path/to/a/valid/file.txt `);
});
it('should not prepend @ to short text that is not a path', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => true }),
);
const shortText = 'ab';
act(() => result.current.insert(shortText, { paste: true }));
expect(getBufferState(result).text).toBe(shortText);
});
});
describe('Shell Mode Behavior', () => {
it('should not prepend @ to valid file paths when shellModeActive is true', () => {
const { result } = renderHook(() =>
useTextBuffer({
viewport,
isValidPath: () => true,
shellModeActive: true,
}),
);
const filePath = '/path/to/a/valid/file.txt';
act(() => result.current.insert(filePath, { paste: true }));
expect(getBufferState(result).text).toBe(filePath); // No @ prefix
});
it('should not prepend @ to quoted paths when shellModeActive is true', () => {
const { result } = renderHook(() =>
useTextBuffer({
viewport,
isValidPath: () => true,
shellModeActive: true,
}),
);
const quotedFilePath = "'/path/to/a/valid/file.txt'";
act(() => result.current.insert(quotedFilePath, { paste: true }));
expect(getBufferState(result).text).toBe(quotedFilePath); // No @ prefix, keeps quotes
});
it('should behave normally with invalid paths when shellModeActive is true', () => {
const { result } = renderHook(() =>
useTextBuffer({
viewport,
isValidPath: () => false,
shellModeActive: true,
}),
);
const notAPath = 'this is just some text';
act(() => result.current.insert(notAPath, { paste: true }));
expect(getBufferState(result).text).toBe(notAPath);
});
it('should behave normally with short text when shellModeActive is true', () => {
const { result } = renderHook(() =>
useTextBuffer({
viewport,
isValidPath: () => true,
shellModeActive: true,
}),
);
const shortText = 'ls';
act(() => result.current.insert(shortText, { paste: true }));
expect(getBufferState(result).text).toBe(shortText); // No @ prefix for short text
});
});
describe('Cursor Movement', () => {
it('move: left/right should work within and across visual lines (due to wrapping)', () => {
// Text: "long line1next line2" (20 chars)
// Viewport width 5. Word wrapping should produce:
// "long " (5)
// "line1" (5)
// "next " (5)
// "line2" (5)
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'long line1next line2', // Corrected: was 'long line1next line2'
viewport: { width: 5, height: 4 },
isValidPath: () => false,
}),
);
// Initial cursor [0,0] logical, visual [0,0] ("l" of "long ")
act(() => result.current.move('right')); // visual [0,1] ("o")
expect(getBufferState(result).visualCursor).toEqual([0, 1]);
act(() => result.current.move('right')); // visual [0,2] ("n")
act(() => result.current.move('right')); // visual [0,3] ("g")
act(() => result.current.move('right')); // visual [0,4] (" ")
expect(getBufferState(result).visualCursor).toEqual([0, 4]);
act(() => result.current.move('right')); // visual [1,0] ("l" of "line1")
expect(getBufferState(result).visualCursor).toEqual([1, 0]);
expect(getBufferState(result).cursor).toEqual([0, 5]); // logical cursor
act(() => result.current.move('left')); // visual [0,4] (" " of "long ")
expect(getBufferState(result).visualCursor).toEqual([0, 4]);
expect(getBufferState(result).cursor).toEqual([0, 4]); // logical cursor
});
it('move: up/down should preserve preferred visual column', () => {
const text = 'abcde\nxy\n12345';
const { result } = renderHook(() =>
useTextBuffer({
initialText: text,
viewport,
isValidPath: () => false,
}),
);
expect(result.current.allVisualLines).toEqual(['abcde', 'xy', '12345']);
// Place cursor at the end of "abcde" -> logical [0,5]
act(() => {
result.current.move('home'); // to [0,0]
});
for (let i = 0; i < 5; i++) {
act(() => {
result.current.move('right'); // to [0,5]
});
}
expect(getBufferState(result).cursor).toEqual([0, 5]);
expect(getBufferState(result).visualCursor).toEqual([0, 5]);
// Set preferredCol by moving up then down to the same spot, then test.
act(() => {
result.current.move('down'); // to xy, logical [1,2], visual [1,2], preferredCol should be 5
});
let state = getBufferState(result);
expect(state.cursor).toEqual([1, 2]); // Logical cursor at end of 'xy'
expect(state.visualCursor).toEqual([1, 2]); // Visual cursor at end of 'xy'
expect(state.preferredCol).toBe(5);
act(() => result.current.move('down')); // to '12345', preferredCol=5.
state = getBufferState(result);
expect(state.cursor).toEqual([2, 5]); // Logical cursor at end of '12345'
expect(state.visualCursor).toEqual([2, 5]); // Visual cursor at end of '12345'
expect(state.preferredCol).toBe(5); // Preferred col is maintained
act(() => result.current.move('left')); // preferredCol should reset
state = getBufferState(result);
expect(state.preferredCol).toBe(null);
});
it('move: home/end should go to visual line start/end', () => {
const initialText = 'line one\nsecond line';
const { result } = renderHook(() =>
useTextBuffer({
initialText,
viewport: { width: 5, height: 5 },
isValidPath: () => false,
}),
);
expect(result.current.allVisualLines).toEqual([
'line',
'one',
'secon',
'd',
'line',
]);
// Initial cursor [0,0] (start of "line")
act(() => result.current.move('down')); // visual cursor from [0,0] to [1,0] ("o" of "one")
act(() => result.current.move('right')); // visual cursor to [1,1] ("n" of "one")
expect(getBufferState(result).visualCursor).toEqual([1, 1]);
act(() => result.current.move('home')); // visual cursor to [1,0] (start of "one")
expect(getBufferState(result).visualCursor).toEqual([1, 0]);
act(() => result.current.move('end')); // visual cursor to [1,3] (end of "one")
expect(getBufferState(result).visualCursor).toEqual([1, 3]); // "one" is 3 chars
});
});
describe('Visual Layout & Viewport', () => {
it('should wrap long lines correctly into visualLines', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'This is a very long line of text.', // 33 chars
viewport: { width: 10, height: 5 },
isValidPath: () => false,
}),
);
const state = getBufferState(result);
// Expected visual lines with word wrapping (viewport width 10):
// "This is a"
// "very long"
// "line of"
// "text."
expect(state.allVisualLines.length).toBe(4);
expect(state.allVisualLines[0]).toBe('This is a');
expect(state.allVisualLines[1]).toBe('very long');
expect(state.allVisualLines[2]).toBe('line of');
expect(state.allVisualLines[3]).toBe('text.');
});
it('should update visualScrollRow when visualCursor moves out of viewport', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'l1\nl2\nl3\nl4\nl5',
viewport: { width: 5, height: 3 }, // Can show 3 visual lines
isValidPath: () => false,
}),
);
// Initial: l1, l2, l3 visible. visualScrollRow = 0. visualCursor = [0,0]
expect(getBufferState(result).visualScrollRow).toBe(0);
expect(getBufferState(result).allVisualLines).toEqual([
'l1',
'l2',
'l3',
'l4',
'l5',
]);
expect(getBufferState(result).viewportVisualLines).toEqual([
'l1',
'l2',
'l3',
]);
act(() => result.current.move('down')); // vc=[1,0]
act(() => result.current.move('down')); // vc=[2,0] (l3)
expect(getBufferState(result).visualScrollRow).toBe(0);
act(() => result.current.move('down')); // vc=[3,0] (l4) - scroll should happen
// Now: l2, l3, l4 visible. visualScrollRow = 1.
let state = getBufferState(result);
expect(state.visualScrollRow).toBe(1);
expect(state.allVisualLines).toEqual(['l1', 'l2', 'l3', 'l4', 'l5']);
expect(state.viewportVisualLines).toEqual(['l2', 'l3', 'l4']);
expect(state.visualCursor).toEqual([3, 0]);
act(() => result.current.move('up')); // vc=[2,0] (l3)
act(() => result.current.move('up')); // vc=[1,0] (l2)
expect(getBufferState(result).visualScrollRow).toBe(1);
act(() => result.current.move('up')); // vc=[0,0] (l1) - scroll up
// Now: l1, l2, l3 visible. visualScrollRow = 0
state = getBufferState(result); // Assign to the existing `state` variable
expect(state.visualScrollRow).toBe(0);
expect(state.allVisualLines).toEqual(['l1', 'l2', 'l3', 'l4', 'l5']);
expect(state.viewportVisualLines).toEqual(['l1', 'l2', 'l3']);
expect(state.visualCursor).toEqual([0, 0]);
});
});
describe('Undo/Redo', () => {
it('should undo and redo an insert operation', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
act(() => result.current.insert('a'));
expect(getBufferState(result).text).toBe('a');
act(() => result.current.undo());
expect(getBufferState(result).text).toBe('');
expect(getBufferState(result).cursor).toEqual([0, 0]);
act(() => result.current.redo());
expect(getBufferState(result).text).toBe('a');
expect(getBufferState(result).cursor).toEqual([0, 1]);
});
it('should undo and redo a newline operation', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'test',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.move('end'));
act(() => result.current.newline());
expect(getBufferState(result).text).toBe('test\n');
act(() => result.current.undo());
expect(getBufferState(result).text).toBe('test');
expect(getBufferState(result).cursor).toEqual([0, 4]);
act(() => result.current.redo());
expect(getBufferState(result).text).toBe('test\n');
expect(getBufferState(result).cursor).toEqual([1, 0]);
});
});
describe('Unicode Handling', () => {
it('insert: should correctly handle multi-byte unicode characters', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
act(() => result.current.insert('你好'));
const state = getBufferState(result);
expect(state.text).toBe('你好');
expect(state.cursor).toEqual([0, 2]); // Cursor is 2 (char count)
expect(state.visualCursor).toEqual([0, 2]);
});
it('backspace: should correctly delete multi-byte unicode characters', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: '你好',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.move('end')); // cursor at [0,2]
act(() => result.current.backspace()); // delete '好'
let state = getBufferState(result);
expect(state.text).toBe('你');
expect(state.cursor).toEqual([0, 1]);
act(() => result.current.backspace()); // delete '你'
state = getBufferState(result);
expect(state.text).toBe('');
expect(state.cursor).toEqual([0, 0]);
});
it('move: left/right should treat multi-byte chars as single units for visual cursor', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: '🐶🐱',
viewport: { width: 5, height: 1 },
isValidPath: () => false,
}),
);
// Initial: visualCursor [0,0]
act(() => result.current.move('right')); // visualCursor [0,1] (after 🐶)
let state = getBufferState(result);
expect(state.cursor).toEqual([0, 1]);
expect(state.visualCursor).toEqual([0, 1]);
act(() => result.current.move('right')); // visualCursor [0,2] (after 🐱)
state = getBufferState(result);
expect(state.cursor).toEqual([0, 2]);
expect(state.visualCursor).toEqual([0, 2]);
act(() => result.current.move('left')); // visualCursor [0,1] (before 🐱 / after 🐶)
state = getBufferState(result);
expect(state.cursor).toEqual([0, 1]);
expect(state.visualCursor).toEqual([0, 1]);
});
});
describe('handleInput', () => {
it('should insert printable characters', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
act(() =>
result.current.handleInput({
name: 'h',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: 'h',
}),
);
act(() =>
result.current.handleInput({
name: 'i',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: 'i',
}),
);
expect(getBufferState(result).text).toBe('hi');
});
it('should handle "Enter" key as newline', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
act(() =>
result.current.handleInput({
name: 'return',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: '\r',
}),
);
expect(getBufferState(result).lines).toEqual(['', '']);
});
it('should handle "Backspace" key', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'a',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.move('end'));
act(() =>
result.current.handleInput({
name: 'backspace',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: '\x7f',
}),
);
expect(getBufferState(result).text).toBe('');
});
it('should handle multiple delete characters in one input', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'abcde',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.move('end')); // cursor at the end
expect(getBufferState(result).cursor).toEqual([0, 5]);
act(() => {
result.current.handleInput({
name: 'backspace',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: '\x7f',
});
result.current.handleInput({
name: 'backspace',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: '\x7f',
});
result.current.handleInput({
name: 'backspace',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: '\x7f',
});
});
expect(getBufferState(result).text).toBe('ab');
expect(getBufferState(result).cursor).toEqual([0, 2]);
});
it('should handle inserts that contain delete characters', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'abcde',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.move('end')); // cursor at the end
expect(getBufferState(result).cursor).toEqual([0, 5]);
act(() => {
result.current.insert('\x7f\x7f\x7f');
});
expect(getBufferState(result).text).toBe('ab');
expect(getBufferState(result).cursor).toEqual([0, 2]);
});
it('should handle inserts with a mix of regular and delete characters', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'abcde',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.move('end')); // cursor at the end
expect(getBufferState(result).cursor).toEqual([0, 5]);
act(() => {
result.current.insert('\x7fI\x7f\x7fNEW');
});
expect(getBufferState(result).text).toBe('abcNEW');
expect(getBufferState(result).cursor).toEqual([0, 6]);
});
it('should handle arrow keys for movement', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'ab',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.move('end')); // cursor [0,2]
act(() =>
result.current.handleInput({
name: 'left',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: '\x1b[D',
}),
); // cursor [0,1]
expect(getBufferState(result).cursor).toEqual([0, 1]);
act(() =>
result.current.handleInput({
name: 'right',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: '\x1b[C',
}),
); // cursor [0,2]
expect(getBufferState(result).cursor).toEqual([0, 2]);
});
it('should strip ANSI escape codes when pasting text', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
const textWithAnsi = '\x1B[31mHello\x1B[0m \x1B[32mWorld\x1B[0m';
// Simulate pasting by calling handleInput with a string longer than 1 char
act(() =>
result.current.handleInput({
name: '',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: textWithAnsi,
}),
);
expect(getBufferState(result).text).toBe('Hello World');
});
it('should handle VSCode terminal Shift+Enter as newline', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
act(() =>
result.current.handleInput({
name: 'return',
ctrl: false,
meta: false,
shift: true,
paste: false,
sequence: '\r',
}),
); // Simulates Shift+Enter in VSCode terminal
expect(getBufferState(result).lines).toEqual(['', '']);
});
it('should correctly handle repeated pasting of long text', () => {
const longText = `not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lore
`;
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
// Simulate pasting the long text multiple times
act(() => {
result.current.insert(longText, { paste: true });
result.current.insert(longText, { paste: true });
result.current.insert(longText, { paste: true });
});
const state = getBufferState(result);
// Check that the text is the result of three concatenations.
expect(state.lines).toStrictEqual(
(longText + longText + longText).split('\n'),
);
const expectedCursorPos = offsetToLogicalPos(
state.text,
state.text.length,
);
expect(state.cursor).toEqual(expectedCursorPos);
});
});
// More tests would be needed for:
// - setText, replaceRange
// - deleteWordLeft, deleteWordRight
// - More complex undo/redo scenarios
// - Selection and clipboard (copy/paste) - might need clipboard API mocks or internal state check
// - openInExternalEditor (heavy mocking of fs, child_process, os)
// - All edge cases for visual scrolling and wrapping with different viewport sizes and text content.
describe('replaceRange', () => {
it('should replace a single-line range with single-line text', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: '@pac',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.replaceRange(0, 1, 0, 4, 'packages'));
const state = getBufferState(result);
expect(state.text).toBe('@packages');
expect(state.cursor).toEqual([0, 9]); // cursor after 'typescript'
});
it('should replace a multi-line range with single-line text', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'hello\nworld\nagain',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.replaceRange(0, 2, 1, 3, ' new ')); // replace 'llo\nwor' with ' new '
const state = getBufferState(result);
expect(state.text).toBe('he new ld\nagain');
expect(state.cursor).toEqual([0, 7]); // cursor after ' new '
});
it('should delete a range when replacing with an empty string', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'hello world',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.replaceRange(0, 5, 0, 11, '')); // delete ' world'
const state = getBufferState(result);
expect(state.text).toBe('hello');
expect(state.cursor).toEqual([0, 5]);
});
it('should handle replacing at the beginning of the text', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'world',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.replaceRange(0, 0, 0, 0, 'hello '));
const state = getBufferState(result);
expect(state.text).toBe('hello world');
expect(state.cursor).toEqual([0, 6]);
});
it('should handle replacing at the end of the text', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'hello',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.replaceRange(0, 5, 0, 5, ' world'));
const state = getBufferState(result);
expect(state.text).toBe('hello world');
expect(state.cursor).toEqual([0, 11]);
});
it('should handle replacing the entire buffer content', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'old text',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.replaceRange(0, 0, 0, 8, 'new text'));
const state = getBufferState(result);
expect(state.text).toBe('new text');
expect(state.cursor).toEqual([0, 8]);
});
it('should correctly replace with unicode characters', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'hello *** world',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.replaceRange(0, 6, 0, 9, '你好'));
const state = getBufferState(result);
expect(state.text).toBe('hello 你好 world');
expect(state.cursor).toEqual([0, 8]); // after '你好'
});
it('should handle invalid range by returning false and not changing text', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'test',
viewport,
isValidPath: () => false,
}),
);
act(() => {
result.current.replaceRange(0, 5, 0, 3, 'fail'); // startCol > endCol in same line
});
expect(getBufferState(result).text).toBe('test');
act(() => {
result.current.replaceRange(1, 0, 0, 0, 'fail'); // startRow > endRow
});
expect(getBufferState(result).text).toBe('test');
});
it('replaceRange: multiple lines with a single character', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'first\nsecond\nthird',
viewport,
isValidPath: () => false,
}),
);
act(() => result.current.replaceRange(0, 2, 2, 3, 'X')); // Replace 'rst\nsecond\nthi'
const state = getBufferState(result);
expect(state.text).toBe('fiXrd');
expect(state.cursor).toEqual([0, 3]); // After 'X'
});
it('should replace a single-line range with multi-line text', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: 'one two three',
viewport,
isValidPath: () => false,
}),
);
// Replace "two" with "new\nline"
act(() => result.current.replaceRange(0, 4, 0, 7, 'new\nline'));
const state = getBufferState(result);
expect(state.lines).toEqual(['one new', 'line three']);
expect(state.text).toBe('one new\nline three');
expect(state.cursor).toEqual([1, 4]); // cursor after 'line'
});
});
describe('Input Sanitization', () => {
it('should strip ANSI escape codes from input', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
const textWithAnsi = '\x1B[31mHello\x1B[0m \x1B[32mWorld\x1B[0m';
act(() =>
result.current.handleInput({
name: '',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: textWithAnsi,
}),
);
expect(getBufferState(result).text).toBe('Hello World');
});
it('should strip control characters from input', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
const textWithControlChars = 'H\x07e\x08l\x0Bl\x0Co'; // BELL, BACKSPACE, VT, FF
act(() =>
result.current.handleInput({
name: '',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: textWithControlChars,
}),
);
expect(getBufferState(result).text).toBe('Hello');
});
it('should strip mixed ANSI and control characters from input', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
const textWithMixed = '\u001B[4mH\u001B[0mello';
act(() =>
result.current.handleInput({
name: '',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: textWithMixed,
}),
);
expect(getBufferState(result).text).toBe('Hello');
});
it('should not strip standard characters or newlines', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
const validText = 'Hello World\nThis is a test.';
act(() =>
result.current.handleInput({
name: '',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: validText,
}),
);
expect(getBufferState(result).text).toBe(validText);
});
it('should sanitize pasted text via handleInput', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
const pastedText = '\u001B[4mPasted\u001B[4m Text';
act(() =>
result.current.handleInput({
name: '',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: pastedText,
}),
);
expect(getBufferState(result).text).toBe('Pasted Text');
});
it('should not strip popular emojis', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
const emojis = '🐍🐳🦀🦄';
act(() =>
result.current.handleInput({
name: '',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: emojis,
}),
);
expect(getBufferState(result).text).toBe(emojis);
});
});
describe('stripAnsi', () => {
it('should correctly strip ANSI escape codes', () => {
const textWithAnsi = '\x1B[31mHello\x1B[0m World';
expect(stripAnsi(textWithAnsi)).toBe('Hello World');
});
it('should handle multiple ANSI codes', () => {
const textWithMultipleAnsi = '\x1B[1m\x1B[34mBold Blue\x1B[0m Text';
expect(stripAnsi(textWithMultipleAnsi)).toBe('Bold Blue Text');
});
it('should not modify text without ANSI codes', () => {
const plainText = 'Plain text';
expect(stripAnsi(plainText)).toBe('Plain text');
});
it('should handle empty string', () => {
expect(stripAnsi('')).toBe('');
});
});
});
describe('offsetToLogicalPos', () => {
it('should return [0,0] for offset 0', () => {
expect(offsetToLogicalPos('any text', 0)).toEqual([0, 0]);
});
it('should handle single line text', () => {
const text = 'hello';
expect(offsetToLogicalPos(text, 0)).toEqual([0, 0]); // Start
expect(offsetToLogicalPos(text, 2)).toEqual([0, 2]); // Middle 'l'
expect(offsetToLogicalPos(text, 5)).toEqual([0, 5]); // End
expect(offsetToLogicalPos(text, 10)).toEqual([0, 5]); // Beyond end
});
it('should handle multi-line text', () => {
const text = 'hello\nworld\n123';
// "hello" (5) + \n (1) + "world" (5) + \n (1) + "123" (3)
// h e l l o \n w o r l d \n 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
// Line 0: "hello" (length 5)
expect(offsetToLogicalPos(text, 0)).toEqual([0, 0]); // Start of 'hello'
expect(offsetToLogicalPos(text, 3)).toEqual([0, 3]); // 'l' in 'hello'
expect(offsetToLogicalPos(text, 5)).toEqual([0, 5]); // End of 'hello' (before \n)
// Line 1: "world" (length 5)
expect(offsetToLogicalPos(text, 6)).toEqual([1, 0]); // Start of 'world' (after \n)
expect(offsetToLogicalPos(text, 8)).toEqual([1, 2]); // 'r' in 'world'
expect(offsetToLogicalPos(text, 11)).toEqual([1, 5]); // End of 'world' (before \n)
// Line 2: "123" (length 3)
expect(offsetToLogicalPos(text, 12)).toEqual([2, 0]); // Start of '123' (after \n)
expect(offsetToLogicalPos(text, 13)).toEqual([2, 1]); // '2' in '123'
expect(offsetToLogicalPos(text, 15)).toEqual([2, 3]); // End of '123'
expect(offsetToLogicalPos(text, 20)).toEqual([2, 3]); // Beyond end of text
});
it('should handle empty lines', () => {
const text = 'a\n\nc'; // "a" (1) + \n (1) + "" (0) + \n (1) + "c" (1)
expect(offsetToLogicalPos(text, 0)).toEqual([0, 0]); // 'a'
expect(offsetToLogicalPos(text, 1)).toEqual([0, 1]); // End of 'a'
expect(offsetToLogicalPos(text, 2)).toEqual([1, 0]); // Start of empty line
expect(offsetToLogicalPos(text, 3)).toEqual([2, 0]); // Start of 'c'
expect(offsetToLogicalPos(text, 4)).toEqual([2, 1]); // End of 'c'
});
it('should handle text ending with a newline', () => {
const text = 'hello\n'; // "hello" (5) + \n (1)
expect(offsetToLogicalPos(text, 5)).toEqual([0, 5]); // End of 'hello'
expect(offsetToLogicalPos(text, 6)).toEqual([1, 0]); // Position on the new empty line after
expect(offsetToLogicalPos(text, 7)).toEqual([1, 0]); // Still on the new empty line
});
it('should handle text starting with a newline', () => {
const text = '\nhello'; // "" (0) + \n (1) + "hello" (5)
expect(offsetToLogicalPos(text, 0)).toEqual([0, 0]); // Start of first empty line
expect(offsetToLogicalPos(text, 1)).toEqual([1, 0]); // Start of 'hello'
expect(offsetToLogicalPos(text, 3)).toEqual([1, 2]); // 'l' in 'hello'
});
it('should handle empty string input', () => {
expect(offsetToLogicalPos('', 0)).toEqual([0, 0]);
expect(offsetToLogicalPos('', 5)).toEqual([0, 0]);
});
it('should handle multi-byte unicode characters correctly', () => {
const text = '你好\n世界'; // "你好" (2 chars) + \n (1) + "世界" (2 chars)
// Total "code points" for offset calculation: 2 + 1 + 2 = 5
expect(offsetToLogicalPos(text, 0)).toEqual([0, 0]); // Start of '你好'
expect(offsetToLogicalPos(text, 1)).toEqual([0, 1]); // After '你', before '好'
expect(offsetToLogicalPos(text, 2)).toEqual([0, 2]); // End of '你好'
expect(offsetToLogicalPos(text, 3)).toEqual([1, 0]); // Start of '世界'
expect(offsetToLogicalPos(text, 4)).toEqual([1, 1]); // After '世', before '界'
expect(offsetToLogicalPos(text, 5)).toEqual([1, 2]); // End of '世界'
expect(offsetToLogicalPos(text, 6)).toEqual([1, 2]); // Beyond end
});
it('should handle offset exactly at newline character', () => {
const text = 'abc\ndef';
// a b c \n d e f
// 0 1 2 3 4 5 6
expect(offsetToLogicalPos(text, 3)).toEqual([0, 3]); // End of 'abc'
// The next character is the newline, so an offset of 4 means the start of the next line.
expect(offsetToLogicalPos(text, 4)).toEqual([1, 0]); // Start of 'def'
});
it('should handle offset in the middle of a multi-byte character (should place at start of that char)', () => {
// This scenario is tricky as "offset" is usually character-based.
// Assuming cpLen and related logic handles this by treating multi-byte as one unit.
// The current implementation of offsetToLogicalPos uses cpLen, so it should be code-point aware.
const text = '🐶🐱'; // 2 code points
expect(offsetToLogicalPos(text, 0)).toEqual([0, 0]);
expect(offsetToLogicalPos(text, 1)).toEqual([0, 1]); // After 🐶
expect(offsetToLogicalPos(text, 2)).toEqual([0, 2]); // After 🐱
});
});
describe('logicalPosToOffset', () => {
it('should convert row/col position to offset correctly', () => {
const lines = ['hello', 'world', '123'];
// Line 0: "hello" (5 chars)
expect(logicalPosToOffset(lines, 0, 0)).toBe(0); // Start of 'hello'
expect(logicalPosToOffset(lines, 0, 3)).toBe(3); // 'l' in 'hello'
expect(logicalPosToOffset(lines, 0, 5)).toBe(5); // End of 'hello'
// Line 1: "world" (5 chars), offset starts at 6 (5 + 1 for newline)
expect(logicalPosToOffset(lines, 1, 0)).toBe(6); // Start of 'world'
expect(logicalPosToOffset(lines, 1, 2)).toBe(8); // 'r' in 'world'
expect(logicalPosToOffset(lines, 1, 5)).toBe(11); // End of 'world'
// Line 2: "123" (3 chars), offset starts at 12 (5 + 1 + 5 + 1)
expect(logicalPosToOffset(lines, 2, 0)).toBe(12); // Start of '123'
expect(logicalPosToOffset(lines, 2, 1)).toBe(13); // '2' in '123'
expect(logicalPosToOffset(lines, 2, 3)).toBe(15); // End of '123'
});
it('should handle empty lines', () => {
const lines = ['a', '', 'c'];
expect(logicalPosToOffset(lines, 0, 0)).toBe(0); // 'a'
expect(logicalPosToOffset(lines, 0, 1)).toBe(1); // End of 'a'
expect(logicalPosToOffset(lines, 1, 0)).toBe(2); // Empty line
expect(logicalPosToOffset(lines, 2, 0)).toBe(3); // 'c'
expect(logicalPosToOffset(lines, 2, 1)).toBe(4); // End of 'c'
});
it('should handle single empty line', () => {
const lines = [''];
expect(logicalPosToOffset(lines, 0, 0)).toBe(0);
});
it('should be inverse of offsetToLogicalPos', () => {
const lines = ['hello', 'world', '123'];
const text = lines.join('\n');
// Test round-trip conversion
for (let offset = 0; offset <= text.length; offset++) {
const [row, col] = offsetToLogicalPos(text, offset);
const convertedOffset = logicalPosToOffset(lines, row, col);
expect(convertedOffset).toBe(offset);
}
});
it('should handle out-of-bounds positions', () => {
const lines = ['hello'];
// Beyond end of line
expect(logicalPosToOffset(lines, 0, 10)).toBe(5); // Clamps to end of line
// Beyond array bounds - should clamp to the last line
expect(logicalPosToOffset(lines, 5, 0)).toBe(0); // Clamps to start of last line (row 0)
expect(logicalPosToOffset(lines, 5, 10)).toBe(5); // Clamps to end of last line
});
});
describe('textBufferReducer vim operations', () => {
describe('vim_delete_line', () => {
it('should delete a single line including newline in multi-line text', () => {
const initialState: TextBufferState = {
lines: ['line1', 'line2', 'line3'],
cursorRow: 1,
cursorCol: 2,
preferredCol: null,
visualLines: [['line1'], ['line2'], ['line3']],
visualScrollRow: 0,
visualCursor: { row: 1, col: 2 },
viewport: { width: 10, height: 5 },
undoStack: [],
redoStack: [],
};
const action: TextBufferAction = {
type: 'vim_delete_line',
payload: { count: 1 },
};
const result = textBufferReducer(initialState, action);
expect(result).toHaveOnlyValidCharacters();
// After deleting line2, we should have line1 and line3, with cursor on line3 (now at index 1)
expect(result.lines).toEqual(['line1', 'line3']);
expect(result.cursorRow).toBe(1);
expect(result.cursorCol).toBe(0);
});
it('should delete multiple lines when count > 1', () => {
const initialState: TextBufferState = {
lines: ['line1', 'line2', 'line3', 'line4'],
cursorRow: 1,
cursorCol: 0,
preferredCol: null,
visualLines: [['line1'], ['line2'], ['line3'], ['line4']],
visualScrollRow: 0,
visualCursor: { row: 1, col: 0 },
viewport: { width: 10, height: 5 },
undoStack: [],
redoStack: [],
};
const action: TextBufferAction = {
type: 'vim_delete_line',
payload: { count: 2 },
};
const result = textBufferReducer(initialState, action);
expect(result).toHaveOnlyValidCharacters();
// Should delete line2 and line3, leaving line1 and line4
expect(result.lines).toEqual(['line1', 'line4']);
expect(result.cursorRow).toBe(1);
expect(result.cursorCol).toBe(0);
});
it('should clear single line content when only one line exists', () => {
const initialState: TextBufferState = {
lines: ['only line'],
cursorRow: 0,
cursorCol: 5,
preferredCol: null,
visualLines: [['only line']],
visualScrollRow: 0,
visualCursor: { row: 0, col: 5 },
viewport: { width: 10, height: 5 },
undoStack: [],
redoStack: [],
};
const action: TextBufferAction = {
type: 'vim_delete_line',
payload: { count: 1 },
};
const result = textBufferReducer(initialState, action);
expect(result).toHaveOnlyValidCharacters();
// Should clear the line content but keep the line
expect(result.lines).toEqual(['']);
expect(result.cursorRow).toBe(0);
expect(result.cursorCol).toBe(0);
});
it('should handle deleting the last line properly', () => {
const initialState: TextBufferState = {
lines: ['line1', 'line2'],
cursorRow: 1,
cursorCol: 0,
preferredCol: null,
visualLines: [['line1'], ['line2']],
visualScrollRow: 0,
visualCursor: { row: 1, col: 0 },
viewport: { width: 10, height: 5 },
undoStack: [],
redoStack: [],
};
const action: TextBufferAction = {
type: 'vim_delete_line',
payload: { count: 1 },
};
const result = textBufferReducer(initialState, action);
expect(result).toHaveOnlyValidCharacters();
// Should delete the last line completely, not leave empty line
expect(result.lines).toEqual(['line1']);
expect(result.cursorRow).toBe(0);
expect(result.cursorCol).toBe(0);
});
it('should handle deleting all lines and maintain valid state for subsequent paste', () => {
const initialState: TextBufferState = {
lines: ['line1', 'line2', 'line3', 'line4'],
cursorRow: 0,
cursorCol: 0,
preferredCol: null,
visualLines: [['line1'], ['line2'], ['line3'], ['line4']],
visualScrollRow: 0,
visualCursor: { row: 0, col: 0 },
viewport: { width: 10, height: 5 },
undoStack: [],
redoStack: [],
};
// Delete all 4 lines with 4dd
const deleteAction: TextBufferAction = {
type: 'vim_delete_line',
payload: { count: 4 },
};
const afterDelete = textBufferReducer(initialState, deleteAction);
expect(afterDelete).toHaveOnlyValidCharacters();
// After deleting all lines, should have one empty line
expect(afterDelete.lines).toEqual(['']);
expect(afterDelete.cursorRow).toBe(0);
expect(afterDelete.cursorCol).toBe(0);
// Now paste multiline content - this should work correctly
const pasteAction: TextBufferAction = {
type: 'insert',
payload: 'new1\nnew2\nnew3\nnew4',
};
const afterPaste = textBufferReducer(afterDelete, pasteAction);
expect(afterPaste).toHaveOnlyValidCharacters();
// All lines including the first one should be present
expect(afterPaste.lines).toEqual(['new1', 'new2', 'new3', 'new4']);
expect(afterPaste.cursorRow).toBe(3);
expect(afterPaste.cursorCol).toBe(4);
});
});
});
describe('Unicode helper functions', () => {
describe('findWordEndInLine with Unicode', () => {
it('should handle combining characters', () => {
// café with combining accent
const cafeWithCombining = 'cafe\u0301';
const result = findWordEndInLine(cafeWithCombining + ' test', 0);
expect(result).toBe(3); // End of 'café' at base character 'e', not combining accent
});
it('should handle precomposed characters with diacritics', () => {
// café with precomposed é (U+00E9)
const cafePrecomposed = 'café';
const result = findWordEndInLine(cafePrecomposed + ' test', 0);
expect(result).toBe(3); // End of 'café' at precomposed character 'é'
});
it('should return null when no word end found', () => {
const result = findWordEndInLine(' ', 0);
expect(result).toBeNull(); // No word end found in whitespace-only string string
});
});
describe('findNextWordStartInLine with Unicode', () => {
it('should handle right-to-left text', () => {
const result = findNextWordStartInLine('hello مرحبا world', 0);
expect(result).toBe(6); // Start of Arabic word
});
it('should handle Chinese characters', () => {
const result = findNextWordStartInLine('hello 你好 world', 0);
expect(result).toBe(6); // Start of Chinese word
});
it('should return null at end of line', () => {
const result = findNextWordStartInLine('hello', 10);
expect(result).toBeNull();
});
it('should handle combining characters', () => {
// café with combining accent + next word
const textWithCombining = 'cafe\u0301 test';
const result = findNextWordStartInLine(textWithCombining, 0);
expect(result).toBe(6); // Start of 'test' after 'café ' (combining char makes string longer)
});
it('should handle precomposed characters with diacritics', () => {
// café with precomposed é + next word
const textPrecomposed = 'café test';
const result = findNextWordStartInLine(textPrecomposed, 0);
expect(result).toBe(5); // Start of 'test' after 'café '
});
});
describe('isWordCharStrict with Unicode', () => {
it('should return true for ASCII word characters', () => {
expect(isWordCharStrict('a')).toBe(true);
expect(isWordCharStrict('Z')).toBe(true);
expect(isWordCharStrict('0')).toBe(true);
expect(isWordCharStrict('_')).toBe(true);
});
it('should return false for punctuation', () => {
expect(isWordCharStrict('.')).toBe(false);
expect(isWordCharStrict(',')).toBe(false);
expect(isWordCharStrict('!')).toBe(false);
});
it('should return true for non-Latin scripts', () => {
expect(isWordCharStrict('你')).toBe(true); // Chinese character
expect(isWordCharStrict('م')).toBe(true); // Arabic character
});
it('should return false for whitespace', () => {
expect(isWordCharStrict(' ')).toBe(false);
expect(isWordCharStrict('\t')).toBe(false);
});
});
describe('cpLen with Unicode', () => {
it('should handle combining characters', () => {
expect(cpLen('é')).toBe(1); // Precomposed
expect(cpLen('e\u0301')).toBe(2); // e + combining acute
});
it('should handle Chinese and Arabic text', () => {
expect(cpLen('hello 你好 world')).toBe(14); // 5 + 1 + 2 + 1 + 5 = 14
expect(cpLen('hello مرحبا world')).toBe(17);
});
});
});
|