|
| 1 | +/** |
| 2 | + * Unit tests for file utilities |
| 3 | + * Target: 100% coverage for pure utility functions |
| 4 | + */ |
| 5 | + |
| 6 | +import * as fs from 'node:fs/promises'; |
| 7 | +import * as os from 'node:os'; |
| 8 | +import * as path from 'node:path'; |
| 9 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 10 | +import { normalizeFilePath, prepareFileForSearch, readFileContent, resolveFilePath } from './file'; |
| 11 | + |
| 12 | +describe('File Utilities', () => { |
| 13 | + describe('resolveFilePath', () => { |
| 14 | + it('should resolve relative path to absolute', () => { |
| 15 | + const repoPath = '/home/user/project'; |
| 16 | + const filePath = 'src/index.ts'; |
| 17 | + |
| 18 | + const result = resolveFilePath(repoPath, filePath); |
| 19 | + |
| 20 | + expect(result).toBe('/home/user/project/src/index.ts'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should handle already absolute paths', () => { |
| 24 | + const repoPath = '/home/user/project'; |
| 25 | + const filePath = '/home/user/project/src/index.ts'; |
| 26 | + |
| 27 | + const result = resolveFilePath(repoPath, filePath); |
| 28 | + |
| 29 | + expect(result).toBe('/home/user/project/src/index.ts'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should handle paths with ../', () => { |
| 33 | + const repoPath = '/home/user/project'; |
| 34 | + const filePath = 'src/../lib/utils.ts'; |
| 35 | + |
| 36 | + const result = resolveFilePath(repoPath, filePath); |
| 37 | + |
| 38 | + expect(result).toBe('/home/user/project/lib/utils.ts'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should handle current directory', () => { |
| 42 | + const repoPath = '/home/user/project'; |
| 43 | + const filePath = './src/index.ts'; |
| 44 | + |
| 45 | + const result = resolveFilePath(repoPath, filePath); |
| 46 | + |
| 47 | + expect(result).toBe('/home/user/project/src/index.ts'); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('normalizeFilePath', () => { |
| 52 | + it('should create relative path from absolute', () => { |
| 53 | + const repoPath = '/home/user/project'; |
| 54 | + const absolutePath = '/home/user/project/src/index.ts'; |
| 55 | + |
| 56 | + const result = normalizeFilePath(repoPath, absolutePath); |
| 57 | + |
| 58 | + expect(result).toBe('src/index.ts'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should handle paths in subdirectories', () => { |
| 62 | + const repoPath = '/home/user/project'; |
| 63 | + const absolutePath = '/home/user/project/packages/core/src/index.ts'; |
| 64 | + |
| 65 | + const result = normalizeFilePath(repoPath, absolutePath); |
| 66 | + |
| 67 | + expect(result).toBe('packages/core/src/index.ts'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should handle same path', () => { |
| 71 | + const repoPath = '/home/user/project'; |
| 72 | + const absolutePath = '/home/user/project'; |
| 73 | + |
| 74 | + const result = normalizeFilePath(repoPath, absolutePath); |
| 75 | + |
| 76 | + expect(result).toBe(''); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should handle paths outside repository', () => { |
| 80 | + const repoPath = '/home/user/project'; |
| 81 | + const absolutePath = '/home/user/other/file.ts'; |
| 82 | + |
| 83 | + const result = normalizeFilePath(repoPath, absolutePath); |
| 84 | + |
| 85 | + expect(result).toContain('..'); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('readFileContent', () => { |
| 90 | + let tempDir: string; |
| 91 | + let testFile: string; |
| 92 | + |
| 93 | + beforeEach(async () => { |
| 94 | + tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'file-test-')); |
| 95 | + testFile = path.join(tempDir, 'test.txt'); |
| 96 | + }); |
| 97 | + |
| 98 | + afterEach(async () => { |
| 99 | + await fs.rm(tempDir, { recursive: true, force: true }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should read file content', async () => { |
| 103 | + const content = 'Hello, World!'; |
| 104 | + await fs.writeFile(testFile, content); |
| 105 | + |
| 106 | + const result = await readFileContent(testFile); |
| 107 | + |
| 108 | + expect(result).toBe(content); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should read multiline content', async () => { |
| 112 | + const content = 'Line 1\nLine 2\nLine 3'; |
| 113 | + await fs.writeFile(testFile, content); |
| 114 | + |
| 115 | + const result = await readFileContent(testFile); |
| 116 | + |
| 117 | + expect(result).toBe(content); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should throw error for non-existent file', async () => { |
| 121 | + const nonExistent = path.join(tempDir, 'does-not-exist.txt'); |
| 122 | + |
| 123 | + await expect(readFileContent(nonExistent)).rejects.toThrow('File not found'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should throw error for empty file', async () => { |
| 127 | + await fs.writeFile(testFile, ''); |
| 128 | + |
| 129 | + await expect(readFileContent(testFile)).rejects.toThrow('File is empty'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should throw error for whitespace-only file', async () => { |
| 133 | + await fs.writeFile(testFile, ' \n \t \n '); |
| 134 | + |
| 135 | + await expect(readFileContent(testFile)).rejects.toThrow('File is empty'); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should handle files with leading/trailing whitespace', async () => { |
| 139 | + const content = ' \n Content \n '; |
| 140 | + await fs.writeFile(testFile, content); |
| 141 | + |
| 142 | + const result = await readFileContent(testFile); |
| 143 | + |
| 144 | + expect(result).toBe(content); |
| 145 | + expect(result.trim()).toBe('Content'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should handle large files', async () => { |
| 149 | + const content = 'x'.repeat(10000); |
| 150 | + await fs.writeFile(testFile, content); |
| 151 | + |
| 152 | + const result = await readFileContent(testFile); |
| 153 | + |
| 154 | + expect(result.length).toBe(10000); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should handle files with special characters', async () => { |
| 158 | + const content = 'Hello 🚀 World\n中文\nΨ'; |
| 159 | + await fs.writeFile(testFile, content, 'utf-8'); |
| 160 | + |
| 161 | + const result = await readFileContent(testFile); |
| 162 | + |
| 163 | + expect(result).toBe(content); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('prepareFileForSearch', () => { |
| 168 | + let tempDir: string; |
| 169 | + let testFile: string; |
| 170 | + |
| 171 | + beforeEach(async () => { |
| 172 | + tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'file-test-')); |
| 173 | + testFile = path.join(tempDir, 'test.txt'); |
| 174 | + }); |
| 175 | + |
| 176 | + afterEach(async () => { |
| 177 | + await fs.rm(tempDir, { recursive: true, force: true }); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should prepare file for search', async () => { |
| 181 | + const content = 'Test content'; |
| 182 | + await fs.writeFile(testFile, content); |
| 183 | + |
| 184 | + const result = await prepareFileForSearch(tempDir, 'test.txt'); |
| 185 | + |
| 186 | + expect(result.content).toBe(content); |
| 187 | + expect(result.absolutePath).toBe(testFile); |
| 188 | + expect(result.relativePath).toBe('test.txt'); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should handle nested directories', async () => { |
| 192 | + const subDir = path.join(tempDir, 'src', 'utils'); |
| 193 | + await fs.mkdir(subDir, { recursive: true }); |
| 194 | + const nestedFile = path.join(subDir, 'helper.ts'); |
| 195 | + await fs.writeFile(nestedFile, 'export function helper() {}'); |
| 196 | + |
| 197 | + const result = await prepareFileForSearch(tempDir, 'src/utils/helper.ts'); |
| 198 | + |
| 199 | + expect(result.content).toContain('helper'); |
| 200 | + expect(result.relativePath).toBe('src/utils/helper.ts'); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should return correct FileContentResult structure', async () => { |
| 204 | + await fs.writeFile(testFile, 'content'); |
| 205 | + |
| 206 | + const result = await prepareFileForSearch(tempDir, 'test.txt'); |
| 207 | + |
| 208 | + expect(result).toHaveProperty('content'); |
| 209 | + expect(result).toHaveProperty('absolutePath'); |
| 210 | + expect(result).toHaveProperty('relativePath'); |
| 211 | + expect(typeof result.content).toBe('string'); |
| 212 | + expect(typeof result.absolutePath).toBe('string'); |
| 213 | + expect(typeof result.relativePath).toBe('string'); |
| 214 | + }); |
| 215 | + |
| 216 | + it('should throw error for non-existent file', async () => { |
| 217 | + await expect(prepareFileForSearch(tempDir, 'nonexistent.txt')).rejects.toThrow( |
| 218 | + 'File not found' |
| 219 | + ); |
| 220 | + }); |
| 221 | + |
| 222 | + it('should throw error for empty file', async () => { |
| 223 | + await fs.writeFile(testFile, ''); |
| 224 | + |
| 225 | + await expect(prepareFileForSearch(tempDir, 'test.txt')).rejects.toThrow('File is empty'); |
| 226 | + }); |
| 227 | + |
| 228 | + it('should handle absolute path input', async () => { |
| 229 | + await fs.writeFile(testFile, 'content'); |
| 230 | + |
| 231 | + const result = await prepareFileForSearch(tempDir, testFile); |
| 232 | + |
| 233 | + expect(result.content).toBe('content'); |
| 234 | + expect(result.relativePath).toBe('test.txt'); |
| 235 | + }); |
| 236 | + }); |
| 237 | +}); |
0 commit comments