File size: 1,607 Bytes
7a1ad33 | 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 | /**
* @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 } from './fileSystemService.js';
vi.mock('fs/promises');
describe('StandardFileSystemService', () => {
let fileSystem: StandardFileSystemService;
beforeEach(() => {
vi.resetAllMocks();
fileSystem = new StandardFileSystemService();
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('readTextFile', () => {
it('should read file content using fs', async () => {
const testContent = 'Hello, World!';
vi.mocked(fs.readFile).mockResolvedValue(testContent);
const result = await fileSystem.readTextFile('/test/file.txt');
expect(fs.readFile).toHaveBeenCalledWith('/test/file.txt', 'utf-8');
expect(result).toBe(testContent);
});
it('should propagate fs.readFile errors', async () => {
const error = new Error('ENOENT: File not found');
vi.mocked(fs.readFile).mockRejectedValue(error);
await expect(fileSystem.readTextFile('/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('/test/file.txt', 'Hello, World!');
expect(fs.writeFile).toHaveBeenCalledWith(
'/test/file.txt',
'Hello, World!',
'utf-8',
);
});
});
});
|