|
| 1 | +import { describe, expect, it, beforeEach, afterEach } from 'vitest'; |
| 2 | +import File from './File'; |
| 3 | +import fs from 'fs'; |
| 4 | +import path from 'path'; |
| 5 | +import os from 'os'; |
| 6 | +const TEST_DIR = path.join(__dirname, 'test'); |
| 7 | +const TEST_FILES = { |
| 8 | + text: path.join(TEST_DIR, 'example.txt'), |
| 9 | + empty: path.join(TEST_DIR, 'empty.txt'), |
| 10 | + docx: path.join(TEST_DIR, 'example.docx'), |
| 11 | + png: path.join(TEST_DIR, 'icon.png'), |
| 12 | + svg: path.join(TEST_DIR, 'icon.svg'), |
| 13 | +}; |
| 14 | + |
| 15 | +describe('File Integration Tests', () => { |
| 16 | + let tempDir: string; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + // Create a temporary directory for test files |
| 20 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'temp-')); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + // Clean up temporary directory |
| 25 | + if (fs.existsSync(tempDir)) { |
| 26 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + it('should create a file from a local file and read its contents', async () => { |
| 31 | + const file = await File.createFromFile(TEST_FILES.text); |
| 32 | + |
| 33 | + expect(file.name).toBe('example.txt'); |
| 34 | + expect(file.mimeType).toBe('text/plain'); |
| 35 | + expect(file.size).toBeGreaterThan(0); |
| 36 | + expect(file.extension).toBe('txt'); |
| 37 | + |
| 38 | + const content = await file.readFileString(); |
| 39 | + expect(content).toBeTruthy(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should handle empty files correctly', async () => { |
| 43 | + const file = await File.createFromFile(TEST_FILES.empty); |
| 44 | + |
| 45 | + expect(file.name).toBe('empty.txt'); |
| 46 | + expect(file.mimeType).toBe('text/plain'); |
| 47 | + expect(file.size).toBe(0); |
| 48 | + expect(file.extension).toBe('txt'); |
| 49 | + |
| 50 | + const content = await file.readFileString(); |
| 51 | + expect(content).toBe(''); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should handle binary files (PNG)', async () => { |
| 55 | + const file = await File.createFromFile(TEST_FILES.png); |
| 56 | + |
| 57 | + expect(file.name).toBe('icon.png'); |
| 58 | + expect(file.mimeType).toBe('image/png'); |
| 59 | + expect(file.size).toBeGreaterThan(0); |
| 60 | + expect(file.extension).toBe('png'); |
| 61 | + |
| 62 | + const bytes = await file.readFileBytes(); |
| 63 | + expect(bytes.byteLength).toBeGreaterThan(0); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should handle DOCX files', async () => { |
| 67 | + const file = await File.createFromFile(TEST_FILES.docx); |
| 68 | + |
| 69 | + expect(file.name).toBe('example.docx'); |
| 70 | + expect(file.mimeType).toBe('application/vnd.openxmlformats-officedocument.wordprocessingml.document'); |
| 71 | + expect(file.size).toBeGreaterThan(0); |
| 72 | + expect(file.extension).toBe('docx'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should save a file to a new location', async () => { |
| 76 | + const file = await File.createFromFile(TEST_FILES.text); |
| 77 | + const newPath = path.join(tempDir, 'new-example.txt'); |
| 78 | + |
| 79 | + const result = await file.saveToFile(newPath); |
| 80 | + |
| 81 | + expect(result.original).toBe(file); |
| 82 | + expect(result.newFile.path).toBe(newPath); |
| 83 | + expect(fs.existsSync(newPath)).toBe(true); |
| 84 | + |
| 85 | + const newContent = await result.newFile.readFileString(); |
| 86 | + const originalContent = await file.readFileString(); |
| 87 | + expect(newContent).toBe(originalContent); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should move a file to a new location', async () => { |
| 91 | + const file = await File.createFromFile(TEST_FILES.text); |
| 92 | + const newPath = path.join(tempDir, 'moved-example.txt'); |
| 93 | + |
| 94 | + const movedFile = await file.moveTo(newPath); |
| 95 | + |
| 96 | + expect(movedFile.path).toBe(newPath); |
| 97 | + expect(fs.existsSync(newPath)).toBe(true); |
| 98 | + expect(fs.existsSync(TEST_FILES.text)).toBe(false); |
| 99 | + |
| 100 | + const nextMovedFile = await movedFile.moveTo(TEST_FILES.text); |
| 101 | + expect(nextMovedFile.path).toBe(TEST_FILES.text); |
| 102 | + expect(fs.existsSync(TEST_FILES.text)).toBe(true); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should append content to a file', async () => { |
| 106 | + const file = await File.createFromFile(TEST_FILES.text); |
| 107 | + const newPath = path.join(tempDir, 'appended-example.txt'); |
| 108 | + const originalContent = await file.readFileString(); |
| 109 | + const appendContent = '\nAppended content'; |
| 110 | + |
| 111 | + const result = await file.saveToFile(newPath); |
| 112 | + await result.newFile.append(appendContent); |
| 113 | + |
| 114 | + const finalContent = await result.newFile.readFileString(); |
| 115 | + expect(finalContent).toBe(originalContent + appendContent); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should prepend content to a file', async () => { |
| 119 | + const file = await File.createFromFile(TEST_FILES.text); |
| 120 | + const newPath = path.join(tempDir, 'prepended-example.txt'); |
| 121 | + const originalContent = await file.readFileString(); |
| 122 | + const prependContent = 'Prepended content\n'; |
| 123 | + |
| 124 | + const result = await file.saveToFile(newPath); |
| 125 | + await result.newFile.prepend(prependContent); |
| 126 | + |
| 127 | + const finalContent = await result.newFile.readFileString(); |
| 128 | + expect(finalContent).toBe(prependContent + originalContent); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should truncate a file to a specific size', async () => { |
| 132 | + const file = await File.createFromFile(TEST_FILES.text); |
| 133 | + const newPath = path.join(tempDir, 'truncated-example.txt'); |
| 134 | + const truncateSize = 5; |
| 135 | + |
| 136 | + const result = await file.saveToFile(newPath); |
| 137 | + await result.newFile.truncate(truncateSize); |
| 138 | + |
| 139 | + const finalContent = await result.newFile.readFileString(); |
| 140 | + expect(finalContent.length).toBe(truncateSize); |
| 141 | + }); |
| 142 | +}); |
0 commit comments