|
1 | 1 | import { describe, test, expect } from 'bun:test' |
2 | 2 |
|
3 | | -import { computeInputLayoutMetrics } from '../text-layout' |
| 3 | +import { computeInputLayoutMetrics, getLastNVisualLines } from '../text-layout' |
4 | 4 |
|
5 | 5 | describe('computeInputLayoutMetrics', () => { |
6 | 6 | test('single-line content keeps height at 1 without gutter', () => { |
@@ -80,3 +80,39 @@ describe('computeInputLayoutMetrics', () => { |
80 | 80 | expect(metrics.gutterEnabled).toBe(false) |
81 | 81 | }) |
82 | 82 | }) |
| 83 | + |
| 84 | +describe('getLastNVisualLines', () => { |
| 85 | + test('returns empty array when n or cols <= 0 or text is empty', () => { |
| 86 | + expect(getLastNVisualLines('', 40, 5)).toEqual({ lines: [], hasMore: false }) |
| 87 | + expect(getLastNVisualLines('hello', 0, 5)).toEqual({ lines: [], hasMore: false }) |
| 88 | + expect(getLastNVisualLines('hello', 40, 0)).toEqual({ lines: [], hasMore: false }) |
| 89 | + }) |
| 90 | + |
| 91 | + test('returns all lines without hasMore when line count <= n', () => { |
| 92 | + const text = 'line 1\nline 2\nline 3' |
| 93 | + const result = getLastNVisualLines(text, 40, 5) |
| 94 | + expect(result.lines).toEqual(['line 1', 'line 2', 'line 3']) |
| 95 | + expect(result.hasMore).toBe(false) |
| 96 | + }) |
| 97 | + |
| 98 | + test('returns only last n lines with hasMore = true when line count > n', () => { |
| 99 | + const text = 'line 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7' |
| 100 | + const result = getLastNVisualLines(text, 40, 3) |
| 101 | + expect(result.lines).toEqual(['line 5', 'line 6', 'line 7']) |
| 102 | + expect(result.hasMore).toBe(true) |
| 103 | + }) |
| 104 | + |
| 105 | + test('correctly bounds array when text has hundreds of wrapped lines', () => { |
| 106 | + const text = Array.from({ length: 100 }, (_, i) => `Line ${i}`).join('\n') |
| 107 | + const result = getLastNVisualLines(text, 40, 4) |
| 108 | + expect(result.lines).toEqual(['Line 96', 'Line 97', 'Line 98', 'Line 99']) |
| 109 | + expect(result.hasMore).toBe(true) |
| 110 | + }) |
| 111 | + |
| 112 | + test('wraps long lines exceeding column width', () => { |
| 113 | + const text = 'supercalifragilisticexpialidocious' |
| 114 | + const result = getLastNVisualLines(text, 10, 2) |
| 115 | + expect(result.lines.length).toBe(2) |
| 116 | + expect(result.hasMore).toBe(true) |
| 117 | + }) |
| 118 | +}) |
0 commit comments