|
1 | 1 | import { mount } from '@vue/test-utils' |
2 | 2 | import { createPinia } from 'pinia' |
3 | 3 | import PrimeVue from 'primevue/config' |
4 | | -import { beforeAll, describe, expect, it } from 'vitest' |
| 4 | +import { beforeAll, describe, expect, it, vi } from 'vitest' |
5 | 5 | import { createApp } from 'vue' |
6 | 6 | import { createI18n } from 'vue-i18n' |
7 | 7 |
|
8 | 8 | import type { ComfyNodeDef as ComfyNodeDefV2 } from '@/schemas/nodeDef/nodeDefSchemaV2' |
| 9 | +import * as markdownRendererUtil from '@/utils/markdownRendererUtil' |
9 | 10 |
|
10 | 11 | import NodePreview from './NodePreview.vue' |
11 | 12 |
|
@@ -129,4 +130,164 @@ describe('NodePreview', () => { |
129 | 130 |
|
130 | 131 | expect(headdot.classes()).toContain('pr-3') |
131 | 132 | }) |
| 133 | + |
| 134 | + describe('Description Rendering', () => { |
| 135 | + it('renders plain text description as HTML', () => { |
| 136 | + const plainTextNodeDef: ComfyNodeDefV2 = { |
| 137 | + ...mockNodeDef, |
| 138 | + description: 'This is a plain text description' |
| 139 | + } |
| 140 | + |
| 141 | + const wrapper = mountComponent(plainTextNodeDef) |
| 142 | + const description = wrapper.find('._sb_description') |
| 143 | + |
| 144 | + expect(description.exists()).toBe(true) |
| 145 | + expect(description.html()).toContain('This is a plain text description') |
| 146 | + }) |
| 147 | + |
| 148 | + it('renders markdown description with formatting', () => { |
| 149 | + const markdownNodeDef: ComfyNodeDefV2 = { |
| 150 | + ...mockNodeDef, |
| 151 | + description: '**Bold text** and *italic text* with `code`' |
| 152 | + } |
| 153 | + |
| 154 | + const wrapper = mountComponent(markdownNodeDef) |
| 155 | + const description = wrapper.find('._sb_description') |
| 156 | + |
| 157 | + expect(description.exists()).toBe(true) |
| 158 | + expect(description.html()).toContain('<strong>Bold text</strong>') |
| 159 | + expect(description.html()).toContain('<em>italic text</em>') |
| 160 | + expect(description.html()).toContain('<code>code</code>') |
| 161 | + }) |
| 162 | + |
| 163 | + it('does not render description element when description is empty', () => { |
| 164 | + const noDescriptionNodeDef: ComfyNodeDefV2 = { |
| 165 | + ...mockNodeDef, |
| 166 | + description: '' |
| 167 | + } |
| 168 | + |
| 169 | + const wrapper = mountComponent(noDescriptionNodeDef) |
| 170 | + const description = wrapper.find('._sb_description') |
| 171 | + |
| 172 | + expect(description.exists()).toBe(false) |
| 173 | + }) |
| 174 | + |
| 175 | + it('does not render description element when description is undefined', () => { |
| 176 | + const { description, ...nodeDefWithoutDescription } = mockNodeDef |
| 177 | + const wrapper = mountComponent( |
| 178 | + nodeDefWithoutDescription as ComfyNodeDefV2 |
| 179 | + ) |
| 180 | + const descriptionElement = wrapper.find('._sb_description') |
| 181 | + |
| 182 | + expect(descriptionElement.exists()).toBe(false) |
| 183 | + }) |
| 184 | + |
| 185 | + it('calls renderMarkdownToHtml utility function', () => { |
| 186 | + const spy = vi.spyOn(markdownRendererUtil, 'renderMarkdownToHtml') |
| 187 | + const testDescription = 'Test **markdown** description' |
| 188 | + |
| 189 | + const nodeDefWithDescription: ComfyNodeDefV2 = { |
| 190 | + ...mockNodeDef, |
| 191 | + description: testDescription |
| 192 | + } |
| 193 | + |
| 194 | + mountComponent(nodeDefWithDescription) |
| 195 | + |
| 196 | + expect(spy).toHaveBeenCalledWith(testDescription) |
| 197 | + spy.mockRestore() |
| 198 | + }) |
| 199 | + |
| 200 | + it('handles potentially unsafe markdown content safely', () => { |
| 201 | + const unsafeNodeDef: ComfyNodeDefV2 = { |
| 202 | + ...mockNodeDef, |
| 203 | + description: |
| 204 | + 'Safe **markdown** content <script>alert("xss")</script> with `code` blocks' |
| 205 | + } |
| 206 | + |
| 207 | + const wrapper = mountComponent(unsafeNodeDef) |
| 208 | + const description = wrapper.find('._sb_description') |
| 209 | + |
| 210 | + // The description should still exist because there's safe content |
| 211 | + if (description.exists()) { |
| 212 | + // Should not contain script tags (sanitized by DOMPurify) |
| 213 | + expect(description.html()).not.toContain('<script>') |
| 214 | + expect(description.html()).not.toContain('alert("xss")') |
| 215 | + // Should contain the safe markdown content rendered as HTML |
| 216 | + expect(description.html()).toContain('<strong>markdown</strong>') |
| 217 | + expect(description.html()).toContain('<code>code</code>') |
| 218 | + } else { |
| 219 | + // If DOMPurify removes everything, that's also acceptable for security |
| 220 | + expect(description.exists()).toBe(false) |
| 221 | + } |
| 222 | + }) |
| 223 | + |
| 224 | + it('handles markdown with line breaks', () => { |
| 225 | + const multilineNodeDef: ComfyNodeDefV2 = { |
| 226 | + ...mockNodeDef, |
| 227 | + description: 'Line 1\n\nLine 3 after empty line' |
| 228 | + } |
| 229 | + |
| 230 | + const wrapper = mountComponent(multilineNodeDef) |
| 231 | + const description = wrapper.find('._sb_description') |
| 232 | + |
| 233 | + expect(description.exists()).toBe(true) |
| 234 | + // Should contain paragraph tags for proper line break handling |
| 235 | + expect(description.html()).toContain('<p>') |
| 236 | + }) |
| 237 | + |
| 238 | + it('handles markdown lists', () => { |
| 239 | + const listNodeDef: ComfyNodeDefV2 = { |
| 240 | + ...mockNodeDef, |
| 241 | + description: '- Item 1\n- Item 2\n- Item 3' |
| 242 | + } |
| 243 | + |
| 244 | + const wrapper = mountComponent(listNodeDef) |
| 245 | + const description = wrapper.find('._sb_description') |
| 246 | + |
| 247 | + expect(description.exists()).toBe(true) |
| 248 | + expect(description.html()).toContain('<ul>') |
| 249 | + expect(description.html()).toContain('<li>') |
| 250 | + }) |
| 251 | + |
| 252 | + it('applies correct styling classes to description', () => { |
| 253 | + const wrapper = mountComponent() |
| 254 | + const description = wrapper.find('._sb_description') |
| 255 | + |
| 256 | + expect(description.classes()).toContain('_sb_description') |
| 257 | + }) |
| 258 | + |
| 259 | + it('uses v-html directive for rendered content', () => { |
| 260 | + const htmlNodeDef: ComfyNodeDefV2 = { |
| 261 | + ...mockNodeDef, |
| 262 | + description: 'Content with **bold** text' |
| 263 | + } |
| 264 | + |
| 265 | + const wrapper = mountComponent(htmlNodeDef) |
| 266 | + const description = wrapper.find('._sb_description') |
| 267 | + |
| 268 | + // The component should render the HTML, not escape it |
| 269 | + expect(description.html()).toContain('<strong>bold</strong>') |
| 270 | + expect(description.html()).not.toContain('<strong>') |
| 271 | + }) |
| 272 | + |
| 273 | + it('prevents XSS attacks by sanitizing dangerous HTML elements', () => { |
| 274 | + const maliciousNodeDef: ComfyNodeDefV2 = { |
| 275 | + ...mockNodeDef, |
| 276 | + description: |
| 277 | + 'Normal text <img src="x" onerror="alert(\'XSS\')" /> and **bold** text' |
| 278 | + } |
| 279 | + |
| 280 | + const wrapper = mountComponent(maliciousNodeDef) |
| 281 | + const description = wrapper.find('._sb_description') |
| 282 | + |
| 283 | + if (description.exists()) { |
| 284 | + // Should not contain dangerous event handlers |
| 285 | + expect(description.html()).not.toContain('onerror') |
| 286 | + expect(description.html()).not.toContain('alert(') |
| 287 | + // Should still contain safe markdown content |
| 288 | + expect(description.html()).toContain('<strong>bold</strong>') |
| 289 | + // May or may not contain img tag depending on DOMPurify config |
| 290 | + } |
| 291 | + }) |
| 292 | + }) |
132 | 293 | }) |
0 commit comments