|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as os from 'os'; |
| 5 | +import { pathExists, pathExistsAsync, getCachedFileContentAsync, getCachedFileContent } from '../helpers'; |
| 6 | + |
| 7 | +suite('Async File Operations Test Suite', () => { |
| 8 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'magento-async-test-')); |
| 9 | + let testFilePath: string; |
| 10 | + |
| 11 | + setup(() => { |
| 12 | + // Create a test file for each test |
| 13 | + testFilePath = path.join(tempDir, 'async-test.log'); |
| 14 | + fs.writeFileSync(testFilePath, 'Test content for async operations'); |
| 15 | + }); |
| 16 | + |
| 17 | + teardown(() => { |
| 18 | + // Clean up test file after each test |
| 19 | + if (fs.existsSync(testFilePath)) { |
| 20 | + fs.unlinkSync(testFilePath); |
| 21 | + } |
| 22 | + }); |
| 23 | + |
| 24 | + suiteTeardown(() => { |
| 25 | + // Clean up temp directory |
| 26 | + try { |
| 27 | + if (fs.existsSync(tempDir)) { |
| 28 | + const files = fs.readdirSync(tempDir); |
| 29 | + files.forEach(file => { |
| 30 | + fs.unlinkSync(path.join(tempDir, file)); |
| 31 | + }); |
| 32 | + fs.rmdirSync(tempDir); |
| 33 | + } |
| 34 | + } catch (err) { |
| 35 | + console.error('Failed to clean up test directory:', err); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + test('pathExistsAsync should work correctly for existing files', async () => { |
| 40 | + const exists = await pathExistsAsync(testFilePath); |
| 41 | + assert.strictEqual(exists, true, 'Should return true for existing file'); |
| 42 | + }); |
| 43 | + |
| 44 | + test('pathExistsAsync should work correctly for non-existing files', async () => { |
| 45 | + const nonExistentPath = path.join(tempDir, 'non-existent.log'); |
| 46 | + const exists = await pathExistsAsync(nonExistentPath); |
| 47 | + assert.strictEqual(exists, false, 'Should return false for non-existing file'); |
| 48 | + }); |
| 49 | + |
| 50 | + test('pathExists and pathExistsAsync should return same results', async () => { |
| 51 | + const syncResult = pathExists(testFilePath); |
| 52 | + const asyncResult = await pathExistsAsync(testFilePath); |
| 53 | + assert.strictEqual(syncResult, asyncResult, 'Sync and async should return same result'); |
| 54 | + |
| 55 | + const nonExistentPath = path.join(tempDir, 'non-existent.log'); |
| 56 | + const syncResultFalse = pathExists(nonExistentPath); |
| 57 | + const asyncResultFalse = await pathExistsAsync(nonExistentPath); |
| 58 | + assert.strictEqual(syncResultFalse, asyncResultFalse, 'Sync and async should return same result for non-existing file'); |
| 59 | + }); |
| 60 | + |
| 61 | + test('getCachedFileContentAsync should read file content correctly', async () => { |
| 62 | + const content = await getCachedFileContentAsync(testFilePath); |
| 63 | + assert.strictEqual(content, 'Test content for async operations', 'Should read file content correctly'); |
| 64 | + }); |
| 65 | + |
| 66 | + test('getCachedFileContentAsync should handle non-existent files gracefully', async () => { |
| 67 | + const nonExistentPath = path.join(tempDir, 'non-existent.log'); |
| 68 | + const content = await getCachedFileContentAsync(nonExistentPath); |
| 69 | + assert.strictEqual(content, null, 'Should return null for non-existent files'); |
| 70 | + }); |
| 71 | + |
| 72 | + test('getCachedFileContentAsync should handle large files with streaming', async () => { |
| 73 | + // Create a large test file (>50MB) |
| 74 | + const largeFilePath = path.join(tempDir, 'large-async-test.log'); |
| 75 | + const largeContent = 'x'.repeat(51 * 1024 * 1024); // 51MB of content |
| 76 | + |
| 77 | + fs.writeFileSync(largeFilePath, largeContent); |
| 78 | + |
| 79 | + try { |
| 80 | + const content = await getCachedFileContentAsync(largeFilePath); |
| 81 | + assert.strictEqual(content, largeContent, 'Should read large file content correctly using streaming'); |
| 82 | + } finally { |
| 83 | + // Clean up large file |
| 84 | + if (fs.existsSync(largeFilePath)) { |
| 85 | + fs.unlinkSync(largeFilePath); |
| 86 | + } |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + test('getCachedFileContentAsync should cache content correctly', async () => { |
| 91 | + // First call - should read from file |
| 92 | + const firstCall = await getCachedFileContentAsync(testFilePath); |
| 93 | + assert.strictEqual(firstCall, 'Test content for async operations', 'First call should read file content'); |
| 94 | + |
| 95 | + // Second call without file modification - should return cached content |
| 96 | + const secondCall = await getCachedFileContentAsync(testFilePath); |
| 97 | + assert.strictEqual(secondCall, 'Test content for async operations', 'Second call should return cached content'); |
| 98 | + }); |
| 99 | + |
| 100 | + test('getCachedFileContentAsync performance should be reasonable', async () => { |
| 101 | + const startTime = Date.now(); |
| 102 | + |
| 103 | + // Read the same file multiple times |
| 104 | + for (let i = 0; i < 10; i++) { |
| 105 | + await getCachedFileContentAsync(testFilePath); |
| 106 | + } |
| 107 | + |
| 108 | + const endTime = Date.now(); |
| 109 | + const duration = endTime - startTime; |
| 110 | + |
| 111 | + // Should complete quickly due to caching |
| 112 | + assert.ok(duration < 1000, `Multiple async reads should complete quickly, took ${duration}ms`); |
| 113 | + }); |
| 114 | + |
| 115 | + test('Async and sync getCachedFileContent should return same results', async () => { |
| 116 | + const syncContent = getCachedFileContent(testFilePath); |
| 117 | + const asyncContent = await getCachedFileContentAsync(testFilePath); |
| 118 | + |
| 119 | + assert.strictEqual(syncContent, asyncContent, 'Sync and async content reading should return same results'); |
| 120 | + }); |
| 121 | +}); |
0 commit comments