|
| 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 | + |
| 6 | +// Import the main module components to test |
| 7 | +import { LogViewerProvider, LogItem } from '../logViewer'; |
| 8 | + |
| 9 | +suite('Log Reader Test Suite', () => { |
| 10 | + // Create a temporary directory for test logs |
| 11 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'magento-logviewer-test-')); |
| 12 | + const logFilePath = path.join(tempDir, 'test.log'); |
| 13 | + |
| 14 | + // Sample log content with different log levels |
| 15 | + const sampleLogContent = `[2025-05-28T21:13:28.751586+00:00] .Info: Broken reference: the 'amcompany_toolbar_link' element cannot be added as child to 'header.links', because the latter doesn't exist |
| 16 | +[2025-05-28T21:13:28.751586+00:00] .Warn: Broken reference: the 'amcompany_toolbar_link' element cannot be added as child to 'header.links', because the latter doesn't exist |
| 17 | +[2025-05-28T21:13:29.123456+00:00] .Debug: Debug message: initializing module |
| 18 | +[2025-05-28T21:13:30.234567+00:00] .Error: Failed to load resource: server responded with status 404 |
| 19 | +[2025-05-28T21:13:31.345678+00:00] .Critical: Database connection failed`; |
| 20 | + |
| 21 | + // Set up and tear down |
| 22 | + suiteSetup(() => { |
| 23 | + // Create the test log file before tests |
| 24 | + fs.writeFileSync(logFilePath, sampleLogContent); |
| 25 | + }); |
| 26 | + |
| 27 | + suiteTeardown(() => { |
| 28 | + // Clean up test files after tests |
| 29 | + try { |
| 30 | + fs.unlinkSync(logFilePath); |
| 31 | + fs.rmdirSync(tempDir); |
| 32 | + } catch (err) { |
| 33 | + console.error('Failed to clean up test files:', err); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + test('Log file should exist', () => { |
| 38 | + assert.strictEqual(fs.existsSync(logFilePath), true, 'Test log file should exist'); |
| 39 | + }); |
| 40 | + |
| 41 | + test('Log file should be readable', () => { |
| 42 | + const content = fs.readFileSync(logFilePath, 'utf-8'); |
| 43 | + assert.strictEqual(content, sampleLogContent, 'Log file content should match the sample content'); |
| 44 | + }); |
| 45 | + |
| 46 | + test('LogViewerProvider should read log file correctly', async () => { |
| 47 | + // Create a LogViewerProvider instance with the temp directory as root |
| 48 | + const logProvider = new LogViewerProvider(tempDir); // Get access to private method (this requires modifying the class or using a type assertion) |
| 49 | + // For this test, we'll use a type assertion to access the private method |
| 50 | + |
| 51 | + // Use a specific interface that defines the method we need for testing |
| 52 | + interface LogViewerInternals { |
| 53 | + getLogFileLines(filePath: string): LogItem[]; |
| 54 | + } |
| 55 | + |
| 56 | + // Cast to our specific interface instead of 'any' |
| 57 | + const provider = logProvider as unknown as LogViewerInternals; |
| 58 | + const logItems = provider.getLogFileLines(logFilePath); |
| 59 | + |
| 60 | + // Get all log levels from the items |
| 61 | + const logLevels = logItems.map((item: LogItem) => item.label?.toString().split(' ')[0]); |
| 62 | + console.log('Found log levels:', logLevels); |
| 63 | + |
| 64 | + // Verify log file is parsed correctly and contains expected entries |
| 65 | + assert.ok(logItems.length > 0, 'Should parse log entries'); |
| 66 | + |
| 67 | + // Find if any log level contains Info, Warn, etc (case-insensitive) |
| 68 | + assert.ok(logLevels.some((level: unknown) => typeof level === 'string' && level.includes('INFO')), 'Should contain INFO level logs'); |
| 69 | + assert.ok(logLevels.some((level: unknown) => typeof level === 'string' && level.includes('WARN')), 'Should contain WARN level logs'); |
| 70 | + assert.ok(logLevels.some((level: unknown) => typeof level === 'string' && level.includes('DEBUG')), 'Should contain DEBUG level logs'); |
| 71 | + assert.ok(logLevels.some((level: unknown) => typeof level === 'string' && level.includes('ERROR')), 'Should contain ERROR level logs'); |
| 72 | + assert.ok(logLevels.some((level: unknown) => typeof level === 'string' && level.includes('CRITICAL')), 'Should contain CRITICAL level logs'); |
| 73 | + }); |
| 74 | +}); |
0 commit comments