-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.test.ts
More file actions
213 lines (183 loc) · 7.37 KB
/
Copy pathdiff.test.ts
File metadata and controls
213 lines (183 loc) · 7.37 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
import { describe, test, expect } from 'bun:test';
import { computeDiff, computeLineDiff } from '../core/diff/diff-compute';
import { mergeAdjacentHunks, splitHunk, navigateHunks } from '../core/diff/hunk';
import { computeInlineDiff } from '../core/diff/inline-diff';
import type { DiffHunk } from '../core/diff/diff-model';
describe('computeDiff', () => {
test('identical texts produce no hunks', async () => {
const result = await computeDiff('hello\nworld', 'hello\nworld');
expect(result.hunks.length).toBe(0);
expect(result.totalAdded).toBe(0);
expect(result.totalDeleted).toBe(0);
});
test('empty original to non-empty modified', async () => {
const result = await computeDiff('', 'hello\nworld');
expect(result.hunks.length).toBe(1);
expect(result.hunks[0].type).toBe('add');
expect(result.totalAdded).toBe(2);
});
test('non-empty original to empty modified', async () => {
const result = await computeDiff('hello\nworld', '');
expect(result.hunks.length).toBe(1);
expect(result.hunks[0].type).toBe('delete');
expect(result.totalDeleted).toBe(2);
});
test('single line addition', async () => {
const result = await computeDiff('a\nc', 'a\nb\nc');
expect(result.totalAdded).toBe(1);
expect(result.hunks.length).toBe(1);
expect(result.hunks[0].type).toBe('add');
});
test('single line deletion', async () => {
const result = await computeDiff('a\nb\nc', 'a\nc');
expect(result.totalDeleted).toBe(1);
expect(result.hunks.length).toBe(1);
expect(result.hunks[0].type).toBe('delete');
});
test('modification (delete + add)', async () => {
const result = await computeDiff('a\nb\nc', 'a\nB\nc');
expect(result.hunks.length).toBe(1);
expect(result.hunks[0].type).toBe('modify');
});
test('both empty produces no hunks', async () => {
const result = await computeDiff('', '');
expect(result.hunks.length).toBe(0);
});
test('multi-hunk diff', async () => {
const original = 'a\nb\nc\nd\ne\nf\ng';
const modified = 'a\nB\nc\nd\ne\nF\ng';
const result = await computeDiff(original, modified);
expect(result.hunks.length).toBe(2);
});
test('hunks have pending state', async () => {
const result = await computeDiff('a', 'b');
for (const hunk of result.hunks) {
expect(hunk.state).toBe('pending');
}
});
});
describe('computeLineDiff', () => {
test('array-based diff', async () => {
const result = await computeLineDiff(['a', 'b', 'c'], ['a', 'c']);
expect(result.totalDeleted).toBe(1);
expect(result.hunks[0].type).toBe('delete');
});
});
describe('mergeAdjacentHunks', () => {
test('merges hunks within context range', () => {
const hunks: DiffHunk[] = [
{ type: 'delete', originalRange: { startLine: 1, endLine: 2 }, modifiedRange: { startLine: 1, endLine: 1 }, state: 'pending' },
{ type: 'add', originalRange: { startLine: 4, endLine: 4 }, modifiedRange: { startLine: 3, endLine: 4 }, state: 'pending' },
];
const merged = mergeAdjacentHunks(hunks, 3);
expect(merged.length).toBe(1);
expect(merged[0].type).toBe('modify');
});
test('does not merge distant hunks', () => {
const hunks: DiffHunk[] = [
{ type: 'delete', originalRange: { startLine: 1, endLine: 2 }, modifiedRange: { startLine: 1, endLine: 1 }, state: 'pending' },
{ type: 'add', originalRange: { startLine: 20, endLine: 20 }, modifiedRange: { startLine: 19, endLine: 20 }, state: 'pending' },
];
const merged = mergeAdjacentHunks(hunks, 3);
expect(merged.length).toBe(2);
});
test('single hunk returns copy', () => {
const hunks: DiffHunk[] = [
{ type: 'add', originalRange: { startLine: 0, endLine: 0 }, modifiedRange: { startLine: 0, endLine: 1 }, state: 'pending' },
];
const merged = mergeAdjacentHunks(hunks);
expect(merged.length).toBe(1);
});
test('empty array returns empty', () => {
expect(mergeAdjacentHunks([])).toEqual([]);
});
});
describe('splitHunk', () => {
test('splits hunk at line boundary', () => {
const hunk: DiffHunk = {
type: 'modify',
originalRange: { startLine: 0, endLine: 10 },
modifiedRange: { startLine: 0, endLine: 10 },
state: 'pending',
};
const result = splitHunk(hunk, 5);
expect(result).not.toBeNull();
expect(result![0].originalRange.endLine).toBe(5);
expect(result![1].originalRange.startLine).toBe(5);
});
test('returns null for out-of-range split', () => {
const hunk: DiffHunk = {
type: 'modify',
originalRange: { startLine: 0, endLine: 10 },
modifiedRange: { startLine: 0, endLine: 10 },
state: 'pending',
};
expect(splitHunk(hunk, 0)).toBeNull();
expect(splitHunk(hunk, 10)).toBeNull();
expect(splitHunk(hunk, 15)).toBeNull();
});
});
describe('navigateHunks', () => {
const hunks: DiffHunk[] = [
{ type: 'modify', originalRange: { startLine: 5, endLine: 8 }, modifiedRange: { startLine: 5, endLine: 8 }, state: 'pending' },
{ type: 'modify', originalRange: { startLine: 15, endLine: 18 }, modifiedRange: { startLine: 15, endLine: 18 }, state: 'pending' },
{ type: 'modify', originalRange: { startLine: 25, endLine: 28 }, modifiedRange: { startLine: 25, endLine: 28 }, state: 'pending' },
];
test('next finds next hunk after line', () => {
const h = navigateHunks(hunks, 10, 'next');
expect(h!.originalRange.startLine).toBe(15);
});
test('next wraps around', () => {
const h = navigateHunks(hunks, 30, 'next');
expect(h!.originalRange.startLine).toBe(5);
});
test('prev finds previous hunk before line', () => {
const h = navigateHunks(hunks, 20, 'prev');
expect(h!.originalRange.startLine).toBe(15);
});
test('prev wraps around', () => {
const h = navigateHunks(hunks, 2, 'prev');
expect(h!.originalRange.startLine).toBe(25);
});
test('empty hunks returns null', () => {
expect(navigateHunks([], 0, 'next')).toBeNull();
expect(navigateHunks([], 0, 'prev')).toBeNull();
});
});
describe('computeInlineDiff', () => {
test('identical lines', () => {
const segments = computeInlineDiff('hello world', 'hello world');
expect(segments.length).toBe(1);
expect(segments[0].type).toBe('unchanged');
expect(segments[0].text).toBe('hello world');
});
test('empty original', () => {
const segments = computeInlineDiff('', 'hello');
expect(segments.length).toBe(1);
expect(segments[0].type).toBe('added');
});
test('empty modified', () => {
const segments = computeInlineDiff('hello', '');
expect(segments.length).toBe(1);
expect(segments[0].type).toBe('deleted');
});
test('character-level changes', () => {
const segments = computeInlineDiff('hello world', 'hello earth');
const unchangedCount = segments.filter(s => s.type === 'unchanged').length;
const changedCount = segments.filter(s => s.type !== 'unchanged').length;
expect(unchangedCount).toBeGreaterThan(0);
expect(changedCount).toBeGreaterThan(0);
});
test('insertion in middle', () => {
const segments = computeInlineDiff('abc', 'aXbc');
const added = segments.filter(s => s.type === 'added');
expect(added.length).toBe(1);
expect(added[0].text).toBe('X');
});
test('deletion in middle', () => {
const segments = computeInlineDiff('aXbc', 'abc');
const deleted = segments.filter(s => s.type === 'deleted');
expect(deleted.length).toBe(1);
expect(deleted[0].text).toBe('X');
});
});