-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathdiff.test.ts
More file actions
343 lines (309 loc) · 11.1 KB
/
Copy pathdiff.test.ts
File metadata and controls
343 lines (309 loc) · 11.1 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
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { extractRelativePath, computeFileModifications, diffFiles, fileModificationsToHTML } from '~/utils/diff';
import { WORK_DIR } from '~/utils/constants';
import type { FileMap } from '~/lib/stores/files';
describe('extractRelativePath', () => {
beforeEach(() => {
vi.spyOn(console, 'log').mockImplementation(() => { });
vi.spyOn(console, 'error').mockImplementation(() => { });
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should strip out WORK_DIR from file paths', () => {
const filePath = `${WORK_DIR}/src/components/Button.tsx`;
const result = extractRelativePath(filePath);
expect(result).toBe('src/components/Button.tsx');
});
it('should handle nested directories', () => {
const filePath = `${WORK_DIR}/app/lib/stores/chat.ts`;
const result = extractRelativePath(filePath);
expect(result).toBe('app/lib/stores/chat.ts');
});
it('should handle root level files', () => {
const filePath = `${WORK_DIR}/package.json`;
const result = extractRelativePath(filePath);
expect(result).toBe('package.json');
});
it('should return original path if WORK_DIR is not at the start', () => {
const filePath = '/some/other/path/file.ts';
const result = extractRelativePath(filePath);
expect(result).toBe('/some/other/path/file.ts');
});
it('should handle empty WORK_DIR', () => {
const originalWorkDir = WORK_DIR;
// Temporarily mock WORK_DIR as empty
(global as any).WORK_DIR = '';
const filePath = '/app/src/main.ts';
const result = extractRelativePath(filePath);
expect(result).toBe('/app/src/main.ts');
// Restore original value
(global as any).WORK_DIR = originalWorkDir;
});
});
describe('diffFiles', () => {
it('should generate unified diff for changed content', () => {
const oldContent = 'Hello World';
const newContent = 'Hello TypeScript';
const result = diffFiles('test.ts', oldContent, newContent);
expect(result).toBeDefined();
expect(result).toContain('-Hello World');
expect(result).toContain('+Hello TypeScript');
});
it('should return undefined for identical files', () => {
const content = 'Hello World';
const result = diffFiles('test.ts', content, content);
expect(result).toBeUndefined();
});
it('should handle multiline changes', () => {
const oldContent = 'line1\nline2\nline3';
const newContent = 'line1\nmodified line2\nline3';
const result = diffFiles('test.ts', oldContent, newContent);
expect(result).toBeDefined();
expect(result).toContain('-line2');
expect(result).toContain('+modified line2');
});
it('should handle added lines', () => {
const oldContent = 'line1\nline2';
const newContent = 'line1\nline2\nline3';
const result = diffFiles('test.ts', oldContent, newContent);
expect(result).toBeDefined();
expect(result).toContain('+line3');
});
it('should handle removed lines', () => {
const oldContent = 'line1\nline2\nline3';
const newContent = 'line1\nline3';
const result = diffFiles('test.ts', oldContent, newContent);
expect(result).toBeDefined();
expect(result).toContain('-line2');
});
it('should not include file header in diff', () => {
const oldContent = 'old content';
const newContent = 'new content';
const result = diffFiles('test.ts', oldContent, newContent);
expect(result).toBeDefined();
expect(result).not.toContain('--- test.ts');
expect(result).not.toContain('+++ test.ts');
});
it('should handle empty old content', () => {
const oldContent = '';
const newContent = 'new content';
const result = diffFiles('test.ts', oldContent, newContent);
expect(result).toBeDefined();
expect(result).toContain('+new content');
});
it('should handle empty new content', () => {
const oldContent = 'old content';
const newContent = '';
const result = diffFiles('test.ts', oldContent, newContent);
expect(result).toBeDefined();
expect(result).toContain('-old content');
});
it('should handle complex code changes', () => {
const oldContent = `function hello() {
console.log("old");
}`;
const newContent = `function hello() {
console.log("new");
return true;
}`;
const result = diffFiles('test.ts', oldContent, newContent);
expect(result).toBeDefined();
expect(result).toContain('- console.log("old");');
expect(result).toContain('+ console.log("new");');
expect(result).toContain('+ return true;');
});
});
describe('computeFileModifications', () => {
it('should compute modifications for changed files', () => {
const files: FileMap = {
'/home/project/test.ts': {
type: 'file',
content: 'new content',
isBinary: false,
},
};
const modifiedFiles = new Map([['/home/project/test.ts', 'old content']]);
const result = computeFileModifications(files, modifiedFiles);
expect(result).toBeDefined();
expect(result).toHaveProperty('/home/project/test.ts');
});
it('should return undefined when no files modified', () => {
const files: FileMap = {
'/home/project/test.ts': {
type: 'file',
content: 'same content',
isBinary: false,
},
};
const modifiedFiles = new Map([['/home/project/test.ts', 'same content']]);
const result = computeFileModifications(files, modifiedFiles);
expect(result).toBeUndefined();
});
it('should choose appropriate type based on size optimization', () => {
const files: FileMap = {
'/home/project/test.ts': {
type: 'file',
content: 'new content',
isBinary: false,
},
};
const modifiedFiles = new Map([['/home/project/test.ts', 'old content']]);
const result = computeFileModifications(files, modifiedFiles);
expect(result).toBeDefined();
expect(result?.['/home/project/test.ts']?.type).toMatch(/^(file|diff)$/);
expect(result?.['/home/project/test.ts']?.content).toBeDefined();
});
it('should optimize by using smaller representation', () => {
const files: FileMap = {
'/home/project/small.ts': {
type: 'file',
content: 'x',
isBinary: false,
},
};
const modifiedFiles = new Map([['/home/project/small.ts', 'y']]);
const result = computeFileModifications(files, modifiedFiles);
expect(result).toBeDefined();
const mod = result?.['/home/project/small.ts'];
expect(mod).toBeDefined();
expect(['file', 'diff']).toContain(mod?.type);
});
it('should skip folders', () => {
const files: FileMap = {
'/home/project/src': {
type: 'folder',
},
};
const modifiedFiles = new Map([['/home/project/src', 'some content']]);
const result = computeFileModifications(files, modifiedFiles);
expect(result).toBeUndefined();
});
it('should handle multiple modified files', () => {
const files: FileMap = {
'/home/project/file1.ts': {
type: 'file',
content: 'new content 1',
isBinary: false,
},
'/home/project/file2.ts': {
type: 'file',
content: 'new content 2',
isBinary: false,
},
};
const modifiedFiles = new Map([
['/home/project/file1.ts', 'old content 1'],
['/home/project/file2.ts', 'old content 2'],
]);
const result = computeFileModifications(files, modifiedFiles);
expect(result).toBeDefined();
expect(Object.keys(result!)).toHaveLength(2);
expect(result).toHaveProperty('/home/project/file1.ts');
expect(result).toHaveProperty('/home/project/file2.ts');
});
it('should ignore non-existent files in modifications map', () => {
const files: FileMap = {
'/home/project/exists.ts': {
type: 'file',
content: 'new content',
isBinary: false,
},
};
const modifiedFiles = new Map([
['/home/project/exists.ts', 'old content'],
['/home/project/nonexistent.ts', 'old content'],
]);
const result = computeFileModifications(files, modifiedFiles);
expect(result).toBeDefined();
expect(Object.keys(result!)).toHaveLength(1);
expect(result).toHaveProperty('/home/project/exists.ts');
});
});
describe('fileModificationsToHTML', () => {
it('should convert modifications to HTML format', () => {
const modifications = {
'/home/project/test.ts': {
type: 'diff' as const,
content: '@@ -1,1 +1,1 @@\n-old\n+new',
},
};
const result = fileModificationsToHTML(modifications);
expect(result).toBeDefined();
expect(result).toContain('<codinit_file_modifications>');
expect(result).toContain('</codinit_file_modifications>');
expect(result).toContain('<diff path="/home/project/test.ts">');
expect(result).toContain('</diff>');
});
it('should return undefined for empty modifications', () => {
const modifications = {};
const result = fileModificationsToHTML(modifications);
expect(result).toBeUndefined();
});
it('should handle file type modifications', () => {
const modifications = {
'/home/project/test.ts': {
type: 'file' as const,
content: 'complete file content',
},
};
const result = fileModificationsToHTML(modifications);
expect(result).toBeDefined();
expect(result).toContain('<file path="/home/project/test.ts">');
expect(result).toContain('complete file content');
expect(result).toContain('</file>');
});
it('should handle multiple modifications', () => {
const modifications = {
'/home/project/file1.ts': {
type: 'diff' as const,
content: 'diff content 1',
},
'/home/project/file2.ts': {
type: 'file' as const,
content: 'file content 2',
},
};
const result = fileModificationsToHTML(modifications);
expect(result).toBeDefined();
expect(result).toContain('file1.ts');
expect(result).toContain('file2.ts');
expect(result).toContain('diff content 1');
expect(result).toContain('file content 2');
});
it('should properly escape file paths with special characters', () => {
const modifications = {
'/home/project/file with spaces.ts': {
type: 'diff' as const,
content: 'diff content',
},
};
const result = fileModificationsToHTML(modifications);
expect(result).toBeDefined();
expect(result).toContain('path="/home/project/file with spaces.ts"');
});
it('should preserve diff content formatting', () => {
const modifications = {
'/home/project/test.ts': {
type: 'diff' as const,
content: '@@ -1,3 +1,3 @@\n line1\n-line2\n+modified line2\n line3',
},
};
const result = fileModificationsToHTML(modifications);
expect(result).toBeDefined();
expect(result).toContain('@@ -1,3 +1,3 @@');
expect(result).toContain('-line2');
expect(result).toContain('+modified line2');
});
it('should handle paths with quotes', () => {
const modifications = {
'/home/project/file"with"quotes.ts': {
type: 'diff' as const,
content: 'content',
},
};
const result = fileModificationsToHTML(modifications);
expect(result).toBeDefined();
expect(result).toContain('path=');
});
});