import { describe, it, expect, beforeAll, afterAll } from 'vitest'; import { FileReader } from '../../src/infrastructure/FileReader.js'; import fs from 'fs'; import path from 'path'; import os from 'os'; describe('FileReader', () => { const fileReader = new FileReader(); let tempDir: string; let testFilePath: string; beforeAll(() => { // テスト用の一時ディレクトリとファイルを作成 tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mdlint-test-')); testFilePath = path.join(tempDir, 'test.md'); fs.writeFileSync(testFilePath, '# Test\n\nContent', 'utf-8'); }); afterAll(() => { // クリーンアップ fs.rmSync(tempDir, { recursive: true, force: true }); }); describe('read', () => { it('should read file content', async () => { const content = await fileReader.read(testFilePath); expect(content).toBe('# Test\n\nContent'); }); it('should throw error for non-existent file', async () => { const nonExistentPath = path.join(tempDir, 'non-existent.md'); await expect(fileReader.read(nonExistentPath)).rejects.toThrow(); }); }); describe('exists', () => { it('should return true for existing file', () => { expect(fileReader.exists(testFilePath)).toBe(true); }); it('should return false for non-existent file', () => { const nonExistentPath = path.join(tempDir, 'non-existent.md'); expect(fileReader.exists(nonExistentPath)).toBe(false); }); }); describe('readSync', () => { it('should read file content synchronously', () => { const content = fileReader.readSync(testFilePath); expect(content).toBe('# Test\n\nContent'); }); it('should throw error for non-existent file', () => { const nonExistentPath = path.join(tempDir, 'non-existent.md'); expect(() => fileReader.readSync(nonExistentPath)).toThrow(); }); }); });