|
| 1 | +const { expect } = require('chai') |
| 2 | + |
| 3 | +// Helper function to simulate the escapeHtml behavior from htmlReporter.js |
| 4 | +function escapeHtml(text) { |
| 5 | + if (!text) return '' |
| 6 | + // Convert non-string values to strings before escaping |
| 7 | + if (typeof text !== 'string') { |
| 8 | + // Handle arrays by recursively flattening and joining with commas |
| 9 | + if (Array.isArray(text)) { |
| 10 | + // Recursive helper to flatten deeply nested arrays with depth limit to prevent stack overflow |
| 11 | + const flattenArray = (arr, depth = 0, maxDepth = 100) => { |
| 12 | + if (depth >= maxDepth) { |
| 13 | + // Safety limit reached, return string representation |
| 14 | + return String(arr) |
| 15 | + } |
| 16 | + return arr |
| 17 | + .map(item => { |
| 18 | + if (Array.isArray(item)) { |
| 19 | + return flattenArray(item, depth + 1, maxDepth) |
| 20 | + } |
| 21 | + return String(item) |
| 22 | + }) |
| 23 | + .join(', ') |
| 24 | + } |
| 25 | + text = flattenArray(text) |
| 26 | + } else { |
| 27 | + text = String(text) |
| 28 | + } |
| 29 | + } |
| 30 | + return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''') |
| 31 | +} |
| 32 | + |
| 33 | +describe('htmlReporter plugin', () => { |
| 34 | + describe('escapeHtml function', () => { |
| 35 | + it('should escape HTML special characters in strings', () => { |
| 36 | + const result = escapeHtml('<script>alert("xss")</script>') |
| 37 | + expect(result).to.include('<script>') |
| 38 | + expect(result).to.include('"') |
| 39 | + }) |
| 40 | + |
| 41 | + it('should handle string inputs correctly', () => { |
| 42 | + const result = escapeHtml('Hello <World>') |
| 43 | + expect(result).to.include('Hello <World>') |
| 44 | + }) |
| 45 | + |
| 46 | + it('should handle array inputs by converting to string', () => { |
| 47 | + const result = escapeHtml(['Item1', 'Item2', 'Item3']) |
| 48 | + expect(result).to.include('Item1, Item2, Item3') |
| 49 | + }) |
| 50 | + |
| 51 | + it('should handle nested arrays by flattening them', () => { |
| 52 | + // This is the key test case from the issue |
| 53 | + const result = escapeHtml(['Edge', ['Chromium (140.0.3485.54)'], 'N/A']) |
| 54 | + expect(result).to.include('Edge') |
| 55 | + expect(result).to.include('Chromium (140.0.3485.54)') |
| 56 | + expect(result).to.include('N/A') |
| 57 | + // Should not crash with "text.replace is not a function" |
| 58 | + }) |
| 59 | + |
| 60 | + it('should handle deeply nested arrays', () => { |
| 61 | + const result = escapeHtml(['Level1', ['Level2', ['Level3']], 'End']) |
| 62 | + expect(result).to.include('Level1') |
| 63 | + expect(result).to.include('Level2') |
| 64 | + expect(result).to.include('Level3') |
| 65 | + expect(result).to.include('End') |
| 66 | + }) |
| 67 | + |
| 68 | + it('should handle null and undefined inputs', () => { |
| 69 | + const resultNull = escapeHtml(null) |
| 70 | + expect(resultNull).to.equal('') |
| 71 | + |
| 72 | + const resultUndefined = escapeHtml(undefined) |
| 73 | + expect(resultUndefined).to.equal('') |
| 74 | + }) |
| 75 | + |
| 76 | + it('should handle empty strings', () => { |
| 77 | + const result = escapeHtml('') |
| 78 | + expect(result).to.equal('') |
| 79 | + }) |
| 80 | + |
| 81 | + it('should handle numbers by converting to strings', () => { |
| 82 | + const result = escapeHtml(42) |
| 83 | + expect(result).to.include('42') |
| 84 | + }) |
| 85 | + |
| 86 | + it('should handle objects by converting to strings', () => { |
| 87 | + const result = escapeHtml({ key: 'value' }) |
| 88 | + expect(result).to.include('[object Object]') |
| 89 | + }) |
| 90 | + |
| 91 | + it('should escape all HTML entities in arrays', () => { |
| 92 | + const result = escapeHtml(['<div>', '"quoted"', "it's", 'A&B']) |
| 93 | + expect(result).to.include('<div>') |
| 94 | + expect(result).to.include('"quoted"') |
| 95 | + expect(result).to.include('it's') |
| 96 | + expect(result).to.include('A&B') |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + describe('generateSystemInfoHtml function', () => { |
| 101 | + it('should handle system info with nested arrays', () => { |
| 102 | + // This tests the real-world scenario from the issue |
| 103 | + const systemInfo = { |
| 104 | + nodeInfo: ['Node', '22.14.0', '~\\AppData\\Local\\fnm_multishells\\19200_1763624547202\\node.EXE'], |
| 105 | + osInfo: ['OS', 'Windows 10 10.0.19045'], |
| 106 | + cpuInfo: ['CPU', '(12) x64 12th Gen Intel(R) Core(TM) i5-12500'], |
| 107 | + chromeInfo: ['Chrome', '142.0.7444.163', 'N/A'], |
| 108 | + edgeInfo: ['Edge', ['Chromium (140.0.3485.54)'], 'N/A'], // This is the problematic case |
| 109 | + firefoxInfo: undefined, |
| 110 | + safariInfo: ['Safari', 'N/A'], |
| 111 | + playwrightBrowsers: 'chromium: 136.0.7103.25, firefox: 137.0, webkit: 18.4', |
| 112 | + } |
| 113 | + |
| 114 | + // Test that processing this system info doesn't crash |
| 115 | + // We simulate the formatInfo function behavior |
| 116 | + const formatValue = value => { |
| 117 | + if (Array.isArray(value) && value.length > 1) { |
| 118 | + const displayValue = value[1] |
| 119 | + return escapeHtml(displayValue) |
| 120 | + } else if (typeof value === 'string') { |
| 121 | + return value |
| 122 | + } |
| 123 | + return '' |
| 124 | + } |
| 125 | + |
| 126 | + // Test each system info value |
| 127 | + expect(formatValue(systemInfo.nodeInfo)).to.include('22.14.0') |
| 128 | + expect(formatValue(systemInfo.osInfo)).to.include('Windows 10') |
| 129 | + expect(formatValue(systemInfo.cpuInfo)).to.include('12th Gen') |
| 130 | + expect(formatValue(systemInfo.chromeInfo)).to.include('142.0.7444.163') |
| 131 | + |
| 132 | + // The critical test: edgeInfo with nested array should not crash |
| 133 | + const edgeResult = formatValue(systemInfo.edgeInfo) |
| 134 | + expect(edgeResult).to.include('Chromium') |
| 135 | + expect(edgeResult).to.include('140.0.3485.54') |
| 136 | + |
| 137 | + expect(formatValue(systemInfo.safariInfo)).to.equal('N/A') |
| 138 | + }) |
| 139 | + |
| 140 | + it('should handle undefined values gracefully', () => { |
| 141 | + const systemInfo = { |
| 142 | + firefoxInfo: undefined, |
| 143 | + } |
| 144 | + |
| 145 | + const formatValue = value => { |
| 146 | + if (Array.isArray(value) && value.length > 1) { |
| 147 | + return 'has value' |
| 148 | + } |
| 149 | + return '' |
| 150 | + } |
| 151 | + |
| 152 | + expect(formatValue(systemInfo.firefoxInfo)).to.equal('') |
| 153 | + }) |
| 154 | + |
| 155 | + it('should handle string values directly', () => { |
| 156 | + const systemInfo = { |
| 157 | + playwrightBrowsers: 'chromium: 136.0.7103.25, firefox: 137.0, webkit: 18.4', |
| 158 | + } |
| 159 | + |
| 160 | + const formatValue = value => { |
| 161 | + if (typeof value === 'string') { |
| 162 | + return value |
| 163 | + } |
| 164 | + return '' |
| 165 | + } |
| 166 | + |
| 167 | + expect(formatValue(systemInfo.playwrightBrowsers)).to.include('chromium') |
| 168 | + expect(formatValue(systemInfo.playwrightBrowsers)).to.include('firefox') |
| 169 | + expect(formatValue(systemInfo.playwrightBrowsers)).to.include('webkit') |
| 170 | + }) |
| 171 | + }) |
| 172 | + |
| 173 | + describe('edge cases', () => { |
| 174 | + it('should handle arrays with HTML content', () => { |
| 175 | + const result = escapeHtml(['<script>', ['alert("xss")'], '</script>']) |
| 176 | + expect(result).to.include('<script>') |
| 177 | + expect(result).to.include('alert("xss")') |
| 178 | + expect(result).to.include('</script>') |
| 179 | + }) |
| 180 | + |
| 181 | + it('should handle mixed array types', () => { |
| 182 | + const result = escapeHtml(['String', 42, true, null, ['nested']]) |
| 183 | + expect(result).to.include('String') |
| 184 | + expect(result).to.include('42') |
| 185 | + expect(result).to.include('true') |
| 186 | + expect(result).to.include('null') |
| 187 | + expect(result).to.include('nested') |
| 188 | + }) |
| 189 | + }) |
| 190 | +}) |
0 commit comments