-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathfileSystemService.test.ts
More file actions
451 lines (371 loc) · 13.6 KB
/
fileSystemService.test.ts
File metadata and controls
451 lines (371 loc) · 13.6 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
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
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import fs from 'node:fs/promises';
import {
StandardFileSystemService,
needsUtf8Bom,
resetUtf8BomCache,
} from './fileSystemService.js';
const mockPlatform = vi.hoisted(() => vi.fn().mockReturnValue('linux'));
const mockGetSystemEncoding = vi.hoisted(() =>
vi.fn().mockReturnValue('utf-8'),
);
vi.mock('fs/promises');
vi.mock('os', () => ({
default: {
platform: mockPlatform,
},
platform: mockPlatform,
}));
vi.mock('../utils/systemEncoding.js', () => ({
getSystemEncoding: mockGetSystemEncoding,
}));
vi.mock('../utils/fileUtils.js', async (importOriginal) => {
const actual = await importOriginal<typeof import('../utils/fileUtils.js')>();
return {
...actual,
readFileWithLineAndLimit: vi.fn(),
};
});
import { readFileWithLineAndLimit } from '../utils/fileUtils.js';
describe('StandardFileSystemService', () => {
let fileSystem: StandardFileSystemService;
beforeEach(() => {
vi.resetAllMocks();
resetUtf8BomCache();
mockPlatform.mockReturnValue('linux');
mockGetSystemEncoding.mockReturnValue('utf-8');
fileSystem = new StandardFileSystemService();
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('readTextFile', () => {
it('should read file content and return ReadTextFileResponse', async () => {
vi.mocked(readFileWithLineAndLimit).mockResolvedValue({
content: 'Hello, World!',
bom: false,
encoding: 'utf-8',
originalLineCount: 1,
});
const result = await fileSystem.readTextFile({ path: '/test/file.txt' });
expect(readFileWithLineAndLimit).toHaveBeenCalledWith({
path: '/test/file.txt',
limit: Infinity,
line: 0,
});
expect(result.content).toBe('Hello, World!');
expect(result._meta?.bom).toBe(false);
expect(result._meta?.encoding).toBe('utf-8');
});
it('should pass limit and line params to readFileWithLineAndLimit', async () => {
vi.mocked(readFileWithLineAndLimit).mockResolvedValue({
content: 'line 5',
bom: false,
encoding: 'utf-8',
originalLineCount: 100,
});
const result = await fileSystem.readTextFile({
path: '/test/file.txt',
limit: 10,
line: 5,
});
expect(readFileWithLineAndLimit).toHaveBeenCalledWith({
path: '/test/file.txt',
limit: 10,
line: 5,
});
expect(result._meta?.originalLineCount).toBe(100);
});
it('should return encoding info for GBK file', async () => {
vi.mocked(readFileWithLineAndLimit).mockResolvedValue({
content: '你好世界',
bom: false,
encoding: 'gb18030',
originalLineCount: 1,
});
const result = await fileSystem.readTextFile({ path: '/test/gbk.txt' });
expect(result.content).toBe('你好世界');
expect(result._meta?.encoding).toBe('gb18030');
expect(result._meta?.bom).toBe(false);
});
it('should propagate readFileWithLineAndLimit errors', async () => {
const error = new Error('ENOENT: File not found');
vi.mocked(readFileWithLineAndLimit).mockRejectedValue(error);
await expect(
fileSystem.readTextFile({ path: '/test/file.txt' }),
).rejects.toThrow('ENOENT: File not found');
});
});
describe('writeTextFile', () => {
it('should write file content using fs', async () => {
vi.mocked(fs.writeFile).mockResolvedValue();
await fileSystem.writeTextFile({
path: '/test/file.txt',
content: 'Hello, World!',
});
expect(fs.writeFile).toHaveBeenCalledWith(
'/test/file.txt',
'Hello, World!',
'utf-8',
);
});
it('should write file with BOM when bom option is true', async () => {
vi.mocked(fs.writeFile).mockResolvedValue();
await fileSystem.writeTextFile({
path: '/test/file.txt',
content: 'Hello, World!',
_meta: { bom: true },
});
// Verify that fs.writeFile was called with a Buffer that starts with BOM
const writeCall = vi.mocked(fs.writeFile).mock.calls[0];
expect(writeCall[0]).toBe('/test/file.txt');
expect(writeCall[1]).toBeInstanceOf(Buffer);
const buffer = writeCall[1] as Buffer;
expect(buffer[0]).toBe(0xef);
expect(buffer[1]).toBe(0xbb);
expect(buffer[2]).toBe(0xbf);
});
it('should write file without BOM when bom option is false', async () => {
vi.mocked(fs.writeFile).mockResolvedValue();
await fileSystem.writeTextFile({
path: '/test/file.txt',
content: 'Hello, World!',
_meta: { bom: false },
});
expect(fs.writeFile).toHaveBeenCalledWith(
'/test/file.txt',
'Hello, World!',
'utf-8',
);
});
it('should not duplicate BOM when content already has BOM character', async () => {
vi.mocked(fs.writeFile).mockResolvedValue();
// Content that includes the BOM character (as readTextFile would return)
const contentWithBOM = '\uFEFF' + 'Hello';
await fileSystem.writeTextFile({
path: '/test/file.txt',
content: contentWithBOM,
_meta: { bom: true },
});
// Verify that fs.writeFile was called with a Buffer that has only one BOM
const writeCall = vi.mocked(fs.writeFile).mock.calls[0];
expect(writeCall[0]).toBe('/test/file.txt');
expect(writeCall[1]).toBeInstanceOf(Buffer);
const buffer = writeCall[1] as Buffer;
// First three bytes should be BOM
expect(buffer[0]).toBe(0xef);
expect(buffer[1]).toBe(0xbb);
expect(buffer[2]).toBe(0xbf);
// Fourth byte should be 'H' (0x48), not another BOM
expect(buffer[3]).toBe(0x48);
// Count BOM sequences in the buffer - should be only one
let bomCount = 0;
for (let i = 0; i <= buffer.length - 3; i++) {
if (
buffer[i] === 0xef &&
buffer[i + 1] === 0xbb &&
buffer[i + 2] === 0xbf
) {
bomCount++;
}
}
expect(bomCount).toBe(1);
});
it('should write file with non-UTF-8 encoding using iconv-lite', async () => {
vi.mocked(fs.writeFile).mockResolvedValue();
await fileSystem.writeTextFile({
path: '/test/file.txt',
content: '你好世界',
_meta: { encoding: 'gbk' },
});
// Verify that fs.writeFile was called with a Buffer (iconv-encoded)
const writeCall = vi.mocked(fs.writeFile).mock.calls[0];
expect(writeCall[0]).toBe('/test/file.txt');
expect(writeCall[1]).toBeInstanceOf(Buffer);
});
it('should write file as UTF-8 when encoding is utf-8', async () => {
vi.mocked(fs.writeFile).mockResolvedValue();
await fileSystem.writeTextFile({
path: '/test/file.txt',
content: 'Hello',
_meta: { encoding: 'utf-8' },
});
expect(fs.writeFile).toHaveBeenCalledWith(
'/test/file.txt',
'Hello',
'utf-8',
);
});
it('should preserve UTF-16LE BOM when writing back a UTF-16LE file', async () => {
vi.mocked(fs.writeFile).mockResolvedValue();
await fileSystem.writeTextFile({
path: '/test/file.txt',
content: 'Hello',
_meta: { encoding: 'utf-16le', bom: true },
});
// iconv-lite encodes as UTF-16LE; with bom:true the FF FE BOM is prepended
const writeCall = vi.mocked(fs.writeFile).mock.calls[0];
expect(writeCall[0]).toBe('/test/file.txt');
expect(writeCall[1]).toBeInstanceOf(Buffer);
const buf = writeCall[1] as Buffer;
// First two bytes must be the UTF-16LE BOM: FF FE
expect(buf[0]).toBe(0xff);
expect(buf[1]).toBe(0xfe);
});
it('should not add BOM when writing UTF-16LE file without bom flag', async () => {
vi.mocked(fs.writeFile).mockResolvedValue();
await fileSystem.writeTextFile({
path: '/test/file.txt',
content: 'Hello',
_meta: { encoding: 'utf-16le', bom: false },
});
// No BOM prepended — raw iconv-encoded buffer written directly
const writeCall = vi.mocked(fs.writeFile).mock.calls[0];
expect(writeCall[0]).toBe('/test/file.txt');
expect(writeCall[1]).toBeInstanceOf(Buffer);
const buf = writeCall[1] as Buffer;
// First two bytes should NOT be FF FE (the UTF-16LE BOM)
expect(!(buf[0] === 0xff && buf[1] === 0xfe)).toBe(true);
});
it('should convert LF to CRLF when writing .bat files on Windows', async () => {
mockPlatform.mockReturnValue('win32');
vi.mocked(fs.writeFile).mockResolvedValue();
await fileSystem.writeTextFile({
path: '/test/script.bat',
content: '@echo off\necho hello\nexit /b 0\n',
});
expect(fs.writeFile).toHaveBeenCalledWith(
'/test/script.bat',
'@echo off\r\necho hello\r\nexit /b 0\r\n',
'utf-8',
);
});
it('should convert LF to CRLF when writing .cmd files on Windows', async () => {
mockPlatform.mockReturnValue('win32');
vi.mocked(fs.writeFile).mockResolvedValue();
await fileSystem.writeTextFile({
path: '/test/script.cmd',
content: '@echo off\necho hello\n',
});
expect(fs.writeFile).toHaveBeenCalledWith(
'/test/script.cmd',
'@echo off\r\necho hello\r\n',
'utf-8',
);
});
it('should not double-convert existing CRLF in .bat files on Windows', async () => {
mockPlatform.mockReturnValue('win32');
vi.mocked(fs.writeFile).mockResolvedValue();
await fileSystem.writeTextFile({
path: '/test/script.bat',
content: '@echo off\r\necho hello\r\n',
});
expect(fs.writeFile).toHaveBeenCalledWith(
'/test/script.bat',
'@echo off\r\necho hello\r\n',
'utf-8',
);
});
it('should handle mixed line endings in .bat files on Windows', async () => {
mockPlatform.mockReturnValue('win32');
vi.mocked(fs.writeFile).mockResolvedValue();
await fileSystem.writeTextFile({
path: '/test/script.bat',
content: 'line1\r\nline2\nline3\r\n',
});
expect(fs.writeFile).toHaveBeenCalledWith(
'/test/script.bat',
'line1\r\nline2\r\nline3\r\n',
'utf-8',
);
});
it('should be case-insensitive for .BAT extension on Windows', async () => {
mockPlatform.mockReturnValue('win32');
vi.mocked(fs.writeFile).mockResolvedValue();
await fileSystem.writeTextFile({
path: '/test/SCRIPT.BAT',
content: 'echo hello\n',
});
expect(fs.writeFile).toHaveBeenCalledWith(
'/test/SCRIPT.BAT',
'echo hello\r\n',
'utf-8',
);
});
it('should not convert line endings for non-.bat/.cmd files on Windows', async () => {
mockPlatform.mockReturnValue('win32');
vi.mocked(fs.writeFile).mockResolvedValue();
await fileSystem.writeTextFile({
path: '/test/script.sh',
content: '#!/bin/bash\necho hello\n',
});
expect(fs.writeFile).toHaveBeenCalledWith(
'/test/script.sh',
'#!/bin/bash\necho hello\n',
'utf-8',
);
});
it('should not convert line endings for .bat files on non-Windows', async () => {
mockPlatform.mockReturnValue('darwin');
vi.mocked(fs.writeFile).mockResolvedValue();
await fileSystem.writeTextFile({
path: '/test/script.bat',
content: '@echo off\necho hello\n',
});
expect(fs.writeFile).toHaveBeenCalledWith(
'/test/script.bat',
'@echo off\necho hello\n',
'utf-8',
);
});
});
describe('needsUtf8Bom', () => {
beforeEach(() => {
resetUtf8BomCache();
});
it('should return true for .ps1 files on Windows with non-UTF-8 code page', () => {
mockPlatform.mockReturnValue('win32');
mockGetSystemEncoding.mockReturnValue('gbk');
expect(needsUtf8Bom('/test/script.ps1')).toBe(true);
});
it('should return true for .PS1 files (case-insensitive)', () => {
mockPlatform.mockReturnValue('win32');
mockGetSystemEncoding.mockReturnValue('gbk');
expect(needsUtf8Bom('/test/SCRIPT.PS1')).toBe(true);
});
it('should return false for .ps1 files on Windows with UTF-8 code page', () => {
mockPlatform.mockReturnValue('win32');
mockGetSystemEncoding.mockReturnValue('utf-8');
expect(needsUtf8Bom('/test/script.ps1')).toBe(false);
});
it('should return false for .ps1 files on non-Windows', () => {
mockPlatform.mockReturnValue('darwin');
expect(needsUtf8Bom('/test/script.ps1')).toBe(false);
});
it('should return false for non-.ps1 files on Windows with non-UTF-8 code page', () => {
mockPlatform.mockReturnValue('win32');
mockGetSystemEncoding.mockReturnValue('gbk');
expect(needsUtf8Bom('/test/script.sh')).toBe(false);
expect(needsUtf8Bom('/test/file.txt')).toBe(false);
expect(needsUtf8Bom('/test/script.bat')).toBe(false);
});
it('should cache the platform/encoding check across calls', () => {
mockPlatform.mockReturnValue('win32');
mockGetSystemEncoding.mockReturnValue('gbk');
needsUtf8Bom('/test/script.ps1');
needsUtf8Bom('/test/other.ps1');
// getSystemEncoding should only be called once due to caching
expect(mockGetSystemEncoding).toHaveBeenCalledTimes(1);
});
it('should treat null system encoding as non-UTF-8', () => {
mockPlatform.mockReturnValue('win32');
mockGetSystemEncoding.mockReturnValue(null);
expect(needsUtf8Bom('/test/script.ps1')).toBe(true);
});
});
});