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