|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { DiffRenderer } from '../src/renderer/DiffRenderer'; |
| 3 | +import { VisualRow } from '../src/renderer/Renderer'; |
| 4 | + |
| 5 | +describe('DiffRenderer.insertSeparators', () => { |
| 6 | + it('should add gaps at start and end of file when appropriate', () => { |
| 7 | + const renderer = new DiffRenderer({} as any, {} as any, {} as any); |
| 8 | + renderer.setFocusedDiffMode(true); |
| 9 | + |
| 10 | + // Case 1: Gap at start only (line 0 is hidden, lines 1 and 2 are visible) |
| 11 | + const rows1: VisualRow[] = [ |
| 12 | + { kind: 'real', lineIndex: 1 }, |
| 13 | + { kind: 'real', lineIndex: 2 }, |
| 14 | + ]; |
| 15 | + const result1 = renderer.insertSeparators(rows1, 3); |
| 16 | + expect(result1).toEqual([ |
| 17 | + { kind: 'separator', hiddenStart: 0, hiddenEnd: 0, hiddenCount: 1 }, |
| 18 | + { kind: 'real', lineIndex: 1 }, |
| 19 | + { kind: 'real', lineIndex: 2 }, |
| 20 | + ]); |
| 21 | + |
| 22 | + // Case 2: Gap at end only (lines 0 and 1 are visible, line 2 is hidden) |
| 23 | + const rows2: VisualRow[] = [ |
| 24 | + { kind: 'real', lineIndex: 0 }, |
| 25 | + { kind: 'real', lineIndex: 1 }, |
| 26 | + ]; |
| 27 | + const result2 = renderer.insertSeparators(rows2, 3); |
| 28 | + expect(result2).toEqual([ |
| 29 | + { kind: 'real', lineIndex: 0 }, |
| 30 | + { kind: 'real', lineIndex: 1 }, |
| 31 | + { kind: 'separator', hiddenStart: 2, hiddenEnd: 2, hiddenCount: 1 }, |
| 32 | + ]); |
| 33 | + |
| 34 | + // Case 3: Gaps at both start and end |
| 35 | + const rows3: VisualRow[] = [ |
| 36 | + { kind: 'real', lineIndex: 2 }, |
| 37 | + { kind: 'real', lineIndex: 3 }, |
| 38 | + ]; |
| 39 | + const result3 = renderer.insertSeparators(rows3, 6); |
| 40 | + expect(result3).toEqual([ |
| 41 | + { kind: 'separator', hiddenStart: 0, hiddenEnd: 1, hiddenCount: 2 }, |
| 42 | + { kind: 'real', lineIndex: 2 }, |
| 43 | + { kind: 'real', lineIndex: 3 }, |
| 44 | + { kind: 'separator', hiddenStart: 4, hiddenEnd: 5, hiddenCount: 2 }, |
| 45 | + ]); |
| 46 | + |
| 47 | + // Case 4: No gaps (first and last lines are visible) |
| 48 | + const rows4: VisualRow[] = [ |
| 49 | + { kind: 'real', lineIndex: 0 }, |
| 50 | + { kind: 'real', lineIndex: 1 }, |
| 51 | + { kind: 'real', lineIndex: 2 }, |
| 52 | + ]; |
| 53 | + const result4 = renderer.insertSeparators(rows4, 3); |
| 54 | + expect(result4).toEqual([ |
| 55 | + { kind: 'real', lineIndex: 0 }, |
| 56 | + { kind: 'real', lineIndex: 1 }, |
| 57 | + { kind: 'real', lineIndex: 2 }, |
| 58 | + ]); |
| 59 | + |
| 60 | + // Case 5: Empty file |
| 61 | + const rows5: VisualRow[] = []; |
| 62 | + const result5 = renderer.insertSeparators(rows5, 0); |
| 63 | + expect(result5).toEqual([]); |
| 64 | + |
| 65 | + // Case 6: Entire file hidden |
| 66 | + const rows6: VisualRow[] = []; |
| 67 | + const result6 = renderer.insertSeparators(rows6, 5); |
| 68 | + expect(result6).toEqual([ |
| 69 | + { kind: 'separator', hiddenStart: 0, hiddenEnd: 4, hiddenCount: 5 }, |
| 70 | + ]); |
| 71 | + |
| 72 | + // Case 7: Only ghost rows in input (entire file is hidden, but ghost rows exist) |
| 73 | + const rows7: VisualRow[] = [ |
| 74 | + { kind: 'ghost', hunkId: 0, anchorLine: 1, originalLineIndex: 0 }, |
| 75 | + ]; |
| 76 | + const result7 = renderer.insertSeparators(rows7, 3); |
| 77 | + expect(result7).toEqual([ |
| 78 | + { kind: 'ghost', hunkId: 0, anchorLine: 1, originalLineIndex: 0 }, |
| 79 | + { kind: 'separator', hiddenStart: 0, hiddenEnd: 2, hiddenCount: 3 }, |
| 80 | + ]); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
0 commit comments