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